blob: ce4066b469559c224648484b277af28596ca6601 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (C) 1993 Linus Torvalds
4 * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
5 * SMP-safe vmalloc/vfree/ioremap, Tigran Aivazian <tigran@veritas.com>, May 2000
6 * Major rework to support vmap/vunmap, Christoph Hellwig, SGI, August 2002
Christoph Lameter930fc452005-10-29 18:15:41 -07007 * Numa awareness, Christoph Lameter, SGI, June 2005
Uladzislau Rezki (Sony)d758ffe2020-08-06 23:24:18 -07008 * Improving global KVA allocator, Uladzislau Rezki, Sony, May 2019
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Nick Piggindb64fe02008-10-18 20:27:03 -070011#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/mm.h>
13#include <linux/module.h>
14#include <linux/highmem.h>
Ingo Molnarc3edc402017-02-02 08:35:14 +010015#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/slab.h>
17#include <linux/spinlock.h>
18#include <linux/interrupt.h>
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +040019#include <linux/proc_fs.h>
Christoph Lametera10aa572008-04-28 02:12:40 -070020#include <linux/seq_file.h>
Rick Edgecombe868b1042019-04-25 17:11:36 -070021#include <linux/set_memory.h>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070022#include <linux/debugobjects.h>
Christoph Lameter23016962008-04-28 02:12:42 -070023#include <linux/kallsyms.h>
Nick Piggindb64fe02008-10-18 20:27:03 -070024#include <linux/list.h>
Chris Wilson4da56b92016-04-04 14:46:42 +010025#include <linux/notifier.h>
Nick Piggindb64fe02008-10-18 20:27:03 -070026#include <linux/rbtree.h>
Matthew Wilcox (Oracle)0f145992020-08-06 23:24:05 -070027#include <linux/xarray.h>
Nick Piggindb64fe02008-10-18 20:27:03 -070028#include <linux/rcupdate.h>
Tejun Heof0aa6612009-02-20 16:29:08 +090029#include <linux/pfn.h>
Catalin Marinas89219d32009-06-11 13:23:19 +010030#include <linux/kmemleak.h>
Arun Sharma600634972011-07-26 16:09:06 -070031#include <linux/atomic.h>
Gideon Israel Dsouza3b321232014-04-07 15:37:26 -070032#include <linux/compiler.h>
Al Viro32fcfd42013-03-10 20:14:08 -040033#include <linux/llist.h>
Toshi Kani0f616be2015-04-14 15:47:17 -070034#include <linux/bitops.h>
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -070035#include <linux/rbtree_augmented.h>
Jann Hornbdebd6a22020-04-20 18:14:11 -070036#include <linux/overflow.h>
Nicholas Pigginc0eb3152021-04-29 22:58:13 -070037#include <linux/pgtable.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080038#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/tlbflush.h>
David Miller2dca6992009-09-21 12:22:34 -070040#include <asm/shmparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Mel Gormandd56b042015-11-06 16:28:43 -080042#include "internal.h"
Joerg Roedel2a681cf2020-08-06 23:22:55 -070043#include "pgalloc-track.h"
Mel Gormandd56b042015-11-06 16:28:43 -080044
Ingo Molnar186525b2019-11-29 08:17:25 +010045bool is_vmalloc_addr(const void *x)
46{
47 unsigned long addr = (unsigned long)x;
48
49 return addr >= VMALLOC_START && addr < VMALLOC_END;
50}
51EXPORT_SYMBOL(is_vmalloc_addr);
52
Al Viro32fcfd42013-03-10 20:14:08 -040053struct vfree_deferred {
54 struct llist_head list;
55 struct work_struct wq;
56};
57static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred);
58
59static void __vunmap(const void *, int);
60
61static void free_work(struct work_struct *w)
62{
63 struct vfree_deferred *p = container_of(w, struct vfree_deferred, wq);
Byungchul Park894e58c2017-09-06 16:24:26 -070064 struct llist_node *t, *llnode;
65
66 llist_for_each_safe(llnode, t, llist_del_all(&p->list))
67 __vunmap((void *)llnode, 1);
Al Viro32fcfd42013-03-10 20:14:08 -040068}
69
Nick Piggindb64fe02008-10-18 20:27:03 -070070/*** Page table manipulation functions ***/
Adrian Bunkb2213852006-09-25 23:31:02 -070071
Joerg Roedel2ba3e692020-06-01 21:52:22 -070072static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
73 pgtbl_mod_mask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 pte_t *pte;
76
77 pte = pte_offset_kernel(pmd, addr);
78 do {
79 pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte);
80 WARN_ON(!pte_none(ptent) && !pte_present(ptent));
81 } while (pte++, addr += PAGE_SIZE, addr != end);
Joerg Roedel2ba3e692020-06-01 21:52:22 -070082 *mask |= PGTBL_PTE_MODIFIED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
Joerg Roedel2ba3e692020-06-01 21:52:22 -070085static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end,
86 pgtbl_mod_mask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
88 pmd_t *pmd;
89 unsigned long next;
Joerg Roedel2ba3e692020-06-01 21:52:22 -070090 int cleared;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 pmd = pmd_offset(pud, addr);
93 do {
94 next = pmd_addr_end(addr, end);
Joerg Roedel2ba3e692020-06-01 21:52:22 -070095
96 cleared = pmd_clear_huge(pmd);
97 if (cleared || pmd_bad(*pmd))
98 *mask |= PGTBL_PMD_MODIFIED;
99
100 if (cleared)
Toshi Kanib9820d82015-04-14 15:47:26 -0700101 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (pmd_none_or_clear_bad(pmd))
103 continue;
Joerg Roedel2ba3e692020-06-01 21:52:22 -0700104 vunmap_pte_range(pmd, addr, next, mask);
Aneesh Kumar K.Ve47110e2020-08-20 17:42:05 -0700105
106 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 } while (pmd++, addr = next, addr != end);
108}
109
Joerg Roedel2ba3e692020-06-01 21:52:22 -0700110static void vunmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end,
111 pgtbl_mod_mask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 pud_t *pud;
114 unsigned long next;
Joerg Roedel2ba3e692020-06-01 21:52:22 -0700115 int cleared;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300117 pud = pud_offset(p4d, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 do {
119 next = pud_addr_end(addr, end);
Joerg Roedel2ba3e692020-06-01 21:52:22 -0700120
121 cleared = pud_clear_huge(pud);
122 if (cleared || pud_bad(*pud))
123 *mask |= PGTBL_PUD_MODIFIED;
124
125 if (cleared)
Toshi Kanib9820d82015-04-14 15:47:26 -0700126 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (pud_none_or_clear_bad(pud))
128 continue;
Joerg Roedel2ba3e692020-06-01 21:52:22 -0700129 vunmap_pmd_range(pud, addr, next, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 } while (pud++, addr = next, addr != end);
131}
132
Joerg Roedel2ba3e692020-06-01 21:52:22 -0700133static void vunmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,
134 pgtbl_mod_mask *mask)
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300135{
136 p4d_t *p4d;
137 unsigned long next;
Joerg Roedel2ba3e692020-06-01 21:52:22 -0700138 int cleared;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300139
140 p4d = p4d_offset(pgd, addr);
141 do {
142 next = p4d_addr_end(addr, end);
Joerg Roedel2ba3e692020-06-01 21:52:22 -0700143
144 cleared = p4d_clear_huge(p4d);
145 if (cleared || p4d_bad(*p4d))
146 *mask |= PGTBL_P4D_MODIFIED;
147
148 if (cleared)
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300149 continue;
150 if (p4d_none_or_clear_bad(p4d))
151 continue;
Joerg Roedel2ba3e692020-06-01 21:52:22 -0700152 vunmap_pud_range(p4d, addr, next, mask);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300153 } while (p4d++, addr = next, addr != end);
154}
155
Christoph Hellwigb521c432020-06-01 21:51:07 -0700156/**
157 * unmap_kernel_range_noflush - unmap kernel VM area
Joerg Roedel2ba3e692020-06-01 21:52:22 -0700158 * @start: start of the VM area to unmap
Christoph Hellwigb521c432020-06-01 21:51:07 -0700159 * @size: size of the VM area to unmap
160 *
161 * Unmap PFN_UP(@size) pages at @addr. The VM area @addr and @size specify
162 * should have been allocated using get_vm_area() and its friends.
163 *
164 * NOTE:
165 * This function does NOT do any cache flushing. The caller is responsible
166 * for calling flush_cache_vunmap() on to-be-mapped areas before calling this
167 * function and flush_tlb_kernel_range() after.
168 */
Joerg Roedel2ba3e692020-06-01 21:52:22 -0700169void unmap_kernel_range_noflush(unsigned long start, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Joerg Roedel2ba3e692020-06-01 21:52:22 -0700171 unsigned long end = start + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 unsigned long next;
Christoph Hellwigb521c432020-06-01 21:51:07 -0700173 pgd_t *pgd;
Joerg Roedel2ba3e692020-06-01 21:52:22 -0700174 unsigned long addr = start;
175 pgtbl_mod_mask mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 BUG_ON(addr >= end);
178 pgd = pgd_offset_k(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 do {
180 next = pgd_addr_end(addr, end);
Joerg Roedel2ba3e692020-06-01 21:52:22 -0700181 if (pgd_bad(*pgd))
182 mask |= PGTBL_PGD_MODIFIED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (pgd_none_or_clear_bad(pgd))
184 continue;
Joerg Roedel2ba3e692020-06-01 21:52:22 -0700185 vunmap_p4d_range(pgd, addr, next, &mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 } while (pgd++, addr = next, addr != end);
Joerg Roedel2ba3e692020-06-01 21:52:22 -0700187
188 if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
189 arch_sync_kernel_mappings(start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Nicholas Piggin0a264882021-04-29 22:58:19 -0700192static int vmap_pages_pte_range(pmd_t *pmd, unsigned long addr,
Joerg Roedel2ba3e692020-06-01 21:52:22 -0700193 unsigned long end, pgprot_t prot, struct page **pages, int *nr,
194 pgtbl_mod_mask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 pte_t *pte;
197
Nick Piggindb64fe02008-10-18 20:27:03 -0700198 /*
199 * nr is a running index into the array which helps higher level
200 * callers keep track of where we're up to.
201 */
202
Joerg Roedel2ba3e692020-06-01 21:52:22 -0700203 pte = pte_alloc_kernel_track(pmd, addr, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 if (!pte)
205 return -ENOMEM;
206 do {
Nick Piggindb64fe02008-10-18 20:27:03 -0700207 struct page *page = pages[*nr];
208
209 if (WARN_ON(!pte_none(*pte)))
210 return -EBUSY;
211 if (WARN_ON(!page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return -ENOMEM;
213 set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
Nick Piggindb64fe02008-10-18 20:27:03 -0700214 (*nr)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 } while (pte++, addr += PAGE_SIZE, addr != end);
Joerg Roedel2ba3e692020-06-01 21:52:22 -0700216 *mask |= PGTBL_PTE_MODIFIED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return 0;
218}
219
Nicholas Piggin0a264882021-04-29 22:58:19 -0700220static int vmap_pages_pmd_range(pud_t *pud, unsigned long addr,
Joerg Roedel2ba3e692020-06-01 21:52:22 -0700221 unsigned long end, pgprot_t prot, struct page **pages, int *nr,
222 pgtbl_mod_mask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 pmd_t *pmd;
225 unsigned long next;
226
Joerg Roedel2ba3e692020-06-01 21:52:22 -0700227 pmd = pmd_alloc_track(&init_mm, pud, addr, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (!pmd)
229 return -ENOMEM;
230 do {
231 next = pmd_addr_end(addr, end);
Nicholas Piggin0a264882021-04-29 22:58:19 -0700232 if (vmap_pages_pte_range(pmd, addr, next, prot, pages, nr, mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return -ENOMEM;
234 } while (pmd++, addr = next, addr != end);
235 return 0;
236}
237
Nicholas Piggin0a264882021-04-29 22:58:19 -0700238static int vmap_pages_pud_range(p4d_t *p4d, unsigned long addr,
Joerg Roedel2ba3e692020-06-01 21:52:22 -0700239 unsigned long end, pgprot_t prot, struct page **pages, int *nr,
240 pgtbl_mod_mask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 pud_t *pud;
243 unsigned long next;
244
Joerg Roedel2ba3e692020-06-01 21:52:22 -0700245 pud = pud_alloc_track(&init_mm, p4d, addr, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (!pud)
247 return -ENOMEM;
248 do {
249 next = pud_addr_end(addr, end);
Nicholas Piggin0a264882021-04-29 22:58:19 -0700250 if (vmap_pages_pmd_range(pud, addr, next, prot, pages, nr, mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return -ENOMEM;
252 } while (pud++, addr = next, addr != end);
253 return 0;
254}
255
Nicholas Piggin0a264882021-04-29 22:58:19 -0700256static int vmap_pages_p4d_range(pgd_t *pgd, unsigned long addr,
Joerg Roedel2ba3e692020-06-01 21:52:22 -0700257 unsigned long end, pgprot_t prot, struct page **pages, int *nr,
258 pgtbl_mod_mask *mask)
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300259{
260 p4d_t *p4d;
261 unsigned long next;
262
Joerg Roedel2ba3e692020-06-01 21:52:22 -0700263 p4d = p4d_alloc_track(&init_mm, pgd, addr, mask);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300264 if (!p4d)
265 return -ENOMEM;
266 do {
267 next = p4d_addr_end(addr, end);
Nicholas Piggin0a264882021-04-29 22:58:19 -0700268 if (vmap_pages_pud_range(p4d, addr, next, prot, pages, nr, mask))
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300269 return -ENOMEM;
270 } while (p4d++, addr = next, addr != end);
271 return 0;
272}
273
Christoph Hellwigb521c432020-06-01 21:51:07 -0700274/**
275 * map_kernel_range_noflush - map kernel VM area with the specified pages
276 * @addr: start of the VM area to map
277 * @size: size of the VM area to map
278 * @prot: page protection flags to use
279 * @pages: pages to map
Nick Piggindb64fe02008-10-18 20:27:03 -0700280 *
Christoph Hellwigb521c432020-06-01 21:51:07 -0700281 * Map PFN_UP(@size) pages at @addr. The VM area @addr and @size specify should
282 * have been allocated using get_vm_area() and its friends.
283 *
284 * NOTE:
285 * This function does NOT do any cache flushing. The caller is responsible for
286 * calling flush_cache_vmap() on to-be-mapped areas before calling this
287 * function.
288 *
289 * RETURNS:
Christoph Hellwig60bb4462020-06-01 21:51:15 -0700290 * 0 on success, -errno on failure.
Nick Piggindb64fe02008-10-18 20:27:03 -0700291 */
Christoph Hellwigb521c432020-06-01 21:51:07 -0700292int map_kernel_range_noflush(unsigned long addr, unsigned long size,
293 pgprot_t prot, struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Joerg Roedel2ba3e692020-06-01 21:52:22 -0700295 unsigned long start = addr;
Christoph Hellwigb521c432020-06-01 21:51:07 -0700296 unsigned long end = addr + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 unsigned long next;
Christoph Hellwigb521c432020-06-01 21:51:07 -0700298 pgd_t *pgd;
Nick Piggindb64fe02008-10-18 20:27:03 -0700299 int err = 0;
300 int nr = 0;
Joerg Roedel2ba3e692020-06-01 21:52:22 -0700301 pgtbl_mod_mask mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 BUG_ON(addr >= end);
304 pgd = pgd_offset_k(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 do {
306 next = pgd_addr_end(addr, end);
Joerg Roedel2ba3e692020-06-01 21:52:22 -0700307 if (pgd_bad(*pgd))
308 mask |= PGTBL_PGD_MODIFIED;
Nicholas Piggin0a264882021-04-29 22:58:19 -0700309 err = vmap_pages_p4d_range(pgd, addr, next, prot, pages, &nr, &mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (err)
Figo.zhangbf88c8c2009-09-21 17:01:47 -0700311 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 } while (pgd++, addr = next, addr != end);
Nick Piggindb64fe02008-10-18 20:27:03 -0700313
Joerg Roedel2ba3e692020-06-01 21:52:22 -0700314 if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
315 arch_sync_kernel_mappings(start, end);
316
Christoph Hellwig60bb4462020-06-01 21:51:15 -0700317 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
Christoph Hellwiged1f3242020-06-01 21:51:19 -0700320int map_kernel_range(unsigned long start, unsigned long size, pgprot_t prot,
321 struct page **pages)
Tejun Heo8fc48982009-02-20 16:29:08 +0900322{
323 int ret;
324
Christoph Hellwiga29adb62020-06-01 21:51:11 -0700325 ret = map_kernel_range_noflush(start, size, prot, pages);
326 flush_cache_vmap(start, start + size);
Tejun Heo8fc48982009-02-20 16:29:08 +0900327 return ret;
328}
329
KAMEZAWA Hiroyuki81ac3ad2009-09-22 16:45:49 -0700330int is_vmalloc_or_module_addr(const void *x)
Linus Torvalds73bdf0a2008-10-15 08:35:12 -0700331{
332 /*
Russell Kingab4f2ee2008-11-06 17:11:07 +0000333 * ARM, x86-64 and sparc64 put modules in a special place,
Linus Torvalds73bdf0a2008-10-15 08:35:12 -0700334 * and fall back on vmalloc() if that fails. Others
335 * just put it in the vmalloc space.
336 */
337#if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
338 unsigned long addr = (unsigned long)x;
339 if (addr >= MODULES_VADDR && addr < MODULES_END)
340 return 1;
341#endif
342 return is_vmalloc_addr(x);
343}
344
Christoph Lameter48667e72008-02-04 22:28:31 -0800345/*
Nicholas Pigginc0eb3152021-04-29 22:58:13 -0700346 * Walk a vmap address to the struct page it maps. Huge vmap mappings will
347 * return the tail page that corresponds to the base page address, which
348 * matches small vmap mappings.
Christoph Lameter48667e72008-02-04 22:28:31 -0800349 */
malcadd688f2014-01-27 17:06:53 -0800350struct page *vmalloc_to_page(const void *vmalloc_addr)
Christoph Lameter48667e72008-02-04 22:28:31 -0800351{
352 unsigned long addr = (unsigned long) vmalloc_addr;
malcadd688f2014-01-27 17:06:53 -0800353 struct page *page = NULL;
Christoph Lameter48667e72008-02-04 22:28:31 -0800354 pgd_t *pgd = pgd_offset_k(addr);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300355 p4d_t *p4d;
356 pud_t *pud;
357 pmd_t *pmd;
358 pte_t *ptep, pte;
Christoph Lameter48667e72008-02-04 22:28:31 -0800359
Ingo Molnar7aa413d2008-06-19 13:28:11 +0200360 /*
361 * XXX we might need to change this if we add VIRTUAL_BUG_ON for
362 * architectures that do not vmalloc module space
363 */
Linus Torvalds73bdf0a2008-10-15 08:35:12 -0700364 VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr));
Jiri Slaby59ea7462008-06-12 13:56:40 +0200365
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300366 if (pgd_none(*pgd))
367 return NULL;
Nicholas Pigginc0eb3152021-04-29 22:58:13 -0700368 if (WARN_ON_ONCE(pgd_leaf(*pgd)))
369 return NULL; /* XXX: no allowance for huge pgd */
370 if (WARN_ON_ONCE(pgd_bad(*pgd)))
371 return NULL;
372
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300373 p4d = p4d_offset(pgd, addr);
374 if (p4d_none(*p4d))
375 return NULL;
Nicholas Pigginc0eb3152021-04-29 22:58:13 -0700376 if (p4d_leaf(*p4d))
377 return p4d_page(*p4d) + ((addr & ~P4D_MASK) >> PAGE_SHIFT);
378 if (WARN_ON_ONCE(p4d_bad(*p4d)))
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300379 return NULL;
Nicholas Pigginc0eb3152021-04-29 22:58:13 -0700380
381 pud = pud_offset(p4d, addr);
382 if (pud_none(*pud))
383 return NULL;
384 if (pud_leaf(*pud))
385 return pud_page(*pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
386 if (WARN_ON_ONCE(pud_bad(*pud)))
387 return NULL;
388
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300389 pmd = pmd_offset(pud, addr);
Nicholas Pigginc0eb3152021-04-29 22:58:13 -0700390 if (pmd_none(*pmd))
391 return NULL;
392 if (pmd_leaf(*pmd))
393 return pmd_page(*pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
394 if (WARN_ON_ONCE(pmd_bad(*pmd)))
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300395 return NULL;
Nick Piggindb64fe02008-10-18 20:27:03 -0700396
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300397 ptep = pte_offset_map(pmd, addr);
398 pte = *ptep;
399 if (pte_present(pte))
400 page = pte_page(pte);
401 pte_unmap(ptep);
Nicholas Pigginc0eb3152021-04-29 22:58:13 -0700402
malcadd688f2014-01-27 17:06:53 -0800403 return page;
Jianyu Zhanece86e222014-01-21 15:49:12 -0800404}
405EXPORT_SYMBOL(vmalloc_to_page);
406
malcadd688f2014-01-27 17:06:53 -0800407/*
408 * Map a vmalloc()-space virtual address to the physical page frame number.
409 */
410unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
411{
412 return page_to_pfn(vmalloc_to_page(vmalloc_addr));
413}
414EXPORT_SYMBOL(vmalloc_to_pfn);
415
Nick Piggindb64fe02008-10-18 20:27:03 -0700416
417/*** Global kva allocator ***/
418
Uladzislau Rezki (Sony)bb850f42019-05-17 14:31:34 -0700419#define DEBUG_AUGMENT_PROPAGATE_CHECK 0
Uladzislau Rezki (Sony)a6cf4e02019-05-17 14:31:37 -0700420#define DEBUG_AUGMENT_LOWEST_MATCH_CHECK 0
Uladzislau Rezki (Sony)bb850f42019-05-17 14:31:34 -0700421
Nick Piggindb64fe02008-10-18 20:27:03 -0700422
Nick Piggindb64fe02008-10-18 20:27:03 -0700423static DEFINE_SPINLOCK(vmap_area_lock);
Uladzislau Rezki (Sony)e36176b2019-11-30 17:54:47 -0800424static DEFINE_SPINLOCK(free_vmap_area_lock);
Joonsoo Kimf1c40692013-04-29 15:07:37 -0700425/* Export for kexec only */
426LIST_HEAD(vmap_area_list);
Nick Piggin89699602011-03-22 16:30:36 -0700427static struct rb_root vmap_area_root = RB_ROOT;
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700428static bool vmap_initialized __read_mostly;
Nick Piggin89699602011-03-22 16:30:36 -0700429
Uladzislau Rezki (Sony)96e2db42020-12-14 19:08:49 -0800430static struct rb_root purge_vmap_area_root = RB_ROOT;
431static LIST_HEAD(purge_vmap_area_list);
432static DEFINE_SPINLOCK(purge_vmap_area_lock);
433
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700434/*
435 * This kmem_cache is used for vmap_area objects. Instead of
436 * allocating from slab we reuse an object from this cache to
437 * make things faster. Especially in "no edge" splitting of
438 * free block.
439 */
440static struct kmem_cache *vmap_area_cachep;
Nick Piggin89699602011-03-22 16:30:36 -0700441
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700442/*
443 * This linked list is used in pair with free_vmap_area_root.
444 * It gives O(1) access to prev/next to perform fast coalescing.
445 */
446static LIST_HEAD(free_vmap_area_list);
447
448/*
449 * This augment red-black tree represents the free vmap space.
450 * All vmap_area objects in this tree are sorted by va->va_start
451 * address. It is used for allocation and merging when a vmap
452 * object is released.
453 *
454 * Each vmap_area node contains a maximum available free block
455 * of its sub-tree, right or left. Therefore it is possible to
456 * find a lowest match of free area.
457 */
458static struct rb_root free_vmap_area_root = RB_ROOT;
459
Uladzislau Rezki (Sony)82dd23e2019-07-11 20:58:57 -0700460/*
461 * Preload a CPU with one object for "no edge" split case. The
462 * aim is to get rid of allocations from the atomic context, thus
463 * to use more permissive allocation masks.
464 */
465static DEFINE_PER_CPU(struct vmap_area *, ne_fit_preload_node);
466
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700467static __always_inline unsigned long
468va_size(struct vmap_area *va)
469{
470 return (va->va_end - va->va_start);
471}
472
473static __always_inline unsigned long
474get_subtree_max_size(struct rb_node *node)
475{
476 struct vmap_area *va;
477
478 va = rb_entry_safe(node, struct vmap_area, rb_node);
479 return va ? va->subtree_max_size : 0;
480}
481
482/*
483 * Gets called when remove the node and rotate.
484 */
485static __always_inline unsigned long
486compute_subtree_max_size(struct vmap_area *va)
487{
488 return max3(va_size(va),
489 get_subtree_max_size(va->rb_node.rb_left),
490 get_subtree_max_size(va->rb_node.rb_right));
491}
492
Michel Lespinasse315cc062019-09-25 16:46:07 -0700493RB_DECLARE_CALLBACKS_MAX(static, free_vmap_area_rb_augment_cb,
494 struct vmap_area, rb_node, unsigned long, subtree_max_size, va_size)
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700495
496static void purge_vmap_area_lazy(void);
497static BLOCKING_NOTIFIER_HEAD(vmap_notify_list);
498static unsigned long lazy_max_pages(void);
Nick Piggindb64fe02008-10-18 20:27:03 -0700499
Roman Gushchin97105f02019-07-11 21:00:13 -0700500static atomic_long_t nr_vmalloc_pages;
501
502unsigned long vmalloc_nr_pages(void)
503{
504 return atomic_long_read(&nr_vmalloc_pages);
505}
506
Nick Piggindb64fe02008-10-18 20:27:03 -0700507static struct vmap_area *__find_vmap_area(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Nick Piggindb64fe02008-10-18 20:27:03 -0700509 struct rb_node *n = vmap_area_root.rb_node;
510
511 while (n) {
512 struct vmap_area *va;
513
514 va = rb_entry(n, struct vmap_area, rb_node);
515 if (addr < va->va_start)
516 n = n->rb_left;
HATAYAMA Daisukecef2ac32013-07-03 15:02:17 -0700517 else if (addr >= va->va_end)
Nick Piggindb64fe02008-10-18 20:27:03 -0700518 n = n->rb_right;
519 else
520 return va;
521 }
522
523 return NULL;
524}
525
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700526/*
527 * This function returns back addresses of parent node
528 * and its left or right link for further processing.
Uladzislau Rezki (Sony)9c801f62020-08-06 23:24:24 -0700529 *
530 * Otherwise NULL is returned. In that case all further
531 * steps regarding inserting of conflicting overlap range
532 * have to be declined and actually considered as a bug.
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700533 */
534static __always_inline struct rb_node **
535find_va_links(struct vmap_area *va,
536 struct rb_root *root, struct rb_node *from,
537 struct rb_node **parent)
Nick Piggindb64fe02008-10-18 20:27:03 -0700538{
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700539 struct vmap_area *tmp_va;
540 struct rb_node **link;
Nick Piggindb64fe02008-10-18 20:27:03 -0700541
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700542 if (root) {
543 link = &root->rb_node;
544 if (unlikely(!*link)) {
545 *parent = NULL;
546 return link;
547 }
548 } else {
549 link = &from;
Nick Piggindb64fe02008-10-18 20:27:03 -0700550 }
551
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700552 /*
553 * Go to the bottom of the tree. When we hit the last point
554 * we end up with parent rb_node and correct direction, i name
555 * it link, where the new va->rb_node will be attached to.
556 */
557 do {
558 tmp_va = rb_entry(*link, struct vmap_area, rb_node);
Nick Piggindb64fe02008-10-18 20:27:03 -0700559
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700560 /*
561 * During the traversal we also do some sanity check.
562 * Trigger the BUG() if there are sides(left/right)
563 * or full overlaps.
564 */
565 if (va->va_start < tmp_va->va_end &&
566 va->va_end <= tmp_va->va_start)
567 link = &(*link)->rb_left;
568 else if (va->va_end > tmp_va->va_start &&
569 va->va_start >= tmp_va->va_end)
570 link = &(*link)->rb_right;
Uladzislau Rezki (Sony)9c801f62020-08-06 23:24:24 -0700571 else {
572 WARN(1, "vmalloc bug: 0x%lx-0x%lx overlaps with 0x%lx-0x%lx\n",
573 va->va_start, va->va_end, tmp_va->va_start, tmp_va->va_end);
574
575 return NULL;
576 }
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700577 } while (*link);
578
579 *parent = &tmp_va->rb_node;
580 return link;
Nick Piggindb64fe02008-10-18 20:27:03 -0700581}
582
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700583static __always_inline struct list_head *
584get_va_next_sibling(struct rb_node *parent, struct rb_node **link)
585{
586 struct list_head *list;
Nick Piggindb64fe02008-10-18 20:27:03 -0700587
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700588 if (unlikely(!parent))
589 /*
590 * The red-black tree where we try to find VA neighbors
591 * before merging or inserting is empty, i.e. it means
592 * there is no free vmap space. Normally it does not
593 * happen but we handle this case anyway.
594 */
595 return NULL;
596
597 list = &rb_entry(parent, struct vmap_area, rb_node)->list;
598 return (&parent->rb_right == link ? list->next : list);
599}
600
601static __always_inline void
602link_va(struct vmap_area *va, struct rb_root *root,
603 struct rb_node *parent, struct rb_node **link, struct list_head *head)
604{
605 /*
606 * VA is still not in the list, but we can
607 * identify its future previous list_head node.
608 */
609 if (likely(parent)) {
610 head = &rb_entry(parent, struct vmap_area, rb_node)->list;
611 if (&parent->rb_right != link)
612 head = head->prev;
613 }
614
615 /* Insert to the rb-tree */
616 rb_link_node(&va->rb_node, parent, link);
617 if (root == &free_vmap_area_root) {
618 /*
619 * Some explanation here. Just perform simple insertion
620 * to the tree. We do not set va->subtree_max_size to
621 * its current size before calling rb_insert_augmented().
622 * It is because of we populate the tree from the bottom
623 * to parent levels when the node _is_ in the tree.
624 *
625 * Therefore we set subtree_max_size to zero after insertion,
626 * to let __augment_tree_propagate_from() puts everything to
627 * the correct order later on.
628 */
629 rb_insert_augmented(&va->rb_node,
630 root, &free_vmap_area_rb_augment_cb);
631 va->subtree_max_size = 0;
632 } else {
633 rb_insert_color(&va->rb_node, root);
634 }
635
636 /* Address-sort this list */
637 list_add(&va->list, head);
638}
639
640static __always_inline void
641unlink_va(struct vmap_area *va, struct rb_root *root)
642{
Uladzislau Rezki (Sony)460e42d2019-07-11 20:59:03 -0700643 if (WARN_ON(RB_EMPTY_NODE(&va->rb_node)))
644 return;
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700645
Uladzislau Rezki (Sony)460e42d2019-07-11 20:59:03 -0700646 if (root == &free_vmap_area_root)
647 rb_erase_augmented(&va->rb_node,
648 root, &free_vmap_area_rb_augment_cb);
649 else
650 rb_erase(&va->rb_node, root);
651
652 list_del(&va->list);
653 RB_CLEAR_NODE(&va->rb_node);
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700654}
655
Uladzislau Rezki (Sony)bb850f42019-05-17 14:31:34 -0700656#if DEBUG_AUGMENT_PROPAGATE_CHECK
657static void
Uladzislau Rezki (Sony)da27c9e2020-08-06 23:24:12 -0700658augment_tree_propagate_check(void)
Uladzislau Rezki (Sony)bb850f42019-05-17 14:31:34 -0700659{
660 struct vmap_area *va;
Uladzislau Rezki (Sony)da27c9e2020-08-06 23:24:12 -0700661 unsigned long computed_size;
Uladzislau Rezki (Sony)bb850f42019-05-17 14:31:34 -0700662
Uladzislau Rezki (Sony)da27c9e2020-08-06 23:24:12 -0700663 list_for_each_entry(va, &free_vmap_area_list, list) {
664 computed_size = compute_subtree_max_size(va);
665 if (computed_size != va->subtree_max_size)
666 pr_emerg("tree is corrupted: %lu, %lu\n",
667 va_size(va), va->subtree_max_size);
Uladzislau Rezki (Sony)bb850f42019-05-17 14:31:34 -0700668 }
Uladzislau Rezki (Sony)bb850f42019-05-17 14:31:34 -0700669}
670#endif
671
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700672/*
673 * This function populates subtree_max_size from bottom to upper
674 * levels starting from VA point. The propagation must be done
675 * when VA size is modified by changing its va_start/va_end. Or
676 * in case of newly inserting of VA to the tree.
677 *
678 * It means that __augment_tree_propagate_from() must be called:
679 * - After VA has been inserted to the tree(free path);
680 * - After VA has been shrunk(allocation path);
681 * - After VA has been increased(merging path).
682 *
683 * Please note that, it does not mean that upper parent nodes
684 * and their subtree_max_size are recalculated all the time up
685 * to the root node.
686 *
687 * 4--8
688 * /\
689 * / \
690 * / \
691 * 2--2 8--8
692 *
693 * For example if we modify the node 4, shrinking it to 2, then
694 * no any modification is required. If we shrink the node 2 to 1
695 * its subtree_max_size is updated only, and set to 1. If we shrink
696 * the node 8 to 6, then its subtree_max_size is set to 6 and parent
697 * node becomes 4--6.
698 */
699static __always_inline void
700augment_tree_propagate_from(struct vmap_area *va)
701{
Uladzislau Rezki (Sony)15ae1442020-08-06 23:24:15 -0700702 /*
703 * Populate the tree from bottom towards the root until
704 * the calculated maximum available size of checked node
705 * is equal to its current one.
706 */
707 free_vmap_area_rb_augment_cb_propagate(&va->rb_node, NULL);
Uladzislau Rezki (Sony)bb850f42019-05-17 14:31:34 -0700708
709#if DEBUG_AUGMENT_PROPAGATE_CHECK
Uladzislau Rezki (Sony)da27c9e2020-08-06 23:24:12 -0700710 augment_tree_propagate_check();
Uladzislau Rezki (Sony)bb850f42019-05-17 14:31:34 -0700711#endif
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700712}
713
714static void
715insert_vmap_area(struct vmap_area *va,
716 struct rb_root *root, struct list_head *head)
717{
718 struct rb_node **link;
719 struct rb_node *parent;
720
721 link = find_va_links(va, root, NULL, &parent);
Uladzislau Rezki (Sony)9c801f62020-08-06 23:24:24 -0700722 if (link)
723 link_va(va, root, parent, link, head);
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700724}
725
726static void
727insert_vmap_area_augment(struct vmap_area *va,
728 struct rb_node *from, struct rb_root *root,
729 struct list_head *head)
730{
731 struct rb_node **link;
732 struct rb_node *parent;
733
734 if (from)
735 link = find_va_links(va, NULL, from, &parent);
736 else
737 link = find_va_links(va, root, NULL, &parent);
738
Uladzislau Rezki (Sony)9c801f62020-08-06 23:24:24 -0700739 if (link) {
740 link_va(va, root, parent, link, head);
741 augment_tree_propagate_from(va);
742 }
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700743}
744
745/*
746 * Merge de-allocated chunk of VA memory with previous
747 * and next free blocks. If coalesce is not done a new
748 * free area is inserted. If VA has been merged, it is
749 * freed.
Uladzislau Rezki (Sony)9c801f62020-08-06 23:24:24 -0700750 *
751 * Please note, it can return NULL in case of overlap
752 * ranges, followed by WARN() report. Despite it is a
753 * buggy behaviour, a system can be alive and keep
754 * ongoing.
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700755 */
Daniel Axtens3c5c3cf2019-11-30 17:54:50 -0800756static __always_inline struct vmap_area *
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700757merge_or_add_vmap_area(struct vmap_area *va,
758 struct rb_root *root, struct list_head *head)
759{
760 struct vmap_area *sibling;
761 struct list_head *next;
762 struct rb_node **link;
763 struct rb_node *parent;
764 bool merged = false;
765
766 /*
767 * Find a place in the tree where VA potentially will be
768 * inserted, unless it is merged with its sibling/siblings.
769 */
770 link = find_va_links(va, root, NULL, &parent);
Uladzislau Rezki (Sony)9c801f62020-08-06 23:24:24 -0700771 if (!link)
772 return NULL;
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700773
774 /*
775 * Get next node of VA to check if merging can be done.
776 */
777 next = get_va_next_sibling(parent, link);
778 if (unlikely(next == NULL))
779 goto insert;
780
781 /*
782 * start end
783 * | |
784 * |<------VA------>|<-----Next----->|
785 * | |
786 * start end
787 */
788 if (next != head) {
789 sibling = list_entry(next, struct vmap_area, list);
790 if (sibling->va_start == va->va_end) {
791 sibling->va_start = va->va_start;
792
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700793 /* Free vmap_area object. */
794 kmem_cache_free(vmap_area_cachep, va);
795
796 /* Point to the new merged area. */
797 va = sibling;
798 merged = true;
799 }
800 }
801
802 /*
803 * start end
804 * | |
805 * |<-----Prev----->|<------VA------>|
806 * | |
807 * start end
808 */
809 if (next->prev != head) {
810 sibling = list_entry(next->prev, struct vmap_area, list);
811 if (sibling->va_end == va->va_start) {
Uladzislau Rezki (Sony)5dd78642020-08-06 23:24:09 -0700812 /*
813 * If both neighbors are coalesced, it is important
814 * to unlink the "next" node first, followed by merging
815 * with "previous" one. Otherwise the tree might not be
816 * fully populated if a sibling's augmented value is
817 * "normalized" because of rotation operations.
818 */
Uladzislau Rezki (Sony)54f63d92019-07-11 20:59:00 -0700819 if (merged)
820 unlink_va(va, root);
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700821
Uladzislau Rezki (Sony)5dd78642020-08-06 23:24:09 -0700822 sibling->va_end = va->va_end;
823
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700824 /* Free vmap_area object. */
825 kmem_cache_free(vmap_area_cachep, va);
Daniel Axtens3c5c3cf2019-11-30 17:54:50 -0800826
827 /* Point to the new merged area. */
828 va = sibling;
829 merged = true;
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700830 }
831 }
832
833insert:
Uladzislau Rezki (Sony)5dd78642020-08-06 23:24:09 -0700834 if (!merged)
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700835 link_va(va, root, parent, link, head);
Daniel Axtens3c5c3cf2019-11-30 17:54:50 -0800836
Uladzislau Rezki (Sony)96e2db42020-12-14 19:08:49 -0800837 return va;
838}
839
840static __always_inline struct vmap_area *
841merge_or_add_vmap_area_augment(struct vmap_area *va,
842 struct rb_root *root, struct list_head *head)
843{
844 va = merge_or_add_vmap_area(va, root, head);
845 if (va)
846 augment_tree_propagate_from(va);
847
Daniel Axtens3c5c3cf2019-11-30 17:54:50 -0800848 return va;
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700849}
850
851static __always_inline bool
852is_within_this_va(struct vmap_area *va, unsigned long size,
853 unsigned long align, unsigned long vstart)
854{
855 unsigned long nva_start_addr;
856
857 if (va->va_start > vstart)
858 nva_start_addr = ALIGN(va->va_start, align);
859 else
860 nva_start_addr = ALIGN(vstart, align);
861
862 /* Can be overflowed due to big size or alignment. */
863 if (nva_start_addr + size < nva_start_addr ||
864 nva_start_addr < vstart)
865 return false;
866
867 return (nva_start_addr + size <= va->va_end);
868}
869
870/*
871 * Find the first free block(lowest start address) in the tree,
872 * that will accomplish the request corresponding to passing
873 * parameters.
874 */
875static __always_inline struct vmap_area *
876find_vmap_lowest_match(unsigned long size,
877 unsigned long align, unsigned long vstart)
878{
879 struct vmap_area *va;
880 struct rb_node *node;
881 unsigned long length;
882
883 /* Start from the root. */
884 node = free_vmap_area_root.rb_node;
885
886 /* Adjust the search size for alignment overhead. */
887 length = size + align - 1;
888
889 while (node) {
890 va = rb_entry(node, struct vmap_area, rb_node);
891
892 if (get_subtree_max_size(node->rb_left) >= length &&
893 vstart < va->va_start) {
894 node = node->rb_left;
895 } else {
896 if (is_within_this_va(va, size, align, vstart))
897 return va;
898
899 /*
900 * Does not make sense to go deeper towards the right
901 * sub-tree if it does not have a free block that is
902 * equal or bigger to the requested search length.
903 */
904 if (get_subtree_max_size(node->rb_right) >= length) {
905 node = node->rb_right;
906 continue;
907 }
908
909 /*
Andrew Morton3806b042019-05-31 22:30:03 -0700910 * OK. We roll back and find the first right sub-tree,
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700911 * that will satisfy the search criteria. It can happen
912 * only once due to "vstart" restriction.
913 */
914 while ((node = rb_parent(node))) {
915 va = rb_entry(node, struct vmap_area, rb_node);
916 if (is_within_this_va(va, size, align, vstart))
917 return va;
918
919 if (get_subtree_max_size(node->rb_right) >= length &&
920 vstart <= va->va_start) {
921 node = node->rb_right;
922 break;
923 }
924 }
925 }
926 }
927
928 return NULL;
929}
930
Uladzislau Rezki (Sony)a6cf4e02019-05-17 14:31:37 -0700931#if DEBUG_AUGMENT_LOWEST_MATCH_CHECK
932#include <linux/random.h>
933
934static struct vmap_area *
935find_vmap_lowest_linear_match(unsigned long size,
936 unsigned long align, unsigned long vstart)
937{
938 struct vmap_area *va;
939
940 list_for_each_entry(va, &free_vmap_area_list, list) {
941 if (!is_within_this_va(va, size, align, vstart))
942 continue;
943
944 return va;
945 }
946
947 return NULL;
948}
949
950static void
951find_vmap_lowest_match_check(unsigned long size)
952{
953 struct vmap_area *va_1, *va_2;
954 unsigned long vstart;
955 unsigned int rnd;
956
957 get_random_bytes(&rnd, sizeof(rnd));
958 vstart = VMALLOC_START + rnd;
959
960 va_1 = find_vmap_lowest_match(size, 1, vstart);
961 va_2 = find_vmap_lowest_linear_match(size, 1, vstart);
962
963 if (va_1 != va_2)
964 pr_emerg("not lowest: t: 0x%p, l: 0x%p, v: 0x%lx\n",
965 va_1, va_2, vstart);
966}
967#endif
968
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700969enum fit_type {
970 NOTHING_FIT = 0,
971 FL_FIT_TYPE = 1, /* full fit */
972 LE_FIT_TYPE = 2, /* left edge fit */
973 RE_FIT_TYPE = 3, /* right edge fit */
974 NE_FIT_TYPE = 4 /* no edge fit */
975};
976
977static __always_inline enum fit_type
978classify_va_fit_type(struct vmap_area *va,
979 unsigned long nva_start_addr, unsigned long size)
980{
981 enum fit_type type;
982
983 /* Check if it is within VA. */
984 if (nva_start_addr < va->va_start ||
985 nva_start_addr + size > va->va_end)
986 return NOTHING_FIT;
987
988 /* Now classify. */
989 if (va->va_start == nva_start_addr) {
990 if (va->va_end == nva_start_addr + size)
991 type = FL_FIT_TYPE;
992 else
993 type = LE_FIT_TYPE;
994 } else if (va->va_end == nva_start_addr + size) {
995 type = RE_FIT_TYPE;
996 } else {
997 type = NE_FIT_TYPE;
998 }
999
1000 return type;
1001}
1002
1003static __always_inline int
1004adjust_va_to_fit_type(struct vmap_area *va,
1005 unsigned long nva_start_addr, unsigned long size,
1006 enum fit_type type)
1007{
Arnd Bergmann2c929232019-06-28 12:07:09 -07001008 struct vmap_area *lva = NULL;
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001009
1010 if (type == FL_FIT_TYPE) {
1011 /*
1012 * No need to split VA, it fully fits.
1013 *
1014 * | |
1015 * V NVA V
1016 * |---------------|
1017 */
1018 unlink_va(va, &free_vmap_area_root);
1019 kmem_cache_free(vmap_area_cachep, va);
1020 } else if (type == LE_FIT_TYPE) {
1021 /*
1022 * Split left edge of fit VA.
1023 *
1024 * | |
1025 * V NVA V R
1026 * |-------|-------|
1027 */
1028 va->va_start += size;
1029 } else if (type == RE_FIT_TYPE) {
1030 /*
1031 * Split right edge of fit VA.
1032 *
1033 * | |
1034 * L V NVA V
1035 * |-------|-------|
1036 */
1037 va->va_end = nva_start_addr;
1038 } else if (type == NE_FIT_TYPE) {
1039 /*
1040 * Split no edge of fit VA.
1041 *
1042 * | |
1043 * L V NVA V R
1044 * |---|-------|---|
1045 */
Uladzislau Rezki (Sony)82dd23e2019-07-11 20:58:57 -07001046 lva = __this_cpu_xchg(ne_fit_preload_node, NULL);
1047 if (unlikely(!lva)) {
1048 /*
1049 * For percpu allocator we do not do any pre-allocation
1050 * and leave it as it is. The reason is it most likely
1051 * never ends up with NE_FIT_TYPE splitting. In case of
1052 * percpu allocations offsets and sizes are aligned to
1053 * fixed align request, i.e. RE_FIT_TYPE and FL_FIT_TYPE
1054 * are its main fitting cases.
1055 *
1056 * There are a few exceptions though, as an example it is
1057 * a first allocation (early boot up) when we have "one"
1058 * big free space that has to be split.
Uladzislau Rezki (Sony)060650a2019-11-30 17:54:40 -08001059 *
1060 * Also we can hit this path in case of regular "vmap"
1061 * allocations, if "this" current CPU was not preloaded.
1062 * See the comment in alloc_vmap_area() why. If so, then
1063 * GFP_NOWAIT is used instead to get an extra object for
1064 * split purpose. That is rare and most time does not
1065 * occur.
1066 *
1067 * What happens if an allocation gets failed. Basically,
1068 * an "overflow" path is triggered to purge lazily freed
1069 * areas to free some memory, then, the "retry" path is
1070 * triggered to repeat one more time. See more details
1071 * in alloc_vmap_area() function.
Uladzislau Rezki (Sony)82dd23e2019-07-11 20:58:57 -07001072 */
1073 lva = kmem_cache_alloc(vmap_area_cachep, GFP_NOWAIT);
1074 if (!lva)
1075 return -1;
1076 }
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001077
1078 /*
1079 * Build the remainder.
1080 */
1081 lva->va_start = va->va_start;
1082 lva->va_end = nva_start_addr;
1083
1084 /*
1085 * Shrink this VA to remaining size.
1086 */
1087 va->va_start = nva_start_addr + size;
1088 } else {
1089 return -1;
1090 }
1091
1092 if (type != FL_FIT_TYPE) {
1093 augment_tree_propagate_from(va);
1094
Arnd Bergmann2c929232019-06-28 12:07:09 -07001095 if (lva) /* type == NE_FIT_TYPE */
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001096 insert_vmap_area_augment(lva, &va->rb_node,
1097 &free_vmap_area_root, &free_vmap_area_list);
1098 }
1099
1100 return 0;
1101}
1102
1103/*
1104 * Returns a start address of the newly allocated area, if success.
1105 * Otherwise a vend is returned that indicates failure.
1106 */
1107static __always_inline unsigned long
1108__alloc_vmap_area(unsigned long size, unsigned long align,
Uladzislau Rezki (Sony)cacca6b2019-07-11 20:58:53 -07001109 unsigned long vstart, unsigned long vend)
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001110{
1111 unsigned long nva_start_addr;
1112 struct vmap_area *va;
1113 enum fit_type type;
1114 int ret;
1115
1116 va = find_vmap_lowest_match(size, align, vstart);
1117 if (unlikely(!va))
1118 return vend;
1119
1120 if (va->va_start > vstart)
1121 nva_start_addr = ALIGN(va->va_start, align);
1122 else
1123 nva_start_addr = ALIGN(vstart, align);
1124
1125 /* Check the "vend" restriction. */
1126 if (nva_start_addr + size > vend)
1127 return vend;
1128
1129 /* Classify what we have found. */
1130 type = classify_va_fit_type(va, nva_start_addr, size);
1131 if (WARN_ON_ONCE(type == NOTHING_FIT))
1132 return vend;
1133
1134 /* Update the free vmap_area. */
1135 ret = adjust_va_to_fit_type(va, nva_start_addr, size, type);
1136 if (ret)
1137 return vend;
1138
Uladzislau Rezki (Sony)a6cf4e02019-05-17 14:31:37 -07001139#if DEBUG_AUGMENT_LOWEST_MATCH_CHECK
1140 find_vmap_lowest_match_check(size);
1141#endif
1142
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001143 return nva_start_addr;
1144}
Chris Wilson4da56b92016-04-04 14:46:42 +01001145
Nick Piggindb64fe02008-10-18 20:27:03 -07001146/*
Andrey Ryabinind98c9e82019-12-17 20:51:38 -08001147 * Free a region of KVA allocated by alloc_vmap_area
1148 */
1149static void free_vmap_area(struct vmap_area *va)
1150{
1151 /*
1152 * Remove from the busy tree/list.
1153 */
1154 spin_lock(&vmap_area_lock);
1155 unlink_va(va, &vmap_area_root);
1156 spin_unlock(&vmap_area_lock);
1157
1158 /*
1159 * Insert/Merge it back to the free tree/list.
1160 */
1161 spin_lock(&free_vmap_area_lock);
Uladzislau Rezki (Sony)96e2db42020-12-14 19:08:49 -08001162 merge_or_add_vmap_area_augment(va, &free_vmap_area_root, &free_vmap_area_list);
Andrey Ryabinind98c9e82019-12-17 20:51:38 -08001163 spin_unlock(&free_vmap_area_lock);
1164}
1165
1166/*
Nick Piggindb64fe02008-10-18 20:27:03 -07001167 * Allocate a region of KVA of the specified size and alignment, within the
1168 * vstart and vend.
1169 */
1170static struct vmap_area *alloc_vmap_area(unsigned long size,
1171 unsigned long align,
1172 unsigned long vstart, unsigned long vend,
1173 int node, gfp_t gfp_mask)
1174{
Uladzislau Rezki (Sony)82dd23e2019-07-11 20:58:57 -07001175 struct vmap_area *va, *pva;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 unsigned long addr;
Nick Piggindb64fe02008-10-18 20:27:03 -07001177 int purged = 0;
Andrey Ryabinind98c9e82019-12-17 20:51:38 -08001178 int ret;
Nick Piggindb64fe02008-10-18 20:27:03 -07001179
Nick Piggin77669702009-02-27 14:03:03 -08001180 BUG_ON(!size);
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08001181 BUG_ON(offset_in_page(size));
Nick Piggin89699602011-03-22 16:30:36 -07001182 BUG_ON(!is_power_of_2(align));
Nick Piggindb64fe02008-10-18 20:27:03 -07001183
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001184 if (unlikely(!vmap_initialized))
1185 return ERR_PTR(-EBUSY);
1186
Christoph Hellwig5803ed22016-12-12 16:44:20 -08001187 might_sleep();
Uladzislau Rezki (Sony)f07116d2019-11-30 17:54:37 -08001188 gfp_mask = gfp_mask & GFP_RECLAIM_MASK;
Chris Wilson4da56b92016-04-04 14:46:42 +01001189
Uladzislau Rezki (Sony)f07116d2019-11-30 17:54:37 -08001190 va = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node);
Nick Piggindb64fe02008-10-18 20:27:03 -07001191 if (unlikely(!va))
1192 return ERR_PTR(-ENOMEM);
1193
Catalin Marinas7f88f882013-11-12 15:07:45 -08001194 /*
1195 * Only scan the relevant parts containing pointers to other objects
1196 * to avoid false negatives.
1197 */
Uladzislau Rezki (Sony)f07116d2019-11-30 17:54:37 -08001198 kmemleak_scan_area(&va->rb_node, SIZE_MAX, gfp_mask);
Catalin Marinas7f88f882013-11-12 15:07:45 -08001199
Nick Piggindb64fe02008-10-18 20:27:03 -07001200retry:
Uladzislau Rezki (Sony)82dd23e2019-07-11 20:58:57 -07001201 /*
Uladzislau Rezki (Sony)81f1ba52019-11-30 17:54:33 -08001202 * Preload this CPU with one extra vmap_area object. It is used
1203 * when fit type of free area is NE_FIT_TYPE. Please note, it
1204 * does not guarantee that an allocation occurs on a CPU that
1205 * is preloaded, instead we minimize the case when it is not.
1206 * It can happen because of cpu migration, because there is a
1207 * race until the below spinlock is taken.
Uladzislau Rezki (Sony)82dd23e2019-07-11 20:58:57 -07001208 *
1209 * The preload is done in non-atomic context, thus it allows us
1210 * to use more permissive allocation masks to be more stable under
Uladzislau Rezki (Sony)81f1ba52019-11-30 17:54:33 -08001211 * low memory condition and high memory pressure. In rare case,
1212 * if not preloaded, GFP_NOWAIT is used.
Uladzislau Rezki (Sony)82dd23e2019-07-11 20:58:57 -07001213 *
Uladzislau Rezki (Sony)81f1ba52019-11-30 17:54:33 -08001214 * Set "pva" to NULL here, because of "retry" path.
Uladzislau Rezki (Sony)82dd23e2019-07-11 20:58:57 -07001215 */
Uladzislau Rezki (Sony)81f1ba52019-11-30 17:54:33 -08001216 pva = NULL;
Uladzislau Rezki (Sony)82dd23e2019-07-11 20:58:57 -07001217
Uladzislau Rezki (Sony)81f1ba52019-11-30 17:54:33 -08001218 if (!this_cpu_read(ne_fit_preload_node))
1219 /*
1220 * Even if it fails we do not really care about that.
1221 * Just proceed as it is. If needed "overflow" path
1222 * will refill the cache we allocate from.
1223 */
Uladzislau Rezki (Sony)f07116d2019-11-30 17:54:37 -08001224 pva = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node);
Uladzislau Rezki (Sony)82dd23e2019-07-11 20:58:57 -07001225
Uladzislau Rezki (Sony)e36176b2019-11-30 17:54:47 -08001226 spin_lock(&free_vmap_area_lock);
Uladzislau Rezki (Sony)81f1ba52019-11-30 17:54:33 -08001227
1228 if (pva && __this_cpu_cmpxchg(ne_fit_preload_node, NULL, pva))
1229 kmem_cache_free(vmap_area_cachep, pva);
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001230
Nick Piggin89699602011-03-22 16:30:36 -07001231 /*
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001232 * If an allocation fails, the "vend" address is
1233 * returned. Therefore trigger the overflow path.
Nick Piggin89699602011-03-22 16:30:36 -07001234 */
Uladzislau Rezki (Sony)cacca6b2019-07-11 20:58:53 -07001235 addr = __alloc_vmap_area(size, align, vstart, vend);
Uladzislau Rezki (Sony)e36176b2019-11-30 17:54:47 -08001236 spin_unlock(&free_vmap_area_lock);
1237
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001238 if (unlikely(addr == vend))
Nick Piggin89699602011-03-22 16:30:36 -07001239 goto overflow;
Nick Piggindb64fe02008-10-18 20:27:03 -07001240
1241 va->va_start = addr;
1242 va->va_end = addr + size;
Pengfei Li688fcbf2019-09-23 15:36:39 -07001243 va->vm = NULL;
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001244
Andrey Ryabinind98c9e82019-12-17 20:51:38 -08001245
Uladzislau Rezki (Sony)e36176b2019-11-30 17:54:47 -08001246 spin_lock(&vmap_area_lock);
1247 insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
Nick Piggindb64fe02008-10-18 20:27:03 -07001248 spin_unlock(&vmap_area_lock);
1249
Wang Xiaoqiang61e16552016-01-15 16:57:19 -08001250 BUG_ON(!IS_ALIGNED(va->va_start, align));
Nick Piggin89699602011-03-22 16:30:36 -07001251 BUG_ON(va->va_start < vstart);
1252 BUG_ON(va->va_end > vend);
1253
Andrey Ryabinind98c9e82019-12-17 20:51:38 -08001254 ret = kasan_populate_vmalloc(addr, size);
1255 if (ret) {
1256 free_vmap_area(va);
1257 return ERR_PTR(ret);
1258 }
1259
Nick Piggindb64fe02008-10-18 20:27:03 -07001260 return va;
Nick Piggin89699602011-03-22 16:30:36 -07001261
1262overflow:
Nick Piggin89699602011-03-22 16:30:36 -07001263 if (!purged) {
1264 purge_vmap_area_lazy();
1265 purged = 1;
1266 goto retry;
1267 }
Chris Wilson4da56b92016-04-04 14:46:42 +01001268
1269 if (gfpflags_allow_blocking(gfp_mask)) {
1270 unsigned long freed = 0;
1271 blocking_notifier_call_chain(&vmap_notify_list, 0, &freed);
1272 if (freed > 0) {
1273 purged = 0;
1274 goto retry;
1275 }
1276 }
1277
Florian Fainelli03497d72017-04-27 11:19:00 -07001278 if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit())
Joe Perches756a0252016-03-17 14:19:47 -07001279 pr_warn("vmap allocation for size %lu failed: use vmalloc=<size> to increase size\n",
1280 size);
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001281
1282 kmem_cache_free(vmap_area_cachep, va);
Nick Piggin89699602011-03-22 16:30:36 -07001283 return ERR_PTR(-EBUSY);
Nick Piggindb64fe02008-10-18 20:27:03 -07001284}
1285
Chris Wilson4da56b92016-04-04 14:46:42 +01001286int register_vmap_purge_notifier(struct notifier_block *nb)
1287{
1288 return blocking_notifier_chain_register(&vmap_notify_list, nb);
1289}
1290EXPORT_SYMBOL_GPL(register_vmap_purge_notifier);
1291
1292int unregister_vmap_purge_notifier(struct notifier_block *nb)
1293{
1294 return blocking_notifier_chain_unregister(&vmap_notify_list, nb);
1295}
1296EXPORT_SYMBOL_GPL(unregister_vmap_purge_notifier);
1297
Nick Piggindb64fe02008-10-18 20:27:03 -07001298/*
Nick Piggindb64fe02008-10-18 20:27:03 -07001299 * lazy_max_pages is the maximum amount of virtual address space we gather up
1300 * before attempting to purge with a TLB flush.
1301 *
1302 * There is a tradeoff here: a larger number will cover more kernel page tables
1303 * and take slightly longer to purge, but it will linearly reduce the number of
1304 * global TLB flushes that must be performed. It would seem natural to scale
1305 * this number up linearly with the number of CPUs (because vmapping activity
1306 * could also scale linearly with the number of CPUs), however it is likely
1307 * that in practice, workloads might be constrained in other ways that mean
1308 * vmap activity will not scale linearly with CPUs. Also, I want to be
1309 * conservative and not introduce a big latency on huge systems, so go with
1310 * a less aggressive log scale. It will still be an improvement over the old
1311 * code, and it will be simple to change the scale factor if we find that it
1312 * becomes a problem on bigger systems.
1313 */
1314static unsigned long lazy_max_pages(void)
1315{
1316 unsigned int log;
1317
1318 log = fls(num_online_cpus());
1319
1320 return log * (32UL * 1024 * 1024 / PAGE_SIZE);
1321}
1322
Uladzislau Rezki (Sony)4d36e6f2019-05-14 15:41:25 -07001323static atomic_long_t vmap_lazy_nr = ATOMIC_LONG_INIT(0);
Nick Piggindb64fe02008-10-18 20:27:03 -07001324
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001325/*
1326 * Serialize vmap purging. There is no actual criticial section protected
1327 * by this look, but we want to avoid concurrent calls for performance
1328 * reasons and to make the pcpu_get_vm_areas more deterministic.
1329 */
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001330static DEFINE_MUTEX(vmap_purge_lock);
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001331
Nick Piggin02b709d2010-02-01 22:25:57 +11001332/* for per-CPU blocks */
1333static void purge_fragmented_blocks_allcpus(void);
1334
Nick Piggindb64fe02008-10-18 20:27:03 -07001335/*
Cliff Wickman3ee48b62010-09-16 11:44:02 -05001336 * called before a call to iounmap() if the caller wants vm_area_struct's
1337 * immediately freed.
1338 */
1339void set_iounmap_nonlazy(void)
1340{
Uladzislau Rezki (Sony)4d36e6f2019-05-14 15:41:25 -07001341 atomic_long_set(&vmap_lazy_nr, lazy_max_pages()+1);
Cliff Wickman3ee48b62010-09-16 11:44:02 -05001342}
1343
1344/*
Nick Piggindb64fe02008-10-18 20:27:03 -07001345 * Purges all lazily-freed vmap areas.
Nick Piggindb64fe02008-10-18 20:27:03 -07001346 */
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001347static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end)
Nick Piggindb64fe02008-10-18 20:27:03 -07001348{
Uladzislau Rezki (Sony)4d36e6f2019-05-14 15:41:25 -07001349 unsigned long resched_threshold;
Uladzislau Rezki (Sony)96e2db42020-12-14 19:08:49 -08001350 struct list_head local_pure_list;
1351 struct vmap_area *va, *n_va;
Nick Piggindb64fe02008-10-18 20:27:03 -07001352
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001353 lockdep_assert_held(&vmap_purge_lock);
Nick Piggin02b709d2010-02-01 22:25:57 +11001354
Uladzislau Rezki (Sony)96e2db42020-12-14 19:08:49 -08001355 spin_lock(&purge_vmap_area_lock);
1356 purge_vmap_area_root = RB_ROOT;
1357 list_replace_init(&purge_vmap_area_list, &local_pure_list);
1358 spin_unlock(&purge_vmap_area_lock);
1359
1360 if (unlikely(list_empty(&local_pure_list)))
Uladzislau Rezki (Sony)68571be92019-05-14 15:41:22 -07001361 return false;
1362
Uladzislau Rezki (Sony)96e2db42020-12-14 19:08:49 -08001363 start = min(start,
1364 list_first_entry(&local_pure_list,
1365 struct vmap_area, list)->va_start);
1366
1367 end = max(end,
1368 list_last_entry(&local_pure_list,
1369 struct vmap_area, list)->va_end);
Nick Piggindb64fe02008-10-18 20:27:03 -07001370
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001371 flush_tlb_kernel_range(start, end);
Uladzislau Rezki (Sony)4d36e6f2019-05-14 15:41:25 -07001372 resched_threshold = lazy_max_pages() << 1;
Nick Piggindb64fe02008-10-18 20:27:03 -07001373
Uladzislau Rezki (Sony)e36176b2019-11-30 17:54:47 -08001374 spin_lock(&free_vmap_area_lock);
Uladzislau Rezki (Sony)96e2db42020-12-14 19:08:49 -08001375 list_for_each_entry_safe(va, n_va, &local_pure_list, list) {
Uladzislau Rezki (Sony)4d36e6f2019-05-14 15:41:25 -07001376 unsigned long nr = (va->va_end - va->va_start) >> PAGE_SHIFT;
Daniel Axtens3c5c3cf2019-11-30 17:54:50 -08001377 unsigned long orig_start = va->va_start;
1378 unsigned long orig_end = va->va_end;
Joel Fernandes763b2182016-12-12 16:44:26 -08001379
Uladzislau Rezki (Sony)dd3b8352019-09-23 15:36:36 -07001380 /*
1381 * Finally insert or merge lazily-freed area. It is
1382 * detached and there is no need to "unlink" it from
1383 * anything.
1384 */
Uladzislau Rezki (Sony)96e2db42020-12-14 19:08:49 -08001385 va = merge_or_add_vmap_area_augment(va, &free_vmap_area_root,
1386 &free_vmap_area_list);
Daniel Axtens3c5c3cf2019-11-30 17:54:50 -08001387
Uladzislau Rezki (Sony)9c801f62020-08-06 23:24:24 -07001388 if (!va)
1389 continue;
1390
Daniel Axtens3c5c3cf2019-11-30 17:54:50 -08001391 if (is_vmalloc_or_module_addr((void *)orig_start))
1392 kasan_release_vmalloc(orig_start, orig_end,
1393 va->va_start, va->va_end);
Uladzislau Rezki (Sony)dd3b8352019-09-23 15:36:36 -07001394
Uladzislau Rezki (Sony)4d36e6f2019-05-14 15:41:25 -07001395 atomic_long_sub(nr, &vmap_lazy_nr);
Uladzislau Rezki (Sony)68571be92019-05-14 15:41:22 -07001396
Uladzislau Rezki (Sony)4d36e6f2019-05-14 15:41:25 -07001397 if (atomic_long_read(&vmap_lazy_nr) < resched_threshold)
Uladzislau Rezki (Sony)e36176b2019-11-30 17:54:47 -08001398 cond_resched_lock(&free_vmap_area_lock);
Joel Fernandes763b2182016-12-12 16:44:26 -08001399 }
Uladzislau Rezki (Sony)e36176b2019-11-30 17:54:47 -08001400 spin_unlock(&free_vmap_area_lock);
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001401 return true;
Nick Piggindb64fe02008-10-18 20:27:03 -07001402}
1403
1404/*
Nick Piggin496850e2008-11-19 15:36:33 -08001405 * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
1406 * is already purging.
1407 */
1408static void try_purge_vmap_area_lazy(void)
1409{
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001410 if (mutex_trylock(&vmap_purge_lock)) {
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001411 __purge_vmap_area_lazy(ULONG_MAX, 0);
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001412 mutex_unlock(&vmap_purge_lock);
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001413 }
Nick Piggin496850e2008-11-19 15:36:33 -08001414}
1415
1416/*
Nick Piggindb64fe02008-10-18 20:27:03 -07001417 * Kick off a purge of the outstanding lazy areas.
1418 */
1419static void purge_vmap_area_lazy(void)
1420{
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001421 mutex_lock(&vmap_purge_lock);
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001422 purge_fragmented_blocks_allcpus();
1423 __purge_vmap_area_lazy(ULONG_MAX, 0);
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001424 mutex_unlock(&vmap_purge_lock);
Nick Piggindb64fe02008-10-18 20:27:03 -07001425}
1426
1427/*
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -08001428 * Free a vmap area, caller ensuring that the area has been unmapped
1429 * and flush_cache_vunmap had been called for the correct range
1430 * previously.
Nick Piggindb64fe02008-10-18 20:27:03 -07001431 */
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -08001432static void free_vmap_area_noflush(struct vmap_area *va)
Nick Piggindb64fe02008-10-18 20:27:03 -07001433{
Uladzislau Rezki (Sony)4d36e6f2019-05-14 15:41:25 -07001434 unsigned long nr_lazy;
Chris Wilson80c4bd72016-05-20 16:57:38 -07001435
Uladzislau Rezki (Sony)dd3b8352019-09-23 15:36:36 -07001436 spin_lock(&vmap_area_lock);
1437 unlink_va(va, &vmap_area_root);
1438 spin_unlock(&vmap_area_lock);
1439
Uladzislau Rezki (Sony)4d36e6f2019-05-14 15:41:25 -07001440 nr_lazy = atomic_long_add_return((va->va_end - va->va_start) >>
1441 PAGE_SHIFT, &vmap_lazy_nr);
Chris Wilson80c4bd72016-05-20 16:57:38 -07001442
Uladzislau Rezki (Sony)96e2db42020-12-14 19:08:49 -08001443 /*
1444 * Merge or place it to the purge tree/list.
1445 */
1446 spin_lock(&purge_vmap_area_lock);
1447 merge_or_add_vmap_area(va,
1448 &purge_vmap_area_root, &purge_vmap_area_list);
1449 spin_unlock(&purge_vmap_area_lock);
Chris Wilson80c4bd72016-05-20 16:57:38 -07001450
Uladzislau Rezki (Sony)96e2db42020-12-14 19:08:49 -08001451 /* After this point, we may free va at any time */
Chris Wilson80c4bd72016-05-20 16:57:38 -07001452 if (unlikely(nr_lazy > lazy_max_pages()))
Nick Piggin496850e2008-11-19 15:36:33 -08001453 try_purge_vmap_area_lazy();
Nick Piggindb64fe02008-10-18 20:27:03 -07001454}
1455
Nick Pigginb29acbd2008-12-01 13:13:47 -08001456/*
1457 * Free and unmap a vmap area
1458 */
1459static void free_unmap_vmap_area(struct vmap_area *va)
1460{
1461 flush_cache_vunmap(va->va_start, va->va_end);
Christoph Hellwig855e57a2020-06-01 21:51:23 -07001462 unmap_kernel_range_noflush(va->va_start, va->va_end - va->va_start);
Vlastimil Babka8e57f8a2020-01-13 16:29:20 -08001463 if (debug_pagealloc_enabled_static())
Chintan Pandya82a2e922018-06-07 17:06:46 -07001464 flush_tlb_kernel_range(va->va_start, va->va_end);
1465
Christoph Hellwigc8eef012016-12-12 16:44:01 -08001466 free_vmap_area_noflush(va);
Nick Pigginb29acbd2008-12-01 13:13:47 -08001467}
1468
Nick Piggindb64fe02008-10-18 20:27:03 -07001469static struct vmap_area *find_vmap_area(unsigned long addr)
1470{
1471 struct vmap_area *va;
1472
1473 spin_lock(&vmap_area_lock);
1474 va = __find_vmap_area(addr);
1475 spin_unlock(&vmap_area_lock);
1476
1477 return va;
1478}
1479
Nick Piggindb64fe02008-10-18 20:27:03 -07001480/*** Per cpu kva allocator ***/
1481
1482/*
1483 * vmap space is limited especially on 32 bit architectures. Ensure there is
1484 * room for at least 16 percpu vmap blocks per CPU.
1485 */
1486/*
1487 * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
1488 * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess
1489 * instead (we just need a rough idea)
1490 */
1491#if BITS_PER_LONG == 32
1492#define VMALLOC_SPACE (128UL*1024*1024)
1493#else
1494#define VMALLOC_SPACE (128UL*1024*1024*1024)
1495#endif
1496
1497#define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE)
1498#define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */
1499#define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */
1500#define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2)
1501#define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */
1502#define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */
Clemens Ladischf982f9152011-06-21 22:09:50 +02001503#define VMAP_BBMAP_BITS \
1504 VMAP_MIN(VMAP_BBMAP_BITS_MAX, \
1505 VMAP_MAX(VMAP_BBMAP_BITS_MIN, \
1506 VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16))
Nick Piggindb64fe02008-10-18 20:27:03 -07001507
1508#define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE)
1509
1510struct vmap_block_queue {
1511 spinlock_t lock;
1512 struct list_head free;
Nick Piggindb64fe02008-10-18 20:27:03 -07001513};
1514
1515struct vmap_block {
1516 spinlock_t lock;
1517 struct vmap_area *va;
Nick Piggindb64fe02008-10-18 20:27:03 -07001518 unsigned long free, dirty;
Roman Pen7d61bfe2015-04-15 16:13:55 -07001519 unsigned long dirty_min, dirty_max; /*< dirty range */
Nick Pigginde560422010-02-01 22:24:18 +11001520 struct list_head free_list;
1521 struct rcu_head rcu_head;
Nick Piggin02b709d2010-02-01 22:25:57 +11001522 struct list_head purge;
Nick Piggindb64fe02008-10-18 20:27:03 -07001523};
1524
1525/* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
1526static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
1527
1528/*
Matthew Wilcox (Oracle)0f145992020-08-06 23:24:05 -07001529 * XArray of vmap blocks, indexed by address, to quickly find a vmap block
Nick Piggindb64fe02008-10-18 20:27:03 -07001530 * in the free path. Could get rid of this if we change the API to return a
1531 * "cookie" from alloc, to be passed to free. But no big deal yet.
1532 */
Matthew Wilcox (Oracle)0f145992020-08-06 23:24:05 -07001533static DEFINE_XARRAY(vmap_blocks);
Nick Piggindb64fe02008-10-18 20:27:03 -07001534
1535/*
1536 * We should probably have a fallback mechanism to allocate virtual memory
1537 * out of partially filled vmap blocks. However vmap block sizing should be
1538 * fairly reasonable according to the vmalloc size, so it shouldn't be a
1539 * big problem.
1540 */
1541
1542static unsigned long addr_to_vb_idx(unsigned long addr)
1543{
1544 addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
1545 addr /= VMAP_BLOCK_SIZE;
1546 return addr;
1547}
1548
Roman Pencf725ce2015-04-15 16:13:52 -07001549static void *vmap_block_vaddr(unsigned long va_start, unsigned long pages_off)
1550{
1551 unsigned long addr;
1552
1553 addr = va_start + (pages_off << PAGE_SHIFT);
1554 BUG_ON(addr_to_vb_idx(addr) != addr_to_vb_idx(va_start));
1555 return (void *)addr;
1556}
1557
1558/**
1559 * new_vmap_block - allocates new vmap_block and occupies 2^order pages in this
1560 * block. Of course pages number can't exceed VMAP_BBMAP_BITS
1561 * @order: how many 2^order pages should be occupied in newly allocated block
1562 * @gfp_mask: flags for the page level allocator
1563 *
Mike Rapoporta862f682019-03-05 15:48:42 -08001564 * Return: virtual address in a newly allocated block or ERR_PTR(-errno)
Roman Pencf725ce2015-04-15 16:13:52 -07001565 */
1566static void *new_vmap_block(unsigned int order, gfp_t gfp_mask)
Nick Piggindb64fe02008-10-18 20:27:03 -07001567{
1568 struct vmap_block_queue *vbq;
1569 struct vmap_block *vb;
1570 struct vmap_area *va;
1571 unsigned long vb_idx;
1572 int node, err;
Roman Pencf725ce2015-04-15 16:13:52 -07001573 void *vaddr;
Nick Piggindb64fe02008-10-18 20:27:03 -07001574
1575 node = numa_node_id();
1576
1577 vb = kmalloc_node(sizeof(struct vmap_block),
1578 gfp_mask & GFP_RECLAIM_MASK, node);
1579 if (unlikely(!vb))
1580 return ERR_PTR(-ENOMEM);
1581
1582 va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
1583 VMALLOC_START, VMALLOC_END,
1584 node, gfp_mask);
Tobias Klauserddf9c6d42011-01-13 15:46:15 -08001585 if (IS_ERR(va)) {
Nick Piggindb64fe02008-10-18 20:27:03 -07001586 kfree(vb);
Julia Lawalle7d86342010-08-09 17:18:28 -07001587 return ERR_CAST(va);
Nick Piggindb64fe02008-10-18 20:27:03 -07001588 }
1589
Roman Pencf725ce2015-04-15 16:13:52 -07001590 vaddr = vmap_block_vaddr(va->va_start, 0);
Nick Piggindb64fe02008-10-18 20:27:03 -07001591 spin_lock_init(&vb->lock);
1592 vb->va = va;
Roman Pencf725ce2015-04-15 16:13:52 -07001593 /* At least something should be left free */
1594 BUG_ON(VMAP_BBMAP_BITS <= (1UL << order));
1595 vb->free = VMAP_BBMAP_BITS - (1UL << order);
Nick Piggindb64fe02008-10-18 20:27:03 -07001596 vb->dirty = 0;
Roman Pen7d61bfe2015-04-15 16:13:55 -07001597 vb->dirty_min = VMAP_BBMAP_BITS;
1598 vb->dirty_max = 0;
Nick Piggindb64fe02008-10-18 20:27:03 -07001599 INIT_LIST_HEAD(&vb->free_list);
Nick Piggindb64fe02008-10-18 20:27:03 -07001600
1601 vb_idx = addr_to_vb_idx(va->va_start);
Matthew Wilcox (Oracle)0f145992020-08-06 23:24:05 -07001602 err = xa_insert(&vmap_blocks, vb_idx, vb, gfp_mask);
1603 if (err) {
1604 kfree(vb);
1605 free_vmap_area(va);
1606 return ERR_PTR(err);
1607 }
Nick Piggindb64fe02008-10-18 20:27:03 -07001608
1609 vbq = &get_cpu_var(vmap_block_queue);
Nick Piggindb64fe02008-10-18 20:27:03 -07001610 spin_lock(&vbq->lock);
Roman Pen68ac5462015-04-15 16:13:48 -07001611 list_add_tail_rcu(&vb->free_list, &vbq->free);
Nick Piggindb64fe02008-10-18 20:27:03 -07001612 spin_unlock(&vbq->lock);
Tejun Heo3f04ba82009-10-29 22:34:12 +09001613 put_cpu_var(vmap_block_queue);
Nick Piggindb64fe02008-10-18 20:27:03 -07001614
Roman Pencf725ce2015-04-15 16:13:52 -07001615 return vaddr;
Nick Piggindb64fe02008-10-18 20:27:03 -07001616}
1617
Nick Piggindb64fe02008-10-18 20:27:03 -07001618static void free_vmap_block(struct vmap_block *vb)
1619{
1620 struct vmap_block *tmp;
Nick Piggindb64fe02008-10-18 20:27:03 -07001621
Matthew Wilcox (Oracle)0f145992020-08-06 23:24:05 -07001622 tmp = xa_erase(&vmap_blocks, addr_to_vb_idx(vb->va->va_start));
Nick Piggindb64fe02008-10-18 20:27:03 -07001623 BUG_ON(tmp != vb);
1624
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -08001625 free_vmap_area_noflush(vb->va);
Lai Jiangshan22a3c7d2011-03-18 12:13:08 +08001626 kfree_rcu(vb, rcu_head);
Nick Piggindb64fe02008-10-18 20:27:03 -07001627}
1628
Nick Piggin02b709d2010-02-01 22:25:57 +11001629static void purge_fragmented_blocks(int cpu)
1630{
1631 LIST_HEAD(purge);
1632 struct vmap_block *vb;
1633 struct vmap_block *n_vb;
1634 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
1635
1636 rcu_read_lock();
1637 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
1638
1639 if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS))
1640 continue;
1641
1642 spin_lock(&vb->lock);
1643 if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) {
1644 vb->free = 0; /* prevent further allocs after releasing lock */
1645 vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */
Roman Pen7d61bfe2015-04-15 16:13:55 -07001646 vb->dirty_min = 0;
1647 vb->dirty_max = VMAP_BBMAP_BITS;
Nick Piggin02b709d2010-02-01 22:25:57 +11001648 spin_lock(&vbq->lock);
1649 list_del_rcu(&vb->free_list);
1650 spin_unlock(&vbq->lock);
1651 spin_unlock(&vb->lock);
1652 list_add_tail(&vb->purge, &purge);
1653 } else
1654 spin_unlock(&vb->lock);
1655 }
1656 rcu_read_unlock();
1657
1658 list_for_each_entry_safe(vb, n_vb, &purge, purge) {
1659 list_del(&vb->purge);
1660 free_vmap_block(vb);
1661 }
1662}
1663
Nick Piggin02b709d2010-02-01 22:25:57 +11001664static void purge_fragmented_blocks_allcpus(void)
1665{
1666 int cpu;
1667
1668 for_each_possible_cpu(cpu)
1669 purge_fragmented_blocks(cpu);
1670}
1671
Nick Piggindb64fe02008-10-18 20:27:03 -07001672static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
1673{
1674 struct vmap_block_queue *vbq;
1675 struct vmap_block *vb;
Roman Pencf725ce2015-04-15 16:13:52 -07001676 void *vaddr = NULL;
Nick Piggindb64fe02008-10-18 20:27:03 -07001677 unsigned int order;
1678
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08001679 BUG_ON(offset_in_page(size));
Nick Piggindb64fe02008-10-18 20:27:03 -07001680 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
Jan Karaaa91c4d2012-07-31 16:41:37 -07001681 if (WARN_ON(size == 0)) {
1682 /*
1683 * Allocating 0 bytes isn't what caller wants since
1684 * get_order(0) returns funny result. Just warn and terminate
1685 * early.
1686 */
1687 return NULL;
1688 }
Nick Piggindb64fe02008-10-18 20:27:03 -07001689 order = get_order(size);
1690
Nick Piggindb64fe02008-10-18 20:27:03 -07001691 rcu_read_lock();
1692 vbq = &get_cpu_var(vmap_block_queue);
1693 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
Roman Pencf725ce2015-04-15 16:13:52 -07001694 unsigned long pages_off;
Nick Piggindb64fe02008-10-18 20:27:03 -07001695
1696 spin_lock(&vb->lock);
Roman Pencf725ce2015-04-15 16:13:52 -07001697 if (vb->free < (1UL << order)) {
1698 spin_unlock(&vb->lock);
1699 continue;
1700 }
Nick Piggin02b709d2010-02-01 22:25:57 +11001701
Roman Pencf725ce2015-04-15 16:13:52 -07001702 pages_off = VMAP_BBMAP_BITS - vb->free;
1703 vaddr = vmap_block_vaddr(vb->va->va_start, pages_off);
Nick Piggin02b709d2010-02-01 22:25:57 +11001704 vb->free -= 1UL << order;
1705 if (vb->free == 0) {
1706 spin_lock(&vbq->lock);
1707 list_del_rcu(&vb->free_list);
1708 spin_unlock(&vbq->lock);
Nick Piggindb64fe02008-10-18 20:27:03 -07001709 }
Roman Pencf725ce2015-04-15 16:13:52 -07001710
Nick Piggindb64fe02008-10-18 20:27:03 -07001711 spin_unlock(&vb->lock);
Nick Piggin02b709d2010-02-01 22:25:57 +11001712 break;
Nick Piggindb64fe02008-10-18 20:27:03 -07001713 }
Nick Piggin02b709d2010-02-01 22:25:57 +11001714
Tejun Heo3f04ba82009-10-29 22:34:12 +09001715 put_cpu_var(vmap_block_queue);
Nick Piggindb64fe02008-10-18 20:27:03 -07001716 rcu_read_unlock();
1717
Roman Pencf725ce2015-04-15 16:13:52 -07001718 /* Allocate new block if nothing was found */
1719 if (!vaddr)
1720 vaddr = new_vmap_block(order, gfp_mask);
Nick Piggindb64fe02008-10-18 20:27:03 -07001721
Roman Pencf725ce2015-04-15 16:13:52 -07001722 return vaddr;
Nick Piggindb64fe02008-10-18 20:27:03 -07001723}
1724
Christoph Hellwig78a0e8c2020-06-01 21:51:02 -07001725static void vb_free(unsigned long addr, unsigned long size)
Nick Piggindb64fe02008-10-18 20:27:03 -07001726{
1727 unsigned long offset;
Nick Piggindb64fe02008-10-18 20:27:03 -07001728 unsigned int order;
1729 struct vmap_block *vb;
1730
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08001731 BUG_ON(offset_in_page(size));
Nick Piggindb64fe02008-10-18 20:27:03 -07001732 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
Nick Pigginb29acbd2008-12-01 13:13:47 -08001733
Christoph Hellwig78a0e8c2020-06-01 21:51:02 -07001734 flush_cache_vunmap(addr, addr + size);
Nick Pigginb29acbd2008-12-01 13:13:47 -08001735
Nick Piggindb64fe02008-10-18 20:27:03 -07001736 order = get_order(size);
Christoph Hellwig78a0e8c2020-06-01 21:51:02 -07001737 offset = (addr & (VMAP_BLOCK_SIZE - 1)) >> PAGE_SHIFT;
Matthew Wilcox (Oracle)0f145992020-08-06 23:24:05 -07001738 vb = xa_load(&vmap_blocks, addr_to_vb_idx(addr));
Nick Piggindb64fe02008-10-18 20:27:03 -07001739
Christoph Hellwigb521c432020-06-01 21:51:07 -07001740 unmap_kernel_range_noflush(addr, size);
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -08001741
Vlastimil Babka8e57f8a2020-01-13 16:29:20 -08001742 if (debug_pagealloc_enabled_static())
Christoph Hellwig78a0e8c2020-06-01 21:51:02 -07001743 flush_tlb_kernel_range(addr, addr + size);
Chintan Pandya82a2e922018-06-07 17:06:46 -07001744
Nick Piggindb64fe02008-10-18 20:27:03 -07001745 spin_lock(&vb->lock);
Roman Pen7d61bfe2015-04-15 16:13:55 -07001746
1747 /* Expand dirty range */
1748 vb->dirty_min = min(vb->dirty_min, offset);
1749 vb->dirty_max = max(vb->dirty_max, offset + (1UL << order));
MinChan Kimd0868172009-03-31 15:19:26 -07001750
Nick Piggindb64fe02008-10-18 20:27:03 -07001751 vb->dirty += 1UL << order;
1752 if (vb->dirty == VMAP_BBMAP_BITS) {
Nick Pigginde560422010-02-01 22:24:18 +11001753 BUG_ON(vb->free);
Nick Piggindb64fe02008-10-18 20:27:03 -07001754 spin_unlock(&vb->lock);
1755 free_vmap_block(vb);
1756 } else
1757 spin_unlock(&vb->lock);
1758}
1759
Rick Edgecombe868b1042019-04-25 17:11:36 -07001760static void _vm_unmap_aliases(unsigned long start, unsigned long end, int flush)
Nick Piggindb64fe02008-10-18 20:27:03 -07001761{
Nick Piggindb64fe02008-10-18 20:27:03 -07001762 int cpu;
Nick Piggindb64fe02008-10-18 20:27:03 -07001763
Jeremy Fitzhardinge9b463332008-10-28 19:22:34 +11001764 if (unlikely(!vmap_initialized))
1765 return;
1766
Christoph Hellwig5803ed22016-12-12 16:44:20 -08001767 might_sleep();
1768
Nick Piggindb64fe02008-10-18 20:27:03 -07001769 for_each_possible_cpu(cpu) {
1770 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
1771 struct vmap_block *vb;
1772
1773 rcu_read_lock();
1774 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
Nick Piggindb64fe02008-10-18 20:27:03 -07001775 spin_lock(&vb->lock);
Roman Pen7d61bfe2015-04-15 16:13:55 -07001776 if (vb->dirty) {
1777 unsigned long va_start = vb->va->va_start;
Nick Piggindb64fe02008-10-18 20:27:03 -07001778 unsigned long s, e;
Joonsoo Kimb136be5e2013-09-11 14:21:40 -07001779
Roman Pen7d61bfe2015-04-15 16:13:55 -07001780 s = va_start + (vb->dirty_min << PAGE_SHIFT);
1781 e = va_start + (vb->dirty_max << PAGE_SHIFT);
Nick Piggindb64fe02008-10-18 20:27:03 -07001782
Roman Pen7d61bfe2015-04-15 16:13:55 -07001783 start = min(s, start);
1784 end = max(e, end);
1785
Nick Piggindb64fe02008-10-18 20:27:03 -07001786 flush = 1;
Nick Piggindb64fe02008-10-18 20:27:03 -07001787 }
1788 spin_unlock(&vb->lock);
1789 }
1790 rcu_read_unlock();
1791 }
1792
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001793 mutex_lock(&vmap_purge_lock);
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001794 purge_fragmented_blocks_allcpus();
1795 if (!__purge_vmap_area_lazy(start, end) && flush)
1796 flush_tlb_kernel_range(start, end);
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001797 mutex_unlock(&vmap_purge_lock);
Nick Piggindb64fe02008-10-18 20:27:03 -07001798}
Rick Edgecombe868b1042019-04-25 17:11:36 -07001799
1800/**
1801 * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
1802 *
1803 * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
1804 * to amortize TLB flushing overheads. What this means is that any page you
1805 * have now, may, in a former life, have been mapped into kernel virtual
1806 * address by the vmap layer and so there might be some CPUs with TLB entries
1807 * still referencing that page (additional to the regular 1:1 kernel mapping).
1808 *
1809 * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
1810 * be sure that none of the pages we have control over will have any aliases
1811 * from the vmap layer.
1812 */
1813void vm_unmap_aliases(void)
1814{
1815 unsigned long start = ULONG_MAX, end = 0;
1816 int flush = 0;
1817
1818 _vm_unmap_aliases(start, end, flush);
1819}
Nick Piggindb64fe02008-10-18 20:27:03 -07001820EXPORT_SYMBOL_GPL(vm_unmap_aliases);
1821
1822/**
1823 * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
1824 * @mem: the pointer returned by vm_map_ram
1825 * @count: the count passed to that vm_map_ram call (cannot unmap partial)
1826 */
1827void vm_unmap_ram(const void *mem, unsigned int count)
1828{
Guillermo Julián Moreno65ee03c2016-06-03 14:55:33 -07001829 unsigned long size = (unsigned long)count << PAGE_SHIFT;
Nick Piggindb64fe02008-10-18 20:27:03 -07001830 unsigned long addr = (unsigned long)mem;
Christoph Hellwig9c3acf62016-12-12 16:44:04 -08001831 struct vmap_area *va;
Nick Piggindb64fe02008-10-18 20:27:03 -07001832
Christoph Hellwig5803ed22016-12-12 16:44:20 -08001833 might_sleep();
Nick Piggindb64fe02008-10-18 20:27:03 -07001834 BUG_ON(!addr);
1835 BUG_ON(addr < VMALLOC_START);
1836 BUG_ON(addr > VMALLOC_END);
Shawn Lina1c0b1a2016-03-17 14:20:37 -07001837 BUG_ON(!PAGE_ALIGNED(addr));
Nick Piggindb64fe02008-10-18 20:27:03 -07001838
Andrey Ryabinind98c9e82019-12-17 20:51:38 -08001839 kasan_poison_vmalloc(mem, size);
1840
Christoph Hellwig9c3acf62016-12-12 16:44:04 -08001841 if (likely(count <= VMAP_MAX_ALLOC)) {
Chintan Pandya05e3ff92018-06-07 17:06:53 -07001842 debug_check_no_locks_freed(mem, size);
Christoph Hellwig78a0e8c2020-06-01 21:51:02 -07001843 vb_free(addr, size);
Christoph Hellwig9c3acf62016-12-12 16:44:04 -08001844 return;
1845 }
1846
1847 va = find_vmap_area(addr);
1848 BUG_ON(!va);
Chintan Pandya05e3ff92018-06-07 17:06:53 -07001849 debug_check_no_locks_freed((void *)va->va_start,
1850 (va->va_end - va->va_start));
Christoph Hellwig9c3acf62016-12-12 16:44:04 -08001851 free_unmap_vmap_area(va);
Nick Piggindb64fe02008-10-18 20:27:03 -07001852}
1853EXPORT_SYMBOL(vm_unmap_ram);
1854
1855/**
1856 * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
1857 * @pages: an array of pointers to the pages to be mapped
1858 * @count: number of pages
1859 * @node: prefer to allocate data structures on this node
Randy Dunlape99c97a2008-10-29 14:01:09 -07001860 *
Gioh Kim36437632014-04-07 15:37:37 -07001861 * If you use this function for less than VMAP_MAX_ALLOC pages, it could be
1862 * faster than vmap so it's good. But if you mix long-life and short-life
1863 * objects with vm_map_ram(), it could consume lots of address space through
1864 * fragmentation (especially on a 32bit machine). You could see failures in
1865 * the end. Please use this function for short-lived objects.
1866 *
Randy Dunlape99c97a2008-10-29 14:01:09 -07001867 * Returns: a pointer to the address that has been mapped, or %NULL on failure
Nick Piggindb64fe02008-10-18 20:27:03 -07001868 */
Christoph Hellwigd4efd792020-06-01 21:51:27 -07001869void *vm_map_ram(struct page **pages, unsigned int count, int node)
Nick Piggindb64fe02008-10-18 20:27:03 -07001870{
Guillermo Julián Moreno65ee03c2016-06-03 14:55:33 -07001871 unsigned long size = (unsigned long)count << PAGE_SHIFT;
Nick Piggindb64fe02008-10-18 20:27:03 -07001872 unsigned long addr;
1873 void *mem;
1874
1875 if (likely(count <= VMAP_MAX_ALLOC)) {
1876 mem = vb_alloc(size, GFP_KERNEL);
1877 if (IS_ERR(mem))
1878 return NULL;
1879 addr = (unsigned long)mem;
1880 } else {
1881 struct vmap_area *va;
1882 va = alloc_vmap_area(size, PAGE_SIZE,
1883 VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
1884 if (IS_ERR(va))
1885 return NULL;
1886
1887 addr = va->va_start;
1888 mem = (void *)addr;
1889 }
Andrey Ryabinind98c9e82019-12-17 20:51:38 -08001890
1891 kasan_unpoison_vmalloc(mem, size);
1892
Christoph Hellwigd4efd792020-06-01 21:51:27 -07001893 if (map_kernel_range(addr, size, PAGE_KERNEL, pages) < 0) {
Nick Piggindb64fe02008-10-18 20:27:03 -07001894 vm_unmap_ram(mem, count);
1895 return NULL;
1896 }
1897 return mem;
1898}
1899EXPORT_SYMBOL(vm_map_ram);
1900
Joonsoo Kim4341fa42013-04-29 15:07:39 -07001901static struct vm_struct *vmlist __initdata;
Mike Rapoport92eac162019-03-05 15:48:36 -08001902
Tejun Heof0aa6612009-02-20 16:29:08 +09001903/**
Nicolas Pitrebe9b7332011-08-25 00:24:21 -04001904 * vm_area_add_early - add vmap area early during boot
1905 * @vm: vm_struct to add
1906 *
1907 * This function is used to add fixed kernel vm area to vmlist before
1908 * vmalloc_init() is called. @vm->addr, @vm->size, and @vm->flags
1909 * should contain proper values and the other fields should be zero.
1910 *
1911 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1912 */
1913void __init vm_area_add_early(struct vm_struct *vm)
1914{
1915 struct vm_struct *tmp, **p;
1916
1917 BUG_ON(vmap_initialized);
1918 for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
1919 if (tmp->addr >= vm->addr) {
1920 BUG_ON(tmp->addr < vm->addr + vm->size);
1921 break;
1922 } else
1923 BUG_ON(tmp->addr + tmp->size > vm->addr);
1924 }
1925 vm->next = *p;
1926 *p = vm;
1927}
1928
1929/**
Tejun Heof0aa6612009-02-20 16:29:08 +09001930 * vm_area_register_early - register vmap area early during boot
1931 * @vm: vm_struct to register
Tejun Heoc0c0a292009-02-24 11:57:21 +09001932 * @align: requested alignment
Tejun Heof0aa6612009-02-20 16:29:08 +09001933 *
1934 * This function is used to register kernel vm area before
1935 * vmalloc_init() is called. @vm->size and @vm->flags should contain
1936 * proper values on entry and other fields should be zero. On return,
1937 * vm->addr contains the allocated address.
1938 *
1939 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1940 */
Tejun Heoc0c0a292009-02-24 11:57:21 +09001941void __init vm_area_register_early(struct vm_struct *vm, size_t align)
Tejun Heof0aa6612009-02-20 16:29:08 +09001942{
1943 static size_t vm_init_off __initdata;
Tejun Heoc0c0a292009-02-24 11:57:21 +09001944 unsigned long addr;
Tejun Heof0aa6612009-02-20 16:29:08 +09001945
Tejun Heoc0c0a292009-02-24 11:57:21 +09001946 addr = ALIGN(VMALLOC_START + vm_init_off, align);
1947 vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START;
1948
1949 vm->addr = (void *)addr;
Tejun Heof0aa6612009-02-20 16:29:08 +09001950
Nicolas Pitrebe9b7332011-08-25 00:24:21 -04001951 vm_area_add_early(vm);
Tejun Heof0aa6612009-02-20 16:29:08 +09001952}
1953
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001954static void vmap_init_free_space(void)
1955{
1956 unsigned long vmap_start = 1;
1957 const unsigned long vmap_end = ULONG_MAX;
1958 struct vmap_area *busy, *free;
1959
1960 /*
1961 * B F B B B F
1962 * -|-----|.....|-----|-----|-----|.....|-
1963 * | The KVA space |
1964 * |<--------------------------------->|
1965 */
1966 list_for_each_entry(busy, &vmap_area_list, list) {
1967 if (busy->va_start - vmap_start > 0) {
1968 free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
1969 if (!WARN_ON_ONCE(!free)) {
1970 free->va_start = vmap_start;
1971 free->va_end = busy->va_start;
1972
1973 insert_vmap_area_augment(free, NULL,
1974 &free_vmap_area_root,
1975 &free_vmap_area_list);
1976 }
1977 }
1978
1979 vmap_start = busy->va_end;
1980 }
1981
1982 if (vmap_end - vmap_start > 0) {
1983 free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
1984 if (!WARN_ON_ONCE(!free)) {
1985 free->va_start = vmap_start;
1986 free->va_end = vmap_end;
1987
1988 insert_vmap_area_augment(free, NULL,
1989 &free_vmap_area_root,
1990 &free_vmap_area_list);
1991 }
1992 }
1993}
1994
Nick Piggindb64fe02008-10-18 20:27:03 -07001995void __init vmalloc_init(void)
1996{
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08001997 struct vmap_area *va;
1998 struct vm_struct *tmp;
Nick Piggindb64fe02008-10-18 20:27:03 -07001999 int i;
2000
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07002001 /*
2002 * Create the cache for vmap_area objects.
2003 */
2004 vmap_area_cachep = KMEM_CACHE(vmap_area, SLAB_PANIC);
2005
Nick Piggindb64fe02008-10-18 20:27:03 -07002006 for_each_possible_cpu(i) {
2007 struct vmap_block_queue *vbq;
Al Viro32fcfd42013-03-10 20:14:08 -04002008 struct vfree_deferred *p;
Nick Piggindb64fe02008-10-18 20:27:03 -07002009
2010 vbq = &per_cpu(vmap_block_queue, i);
2011 spin_lock_init(&vbq->lock);
2012 INIT_LIST_HEAD(&vbq->free);
Al Viro32fcfd42013-03-10 20:14:08 -04002013 p = &per_cpu(vfree_deferred, i);
2014 init_llist_head(&p->list);
2015 INIT_WORK(&p->wq, free_work);
Nick Piggindb64fe02008-10-18 20:27:03 -07002016 }
Jeremy Fitzhardinge9b463332008-10-28 19:22:34 +11002017
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08002018 /* Import existing vmlist entries. */
2019 for (tmp = vmlist; tmp; tmp = tmp->next) {
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07002020 va = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
2021 if (WARN_ON_ONCE(!va))
2022 continue;
2023
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08002024 va->va_start = (unsigned long)tmp->addr;
2025 va->va_end = va->va_start + tmp->size;
KyongHodbda5912012-05-29 15:06:49 -07002026 va->vm = tmp;
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07002027 insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08002028 }
Tejun Heoca23e402009-08-14 15:00:52 +09002029
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07002030 /*
2031 * Now we can initialize a free vmap space.
2032 */
2033 vmap_init_free_space();
Jeremy Fitzhardinge9b463332008-10-28 19:22:34 +11002034 vmap_initialized = true;
Nick Piggindb64fe02008-10-18 20:27:03 -07002035}
2036
Tejun Heo8fc48982009-02-20 16:29:08 +09002037/**
Tejun Heo8fc48982009-02-20 16:29:08 +09002038 * unmap_kernel_range - unmap kernel VM area and flush cache and TLB
2039 * @addr: start of the VM area to unmap
2040 * @size: size of the VM area to unmap
2041 *
2042 * Similar to unmap_kernel_range_noflush() but flushes vcache before
2043 * the unmapping and tlb after.
2044 */
Nick Piggindb64fe02008-10-18 20:27:03 -07002045void unmap_kernel_range(unsigned long addr, unsigned long size)
2046{
2047 unsigned long end = addr + size;
Tejun Heof6fcba72009-02-20 15:38:48 -08002048
2049 flush_cache_vunmap(addr, end);
Christoph Hellwigb521c432020-06-01 21:51:07 -07002050 unmap_kernel_range_noflush(addr, size);
Nick Piggindb64fe02008-10-18 20:27:03 -07002051 flush_tlb_kernel_range(addr, end);
2052}
2053
Uladzislau Rezki (Sony)e36176b2019-11-30 17:54:47 -08002054static inline void setup_vmalloc_vm_locked(struct vm_struct *vm,
2055 struct vmap_area *va, unsigned long flags, const void *caller)
Tejun Heocf88c792009-08-14 15:00:52 +09002056{
Tejun Heocf88c792009-08-14 15:00:52 +09002057 vm->flags = flags;
2058 vm->addr = (void *)va->va_start;
2059 vm->size = va->va_end - va->va_start;
2060 vm->caller = caller;
Minchan Kimdb1aeca2012-01-10 15:08:39 -08002061 va->vm = vm;
Uladzislau Rezki (Sony)e36176b2019-11-30 17:54:47 -08002062}
2063
2064static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
2065 unsigned long flags, const void *caller)
2066{
2067 spin_lock(&vmap_area_lock);
2068 setup_vmalloc_vm_locked(vm, va, flags, caller);
Joonsoo Kimc69480a2013-04-29 15:07:30 -07002069 spin_unlock(&vmap_area_lock);
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07002070}
Tejun Heocf88c792009-08-14 15:00:52 +09002071
Zhang Yanfei20fc02b2013-07-08 15:59:58 -07002072static void clear_vm_uninitialized_flag(struct vm_struct *vm)
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07002073{
Joonsoo Kimd4033af2013-04-29 15:07:35 -07002074 /*
Zhang Yanfei20fc02b2013-07-08 15:59:58 -07002075 * Before removing VM_UNINITIALIZED,
Joonsoo Kimd4033af2013-04-29 15:07:35 -07002076 * we should make sure that vm has proper values.
2077 * Pair with smp_rmb() in show_numa_info().
2078 */
2079 smp_wmb();
Zhang Yanfei20fc02b2013-07-08 15:59:58 -07002080 vm->flags &= ~VM_UNINITIALIZED;
Tejun Heocf88c792009-08-14 15:00:52 +09002081}
2082
Nick Piggindb64fe02008-10-18 20:27:03 -07002083static struct vm_struct *__get_vm_area_node(unsigned long size,
David Miller2dca6992009-09-21 12:22:34 -07002084 unsigned long align, unsigned long flags, unsigned long start,
Marek Szyprowski5e6cafc2012-04-13 12:32:09 +02002085 unsigned long end, int node, gfp_t gfp_mask, const void *caller)
Nick Piggindb64fe02008-10-18 20:27:03 -07002086{
Kautuk Consul00065262011-12-19 17:12:04 -08002087 struct vmap_area *va;
Nick Piggindb64fe02008-10-18 20:27:03 -07002088 struct vm_struct *area;
Andrey Ryabinind98c9e82019-12-17 20:51:38 -08002089 unsigned long requested_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Giridhar Pemmasani52fd24c2006-10-28 10:38:34 -07002091 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 size = PAGE_ALIGN(size);
OGAWA Hirofumi31be8302006-11-16 01:19:29 -08002093 if (unlikely(!size))
2094 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
zijun_hu252e5c62016-10-07 16:57:26 -07002096 if (flags & VM_IOREMAP)
2097 align = 1ul << clamp_t(int, get_count_order_long(size),
2098 PAGE_SHIFT, IOREMAP_MAX_ORDER);
2099
Tejun Heocf88c792009-08-14 15:00:52 +09002100 area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 if (unlikely(!area))
2102 return NULL;
2103
Andrey Ryabinin71394fe2015-02-13 14:40:03 -08002104 if (!(flags & VM_NO_GUARD))
2105 size += PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
Nick Piggindb64fe02008-10-18 20:27:03 -07002107 va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
2108 if (IS_ERR(va)) {
2109 kfree(area);
2110 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Andrey Ryabinind98c9e82019-12-17 20:51:38 -08002113 kasan_unpoison_vmalloc((void *)va->va_start, requested_size);
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07002114
Andrey Ryabinind98c9e82019-12-17 20:51:38 -08002115 setup_vmalloc_vm(area, va, flags, caller);
Daniel Axtens3c5c3cf2019-11-30 17:54:50 -08002116
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 return area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118}
2119
Benjamin Herrenschmidtc2968612009-02-18 14:48:12 -08002120struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
2121 unsigned long start, unsigned long end,
Marek Szyprowski5e6cafc2012-04-13 12:32:09 +02002122 const void *caller)
Benjamin Herrenschmidtc2968612009-02-18 14:48:12 -08002123{
David Rientjes00ef2d22013-02-22 16:35:36 -08002124 return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE,
2125 GFP_KERNEL, caller);
Benjamin Herrenschmidtc2968612009-02-18 14:48:12 -08002126}
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002129 * get_vm_area - reserve a contiguous kernel virtual area
2130 * @size: size of the area
2131 * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002133 * Search an area of @size in the kernel virtual mapping area,
2134 * and reserved it for out purposes. Returns the area descriptor
2135 * on success or %NULL on failure.
Mike Rapoporta862f682019-03-05 15:48:42 -08002136 *
2137 * Return: the area descriptor on success or %NULL on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 */
2139struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
2140{
David Miller2dca6992009-09-21 12:22:34 -07002141 return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
David Rientjes00ef2d22013-02-22 16:35:36 -08002142 NUMA_NO_NODE, GFP_KERNEL,
2143 __builtin_return_address(0));
Christoph Lameter23016962008-04-28 02:12:42 -07002144}
2145
2146struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
Marek Szyprowski5e6cafc2012-04-13 12:32:09 +02002147 const void *caller)
Christoph Lameter23016962008-04-28 02:12:42 -07002148{
David Miller2dca6992009-09-21 12:22:34 -07002149 return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
David Rientjes00ef2d22013-02-22 16:35:36 -08002150 NUMA_NO_NODE, GFP_KERNEL, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151}
2152
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02002153/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002154 * find_vm_area - find a continuous kernel virtual area
2155 * @addr: base address
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02002156 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002157 * Search for the kernel VM area starting at @addr, and return it.
2158 * It is up to the caller to do all required locking to keep the returned
2159 * pointer valid.
Mike Rapoporta862f682019-03-05 15:48:42 -08002160 *
Hui Su74640612020-10-13 16:54:51 -07002161 * Return: the area descriptor on success or %NULL on failure.
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02002162 */
2163struct vm_struct *find_vm_area(const void *addr)
Nick Piggin83342312006-06-23 02:03:20 -07002164{
Nick Piggindb64fe02008-10-18 20:27:03 -07002165 struct vmap_area *va;
Nick Piggin83342312006-06-23 02:03:20 -07002166
Nick Piggindb64fe02008-10-18 20:27:03 -07002167 va = find_vmap_area((unsigned long)addr);
Pengfei Li688fcbf2019-09-23 15:36:39 -07002168 if (!va)
2169 return NULL;
Nick Piggin83342312006-06-23 02:03:20 -07002170
Pengfei Li688fcbf2019-09-23 15:36:39 -07002171 return va->vm;
Andi Kleen7856dfe2005-05-20 14:27:57 -07002172}
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002175 * remove_vm_area - find and remove a continuous kernel virtual area
2176 * @addr: base address
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002178 * Search for the kernel VM area starting at @addr, and remove it.
2179 * This function returns the found VM area, but using it is NOT safe
2180 * on SMP machines, except for its size or flags.
Mike Rapoporta862f682019-03-05 15:48:42 -08002181 *
Hui Su74640612020-10-13 16:54:51 -07002182 * Return: the area descriptor on success or %NULL on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -08002184struct vm_struct *remove_vm_area(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
Nick Piggindb64fe02008-10-18 20:27:03 -07002186 struct vmap_area *va;
2187
Christoph Hellwig5803ed22016-12-12 16:44:20 -08002188 might_sleep();
2189
Uladzislau Rezki (Sony)dd3b8352019-09-23 15:36:36 -07002190 spin_lock(&vmap_area_lock);
2191 va = __find_vmap_area((unsigned long)addr);
Pengfei Li688fcbf2019-09-23 15:36:39 -07002192 if (va && va->vm) {
Minchan Kimdb1aeca2012-01-10 15:08:39 -08002193 struct vm_struct *vm = va->vm;
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07002194
Joonsoo Kimc69480a2013-04-29 15:07:30 -07002195 va->vm = NULL;
Joonsoo Kimc69480a2013-04-29 15:07:30 -07002196 spin_unlock(&vmap_area_lock);
2197
Andrey Ryabinina5af5aa2015-03-12 16:26:11 -07002198 kasan_free_shadow(vm);
KAMEZAWA Hiroyukidd32c272009-09-21 17:02:32 -07002199 free_unmap_vmap_area(va);
KAMEZAWA Hiroyukidd32c272009-09-21 17:02:32 -07002200
Nick Piggindb64fe02008-10-18 20:27:03 -07002201 return vm;
2202 }
Uladzislau Rezki (Sony)dd3b8352019-09-23 15:36:36 -07002203
2204 spin_unlock(&vmap_area_lock);
Nick Piggindb64fe02008-10-18 20:27:03 -07002205 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206}
2207
Rick Edgecombe868b1042019-04-25 17:11:36 -07002208static inline void set_area_direct_map(const struct vm_struct *area,
2209 int (*set_direct_map)(struct page *page))
2210{
2211 int i;
2212
2213 for (i = 0; i < area->nr_pages; i++)
2214 if (page_address(area->pages[i]))
2215 set_direct_map(area->pages[i]);
2216}
2217
2218/* Handle removing and resetting vm mappings related to the vm_struct. */
2219static void vm_remove_mappings(struct vm_struct *area, int deallocate_pages)
2220{
Rick Edgecombe868b1042019-04-25 17:11:36 -07002221 unsigned long start = ULONG_MAX, end = 0;
2222 int flush_reset = area->flags & VM_FLUSH_RESET_PERMS;
Rick Edgecombe31e67342019-05-27 14:10:58 -07002223 int flush_dmap = 0;
Rick Edgecombe868b1042019-04-25 17:11:36 -07002224 int i;
2225
Rick Edgecombe868b1042019-04-25 17:11:36 -07002226 remove_vm_area(area->addr);
2227
2228 /* If this is not VM_FLUSH_RESET_PERMS memory, no need for the below. */
2229 if (!flush_reset)
2230 return;
2231
2232 /*
2233 * If not deallocating pages, just do the flush of the VM area and
2234 * return.
2235 */
2236 if (!deallocate_pages) {
2237 vm_unmap_aliases();
2238 return;
2239 }
2240
2241 /*
2242 * If execution gets here, flush the vm mapping and reset the direct
2243 * map. Find the start and end range of the direct mappings to make sure
2244 * the vm_unmap_aliases() flush includes the direct map.
2245 */
2246 for (i = 0; i < area->nr_pages; i++) {
Rick Edgecombe8e41f872019-05-27 14:10:57 -07002247 unsigned long addr = (unsigned long)page_address(area->pages[i]);
2248 if (addr) {
Rick Edgecombe868b1042019-04-25 17:11:36 -07002249 start = min(addr, start);
Rick Edgecombe8e41f872019-05-27 14:10:57 -07002250 end = max(addr + PAGE_SIZE, end);
Rick Edgecombe31e67342019-05-27 14:10:58 -07002251 flush_dmap = 1;
Rick Edgecombe868b1042019-04-25 17:11:36 -07002252 }
2253 }
2254
2255 /*
2256 * Set direct map to something invalid so that it won't be cached if
2257 * there are any accesses after the TLB flush, then flush the TLB and
2258 * reset the direct map permissions to the default.
2259 */
2260 set_area_direct_map(area, set_direct_map_invalid_noflush);
Rick Edgecombe31e67342019-05-27 14:10:58 -07002261 _vm_unmap_aliases(start, end, flush_dmap);
Rick Edgecombe868b1042019-04-25 17:11:36 -07002262 set_area_direct_map(area, set_direct_map_default_noflush);
2263}
2264
Christoph Lameterb3bdda02008-02-04 22:28:32 -08002265static void __vunmap(const void *addr, int deallocate_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266{
2267 struct vm_struct *area;
2268
2269 if (!addr)
2270 return;
2271
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002272 if (WARN(!PAGE_ALIGNED(addr), "Trying to vfree() bad address (%p)\n",
Dan Carpenterab15d9b2013-07-08 15:59:53 -07002273 addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Liviu Dudau6ade2032019-03-05 15:42:54 -08002276 area = find_vm_area(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 if (unlikely(!area)) {
Arjan van de Ven4c8573e2008-07-25 19:45:37 -07002278 WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 return;
2281 }
2282
Chintan Pandya05e3ff92018-06-07 17:06:53 -07002283 debug_check_no_locks_freed(area->addr, get_vm_area_size(area));
2284 debug_check_no_obj_freed(area->addr, get_vm_area_size(area));
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07002285
Vincenzo Frascinoc0410982020-12-14 19:09:06 -08002286 kasan_poison_vmalloc(area->addr, get_vm_area_size(area));
Daniel Axtens3c5c3cf2019-11-30 17:54:50 -08002287
Rick Edgecombe868b1042019-04-25 17:11:36 -07002288 vm_remove_mappings(area, deallocate_pages);
2289
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 if (deallocate_pages) {
2291 int i;
2292
2293 for (i = 0; i < area->nr_pages; i++) {
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08002294 struct page *page = area->pages[i];
2295
2296 BUG_ON(!page);
Vladimir Davydov49491482016-07-26 15:24:24 -07002297 __free_pages(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 }
Roman Gushchin97105f02019-07-11 21:00:13 -07002299 atomic_long_sub(area->nr_pages, &nr_vmalloc_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
David Rientjes244d63e2016-01-14 15:19:35 -08002301 kvfree(area->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 }
2303
2304 kfree(area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305}
Andrey Ryabininbf22e372016-12-12 16:44:10 -08002306
2307static inline void __vfree_deferred(const void *addr)
2308{
2309 /*
2310 * Use raw_cpu_ptr() because this can be called from preemptible
2311 * context. Preemption is absolutely fine here, because the llist_add()
2312 * implementation is lockless, so it works even if we are adding to
Jeongtae Park73221d82020-06-04 16:47:19 -07002313 * another cpu's list. schedule_work() should be fine with this too.
Andrey Ryabininbf22e372016-12-12 16:44:10 -08002314 */
2315 struct vfree_deferred *p = raw_cpu_ptr(&vfree_deferred);
2316
2317 if (llist_add((struct llist_node *)addr, &p->list))
2318 schedule_work(&p->wq);
2319}
2320
2321/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002322 * vfree_atomic - release memory allocated by vmalloc()
2323 * @addr: memory base address
Andrey Ryabininbf22e372016-12-12 16:44:10 -08002324 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002325 * This one is just like vfree() but can be called in any atomic context
2326 * except NMIs.
Andrey Ryabininbf22e372016-12-12 16:44:10 -08002327 */
2328void vfree_atomic(const void *addr)
2329{
2330 BUG_ON(in_nmi());
2331
2332 kmemleak_free(addr);
2333
2334 if (!addr)
2335 return;
2336 __vfree_deferred(addr);
2337}
2338
Roman Penyaevc67dc622019-03-05 15:43:24 -08002339static void __vfree(const void *addr)
2340{
2341 if (unlikely(in_interrupt()))
2342 __vfree_deferred(addr);
2343 else
2344 __vunmap(addr, 1);
2345}
2346
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347/**
Matthew Wilcox (Oracle)fa307472020-10-17 16:15:03 -07002348 * vfree - Release memory allocated by vmalloc()
2349 * @addr: Memory base address
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 *
Matthew Wilcox (Oracle)fa307472020-10-17 16:15:03 -07002351 * Free the virtually continuous memory area starting at @addr, as obtained
2352 * from one of the vmalloc() family of APIs. This will usually also free the
2353 * physical memory underlying the virtual allocation, but that memory is
2354 * reference counted, so it will not be freed until the last user goes away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 *
Matthew Wilcox (Oracle)fa307472020-10-17 16:15:03 -07002356 * If @addr is NULL, no operation is performed.
Andrew Mortonc9fcee52013-05-07 16:18:18 -07002357 *
Matthew Wilcox (Oracle)fa307472020-10-17 16:15:03 -07002358 * Context:
Mike Rapoport92eac162019-03-05 15:48:36 -08002359 * May sleep if called *not* from interrupt context.
Matthew Wilcox (Oracle)fa307472020-10-17 16:15:03 -07002360 * Must not be called in NMI context (strictly speaking, it could be
2361 * if we have CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG, but making the calling
2362 * conventions for vfree() arch-depenedent would be a really bad idea).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -08002364void vfree(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365{
Al Viro32fcfd42013-03-10 20:14:08 -04002366 BUG_ON(in_nmi());
Catalin Marinas89219d32009-06-11 13:23:19 +01002367
2368 kmemleak_free(addr);
2369
Andrey Ryabinina8dda162018-10-26 15:07:07 -07002370 might_sleep_if(!in_interrupt());
2371
Al Viro32fcfd42013-03-10 20:14:08 -04002372 if (!addr)
2373 return;
Roman Penyaevc67dc622019-03-05 15:43:24 -08002374
2375 __vfree(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377EXPORT_SYMBOL(vfree);
2378
2379/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002380 * vunmap - release virtual mapping obtained by vmap()
2381 * @addr: memory base address
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002383 * Free the virtually contiguous memory area starting at @addr,
2384 * which was created from the page array passed to vmap().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002386 * Must not be called in interrupt context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -08002388void vunmap(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389{
2390 BUG_ON(in_interrupt());
Peter Zijlstra34754b62009-02-25 16:04:03 +01002391 might_sleep();
Al Viro32fcfd42013-03-10 20:14:08 -04002392 if (addr)
2393 __vunmap(addr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395EXPORT_SYMBOL(vunmap);
2396
2397/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002398 * vmap - map an array of pages into virtually contiguous space
2399 * @pages: array of page pointers
2400 * @count: number of pages to map
2401 * @flags: vm_area->flags
2402 * @prot: page protection for the mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 *
Christoph Hellwigb944afc2020-10-17 16:15:06 -07002404 * Maps @count pages from @pages into contiguous kernel virtual space.
2405 * If @flags contains %VM_MAP_PUT_PAGES the ownership of the pages array itself
2406 * (which must be kmalloc or vmalloc memory) and one reference per pages in it
2407 * are transferred from the caller to vmap(), and will be freed / dropped when
2408 * vfree() is called on the return value.
Mike Rapoporta862f682019-03-05 15:48:42 -08002409 *
2410 * Return: the address of the area or %NULL on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 */
2412void *vmap(struct page **pages, unsigned int count,
Mike Rapoport92eac162019-03-05 15:48:36 -08002413 unsigned long flags, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414{
2415 struct vm_struct *area;
Guillermo Julián Moreno65ee03c2016-06-03 14:55:33 -07002416 unsigned long size; /* In bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
Peter Zijlstra34754b62009-02-25 16:04:03 +01002418 might_sleep();
2419
Arun KSca79b0c2018-12-28 00:34:29 -08002420 if (count > totalram_pages())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 return NULL;
2422
Guillermo Julián Moreno65ee03c2016-06-03 14:55:33 -07002423 size = (unsigned long)count << PAGE_SHIFT;
2424 area = get_vm_area_caller(size, flags, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 if (!area)
2426 return NULL;
Christoph Lameter23016962008-04-28 02:12:42 -07002427
Christoph Hellwigcca98e92020-06-01 21:51:32 -07002428 if (map_kernel_range((unsigned long)area->addr, size, pgprot_nx(prot),
Christoph Hellwiged1f3242020-06-01 21:51:19 -07002429 pages) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 vunmap(area->addr);
2431 return NULL;
2432 }
2433
Miaohe Linc22ee522021-01-12 15:49:18 -08002434 if (flags & VM_MAP_PUT_PAGES) {
Christoph Hellwigb944afc2020-10-17 16:15:06 -07002435 area->pages = pages;
Miaohe Linc22ee522021-01-12 15:49:18 -08002436 area->nr_pages = count;
2437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 return area->addr;
2439}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440EXPORT_SYMBOL(vmap);
2441
Christoph Hellwig3e9a9e22020-10-17 16:15:10 -07002442#ifdef CONFIG_VMAP_PFN
2443struct vmap_pfn_data {
2444 unsigned long *pfns;
2445 pgprot_t prot;
2446 unsigned int idx;
2447};
2448
2449static int vmap_pfn_apply(pte_t *pte, unsigned long addr, void *private)
2450{
2451 struct vmap_pfn_data *data = private;
2452
2453 if (WARN_ON_ONCE(pfn_valid(data->pfns[data->idx])))
2454 return -EINVAL;
2455 *pte = pte_mkspecial(pfn_pte(data->pfns[data->idx++], data->prot));
2456 return 0;
2457}
2458
2459/**
2460 * vmap_pfn - map an array of PFNs into virtually contiguous space
2461 * @pfns: array of PFNs
2462 * @count: number of pages to map
2463 * @prot: page protection for the mapping
2464 *
2465 * Maps @count PFNs from @pfns into contiguous kernel virtual space and returns
2466 * the start address of the mapping.
2467 */
2468void *vmap_pfn(unsigned long *pfns, unsigned int count, pgprot_t prot)
2469{
2470 struct vmap_pfn_data data = { .pfns = pfns, .prot = pgprot_nx(prot) };
2471 struct vm_struct *area;
2472
2473 area = get_vm_area_caller(count * PAGE_SIZE, VM_IOREMAP,
2474 __builtin_return_address(0));
2475 if (!area)
2476 return NULL;
2477 if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
2478 count * PAGE_SIZE, vmap_pfn_apply, &data)) {
2479 free_vm_area(area);
2480 return NULL;
2481 }
2482 return area->addr;
2483}
2484EXPORT_SYMBOL_GPL(vmap_pfn);
2485#endif /* CONFIG_VMAP_PFN */
2486
Adrian Bunke31d9eb2008-02-04 22:29:09 -08002487static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
Wanpeng Li3722e132013-11-12 15:07:29 -08002488 pgprot_t prot, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489{
David Rientjes930f0362014-08-06 16:06:28 -07002490 const gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO;
Christoph Hellwigf2559352020-10-17 16:15:43 -07002491 unsigned int nr_pages = get_vm_area_size(area) >> PAGE_SHIFT;
Andrew Morton34fe6532020-12-14 19:08:43 -08002492 unsigned long array_size;
2493 unsigned int i;
Christoph Hellwigf2559352020-10-17 16:15:43 -07002494 struct page **pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
Andrew Morton34fe6532020-12-14 19:08:43 -08002496 array_size = (unsigned long)nr_pages * sizeof(struct page *);
Christoph Hellwigf2559352020-10-17 16:15:43 -07002497 gfp_mask |= __GFP_NOWARN;
2498 if (!(gfp_mask & (GFP_DMA | GFP_DMA32)))
2499 gfp_mask |= __GFP_HIGHMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 /* Please note that the recursion is strictly bounded. */
Jan Kiszka8757d5f2006-07-14 00:23:56 -07002502 if (array_size > PAGE_SIZE) {
Christoph Hellwigf2559352020-10-17 16:15:43 -07002503 pages = __vmalloc_node(array_size, 1, nested_gfp, node,
2504 area->caller);
Andrew Morton286e1ea2006-10-17 00:09:57 -07002505 } else {
Jan Beulich976d6df2009-12-14 17:58:39 -08002506 pages = kmalloc_node(array_size, nested_gfp, node);
Andrew Morton286e1ea2006-10-17 00:09:57 -07002507 }
Austin Kim7ea362422019-09-23 15:36:42 -07002508
2509 if (!pages) {
Uladzislau Rezki (Sony)8945a722020-12-14 19:08:46 -08002510 free_vm_area(area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 return NULL;
2512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
Austin Kim7ea362422019-09-23 15:36:42 -07002514 area->pages = pages;
2515 area->nr_pages = nr_pages;
2516
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 for (i = 0; i < area->nr_pages; i++) {
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08002518 struct page *page;
2519
Jianguo Wu4b909512013-11-12 15:07:11 -08002520 if (node == NUMA_NO_NODE)
Christoph Hellwigf2559352020-10-17 16:15:43 -07002521 page = alloc_page(gfp_mask);
Christoph Lameter930fc452005-10-29 18:15:41 -07002522 else
Christoph Hellwigf2559352020-10-17 16:15:43 -07002523 page = alloc_pages_node(node, gfp_mask, 0);
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08002524
2525 if (unlikely(!page)) {
Hui Su82afbc32020-10-13 16:54:48 -07002526 /* Successfully allocated i pages, free them in __vfree() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 area->nr_pages = i;
Roman Gushchin97105f02019-07-11 21:00:13 -07002528 atomic_long_add(area->nr_pages, &nr_vmalloc_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 goto fail;
2530 }
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08002531 area->pages[i] = page;
Liu Xiangdcf61ff2019-11-30 17:54:30 -08002532 if (gfpflags_allow_blocking(gfp_mask))
Eric Dumazet660654f2014-08-06 16:06:25 -07002533 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 }
Roman Gushchin97105f02019-07-11 21:00:13 -07002535 atomic_long_add(area->nr_pages, &nr_vmalloc_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
Christoph Hellwiged1f3242020-06-01 21:51:19 -07002537 if (map_kernel_range((unsigned long)area->addr, get_vm_area_size(area),
2538 prot, pages) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 goto fail;
Christoph Hellwiged1f3242020-06-01 21:51:19 -07002540
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 return area->addr;
2542
2543fail:
Michal Hockoa8e99252017-02-22 15:46:10 -08002544 warn_alloc(gfp_mask, NULL,
Michal Hocko7877cdc2016-10-07 17:01:55 -07002545 "vmalloc: allocation failure, allocated %ld of %ld bytes",
Dave Hansen22943ab2011-05-24 17:12:18 -07002546 (area->nr_pages*PAGE_SIZE), area->size);
Roman Penyaevc67dc622019-03-05 15:43:24 -08002547 __vfree(area->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 return NULL;
2549}
2550
David Rientjesd0a21262011-01-13 15:46:02 -08002551/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002552 * __vmalloc_node_range - allocate virtually contiguous memory
2553 * @size: allocation size
2554 * @align: desired alignment
2555 * @start: vm area range start
2556 * @end: vm area range end
2557 * @gfp_mask: flags for the page level allocator
2558 * @prot: protection mask for the allocated pages
2559 * @vm_flags: additional vm area flags (e.g. %VM_NO_GUARD)
2560 * @node: node to use for allocation or NUMA_NO_NODE
2561 * @caller: caller's return address
David Rientjesd0a21262011-01-13 15:46:02 -08002562 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002563 * Allocate enough pages to cover @size from the page level
2564 * allocator with @gfp_mask flags. Map them into contiguous
2565 * kernel virtual space, using a pagetable protection of @prot.
Mike Rapoporta862f682019-03-05 15:48:42 -08002566 *
2567 * Return: the address of the area or %NULL on failure
David Rientjesd0a21262011-01-13 15:46:02 -08002568 */
2569void *__vmalloc_node_range(unsigned long size, unsigned long align,
2570 unsigned long start, unsigned long end, gfp_t gfp_mask,
Andrey Ryabinincb9e3c22015-02-13 14:40:07 -08002571 pgprot_t prot, unsigned long vm_flags, int node,
2572 const void *caller)
Christoph Lameter930fc452005-10-29 18:15:41 -07002573{
David Rientjesd0a21262011-01-13 15:46:02 -08002574 struct vm_struct *area;
2575 void *addr;
2576 unsigned long real_size = size;
2577
2578 size = PAGE_ALIGN(size);
Arun KSca79b0c2018-12-28 00:34:29 -08002579 if (!size || (size >> PAGE_SHIFT) > totalram_pages())
Joe Perchesde7d2b52011-10-31 17:08:48 -07002580 goto fail;
David Rientjesd0a21262011-01-13 15:46:02 -08002581
Andrey Ryabinind98c9e82019-12-17 20:51:38 -08002582 area = __get_vm_area_node(real_size, align, VM_ALLOC | VM_UNINITIALIZED |
Andrey Ryabinincb9e3c22015-02-13 14:40:07 -08002583 vm_flags, start, end, node, gfp_mask, caller);
David Rientjesd0a21262011-01-13 15:46:02 -08002584 if (!area)
Joe Perchesde7d2b52011-10-31 17:08:48 -07002585 goto fail;
David Rientjesd0a21262011-01-13 15:46:02 -08002586
Wanpeng Li3722e132013-11-12 15:07:29 -08002587 addr = __vmalloc_area_node(area, gfp_mask, prot, node);
Mel Gorman1368edf2011-12-08 14:34:30 -08002588 if (!addr)
Wanpeng Lib82225f32013-11-12 15:07:33 -08002589 return NULL;
Catalin Marinas89219d32009-06-11 13:23:19 +01002590
2591 /*
Zhang Yanfei20fc02b2013-07-08 15:59:58 -07002592 * In this function, newly allocated vm_struct has VM_UNINITIALIZED
2593 * flag. It means that vm_struct is not fully initialized.
Joonsoo Kim4341fa42013-04-29 15:07:39 -07002594 * Now, it is fully initialized, so remove this flag here.
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07002595 */
Zhang Yanfei20fc02b2013-07-08 15:59:58 -07002596 clear_vm_uninitialized_flag(area);
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07002597
Catalin Marinas94f4a162017-07-06 15:40:22 -07002598 kmemleak_vmalloc(area, size, gfp_mask);
Catalin Marinas89219d32009-06-11 13:23:19 +01002599
2600 return addr;
Joe Perchesde7d2b52011-10-31 17:08:48 -07002601
2602fail:
Michal Hockoa8e99252017-02-22 15:46:10 -08002603 warn_alloc(gfp_mask, NULL,
Michal Hocko7877cdc2016-10-07 17:01:55 -07002604 "vmalloc: allocation failure: %lu bytes", real_size);
Joe Perchesde7d2b52011-10-31 17:08:48 -07002605 return NULL;
Christoph Lameter930fc452005-10-29 18:15:41 -07002606}
2607
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002609 * __vmalloc_node - allocate virtually contiguous memory
2610 * @size: allocation size
2611 * @align: desired alignment
2612 * @gfp_mask: flags for the page level allocator
Mike Rapoport92eac162019-03-05 15:48:36 -08002613 * @node: node to use for allocation or NUMA_NO_NODE
2614 * @caller: caller's return address
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 *
Christoph Hellwigf38fcb92020-06-01 21:51:45 -07002616 * Allocate enough pages to cover @size from the page level allocator with
2617 * @gfp_mask flags. Map them into contiguous kernel virtual space.
Michal Hockoa7c3e902017-05-08 15:57:09 -07002618 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002619 * Reclaim modifiers in @gfp_mask - __GFP_NORETRY, __GFP_RETRY_MAYFAIL
2620 * and __GFP_NOFAIL are not supported
Michal Hockoa7c3e902017-05-08 15:57:09 -07002621 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002622 * Any use of gfp flags outside of GFP_KERNEL should be consulted
2623 * with mm people.
Mike Rapoporta862f682019-03-05 15:48:42 -08002624 *
2625 * Return: pointer to the allocated memory or %NULL on error
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 */
Christoph Hellwig2b905942020-06-01 21:51:53 -07002627void *__vmalloc_node(unsigned long size, unsigned long align,
Christoph Hellwigf38fcb92020-06-01 21:51:45 -07002628 gfp_t gfp_mask, int node, const void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629{
David Rientjesd0a21262011-01-13 15:46:02 -08002630 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
Christoph Hellwigf38fcb92020-06-01 21:51:45 -07002631 gfp_mask, PAGE_KERNEL, 0, node, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632}
Christoph Hellwigc3f896d2020-06-01 21:51:57 -07002633/*
2634 * This is only for performance analysis of vmalloc and stress purpose.
2635 * It is required by vmalloc test module, therefore do not use it other
2636 * than that.
2637 */
2638#ifdef CONFIG_TEST_VMALLOC_MODULE
2639EXPORT_SYMBOL_GPL(__vmalloc_node);
2640#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Christoph Hellwig88dca4c2020-06-01 21:51:40 -07002642void *__vmalloc(unsigned long size, gfp_t gfp_mask)
Christoph Lameter930fc452005-10-29 18:15:41 -07002643{
Christoph Hellwigf38fcb92020-06-01 21:51:45 -07002644 return __vmalloc_node(size, 1, gfp_mask, NUMA_NO_NODE,
Christoph Lameter23016962008-04-28 02:12:42 -07002645 __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -07002646}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647EXPORT_SYMBOL(__vmalloc);
2648
2649/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002650 * vmalloc - allocate virtually contiguous memory
2651 * @size: allocation size
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002653 * Allocate enough pages to cover @size from the page level
2654 * allocator and map them into contiguous kernel virtual space.
2655 *
2656 * For tight control over page level allocator and protection flags
2657 * use __vmalloc() instead.
Mike Rapoporta862f682019-03-05 15:48:42 -08002658 *
2659 * Return: pointer to the allocated memory or %NULL on error
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 */
2661void *vmalloc(unsigned long size)
2662{
Christoph Hellwig4d39d722020-06-01 21:51:49 -07002663 return __vmalloc_node(size, 1, GFP_KERNEL, NUMA_NO_NODE,
2664 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666EXPORT_SYMBOL(vmalloc);
2667
Christoph Lameter930fc452005-10-29 18:15:41 -07002668/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002669 * vzalloc - allocate virtually contiguous memory with zero fill
2670 * @size: allocation size
Dave Younge1ca7782010-10-26 14:22:06 -07002671 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002672 * Allocate enough pages to cover @size from the page level
2673 * allocator and map them into contiguous kernel virtual space.
2674 * The memory allocated is set to zero.
2675 *
2676 * For tight control over page level allocator and protection flags
2677 * use __vmalloc() instead.
Mike Rapoporta862f682019-03-05 15:48:42 -08002678 *
2679 * Return: pointer to the allocated memory or %NULL on error
Dave Younge1ca7782010-10-26 14:22:06 -07002680 */
2681void *vzalloc(unsigned long size)
2682{
Christoph Hellwig4d39d722020-06-01 21:51:49 -07002683 return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_ZERO, NUMA_NO_NODE,
2684 __builtin_return_address(0));
Dave Younge1ca7782010-10-26 14:22:06 -07002685}
2686EXPORT_SYMBOL(vzalloc);
2687
2688/**
Rolf Eike Beeread04082006-09-27 01:50:13 -07002689 * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
2690 * @size: allocation size
Nick Piggin83342312006-06-23 02:03:20 -07002691 *
Rolf Eike Beeread04082006-09-27 01:50:13 -07002692 * The resulting memory area is zeroed so it can be mapped to userspace
2693 * without leaking data.
Mike Rapoporta862f682019-03-05 15:48:42 -08002694 *
2695 * Return: pointer to the allocated memory or %NULL on error
Nick Piggin83342312006-06-23 02:03:20 -07002696 */
2697void *vmalloc_user(unsigned long size)
2698{
Roman Penyaevbc84c532019-03-05 15:43:27 -08002699 return __vmalloc_node_range(size, SHMLBA, VMALLOC_START, VMALLOC_END,
2700 GFP_KERNEL | __GFP_ZERO, PAGE_KERNEL,
2701 VM_USERMAP, NUMA_NO_NODE,
2702 __builtin_return_address(0));
Nick Piggin83342312006-06-23 02:03:20 -07002703}
2704EXPORT_SYMBOL(vmalloc_user);
2705
2706/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002707 * vmalloc_node - allocate memory on a specific node
2708 * @size: allocation size
2709 * @node: numa node
Christoph Lameter930fc452005-10-29 18:15:41 -07002710 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002711 * Allocate enough pages to cover @size from the page level
2712 * allocator and map them into contiguous kernel virtual space.
Christoph Lameter930fc452005-10-29 18:15:41 -07002713 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002714 * For tight control over page level allocator and protection flags
2715 * use __vmalloc() instead.
Mike Rapoporta862f682019-03-05 15:48:42 -08002716 *
2717 * Return: pointer to the allocated memory or %NULL on error
Christoph Lameter930fc452005-10-29 18:15:41 -07002718 */
2719void *vmalloc_node(unsigned long size, int node)
2720{
Christoph Hellwigf38fcb92020-06-01 21:51:45 -07002721 return __vmalloc_node(size, 1, GFP_KERNEL, node,
2722 __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -07002723}
2724EXPORT_SYMBOL(vmalloc_node);
2725
Dave Younge1ca7782010-10-26 14:22:06 -07002726/**
2727 * vzalloc_node - allocate memory on a specific node with zero fill
2728 * @size: allocation size
2729 * @node: numa node
2730 *
2731 * Allocate enough pages to cover @size from the page level
2732 * allocator and map them into contiguous kernel virtual space.
2733 * The memory allocated is set to zero.
2734 *
Mike Rapoporta862f682019-03-05 15:48:42 -08002735 * Return: pointer to the allocated memory or %NULL on error
Dave Younge1ca7782010-10-26 14:22:06 -07002736 */
2737void *vzalloc_node(unsigned long size, int node)
2738{
Christoph Hellwig4d39d722020-06-01 21:51:49 -07002739 return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_ZERO, node,
2740 __builtin_return_address(0));
Dave Younge1ca7782010-10-26 14:22:06 -07002741}
2742EXPORT_SYMBOL(vzalloc_node);
2743
Andi Kleen0d08e0d2007-05-02 19:27:12 +02002744#if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
Michal Hocko698d0832018-02-21 14:46:01 -08002745#define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL)
Andi Kleen0d08e0d2007-05-02 19:27:12 +02002746#elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
Michal Hocko698d0832018-02-21 14:46:01 -08002747#define GFP_VMALLOC32 (GFP_DMA | GFP_KERNEL)
Andi Kleen0d08e0d2007-05-02 19:27:12 +02002748#else
Michal Hocko698d0832018-02-21 14:46:01 -08002749/*
2750 * 64b systems should always have either DMA or DMA32 zones. For others
2751 * GFP_DMA32 should do the right thing and use the normal zone.
2752 */
2753#define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
Andi Kleen0d08e0d2007-05-02 19:27:12 +02002754#endif
2755
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002757 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
2758 * @size: allocation size
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002760 * Allocate enough 32bit PA addressable pages to cover @size from the
2761 * page level allocator and map them into contiguous kernel virtual space.
Mike Rapoporta862f682019-03-05 15:48:42 -08002762 *
2763 * Return: pointer to the allocated memory or %NULL on error
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 */
2765void *vmalloc_32(unsigned long size)
2766{
Christoph Hellwigf38fcb92020-06-01 21:51:45 -07002767 return __vmalloc_node(size, 1, GFP_VMALLOC32, NUMA_NO_NODE,
2768 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770EXPORT_SYMBOL(vmalloc_32);
2771
Nick Piggin83342312006-06-23 02:03:20 -07002772/**
Rolf Eike Beeread04082006-09-27 01:50:13 -07002773 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
Mike Rapoport92eac162019-03-05 15:48:36 -08002774 * @size: allocation size
Rolf Eike Beeread04082006-09-27 01:50:13 -07002775 *
2776 * The resulting memory area is 32bit addressable and zeroed so it can be
2777 * mapped to userspace without leaking data.
Mike Rapoporta862f682019-03-05 15:48:42 -08002778 *
2779 * Return: pointer to the allocated memory or %NULL on error
Nick Piggin83342312006-06-23 02:03:20 -07002780 */
2781void *vmalloc_32_user(unsigned long size)
2782{
Roman Penyaevbc84c532019-03-05 15:43:27 -08002783 return __vmalloc_node_range(size, SHMLBA, VMALLOC_START, VMALLOC_END,
2784 GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
2785 VM_USERMAP, NUMA_NO_NODE,
2786 __builtin_return_address(0));
Nick Piggin83342312006-06-23 02:03:20 -07002787}
2788EXPORT_SYMBOL(vmalloc_32_user);
2789
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002790/*
2791 * small helper routine , copy contents to buf from addr.
2792 * If the page is not present, fill zero.
2793 */
2794
2795static int aligned_vread(char *buf, char *addr, unsigned long count)
2796{
2797 struct page *p;
2798 int copied = 0;
2799
2800 while (count) {
2801 unsigned long offset, length;
2802
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08002803 offset = offset_in_page(addr);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002804 length = PAGE_SIZE - offset;
2805 if (length > count)
2806 length = count;
2807 p = vmalloc_to_page(addr);
2808 /*
2809 * To do safe access to this _mapped_ area, we need
2810 * lock. But adding lock here means that we need to add
2811 * overhead of vmalloc()/vfree() calles for this _debug_
2812 * interface, rarely used. Instead of that, we'll use
2813 * kmap() and get small overhead in this access function.
2814 */
2815 if (p) {
2816 /*
2817 * we can expect USER0 is not used (see vread/vwrite's
2818 * function description)
2819 */
Cong Wang9b04c5f2011-11-25 23:14:39 +08002820 void *map = kmap_atomic(p);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002821 memcpy(buf, map + offset, length);
Cong Wang9b04c5f2011-11-25 23:14:39 +08002822 kunmap_atomic(map);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002823 } else
2824 memset(buf, 0, length);
2825
2826 addr += length;
2827 buf += length;
2828 copied += length;
2829 count -= length;
2830 }
2831 return copied;
2832}
2833
2834static int aligned_vwrite(char *buf, char *addr, unsigned long count)
2835{
2836 struct page *p;
2837 int copied = 0;
2838
2839 while (count) {
2840 unsigned long offset, length;
2841
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08002842 offset = offset_in_page(addr);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002843 length = PAGE_SIZE - offset;
2844 if (length > count)
2845 length = count;
2846 p = vmalloc_to_page(addr);
2847 /*
2848 * To do safe access to this _mapped_ area, we need
2849 * lock. But adding lock here means that we need to add
2850 * overhead of vmalloc()/vfree() calles for this _debug_
2851 * interface, rarely used. Instead of that, we'll use
2852 * kmap() and get small overhead in this access function.
2853 */
2854 if (p) {
2855 /*
2856 * we can expect USER0 is not used (see vread/vwrite's
2857 * function description)
2858 */
Cong Wang9b04c5f2011-11-25 23:14:39 +08002859 void *map = kmap_atomic(p);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002860 memcpy(map + offset, buf, length);
Cong Wang9b04c5f2011-11-25 23:14:39 +08002861 kunmap_atomic(map);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002862 }
2863 addr += length;
2864 buf += length;
2865 copied += length;
2866 count -= length;
2867 }
2868 return copied;
2869}
2870
2871/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002872 * vread() - read vmalloc area in a safe way.
2873 * @buf: buffer for reading data
2874 * @addr: vm address.
2875 * @count: number of bytes to be read.
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002876 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002877 * This function checks that addr is a valid vmalloc'ed area, and
2878 * copy data from that area to a given buffer. If the given memory range
2879 * of [addr...addr+count) includes some valid address, data is copied to
2880 * proper area of @buf. If there are memory holes, they'll be zero-filled.
2881 * IOREMAP area is treated as memory hole and no copy is done.
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002882 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002883 * If [addr...addr+count) doesn't includes any intersects with alive
2884 * vm_struct area, returns 0. @buf should be kernel's buffer.
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002885 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002886 * Note: In usual ops, vread() is never necessary because the caller
2887 * should know vmalloc() area is valid and can use memcpy().
2888 * This is for routines which have to access vmalloc area without
Geert Uytterhoevend9009d62019-07-11 20:59:06 -07002889 * any information, as /dev/kmem.
Mike Rapoporta862f682019-03-05 15:48:42 -08002890 *
2891 * Return: number of bytes for which addr and buf should be increased
2892 * (same number as @count) or %0 if [addr...addr+count) doesn't
2893 * include any intersection with valid vmalloc area
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002894 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895long vread(char *buf, char *addr, unsigned long count)
2896{
Joonsoo Kime81ce852013-04-29 15:07:32 -07002897 struct vmap_area *va;
2898 struct vm_struct *vm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 char *vaddr, *buf_start = buf;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002900 unsigned long buflen = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 unsigned long n;
2902
2903 /* Don't allow overflow */
2904 if ((unsigned long) addr + count < count)
2905 count = -(unsigned long) addr;
2906
Joonsoo Kime81ce852013-04-29 15:07:32 -07002907 spin_lock(&vmap_area_lock);
Serapheim Dimitropoulosf6087882021-04-29 22:58:07 -07002908 va = __find_vmap_area((unsigned long)addr);
2909 if (!va)
2910 goto finished;
2911 list_for_each_entry_from(va, &vmap_area_list, list) {
Joonsoo Kime81ce852013-04-29 15:07:32 -07002912 if (!count)
2913 break;
2914
Pengfei Li688fcbf2019-09-23 15:36:39 -07002915 if (!va->vm)
Joonsoo Kime81ce852013-04-29 15:07:32 -07002916 continue;
2917
2918 vm = va->vm;
2919 vaddr = (char *) vm->addr;
Wanpeng Li762216a2013-09-11 14:22:42 -07002920 if (addr >= vaddr + get_vm_area_size(vm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 continue;
2922 while (addr < vaddr) {
2923 if (count == 0)
2924 goto finished;
2925 *buf = '\0';
2926 buf++;
2927 addr++;
2928 count--;
2929 }
Wanpeng Li762216a2013-09-11 14:22:42 -07002930 n = vaddr + get_vm_area_size(vm) - addr;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002931 if (n > count)
2932 n = count;
Joonsoo Kime81ce852013-04-29 15:07:32 -07002933 if (!(vm->flags & VM_IOREMAP))
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002934 aligned_vread(buf, addr, n);
2935 else /* IOREMAP area is treated as memory hole */
2936 memset(buf, 0, n);
2937 buf += n;
2938 addr += n;
2939 count -= n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 }
2941finished:
Joonsoo Kime81ce852013-04-29 15:07:32 -07002942 spin_unlock(&vmap_area_lock);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002943
2944 if (buf == buf_start)
2945 return 0;
2946 /* zero-fill memory holes */
2947 if (buf != buf_start + buflen)
2948 memset(buf, 0, buflen - (buf - buf_start));
2949
2950 return buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951}
2952
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002953/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002954 * vwrite() - write vmalloc area in a safe way.
2955 * @buf: buffer for source data
2956 * @addr: vm address.
2957 * @count: number of bytes to be read.
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002958 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002959 * This function checks that addr is a valid vmalloc'ed area, and
2960 * copy data from a buffer to the given addr. If specified range of
2961 * [addr...addr+count) includes some valid address, data is copied from
2962 * proper area of @buf. If there are memory holes, no copy to hole.
2963 * IOREMAP area is treated as memory hole and no copy is done.
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002964 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002965 * If [addr...addr+count) doesn't includes any intersects with alive
2966 * vm_struct area, returns 0. @buf should be kernel's buffer.
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002967 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002968 * Note: In usual ops, vwrite() is never necessary because the caller
2969 * should know vmalloc() area is valid and can use memcpy().
2970 * This is for routines which have to access vmalloc area without
Geert Uytterhoevend9009d62019-07-11 20:59:06 -07002971 * any information, as /dev/kmem.
Mike Rapoporta862f682019-03-05 15:48:42 -08002972 *
2973 * Return: number of bytes for which addr and buf should be
2974 * increased (same number as @count) or %0 if [addr...addr+count)
2975 * doesn't include any intersection with valid vmalloc area
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002976 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977long vwrite(char *buf, char *addr, unsigned long count)
2978{
Joonsoo Kime81ce852013-04-29 15:07:32 -07002979 struct vmap_area *va;
2980 struct vm_struct *vm;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002981 char *vaddr;
2982 unsigned long n, buflen;
2983 int copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
2985 /* Don't allow overflow */
2986 if ((unsigned long) addr + count < count)
2987 count = -(unsigned long) addr;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002988 buflen = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
Joonsoo Kime81ce852013-04-29 15:07:32 -07002990 spin_lock(&vmap_area_lock);
2991 list_for_each_entry(va, &vmap_area_list, list) {
2992 if (!count)
2993 break;
2994
Pengfei Li688fcbf2019-09-23 15:36:39 -07002995 if (!va->vm)
Joonsoo Kime81ce852013-04-29 15:07:32 -07002996 continue;
2997
2998 vm = va->vm;
2999 vaddr = (char *) vm->addr;
Wanpeng Li762216a2013-09-11 14:22:42 -07003000 if (addr >= vaddr + get_vm_area_size(vm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 continue;
3002 while (addr < vaddr) {
3003 if (count == 0)
3004 goto finished;
3005 buf++;
3006 addr++;
3007 count--;
3008 }
Wanpeng Li762216a2013-09-11 14:22:42 -07003009 n = vaddr + get_vm_area_size(vm) - addr;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07003010 if (n > count)
3011 n = count;
Joonsoo Kime81ce852013-04-29 15:07:32 -07003012 if (!(vm->flags & VM_IOREMAP)) {
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07003013 aligned_vwrite(buf, addr, n);
3014 copied++;
3015 }
3016 buf += n;
3017 addr += n;
3018 count -= n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 }
3020finished:
Joonsoo Kime81ce852013-04-29 15:07:32 -07003021 spin_unlock(&vmap_area_lock);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07003022 if (!copied)
3023 return 0;
3024 return buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025}
Nick Piggin83342312006-06-23 02:03:20 -07003026
3027/**
Mike Rapoport92eac162019-03-05 15:48:36 -08003028 * remap_vmalloc_range_partial - map vmalloc pages to userspace
3029 * @vma: vma to cover
3030 * @uaddr: target user address to start at
3031 * @kaddr: virtual address of vmalloc kernel memory
Jann Hornbdebd6a22020-04-20 18:14:11 -07003032 * @pgoff: offset from @kaddr to start at
Mike Rapoport92eac162019-03-05 15:48:36 -08003033 * @size: size of map area
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07003034 *
Mike Rapoport92eac162019-03-05 15:48:36 -08003035 * Returns: 0 for success, -Exxx on failure
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07003036 *
Mike Rapoport92eac162019-03-05 15:48:36 -08003037 * This function checks that @kaddr is a valid vmalloc'ed area,
3038 * and that it is big enough to cover the range starting at
3039 * @uaddr in @vma. Will return failure if that criteria isn't
3040 * met.
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07003041 *
Mike Rapoport92eac162019-03-05 15:48:36 -08003042 * Similar to remap_pfn_range() (see mm/memory.c)
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07003043 */
3044int remap_vmalloc_range_partial(struct vm_area_struct *vma, unsigned long uaddr,
Jann Hornbdebd6a22020-04-20 18:14:11 -07003045 void *kaddr, unsigned long pgoff,
3046 unsigned long size)
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07003047{
3048 struct vm_struct *area;
Jann Hornbdebd6a22020-04-20 18:14:11 -07003049 unsigned long off;
3050 unsigned long end_index;
3051
3052 if (check_shl_overflow(pgoff, PAGE_SHIFT, &off))
3053 return -EINVAL;
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07003054
3055 size = PAGE_ALIGN(size);
3056
3057 if (!PAGE_ALIGNED(uaddr) || !PAGE_ALIGNED(kaddr))
3058 return -EINVAL;
3059
3060 area = find_vm_area(kaddr);
3061 if (!area)
3062 return -EINVAL;
3063
Christoph Hellwigfe9041c2019-06-03 08:55:13 +02003064 if (!(area->flags & (VM_USERMAP | VM_DMA_COHERENT)))
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07003065 return -EINVAL;
3066
Jann Hornbdebd6a22020-04-20 18:14:11 -07003067 if (check_add_overflow(size, off, &end_index) ||
3068 end_index > get_vm_area_size(area))
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07003069 return -EINVAL;
Jann Hornbdebd6a22020-04-20 18:14:11 -07003070 kaddr += off;
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07003071
3072 do {
3073 struct page *page = vmalloc_to_page(kaddr);
3074 int ret;
3075
3076 ret = vm_insert_page(vma, uaddr, page);
3077 if (ret)
3078 return ret;
3079
3080 uaddr += PAGE_SIZE;
3081 kaddr += PAGE_SIZE;
3082 size -= PAGE_SIZE;
3083 } while (size > 0);
3084
3085 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3086
3087 return 0;
3088}
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07003089
3090/**
Mike Rapoport92eac162019-03-05 15:48:36 -08003091 * remap_vmalloc_range - map vmalloc pages to userspace
3092 * @vma: vma to cover (map full range of vma)
3093 * @addr: vmalloc memory
3094 * @pgoff: number of pages into addr before first page to map
Randy Dunlap76824862008-03-19 17:00:40 -07003095 *
Mike Rapoport92eac162019-03-05 15:48:36 -08003096 * Returns: 0 for success, -Exxx on failure
Nick Piggin83342312006-06-23 02:03:20 -07003097 *
Mike Rapoport92eac162019-03-05 15:48:36 -08003098 * This function checks that addr is a valid vmalloc'ed area, and
3099 * that it is big enough to cover the vma. Will return failure if
3100 * that criteria isn't met.
Nick Piggin83342312006-06-23 02:03:20 -07003101 *
Mike Rapoport92eac162019-03-05 15:48:36 -08003102 * Similar to remap_pfn_range() (see mm/memory.c)
Nick Piggin83342312006-06-23 02:03:20 -07003103 */
3104int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
3105 unsigned long pgoff)
3106{
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07003107 return remap_vmalloc_range_partial(vma, vma->vm_start,
Jann Hornbdebd6a22020-04-20 18:14:11 -07003108 addr, pgoff,
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07003109 vma->vm_end - vma->vm_start);
Nick Piggin83342312006-06-23 02:03:20 -07003110}
3111EXPORT_SYMBOL(remap_vmalloc_range);
3112
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07003113void free_vm_area(struct vm_struct *area)
3114{
3115 struct vm_struct *ret;
3116 ret = remove_vm_area(area->addr);
3117 BUG_ON(ret != area);
3118 kfree(area);
3119}
3120EXPORT_SYMBOL_GPL(free_vm_area);
Christoph Lametera10aa572008-04-28 02:12:40 -07003121
Tejun Heo4f8b02b2010-09-03 18:22:47 +02003122#ifdef CONFIG_SMP
Tejun Heoca23e402009-08-14 15:00:52 +09003123static struct vmap_area *node_to_va(struct rb_node *n)
3124{
Geliang Tang4583e772017-02-22 15:41:54 -08003125 return rb_entry_safe(n, struct vmap_area, rb_node);
Tejun Heoca23e402009-08-14 15:00:52 +09003126}
3127
3128/**
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003129 * pvm_find_va_enclose_addr - find the vmap_area @addr belongs to
3130 * @addr: target address
Tejun Heoca23e402009-08-14 15:00:52 +09003131 *
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003132 * Returns: vmap_area if it is found. If there is no such area
3133 * the first highest(reverse order) vmap_area is returned
3134 * i.e. va->va_start < addr && va->va_end < addr or NULL
3135 * if there are no any areas before @addr.
Tejun Heoca23e402009-08-14 15:00:52 +09003136 */
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003137static struct vmap_area *
3138pvm_find_va_enclose_addr(unsigned long addr)
Tejun Heoca23e402009-08-14 15:00:52 +09003139{
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003140 struct vmap_area *va, *tmp;
3141 struct rb_node *n;
3142
3143 n = free_vmap_area_root.rb_node;
3144 va = NULL;
Tejun Heoca23e402009-08-14 15:00:52 +09003145
3146 while (n) {
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003147 tmp = rb_entry(n, struct vmap_area, rb_node);
3148 if (tmp->va_start <= addr) {
3149 va = tmp;
3150 if (tmp->va_end >= addr)
3151 break;
3152
Tejun Heoca23e402009-08-14 15:00:52 +09003153 n = n->rb_right;
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003154 } else {
3155 n = n->rb_left;
3156 }
Tejun Heoca23e402009-08-14 15:00:52 +09003157 }
3158
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003159 return va;
Tejun Heoca23e402009-08-14 15:00:52 +09003160}
3161
3162/**
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003163 * pvm_determine_end_from_reverse - find the highest aligned address
3164 * of free block below VMALLOC_END
3165 * @va:
3166 * in - the VA we start the search(reverse order);
3167 * out - the VA with the highest aligned end address.
Alex Shi799fa852020-12-14 19:08:53 -08003168 * @align: alignment for required highest address
Tejun Heoca23e402009-08-14 15:00:52 +09003169 *
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003170 * Returns: determined end address within vmap_area
Tejun Heoca23e402009-08-14 15:00:52 +09003171 */
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003172static unsigned long
3173pvm_determine_end_from_reverse(struct vmap_area **va, unsigned long align)
Tejun Heoca23e402009-08-14 15:00:52 +09003174{
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003175 unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
Tejun Heoca23e402009-08-14 15:00:52 +09003176 unsigned long addr;
3177
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003178 if (likely(*va)) {
3179 list_for_each_entry_from_reverse((*va),
3180 &free_vmap_area_list, list) {
3181 addr = min((*va)->va_end & ~(align - 1), vmalloc_end);
3182 if ((*va)->va_start < addr)
3183 return addr;
3184 }
Tejun Heoca23e402009-08-14 15:00:52 +09003185 }
3186
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003187 return 0;
Tejun Heoca23e402009-08-14 15:00:52 +09003188}
3189
3190/**
3191 * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator
3192 * @offsets: array containing offset of each area
3193 * @sizes: array containing size of each area
3194 * @nr_vms: the number of areas to allocate
3195 * @align: alignment, all entries in @offsets and @sizes must be aligned to this
Tejun Heoca23e402009-08-14 15:00:52 +09003196 *
3197 * Returns: kmalloc'd vm_struct pointer array pointing to allocated
3198 * vm_structs on success, %NULL on failure
3199 *
3200 * Percpu allocator wants to use congruent vm areas so that it can
3201 * maintain the offsets among percpu areas. This function allocates
David Rientjesec3f64f2011-01-13 15:46:01 -08003202 * congruent vmalloc areas for it with GFP_KERNEL. These areas tend to
3203 * be scattered pretty far, distance between two areas easily going up
3204 * to gigabytes. To avoid interacting with regular vmallocs, these
3205 * areas are allocated from top.
Tejun Heoca23e402009-08-14 15:00:52 +09003206 *
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003207 * Despite its complicated look, this allocator is rather simple. It
3208 * does everything top-down and scans free blocks from the end looking
3209 * for matching base. While scanning, if any of the areas do not fit the
3210 * base address is pulled down to fit the area. Scanning is repeated till
3211 * all the areas fit and then all necessary data structures are inserted
3212 * and the result is returned.
Tejun Heoca23e402009-08-14 15:00:52 +09003213 */
3214struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
3215 const size_t *sizes, int nr_vms,
David Rientjesec3f64f2011-01-13 15:46:01 -08003216 size_t align)
Tejun Heoca23e402009-08-14 15:00:52 +09003217{
3218 const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align);
3219 const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003220 struct vmap_area **vas, *va;
Tejun Heoca23e402009-08-14 15:00:52 +09003221 struct vm_struct **vms;
3222 int area, area2, last_area, term_area;
Daniel Axtens253a4962019-12-17 20:51:49 -08003223 unsigned long base, start, size, end, last_end, orig_start, orig_end;
Tejun Heoca23e402009-08-14 15:00:52 +09003224 bool purged = false;
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003225 enum fit_type type;
Tejun Heoca23e402009-08-14 15:00:52 +09003226
Tejun Heoca23e402009-08-14 15:00:52 +09003227 /* verify parameters and allocate data structures */
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08003228 BUG_ON(offset_in_page(align) || !is_power_of_2(align));
Tejun Heoca23e402009-08-14 15:00:52 +09003229 for (last_area = 0, area = 0; area < nr_vms; area++) {
3230 start = offsets[area];
3231 end = start + sizes[area];
3232
3233 /* is everything aligned properly? */
3234 BUG_ON(!IS_ALIGNED(offsets[area], align));
3235 BUG_ON(!IS_ALIGNED(sizes[area], align));
3236
3237 /* detect the area with the highest address */
3238 if (start > offsets[last_area])
3239 last_area = area;
3240
Wei Yangc568da22017-09-06 16:24:09 -07003241 for (area2 = area + 1; area2 < nr_vms; area2++) {
Tejun Heoca23e402009-08-14 15:00:52 +09003242 unsigned long start2 = offsets[area2];
3243 unsigned long end2 = start2 + sizes[area2];
3244
Wei Yangc568da22017-09-06 16:24:09 -07003245 BUG_ON(start2 < end && start < end2);
Tejun Heoca23e402009-08-14 15:00:52 +09003246 }
3247 }
3248 last_end = offsets[last_area] + sizes[last_area];
3249
3250 if (vmalloc_end - vmalloc_start < last_end) {
3251 WARN_ON(true);
3252 return NULL;
3253 }
3254
Thomas Meyer4d67d862012-05-29 15:06:21 -07003255 vms = kcalloc(nr_vms, sizeof(vms[0]), GFP_KERNEL);
3256 vas = kcalloc(nr_vms, sizeof(vas[0]), GFP_KERNEL);
Tejun Heoca23e402009-08-14 15:00:52 +09003257 if (!vas || !vms)
Kautuk Consulf1db7af2012-01-12 17:20:08 -08003258 goto err_free2;
Tejun Heoca23e402009-08-14 15:00:52 +09003259
3260 for (area = 0; area < nr_vms; area++) {
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003261 vas[area] = kmem_cache_zalloc(vmap_area_cachep, GFP_KERNEL);
David Rientjesec3f64f2011-01-13 15:46:01 -08003262 vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL);
Tejun Heoca23e402009-08-14 15:00:52 +09003263 if (!vas[area] || !vms[area])
3264 goto err_free;
3265 }
3266retry:
Uladzislau Rezki (Sony)e36176b2019-11-30 17:54:47 -08003267 spin_lock(&free_vmap_area_lock);
Tejun Heoca23e402009-08-14 15:00:52 +09003268
3269 /* start scanning - we scan from the top, begin with the last area */
3270 area = term_area = last_area;
3271 start = offsets[area];
3272 end = start + sizes[area];
3273
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003274 va = pvm_find_va_enclose_addr(vmalloc_end);
3275 base = pvm_determine_end_from_reverse(&va, align) - end;
Tejun Heoca23e402009-08-14 15:00:52 +09003276
3277 while (true) {
Tejun Heoca23e402009-08-14 15:00:52 +09003278 /*
3279 * base might have underflowed, add last_end before
3280 * comparing.
3281 */
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003282 if (base + last_end < vmalloc_start + last_end)
3283 goto overflow;
Tejun Heoca23e402009-08-14 15:00:52 +09003284
3285 /*
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003286 * Fitting base has not been found.
Tejun Heoca23e402009-08-14 15:00:52 +09003287 */
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003288 if (va == NULL)
3289 goto overflow;
Tejun Heoca23e402009-08-14 15:00:52 +09003290
3291 /*
Qiujun Huangd8cc3232020-04-06 20:04:02 -07003292 * If required width exceeds current VA block, move
Kuppuswamy Sathyanarayanan5336e522019-08-13 15:37:31 -07003293 * base downwards and then recheck.
3294 */
3295 if (base + end > va->va_end) {
3296 base = pvm_determine_end_from_reverse(&va, align) - end;
3297 term_area = area;
3298 continue;
3299 }
3300
3301 /*
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003302 * If this VA does not fit, move base downwards and recheck.
Tejun Heoca23e402009-08-14 15:00:52 +09003303 */
Kuppuswamy Sathyanarayanan5336e522019-08-13 15:37:31 -07003304 if (base + start < va->va_start) {
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003305 va = node_to_va(rb_prev(&va->rb_node));
3306 base = pvm_determine_end_from_reverse(&va, align) - end;
Tejun Heoca23e402009-08-14 15:00:52 +09003307 term_area = area;
3308 continue;
3309 }
3310
3311 /*
3312 * This area fits, move on to the previous one. If
3313 * the previous one is the terminal one, we're done.
3314 */
3315 area = (area + nr_vms - 1) % nr_vms;
3316 if (area == term_area)
3317 break;
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003318
Tejun Heoca23e402009-08-14 15:00:52 +09003319 start = offsets[area];
3320 end = start + sizes[area];
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003321 va = pvm_find_va_enclose_addr(base + end);
Tejun Heoca23e402009-08-14 15:00:52 +09003322 }
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003323
Tejun Heoca23e402009-08-14 15:00:52 +09003324 /* we've found a fitting base, insert all va's */
3325 for (area = 0; area < nr_vms; area++) {
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003326 int ret;
Tejun Heoca23e402009-08-14 15:00:52 +09003327
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003328 start = base + offsets[area];
3329 size = sizes[area];
3330
3331 va = pvm_find_va_enclose_addr(start);
3332 if (WARN_ON_ONCE(va == NULL))
3333 /* It is a BUG(), but trigger recovery instead. */
3334 goto recovery;
3335
3336 type = classify_va_fit_type(va, start, size);
3337 if (WARN_ON_ONCE(type == NOTHING_FIT))
3338 /* It is a BUG(), but trigger recovery instead. */
3339 goto recovery;
3340
3341 ret = adjust_va_to_fit_type(va, start, size, type);
3342 if (unlikely(ret))
3343 goto recovery;
3344
3345 /* Allocated area. */
3346 va = vas[area];
3347 va->va_start = start;
3348 va->va_end = start + size;
Tejun Heoca23e402009-08-14 15:00:52 +09003349 }
3350
Uladzislau Rezki (Sony)e36176b2019-11-30 17:54:47 -08003351 spin_unlock(&free_vmap_area_lock);
Tejun Heoca23e402009-08-14 15:00:52 +09003352
Daniel Axtens253a4962019-12-17 20:51:49 -08003353 /* populate the kasan shadow space */
3354 for (area = 0; area < nr_vms; area++) {
3355 if (kasan_populate_vmalloc(vas[area]->va_start, sizes[area]))
3356 goto err_free_shadow;
3357
3358 kasan_unpoison_vmalloc((void *)vas[area]->va_start,
3359 sizes[area]);
3360 }
3361
Tejun Heoca23e402009-08-14 15:00:52 +09003362 /* insert all vm's */
Uladzislau Rezki (Sony)e36176b2019-11-30 17:54:47 -08003363 spin_lock(&vmap_area_lock);
3364 for (area = 0; area < nr_vms; area++) {
3365 insert_vmap_area(vas[area], &vmap_area_root, &vmap_area_list);
3366
3367 setup_vmalloc_vm_locked(vms[area], vas[area], VM_ALLOC,
Zhang Yanfei3645cb42013-07-03 15:04:48 -07003368 pcpu_get_vm_areas);
Uladzislau Rezki (Sony)e36176b2019-11-30 17:54:47 -08003369 }
3370 spin_unlock(&vmap_area_lock);
Tejun Heoca23e402009-08-14 15:00:52 +09003371
3372 kfree(vas);
3373 return vms;
3374
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003375recovery:
Uladzislau Rezki (Sony)e36176b2019-11-30 17:54:47 -08003376 /*
3377 * Remove previously allocated areas. There is no
3378 * need in removing these areas from the busy tree,
3379 * because they are inserted only on the final step
3380 * and when pcpu_get_vm_areas() is success.
3381 */
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003382 while (area--) {
Daniel Axtens253a4962019-12-17 20:51:49 -08003383 orig_start = vas[area]->va_start;
3384 orig_end = vas[area]->va_end;
Uladzislau Rezki (Sony)96e2db42020-12-14 19:08:49 -08003385 va = merge_or_add_vmap_area_augment(vas[area], &free_vmap_area_root,
3386 &free_vmap_area_list);
Uladzislau Rezki (Sony)9c801f62020-08-06 23:24:24 -07003387 if (va)
3388 kasan_release_vmalloc(orig_start, orig_end,
3389 va->va_start, va->va_end);
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003390 vas[area] = NULL;
3391 }
3392
3393overflow:
Uladzislau Rezki (Sony)e36176b2019-11-30 17:54:47 -08003394 spin_unlock(&free_vmap_area_lock);
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003395 if (!purged) {
3396 purge_vmap_area_lazy();
3397 purged = true;
3398
3399 /* Before "retry", check if we recover. */
3400 for (area = 0; area < nr_vms; area++) {
3401 if (vas[area])
3402 continue;
3403
3404 vas[area] = kmem_cache_zalloc(
3405 vmap_area_cachep, GFP_KERNEL);
3406 if (!vas[area])
3407 goto err_free;
3408 }
3409
3410 goto retry;
3411 }
3412
Tejun Heoca23e402009-08-14 15:00:52 +09003413err_free:
3414 for (area = 0; area < nr_vms; area++) {
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003415 if (vas[area])
3416 kmem_cache_free(vmap_area_cachep, vas[area]);
3417
Kautuk Consulf1db7af2012-01-12 17:20:08 -08003418 kfree(vms[area]);
Tejun Heoca23e402009-08-14 15:00:52 +09003419 }
Kautuk Consulf1db7af2012-01-12 17:20:08 -08003420err_free2:
Tejun Heoca23e402009-08-14 15:00:52 +09003421 kfree(vas);
3422 kfree(vms);
3423 return NULL;
Daniel Axtens253a4962019-12-17 20:51:49 -08003424
3425err_free_shadow:
3426 spin_lock(&free_vmap_area_lock);
3427 /*
3428 * We release all the vmalloc shadows, even the ones for regions that
3429 * hadn't been successfully added. This relies on kasan_release_vmalloc
3430 * being able to tolerate this case.
3431 */
3432 for (area = 0; area < nr_vms; area++) {
3433 orig_start = vas[area]->va_start;
3434 orig_end = vas[area]->va_end;
Uladzislau Rezki (Sony)96e2db42020-12-14 19:08:49 -08003435 va = merge_or_add_vmap_area_augment(vas[area], &free_vmap_area_root,
3436 &free_vmap_area_list);
Uladzislau Rezki (Sony)9c801f62020-08-06 23:24:24 -07003437 if (va)
3438 kasan_release_vmalloc(orig_start, orig_end,
3439 va->va_start, va->va_end);
Daniel Axtens253a4962019-12-17 20:51:49 -08003440 vas[area] = NULL;
3441 kfree(vms[area]);
3442 }
3443 spin_unlock(&free_vmap_area_lock);
3444 kfree(vas);
3445 kfree(vms);
3446 return NULL;
Tejun Heoca23e402009-08-14 15:00:52 +09003447}
3448
3449/**
3450 * pcpu_free_vm_areas - free vmalloc areas for percpu allocator
3451 * @vms: vm_struct pointer array returned by pcpu_get_vm_areas()
3452 * @nr_vms: the number of allocated areas
3453 *
3454 * Free vm_structs and the array allocated by pcpu_get_vm_areas().
3455 */
3456void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
3457{
3458 int i;
3459
3460 for (i = 0; i < nr_vms; i++)
3461 free_vm_area(vms[i]);
3462 kfree(vms);
3463}
Tejun Heo4f8b02b2010-09-03 18:22:47 +02003464#endif /* CONFIG_SMP */
Christoph Lametera10aa572008-04-28 02:12:40 -07003465
Paul E. McKenney5bb1bb32021-01-07 13:46:11 -08003466#ifdef CONFIG_PRINTK
Paul E. McKenney98f18082020-12-08 16:13:57 -08003467bool vmalloc_dump_obj(void *object)
3468{
3469 struct vm_struct *vm;
3470 void *objp = (void *)PAGE_ALIGN((unsigned long)object);
3471
3472 vm = find_vm_area(objp);
3473 if (!vm)
3474 return false;
Paul E. McKenneybd34dcd2020-12-09 15:15:27 -08003475 pr_cont(" %u-page vmalloc region starting at %#lx allocated at %pS\n",
3476 vm->nr_pages, (unsigned long)vm->addr, vm->caller);
Paul E. McKenney98f18082020-12-08 16:13:57 -08003477 return true;
3478}
Paul E. McKenney5bb1bb32021-01-07 13:46:11 -08003479#endif
Paul E. McKenney98f18082020-12-08 16:13:57 -08003480
Christoph Lametera10aa572008-04-28 02:12:40 -07003481#ifdef CONFIG_PROC_FS
3482static void *s_start(struct seq_file *m, loff_t *pos)
Uladzislau Rezki (Sony)e36176b2019-11-30 17:54:47 -08003483 __acquires(&vmap_purge_lock)
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003484 __acquires(&vmap_area_lock)
Christoph Lametera10aa572008-04-28 02:12:40 -07003485{
Uladzislau Rezki (Sony)e36176b2019-11-30 17:54:47 -08003486 mutex_lock(&vmap_purge_lock);
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003487 spin_lock(&vmap_area_lock);
Uladzislau Rezki (Sony)e36176b2019-11-30 17:54:47 -08003488
zijun_hu3f500062016-12-12 16:42:17 -08003489 return seq_list_start(&vmap_area_list, *pos);
Christoph Lametera10aa572008-04-28 02:12:40 -07003490}
3491
3492static void *s_next(struct seq_file *m, void *p, loff_t *pos)
3493{
zijun_hu3f500062016-12-12 16:42:17 -08003494 return seq_list_next(p, &vmap_area_list, pos);
Christoph Lametera10aa572008-04-28 02:12:40 -07003495}
3496
3497static void s_stop(struct seq_file *m, void *p)
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003498 __releases(&vmap_area_lock)
Waiman Long0a7dd4e2020-12-14 19:08:59 -08003499 __releases(&vmap_purge_lock)
Christoph Lametera10aa572008-04-28 02:12:40 -07003500{
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003501 spin_unlock(&vmap_area_lock);
Waiman Long0a7dd4e2020-12-14 19:08:59 -08003502 mutex_unlock(&vmap_purge_lock);
Christoph Lametera10aa572008-04-28 02:12:40 -07003503}
3504
Eric Dumazeta47a1262008-07-23 21:27:38 -07003505static void show_numa_info(struct seq_file *m, struct vm_struct *v)
3506{
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08003507 if (IS_ENABLED(CONFIG_NUMA)) {
Eric Dumazeta47a1262008-07-23 21:27:38 -07003508 unsigned int nr, *counters = m->private;
3509
3510 if (!counters)
3511 return;
3512
Wanpeng Liaf123462013-11-12 15:07:32 -08003513 if (v->flags & VM_UNINITIALIZED)
3514 return;
Dmitry Vyukov7e5b5282014-12-12 16:56:30 -08003515 /* Pair with smp_wmb() in clear_vm_uninitialized_flag() */
3516 smp_rmb();
Wanpeng Liaf123462013-11-12 15:07:32 -08003517
Eric Dumazeta47a1262008-07-23 21:27:38 -07003518 memset(counters, 0, nr_node_ids * sizeof(unsigned int));
3519
3520 for (nr = 0; nr < v->nr_pages; nr++)
3521 counters[page_to_nid(v->pages[nr])]++;
3522
3523 for_each_node_state(nr, N_HIGH_MEMORY)
3524 if (counters[nr])
3525 seq_printf(m, " N%u=%u", nr, counters[nr]);
3526 }
3527}
3528
Uladzislau Rezki (Sony)dd3b8352019-09-23 15:36:36 -07003529static void show_purge_info(struct seq_file *m)
3530{
Uladzislau Rezki (Sony)dd3b8352019-09-23 15:36:36 -07003531 struct vmap_area *va;
3532
Uladzislau Rezki (Sony)96e2db42020-12-14 19:08:49 -08003533 spin_lock(&purge_vmap_area_lock);
3534 list_for_each_entry(va, &purge_vmap_area_list, list) {
Uladzislau Rezki (Sony)dd3b8352019-09-23 15:36:36 -07003535 seq_printf(m, "0x%pK-0x%pK %7ld unpurged vm_area\n",
3536 (void *)va->va_start, (void *)va->va_end,
3537 va->va_end - va->va_start);
3538 }
Uladzislau Rezki (Sony)96e2db42020-12-14 19:08:49 -08003539 spin_unlock(&purge_vmap_area_lock);
Uladzislau Rezki (Sony)dd3b8352019-09-23 15:36:36 -07003540}
3541
Christoph Lametera10aa572008-04-28 02:12:40 -07003542static int s_show(struct seq_file *m, void *p)
3543{
zijun_hu3f500062016-12-12 16:42:17 -08003544 struct vmap_area *va;
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003545 struct vm_struct *v;
3546
zijun_hu3f500062016-12-12 16:42:17 -08003547 va = list_entry(p, struct vmap_area, list);
3548
Wanpeng Lic2ce8c12013-11-12 15:07:31 -08003549 /*
Pengfei Li688fcbf2019-09-23 15:36:39 -07003550 * s_show can encounter race with remove_vm_area, !vm on behalf
3551 * of vmap area is being tear down or vm_map_ram allocation.
Wanpeng Lic2ce8c12013-11-12 15:07:31 -08003552 */
Pengfei Li688fcbf2019-09-23 15:36:39 -07003553 if (!va->vm) {
Uladzislau Rezki (Sony)dd3b8352019-09-23 15:36:36 -07003554 seq_printf(m, "0x%pK-0x%pK %7ld vm_map_ram\n",
Yisheng Xie78c72742017-07-10 15:48:09 -07003555 (void *)va->va_start, (void *)va->va_end,
Uladzislau Rezki (Sony)dd3b8352019-09-23 15:36:36 -07003556 va->va_end - va->va_start);
Yisheng Xie78c72742017-07-10 15:48:09 -07003557
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003558 return 0;
Yisheng Xie78c72742017-07-10 15:48:09 -07003559 }
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003560
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003561 v = va->vm;
Christoph Lametera10aa572008-04-28 02:12:40 -07003562
Kees Cook45ec1692012-10-08 16:34:09 -07003563 seq_printf(m, "0x%pK-0x%pK %7ld",
Christoph Lametera10aa572008-04-28 02:12:40 -07003564 v->addr, v->addr + v->size, v->size);
3565
Joe Perches62c70bc2011-01-13 15:45:52 -08003566 if (v->caller)
3567 seq_printf(m, " %pS", v->caller);
Christoph Lameter23016962008-04-28 02:12:42 -07003568
Christoph Lametera10aa572008-04-28 02:12:40 -07003569 if (v->nr_pages)
3570 seq_printf(m, " pages=%d", v->nr_pages);
3571
3572 if (v->phys_addr)
Miles Chen199eaa02017-02-24 14:59:51 -08003573 seq_printf(m, " phys=%pa", &v->phys_addr);
Christoph Lametera10aa572008-04-28 02:12:40 -07003574
3575 if (v->flags & VM_IOREMAP)
Fabian Frederickf4527c92014-06-04 16:08:09 -07003576 seq_puts(m, " ioremap");
Christoph Lametera10aa572008-04-28 02:12:40 -07003577
3578 if (v->flags & VM_ALLOC)
Fabian Frederickf4527c92014-06-04 16:08:09 -07003579 seq_puts(m, " vmalloc");
Christoph Lametera10aa572008-04-28 02:12:40 -07003580
3581 if (v->flags & VM_MAP)
Fabian Frederickf4527c92014-06-04 16:08:09 -07003582 seq_puts(m, " vmap");
Christoph Lametera10aa572008-04-28 02:12:40 -07003583
3584 if (v->flags & VM_USERMAP)
Fabian Frederickf4527c92014-06-04 16:08:09 -07003585 seq_puts(m, " user");
Christoph Lametera10aa572008-04-28 02:12:40 -07003586
Christoph Hellwigfe9041c2019-06-03 08:55:13 +02003587 if (v->flags & VM_DMA_COHERENT)
3588 seq_puts(m, " dma-coherent");
3589
David Rientjes244d63e2016-01-14 15:19:35 -08003590 if (is_vmalloc_addr(v->pages))
Fabian Frederickf4527c92014-06-04 16:08:09 -07003591 seq_puts(m, " vpages");
Christoph Lametera10aa572008-04-28 02:12:40 -07003592
Eric Dumazeta47a1262008-07-23 21:27:38 -07003593 show_numa_info(m, v);
Christoph Lametera10aa572008-04-28 02:12:40 -07003594 seq_putc(m, '\n');
Uladzislau Rezki (Sony)dd3b8352019-09-23 15:36:36 -07003595
3596 /*
Uladzislau Rezki (Sony)96e2db42020-12-14 19:08:49 -08003597 * As a final step, dump "unpurged" areas.
Uladzislau Rezki (Sony)dd3b8352019-09-23 15:36:36 -07003598 */
3599 if (list_is_last(&va->list, &vmap_area_list))
3600 show_purge_info(m);
3601
Christoph Lametera10aa572008-04-28 02:12:40 -07003602 return 0;
3603}
3604
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04003605static const struct seq_operations vmalloc_op = {
Christoph Lametera10aa572008-04-28 02:12:40 -07003606 .start = s_start,
3607 .next = s_next,
3608 .stop = s_stop,
3609 .show = s_show,
3610};
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04003611
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04003612static int __init proc_vmalloc_init(void)
3613{
Christoph Hellwigfddda2b2018-04-13 19:44:18 +02003614 if (IS_ENABLED(CONFIG_NUMA))
Joe Perches0825a6f2018-06-14 15:27:58 -07003615 proc_create_seq_private("vmallocinfo", 0400, NULL,
Christoph Hellwig44414d82018-04-24 17:05:17 +02003616 &vmalloc_op,
3617 nr_node_ids * sizeof(unsigned int), NULL);
Christoph Hellwigfddda2b2018-04-13 19:44:18 +02003618 else
Joe Perches0825a6f2018-06-14 15:27:58 -07003619 proc_create_seq("vmallocinfo", 0400, NULL, &vmalloc_op);
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04003620 return 0;
3621}
3622module_init(proc_vmalloc_init);
Joonsoo Kimdb3808c2013-04-29 15:07:28 -07003623
Christoph Lametera10aa572008-04-28 02:12:40 -07003624#endif