blob: f8f61ff3235bc8ed72fe886e130aef8d034553c7 [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
Yisheng Xie78c72742017-07-10 15:48:09 -0700328#define VM_LAZY_FREE 0x02
Nick Piggindb64fe02008-10-18 20:27:03 -0700329#define VM_VM_AREA 0x04
330
Nick Piggindb64fe02008-10-18 20:27:03 -0700331static DEFINE_SPINLOCK(vmap_area_lock);
Joonsoo Kimf1c40692013-04-29 15:07:37 -0700332/* Export for kexec only */
333LIST_HEAD(vmap_area_list);
Chris Wilson80c4bd72016-05-20 16:57:38 -0700334static LLIST_HEAD(vmap_purge_list);
Nick Piggin89699602011-03-22 16:30:36 -0700335static struct rb_root vmap_area_root = RB_ROOT;
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700336static bool vmap_initialized __read_mostly;
Nick Piggin89699602011-03-22 16:30:36 -0700337
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700338/*
339 * This kmem_cache is used for vmap_area objects. Instead of
340 * allocating from slab we reuse an object from this cache to
341 * make things faster. Especially in "no edge" splitting of
342 * free block.
343 */
344static struct kmem_cache *vmap_area_cachep;
Nick Piggin89699602011-03-22 16:30:36 -0700345
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700346/*
347 * This linked list is used in pair with free_vmap_area_root.
348 * It gives O(1) access to prev/next to perform fast coalescing.
349 */
350static LIST_HEAD(free_vmap_area_list);
351
352/*
353 * This augment red-black tree represents the free vmap space.
354 * All vmap_area objects in this tree are sorted by va->va_start
355 * address. It is used for allocation and merging when a vmap
356 * object is released.
357 *
358 * Each vmap_area node contains a maximum available free block
359 * of its sub-tree, right or left. Therefore it is possible to
360 * find a lowest match of free area.
361 */
362static struct rb_root free_vmap_area_root = RB_ROOT;
363
364static __always_inline unsigned long
365va_size(struct vmap_area *va)
366{
367 return (va->va_end - va->va_start);
368}
369
370static __always_inline unsigned long
371get_subtree_max_size(struct rb_node *node)
372{
373 struct vmap_area *va;
374
375 va = rb_entry_safe(node, struct vmap_area, rb_node);
376 return va ? va->subtree_max_size : 0;
377}
378
379/*
380 * Gets called when remove the node and rotate.
381 */
382static __always_inline unsigned long
383compute_subtree_max_size(struct vmap_area *va)
384{
385 return max3(va_size(va),
386 get_subtree_max_size(va->rb_node.rb_left),
387 get_subtree_max_size(va->rb_node.rb_right));
388}
389
390RB_DECLARE_CALLBACKS(static, free_vmap_area_rb_augment_cb,
391 struct vmap_area, rb_node, unsigned long, subtree_max_size,
392 compute_subtree_max_size)
393
394static void purge_vmap_area_lazy(void);
395static BLOCKING_NOTIFIER_HEAD(vmap_notify_list);
396static unsigned long lazy_max_pages(void);
Nick Piggindb64fe02008-10-18 20:27:03 -0700397
398static struct vmap_area *__find_vmap_area(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Nick Piggindb64fe02008-10-18 20:27:03 -0700400 struct rb_node *n = vmap_area_root.rb_node;
401
402 while (n) {
403 struct vmap_area *va;
404
405 va = rb_entry(n, struct vmap_area, rb_node);
406 if (addr < va->va_start)
407 n = n->rb_left;
HATAYAMA Daisukecef2ac32013-07-03 15:02:17 -0700408 else if (addr >= va->va_end)
Nick Piggindb64fe02008-10-18 20:27:03 -0700409 n = n->rb_right;
410 else
411 return va;
412 }
413
414 return NULL;
415}
416
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700417/*
418 * This function returns back addresses of parent node
419 * and its left or right link for further processing.
420 */
421static __always_inline struct rb_node **
422find_va_links(struct vmap_area *va,
423 struct rb_root *root, struct rb_node *from,
424 struct rb_node **parent)
Nick Piggindb64fe02008-10-18 20:27:03 -0700425{
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700426 struct vmap_area *tmp_va;
427 struct rb_node **link;
Nick Piggindb64fe02008-10-18 20:27:03 -0700428
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700429 if (root) {
430 link = &root->rb_node;
431 if (unlikely(!*link)) {
432 *parent = NULL;
433 return link;
434 }
435 } else {
436 link = &from;
Nick Piggindb64fe02008-10-18 20:27:03 -0700437 }
438
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700439 /*
440 * Go to the bottom of the tree. When we hit the last point
441 * we end up with parent rb_node and correct direction, i name
442 * it link, where the new va->rb_node will be attached to.
443 */
444 do {
445 tmp_va = rb_entry(*link, struct vmap_area, rb_node);
Nick Piggindb64fe02008-10-18 20:27:03 -0700446
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700447 /*
448 * During the traversal we also do some sanity check.
449 * Trigger the BUG() if there are sides(left/right)
450 * or full overlaps.
451 */
452 if (va->va_start < tmp_va->va_end &&
453 va->va_end <= tmp_va->va_start)
454 link = &(*link)->rb_left;
455 else if (va->va_end > tmp_va->va_start &&
456 va->va_start >= tmp_va->va_end)
457 link = &(*link)->rb_right;
458 else
459 BUG();
460 } while (*link);
461
462 *parent = &tmp_va->rb_node;
463 return link;
Nick Piggindb64fe02008-10-18 20:27:03 -0700464}
465
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700466static __always_inline struct list_head *
467get_va_next_sibling(struct rb_node *parent, struct rb_node **link)
468{
469 struct list_head *list;
Nick Piggindb64fe02008-10-18 20:27:03 -0700470
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700471 if (unlikely(!parent))
472 /*
473 * The red-black tree where we try to find VA neighbors
474 * before merging or inserting is empty, i.e. it means
475 * there is no free vmap space. Normally it does not
476 * happen but we handle this case anyway.
477 */
478 return NULL;
479
480 list = &rb_entry(parent, struct vmap_area, rb_node)->list;
481 return (&parent->rb_right == link ? list->next : list);
482}
483
484static __always_inline void
485link_va(struct vmap_area *va, struct rb_root *root,
486 struct rb_node *parent, struct rb_node **link, struct list_head *head)
487{
488 /*
489 * VA is still not in the list, but we can
490 * identify its future previous list_head node.
491 */
492 if (likely(parent)) {
493 head = &rb_entry(parent, struct vmap_area, rb_node)->list;
494 if (&parent->rb_right != link)
495 head = head->prev;
496 }
497
498 /* Insert to the rb-tree */
499 rb_link_node(&va->rb_node, parent, link);
500 if (root == &free_vmap_area_root) {
501 /*
502 * Some explanation here. Just perform simple insertion
503 * to the tree. We do not set va->subtree_max_size to
504 * its current size before calling rb_insert_augmented().
505 * It is because of we populate the tree from the bottom
506 * to parent levels when the node _is_ in the tree.
507 *
508 * Therefore we set subtree_max_size to zero after insertion,
509 * to let __augment_tree_propagate_from() puts everything to
510 * the correct order later on.
511 */
512 rb_insert_augmented(&va->rb_node,
513 root, &free_vmap_area_rb_augment_cb);
514 va->subtree_max_size = 0;
515 } else {
516 rb_insert_color(&va->rb_node, root);
517 }
518
519 /* Address-sort this list */
520 list_add(&va->list, head);
521}
522
523static __always_inline void
524unlink_va(struct vmap_area *va, struct rb_root *root)
525{
526 /*
527 * During merging a VA node can be empty, therefore
528 * not linked with the tree nor list. Just check it.
529 */
530 if (!RB_EMPTY_NODE(&va->rb_node)) {
531 if (root == &free_vmap_area_root)
532 rb_erase_augmented(&va->rb_node,
533 root, &free_vmap_area_rb_augment_cb);
534 else
535 rb_erase(&va->rb_node, root);
536
537 list_del(&va->list);
538 RB_CLEAR_NODE(&va->rb_node);
539 }
540}
541
542/*
543 * This function populates subtree_max_size from bottom to upper
544 * levels starting from VA point. The propagation must be done
545 * when VA size is modified by changing its va_start/va_end. Or
546 * in case of newly inserting of VA to the tree.
547 *
548 * It means that __augment_tree_propagate_from() must be called:
549 * - After VA has been inserted to the tree(free path);
550 * - After VA has been shrunk(allocation path);
551 * - After VA has been increased(merging path).
552 *
553 * Please note that, it does not mean that upper parent nodes
554 * and their subtree_max_size are recalculated all the time up
555 * to the root node.
556 *
557 * 4--8
558 * /\
559 * / \
560 * / \
561 * 2--2 8--8
562 *
563 * For example if we modify the node 4, shrinking it to 2, then
564 * no any modification is required. If we shrink the node 2 to 1
565 * its subtree_max_size is updated only, and set to 1. If we shrink
566 * the node 8 to 6, then its subtree_max_size is set to 6 and parent
567 * node becomes 4--6.
568 */
569static __always_inline void
570augment_tree_propagate_from(struct vmap_area *va)
571{
572 struct rb_node *node = &va->rb_node;
573 unsigned long new_va_sub_max_size;
574
575 while (node) {
576 va = rb_entry(node, struct vmap_area, rb_node);
577 new_va_sub_max_size = compute_subtree_max_size(va);
578
579 /*
580 * If the newly calculated maximum available size of the
581 * subtree is equal to the current one, then it means that
582 * the tree is propagated correctly. So we have to stop at
583 * this point to save cycles.
584 */
585 if (va->subtree_max_size == new_va_sub_max_size)
586 break;
587
588 va->subtree_max_size = new_va_sub_max_size;
589 node = rb_parent(&va->rb_node);
590 }
591}
592
593static void
594insert_vmap_area(struct vmap_area *va,
595 struct rb_root *root, struct list_head *head)
596{
597 struct rb_node **link;
598 struct rb_node *parent;
599
600 link = find_va_links(va, root, NULL, &parent);
601 link_va(va, root, parent, link, head);
602}
603
604static void
605insert_vmap_area_augment(struct vmap_area *va,
606 struct rb_node *from, struct rb_root *root,
607 struct list_head *head)
608{
609 struct rb_node **link;
610 struct rb_node *parent;
611
612 if (from)
613 link = find_va_links(va, NULL, from, &parent);
614 else
615 link = find_va_links(va, root, NULL, &parent);
616
617 link_va(va, root, parent, link, head);
618 augment_tree_propagate_from(va);
619}
620
621/*
622 * Merge de-allocated chunk of VA memory with previous
623 * and next free blocks. If coalesce is not done a new
624 * free area is inserted. If VA has been merged, it is
625 * freed.
626 */
627static __always_inline void
628merge_or_add_vmap_area(struct vmap_area *va,
629 struct rb_root *root, struct list_head *head)
630{
631 struct vmap_area *sibling;
632 struct list_head *next;
633 struct rb_node **link;
634 struct rb_node *parent;
635 bool merged = false;
636
637 /*
638 * Find a place in the tree where VA potentially will be
639 * inserted, unless it is merged with its sibling/siblings.
640 */
641 link = find_va_links(va, root, NULL, &parent);
642
643 /*
644 * Get next node of VA to check if merging can be done.
645 */
646 next = get_va_next_sibling(parent, link);
647 if (unlikely(next == NULL))
648 goto insert;
649
650 /*
651 * start end
652 * | |
653 * |<------VA------>|<-----Next----->|
654 * | |
655 * start end
656 */
657 if (next != head) {
658 sibling = list_entry(next, struct vmap_area, list);
659 if (sibling->va_start == va->va_end) {
660 sibling->va_start = va->va_start;
661
662 /* Check and update the tree if needed. */
663 augment_tree_propagate_from(sibling);
664
665 /* Remove this VA, it has been merged. */
666 unlink_va(va, root);
667
668 /* Free vmap_area object. */
669 kmem_cache_free(vmap_area_cachep, va);
670
671 /* Point to the new merged area. */
672 va = sibling;
673 merged = true;
674 }
675 }
676
677 /*
678 * start end
679 * | |
680 * |<-----Prev----->|<------VA------>|
681 * | |
682 * start end
683 */
684 if (next->prev != head) {
685 sibling = list_entry(next->prev, struct vmap_area, list);
686 if (sibling->va_end == va->va_start) {
687 sibling->va_end = va->va_end;
688
689 /* Check and update the tree if needed. */
690 augment_tree_propagate_from(sibling);
691
692 /* Remove this VA, it has been merged. */
693 unlink_va(va, root);
694
695 /* Free vmap_area object. */
696 kmem_cache_free(vmap_area_cachep, va);
697
698 return;
699 }
700 }
701
702insert:
703 if (!merged) {
704 link_va(va, root, parent, link, head);
705 augment_tree_propagate_from(va);
706 }
707}
708
709static __always_inline bool
710is_within_this_va(struct vmap_area *va, unsigned long size,
711 unsigned long align, unsigned long vstart)
712{
713 unsigned long nva_start_addr;
714
715 if (va->va_start > vstart)
716 nva_start_addr = ALIGN(va->va_start, align);
717 else
718 nva_start_addr = ALIGN(vstart, align);
719
720 /* Can be overflowed due to big size or alignment. */
721 if (nva_start_addr + size < nva_start_addr ||
722 nva_start_addr < vstart)
723 return false;
724
725 return (nva_start_addr + size <= va->va_end);
726}
727
728/*
729 * Find the first free block(lowest start address) in the tree,
730 * that will accomplish the request corresponding to passing
731 * parameters.
732 */
733static __always_inline struct vmap_area *
734find_vmap_lowest_match(unsigned long size,
735 unsigned long align, unsigned long vstart)
736{
737 struct vmap_area *va;
738 struct rb_node *node;
739 unsigned long length;
740
741 /* Start from the root. */
742 node = free_vmap_area_root.rb_node;
743
744 /* Adjust the search size for alignment overhead. */
745 length = size + align - 1;
746
747 while (node) {
748 va = rb_entry(node, struct vmap_area, rb_node);
749
750 if (get_subtree_max_size(node->rb_left) >= length &&
751 vstart < va->va_start) {
752 node = node->rb_left;
753 } else {
754 if (is_within_this_va(va, size, align, vstart))
755 return va;
756
757 /*
758 * Does not make sense to go deeper towards the right
759 * sub-tree if it does not have a free block that is
760 * equal or bigger to the requested search length.
761 */
762 if (get_subtree_max_size(node->rb_right) >= length) {
763 node = node->rb_right;
764 continue;
765 }
766
767 /*
768 * OK. We roll back and find the fist right sub-tree,
769 * that will satisfy the search criteria. It can happen
770 * only once due to "vstart" restriction.
771 */
772 while ((node = rb_parent(node))) {
773 va = rb_entry(node, struct vmap_area, rb_node);
774 if (is_within_this_va(va, size, align, vstart))
775 return va;
776
777 if (get_subtree_max_size(node->rb_right) >= length &&
778 vstart <= va->va_start) {
779 node = node->rb_right;
780 break;
781 }
782 }
783 }
784 }
785
786 return NULL;
787}
788
789enum fit_type {
790 NOTHING_FIT = 0,
791 FL_FIT_TYPE = 1, /* full fit */
792 LE_FIT_TYPE = 2, /* left edge fit */
793 RE_FIT_TYPE = 3, /* right edge fit */
794 NE_FIT_TYPE = 4 /* no edge fit */
795};
796
797static __always_inline enum fit_type
798classify_va_fit_type(struct vmap_area *va,
799 unsigned long nva_start_addr, unsigned long size)
800{
801 enum fit_type type;
802
803 /* Check if it is within VA. */
804 if (nva_start_addr < va->va_start ||
805 nva_start_addr + size > va->va_end)
806 return NOTHING_FIT;
807
808 /* Now classify. */
809 if (va->va_start == nva_start_addr) {
810 if (va->va_end == nva_start_addr + size)
811 type = FL_FIT_TYPE;
812 else
813 type = LE_FIT_TYPE;
814 } else if (va->va_end == nva_start_addr + size) {
815 type = RE_FIT_TYPE;
816 } else {
817 type = NE_FIT_TYPE;
818 }
819
820 return type;
821}
822
823static __always_inline int
824adjust_va_to_fit_type(struct vmap_area *va,
825 unsigned long nva_start_addr, unsigned long size,
826 enum fit_type type)
827{
828 struct vmap_area *lva;
829
830 if (type == FL_FIT_TYPE) {
831 /*
832 * No need to split VA, it fully fits.
833 *
834 * | |
835 * V NVA V
836 * |---------------|
837 */
838 unlink_va(va, &free_vmap_area_root);
839 kmem_cache_free(vmap_area_cachep, va);
840 } else if (type == LE_FIT_TYPE) {
841 /*
842 * Split left edge of fit VA.
843 *
844 * | |
845 * V NVA V R
846 * |-------|-------|
847 */
848 va->va_start += size;
849 } else if (type == RE_FIT_TYPE) {
850 /*
851 * Split right edge of fit VA.
852 *
853 * | |
854 * L V NVA V
855 * |-------|-------|
856 */
857 va->va_end = nva_start_addr;
858 } else if (type == NE_FIT_TYPE) {
859 /*
860 * Split no edge of fit VA.
861 *
862 * | |
863 * L V NVA V R
864 * |---|-------|---|
865 */
866 lva = kmem_cache_alloc(vmap_area_cachep, GFP_NOWAIT);
867 if (unlikely(!lva))
868 return -1;
869
870 /*
871 * Build the remainder.
872 */
873 lva->va_start = va->va_start;
874 lva->va_end = nva_start_addr;
875
876 /*
877 * Shrink this VA to remaining size.
878 */
879 va->va_start = nva_start_addr + size;
880 } else {
881 return -1;
882 }
883
884 if (type != FL_FIT_TYPE) {
885 augment_tree_propagate_from(va);
886
887 if (type == NE_FIT_TYPE)
888 insert_vmap_area_augment(lva, &va->rb_node,
889 &free_vmap_area_root, &free_vmap_area_list);
890 }
891
892 return 0;
893}
894
895/*
896 * Returns a start address of the newly allocated area, if success.
897 * Otherwise a vend is returned that indicates failure.
898 */
899static __always_inline unsigned long
900__alloc_vmap_area(unsigned long size, unsigned long align,
901 unsigned long vstart, unsigned long vend, int node)
902{
903 unsigned long nva_start_addr;
904 struct vmap_area *va;
905 enum fit_type type;
906 int ret;
907
908 va = find_vmap_lowest_match(size, align, vstart);
909 if (unlikely(!va))
910 return vend;
911
912 if (va->va_start > vstart)
913 nva_start_addr = ALIGN(va->va_start, align);
914 else
915 nva_start_addr = ALIGN(vstart, align);
916
917 /* Check the "vend" restriction. */
918 if (nva_start_addr + size > vend)
919 return vend;
920
921 /* Classify what we have found. */
922 type = classify_va_fit_type(va, nva_start_addr, size);
923 if (WARN_ON_ONCE(type == NOTHING_FIT))
924 return vend;
925
926 /* Update the free vmap_area. */
927 ret = adjust_va_to_fit_type(va, nva_start_addr, size, type);
928 if (ret)
929 return vend;
930
931 return nva_start_addr;
932}
Chris Wilson4da56b92016-04-04 14:46:42 +0100933
Nick Piggindb64fe02008-10-18 20:27:03 -0700934/*
935 * Allocate a region of KVA of the specified size and alignment, within the
936 * vstart and vend.
937 */
938static struct vmap_area *alloc_vmap_area(unsigned long size,
939 unsigned long align,
940 unsigned long vstart, unsigned long vend,
941 int node, gfp_t gfp_mask)
942{
943 struct vmap_area *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 unsigned long addr;
Nick Piggindb64fe02008-10-18 20:27:03 -0700945 int purged = 0;
946
Nick Piggin77669702009-02-27 14:03:03 -0800947 BUG_ON(!size);
Alexander Kuleshov891c49a2015-11-05 18:46:51 -0800948 BUG_ON(offset_in_page(size));
Nick Piggin89699602011-03-22 16:30:36 -0700949 BUG_ON(!is_power_of_2(align));
Nick Piggindb64fe02008-10-18 20:27:03 -0700950
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700951 if (unlikely(!vmap_initialized))
952 return ERR_PTR(-EBUSY);
953
Christoph Hellwig5803ed22016-12-12 16:44:20 -0800954 might_sleep();
Chris Wilson4da56b92016-04-04 14:46:42 +0100955
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700956 va = kmem_cache_alloc_node(vmap_area_cachep,
Nick Piggindb64fe02008-10-18 20:27:03 -0700957 gfp_mask & GFP_RECLAIM_MASK, node);
958 if (unlikely(!va))
959 return ERR_PTR(-ENOMEM);
960
Catalin Marinas7f88f882013-11-12 15:07:45 -0800961 /*
962 * Only scan the relevant parts containing pointers to other objects
963 * to avoid false negatives.
964 */
965 kmemleak_scan_area(&va->rb_node, SIZE_MAX, gfp_mask & GFP_RECLAIM_MASK);
966
Nick Piggindb64fe02008-10-18 20:27:03 -0700967retry:
968 spin_lock(&vmap_area_lock);
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700969
Nick Piggin89699602011-03-22 16:30:36 -0700970 /*
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700971 * If an allocation fails, the "vend" address is
972 * returned. Therefore trigger the overflow path.
Nick Piggin89699602011-03-22 16:30:36 -0700973 */
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700974 addr = __alloc_vmap_area(size, align, vstart, vend, node);
975 if (unlikely(addr == vend))
Nick Piggin89699602011-03-22 16:30:36 -0700976 goto overflow;
Nick Piggindb64fe02008-10-18 20:27:03 -0700977
978 va->va_start = addr;
979 va->va_end = addr + size;
980 va->flags = 0;
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -0700981 insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
982
Nick Piggindb64fe02008-10-18 20:27:03 -0700983 spin_unlock(&vmap_area_lock);
984
Wang Xiaoqiang61e16552016-01-15 16:57:19 -0800985 BUG_ON(!IS_ALIGNED(va->va_start, align));
Nick Piggin89699602011-03-22 16:30:36 -0700986 BUG_ON(va->va_start < vstart);
987 BUG_ON(va->va_end > vend);
988
Nick Piggindb64fe02008-10-18 20:27:03 -0700989 return va;
Nick Piggin89699602011-03-22 16:30:36 -0700990
991overflow:
992 spin_unlock(&vmap_area_lock);
993 if (!purged) {
994 purge_vmap_area_lazy();
995 purged = 1;
996 goto retry;
997 }
Chris Wilson4da56b92016-04-04 14:46:42 +0100998
999 if (gfpflags_allow_blocking(gfp_mask)) {
1000 unsigned long freed = 0;
1001 blocking_notifier_call_chain(&vmap_notify_list, 0, &freed);
1002 if (freed > 0) {
1003 purged = 0;
1004 goto retry;
1005 }
1006 }
1007
Florian Fainelli03497d72017-04-27 11:19:00 -07001008 if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit())
Joe Perches756a0252016-03-17 14:19:47 -07001009 pr_warn("vmap allocation for size %lu failed: use vmalloc=<size> to increase size\n",
1010 size);
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001011
1012 kmem_cache_free(vmap_area_cachep, va);
Nick Piggin89699602011-03-22 16:30:36 -07001013 return ERR_PTR(-EBUSY);
Nick Piggindb64fe02008-10-18 20:27:03 -07001014}
1015
Chris Wilson4da56b92016-04-04 14:46:42 +01001016int register_vmap_purge_notifier(struct notifier_block *nb)
1017{
1018 return blocking_notifier_chain_register(&vmap_notify_list, nb);
1019}
1020EXPORT_SYMBOL_GPL(register_vmap_purge_notifier);
1021
1022int unregister_vmap_purge_notifier(struct notifier_block *nb)
1023{
1024 return blocking_notifier_chain_unregister(&vmap_notify_list, nb);
1025}
1026EXPORT_SYMBOL_GPL(unregister_vmap_purge_notifier);
1027
Nick Piggindb64fe02008-10-18 20:27:03 -07001028static void __free_vmap_area(struct vmap_area *va)
1029{
1030 BUG_ON(RB_EMPTY_NODE(&va->rb_node));
Nick Piggin89699602011-03-22 16:30:36 -07001031
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001032 /*
1033 * Remove from the busy tree/list.
1034 */
1035 unlink_va(va, &vmap_area_root);
Nick Piggindb64fe02008-10-18 20:27:03 -07001036
Tejun Heoca23e402009-08-14 15:00:52 +09001037 /*
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001038 * Merge VA with its neighbors, otherwise just add it.
Tejun Heoca23e402009-08-14 15:00:52 +09001039 */
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001040 merge_or_add_vmap_area(va,
1041 &free_vmap_area_root, &free_vmap_area_list);
Nick Piggindb64fe02008-10-18 20:27:03 -07001042}
1043
1044/*
1045 * Free a region of KVA allocated by alloc_vmap_area
1046 */
1047static void free_vmap_area(struct vmap_area *va)
1048{
1049 spin_lock(&vmap_area_lock);
1050 __free_vmap_area(va);
1051 spin_unlock(&vmap_area_lock);
1052}
1053
1054/*
1055 * Clear the pagetable entries of a given vmap_area
1056 */
1057static void unmap_vmap_area(struct vmap_area *va)
1058{
1059 vunmap_page_range(va->va_start, va->va_end);
1060}
1061
1062/*
1063 * lazy_max_pages is the maximum amount of virtual address space we gather up
1064 * before attempting to purge with a TLB flush.
1065 *
1066 * There is a tradeoff here: a larger number will cover more kernel page tables
1067 * and take slightly longer to purge, but it will linearly reduce the number of
1068 * global TLB flushes that must be performed. It would seem natural to scale
1069 * this number up linearly with the number of CPUs (because vmapping activity
1070 * could also scale linearly with the number of CPUs), however it is likely
1071 * that in practice, workloads might be constrained in other ways that mean
1072 * vmap activity will not scale linearly with CPUs. Also, I want to be
1073 * conservative and not introduce a big latency on huge systems, so go with
1074 * a less aggressive log scale. It will still be an improvement over the old
1075 * code, and it will be simple to change the scale factor if we find that it
1076 * becomes a problem on bigger systems.
1077 */
1078static unsigned long lazy_max_pages(void)
1079{
1080 unsigned int log;
1081
1082 log = fls(num_online_cpus());
1083
1084 return log * (32UL * 1024 * 1024 / PAGE_SIZE);
1085}
1086
Uladzislau Rezki (Sony)4d36e6f2019-05-14 15:41:25 -07001087static atomic_long_t vmap_lazy_nr = ATOMIC_LONG_INIT(0);
Nick Piggindb64fe02008-10-18 20:27:03 -07001088
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001089/*
1090 * Serialize vmap purging. There is no actual criticial section protected
1091 * by this look, but we want to avoid concurrent calls for performance
1092 * reasons and to make the pcpu_get_vm_areas more deterministic.
1093 */
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001094static DEFINE_MUTEX(vmap_purge_lock);
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001095
Nick Piggin02b709d2010-02-01 22:25:57 +11001096/* for per-CPU blocks */
1097static void purge_fragmented_blocks_allcpus(void);
1098
Nick Piggindb64fe02008-10-18 20:27:03 -07001099/*
Cliff Wickman3ee48b62010-09-16 11:44:02 -05001100 * called before a call to iounmap() if the caller wants vm_area_struct's
1101 * immediately freed.
1102 */
1103void set_iounmap_nonlazy(void)
1104{
Uladzislau Rezki (Sony)4d36e6f2019-05-14 15:41:25 -07001105 atomic_long_set(&vmap_lazy_nr, lazy_max_pages()+1);
Cliff Wickman3ee48b62010-09-16 11:44:02 -05001106}
1107
1108/*
Nick Piggindb64fe02008-10-18 20:27:03 -07001109 * Purges all lazily-freed vmap areas.
Nick Piggindb64fe02008-10-18 20:27:03 -07001110 */
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001111static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end)
Nick Piggindb64fe02008-10-18 20:27:03 -07001112{
Uladzislau Rezki (Sony)4d36e6f2019-05-14 15:41:25 -07001113 unsigned long resched_threshold;
Chris Wilson80c4bd72016-05-20 16:57:38 -07001114 struct llist_node *valist;
Nick Piggindb64fe02008-10-18 20:27:03 -07001115 struct vmap_area *va;
Vegard Nossumcbb76672009-02-27 14:03:04 -08001116 struct vmap_area *n_va;
Nick Piggindb64fe02008-10-18 20:27:03 -07001117
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001118 lockdep_assert_held(&vmap_purge_lock);
Nick Piggin02b709d2010-02-01 22:25:57 +11001119
Chris Wilson80c4bd72016-05-20 16:57:38 -07001120 valist = llist_del_all(&vmap_purge_list);
Uladzislau Rezki (Sony)68571be92019-05-14 15:41:22 -07001121 if (unlikely(valist == NULL))
1122 return false;
1123
1124 /*
1125 * TODO: to calculate a flush range without looping.
1126 * The list can be up to lazy_max_pages() elements.
1127 */
Chris Wilson80c4bd72016-05-20 16:57:38 -07001128 llist_for_each_entry(va, valist, purge_list) {
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001129 if (va->va_start < start)
1130 start = va->va_start;
1131 if (va->va_end > end)
1132 end = va->va_end;
Nick Piggindb64fe02008-10-18 20:27:03 -07001133 }
Nick Piggindb64fe02008-10-18 20:27:03 -07001134
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001135 flush_tlb_kernel_range(start, end);
Uladzislau Rezki (Sony)4d36e6f2019-05-14 15:41:25 -07001136 resched_threshold = lazy_max_pages() << 1;
Nick Piggindb64fe02008-10-18 20:27:03 -07001137
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001138 spin_lock(&vmap_area_lock);
Joel Fernandes763b2182016-12-12 16:44:26 -08001139 llist_for_each_entry_safe(va, n_va, valist, purge_list) {
Uladzislau Rezki (Sony)4d36e6f2019-05-14 15:41:25 -07001140 unsigned long nr = (va->va_end - va->va_start) >> PAGE_SHIFT;
Joel Fernandes763b2182016-12-12 16:44:26 -08001141
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001142 __free_vmap_area(va);
Uladzislau Rezki (Sony)4d36e6f2019-05-14 15:41:25 -07001143 atomic_long_sub(nr, &vmap_lazy_nr);
Uladzislau Rezki (Sony)68571be92019-05-14 15:41:22 -07001144
Uladzislau Rezki (Sony)4d36e6f2019-05-14 15:41:25 -07001145 if (atomic_long_read(&vmap_lazy_nr) < resched_threshold)
Uladzislau Rezki (Sony)68571be92019-05-14 15:41:22 -07001146 cond_resched_lock(&vmap_area_lock);
Joel Fernandes763b2182016-12-12 16:44:26 -08001147 }
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001148 spin_unlock(&vmap_area_lock);
1149 return true;
Nick Piggindb64fe02008-10-18 20:27:03 -07001150}
1151
1152/*
Nick Piggin496850e2008-11-19 15:36:33 -08001153 * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
1154 * is already purging.
1155 */
1156static void try_purge_vmap_area_lazy(void)
1157{
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001158 if (mutex_trylock(&vmap_purge_lock)) {
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001159 __purge_vmap_area_lazy(ULONG_MAX, 0);
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001160 mutex_unlock(&vmap_purge_lock);
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001161 }
Nick Piggin496850e2008-11-19 15:36:33 -08001162}
1163
1164/*
Nick Piggindb64fe02008-10-18 20:27:03 -07001165 * Kick off a purge of the outstanding lazy areas.
1166 */
1167static void purge_vmap_area_lazy(void)
1168{
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001169 mutex_lock(&vmap_purge_lock);
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001170 purge_fragmented_blocks_allcpus();
1171 __purge_vmap_area_lazy(ULONG_MAX, 0);
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001172 mutex_unlock(&vmap_purge_lock);
Nick Piggindb64fe02008-10-18 20:27:03 -07001173}
1174
1175/*
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -08001176 * Free a vmap area, caller ensuring that the area has been unmapped
1177 * and flush_cache_vunmap had been called for the correct range
1178 * previously.
Nick Piggindb64fe02008-10-18 20:27:03 -07001179 */
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -08001180static void free_vmap_area_noflush(struct vmap_area *va)
Nick Piggindb64fe02008-10-18 20:27:03 -07001181{
Uladzislau Rezki (Sony)4d36e6f2019-05-14 15:41:25 -07001182 unsigned long nr_lazy;
Chris Wilson80c4bd72016-05-20 16:57:38 -07001183
Uladzislau Rezki (Sony)4d36e6f2019-05-14 15:41:25 -07001184 nr_lazy = atomic_long_add_return((va->va_end - va->va_start) >>
1185 PAGE_SHIFT, &vmap_lazy_nr);
Chris Wilson80c4bd72016-05-20 16:57:38 -07001186
1187 /* After this point, we may free va at any time */
1188 llist_add(&va->purge_list, &vmap_purge_list);
1189
1190 if (unlikely(nr_lazy > lazy_max_pages()))
Nick Piggin496850e2008-11-19 15:36:33 -08001191 try_purge_vmap_area_lazy();
Nick Piggindb64fe02008-10-18 20:27:03 -07001192}
1193
Nick Pigginb29acbd2008-12-01 13:13:47 -08001194/*
1195 * Free and unmap a vmap area
1196 */
1197static void free_unmap_vmap_area(struct vmap_area *va)
1198{
1199 flush_cache_vunmap(va->va_start, va->va_end);
Christoph Hellwigc8eef012016-12-12 16:44:01 -08001200 unmap_vmap_area(va);
Chintan Pandya82a2e922018-06-07 17:06:46 -07001201 if (debug_pagealloc_enabled())
1202 flush_tlb_kernel_range(va->va_start, va->va_end);
1203
Christoph Hellwigc8eef012016-12-12 16:44:01 -08001204 free_vmap_area_noflush(va);
Nick Pigginb29acbd2008-12-01 13:13:47 -08001205}
1206
Nick Piggindb64fe02008-10-18 20:27:03 -07001207static struct vmap_area *find_vmap_area(unsigned long addr)
1208{
1209 struct vmap_area *va;
1210
1211 spin_lock(&vmap_area_lock);
1212 va = __find_vmap_area(addr);
1213 spin_unlock(&vmap_area_lock);
1214
1215 return va;
1216}
1217
Nick Piggindb64fe02008-10-18 20:27:03 -07001218/*** Per cpu kva allocator ***/
1219
1220/*
1221 * vmap space is limited especially on 32 bit architectures. Ensure there is
1222 * room for at least 16 percpu vmap blocks per CPU.
1223 */
1224/*
1225 * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
1226 * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess
1227 * instead (we just need a rough idea)
1228 */
1229#if BITS_PER_LONG == 32
1230#define VMALLOC_SPACE (128UL*1024*1024)
1231#else
1232#define VMALLOC_SPACE (128UL*1024*1024*1024)
1233#endif
1234
1235#define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE)
1236#define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */
1237#define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */
1238#define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2)
1239#define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */
1240#define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */
Clemens Ladischf982f9152011-06-21 22:09:50 +02001241#define VMAP_BBMAP_BITS \
1242 VMAP_MIN(VMAP_BBMAP_BITS_MAX, \
1243 VMAP_MAX(VMAP_BBMAP_BITS_MIN, \
1244 VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16))
Nick Piggindb64fe02008-10-18 20:27:03 -07001245
1246#define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE)
1247
1248struct vmap_block_queue {
1249 spinlock_t lock;
1250 struct list_head free;
Nick Piggindb64fe02008-10-18 20:27:03 -07001251};
1252
1253struct vmap_block {
1254 spinlock_t lock;
1255 struct vmap_area *va;
Nick Piggindb64fe02008-10-18 20:27:03 -07001256 unsigned long free, dirty;
Roman Pen7d61bfe2015-04-15 16:13:55 -07001257 unsigned long dirty_min, dirty_max; /*< dirty range */
Nick Pigginde560422010-02-01 22:24:18 +11001258 struct list_head free_list;
1259 struct rcu_head rcu_head;
Nick Piggin02b709d2010-02-01 22:25:57 +11001260 struct list_head purge;
Nick Piggindb64fe02008-10-18 20:27:03 -07001261};
1262
1263/* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
1264static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
1265
1266/*
1267 * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block
1268 * in the free path. Could get rid of this if we change the API to return a
1269 * "cookie" from alloc, to be passed to free. But no big deal yet.
1270 */
1271static DEFINE_SPINLOCK(vmap_block_tree_lock);
1272static RADIX_TREE(vmap_block_tree, GFP_ATOMIC);
1273
1274/*
1275 * We should probably have a fallback mechanism to allocate virtual memory
1276 * out of partially filled vmap blocks. However vmap block sizing should be
1277 * fairly reasonable according to the vmalloc size, so it shouldn't be a
1278 * big problem.
1279 */
1280
1281static unsigned long addr_to_vb_idx(unsigned long addr)
1282{
1283 addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
1284 addr /= VMAP_BLOCK_SIZE;
1285 return addr;
1286}
1287
Roman Pencf725ce2015-04-15 16:13:52 -07001288static void *vmap_block_vaddr(unsigned long va_start, unsigned long pages_off)
1289{
1290 unsigned long addr;
1291
1292 addr = va_start + (pages_off << PAGE_SHIFT);
1293 BUG_ON(addr_to_vb_idx(addr) != addr_to_vb_idx(va_start));
1294 return (void *)addr;
1295}
1296
1297/**
1298 * new_vmap_block - allocates new vmap_block and occupies 2^order pages in this
1299 * block. Of course pages number can't exceed VMAP_BBMAP_BITS
1300 * @order: how many 2^order pages should be occupied in newly allocated block
1301 * @gfp_mask: flags for the page level allocator
1302 *
Mike Rapoporta862f682019-03-05 15:48:42 -08001303 * Return: virtual address in a newly allocated block or ERR_PTR(-errno)
Roman Pencf725ce2015-04-15 16:13:52 -07001304 */
1305static void *new_vmap_block(unsigned int order, gfp_t gfp_mask)
Nick Piggindb64fe02008-10-18 20:27:03 -07001306{
1307 struct vmap_block_queue *vbq;
1308 struct vmap_block *vb;
1309 struct vmap_area *va;
1310 unsigned long vb_idx;
1311 int node, err;
Roman Pencf725ce2015-04-15 16:13:52 -07001312 void *vaddr;
Nick Piggindb64fe02008-10-18 20:27:03 -07001313
1314 node = numa_node_id();
1315
1316 vb = kmalloc_node(sizeof(struct vmap_block),
1317 gfp_mask & GFP_RECLAIM_MASK, node);
1318 if (unlikely(!vb))
1319 return ERR_PTR(-ENOMEM);
1320
1321 va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
1322 VMALLOC_START, VMALLOC_END,
1323 node, gfp_mask);
Tobias Klauserddf9c6d42011-01-13 15:46:15 -08001324 if (IS_ERR(va)) {
Nick Piggindb64fe02008-10-18 20:27:03 -07001325 kfree(vb);
Julia Lawalle7d86342010-08-09 17:18:28 -07001326 return ERR_CAST(va);
Nick Piggindb64fe02008-10-18 20:27:03 -07001327 }
1328
1329 err = radix_tree_preload(gfp_mask);
1330 if (unlikely(err)) {
1331 kfree(vb);
1332 free_vmap_area(va);
1333 return ERR_PTR(err);
1334 }
1335
Roman Pencf725ce2015-04-15 16:13:52 -07001336 vaddr = vmap_block_vaddr(va->va_start, 0);
Nick Piggindb64fe02008-10-18 20:27:03 -07001337 spin_lock_init(&vb->lock);
1338 vb->va = va;
Roman Pencf725ce2015-04-15 16:13:52 -07001339 /* At least something should be left free */
1340 BUG_ON(VMAP_BBMAP_BITS <= (1UL << order));
1341 vb->free = VMAP_BBMAP_BITS - (1UL << order);
Nick Piggindb64fe02008-10-18 20:27:03 -07001342 vb->dirty = 0;
Roman Pen7d61bfe2015-04-15 16:13:55 -07001343 vb->dirty_min = VMAP_BBMAP_BITS;
1344 vb->dirty_max = 0;
Nick Piggindb64fe02008-10-18 20:27:03 -07001345 INIT_LIST_HEAD(&vb->free_list);
Nick Piggindb64fe02008-10-18 20:27:03 -07001346
1347 vb_idx = addr_to_vb_idx(va->va_start);
1348 spin_lock(&vmap_block_tree_lock);
1349 err = radix_tree_insert(&vmap_block_tree, vb_idx, vb);
1350 spin_unlock(&vmap_block_tree_lock);
1351 BUG_ON(err);
1352 radix_tree_preload_end();
1353
1354 vbq = &get_cpu_var(vmap_block_queue);
Nick Piggindb64fe02008-10-18 20:27:03 -07001355 spin_lock(&vbq->lock);
Roman Pen68ac5462015-04-15 16:13:48 -07001356 list_add_tail_rcu(&vb->free_list, &vbq->free);
Nick Piggindb64fe02008-10-18 20:27:03 -07001357 spin_unlock(&vbq->lock);
Tejun Heo3f04ba82009-10-29 22:34:12 +09001358 put_cpu_var(vmap_block_queue);
Nick Piggindb64fe02008-10-18 20:27:03 -07001359
Roman Pencf725ce2015-04-15 16:13:52 -07001360 return vaddr;
Nick Piggindb64fe02008-10-18 20:27:03 -07001361}
1362
Nick Piggindb64fe02008-10-18 20:27:03 -07001363static void free_vmap_block(struct vmap_block *vb)
1364{
1365 struct vmap_block *tmp;
1366 unsigned long vb_idx;
1367
Nick Piggindb64fe02008-10-18 20:27:03 -07001368 vb_idx = addr_to_vb_idx(vb->va->va_start);
1369 spin_lock(&vmap_block_tree_lock);
1370 tmp = radix_tree_delete(&vmap_block_tree, vb_idx);
1371 spin_unlock(&vmap_block_tree_lock);
1372 BUG_ON(tmp != vb);
1373
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -08001374 free_vmap_area_noflush(vb->va);
Lai Jiangshan22a3c7d2011-03-18 12:13:08 +08001375 kfree_rcu(vb, rcu_head);
Nick Piggindb64fe02008-10-18 20:27:03 -07001376}
1377
Nick Piggin02b709d2010-02-01 22:25:57 +11001378static void purge_fragmented_blocks(int cpu)
1379{
1380 LIST_HEAD(purge);
1381 struct vmap_block *vb;
1382 struct vmap_block *n_vb;
1383 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
1384
1385 rcu_read_lock();
1386 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
1387
1388 if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS))
1389 continue;
1390
1391 spin_lock(&vb->lock);
1392 if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) {
1393 vb->free = 0; /* prevent further allocs after releasing lock */
1394 vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */
Roman Pen7d61bfe2015-04-15 16:13:55 -07001395 vb->dirty_min = 0;
1396 vb->dirty_max = VMAP_BBMAP_BITS;
Nick Piggin02b709d2010-02-01 22:25:57 +11001397 spin_lock(&vbq->lock);
1398 list_del_rcu(&vb->free_list);
1399 spin_unlock(&vbq->lock);
1400 spin_unlock(&vb->lock);
1401 list_add_tail(&vb->purge, &purge);
1402 } else
1403 spin_unlock(&vb->lock);
1404 }
1405 rcu_read_unlock();
1406
1407 list_for_each_entry_safe(vb, n_vb, &purge, purge) {
1408 list_del(&vb->purge);
1409 free_vmap_block(vb);
1410 }
1411}
1412
Nick Piggin02b709d2010-02-01 22:25:57 +11001413static void purge_fragmented_blocks_allcpus(void)
1414{
1415 int cpu;
1416
1417 for_each_possible_cpu(cpu)
1418 purge_fragmented_blocks(cpu);
1419}
1420
Nick Piggindb64fe02008-10-18 20:27:03 -07001421static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
1422{
1423 struct vmap_block_queue *vbq;
1424 struct vmap_block *vb;
Roman Pencf725ce2015-04-15 16:13:52 -07001425 void *vaddr = NULL;
Nick Piggindb64fe02008-10-18 20:27:03 -07001426 unsigned int order;
1427
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08001428 BUG_ON(offset_in_page(size));
Nick Piggindb64fe02008-10-18 20:27:03 -07001429 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
Jan Karaaa91c4d2012-07-31 16:41:37 -07001430 if (WARN_ON(size == 0)) {
1431 /*
1432 * Allocating 0 bytes isn't what caller wants since
1433 * get_order(0) returns funny result. Just warn and terminate
1434 * early.
1435 */
1436 return NULL;
1437 }
Nick Piggindb64fe02008-10-18 20:27:03 -07001438 order = get_order(size);
1439
Nick Piggindb64fe02008-10-18 20:27:03 -07001440 rcu_read_lock();
1441 vbq = &get_cpu_var(vmap_block_queue);
1442 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
Roman Pencf725ce2015-04-15 16:13:52 -07001443 unsigned long pages_off;
Nick Piggindb64fe02008-10-18 20:27:03 -07001444
1445 spin_lock(&vb->lock);
Roman Pencf725ce2015-04-15 16:13:52 -07001446 if (vb->free < (1UL << order)) {
1447 spin_unlock(&vb->lock);
1448 continue;
1449 }
Nick Piggin02b709d2010-02-01 22:25:57 +11001450
Roman Pencf725ce2015-04-15 16:13:52 -07001451 pages_off = VMAP_BBMAP_BITS - vb->free;
1452 vaddr = vmap_block_vaddr(vb->va->va_start, pages_off);
Nick Piggin02b709d2010-02-01 22:25:57 +11001453 vb->free -= 1UL << order;
1454 if (vb->free == 0) {
1455 spin_lock(&vbq->lock);
1456 list_del_rcu(&vb->free_list);
1457 spin_unlock(&vbq->lock);
Nick Piggindb64fe02008-10-18 20:27:03 -07001458 }
Roman Pencf725ce2015-04-15 16:13:52 -07001459
Nick Piggindb64fe02008-10-18 20:27:03 -07001460 spin_unlock(&vb->lock);
Nick Piggin02b709d2010-02-01 22:25:57 +11001461 break;
Nick Piggindb64fe02008-10-18 20:27:03 -07001462 }
Nick Piggin02b709d2010-02-01 22:25:57 +11001463
Tejun Heo3f04ba82009-10-29 22:34:12 +09001464 put_cpu_var(vmap_block_queue);
Nick Piggindb64fe02008-10-18 20:27:03 -07001465 rcu_read_unlock();
1466
Roman Pencf725ce2015-04-15 16:13:52 -07001467 /* Allocate new block if nothing was found */
1468 if (!vaddr)
1469 vaddr = new_vmap_block(order, gfp_mask);
Nick Piggindb64fe02008-10-18 20:27:03 -07001470
Roman Pencf725ce2015-04-15 16:13:52 -07001471 return vaddr;
Nick Piggindb64fe02008-10-18 20:27:03 -07001472}
1473
1474static void vb_free(const void *addr, unsigned long size)
1475{
1476 unsigned long offset;
1477 unsigned long vb_idx;
1478 unsigned int order;
1479 struct vmap_block *vb;
1480
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08001481 BUG_ON(offset_in_page(size));
Nick Piggindb64fe02008-10-18 20:27:03 -07001482 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
Nick Pigginb29acbd2008-12-01 13:13:47 -08001483
1484 flush_cache_vunmap((unsigned long)addr, (unsigned long)addr + size);
1485
Nick Piggindb64fe02008-10-18 20:27:03 -07001486 order = get_order(size);
1487
1488 offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1);
Roman Pen7d61bfe2015-04-15 16:13:55 -07001489 offset >>= PAGE_SHIFT;
Nick Piggindb64fe02008-10-18 20:27:03 -07001490
1491 vb_idx = addr_to_vb_idx((unsigned long)addr);
1492 rcu_read_lock();
1493 vb = radix_tree_lookup(&vmap_block_tree, vb_idx);
1494 rcu_read_unlock();
1495 BUG_ON(!vb);
1496
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -08001497 vunmap_page_range((unsigned long)addr, (unsigned long)addr + size);
1498
Chintan Pandya82a2e922018-06-07 17:06:46 -07001499 if (debug_pagealloc_enabled())
1500 flush_tlb_kernel_range((unsigned long)addr,
1501 (unsigned long)addr + size);
1502
Nick Piggindb64fe02008-10-18 20:27:03 -07001503 spin_lock(&vb->lock);
Roman Pen7d61bfe2015-04-15 16:13:55 -07001504
1505 /* Expand dirty range */
1506 vb->dirty_min = min(vb->dirty_min, offset);
1507 vb->dirty_max = max(vb->dirty_max, offset + (1UL << order));
MinChan Kimd0868172009-03-31 15:19:26 -07001508
Nick Piggindb64fe02008-10-18 20:27:03 -07001509 vb->dirty += 1UL << order;
1510 if (vb->dirty == VMAP_BBMAP_BITS) {
Nick Pigginde560422010-02-01 22:24:18 +11001511 BUG_ON(vb->free);
Nick Piggindb64fe02008-10-18 20:27:03 -07001512 spin_unlock(&vb->lock);
1513 free_vmap_block(vb);
1514 } else
1515 spin_unlock(&vb->lock);
1516}
1517
Rick Edgecombe868b1042019-04-25 17:11:36 -07001518static void _vm_unmap_aliases(unsigned long start, unsigned long end, int flush)
Nick Piggindb64fe02008-10-18 20:27:03 -07001519{
Nick Piggindb64fe02008-10-18 20:27:03 -07001520 int cpu;
Nick Piggindb64fe02008-10-18 20:27:03 -07001521
Jeremy Fitzhardinge9b463332008-10-28 19:22:34 +11001522 if (unlikely(!vmap_initialized))
1523 return;
1524
Christoph Hellwig5803ed22016-12-12 16:44:20 -08001525 might_sleep();
1526
Nick Piggindb64fe02008-10-18 20:27:03 -07001527 for_each_possible_cpu(cpu) {
1528 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
1529 struct vmap_block *vb;
1530
1531 rcu_read_lock();
1532 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
Nick Piggindb64fe02008-10-18 20:27:03 -07001533 spin_lock(&vb->lock);
Roman Pen7d61bfe2015-04-15 16:13:55 -07001534 if (vb->dirty) {
1535 unsigned long va_start = vb->va->va_start;
Nick Piggindb64fe02008-10-18 20:27:03 -07001536 unsigned long s, e;
Joonsoo Kimb136be5e2013-09-11 14:21:40 -07001537
Roman Pen7d61bfe2015-04-15 16:13:55 -07001538 s = va_start + (vb->dirty_min << PAGE_SHIFT);
1539 e = va_start + (vb->dirty_max << PAGE_SHIFT);
Nick Piggindb64fe02008-10-18 20:27:03 -07001540
Roman Pen7d61bfe2015-04-15 16:13:55 -07001541 start = min(s, start);
1542 end = max(e, end);
1543
Nick Piggindb64fe02008-10-18 20:27:03 -07001544 flush = 1;
Nick Piggindb64fe02008-10-18 20:27:03 -07001545 }
1546 spin_unlock(&vb->lock);
1547 }
1548 rcu_read_unlock();
1549 }
1550
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001551 mutex_lock(&vmap_purge_lock);
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001552 purge_fragmented_blocks_allcpus();
1553 if (!__purge_vmap_area_lazy(start, end) && flush)
1554 flush_tlb_kernel_range(start, end);
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001555 mutex_unlock(&vmap_purge_lock);
Nick Piggindb64fe02008-10-18 20:27:03 -07001556}
Rick Edgecombe868b1042019-04-25 17:11:36 -07001557
1558/**
1559 * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
1560 *
1561 * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
1562 * to amortize TLB flushing overheads. What this means is that any page you
1563 * have now, may, in a former life, have been mapped into kernel virtual
1564 * address by the vmap layer and so there might be some CPUs with TLB entries
1565 * still referencing that page (additional to the regular 1:1 kernel mapping).
1566 *
1567 * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
1568 * be sure that none of the pages we have control over will have any aliases
1569 * from the vmap layer.
1570 */
1571void vm_unmap_aliases(void)
1572{
1573 unsigned long start = ULONG_MAX, end = 0;
1574 int flush = 0;
1575
1576 _vm_unmap_aliases(start, end, flush);
1577}
Nick Piggindb64fe02008-10-18 20:27:03 -07001578EXPORT_SYMBOL_GPL(vm_unmap_aliases);
1579
1580/**
1581 * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
1582 * @mem: the pointer returned by vm_map_ram
1583 * @count: the count passed to that vm_map_ram call (cannot unmap partial)
1584 */
1585void vm_unmap_ram(const void *mem, unsigned int count)
1586{
Guillermo Julián Moreno65ee03c2016-06-03 14:55:33 -07001587 unsigned long size = (unsigned long)count << PAGE_SHIFT;
Nick Piggindb64fe02008-10-18 20:27:03 -07001588 unsigned long addr = (unsigned long)mem;
Christoph Hellwig9c3acf62016-12-12 16:44:04 -08001589 struct vmap_area *va;
Nick Piggindb64fe02008-10-18 20:27:03 -07001590
Christoph Hellwig5803ed22016-12-12 16:44:20 -08001591 might_sleep();
Nick Piggindb64fe02008-10-18 20:27:03 -07001592 BUG_ON(!addr);
1593 BUG_ON(addr < VMALLOC_START);
1594 BUG_ON(addr > VMALLOC_END);
Shawn Lina1c0b1a2016-03-17 14:20:37 -07001595 BUG_ON(!PAGE_ALIGNED(addr));
Nick Piggindb64fe02008-10-18 20:27:03 -07001596
Christoph Hellwig9c3acf62016-12-12 16:44:04 -08001597 if (likely(count <= VMAP_MAX_ALLOC)) {
Chintan Pandya05e3ff92018-06-07 17:06:53 -07001598 debug_check_no_locks_freed(mem, size);
Nick Piggindb64fe02008-10-18 20:27:03 -07001599 vb_free(mem, size);
Christoph Hellwig9c3acf62016-12-12 16:44:04 -08001600 return;
1601 }
1602
1603 va = find_vmap_area(addr);
1604 BUG_ON(!va);
Chintan Pandya05e3ff92018-06-07 17:06:53 -07001605 debug_check_no_locks_freed((void *)va->va_start,
1606 (va->va_end - va->va_start));
Christoph Hellwig9c3acf62016-12-12 16:44:04 -08001607 free_unmap_vmap_area(va);
Nick Piggindb64fe02008-10-18 20:27:03 -07001608}
1609EXPORT_SYMBOL(vm_unmap_ram);
1610
1611/**
1612 * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
1613 * @pages: an array of pointers to the pages to be mapped
1614 * @count: number of pages
1615 * @node: prefer to allocate data structures on this node
1616 * @prot: memory protection to use. PAGE_KERNEL for regular RAM
Randy Dunlape99c97a2008-10-29 14:01:09 -07001617 *
Gioh Kim36437632014-04-07 15:37:37 -07001618 * If you use this function for less than VMAP_MAX_ALLOC pages, it could be
1619 * faster than vmap so it's good. But if you mix long-life and short-life
1620 * objects with vm_map_ram(), it could consume lots of address space through
1621 * fragmentation (especially on a 32bit machine). You could see failures in
1622 * the end. Please use this function for short-lived objects.
1623 *
Randy Dunlape99c97a2008-10-29 14:01:09 -07001624 * Returns: a pointer to the address that has been mapped, or %NULL on failure
Nick Piggindb64fe02008-10-18 20:27:03 -07001625 */
1626void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
1627{
Guillermo Julián Moreno65ee03c2016-06-03 14:55:33 -07001628 unsigned long size = (unsigned long)count << PAGE_SHIFT;
Nick Piggindb64fe02008-10-18 20:27:03 -07001629 unsigned long addr;
1630 void *mem;
1631
1632 if (likely(count <= VMAP_MAX_ALLOC)) {
1633 mem = vb_alloc(size, GFP_KERNEL);
1634 if (IS_ERR(mem))
1635 return NULL;
1636 addr = (unsigned long)mem;
1637 } else {
1638 struct vmap_area *va;
1639 va = alloc_vmap_area(size, PAGE_SIZE,
1640 VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
1641 if (IS_ERR(va))
1642 return NULL;
1643
1644 addr = va->va_start;
1645 mem = (void *)addr;
1646 }
1647 if (vmap_page_range(addr, addr + size, prot, pages) < 0) {
1648 vm_unmap_ram(mem, count);
1649 return NULL;
1650 }
1651 return mem;
1652}
1653EXPORT_SYMBOL(vm_map_ram);
1654
Joonsoo Kim4341fa42013-04-29 15:07:39 -07001655static struct vm_struct *vmlist __initdata;
Mike Rapoport92eac162019-03-05 15:48:36 -08001656
Tejun Heof0aa6612009-02-20 16:29:08 +09001657/**
Nicolas Pitrebe9b7332011-08-25 00:24:21 -04001658 * vm_area_add_early - add vmap area early during boot
1659 * @vm: vm_struct to add
1660 *
1661 * This function is used to add fixed kernel vm area to vmlist before
1662 * vmalloc_init() is called. @vm->addr, @vm->size, and @vm->flags
1663 * should contain proper values and the other fields should be zero.
1664 *
1665 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1666 */
1667void __init vm_area_add_early(struct vm_struct *vm)
1668{
1669 struct vm_struct *tmp, **p;
1670
1671 BUG_ON(vmap_initialized);
1672 for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
1673 if (tmp->addr >= vm->addr) {
1674 BUG_ON(tmp->addr < vm->addr + vm->size);
1675 break;
1676 } else
1677 BUG_ON(tmp->addr + tmp->size > vm->addr);
1678 }
1679 vm->next = *p;
1680 *p = vm;
1681}
1682
1683/**
Tejun Heof0aa6612009-02-20 16:29:08 +09001684 * vm_area_register_early - register vmap area early during boot
1685 * @vm: vm_struct to register
Tejun Heoc0c0a292009-02-24 11:57:21 +09001686 * @align: requested alignment
Tejun Heof0aa6612009-02-20 16:29:08 +09001687 *
1688 * This function is used to register kernel vm area before
1689 * vmalloc_init() is called. @vm->size and @vm->flags should contain
1690 * proper values on entry and other fields should be zero. On return,
1691 * vm->addr contains the allocated address.
1692 *
1693 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1694 */
Tejun Heoc0c0a292009-02-24 11:57:21 +09001695void __init vm_area_register_early(struct vm_struct *vm, size_t align)
Tejun Heof0aa6612009-02-20 16:29:08 +09001696{
1697 static size_t vm_init_off __initdata;
Tejun Heoc0c0a292009-02-24 11:57:21 +09001698 unsigned long addr;
Tejun Heof0aa6612009-02-20 16:29:08 +09001699
Tejun Heoc0c0a292009-02-24 11:57:21 +09001700 addr = ALIGN(VMALLOC_START + vm_init_off, align);
1701 vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START;
1702
1703 vm->addr = (void *)addr;
Tejun Heof0aa6612009-02-20 16:29:08 +09001704
Nicolas Pitrebe9b7332011-08-25 00:24:21 -04001705 vm_area_add_early(vm);
Tejun Heof0aa6612009-02-20 16:29:08 +09001706}
1707
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001708static void vmap_init_free_space(void)
1709{
1710 unsigned long vmap_start = 1;
1711 const unsigned long vmap_end = ULONG_MAX;
1712 struct vmap_area *busy, *free;
1713
1714 /*
1715 * B F B B B F
1716 * -|-----|.....|-----|-----|-----|.....|-
1717 * | The KVA space |
1718 * |<--------------------------------->|
1719 */
1720 list_for_each_entry(busy, &vmap_area_list, list) {
1721 if (busy->va_start - vmap_start > 0) {
1722 free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
1723 if (!WARN_ON_ONCE(!free)) {
1724 free->va_start = vmap_start;
1725 free->va_end = busy->va_start;
1726
1727 insert_vmap_area_augment(free, NULL,
1728 &free_vmap_area_root,
1729 &free_vmap_area_list);
1730 }
1731 }
1732
1733 vmap_start = busy->va_end;
1734 }
1735
1736 if (vmap_end - vmap_start > 0) {
1737 free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
1738 if (!WARN_ON_ONCE(!free)) {
1739 free->va_start = vmap_start;
1740 free->va_end = vmap_end;
1741
1742 insert_vmap_area_augment(free, NULL,
1743 &free_vmap_area_root,
1744 &free_vmap_area_list);
1745 }
1746 }
1747}
1748
Nick Piggindb64fe02008-10-18 20:27:03 -07001749void __init vmalloc_init(void)
1750{
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08001751 struct vmap_area *va;
1752 struct vm_struct *tmp;
Nick Piggindb64fe02008-10-18 20:27:03 -07001753 int i;
1754
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001755 /*
1756 * Create the cache for vmap_area objects.
1757 */
1758 vmap_area_cachep = KMEM_CACHE(vmap_area, SLAB_PANIC);
1759
Nick Piggindb64fe02008-10-18 20:27:03 -07001760 for_each_possible_cpu(i) {
1761 struct vmap_block_queue *vbq;
Al Viro32fcfd42013-03-10 20:14:08 -04001762 struct vfree_deferred *p;
Nick Piggindb64fe02008-10-18 20:27:03 -07001763
1764 vbq = &per_cpu(vmap_block_queue, i);
1765 spin_lock_init(&vbq->lock);
1766 INIT_LIST_HEAD(&vbq->free);
Al Viro32fcfd42013-03-10 20:14:08 -04001767 p = &per_cpu(vfree_deferred, i);
1768 init_llist_head(&p->list);
1769 INIT_WORK(&p->wq, free_work);
Nick Piggindb64fe02008-10-18 20:27:03 -07001770 }
Jeremy Fitzhardinge9b463332008-10-28 19:22:34 +11001771
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08001772 /* Import existing vmlist entries. */
1773 for (tmp = vmlist; tmp; tmp = tmp->next) {
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001774 va = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
1775 if (WARN_ON_ONCE(!va))
1776 continue;
1777
KyongHodbda5912012-05-29 15:06:49 -07001778 va->flags = VM_VM_AREA;
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08001779 va->va_start = (unsigned long)tmp->addr;
1780 va->va_end = va->va_start + tmp->size;
KyongHodbda5912012-05-29 15:06:49 -07001781 va->vm = tmp;
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001782 insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08001783 }
Tejun Heoca23e402009-08-14 15:00:52 +09001784
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07001785 /*
1786 * Now we can initialize a free vmap space.
1787 */
1788 vmap_init_free_space();
Jeremy Fitzhardinge9b463332008-10-28 19:22:34 +11001789 vmap_initialized = true;
Nick Piggindb64fe02008-10-18 20:27:03 -07001790}
1791
Tejun Heo8fc48982009-02-20 16:29:08 +09001792/**
1793 * map_kernel_range_noflush - map kernel VM area with the specified pages
1794 * @addr: start of the VM area to map
1795 * @size: size of the VM area to map
1796 * @prot: page protection flags to use
1797 * @pages: pages to map
1798 *
1799 * Map PFN_UP(@size) pages at @addr. The VM area @addr and @size
1800 * specify should have been allocated using get_vm_area() and its
1801 * friends.
1802 *
1803 * NOTE:
1804 * This function does NOT do any cache flushing. The caller is
1805 * responsible for calling flush_cache_vmap() on to-be-mapped areas
1806 * before calling this function.
1807 *
1808 * RETURNS:
1809 * The number of pages mapped on success, -errno on failure.
1810 */
1811int map_kernel_range_noflush(unsigned long addr, unsigned long size,
1812 pgprot_t prot, struct page **pages)
1813{
1814 return vmap_page_range_noflush(addr, addr + size, prot, pages);
1815}
1816
1817/**
1818 * unmap_kernel_range_noflush - unmap kernel VM area
1819 * @addr: start of the VM area to unmap
1820 * @size: size of the VM area to unmap
1821 *
1822 * Unmap PFN_UP(@size) pages at @addr. The VM area @addr and @size
1823 * specify should have been allocated using get_vm_area() and its
1824 * friends.
1825 *
1826 * NOTE:
1827 * This function does NOT do any cache flushing. The caller is
1828 * responsible for calling flush_cache_vunmap() on to-be-mapped areas
1829 * before calling this function and flush_tlb_kernel_range() after.
1830 */
1831void unmap_kernel_range_noflush(unsigned long addr, unsigned long size)
1832{
1833 vunmap_page_range(addr, addr + size);
1834}
Huang Ying81e88fd2011-01-12 14:44:55 +08001835EXPORT_SYMBOL_GPL(unmap_kernel_range_noflush);
Tejun Heo8fc48982009-02-20 16:29:08 +09001836
1837/**
1838 * unmap_kernel_range - unmap kernel VM area and flush cache and TLB
1839 * @addr: start of the VM area to unmap
1840 * @size: size of the VM area to unmap
1841 *
1842 * Similar to unmap_kernel_range_noflush() but flushes vcache before
1843 * the unmapping and tlb after.
1844 */
Nick Piggindb64fe02008-10-18 20:27:03 -07001845void unmap_kernel_range(unsigned long addr, unsigned long size)
1846{
1847 unsigned long end = addr + size;
Tejun Heof6fcba72009-02-20 15:38:48 -08001848
1849 flush_cache_vunmap(addr, end);
Nick Piggindb64fe02008-10-18 20:27:03 -07001850 vunmap_page_range(addr, end);
1851 flush_tlb_kernel_range(addr, end);
1852}
Minchan Kim93ef6d6c2014-06-04 16:11:09 -07001853EXPORT_SYMBOL_GPL(unmap_kernel_range);
Nick Piggindb64fe02008-10-18 20:27:03 -07001854
WANG Chaof6f8ed42014-08-06 16:06:58 -07001855int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page **pages)
Nick Piggindb64fe02008-10-18 20:27:03 -07001856{
1857 unsigned long addr = (unsigned long)area->addr;
Wanpeng Li762216a2013-09-11 14:22:42 -07001858 unsigned long end = addr + get_vm_area_size(area);
Nick Piggindb64fe02008-10-18 20:27:03 -07001859 int err;
1860
WANG Chaof6f8ed42014-08-06 16:06:58 -07001861 err = vmap_page_range(addr, end, prot, pages);
Nick Piggindb64fe02008-10-18 20:27:03 -07001862
WANG Chaof6f8ed42014-08-06 16:06:58 -07001863 return err > 0 ? 0 : err;
Nick Piggindb64fe02008-10-18 20:27:03 -07001864}
1865EXPORT_SYMBOL_GPL(map_vm_area);
1866
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001867static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
Marek Szyprowski5e6cafc2012-04-13 12:32:09 +02001868 unsigned long flags, const void *caller)
Tejun Heocf88c792009-08-14 15:00:52 +09001869{
Joonsoo Kimc69480a2013-04-29 15:07:30 -07001870 spin_lock(&vmap_area_lock);
Tejun Heocf88c792009-08-14 15:00:52 +09001871 vm->flags = flags;
1872 vm->addr = (void *)va->va_start;
1873 vm->size = va->va_end - va->va_start;
1874 vm->caller = caller;
Minchan Kimdb1aeca2012-01-10 15:08:39 -08001875 va->vm = vm;
Tejun Heocf88c792009-08-14 15:00:52 +09001876 va->flags |= VM_VM_AREA;
Joonsoo Kimc69480a2013-04-29 15:07:30 -07001877 spin_unlock(&vmap_area_lock);
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001878}
Tejun Heocf88c792009-08-14 15:00:52 +09001879
Zhang Yanfei20fc02b2013-07-08 15:59:58 -07001880static void clear_vm_uninitialized_flag(struct vm_struct *vm)
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001881{
Joonsoo Kimd4033af2013-04-29 15:07:35 -07001882 /*
Zhang Yanfei20fc02b2013-07-08 15:59:58 -07001883 * Before removing VM_UNINITIALIZED,
Joonsoo Kimd4033af2013-04-29 15:07:35 -07001884 * we should make sure that vm has proper values.
1885 * Pair with smp_rmb() in show_numa_info().
1886 */
1887 smp_wmb();
Zhang Yanfei20fc02b2013-07-08 15:59:58 -07001888 vm->flags &= ~VM_UNINITIALIZED;
Tejun Heocf88c792009-08-14 15:00:52 +09001889}
1890
Nick Piggindb64fe02008-10-18 20:27:03 -07001891static struct vm_struct *__get_vm_area_node(unsigned long size,
David Miller2dca6992009-09-21 12:22:34 -07001892 unsigned long align, unsigned long flags, unsigned long start,
Marek Szyprowski5e6cafc2012-04-13 12:32:09 +02001893 unsigned long end, int node, gfp_t gfp_mask, const void *caller)
Nick Piggindb64fe02008-10-18 20:27:03 -07001894{
Kautuk Consul00065262011-12-19 17:12:04 -08001895 struct vmap_area *va;
Nick Piggindb64fe02008-10-18 20:27:03 -07001896 struct vm_struct *area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Giridhar Pemmasani52fd24c2006-10-28 10:38:34 -07001898 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 size = PAGE_ALIGN(size);
OGAWA Hirofumi31be8302006-11-16 01:19:29 -08001900 if (unlikely(!size))
1901 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
zijun_hu252e5c62016-10-07 16:57:26 -07001903 if (flags & VM_IOREMAP)
1904 align = 1ul << clamp_t(int, get_count_order_long(size),
1905 PAGE_SHIFT, IOREMAP_MAX_ORDER);
1906
Tejun Heocf88c792009-08-14 15:00:52 +09001907 area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 if (unlikely(!area))
1909 return NULL;
1910
Andrey Ryabinin71394fe2015-02-13 14:40:03 -08001911 if (!(flags & VM_NO_GUARD))
1912 size += PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Nick Piggindb64fe02008-10-18 20:27:03 -07001914 va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
1915 if (IS_ERR(va)) {
1916 kfree(area);
1917 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Zhang Yanfeid82b1d82013-07-03 15:04:47 -07001920 setup_vmalloc_vm(area, va, flags, caller);
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001921
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 return area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}
1924
Christoph Lameter930fc452005-10-29 18:15:41 -07001925struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
1926 unsigned long start, unsigned long end)
1927{
David Rientjes00ef2d22013-02-22 16:35:36 -08001928 return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE,
1929 GFP_KERNEL, __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -07001930}
Rusty Russell5992b6d2007-07-19 01:49:21 -07001931EXPORT_SYMBOL_GPL(__get_vm_area);
Christoph Lameter930fc452005-10-29 18:15:41 -07001932
Benjamin Herrenschmidtc2968612009-02-18 14:48:12 -08001933struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
1934 unsigned long start, unsigned long end,
Marek Szyprowski5e6cafc2012-04-13 12:32:09 +02001935 const void *caller)
Benjamin Herrenschmidtc2968612009-02-18 14:48:12 -08001936{
David Rientjes00ef2d22013-02-22 16:35:36 -08001937 return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE,
1938 GFP_KERNEL, caller);
Benjamin Herrenschmidtc2968612009-02-18 14:48:12 -08001939}
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941/**
Mike Rapoport92eac162019-03-05 15:48:36 -08001942 * get_vm_area - reserve a contiguous kernel virtual area
1943 * @size: size of the area
1944 * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 *
Mike Rapoport92eac162019-03-05 15:48:36 -08001946 * Search an area of @size in the kernel virtual mapping area,
1947 * and reserved it for out purposes. Returns the area descriptor
1948 * on success or %NULL on failure.
Mike Rapoporta862f682019-03-05 15:48:42 -08001949 *
1950 * Return: the area descriptor on success or %NULL on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 */
1952struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
1953{
David Miller2dca6992009-09-21 12:22:34 -07001954 return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
David Rientjes00ef2d22013-02-22 16:35:36 -08001955 NUMA_NO_NODE, GFP_KERNEL,
1956 __builtin_return_address(0));
Christoph Lameter23016962008-04-28 02:12:42 -07001957}
1958
1959struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
Marek Szyprowski5e6cafc2012-04-13 12:32:09 +02001960 const void *caller)
Christoph Lameter23016962008-04-28 02:12:42 -07001961{
David Miller2dca6992009-09-21 12:22:34 -07001962 return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
David Rientjes00ef2d22013-02-22 16:35:36 -08001963 NUMA_NO_NODE, GFP_KERNEL, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964}
1965
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001966/**
Mike Rapoport92eac162019-03-05 15:48:36 -08001967 * find_vm_area - find a continuous kernel virtual area
1968 * @addr: base address
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001969 *
Mike Rapoport92eac162019-03-05 15:48:36 -08001970 * Search for the kernel VM area starting at @addr, and return it.
1971 * It is up to the caller to do all required locking to keep the returned
1972 * pointer valid.
Mike Rapoporta862f682019-03-05 15:48:42 -08001973 *
1974 * Return: pointer to the found area or %NULL on faulure
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001975 */
1976struct vm_struct *find_vm_area(const void *addr)
Nick Piggin83342312006-06-23 02:03:20 -07001977{
Nick Piggindb64fe02008-10-18 20:27:03 -07001978 struct vmap_area *va;
Nick Piggin83342312006-06-23 02:03:20 -07001979
Nick Piggindb64fe02008-10-18 20:27:03 -07001980 va = find_vmap_area((unsigned long)addr);
1981 if (va && va->flags & VM_VM_AREA)
Minchan Kimdb1aeca2012-01-10 15:08:39 -08001982 return va->vm;
Nick Piggin83342312006-06-23 02:03:20 -07001983
Andi Kleen7856dfe2005-05-20 14:27:57 -07001984 return NULL;
Andi Kleen7856dfe2005-05-20 14:27:57 -07001985}
1986
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987/**
Mike Rapoport92eac162019-03-05 15:48:36 -08001988 * remove_vm_area - find and remove a continuous kernel virtual area
1989 * @addr: base address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 *
Mike Rapoport92eac162019-03-05 15:48:36 -08001991 * Search for the kernel VM area starting at @addr, and remove it.
1992 * This function returns the found VM area, but using it is NOT safe
1993 * on SMP machines, except for its size or flags.
Mike Rapoporta862f682019-03-05 15:48:42 -08001994 *
1995 * Return: pointer to the found area or %NULL on faulure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -08001997struct vm_struct *remove_vm_area(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998{
Nick Piggindb64fe02008-10-18 20:27:03 -07001999 struct vmap_area *va;
2000
Christoph Hellwig5803ed22016-12-12 16:44:20 -08002001 might_sleep();
2002
Nick Piggindb64fe02008-10-18 20:27:03 -07002003 va = find_vmap_area((unsigned long)addr);
2004 if (va && va->flags & VM_VM_AREA) {
Minchan Kimdb1aeca2012-01-10 15:08:39 -08002005 struct vm_struct *vm = va->vm;
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07002006
Joonsoo Kimc69480a2013-04-29 15:07:30 -07002007 spin_lock(&vmap_area_lock);
2008 va->vm = NULL;
2009 va->flags &= ~VM_VM_AREA;
Yisheng Xie78c72742017-07-10 15:48:09 -07002010 va->flags |= VM_LAZY_FREE;
Joonsoo Kimc69480a2013-04-29 15:07:30 -07002011 spin_unlock(&vmap_area_lock);
2012
Andrey Ryabinina5af5aa2015-03-12 16:26:11 -07002013 kasan_free_shadow(vm);
KAMEZAWA Hiroyukidd32c272009-09-21 17:02:32 -07002014 free_unmap_vmap_area(va);
KAMEZAWA Hiroyukidd32c272009-09-21 17:02:32 -07002015
Nick Piggindb64fe02008-10-18 20:27:03 -07002016 return vm;
2017 }
2018 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019}
2020
Rick Edgecombe868b1042019-04-25 17:11:36 -07002021static inline void set_area_direct_map(const struct vm_struct *area,
2022 int (*set_direct_map)(struct page *page))
2023{
2024 int i;
2025
2026 for (i = 0; i < area->nr_pages; i++)
2027 if (page_address(area->pages[i]))
2028 set_direct_map(area->pages[i]);
2029}
2030
2031/* Handle removing and resetting vm mappings related to the vm_struct. */
2032static void vm_remove_mappings(struct vm_struct *area, int deallocate_pages)
2033{
2034 unsigned long addr = (unsigned long)area->addr;
2035 unsigned long start = ULONG_MAX, end = 0;
2036 int flush_reset = area->flags & VM_FLUSH_RESET_PERMS;
2037 int i;
2038
2039 /*
2040 * The below block can be removed when all architectures that have
2041 * direct map permissions also have set_direct_map_() implementations.
2042 * This is concerned with resetting the direct map any an vm alias with
2043 * execute permissions, without leaving a RW+X window.
2044 */
2045 if (flush_reset && !IS_ENABLED(CONFIG_ARCH_HAS_SET_DIRECT_MAP)) {
2046 set_memory_nx(addr, area->nr_pages);
2047 set_memory_rw(addr, area->nr_pages);
2048 }
2049
2050 remove_vm_area(area->addr);
2051
2052 /* If this is not VM_FLUSH_RESET_PERMS memory, no need for the below. */
2053 if (!flush_reset)
2054 return;
2055
2056 /*
2057 * If not deallocating pages, just do the flush of the VM area and
2058 * return.
2059 */
2060 if (!deallocate_pages) {
2061 vm_unmap_aliases();
2062 return;
2063 }
2064
2065 /*
2066 * If execution gets here, flush the vm mapping and reset the direct
2067 * map. Find the start and end range of the direct mappings to make sure
2068 * the vm_unmap_aliases() flush includes the direct map.
2069 */
2070 for (i = 0; i < area->nr_pages; i++) {
2071 if (page_address(area->pages[i])) {
2072 start = min(addr, start);
2073 end = max(addr, end);
2074 }
2075 }
2076
2077 /*
2078 * Set direct map to something invalid so that it won't be cached if
2079 * there are any accesses after the TLB flush, then flush the TLB and
2080 * reset the direct map permissions to the default.
2081 */
2082 set_area_direct_map(area, set_direct_map_invalid_noflush);
2083 _vm_unmap_aliases(start, end, 1);
2084 set_area_direct_map(area, set_direct_map_default_noflush);
2085}
2086
Christoph Lameterb3bdda02008-02-04 22:28:32 -08002087static void __vunmap(const void *addr, int deallocate_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
2089 struct vm_struct *area;
2090
2091 if (!addr)
2092 return;
2093
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002094 if (WARN(!PAGE_ALIGNED(addr), "Trying to vfree() bad address (%p)\n",
Dan Carpenterab15d9b2013-07-08 15:59:53 -07002095 addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Liviu Dudau6ade2032019-03-05 15:42:54 -08002098 area = find_vm_area(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 if (unlikely(!area)) {
Arjan van de Ven4c8573e2008-07-25 19:45:37 -07002100 WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 return;
2103 }
2104
Chintan Pandya05e3ff92018-06-07 17:06:53 -07002105 debug_check_no_locks_freed(area->addr, get_vm_area_size(area));
2106 debug_check_no_obj_freed(area->addr, get_vm_area_size(area));
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07002107
Rick Edgecombe868b1042019-04-25 17:11:36 -07002108 vm_remove_mappings(area, deallocate_pages);
2109
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 if (deallocate_pages) {
2111 int i;
2112
2113 for (i = 0; i < area->nr_pages; i++) {
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08002114 struct page *page = area->pages[i];
2115
2116 BUG_ON(!page);
Vladimir Davydov49491482016-07-26 15:24:24 -07002117 __free_pages(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 }
2119
David Rientjes244d63e2016-01-14 15:19:35 -08002120 kvfree(area->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 }
2122
2123 kfree(area);
2124 return;
2125}
Andrey Ryabininbf22e372016-12-12 16:44:10 -08002126
2127static inline void __vfree_deferred(const void *addr)
2128{
2129 /*
2130 * Use raw_cpu_ptr() because this can be called from preemptible
2131 * context. Preemption is absolutely fine here, because the llist_add()
2132 * implementation is lockless, so it works even if we are adding to
2133 * nother cpu's list. schedule_work() should be fine with this too.
2134 */
2135 struct vfree_deferred *p = raw_cpu_ptr(&vfree_deferred);
2136
2137 if (llist_add((struct llist_node *)addr, &p->list))
2138 schedule_work(&p->wq);
2139}
2140
2141/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002142 * vfree_atomic - release memory allocated by vmalloc()
2143 * @addr: memory base address
Andrey Ryabininbf22e372016-12-12 16:44:10 -08002144 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002145 * This one is just like vfree() but can be called in any atomic context
2146 * except NMIs.
Andrey Ryabininbf22e372016-12-12 16:44:10 -08002147 */
2148void vfree_atomic(const void *addr)
2149{
2150 BUG_ON(in_nmi());
2151
2152 kmemleak_free(addr);
2153
2154 if (!addr)
2155 return;
2156 __vfree_deferred(addr);
2157}
2158
Roman Penyaevc67dc622019-03-05 15:43:24 -08002159static void __vfree(const void *addr)
2160{
2161 if (unlikely(in_interrupt()))
2162 __vfree_deferred(addr);
2163 else
2164 __vunmap(addr, 1);
2165}
2166
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002168 * vfree - release memory allocated by vmalloc()
2169 * @addr: memory base address
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002171 * Free the virtually continuous memory area starting at @addr, as
2172 * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
2173 * NULL, no operation is performed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002175 * Must not be called in NMI context (strictly speaking, only if we don't
2176 * have CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG, but making the calling
2177 * conventions for vfree() arch-depenedent would be a really bad idea)
Andrew Mortonc9fcee52013-05-07 16:18:18 -07002178 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002179 * May sleep if called *not* from interrupt context.
Andrey Ryabinin3ca4ea32018-10-26 15:07:03 -07002180 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002181 * NOTE: assumes that the object at @addr has a size >= sizeof(llist_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -08002183void vfree(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
Al Viro32fcfd42013-03-10 20:14:08 -04002185 BUG_ON(in_nmi());
Catalin Marinas89219d32009-06-11 13:23:19 +01002186
2187 kmemleak_free(addr);
2188
Andrey Ryabinina8dda162018-10-26 15:07:07 -07002189 might_sleep_if(!in_interrupt());
2190
Al Viro32fcfd42013-03-10 20:14:08 -04002191 if (!addr)
2192 return;
Roman Penyaevc67dc622019-03-05 15:43:24 -08002193
2194 __vfree(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196EXPORT_SYMBOL(vfree);
2197
2198/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002199 * vunmap - release virtual mapping obtained by vmap()
2200 * @addr: memory base address
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002202 * Free the virtually contiguous memory area starting at @addr,
2203 * which was created from the page array passed to vmap().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002205 * Must not be called in interrupt context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -08002207void vunmap(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
2209 BUG_ON(in_interrupt());
Peter Zijlstra34754b62009-02-25 16:04:03 +01002210 might_sleep();
Al Viro32fcfd42013-03-10 20:14:08 -04002211 if (addr)
2212 __vunmap(addr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214EXPORT_SYMBOL(vunmap);
2215
2216/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002217 * vmap - map an array of pages into virtually contiguous space
2218 * @pages: array of page pointers
2219 * @count: number of pages to map
2220 * @flags: vm_area->flags
2221 * @prot: page protection for the mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002223 * Maps @count pages from @pages into contiguous kernel virtual
2224 * space.
Mike Rapoporta862f682019-03-05 15:48:42 -08002225 *
2226 * Return: the address of the area or %NULL on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 */
2228void *vmap(struct page **pages, unsigned int count,
Mike Rapoport92eac162019-03-05 15:48:36 -08002229 unsigned long flags, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
2231 struct vm_struct *area;
Guillermo Julián Moreno65ee03c2016-06-03 14:55:33 -07002232 unsigned long size; /* In bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
Peter Zijlstra34754b62009-02-25 16:04:03 +01002234 might_sleep();
2235
Arun KSca79b0c2018-12-28 00:34:29 -08002236 if (count > totalram_pages())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 return NULL;
2238
Guillermo Julián Moreno65ee03c2016-06-03 14:55:33 -07002239 size = (unsigned long)count << PAGE_SHIFT;
2240 area = get_vm_area_caller(size, flags, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 if (!area)
2242 return NULL;
Christoph Lameter23016962008-04-28 02:12:42 -07002243
WANG Chaof6f8ed42014-08-06 16:06:58 -07002244 if (map_vm_area(area, prot, pages)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 vunmap(area->addr);
2246 return NULL;
2247 }
2248
2249 return area->addr;
2250}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251EXPORT_SYMBOL(vmap);
2252
Michal Hocko8594a212017-05-12 15:46:41 -07002253static void *__vmalloc_node(unsigned long size, unsigned long align,
2254 gfp_t gfp_mask, pgprot_t prot,
2255 int node, const void *caller);
Adrian Bunke31d9eb2008-02-04 22:29:09 -08002256static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
Wanpeng Li3722e132013-11-12 15:07:29 -08002257 pgprot_t prot, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258{
2259 struct page **pages;
2260 unsigned int nr_pages, array_size, i;
David Rientjes930f0362014-08-06 16:06:28 -07002261 const gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO;
Laura Abbott704b8622017-08-18 15:16:27 -07002262 const gfp_t alloc_mask = gfp_mask | __GFP_NOWARN;
2263 const gfp_t highmem_mask = (gfp_mask & (GFP_DMA | GFP_DMA32)) ?
2264 0 :
2265 __GFP_HIGHMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Wanpeng Li762216a2013-09-11 14:22:42 -07002267 nr_pages = get_vm_area_size(area) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 array_size = (nr_pages * sizeof(struct page *));
2269
2270 area->nr_pages = nr_pages;
2271 /* Please note that the recursion is strictly bounded. */
Jan Kiszka8757d5f2006-07-14 00:23:56 -07002272 if (array_size > PAGE_SIZE) {
Laura Abbott704b8622017-08-18 15:16:27 -07002273 pages = __vmalloc_node(array_size, 1, nested_gfp|highmem_mask,
Wanpeng Li3722e132013-11-12 15:07:29 -08002274 PAGE_KERNEL, node, area->caller);
Andrew Morton286e1ea2006-10-17 00:09:57 -07002275 } else {
Jan Beulich976d6df2009-12-14 17:58:39 -08002276 pages = kmalloc_node(array_size, nested_gfp, node);
Andrew Morton286e1ea2006-10-17 00:09:57 -07002277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 area->pages = pages;
2279 if (!area->pages) {
2280 remove_vm_area(area->addr);
2281 kfree(area);
2282 return NULL;
2283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
2285 for (i = 0; i < area->nr_pages; i++) {
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08002286 struct page *page;
2287
Jianguo Wu4b909512013-11-12 15:07:11 -08002288 if (node == NUMA_NO_NODE)
Laura Abbott704b8622017-08-18 15:16:27 -07002289 page = alloc_page(alloc_mask|highmem_mask);
Christoph Lameter930fc452005-10-29 18:15:41 -07002290 else
Laura Abbott704b8622017-08-18 15:16:27 -07002291 page = alloc_pages_node(node, alloc_mask|highmem_mask, 0);
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08002292
2293 if (unlikely(!page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 /* Successfully allocated i pages, free them in __vunmap() */
2295 area->nr_pages = i;
2296 goto fail;
2297 }
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08002298 area->pages[i] = page;
Laura Abbott704b8622017-08-18 15:16:27 -07002299 if (gfpflags_allow_blocking(gfp_mask|highmem_mask))
Eric Dumazet660654f2014-08-06 16:06:25 -07002300 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 }
2302
WANG Chaof6f8ed42014-08-06 16:06:58 -07002303 if (map_vm_area(area, prot, pages))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 goto fail;
2305 return area->addr;
2306
2307fail:
Michal Hockoa8e99252017-02-22 15:46:10 -08002308 warn_alloc(gfp_mask, NULL,
Michal Hocko7877cdc2016-10-07 17:01:55 -07002309 "vmalloc: allocation failure, allocated %ld of %ld bytes",
Dave Hansen22943ab2011-05-24 17:12:18 -07002310 (area->nr_pages*PAGE_SIZE), area->size);
Roman Penyaevc67dc622019-03-05 15:43:24 -08002311 __vfree(area->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 return NULL;
2313}
2314
David Rientjesd0a21262011-01-13 15:46:02 -08002315/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002316 * __vmalloc_node_range - allocate virtually contiguous memory
2317 * @size: allocation size
2318 * @align: desired alignment
2319 * @start: vm area range start
2320 * @end: vm area range end
2321 * @gfp_mask: flags for the page level allocator
2322 * @prot: protection mask for the allocated pages
2323 * @vm_flags: additional vm area flags (e.g. %VM_NO_GUARD)
2324 * @node: node to use for allocation or NUMA_NO_NODE
2325 * @caller: caller's return address
David Rientjesd0a21262011-01-13 15:46:02 -08002326 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002327 * Allocate enough pages to cover @size from the page level
2328 * allocator with @gfp_mask flags. Map them into contiguous
2329 * kernel virtual space, using a pagetable protection of @prot.
Mike Rapoporta862f682019-03-05 15:48:42 -08002330 *
2331 * Return: the address of the area or %NULL on failure
David Rientjesd0a21262011-01-13 15:46:02 -08002332 */
2333void *__vmalloc_node_range(unsigned long size, unsigned long align,
2334 unsigned long start, unsigned long end, gfp_t gfp_mask,
Andrey Ryabinincb9e3c22015-02-13 14:40:07 -08002335 pgprot_t prot, unsigned long vm_flags, int node,
2336 const void *caller)
Christoph Lameter930fc452005-10-29 18:15:41 -07002337{
David Rientjesd0a21262011-01-13 15:46:02 -08002338 struct vm_struct *area;
2339 void *addr;
2340 unsigned long real_size = size;
2341
2342 size = PAGE_ALIGN(size);
Arun KSca79b0c2018-12-28 00:34:29 -08002343 if (!size || (size >> PAGE_SHIFT) > totalram_pages())
Joe Perchesde7d2b52011-10-31 17:08:48 -07002344 goto fail;
David Rientjesd0a21262011-01-13 15:46:02 -08002345
Andrey Ryabinincb9e3c22015-02-13 14:40:07 -08002346 area = __get_vm_area_node(size, align, VM_ALLOC | VM_UNINITIALIZED |
2347 vm_flags, start, end, node, gfp_mask, caller);
David Rientjesd0a21262011-01-13 15:46:02 -08002348 if (!area)
Joe Perchesde7d2b52011-10-31 17:08:48 -07002349 goto fail;
David Rientjesd0a21262011-01-13 15:46:02 -08002350
Wanpeng Li3722e132013-11-12 15:07:29 -08002351 addr = __vmalloc_area_node(area, gfp_mask, prot, node);
Mel Gorman1368edf2011-12-08 14:34:30 -08002352 if (!addr)
Wanpeng Lib82225f32013-11-12 15:07:33 -08002353 return NULL;
Catalin Marinas89219d32009-06-11 13:23:19 +01002354
2355 /*
Zhang Yanfei20fc02b2013-07-08 15:59:58 -07002356 * In this function, newly allocated vm_struct has VM_UNINITIALIZED
2357 * flag. It means that vm_struct is not fully initialized.
Joonsoo Kim4341fa42013-04-29 15:07:39 -07002358 * Now, it is fully initialized, so remove this flag here.
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07002359 */
Zhang Yanfei20fc02b2013-07-08 15:59:58 -07002360 clear_vm_uninitialized_flag(area);
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07002361
Catalin Marinas94f4a162017-07-06 15:40:22 -07002362 kmemleak_vmalloc(area, size, gfp_mask);
Catalin Marinas89219d32009-06-11 13:23:19 +01002363
2364 return addr;
Joe Perchesde7d2b52011-10-31 17:08:48 -07002365
2366fail:
Michal Hockoa8e99252017-02-22 15:46:10 -08002367 warn_alloc(gfp_mask, NULL,
Michal Hocko7877cdc2016-10-07 17:01:55 -07002368 "vmalloc: allocation failure: %lu bytes", real_size);
Joe Perchesde7d2b52011-10-31 17:08:48 -07002369 return NULL;
Christoph Lameter930fc452005-10-29 18:15:41 -07002370}
2371
Uladzislau Rezki (Sony)153178e2019-03-05 15:43:30 -08002372/*
2373 * This is only for performance analysis of vmalloc and stress purpose.
2374 * It is required by vmalloc test module, therefore do not use it other
2375 * than that.
2376 */
2377#ifdef CONFIG_TEST_VMALLOC_MODULE
2378EXPORT_SYMBOL_GPL(__vmalloc_node_range);
2379#endif
2380
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002382 * __vmalloc_node - allocate virtually contiguous memory
2383 * @size: allocation size
2384 * @align: desired alignment
2385 * @gfp_mask: flags for the page level allocator
2386 * @prot: protection mask for the allocated pages
2387 * @node: node to use for allocation or NUMA_NO_NODE
2388 * @caller: caller's return address
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002390 * Allocate enough pages to cover @size from the page level
2391 * allocator with @gfp_mask flags. Map them into contiguous
2392 * kernel virtual space, using a pagetable protection of @prot.
Michal Hockoa7c3e902017-05-08 15:57:09 -07002393 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002394 * Reclaim modifiers in @gfp_mask - __GFP_NORETRY, __GFP_RETRY_MAYFAIL
2395 * and __GFP_NOFAIL are not supported
Michal Hockoa7c3e902017-05-08 15:57:09 -07002396 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002397 * Any use of gfp flags outside of GFP_KERNEL should be consulted
2398 * with mm people.
Mike Rapoporta862f682019-03-05 15:48:42 -08002399 *
2400 * Return: pointer to the allocated memory or %NULL on error
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 */
Michal Hocko8594a212017-05-12 15:46:41 -07002402static void *__vmalloc_node(unsigned long size, unsigned long align,
David Miller2dca6992009-09-21 12:22:34 -07002403 gfp_t gfp_mask, pgprot_t prot,
Marek Szyprowski5e6cafc2012-04-13 12:32:09 +02002404 int node, const void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405{
David Rientjesd0a21262011-01-13 15:46:02 -08002406 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
Andrey Ryabinincb9e3c22015-02-13 14:40:07 -08002407 gfp_mask, prot, 0, node, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408}
2409
Christoph Lameter930fc452005-10-29 18:15:41 -07002410void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
2411{
David Rientjes00ef2d22013-02-22 16:35:36 -08002412 return __vmalloc_node(size, 1, gfp_mask, prot, NUMA_NO_NODE,
Christoph Lameter23016962008-04-28 02:12:42 -07002413 __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -07002414}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415EXPORT_SYMBOL(__vmalloc);
2416
Michal Hocko8594a212017-05-12 15:46:41 -07002417static inline void *__vmalloc_node_flags(unsigned long size,
2418 int node, gfp_t flags)
2419{
2420 return __vmalloc_node(size, 1, flags, PAGE_KERNEL,
2421 node, __builtin_return_address(0));
2422}
2423
2424
2425void *__vmalloc_node_flags_caller(unsigned long size, int node, gfp_t flags,
2426 void *caller)
2427{
2428 return __vmalloc_node(size, 1, flags, PAGE_KERNEL, node, caller);
2429}
2430
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002432 * vmalloc - allocate virtually contiguous memory
2433 * @size: allocation size
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002435 * Allocate enough pages to cover @size from the page level
2436 * allocator and map them into contiguous kernel virtual space.
2437 *
2438 * For tight control over page level allocator and protection flags
2439 * use __vmalloc() instead.
Mike Rapoporta862f682019-03-05 15:48:42 -08002440 *
2441 * Return: pointer to the allocated memory or %NULL on error
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 */
2443void *vmalloc(unsigned long size)
2444{
David Rientjes00ef2d22013-02-22 16:35:36 -08002445 return __vmalloc_node_flags(size, NUMA_NO_NODE,
Michal Hocko19809c22017-05-08 15:57:44 -07002446 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448EXPORT_SYMBOL(vmalloc);
2449
Christoph Lameter930fc452005-10-29 18:15:41 -07002450/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002451 * vzalloc - allocate virtually contiguous memory with zero fill
2452 * @size: allocation size
Dave Younge1ca7782010-10-26 14:22:06 -07002453 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002454 * Allocate enough pages to cover @size from the page level
2455 * allocator and map them into contiguous kernel virtual space.
2456 * The memory allocated is set to zero.
2457 *
2458 * For tight control over page level allocator and protection flags
2459 * use __vmalloc() instead.
Mike Rapoporta862f682019-03-05 15:48:42 -08002460 *
2461 * Return: pointer to the allocated memory or %NULL on error
Dave Younge1ca7782010-10-26 14:22:06 -07002462 */
2463void *vzalloc(unsigned long size)
2464{
David Rientjes00ef2d22013-02-22 16:35:36 -08002465 return __vmalloc_node_flags(size, NUMA_NO_NODE,
Michal Hocko19809c22017-05-08 15:57:44 -07002466 GFP_KERNEL | __GFP_ZERO);
Dave Younge1ca7782010-10-26 14:22:06 -07002467}
2468EXPORT_SYMBOL(vzalloc);
2469
2470/**
Rolf Eike Beeread04082006-09-27 01:50:13 -07002471 * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
2472 * @size: allocation size
Nick Piggin83342312006-06-23 02:03:20 -07002473 *
Rolf Eike Beeread04082006-09-27 01:50:13 -07002474 * The resulting memory area is zeroed so it can be mapped to userspace
2475 * without leaking data.
Mike Rapoporta862f682019-03-05 15:48:42 -08002476 *
2477 * Return: pointer to the allocated memory or %NULL on error
Nick Piggin83342312006-06-23 02:03:20 -07002478 */
2479void *vmalloc_user(unsigned long size)
2480{
Roman Penyaevbc84c532019-03-05 15:43:27 -08002481 return __vmalloc_node_range(size, SHMLBA, VMALLOC_START, VMALLOC_END,
2482 GFP_KERNEL | __GFP_ZERO, PAGE_KERNEL,
2483 VM_USERMAP, NUMA_NO_NODE,
2484 __builtin_return_address(0));
Nick Piggin83342312006-06-23 02:03:20 -07002485}
2486EXPORT_SYMBOL(vmalloc_user);
2487
2488/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002489 * vmalloc_node - allocate memory on a specific node
2490 * @size: allocation size
2491 * @node: numa node
Christoph Lameter930fc452005-10-29 18:15:41 -07002492 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002493 * Allocate enough pages to cover @size from the page level
2494 * allocator and map them into contiguous kernel virtual space.
Christoph Lameter930fc452005-10-29 18:15:41 -07002495 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002496 * For tight control over page level allocator and protection flags
2497 * use __vmalloc() instead.
Mike Rapoporta862f682019-03-05 15:48:42 -08002498 *
2499 * Return: pointer to the allocated memory or %NULL on error
Christoph Lameter930fc452005-10-29 18:15:41 -07002500 */
2501void *vmalloc_node(unsigned long size, int node)
2502{
Michal Hocko19809c22017-05-08 15:57:44 -07002503 return __vmalloc_node(size, 1, GFP_KERNEL, PAGE_KERNEL,
Christoph Lameter23016962008-04-28 02:12:42 -07002504 node, __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -07002505}
2506EXPORT_SYMBOL(vmalloc_node);
2507
Dave Younge1ca7782010-10-26 14:22:06 -07002508/**
2509 * vzalloc_node - allocate memory on a specific node with zero fill
2510 * @size: allocation size
2511 * @node: numa node
2512 *
2513 * Allocate enough pages to cover @size from the page level
2514 * allocator and map them into contiguous kernel virtual space.
2515 * The memory allocated is set to zero.
2516 *
2517 * For tight control over page level allocator and protection flags
2518 * use __vmalloc_node() instead.
Mike Rapoporta862f682019-03-05 15:48:42 -08002519 *
2520 * Return: pointer to the allocated memory or %NULL on error
Dave Younge1ca7782010-10-26 14:22:06 -07002521 */
2522void *vzalloc_node(unsigned long size, int node)
2523{
2524 return __vmalloc_node_flags(size, node,
Michal Hocko19809c22017-05-08 15:57:44 -07002525 GFP_KERNEL | __GFP_ZERO);
Dave Younge1ca7782010-10-26 14:22:06 -07002526}
2527EXPORT_SYMBOL(vzalloc_node);
2528
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002530 * vmalloc_exec - allocate virtually contiguous, executable memory
2531 * @size: allocation size
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002533 * Kernel-internal function to allocate enough pages to cover @size
2534 * the page level allocator and map them into contiguous and
2535 * executable kernel virtual space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002537 * For tight control over page level allocator and protection flags
2538 * use __vmalloc() instead.
Mike Rapoporta862f682019-03-05 15:48:42 -08002539 *
2540 * Return: pointer to the allocated memory or %NULL on error
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542void *vmalloc_exec(unsigned long size)
2543{
Rick Edgecombe868b1042019-04-25 17:11:36 -07002544 return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END,
2545 GFP_KERNEL, PAGE_KERNEL_EXEC, VM_FLUSH_RESET_PERMS,
2546 NUMA_NO_NODE, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547}
2548
Andi Kleen0d08e0d2007-05-02 19:27:12 +02002549#if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
Michal Hocko698d0832018-02-21 14:46:01 -08002550#define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL)
Andi Kleen0d08e0d2007-05-02 19:27:12 +02002551#elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
Michal Hocko698d0832018-02-21 14:46:01 -08002552#define GFP_VMALLOC32 (GFP_DMA | GFP_KERNEL)
Andi Kleen0d08e0d2007-05-02 19:27:12 +02002553#else
Michal Hocko698d0832018-02-21 14:46:01 -08002554/*
2555 * 64b systems should always have either DMA or DMA32 zones. For others
2556 * GFP_DMA32 should do the right thing and use the normal zone.
2557 */
2558#define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
Andi Kleen0d08e0d2007-05-02 19:27:12 +02002559#endif
2560
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002562 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
2563 * @size: allocation size
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002565 * Allocate enough 32bit PA addressable pages to cover @size from the
2566 * page level allocator and map them into contiguous kernel virtual space.
Mike Rapoporta862f682019-03-05 15:48:42 -08002567 *
2568 * Return: pointer to the allocated memory or %NULL on error
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 */
2570void *vmalloc_32(unsigned long size)
2571{
David Miller2dca6992009-09-21 12:22:34 -07002572 return __vmalloc_node(size, 1, GFP_VMALLOC32, PAGE_KERNEL,
David Rientjes00ef2d22013-02-22 16:35:36 -08002573 NUMA_NO_NODE, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575EXPORT_SYMBOL(vmalloc_32);
2576
Nick Piggin83342312006-06-23 02:03:20 -07002577/**
Rolf Eike Beeread04082006-09-27 01:50:13 -07002578 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
Mike Rapoport92eac162019-03-05 15:48:36 -08002579 * @size: allocation size
Rolf Eike Beeread04082006-09-27 01:50:13 -07002580 *
2581 * The resulting memory area is 32bit addressable and zeroed so it can be
2582 * mapped to userspace without leaking data.
Mike Rapoporta862f682019-03-05 15:48:42 -08002583 *
2584 * Return: pointer to the allocated memory or %NULL on error
Nick Piggin83342312006-06-23 02:03:20 -07002585 */
2586void *vmalloc_32_user(unsigned long size)
2587{
Roman Penyaevbc84c532019-03-05 15:43:27 -08002588 return __vmalloc_node_range(size, SHMLBA, VMALLOC_START, VMALLOC_END,
2589 GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
2590 VM_USERMAP, NUMA_NO_NODE,
2591 __builtin_return_address(0));
Nick Piggin83342312006-06-23 02:03:20 -07002592}
2593EXPORT_SYMBOL(vmalloc_32_user);
2594
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002595/*
2596 * small helper routine , copy contents to buf from addr.
2597 * If the page is not present, fill zero.
2598 */
2599
2600static int aligned_vread(char *buf, char *addr, unsigned long count)
2601{
2602 struct page *p;
2603 int copied = 0;
2604
2605 while (count) {
2606 unsigned long offset, length;
2607
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08002608 offset = offset_in_page(addr);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002609 length = PAGE_SIZE - offset;
2610 if (length > count)
2611 length = count;
2612 p = vmalloc_to_page(addr);
2613 /*
2614 * To do safe access to this _mapped_ area, we need
2615 * lock. But adding lock here means that we need to add
2616 * overhead of vmalloc()/vfree() calles for this _debug_
2617 * interface, rarely used. Instead of that, we'll use
2618 * kmap() and get small overhead in this access function.
2619 */
2620 if (p) {
2621 /*
2622 * we can expect USER0 is not used (see vread/vwrite's
2623 * function description)
2624 */
Cong Wang9b04c5f2011-11-25 23:14:39 +08002625 void *map = kmap_atomic(p);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002626 memcpy(buf, map + offset, length);
Cong Wang9b04c5f2011-11-25 23:14:39 +08002627 kunmap_atomic(map);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002628 } else
2629 memset(buf, 0, length);
2630
2631 addr += length;
2632 buf += length;
2633 copied += length;
2634 count -= length;
2635 }
2636 return copied;
2637}
2638
2639static int aligned_vwrite(char *buf, char *addr, unsigned long count)
2640{
2641 struct page *p;
2642 int copied = 0;
2643
2644 while (count) {
2645 unsigned long offset, length;
2646
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08002647 offset = offset_in_page(addr);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002648 length = PAGE_SIZE - offset;
2649 if (length > count)
2650 length = count;
2651 p = vmalloc_to_page(addr);
2652 /*
2653 * To do safe access to this _mapped_ area, we need
2654 * lock. But adding lock here means that we need to add
2655 * overhead of vmalloc()/vfree() calles for this _debug_
2656 * interface, rarely used. Instead of that, we'll use
2657 * kmap() and get small overhead in this access function.
2658 */
2659 if (p) {
2660 /*
2661 * we can expect USER0 is not used (see vread/vwrite's
2662 * function description)
2663 */
Cong Wang9b04c5f2011-11-25 23:14:39 +08002664 void *map = kmap_atomic(p);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002665 memcpy(map + offset, buf, length);
Cong Wang9b04c5f2011-11-25 23:14:39 +08002666 kunmap_atomic(map);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002667 }
2668 addr += length;
2669 buf += length;
2670 copied += length;
2671 count -= length;
2672 }
2673 return copied;
2674}
2675
2676/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002677 * vread() - read vmalloc area in a safe way.
2678 * @buf: buffer for reading data
2679 * @addr: vm address.
2680 * @count: number of bytes to be read.
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002681 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002682 * This function checks that addr is a valid vmalloc'ed area, and
2683 * copy data from that area to a given buffer. If the given memory range
2684 * of [addr...addr+count) includes some valid address, data is copied to
2685 * proper area of @buf. If there are memory holes, they'll be zero-filled.
2686 * IOREMAP area is treated as memory hole and no copy is done.
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002687 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002688 * If [addr...addr+count) doesn't includes any intersects with alive
2689 * vm_struct area, returns 0. @buf should be kernel's buffer.
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002690 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002691 * Note: In usual ops, vread() is never necessary because the caller
2692 * should know vmalloc() area is valid and can use memcpy().
2693 * This is for routines which have to access vmalloc area without
2694 * any informaion, as /dev/kmem.
Mike Rapoporta862f682019-03-05 15:48:42 -08002695 *
2696 * Return: number of bytes for which addr and buf should be increased
2697 * (same number as @count) or %0 if [addr...addr+count) doesn't
2698 * include any intersection with valid vmalloc area
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002699 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700long vread(char *buf, char *addr, unsigned long count)
2701{
Joonsoo Kime81ce852013-04-29 15:07:32 -07002702 struct vmap_area *va;
2703 struct vm_struct *vm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 char *vaddr, *buf_start = buf;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002705 unsigned long buflen = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 unsigned long n;
2707
2708 /* Don't allow overflow */
2709 if ((unsigned long) addr + count < count)
2710 count = -(unsigned long) addr;
2711
Joonsoo Kime81ce852013-04-29 15:07:32 -07002712 spin_lock(&vmap_area_lock);
2713 list_for_each_entry(va, &vmap_area_list, list) {
2714 if (!count)
2715 break;
2716
2717 if (!(va->flags & VM_VM_AREA))
2718 continue;
2719
2720 vm = va->vm;
2721 vaddr = (char *) vm->addr;
Wanpeng Li762216a2013-09-11 14:22:42 -07002722 if (addr >= vaddr + get_vm_area_size(vm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 continue;
2724 while (addr < vaddr) {
2725 if (count == 0)
2726 goto finished;
2727 *buf = '\0';
2728 buf++;
2729 addr++;
2730 count--;
2731 }
Wanpeng Li762216a2013-09-11 14:22:42 -07002732 n = vaddr + get_vm_area_size(vm) - addr;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002733 if (n > count)
2734 n = count;
Joonsoo Kime81ce852013-04-29 15:07:32 -07002735 if (!(vm->flags & VM_IOREMAP))
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002736 aligned_vread(buf, addr, n);
2737 else /* IOREMAP area is treated as memory hole */
2738 memset(buf, 0, n);
2739 buf += n;
2740 addr += n;
2741 count -= n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 }
2743finished:
Joonsoo Kime81ce852013-04-29 15:07:32 -07002744 spin_unlock(&vmap_area_lock);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002745
2746 if (buf == buf_start)
2747 return 0;
2748 /* zero-fill memory holes */
2749 if (buf != buf_start + buflen)
2750 memset(buf, 0, buflen - (buf - buf_start));
2751
2752 return buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753}
2754
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002755/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002756 * vwrite() - write vmalloc area in a safe way.
2757 * @buf: buffer for source data
2758 * @addr: vm address.
2759 * @count: number of bytes to be read.
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002760 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002761 * This function checks that addr is a valid vmalloc'ed area, and
2762 * copy data from a buffer to the given addr. If specified range of
2763 * [addr...addr+count) includes some valid address, data is copied from
2764 * proper area of @buf. If there are memory holes, no copy to hole.
2765 * IOREMAP area is treated as memory hole and no copy is done.
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002766 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002767 * If [addr...addr+count) doesn't includes any intersects with alive
2768 * vm_struct area, returns 0. @buf should be kernel's buffer.
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002769 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002770 * Note: In usual ops, vwrite() is never necessary because the caller
2771 * should know vmalloc() area is valid and can use memcpy().
2772 * This is for routines which have to access vmalloc area without
2773 * any informaion, as /dev/kmem.
Mike Rapoporta862f682019-03-05 15:48:42 -08002774 *
2775 * Return: number of bytes for which addr and buf should be
2776 * increased (same number as @count) or %0 if [addr...addr+count)
2777 * doesn't include any intersection with valid vmalloc area
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002778 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779long vwrite(char *buf, char *addr, unsigned long count)
2780{
Joonsoo Kime81ce852013-04-29 15:07:32 -07002781 struct vmap_area *va;
2782 struct vm_struct *vm;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002783 char *vaddr;
2784 unsigned long n, buflen;
2785 int copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
2787 /* Don't allow overflow */
2788 if ((unsigned long) addr + count < count)
2789 count = -(unsigned long) addr;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002790 buflen = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791
Joonsoo Kime81ce852013-04-29 15:07:32 -07002792 spin_lock(&vmap_area_lock);
2793 list_for_each_entry(va, &vmap_area_list, list) {
2794 if (!count)
2795 break;
2796
2797 if (!(va->flags & VM_VM_AREA))
2798 continue;
2799
2800 vm = va->vm;
2801 vaddr = (char *) vm->addr;
Wanpeng Li762216a2013-09-11 14:22:42 -07002802 if (addr >= vaddr + get_vm_area_size(vm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 continue;
2804 while (addr < vaddr) {
2805 if (count == 0)
2806 goto finished;
2807 buf++;
2808 addr++;
2809 count--;
2810 }
Wanpeng Li762216a2013-09-11 14:22:42 -07002811 n = vaddr + get_vm_area_size(vm) - addr;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002812 if (n > count)
2813 n = count;
Joonsoo Kime81ce852013-04-29 15:07:32 -07002814 if (!(vm->flags & VM_IOREMAP)) {
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002815 aligned_vwrite(buf, addr, n);
2816 copied++;
2817 }
2818 buf += n;
2819 addr += n;
2820 count -= n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 }
2822finished:
Joonsoo Kime81ce852013-04-29 15:07:32 -07002823 spin_unlock(&vmap_area_lock);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002824 if (!copied)
2825 return 0;
2826 return buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827}
Nick Piggin83342312006-06-23 02:03:20 -07002828
2829/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002830 * remap_vmalloc_range_partial - map vmalloc pages to userspace
2831 * @vma: vma to cover
2832 * @uaddr: target user address to start at
2833 * @kaddr: virtual address of vmalloc kernel memory
2834 * @size: size of map area
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002835 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002836 * Returns: 0 for success, -Exxx on failure
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002837 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002838 * This function checks that @kaddr is a valid vmalloc'ed area,
2839 * and that it is big enough to cover the range starting at
2840 * @uaddr in @vma. Will return failure if that criteria isn't
2841 * met.
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002842 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002843 * Similar to remap_pfn_range() (see mm/memory.c)
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002844 */
2845int remap_vmalloc_range_partial(struct vm_area_struct *vma, unsigned long uaddr,
2846 void *kaddr, unsigned long size)
2847{
2848 struct vm_struct *area;
2849
2850 size = PAGE_ALIGN(size);
2851
2852 if (!PAGE_ALIGNED(uaddr) || !PAGE_ALIGNED(kaddr))
2853 return -EINVAL;
2854
2855 area = find_vm_area(kaddr);
2856 if (!area)
2857 return -EINVAL;
2858
2859 if (!(area->flags & VM_USERMAP))
2860 return -EINVAL;
2861
Roman Penyaev401592d2019-03-05 15:43:20 -08002862 if (kaddr + size > area->addr + get_vm_area_size(area))
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002863 return -EINVAL;
2864
2865 do {
2866 struct page *page = vmalloc_to_page(kaddr);
2867 int ret;
2868
2869 ret = vm_insert_page(vma, uaddr, page);
2870 if (ret)
2871 return ret;
2872
2873 uaddr += PAGE_SIZE;
2874 kaddr += PAGE_SIZE;
2875 size -= PAGE_SIZE;
2876 } while (size > 0);
2877
2878 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
2879
2880 return 0;
2881}
2882EXPORT_SYMBOL(remap_vmalloc_range_partial);
2883
2884/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002885 * remap_vmalloc_range - map vmalloc pages to userspace
2886 * @vma: vma to cover (map full range of vma)
2887 * @addr: vmalloc memory
2888 * @pgoff: number of pages into addr before first page to map
Randy Dunlap76824862008-03-19 17:00:40 -07002889 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002890 * Returns: 0 for success, -Exxx on failure
Nick Piggin83342312006-06-23 02:03:20 -07002891 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002892 * This function checks that addr is a valid vmalloc'ed area, and
2893 * that it is big enough to cover the vma. Will return failure if
2894 * that criteria isn't met.
Nick Piggin83342312006-06-23 02:03:20 -07002895 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002896 * Similar to remap_pfn_range() (see mm/memory.c)
Nick Piggin83342312006-06-23 02:03:20 -07002897 */
2898int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
2899 unsigned long pgoff)
2900{
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002901 return remap_vmalloc_range_partial(vma, vma->vm_start,
2902 addr + (pgoff << PAGE_SHIFT),
2903 vma->vm_end - vma->vm_start);
Nick Piggin83342312006-06-23 02:03:20 -07002904}
2905EXPORT_SYMBOL(remap_vmalloc_range);
2906
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -07002907/*
2908 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
2909 * have one.
2910 */
Gideon Israel Dsouza3b321232014-04-07 15:37:26 -07002911void __weak vmalloc_sync_all(void)
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -07002912{
2913}
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002914
2915
Martin Schwidefsky2f569af2008-02-08 04:22:04 -08002916static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002917{
David Vrabelcd129092011-09-29 16:53:32 +01002918 pte_t ***p = data;
2919
2920 if (p) {
2921 *(*p) = pte;
2922 (*p)++;
2923 }
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002924 return 0;
2925}
2926
2927/**
Mike Rapoport92eac162019-03-05 15:48:36 -08002928 * alloc_vm_area - allocate a range of kernel address space
2929 * @size: size of the area
2930 * @ptes: returns the PTEs for the address space
Randy Dunlap76824862008-03-19 17:00:40 -07002931 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002932 * Returns: NULL on failure, vm_struct on success
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002933 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002934 * This function reserves a range of kernel address space, and
2935 * allocates pagetables to map that range. No actual mappings
2936 * are created.
David Vrabelcd129092011-09-29 16:53:32 +01002937 *
Mike Rapoport92eac162019-03-05 15:48:36 -08002938 * If @ptes is non-NULL, pointers to the PTEs (in init_mm)
2939 * allocated for the VM area are returned.
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002940 */
David Vrabelcd129092011-09-29 16:53:32 +01002941struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002942{
2943 struct vm_struct *area;
2944
Christoph Lameter23016962008-04-28 02:12:42 -07002945 area = get_vm_area_caller(size, VM_IOREMAP,
2946 __builtin_return_address(0));
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002947 if (area == NULL)
2948 return NULL;
2949
2950 /*
2951 * This ensures that page tables are constructed for this region
2952 * of kernel virtual address space and mapped into init_mm.
2953 */
2954 if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
David Vrabelcd129092011-09-29 16:53:32 +01002955 size, f, ptes ? &ptes : NULL)) {
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002956 free_vm_area(area);
2957 return NULL;
2958 }
2959
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002960 return area;
2961}
2962EXPORT_SYMBOL_GPL(alloc_vm_area);
2963
2964void free_vm_area(struct vm_struct *area)
2965{
2966 struct vm_struct *ret;
2967 ret = remove_vm_area(area->addr);
2968 BUG_ON(ret != area);
2969 kfree(area);
2970}
2971EXPORT_SYMBOL_GPL(free_vm_area);
Christoph Lametera10aa572008-04-28 02:12:40 -07002972
Tejun Heo4f8b02b2010-09-03 18:22:47 +02002973#ifdef CONFIG_SMP
Tejun Heoca23e402009-08-14 15:00:52 +09002974static struct vmap_area *node_to_va(struct rb_node *n)
2975{
Geliang Tang4583e772017-02-22 15:41:54 -08002976 return rb_entry_safe(n, struct vmap_area, rb_node);
Tejun Heoca23e402009-08-14 15:00:52 +09002977}
2978
2979/**
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07002980 * pvm_find_va_enclose_addr - find the vmap_area @addr belongs to
2981 * @addr: target address
Tejun Heoca23e402009-08-14 15:00:52 +09002982 *
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07002983 * Returns: vmap_area if it is found. If there is no such area
2984 * the first highest(reverse order) vmap_area is returned
2985 * i.e. va->va_start < addr && va->va_end < addr or NULL
2986 * if there are no any areas before @addr.
Tejun Heoca23e402009-08-14 15:00:52 +09002987 */
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07002988static struct vmap_area *
2989pvm_find_va_enclose_addr(unsigned long addr)
Tejun Heoca23e402009-08-14 15:00:52 +09002990{
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07002991 struct vmap_area *va, *tmp;
2992 struct rb_node *n;
2993
2994 n = free_vmap_area_root.rb_node;
2995 va = NULL;
Tejun Heoca23e402009-08-14 15:00:52 +09002996
2997 while (n) {
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07002998 tmp = rb_entry(n, struct vmap_area, rb_node);
2999 if (tmp->va_start <= addr) {
3000 va = tmp;
3001 if (tmp->va_end >= addr)
3002 break;
3003
Tejun Heoca23e402009-08-14 15:00:52 +09003004 n = n->rb_right;
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003005 } else {
3006 n = n->rb_left;
3007 }
Tejun Heoca23e402009-08-14 15:00:52 +09003008 }
3009
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003010 return va;
Tejun Heoca23e402009-08-14 15:00:52 +09003011}
3012
3013/**
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003014 * pvm_determine_end_from_reverse - find the highest aligned address
3015 * of free block below VMALLOC_END
3016 * @va:
3017 * in - the VA we start the search(reverse order);
3018 * out - the VA with the highest aligned end address.
Tejun Heoca23e402009-08-14 15:00:52 +09003019 *
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003020 * Returns: determined end address within vmap_area
Tejun Heoca23e402009-08-14 15:00:52 +09003021 */
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003022static unsigned long
3023pvm_determine_end_from_reverse(struct vmap_area **va, unsigned long align)
Tejun Heoca23e402009-08-14 15:00:52 +09003024{
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003025 unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
Tejun Heoca23e402009-08-14 15:00:52 +09003026 unsigned long addr;
3027
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003028 if (likely(*va)) {
3029 list_for_each_entry_from_reverse((*va),
3030 &free_vmap_area_list, list) {
3031 addr = min((*va)->va_end & ~(align - 1), vmalloc_end);
3032 if ((*va)->va_start < addr)
3033 return addr;
3034 }
Tejun Heoca23e402009-08-14 15:00:52 +09003035 }
3036
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003037 return 0;
Tejun Heoca23e402009-08-14 15:00:52 +09003038}
3039
3040/**
3041 * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator
3042 * @offsets: array containing offset of each area
3043 * @sizes: array containing size of each area
3044 * @nr_vms: the number of areas to allocate
3045 * @align: alignment, all entries in @offsets and @sizes must be aligned to this
Tejun Heoca23e402009-08-14 15:00:52 +09003046 *
3047 * Returns: kmalloc'd vm_struct pointer array pointing to allocated
3048 * vm_structs on success, %NULL on failure
3049 *
3050 * Percpu allocator wants to use congruent vm areas so that it can
3051 * maintain the offsets among percpu areas. This function allocates
David Rientjesec3f64f2011-01-13 15:46:01 -08003052 * congruent vmalloc areas for it with GFP_KERNEL. These areas tend to
3053 * be scattered pretty far, distance between two areas easily going up
3054 * to gigabytes. To avoid interacting with regular vmallocs, these
3055 * areas are allocated from top.
Tejun Heoca23e402009-08-14 15:00:52 +09003056 *
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003057 * Despite its complicated look, this allocator is rather simple. It
3058 * does everything top-down and scans free blocks from the end looking
3059 * for matching base. While scanning, if any of the areas do not fit the
3060 * base address is pulled down to fit the area. Scanning is repeated till
3061 * all the areas fit and then all necessary data structures are inserted
3062 * and the result is returned.
Tejun Heoca23e402009-08-14 15:00:52 +09003063 */
3064struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
3065 const size_t *sizes, int nr_vms,
David Rientjesec3f64f2011-01-13 15:46:01 -08003066 size_t align)
Tejun Heoca23e402009-08-14 15:00:52 +09003067{
3068 const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align);
3069 const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003070 struct vmap_area **vas, *va;
Tejun Heoca23e402009-08-14 15:00:52 +09003071 struct vm_struct **vms;
3072 int area, area2, last_area, term_area;
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003073 unsigned long base, start, size, end, last_end;
Tejun Heoca23e402009-08-14 15:00:52 +09003074 bool purged = false;
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003075 enum fit_type type;
Tejun Heoca23e402009-08-14 15:00:52 +09003076
Tejun Heoca23e402009-08-14 15:00:52 +09003077 /* verify parameters and allocate data structures */
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08003078 BUG_ON(offset_in_page(align) || !is_power_of_2(align));
Tejun Heoca23e402009-08-14 15:00:52 +09003079 for (last_area = 0, area = 0; area < nr_vms; area++) {
3080 start = offsets[area];
3081 end = start + sizes[area];
3082
3083 /* is everything aligned properly? */
3084 BUG_ON(!IS_ALIGNED(offsets[area], align));
3085 BUG_ON(!IS_ALIGNED(sizes[area], align));
3086
3087 /* detect the area with the highest address */
3088 if (start > offsets[last_area])
3089 last_area = area;
3090
Wei Yangc568da22017-09-06 16:24:09 -07003091 for (area2 = area + 1; area2 < nr_vms; area2++) {
Tejun Heoca23e402009-08-14 15:00:52 +09003092 unsigned long start2 = offsets[area2];
3093 unsigned long end2 = start2 + sizes[area2];
3094
Wei Yangc568da22017-09-06 16:24:09 -07003095 BUG_ON(start2 < end && start < end2);
Tejun Heoca23e402009-08-14 15:00:52 +09003096 }
3097 }
3098 last_end = offsets[last_area] + sizes[last_area];
3099
3100 if (vmalloc_end - vmalloc_start < last_end) {
3101 WARN_ON(true);
3102 return NULL;
3103 }
3104
Thomas Meyer4d67d862012-05-29 15:06:21 -07003105 vms = kcalloc(nr_vms, sizeof(vms[0]), GFP_KERNEL);
3106 vas = kcalloc(nr_vms, sizeof(vas[0]), GFP_KERNEL);
Tejun Heoca23e402009-08-14 15:00:52 +09003107 if (!vas || !vms)
Kautuk Consulf1db7af2012-01-12 17:20:08 -08003108 goto err_free2;
Tejun Heoca23e402009-08-14 15:00:52 +09003109
3110 for (area = 0; area < nr_vms; area++) {
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003111 vas[area] = kmem_cache_zalloc(vmap_area_cachep, GFP_KERNEL);
David Rientjesec3f64f2011-01-13 15:46:01 -08003112 vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL);
Tejun Heoca23e402009-08-14 15:00:52 +09003113 if (!vas[area] || !vms[area])
3114 goto err_free;
3115 }
3116retry:
3117 spin_lock(&vmap_area_lock);
3118
3119 /* start scanning - we scan from the top, begin with the last area */
3120 area = term_area = last_area;
3121 start = offsets[area];
3122 end = start + sizes[area];
3123
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003124 va = pvm_find_va_enclose_addr(vmalloc_end);
3125 base = pvm_determine_end_from_reverse(&va, align) - end;
Tejun Heoca23e402009-08-14 15:00:52 +09003126
3127 while (true) {
Tejun Heoca23e402009-08-14 15:00:52 +09003128 /*
3129 * base might have underflowed, add last_end before
3130 * comparing.
3131 */
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003132 if (base + last_end < vmalloc_start + last_end)
3133 goto overflow;
Tejun Heoca23e402009-08-14 15:00:52 +09003134
3135 /*
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003136 * Fitting base has not been found.
Tejun Heoca23e402009-08-14 15:00:52 +09003137 */
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003138 if (va == NULL)
3139 goto overflow;
Tejun Heoca23e402009-08-14 15:00:52 +09003140
3141 /*
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003142 * If this VA does not fit, move base downwards and recheck.
Tejun Heoca23e402009-08-14 15:00:52 +09003143 */
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003144 if (base + start < va->va_start || base + end > va->va_end) {
3145 va = node_to_va(rb_prev(&va->rb_node));
3146 base = pvm_determine_end_from_reverse(&va, align) - end;
Tejun Heoca23e402009-08-14 15:00:52 +09003147 term_area = area;
3148 continue;
3149 }
3150
3151 /*
3152 * This area fits, move on to the previous one. If
3153 * the previous one is the terminal one, we're done.
3154 */
3155 area = (area + nr_vms - 1) % nr_vms;
3156 if (area == term_area)
3157 break;
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003158
Tejun Heoca23e402009-08-14 15:00:52 +09003159 start = offsets[area];
3160 end = start + sizes[area];
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003161 va = pvm_find_va_enclose_addr(base + end);
Tejun Heoca23e402009-08-14 15:00:52 +09003162 }
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003163
Tejun Heoca23e402009-08-14 15:00:52 +09003164 /* we've found a fitting base, insert all va's */
3165 for (area = 0; area < nr_vms; area++) {
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003166 int ret;
Tejun Heoca23e402009-08-14 15:00:52 +09003167
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003168 start = base + offsets[area];
3169 size = sizes[area];
3170
3171 va = pvm_find_va_enclose_addr(start);
3172 if (WARN_ON_ONCE(va == NULL))
3173 /* It is a BUG(), but trigger recovery instead. */
3174 goto recovery;
3175
3176 type = classify_va_fit_type(va, start, size);
3177 if (WARN_ON_ONCE(type == NOTHING_FIT))
3178 /* It is a BUG(), but trigger recovery instead. */
3179 goto recovery;
3180
3181 ret = adjust_va_to_fit_type(va, start, size, type);
3182 if (unlikely(ret))
3183 goto recovery;
3184
3185 /* Allocated area. */
3186 va = vas[area];
3187 va->va_start = start;
3188 va->va_end = start + size;
3189
3190 insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
Tejun Heoca23e402009-08-14 15:00:52 +09003191 }
3192
Tejun Heoca23e402009-08-14 15:00:52 +09003193 spin_unlock(&vmap_area_lock);
3194
3195 /* insert all vm's */
3196 for (area = 0; area < nr_vms; area++)
Zhang Yanfei3645cb42013-07-03 15:04:48 -07003197 setup_vmalloc_vm(vms[area], vas[area], VM_ALLOC,
3198 pcpu_get_vm_areas);
Tejun Heoca23e402009-08-14 15:00:52 +09003199
3200 kfree(vas);
3201 return vms;
3202
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003203recovery:
3204 /* Remove previously inserted areas. */
3205 while (area--) {
3206 __free_vmap_area(vas[area]);
3207 vas[area] = NULL;
3208 }
3209
3210overflow:
3211 spin_unlock(&vmap_area_lock);
3212 if (!purged) {
3213 purge_vmap_area_lazy();
3214 purged = true;
3215
3216 /* Before "retry", check if we recover. */
3217 for (area = 0; area < nr_vms; area++) {
3218 if (vas[area])
3219 continue;
3220
3221 vas[area] = kmem_cache_zalloc(
3222 vmap_area_cachep, GFP_KERNEL);
3223 if (!vas[area])
3224 goto err_free;
3225 }
3226
3227 goto retry;
3228 }
3229
Tejun Heoca23e402009-08-14 15:00:52 +09003230err_free:
3231 for (area = 0; area < nr_vms; area++) {
Uladzislau Rezki (Sony)68ad4a32019-05-17 14:31:31 -07003232 if (vas[area])
3233 kmem_cache_free(vmap_area_cachep, vas[area]);
3234
Kautuk Consulf1db7af2012-01-12 17:20:08 -08003235 kfree(vms[area]);
Tejun Heoca23e402009-08-14 15:00:52 +09003236 }
Kautuk Consulf1db7af2012-01-12 17:20:08 -08003237err_free2:
Tejun Heoca23e402009-08-14 15:00:52 +09003238 kfree(vas);
3239 kfree(vms);
3240 return NULL;
3241}
3242
3243/**
3244 * pcpu_free_vm_areas - free vmalloc areas for percpu allocator
3245 * @vms: vm_struct pointer array returned by pcpu_get_vm_areas()
3246 * @nr_vms: the number of allocated areas
3247 *
3248 * Free vm_structs and the array allocated by pcpu_get_vm_areas().
3249 */
3250void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
3251{
3252 int i;
3253
3254 for (i = 0; i < nr_vms; i++)
3255 free_vm_area(vms[i]);
3256 kfree(vms);
3257}
Tejun Heo4f8b02b2010-09-03 18:22:47 +02003258#endif /* CONFIG_SMP */
Christoph Lametera10aa572008-04-28 02:12:40 -07003259
3260#ifdef CONFIG_PROC_FS
3261static void *s_start(struct seq_file *m, loff_t *pos)
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003262 __acquires(&vmap_area_lock)
Christoph Lametera10aa572008-04-28 02:12:40 -07003263{
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003264 spin_lock(&vmap_area_lock);
zijun_hu3f500062016-12-12 16:42:17 -08003265 return seq_list_start(&vmap_area_list, *pos);
Christoph Lametera10aa572008-04-28 02:12:40 -07003266}
3267
3268static void *s_next(struct seq_file *m, void *p, loff_t *pos)
3269{
zijun_hu3f500062016-12-12 16:42:17 -08003270 return seq_list_next(p, &vmap_area_list, pos);
Christoph Lametera10aa572008-04-28 02:12:40 -07003271}
3272
3273static void s_stop(struct seq_file *m, void *p)
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003274 __releases(&vmap_area_lock)
Christoph Lametera10aa572008-04-28 02:12:40 -07003275{
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003276 spin_unlock(&vmap_area_lock);
Christoph Lametera10aa572008-04-28 02:12:40 -07003277}
3278
Eric Dumazeta47a1262008-07-23 21:27:38 -07003279static void show_numa_info(struct seq_file *m, struct vm_struct *v)
3280{
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08003281 if (IS_ENABLED(CONFIG_NUMA)) {
Eric Dumazeta47a1262008-07-23 21:27:38 -07003282 unsigned int nr, *counters = m->private;
3283
3284 if (!counters)
3285 return;
3286
Wanpeng Liaf123462013-11-12 15:07:32 -08003287 if (v->flags & VM_UNINITIALIZED)
3288 return;
Dmitry Vyukov7e5b5282014-12-12 16:56:30 -08003289 /* Pair with smp_wmb() in clear_vm_uninitialized_flag() */
3290 smp_rmb();
Wanpeng Liaf123462013-11-12 15:07:32 -08003291
Eric Dumazeta47a1262008-07-23 21:27:38 -07003292 memset(counters, 0, nr_node_ids * sizeof(unsigned int));
3293
3294 for (nr = 0; nr < v->nr_pages; nr++)
3295 counters[page_to_nid(v->pages[nr])]++;
3296
3297 for_each_node_state(nr, N_HIGH_MEMORY)
3298 if (counters[nr])
3299 seq_printf(m, " N%u=%u", nr, counters[nr]);
3300 }
3301}
3302
Christoph Lametera10aa572008-04-28 02:12:40 -07003303static int s_show(struct seq_file *m, void *p)
3304{
zijun_hu3f500062016-12-12 16:42:17 -08003305 struct vmap_area *va;
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003306 struct vm_struct *v;
3307
zijun_hu3f500062016-12-12 16:42:17 -08003308 va = list_entry(p, struct vmap_area, list);
3309
Wanpeng Lic2ce8c12013-11-12 15:07:31 -08003310 /*
3311 * s_show can encounter race with remove_vm_area, !VM_VM_AREA on
3312 * behalf of vmap area is being tear down or vm_map_ram allocation.
3313 */
Yisheng Xie78c72742017-07-10 15:48:09 -07003314 if (!(va->flags & VM_VM_AREA)) {
3315 seq_printf(m, "0x%pK-0x%pK %7ld %s\n",
3316 (void *)va->va_start, (void *)va->va_end,
3317 va->va_end - va->va_start,
3318 va->flags & VM_LAZY_FREE ? "unpurged vm_area" : "vm_map_ram");
3319
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003320 return 0;
Yisheng Xie78c72742017-07-10 15:48:09 -07003321 }
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003322
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003323 v = va->vm;
Christoph Lametera10aa572008-04-28 02:12:40 -07003324
Kees Cook45ec1692012-10-08 16:34:09 -07003325 seq_printf(m, "0x%pK-0x%pK %7ld",
Christoph Lametera10aa572008-04-28 02:12:40 -07003326 v->addr, v->addr + v->size, v->size);
3327
Joe Perches62c70bc2011-01-13 15:45:52 -08003328 if (v->caller)
3329 seq_printf(m, " %pS", v->caller);
Christoph Lameter23016962008-04-28 02:12:42 -07003330
Christoph Lametera10aa572008-04-28 02:12:40 -07003331 if (v->nr_pages)
3332 seq_printf(m, " pages=%d", v->nr_pages);
3333
3334 if (v->phys_addr)
Miles Chen199eaa02017-02-24 14:59:51 -08003335 seq_printf(m, " phys=%pa", &v->phys_addr);
Christoph Lametera10aa572008-04-28 02:12:40 -07003336
3337 if (v->flags & VM_IOREMAP)
Fabian Frederickf4527c92014-06-04 16:08:09 -07003338 seq_puts(m, " ioremap");
Christoph Lametera10aa572008-04-28 02:12:40 -07003339
3340 if (v->flags & VM_ALLOC)
Fabian Frederickf4527c92014-06-04 16:08:09 -07003341 seq_puts(m, " vmalloc");
Christoph Lametera10aa572008-04-28 02:12:40 -07003342
3343 if (v->flags & VM_MAP)
Fabian Frederickf4527c92014-06-04 16:08:09 -07003344 seq_puts(m, " vmap");
Christoph Lametera10aa572008-04-28 02:12:40 -07003345
3346 if (v->flags & VM_USERMAP)
Fabian Frederickf4527c92014-06-04 16:08:09 -07003347 seq_puts(m, " user");
Christoph Lametera10aa572008-04-28 02:12:40 -07003348
David Rientjes244d63e2016-01-14 15:19:35 -08003349 if (is_vmalloc_addr(v->pages))
Fabian Frederickf4527c92014-06-04 16:08:09 -07003350 seq_puts(m, " vpages");
Christoph Lametera10aa572008-04-28 02:12:40 -07003351
Eric Dumazeta47a1262008-07-23 21:27:38 -07003352 show_numa_info(m, v);
Christoph Lametera10aa572008-04-28 02:12:40 -07003353 seq_putc(m, '\n');
3354 return 0;
3355}
3356
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04003357static const struct seq_operations vmalloc_op = {
Christoph Lametera10aa572008-04-28 02:12:40 -07003358 .start = s_start,
3359 .next = s_next,
3360 .stop = s_stop,
3361 .show = s_show,
3362};
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04003363
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04003364static int __init proc_vmalloc_init(void)
3365{
Christoph Hellwigfddda2b2018-04-13 19:44:18 +02003366 if (IS_ENABLED(CONFIG_NUMA))
Joe Perches0825a6f2018-06-14 15:27:58 -07003367 proc_create_seq_private("vmallocinfo", 0400, NULL,
Christoph Hellwig44414d82018-04-24 17:05:17 +02003368 &vmalloc_op,
3369 nr_node_ids * sizeof(unsigned int), NULL);
Christoph Hellwigfddda2b2018-04-13 19:44:18 +02003370 else
Joe Perches0825a6f2018-06-14 15:27:58 -07003371 proc_create_seq("vmallocinfo", 0400, NULL, &vmalloc_op);
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04003372 return 0;
3373}
3374module_init(proc_vmalloc_init);
Joonsoo Kimdb3808c2013-04-29 15:07:28 -07003375
Christoph Lametera10aa572008-04-28 02:12:40 -07003376#endif