blob: c42872ed82acb6c0f0f367181c64a3f91eeb15c4 [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>
Rick Edgecombe868b1042019-04-25 17:11:36 -070021#include <linux/set_memory.h>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070022#include <linux/debugobjects.h>
Christoph Lameter23016962008-04-28 02:12:42 -070023#include <linux/kallsyms.h>
Nick Piggindb64fe02008-10-18 20:27:03 -070024#include <linux/list.h>
Chris Wilson4da56b92016-04-04 14:46:42 +010025#include <linux/notifier.h>
Nick Piggindb64fe02008-10-18 20:27:03 -070026#include <linux/rbtree.h>
27#include <linux/radix-tree.h>
28#include <linux/rcupdate.h>
Tejun Heof0aa6612009-02-20 16:29:08 +090029#include <linux/pfn.h>
Catalin Marinas89219d32009-06-11 13:23:19 +010030#include <linux/kmemleak.h>
Arun Sharma600634972011-07-26 16:09:06 -070031#include <linux/atomic.h>
Gideon Israel Dsouza3b321232014-04-07 15:37:26 -070032#include <linux/compiler.h>
Al Viro32fcfd42013-03-10 20:14:08 -040033#include <linux/llist.h>
Toshi Kani0f616be2015-04-14 15:47:17 -070034#include <linux/bitops.h>
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -070035#include <linux/rbtree_augmented.h>
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)bb850f42019-05-17 14:31:34 -0700328#define DEBUG_AUGMENT_PROPAGATE_CHECK 0
Uladzislau Rezki (Sony)a6cf4e02019-05-17 14:31:37 -0700329#define DEBUG_AUGMENT_LOWEST_MATCH_CHECK 0
Uladzislau Rezki (Sony)bb850f42019-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)68ad4a32019-05-17 14:31:31 -0700339static bool vmap_initialized __read_mostly;
Nick Piggin89699602011-03-22 16:30:36 -0700340
Uladzislau Rezki (Sony)68ad4a32019-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)68ad4a32019-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
401static struct vmap_area *__find_vmap_area(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Nick Piggindb64fe02008-10-18 20:27:03 -0700403 struct rb_node *n = vmap_area_root.rb_node;
404
405 while (n) {
406 struct vmap_area *va;
407
408 va = rb_entry(n, struct vmap_area, rb_node);
409 if (addr < va->va_start)
410 n = n->rb_left;
HATAYAMA Daisukecef2ac32013-07-03 15:02:17 -0700411 else if (addr >= va->va_end)
Nick Piggindb64fe02008-10-18 20:27:03 -0700412 n = n->rb_right;
413 else
414 return va;
415 }
416
417 return NULL;
418}
419
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700420/*
421 * This function returns back addresses of parent node
422 * and its left or right link for further processing.
423 */
424static __always_inline struct rb_node **
425find_va_links(struct vmap_area *va,
426 struct rb_root *root, struct rb_node *from,
427 struct rb_node **parent)
Nick Piggindb64fe02008-10-18 20:27:03 -0700428{
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700429 struct vmap_area *tmp_va;
430 struct rb_node **link;
Nick Piggindb64fe02008-10-18 20:27:03 -0700431
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700432 if (root) {
433 link = &root->rb_node;
434 if (unlikely(!*link)) {
435 *parent = NULL;
436 return link;
437 }
438 } else {
439 link = &from;
Nick Piggindb64fe02008-10-18 20:27:03 -0700440 }
441
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700442 /*
443 * Go to the bottom of the tree. When we hit the last point
444 * we end up with parent rb_node and correct direction, i name
445 * it link, where the new va->rb_node will be attached to.
446 */
447 do {
448 tmp_va = rb_entry(*link, struct vmap_area, rb_node);
Nick Piggindb64fe02008-10-18 20:27:03 -0700449
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700450 /*
451 * During the traversal we also do some sanity check.
452 * Trigger the BUG() if there are sides(left/right)
453 * or full overlaps.
454 */
455 if (va->va_start < tmp_va->va_end &&
456 va->va_end <= tmp_va->va_start)
457 link = &(*link)->rb_left;
458 else if (va->va_end > tmp_va->va_start &&
459 va->va_start >= tmp_va->va_end)
460 link = &(*link)->rb_right;
461 else
462 BUG();
463 } while (*link);
464
465 *parent = &tmp_va->rb_node;
466 return link;
Nick Piggindb64fe02008-10-18 20:27:03 -0700467}
468
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700469static __always_inline struct list_head *
470get_va_next_sibling(struct rb_node *parent, struct rb_node **link)
471{
472 struct list_head *list;
Nick Piggindb64fe02008-10-18 20:27:03 -0700473
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700474 if (unlikely(!parent))
475 /*
476 * The red-black tree where we try to find VA neighbors
477 * before merging or inserting is empty, i.e. it means
478 * there is no free vmap space. Normally it does not
479 * happen but we handle this case anyway.
480 */
481 return NULL;
482
483 list = &rb_entry(parent, struct vmap_area, rb_node)->list;
484 return (&parent->rb_right == link ? list->next : list);
485}
486
487static __always_inline void
488link_va(struct vmap_area *va, struct rb_root *root,
489 struct rb_node *parent, struct rb_node **link, struct list_head *head)
490{
491 /*
492 * VA is still not in the list, but we can
493 * identify its future previous list_head node.
494 */
495 if (likely(parent)) {
496 head = &rb_entry(parent, struct vmap_area, rb_node)->list;
497 if (&parent->rb_right != link)
498 head = head->prev;
499 }
500
501 /* Insert to the rb-tree */
502 rb_link_node(&va->rb_node, parent, link);
503 if (root == &free_vmap_area_root) {
504 /*
505 * Some explanation here. Just perform simple insertion
506 * to the tree. We do not set va->subtree_max_size to
507 * its current size before calling rb_insert_augmented().
508 * It is because of we populate the tree from the bottom
509 * to parent levels when the node _is_ in the tree.
510 *
511 * Therefore we set subtree_max_size to zero after insertion,
512 * to let __augment_tree_propagate_from() puts everything to
513 * the correct order later on.
514 */
515 rb_insert_augmented(&va->rb_node,
516 root, &free_vmap_area_rb_augment_cb);
517 va->subtree_max_size = 0;
518 } else {
519 rb_insert_color(&va->rb_node, root);
520 }
521
522 /* Address-sort this list */
523 list_add(&va->list, head);
524}
525
526static __always_inline void
527unlink_va(struct vmap_area *va, struct rb_root *root)
528{
529 /*
530 * During merging a VA node can be empty, therefore
531 * not linked with the tree nor list. Just check it.
532 */
533 if (!RB_EMPTY_NODE(&va->rb_node)) {
534 if (root == &free_vmap_area_root)
535 rb_erase_augmented(&va->rb_node,
536 root, &free_vmap_area_rb_augment_cb);
537 else
538 rb_erase(&va->rb_node, root);
539
540 list_del(&va->list);
541 RB_CLEAR_NODE(&va->rb_node);
542 }
543}
544
Uladzislau Rezki (Sony)bb850f42019-05-17 14:31:34 -0700545#if DEBUG_AUGMENT_PROPAGATE_CHECK
546static void
547augment_tree_propagate_check(struct rb_node *n)
548{
549 struct vmap_area *va;
550 struct rb_node *node;
551 unsigned long size;
552 bool found = false;
553
554 if (n == NULL)
555 return;
556
557 va = rb_entry(n, struct vmap_area, rb_node);
558 size = va->subtree_max_size;
559 node = n;
560
561 while (node) {
562 va = rb_entry(node, struct vmap_area, rb_node);
563
564 if (get_subtree_max_size(node->rb_left) == size) {
565 node = node->rb_left;
566 } else {
567 if (va_size(va) == size) {
568 found = true;
569 break;
570 }
571
572 node = node->rb_right;
573 }
574 }
575
576 if (!found) {
577 va = rb_entry(n, struct vmap_area, rb_node);
578 pr_emerg("tree is corrupted: %lu, %lu\n",
579 va_size(va), va->subtree_max_size);
580 }
581
582 augment_tree_propagate_check(n->rb_left);
583 augment_tree_propagate_check(n->rb_right);
584}
585#endif
586
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700587/*
588 * This function populates subtree_max_size from bottom to upper
589 * levels starting from VA point. The propagation must be done
590 * when VA size is modified by changing its va_start/va_end. Or
591 * in case of newly inserting of VA to the tree.
592 *
593 * It means that __augment_tree_propagate_from() must be called:
594 * - After VA has been inserted to the tree(free path);
595 * - After VA has been shrunk(allocation path);
596 * - After VA has been increased(merging path).
597 *
598 * Please note that, it does not mean that upper parent nodes
599 * and their subtree_max_size are recalculated all the time up
600 * to the root node.
601 *
602 * 4--8
603 * /\
604 * / \
605 * / \
606 * 2--2 8--8
607 *
608 * For example if we modify the node 4, shrinking it to 2, then
609 * no any modification is required. If we shrink the node 2 to 1
610 * its subtree_max_size is updated only, and set to 1. If we shrink
611 * the node 8 to 6, then its subtree_max_size is set to 6 and parent
612 * node becomes 4--6.
613 */
614static __always_inline void
615augment_tree_propagate_from(struct vmap_area *va)
616{
617 struct rb_node *node = &va->rb_node;
618 unsigned long new_va_sub_max_size;
619
620 while (node) {
621 va = rb_entry(node, struct vmap_area, rb_node);
622 new_va_sub_max_size = compute_subtree_max_size(va);
623
624 /*
625 * If the newly calculated maximum available size of the
626 * subtree is equal to the current one, then it means that
627 * the tree is propagated correctly. So we have to stop at
628 * this point to save cycles.
629 */
630 if (va->subtree_max_size == new_va_sub_max_size)
631 break;
632
633 va->subtree_max_size = new_va_sub_max_size;
634 node = rb_parent(&va->rb_node);
635 }
Uladzislau Rezki (Sony)bb850f42019-05-17 14:31:34 -0700636
637#if DEBUG_AUGMENT_PROPAGATE_CHECK
638 augment_tree_propagate_check(free_vmap_area_root.rb_node);
639#endif
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700640}
641
642static void
643insert_vmap_area(struct vmap_area *va,
644 struct rb_root *root, struct list_head *head)
645{
646 struct rb_node **link;
647 struct rb_node *parent;
648
649 link = find_va_links(va, root, NULL, &parent);
650 link_va(va, root, parent, link, head);
651}
652
653static void
654insert_vmap_area_augment(struct vmap_area *va,
655 struct rb_node *from, struct rb_root *root,
656 struct list_head *head)
657{
658 struct rb_node **link;
659 struct rb_node *parent;
660
661 if (from)
662 link = find_va_links(va, NULL, from, &parent);
663 else
664 link = find_va_links(va, root, NULL, &parent);
665
666 link_va(va, root, parent, link, head);
667 augment_tree_propagate_from(va);
668}
669
670/*
671 * Merge de-allocated chunk of VA memory with previous
672 * and next free blocks. If coalesce is not done a new
673 * free area is inserted. If VA has been merged, it is
674 * freed.
675 */
676static __always_inline void
677merge_or_add_vmap_area(struct vmap_area *va,
678 struct rb_root *root, struct list_head *head)
679{
680 struct vmap_area *sibling;
681 struct list_head *next;
682 struct rb_node **link;
683 struct rb_node *parent;
684 bool merged = false;
685
686 /*
687 * Find a place in the tree where VA potentially will be
688 * inserted, unless it is merged with its sibling/siblings.
689 */
690 link = find_va_links(va, root, NULL, &parent);
691
692 /*
693 * Get next node of VA to check if merging can be done.
694 */
695 next = get_va_next_sibling(parent, link);
696 if (unlikely(next == NULL))
697 goto insert;
698
699 /*
700 * start end
701 * | |
702 * |<------VA------>|<-----Next----->|
703 * | |
704 * start end
705 */
706 if (next != head) {
707 sibling = list_entry(next, struct vmap_area, list);
708 if (sibling->va_start == va->va_end) {
709 sibling->va_start = va->va_start;
710
711 /* Check and update the tree if needed. */
712 augment_tree_propagate_from(sibling);
713
714 /* Remove this VA, it has been merged. */
715 unlink_va(va, root);
716
717 /* Free vmap_area object. */
718 kmem_cache_free(vmap_area_cachep, va);
719
720 /* Point to the new merged area. */
721 va = sibling;
722 merged = true;
723 }
724 }
725
726 /*
727 * start end
728 * | |
729 * |<-----Prev----->|<------VA------>|
730 * | |
731 * start end
732 */
733 if (next->prev != head) {
734 sibling = list_entry(next->prev, struct vmap_area, list);
735 if (sibling->va_end == va->va_start) {
736 sibling->va_end = va->va_end;
737
738 /* Check and update the tree if needed. */
739 augment_tree_propagate_from(sibling);
740
741 /* Remove this VA, it has been merged. */
742 unlink_va(va, root);
743
744 /* Free vmap_area object. */
745 kmem_cache_free(vmap_area_cachep, va);
746
747 return;
748 }
749 }
750
751insert:
752 if (!merged) {
753 link_va(va, root, parent, link, head);
754 augment_tree_propagate_from(va);
755 }
756}
757
758static __always_inline bool
759is_within_this_va(struct vmap_area *va, unsigned long size,
760 unsigned long align, unsigned long vstart)
761{
762 unsigned long nva_start_addr;
763
764 if (va->va_start > vstart)
765 nva_start_addr = ALIGN(va->va_start, align);
766 else
767 nva_start_addr = ALIGN(vstart, align);
768
769 /* Can be overflowed due to big size or alignment. */
770 if (nva_start_addr + size < nva_start_addr ||
771 nva_start_addr < vstart)
772 return false;
773
774 return (nva_start_addr + size <= va->va_end);
775}
776
777/*
778 * Find the first free block(lowest start address) in the tree,
779 * that will accomplish the request corresponding to passing
780 * parameters.
781 */
782static __always_inline struct vmap_area *
783find_vmap_lowest_match(unsigned long size,
784 unsigned long align, unsigned long vstart)
785{
786 struct vmap_area *va;
787 struct rb_node *node;
788 unsigned long length;
789
790 /* Start from the root. */
791 node = free_vmap_area_root.rb_node;
792
793 /* Adjust the search size for alignment overhead. */
794 length = size + align - 1;
795
796 while (node) {
797 va = rb_entry(node, struct vmap_area, rb_node);
798
799 if (get_subtree_max_size(node->rb_left) >= length &&
800 vstart < va->va_start) {
801 node = node->rb_left;
802 } else {
803 if (is_within_this_va(va, size, align, vstart))
804 return va;
805
806 /*
807 * Does not make sense to go deeper towards the right
808 * sub-tree if it does not have a free block that is
809 * equal or bigger to the requested search length.
810 */
811 if (get_subtree_max_size(node->rb_right) >= length) {
812 node = node->rb_right;
813 continue;
814 }
815
816 /*
817 * OK. We roll back and find the fist right sub-tree,
818 * that will satisfy the search criteria. It can happen
819 * only once due to "vstart" restriction.
820 */
821 while ((node = rb_parent(node))) {
822 va = rb_entry(node, struct vmap_area, rb_node);
823 if (is_within_this_va(va, size, align, vstart))
824 return va;
825
826 if (get_subtree_max_size(node->rb_right) >= length &&
827 vstart <= va->va_start) {
828 node = node->rb_right;
829 break;
830 }
831 }
832 }
833 }
834
835 return NULL;
836}
837
Uladzislau Rezki (Sony)a6cf4e02019-05-17 14:31:37 -0700838#if DEBUG_AUGMENT_LOWEST_MATCH_CHECK
839#include <linux/random.h>
840
841static struct vmap_area *
842find_vmap_lowest_linear_match(unsigned long size,
843 unsigned long align, unsigned long vstart)
844{
845 struct vmap_area *va;
846
847 list_for_each_entry(va, &free_vmap_area_list, list) {
848 if (!is_within_this_va(va, size, align, vstart))
849 continue;
850
851 return va;
852 }
853
854 return NULL;
855}
856
857static void
858find_vmap_lowest_match_check(unsigned long size)
859{
860 struct vmap_area *va_1, *va_2;
861 unsigned long vstart;
862 unsigned int rnd;
863
864 get_random_bytes(&rnd, sizeof(rnd));
865 vstart = VMALLOC_START + rnd;
866
867 va_1 = find_vmap_lowest_match(size, 1, vstart);
868 va_2 = find_vmap_lowest_linear_match(size, 1, vstart);
869
870 if (va_1 != va_2)
871 pr_emerg("not lowest: t: 0x%p, l: 0x%p, v: 0x%lx\n",
872 va_1, va_2, vstart);
873}
874#endif
875
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700876enum fit_type {
877 NOTHING_FIT = 0,
878 FL_FIT_TYPE = 1, /* full fit */
879 LE_FIT_TYPE = 2, /* left edge fit */
880 RE_FIT_TYPE = 3, /* right edge fit */
881 NE_FIT_TYPE = 4 /* no edge fit */
882};
883
884static __always_inline enum fit_type
885classify_va_fit_type(struct vmap_area *va,
886 unsigned long nva_start_addr, unsigned long size)
887{
888 enum fit_type type;
889
890 /* Check if it is within VA. */
891 if (nva_start_addr < va->va_start ||
892 nva_start_addr + size > va->va_end)
893 return NOTHING_FIT;
894
895 /* Now classify. */
896 if (va->va_start == nva_start_addr) {
897 if (va->va_end == nva_start_addr + size)
898 type = FL_FIT_TYPE;
899 else
900 type = LE_FIT_TYPE;
901 } else if (va->va_end == nva_start_addr + size) {
902 type = RE_FIT_TYPE;
903 } else {
904 type = NE_FIT_TYPE;
905 }
906
907 return type;
908}
909
910static __always_inline int
911adjust_va_to_fit_type(struct vmap_area *va,
912 unsigned long nva_start_addr, unsigned long size,
913 enum fit_type type)
914{
915 struct vmap_area *lva;
916
917 if (type == FL_FIT_TYPE) {
918 /*
919 * No need to split VA, it fully fits.
920 *
921 * | |
922 * V NVA V
923 * |---------------|
924 */
925 unlink_va(va, &free_vmap_area_root);
926 kmem_cache_free(vmap_area_cachep, va);
927 } else if (type == LE_FIT_TYPE) {
928 /*
929 * Split left edge of fit VA.
930 *
931 * | |
932 * V NVA V R
933 * |-------|-------|
934 */
935 va->va_start += size;
936 } else if (type == RE_FIT_TYPE) {
937 /*
938 * Split right edge of fit VA.
939 *
940 * | |
941 * L V NVA V
942 * |-------|-------|
943 */
944 va->va_end = nva_start_addr;
945 } else if (type == NE_FIT_TYPE) {
946 /*
947 * Split no edge of fit VA.
948 *
949 * | |
950 * L V NVA V R
951 * |---|-------|---|
952 */
953 lva = kmem_cache_alloc(vmap_area_cachep, GFP_NOWAIT);
954 if (unlikely(!lva))
955 return -1;
956
957 /*
958 * Build the remainder.
959 */
960 lva->va_start = va->va_start;
961 lva->va_end = nva_start_addr;
962
963 /*
964 * Shrink this VA to remaining size.
965 */
966 va->va_start = nva_start_addr + size;
967 } else {
968 return -1;
969 }
970
971 if (type != FL_FIT_TYPE) {
972 augment_tree_propagate_from(va);
973
974 if (type == NE_FIT_TYPE)
975 insert_vmap_area_augment(lva, &va->rb_node,
976 &free_vmap_area_root, &free_vmap_area_list);
977 }
978
979 return 0;
980}
981
982/*
983 * Returns a start address of the newly allocated area, if success.
984 * Otherwise a vend is returned that indicates failure.
985 */
986static __always_inline unsigned long
987__alloc_vmap_area(unsigned long size, unsigned long align,
988 unsigned long vstart, unsigned long vend, int node)
989{
990 unsigned long nva_start_addr;
991 struct vmap_area *va;
992 enum fit_type type;
993 int ret;
994
995 va = find_vmap_lowest_match(size, align, vstart);
996 if (unlikely(!va))
997 return vend;
998
999 if (va->va_start > vstart)
1000 nva_start_addr = ALIGN(va->va_start, align);
1001 else
1002 nva_start_addr = ALIGN(vstart, align);
1003
1004 /* Check the "vend" restriction. */
1005 if (nva_start_addr + size > vend)
1006 return vend;
1007
1008 /* Classify what we have found. */
1009 type = classify_va_fit_type(va, nva_start_addr, size);
1010 if (WARN_ON_ONCE(type == NOTHING_FIT))
1011 return vend;
1012
1013 /* Update the free vmap_area. */
1014 ret = adjust_va_to_fit_type(va, nva_start_addr, size, type);
1015 if (ret)
1016 return vend;
1017
Uladzislau Rezki (Sony)a6cf4e02019-05-17 14:31:37 -07001018#if DEBUG_AUGMENT_LOWEST_MATCH_CHECK
1019 find_vmap_lowest_match_check(size);
1020#endif
1021
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001022 return nva_start_addr;
1023}
Chris Wilson4da56b92016-04-04 14:46:42 +01001024
Nick Piggindb64fe02008-10-18 20:27:03 -07001025/*
1026 * Allocate a region of KVA of the specified size and alignment, within the
1027 * vstart and vend.
1028 */
1029static struct vmap_area *alloc_vmap_area(unsigned long size,
1030 unsigned long align,
1031 unsigned long vstart, unsigned long vend,
1032 int node, gfp_t gfp_mask)
1033{
1034 struct vmap_area *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 unsigned long addr;
Nick Piggindb64fe02008-10-18 20:27:03 -07001036 int purged = 0;
1037
Nick Piggin77669702009-02-27 14:03:03 -08001038 BUG_ON(!size);
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08001039 BUG_ON(offset_in_page(size));
Nick Piggin89699602011-03-22 16:30:36 -07001040 BUG_ON(!is_power_of_2(align));
Nick Piggindb64fe02008-10-18 20:27:03 -07001041
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001042 if (unlikely(!vmap_initialized))
1043 return ERR_PTR(-EBUSY);
1044
Christoph Hellwig5803ed22016-12-12 16:44:20 -08001045 might_sleep();
Chris Wilson4da56b92016-04-04 14:46:42 +01001046
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001047 va = kmem_cache_alloc_node(vmap_area_cachep,
Nick Piggindb64fe02008-10-18 20:27:03 -07001048 gfp_mask & GFP_RECLAIM_MASK, node);
1049 if (unlikely(!va))
1050 return ERR_PTR(-ENOMEM);
1051
Catalin Marinas7f88f882013-11-12 15:07:45 -08001052 /*
1053 * Only scan the relevant parts containing pointers to other objects
1054 * to avoid false negatives.
1055 */
1056 kmemleak_scan_area(&va->rb_node, SIZE_MAX, gfp_mask & GFP_RECLAIM_MASK);
1057
Nick Piggindb64fe02008-10-18 20:27:03 -07001058retry:
1059 spin_lock(&vmap_area_lock);
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001060
Nick Piggin89699602011-03-22 16:30:36 -07001061 /*
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001062 * If an allocation fails, the "vend" address is
1063 * returned. Therefore trigger the overflow path.
Nick Piggin89699602011-03-22 16:30:36 -07001064 */
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001065 addr = __alloc_vmap_area(size, align, vstart, vend, node);
1066 if (unlikely(addr == vend))
Nick Piggin89699602011-03-22 16:30:36 -07001067 goto overflow;
Nick Piggindb64fe02008-10-18 20:27:03 -07001068
1069 va->va_start = addr;
1070 va->va_end = addr + size;
1071 va->flags = 0;
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001072 insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
1073
Nick Piggindb64fe02008-10-18 20:27:03 -07001074 spin_unlock(&vmap_area_lock);
1075
Wang Xiaoqiang61e16552016-01-15 16:57:19 -08001076 BUG_ON(!IS_ALIGNED(va->va_start, align));
Nick Piggin89699602011-03-22 16:30:36 -07001077 BUG_ON(va->va_start < vstart);
1078 BUG_ON(va->va_end > vend);
1079
Nick Piggindb64fe02008-10-18 20:27:03 -07001080 return va;
Nick Piggin89699602011-03-22 16:30:36 -07001081
1082overflow:
1083 spin_unlock(&vmap_area_lock);
1084 if (!purged) {
1085 purge_vmap_area_lazy();
1086 purged = 1;
1087 goto retry;
1088 }
Chris Wilson4da56b92016-04-04 14:46:42 +01001089
1090 if (gfpflags_allow_blocking(gfp_mask)) {
1091 unsigned long freed = 0;
1092 blocking_notifier_call_chain(&vmap_notify_list, 0, &freed);
1093 if (freed > 0) {
1094 purged = 0;
1095 goto retry;
1096 }
1097 }
1098
Florian Fainelli03497d72017-04-27 11:19:00 -07001099 if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit())
Joe Perches756a0252016-03-17 14:19:47 -07001100 pr_warn("vmap allocation for size %lu failed: use vmalloc=<size> to increase size\n",
1101 size);
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001102
1103 kmem_cache_free(vmap_area_cachep, va);
Nick Piggin89699602011-03-22 16:30:36 -07001104 return ERR_PTR(-EBUSY);
Nick Piggindb64fe02008-10-18 20:27:03 -07001105}
1106
Chris Wilson4da56b92016-04-04 14:46:42 +01001107int register_vmap_purge_notifier(struct notifier_block *nb)
1108{
1109 return blocking_notifier_chain_register(&vmap_notify_list, nb);
1110}
1111EXPORT_SYMBOL_GPL(register_vmap_purge_notifier);
1112
1113int unregister_vmap_purge_notifier(struct notifier_block *nb)
1114{
1115 return blocking_notifier_chain_unregister(&vmap_notify_list, nb);
1116}
1117EXPORT_SYMBOL_GPL(unregister_vmap_purge_notifier);
1118
Nick Piggindb64fe02008-10-18 20:27:03 -07001119static void __free_vmap_area(struct vmap_area *va)
1120{
1121 BUG_ON(RB_EMPTY_NODE(&va->rb_node));
Nick Piggin89699602011-03-22 16:30:36 -07001122
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001123 /*
1124 * Remove from the busy tree/list.
1125 */
1126 unlink_va(va, &vmap_area_root);
Nick Piggindb64fe02008-10-18 20:27:03 -07001127
Tejun Heoca23e402009-08-14 15:00:52 +09001128 /*
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001129 * Merge VA with its neighbors, otherwise just add it.
Tejun Heoca23e402009-08-14 15:00:52 +09001130 */
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001131 merge_or_add_vmap_area(va,
1132 &free_vmap_area_root, &free_vmap_area_list);
Nick Piggindb64fe02008-10-18 20:27:03 -07001133}
1134
1135/*
1136 * Free a region of KVA allocated by alloc_vmap_area
1137 */
1138static void free_vmap_area(struct vmap_area *va)
1139{
1140 spin_lock(&vmap_area_lock);
1141 __free_vmap_area(va);
1142 spin_unlock(&vmap_area_lock);
1143}
1144
1145/*
1146 * Clear the pagetable entries of a given vmap_area
1147 */
1148static void unmap_vmap_area(struct vmap_area *va)
1149{
1150 vunmap_page_range(va->va_start, va->va_end);
1151}
1152
1153/*
1154 * lazy_max_pages is the maximum amount of virtual address space we gather up
1155 * before attempting to purge with a TLB flush.
1156 *
1157 * There is a tradeoff here: a larger number will cover more kernel page tables
1158 * and take slightly longer to purge, but it will linearly reduce the number of
1159 * global TLB flushes that must be performed. It would seem natural to scale
1160 * this number up linearly with the number of CPUs (because vmapping activity
1161 * could also scale linearly with the number of CPUs), however it is likely
1162 * that in practice, workloads might be constrained in other ways that mean
1163 * vmap activity will not scale linearly with CPUs. Also, I want to be
1164 * conservative and not introduce a big latency on huge systems, so go with
1165 * a less aggressive log scale. It will still be an improvement over the old
1166 * code, and it will be simple to change the scale factor if we find that it
1167 * becomes a problem on bigger systems.
1168 */
1169static unsigned long lazy_max_pages(void)
1170{
1171 unsigned int log;
1172
1173 log = fls(num_online_cpus());
1174
1175 return log * (32UL * 1024 * 1024 / PAGE_SIZE);
1176}
1177
Uladzislau Rezki (Sony)4d36e6f2019-05-14 15:41:25 -07001178static atomic_long_t vmap_lazy_nr = ATOMIC_LONG_INIT(0);
Nick Piggindb64fe02008-10-18 20:27:03 -07001179
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001180/*
1181 * Serialize vmap purging. There is no actual criticial section protected
1182 * by this look, but we want to avoid concurrent calls for performance
1183 * reasons and to make the pcpu_get_vm_areas more deterministic.
1184 */
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001185static DEFINE_MUTEX(vmap_purge_lock);
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001186
Nick Piggin02b709d2010-02-01 22:25:57 +11001187/* for per-CPU blocks */
1188static void purge_fragmented_blocks_allcpus(void);
1189
Nick Piggindb64fe02008-10-18 20:27:03 -07001190/*
Cliff Wickman3ee48b62010-09-16 11:44:02 -05001191 * called before a call to iounmap() if the caller wants vm_area_struct's
1192 * immediately freed.
1193 */
1194void set_iounmap_nonlazy(void)
1195{
Uladzislau Rezki (Sony)4d36e6f2019-05-14 15:41:25 -07001196 atomic_long_set(&vmap_lazy_nr, lazy_max_pages()+1);
Cliff Wickman3ee48b62010-09-16 11:44:02 -05001197}
1198
1199/*
Nick Piggindb64fe02008-10-18 20:27:03 -07001200 * Purges all lazily-freed vmap areas.
Nick Piggindb64fe02008-10-18 20:27:03 -07001201 */
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001202static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end)
Nick Piggindb64fe02008-10-18 20:27:03 -07001203{
Uladzislau Rezki (Sony)4d36e6f2019-05-14 15:41:25 -07001204 unsigned long resched_threshold;
Chris Wilson80c4bd72016-05-20 16:57:38 -07001205 struct llist_node *valist;
Nick Piggindb64fe02008-10-18 20:27:03 -07001206 struct vmap_area *va;
Vegard Nossumcbb76672009-02-27 14:03:04 -08001207 struct vmap_area *n_va;
Nick Piggindb64fe02008-10-18 20:27:03 -07001208
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001209 lockdep_assert_held(&vmap_purge_lock);
Nick Piggin02b709d2010-02-01 22:25:57 +11001210
Chris Wilson80c4bd72016-05-20 16:57:38 -07001211 valist = llist_del_all(&vmap_purge_list);
Uladzislau Rezki (Sony)68571be92019-05-14 15:41:22 -07001212 if (unlikely(valist == NULL))
1213 return false;
1214
1215 /*
1216 * TODO: to calculate a flush range without looping.
1217 * The list can be up to lazy_max_pages() elements.
1218 */
Chris Wilson80c4bd72016-05-20 16:57:38 -07001219 llist_for_each_entry(va, valist, purge_list) {
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001220 if (va->va_start < start)
1221 start = va->va_start;
1222 if (va->va_end > end)
1223 end = va->va_end;
Nick Piggindb64fe02008-10-18 20:27:03 -07001224 }
Nick Piggindb64fe02008-10-18 20:27:03 -07001225
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001226 flush_tlb_kernel_range(start, end);
Uladzislau Rezki (Sony)4d36e6f2019-05-14 15:41:25 -07001227 resched_threshold = lazy_max_pages() << 1;
Nick Piggindb64fe02008-10-18 20:27:03 -07001228
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001229 spin_lock(&vmap_area_lock);
Joel Fernandes763b2182016-12-12 16:44:26 -08001230 llist_for_each_entry_safe(va, n_va, valist, purge_list) {
Uladzislau Rezki (Sony)4d36e6f2019-05-14 15:41:25 -07001231 unsigned long nr = (va->va_end - va->va_start) >> PAGE_SHIFT;
Joel Fernandes763b2182016-12-12 16:44:26 -08001232
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001233 __free_vmap_area(va);
Uladzislau Rezki (Sony)4d36e6f2019-05-14 15:41:25 -07001234 atomic_long_sub(nr, &vmap_lazy_nr);
Uladzislau Rezki (Sony)68571be92019-05-14 15:41:22 -07001235
Uladzislau Rezki (Sony)4d36e6f2019-05-14 15:41:25 -07001236 if (atomic_long_read(&vmap_lazy_nr) < resched_threshold)
Uladzislau Rezki (Sony)68571be92019-05-14 15:41:22 -07001237 cond_resched_lock(&vmap_area_lock);
Joel Fernandes763b2182016-12-12 16:44:26 -08001238 }
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001239 spin_unlock(&vmap_area_lock);
1240 return true;
Nick Piggindb64fe02008-10-18 20:27:03 -07001241}
1242
1243/*
Nick Piggin496850e2008-11-19 15:36:33 -08001244 * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
1245 * is already purging.
1246 */
1247static void try_purge_vmap_area_lazy(void)
1248{
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001249 if (mutex_trylock(&vmap_purge_lock)) {
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001250 __purge_vmap_area_lazy(ULONG_MAX, 0);
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001251 mutex_unlock(&vmap_purge_lock);
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001252 }
Nick Piggin496850e2008-11-19 15:36:33 -08001253}
1254
1255/*
Nick Piggindb64fe02008-10-18 20:27:03 -07001256 * Kick off a purge of the outstanding lazy areas.
1257 */
1258static void purge_vmap_area_lazy(void)
1259{
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001260 mutex_lock(&vmap_purge_lock);
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001261 purge_fragmented_blocks_allcpus();
1262 __purge_vmap_area_lazy(ULONG_MAX, 0);
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001263 mutex_unlock(&vmap_purge_lock);
Nick Piggindb64fe02008-10-18 20:27:03 -07001264}
1265
1266/*
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -08001267 * Free a vmap area, caller ensuring that the area has been unmapped
1268 * and flush_cache_vunmap had been called for the correct range
1269 * previously.
Nick Piggindb64fe02008-10-18 20:27:03 -07001270 */
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -08001271static void free_vmap_area_noflush(struct vmap_area *va)
Nick Piggindb64fe02008-10-18 20:27:03 -07001272{
Uladzislau Rezki (Sony)4d36e6f2019-05-14 15:41:25 -07001273 unsigned long nr_lazy;
Chris Wilson80c4bd72016-05-20 16:57:38 -07001274
Uladzislau Rezki (Sony)4d36e6f2019-05-14 15:41:25 -07001275 nr_lazy = atomic_long_add_return((va->va_end - va->va_start) >>
1276 PAGE_SHIFT, &vmap_lazy_nr);
Chris Wilson80c4bd72016-05-20 16:57:38 -07001277
1278 /* After this point, we may free va at any time */
1279 llist_add(&va->purge_list, &vmap_purge_list);
1280
1281 if (unlikely(nr_lazy > lazy_max_pages()))
Nick Piggin496850e2008-11-19 15:36:33 -08001282 try_purge_vmap_area_lazy();
Nick Piggindb64fe02008-10-18 20:27:03 -07001283}
1284
Nick Pigginb29acbd2008-12-01 13:13:47 -08001285/*
1286 * Free and unmap a vmap area
1287 */
1288static void free_unmap_vmap_area(struct vmap_area *va)
1289{
1290 flush_cache_vunmap(va->va_start, va->va_end);
Christoph Hellwigc8eef012016-12-12 16:44:01 -08001291 unmap_vmap_area(va);
Chintan Pandya82a2e922018-06-07 17:06:46 -07001292 if (debug_pagealloc_enabled())
1293 flush_tlb_kernel_range(va->va_start, va->va_end);
1294
Christoph Hellwigc8eef012016-12-12 16:44:01 -08001295 free_vmap_area_noflush(va);
Nick Pigginb29acbd2008-12-01 13:13:47 -08001296}
1297
Nick Piggindb64fe02008-10-18 20:27:03 -07001298static struct vmap_area *find_vmap_area(unsigned long addr)
1299{
1300 struct vmap_area *va;
1301
1302 spin_lock(&vmap_area_lock);
1303 va = __find_vmap_area(addr);
1304 spin_unlock(&vmap_area_lock);
1305
1306 return va;
1307}
1308
Nick Piggindb64fe02008-10-18 20:27:03 -07001309/*** Per cpu kva allocator ***/
1310
1311/*
1312 * vmap space is limited especially on 32 bit architectures. Ensure there is
1313 * room for at least 16 percpu vmap blocks per CPU.
1314 */
1315/*
1316 * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
1317 * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess
1318 * instead (we just need a rough idea)
1319 */
1320#if BITS_PER_LONG == 32
1321#define VMALLOC_SPACE (128UL*1024*1024)
1322#else
1323#define VMALLOC_SPACE (128UL*1024*1024*1024)
1324#endif
1325
1326#define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE)
1327#define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */
1328#define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */
1329#define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2)
1330#define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */
1331#define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */
Clemens Ladischf982f9152011-06-21 22:09:50 +02001332#define VMAP_BBMAP_BITS \
1333 VMAP_MIN(VMAP_BBMAP_BITS_MAX, \
1334 VMAP_MAX(VMAP_BBMAP_BITS_MIN, \
1335 VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16))
Nick Piggindb64fe02008-10-18 20:27:03 -07001336
1337#define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE)
1338
1339struct vmap_block_queue {
1340 spinlock_t lock;
1341 struct list_head free;
Nick Piggindb64fe02008-10-18 20:27:03 -07001342};
1343
1344struct vmap_block {
1345 spinlock_t lock;
1346 struct vmap_area *va;
Nick Piggindb64fe02008-10-18 20:27:03 -07001347 unsigned long free, dirty;
Roman Pen7d61bfe2015-04-15 16:13:55 -07001348 unsigned long dirty_min, dirty_max; /*< dirty range */
Nick Pigginde560422010-02-01 22:24:18 +11001349 struct list_head free_list;
1350 struct rcu_head rcu_head;
Nick Piggin02b709d2010-02-01 22:25:57 +11001351 struct list_head purge;
Nick Piggindb64fe02008-10-18 20:27:03 -07001352};
1353
1354/* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
1355static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
1356
1357/*
1358 * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block
1359 * in the free path. Could get rid of this if we change the API to return a
1360 * "cookie" from alloc, to be passed to free. But no big deal yet.
1361 */
1362static DEFINE_SPINLOCK(vmap_block_tree_lock);
1363static RADIX_TREE(vmap_block_tree, GFP_ATOMIC);
1364
1365/*
1366 * We should probably have a fallback mechanism to allocate virtual memory
1367 * out of partially filled vmap blocks. However vmap block sizing should be
1368 * fairly reasonable according to the vmalloc size, so it shouldn't be a
1369 * big problem.
1370 */
1371
1372static unsigned long addr_to_vb_idx(unsigned long addr)
1373{
1374 addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
1375 addr /= VMAP_BLOCK_SIZE;
1376 return addr;
1377}
1378
Roman Pencf725ce2015-04-15 16:13:52 -07001379static void *vmap_block_vaddr(unsigned long va_start, unsigned long pages_off)
1380{
1381 unsigned long addr;
1382
1383 addr = va_start + (pages_off << PAGE_SHIFT);
1384 BUG_ON(addr_to_vb_idx(addr) != addr_to_vb_idx(va_start));
1385 return (void *)addr;
1386}
1387
1388/**
1389 * new_vmap_block - allocates new vmap_block and occupies 2^order pages in this
1390 * block. Of course pages number can't exceed VMAP_BBMAP_BITS
1391 * @order: how many 2^order pages should be occupied in newly allocated block
1392 * @gfp_mask: flags for the page level allocator
1393 *
Mike Rapoporta862f682019-03-05 15:48:42 -08001394 * Return: virtual address in a newly allocated block or ERR_PTR(-errno)
Roman Pencf725ce2015-04-15 16:13:52 -07001395 */
1396static void *new_vmap_block(unsigned int order, gfp_t gfp_mask)
Nick Piggindb64fe02008-10-18 20:27:03 -07001397{
1398 struct vmap_block_queue *vbq;
1399 struct vmap_block *vb;
1400 struct vmap_area *va;
1401 unsigned long vb_idx;
1402 int node, err;
Roman Pencf725ce2015-04-15 16:13:52 -07001403 void *vaddr;
Nick Piggindb64fe02008-10-18 20:27:03 -07001404
1405 node = numa_node_id();
1406
1407 vb = kmalloc_node(sizeof(struct vmap_block),
1408 gfp_mask & GFP_RECLAIM_MASK, node);
1409 if (unlikely(!vb))
1410 return ERR_PTR(-ENOMEM);
1411
1412 va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
1413 VMALLOC_START, VMALLOC_END,
1414 node, gfp_mask);
Tobias Klauserddf9c6d42011-01-13 15:46:15 -08001415 if (IS_ERR(va)) {
Nick Piggindb64fe02008-10-18 20:27:03 -07001416 kfree(vb);
Julia Lawalle7d86342010-08-09 17:18:28 -07001417 return ERR_CAST(va);
Nick Piggindb64fe02008-10-18 20:27:03 -07001418 }
1419
1420 err = radix_tree_preload(gfp_mask);
1421 if (unlikely(err)) {
1422 kfree(vb);
1423 free_vmap_area(va);
1424 return ERR_PTR(err);
1425 }
1426
Roman Pencf725ce2015-04-15 16:13:52 -07001427 vaddr = vmap_block_vaddr(va->va_start, 0);
Nick Piggindb64fe02008-10-18 20:27:03 -07001428 spin_lock_init(&vb->lock);
1429 vb->va = va;
Roman Pencf725ce2015-04-15 16:13:52 -07001430 /* At least something should be left free */
1431 BUG_ON(VMAP_BBMAP_BITS <= (1UL << order));
1432 vb->free = VMAP_BBMAP_BITS - (1UL << order);
Nick Piggindb64fe02008-10-18 20:27:03 -07001433 vb->dirty = 0;
Roman Pen7d61bfe2015-04-15 16:13:55 -07001434 vb->dirty_min = VMAP_BBMAP_BITS;
1435 vb->dirty_max = 0;
Nick Piggindb64fe02008-10-18 20:27:03 -07001436 INIT_LIST_HEAD(&vb->free_list);
Nick Piggindb64fe02008-10-18 20:27:03 -07001437
1438 vb_idx = addr_to_vb_idx(va->va_start);
1439 spin_lock(&vmap_block_tree_lock);
1440 err = radix_tree_insert(&vmap_block_tree, vb_idx, vb);
1441 spin_unlock(&vmap_block_tree_lock);
1442 BUG_ON(err);
1443 radix_tree_preload_end();
1444
1445 vbq = &get_cpu_var(vmap_block_queue);
Nick Piggindb64fe02008-10-18 20:27:03 -07001446 spin_lock(&vbq->lock);
Roman Pen68ac5462015-04-15 16:13:48 -07001447 list_add_tail_rcu(&vb->free_list, &vbq->free);
Nick Piggindb64fe02008-10-18 20:27:03 -07001448 spin_unlock(&vbq->lock);
Tejun Heo3f04ba82009-10-29 22:34:12 +09001449 put_cpu_var(vmap_block_queue);
Nick Piggindb64fe02008-10-18 20:27:03 -07001450
Roman Pencf725ce2015-04-15 16:13:52 -07001451 return vaddr;
Nick Piggindb64fe02008-10-18 20:27:03 -07001452}
1453
Nick Piggindb64fe02008-10-18 20:27:03 -07001454static void free_vmap_block(struct vmap_block *vb)
1455{
1456 struct vmap_block *tmp;
1457 unsigned long vb_idx;
1458
Nick Piggindb64fe02008-10-18 20:27:03 -07001459 vb_idx = addr_to_vb_idx(vb->va->va_start);
1460 spin_lock(&vmap_block_tree_lock);
1461 tmp = radix_tree_delete(&vmap_block_tree, vb_idx);
1462 spin_unlock(&vmap_block_tree_lock);
1463 BUG_ON(tmp != vb);
1464
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -08001465 free_vmap_area_noflush(vb->va);
Lai Jiangshan22a3c7d2011-03-18 12:13:08 +08001466 kfree_rcu(vb, rcu_head);
Nick Piggindb64fe02008-10-18 20:27:03 -07001467}
1468
Nick Piggin02b709d2010-02-01 22:25:57 +11001469static void purge_fragmented_blocks(int cpu)
1470{
1471 LIST_HEAD(purge);
1472 struct vmap_block *vb;
1473 struct vmap_block *n_vb;
1474 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
1475
1476 rcu_read_lock();
1477 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
1478
1479 if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS))
1480 continue;
1481
1482 spin_lock(&vb->lock);
1483 if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) {
1484 vb->free = 0; /* prevent further allocs after releasing lock */
1485 vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */
Roman Pen7d61bfe2015-04-15 16:13:55 -07001486 vb->dirty_min = 0;
1487 vb->dirty_max = VMAP_BBMAP_BITS;
Nick Piggin02b709d2010-02-01 22:25:57 +11001488 spin_lock(&vbq->lock);
1489 list_del_rcu(&vb->free_list);
1490 spin_unlock(&vbq->lock);
1491 spin_unlock(&vb->lock);
1492 list_add_tail(&vb->purge, &purge);
1493 } else
1494 spin_unlock(&vb->lock);
1495 }
1496 rcu_read_unlock();
1497
1498 list_for_each_entry_safe(vb, n_vb, &purge, purge) {
1499 list_del(&vb->purge);
1500 free_vmap_block(vb);
1501 }
1502}
1503
Nick Piggin02b709d2010-02-01 22:25:57 +11001504static void purge_fragmented_blocks_allcpus(void)
1505{
1506 int cpu;
1507
1508 for_each_possible_cpu(cpu)
1509 purge_fragmented_blocks(cpu);
1510}
1511
Nick Piggindb64fe02008-10-18 20:27:03 -07001512static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
1513{
1514 struct vmap_block_queue *vbq;
1515 struct vmap_block *vb;
Roman Pencf725ce2015-04-15 16:13:52 -07001516 void *vaddr = NULL;
Nick Piggindb64fe02008-10-18 20:27:03 -07001517 unsigned int order;
1518
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08001519 BUG_ON(offset_in_page(size));
Nick Piggindb64fe02008-10-18 20:27:03 -07001520 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
Jan Karaaa91c4d2012-07-31 16:41:37 -07001521 if (WARN_ON(size == 0)) {
1522 /*
1523 * Allocating 0 bytes isn't what caller wants since
1524 * get_order(0) returns funny result. Just warn and terminate
1525 * early.
1526 */
1527 return NULL;
1528 }
Nick Piggindb64fe02008-10-18 20:27:03 -07001529 order = get_order(size);
1530
Nick Piggindb64fe02008-10-18 20:27:03 -07001531 rcu_read_lock();
1532 vbq = &get_cpu_var(vmap_block_queue);
1533 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
Roman Pencf725ce2015-04-15 16:13:52 -07001534 unsigned long pages_off;
Nick Piggindb64fe02008-10-18 20:27:03 -07001535
1536 spin_lock(&vb->lock);
Roman Pencf725ce2015-04-15 16:13:52 -07001537 if (vb->free < (1UL << order)) {
1538 spin_unlock(&vb->lock);
1539 continue;
1540 }
Nick Piggin02b709d2010-02-01 22:25:57 +11001541
Roman Pencf725ce2015-04-15 16:13:52 -07001542 pages_off = VMAP_BBMAP_BITS - vb->free;
1543 vaddr = vmap_block_vaddr(vb->va->va_start, pages_off);
Nick Piggin02b709d2010-02-01 22:25:57 +11001544 vb->free -= 1UL << order;
1545 if (vb->free == 0) {
1546 spin_lock(&vbq->lock);
1547 list_del_rcu(&vb->free_list);
1548 spin_unlock(&vbq->lock);
Nick Piggindb64fe02008-10-18 20:27:03 -07001549 }
Roman Pencf725ce2015-04-15 16:13:52 -07001550
Nick Piggindb64fe02008-10-18 20:27:03 -07001551 spin_unlock(&vb->lock);
Nick Piggin02b709d2010-02-01 22:25:57 +11001552 break;
Nick Piggindb64fe02008-10-18 20:27:03 -07001553 }
Nick Piggin02b709d2010-02-01 22:25:57 +11001554
Tejun Heo3f04ba82009-10-29 22:34:12 +09001555 put_cpu_var(vmap_block_queue);
Nick Piggindb64fe02008-10-18 20:27:03 -07001556 rcu_read_unlock();
1557
Roman Pencf725ce2015-04-15 16:13:52 -07001558 /* Allocate new block if nothing was found */
1559 if (!vaddr)
1560 vaddr = new_vmap_block(order, gfp_mask);
Nick Piggindb64fe02008-10-18 20:27:03 -07001561
Roman Pencf725ce2015-04-15 16:13:52 -07001562 return vaddr;
Nick Piggindb64fe02008-10-18 20:27:03 -07001563}
1564
1565static void vb_free(const void *addr, unsigned long size)
1566{
1567 unsigned long offset;
1568 unsigned long vb_idx;
1569 unsigned int order;
1570 struct vmap_block *vb;
1571
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08001572 BUG_ON(offset_in_page(size));
Nick Piggindb64fe02008-10-18 20:27:03 -07001573 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
Nick Pigginb29acbd2008-12-01 13:13:47 -08001574
1575 flush_cache_vunmap((unsigned long)addr, (unsigned long)addr + size);
1576
Nick Piggindb64fe02008-10-18 20:27:03 -07001577 order = get_order(size);
1578
1579 offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1);
Roman Pen7d61bfe2015-04-15 16:13:55 -07001580 offset >>= PAGE_SHIFT;
Nick Piggindb64fe02008-10-18 20:27:03 -07001581
1582 vb_idx = addr_to_vb_idx((unsigned long)addr);
1583 rcu_read_lock();
1584 vb = radix_tree_lookup(&vmap_block_tree, vb_idx);
1585 rcu_read_unlock();
1586 BUG_ON(!vb);
1587
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -08001588 vunmap_page_range((unsigned long)addr, (unsigned long)addr + size);
1589
Chintan Pandya82a2e922018-06-07 17:06:46 -07001590 if (debug_pagealloc_enabled())
1591 flush_tlb_kernel_range((unsigned long)addr,
1592 (unsigned long)addr + size);
1593
Nick Piggindb64fe02008-10-18 20:27:03 -07001594 spin_lock(&vb->lock);
Roman Pen7d61bfe2015-04-15 16:13:55 -07001595
1596 /* Expand dirty range */
1597 vb->dirty_min = min(vb->dirty_min, offset);
1598 vb->dirty_max = max(vb->dirty_max, offset + (1UL << order));
MinChan Kimd0868172009-03-31 15:19:26 -07001599
Nick Piggindb64fe02008-10-18 20:27:03 -07001600 vb->dirty += 1UL << order;
1601 if (vb->dirty == VMAP_BBMAP_BITS) {
Nick Pigginde560422010-02-01 22:24:18 +11001602 BUG_ON(vb->free);
Nick Piggindb64fe02008-10-18 20:27:03 -07001603 spin_unlock(&vb->lock);
1604 free_vmap_block(vb);
1605 } else
1606 spin_unlock(&vb->lock);
1607}
1608
Rick Edgecombe868b1042019-04-25 17:11:36 -07001609static void _vm_unmap_aliases(unsigned long start, unsigned long end, int flush)
Nick Piggindb64fe02008-10-18 20:27:03 -07001610{
Nick Piggindb64fe02008-10-18 20:27:03 -07001611 int cpu;
Nick Piggindb64fe02008-10-18 20:27:03 -07001612
Jeremy Fitzhardinge9b463332008-10-28 19:22:34 +11001613 if (unlikely(!vmap_initialized))
1614 return;
1615
Christoph Hellwig5803ed22016-12-12 16:44:20 -08001616 might_sleep();
1617
Nick Piggindb64fe02008-10-18 20:27:03 -07001618 for_each_possible_cpu(cpu) {
1619 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
1620 struct vmap_block *vb;
1621
1622 rcu_read_lock();
1623 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
Nick Piggindb64fe02008-10-18 20:27:03 -07001624 spin_lock(&vb->lock);
Roman Pen7d61bfe2015-04-15 16:13:55 -07001625 if (vb->dirty) {
1626 unsigned long va_start = vb->va->va_start;
Nick Piggindb64fe02008-10-18 20:27:03 -07001627 unsigned long s, e;
Joonsoo Kimb136be5e2013-09-11 14:21:40 -07001628
Roman Pen7d61bfe2015-04-15 16:13:55 -07001629 s = va_start + (vb->dirty_min << PAGE_SHIFT);
1630 e = va_start + (vb->dirty_max << PAGE_SHIFT);
Nick Piggindb64fe02008-10-18 20:27:03 -07001631
Roman Pen7d61bfe2015-04-15 16:13:55 -07001632 start = min(s, start);
1633 end = max(e, end);
1634
Nick Piggindb64fe02008-10-18 20:27:03 -07001635 flush = 1;
Nick Piggindb64fe02008-10-18 20:27:03 -07001636 }
1637 spin_unlock(&vb->lock);
1638 }
1639 rcu_read_unlock();
1640 }
1641
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001642 mutex_lock(&vmap_purge_lock);
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001643 purge_fragmented_blocks_allcpus();
1644 if (!__purge_vmap_area_lazy(start, end) && flush)
1645 flush_tlb_kernel_range(start, end);
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001646 mutex_unlock(&vmap_purge_lock);
Nick Piggindb64fe02008-10-18 20:27:03 -07001647}
Rick Edgecombe868b1042019-04-25 17:11:36 -07001648
1649/**
1650 * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
1651 *
1652 * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
1653 * to amortize TLB flushing overheads. What this means is that any page you
1654 * have now, may, in a former life, have been mapped into kernel virtual
1655 * address by the vmap layer and so there might be some CPUs with TLB entries
1656 * still referencing that page (additional to the regular 1:1 kernel mapping).
1657 *
1658 * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
1659 * be sure that none of the pages we have control over will have any aliases
1660 * from the vmap layer.
1661 */
1662void vm_unmap_aliases(void)
1663{
1664 unsigned long start = ULONG_MAX, end = 0;
1665 int flush = 0;
1666
1667 _vm_unmap_aliases(start, end, flush);
1668}
Nick Piggindb64fe02008-10-18 20:27:03 -07001669EXPORT_SYMBOL_GPL(vm_unmap_aliases);
1670
1671/**
1672 * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
1673 * @mem: the pointer returned by vm_map_ram
1674 * @count: the count passed to that vm_map_ram call (cannot unmap partial)
1675 */
1676void vm_unmap_ram(const void *mem, unsigned int count)
1677{
Guillermo Julián Moreno65ee03c2016-06-03 14:55:33 -07001678 unsigned long size = (unsigned long)count << PAGE_SHIFT;
Nick Piggindb64fe02008-10-18 20:27:03 -07001679 unsigned long addr = (unsigned long)mem;
Christoph Hellwig9c3acf62016-12-12 16:44:04 -08001680 struct vmap_area *va;
Nick Piggindb64fe02008-10-18 20:27:03 -07001681
Christoph Hellwig5803ed22016-12-12 16:44:20 -08001682 might_sleep();
Nick Piggindb64fe02008-10-18 20:27:03 -07001683 BUG_ON(!addr);
1684 BUG_ON(addr < VMALLOC_START);
1685 BUG_ON(addr > VMALLOC_END);
Shawn Lina1c0b1a2016-03-17 14:20:37 -07001686 BUG_ON(!PAGE_ALIGNED(addr));
Nick Piggindb64fe02008-10-18 20:27:03 -07001687
Christoph Hellwig9c3acf62016-12-12 16:44:04 -08001688 if (likely(count <= VMAP_MAX_ALLOC)) {
Chintan Pandya05e3ff92018-06-07 17:06:53 -07001689 debug_check_no_locks_freed(mem, size);
Nick Piggindb64fe02008-10-18 20:27:03 -07001690 vb_free(mem, size);
Christoph Hellwig9c3acf62016-12-12 16:44:04 -08001691 return;
1692 }
1693
1694 va = find_vmap_area(addr);
1695 BUG_ON(!va);
Chintan Pandya05e3ff92018-06-07 17:06:53 -07001696 debug_check_no_locks_freed((void *)va->va_start,
1697 (va->va_end - va->va_start));
Christoph Hellwig9c3acf62016-12-12 16:44:04 -08001698 free_unmap_vmap_area(va);
Nick Piggindb64fe02008-10-18 20:27:03 -07001699}
1700EXPORT_SYMBOL(vm_unmap_ram);
1701
1702/**
1703 * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
1704 * @pages: an array of pointers to the pages to be mapped
1705 * @count: number of pages
1706 * @node: prefer to allocate data structures on this node
1707 * @prot: memory protection to use. PAGE_KERNEL for regular RAM
Randy Dunlape99c97a2008-10-29 14:01:09 -07001708 *
Gioh Kim36437632014-04-07 15:37:37 -07001709 * If you use this function for less than VMAP_MAX_ALLOC pages, it could be
1710 * faster than vmap so it's good. But if you mix long-life and short-life
1711 * objects with vm_map_ram(), it could consume lots of address space through
1712 * fragmentation (especially on a 32bit machine). You could see failures in
1713 * the end. Please use this function for short-lived objects.
1714 *
Randy Dunlape99c97a2008-10-29 14:01:09 -07001715 * Returns: a pointer to the address that has been mapped, or %NULL on failure
Nick Piggindb64fe02008-10-18 20:27:03 -07001716 */
1717void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
1718{
Guillermo Julián Moreno65ee03c2016-06-03 14:55:33 -07001719 unsigned long size = (unsigned long)count << PAGE_SHIFT;
Nick Piggindb64fe02008-10-18 20:27:03 -07001720 unsigned long addr;
1721 void *mem;
1722
1723 if (likely(count <= VMAP_MAX_ALLOC)) {
1724 mem = vb_alloc(size, GFP_KERNEL);
1725 if (IS_ERR(mem))
1726 return NULL;
1727 addr = (unsigned long)mem;
1728 } else {
1729 struct vmap_area *va;
1730 va = alloc_vmap_area(size, PAGE_SIZE,
1731 VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
1732 if (IS_ERR(va))
1733 return NULL;
1734
1735 addr = va->va_start;
1736 mem = (void *)addr;
1737 }
1738 if (vmap_page_range(addr, addr + size, prot, pages) < 0) {
1739 vm_unmap_ram(mem, count);
1740 return NULL;
1741 }
1742 return mem;
1743}
1744EXPORT_SYMBOL(vm_map_ram);
1745
Joonsoo Kim4341fa42013-04-29 15:07:39 -07001746static struct vm_struct *vmlist __initdata;
Mike Rapoport92eac162019-03-05 15:48:36 -08001747
Tejun Heof0aa6612009-02-20 16:29:08 +09001748/**
Nicolas Pitrebe9b7332011-08-25 00:24:21 -04001749 * vm_area_add_early - add vmap area early during boot
1750 * @vm: vm_struct to add
1751 *
1752 * This function is used to add fixed kernel vm area to vmlist before
1753 * vmalloc_init() is called. @vm->addr, @vm->size, and @vm->flags
1754 * should contain proper values and the other fields should be zero.
1755 *
1756 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1757 */
1758void __init vm_area_add_early(struct vm_struct *vm)
1759{
1760 struct vm_struct *tmp, **p;
1761
1762 BUG_ON(vmap_initialized);
1763 for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
1764 if (tmp->addr >= vm->addr) {
1765 BUG_ON(tmp->addr < vm->addr + vm->size);
1766 break;
1767 } else
1768 BUG_ON(tmp->addr + tmp->size > vm->addr);
1769 }
1770 vm->next = *p;
1771 *p = vm;
1772}
1773
1774/**
Tejun Heof0aa6612009-02-20 16:29:08 +09001775 * vm_area_register_early - register vmap area early during boot
1776 * @vm: vm_struct to register
Tejun Heoc0c0a292009-02-24 11:57:21 +09001777 * @align: requested alignment
Tejun Heof0aa6612009-02-20 16:29:08 +09001778 *
1779 * This function is used to register kernel vm area before
1780 * vmalloc_init() is called. @vm->size and @vm->flags should contain
1781 * proper values on entry and other fields should be zero. On return,
1782 * vm->addr contains the allocated address.
1783 *
1784 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1785 */
Tejun Heoc0c0a292009-02-24 11:57:21 +09001786void __init vm_area_register_early(struct vm_struct *vm, size_t align)
Tejun Heof0aa6612009-02-20 16:29:08 +09001787{
1788 static size_t vm_init_off __initdata;
Tejun Heoc0c0a292009-02-24 11:57:21 +09001789 unsigned long addr;
Tejun Heof0aa6612009-02-20 16:29:08 +09001790
Tejun Heoc0c0a292009-02-24 11:57:21 +09001791 addr = ALIGN(VMALLOC_START + vm_init_off, align);
1792 vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START;
1793
1794 vm->addr = (void *)addr;
Tejun Heof0aa6612009-02-20 16:29:08 +09001795
Nicolas Pitrebe9b7332011-08-25 00:24:21 -04001796 vm_area_add_early(vm);
Tejun Heof0aa6612009-02-20 16:29:08 +09001797}
1798
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001799static void vmap_init_free_space(void)
1800{
1801 unsigned long vmap_start = 1;
1802 const unsigned long vmap_end = ULONG_MAX;
1803 struct vmap_area *busy, *free;
1804
1805 /*
1806 * B F B B B F
1807 * -|-----|.....|-----|-----|-----|.....|-
1808 * | The KVA space |
1809 * |<--------------------------------->|
1810 */
1811 list_for_each_entry(busy, &vmap_area_list, list) {
1812 if (busy->va_start - vmap_start > 0) {
1813 free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
1814 if (!WARN_ON_ONCE(!free)) {
1815 free->va_start = vmap_start;
1816 free->va_end = busy->va_start;
1817
1818 insert_vmap_area_augment(free, NULL,
1819 &free_vmap_area_root,
1820 &free_vmap_area_list);
1821 }
1822 }
1823
1824 vmap_start = busy->va_end;
1825 }
1826
1827 if (vmap_end - vmap_start > 0) {
1828 free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
1829 if (!WARN_ON_ONCE(!free)) {
1830 free->va_start = vmap_start;
1831 free->va_end = vmap_end;
1832
1833 insert_vmap_area_augment(free, NULL,
1834 &free_vmap_area_root,
1835 &free_vmap_area_list);
1836 }
1837 }
1838}
1839
Nick Piggindb64fe02008-10-18 20:27:03 -07001840void __init vmalloc_init(void)
1841{
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08001842 struct vmap_area *va;
1843 struct vm_struct *tmp;
Nick Piggindb64fe02008-10-18 20:27:03 -07001844 int i;
1845
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001846 /*
1847 * Create the cache for vmap_area objects.
1848 */
1849 vmap_area_cachep = KMEM_CACHE(vmap_area, SLAB_PANIC);
1850
Nick Piggindb64fe02008-10-18 20:27:03 -07001851 for_each_possible_cpu(i) {
1852 struct vmap_block_queue *vbq;
Al Viro32fcfd42013-03-10 20:14:08 -04001853 struct vfree_deferred *p;
Nick Piggindb64fe02008-10-18 20:27:03 -07001854
1855 vbq = &per_cpu(vmap_block_queue, i);
1856 spin_lock_init(&vbq->lock);
1857 INIT_LIST_HEAD(&vbq->free);
Al Viro32fcfd42013-03-10 20:14:08 -04001858 p = &per_cpu(vfree_deferred, i);
1859 init_llist_head(&p->list);
1860 INIT_WORK(&p->wq, free_work);
Nick Piggindb64fe02008-10-18 20:27:03 -07001861 }
Jeremy Fitzhardinge9b463332008-10-28 19:22:34 +11001862
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08001863 /* Import existing vmlist entries. */
1864 for (tmp = vmlist; tmp; tmp = tmp->next) {
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001865 va = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
1866 if (WARN_ON_ONCE(!va))
1867 continue;
1868
KyongHodbda5912012-05-29 15:06:49 -07001869 va->flags = VM_VM_AREA;
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08001870 va->va_start = (unsigned long)tmp->addr;
1871 va->va_end = va->va_start + tmp->size;
KyongHodbda5912012-05-29 15:06:49 -07001872 va->vm = tmp;
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001873 insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08001874 }
Tejun Heoca23e402009-08-14 15:00:52 +09001875
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001876 /*
1877 * Now we can initialize a free vmap space.
1878 */
1879 vmap_init_free_space();
Jeremy Fitzhardinge9b463332008-10-28 19:22:34 +11001880 vmap_initialized = true;
Nick Piggindb64fe02008-10-18 20:27:03 -07001881}
1882
Tejun Heo8fc48982009-02-20 16:29:08 +09001883/**
1884 * map_kernel_range_noflush - map kernel VM area with the specified pages
1885 * @addr: start of the VM area to map
1886 * @size: size of the VM area to map
1887 * @prot: page protection flags to use
1888 * @pages: pages to map
1889 *
1890 * Map PFN_UP(@size) pages at @addr. The VM area @addr and @size
1891 * specify should have been allocated using get_vm_area() and its
1892 * friends.
1893 *
1894 * NOTE:
1895 * This function does NOT do any cache flushing. The caller is
1896 * responsible for calling flush_cache_vmap() on to-be-mapped areas
1897 * before calling this function.
1898 *
1899 * RETURNS:
1900 * The number of pages mapped on success, -errno on failure.
1901 */
1902int map_kernel_range_noflush(unsigned long addr, unsigned long size,
1903 pgprot_t prot, struct page **pages)
1904{
1905 return vmap_page_range_noflush(addr, addr + size, prot, pages);
1906}
1907
1908/**
1909 * unmap_kernel_range_noflush - unmap kernel VM area
1910 * @addr: start of the VM area to unmap
1911 * @size: size of the VM area to unmap
1912 *
1913 * Unmap PFN_UP(@size) pages at @addr. The VM area @addr and @size
1914 * specify should have been allocated using get_vm_area() and its
1915 * friends.
1916 *
1917 * NOTE:
1918 * This function does NOT do any cache flushing. The caller is
1919 * responsible for calling flush_cache_vunmap() on to-be-mapped areas
1920 * before calling this function and flush_tlb_kernel_range() after.
1921 */
1922void unmap_kernel_range_noflush(unsigned long addr, unsigned long size)
1923{
1924 vunmap_page_range(addr, addr + size);
1925}
Huang Ying81e88fd2011-01-12 14:44:55 +08001926EXPORT_SYMBOL_GPL(unmap_kernel_range_noflush);
Tejun Heo8fc48982009-02-20 16:29:08 +09001927
1928/**
1929 * unmap_kernel_range - unmap kernel VM area and flush cache and TLB
1930 * @addr: start of the VM area to unmap
1931 * @size: size of the VM area to unmap
1932 *
1933 * Similar to unmap_kernel_range_noflush() but flushes vcache before
1934 * the unmapping and tlb after.
1935 */
Nick Piggindb64fe02008-10-18 20:27:03 -07001936void unmap_kernel_range(unsigned long addr, unsigned long size)
1937{
1938 unsigned long end = addr + size;
Tejun Heof6fcba72009-02-20 15:38:48 -08001939
1940 flush_cache_vunmap(addr, end);
Nick Piggindb64fe02008-10-18 20:27:03 -07001941 vunmap_page_range(addr, end);
1942 flush_tlb_kernel_range(addr, end);
1943}
Minchan Kim93ef6d6c2014-06-04 16:11:09 -07001944EXPORT_SYMBOL_GPL(unmap_kernel_range);
Nick Piggindb64fe02008-10-18 20:27:03 -07001945
WANG Chaof6f8ed42014-08-06 16:06:58 -07001946int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page **pages)
Nick Piggindb64fe02008-10-18 20:27:03 -07001947{
1948 unsigned long addr = (unsigned long)area->addr;
Wanpeng Li762216a2013-09-11 14:22:42 -07001949 unsigned long end = addr + get_vm_area_size(area);
Nick Piggindb64fe02008-10-18 20:27:03 -07001950 int err;
1951
WANG Chaof6f8ed42014-08-06 16:06:58 -07001952 err = vmap_page_range(addr, end, prot, pages);
Nick Piggindb64fe02008-10-18 20:27:03 -07001953
WANG Chaof6f8ed42014-08-06 16:06:58 -07001954 return err > 0 ? 0 : err;
Nick Piggindb64fe02008-10-18 20:27:03 -07001955}
1956EXPORT_SYMBOL_GPL(map_vm_area);
1957
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001958static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
Marek Szyprowski5e6cafc2012-04-13 12:32:09 +02001959 unsigned long flags, const void *caller)
Tejun Heocf88c792009-08-14 15:00:52 +09001960{
Joonsoo Kimc69480a2013-04-29 15:07:30 -07001961 spin_lock(&vmap_area_lock);
Tejun Heocf88c792009-08-14 15:00:52 +09001962 vm->flags = flags;
1963 vm->addr = (void *)va->va_start;
1964 vm->size = va->va_end - va->va_start;
1965 vm->caller = caller;
Minchan Kimdb1aeca2012-01-10 15:08:39 -08001966 va->vm = vm;
Tejun Heocf88c792009-08-14 15:00:52 +09001967 va->flags |= VM_VM_AREA;
Joonsoo Kimc69480a2013-04-29 15:07:30 -07001968 spin_unlock(&vmap_area_lock);
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001969}
Tejun Heocf88c792009-08-14 15:00:52 +09001970
Zhang Yanfei20fc02b2013-07-08 15:59:58 -07001971static void clear_vm_uninitialized_flag(struct vm_struct *vm)
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001972{
Joonsoo Kimd4033af2013-04-29 15:07:35 -07001973 /*
Zhang Yanfei20fc02b2013-07-08 15:59:58 -07001974 * Before removing VM_UNINITIALIZED,
Joonsoo Kimd4033af2013-04-29 15:07:35 -07001975 * we should make sure that vm has proper values.
1976 * Pair with smp_rmb() in show_numa_info().
1977 */
1978 smp_wmb();
Zhang Yanfei20fc02b2013-07-08 15:59:58 -07001979 vm->flags &= ~VM_UNINITIALIZED;
Tejun Heocf88c792009-08-14 15:00:52 +09001980}
1981
Nick Piggindb64fe02008-10-18 20:27:03 -07001982static struct vm_struct *__get_vm_area_node(unsigned long size,
David Miller2dca6992009-09-21 12:22:34 -07001983 unsigned long align, unsigned long flags, unsigned long start,
Marek Szyprowski5e6cafc2012-04-13 12:32:09 +02001984 unsigned long end, int node, gfp_t gfp_mask, const void *caller)
Nick Piggindb64fe02008-10-18 20:27:03 -07001985{
Kautuk Consul00065262011-12-19 17:12:04 -08001986 struct vmap_area *va;
Nick Piggindb64fe02008-10-18 20:27:03 -07001987 struct vm_struct *area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Giridhar Pemmasani52fd24c2006-10-28 10:38:34 -07001989 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 size = PAGE_ALIGN(size);
OGAWA Hirofumi31be8302006-11-16 01:19:29 -08001991 if (unlikely(!size))
1992 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
zijun_hu252e5c62016-10-07 16:57:26 -07001994 if (flags & VM_IOREMAP)
1995 align = 1ul << clamp_t(int, get_count_order_long(size),
1996 PAGE_SHIFT, IOREMAP_MAX_ORDER);
1997
Tejun Heocf88c792009-08-14 15:00:52 +09001998 area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 if (unlikely(!area))
2000 return NULL;
2001
Andrey Ryabinin71394fe2015-02-13 14:40:03 -08002002 if (!(flags & VM_NO_GUARD))
2003 size += PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
Nick Piggindb64fe02008-10-18 20:27:03 -07002005 va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
2006 if (IS_ERR(va)) {
2007 kfree(area);
2008 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Zhang Yanfeid82b1d82013-07-03 15:04:47 -07002011 setup_vmalloc_vm(area, va, flags, caller);
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07002012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 return area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014}
2015
Christoph Lameter930fc452005-10-29 18:15:41 -07002016struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
2017 unsigned long start, unsigned long end)
2018{
David Rientjes00ef2d22013-02-22 16:35:36 -08002019 return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE,
2020 GFP_KERNEL, __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -07002021}
Rusty Russell5992b6d2007-07-19 01:49:21 -07002022EXPORT_SYMBOL_GPL(__get_vm_area);
Christoph Lameter930fc452005-10-29 18:15:41 -07002023
Benjamin Herrenschmidtc2968612009-02-18 14:48:12 -08002024struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
2025 unsigned long start, unsigned long end,
Marek Szyprowski5e6cafc2012-04-13 12:32:09 +02002026 const void *caller)
Benjamin Herrenschmidtc2968612009-02-18 14:48:12 -08002027{
David Rientjes00ef2d22013-02-22 16:35:36 -08002028 return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE,
2029 GFP_KERNEL, caller);
Benjamin Herrenschmidtc2968612009-02-18 14:48:12 -08002030}
2031
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002033 * get_vm_area - reserve a contiguous kernel virtual area
2034 * @size: size of the area
2035 * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002037 * Search an area of @size in the kernel virtual mapping area,
2038 * and reserved it for out purposes. Returns the area descriptor
2039 * on success or %NULL on failure.
Mike Rapoporta862f682019-03-05 15:48:42 -08002040 *
2041 * Return: the area descriptor on success or %NULL on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 */
2043struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
2044{
David Miller2dca6992009-09-21 12:22:34 -07002045 return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
David Rientjes00ef2d22013-02-22 16:35:36 -08002046 NUMA_NO_NODE, GFP_KERNEL,
2047 __builtin_return_address(0));
Christoph Lameter23016962008-04-28 02:12:42 -07002048}
2049
2050struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
Marek Szyprowski5e6cafc2012-04-13 12:32:09 +02002051 const void *caller)
Christoph Lameter23016962008-04-28 02:12:42 -07002052{
David Miller2dca6992009-09-21 12:22:34 -07002053 return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
David Rientjes00ef2d22013-02-22 16:35:36 -08002054 NUMA_NO_NODE, GFP_KERNEL, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055}
2056
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02002057/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002058 * find_vm_area - find a continuous kernel virtual area
2059 * @addr: base address
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02002060 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002061 * Search for the kernel VM area starting at @addr, and return it.
2062 * It is up to the caller to do all required locking to keep the returned
2063 * pointer valid.
Mike Rapoporta862f682019-03-05 15:48:42 -08002064 *
2065 * Return: pointer to the found area or %NULL on faulure
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02002066 */
2067struct vm_struct *find_vm_area(const void *addr)
Nick Piggin83342312006-06-23 02:03:20 -07002068{
Nick Piggindb64fe02008-10-18 20:27:03 -07002069 struct vmap_area *va;
Nick Piggin83342312006-06-23 02:03:20 -07002070
Nick Piggindb64fe02008-10-18 20:27:03 -07002071 va = find_vmap_area((unsigned long)addr);
2072 if (va && va->flags & VM_VM_AREA)
Minchan Kimdb1aeca2012-01-10 15:08:39 -08002073 return va->vm;
Nick Piggin83342312006-06-23 02:03:20 -07002074
Andi Kleen7856dfe2005-05-20 14:27:57 -07002075 return NULL;
Andi Kleen7856dfe2005-05-20 14:27:57 -07002076}
2077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002079 * remove_vm_area - find and remove a continuous kernel virtual area
2080 * @addr: base address
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002082 * Search for the kernel VM area starting at @addr, and remove it.
2083 * This function returns the found VM area, but using it is NOT safe
2084 * on SMP machines, except for its size or flags.
Mike Rapoporta862f682019-03-05 15:48:42 -08002085 *
2086 * Return: pointer to the found area or %NULL on faulure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -08002088struct vm_struct *remove_vm_area(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089{
Nick Piggindb64fe02008-10-18 20:27:03 -07002090 struct vmap_area *va;
2091
Christoph Hellwig5803ed22016-12-12 16:44:20 -08002092 might_sleep();
2093
Nick Piggindb64fe02008-10-18 20:27:03 -07002094 va = find_vmap_area((unsigned long)addr);
2095 if (va && va->flags & VM_VM_AREA) {
Minchan Kimdb1aeca2012-01-10 15:08:39 -08002096 struct vm_struct *vm = va->vm;
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07002097
Joonsoo Kimc69480a2013-04-29 15:07:30 -07002098 spin_lock(&vmap_area_lock);
2099 va->vm = NULL;
2100 va->flags &= ~VM_VM_AREA;
Yisheng Xie78c72742017-07-10 15:48:09 -07002101 va->flags |= VM_LAZY_FREE;
Joonsoo Kimc69480a2013-04-29 15:07:30 -07002102 spin_unlock(&vmap_area_lock);
2103
Andrey Ryabinina5af5aa2015-03-12 16:26:11 -07002104 kasan_free_shadow(vm);
KAMEZAWA Hiroyukidd32c272009-09-21 17:02:32 -07002105 free_unmap_vmap_area(va);
KAMEZAWA Hiroyukidd32c272009-09-21 17:02:32 -07002106
Nick Piggindb64fe02008-10-18 20:27:03 -07002107 return vm;
2108 }
2109 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110}
2111
Rick Edgecombe868b1042019-04-25 17:11:36 -07002112static inline void set_area_direct_map(const struct vm_struct *area,
2113 int (*set_direct_map)(struct page *page))
2114{
2115 int i;
2116
2117 for (i = 0; i < area->nr_pages; i++)
2118 if (page_address(area->pages[i]))
2119 set_direct_map(area->pages[i]);
2120}
2121
2122/* Handle removing and resetting vm mappings related to the vm_struct. */
2123static void vm_remove_mappings(struct vm_struct *area, int deallocate_pages)
2124{
2125 unsigned long addr = (unsigned long)area->addr;
2126 unsigned long start = ULONG_MAX, end = 0;
2127 int flush_reset = area->flags & VM_FLUSH_RESET_PERMS;
2128 int i;
2129
2130 /*
2131 * The below block can be removed when all architectures that have
2132 * direct map permissions also have set_direct_map_() implementations.
2133 * This is concerned with resetting the direct map any an vm alias with
2134 * execute permissions, without leaving a RW+X window.
2135 */
2136 if (flush_reset && !IS_ENABLED(CONFIG_ARCH_HAS_SET_DIRECT_MAP)) {
2137 set_memory_nx(addr, area->nr_pages);
2138 set_memory_rw(addr, area->nr_pages);
2139 }
2140
2141 remove_vm_area(area->addr);
2142
2143 /* If this is not VM_FLUSH_RESET_PERMS memory, no need for the below. */
2144 if (!flush_reset)
2145 return;
2146
2147 /*
2148 * If not deallocating pages, just do the flush of the VM area and
2149 * return.
2150 */
2151 if (!deallocate_pages) {
2152 vm_unmap_aliases();
2153 return;
2154 }
2155
2156 /*
2157 * If execution gets here, flush the vm mapping and reset the direct
2158 * map. Find the start and end range of the direct mappings to make sure
2159 * the vm_unmap_aliases() flush includes the direct map.
2160 */
2161 for (i = 0; i < area->nr_pages; i++) {
2162 if (page_address(area->pages[i])) {
2163 start = min(addr, start);
2164 end = max(addr, end);
2165 }
2166 }
2167
2168 /*
2169 * Set direct map to something invalid so that it won't be cached if
2170 * there are any accesses after the TLB flush, then flush the TLB and
2171 * reset the direct map permissions to the default.
2172 */
2173 set_area_direct_map(area, set_direct_map_invalid_noflush);
2174 _vm_unmap_aliases(start, end, 1);
2175 set_area_direct_map(area, set_direct_map_default_noflush);
2176}
2177
Christoph Lameterb3bdda02008-02-04 22:28:32 -08002178static void __vunmap(const void *addr, int deallocate_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
2180 struct vm_struct *area;
2181
2182 if (!addr)
2183 return;
2184
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002185 if (WARN(!PAGE_ALIGNED(addr), "Trying to vfree() bad address (%p)\n",
Dan Carpenterab15d9b2013-07-08 15:59:53 -07002186 addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Liviu Dudau6ade2032019-03-05 15:42:54 -08002189 area = find_vm_area(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 if (unlikely(!area)) {
Arjan van de Ven4c8573e2008-07-25 19:45:37 -07002191 WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 return;
2194 }
2195
Chintan Pandya05e3ff92018-06-07 17:06:53 -07002196 debug_check_no_locks_freed(area->addr, get_vm_area_size(area));
2197 debug_check_no_obj_freed(area->addr, get_vm_area_size(area));
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07002198
Rick Edgecombe868b1042019-04-25 17:11:36 -07002199 vm_remove_mappings(area, deallocate_pages);
2200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 if (deallocate_pages) {
2202 int i;
2203
2204 for (i = 0; i < area->nr_pages; i++) {
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08002205 struct page *page = area->pages[i];
2206
2207 BUG_ON(!page);
Vladimir Davydov49491482016-07-26 15:24:24 -07002208 __free_pages(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 }
2210
David Rientjes244d63e2016-01-14 15:19:35 -08002211 kvfree(area->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 }
2213
2214 kfree(area);
2215 return;
2216}
Andrey Ryabininbf22e372016-12-12 16:44:10 -08002217
2218static inline void __vfree_deferred(const void *addr)
2219{
2220 /*
2221 * Use raw_cpu_ptr() because this can be called from preemptible
2222 * context. Preemption is absolutely fine here, because the llist_add()
2223 * implementation is lockless, so it works even if we are adding to
2224 * nother cpu's list. schedule_work() should be fine with this too.
2225 */
2226 struct vfree_deferred *p = raw_cpu_ptr(&vfree_deferred);
2227
2228 if (llist_add((struct llist_node *)addr, &p->list))
2229 schedule_work(&p->wq);
2230}
2231
2232/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002233 * vfree_atomic - release memory allocated by vmalloc()
2234 * @addr: memory base address
Andrey Ryabininbf22e372016-12-12 16:44:10 -08002235 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002236 * This one is just like vfree() but can be called in any atomic context
2237 * except NMIs.
Andrey Ryabininbf22e372016-12-12 16:44:10 -08002238 */
2239void vfree_atomic(const void *addr)
2240{
2241 BUG_ON(in_nmi());
2242
2243 kmemleak_free(addr);
2244
2245 if (!addr)
2246 return;
2247 __vfree_deferred(addr);
2248}
2249
Roman Penyaevc67dc622019-03-05 15:43:24 -08002250static void __vfree(const void *addr)
2251{
2252 if (unlikely(in_interrupt()))
2253 __vfree_deferred(addr);
2254 else
2255 __vunmap(addr, 1);
2256}
2257
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002259 * vfree - release memory allocated by vmalloc()
2260 * @addr: memory base address
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002262 * Free the virtually continuous memory area starting at @addr, as
2263 * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
2264 * NULL, no operation is performed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002266 * Must not be called in NMI context (strictly speaking, only if we don't
2267 * have CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG, but making the calling
2268 * conventions for vfree() arch-depenedent would be a really bad idea)
Andrew Mortonc9fcee52013-05-07 16:18:18 -07002269 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002270 * May sleep if called *not* from interrupt context.
Andrey Ryabinin3ca4ea32018-10-26 15:07:03 -07002271 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002272 * NOTE: assumes that the object at @addr has a size >= sizeof(llist_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -08002274void vfree(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275{
Al Viro32fcfd42013-03-10 20:14:08 -04002276 BUG_ON(in_nmi());
Catalin Marinas89219d32009-06-11 13:23:19 +01002277
2278 kmemleak_free(addr);
2279
Andrey Ryabinina8dda162018-10-26 15:07:07 -07002280 might_sleep_if(!in_interrupt());
2281
Al Viro32fcfd42013-03-10 20:14:08 -04002282 if (!addr)
2283 return;
Roman Penyaevc67dc622019-03-05 15:43:24 -08002284
2285 __vfree(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287EXPORT_SYMBOL(vfree);
2288
2289/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002290 * vunmap - release virtual mapping obtained by vmap()
2291 * @addr: memory base address
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002293 * Free the virtually contiguous memory area starting at @addr,
2294 * which was created from the page array passed to vmap().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002296 * Must not be called in interrupt context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -08002298void vunmap(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299{
2300 BUG_ON(in_interrupt());
Peter Zijlstra34754b62009-02-25 16:04:03 +01002301 might_sleep();
Al Viro32fcfd42013-03-10 20:14:08 -04002302 if (addr)
2303 __vunmap(addr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305EXPORT_SYMBOL(vunmap);
2306
2307/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002308 * vmap - map an array of pages into virtually contiguous space
2309 * @pages: array of page pointers
2310 * @count: number of pages to map
2311 * @flags: vm_area->flags
2312 * @prot: page protection for the mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002314 * Maps @count pages from @pages into contiguous kernel virtual
2315 * space.
Mike Rapoporta862f682019-03-05 15:48:42 -08002316 *
2317 * Return: the address of the area or %NULL on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 */
2319void *vmap(struct page **pages, unsigned int count,
Mike Rapoport92eac162019-03-05 15:48:36 -08002320 unsigned long flags, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321{
2322 struct vm_struct *area;
Guillermo Julián Moreno65ee03c2016-06-03 14:55:33 -07002323 unsigned long size; /* In bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Peter Zijlstra34754b62009-02-25 16:04:03 +01002325 might_sleep();
2326
Arun KSca79b0c2018-12-28 00:34:29 -08002327 if (count > totalram_pages())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 return NULL;
2329
Guillermo Julián Moreno65ee03c2016-06-03 14:55:33 -07002330 size = (unsigned long)count << PAGE_SHIFT;
2331 area = get_vm_area_caller(size, flags, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 if (!area)
2333 return NULL;
Christoph Lameter23016962008-04-28 02:12:42 -07002334
WANG Chaof6f8ed42014-08-06 16:06:58 -07002335 if (map_vm_area(area, prot, pages)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 vunmap(area->addr);
2337 return NULL;
2338 }
2339
2340 return area->addr;
2341}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342EXPORT_SYMBOL(vmap);
2343
Michal Hocko8594a212017-05-12 15:46:41 -07002344static void *__vmalloc_node(unsigned long size, unsigned long align,
2345 gfp_t gfp_mask, pgprot_t prot,
2346 int node, const void *caller);
Adrian Bunke31d9eb2008-02-04 22:29:09 -08002347static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
Wanpeng Li3722e132013-11-12 15:07:29 -08002348 pgprot_t prot, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349{
2350 struct page **pages;
2351 unsigned int nr_pages, array_size, i;
David Rientjes930f0362014-08-06 16:06:28 -07002352 const gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO;
Laura Abbott704b8622017-08-18 15:16:27 -07002353 const gfp_t alloc_mask = gfp_mask | __GFP_NOWARN;
2354 const gfp_t highmem_mask = (gfp_mask & (GFP_DMA | GFP_DMA32)) ?
2355 0 :
2356 __GFP_HIGHMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
Wanpeng Li762216a2013-09-11 14:22:42 -07002358 nr_pages = get_vm_area_size(area) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 array_size = (nr_pages * sizeof(struct page *));
2360
2361 area->nr_pages = nr_pages;
2362 /* Please note that the recursion is strictly bounded. */
Jan Kiszka8757d5f2006-07-14 00:23:56 -07002363 if (array_size > PAGE_SIZE) {
Laura Abbott704b8622017-08-18 15:16:27 -07002364 pages = __vmalloc_node(array_size, 1, nested_gfp|highmem_mask,
Wanpeng Li3722e132013-11-12 15:07:29 -08002365 PAGE_KERNEL, node, area->caller);
Andrew Morton286e1ea2006-10-17 00:09:57 -07002366 } else {
Jan Beulich976d6df2009-12-14 17:58:39 -08002367 pages = kmalloc_node(array_size, nested_gfp, node);
Andrew Morton286e1ea2006-10-17 00:09:57 -07002368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 area->pages = pages;
2370 if (!area->pages) {
2371 remove_vm_area(area->addr);
2372 kfree(area);
2373 return NULL;
2374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
2376 for (i = 0; i < area->nr_pages; i++) {
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08002377 struct page *page;
2378
Jianguo Wu4b909512013-11-12 15:07:11 -08002379 if (node == NUMA_NO_NODE)
Laura Abbott704b8622017-08-18 15:16:27 -07002380 page = alloc_page(alloc_mask|highmem_mask);
Christoph Lameter930fc452005-10-29 18:15:41 -07002381 else
Laura Abbott704b8622017-08-18 15:16:27 -07002382 page = alloc_pages_node(node, alloc_mask|highmem_mask, 0);
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08002383
2384 if (unlikely(!page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 /* Successfully allocated i pages, free them in __vunmap() */
2386 area->nr_pages = i;
2387 goto fail;
2388 }
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08002389 area->pages[i] = page;
Laura Abbott704b8622017-08-18 15:16:27 -07002390 if (gfpflags_allow_blocking(gfp_mask|highmem_mask))
Eric Dumazet660654f2014-08-06 16:06:25 -07002391 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 }
2393
WANG Chaof6f8ed42014-08-06 16:06:58 -07002394 if (map_vm_area(area, prot, pages))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 goto fail;
2396 return area->addr;
2397
2398fail:
Michal Hockoa8e99252017-02-22 15:46:10 -08002399 warn_alloc(gfp_mask, NULL,
Michal Hocko7877cdc2016-10-07 17:01:55 -07002400 "vmalloc: allocation failure, allocated %ld of %ld bytes",
Dave Hansen22943ab2011-05-24 17:12:18 -07002401 (area->nr_pages*PAGE_SIZE), area->size);
Roman Penyaevc67dc622019-03-05 15:43:24 -08002402 __vfree(area->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 return NULL;
2404}
2405
David Rientjesd0a21262011-01-13 15:46:02 -08002406/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002407 * __vmalloc_node_range - allocate virtually contiguous memory
2408 * @size: allocation size
2409 * @align: desired alignment
2410 * @start: vm area range start
2411 * @end: vm area range end
2412 * @gfp_mask: flags for the page level allocator
2413 * @prot: protection mask for the allocated pages
2414 * @vm_flags: additional vm area flags (e.g. %VM_NO_GUARD)
2415 * @node: node to use for allocation or NUMA_NO_NODE
2416 * @caller: caller's return address
David Rientjesd0a21262011-01-13 15:46:02 -08002417 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002418 * Allocate enough pages to cover @size from the page level
2419 * allocator with @gfp_mask flags. Map them into contiguous
2420 * kernel virtual space, using a pagetable protection of @prot.
Mike Rapoporta862f682019-03-05 15:48:42 -08002421 *
2422 * Return: the address of the area or %NULL on failure
David Rientjesd0a21262011-01-13 15:46:02 -08002423 */
2424void *__vmalloc_node_range(unsigned long size, unsigned long align,
2425 unsigned long start, unsigned long end, gfp_t gfp_mask,
Andrey Ryabinincb9e3c22015-02-13 14:40:07 -08002426 pgprot_t prot, unsigned long vm_flags, int node,
2427 const void *caller)
Christoph Lameter930fc452005-10-29 18:15:41 -07002428{
David Rientjesd0a21262011-01-13 15:46:02 -08002429 struct vm_struct *area;
2430 void *addr;
2431 unsigned long real_size = size;
2432
2433 size = PAGE_ALIGN(size);
Arun KSca79b0c2018-12-28 00:34:29 -08002434 if (!size || (size >> PAGE_SHIFT) > totalram_pages())
Joe Perchesde7d2b52011-10-31 17:08:48 -07002435 goto fail;
David Rientjesd0a21262011-01-13 15:46:02 -08002436
Andrey Ryabinincb9e3c22015-02-13 14:40:07 -08002437 area = __get_vm_area_node(size, align, VM_ALLOC | VM_UNINITIALIZED |
2438 vm_flags, start, end, node, gfp_mask, caller);
David Rientjesd0a21262011-01-13 15:46:02 -08002439 if (!area)
Joe Perchesde7d2b52011-10-31 17:08:48 -07002440 goto fail;
David Rientjesd0a21262011-01-13 15:46:02 -08002441
Wanpeng Li3722e132013-11-12 15:07:29 -08002442 addr = __vmalloc_area_node(area, gfp_mask, prot, node);
Mel Gorman1368edf2011-12-08 14:34:30 -08002443 if (!addr)
Wanpeng Lib82225f32013-11-12 15:07:33 -08002444 return NULL;
Catalin Marinas89219d32009-06-11 13:23:19 +01002445
2446 /*
Zhang Yanfei20fc02b2013-07-08 15:59:58 -07002447 * In this function, newly allocated vm_struct has VM_UNINITIALIZED
2448 * flag. It means that vm_struct is not fully initialized.
Joonsoo Kim4341fa42013-04-29 15:07:39 -07002449 * Now, it is fully initialized, so remove this flag here.
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07002450 */
Zhang Yanfei20fc02b2013-07-08 15:59:58 -07002451 clear_vm_uninitialized_flag(area);
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07002452
Catalin Marinas94f4a162017-07-06 15:40:22 -07002453 kmemleak_vmalloc(area, size, gfp_mask);
Catalin Marinas89219d32009-06-11 13:23:19 +01002454
2455 return addr;
Joe Perchesde7d2b52011-10-31 17:08:48 -07002456
2457fail:
Michal Hockoa8e99252017-02-22 15:46:10 -08002458 warn_alloc(gfp_mask, NULL,
Michal Hocko7877cdc2016-10-07 17:01:55 -07002459 "vmalloc: allocation failure: %lu bytes", real_size);
Joe Perchesde7d2b52011-10-31 17:08:48 -07002460 return NULL;
Christoph Lameter930fc452005-10-29 18:15:41 -07002461}
2462
Uladzislau Rezki (Sony)153178e2019-03-05 15:43:30 -08002463/*
2464 * This is only for performance analysis of vmalloc and stress purpose.
2465 * It is required by vmalloc test module, therefore do not use it other
2466 * than that.
2467 */
2468#ifdef CONFIG_TEST_VMALLOC_MODULE
2469EXPORT_SYMBOL_GPL(__vmalloc_node_range);
2470#endif
2471
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002473 * __vmalloc_node - allocate virtually contiguous memory
2474 * @size: allocation size
2475 * @align: desired alignment
2476 * @gfp_mask: flags for the page level allocator
2477 * @prot: protection mask for the allocated pages
2478 * @node: node to use for allocation or NUMA_NO_NODE
2479 * @caller: caller's return address
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002481 * Allocate enough pages to cover @size from the page level
2482 * allocator with @gfp_mask flags. Map them into contiguous
2483 * kernel virtual space, using a pagetable protection of @prot.
Michal Hockoa7c3e902017-05-08 15:57:09 -07002484 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002485 * Reclaim modifiers in @gfp_mask - __GFP_NORETRY, __GFP_RETRY_MAYFAIL
2486 * and __GFP_NOFAIL are not supported
Michal Hockoa7c3e902017-05-08 15:57:09 -07002487 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002488 * Any use of gfp flags outside of GFP_KERNEL should be consulted
2489 * with mm people.
Mike Rapoporta862f682019-03-05 15:48:42 -08002490 *
2491 * Return: pointer to the allocated memory or %NULL on error
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 */
Michal Hocko8594a212017-05-12 15:46:41 -07002493static void *__vmalloc_node(unsigned long size, unsigned long align,
David Miller2dca6992009-09-21 12:22:34 -07002494 gfp_t gfp_mask, pgprot_t prot,
Marek Szyprowski5e6cafc2012-04-13 12:32:09 +02002495 int node, const void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496{
David Rientjesd0a21262011-01-13 15:46:02 -08002497 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
Andrey Ryabinincb9e3c22015-02-13 14:40:07 -08002498 gfp_mask, prot, 0, node, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499}
2500
Christoph Lameter930fc452005-10-29 18:15:41 -07002501void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
2502{
David Rientjes00ef2d22013-02-22 16:35:36 -08002503 return __vmalloc_node(size, 1, gfp_mask, prot, NUMA_NO_NODE,
Christoph Lameter23016962008-04-28 02:12:42 -07002504 __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -07002505}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506EXPORT_SYMBOL(__vmalloc);
2507
Michal Hocko8594a212017-05-12 15:46:41 -07002508static inline void *__vmalloc_node_flags(unsigned long size,
2509 int node, gfp_t flags)
2510{
2511 return __vmalloc_node(size, 1, flags, PAGE_KERNEL,
2512 node, __builtin_return_address(0));
2513}
2514
2515
2516void *__vmalloc_node_flags_caller(unsigned long size, int node, gfp_t flags,
2517 void *caller)
2518{
2519 return __vmalloc_node(size, 1, flags, PAGE_KERNEL, node, caller);
2520}
2521
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002523 * vmalloc - allocate virtually contiguous memory
2524 * @size: allocation size
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002526 * Allocate enough pages to cover @size from the page level
2527 * allocator and map them into contiguous kernel virtual space.
2528 *
2529 * For tight control over page level allocator and protection flags
2530 * use __vmalloc() instead.
Mike Rapoporta862f682019-03-05 15:48:42 -08002531 *
2532 * Return: pointer to the allocated memory or %NULL on error
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 */
2534void *vmalloc(unsigned long size)
2535{
David Rientjes00ef2d22013-02-22 16:35:36 -08002536 return __vmalloc_node_flags(size, NUMA_NO_NODE,
Michal Hocko19809c22017-05-08 15:57:44 -07002537 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539EXPORT_SYMBOL(vmalloc);
2540
Christoph Lameter930fc452005-10-29 18:15:41 -07002541/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002542 * vzalloc - allocate virtually contiguous memory with zero fill
2543 * @size: allocation size
Dave Younge1ca7782010-10-26 14:22:06 -07002544 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002545 * Allocate enough pages to cover @size from the page level
2546 * allocator and map them into contiguous kernel virtual space.
2547 * The memory allocated is set to zero.
2548 *
2549 * For tight control over page level allocator and protection flags
2550 * use __vmalloc() instead.
Mike Rapoporta862f682019-03-05 15:48:42 -08002551 *
2552 * Return: pointer to the allocated memory or %NULL on error
Dave Younge1ca7782010-10-26 14:22:06 -07002553 */
2554void *vzalloc(unsigned long size)
2555{
David Rientjes00ef2d22013-02-22 16:35:36 -08002556 return __vmalloc_node_flags(size, NUMA_NO_NODE,
Michal Hocko19809c22017-05-08 15:57:44 -07002557 GFP_KERNEL | __GFP_ZERO);
Dave Younge1ca7782010-10-26 14:22:06 -07002558}
2559EXPORT_SYMBOL(vzalloc);
2560
2561/**
Rolf Eike Beeread04082006-09-27 01:50:13 -07002562 * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
2563 * @size: allocation size
Nick Piggin83342312006-06-23 02:03:20 -07002564 *
Rolf Eike Beeread04082006-09-27 01:50:13 -07002565 * The resulting memory area is zeroed so it can be mapped to userspace
2566 * without leaking data.
Mike Rapoporta862f682019-03-05 15:48:42 -08002567 *
2568 * Return: pointer to the allocated memory or %NULL on error
Nick Piggin83342312006-06-23 02:03:20 -07002569 */
2570void *vmalloc_user(unsigned long size)
2571{
Roman Penyaevbc84c532019-03-05 15:43:27 -08002572 return __vmalloc_node_range(size, SHMLBA, VMALLOC_START, VMALLOC_END,
2573 GFP_KERNEL | __GFP_ZERO, PAGE_KERNEL,
2574 VM_USERMAP, NUMA_NO_NODE,
2575 __builtin_return_address(0));
Nick Piggin83342312006-06-23 02:03:20 -07002576}
2577EXPORT_SYMBOL(vmalloc_user);
2578
2579/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002580 * vmalloc_node - allocate memory on a specific node
2581 * @size: allocation size
2582 * @node: numa node
Christoph Lameter930fc452005-10-29 18:15:41 -07002583 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002584 * Allocate enough pages to cover @size from the page level
2585 * allocator and map them into contiguous kernel virtual space.
Christoph Lameter930fc452005-10-29 18:15:41 -07002586 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002587 * For tight control over page level allocator and protection flags
2588 * use __vmalloc() instead.
Mike Rapoporta862f682019-03-05 15:48:42 -08002589 *
2590 * Return: pointer to the allocated memory or %NULL on error
Christoph Lameter930fc452005-10-29 18:15:41 -07002591 */
2592void *vmalloc_node(unsigned long size, int node)
2593{
Michal Hocko19809c22017-05-08 15:57:44 -07002594 return __vmalloc_node(size, 1, GFP_KERNEL, PAGE_KERNEL,
Christoph Lameter23016962008-04-28 02:12:42 -07002595 node, __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -07002596}
2597EXPORT_SYMBOL(vmalloc_node);
2598
Dave Younge1ca7782010-10-26 14:22:06 -07002599/**
2600 * vzalloc_node - allocate memory on a specific node with zero fill
2601 * @size: allocation size
2602 * @node: numa node
2603 *
2604 * Allocate enough pages to cover @size from the page level
2605 * allocator and map them into contiguous kernel virtual space.
2606 * The memory allocated is set to zero.
2607 *
2608 * For tight control over page level allocator and protection flags
2609 * use __vmalloc_node() instead.
Mike Rapoporta862f682019-03-05 15:48:42 -08002610 *
2611 * Return: pointer to the allocated memory or %NULL on error
Dave Younge1ca7782010-10-26 14:22:06 -07002612 */
2613void *vzalloc_node(unsigned long size, int node)
2614{
2615 return __vmalloc_node_flags(size, node,
Michal Hocko19809c22017-05-08 15:57:44 -07002616 GFP_KERNEL | __GFP_ZERO);
Dave Younge1ca7782010-10-26 14:22:06 -07002617}
2618EXPORT_SYMBOL(vzalloc_node);
2619
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002621 * vmalloc_exec - allocate virtually contiguous, executable memory
2622 * @size: allocation size
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002624 * Kernel-internal function to allocate enough pages to cover @size
2625 * the page level allocator and map them into contiguous and
2626 * executable kernel virtual space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002628 * For tight control over page level allocator and protection flags
2629 * use __vmalloc() instead.
Mike Rapoporta862f682019-03-05 15:48:42 -08002630 *
2631 * Return: pointer to the allocated memory or %NULL on error
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633void *vmalloc_exec(unsigned long size)
2634{
Rick Edgecombe868b1042019-04-25 17:11:36 -07002635 return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END,
2636 GFP_KERNEL, PAGE_KERNEL_EXEC, VM_FLUSH_RESET_PERMS,
2637 NUMA_NO_NODE, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638}
2639
Andi Kleen0d08e0d2007-05-02 19:27:12 +02002640#if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
Michal Hocko698d0832018-02-21 14:46:01 -08002641#define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL)
Andi Kleen0d08e0d2007-05-02 19:27:12 +02002642#elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
Michal Hocko698d0832018-02-21 14:46:01 -08002643#define GFP_VMALLOC32 (GFP_DMA | GFP_KERNEL)
Andi Kleen0d08e0d2007-05-02 19:27:12 +02002644#else
Michal Hocko698d0832018-02-21 14:46:01 -08002645/*
2646 * 64b systems should always have either DMA or DMA32 zones. For others
2647 * GFP_DMA32 should do the right thing and use the normal zone.
2648 */
2649#define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
Andi Kleen0d08e0d2007-05-02 19:27:12 +02002650#endif
2651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002653 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
2654 * @size: allocation size
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002656 * Allocate enough 32bit PA addressable pages to cover @size from the
2657 * page level allocator and map them into contiguous kernel virtual space.
Mike Rapoporta862f682019-03-05 15:48:42 -08002658 *
2659 * Return: pointer to the allocated memory or %NULL on error
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 */
2661void *vmalloc_32(unsigned long size)
2662{
David Miller2dca6992009-09-21 12:22:34 -07002663 return __vmalloc_node(size, 1, GFP_VMALLOC32, PAGE_KERNEL,
David Rientjes00ef2d22013-02-22 16:35:36 -08002664 NUMA_NO_NODE, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666EXPORT_SYMBOL(vmalloc_32);
2667
Nick Piggin83342312006-06-23 02:03:20 -07002668/**
Rolf Eike Beeread04082006-09-27 01:50:13 -07002669 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
Mike Rapoport92eac162019-03-05 15:48:36 -08002670 * @size: allocation size
Rolf Eike Beeread04082006-09-27 01:50:13 -07002671 *
2672 * The resulting memory area is 32bit addressable and zeroed so it can be
2673 * mapped to userspace without leaking data.
Mike Rapoporta862f682019-03-05 15:48:42 -08002674 *
2675 * Return: pointer to the allocated memory or %NULL on error
Nick Piggin83342312006-06-23 02:03:20 -07002676 */
2677void *vmalloc_32_user(unsigned long size)
2678{
Roman Penyaevbc84c532019-03-05 15:43:27 -08002679 return __vmalloc_node_range(size, SHMLBA, VMALLOC_START, VMALLOC_END,
2680 GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
2681 VM_USERMAP, NUMA_NO_NODE,
2682 __builtin_return_address(0));
Nick Piggin83342312006-06-23 02:03:20 -07002683}
2684EXPORT_SYMBOL(vmalloc_32_user);
2685
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002686/*
2687 * small helper routine , copy contents to buf from addr.
2688 * If the page is not present, fill zero.
2689 */
2690
2691static int aligned_vread(char *buf, char *addr, unsigned long count)
2692{
2693 struct page *p;
2694 int copied = 0;
2695
2696 while (count) {
2697 unsigned long offset, length;
2698
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08002699 offset = offset_in_page(addr);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002700 length = PAGE_SIZE - offset;
2701 if (length > count)
2702 length = count;
2703 p = vmalloc_to_page(addr);
2704 /*
2705 * To do safe access to this _mapped_ area, we need
2706 * lock. But adding lock here means that we need to add
2707 * overhead of vmalloc()/vfree() calles for this _debug_
2708 * interface, rarely used. Instead of that, we'll use
2709 * kmap() and get small overhead in this access function.
2710 */
2711 if (p) {
2712 /*
2713 * we can expect USER0 is not used (see vread/vwrite's
2714 * function description)
2715 */
Cong Wang9b04c5f2011-11-25 23:14:39 +08002716 void *map = kmap_atomic(p);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002717 memcpy(buf, map + offset, length);
Cong Wang9b04c5f2011-11-25 23:14:39 +08002718 kunmap_atomic(map);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002719 } else
2720 memset(buf, 0, length);
2721
2722 addr += length;
2723 buf += length;
2724 copied += length;
2725 count -= length;
2726 }
2727 return copied;
2728}
2729
2730static int aligned_vwrite(char *buf, char *addr, unsigned long count)
2731{
2732 struct page *p;
2733 int copied = 0;
2734
2735 while (count) {
2736 unsigned long offset, length;
2737
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08002738 offset = offset_in_page(addr);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002739 length = PAGE_SIZE - offset;
2740 if (length > count)
2741 length = count;
2742 p = vmalloc_to_page(addr);
2743 /*
2744 * To do safe access to this _mapped_ area, we need
2745 * lock. But adding lock here means that we need to add
2746 * overhead of vmalloc()/vfree() calles for this _debug_
2747 * interface, rarely used. Instead of that, we'll use
2748 * kmap() and get small overhead in this access function.
2749 */
2750 if (p) {
2751 /*
2752 * we can expect USER0 is not used (see vread/vwrite's
2753 * function description)
2754 */
Cong Wang9b04c5f2011-11-25 23:14:39 +08002755 void *map = kmap_atomic(p);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002756 memcpy(map + offset, buf, length);
Cong Wang9b04c5f2011-11-25 23:14:39 +08002757 kunmap_atomic(map);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002758 }
2759 addr += length;
2760 buf += length;
2761 copied += length;
2762 count -= length;
2763 }
2764 return copied;
2765}
2766
2767/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002768 * vread() - read vmalloc area in a safe way.
2769 * @buf: buffer for reading data
2770 * @addr: vm address.
2771 * @count: number of bytes to be read.
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002772 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002773 * This function checks that addr is a valid vmalloc'ed area, and
2774 * copy data from that area to a given buffer. If the given memory range
2775 * of [addr...addr+count) includes some valid address, data is copied to
2776 * proper area of @buf. If there are memory holes, they'll be zero-filled.
2777 * IOREMAP area is treated as memory hole and no copy is done.
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002778 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002779 * If [addr...addr+count) doesn't includes any intersects with alive
2780 * vm_struct area, returns 0. @buf should be kernel's buffer.
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002781 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002782 * Note: In usual ops, vread() is never necessary because the caller
2783 * should know vmalloc() area is valid and can use memcpy().
2784 * This is for routines which have to access vmalloc area without
2785 * any informaion, as /dev/kmem.
Mike Rapoporta862f682019-03-05 15:48:42 -08002786 *
2787 * Return: number of bytes for which addr and buf should be increased
2788 * (same number as @count) or %0 if [addr...addr+count) doesn't
2789 * include any intersection with valid vmalloc area
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002790 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791long vread(char *buf, char *addr, unsigned long count)
2792{
Joonsoo Kime81ce852013-04-29 15:07:32 -07002793 struct vmap_area *va;
2794 struct vm_struct *vm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 char *vaddr, *buf_start = buf;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002796 unsigned long buflen = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 unsigned long n;
2798
2799 /* Don't allow overflow */
2800 if ((unsigned long) addr + count < count)
2801 count = -(unsigned long) addr;
2802
Joonsoo Kime81ce852013-04-29 15:07:32 -07002803 spin_lock(&vmap_area_lock);
2804 list_for_each_entry(va, &vmap_area_list, list) {
2805 if (!count)
2806 break;
2807
2808 if (!(va->flags & VM_VM_AREA))
2809 continue;
2810
2811 vm = va->vm;
2812 vaddr = (char *) vm->addr;
Wanpeng Li762216a2013-09-11 14:22:42 -07002813 if (addr >= vaddr + get_vm_area_size(vm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 continue;
2815 while (addr < vaddr) {
2816 if (count == 0)
2817 goto finished;
2818 *buf = '\0';
2819 buf++;
2820 addr++;
2821 count--;
2822 }
Wanpeng Li762216a2013-09-11 14:22:42 -07002823 n = vaddr + get_vm_area_size(vm) - addr;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002824 if (n > count)
2825 n = count;
Joonsoo Kime81ce852013-04-29 15:07:32 -07002826 if (!(vm->flags & VM_IOREMAP))
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002827 aligned_vread(buf, addr, n);
2828 else /* IOREMAP area is treated as memory hole */
2829 memset(buf, 0, n);
2830 buf += n;
2831 addr += n;
2832 count -= n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 }
2834finished:
Joonsoo Kime81ce852013-04-29 15:07:32 -07002835 spin_unlock(&vmap_area_lock);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002836
2837 if (buf == buf_start)
2838 return 0;
2839 /* zero-fill memory holes */
2840 if (buf != buf_start + buflen)
2841 memset(buf, 0, buflen - (buf - buf_start));
2842
2843 return buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844}
2845
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002846/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002847 * vwrite() - write vmalloc area in a safe way.
2848 * @buf: buffer for source data
2849 * @addr: vm address.
2850 * @count: number of bytes to be read.
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002851 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002852 * This function checks that addr is a valid vmalloc'ed area, and
2853 * copy data from a buffer to the given addr. If specified range of
2854 * [addr...addr+count) includes some valid address, data is copied from
2855 * proper area of @buf. If there are memory holes, no copy to hole.
2856 * IOREMAP area is treated as memory hole and no copy is done.
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002857 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002858 * If [addr...addr+count) doesn't includes any intersects with alive
2859 * vm_struct area, returns 0. @buf should be kernel's buffer.
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002860 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002861 * Note: In usual ops, vwrite() is never necessary because the caller
2862 * should know vmalloc() area is valid and can use memcpy().
2863 * This is for routines which have to access vmalloc area without
2864 * any informaion, as /dev/kmem.
Mike Rapoporta862f682019-03-05 15:48:42 -08002865 *
2866 * Return: number of bytes for which addr and buf should be
2867 * increased (same number as @count) or %0 if [addr...addr+count)
2868 * doesn't include any intersection with valid vmalloc area
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002869 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870long vwrite(char *buf, char *addr, unsigned long count)
2871{
Joonsoo Kime81ce852013-04-29 15:07:32 -07002872 struct vmap_area *va;
2873 struct vm_struct *vm;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002874 char *vaddr;
2875 unsigned long n, buflen;
2876 int copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
2878 /* Don't allow overflow */
2879 if ((unsigned long) addr + count < count)
2880 count = -(unsigned long) addr;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002881 buflen = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
Joonsoo Kime81ce852013-04-29 15:07:32 -07002883 spin_lock(&vmap_area_lock);
2884 list_for_each_entry(va, &vmap_area_list, list) {
2885 if (!count)
2886 break;
2887
2888 if (!(va->flags & VM_VM_AREA))
2889 continue;
2890
2891 vm = va->vm;
2892 vaddr = (char *) vm->addr;
Wanpeng Li762216a2013-09-11 14:22:42 -07002893 if (addr >= vaddr + get_vm_area_size(vm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 continue;
2895 while (addr < vaddr) {
2896 if (count == 0)
2897 goto finished;
2898 buf++;
2899 addr++;
2900 count--;
2901 }
Wanpeng Li762216a2013-09-11 14:22:42 -07002902 n = vaddr + get_vm_area_size(vm) - addr;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002903 if (n > count)
2904 n = count;
Joonsoo Kime81ce852013-04-29 15:07:32 -07002905 if (!(vm->flags & VM_IOREMAP)) {
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002906 aligned_vwrite(buf, addr, n);
2907 copied++;
2908 }
2909 buf += n;
2910 addr += n;
2911 count -= n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 }
2913finished:
Joonsoo Kime81ce852013-04-29 15:07:32 -07002914 spin_unlock(&vmap_area_lock);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002915 if (!copied)
2916 return 0;
2917 return buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918}
Nick Piggin83342312006-06-23 02:03:20 -07002919
2920/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002921 * remap_vmalloc_range_partial - map vmalloc pages to userspace
2922 * @vma: vma to cover
2923 * @uaddr: target user address to start at
2924 * @kaddr: virtual address of vmalloc kernel memory
2925 * @size: size of map area
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002926 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002927 * Returns: 0 for success, -Exxx on failure
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002928 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002929 * This function checks that @kaddr is a valid vmalloc'ed area,
2930 * and that it is big enough to cover the range starting at
2931 * @uaddr in @vma. Will return failure if that criteria isn't
2932 * met.
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002933 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002934 * Similar to remap_pfn_range() (see mm/memory.c)
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002935 */
2936int remap_vmalloc_range_partial(struct vm_area_struct *vma, unsigned long uaddr,
2937 void *kaddr, unsigned long size)
2938{
2939 struct vm_struct *area;
2940
2941 size = PAGE_ALIGN(size);
2942
2943 if (!PAGE_ALIGNED(uaddr) || !PAGE_ALIGNED(kaddr))
2944 return -EINVAL;
2945
2946 area = find_vm_area(kaddr);
2947 if (!area)
2948 return -EINVAL;
2949
2950 if (!(area->flags & VM_USERMAP))
2951 return -EINVAL;
2952
Roman Penyaev401592d2019-03-05 15:43:20 -08002953 if (kaddr + size > area->addr + get_vm_area_size(area))
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002954 return -EINVAL;
2955
2956 do {
2957 struct page *page = vmalloc_to_page(kaddr);
2958 int ret;
2959
2960 ret = vm_insert_page(vma, uaddr, page);
2961 if (ret)
2962 return ret;
2963
2964 uaddr += PAGE_SIZE;
2965 kaddr += PAGE_SIZE;
2966 size -= PAGE_SIZE;
2967 } while (size > 0);
2968
2969 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
2970
2971 return 0;
2972}
2973EXPORT_SYMBOL(remap_vmalloc_range_partial);
2974
2975/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002976 * remap_vmalloc_range - map vmalloc pages to userspace
2977 * @vma: vma to cover (map full range of vma)
2978 * @addr: vmalloc memory
2979 * @pgoff: number of pages into addr before first page to map
Randy Dunlap76824862008-03-19 17:00:40 -07002980 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002981 * Returns: 0 for success, -Exxx on failure
Nick Piggin83342312006-06-23 02:03:20 -07002982 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002983 * This function checks that addr is a valid vmalloc'ed area, and
2984 * that it is big enough to cover the vma. Will return failure if
2985 * that criteria isn't met.
Nick Piggin83342312006-06-23 02:03:20 -07002986 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002987 * Similar to remap_pfn_range() (see mm/memory.c)
Nick Piggin83342312006-06-23 02:03:20 -07002988 */
2989int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
2990 unsigned long pgoff)
2991{
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002992 return remap_vmalloc_range_partial(vma, vma->vm_start,
2993 addr + (pgoff << PAGE_SHIFT),
2994 vma->vm_end - vma->vm_start);
Nick Piggin83342312006-06-23 02:03:20 -07002995}
2996EXPORT_SYMBOL(remap_vmalloc_range);
2997
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -07002998/*
2999 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
3000 * have one.
3001 */
Gideon Israel Dsouza3b321232014-04-07 15:37:26 -07003002void __weak vmalloc_sync_all(void)
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -07003003{
3004}
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07003005
3006
Martin Schwidefsky2f569af2008-02-08 04:22:04 -08003007static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07003008{
David Vrabelcd129092011-09-29 16:53:32 +01003009 pte_t ***p = data;
3010
3011 if (p) {
3012 *(*p) = pte;
3013 (*p)++;
3014 }
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07003015 return 0;
3016}
3017
3018/**
Mike Rapoport92eac162019-03-05 15:48:36 -08003019 * alloc_vm_area - allocate a range of kernel address space
3020 * @size: size of the area
3021 * @ptes: returns the PTEs for the address space
Randy Dunlap76824862008-03-19 17:00:40 -07003022 *
Mike Rapoport92eac162019-03-05 15:48:36 -08003023 * Returns: NULL on failure, vm_struct on success
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07003024 *
Mike Rapoport92eac162019-03-05 15:48:36 -08003025 * This function reserves a range of kernel address space, and
3026 * allocates pagetables to map that range. No actual mappings
3027 * are created.
David Vrabelcd129092011-09-29 16:53:32 +01003028 *
Mike Rapoport92eac162019-03-05 15:48:36 -08003029 * If @ptes is non-NULL, pointers to the PTEs (in init_mm)
3030 * allocated for the VM area are returned.
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07003031 */
David Vrabelcd129092011-09-29 16:53:32 +01003032struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07003033{
3034 struct vm_struct *area;
3035
Christoph Lameter23016962008-04-28 02:12:42 -07003036 area = get_vm_area_caller(size, VM_IOREMAP,
3037 __builtin_return_address(0));
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07003038 if (area == NULL)
3039 return NULL;
3040
3041 /*
3042 * This ensures that page tables are constructed for this region
3043 * of kernel virtual address space and mapped into init_mm.
3044 */
3045 if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
David Vrabelcd129092011-09-29 16:53:32 +01003046 size, f, ptes ? &ptes : NULL)) {
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07003047 free_vm_area(area);
3048 return NULL;
3049 }
3050
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07003051 return area;
3052}
3053EXPORT_SYMBOL_GPL(alloc_vm_area);
3054
3055void free_vm_area(struct vm_struct *area)
3056{
3057 struct vm_struct *ret;
3058 ret = remove_vm_area(area->addr);
3059 BUG_ON(ret != area);
3060 kfree(area);
3061}
3062EXPORT_SYMBOL_GPL(free_vm_area);
Christoph Lametera10aa572008-04-28 02:12:40 -07003063
Tejun Heo4f8b02b2010-09-03 18:22:47 +02003064#ifdef CONFIG_SMP
Tejun Heoca23e402009-08-14 15:00:52 +09003065static struct vmap_area *node_to_va(struct rb_node *n)
3066{
Geliang Tang4583e772017-02-22 15:41:54 -08003067 return rb_entry_safe(n, struct vmap_area, rb_node);
Tejun Heoca23e402009-08-14 15:00:52 +09003068}
3069
3070/**
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003071 * pvm_find_va_enclose_addr - find the vmap_area @addr belongs to
3072 * @addr: target address
Tejun Heoca23e402009-08-14 15:00:52 +09003073 *
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003074 * Returns: vmap_area if it is found. If there is no such area
3075 * the first highest(reverse order) vmap_area is returned
3076 * i.e. va->va_start < addr && va->va_end < addr or NULL
3077 * if there are no any areas before @addr.
Tejun Heoca23e402009-08-14 15:00:52 +09003078 */
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003079static struct vmap_area *
3080pvm_find_va_enclose_addr(unsigned long addr)
Tejun Heoca23e402009-08-14 15:00:52 +09003081{
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003082 struct vmap_area *va, *tmp;
3083 struct rb_node *n;
3084
3085 n = free_vmap_area_root.rb_node;
3086 va = NULL;
Tejun Heoca23e402009-08-14 15:00:52 +09003087
3088 while (n) {
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003089 tmp = rb_entry(n, struct vmap_area, rb_node);
3090 if (tmp->va_start <= addr) {
3091 va = tmp;
3092 if (tmp->va_end >= addr)
3093 break;
3094
Tejun Heoca23e402009-08-14 15:00:52 +09003095 n = n->rb_right;
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003096 } else {
3097 n = n->rb_left;
3098 }
Tejun Heoca23e402009-08-14 15:00:52 +09003099 }
3100
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003101 return va;
Tejun Heoca23e402009-08-14 15:00:52 +09003102}
3103
3104/**
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003105 * pvm_determine_end_from_reverse - find the highest aligned address
3106 * of free block below VMALLOC_END
3107 * @va:
3108 * in - the VA we start the search(reverse order);
3109 * out - the VA with the highest aligned end address.
Tejun Heoca23e402009-08-14 15:00:52 +09003110 *
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003111 * Returns: determined end address within vmap_area
Tejun Heoca23e402009-08-14 15:00:52 +09003112 */
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003113static unsigned long
3114pvm_determine_end_from_reverse(struct vmap_area **va, unsigned long align)
Tejun Heoca23e402009-08-14 15:00:52 +09003115{
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003116 unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
Tejun Heoca23e402009-08-14 15:00:52 +09003117 unsigned long addr;
3118
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003119 if (likely(*va)) {
3120 list_for_each_entry_from_reverse((*va),
3121 &free_vmap_area_list, list) {
3122 addr = min((*va)->va_end & ~(align - 1), vmalloc_end);
3123 if ((*va)->va_start < addr)
3124 return addr;
3125 }
Tejun Heoca23e402009-08-14 15:00:52 +09003126 }
3127
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003128 return 0;
Tejun Heoca23e402009-08-14 15:00:52 +09003129}
3130
3131/**
3132 * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator
3133 * @offsets: array containing offset of each area
3134 * @sizes: array containing size of each area
3135 * @nr_vms: the number of areas to allocate
3136 * @align: alignment, all entries in @offsets and @sizes must be aligned to this
Tejun Heoca23e402009-08-14 15:00:52 +09003137 *
3138 * Returns: kmalloc'd vm_struct pointer array pointing to allocated
3139 * vm_structs on success, %NULL on failure
3140 *
3141 * Percpu allocator wants to use congruent vm areas so that it can
3142 * maintain the offsets among percpu areas. This function allocates
David Rientjesec3f64f2011-01-13 15:46:01 -08003143 * congruent vmalloc areas for it with GFP_KERNEL. These areas tend to
3144 * be scattered pretty far, distance between two areas easily going up
3145 * to gigabytes. To avoid interacting with regular vmallocs, these
3146 * areas are allocated from top.
Tejun Heoca23e402009-08-14 15:00:52 +09003147 *
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003148 * Despite its complicated look, this allocator is rather simple. It
3149 * does everything top-down and scans free blocks from the end looking
3150 * for matching base. While scanning, if any of the areas do not fit the
3151 * base address is pulled down to fit the area. Scanning is repeated till
3152 * all the areas fit and then all necessary data structures are inserted
3153 * and the result is returned.
Tejun Heoca23e402009-08-14 15:00:52 +09003154 */
3155struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
3156 const size_t *sizes, int nr_vms,
David Rientjesec3f64f2011-01-13 15:46:01 -08003157 size_t align)
Tejun Heoca23e402009-08-14 15:00:52 +09003158{
3159 const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align);
3160 const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003161 struct vmap_area **vas, *va;
Tejun Heoca23e402009-08-14 15:00:52 +09003162 struct vm_struct **vms;
3163 int area, area2, last_area, term_area;
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003164 unsigned long base, start, size, end, last_end;
Tejun Heoca23e402009-08-14 15:00:52 +09003165 bool purged = false;
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003166 enum fit_type type;
Tejun Heoca23e402009-08-14 15:00:52 +09003167
Tejun Heoca23e402009-08-14 15:00:52 +09003168 /* verify parameters and allocate data structures */
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08003169 BUG_ON(offset_in_page(align) || !is_power_of_2(align));
Tejun Heoca23e402009-08-14 15:00:52 +09003170 for (last_area = 0, area = 0; area < nr_vms; area++) {
3171 start = offsets[area];
3172 end = start + sizes[area];
3173
3174 /* is everything aligned properly? */
3175 BUG_ON(!IS_ALIGNED(offsets[area], align));
3176 BUG_ON(!IS_ALIGNED(sizes[area], align));
3177
3178 /* detect the area with the highest address */
3179 if (start > offsets[last_area])
3180 last_area = area;
3181
Wei Yangc568da22017-09-06 16:24:09 -07003182 for (area2 = area + 1; area2 < nr_vms; area2++) {
Tejun Heoca23e402009-08-14 15:00:52 +09003183 unsigned long start2 = offsets[area2];
3184 unsigned long end2 = start2 + sizes[area2];
3185
Wei Yangc568da22017-09-06 16:24:09 -07003186 BUG_ON(start2 < end && start < end2);
Tejun Heoca23e402009-08-14 15:00:52 +09003187 }
3188 }
3189 last_end = offsets[last_area] + sizes[last_area];
3190
3191 if (vmalloc_end - vmalloc_start < last_end) {
3192 WARN_ON(true);
3193 return NULL;
3194 }
3195
Thomas Meyer4d67d862012-05-29 15:06:21 -07003196 vms = kcalloc(nr_vms, sizeof(vms[0]), GFP_KERNEL);
3197 vas = kcalloc(nr_vms, sizeof(vas[0]), GFP_KERNEL);
Tejun Heoca23e402009-08-14 15:00:52 +09003198 if (!vas || !vms)
Kautuk Consulf1db7af2012-01-12 17:20:08 -08003199 goto err_free2;
Tejun Heoca23e402009-08-14 15:00:52 +09003200
3201 for (area = 0; area < nr_vms; area++) {
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003202 vas[area] = kmem_cache_zalloc(vmap_area_cachep, GFP_KERNEL);
David Rientjesec3f64f2011-01-13 15:46:01 -08003203 vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL);
Tejun Heoca23e402009-08-14 15:00:52 +09003204 if (!vas[area] || !vms[area])
3205 goto err_free;
3206 }
3207retry:
3208 spin_lock(&vmap_area_lock);
3209
3210 /* start scanning - we scan from the top, begin with the last area */
3211 area = term_area = last_area;
3212 start = offsets[area];
3213 end = start + sizes[area];
3214
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003215 va = pvm_find_va_enclose_addr(vmalloc_end);
3216 base = pvm_determine_end_from_reverse(&va, align) - end;
Tejun Heoca23e402009-08-14 15:00:52 +09003217
3218 while (true) {
Tejun Heoca23e402009-08-14 15:00:52 +09003219 /*
3220 * base might have underflowed, add last_end before
3221 * comparing.
3222 */
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003223 if (base + last_end < vmalloc_start + last_end)
3224 goto overflow;
Tejun Heoca23e402009-08-14 15:00:52 +09003225
3226 /*
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003227 * Fitting base has not been found.
Tejun Heoca23e402009-08-14 15:00:52 +09003228 */
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003229 if (va == NULL)
3230 goto overflow;
Tejun Heoca23e402009-08-14 15:00:52 +09003231
3232 /*
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003233 * If this VA does not fit, move base downwards and recheck.
Tejun Heoca23e402009-08-14 15:00:52 +09003234 */
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003235 if (base + start < va->va_start || base + end > va->va_end) {
3236 va = node_to_va(rb_prev(&va->rb_node));
3237 base = pvm_determine_end_from_reverse(&va, align) - end;
Tejun Heoca23e402009-08-14 15:00:52 +09003238 term_area = area;
3239 continue;
3240 }
3241
3242 /*
3243 * This area fits, move on to the previous one. If
3244 * the previous one is the terminal one, we're done.
3245 */
3246 area = (area + nr_vms - 1) % nr_vms;
3247 if (area == term_area)
3248 break;
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003249
Tejun Heoca23e402009-08-14 15:00:52 +09003250 start = offsets[area];
3251 end = start + sizes[area];
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003252 va = pvm_find_va_enclose_addr(base + end);
Tejun Heoca23e402009-08-14 15:00:52 +09003253 }
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003254
Tejun Heoca23e402009-08-14 15:00:52 +09003255 /* we've found a fitting base, insert all va's */
3256 for (area = 0; area < nr_vms; area++) {
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003257 int ret;
Tejun Heoca23e402009-08-14 15:00:52 +09003258
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003259 start = base + offsets[area];
3260 size = sizes[area];
3261
3262 va = pvm_find_va_enclose_addr(start);
3263 if (WARN_ON_ONCE(va == NULL))
3264 /* It is a BUG(), but trigger recovery instead. */
3265 goto recovery;
3266
3267 type = classify_va_fit_type(va, start, size);
3268 if (WARN_ON_ONCE(type == NOTHING_FIT))
3269 /* It is a BUG(), but trigger recovery instead. */
3270 goto recovery;
3271
3272 ret = adjust_va_to_fit_type(va, start, size, type);
3273 if (unlikely(ret))
3274 goto recovery;
3275
3276 /* Allocated area. */
3277 va = vas[area];
3278 va->va_start = start;
3279 va->va_end = start + size;
3280
3281 insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
Tejun Heoca23e402009-08-14 15:00:52 +09003282 }
3283
Tejun Heoca23e402009-08-14 15:00:52 +09003284 spin_unlock(&vmap_area_lock);
3285
3286 /* insert all vm's */
3287 for (area = 0; area < nr_vms; area++)
Zhang Yanfei3645cb42013-07-03 15:04:48 -07003288 setup_vmalloc_vm(vms[area], vas[area], VM_ALLOC,
3289 pcpu_get_vm_areas);
Tejun Heoca23e402009-08-14 15:00:52 +09003290
3291 kfree(vas);
3292 return vms;
3293
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003294recovery:
3295 /* Remove previously inserted areas. */
3296 while (area--) {
3297 __free_vmap_area(vas[area]);
3298 vas[area] = NULL;
3299 }
3300
3301overflow:
3302 spin_unlock(&vmap_area_lock);
3303 if (!purged) {
3304 purge_vmap_area_lazy();
3305 purged = true;
3306
3307 /* Before "retry", check if we recover. */
3308 for (area = 0; area < nr_vms; area++) {
3309 if (vas[area])
3310 continue;
3311
3312 vas[area] = kmem_cache_zalloc(
3313 vmap_area_cachep, GFP_KERNEL);
3314 if (!vas[area])
3315 goto err_free;
3316 }
3317
3318 goto retry;
3319 }
3320
Tejun Heoca23e402009-08-14 15:00:52 +09003321err_free:
3322 for (area = 0; area < nr_vms; area++) {
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003323 if (vas[area])
3324 kmem_cache_free(vmap_area_cachep, vas[area]);
3325
Kautuk Consulf1db7af2012-01-12 17:20:08 -08003326 kfree(vms[area]);
Tejun Heoca23e402009-08-14 15:00:52 +09003327 }
Kautuk Consulf1db7af2012-01-12 17:20:08 -08003328err_free2:
Tejun Heoca23e402009-08-14 15:00:52 +09003329 kfree(vas);
3330 kfree(vms);
3331 return NULL;
3332}
3333
3334/**
3335 * pcpu_free_vm_areas - free vmalloc areas for percpu allocator
3336 * @vms: vm_struct pointer array returned by pcpu_get_vm_areas()
3337 * @nr_vms: the number of allocated areas
3338 *
3339 * Free vm_structs and the array allocated by pcpu_get_vm_areas().
3340 */
3341void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
3342{
3343 int i;
3344
3345 for (i = 0; i < nr_vms; i++)
3346 free_vm_area(vms[i]);
3347 kfree(vms);
3348}
Tejun Heo4f8b02b2010-09-03 18:22:47 +02003349#endif /* CONFIG_SMP */
Christoph Lametera10aa572008-04-28 02:12:40 -07003350
3351#ifdef CONFIG_PROC_FS
3352static void *s_start(struct seq_file *m, loff_t *pos)
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003353 __acquires(&vmap_area_lock)
Christoph Lametera10aa572008-04-28 02:12:40 -07003354{
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003355 spin_lock(&vmap_area_lock);
zijun_hu3f500062016-12-12 16:42:17 -08003356 return seq_list_start(&vmap_area_list, *pos);
Christoph Lametera10aa572008-04-28 02:12:40 -07003357}
3358
3359static void *s_next(struct seq_file *m, void *p, loff_t *pos)
3360{
zijun_hu3f500062016-12-12 16:42:17 -08003361 return seq_list_next(p, &vmap_area_list, pos);
Christoph Lametera10aa572008-04-28 02:12:40 -07003362}
3363
3364static void s_stop(struct seq_file *m, void *p)
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003365 __releases(&vmap_area_lock)
Christoph Lametera10aa572008-04-28 02:12:40 -07003366{
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003367 spin_unlock(&vmap_area_lock);
Christoph Lametera10aa572008-04-28 02:12:40 -07003368}
3369
Eric Dumazeta47a1262008-07-23 21:27:38 -07003370static void show_numa_info(struct seq_file *m, struct vm_struct *v)
3371{
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08003372 if (IS_ENABLED(CONFIG_NUMA)) {
Eric Dumazeta47a1262008-07-23 21:27:38 -07003373 unsigned int nr, *counters = m->private;
3374
3375 if (!counters)
3376 return;
3377
Wanpeng Liaf123462013-11-12 15:07:32 -08003378 if (v->flags & VM_UNINITIALIZED)
3379 return;
Dmitry Vyukov7e5b5282014-12-12 16:56:30 -08003380 /* Pair with smp_wmb() in clear_vm_uninitialized_flag() */
3381 smp_rmb();
Wanpeng Liaf123462013-11-12 15:07:32 -08003382
Eric Dumazeta47a1262008-07-23 21:27:38 -07003383 memset(counters, 0, nr_node_ids * sizeof(unsigned int));
3384
3385 for (nr = 0; nr < v->nr_pages; nr++)
3386 counters[page_to_nid(v->pages[nr])]++;
3387
3388 for_each_node_state(nr, N_HIGH_MEMORY)
3389 if (counters[nr])
3390 seq_printf(m, " N%u=%u", nr, counters[nr]);
3391 }
3392}
3393
Christoph Lametera10aa572008-04-28 02:12:40 -07003394static int s_show(struct seq_file *m, void *p)
3395{
zijun_hu3f500062016-12-12 16:42:17 -08003396 struct vmap_area *va;
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003397 struct vm_struct *v;
3398
zijun_hu3f500062016-12-12 16:42:17 -08003399 va = list_entry(p, struct vmap_area, list);
3400
Wanpeng Lic2ce8c12013-11-12 15:07:31 -08003401 /*
3402 * s_show can encounter race with remove_vm_area, !VM_VM_AREA on
3403 * behalf of vmap area is being tear down or vm_map_ram allocation.
3404 */
Yisheng Xie78c72742017-07-10 15:48:09 -07003405 if (!(va->flags & VM_VM_AREA)) {
3406 seq_printf(m, "0x%pK-0x%pK %7ld %s\n",
3407 (void *)va->va_start, (void *)va->va_end,
3408 va->va_end - va->va_start,
3409 va->flags & VM_LAZY_FREE ? "unpurged vm_area" : "vm_map_ram");
3410
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003411 return 0;
Yisheng Xie78c72742017-07-10 15:48:09 -07003412 }
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003413
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003414 v = va->vm;
Christoph Lametera10aa572008-04-28 02:12:40 -07003415
Kees Cook45ec1692012-10-08 16:34:09 -07003416 seq_printf(m, "0x%pK-0x%pK %7ld",
Christoph Lametera10aa572008-04-28 02:12:40 -07003417 v->addr, v->addr + v->size, v->size);
3418
Joe Perches62c70bc2011-01-13 15:45:52 -08003419 if (v->caller)
3420 seq_printf(m, " %pS", v->caller);
Christoph Lameter23016962008-04-28 02:12:42 -07003421
Christoph Lametera10aa572008-04-28 02:12:40 -07003422 if (v->nr_pages)
3423 seq_printf(m, " pages=%d", v->nr_pages);
3424
3425 if (v->phys_addr)
Miles Chen199eaa02017-02-24 14:59:51 -08003426 seq_printf(m, " phys=%pa", &v->phys_addr);
Christoph Lametera10aa572008-04-28 02:12:40 -07003427
3428 if (v->flags & VM_IOREMAP)
Fabian Frederickf4527c92014-06-04 16:08:09 -07003429 seq_puts(m, " ioremap");
Christoph Lametera10aa572008-04-28 02:12:40 -07003430
3431 if (v->flags & VM_ALLOC)
Fabian Frederickf4527c92014-06-04 16:08:09 -07003432 seq_puts(m, " vmalloc");
Christoph Lametera10aa572008-04-28 02:12:40 -07003433
3434 if (v->flags & VM_MAP)
Fabian Frederickf4527c92014-06-04 16:08:09 -07003435 seq_puts(m, " vmap");
Christoph Lametera10aa572008-04-28 02:12:40 -07003436
3437 if (v->flags & VM_USERMAP)
Fabian Frederickf4527c92014-06-04 16:08:09 -07003438 seq_puts(m, " user");
Christoph Lametera10aa572008-04-28 02:12:40 -07003439
David Rientjes244d63e2016-01-14 15:19:35 -08003440 if (is_vmalloc_addr(v->pages))
Fabian Frederickf4527c92014-06-04 16:08:09 -07003441 seq_puts(m, " vpages");
Christoph Lametera10aa572008-04-28 02:12:40 -07003442
Eric Dumazeta47a1262008-07-23 21:27:38 -07003443 show_numa_info(m, v);
Christoph Lametera10aa572008-04-28 02:12:40 -07003444 seq_putc(m, '\n');
3445 return 0;
3446}
3447
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04003448static const struct seq_operations vmalloc_op = {
Christoph Lametera10aa572008-04-28 02:12:40 -07003449 .start = s_start,
3450 .next = s_next,
3451 .stop = s_stop,
3452 .show = s_show,
3453};
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04003454
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04003455static int __init proc_vmalloc_init(void)
3456{
Christoph Hellwigfddda2b2018-04-13 19:44:18 +02003457 if (IS_ENABLED(CONFIG_NUMA))
Joe Perches0825a6f2018-06-14 15:27:58 -07003458 proc_create_seq_private("vmallocinfo", 0400, NULL,
Christoph Hellwig44414d82018-04-24 17:05:17 +02003459 &vmalloc_op,
3460 nr_node_ids * sizeof(unsigned int), NULL);
Christoph Hellwigfddda2b2018-04-13 19:44:18 +02003461 else
Joe Perches0825a6f2018-06-14 15:27:58 -07003462 proc_create_seq("vmallocinfo", 0400, NULL, &vmalloc_op);
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04003463 return 0;
3464}
3465module_init(proc_vmalloc_init);
Joonsoo Kimdb3808c2013-04-29 15:07:28 -07003466
Christoph Lametera10aa572008-04-28 02:12:40 -07003467#endif