blob: 8f63ba92fdb93f9a594c0a41db0e3a94a60a5863 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/mm/vmalloc.c
3 *
4 * Copyright (C) 1993 Linus Torvalds
5 * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
6 * SMP-safe vmalloc/vfree/ioremap, Tigran Aivazian <tigran@veritas.com>, May 2000
7 * Major rework to support vmap/vunmap, Christoph Hellwig, SGI, August 2002
Christoph Lameter930fc452005-10-29 18:15:41 -07008 * Numa awareness, Christoph Lameter, SGI, June 2005
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>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070021#include <linux/debugobjects.h>
Christoph Lameter23016962008-04-28 02:12:42 -070022#include <linux/kallsyms.h>
Nick Piggindb64fe02008-10-18 20:27:03 -070023#include <linux/list.h>
Chris Wilson4da56b92016-04-04 14:46:42 +010024#include <linux/notifier.h>
Nick Piggindb64fe02008-10-18 20:27:03 -070025#include <linux/rbtree.h>
26#include <linux/radix-tree.h>
27#include <linux/rcupdate.h>
Tejun Heof0aa6612009-02-20 16:29:08 +090028#include <linux/pfn.h>
Catalin Marinas89219d32009-06-11 13:23:19 +010029#include <linux/kmemleak.h>
Arun Sharma600634972011-07-26 16:09:06 -070030#include <linux/atomic.h>
Gideon Israel Dsouza3b321232014-04-07 15:37:26 -070031#include <linux/compiler.h>
Al Viro32fcfd42013-03-10 20:14:08 -040032#include <linux/llist.h>
Toshi Kani0f616be2015-04-14 15:47:17 -070033#include <linux/bitops.h>
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -070034#include <linux/rbtree_augmented.h>
Jann Hornd8da38e2020-04-20 18:14:11 -070035#include <linux/overflow.h>
Gideon Israel Dsouza3b321232014-04-07 15:37:26 -070036
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080037#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/tlbflush.h>
David Miller2dca6992009-09-21 12:22:34 -070039#include <asm/shmparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Mel Gormandd56b042015-11-06 16:28:43 -080041#include "internal.h"
42
Al Viro32fcfd42013-03-10 20:14:08 -040043struct vfree_deferred {
44 struct llist_head list;
45 struct work_struct wq;
46};
47static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred);
48
49static void __vunmap(const void *, int);
50
51static void free_work(struct work_struct *w)
52{
53 struct vfree_deferred *p = container_of(w, struct vfree_deferred, wq);
Byungchul Park894e58c2017-09-06 16:24:26 -070054 struct llist_node *t, *llnode;
55
56 llist_for_each_safe(llnode, t, llist_del_all(&p->list))
57 __vunmap((void *)llnode, 1);
Al Viro32fcfd42013-03-10 20:14:08 -040058}
59
Nick Piggindb64fe02008-10-18 20:27:03 -070060/*** Page table manipulation functions ***/
Adrian Bunkb2213852006-09-25 23:31:02 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end)
63{
64 pte_t *pte;
65
66 pte = pte_offset_kernel(pmd, addr);
67 do {
68 pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte);
69 WARN_ON(!pte_none(ptent) && !pte_present(ptent));
70 } while (pte++, addr += PAGE_SIZE, addr != end);
71}
72
Nick Piggindb64fe02008-10-18 20:27:03 -070073static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 pmd_t *pmd;
76 unsigned long next;
77
78 pmd = pmd_offset(pud, addr);
79 do {
80 next = pmd_addr_end(addr, end);
Toshi Kanib9820d82015-04-14 15:47:26 -070081 if (pmd_clear_huge(pmd))
82 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 if (pmd_none_or_clear_bad(pmd))
84 continue;
85 vunmap_pte_range(pmd, addr, next);
86 } while (pmd++, addr = next, addr != end);
87}
88
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +030089static void vunmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 pud_t *pud;
92 unsigned long next;
93
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +030094 pud = pud_offset(p4d, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 do {
96 next = pud_addr_end(addr, end);
Toshi Kanib9820d82015-04-14 15:47:26 -070097 if (pud_clear_huge(pud))
98 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (pud_none_or_clear_bad(pud))
100 continue;
101 vunmap_pmd_range(pud, addr, next);
102 } while (pud++, addr = next, addr != end);
103}
104
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300105static void vunmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end)
106{
107 p4d_t *p4d;
108 unsigned long next;
109
110 p4d = p4d_offset(pgd, addr);
111 do {
112 next = p4d_addr_end(addr, end);
113 if (p4d_clear_huge(p4d))
114 continue;
115 if (p4d_none_or_clear_bad(p4d))
116 continue;
117 vunmap_pud_range(p4d, addr, next);
118 } while (p4d++, addr = next, addr != end);
119}
120
Nick Piggindb64fe02008-10-18 20:27:03 -0700121static void vunmap_page_range(unsigned long addr, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 pgd_t *pgd;
124 unsigned long next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 BUG_ON(addr >= end);
127 pgd = pgd_offset_k(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 do {
129 next = pgd_addr_end(addr, end);
130 if (pgd_none_or_clear_bad(pgd))
131 continue;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300132 vunmap_p4d_range(pgd, addr, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 } while (pgd++, addr = next, addr != end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
136static int vmap_pte_range(pmd_t *pmd, unsigned long addr,
Nick Piggindb64fe02008-10-18 20:27:03 -0700137 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 pte_t *pte;
140
Nick Piggindb64fe02008-10-18 20:27:03 -0700141 /*
142 * nr is a running index into the array which helps higher level
143 * callers keep track of where we're up to.
144 */
145
Hugh Dickins872fec12005-10-29 18:16:21 -0700146 pte = pte_alloc_kernel(pmd, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (!pte)
148 return -ENOMEM;
149 do {
Nick Piggindb64fe02008-10-18 20:27:03 -0700150 struct page *page = pages[*nr];
151
152 if (WARN_ON(!pte_none(*pte)))
153 return -EBUSY;
154 if (WARN_ON(!page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return -ENOMEM;
156 set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
Nick Piggindb64fe02008-10-18 20:27:03 -0700157 (*nr)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 } while (pte++, addr += PAGE_SIZE, addr != end);
159 return 0;
160}
161
Nick Piggindb64fe02008-10-18 20:27:03 -0700162static int vmap_pmd_range(pud_t *pud, unsigned long addr,
163 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 pmd_t *pmd;
166 unsigned long next;
167
168 pmd = pmd_alloc(&init_mm, pud, addr);
169 if (!pmd)
170 return -ENOMEM;
171 do {
172 next = pmd_addr_end(addr, end);
Nick Piggindb64fe02008-10-18 20:27:03 -0700173 if (vmap_pte_range(pmd, addr, next, prot, pages, nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return -ENOMEM;
175 } while (pmd++, addr = next, addr != end);
176 return 0;
177}
178
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300179static int vmap_pud_range(p4d_t *p4d, unsigned long addr,
Nick Piggindb64fe02008-10-18 20:27:03 -0700180 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 pud_t *pud;
183 unsigned long next;
184
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300185 pud = pud_alloc(&init_mm, p4d, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (!pud)
187 return -ENOMEM;
188 do {
189 next = pud_addr_end(addr, end);
Nick Piggindb64fe02008-10-18 20:27:03 -0700190 if (vmap_pmd_range(pud, addr, next, prot, pages, nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return -ENOMEM;
192 } while (pud++, addr = next, addr != end);
193 return 0;
194}
195
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300196static int vmap_p4d_range(pgd_t *pgd, unsigned long addr,
197 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
198{
199 p4d_t *p4d;
200 unsigned long next;
201
202 p4d = p4d_alloc(&init_mm, pgd, addr);
203 if (!p4d)
204 return -ENOMEM;
205 do {
206 next = p4d_addr_end(addr, end);
207 if (vmap_pud_range(p4d, addr, next, prot, pages, nr))
208 return -ENOMEM;
209 } while (p4d++, addr = next, addr != end);
210 return 0;
211}
212
Nick Piggindb64fe02008-10-18 20:27:03 -0700213/*
214 * Set up page tables in kva (addr, end). The ptes shall have prot "prot", and
215 * will have pfns corresponding to the "pages" array.
216 *
217 * Ie. pte at addr+N*PAGE_SIZE shall point to pfn corresponding to pages[N]
218 */
Tejun Heo8fc48982009-02-20 16:29:08 +0900219static int vmap_page_range_noflush(unsigned long start, unsigned long end,
220 pgprot_t prot, struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 pgd_t *pgd;
223 unsigned long next;
Adam Lackorzynski2e4e27c2009-01-04 12:00:46 -0800224 unsigned long addr = start;
Nick Piggindb64fe02008-10-18 20:27:03 -0700225 int err = 0;
226 int nr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 BUG_ON(addr >= end);
229 pgd = pgd_offset_k(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 do {
231 next = pgd_addr_end(addr, end);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300232 err = vmap_p4d_range(pgd, addr, next, prot, pages, &nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (err)
Figo.zhangbf88c8c2009-09-21 17:01:47 -0700234 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 } while (pgd++, addr = next, addr != end);
Nick Piggindb64fe02008-10-18 20:27:03 -0700236
Nick Piggindb64fe02008-10-18 20:27:03 -0700237 return nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
Tejun Heo8fc48982009-02-20 16:29:08 +0900240static int vmap_page_range(unsigned long start, unsigned long end,
241 pgprot_t prot, struct page **pages)
242{
243 int ret;
244
245 ret = vmap_page_range_noflush(start, end, prot, pages);
246 flush_cache_vmap(start, end);
247 return ret;
248}
249
KAMEZAWA Hiroyuki81ac3ad2009-09-22 16:45:49 -0700250int is_vmalloc_or_module_addr(const void *x)
Linus Torvalds73bdf0a2008-10-15 08:35:12 -0700251{
252 /*
Russell Kingab4f2ee2008-11-06 17:11:07 +0000253 * ARM, x86-64 and sparc64 put modules in a special place,
Linus Torvalds73bdf0a2008-10-15 08:35:12 -0700254 * and fall back on vmalloc() if that fails. Others
255 * just put it in the vmalloc space.
256 */
257#if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
258 unsigned long addr = (unsigned long)x;
259 if (addr >= MODULES_VADDR && addr < MODULES_END)
260 return 1;
261#endif
262 return is_vmalloc_addr(x);
263}
264
Christoph Lameter48667e72008-02-04 22:28:31 -0800265/*
malcadd688f2014-01-27 17:06:53 -0800266 * Walk a vmap address to the struct page it maps.
Christoph Lameter48667e72008-02-04 22:28:31 -0800267 */
malcadd688f2014-01-27 17:06:53 -0800268struct page *vmalloc_to_page(const void *vmalloc_addr)
Christoph Lameter48667e72008-02-04 22:28:31 -0800269{
270 unsigned long addr = (unsigned long) vmalloc_addr;
malcadd688f2014-01-27 17:06:53 -0800271 struct page *page = NULL;
Christoph Lameter48667e72008-02-04 22:28:31 -0800272 pgd_t *pgd = pgd_offset_k(addr);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300273 p4d_t *p4d;
274 pud_t *pud;
275 pmd_t *pmd;
276 pte_t *ptep, pte;
Christoph Lameter48667e72008-02-04 22:28:31 -0800277
Ingo Molnar7aa413d2008-06-19 13:28:11 +0200278 /*
279 * XXX we might need to change this if we add VIRTUAL_BUG_ON for
280 * architectures that do not vmalloc module space
281 */
Linus Torvalds73bdf0a2008-10-15 08:35:12 -0700282 VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr));
Jiri Slaby59ea7462008-06-12 13:56:40 +0200283
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300284 if (pgd_none(*pgd))
285 return NULL;
286 p4d = p4d_offset(pgd, addr);
287 if (p4d_none(*p4d))
288 return NULL;
289 pud = pud_offset(p4d, addr);
Ard Biesheuvel029c54b2017-06-23 15:08:41 -0700290
291 /*
292 * Don't dereference bad PUD or PMD (below) entries. This will also
293 * identify huge mappings, which we may encounter on architectures
294 * that define CONFIG_HAVE_ARCH_HUGE_VMAP=y. Such regions will be
295 * identified as vmalloc addresses by is_vmalloc_addr(), but are
296 * not [unambiguously] associated with a struct page, so there is
297 * no correct value to return for them.
298 */
299 WARN_ON_ONCE(pud_bad(*pud));
300 if (pud_none(*pud) || pud_bad(*pud))
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300301 return NULL;
302 pmd = pmd_offset(pud, addr);
Ard Biesheuvel029c54b2017-06-23 15:08:41 -0700303 WARN_ON_ONCE(pmd_bad(*pmd));
304 if (pmd_none(*pmd) || pmd_bad(*pmd))
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300305 return NULL;
Nick Piggindb64fe02008-10-18 20:27:03 -0700306
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300307 ptep = pte_offset_map(pmd, addr);
308 pte = *ptep;
309 if (pte_present(pte))
310 page = pte_page(pte);
311 pte_unmap(ptep);
malcadd688f2014-01-27 17:06:53 -0800312 return page;
Jianyu Zhanece86e222014-01-21 15:49:12 -0800313}
314EXPORT_SYMBOL(vmalloc_to_page);
315
malcadd688f2014-01-27 17:06:53 -0800316/*
317 * Map a vmalloc()-space virtual address to the physical page frame number.
318 */
319unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
320{
321 return page_to_pfn(vmalloc_to_page(vmalloc_addr));
322}
323EXPORT_SYMBOL(vmalloc_to_pfn);
324
Nick Piggindb64fe02008-10-18 20:27:03 -0700325
326/*** Global kva allocator ***/
327
Uladzislau Rezki (Sony)8087b172019-05-17 14:31:34 -0700328#define DEBUG_AUGMENT_PROPAGATE_CHECK 0
Uladzislau Rezki (Sony)367c1e42019-05-17 14:31:37 -0700329#define DEBUG_AUGMENT_LOWEST_MATCH_CHECK 0
Uladzislau Rezki (Sony)8087b172019-05-17 14:31:34 -0700330
Yisheng Xie78c72742017-07-10 15:48:09 -0700331#define VM_LAZY_FREE 0x02
Nick Piggindb64fe02008-10-18 20:27:03 -0700332#define VM_VM_AREA 0x04
333
Nick Piggindb64fe02008-10-18 20:27:03 -0700334static DEFINE_SPINLOCK(vmap_area_lock);
Joonsoo Kimf1c40692013-04-29 15:07:37 -0700335/* Export for kexec only */
336LIST_HEAD(vmap_area_list);
Chris Wilson80c4bd72016-05-20 16:57:38 -0700337static LLIST_HEAD(vmap_purge_list);
Nick Piggin89699602011-03-22 16:30:36 -0700338static struct rb_root vmap_area_root = RB_ROOT;
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -0700339static bool vmap_initialized __read_mostly;
Nick Piggin89699602011-03-22 16:30:36 -0700340
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -0700341/*
342 * This kmem_cache is used for vmap_area objects. Instead of
343 * allocating from slab we reuse an object from this cache to
344 * make things faster. Especially in "no edge" splitting of
345 * free block.
346 */
347static struct kmem_cache *vmap_area_cachep;
Nick Piggin89699602011-03-22 16:30:36 -0700348
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -0700349/*
350 * This linked list is used in pair with free_vmap_area_root.
351 * It gives O(1) access to prev/next to perform fast coalescing.
352 */
353static LIST_HEAD(free_vmap_area_list);
354
355/*
356 * This augment red-black tree represents the free vmap space.
357 * All vmap_area objects in this tree are sorted by va->va_start
358 * address. It is used for allocation and merging when a vmap
359 * object is released.
360 *
361 * Each vmap_area node contains a maximum available free block
362 * of its sub-tree, right or left. Therefore it is possible to
363 * find a lowest match of free area.
364 */
365static struct rb_root free_vmap_area_root = RB_ROOT;
366
367static __always_inline unsigned long
368va_size(struct vmap_area *va)
369{
370 return (va->va_end - va->va_start);
371}
372
373static __always_inline unsigned long
374get_subtree_max_size(struct rb_node *node)
375{
376 struct vmap_area *va;
377
378 va = rb_entry_safe(node, struct vmap_area, rb_node);
379 return va ? va->subtree_max_size : 0;
380}
381
382/*
383 * Gets called when remove the node and rotate.
384 */
385static __always_inline unsigned long
386compute_subtree_max_size(struct vmap_area *va)
387{
388 return max3(va_size(va),
389 get_subtree_max_size(va->rb_node.rb_left),
390 get_subtree_max_size(va->rb_node.rb_right));
391}
392
393RB_DECLARE_CALLBACKS(static, free_vmap_area_rb_augment_cb,
394 struct vmap_area, rb_node, unsigned long, subtree_max_size,
395 compute_subtree_max_size)
396
397static void purge_vmap_area_lazy(void);
398static BLOCKING_NOTIFIER_HEAD(vmap_notify_list);
399static unsigned long lazy_max_pages(void);
Nick Piggindb64fe02008-10-18 20:27:03 -0700400
Roman Gushchindb70fefd2019-02-25 12:30:37 -0800401static atomic_long_t nr_vmalloc_pages;
402
403unsigned long vmalloc_nr_pages(void)
404{
405 return atomic_long_read(&nr_vmalloc_pages);
406}
407
Susheel Khianid6f731e2013-08-22 13:46:07 -0700408#ifdef CONFIG_ENABLE_VMALLOC_SAVING
409#define POSSIBLE_VMALLOC_START PAGE_OFFSET
410
411#define VMALLOC_BITMAP_SIZE ((VMALLOC_END - PAGE_OFFSET) >> \
412 PAGE_SHIFT)
413#define VMALLOC_TO_BIT(addr) ((addr - PAGE_OFFSET) >> PAGE_SHIFT)
414#define BIT_TO_VMALLOC(i) (PAGE_OFFSET + i * PAGE_SIZE)
415
416unsigned long total_vmalloc_size;
417unsigned long vmalloc_reserved;
418
419DECLARE_BITMAP(possible_areas, VMALLOC_BITMAP_SIZE);
420
421void mark_vmalloc_reserved_area(void *x, unsigned long size)
422{
423 unsigned long addr = (unsigned long)x;
424
425 bitmap_set(possible_areas, VMALLOC_TO_BIT(addr), size >> PAGE_SHIFT);
426 vmalloc_reserved += size;
427}
428
429int is_vmalloc_addr(const void *x)
430{
431 unsigned long addr = (unsigned long)x;
432
433 if (addr < POSSIBLE_VMALLOC_START || addr >= VMALLOC_END)
434 return 0;
435
436 if (test_bit(VMALLOC_TO_BIT(addr), possible_areas))
437 return 0;
438
439 return 1;
440}
441
442static void calc_total_vmalloc_size(void)
443{
444 total_vmalloc_size = VMALLOC_END - POSSIBLE_VMALLOC_START -
445 vmalloc_reserved;
446}
447#else
448int is_vmalloc_addr(const void *x)
449{
450 unsigned long addr = (unsigned long)x;
451
452 return addr >= VMALLOC_START && addr < VMALLOC_END;
453}
454
455static void calc_total_vmalloc_size(void) { }
456#endif
457EXPORT_SYMBOL(is_vmalloc_addr);
458
Ivaylo Georgiev4c30d462020-03-25 23:32:38 -0700459static atomic_long_t nr_vmalloc_pages;
460
Nick Piggindb64fe02008-10-18 20:27:03 -0700461static struct vmap_area *__find_vmap_area(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Nick Piggindb64fe02008-10-18 20:27:03 -0700463 struct rb_node *n = vmap_area_root.rb_node;
464
465 while (n) {
466 struct vmap_area *va;
467
468 va = rb_entry(n, struct vmap_area, rb_node);
469 if (addr < va->va_start)
470 n = n->rb_left;
HATAYAMA Daisukecef2ac32013-07-03 15:02:17 -0700471 else if (addr >= va->va_end)
Nick Piggindb64fe02008-10-18 20:27:03 -0700472 n = n->rb_right;
473 else
474 return va;
475 }
476
477 return NULL;
478}
479
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -0700480/*
481 * This function returns back addresses of parent node
482 * and its left or right link for further processing.
483 */
484static __always_inline struct rb_node **
485find_va_links(struct vmap_area *va,
486 struct rb_root *root, struct rb_node *from,
487 struct rb_node **parent)
Nick Piggindb64fe02008-10-18 20:27:03 -0700488{
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -0700489 struct vmap_area *tmp_va;
490 struct rb_node **link;
Nick Piggindb64fe02008-10-18 20:27:03 -0700491
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -0700492 if (root) {
493 link = &root->rb_node;
494 if (unlikely(!*link)) {
495 *parent = NULL;
496 return link;
497 }
498 } else {
499 link = &from;
Nick Piggindb64fe02008-10-18 20:27:03 -0700500 }
501
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -0700502 /*
503 * Go to the bottom of the tree. When we hit the last point
504 * we end up with parent rb_node and correct direction, i name
505 * it link, where the new va->rb_node will be attached to.
506 */
507 do {
508 tmp_va = rb_entry(*link, struct vmap_area, rb_node);
Nick Piggindb64fe02008-10-18 20:27:03 -0700509
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -0700510 /*
511 * During the traversal we also do some sanity check.
512 * Trigger the BUG() if there are sides(left/right)
513 * or full overlaps.
514 */
515 if (va->va_start < tmp_va->va_end &&
516 va->va_end <= tmp_va->va_start)
517 link = &(*link)->rb_left;
518 else if (va->va_end > tmp_va->va_start &&
519 va->va_start >= tmp_va->va_end)
520 link = &(*link)->rb_right;
521 else
522 BUG();
523 } while (*link);
524
525 *parent = &tmp_va->rb_node;
526 return link;
Nick Piggindb64fe02008-10-18 20:27:03 -0700527}
528
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -0700529static __always_inline struct list_head *
530get_va_next_sibling(struct rb_node *parent, struct rb_node **link)
531{
532 struct list_head *list;
Nick Piggindb64fe02008-10-18 20:27:03 -0700533
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -0700534 if (unlikely(!parent))
535 /*
536 * The red-black tree where we try to find VA neighbors
537 * before merging or inserting is empty, i.e. it means
538 * there is no free vmap space. Normally it does not
539 * happen but we handle this case anyway.
540 */
541 return NULL;
542
543 list = &rb_entry(parent, struct vmap_area, rb_node)->list;
544 return (&parent->rb_right == link ? list->next : list);
545}
546
547static __always_inline void
548link_va(struct vmap_area *va, struct rb_root *root,
549 struct rb_node *parent, struct rb_node **link, struct list_head *head)
550{
551 /*
552 * VA is still not in the list, but we can
553 * identify its future previous list_head node.
554 */
555 if (likely(parent)) {
556 head = &rb_entry(parent, struct vmap_area, rb_node)->list;
557 if (&parent->rb_right != link)
558 head = head->prev;
559 }
560
561 /* Insert to the rb-tree */
562 rb_link_node(&va->rb_node, parent, link);
563 if (root == &free_vmap_area_root) {
564 /*
565 * Some explanation here. Just perform simple insertion
566 * to the tree. We do not set va->subtree_max_size to
567 * its current size before calling rb_insert_augmented().
568 * It is because of we populate the tree from the bottom
569 * to parent levels when the node _is_ in the tree.
570 *
571 * Therefore we set subtree_max_size to zero after insertion,
572 * to let __augment_tree_propagate_from() puts everything to
573 * the correct order later on.
574 */
575 rb_insert_augmented(&va->rb_node,
576 root, &free_vmap_area_rb_augment_cb);
577 va->subtree_max_size = 0;
578 } else {
579 rb_insert_color(&va->rb_node, root);
580 }
581
582 /* Address-sort this list */
583 list_add(&va->list, head);
584}
585
586static __always_inline void
587unlink_va(struct vmap_area *va, struct rb_root *root)
588{
589 /*
590 * During merging a VA node can be empty, therefore
591 * not linked with the tree nor list. Just check it.
592 */
593 if (!RB_EMPTY_NODE(&va->rb_node)) {
594 if (root == &free_vmap_area_root)
595 rb_erase_augmented(&va->rb_node,
596 root, &free_vmap_area_rb_augment_cb);
597 else
598 rb_erase(&va->rb_node, root);
599
600 list_del(&va->list);
601 RB_CLEAR_NODE(&va->rb_node);
602 }
603}
604
Uladzislau Rezki (Sony)8087b172019-05-17 14:31:34 -0700605#if DEBUG_AUGMENT_PROPAGATE_CHECK
606static void
607augment_tree_propagate_check(struct rb_node *n)
608{
609 struct vmap_area *va;
610 struct rb_node *node;
611 unsigned long size;
612 bool found = false;
613
614 if (n == NULL)
615 return;
616
617 va = rb_entry(n, struct vmap_area, rb_node);
618 size = va->subtree_max_size;
619 node = n;
620
621 while (node) {
622 va = rb_entry(node, struct vmap_area, rb_node);
623
624 if (get_subtree_max_size(node->rb_left) == size) {
625 node = node->rb_left;
626 } else {
627 if (va_size(va) == size) {
628 found = true;
629 break;
630 }
631
632 node = node->rb_right;
633 }
634 }
635
636 if (!found) {
637 va = rb_entry(n, struct vmap_area, rb_node);
638 pr_emerg("tree is corrupted: %lu, %lu\n",
639 va_size(va), va->subtree_max_size);
640 }
641
642 augment_tree_propagate_check(n->rb_left);
643 augment_tree_propagate_check(n->rb_right);
644}
645#endif
646
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -0700647/*
648 * This function populates subtree_max_size from bottom to upper
649 * levels starting from VA point. The propagation must be done
650 * when VA size is modified by changing its va_start/va_end. Or
651 * in case of newly inserting of VA to the tree.
652 *
653 * It means that __augment_tree_propagate_from() must be called:
654 * - After VA has been inserted to the tree(free path);
655 * - After VA has been shrunk(allocation path);
656 * - After VA has been increased(merging path).
657 *
658 * Please note that, it does not mean that upper parent nodes
659 * and their subtree_max_size are recalculated all the time up
660 * to the root node.
661 *
662 * 4--8
663 * /\
664 * / \
665 * / \
666 * 2--2 8--8
667 *
668 * For example if we modify the node 4, shrinking it to 2, then
669 * no any modification is required. If we shrink the node 2 to 1
670 * its subtree_max_size is updated only, and set to 1. If we shrink
671 * the node 8 to 6, then its subtree_max_size is set to 6 and parent
672 * node becomes 4--6.
673 */
674static __always_inline void
675augment_tree_propagate_from(struct vmap_area *va)
676{
677 struct rb_node *node = &va->rb_node;
678 unsigned long new_va_sub_max_size;
679
680 while (node) {
681 va = rb_entry(node, struct vmap_area, rb_node);
682 new_va_sub_max_size = compute_subtree_max_size(va);
683
684 /*
685 * If the newly calculated maximum available size of the
686 * subtree is equal to the current one, then it means that
687 * the tree is propagated correctly. So we have to stop at
688 * this point to save cycles.
689 */
690 if (va->subtree_max_size == new_va_sub_max_size)
691 break;
692
693 va->subtree_max_size = new_va_sub_max_size;
694 node = rb_parent(&va->rb_node);
695 }
Uladzislau Rezki (Sony)8087b172019-05-17 14:31:34 -0700696
697#if DEBUG_AUGMENT_PROPAGATE_CHECK
698 augment_tree_propagate_check(free_vmap_area_root.rb_node);
699#endif
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -0700700}
701
702static void
703insert_vmap_area(struct vmap_area *va,
704 struct rb_root *root, struct list_head *head)
705{
706 struct rb_node **link;
707 struct rb_node *parent;
708
709 link = find_va_links(va, root, NULL, &parent);
710 link_va(va, root, parent, link, head);
711}
712
713static void
714insert_vmap_area_augment(struct vmap_area *va,
715 struct rb_node *from, struct rb_root *root,
716 struct list_head *head)
717{
718 struct rb_node **link;
719 struct rb_node *parent;
720
721 if (from)
722 link = find_va_links(va, NULL, from, &parent);
723 else
724 link = find_va_links(va, root, NULL, &parent);
725
726 link_va(va, root, parent, link, head);
727 augment_tree_propagate_from(va);
728}
729
730/*
731 * Merge de-allocated chunk of VA memory with previous
732 * and next free blocks. If coalesce is not done a new
733 * free area is inserted. If VA has been merged, it is
734 * freed.
735 */
736static __always_inline void
737merge_or_add_vmap_area(struct vmap_area *va,
738 struct rb_root *root, struct list_head *head)
739{
740 struct vmap_area *sibling;
741 struct list_head *next;
742 struct rb_node **link;
743 struct rb_node *parent;
744 bool merged = false;
745
746 /*
747 * Find a place in the tree where VA potentially will be
748 * inserted, unless it is merged with its sibling/siblings.
749 */
750 link = find_va_links(va, root, NULL, &parent);
751
752 /*
753 * Get next node of VA to check if merging can be done.
754 */
755 next = get_va_next_sibling(parent, link);
756 if (unlikely(next == NULL))
757 goto insert;
758
759 /*
760 * start end
761 * | |
762 * |<------VA------>|<-----Next----->|
763 * | |
764 * start end
765 */
766 if (next != head) {
767 sibling = list_entry(next, struct vmap_area, list);
768 if (sibling->va_start == va->va_end) {
769 sibling->va_start = va->va_start;
770
771 /* Check and update the tree if needed. */
772 augment_tree_propagate_from(sibling);
773
774 /* Remove this VA, it has been merged. */
775 unlink_va(va, root);
776
777 /* Free vmap_area object. */
778 kmem_cache_free(vmap_area_cachep, va);
779
780 /* Point to the new merged area. */
781 va = sibling;
782 merged = true;
783 }
784 }
785
786 /*
787 * start end
788 * | |
789 * |<-----Prev----->|<------VA------>|
790 * | |
791 * start end
792 */
793 if (next->prev != head) {
794 sibling = list_entry(next->prev, struct vmap_area, list);
795 if (sibling->va_end == va->va_start) {
796 sibling->va_end = va->va_end;
797
798 /* Check and update the tree if needed. */
799 augment_tree_propagate_from(sibling);
800
801 /* Remove this VA, it has been merged. */
802 unlink_va(va, root);
803
804 /* Free vmap_area object. */
805 kmem_cache_free(vmap_area_cachep, va);
806
807 return;
808 }
809 }
810
811insert:
812 if (!merged) {
813 link_va(va, root, parent, link, head);
814 augment_tree_propagate_from(va);
815 }
816}
817
818static __always_inline bool
819is_within_this_va(struct vmap_area *va, unsigned long size,
820 unsigned long align, unsigned long vstart)
821{
822 unsigned long nva_start_addr;
823
824 if (va->va_start > vstart)
825 nva_start_addr = ALIGN(va->va_start, align);
826 else
827 nva_start_addr = ALIGN(vstart, align);
828
829 /* Can be overflowed due to big size or alignment. */
830 if (nva_start_addr + size < nva_start_addr ||
831 nva_start_addr < vstart)
832 return false;
833
834 return (nva_start_addr + size <= va->va_end);
835}
836
837/*
838 * Find the first free block(lowest start address) in the tree,
839 * that will accomplish the request corresponding to passing
840 * parameters.
841 */
842static __always_inline struct vmap_area *
843find_vmap_lowest_match(unsigned long size,
844 unsigned long align, unsigned long vstart)
845{
846 struct vmap_area *va;
847 struct rb_node *node;
848 unsigned long length;
849
850 /* Start from the root. */
851 node = free_vmap_area_root.rb_node;
852
853 /* Adjust the search size for alignment overhead. */
854 length = size + align - 1;
855
856 while (node) {
857 va = rb_entry(node, struct vmap_area, rb_node);
858
859 if (get_subtree_max_size(node->rb_left) >= length &&
860 vstart < va->va_start) {
861 node = node->rb_left;
862 } else {
863 if (is_within_this_va(va, size, align, vstart))
864 return va;
865
866 /*
867 * Does not make sense to go deeper towards the right
868 * sub-tree if it does not have a free block that is
869 * equal or bigger to the requested search length.
870 */
871 if (get_subtree_max_size(node->rb_right) >= length) {
872 node = node->rb_right;
873 continue;
874 }
875
876 /*
877 * OK. We roll back and find the fist right sub-tree,
878 * that will satisfy the search criteria. It can happen
879 * only once due to "vstart" restriction.
880 */
881 while ((node = rb_parent(node))) {
882 va = rb_entry(node, struct vmap_area, rb_node);
883 if (is_within_this_va(va, size, align, vstart))
884 return va;
885
886 if (get_subtree_max_size(node->rb_right) >= length &&
887 vstart <= va->va_start) {
888 node = node->rb_right;
889 break;
890 }
891 }
892 }
893 }
894
895 return NULL;
896}
897
Uladzislau Rezki (Sony)367c1e42019-05-17 14:31:37 -0700898#if DEBUG_AUGMENT_LOWEST_MATCH_CHECK
899#include <linux/random.h>
900
901static struct vmap_area *
902find_vmap_lowest_linear_match(unsigned long size,
903 unsigned long align, unsigned long vstart)
904{
905 struct vmap_area *va;
906
907 list_for_each_entry(va, &free_vmap_area_list, list) {
908 if (!is_within_this_va(va, size, align, vstart))
909 continue;
910
911 return va;
912 }
913
914 return NULL;
915}
916
917static void
918find_vmap_lowest_match_check(unsigned long size)
919{
920 struct vmap_area *va_1, *va_2;
921 unsigned long vstart;
922 unsigned int rnd;
923
924 get_random_bytes(&rnd, sizeof(rnd));
925 vstart = VMALLOC_START + rnd;
926
927 va_1 = find_vmap_lowest_match(size, 1, vstart);
928 va_2 = find_vmap_lowest_linear_match(size, 1, vstart);
929
930 if (va_1 != va_2)
931 pr_emerg("not lowest: t: 0x%p, l: 0x%p, v: 0x%lx\n",
932 va_1, va_2, vstart);
933}
934#endif
935
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -0700936enum fit_type {
937 NOTHING_FIT = 0,
938 FL_FIT_TYPE = 1, /* full fit */
939 LE_FIT_TYPE = 2, /* left edge fit */
940 RE_FIT_TYPE = 3, /* right edge fit */
941 NE_FIT_TYPE = 4 /* no edge fit */
942};
943
944static __always_inline enum fit_type
945classify_va_fit_type(struct vmap_area *va,
946 unsigned long nva_start_addr, unsigned long size)
947{
948 enum fit_type type;
949
950 /* Check if it is within VA. */
951 if (nva_start_addr < va->va_start ||
952 nva_start_addr + size > va->va_end)
953 return NOTHING_FIT;
954
955 /* Now classify. */
956 if (va->va_start == nva_start_addr) {
957 if (va->va_end == nva_start_addr + size)
958 type = FL_FIT_TYPE;
959 else
960 type = LE_FIT_TYPE;
961 } else if (va->va_end == nva_start_addr + size) {
962 type = RE_FIT_TYPE;
963 } else {
964 type = NE_FIT_TYPE;
965 }
966
967 return type;
968}
969
970static __always_inline int
971adjust_va_to_fit_type(struct vmap_area *va,
972 unsigned long nva_start_addr, unsigned long size,
973 enum fit_type type)
974{
Arnd Bergmannaaac8ed2019-06-28 12:07:09 -0700975 struct vmap_area *lva = NULL;
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -0700976
977 if (type == FL_FIT_TYPE) {
978 /*
979 * No need to split VA, it fully fits.
980 *
981 * | |
982 * V NVA V
983 * |---------------|
984 */
985 unlink_va(va, &free_vmap_area_root);
986 kmem_cache_free(vmap_area_cachep, va);
987 } else if (type == LE_FIT_TYPE) {
988 /*
989 * Split left edge of fit VA.
990 *
991 * | |
992 * V NVA V R
993 * |-------|-------|
994 */
995 va->va_start += size;
996 } else if (type == RE_FIT_TYPE) {
997 /*
998 * Split right edge of fit VA.
999 *
1000 * | |
1001 * L V NVA V
1002 * |-------|-------|
1003 */
1004 va->va_end = nva_start_addr;
1005 } else if (type == NE_FIT_TYPE) {
1006 /*
1007 * Split no edge of fit VA.
1008 *
1009 * | |
1010 * L V NVA V R
1011 * |---|-------|---|
1012 */
1013 lva = kmem_cache_alloc(vmap_area_cachep, GFP_NOWAIT);
1014 if (unlikely(!lva))
1015 return -1;
1016
1017 /*
1018 * Build the remainder.
1019 */
1020 lva->va_start = va->va_start;
1021 lva->va_end = nva_start_addr;
1022
1023 /*
1024 * Shrink this VA to remaining size.
1025 */
1026 va->va_start = nva_start_addr + size;
1027 } else {
1028 return -1;
1029 }
1030
1031 if (type != FL_FIT_TYPE) {
1032 augment_tree_propagate_from(va);
1033
Arnd Bergmannaaac8ed2019-06-28 12:07:09 -07001034 if (lva) /* type == NE_FIT_TYPE */
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001035 insert_vmap_area_augment(lva, &va->rb_node,
1036 &free_vmap_area_root, &free_vmap_area_list);
1037 }
1038
1039 return 0;
1040}
1041
1042/*
1043 * Returns a start address of the newly allocated area, if success.
1044 * Otherwise a vend is returned that indicates failure.
1045 */
1046static __always_inline unsigned long
1047__alloc_vmap_area(unsigned long size, unsigned long align,
1048 unsigned long vstart, unsigned long vend, int node)
1049{
1050 unsigned long nva_start_addr;
1051 struct vmap_area *va;
1052 enum fit_type type;
1053 int ret;
1054
1055 va = find_vmap_lowest_match(size, align, vstart);
1056 if (unlikely(!va))
1057 return vend;
1058
1059 if (va->va_start > vstart)
1060 nva_start_addr = ALIGN(va->va_start, align);
1061 else
1062 nva_start_addr = ALIGN(vstart, align);
1063
1064 /* Check the "vend" restriction. */
1065 if (nva_start_addr + size > vend)
1066 return vend;
1067
1068 /* Classify what we have found. */
1069 type = classify_va_fit_type(va, nva_start_addr, size);
1070 if (WARN_ON_ONCE(type == NOTHING_FIT))
1071 return vend;
1072
1073 /* Update the free vmap_area. */
1074 ret = adjust_va_to_fit_type(va, nva_start_addr, size, type);
1075 if (ret)
1076 return vend;
1077
Uladzislau Rezki (Sony)367c1e42019-05-17 14:31:37 -07001078#if DEBUG_AUGMENT_LOWEST_MATCH_CHECK
1079 find_vmap_lowest_match_check(size);
1080#endif
1081
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001082 return nva_start_addr;
1083}
Chris Wilson4da56b92016-04-04 14:46:42 +01001084
Nick Piggindb64fe02008-10-18 20:27:03 -07001085/*
1086 * Allocate a region of KVA of the specified size and alignment, within the
1087 * vstart and vend.
1088 */
1089static struct vmap_area *alloc_vmap_area(unsigned long size,
1090 unsigned long align,
1091 unsigned long vstart, unsigned long vend,
1092 int node, gfp_t gfp_mask)
1093{
1094 struct vmap_area *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 unsigned long addr;
Nick Piggindb64fe02008-10-18 20:27:03 -07001096 int purged = 0;
1097
Nick Piggin77669702009-02-27 14:03:03 -08001098 BUG_ON(!size);
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08001099 BUG_ON(offset_in_page(size));
Nick Piggin89699602011-03-22 16:30:36 -07001100 BUG_ON(!is_power_of_2(align));
Nick Piggindb64fe02008-10-18 20:27:03 -07001101
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001102 if (unlikely(!vmap_initialized))
1103 return ERR_PTR(-EBUSY);
1104
Christoph Hellwig5803ed22016-12-12 16:44:20 -08001105 might_sleep();
Chris Wilson4da56b92016-04-04 14:46:42 +01001106
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001107 va = kmem_cache_alloc_node(vmap_area_cachep,
Nick Piggindb64fe02008-10-18 20:27:03 -07001108 gfp_mask & GFP_RECLAIM_MASK, node);
1109 if (unlikely(!va))
1110 return ERR_PTR(-ENOMEM);
1111
Catalin Marinas7f88f882013-11-12 15:07:45 -08001112 /*
1113 * Only scan the relevant parts containing pointers to other objects
1114 * to avoid false negatives.
1115 */
1116 kmemleak_scan_area(&va->rb_node, SIZE_MAX, gfp_mask & GFP_RECLAIM_MASK);
1117
Nick Piggindb64fe02008-10-18 20:27:03 -07001118retry:
1119 spin_lock(&vmap_area_lock);
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001120
Nick Piggin89699602011-03-22 16:30:36 -07001121 /*
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001122 * If an allocation fails, the "vend" address is
1123 * returned. Therefore trigger the overflow path.
Nick Piggin89699602011-03-22 16:30:36 -07001124 */
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001125 addr = __alloc_vmap_area(size, align, vstart, vend, node);
1126 if (unlikely(addr == vend))
Nick Piggin89699602011-03-22 16:30:36 -07001127 goto overflow;
Nick Piggindb64fe02008-10-18 20:27:03 -07001128
1129 va->va_start = addr;
1130 va->va_end = addr + size;
1131 va->flags = 0;
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001132 insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
1133
Nick Piggindb64fe02008-10-18 20:27:03 -07001134 spin_unlock(&vmap_area_lock);
1135
Wang Xiaoqiang61e16552016-01-15 16:57:19 -08001136 BUG_ON(!IS_ALIGNED(va->va_start, align));
Nick Piggin89699602011-03-22 16:30:36 -07001137 BUG_ON(va->va_start < vstart);
1138 BUG_ON(va->va_end > vend);
1139
Nick Piggindb64fe02008-10-18 20:27:03 -07001140 return va;
Nick Piggin89699602011-03-22 16:30:36 -07001141
1142overflow:
1143 spin_unlock(&vmap_area_lock);
1144 if (!purged) {
1145 purge_vmap_area_lazy();
1146 purged = 1;
1147 goto retry;
1148 }
Chris Wilson4da56b92016-04-04 14:46:42 +01001149
1150 if (gfpflags_allow_blocking(gfp_mask)) {
1151 unsigned long freed = 0;
1152 blocking_notifier_call_chain(&vmap_notify_list, 0, &freed);
1153 if (freed > 0) {
1154 purged = 0;
1155 goto retry;
1156 }
1157 }
1158
Florian Fainelli03497d72017-04-27 11:19:00 -07001159 if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit())
Joe Perches756a0252016-03-17 14:19:47 -07001160 pr_warn("vmap allocation for size %lu failed: use vmalloc=<size> to increase size\n",
1161 size);
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001162
1163 kmem_cache_free(vmap_area_cachep, va);
Nick Piggin89699602011-03-22 16:30:36 -07001164 return ERR_PTR(-EBUSY);
Nick Piggindb64fe02008-10-18 20:27:03 -07001165}
1166
Chris Wilson4da56b92016-04-04 14:46:42 +01001167int register_vmap_purge_notifier(struct notifier_block *nb)
1168{
1169 return blocking_notifier_chain_register(&vmap_notify_list, nb);
1170}
1171EXPORT_SYMBOL_GPL(register_vmap_purge_notifier);
1172
1173int unregister_vmap_purge_notifier(struct notifier_block *nb)
1174{
1175 return blocking_notifier_chain_unregister(&vmap_notify_list, nb);
1176}
1177EXPORT_SYMBOL_GPL(unregister_vmap_purge_notifier);
1178
Nick Piggindb64fe02008-10-18 20:27:03 -07001179static void __free_vmap_area(struct vmap_area *va)
1180{
1181 BUG_ON(RB_EMPTY_NODE(&va->rb_node));
Nick Piggin89699602011-03-22 16:30:36 -07001182
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001183 /*
1184 * Remove from the busy tree/list.
1185 */
1186 unlink_va(va, &vmap_area_root);
Nick Piggindb64fe02008-10-18 20:27:03 -07001187
Tejun Heoca23e402009-08-14 15:00:52 +09001188 /*
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001189 * Merge VA with its neighbors, otherwise just add it.
Tejun Heoca23e402009-08-14 15:00:52 +09001190 */
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001191 merge_or_add_vmap_area(va,
1192 &free_vmap_area_root, &free_vmap_area_list);
Nick Piggindb64fe02008-10-18 20:27:03 -07001193}
1194
1195/*
1196 * Free a region of KVA allocated by alloc_vmap_area
1197 */
1198static void free_vmap_area(struct vmap_area *va)
1199{
1200 spin_lock(&vmap_area_lock);
1201 __free_vmap_area(va);
1202 spin_unlock(&vmap_area_lock);
1203}
1204
1205/*
1206 * Clear the pagetable entries of a given vmap_area
1207 */
1208static void unmap_vmap_area(struct vmap_area *va)
1209{
1210 vunmap_page_range(va->va_start, va->va_end);
1211}
1212
1213/*
1214 * lazy_max_pages is the maximum amount of virtual address space we gather up
1215 * before attempting to purge with a TLB flush.
1216 *
1217 * There is a tradeoff here: a larger number will cover more kernel page tables
1218 * and take slightly longer to purge, but it will linearly reduce the number of
1219 * global TLB flushes that must be performed. It would seem natural to scale
1220 * this number up linearly with the number of CPUs (because vmapping activity
1221 * could also scale linearly with the number of CPUs), however it is likely
1222 * that in practice, workloads might be constrained in other ways that mean
1223 * vmap activity will not scale linearly with CPUs. Also, I want to be
1224 * conservative and not introduce a big latency on huge systems, so go with
1225 * a less aggressive log scale. It will still be an improvement over the old
1226 * code, and it will be simple to change the scale factor if we find that it
1227 * becomes a problem on bigger systems.
1228 */
1229static unsigned long lazy_max_pages(void)
1230{
1231 unsigned int log;
1232
1233 log = fls(num_online_cpus());
1234
Zhenhua Huang00af8482018-06-21 12:54:40 +08001235 return log * (1UL * CONFIG_VMAP_LAZY_PURGING_FACTOR *
1236 1024 * 1024 / PAGE_SIZE);
Nick Piggindb64fe02008-10-18 20:27:03 -07001237}
1238
Uladzislau Rezki (Sony)b127ecf2019-05-14 15:41:25 -07001239static atomic_long_t vmap_lazy_nr = ATOMIC_LONG_INIT(0);
Nick Piggindb64fe02008-10-18 20:27:03 -07001240
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001241/*
1242 * Serialize vmap purging. There is no actual criticial section protected
1243 * by this look, but we want to avoid concurrent calls for performance
1244 * reasons and to make the pcpu_get_vm_areas more deterministic.
1245 */
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001246static DEFINE_MUTEX(vmap_purge_lock);
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001247
Nick Piggin02b709d2010-02-01 22:25:57 +11001248/* for per-CPU blocks */
1249static void purge_fragmented_blocks_allcpus(void);
1250
Nick Piggindb64fe02008-10-18 20:27:03 -07001251/*
Cliff Wickman3ee48b62010-09-16 11:44:02 -05001252 * called before a call to iounmap() if the caller wants vm_area_struct's
1253 * immediately freed.
1254 */
1255void set_iounmap_nonlazy(void)
1256{
Uladzislau Rezki (Sony)b127ecf2019-05-14 15:41:25 -07001257 atomic_long_set(&vmap_lazy_nr, lazy_max_pages()+1);
Cliff Wickman3ee48b62010-09-16 11:44:02 -05001258}
1259
1260/*
Nick Piggindb64fe02008-10-18 20:27:03 -07001261 * Purges all lazily-freed vmap areas.
Nick Piggindb64fe02008-10-18 20:27:03 -07001262 */
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001263static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end)
Nick Piggindb64fe02008-10-18 20:27:03 -07001264{
Uladzislau Rezki (Sony)b127ecf2019-05-14 15:41:25 -07001265 unsigned long resched_threshold;
Chris Wilson80c4bd72016-05-20 16:57:38 -07001266 struct llist_node *valist;
Nick Piggindb64fe02008-10-18 20:27:03 -07001267 struct vmap_area *va;
Vegard Nossumcbb76672009-02-27 14:03:04 -08001268 struct vmap_area *n_va;
Qingqing Zhouae7c5d12020-08-20 23:21:11 +08001269 unsigned long flush_all_threshold = VMALLOC_END - VMALLOC_START;
Nick Piggindb64fe02008-10-18 20:27:03 -07001270
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001271 lockdep_assert_held(&vmap_purge_lock);
Nick Piggin02b709d2010-02-01 22:25:57 +11001272
Chris Wilson80c4bd72016-05-20 16:57:38 -07001273 valist = llist_del_all(&vmap_purge_list);
Uladzislau Rezki (Sony)af3fbc72019-05-14 15:41:22 -07001274 if (unlikely(valist == NULL))
1275 return false;
1276
1277 /*
1278 * TODO: to calculate a flush range without looping.
1279 * The list can be up to lazy_max_pages() elements.
1280 */
Chris Wilson80c4bd72016-05-20 16:57:38 -07001281 llist_for_each_entry(va, valist, purge_list) {
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001282 if (va->va_start < start)
1283 start = va->va_start;
1284 if (va->va_end > end)
1285 end = va->va_end;
Nick Piggindb64fe02008-10-18 20:27:03 -07001286 }
Nick Piggindb64fe02008-10-18 20:27:03 -07001287
Qingqing Zhouae7c5d12020-08-20 23:21:11 +08001288 if (end - start <= flush_all_threshold)
1289 flush_tlb_kernel_range(start, end);
1290 else
1291 flush_tlb_all();
Uladzislau Rezki (Sony)b127ecf2019-05-14 15:41:25 -07001292 resched_threshold = lazy_max_pages() << 1;
Nick Piggindb64fe02008-10-18 20:27:03 -07001293
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001294 spin_lock(&vmap_area_lock);
Joel Fernandes763b2182016-12-12 16:44:26 -08001295 llist_for_each_entry_safe(va, n_va, valist, purge_list) {
Uladzislau Rezki (Sony)b127ecf2019-05-14 15:41:25 -07001296 unsigned long nr = (va->va_end - va->va_start) >> PAGE_SHIFT;
Joel Fernandes763b2182016-12-12 16:44:26 -08001297
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001298 __free_vmap_area(va);
Uladzislau Rezki (Sony)b127ecf2019-05-14 15:41:25 -07001299 atomic_long_sub(nr, &vmap_lazy_nr);
Uladzislau Rezki (Sony)af3fbc72019-05-14 15:41:22 -07001300
Uladzislau Rezki (Sony)b127ecf2019-05-14 15:41:25 -07001301 if (atomic_long_read(&vmap_lazy_nr) < resched_threshold)
Uladzislau Rezki (Sony)af3fbc72019-05-14 15:41:22 -07001302 cond_resched_lock(&vmap_area_lock);
Joel Fernandes763b2182016-12-12 16:44:26 -08001303 }
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001304 spin_unlock(&vmap_area_lock);
1305 return true;
Nick Piggindb64fe02008-10-18 20:27:03 -07001306}
1307
1308/*
Nick Piggin496850e2008-11-19 15:36:33 -08001309 * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
1310 * is already purging.
1311 */
1312static void try_purge_vmap_area_lazy(void)
1313{
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001314 if (mutex_trylock(&vmap_purge_lock)) {
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001315 __purge_vmap_area_lazy(ULONG_MAX, 0);
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001316 mutex_unlock(&vmap_purge_lock);
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001317 }
Nick Piggin496850e2008-11-19 15:36:33 -08001318}
1319
1320/*
Nick Piggindb64fe02008-10-18 20:27:03 -07001321 * Kick off a purge of the outstanding lazy areas.
1322 */
1323static void purge_vmap_area_lazy(void)
1324{
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001325 mutex_lock(&vmap_purge_lock);
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001326 purge_fragmented_blocks_allcpus();
1327 __purge_vmap_area_lazy(ULONG_MAX, 0);
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001328 mutex_unlock(&vmap_purge_lock);
Nick Piggindb64fe02008-10-18 20:27:03 -07001329}
1330
1331/*
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -08001332 * Free a vmap area, caller ensuring that the area has been unmapped
1333 * and flush_cache_vunmap had been called for the correct range
1334 * previously.
Nick Piggindb64fe02008-10-18 20:27:03 -07001335 */
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -08001336static void free_vmap_area_noflush(struct vmap_area *va)
Nick Piggindb64fe02008-10-18 20:27:03 -07001337{
Uladzislau Rezki (Sony)b127ecf2019-05-14 15:41:25 -07001338 unsigned long nr_lazy;
Chris Wilson80c4bd72016-05-20 16:57:38 -07001339
Uladzislau Rezki (Sony)b127ecf2019-05-14 15:41:25 -07001340 nr_lazy = atomic_long_add_return((va->va_end - va->va_start) >>
1341 PAGE_SHIFT, &vmap_lazy_nr);
Chris Wilson80c4bd72016-05-20 16:57:38 -07001342
1343 /* After this point, we may free va at any time */
1344 llist_add(&va->purge_list, &vmap_purge_list);
1345
1346 if (unlikely(nr_lazy > lazy_max_pages()))
Nick Piggin496850e2008-11-19 15:36:33 -08001347 try_purge_vmap_area_lazy();
Nick Piggindb64fe02008-10-18 20:27:03 -07001348}
1349
Nick Pigginb29acbd2008-12-01 13:13:47 -08001350/*
1351 * Free and unmap a vmap area
1352 */
1353static void free_unmap_vmap_area(struct vmap_area *va)
1354{
1355 flush_cache_vunmap(va->va_start, va->va_end);
Christoph Hellwigc8eef012016-12-12 16:44:01 -08001356 unmap_vmap_area(va);
Chintan Pandya82a2e922018-06-07 17:06:46 -07001357 if (debug_pagealloc_enabled())
1358 flush_tlb_kernel_range(va->va_start, va->va_end);
1359
Christoph Hellwigc8eef012016-12-12 16:44:01 -08001360 free_vmap_area_noflush(va);
Nick Pigginb29acbd2008-12-01 13:13:47 -08001361}
1362
Nick Piggindb64fe02008-10-18 20:27:03 -07001363static struct vmap_area *find_vmap_area(unsigned long addr)
1364{
1365 struct vmap_area *va;
1366
1367 spin_lock(&vmap_area_lock);
1368 va = __find_vmap_area(addr);
1369 spin_unlock(&vmap_area_lock);
1370
1371 return va;
1372}
1373
Nick Piggindb64fe02008-10-18 20:27:03 -07001374/*** Per cpu kva allocator ***/
1375
1376/*
1377 * vmap space is limited especially on 32 bit architectures. Ensure there is
1378 * room for at least 16 percpu vmap blocks per CPU.
1379 */
1380/*
1381 * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
1382 * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess
1383 * instead (we just need a rough idea)
1384 */
1385#if BITS_PER_LONG == 32
1386#define VMALLOC_SPACE (128UL*1024*1024)
1387#else
1388#define VMALLOC_SPACE (128UL*1024*1024*1024)
1389#endif
1390
1391#define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE)
1392#define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */
1393#define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */
1394#define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2)
1395#define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */
1396#define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */
Clemens Ladischf982f912011-06-21 22:09:50 +02001397#define VMAP_BBMAP_BITS \
1398 VMAP_MIN(VMAP_BBMAP_BITS_MAX, \
1399 VMAP_MAX(VMAP_BBMAP_BITS_MIN, \
1400 VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16))
Nick Piggindb64fe02008-10-18 20:27:03 -07001401
1402#define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE)
1403
1404struct vmap_block_queue {
1405 spinlock_t lock;
1406 struct list_head free;
Nick Piggindb64fe02008-10-18 20:27:03 -07001407};
1408
1409struct vmap_block {
1410 spinlock_t lock;
1411 struct vmap_area *va;
Nick Piggindb64fe02008-10-18 20:27:03 -07001412 unsigned long free, dirty;
Roman Pen7d61bfe2015-04-15 16:13:55 -07001413 unsigned long dirty_min, dirty_max; /*< dirty range */
Nick Pigginde560422010-02-01 22:24:18 +11001414 struct list_head free_list;
1415 struct rcu_head rcu_head;
Nick Piggin02b709d2010-02-01 22:25:57 +11001416 struct list_head purge;
Nick Piggindb64fe02008-10-18 20:27:03 -07001417};
1418
1419/* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
1420static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
1421
1422/*
1423 * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block
1424 * in the free path. Could get rid of this if we change the API to return a
1425 * "cookie" from alloc, to be passed to free. But no big deal yet.
1426 */
1427static DEFINE_SPINLOCK(vmap_block_tree_lock);
1428static RADIX_TREE(vmap_block_tree, GFP_ATOMIC);
1429
1430/*
1431 * We should probably have a fallback mechanism to allocate virtual memory
1432 * out of partially filled vmap blocks. However vmap block sizing should be
1433 * fairly reasonable according to the vmalloc size, so it shouldn't be a
1434 * big problem.
1435 */
1436
1437static unsigned long addr_to_vb_idx(unsigned long addr)
1438{
1439 addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
1440 addr /= VMAP_BLOCK_SIZE;
1441 return addr;
1442}
1443
Roman Pencf725ce2015-04-15 16:13:52 -07001444static void *vmap_block_vaddr(unsigned long va_start, unsigned long pages_off)
1445{
1446 unsigned long addr;
1447
1448 addr = va_start + (pages_off << PAGE_SHIFT);
1449 BUG_ON(addr_to_vb_idx(addr) != addr_to_vb_idx(va_start));
1450 return (void *)addr;
1451}
1452
1453/**
1454 * new_vmap_block - allocates new vmap_block and occupies 2^order pages in this
1455 * block. Of course pages number can't exceed VMAP_BBMAP_BITS
1456 * @order: how many 2^order pages should be occupied in newly allocated block
1457 * @gfp_mask: flags for the page level allocator
1458 *
1459 * Returns: virtual address in a newly allocated block or ERR_PTR(-errno)
1460 */
1461static void *new_vmap_block(unsigned int order, gfp_t gfp_mask)
Nick Piggindb64fe02008-10-18 20:27:03 -07001462{
1463 struct vmap_block_queue *vbq;
1464 struct vmap_block *vb;
1465 struct vmap_area *va;
1466 unsigned long vb_idx;
1467 int node, err;
Roman Pencf725ce2015-04-15 16:13:52 -07001468 void *vaddr;
Nick Piggindb64fe02008-10-18 20:27:03 -07001469
1470 node = numa_node_id();
1471
1472 vb = kmalloc_node(sizeof(struct vmap_block),
1473 gfp_mask & GFP_RECLAIM_MASK, node);
1474 if (unlikely(!vb))
1475 return ERR_PTR(-ENOMEM);
1476
1477 va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
1478 VMALLOC_START, VMALLOC_END,
1479 node, gfp_mask);
Tobias Klauserddf9c6d42011-01-13 15:46:15 -08001480 if (IS_ERR(va)) {
Nick Piggindb64fe02008-10-18 20:27:03 -07001481 kfree(vb);
Julia Lawalle7d86342010-08-09 17:18:28 -07001482 return ERR_CAST(va);
Nick Piggindb64fe02008-10-18 20:27:03 -07001483 }
1484
1485 err = radix_tree_preload(gfp_mask);
1486 if (unlikely(err)) {
1487 kfree(vb);
1488 free_vmap_area(va);
1489 return ERR_PTR(err);
1490 }
1491
Roman Pencf725ce2015-04-15 16:13:52 -07001492 vaddr = vmap_block_vaddr(va->va_start, 0);
Nick Piggindb64fe02008-10-18 20:27:03 -07001493 spin_lock_init(&vb->lock);
1494 vb->va = va;
Roman Pencf725ce2015-04-15 16:13:52 -07001495 /* At least something should be left free */
1496 BUG_ON(VMAP_BBMAP_BITS <= (1UL << order));
1497 vb->free = VMAP_BBMAP_BITS - (1UL << order);
Nick Piggindb64fe02008-10-18 20:27:03 -07001498 vb->dirty = 0;
Roman Pen7d61bfe2015-04-15 16:13:55 -07001499 vb->dirty_min = VMAP_BBMAP_BITS;
1500 vb->dirty_max = 0;
Nick Piggindb64fe02008-10-18 20:27:03 -07001501 INIT_LIST_HEAD(&vb->free_list);
Nick Piggindb64fe02008-10-18 20:27:03 -07001502
1503 vb_idx = addr_to_vb_idx(va->va_start);
1504 spin_lock(&vmap_block_tree_lock);
1505 err = radix_tree_insert(&vmap_block_tree, vb_idx, vb);
1506 spin_unlock(&vmap_block_tree_lock);
1507 BUG_ON(err);
1508 radix_tree_preload_end();
1509
1510 vbq = &get_cpu_var(vmap_block_queue);
Nick Piggindb64fe02008-10-18 20:27:03 -07001511 spin_lock(&vbq->lock);
Roman Pen68ac5462015-04-15 16:13:48 -07001512 list_add_tail_rcu(&vb->free_list, &vbq->free);
Nick Piggindb64fe02008-10-18 20:27:03 -07001513 spin_unlock(&vbq->lock);
Tejun Heo3f04ba82009-10-29 22:34:12 +09001514 put_cpu_var(vmap_block_queue);
Nick Piggindb64fe02008-10-18 20:27:03 -07001515
Roman Pencf725ce2015-04-15 16:13:52 -07001516 return vaddr;
Nick Piggindb64fe02008-10-18 20:27:03 -07001517}
1518
Nick Piggindb64fe02008-10-18 20:27:03 -07001519static void free_vmap_block(struct vmap_block *vb)
1520{
1521 struct vmap_block *tmp;
1522 unsigned long vb_idx;
1523
Nick Piggindb64fe02008-10-18 20:27:03 -07001524 vb_idx = addr_to_vb_idx(vb->va->va_start);
1525 spin_lock(&vmap_block_tree_lock);
1526 tmp = radix_tree_delete(&vmap_block_tree, vb_idx);
1527 spin_unlock(&vmap_block_tree_lock);
1528 BUG_ON(tmp != vb);
1529
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -08001530 free_vmap_area_noflush(vb->va);
Lai Jiangshan22a3c7d2011-03-18 12:13:08 +08001531 kfree_rcu(vb, rcu_head);
Nick Piggindb64fe02008-10-18 20:27:03 -07001532}
1533
Nick Piggin02b709d2010-02-01 22:25:57 +11001534static void purge_fragmented_blocks(int cpu)
1535{
1536 LIST_HEAD(purge);
1537 struct vmap_block *vb;
1538 struct vmap_block *n_vb;
1539 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
1540
1541 rcu_read_lock();
1542 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
1543
1544 if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS))
1545 continue;
1546
1547 spin_lock(&vb->lock);
1548 if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) {
1549 vb->free = 0; /* prevent further allocs after releasing lock */
1550 vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */
Roman Pen7d61bfe2015-04-15 16:13:55 -07001551 vb->dirty_min = 0;
1552 vb->dirty_max = VMAP_BBMAP_BITS;
Nick Piggin02b709d2010-02-01 22:25:57 +11001553 spin_lock(&vbq->lock);
1554 list_del_rcu(&vb->free_list);
1555 spin_unlock(&vbq->lock);
1556 spin_unlock(&vb->lock);
1557 list_add_tail(&vb->purge, &purge);
1558 } else
1559 spin_unlock(&vb->lock);
1560 }
1561 rcu_read_unlock();
1562
1563 list_for_each_entry_safe(vb, n_vb, &purge, purge) {
1564 list_del(&vb->purge);
1565 free_vmap_block(vb);
1566 }
1567}
1568
Nick Piggin02b709d2010-02-01 22:25:57 +11001569static void purge_fragmented_blocks_allcpus(void)
1570{
1571 int cpu;
1572
1573 for_each_possible_cpu(cpu)
1574 purge_fragmented_blocks(cpu);
1575}
1576
Nick Piggindb64fe02008-10-18 20:27:03 -07001577static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
1578{
1579 struct vmap_block_queue *vbq;
1580 struct vmap_block *vb;
Roman Pencf725ce2015-04-15 16:13:52 -07001581 void *vaddr = NULL;
Nick Piggindb64fe02008-10-18 20:27:03 -07001582 unsigned int order;
1583
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08001584 BUG_ON(offset_in_page(size));
Nick Piggindb64fe02008-10-18 20:27:03 -07001585 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
Jan Karaaa91c4d2012-07-31 16:41:37 -07001586 if (WARN_ON(size == 0)) {
1587 /*
1588 * Allocating 0 bytes isn't what caller wants since
1589 * get_order(0) returns funny result. Just warn and terminate
1590 * early.
1591 */
1592 return NULL;
1593 }
Nick Piggindb64fe02008-10-18 20:27:03 -07001594 order = get_order(size);
1595
Nick Piggindb64fe02008-10-18 20:27:03 -07001596 rcu_read_lock();
1597 vbq = &get_cpu_var(vmap_block_queue);
1598 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
Roman Pencf725ce2015-04-15 16:13:52 -07001599 unsigned long pages_off;
Nick Piggindb64fe02008-10-18 20:27:03 -07001600
1601 spin_lock(&vb->lock);
Roman Pencf725ce2015-04-15 16:13:52 -07001602 if (vb->free < (1UL << order)) {
1603 spin_unlock(&vb->lock);
1604 continue;
1605 }
Nick Piggin02b709d2010-02-01 22:25:57 +11001606
Roman Pencf725ce2015-04-15 16:13:52 -07001607 pages_off = VMAP_BBMAP_BITS - vb->free;
1608 vaddr = vmap_block_vaddr(vb->va->va_start, pages_off);
Nick Piggin02b709d2010-02-01 22:25:57 +11001609 vb->free -= 1UL << order;
1610 if (vb->free == 0) {
1611 spin_lock(&vbq->lock);
1612 list_del_rcu(&vb->free_list);
1613 spin_unlock(&vbq->lock);
Nick Piggindb64fe02008-10-18 20:27:03 -07001614 }
Roman Pencf725ce2015-04-15 16:13:52 -07001615
Nick Piggindb64fe02008-10-18 20:27:03 -07001616 spin_unlock(&vb->lock);
Nick Piggin02b709d2010-02-01 22:25:57 +11001617 break;
Nick Piggindb64fe02008-10-18 20:27:03 -07001618 }
Nick Piggin02b709d2010-02-01 22:25:57 +11001619
Tejun Heo3f04ba82009-10-29 22:34:12 +09001620 put_cpu_var(vmap_block_queue);
Nick Piggindb64fe02008-10-18 20:27:03 -07001621 rcu_read_unlock();
1622
Roman Pencf725ce2015-04-15 16:13:52 -07001623 /* Allocate new block if nothing was found */
1624 if (!vaddr)
1625 vaddr = new_vmap_block(order, gfp_mask);
Nick Piggindb64fe02008-10-18 20:27:03 -07001626
Roman Pencf725ce2015-04-15 16:13:52 -07001627 return vaddr;
Nick Piggindb64fe02008-10-18 20:27:03 -07001628}
1629
1630static void vb_free(const void *addr, unsigned long size)
1631{
1632 unsigned long offset;
1633 unsigned long vb_idx;
1634 unsigned int order;
1635 struct vmap_block *vb;
1636
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08001637 BUG_ON(offset_in_page(size));
Nick Piggindb64fe02008-10-18 20:27:03 -07001638 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
Nick Pigginb29acbd2008-12-01 13:13:47 -08001639
1640 flush_cache_vunmap((unsigned long)addr, (unsigned long)addr + size);
1641
Nick Piggindb64fe02008-10-18 20:27:03 -07001642 order = get_order(size);
1643
1644 offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1);
Roman Pen7d61bfe2015-04-15 16:13:55 -07001645 offset >>= PAGE_SHIFT;
Nick Piggindb64fe02008-10-18 20:27:03 -07001646
1647 vb_idx = addr_to_vb_idx((unsigned long)addr);
1648 rcu_read_lock();
1649 vb = radix_tree_lookup(&vmap_block_tree, vb_idx);
1650 rcu_read_unlock();
1651 BUG_ON(!vb);
1652
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -08001653 vunmap_page_range((unsigned long)addr, (unsigned long)addr + size);
1654
Chintan Pandya82a2e922018-06-07 17:06:46 -07001655 if (debug_pagealloc_enabled())
1656 flush_tlb_kernel_range((unsigned long)addr,
1657 (unsigned long)addr + size);
1658
Nick Piggindb64fe02008-10-18 20:27:03 -07001659 spin_lock(&vb->lock);
Roman Pen7d61bfe2015-04-15 16:13:55 -07001660
1661 /* Expand dirty range */
1662 vb->dirty_min = min(vb->dirty_min, offset);
1663 vb->dirty_max = max(vb->dirty_max, offset + (1UL << order));
MinChan Kimd0868172009-03-31 15:19:26 -07001664
Nick Piggindb64fe02008-10-18 20:27:03 -07001665 vb->dirty += 1UL << order;
1666 if (vb->dirty == VMAP_BBMAP_BITS) {
Nick Pigginde560422010-02-01 22:24:18 +11001667 BUG_ON(vb->free);
Nick Piggindb64fe02008-10-18 20:27:03 -07001668 spin_unlock(&vb->lock);
1669 free_vmap_block(vb);
1670 } else
1671 spin_unlock(&vb->lock);
1672}
1673
1674/**
1675 * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
1676 *
1677 * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
1678 * to amortize TLB flushing overheads. What this means is that any page you
1679 * have now, may, in a former life, have been mapped into kernel virtual
1680 * address by the vmap layer and so there might be some CPUs with TLB entries
1681 * still referencing that page (additional to the regular 1:1 kernel mapping).
1682 *
1683 * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
1684 * be sure that none of the pages we have control over will have any aliases
1685 * from the vmap layer.
1686 */
1687void vm_unmap_aliases(void)
1688{
1689 unsigned long start = ULONG_MAX, end = 0;
1690 int cpu;
1691 int flush = 0;
1692
Jeremy Fitzhardinge9b463332008-10-28 19:22:34 +11001693 if (unlikely(!vmap_initialized))
1694 return;
1695
Christoph Hellwig5803ed22016-12-12 16:44:20 -08001696 might_sleep();
1697
Nick Piggindb64fe02008-10-18 20:27:03 -07001698 for_each_possible_cpu(cpu) {
1699 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
1700 struct vmap_block *vb;
1701
1702 rcu_read_lock();
1703 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
Nick Piggindb64fe02008-10-18 20:27:03 -07001704 spin_lock(&vb->lock);
Roman Pen7d61bfe2015-04-15 16:13:55 -07001705 if (vb->dirty) {
1706 unsigned long va_start = vb->va->va_start;
Nick Piggindb64fe02008-10-18 20:27:03 -07001707 unsigned long s, e;
Joonsoo Kimb136be5e2013-09-11 14:21:40 -07001708
Roman Pen7d61bfe2015-04-15 16:13:55 -07001709 s = va_start + (vb->dirty_min << PAGE_SHIFT);
1710 e = va_start + (vb->dirty_max << PAGE_SHIFT);
Nick Piggindb64fe02008-10-18 20:27:03 -07001711
Roman Pen7d61bfe2015-04-15 16:13:55 -07001712 start = min(s, start);
1713 end = max(e, end);
1714
Nick Piggindb64fe02008-10-18 20:27:03 -07001715 flush = 1;
Nick Piggindb64fe02008-10-18 20:27:03 -07001716 }
1717 spin_unlock(&vb->lock);
1718 }
1719 rcu_read_unlock();
1720 }
1721
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001722 mutex_lock(&vmap_purge_lock);
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001723 purge_fragmented_blocks_allcpus();
1724 if (!__purge_vmap_area_lazy(start, end) && flush)
1725 flush_tlb_kernel_range(start, end);
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001726 mutex_unlock(&vmap_purge_lock);
Nick Piggindb64fe02008-10-18 20:27:03 -07001727}
1728EXPORT_SYMBOL_GPL(vm_unmap_aliases);
1729
1730/**
1731 * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
1732 * @mem: the pointer returned by vm_map_ram
1733 * @count: the count passed to that vm_map_ram call (cannot unmap partial)
1734 */
1735void vm_unmap_ram(const void *mem, unsigned int count)
1736{
Guillermo Julián Moreno65ee03c2016-06-03 14:55:33 -07001737 unsigned long size = (unsigned long)count << PAGE_SHIFT;
Nick Piggindb64fe02008-10-18 20:27:03 -07001738 unsigned long addr = (unsigned long)mem;
Christoph Hellwig9c3acf62016-12-12 16:44:04 -08001739 struct vmap_area *va;
Nick Piggindb64fe02008-10-18 20:27:03 -07001740
Christoph Hellwig5803ed22016-12-12 16:44:20 -08001741 might_sleep();
Nick Piggindb64fe02008-10-18 20:27:03 -07001742 BUG_ON(!addr);
1743 BUG_ON(addr < VMALLOC_START);
1744 BUG_ON(addr > VMALLOC_END);
Shawn Lina1c0b1a2016-03-17 14:20:37 -07001745 BUG_ON(!PAGE_ALIGNED(addr));
Nick Piggindb64fe02008-10-18 20:27:03 -07001746
Christoph Hellwig9c3acf62016-12-12 16:44:04 -08001747 if (likely(count <= VMAP_MAX_ALLOC)) {
Chintan Pandya05e3ff92018-06-07 17:06:53 -07001748 debug_check_no_locks_freed(mem, size);
Nick Piggindb64fe02008-10-18 20:27:03 -07001749 vb_free(mem, size);
Christoph Hellwig9c3acf62016-12-12 16:44:04 -08001750 return;
1751 }
1752
1753 va = find_vmap_area(addr);
1754 BUG_ON(!va);
Chintan Pandya05e3ff92018-06-07 17:06:53 -07001755 debug_check_no_locks_freed((void *)va->va_start,
1756 (va->va_end - va->va_start));
Christoph Hellwig9c3acf62016-12-12 16:44:04 -08001757 free_unmap_vmap_area(va);
Nick Piggindb64fe02008-10-18 20:27:03 -07001758}
1759EXPORT_SYMBOL(vm_unmap_ram);
1760
1761/**
1762 * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
1763 * @pages: an array of pointers to the pages to be mapped
1764 * @count: number of pages
1765 * @node: prefer to allocate data structures on this node
1766 * @prot: memory protection to use. PAGE_KERNEL for regular RAM
Randy Dunlape99c97a2008-10-29 14:01:09 -07001767 *
Gioh Kim36437632014-04-07 15:37:37 -07001768 * If you use this function for less than VMAP_MAX_ALLOC pages, it could be
1769 * faster than vmap so it's good. But if you mix long-life and short-life
1770 * objects with vm_map_ram(), it could consume lots of address space through
1771 * fragmentation (especially on a 32bit machine). You could see failures in
1772 * the end. Please use this function for short-lived objects.
1773 *
Randy Dunlape99c97a2008-10-29 14:01:09 -07001774 * Returns: a pointer to the address that has been mapped, or %NULL on failure
Nick Piggindb64fe02008-10-18 20:27:03 -07001775 */
1776void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
1777{
Guillermo Julián Moreno65ee03c2016-06-03 14:55:33 -07001778 unsigned long size = (unsigned long)count << PAGE_SHIFT;
Nick Piggindb64fe02008-10-18 20:27:03 -07001779 unsigned long addr;
1780 void *mem;
1781
1782 if (likely(count <= VMAP_MAX_ALLOC)) {
1783 mem = vb_alloc(size, GFP_KERNEL);
1784 if (IS_ERR(mem))
1785 return NULL;
1786 addr = (unsigned long)mem;
1787 } else {
1788 struct vmap_area *va;
1789 va = alloc_vmap_area(size, PAGE_SIZE,
1790 VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
1791 if (IS_ERR(va))
1792 return NULL;
1793
1794 addr = va->va_start;
1795 mem = (void *)addr;
1796 }
1797 if (vmap_page_range(addr, addr + size, prot, pages) < 0) {
1798 vm_unmap_ram(mem, count);
1799 return NULL;
1800 }
1801 return mem;
1802}
1803EXPORT_SYMBOL(vm_map_ram);
1804
Joonsoo Kim4341fa42013-04-29 15:07:39 -07001805static struct vm_struct *vmlist __initdata;
Susheel Khiani4be85ac2015-09-08 15:05:43 +05301806
1807/**
1808 * vm_area_check_early - check if vmap area is already mapped
1809 * @vm: vm_struct to be checked
1810 *
1811 * This function is used to check if the vmap area has been
1812 * mapped already. @vm->addr, @vm->size and @vm->flags should
1813 * contain proper values.
1814 *
1815 */
1816int __init vm_area_check_early(struct vm_struct *vm)
1817{
1818 struct vm_struct *tmp, **p;
1819
1820 BUG_ON(vmap_initialized);
1821 for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
1822 if (tmp->addr >= vm->addr) {
1823 if (tmp->addr < vm->addr + vm->size)
1824 return 1;
1825 } else {
1826 if (tmp->addr + tmp->size > vm->addr)
1827 return 1;
1828 }
1829 }
1830 return 0;
1831}
1832
Tejun Heof0aa6612009-02-20 16:29:08 +09001833/**
Nicolas Pitrebe9b7332011-08-25 00:24:21 -04001834 * vm_area_add_early - add vmap area early during boot
1835 * @vm: vm_struct to add
1836 *
1837 * This function is used to add fixed kernel vm area to vmlist before
1838 * vmalloc_init() is called. @vm->addr, @vm->size, and @vm->flags
1839 * should contain proper values and the other fields should be zero.
1840 *
1841 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1842 */
1843void __init vm_area_add_early(struct vm_struct *vm)
1844{
1845 struct vm_struct *tmp, **p;
1846
1847 BUG_ON(vmap_initialized);
1848 for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
1849 if (tmp->addr >= vm->addr) {
1850 BUG_ON(tmp->addr < vm->addr + vm->size);
1851 break;
1852 } else
1853 BUG_ON(tmp->addr + tmp->size > vm->addr);
1854 }
1855 vm->next = *p;
1856 *p = vm;
1857}
1858
1859/**
Tejun Heof0aa6612009-02-20 16:29:08 +09001860 * vm_area_register_early - register vmap area early during boot
1861 * @vm: vm_struct to register
Tejun Heoc0c0a292009-02-24 11:57:21 +09001862 * @align: requested alignment
Tejun Heof0aa6612009-02-20 16:29:08 +09001863 *
1864 * This function is used to register kernel vm area before
1865 * vmalloc_init() is called. @vm->size and @vm->flags should contain
1866 * proper values on entry and other fields should be zero. On return,
1867 * vm->addr contains the allocated address.
1868 *
1869 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1870 */
Tejun Heoc0c0a292009-02-24 11:57:21 +09001871void __init vm_area_register_early(struct vm_struct *vm, size_t align)
Tejun Heof0aa6612009-02-20 16:29:08 +09001872{
1873 static size_t vm_init_off __initdata;
Tejun Heoc0c0a292009-02-24 11:57:21 +09001874 unsigned long addr;
Tejun Heof0aa6612009-02-20 16:29:08 +09001875
Tejun Heoc0c0a292009-02-24 11:57:21 +09001876 addr = ALIGN(VMALLOC_START + vm_init_off, align);
1877 vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START;
1878
1879 vm->addr = (void *)addr;
Tejun Heof0aa6612009-02-20 16:29:08 +09001880
Nicolas Pitrebe9b7332011-08-25 00:24:21 -04001881 vm_area_add_early(vm);
Tejun Heof0aa6612009-02-20 16:29:08 +09001882}
1883
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001884static void vmap_init_free_space(void)
1885{
1886 unsigned long vmap_start = 1;
1887 const unsigned long vmap_end = ULONG_MAX;
1888 struct vmap_area *busy, *free;
1889
1890 /*
1891 * B F B B B F
1892 * -|-----|.....|-----|-----|-----|.....|-
1893 * | The KVA space |
1894 * |<--------------------------------->|
1895 */
1896 list_for_each_entry(busy, &vmap_area_list, list) {
1897 if (busy->va_start - vmap_start > 0) {
1898 free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
1899 if (!WARN_ON_ONCE(!free)) {
1900 free->va_start = vmap_start;
1901 free->va_end = busy->va_start;
1902
1903 insert_vmap_area_augment(free, NULL,
1904 &free_vmap_area_root,
1905 &free_vmap_area_list);
1906 }
1907 }
1908
1909 vmap_start = busy->va_end;
1910 }
1911
1912 if (vmap_end - vmap_start > 0) {
1913 free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
1914 if (!WARN_ON_ONCE(!free)) {
1915 free->va_start = vmap_start;
1916 free->va_end = vmap_end;
1917
1918 insert_vmap_area_augment(free, NULL,
1919 &free_vmap_area_root,
1920 &free_vmap_area_list);
1921 }
1922 }
1923}
1924
Nick Piggindb64fe02008-10-18 20:27:03 -07001925void __init vmalloc_init(void)
1926{
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08001927 struct vmap_area *va;
1928 struct vm_struct *tmp;
Nick Piggindb64fe02008-10-18 20:27:03 -07001929 int i;
1930
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001931 /*
1932 * Create the cache for vmap_area objects.
1933 */
1934 vmap_area_cachep = KMEM_CACHE(vmap_area, SLAB_PANIC);
1935
Nick Piggindb64fe02008-10-18 20:27:03 -07001936 for_each_possible_cpu(i) {
1937 struct vmap_block_queue *vbq;
Al Viro32fcfd42013-03-10 20:14:08 -04001938 struct vfree_deferred *p;
Nick Piggindb64fe02008-10-18 20:27:03 -07001939
1940 vbq = &per_cpu(vmap_block_queue, i);
1941 spin_lock_init(&vbq->lock);
1942 INIT_LIST_HEAD(&vbq->free);
Al Viro32fcfd42013-03-10 20:14:08 -04001943 p = &per_cpu(vfree_deferred, i);
1944 init_llist_head(&p->list);
1945 INIT_WORK(&p->wq, free_work);
Nick Piggindb64fe02008-10-18 20:27:03 -07001946 }
Jeremy Fitzhardinge9b463332008-10-28 19:22:34 +11001947
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08001948 /* Import existing vmlist entries. */
1949 for (tmp = vmlist; tmp; tmp = tmp->next) {
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001950 va = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
1951 if (WARN_ON_ONCE(!va))
1952 continue;
1953
KyongHodbda5912012-05-29 15:06:49 -07001954 va->flags = VM_VM_AREA;
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08001955 va->va_start = (unsigned long)tmp->addr;
1956 va->va_end = va->va_start + tmp->size;
KyongHodbda5912012-05-29 15:06:49 -07001957 va->vm = tmp;
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001958 insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08001959 }
Tejun Heoca23e402009-08-14 15:00:52 +09001960
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001961 /*
1962 * Now we can initialize a free vmap space.
1963 */
1964 vmap_init_free_space();
Susheel Khianid6f731e2013-08-22 13:46:07 -07001965 calc_total_vmalloc_size();
Jeremy Fitzhardinge9b463332008-10-28 19:22:34 +11001966 vmap_initialized = true;
Nick Piggindb64fe02008-10-18 20:27:03 -07001967}
1968
Tejun Heo8fc48982009-02-20 16:29:08 +09001969/**
1970 * map_kernel_range_noflush - map kernel VM area with the specified pages
1971 * @addr: start of the VM area to map
1972 * @size: size of the VM area to map
1973 * @prot: page protection flags to use
1974 * @pages: pages to map
1975 *
1976 * Map PFN_UP(@size) pages at @addr. The VM area @addr and @size
1977 * specify should have been allocated using get_vm_area() and its
1978 * friends.
1979 *
1980 * NOTE:
1981 * This function does NOT do any cache flushing. The caller is
1982 * responsible for calling flush_cache_vmap() on to-be-mapped areas
1983 * before calling this function.
1984 *
1985 * RETURNS:
1986 * The number of pages mapped on success, -errno on failure.
1987 */
1988int map_kernel_range_noflush(unsigned long addr, unsigned long size,
1989 pgprot_t prot, struct page **pages)
1990{
1991 return vmap_page_range_noflush(addr, addr + size, prot, pages);
1992}
1993
1994/**
1995 * unmap_kernel_range_noflush - unmap kernel VM area
1996 * @addr: start of the VM area to unmap
1997 * @size: size of the VM area to unmap
1998 *
1999 * Unmap PFN_UP(@size) pages at @addr. The VM area @addr and @size
2000 * specify should have been allocated using get_vm_area() and its
2001 * friends.
2002 *
2003 * NOTE:
2004 * This function does NOT do any cache flushing. The caller is
2005 * responsible for calling flush_cache_vunmap() on to-be-mapped areas
2006 * before calling this function and flush_tlb_kernel_range() after.
2007 */
2008void unmap_kernel_range_noflush(unsigned long addr, unsigned long size)
2009{
2010 vunmap_page_range(addr, addr + size);
2011}
Huang Ying81e88fd2011-01-12 14:44:55 +08002012EXPORT_SYMBOL_GPL(unmap_kernel_range_noflush);
Tejun Heo8fc48982009-02-20 16:29:08 +09002013
2014/**
2015 * unmap_kernel_range - unmap kernel VM area and flush cache and TLB
2016 * @addr: start of the VM area to unmap
2017 * @size: size of the VM area to unmap
2018 *
2019 * Similar to unmap_kernel_range_noflush() but flushes vcache before
2020 * the unmapping and tlb after.
2021 */
Nick Piggindb64fe02008-10-18 20:27:03 -07002022void unmap_kernel_range(unsigned long addr, unsigned long size)
2023{
2024 unsigned long end = addr + size;
Tejun Heof6fcba72009-02-20 15:38:48 -08002025
2026 flush_cache_vunmap(addr, end);
Nick Piggindb64fe02008-10-18 20:27:03 -07002027 vunmap_page_range(addr, end);
2028 flush_tlb_kernel_range(addr, end);
2029}
Minchan Kim93ef6d6c2014-06-04 16:11:09 -07002030EXPORT_SYMBOL_GPL(unmap_kernel_range);
Nick Piggindb64fe02008-10-18 20:27:03 -07002031
WANG Chaof6f8ed42014-08-06 16:06:58 -07002032int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page **pages)
Nick Piggindb64fe02008-10-18 20:27:03 -07002033{
2034 unsigned long addr = (unsigned long)area->addr;
Wanpeng Li762216a2013-09-11 14:22:42 -07002035 unsigned long end = addr + get_vm_area_size(area);
Nick Piggindb64fe02008-10-18 20:27:03 -07002036 int err;
2037
WANG Chaof6f8ed42014-08-06 16:06:58 -07002038 err = vmap_page_range(addr, end, prot, pages);
Nick Piggindb64fe02008-10-18 20:27:03 -07002039
WANG Chaof6f8ed42014-08-06 16:06:58 -07002040 return err > 0 ? 0 : err;
Nick Piggindb64fe02008-10-18 20:27:03 -07002041}
2042EXPORT_SYMBOL_GPL(map_vm_area);
2043
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07002044static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
Marek Szyprowski5e6cafc2012-04-13 12:32:09 +02002045 unsigned long flags, const void *caller)
Tejun Heocf88c792009-08-14 15:00:52 +09002046{
Joonsoo Kimc69480a2013-04-29 15:07:30 -07002047 spin_lock(&vmap_area_lock);
Tejun Heocf88c792009-08-14 15:00:52 +09002048 vm->flags = flags;
2049 vm->addr = (void *)va->va_start;
2050 vm->size = va->va_end - va->va_start;
2051 vm->caller = caller;
Minchan Kimdb1aeca2012-01-10 15:08:39 -08002052 va->vm = vm;
Tejun Heocf88c792009-08-14 15:00:52 +09002053 va->flags |= VM_VM_AREA;
Joonsoo Kimc69480a2013-04-29 15:07:30 -07002054 spin_unlock(&vmap_area_lock);
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07002055}
Tejun Heocf88c792009-08-14 15:00:52 +09002056
Zhang Yanfei20fc02b2013-07-08 15:59:58 -07002057static void clear_vm_uninitialized_flag(struct vm_struct *vm)
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07002058{
Joonsoo Kimd4033af2013-04-29 15:07:35 -07002059 /*
Zhang Yanfei20fc02b2013-07-08 15:59:58 -07002060 * Before removing VM_UNINITIALIZED,
Joonsoo Kimd4033af2013-04-29 15:07:35 -07002061 * we should make sure that vm has proper values.
2062 * Pair with smp_rmb() in show_numa_info().
2063 */
2064 smp_wmb();
Zhang Yanfei20fc02b2013-07-08 15:59:58 -07002065 vm->flags &= ~VM_UNINITIALIZED;
Tejun Heocf88c792009-08-14 15:00:52 +09002066}
2067
Nick Piggindb64fe02008-10-18 20:27:03 -07002068static struct vm_struct *__get_vm_area_node(unsigned long size,
David Miller2dca6992009-09-21 12:22:34 -07002069 unsigned long align, unsigned long flags, unsigned long start,
Marek Szyprowski5e6cafc2012-04-13 12:32:09 +02002070 unsigned long end, int node, gfp_t gfp_mask, const void *caller)
Nick Piggindb64fe02008-10-18 20:27:03 -07002071{
Kautuk Consul00065262011-12-19 17:12:04 -08002072 struct vmap_area *va;
Nick Piggindb64fe02008-10-18 20:27:03 -07002073 struct vm_struct *area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Giridhar Pemmasani52fd24c2006-10-28 10:38:34 -07002075 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 size = PAGE_ALIGN(size);
OGAWA Hirofumi31be8302006-11-16 01:19:29 -08002077 if (unlikely(!size))
2078 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
zijun_hu252e5c62016-10-07 16:57:26 -07002080 if (flags & VM_IOREMAP)
2081 align = 1ul << clamp_t(int, get_count_order_long(size),
2082 PAGE_SHIFT, IOREMAP_MAX_ORDER);
2083
Tejun Heocf88c792009-08-14 15:00:52 +09002084 area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 if (unlikely(!area))
2086 return NULL;
2087
Andrey Ryabinin71394fe2015-02-13 14:40:03 -08002088 if (!(flags & VM_NO_GUARD))
2089 size += PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Nick Piggindb64fe02008-10-18 20:27:03 -07002091 va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
2092 if (IS_ERR(va)) {
2093 kfree(area);
2094 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Zhang Yanfeid82b1d82013-07-03 15:04:47 -07002097 setup_vmalloc_vm(area, va, flags, caller);
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07002098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 return area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100}
2101
Christoph Lameter930fc452005-10-29 18:15:41 -07002102struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
2103 unsigned long start, unsigned long end)
2104{
David Rientjes00ef2d22013-02-22 16:35:36 -08002105 return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE,
2106 GFP_KERNEL, __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -07002107}
Rusty Russell5992b6d2007-07-19 01:49:21 -07002108EXPORT_SYMBOL_GPL(__get_vm_area);
Christoph Lameter930fc452005-10-29 18:15:41 -07002109
Benjamin Herrenschmidtc2968612009-02-18 14:48:12 -08002110struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
2111 unsigned long start, unsigned long end,
Marek Szyprowski5e6cafc2012-04-13 12:32:09 +02002112 const void *caller)
Benjamin Herrenschmidtc2968612009-02-18 14:48:12 -08002113{
David Rientjes00ef2d22013-02-22 16:35:36 -08002114 return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE,
2115 GFP_KERNEL, caller);
Benjamin Herrenschmidtc2968612009-02-18 14:48:12 -08002116}
2117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118/**
Simon Arlott183ff222007-10-20 01:27:18 +02002119 * get_vm_area - reserve a contiguous kernel virtual area
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 * @size: size of the area
2121 * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC
2122 *
2123 * Search an area of @size in the kernel virtual mapping area,
2124 * and reserved it for out purposes. Returns the area descriptor
2125 * on success or %NULL on failure.
2126 */
2127struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
2128{
Susheel Khiani71a14bc2015-09-03 18:21:23 +05302129#ifdef CONFIG_ENABLE_VMALLOC_SAVING
2130 return __get_vm_area_node(size, 1, flags, PAGE_OFFSET, VMALLOC_END,
2131 NUMA_NO_NODE, GFP_KERNEL,
2132 __builtin_return_address(0));
2133#else
David Miller2dca6992009-09-21 12:22:34 -07002134 return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
David Rientjes00ef2d22013-02-22 16:35:36 -08002135 NUMA_NO_NODE, GFP_KERNEL,
2136 __builtin_return_address(0));
Susheel Khiani71a14bc2015-09-03 18:21:23 +05302137#endif
Christoph Lameter23016962008-04-28 02:12:42 -07002138}
Hridya Valsaraju7e7f4542019-11-14 16:20:15 -08002139EXPORT_SYMBOL_GPL(get_vm_area);
Christoph Lameter23016962008-04-28 02:12:42 -07002140
2141struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
Marek Szyprowski5e6cafc2012-04-13 12:32:09 +02002142 const void *caller)
Christoph Lameter23016962008-04-28 02:12:42 -07002143{
Susheel Khiani71a14bc2015-09-03 18:21:23 +05302144#ifdef CONFIG_ENABLE_VMALLOC_SAVING
2145 return __get_vm_area_node(size, 1, flags, PAGE_OFFSET, VMALLOC_END,
2146 NUMA_NO_NODE, GFP_KERNEL, caller);
2147#else
David Miller2dca6992009-09-21 12:22:34 -07002148 return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
David Rientjes00ef2d22013-02-22 16:35:36 -08002149 NUMA_NO_NODE, GFP_KERNEL, caller);
Susheel Khiani71a14bc2015-09-03 18:21:23 +05302150#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151}
2152
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02002153/**
2154 * find_vm_area - find a continuous kernel virtual area
2155 * @addr: base address
2156 *
2157 * 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.
2160 */
2161struct vm_struct *find_vm_area(const void *addr)
Nick Piggin83342312006-06-23 02:03:20 -07002162{
Nick Piggindb64fe02008-10-18 20:27:03 -07002163 struct vmap_area *va;
Nick Piggin83342312006-06-23 02:03:20 -07002164
Nick Piggindb64fe02008-10-18 20:27:03 -07002165 va = find_vmap_area((unsigned long)addr);
2166 if (va && va->flags & VM_VM_AREA)
Minchan Kimdb1aeca2012-01-10 15:08:39 -08002167 return va->vm;
Nick Piggin83342312006-06-23 02:03:20 -07002168
Andi Kleen7856dfe2005-05-20 14:27:57 -07002169 return NULL;
Andi Kleen7856dfe2005-05-20 14:27:57 -07002170}
2171
Roman Gushchin48480dc2019-04-24 07:37:21 +10002172static struct vm_struct *__remove_vm_area(struct vmap_area *va)
2173{
2174 struct vm_struct *vm = va->vm;
2175
Roman Gushchin48480dc2019-04-24 07:37:21 +10002176 spin_lock(&vmap_area_lock);
2177 va->vm = NULL;
2178 va->flags &= ~VM_VM_AREA;
2179 va->flags |= VM_LAZY_FREE;
2180 spin_unlock(&vmap_area_lock);
2181
2182 kasan_free_shadow(vm);
2183 free_unmap_vmap_area(va);
2184
2185 return vm;
2186}
2187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188/**
Simon Arlott183ff222007-10-20 01:27:18 +02002189 * remove_vm_area - find and remove a continuous kernel virtual area
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 * @addr: base address
2191 *
2192 * Search for the kernel VM area starting at @addr, and remove it.
2193 * This function returns the found VM area, but using it is NOT safe
Andi Kleen7856dfe2005-05-20 14:27:57 -07002194 * on SMP machines, except for its size or flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -08002196struct vm_struct *remove_vm_area(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
Roman Gushchin48480dc2019-04-24 07:37:21 +10002198 struct vm_struct *vm = NULL;
Nick Piggindb64fe02008-10-18 20:27:03 -07002199 struct vmap_area *va;
2200
2201 va = find_vmap_area((unsigned long)addr);
Roman Gushchin48480dc2019-04-24 07:37:21 +10002202 if (va && va->flags & VM_VM_AREA)
2203 vm = __remove_vm_area(va);
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07002204
Roman Gushchin48480dc2019-04-24 07:37:21 +10002205 return vm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206}
2207
Christoph Lameterb3bdda02008-02-04 22:28:32 -08002208static void __vunmap(const void *addr, int deallocate_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
2210 struct vm_struct *area;
Roman Gushchin48480dc2019-04-24 07:37:21 +10002211 struct vmap_area *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
2213 if (!addr)
2214 return;
2215
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002216 if (WARN(!PAGE_ALIGNED(addr), "Trying to vfree() bad address (%p)\n",
Dan Carpenterab15d9b2013-07-08 15:59:53 -07002217 addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Roman Gushchin48480dc2019-04-24 07:37:21 +10002220 va = find_vmap_area((unsigned long)addr);
2221 if (unlikely(!va || !(va->flags & VM_VM_AREA))) {
Arjan van de Ven4c8573e2008-07-25 19:45:37 -07002222 WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 return;
2225 }
2226
Roman Gushchin48480dc2019-04-24 07:37:21 +10002227 area = va->vm;
2228 debug_check_no_locks_freed(addr, get_vm_area_size(area));
2229 debug_check_no_obj_freed(addr, get_vm_area_size(area));
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07002230
Roman Gushchin48480dc2019-04-24 07:37:21 +10002231 __remove_vm_area(va);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 if (deallocate_pages) {
2233 int i;
2234
2235 for (i = 0; i < area->nr_pages; i++) {
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08002236 struct page *page = area->pages[i];
2237
2238 BUG_ON(!page);
Vladimir Davydov49491482016-07-26 15:24:24 -07002239 __free_pages(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 }
Roman Gushchindb70fefd2019-02-25 12:30:37 -08002241 atomic_long_sub(area->nr_pages, &nr_vmalloc_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
David Rientjes244d63e2016-01-14 15:19:35 -08002243 kvfree(area->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 }
2245
2246 kfree(area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247}
Andrey Ryabininbf22e372016-12-12 16:44:10 -08002248
2249static inline void __vfree_deferred(const void *addr)
2250{
2251 /*
2252 * Use raw_cpu_ptr() because this can be called from preemptible
2253 * context. Preemption is absolutely fine here, because the llist_add()
2254 * implementation is lockless, so it works even if we are adding to
2255 * nother cpu's list. schedule_work() should be fine with this too.
2256 */
2257 struct vfree_deferred *p = raw_cpu_ptr(&vfree_deferred);
2258
2259 if (llist_add((struct llist_node *)addr, &p->list))
2260 schedule_work(&p->wq);
2261}
2262
2263/**
2264 * vfree_atomic - release memory allocated by vmalloc()
2265 * @addr: memory base address
2266 *
2267 * This one is just like vfree() but can be called in any atomic context
2268 * except NMIs.
2269 */
2270void vfree_atomic(const void *addr)
2271{
2272 BUG_ON(in_nmi());
2273
2274 kmemleak_free(addr);
2275
2276 if (!addr)
2277 return;
2278 __vfree_deferred(addr);
2279}
2280
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281/**
2282 * vfree - release memory allocated by vmalloc()
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 * @addr: memory base address
2284 *
Simon Arlott183ff222007-10-20 01:27:18 +02002285 * Free the virtually continuous memory area starting at @addr, as
Pekka Enberg80e93ef2005-09-09 13:10:16 -07002286 * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
2287 * NULL, no operation is performed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 *
Al Viro32fcfd42013-03-10 20:14:08 -04002289 * Must not be called in NMI context (strictly speaking, only if we don't
2290 * have CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG, but making the calling
2291 * conventions for vfree() arch-depenedent would be a really bad idea)
Andrew Mortonc9fcee52013-05-07 16:18:18 -07002292 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -03002293 * NOTE: assumes that the object at @addr has a size >= sizeof(llist_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -08002295void vfree(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296{
Al Viro32fcfd42013-03-10 20:14:08 -04002297 BUG_ON(in_nmi());
Catalin Marinas89219d32009-06-11 13:23:19 +01002298
2299 kmemleak_free(addr);
2300
Al Viro32fcfd42013-03-10 20:14:08 -04002301 if (!addr)
2302 return;
Andrey Ryabininbf22e372016-12-12 16:44:10 -08002303 if (unlikely(in_interrupt()))
2304 __vfree_deferred(addr);
2305 else
Al Viro32fcfd42013-03-10 20:14:08 -04002306 __vunmap(addr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308EXPORT_SYMBOL(vfree);
2309
2310/**
2311 * vunmap - release virtual mapping obtained by vmap()
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 * @addr: memory base address
2313 *
2314 * Free the virtually contiguous memory area starting at @addr,
2315 * which was created from the page array passed to vmap().
2316 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -07002317 * Must not be called in interrupt context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -08002319void vunmap(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
2321 BUG_ON(in_interrupt());
Peter Zijlstra34754b62009-02-25 16:04:03 +01002322 might_sleep();
Al Viro32fcfd42013-03-10 20:14:08 -04002323 if (addr)
2324 __vunmap(addr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326EXPORT_SYMBOL(vunmap);
2327
2328/**
2329 * vmap - map an array of pages into virtually contiguous space
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 * @pages: array of page pointers
2331 * @count: number of pages to map
2332 * @flags: vm_area->flags
2333 * @prot: page protection for the mapping
2334 *
2335 * Maps @count pages from @pages into contiguous kernel virtual
2336 * space.
2337 */
2338void *vmap(struct page **pages, unsigned int count,
2339 unsigned long flags, pgprot_t prot)
2340{
2341 struct vm_struct *area;
Guillermo Julián Moreno65ee03c2016-06-03 14:55:33 -07002342 unsigned long size; /* In bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Peter Zijlstra34754b62009-02-25 16:04:03 +01002344 might_sleep();
2345
Jan Beulich44813742009-09-21 17:03:05 -07002346 if (count > totalram_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 return NULL;
2348
Guillermo Julián Moreno65ee03c2016-06-03 14:55:33 -07002349 size = (unsigned long)count << PAGE_SHIFT;
2350 area = get_vm_area_caller(size, flags, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 if (!area)
2352 return NULL;
Christoph Lameter23016962008-04-28 02:12:42 -07002353
WANG Chaof6f8ed42014-08-06 16:06:58 -07002354 if (map_vm_area(area, prot, pages)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 vunmap(area->addr);
2356 return NULL;
2357 }
2358
2359 return area->addr;
2360}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361EXPORT_SYMBOL(vmap);
2362
Michal Hocko8594a212017-05-12 15:46:41 -07002363static void *__vmalloc_node(unsigned long size, unsigned long align,
2364 gfp_t gfp_mask, pgprot_t prot,
2365 int node, const void *caller);
Adrian Bunke31d9eb2008-02-04 22:29:09 -08002366static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
Wanpeng Li3722e132013-11-12 15:07:29 -08002367 pgprot_t prot, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368{
2369 struct page **pages;
2370 unsigned int nr_pages, array_size, i;
David Rientjes930f0362014-08-06 16:06:28 -07002371 const gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO;
Laura Abbott704b8622017-08-18 15:16:27 -07002372 const gfp_t alloc_mask = gfp_mask | __GFP_NOWARN;
2373 const gfp_t highmem_mask = (gfp_mask & (GFP_DMA | GFP_DMA32)) ?
2374 0 :
2375 __GFP_HIGHMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Wanpeng Li762216a2013-09-11 14:22:42 -07002377 nr_pages = get_vm_area_size(area) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 array_size = (nr_pages * sizeof(struct page *));
2379
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 /* Please note that the recursion is strictly bounded. */
Jan Kiszka8757d5f2006-07-14 00:23:56 -07002381 if (array_size > PAGE_SIZE) {
Laura Abbott704b8622017-08-18 15:16:27 -07002382 pages = __vmalloc_node(array_size, 1, nested_gfp|highmem_mask,
Wanpeng Li3722e132013-11-12 15:07:29 -08002383 PAGE_KERNEL, node, area->caller);
Andrew Morton286e1ea2006-10-17 00:09:57 -07002384 } else {
Jan Beulich976d6df2009-12-14 17:58:39 -08002385 pages = kmalloc_node(array_size, nested_gfp, node);
Andrew Morton286e1ea2006-10-17 00:09:57 -07002386 }
Austin Kim1c6c19b2019-09-23 15:36:42 -07002387
2388 if (!pages) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 remove_vm_area(area->addr);
2390 kfree(area);
2391 return NULL;
2392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Austin Kim1c6c19b2019-09-23 15:36:42 -07002394 area->pages = pages;
2395 area->nr_pages = nr_pages;
2396
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 for (i = 0; i < area->nr_pages; i++) {
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08002398 struct page *page;
2399
Jianguo Wu4b909512013-11-12 15:07:11 -08002400 if (node == NUMA_NO_NODE)
Laura Abbott704b8622017-08-18 15:16:27 -07002401 page = alloc_page(alloc_mask|highmem_mask);
Christoph Lameter930fc452005-10-29 18:15:41 -07002402 else
Laura Abbott704b8622017-08-18 15:16:27 -07002403 page = alloc_pages_node(node, alloc_mask|highmem_mask, 0);
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08002404
2405 if (unlikely(!page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 /* Successfully allocated i pages, free them in __vunmap() */
2407 area->nr_pages = i;
Roman Gushchindb70fefd2019-02-25 12:30:37 -08002408 atomic_long_add(area->nr_pages, &nr_vmalloc_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 goto fail;
2410 }
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08002411 area->pages[i] = page;
Laura Abbott704b8622017-08-18 15:16:27 -07002412 if (gfpflags_allow_blocking(gfp_mask|highmem_mask))
Eric Dumazet660654f2014-08-06 16:06:25 -07002413 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 }
Roman Gushchindb70fefd2019-02-25 12:30:37 -08002415 atomic_long_add(area->nr_pages, &nr_vmalloc_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
WANG Chaof6f8ed42014-08-06 16:06:58 -07002417 if (map_vm_area(area, prot, pages))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 goto fail;
2419 return area->addr;
2420
2421fail:
Michal Hockoa8e99252017-02-22 15:46:10 -08002422 warn_alloc(gfp_mask, NULL,
Michal Hocko7877cdc2016-10-07 17:01:55 -07002423 "vmalloc: allocation failure, allocated %ld of %ld bytes",
Dave Hansen22943ab2011-05-24 17:12:18 -07002424 (area->nr_pages*PAGE_SIZE), area->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 vfree(area->addr);
2426 return NULL;
2427}
2428
David Rientjesd0a21262011-01-13 15:46:02 -08002429/**
2430 * __vmalloc_node_range - allocate virtually contiguous memory
2431 * @size: allocation size
2432 * @align: desired alignment
2433 * @start: vm area range start
2434 * @end: vm area range end
2435 * @gfp_mask: flags for the page level allocator
2436 * @prot: protection mask for the allocated pages
Andrey Ryabinincb9e3c22015-02-13 14:40:07 -08002437 * @vm_flags: additional vm area flags (e.g. %VM_NO_GUARD)
David Rientjes00ef2d22013-02-22 16:35:36 -08002438 * @node: node to use for allocation or NUMA_NO_NODE
David Rientjesd0a21262011-01-13 15:46:02 -08002439 * @caller: caller's return address
2440 *
2441 * Allocate enough pages to cover @size from the page level
2442 * allocator with @gfp_mask flags. Map them into contiguous
2443 * kernel virtual space, using a pagetable protection of @prot.
2444 */
2445void *__vmalloc_node_range(unsigned long size, unsigned long align,
2446 unsigned long start, unsigned long end, gfp_t gfp_mask,
Andrey Ryabinincb9e3c22015-02-13 14:40:07 -08002447 pgprot_t prot, unsigned long vm_flags, int node,
2448 const void *caller)
Christoph Lameter930fc452005-10-29 18:15:41 -07002449{
David Rientjesd0a21262011-01-13 15:46:02 -08002450 struct vm_struct *area;
2451 void *addr;
2452 unsigned long real_size = size;
2453
2454 size = PAGE_ALIGN(size);
2455 if (!size || (size >> PAGE_SHIFT) > totalram_pages)
Joe Perchesde7d2b52011-10-31 17:08:48 -07002456 goto fail;
David Rientjesd0a21262011-01-13 15:46:02 -08002457
Andrey Ryabinincb9e3c22015-02-13 14:40:07 -08002458 area = __get_vm_area_node(size, align, VM_ALLOC | VM_UNINITIALIZED |
2459 vm_flags, start, end, node, gfp_mask, caller);
David Rientjesd0a21262011-01-13 15:46:02 -08002460 if (!area)
Joe Perchesde7d2b52011-10-31 17:08:48 -07002461 goto fail;
David Rientjesd0a21262011-01-13 15:46:02 -08002462
Wanpeng Li3722e132013-11-12 15:07:29 -08002463 addr = __vmalloc_area_node(area, gfp_mask, prot, node);
Mel Gorman1368edf2011-12-08 14:34:30 -08002464 if (!addr)
Wanpeng Lib82225f32013-11-12 15:07:33 -08002465 return NULL;
Catalin Marinas89219d32009-06-11 13:23:19 +01002466
2467 /*
Joerg Roedel46b306f2019-07-19 20:46:52 +02002468 * First make sure the mappings are removed from all page-tables
2469 * before they are freed.
2470 */
Joerg Roedel6c1051f2020-03-21 18:22:41 -07002471 vmalloc_sync_unmappings();
Joerg Roedel46b306f2019-07-19 20:46:52 +02002472
2473 /*
Zhang Yanfei20fc02b2013-07-08 15:59:58 -07002474 * In this function, newly allocated vm_struct has VM_UNINITIALIZED
2475 * flag. It means that vm_struct is not fully initialized.
Joonsoo Kim4341fa42013-04-29 15:07:39 -07002476 * Now, it is fully initialized, so remove this flag here.
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07002477 */
Zhang Yanfei20fc02b2013-07-08 15:59:58 -07002478 clear_vm_uninitialized_flag(area);
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07002479
Catalin Marinas94f4a162017-07-06 15:40:22 -07002480 kmemleak_vmalloc(area, size, gfp_mask);
Catalin Marinas89219d32009-06-11 13:23:19 +01002481
2482 return addr;
Joe Perchesde7d2b52011-10-31 17:08:48 -07002483
2484fail:
Michal Hockoa8e99252017-02-22 15:46:10 -08002485 warn_alloc(gfp_mask, NULL,
Michal Hocko7877cdc2016-10-07 17:01:55 -07002486 "vmalloc: allocation failure: %lu bytes", real_size);
Joe Perchesde7d2b52011-10-31 17:08:48 -07002487 return NULL;
Christoph Lameter930fc452005-10-29 18:15:41 -07002488}
2489
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490/**
Christoph Lameter930fc452005-10-29 18:15:41 -07002491 * __vmalloc_node - allocate virtually contiguous memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 * @size: allocation size
David Miller2dca6992009-09-21 12:22:34 -07002493 * @align: desired alignment
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 * @gfp_mask: flags for the page level allocator
2495 * @prot: protection mask for the allocated pages
David Rientjes00ef2d22013-02-22 16:35:36 -08002496 * @node: node to use for allocation or NUMA_NO_NODE
Randy Dunlapc85d1942008-05-01 04:34:48 -07002497 * @caller: caller's return address
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 *
2499 * Allocate enough pages to cover @size from the page level
2500 * allocator with @gfp_mask flags. Map them into contiguous
2501 * kernel virtual space, using a pagetable protection of @prot.
Michal Hockoa7c3e902017-05-08 15:57:09 -07002502 *
Michal Hockodcda9b02017-07-12 14:36:45 -07002503 * Reclaim modifiers in @gfp_mask - __GFP_NORETRY, __GFP_RETRY_MAYFAIL
Michal Hockoa7c3e902017-05-08 15:57:09 -07002504 * and __GFP_NOFAIL are not supported
2505 *
2506 * Any use of gfp flags outside of GFP_KERNEL should be consulted
2507 * with mm people.
2508 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 */
Michal Hocko8594a212017-05-12 15:46:41 -07002510static void *__vmalloc_node(unsigned long size, unsigned long align,
David Miller2dca6992009-09-21 12:22:34 -07002511 gfp_t gfp_mask, pgprot_t prot,
Marek Szyprowski5e6cafc2012-04-13 12:32:09 +02002512 int node, const void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513{
Qingqing Zhou59782092020-01-03 20:08:43 +08002514#ifdef CONFIG_ENABLE_VMALLOC_SAVING
2515 return __vmalloc_node_range(size, align, PAGE_OFFSET, VMALLOC_END,
2516 gfp_mask, prot, 0, node, caller);
2517#else
David Rientjesd0a21262011-01-13 15:46:02 -08002518 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
Andrey Ryabinincb9e3c22015-02-13 14:40:07 -08002519 gfp_mask, prot, 0, node, caller);
Qingqing Zhou59782092020-01-03 20:08:43 +08002520#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521}
2522
Christoph Lameter930fc452005-10-29 18:15:41 -07002523void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
2524{
David Rientjes00ef2d22013-02-22 16:35:36 -08002525 return __vmalloc_node(size, 1, gfp_mask, prot, NUMA_NO_NODE,
Christoph Lameter23016962008-04-28 02:12:42 -07002526 __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -07002527}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528EXPORT_SYMBOL(__vmalloc);
2529
Michal Hocko8594a212017-05-12 15:46:41 -07002530static inline void *__vmalloc_node_flags(unsigned long size,
2531 int node, gfp_t flags)
2532{
2533 return __vmalloc_node(size, 1, flags, PAGE_KERNEL,
2534 node, __builtin_return_address(0));
2535}
2536
2537
2538void *__vmalloc_node_flags_caller(unsigned long size, int node, gfp_t flags,
2539 void *caller)
2540{
2541 return __vmalloc_node(size, 1, flags, PAGE_KERNEL, node, caller);
2542}
2543
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544/**
2545 * vmalloc - allocate virtually contiguous memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 * @size: allocation size
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 * Allocate enough pages to cover @size from the page level
2548 * allocator and map them into contiguous kernel virtual space.
2549 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +02002550 * For tight control over page level allocator and protection flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 * use __vmalloc() instead.
2552 */
2553void *vmalloc(unsigned long size)
2554{
David Rientjes00ef2d22013-02-22 16:35:36 -08002555 return __vmalloc_node_flags(size, NUMA_NO_NODE,
Michal Hocko19809c22017-05-08 15:57:44 -07002556 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558EXPORT_SYMBOL(vmalloc);
2559
Christoph Lameter930fc452005-10-29 18:15:41 -07002560/**
Dave Younge1ca7782010-10-26 14:22:06 -07002561 * vzalloc - allocate virtually contiguous memory with zero fill
2562 * @size: allocation size
2563 * Allocate enough pages to cover @size from the page level
2564 * allocator and map them into contiguous kernel virtual space.
2565 * The memory allocated is set to zero.
2566 *
2567 * For tight control over page level allocator and protection flags
2568 * use __vmalloc() instead.
2569 */
2570void *vzalloc(unsigned long size)
2571{
David Rientjes00ef2d22013-02-22 16:35:36 -08002572 return __vmalloc_node_flags(size, NUMA_NO_NODE,
Michal Hocko19809c22017-05-08 15:57:44 -07002573 GFP_KERNEL | __GFP_ZERO);
Dave Younge1ca7782010-10-26 14:22:06 -07002574}
2575EXPORT_SYMBOL(vzalloc);
2576
2577/**
Rolf Eike Beeread04082006-09-27 01:50:13 -07002578 * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
2579 * @size: allocation size
Nick Piggin83342312006-06-23 02:03:20 -07002580 *
Rolf Eike Beeread04082006-09-27 01:50:13 -07002581 * The resulting memory area is zeroed so it can be mapped to userspace
2582 * without leaking data.
Nick Piggin83342312006-06-23 02:03:20 -07002583 */
2584void *vmalloc_user(unsigned long size)
2585{
2586 struct vm_struct *area;
2587 void *ret;
2588
David Miller2dca6992009-09-21 12:22:34 -07002589 ret = __vmalloc_node(size, SHMLBA,
Michal Hocko19809c22017-05-08 15:57:44 -07002590 GFP_KERNEL | __GFP_ZERO,
David Rientjes00ef2d22013-02-22 16:35:36 -08002591 PAGE_KERNEL, NUMA_NO_NODE,
2592 __builtin_return_address(0));
Eric Dumazet2b4ac442006-11-10 12:27:48 -08002593 if (ret) {
Nick Piggindb64fe02008-10-18 20:27:03 -07002594 area = find_vm_area(ret);
Eric Dumazet2b4ac442006-11-10 12:27:48 -08002595 area->flags |= VM_USERMAP;
Eric Dumazet2b4ac442006-11-10 12:27:48 -08002596 }
Nick Piggin83342312006-06-23 02:03:20 -07002597 return ret;
2598}
2599EXPORT_SYMBOL(vmalloc_user);
2600
2601/**
Christoph Lameter930fc452005-10-29 18:15:41 -07002602 * vmalloc_node - allocate memory on a specific node
Christoph Lameter930fc452005-10-29 18:15:41 -07002603 * @size: allocation size
Randy Dunlapd44e0782005-11-07 01:01:10 -08002604 * @node: numa node
Christoph Lameter930fc452005-10-29 18:15:41 -07002605 *
2606 * Allocate enough pages to cover @size from the page level
2607 * allocator and map them into contiguous kernel virtual space.
2608 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +02002609 * For tight control over page level allocator and protection flags
Christoph Lameter930fc452005-10-29 18:15:41 -07002610 * use __vmalloc() instead.
2611 */
2612void *vmalloc_node(unsigned long size, int node)
2613{
Michal Hocko19809c22017-05-08 15:57:44 -07002614 return __vmalloc_node(size, 1, GFP_KERNEL, PAGE_KERNEL,
Christoph Lameter23016962008-04-28 02:12:42 -07002615 node, __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -07002616}
2617EXPORT_SYMBOL(vmalloc_node);
2618
Dave Younge1ca7782010-10-26 14:22:06 -07002619/**
2620 * vzalloc_node - allocate memory on a specific node with zero fill
2621 * @size: allocation size
2622 * @node: numa node
2623 *
2624 * Allocate enough pages to cover @size from the page level
2625 * allocator and map them into contiguous kernel virtual space.
2626 * The memory allocated is set to zero.
2627 *
2628 * For tight control over page level allocator and protection flags
2629 * use __vmalloc_node() instead.
2630 */
2631void *vzalloc_node(unsigned long size, int node)
2632{
2633 return __vmalloc_node_flags(size, node,
Michal Hocko19809c22017-05-08 15:57:44 -07002634 GFP_KERNEL | __GFP_ZERO);
Dave Younge1ca7782010-10-26 14:22:06 -07002635}
2636EXPORT_SYMBOL(vzalloc_node);
2637
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638/**
2639 * vmalloc_exec - allocate virtually contiguous, executable memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 * @size: allocation size
2641 *
2642 * Kernel-internal function to allocate enough pages to cover @size
2643 * the page level allocator and map them into contiguous and
2644 * executable kernel virtual space.
2645 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +02002646 * For tight control over page level allocator and protection flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 * use __vmalloc() instead.
2648 */
2649
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650void *vmalloc_exec(unsigned long size)
2651{
Michal Hocko19809c22017-05-08 15:57:44 -07002652 return __vmalloc_node(size, 1, GFP_KERNEL, PAGE_KERNEL_EXEC,
David Rientjes00ef2d22013-02-22 16:35:36 -08002653 NUMA_NO_NODE, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654}
2655
Andi Kleen0d08e0d2007-05-02 19:27:12 +02002656#if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
Michal Hocko698d0832018-02-21 14:46:01 -08002657#define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL)
Andi Kleen0d08e0d2007-05-02 19:27:12 +02002658#elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
Michal Hocko698d0832018-02-21 14:46:01 -08002659#define GFP_VMALLOC32 (GFP_DMA | GFP_KERNEL)
Andi Kleen0d08e0d2007-05-02 19:27:12 +02002660#else
Michal Hocko698d0832018-02-21 14:46:01 -08002661/*
2662 * 64b systems should always have either DMA or DMA32 zones. For others
2663 * GFP_DMA32 should do the right thing and use the normal zone.
2664 */
2665#define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
Andi Kleen0d08e0d2007-05-02 19:27:12 +02002666#endif
2667
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668/**
2669 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 * @size: allocation size
2671 *
2672 * Allocate enough 32bit PA addressable pages to cover @size from the
2673 * page level allocator and map them into contiguous kernel virtual space.
2674 */
2675void *vmalloc_32(unsigned long size)
2676{
David Miller2dca6992009-09-21 12:22:34 -07002677 return __vmalloc_node(size, 1, GFP_VMALLOC32, PAGE_KERNEL,
David Rientjes00ef2d22013-02-22 16:35:36 -08002678 NUMA_NO_NODE, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680EXPORT_SYMBOL(vmalloc_32);
2681
Nick Piggin83342312006-06-23 02:03:20 -07002682/**
Rolf Eike Beeread04082006-09-27 01:50:13 -07002683 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
Nick Piggin83342312006-06-23 02:03:20 -07002684 * @size: allocation size
Rolf Eike Beeread04082006-09-27 01:50:13 -07002685 *
2686 * The resulting memory area is 32bit addressable and zeroed so it can be
2687 * mapped to userspace without leaking data.
Nick Piggin83342312006-06-23 02:03:20 -07002688 */
2689void *vmalloc_32_user(unsigned long size)
2690{
2691 struct vm_struct *area;
2692 void *ret;
2693
David Miller2dca6992009-09-21 12:22:34 -07002694 ret = __vmalloc_node(size, 1, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
David Rientjes00ef2d22013-02-22 16:35:36 -08002695 NUMA_NO_NODE, __builtin_return_address(0));
Eric Dumazet2b4ac442006-11-10 12:27:48 -08002696 if (ret) {
Nick Piggindb64fe02008-10-18 20:27:03 -07002697 area = find_vm_area(ret);
Eric Dumazet2b4ac442006-11-10 12:27:48 -08002698 area->flags |= VM_USERMAP;
Eric Dumazet2b4ac442006-11-10 12:27:48 -08002699 }
Nick Piggin83342312006-06-23 02:03:20 -07002700 return ret;
2701}
2702EXPORT_SYMBOL(vmalloc_32_user);
2703
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002704/*
2705 * small helper routine , copy contents to buf from addr.
2706 * If the page is not present, fill zero.
2707 */
2708
2709static int aligned_vread(char *buf, char *addr, unsigned long count)
2710{
2711 struct page *p;
2712 int copied = 0;
2713
2714 while (count) {
2715 unsigned long offset, length;
2716
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08002717 offset = offset_in_page(addr);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002718 length = PAGE_SIZE - offset;
2719 if (length > count)
2720 length = count;
2721 p = vmalloc_to_page(addr);
2722 /*
2723 * To do safe access to this _mapped_ area, we need
2724 * lock. But adding lock here means that we need to add
2725 * overhead of vmalloc()/vfree() calles for this _debug_
2726 * interface, rarely used. Instead of that, we'll use
2727 * kmap() and get small overhead in this access function.
2728 */
2729 if (p) {
2730 /*
2731 * we can expect USER0 is not used (see vread/vwrite's
2732 * function description)
2733 */
Cong Wang9b04c5f2011-11-25 23:14:39 +08002734 void *map = kmap_atomic(p);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002735 memcpy(buf, map + offset, length);
Cong Wang9b04c5f2011-11-25 23:14:39 +08002736 kunmap_atomic(map);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002737 } else
2738 memset(buf, 0, length);
2739
2740 addr += length;
2741 buf += length;
2742 copied += length;
2743 count -= length;
2744 }
2745 return copied;
2746}
2747
2748static int aligned_vwrite(char *buf, char *addr, unsigned long count)
2749{
2750 struct page *p;
2751 int copied = 0;
2752
2753 while (count) {
2754 unsigned long offset, length;
2755
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08002756 offset = offset_in_page(addr);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002757 length = PAGE_SIZE - offset;
2758 if (length > count)
2759 length = count;
2760 p = vmalloc_to_page(addr);
2761 /*
2762 * To do safe access to this _mapped_ area, we need
2763 * lock. But adding lock here means that we need to add
2764 * overhead of vmalloc()/vfree() calles for this _debug_
2765 * interface, rarely used. Instead of that, we'll use
2766 * kmap() and get small overhead in this access function.
2767 */
2768 if (p) {
2769 /*
2770 * we can expect USER0 is not used (see vread/vwrite's
2771 * function description)
2772 */
Cong Wang9b04c5f2011-11-25 23:14:39 +08002773 void *map = kmap_atomic(p);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002774 memcpy(map + offset, buf, length);
Cong Wang9b04c5f2011-11-25 23:14:39 +08002775 kunmap_atomic(map);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002776 }
2777 addr += length;
2778 buf += length;
2779 copied += length;
2780 count -= length;
2781 }
2782 return copied;
2783}
2784
2785/**
2786 * vread() - read vmalloc area in a safe way.
2787 * @buf: buffer for reading data
2788 * @addr: vm address.
2789 * @count: number of bytes to be read.
2790 *
2791 * Returns # of bytes which addr and buf should be increased.
2792 * (same number to @count). Returns 0 if [addr...addr+count) doesn't
2793 * includes any intersect with alive vmalloc area.
2794 *
2795 * This function checks that addr is a valid vmalloc'ed area, and
2796 * copy data from that area to a given buffer. If the given memory range
2797 * of [addr...addr+count) includes some valid address, data is copied to
2798 * proper area of @buf. If there are memory holes, they'll be zero-filled.
2799 * IOREMAP area is treated as memory hole and no copy is done.
2800 *
2801 * If [addr...addr+count) doesn't includes any intersects with alive
Cong Wanga8e52022012-06-23 11:30:16 +08002802 * vm_struct area, returns 0. @buf should be kernel's buffer.
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002803 *
2804 * Note: In usual ops, vread() is never necessary because the caller
2805 * should know vmalloc() area is valid and can use memcpy().
2806 * This is for routines which have to access vmalloc area without
2807 * any informaion, as /dev/kmem.
2808 *
2809 */
2810
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811long vread(char *buf, char *addr, unsigned long count)
2812{
Joonsoo Kime81ce852013-04-29 15:07:32 -07002813 struct vmap_area *va;
2814 struct vm_struct *vm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 char *vaddr, *buf_start = buf;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002816 unsigned long buflen = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 unsigned long n;
2818
2819 /* Don't allow overflow */
2820 if ((unsigned long) addr + count < count)
2821 count = -(unsigned long) addr;
2822
Joonsoo Kime81ce852013-04-29 15:07:32 -07002823 spin_lock(&vmap_area_lock);
2824 list_for_each_entry(va, &vmap_area_list, list) {
2825 if (!count)
2826 break;
2827
2828 if (!(va->flags & VM_VM_AREA))
2829 continue;
2830
2831 vm = va->vm;
2832 vaddr = (char *) vm->addr;
Wanpeng Li762216a2013-09-11 14:22:42 -07002833 if (addr >= vaddr + get_vm_area_size(vm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 continue;
2835 while (addr < vaddr) {
2836 if (count == 0)
2837 goto finished;
2838 *buf = '\0';
2839 buf++;
2840 addr++;
2841 count--;
2842 }
Wanpeng Li762216a2013-09-11 14:22:42 -07002843 n = vaddr + get_vm_area_size(vm) - addr;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002844 if (n > count)
2845 n = count;
Joonsoo Kime81ce852013-04-29 15:07:32 -07002846 if (!(vm->flags & VM_IOREMAP))
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002847 aligned_vread(buf, addr, n);
2848 else /* IOREMAP area is treated as memory hole */
2849 memset(buf, 0, n);
2850 buf += n;
2851 addr += n;
2852 count -= n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 }
2854finished:
Joonsoo Kime81ce852013-04-29 15:07:32 -07002855 spin_unlock(&vmap_area_lock);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002856
2857 if (buf == buf_start)
2858 return 0;
2859 /* zero-fill memory holes */
2860 if (buf != buf_start + buflen)
2861 memset(buf, 0, buflen - (buf - buf_start));
2862
2863 return buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864}
2865
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002866/**
2867 * vwrite() - write vmalloc area in a safe way.
2868 * @buf: buffer for source data
2869 * @addr: vm address.
2870 * @count: number of bytes to be read.
2871 *
2872 * Returns # of bytes which addr and buf should be incresed.
2873 * (same number to @count).
2874 * If [addr...addr+count) doesn't includes any intersect with valid
2875 * vmalloc area, returns 0.
2876 *
2877 * This function checks that addr is a valid vmalloc'ed area, and
2878 * copy data from a buffer to the given addr. If specified range of
2879 * [addr...addr+count) includes some valid address, data is copied from
2880 * proper area of @buf. If there are memory holes, no copy to hole.
2881 * IOREMAP area is treated as memory hole and no copy is done.
2882 *
2883 * If [addr...addr+count) doesn't includes any intersects with alive
Cong Wanga8e52022012-06-23 11:30:16 +08002884 * vm_struct area, returns 0. @buf should be kernel's buffer.
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002885 *
2886 * Note: In usual ops, vwrite() 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
2889 * any informaion, as /dev/kmem.
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002890 */
2891
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892long vwrite(char *buf, char *addr, unsigned long count)
2893{
Joonsoo Kime81ce852013-04-29 15:07:32 -07002894 struct vmap_area *va;
2895 struct vm_struct *vm;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002896 char *vaddr;
2897 unsigned long n, buflen;
2898 int copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
2900 /* Don't allow overflow */
2901 if ((unsigned long) addr + count < count)
2902 count = -(unsigned long) addr;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002903 buflen = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
Joonsoo Kime81ce852013-04-29 15:07:32 -07002905 spin_lock(&vmap_area_lock);
2906 list_for_each_entry(va, &vmap_area_list, list) {
2907 if (!count)
2908 break;
2909
2910 if (!(va->flags & VM_VM_AREA))
2911 continue;
2912
2913 vm = va->vm;
2914 vaddr = (char *) vm->addr;
Wanpeng Li762216a2013-09-11 14:22:42 -07002915 if (addr >= vaddr + get_vm_area_size(vm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 continue;
2917 while (addr < vaddr) {
2918 if (count == 0)
2919 goto finished;
2920 buf++;
2921 addr++;
2922 count--;
2923 }
Wanpeng Li762216a2013-09-11 14:22:42 -07002924 n = vaddr + get_vm_area_size(vm) - addr;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002925 if (n > count)
2926 n = count;
Joonsoo Kime81ce852013-04-29 15:07:32 -07002927 if (!(vm->flags & VM_IOREMAP)) {
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002928 aligned_vwrite(buf, addr, n);
2929 copied++;
2930 }
2931 buf += n;
2932 addr += n;
2933 count -= n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 }
2935finished:
Joonsoo Kime81ce852013-04-29 15:07:32 -07002936 spin_unlock(&vmap_area_lock);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002937 if (!copied)
2938 return 0;
2939 return buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940}
Nick Piggin83342312006-06-23 02:03:20 -07002941
2942/**
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002943 * remap_vmalloc_range_partial - map vmalloc pages to userspace
2944 * @vma: vma to cover
2945 * @uaddr: target user address to start at
2946 * @kaddr: virtual address of vmalloc kernel memory
Jann Hornd8da38e2020-04-20 18:14:11 -07002947 * @pgoff: offset from @kaddr to start at
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002948 * @size: size of map area
2949 *
2950 * Returns: 0 for success, -Exxx on failure
2951 *
2952 * This function checks that @kaddr is a valid vmalloc'ed area,
2953 * and that it is big enough to cover the range starting at
2954 * @uaddr in @vma. Will return failure if that criteria isn't
2955 * met.
2956 *
2957 * Similar to remap_pfn_range() (see mm/memory.c)
2958 */
2959int remap_vmalloc_range_partial(struct vm_area_struct *vma, unsigned long uaddr,
Jann Hornd8da38e2020-04-20 18:14:11 -07002960 void *kaddr, unsigned long pgoff,
2961 unsigned long size)
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002962{
2963 struct vm_struct *area;
Jann Hornd8da38e2020-04-20 18:14:11 -07002964 unsigned long off;
2965 unsigned long end_index;
2966
2967 if (check_shl_overflow(pgoff, PAGE_SHIFT, &off))
2968 return -EINVAL;
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002969
2970 size = PAGE_ALIGN(size);
2971
2972 if (!PAGE_ALIGNED(uaddr) || !PAGE_ALIGNED(kaddr))
2973 return -EINVAL;
2974
2975 area = find_vm_area(kaddr);
2976 if (!area)
2977 return -EINVAL;
2978
2979 if (!(area->flags & VM_USERMAP))
2980 return -EINVAL;
2981
Jann Hornd8da38e2020-04-20 18:14:11 -07002982 if (check_add_overflow(size, off, &end_index) ||
2983 end_index > get_vm_area_size(area))
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002984 return -EINVAL;
Jann Hornd8da38e2020-04-20 18:14:11 -07002985 kaddr += off;
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002986
2987 do {
2988 struct page *page = vmalloc_to_page(kaddr);
2989 int ret;
2990
2991 ret = vm_insert_page(vma, uaddr, page);
2992 if (ret)
2993 return ret;
2994
2995 uaddr += PAGE_SIZE;
2996 kaddr += PAGE_SIZE;
2997 size -= PAGE_SIZE;
2998 } while (size > 0);
2999
3000 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3001
3002 return 0;
3003}
3004EXPORT_SYMBOL(remap_vmalloc_range_partial);
3005
3006/**
Nick Piggin83342312006-06-23 02:03:20 -07003007 * remap_vmalloc_range - map vmalloc pages to userspace
Nick Piggin83342312006-06-23 02:03:20 -07003008 * @vma: vma to cover (map full range of vma)
3009 * @addr: vmalloc memory
3010 * @pgoff: number of pages into addr before first page to map
Randy Dunlap76824862008-03-19 17:00:40 -07003011 *
3012 * Returns: 0 for success, -Exxx on failure
Nick Piggin83342312006-06-23 02:03:20 -07003013 *
3014 * This function checks that addr is a valid vmalloc'ed area, and
3015 * that it is big enough to cover the vma. Will return failure if
3016 * that criteria isn't met.
3017 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08003018 * Similar to remap_pfn_range() (see mm/memory.c)
Nick Piggin83342312006-06-23 02:03:20 -07003019 */
3020int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
3021 unsigned long pgoff)
3022{
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07003023 return remap_vmalloc_range_partial(vma, vma->vm_start,
Jann Hornd8da38e2020-04-20 18:14:11 -07003024 addr, pgoff,
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07003025 vma->vm_end - vma->vm_start);
Nick Piggin83342312006-06-23 02:03:20 -07003026}
3027EXPORT_SYMBOL(remap_vmalloc_range);
3028
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -07003029/*
Joerg Roedel6c1051f2020-03-21 18:22:41 -07003030 * Implement stubs for vmalloc_sync_[un]mappings () if the architecture chose
3031 * not to have one.
Joerg Roedel46b306f2019-07-19 20:46:52 +02003032 *
3033 * The purpose of this function is to make sure the vmalloc area
3034 * mappings are identical in all page-tables in the system.
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -07003035 */
Joerg Roedel6c1051f2020-03-21 18:22:41 -07003036void __weak vmalloc_sync_mappings(void)
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -07003037{
3038}
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07003039
Joerg Roedel6c1051f2020-03-21 18:22:41 -07003040void __weak vmalloc_sync_unmappings(void)
3041{
3042}
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07003043
Martin Schwidefsky2f569af2008-02-08 04:22:04 -08003044static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07003045{
David Vrabelcd129092011-09-29 16:53:32 +01003046 pte_t ***p = data;
3047
3048 if (p) {
3049 *(*p) = pte;
3050 (*p)++;
3051 }
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07003052 return 0;
3053}
3054
3055/**
3056 * alloc_vm_area - allocate a range of kernel address space
3057 * @size: size of the area
David Vrabelcd129092011-09-29 16:53:32 +01003058 * @ptes: returns the PTEs for the address space
Randy Dunlap76824862008-03-19 17:00:40 -07003059 *
3060 * Returns: NULL on failure, vm_struct on success
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07003061 *
3062 * This function reserves a range of kernel address space, and
3063 * allocates pagetables to map that range. No actual mappings
David Vrabelcd129092011-09-29 16:53:32 +01003064 * are created.
3065 *
3066 * If @ptes is non-NULL, pointers to the PTEs (in init_mm)
3067 * allocated for the VM area are returned.
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07003068 */
David Vrabelcd129092011-09-29 16:53:32 +01003069struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07003070{
3071 struct vm_struct *area;
3072
Christoph Lameter23016962008-04-28 02:12:42 -07003073 area = get_vm_area_caller(size, VM_IOREMAP,
3074 __builtin_return_address(0));
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07003075 if (area == NULL)
3076 return NULL;
3077
3078 /*
3079 * This ensures that page tables are constructed for this region
3080 * of kernel virtual address space and mapped into init_mm.
3081 */
3082 if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
David Vrabelcd129092011-09-29 16:53:32 +01003083 size, f, ptes ? &ptes : NULL)) {
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07003084 free_vm_area(area);
3085 return NULL;
3086 }
3087
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07003088 return area;
3089}
3090EXPORT_SYMBOL_GPL(alloc_vm_area);
3091
3092void free_vm_area(struct vm_struct *area)
3093{
3094 struct vm_struct *ret;
3095 ret = remove_vm_area(area->addr);
3096 BUG_ON(ret != area);
3097 kfree(area);
3098}
3099EXPORT_SYMBOL_GPL(free_vm_area);
Christoph Lametera10aa572008-04-28 02:12:40 -07003100
Tejun Heo4f8b02b2010-09-03 18:22:47 +02003101#ifdef CONFIG_SMP
Tejun Heoca23e402009-08-14 15:00:52 +09003102static struct vmap_area *node_to_va(struct rb_node *n)
3103{
Geliang Tang4583e772017-02-22 15:41:54 -08003104 return rb_entry_safe(n, struct vmap_area, rb_node);
Tejun Heoca23e402009-08-14 15:00:52 +09003105}
3106
3107/**
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003108 * pvm_find_va_enclose_addr - find the vmap_area @addr belongs to
3109 * @addr: target address
Tejun Heoca23e402009-08-14 15:00:52 +09003110 *
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003111 * Returns: vmap_area if it is found. If there is no such area
3112 * the first highest(reverse order) vmap_area is returned
3113 * i.e. va->va_start < addr && va->va_end < addr or NULL
3114 * if there are no any areas before @addr.
Tejun Heoca23e402009-08-14 15:00:52 +09003115 */
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003116static struct vmap_area *
3117pvm_find_va_enclose_addr(unsigned long addr)
Tejun Heoca23e402009-08-14 15:00:52 +09003118{
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003119 struct vmap_area *va, *tmp;
3120 struct rb_node *n;
3121
3122 n = free_vmap_area_root.rb_node;
3123 va = NULL;
Tejun Heoca23e402009-08-14 15:00:52 +09003124
3125 while (n) {
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003126 tmp = rb_entry(n, struct vmap_area, rb_node);
3127 if (tmp->va_start <= addr) {
3128 va = tmp;
3129 if (tmp->va_end >= addr)
3130 break;
3131
Tejun Heoca23e402009-08-14 15:00:52 +09003132 n = n->rb_right;
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003133 } else {
3134 n = n->rb_left;
3135 }
Tejun Heoca23e402009-08-14 15:00:52 +09003136 }
3137
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003138 return va;
Tejun Heoca23e402009-08-14 15:00:52 +09003139}
3140
3141/**
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003142 * pvm_determine_end_from_reverse - find the highest aligned address
3143 * of free block below VMALLOC_END
3144 * @va:
3145 * in - the VA we start the search(reverse order);
3146 * out - the VA with the highest aligned end address.
Tejun Heoca23e402009-08-14 15:00:52 +09003147 *
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003148 * Returns: determined end address within vmap_area
Tejun Heoca23e402009-08-14 15:00:52 +09003149 */
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003150static unsigned long
3151pvm_determine_end_from_reverse(struct vmap_area **va, unsigned long align)
Tejun Heoca23e402009-08-14 15:00:52 +09003152{
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003153 unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
Tejun Heoca23e402009-08-14 15:00:52 +09003154 unsigned long addr;
3155
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003156 if (likely(*va)) {
3157 list_for_each_entry_from_reverse((*va),
3158 &free_vmap_area_list, list) {
3159 addr = min((*va)->va_end & ~(align - 1), vmalloc_end);
3160 if ((*va)->va_start < addr)
3161 return addr;
3162 }
Tejun Heoca23e402009-08-14 15:00:52 +09003163 }
3164
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003165 return 0;
Tejun Heoca23e402009-08-14 15:00:52 +09003166}
3167
3168/**
3169 * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator
3170 * @offsets: array containing offset of each area
3171 * @sizes: array containing size of each area
3172 * @nr_vms: the number of areas to allocate
3173 * @align: alignment, all entries in @offsets and @sizes must be aligned to this
Tejun Heoca23e402009-08-14 15:00:52 +09003174 *
3175 * Returns: kmalloc'd vm_struct pointer array pointing to allocated
3176 * vm_structs on success, %NULL on failure
3177 *
3178 * Percpu allocator wants to use congruent vm areas so that it can
3179 * maintain the offsets among percpu areas. This function allocates
David Rientjesec3f64f2011-01-13 15:46:01 -08003180 * congruent vmalloc areas for it with GFP_KERNEL. These areas tend to
3181 * be scattered pretty far, distance between two areas easily going up
3182 * to gigabytes. To avoid interacting with regular vmallocs, these
3183 * areas are allocated from top.
Tejun Heoca23e402009-08-14 15:00:52 +09003184 *
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003185 * Despite its complicated look, this allocator is rather simple. It
3186 * does everything top-down and scans free blocks from the end looking
3187 * for matching base. While scanning, if any of the areas do not fit the
3188 * base address is pulled down to fit the area. Scanning is repeated till
3189 * all the areas fit and then all necessary data structures are inserted
3190 * and the result is returned.
Tejun Heoca23e402009-08-14 15:00:52 +09003191 */
3192struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
3193 const size_t *sizes, int nr_vms,
David Rientjesec3f64f2011-01-13 15:46:01 -08003194 size_t align)
Tejun Heoca23e402009-08-14 15:00:52 +09003195{
3196 const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align);
3197 const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003198 struct vmap_area **vas, *va;
Tejun Heoca23e402009-08-14 15:00:52 +09003199 struct vm_struct **vms;
3200 int area, area2, last_area, term_area;
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003201 unsigned long base, start, size, end, last_end;
Tejun Heoca23e402009-08-14 15:00:52 +09003202 bool purged = false;
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003203 enum fit_type type;
Tejun Heoca23e402009-08-14 15:00:52 +09003204
Tejun Heoca23e402009-08-14 15:00:52 +09003205 /* verify parameters and allocate data structures */
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08003206 BUG_ON(offset_in_page(align) || !is_power_of_2(align));
Tejun Heoca23e402009-08-14 15:00:52 +09003207 for (last_area = 0, area = 0; area < nr_vms; area++) {
3208 start = offsets[area];
3209 end = start + sizes[area];
3210
3211 /* is everything aligned properly? */
3212 BUG_ON(!IS_ALIGNED(offsets[area], align));
3213 BUG_ON(!IS_ALIGNED(sizes[area], align));
3214
3215 /* detect the area with the highest address */
3216 if (start > offsets[last_area])
3217 last_area = area;
3218
Wei Yangc568da22017-09-06 16:24:09 -07003219 for (area2 = area + 1; area2 < nr_vms; area2++) {
Tejun Heoca23e402009-08-14 15:00:52 +09003220 unsigned long start2 = offsets[area2];
3221 unsigned long end2 = start2 + sizes[area2];
3222
Wei Yangc568da22017-09-06 16:24:09 -07003223 BUG_ON(start2 < end && start < end2);
Tejun Heoca23e402009-08-14 15:00:52 +09003224 }
3225 }
3226 last_end = offsets[last_area] + sizes[last_area];
3227
3228 if (vmalloc_end - vmalloc_start < last_end) {
3229 WARN_ON(true);
3230 return NULL;
3231 }
3232
Thomas Meyer4d67d862012-05-29 15:06:21 -07003233 vms = kcalloc(nr_vms, sizeof(vms[0]), GFP_KERNEL);
3234 vas = kcalloc(nr_vms, sizeof(vas[0]), GFP_KERNEL);
Tejun Heoca23e402009-08-14 15:00:52 +09003235 if (!vas || !vms)
Kautuk Consulf1db7af2012-01-12 17:20:08 -08003236 goto err_free2;
Tejun Heoca23e402009-08-14 15:00:52 +09003237
3238 for (area = 0; area < nr_vms; area++) {
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003239 vas[area] = kmem_cache_zalloc(vmap_area_cachep, GFP_KERNEL);
David Rientjesec3f64f2011-01-13 15:46:01 -08003240 vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL);
Tejun Heoca23e402009-08-14 15:00:52 +09003241 if (!vas[area] || !vms[area])
3242 goto err_free;
3243 }
3244retry:
3245 spin_lock(&vmap_area_lock);
3246
3247 /* start scanning - we scan from the top, begin with the last area */
3248 area = term_area = last_area;
3249 start = offsets[area];
3250 end = start + sizes[area];
3251
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003252 va = pvm_find_va_enclose_addr(vmalloc_end);
3253 base = pvm_determine_end_from_reverse(&va, align) - end;
Tejun Heoca23e402009-08-14 15:00:52 +09003254
3255 while (true) {
Tejun Heoca23e402009-08-14 15:00:52 +09003256 /*
3257 * base might have underflowed, add last_end before
3258 * comparing.
3259 */
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003260 if (base + last_end < vmalloc_start + last_end)
3261 goto overflow;
Tejun Heoca23e402009-08-14 15:00:52 +09003262
3263 /*
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003264 * Fitting base has not been found.
Tejun Heoca23e402009-08-14 15:00:52 +09003265 */
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003266 if (va == NULL)
3267 goto overflow;
Tejun Heoca23e402009-08-14 15:00:52 +09003268
3269 /*
Kuppuswamy Sathyanarayananf36154e2019-08-13 15:37:31 -07003270 * If required width exeeds current VA block, move
3271 * base downwards and then recheck.
3272 */
3273 if (base + end > va->va_end) {
3274 base = pvm_determine_end_from_reverse(&va, align) - end;
3275 term_area = area;
3276 continue;
3277 }
3278
3279 /*
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003280 * If this VA does not fit, move base downwards and recheck.
Tejun Heoca23e402009-08-14 15:00:52 +09003281 */
Kuppuswamy Sathyanarayananf36154e2019-08-13 15:37:31 -07003282 if (base + start < va->va_start) {
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003283 va = node_to_va(rb_prev(&va->rb_node));
3284 base = pvm_determine_end_from_reverse(&va, align) - end;
Tejun Heoca23e402009-08-14 15:00:52 +09003285 term_area = area;
3286 continue;
3287 }
3288
3289 /*
3290 * This area fits, move on to the previous one. If
3291 * the previous one is the terminal one, we're done.
3292 */
3293 area = (area + nr_vms - 1) % nr_vms;
3294 if (area == term_area)
3295 break;
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003296
Tejun Heoca23e402009-08-14 15:00:52 +09003297 start = offsets[area];
3298 end = start + sizes[area];
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003299 va = pvm_find_va_enclose_addr(base + end);
Tejun Heoca23e402009-08-14 15:00:52 +09003300 }
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003301
Tejun Heoca23e402009-08-14 15:00:52 +09003302 /* we've found a fitting base, insert all va's */
3303 for (area = 0; area < nr_vms; area++) {
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003304 int ret;
Tejun Heoca23e402009-08-14 15:00:52 +09003305
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003306 start = base + offsets[area];
3307 size = sizes[area];
3308
3309 va = pvm_find_va_enclose_addr(start);
3310 if (WARN_ON_ONCE(va == NULL))
3311 /* It is a BUG(), but trigger recovery instead. */
3312 goto recovery;
3313
3314 type = classify_va_fit_type(va, start, size);
3315 if (WARN_ON_ONCE(type == NOTHING_FIT))
3316 /* It is a BUG(), but trigger recovery instead. */
3317 goto recovery;
3318
3319 ret = adjust_va_to_fit_type(va, start, size, type);
3320 if (unlikely(ret))
3321 goto recovery;
3322
3323 /* Allocated area. */
3324 va = vas[area];
3325 va->va_start = start;
3326 va->va_end = start + size;
3327
3328 insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
Tejun Heoca23e402009-08-14 15:00:52 +09003329 }
3330
Tejun Heoca23e402009-08-14 15:00:52 +09003331 spin_unlock(&vmap_area_lock);
3332
3333 /* insert all vm's */
3334 for (area = 0; area < nr_vms; area++)
Zhang Yanfei3645cb42013-07-03 15:04:48 -07003335 setup_vmalloc_vm(vms[area], vas[area], VM_ALLOC,
3336 pcpu_get_vm_areas);
Tejun Heoca23e402009-08-14 15:00:52 +09003337
3338 kfree(vas);
3339 return vms;
3340
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003341recovery:
3342 /* Remove previously inserted areas. */
3343 while (area--) {
3344 __free_vmap_area(vas[area]);
3345 vas[area] = NULL;
3346 }
3347
3348overflow:
3349 spin_unlock(&vmap_area_lock);
3350 if (!purged) {
3351 purge_vmap_area_lazy();
3352 purged = true;
3353
3354 /* Before "retry", check if we recover. */
3355 for (area = 0; area < nr_vms; area++) {
3356 if (vas[area])
3357 continue;
3358
3359 vas[area] = kmem_cache_zalloc(
3360 vmap_area_cachep, GFP_KERNEL);
3361 if (!vas[area])
3362 goto err_free;
3363 }
3364
3365 goto retry;
3366 }
3367
Tejun Heoca23e402009-08-14 15:00:52 +09003368err_free:
3369 for (area = 0; area < nr_vms; area++) {
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003370 if (vas[area])
3371 kmem_cache_free(vmap_area_cachep, vas[area]);
3372
Kautuk Consulf1db7af2012-01-12 17:20:08 -08003373 kfree(vms[area]);
Tejun Heoca23e402009-08-14 15:00:52 +09003374 }
Kautuk Consulf1db7af2012-01-12 17:20:08 -08003375err_free2:
Tejun Heoca23e402009-08-14 15:00:52 +09003376 kfree(vas);
3377 kfree(vms);
3378 return NULL;
3379}
3380
3381/**
3382 * pcpu_free_vm_areas - free vmalloc areas for percpu allocator
3383 * @vms: vm_struct pointer array returned by pcpu_get_vm_areas()
3384 * @nr_vms: the number of allocated areas
3385 *
3386 * Free vm_structs and the array allocated by pcpu_get_vm_areas().
3387 */
3388void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
3389{
3390 int i;
3391
3392 for (i = 0; i < nr_vms; i++)
3393 free_vm_area(vms[i]);
3394 kfree(vms);
3395}
Tejun Heo4f8b02b2010-09-03 18:22:47 +02003396#endif /* CONFIG_SMP */
Christoph Lametera10aa572008-04-28 02:12:40 -07003397
3398#ifdef CONFIG_PROC_FS
3399static void *s_start(struct seq_file *m, loff_t *pos)
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003400 __acquires(&vmap_area_lock)
Christoph Lametera10aa572008-04-28 02:12:40 -07003401{
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003402 spin_lock(&vmap_area_lock);
zijun_hu3f500062016-12-12 16:42:17 -08003403 return seq_list_start(&vmap_area_list, *pos);
Christoph Lametera10aa572008-04-28 02:12:40 -07003404}
3405
3406static void *s_next(struct seq_file *m, void *p, loff_t *pos)
3407{
zijun_hu3f500062016-12-12 16:42:17 -08003408 return seq_list_next(p, &vmap_area_list, pos);
Christoph Lametera10aa572008-04-28 02:12:40 -07003409}
3410
3411static void s_stop(struct seq_file *m, void *p)
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003412 __releases(&vmap_area_lock)
Christoph Lametera10aa572008-04-28 02:12:40 -07003413{
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003414 spin_unlock(&vmap_area_lock);
Christoph Lametera10aa572008-04-28 02:12:40 -07003415}
3416
Eric Dumazeta47a1262008-07-23 21:27:38 -07003417static void show_numa_info(struct seq_file *m, struct vm_struct *v)
3418{
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08003419 if (IS_ENABLED(CONFIG_NUMA)) {
Eric Dumazeta47a1262008-07-23 21:27:38 -07003420 unsigned int nr, *counters = m->private;
3421
3422 if (!counters)
3423 return;
3424
Wanpeng Liaf123462013-11-12 15:07:32 -08003425 if (v->flags & VM_UNINITIALIZED)
3426 return;
Dmitry Vyukov7e5b5282014-12-12 16:56:30 -08003427 /* Pair with smp_wmb() in clear_vm_uninitialized_flag() */
3428 smp_rmb();
Wanpeng Liaf123462013-11-12 15:07:32 -08003429
Eric Dumazeta47a1262008-07-23 21:27:38 -07003430 memset(counters, 0, nr_node_ids * sizeof(unsigned int));
3431
3432 for (nr = 0; nr < v->nr_pages; nr++)
3433 counters[page_to_nid(v->pages[nr])]++;
3434
3435 for_each_node_state(nr, N_HIGH_MEMORY)
3436 if (counters[nr])
3437 seq_printf(m, " N%u=%u", nr, counters[nr]);
3438 }
3439}
3440
Christoph Lametera10aa572008-04-28 02:12:40 -07003441static int s_show(struct seq_file *m, void *p)
3442{
zijun_hu3f500062016-12-12 16:42:17 -08003443 struct vmap_area *va;
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003444 struct vm_struct *v;
3445
zijun_hu3f500062016-12-12 16:42:17 -08003446 va = list_entry(p, struct vmap_area, list);
3447
Wanpeng Lic2ce8c12013-11-12 15:07:31 -08003448 /*
3449 * s_show can encounter race with remove_vm_area, !VM_VM_AREA on
3450 * behalf of vmap area is being tear down or vm_map_ram allocation.
3451 */
Yisheng Xie78c72742017-07-10 15:48:09 -07003452 if (!(va->flags & VM_VM_AREA)) {
3453 seq_printf(m, "0x%pK-0x%pK %7ld %s\n",
3454 (void *)va->va_start, (void *)va->va_end,
3455 va->va_end - va->va_start,
3456 va->flags & VM_LAZY_FREE ? "unpurged vm_area" : "vm_map_ram");
3457
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003458 return 0;
Yisheng Xie78c72742017-07-10 15:48:09 -07003459 }
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003460
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003461 v = va->vm;
Christoph Lametera10aa572008-04-28 02:12:40 -07003462
Shiraz Hashimd57b68c2017-10-27 11:49:01 +05303463 if (v->flags & VM_LOWMEM)
3464 return 0;
3465
Kees Cook45ec1692012-10-08 16:34:09 -07003466 seq_printf(m, "0x%pK-0x%pK %7ld",
Christoph Lametera10aa572008-04-28 02:12:40 -07003467 v->addr, v->addr + v->size, v->size);
3468
Joe Perches62c70bc2011-01-13 15:45:52 -08003469 if (v->caller)
3470 seq_printf(m, " %pS", v->caller);
Christoph Lameter23016962008-04-28 02:12:42 -07003471
Christoph Lametera10aa572008-04-28 02:12:40 -07003472 if (v->nr_pages)
3473 seq_printf(m, " pages=%d", v->nr_pages);
3474
3475 if (v->phys_addr)
Miles Chen199eaa02017-02-24 14:59:51 -08003476 seq_printf(m, " phys=%pa", &v->phys_addr);
Christoph Lametera10aa572008-04-28 02:12:40 -07003477
3478 if (v->flags & VM_IOREMAP)
Fabian Frederickf4527c92014-06-04 16:08:09 -07003479 seq_puts(m, " ioremap");
Christoph Lametera10aa572008-04-28 02:12:40 -07003480
3481 if (v->flags & VM_ALLOC)
Fabian Frederickf4527c92014-06-04 16:08:09 -07003482 seq_puts(m, " vmalloc");
Christoph Lametera10aa572008-04-28 02:12:40 -07003483
3484 if (v->flags & VM_MAP)
Fabian Frederickf4527c92014-06-04 16:08:09 -07003485 seq_puts(m, " vmap");
Christoph Lametera10aa572008-04-28 02:12:40 -07003486
3487 if (v->flags & VM_USERMAP)
Fabian Frederickf4527c92014-06-04 16:08:09 -07003488 seq_puts(m, " user");
Christoph Lametera10aa572008-04-28 02:12:40 -07003489
David Rientjes244d63e2016-01-14 15:19:35 -08003490 if (is_vmalloc_addr(v->pages))
Fabian Frederickf4527c92014-06-04 16:08:09 -07003491 seq_puts(m, " vpages");
Christoph Lametera10aa572008-04-28 02:12:40 -07003492
Eric Dumazeta47a1262008-07-23 21:27:38 -07003493 show_numa_info(m, v);
Christoph Lametera10aa572008-04-28 02:12:40 -07003494 seq_putc(m, '\n');
3495 return 0;
3496}
3497
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04003498static const struct seq_operations vmalloc_op = {
Christoph Lametera10aa572008-04-28 02:12:40 -07003499 .start = s_start,
3500 .next = s_next,
3501 .stop = s_stop,
3502 .show = s_show,
3503};
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04003504
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04003505static int __init proc_vmalloc_init(void)
3506{
Christoph Hellwigfddda2b2018-04-13 19:44:18 +02003507 if (IS_ENABLED(CONFIG_NUMA))
Joe Perches0825a6f2018-06-14 15:27:58 -07003508 proc_create_seq_private("vmallocinfo", 0400, NULL,
Christoph Hellwig44414d82018-04-24 17:05:17 +02003509 &vmalloc_op,
3510 nr_node_ids * sizeof(unsigned int), NULL);
Christoph Hellwigfddda2b2018-04-13 19:44:18 +02003511 else
Joe Perches0825a6f2018-06-14 15:27:58 -07003512 proc_create_seq("vmallocinfo", 0400, NULL, &vmalloc_op);
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04003513 return 0;
3514}
3515module_init(proc_vmalloc_init);
Joonsoo Kimdb3808c2013-04-29 15:07:28 -07003516
Christoph Lametera10aa572008-04-28 02:12:40 -07003517#endif
3518