blob: d7ca718ed614a2a26c26689f81fe6ff661780d80 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/mm/vmalloc.c
3 *
4 * Copyright (C) 1993 Linus Torvalds
5 * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
6 * SMP-safe vmalloc/vfree/ioremap, Tigran Aivazian <tigran@veritas.com>, May 2000
7 * Major rework to support vmap/vunmap, Christoph Hellwig, SGI, August 2002
Christoph Lameter930fc452005-10-29 18:15:41 -07008 * Numa awareness, Christoph Lameter, SGI, June 2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Nick Piggindb64fe02008-10-18 20:27:03 -070011#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/mm.h>
13#include <linux/module.h>
14#include <linux/highmem.h>
Ingo Molnarc3edc402017-02-02 08:35:14 +010015#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/slab.h>
17#include <linux/spinlock.h>
18#include <linux/interrupt.h>
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +040019#include <linux/proc_fs.h>
Christoph Lametera10aa572008-04-28 02:12:40 -070020#include <linux/seq_file.h>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070021#include <linux/debugobjects.h>
Christoph Lameter23016962008-04-28 02:12:42 -070022#include <linux/kallsyms.h>
Nick Piggindb64fe02008-10-18 20:27:03 -070023#include <linux/list.h>
Chris Wilson4da56b92016-04-04 14:46:42 +010024#include <linux/notifier.h>
Nick Piggindb64fe02008-10-18 20:27:03 -070025#include <linux/rbtree.h>
26#include <linux/radix-tree.h>
27#include <linux/rcupdate.h>
Tejun Heof0aa6612009-02-20 16:29:08 +090028#include <linux/pfn.h>
Catalin Marinas89219d32009-06-11 13:23:19 +010029#include <linux/kmemleak.h>
Arun Sharma600634972011-07-26 16:09:06 -070030#include <linux/atomic.h>
Gideon Israel Dsouza3b321232014-04-07 15:37:26 -070031#include <linux/compiler.h>
Al Viro32fcfd42013-03-10 20:14:08 -040032#include <linux/llist.h>
Toshi Kani0f616be2015-04-14 15:47:17 -070033#include <linux/bitops.h>
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -070034#include <linux/rbtree_augmented.h>
Gideon Israel Dsouza3b321232014-04-07 15:37:26 -070035
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080036#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/tlbflush.h>
David Miller2dca6992009-09-21 12:22:34 -070038#include <asm/shmparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Mel Gormandd56b042015-11-06 16:28:43 -080040#include "internal.h"
41
Al Viro32fcfd42013-03-10 20:14:08 -040042struct vfree_deferred {
43 struct llist_head list;
44 struct work_struct wq;
45};
46static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred);
47
48static void __vunmap(const void *, int);
49
50static void free_work(struct work_struct *w)
51{
52 struct vfree_deferred *p = container_of(w, struct vfree_deferred, wq);
Byungchul Park894e58c2017-09-06 16:24:26 -070053 struct llist_node *t, *llnode;
54
55 llist_for_each_safe(llnode, t, llist_del_all(&p->list))
56 __vunmap((void *)llnode, 1);
Al Viro32fcfd42013-03-10 20:14:08 -040057}
58
Nick Piggindb64fe02008-10-18 20:27:03 -070059/*** Page table manipulation functions ***/
Adrian Bunkb2213852006-09-25 23:31:02 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end)
62{
63 pte_t *pte;
64
65 pte = pte_offset_kernel(pmd, addr);
66 do {
67 pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte);
68 WARN_ON(!pte_none(ptent) && !pte_present(ptent));
69 } while (pte++, addr += PAGE_SIZE, addr != end);
70}
71
Nick Piggindb64fe02008-10-18 20:27:03 -070072static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
74 pmd_t *pmd;
75 unsigned long next;
76
77 pmd = pmd_offset(pud, addr);
78 do {
79 next = pmd_addr_end(addr, end);
Toshi Kanib9820d82015-04-14 15:47:26 -070080 if (pmd_clear_huge(pmd))
81 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if (pmd_none_or_clear_bad(pmd))
83 continue;
84 vunmap_pte_range(pmd, addr, next);
85 } while (pmd++, addr = next, addr != end);
86}
87
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +030088static void vunmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90 pud_t *pud;
91 unsigned long next;
92
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +030093 pud = pud_offset(p4d, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 do {
95 next = pud_addr_end(addr, end);
Toshi Kanib9820d82015-04-14 15:47:26 -070096 if (pud_clear_huge(pud))
97 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (pud_none_or_clear_bad(pud))
99 continue;
100 vunmap_pmd_range(pud, addr, next);
101 } while (pud++, addr = next, addr != end);
102}
103
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300104static void vunmap_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end)
105{
106 p4d_t *p4d;
107 unsigned long next;
108
109 p4d = p4d_offset(pgd, addr);
110 do {
111 next = p4d_addr_end(addr, end);
112 if (p4d_clear_huge(p4d))
113 continue;
114 if (p4d_none_or_clear_bad(p4d))
115 continue;
116 vunmap_pud_range(p4d, addr, next);
117 } while (p4d++, addr = next, addr != end);
118}
119
Nick Piggindb64fe02008-10-18 20:27:03 -0700120static void vunmap_page_range(unsigned long addr, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 pgd_t *pgd;
123 unsigned long next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 BUG_ON(addr >= end);
126 pgd = pgd_offset_k(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 do {
128 next = pgd_addr_end(addr, end);
129 if (pgd_none_or_clear_bad(pgd))
130 continue;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300131 vunmap_p4d_range(pgd, addr, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 } while (pgd++, addr = next, addr != end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
135static int vmap_pte_range(pmd_t *pmd, unsigned long addr,
Nick Piggindb64fe02008-10-18 20:27:03 -0700136 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 pte_t *pte;
139
Nick Piggindb64fe02008-10-18 20:27:03 -0700140 /*
141 * nr is a running index into the array which helps higher level
142 * callers keep track of where we're up to.
143 */
144
Hugh Dickins872fec12005-10-29 18:16:21 -0700145 pte = pte_alloc_kernel(pmd, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (!pte)
147 return -ENOMEM;
148 do {
Nick Piggindb64fe02008-10-18 20:27:03 -0700149 struct page *page = pages[*nr];
150
151 if (WARN_ON(!pte_none(*pte)))
152 return -EBUSY;
153 if (WARN_ON(!page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 return -ENOMEM;
155 set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
Nick Piggindb64fe02008-10-18 20:27:03 -0700156 (*nr)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 } while (pte++, addr += PAGE_SIZE, addr != end);
158 return 0;
159}
160
Nick Piggindb64fe02008-10-18 20:27:03 -0700161static int vmap_pmd_range(pud_t *pud, unsigned long addr,
162 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 pmd_t *pmd;
165 unsigned long next;
166
167 pmd = pmd_alloc(&init_mm, pud, addr);
168 if (!pmd)
169 return -ENOMEM;
170 do {
171 next = pmd_addr_end(addr, end);
Nick Piggindb64fe02008-10-18 20:27:03 -0700172 if (vmap_pte_range(pmd, addr, next, prot, pages, nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return -ENOMEM;
174 } while (pmd++, addr = next, addr != end);
175 return 0;
176}
177
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300178static int vmap_pud_range(p4d_t *p4d, unsigned long addr,
Nick Piggindb64fe02008-10-18 20:27:03 -0700179 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 pud_t *pud;
182 unsigned long next;
183
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300184 pud = pud_alloc(&init_mm, p4d, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (!pud)
186 return -ENOMEM;
187 do {
188 next = pud_addr_end(addr, end);
Nick Piggindb64fe02008-10-18 20:27:03 -0700189 if (vmap_pmd_range(pud, addr, next, prot, pages, nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return -ENOMEM;
191 } while (pud++, addr = next, addr != end);
192 return 0;
193}
194
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300195static int vmap_p4d_range(pgd_t *pgd, unsigned long addr,
196 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
197{
198 p4d_t *p4d;
199 unsigned long next;
200
201 p4d = p4d_alloc(&init_mm, pgd, addr);
202 if (!p4d)
203 return -ENOMEM;
204 do {
205 next = p4d_addr_end(addr, end);
206 if (vmap_pud_range(p4d, addr, next, prot, pages, nr))
207 return -ENOMEM;
208 } while (p4d++, addr = next, addr != end);
209 return 0;
210}
211
Nick Piggindb64fe02008-10-18 20:27:03 -0700212/*
213 * Set up page tables in kva (addr, end). The ptes shall have prot "prot", and
214 * will have pfns corresponding to the "pages" array.
215 *
216 * Ie. pte at addr+N*PAGE_SIZE shall point to pfn corresponding to pages[N]
217 */
Tejun Heo8fc48982009-02-20 16:29:08 +0900218static int vmap_page_range_noflush(unsigned long start, unsigned long end,
219 pgprot_t prot, struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 pgd_t *pgd;
222 unsigned long next;
Adam Lackorzynski2e4e27c2009-01-04 12:00:46 -0800223 unsigned long addr = start;
Nick Piggindb64fe02008-10-18 20:27:03 -0700224 int err = 0;
225 int nr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 BUG_ON(addr >= end);
228 pgd = pgd_offset_k(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 do {
230 next = pgd_addr_end(addr, end);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300231 err = vmap_p4d_range(pgd, addr, next, prot, pages, &nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (err)
Figo.zhangbf88c8c2009-09-21 17:01:47 -0700233 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 } while (pgd++, addr = next, addr != end);
Nick Piggindb64fe02008-10-18 20:27:03 -0700235
Nick Piggindb64fe02008-10-18 20:27:03 -0700236 return nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
Tejun Heo8fc48982009-02-20 16:29:08 +0900239static int vmap_page_range(unsigned long start, unsigned long end,
240 pgprot_t prot, struct page **pages)
241{
242 int ret;
243
244 ret = vmap_page_range_noflush(start, end, prot, pages);
245 flush_cache_vmap(start, end);
246 return ret;
247}
248
KAMEZAWA Hiroyuki81ac3ad2009-09-22 16:45:49 -0700249int is_vmalloc_or_module_addr(const void *x)
Linus Torvalds73bdf0a2008-10-15 08:35:12 -0700250{
251 /*
Russell Kingab4f2ee2008-11-06 17:11:07 +0000252 * ARM, x86-64 and sparc64 put modules in a special place,
Linus Torvalds73bdf0a2008-10-15 08:35:12 -0700253 * and fall back on vmalloc() if that fails. Others
254 * just put it in the vmalloc space.
255 */
256#if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
257 unsigned long addr = (unsigned long)x;
258 if (addr >= MODULES_VADDR && addr < MODULES_END)
259 return 1;
260#endif
261 return is_vmalloc_addr(x);
262}
263
Christoph Lameter48667e72008-02-04 22:28:31 -0800264/*
malcadd688f2014-01-27 17:06:53 -0800265 * Walk a vmap address to the struct page it maps.
Christoph Lameter48667e72008-02-04 22:28:31 -0800266 */
malcadd688f2014-01-27 17:06:53 -0800267struct page *vmalloc_to_page(const void *vmalloc_addr)
Christoph Lameter48667e72008-02-04 22:28:31 -0800268{
269 unsigned long addr = (unsigned long) vmalloc_addr;
malcadd688f2014-01-27 17:06:53 -0800270 struct page *page = NULL;
Christoph Lameter48667e72008-02-04 22:28:31 -0800271 pgd_t *pgd = pgd_offset_k(addr);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300272 p4d_t *p4d;
273 pud_t *pud;
274 pmd_t *pmd;
275 pte_t *ptep, pte;
Christoph Lameter48667e72008-02-04 22:28:31 -0800276
Ingo Molnar7aa413d2008-06-19 13:28:11 +0200277 /*
278 * XXX we might need to change this if we add VIRTUAL_BUG_ON for
279 * architectures that do not vmalloc module space
280 */
Linus Torvalds73bdf0a2008-10-15 08:35:12 -0700281 VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr));
Jiri Slaby59ea7462008-06-12 13:56:40 +0200282
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300283 if (pgd_none(*pgd))
284 return NULL;
285 p4d = p4d_offset(pgd, addr);
286 if (p4d_none(*p4d))
287 return NULL;
288 pud = pud_offset(p4d, addr);
Ard Biesheuvel029c54b2017-06-23 15:08:41 -0700289
290 /*
291 * Don't dereference bad PUD or PMD (below) entries. This will also
292 * identify huge mappings, which we may encounter on architectures
293 * that define CONFIG_HAVE_ARCH_HUGE_VMAP=y. Such regions will be
294 * identified as vmalloc addresses by is_vmalloc_addr(), but are
295 * not [unambiguously] associated with a struct page, so there is
296 * no correct value to return for them.
297 */
298 WARN_ON_ONCE(pud_bad(*pud));
299 if (pud_none(*pud) || pud_bad(*pud))
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300300 return NULL;
301 pmd = pmd_offset(pud, addr);
Ard Biesheuvel029c54b2017-06-23 15:08:41 -0700302 WARN_ON_ONCE(pmd_bad(*pmd));
303 if (pmd_none(*pmd) || pmd_bad(*pmd))
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300304 return NULL;
Nick Piggindb64fe02008-10-18 20:27:03 -0700305
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300306 ptep = pte_offset_map(pmd, addr);
307 pte = *ptep;
308 if (pte_present(pte))
309 page = pte_page(pte);
310 pte_unmap(ptep);
malcadd688f2014-01-27 17:06:53 -0800311 return page;
Jianyu Zhanece86e222014-01-21 15:49:12 -0800312}
313EXPORT_SYMBOL(vmalloc_to_page);
314
malcadd688f2014-01-27 17:06:53 -0800315/*
316 * Map a vmalloc()-space virtual address to the physical page frame number.
317 */
318unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
319{
320 return page_to_pfn(vmalloc_to_page(vmalloc_addr));
321}
322EXPORT_SYMBOL(vmalloc_to_pfn);
323
Nick Piggindb64fe02008-10-18 20:27:03 -0700324
325/*** Global kva allocator ***/
326
Uladzislau Rezki (Sony)8087b172019-05-17 14:31:34 -0700327#define DEBUG_AUGMENT_PROPAGATE_CHECK 0
Uladzislau Rezki (Sony)367c1e42019-05-17 14:31:37 -0700328#define DEBUG_AUGMENT_LOWEST_MATCH_CHECK 0
Uladzislau Rezki (Sony)8087b172019-05-17 14:31:34 -0700329
Yisheng Xie78c72742017-07-10 15:48:09 -0700330#define VM_LAZY_FREE 0x02
Nick Piggindb64fe02008-10-18 20:27:03 -0700331#define VM_VM_AREA 0x04
332
Nick Piggindb64fe02008-10-18 20:27:03 -0700333static DEFINE_SPINLOCK(vmap_area_lock);
Joonsoo Kimf1c40692013-04-29 15:07:37 -0700334/* Export for kexec only */
335LIST_HEAD(vmap_area_list);
Chris Wilson80c4bd72016-05-20 16:57:38 -0700336static LLIST_HEAD(vmap_purge_list);
Nick Piggin89699602011-03-22 16:30:36 -0700337static struct rb_root vmap_area_root = RB_ROOT;
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -0700338static bool vmap_initialized __read_mostly;
Nick Piggin89699602011-03-22 16:30:36 -0700339
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -0700340/*
341 * This kmem_cache is used for vmap_area objects. Instead of
342 * allocating from slab we reuse an object from this cache to
343 * make things faster. Especially in "no edge" splitting of
344 * free block.
345 */
346static struct kmem_cache *vmap_area_cachep;
Nick Piggin89699602011-03-22 16:30:36 -0700347
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -0700348/*
349 * This linked list is used in pair with free_vmap_area_root.
350 * It gives O(1) access to prev/next to perform fast coalescing.
351 */
352static LIST_HEAD(free_vmap_area_list);
353
354/*
355 * This augment red-black tree represents the free vmap space.
356 * All vmap_area objects in this tree are sorted by va->va_start
357 * address. It is used for allocation and merging when a vmap
358 * object is released.
359 *
360 * Each vmap_area node contains a maximum available free block
361 * of its sub-tree, right or left. Therefore it is possible to
362 * find a lowest match of free area.
363 */
364static struct rb_root free_vmap_area_root = RB_ROOT;
365
366static __always_inline unsigned long
367va_size(struct vmap_area *va)
368{
369 return (va->va_end - va->va_start);
370}
371
372static __always_inline unsigned long
373get_subtree_max_size(struct rb_node *node)
374{
375 struct vmap_area *va;
376
377 va = rb_entry_safe(node, struct vmap_area, rb_node);
378 return va ? va->subtree_max_size : 0;
379}
380
381/*
382 * Gets called when remove the node and rotate.
383 */
384static __always_inline unsigned long
385compute_subtree_max_size(struct vmap_area *va)
386{
387 return max3(va_size(va),
388 get_subtree_max_size(va->rb_node.rb_left),
389 get_subtree_max_size(va->rb_node.rb_right));
390}
391
392RB_DECLARE_CALLBACKS(static, free_vmap_area_rb_augment_cb,
393 struct vmap_area, rb_node, unsigned long, subtree_max_size,
394 compute_subtree_max_size)
395
396static void purge_vmap_area_lazy(void);
397static BLOCKING_NOTIFIER_HEAD(vmap_notify_list);
398static unsigned long lazy_max_pages(void);
Nick Piggindb64fe02008-10-18 20:27:03 -0700399
Roman Gushchindb70fefd2019-02-25 12:30:37 -0800400static atomic_long_t nr_vmalloc_pages;
401
402unsigned long vmalloc_nr_pages(void)
403{
404 return atomic_long_read(&nr_vmalloc_pages);
405}
406
Nick Piggindb64fe02008-10-18 20:27:03 -0700407static struct vmap_area *__find_vmap_area(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Nick Piggindb64fe02008-10-18 20:27:03 -0700409 struct rb_node *n = vmap_area_root.rb_node;
410
411 while (n) {
412 struct vmap_area *va;
413
414 va = rb_entry(n, struct vmap_area, rb_node);
415 if (addr < va->va_start)
416 n = n->rb_left;
HATAYAMA Daisukecef2ac32013-07-03 15:02:17 -0700417 else if (addr >= va->va_end)
Nick Piggindb64fe02008-10-18 20:27:03 -0700418 n = n->rb_right;
419 else
420 return va;
421 }
422
423 return NULL;
424}
425
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -0700426/*
427 * This function returns back addresses of parent node
428 * and its left or right link for further processing.
429 */
430static __always_inline struct rb_node **
431find_va_links(struct vmap_area *va,
432 struct rb_root *root, struct rb_node *from,
433 struct rb_node **parent)
Nick Piggindb64fe02008-10-18 20:27:03 -0700434{
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -0700435 struct vmap_area *tmp_va;
436 struct rb_node **link;
Nick Piggindb64fe02008-10-18 20:27:03 -0700437
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -0700438 if (root) {
439 link = &root->rb_node;
440 if (unlikely(!*link)) {
441 *parent = NULL;
442 return link;
443 }
444 } else {
445 link = &from;
Nick Piggindb64fe02008-10-18 20:27:03 -0700446 }
447
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -0700448 /*
449 * Go to the bottom of the tree. When we hit the last point
450 * we end up with parent rb_node and correct direction, i name
451 * it link, where the new va->rb_node will be attached to.
452 */
453 do {
454 tmp_va = rb_entry(*link, struct vmap_area, rb_node);
Nick Piggindb64fe02008-10-18 20:27:03 -0700455
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -0700456 /*
457 * During the traversal we also do some sanity check.
458 * Trigger the BUG() if there are sides(left/right)
459 * or full overlaps.
460 */
461 if (va->va_start < tmp_va->va_end &&
462 va->va_end <= tmp_va->va_start)
463 link = &(*link)->rb_left;
464 else if (va->va_end > tmp_va->va_start &&
465 va->va_start >= tmp_va->va_end)
466 link = &(*link)->rb_right;
467 else
468 BUG();
469 } while (*link);
470
471 *parent = &tmp_va->rb_node;
472 return link;
Nick Piggindb64fe02008-10-18 20:27:03 -0700473}
474
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -0700475static __always_inline struct list_head *
476get_va_next_sibling(struct rb_node *parent, struct rb_node **link)
477{
478 struct list_head *list;
Nick Piggindb64fe02008-10-18 20:27:03 -0700479
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -0700480 if (unlikely(!parent))
481 /*
482 * The red-black tree where we try to find VA neighbors
483 * before merging or inserting is empty, i.e. it means
484 * there is no free vmap space. Normally it does not
485 * happen but we handle this case anyway.
486 */
487 return NULL;
488
489 list = &rb_entry(parent, struct vmap_area, rb_node)->list;
490 return (&parent->rb_right == link ? list->next : list);
491}
492
493static __always_inline void
494link_va(struct vmap_area *va, struct rb_root *root,
495 struct rb_node *parent, struct rb_node **link, struct list_head *head)
496{
497 /*
498 * VA is still not in the list, but we can
499 * identify its future previous list_head node.
500 */
501 if (likely(parent)) {
502 head = &rb_entry(parent, struct vmap_area, rb_node)->list;
503 if (&parent->rb_right != link)
504 head = head->prev;
505 }
506
507 /* Insert to the rb-tree */
508 rb_link_node(&va->rb_node, parent, link);
509 if (root == &free_vmap_area_root) {
510 /*
511 * Some explanation here. Just perform simple insertion
512 * to the tree. We do not set va->subtree_max_size to
513 * its current size before calling rb_insert_augmented().
514 * It is because of we populate the tree from the bottom
515 * to parent levels when the node _is_ in the tree.
516 *
517 * Therefore we set subtree_max_size to zero after insertion,
518 * to let __augment_tree_propagate_from() puts everything to
519 * the correct order later on.
520 */
521 rb_insert_augmented(&va->rb_node,
522 root, &free_vmap_area_rb_augment_cb);
523 va->subtree_max_size = 0;
524 } else {
525 rb_insert_color(&va->rb_node, root);
526 }
527
528 /* Address-sort this list */
529 list_add(&va->list, head);
530}
531
532static __always_inline void
533unlink_va(struct vmap_area *va, struct rb_root *root)
534{
535 /*
536 * During merging a VA node can be empty, therefore
537 * not linked with the tree nor list. Just check it.
538 */
539 if (!RB_EMPTY_NODE(&va->rb_node)) {
540 if (root == &free_vmap_area_root)
541 rb_erase_augmented(&va->rb_node,
542 root, &free_vmap_area_rb_augment_cb);
543 else
544 rb_erase(&va->rb_node, root);
545
546 list_del(&va->list);
547 RB_CLEAR_NODE(&va->rb_node);
548 }
549}
550
Uladzislau Rezki (Sony)8087b172019-05-17 14:31:34 -0700551#if DEBUG_AUGMENT_PROPAGATE_CHECK
552static void
553augment_tree_propagate_check(struct rb_node *n)
554{
555 struct vmap_area *va;
556 struct rb_node *node;
557 unsigned long size;
558 bool found = false;
559
560 if (n == NULL)
561 return;
562
563 va = rb_entry(n, struct vmap_area, rb_node);
564 size = va->subtree_max_size;
565 node = n;
566
567 while (node) {
568 va = rb_entry(node, struct vmap_area, rb_node);
569
570 if (get_subtree_max_size(node->rb_left) == size) {
571 node = node->rb_left;
572 } else {
573 if (va_size(va) == size) {
574 found = true;
575 break;
576 }
577
578 node = node->rb_right;
579 }
580 }
581
582 if (!found) {
583 va = rb_entry(n, struct vmap_area, rb_node);
584 pr_emerg("tree is corrupted: %lu, %lu\n",
585 va_size(va), va->subtree_max_size);
586 }
587
588 augment_tree_propagate_check(n->rb_left);
589 augment_tree_propagate_check(n->rb_right);
590}
591#endif
592
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -0700593/*
594 * This function populates subtree_max_size from bottom to upper
595 * levels starting from VA point. The propagation must be done
596 * when VA size is modified by changing its va_start/va_end. Or
597 * in case of newly inserting of VA to the tree.
598 *
599 * It means that __augment_tree_propagate_from() must be called:
600 * - After VA has been inserted to the tree(free path);
601 * - After VA has been shrunk(allocation path);
602 * - After VA has been increased(merging path).
603 *
604 * Please note that, it does not mean that upper parent nodes
605 * and their subtree_max_size are recalculated all the time up
606 * to the root node.
607 *
608 * 4--8
609 * /\
610 * / \
611 * / \
612 * 2--2 8--8
613 *
614 * For example if we modify the node 4, shrinking it to 2, then
615 * no any modification is required. If we shrink the node 2 to 1
616 * its subtree_max_size is updated only, and set to 1. If we shrink
617 * the node 8 to 6, then its subtree_max_size is set to 6 and parent
618 * node becomes 4--6.
619 */
620static __always_inline void
621augment_tree_propagate_from(struct vmap_area *va)
622{
623 struct rb_node *node = &va->rb_node;
624 unsigned long new_va_sub_max_size;
625
626 while (node) {
627 va = rb_entry(node, struct vmap_area, rb_node);
628 new_va_sub_max_size = compute_subtree_max_size(va);
629
630 /*
631 * If the newly calculated maximum available size of the
632 * subtree is equal to the current one, then it means that
633 * the tree is propagated correctly. So we have to stop at
634 * this point to save cycles.
635 */
636 if (va->subtree_max_size == new_va_sub_max_size)
637 break;
638
639 va->subtree_max_size = new_va_sub_max_size;
640 node = rb_parent(&va->rb_node);
641 }
Uladzislau Rezki (Sony)8087b172019-05-17 14:31:34 -0700642
643#if DEBUG_AUGMENT_PROPAGATE_CHECK
644 augment_tree_propagate_check(free_vmap_area_root.rb_node);
645#endif
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -0700646}
647
648static void
649insert_vmap_area(struct vmap_area *va,
650 struct rb_root *root, struct list_head *head)
651{
652 struct rb_node **link;
653 struct rb_node *parent;
654
655 link = find_va_links(va, root, NULL, &parent);
656 link_va(va, root, parent, link, head);
657}
658
659static void
660insert_vmap_area_augment(struct vmap_area *va,
661 struct rb_node *from, struct rb_root *root,
662 struct list_head *head)
663{
664 struct rb_node **link;
665 struct rb_node *parent;
666
667 if (from)
668 link = find_va_links(va, NULL, from, &parent);
669 else
670 link = find_va_links(va, root, NULL, &parent);
671
672 link_va(va, root, parent, link, head);
673 augment_tree_propagate_from(va);
674}
675
676/*
677 * Merge de-allocated chunk of VA memory with previous
678 * and next free blocks. If coalesce is not done a new
679 * free area is inserted. If VA has been merged, it is
680 * freed.
681 */
682static __always_inline void
683merge_or_add_vmap_area(struct vmap_area *va,
684 struct rb_root *root, struct list_head *head)
685{
686 struct vmap_area *sibling;
687 struct list_head *next;
688 struct rb_node **link;
689 struct rb_node *parent;
690 bool merged = false;
691
692 /*
693 * Find a place in the tree where VA potentially will be
694 * inserted, unless it is merged with its sibling/siblings.
695 */
696 link = find_va_links(va, root, NULL, &parent);
697
698 /*
699 * Get next node of VA to check if merging can be done.
700 */
701 next = get_va_next_sibling(parent, link);
702 if (unlikely(next == NULL))
703 goto insert;
704
705 /*
706 * start end
707 * | |
708 * |<------VA------>|<-----Next----->|
709 * | |
710 * start end
711 */
712 if (next != head) {
713 sibling = list_entry(next, struct vmap_area, list);
714 if (sibling->va_start == va->va_end) {
715 sibling->va_start = va->va_start;
716
717 /* Check and update the tree if needed. */
718 augment_tree_propagate_from(sibling);
719
720 /* Remove this VA, it has been merged. */
721 unlink_va(va, root);
722
723 /* Free vmap_area object. */
724 kmem_cache_free(vmap_area_cachep, va);
725
726 /* Point to the new merged area. */
727 va = sibling;
728 merged = true;
729 }
730 }
731
732 /*
733 * start end
734 * | |
735 * |<-----Prev----->|<------VA------>|
736 * | |
737 * start end
738 */
739 if (next->prev != head) {
740 sibling = list_entry(next->prev, struct vmap_area, list);
741 if (sibling->va_end == va->va_start) {
742 sibling->va_end = va->va_end;
743
744 /* Check and update the tree if needed. */
745 augment_tree_propagate_from(sibling);
746
747 /* Remove this VA, it has been merged. */
748 unlink_va(va, root);
749
750 /* Free vmap_area object. */
751 kmem_cache_free(vmap_area_cachep, va);
752
753 return;
754 }
755 }
756
757insert:
758 if (!merged) {
759 link_va(va, root, parent, link, head);
760 augment_tree_propagate_from(va);
761 }
762}
763
764static __always_inline bool
765is_within_this_va(struct vmap_area *va, unsigned long size,
766 unsigned long align, unsigned long vstart)
767{
768 unsigned long nva_start_addr;
769
770 if (va->va_start > vstart)
771 nva_start_addr = ALIGN(va->va_start, align);
772 else
773 nva_start_addr = ALIGN(vstart, align);
774
775 /* Can be overflowed due to big size or alignment. */
776 if (nva_start_addr + size < nva_start_addr ||
777 nva_start_addr < vstart)
778 return false;
779
780 return (nva_start_addr + size <= va->va_end);
781}
782
783/*
784 * Find the first free block(lowest start address) in the tree,
785 * that will accomplish the request corresponding to passing
786 * parameters.
787 */
788static __always_inline struct vmap_area *
789find_vmap_lowest_match(unsigned long size,
790 unsigned long align, unsigned long vstart)
791{
792 struct vmap_area *va;
793 struct rb_node *node;
794 unsigned long length;
795
796 /* Start from the root. */
797 node = free_vmap_area_root.rb_node;
798
799 /* Adjust the search size for alignment overhead. */
800 length = size + align - 1;
801
802 while (node) {
803 va = rb_entry(node, struct vmap_area, rb_node);
804
805 if (get_subtree_max_size(node->rb_left) >= length &&
806 vstart < va->va_start) {
807 node = node->rb_left;
808 } else {
809 if (is_within_this_va(va, size, align, vstart))
810 return va;
811
812 /*
813 * Does not make sense to go deeper towards the right
814 * sub-tree if it does not have a free block that is
815 * equal or bigger to the requested search length.
816 */
817 if (get_subtree_max_size(node->rb_right) >= length) {
818 node = node->rb_right;
819 continue;
820 }
821
822 /*
823 * OK. We roll back and find the fist right sub-tree,
824 * that will satisfy the search criteria. It can happen
825 * only once due to "vstart" restriction.
826 */
827 while ((node = rb_parent(node))) {
828 va = rb_entry(node, struct vmap_area, rb_node);
829 if (is_within_this_va(va, size, align, vstart))
830 return va;
831
832 if (get_subtree_max_size(node->rb_right) >= length &&
833 vstart <= va->va_start) {
834 node = node->rb_right;
835 break;
836 }
837 }
838 }
839 }
840
841 return NULL;
842}
843
Uladzislau Rezki (Sony)367c1e42019-05-17 14:31:37 -0700844#if DEBUG_AUGMENT_LOWEST_MATCH_CHECK
845#include <linux/random.h>
846
847static struct vmap_area *
848find_vmap_lowest_linear_match(unsigned long size,
849 unsigned long align, unsigned long vstart)
850{
851 struct vmap_area *va;
852
853 list_for_each_entry(va, &free_vmap_area_list, list) {
854 if (!is_within_this_va(va, size, align, vstart))
855 continue;
856
857 return va;
858 }
859
860 return NULL;
861}
862
863static void
864find_vmap_lowest_match_check(unsigned long size)
865{
866 struct vmap_area *va_1, *va_2;
867 unsigned long vstart;
868 unsigned int rnd;
869
870 get_random_bytes(&rnd, sizeof(rnd));
871 vstart = VMALLOC_START + rnd;
872
873 va_1 = find_vmap_lowest_match(size, 1, vstart);
874 va_2 = find_vmap_lowest_linear_match(size, 1, vstart);
875
876 if (va_1 != va_2)
877 pr_emerg("not lowest: t: 0x%p, l: 0x%p, v: 0x%lx\n",
878 va_1, va_2, vstart);
879}
880#endif
881
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -0700882enum fit_type {
883 NOTHING_FIT = 0,
884 FL_FIT_TYPE = 1, /* full fit */
885 LE_FIT_TYPE = 2, /* left edge fit */
886 RE_FIT_TYPE = 3, /* right edge fit */
887 NE_FIT_TYPE = 4 /* no edge fit */
888};
889
890static __always_inline enum fit_type
891classify_va_fit_type(struct vmap_area *va,
892 unsigned long nva_start_addr, unsigned long size)
893{
894 enum fit_type type;
895
896 /* Check if it is within VA. */
897 if (nva_start_addr < va->va_start ||
898 nva_start_addr + size > va->va_end)
899 return NOTHING_FIT;
900
901 /* Now classify. */
902 if (va->va_start == nva_start_addr) {
903 if (va->va_end == nva_start_addr + size)
904 type = FL_FIT_TYPE;
905 else
906 type = LE_FIT_TYPE;
907 } else if (va->va_end == nva_start_addr + size) {
908 type = RE_FIT_TYPE;
909 } else {
910 type = NE_FIT_TYPE;
911 }
912
913 return type;
914}
915
916static __always_inline int
917adjust_va_to_fit_type(struct vmap_area *va,
918 unsigned long nva_start_addr, unsigned long size,
919 enum fit_type type)
920{
921 struct vmap_area *lva;
922
923 if (type == FL_FIT_TYPE) {
924 /*
925 * No need to split VA, it fully fits.
926 *
927 * | |
928 * V NVA V
929 * |---------------|
930 */
931 unlink_va(va, &free_vmap_area_root);
932 kmem_cache_free(vmap_area_cachep, va);
933 } else if (type == LE_FIT_TYPE) {
934 /*
935 * Split left edge of fit VA.
936 *
937 * | |
938 * V NVA V R
939 * |-------|-------|
940 */
941 va->va_start += size;
942 } else if (type == RE_FIT_TYPE) {
943 /*
944 * Split right edge of fit VA.
945 *
946 * | |
947 * L V NVA V
948 * |-------|-------|
949 */
950 va->va_end = nva_start_addr;
951 } else if (type == NE_FIT_TYPE) {
952 /*
953 * Split no edge of fit VA.
954 *
955 * | |
956 * L V NVA V R
957 * |---|-------|---|
958 */
959 lva = kmem_cache_alloc(vmap_area_cachep, GFP_NOWAIT);
960 if (unlikely(!lva))
961 return -1;
962
963 /*
964 * Build the remainder.
965 */
966 lva->va_start = va->va_start;
967 lva->va_end = nva_start_addr;
968
969 /*
970 * Shrink this VA to remaining size.
971 */
972 va->va_start = nva_start_addr + size;
973 } else {
974 return -1;
975 }
976
977 if (type != FL_FIT_TYPE) {
978 augment_tree_propagate_from(va);
979
980 if (type == NE_FIT_TYPE)
981 insert_vmap_area_augment(lva, &va->rb_node,
982 &free_vmap_area_root, &free_vmap_area_list);
983 }
984
985 return 0;
986}
987
988/*
989 * Returns a start address of the newly allocated area, if success.
990 * Otherwise a vend is returned that indicates failure.
991 */
992static __always_inline unsigned long
993__alloc_vmap_area(unsigned long size, unsigned long align,
994 unsigned long vstart, unsigned long vend, int node)
995{
996 unsigned long nva_start_addr;
997 struct vmap_area *va;
998 enum fit_type type;
999 int ret;
1000
1001 va = find_vmap_lowest_match(size, align, vstart);
1002 if (unlikely(!va))
1003 return vend;
1004
1005 if (va->va_start > vstart)
1006 nva_start_addr = ALIGN(va->va_start, align);
1007 else
1008 nva_start_addr = ALIGN(vstart, align);
1009
1010 /* Check the "vend" restriction. */
1011 if (nva_start_addr + size > vend)
1012 return vend;
1013
1014 /* Classify what we have found. */
1015 type = classify_va_fit_type(va, nva_start_addr, size);
1016 if (WARN_ON_ONCE(type == NOTHING_FIT))
1017 return vend;
1018
1019 /* Update the free vmap_area. */
1020 ret = adjust_va_to_fit_type(va, nva_start_addr, size, type);
1021 if (ret)
1022 return vend;
1023
Uladzislau Rezki (Sony)367c1e42019-05-17 14:31:37 -07001024#if DEBUG_AUGMENT_LOWEST_MATCH_CHECK
1025 find_vmap_lowest_match_check(size);
1026#endif
1027
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001028 return nva_start_addr;
1029}
Chris Wilson4da56b92016-04-04 14:46:42 +01001030
Nick Piggindb64fe02008-10-18 20:27:03 -07001031/*
1032 * Allocate a region of KVA of the specified size and alignment, within the
1033 * vstart and vend.
1034 */
1035static struct vmap_area *alloc_vmap_area(unsigned long size,
1036 unsigned long align,
1037 unsigned long vstart, unsigned long vend,
1038 int node, gfp_t gfp_mask)
1039{
1040 struct vmap_area *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 unsigned long addr;
Nick Piggindb64fe02008-10-18 20:27:03 -07001042 int purged = 0;
1043
Nick Piggin77669702009-02-27 14:03:03 -08001044 BUG_ON(!size);
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08001045 BUG_ON(offset_in_page(size));
Nick Piggin89699602011-03-22 16:30:36 -07001046 BUG_ON(!is_power_of_2(align));
Nick Piggindb64fe02008-10-18 20:27:03 -07001047
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001048 if (unlikely(!vmap_initialized))
1049 return ERR_PTR(-EBUSY);
1050
Christoph Hellwig5803ed22016-12-12 16:44:20 -08001051 might_sleep();
Chris Wilson4da56b92016-04-04 14:46:42 +01001052
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001053 va = kmem_cache_alloc_node(vmap_area_cachep,
Nick Piggindb64fe02008-10-18 20:27:03 -07001054 gfp_mask & GFP_RECLAIM_MASK, node);
1055 if (unlikely(!va))
1056 return ERR_PTR(-ENOMEM);
1057
Catalin Marinas7f88f882013-11-12 15:07:45 -08001058 /*
1059 * Only scan the relevant parts containing pointers to other objects
1060 * to avoid false negatives.
1061 */
1062 kmemleak_scan_area(&va->rb_node, SIZE_MAX, gfp_mask & GFP_RECLAIM_MASK);
1063
Nick Piggindb64fe02008-10-18 20:27:03 -07001064retry:
1065 spin_lock(&vmap_area_lock);
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001066
Nick Piggin89699602011-03-22 16:30:36 -07001067 /*
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001068 * If an allocation fails, the "vend" address is
1069 * returned. Therefore trigger the overflow path.
Nick Piggin89699602011-03-22 16:30:36 -07001070 */
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001071 addr = __alloc_vmap_area(size, align, vstart, vend, node);
1072 if (unlikely(addr == vend))
Nick Piggin89699602011-03-22 16:30:36 -07001073 goto overflow;
Nick Piggindb64fe02008-10-18 20:27:03 -07001074
1075 va->va_start = addr;
1076 va->va_end = addr + size;
1077 va->flags = 0;
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001078 insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
1079
Nick Piggindb64fe02008-10-18 20:27:03 -07001080 spin_unlock(&vmap_area_lock);
1081
Wang Xiaoqiang61e16552016-01-15 16:57:19 -08001082 BUG_ON(!IS_ALIGNED(va->va_start, align));
Nick Piggin89699602011-03-22 16:30:36 -07001083 BUG_ON(va->va_start < vstart);
1084 BUG_ON(va->va_end > vend);
1085
Nick Piggindb64fe02008-10-18 20:27:03 -07001086 return va;
Nick Piggin89699602011-03-22 16:30:36 -07001087
1088overflow:
1089 spin_unlock(&vmap_area_lock);
1090 if (!purged) {
1091 purge_vmap_area_lazy();
1092 purged = 1;
1093 goto retry;
1094 }
Chris Wilson4da56b92016-04-04 14:46:42 +01001095
1096 if (gfpflags_allow_blocking(gfp_mask)) {
1097 unsigned long freed = 0;
1098 blocking_notifier_call_chain(&vmap_notify_list, 0, &freed);
1099 if (freed > 0) {
1100 purged = 0;
1101 goto retry;
1102 }
1103 }
1104
Florian Fainelli03497d72017-04-27 11:19:00 -07001105 if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit())
Joe Perches756a0252016-03-17 14:19:47 -07001106 pr_warn("vmap allocation for size %lu failed: use vmalloc=<size> to increase size\n",
1107 size);
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001108
1109 kmem_cache_free(vmap_area_cachep, va);
Nick Piggin89699602011-03-22 16:30:36 -07001110 return ERR_PTR(-EBUSY);
Nick Piggindb64fe02008-10-18 20:27:03 -07001111}
1112
Chris Wilson4da56b92016-04-04 14:46:42 +01001113int register_vmap_purge_notifier(struct notifier_block *nb)
1114{
1115 return blocking_notifier_chain_register(&vmap_notify_list, nb);
1116}
1117EXPORT_SYMBOL_GPL(register_vmap_purge_notifier);
1118
1119int unregister_vmap_purge_notifier(struct notifier_block *nb)
1120{
1121 return blocking_notifier_chain_unregister(&vmap_notify_list, nb);
1122}
1123EXPORT_SYMBOL_GPL(unregister_vmap_purge_notifier);
1124
Nick Piggindb64fe02008-10-18 20:27:03 -07001125static void __free_vmap_area(struct vmap_area *va)
1126{
1127 BUG_ON(RB_EMPTY_NODE(&va->rb_node));
Nick Piggin89699602011-03-22 16:30:36 -07001128
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001129 /*
1130 * Remove from the busy tree/list.
1131 */
1132 unlink_va(va, &vmap_area_root);
Nick Piggindb64fe02008-10-18 20:27:03 -07001133
Tejun Heoca23e402009-08-14 15:00:52 +09001134 /*
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001135 * Merge VA with its neighbors, otherwise just add it.
Tejun Heoca23e402009-08-14 15:00:52 +09001136 */
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001137 merge_or_add_vmap_area(va,
1138 &free_vmap_area_root, &free_vmap_area_list);
Nick Piggindb64fe02008-10-18 20:27:03 -07001139}
1140
1141/*
1142 * Free a region of KVA allocated by alloc_vmap_area
1143 */
1144static void free_vmap_area(struct vmap_area *va)
1145{
1146 spin_lock(&vmap_area_lock);
1147 __free_vmap_area(va);
1148 spin_unlock(&vmap_area_lock);
1149}
1150
1151/*
1152 * Clear the pagetable entries of a given vmap_area
1153 */
1154static void unmap_vmap_area(struct vmap_area *va)
1155{
1156 vunmap_page_range(va->va_start, va->va_end);
1157}
1158
1159/*
1160 * lazy_max_pages is the maximum amount of virtual address space we gather up
1161 * before attempting to purge with a TLB flush.
1162 *
1163 * There is a tradeoff here: a larger number will cover more kernel page tables
1164 * and take slightly longer to purge, but it will linearly reduce the number of
1165 * global TLB flushes that must be performed. It would seem natural to scale
1166 * this number up linearly with the number of CPUs (because vmapping activity
1167 * could also scale linearly with the number of CPUs), however it is likely
1168 * that in practice, workloads might be constrained in other ways that mean
1169 * vmap activity will not scale linearly with CPUs. Also, I want to be
1170 * conservative and not introduce a big latency on huge systems, so go with
1171 * a less aggressive log scale. It will still be an improvement over the old
1172 * code, and it will be simple to change the scale factor if we find that it
1173 * becomes a problem on bigger systems.
1174 */
1175static unsigned long lazy_max_pages(void)
1176{
1177 unsigned int log;
1178
1179 log = fls(num_online_cpus());
1180
1181 return log * (32UL * 1024 * 1024 / PAGE_SIZE);
1182}
1183
1184static atomic_t vmap_lazy_nr = ATOMIC_INIT(0);
1185
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001186/*
1187 * Serialize vmap purging. There is no actual criticial section protected
1188 * by this look, but we want to avoid concurrent calls for performance
1189 * reasons and to make the pcpu_get_vm_areas more deterministic.
1190 */
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001191static DEFINE_MUTEX(vmap_purge_lock);
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001192
Nick Piggin02b709d2010-02-01 22:25:57 +11001193/* for per-CPU blocks */
1194static void purge_fragmented_blocks_allcpus(void);
1195
Nick Piggindb64fe02008-10-18 20:27:03 -07001196/*
Cliff Wickman3ee48b62010-09-16 11:44:02 -05001197 * called before a call to iounmap() if the caller wants vm_area_struct's
1198 * immediately freed.
1199 */
1200void set_iounmap_nonlazy(void)
1201{
1202 atomic_set(&vmap_lazy_nr, lazy_max_pages()+1);
1203}
1204
1205/*
Nick Piggindb64fe02008-10-18 20:27:03 -07001206 * Purges all lazily-freed vmap areas.
Nick Piggindb64fe02008-10-18 20:27:03 -07001207 */
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001208static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end)
Nick Piggindb64fe02008-10-18 20:27:03 -07001209{
Chris Wilson80c4bd72016-05-20 16:57:38 -07001210 struct llist_node *valist;
Nick Piggindb64fe02008-10-18 20:27:03 -07001211 struct vmap_area *va;
Vegard Nossumcbb76672009-02-27 14:03:04 -08001212 struct vmap_area *n_va;
Joel Fernandes763b2182016-12-12 16:44:26 -08001213 bool do_free = false;
Nick Piggindb64fe02008-10-18 20:27:03 -07001214
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001215 lockdep_assert_held(&vmap_purge_lock);
Nick Piggin02b709d2010-02-01 22:25:57 +11001216
Chris Wilson80c4bd72016-05-20 16:57:38 -07001217 valist = llist_del_all(&vmap_purge_list);
1218 llist_for_each_entry(va, valist, purge_list) {
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001219 if (va->va_start < start)
1220 start = va->va_start;
1221 if (va->va_end > end)
1222 end = va->va_end;
Joel Fernandes763b2182016-12-12 16:44:26 -08001223 do_free = true;
Nick Piggindb64fe02008-10-18 20:27:03 -07001224 }
Nick Piggindb64fe02008-10-18 20:27:03 -07001225
Joel Fernandes763b2182016-12-12 16:44:26 -08001226 if (!do_free)
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001227 return false;
Nick Piggindb64fe02008-10-18 20:27:03 -07001228
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001229 flush_tlb_kernel_range(start, end);
Nick Piggindb64fe02008-10-18 20:27:03 -07001230
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001231 spin_lock(&vmap_area_lock);
Joel Fernandes763b2182016-12-12 16:44:26 -08001232 llist_for_each_entry_safe(va, n_va, valist, purge_list) {
1233 int nr = (va->va_end - va->va_start) >> PAGE_SHIFT;
1234
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001235 __free_vmap_area(va);
Joel Fernandes763b2182016-12-12 16:44:26 -08001236 atomic_sub(nr, &vmap_lazy_nr);
1237 cond_resched_lock(&vmap_area_lock);
1238 }
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001239 spin_unlock(&vmap_area_lock);
1240 return true;
Nick Piggindb64fe02008-10-18 20:27:03 -07001241}
1242
1243/*
Nick Piggin496850e2008-11-19 15:36:33 -08001244 * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
1245 * is already purging.
1246 */
1247static void try_purge_vmap_area_lazy(void)
1248{
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001249 if (mutex_trylock(&vmap_purge_lock)) {
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001250 __purge_vmap_area_lazy(ULONG_MAX, 0);
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001251 mutex_unlock(&vmap_purge_lock);
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001252 }
Nick Piggin496850e2008-11-19 15:36:33 -08001253}
1254
1255/*
Nick Piggindb64fe02008-10-18 20:27:03 -07001256 * Kick off a purge of the outstanding lazy areas.
1257 */
1258static void purge_vmap_area_lazy(void)
1259{
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001260 mutex_lock(&vmap_purge_lock);
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001261 purge_fragmented_blocks_allcpus();
1262 __purge_vmap_area_lazy(ULONG_MAX, 0);
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001263 mutex_unlock(&vmap_purge_lock);
Nick Piggindb64fe02008-10-18 20:27:03 -07001264}
1265
1266/*
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -08001267 * Free a vmap area, caller ensuring that the area has been unmapped
1268 * and flush_cache_vunmap had been called for the correct range
1269 * previously.
Nick Piggindb64fe02008-10-18 20:27:03 -07001270 */
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -08001271static void free_vmap_area_noflush(struct vmap_area *va)
Nick Piggindb64fe02008-10-18 20:27:03 -07001272{
Chris Wilson80c4bd72016-05-20 16:57:38 -07001273 int nr_lazy;
1274
1275 nr_lazy = atomic_add_return((va->va_end - va->va_start) >> PAGE_SHIFT,
1276 &vmap_lazy_nr);
1277
1278 /* After this point, we may free va at any time */
1279 llist_add(&va->purge_list, &vmap_purge_list);
1280
1281 if (unlikely(nr_lazy > lazy_max_pages()))
Nick Piggin496850e2008-11-19 15:36:33 -08001282 try_purge_vmap_area_lazy();
Nick Piggindb64fe02008-10-18 20:27:03 -07001283}
1284
Nick Pigginb29acbd2008-12-01 13:13:47 -08001285/*
1286 * Free and unmap a vmap area
1287 */
1288static void free_unmap_vmap_area(struct vmap_area *va)
1289{
1290 flush_cache_vunmap(va->va_start, va->va_end);
Christoph Hellwigc8eef012016-12-12 16:44:01 -08001291 unmap_vmap_area(va);
Chintan Pandya82a2e922018-06-07 17:06:46 -07001292 if (debug_pagealloc_enabled())
1293 flush_tlb_kernel_range(va->va_start, va->va_end);
1294
Christoph Hellwigc8eef012016-12-12 16:44:01 -08001295 free_vmap_area_noflush(va);
Nick Pigginb29acbd2008-12-01 13:13:47 -08001296}
1297
Nick Piggindb64fe02008-10-18 20:27:03 -07001298static struct vmap_area *find_vmap_area(unsigned long addr)
1299{
1300 struct vmap_area *va;
1301
1302 spin_lock(&vmap_area_lock);
1303 va = __find_vmap_area(addr);
1304 spin_unlock(&vmap_area_lock);
1305
1306 return va;
1307}
1308
Nick Piggindb64fe02008-10-18 20:27:03 -07001309/*** Per cpu kva allocator ***/
1310
1311/*
1312 * vmap space is limited especially on 32 bit architectures. Ensure there is
1313 * room for at least 16 percpu vmap blocks per CPU.
1314 */
1315/*
1316 * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
1317 * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess
1318 * instead (we just need a rough idea)
1319 */
1320#if BITS_PER_LONG == 32
1321#define VMALLOC_SPACE (128UL*1024*1024)
1322#else
1323#define VMALLOC_SPACE (128UL*1024*1024*1024)
1324#endif
1325
1326#define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE)
1327#define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */
1328#define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */
1329#define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2)
1330#define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */
1331#define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */
Clemens Ladischf982f912011-06-21 22:09:50 +02001332#define VMAP_BBMAP_BITS \
1333 VMAP_MIN(VMAP_BBMAP_BITS_MAX, \
1334 VMAP_MAX(VMAP_BBMAP_BITS_MIN, \
1335 VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16))
Nick Piggindb64fe02008-10-18 20:27:03 -07001336
1337#define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE)
1338
1339struct vmap_block_queue {
1340 spinlock_t lock;
1341 struct list_head free;
Nick Piggindb64fe02008-10-18 20:27:03 -07001342};
1343
1344struct vmap_block {
1345 spinlock_t lock;
1346 struct vmap_area *va;
Nick Piggindb64fe02008-10-18 20:27:03 -07001347 unsigned long free, dirty;
Roman Pen7d61bfe2015-04-15 16:13:55 -07001348 unsigned long dirty_min, dirty_max; /*< dirty range */
Nick Pigginde560422010-02-01 22:24:18 +11001349 struct list_head free_list;
1350 struct rcu_head rcu_head;
Nick Piggin02b709d2010-02-01 22:25:57 +11001351 struct list_head purge;
Nick Piggindb64fe02008-10-18 20:27:03 -07001352};
1353
1354/* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
1355static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
1356
1357/*
1358 * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block
1359 * in the free path. Could get rid of this if we change the API to return a
1360 * "cookie" from alloc, to be passed to free. But no big deal yet.
1361 */
1362static DEFINE_SPINLOCK(vmap_block_tree_lock);
1363static RADIX_TREE(vmap_block_tree, GFP_ATOMIC);
1364
1365/*
1366 * We should probably have a fallback mechanism to allocate virtual memory
1367 * out of partially filled vmap blocks. However vmap block sizing should be
1368 * fairly reasonable according to the vmalloc size, so it shouldn't be a
1369 * big problem.
1370 */
1371
1372static unsigned long addr_to_vb_idx(unsigned long addr)
1373{
1374 addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
1375 addr /= VMAP_BLOCK_SIZE;
1376 return addr;
1377}
1378
Roman Pencf725ce2015-04-15 16:13:52 -07001379static void *vmap_block_vaddr(unsigned long va_start, unsigned long pages_off)
1380{
1381 unsigned long addr;
1382
1383 addr = va_start + (pages_off << PAGE_SHIFT);
1384 BUG_ON(addr_to_vb_idx(addr) != addr_to_vb_idx(va_start));
1385 return (void *)addr;
1386}
1387
1388/**
1389 * new_vmap_block - allocates new vmap_block and occupies 2^order pages in this
1390 * block. Of course pages number can't exceed VMAP_BBMAP_BITS
1391 * @order: how many 2^order pages should be occupied in newly allocated block
1392 * @gfp_mask: flags for the page level allocator
1393 *
1394 * Returns: virtual address in a newly allocated block or ERR_PTR(-errno)
1395 */
1396static void *new_vmap_block(unsigned int order, gfp_t gfp_mask)
Nick Piggindb64fe02008-10-18 20:27:03 -07001397{
1398 struct vmap_block_queue *vbq;
1399 struct vmap_block *vb;
1400 struct vmap_area *va;
1401 unsigned long vb_idx;
1402 int node, err;
Roman Pencf725ce2015-04-15 16:13:52 -07001403 void *vaddr;
Nick Piggindb64fe02008-10-18 20:27:03 -07001404
1405 node = numa_node_id();
1406
1407 vb = kmalloc_node(sizeof(struct vmap_block),
1408 gfp_mask & GFP_RECLAIM_MASK, node);
1409 if (unlikely(!vb))
1410 return ERR_PTR(-ENOMEM);
1411
1412 va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
1413 VMALLOC_START, VMALLOC_END,
1414 node, gfp_mask);
Tobias Klauserddf9c6d42011-01-13 15:46:15 -08001415 if (IS_ERR(va)) {
Nick Piggindb64fe02008-10-18 20:27:03 -07001416 kfree(vb);
Julia Lawalle7d86342010-08-09 17:18:28 -07001417 return ERR_CAST(va);
Nick Piggindb64fe02008-10-18 20:27:03 -07001418 }
1419
1420 err = radix_tree_preload(gfp_mask);
1421 if (unlikely(err)) {
1422 kfree(vb);
1423 free_vmap_area(va);
1424 return ERR_PTR(err);
1425 }
1426
Roman Pencf725ce2015-04-15 16:13:52 -07001427 vaddr = vmap_block_vaddr(va->va_start, 0);
Nick Piggindb64fe02008-10-18 20:27:03 -07001428 spin_lock_init(&vb->lock);
1429 vb->va = va;
Roman Pencf725ce2015-04-15 16:13:52 -07001430 /* At least something should be left free */
1431 BUG_ON(VMAP_BBMAP_BITS <= (1UL << order));
1432 vb->free = VMAP_BBMAP_BITS - (1UL << order);
Nick Piggindb64fe02008-10-18 20:27:03 -07001433 vb->dirty = 0;
Roman Pen7d61bfe2015-04-15 16:13:55 -07001434 vb->dirty_min = VMAP_BBMAP_BITS;
1435 vb->dirty_max = 0;
Nick Piggindb64fe02008-10-18 20:27:03 -07001436 INIT_LIST_HEAD(&vb->free_list);
Nick Piggindb64fe02008-10-18 20:27:03 -07001437
1438 vb_idx = addr_to_vb_idx(va->va_start);
1439 spin_lock(&vmap_block_tree_lock);
1440 err = radix_tree_insert(&vmap_block_tree, vb_idx, vb);
1441 spin_unlock(&vmap_block_tree_lock);
1442 BUG_ON(err);
1443 radix_tree_preload_end();
1444
1445 vbq = &get_cpu_var(vmap_block_queue);
Nick Piggindb64fe02008-10-18 20:27:03 -07001446 spin_lock(&vbq->lock);
Roman Pen68ac5462015-04-15 16:13:48 -07001447 list_add_tail_rcu(&vb->free_list, &vbq->free);
Nick Piggindb64fe02008-10-18 20:27:03 -07001448 spin_unlock(&vbq->lock);
Tejun Heo3f04ba82009-10-29 22:34:12 +09001449 put_cpu_var(vmap_block_queue);
Nick Piggindb64fe02008-10-18 20:27:03 -07001450
Roman Pencf725ce2015-04-15 16:13:52 -07001451 return vaddr;
Nick Piggindb64fe02008-10-18 20:27:03 -07001452}
1453
Nick Piggindb64fe02008-10-18 20:27:03 -07001454static void free_vmap_block(struct vmap_block *vb)
1455{
1456 struct vmap_block *tmp;
1457 unsigned long vb_idx;
1458
Nick Piggindb64fe02008-10-18 20:27:03 -07001459 vb_idx = addr_to_vb_idx(vb->va->va_start);
1460 spin_lock(&vmap_block_tree_lock);
1461 tmp = radix_tree_delete(&vmap_block_tree, vb_idx);
1462 spin_unlock(&vmap_block_tree_lock);
1463 BUG_ON(tmp != vb);
1464
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -08001465 free_vmap_area_noflush(vb->va);
Lai Jiangshan22a3c7d2011-03-18 12:13:08 +08001466 kfree_rcu(vb, rcu_head);
Nick Piggindb64fe02008-10-18 20:27:03 -07001467}
1468
Nick Piggin02b709d2010-02-01 22:25:57 +11001469static void purge_fragmented_blocks(int cpu)
1470{
1471 LIST_HEAD(purge);
1472 struct vmap_block *vb;
1473 struct vmap_block *n_vb;
1474 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
1475
1476 rcu_read_lock();
1477 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
1478
1479 if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS))
1480 continue;
1481
1482 spin_lock(&vb->lock);
1483 if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) {
1484 vb->free = 0; /* prevent further allocs after releasing lock */
1485 vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */
Roman Pen7d61bfe2015-04-15 16:13:55 -07001486 vb->dirty_min = 0;
1487 vb->dirty_max = VMAP_BBMAP_BITS;
Nick Piggin02b709d2010-02-01 22:25:57 +11001488 spin_lock(&vbq->lock);
1489 list_del_rcu(&vb->free_list);
1490 spin_unlock(&vbq->lock);
1491 spin_unlock(&vb->lock);
1492 list_add_tail(&vb->purge, &purge);
1493 } else
1494 spin_unlock(&vb->lock);
1495 }
1496 rcu_read_unlock();
1497
1498 list_for_each_entry_safe(vb, n_vb, &purge, purge) {
1499 list_del(&vb->purge);
1500 free_vmap_block(vb);
1501 }
1502}
1503
Nick Piggin02b709d2010-02-01 22:25:57 +11001504static void purge_fragmented_blocks_allcpus(void)
1505{
1506 int cpu;
1507
1508 for_each_possible_cpu(cpu)
1509 purge_fragmented_blocks(cpu);
1510}
1511
Nick Piggindb64fe02008-10-18 20:27:03 -07001512static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
1513{
1514 struct vmap_block_queue *vbq;
1515 struct vmap_block *vb;
Roman Pencf725ce2015-04-15 16:13:52 -07001516 void *vaddr = NULL;
Nick Piggindb64fe02008-10-18 20:27:03 -07001517 unsigned int order;
1518
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08001519 BUG_ON(offset_in_page(size));
Nick Piggindb64fe02008-10-18 20:27:03 -07001520 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
Jan Karaaa91c4d2012-07-31 16:41:37 -07001521 if (WARN_ON(size == 0)) {
1522 /*
1523 * Allocating 0 bytes isn't what caller wants since
1524 * get_order(0) returns funny result. Just warn and terminate
1525 * early.
1526 */
1527 return NULL;
1528 }
Nick Piggindb64fe02008-10-18 20:27:03 -07001529 order = get_order(size);
1530
Nick Piggindb64fe02008-10-18 20:27:03 -07001531 rcu_read_lock();
1532 vbq = &get_cpu_var(vmap_block_queue);
1533 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
Roman Pencf725ce2015-04-15 16:13:52 -07001534 unsigned long pages_off;
Nick Piggindb64fe02008-10-18 20:27:03 -07001535
1536 spin_lock(&vb->lock);
Roman Pencf725ce2015-04-15 16:13:52 -07001537 if (vb->free < (1UL << order)) {
1538 spin_unlock(&vb->lock);
1539 continue;
1540 }
Nick Piggin02b709d2010-02-01 22:25:57 +11001541
Roman Pencf725ce2015-04-15 16:13:52 -07001542 pages_off = VMAP_BBMAP_BITS - vb->free;
1543 vaddr = vmap_block_vaddr(vb->va->va_start, pages_off);
Nick Piggin02b709d2010-02-01 22:25:57 +11001544 vb->free -= 1UL << order;
1545 if (vb->free == 0) {
1546 spin_lock(&vbq->lock);
1547 list_del_rcu(&vb->free_list);
1548 spin_unlock(&vbq->lock);
Nick Piggindb64fe02008-10-18 20:27:03 -07001549 }
Roman Pencf725ce2015-04-15 16:13:52 -07001550
Nick Piggindb64fe02008-10-18 20:27:03 -07001551 spin_unlock(&vb->lock);
Nick Piggin02b709d2010-02-01 22:25:57 +11001552 break;
Nick Piggindb64fe02008-10-18 20:27:03 -07001553 }
Nick Piggin02b709d2010-02-01 22:25:57 +11001554
Tejun Heo3f04ba82009-10-29 22:34:12 +09001555 put_cpu_var(vmap_block_queue);
Nick Piggindb64fe02008-10-18 20:27:03 -07001556 rcu_read_unlock();
1557
Roman Pencf725ce2015-04-15 16:13:52 -07001558 /* Allocate new block if nothing was found */
1559 if (!vaddr)
1560 vaddr = new_vmap_block(order, gfp_mask);
Nick Piggindb64fe02008-10-18 20:27:03 -07001561
Roman Pencf725ce2015-04-15 16:13:52 -07001562 return vaddr;
Nick Piggindb64fe02008-10-18 20:27:03 -07001563}
1564
1565static void vb_free(const void *addr, unsigned long size)
1566{
1567 unsigned long offset;
1568 unsigned long vb_idx;
1569 unsigned int order;
1570 struct vmap_block *vb;
1571
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08001572 BUG_ON(offset_in_page(size));
Nick Piggindb64fe02008-10-18 20:27:03 -07001573 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
Nick Pigginb29acbd2008-12-01 13:13:47 -08001574
1575 flush_cache_vunmap((unsigned long)addr, (unsigned long)addr + size);
1576
Nick Piggindb64fe02008-10-18 20:27:03 -07001577 order = get_order(size);
1578
1579 offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1);
Roman Pen7d61bfe2015-04-15 16:13:55 -07001580 offset >>= PAGE_SHIFT;
Nick Piggindb64fe02008-10-18 20:27:03 -07001581
1582 vb_idx = addr_to_vb_idx((unsigned long)addr);
1583 rcu_read_lock();
1584 vb = radix_tree_lookup(&vmap_block_tree, vb_idx);
1585 rcu_read_unlock();
1586 BUG_ON(!vb);
1587
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -08001588 vunmap_page_range((unsigned long)addr, (unsigned long)addr + size);
1589
Chintan Pandya82a2e922018-06-07 17:06:46 -07001590 if (debug_pagealloc_enabled())
1591 flush_tlb_kernel_range((unsigned long)addr,
1592 (unsigned long)addr + size);
1593
Nick Piggindb64fe02008-10-18 20:27:03 -07001594 spin_lock(&vb->lock);
Roman Pen7d61bfe2015-04-15 16:13:55 -07001595
1596 /* Expand dirty range */
1597 vb->dirty_min = min(vb->dirty_min, offset);
1598 vb->dirty_max = max(vb->dirty_max, offset + (1UL << order));
MinChan Kimd0868172009-03-31 15:19:26 -07001599
Nick Piggindb64fe02008-10-18 20:27:03 -07001600 vb->dirty += 1UL << order;
1601 if (vb->dirty == VMAP_BBMAP_BITS) {
Nick Pigginde560422010-02-01 22:24:18 +11001602 BUG_ON(vb->free);
Nick Piggindb64fe02008-10-18 20:27:03 -07001603 spin_unlock(&vb->lock);
1604 free_vmap_block(vb);
1605 } else
1606 spin_unlock(&vb->lock);
1607}
1608
1609/**
1610 * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
1611 *
1612 * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
1613 * to amortize TLB flushing overheads. What this means is that any page you
1614 * have now, may, in a former life, have been mapped into kernel virtual
1615 * address by the vmap layer and so there might be some CPUs with TLB entries
1616 * still referencing that page (additional to the regular 1:1 kernel mapping).
1617 *
1618 * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
1619 * be sure that none of the pages we have control over will have any aliases
1620 * from the vmap layer.
1621 */
1622void vm_unmap_aliases(void)
1623{
1624 unsigned long start = ULONG_MAX, end = 0;
1625 int cpu;
1626 int flush = 0;
1627
Jeremy Fitzhardinge9b463332008-10-28 19:22:34 +11001628 if (unlikely(!vmap_initialized))
1629 return;
1630
Christoph Hellwig5803ed22016-12-12 16:44:20 -08001631 might_sleep();
1632
Nick Piggindb64fe02008-10-18 20:27:03 -07001633 for_each_possible_cpu(cpu) {
1634 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
1635 struct vmap_block *vb;
1636
1637 rcu_read_lock();
1638 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
Nick Piggindb64fe02008-10-18 20:27:03 -07001639 spin_lock(&vb->lock);
Roman Pen7d61bfe2015-04-15 16:13:55 -07001640 if (vb->dirty) {
1641 unsigned long va_start = vb->va->va_start;
Nick Piggindb64fe02008-10-18 20:27:03 -07001642 unsigned long s, e;
Joonsoo Kimb136be5e2013-09-11 14:21:40 -07001643
Roman Pen7d61bfe2015-04-15 16:13:55 -07001644 s = va_start + (vb->dirty_min << PAGE_SHIFT);
1645 e = va_start + (vb->dirty_max << PAGE_SHIFT);
Nick Piggindb64fe02008-10-18 20:27:03 -07001646
Roman Pen7d61bfe2015-04-15 16:13:55 -07001647 start = min(s, start);
1648 end = max(e, end);
1649
Nick Piggindb64fe02008-10-18 20:27:03 -07001650 flush = 1;
Nick Piggindb64fe02008-10-18 20:27:03 -07001651 }
1652 spin_unlock(&vb->lock);
1653 }
1654 rcu_read_unlock();
1655 }
1656
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001657 mutex_lock(&vmap_purge_lock);
Christoph Hellwig0574ecd2016-12-12 16:44:07 -08001658 purge_fragmented_blocks_allcpus();
1659 if (!__purge_vmap_area_lazy(start, end) && flush)
1660 flush_tlb_kernel_range(start, end);
Christoph Hellwigf9e09972016-12-12 16:44:23 -08001661 mutex_unlock(&vmap_purge_lock);
Nick Piggindb64fe02008-10-18 20:27:03 -07001662}
1663EXPORT_SYMBOL_GPL(vm_unmap_aliases);
1664
1665/**
1666 * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
1667 * @mem: the pointer returned by vm_map_ram
1668 * @count: the count passed to that vm_map_ram call (cannot unmap partial)
1669 */
1670void vm_unmap_ram(const void *mem, unsigned int count)
1671{
Guillermo Julián Moreno65ee03c2016-06-03 14:55:33 -07001672 unsigned long size = (unsigned long)count << PAGE_SHIFT;
Nick Piggindb64fe02008-10-18 20:27:03 -07001673 unsigned long addr = (unsigned long)mem;
Christoph Hellwig9c3acf62016-12-12 16:44:04 -08001674 struct vmap_area *va;
Nick Piggindb64fe02008-10-18 20:27:03 -07001675
Christoph Hellwig5803ed22016-12-12 16:44:20 -08001676 might_sleep();
Nick Piggindb64fe02008-10-18 20:27:03 -07001677 BUG_ON(!addr);
1678 BUG_ON(addr < VMALLOC_START);
1679 BUG_ON(addr > VMALLOC_END);
Shawn Lina1c0b1a2016-03-17 14:20:37 -07001680 BUG_ON(!PAGE_ALIGNED(addr));
Nick Piggindb64fe02008-10-18 20:27:03 -07001681
Christoph Hellwig9c3acf62016-12-12 16:44:04 -08001682 if (likely(count <= VMAP_MAX_ALLOC)) {
Chintan Pandya05e3ff92018-06-07 17:06:53 -07001683 debug_check_no_locks_freed(mem, size);
Nick Piggindb64fe02008-10-18 20:27:03 -07001684 vb_free(mem, size);
Christoph Hellwig9c3acf62016-12-12 16:44:04 -08001685 return;
1686 }
1687
1688 va = find_vmap_area(addr);
1689 BUG_ON(!va);
Chintan Pandya05e3ff92018-06-07 17:06:53 -07001690 debug_check_no_locks_freed((void *)va->va_start,
1691 (va->va_end - va->va_start));
Christoph Hellwig9c3acf62016-12-12 16:44:04 -08001692 free_unmap_vmap_area(va);
Nick Piggindb64fe02008-10-18 20:27:03 -07001693}
1694EXPORT_SYMBOL(vm_unmap_ram);
1695
1696/**
1697 * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
1698 * @pages: an array of pointers to the pages to be mapped
1699 * @count: number of pages
1700 * @node: prefer to allocate data structures on this node
1701 * @prot: memory protection to use. PAGE_KERNEL for regular RAM
Randy Dunlape99c97a2008-10-29 14:01:09 -07001702 *
Gioh Kim36437632014-04-07 15:37:37 -07001703 * If you use this function for less than VMAP_MAX_ALLOC pages, it could be
1704 * faster than vmap so it's good. But if you mix long-life and short-life
1705 * objects with vm_map_ram(), it could consume lots of address space through
1706 * fragmentation (especially on a 32bit machine). You could see failures in
1707 * the end. Please use this function for short-lived objects.
1708 *
Randy Dunlape99c97a2008-10-29 14:01:09 -07001709 * Returns: a pointer to the address that has been mapped, or %NULL on failure
Nick Piggindb64fe02008-10-18 20:27:03 -07001710 */
1711void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
1712{
Guillermo Julián Moreno65ee03c2016-06-03 14:55:33 -07001713 unsigned long size = (unsigned long)count << PAGE_SHIFT;
Nick Piggindb64fe02008-10-18 20:27:03 -07001714 unsigned long addr;
1715 void *mem;
1716
1717 if (likely(count <= VMAP_MAX_ALLOC)) {
1718 mem = vb_alloc(size, GFP_KERNEL);
1719 if (IS_ERR(mem))
1720 return NULL;
1721 addr = (unsigned long)mem;
1722 } else {
1723 struct vmap_area *va;
1724 va = alloc_vmap_area(size, PAGE_SIZE,
1725 VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
1726 if (IS_ERR(va))
1727 return NULL;
1728
1729 addr = va->va_start;
1730 mem = (void *)addr;
1731 }
1732 if (vmap_page_range(addr, addr + size, prot, pages) < 0) {
1733 vm_unmap_ram(mem, count);
1734 return NULL;
1735 }
1736 return mem;
1737}
1738EXPORT_SYMBOL(vm_map_ram);
1739
Joonsoo Kim4341fa42013-04-29 15:07:39 -07001740static struct vm_struct *vmlist __initdata;
Tejun Heof0aa6612009-02-20 16:29:08 +09001741/**
Nicolas Pitrebe9b7332011-08-25 00:24:21 -04001742 * vm_area_add_early - add vmap area early during boot
1743 * @vm: vm_struct to add
1744 *
1745 * This function is used to add fixed kernel vm area to vmlist before
1746 * vmalloc_init() is called. @vm->addr, @vm->size, and @vm->flags
1747 * should contain proper values and the other fields should be zero.
1748 *
1749 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1750 */
1751void __init vm_area_add_early(struct vm_struct *vm)
1752{
1753 struct vm_struct *tmp, **p;
1754
1755 BUG_ON(vmap_initialized);
1756 for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
1757 if (tmp->addr >= vm->addr) {
1758 BUG_ON(tmp->addr < vm->addr + vm->size);
1759 break;
1760 } else
1761 BUG_ON(tmp->addr + tmp->size > vm->addr);
1762 }
1763 vm->next = *p;
1764 *p = vm;
1765}
1766
1767/**
Tejun Heof0aa6612009-02-20 16:29:08 +09001768 * vm_area_register_early - register vmap area early during boot
1769 * @vm: vm_struct to register
Tejun Heoc0c0a292009-02-24 11:57:21 +09001770 * @align: requested alignment
Tejun Heof0aa6612009-02-20 16:29:08 +09001771 *
1772 * This function is used to register kernel vm area before
1773 * vmalloc_init() is called. @vm->size and @vm->flags should contain
1774 * proper values on entry and other fields should be zero. On return,
1775 * vm->addr contains the allocated address.
1776 *
1777 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1778 */
Tejun Heoc0c0a292009-02-24 11:57:21 +09001779void __init vm_area_register_early(struct vm_struct *vm, size_t align)
Tejun Heof0aa6612009-02-20 16:29:08 +09001780{
1781 static size_t vm_init_off __initdata;
Tejun Heoc0c0a292009-02-24 11:57:21 +09001782 unsigned long addr;
Tejun Heof0aa6612009-02-20 16:29:08 +09001783
Tejun Heoc0c0a292009-02-24 11:57:21 +09001784 addr = ALIGN(VMALLOC_START + vm_init_off, align);
1785 vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START;
1786
1787 vm->addr = (void *)addr;
Tejun Heof0aa6612009-02-20 16:29:08 +09001788
Nicolas Pitrebe9b7332011-08-25 00:24:21 -04001789 vm_area_add_early(vm);
Tejun Heof0aa6612009-02-20 16:29:08 +09001790}
1791
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001792static void vmap_init_free_space(void)
1793{
1794 unsigned long vmap_start = 1;
1795 const unsigned long vmap_end = ULONG_MAX;
1796 struct vmap_area *busy, *free;
1797
1798 /*
1799 * B F B B B F
1800 * -|-----|.....|-----|-----|-----|.....|-
1801 * | The KVA space |
1802 * |<--------------------------------->|
1803 */
1804 list_for_each_entry(busy, &vmap_area_list, list) {
1805 if (busy->va_start - vmap_start > 0) {
1806 free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
1807 if (!WARN_ON_ONCE(!free)) {
1808 free->va_start = vmap_start;
1809 free->va_end = busy->va_start;
1810
1811 insert_vmap_area_augment(free, NULL,
1812 &free_vmap_area_root,
1813 &free_vmap_area_list);
1814 }
1815 }
1816
1817 vmap_start = busy->va_end;
1818 }
1819
1820 if (vmap_end - vmap_start > 0) {
1821 free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
1822 if (!WARN_ON_ONCE(!free)) {
1823 free->va_start = vmap_start;
1824 free->va_end = vmap_end;
1825
1826 insert_vmap_area_augment(free, NULL,
1827 &free_vmap_area_root,
1828 &free_vmap_area_list);
1829 }
1830 }
1831}
1832
Nick Piggindb64fe02008-10-18 20:27:03 -07001833void __init vmalloc_init(void)
1834{
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08001835 struct vmap_area *va;
1836 struct vm_struct *tmp;
Nick Piggindb64fe02008-10-18 20:27:03 -07001837 int i;
1838
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001839 /*
1840 * Create the cache for vmap_area objects.
1841 */
1842 vmap_area_cachep = KMEM_CACHE(vmap_area, SLAB_PANIC);
1843
Nick Piggindb64fe02008-10-18 20:27:03 -07001844 for_each_possible_cpu(i) {
1845 struct vmap_block_queue *vbq;
Al Viro32fcfd42013-03-10 20:14:08 -04001846 struct vfree_deferred *p;
Nick Piggindb64fe02008-10-18 20:27:03 -07001847
1848 vbq = &per_cpu(vmap_block_queue, i);
1849 spin_lock_init(&vbq->lock);
1850 INIT_LIST_HEAD(&vbq->free);
Al Viro32fcfd42013-03-10 20:14:08 -04001851 p = &per_cpu(vfree_deferred, i);
1852 init_llist_head(&p->list);
1853 INIT_WORK(&p->wq, free_work);
Nick Piggindb64fe02008-10-18 20:27:03 -07001854 }
Jeremy Fitzhardinge9b463332008-10-28 19:22:34 +11001855
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08001856 /* Import existing vmlist entries. */
1857 for (tmp = vmlist; tmp; tmp = tmp->next) {
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001858 va = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
1859 if (WARN_ON_ONCE(!va))
1860 continue;
1861
KyongHodbda5912012-05-29 15:06:49 -07001862 va->flags = VM_VM_AREA;
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08001863 va->va_start = (unsigned long)tmp->addr;
1864 va->va_end = va->va_start + tmp->size;
KyongHodbda5912012-05-29 15:06:49 -07001865 va->vm = tmp;
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001866 insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08001867 }
Tejun Heoca23e402009-08-14 15:00:52 +09001868
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07001869 /*
1870 * Now we can initialize a free vmap space.
1871 */
1872 vmap_init_free_space();
Jeremy Fitzhardinge9b463332008-10-28 19:22:34 +11001873 vmap_initialized = true;
Nick Piggindb64fe02008-10-18 20:27:03 -07001874}
1875
Tejun Heo8fc48982009-02-20 16:29:08 +09001876/**
1877 * map_kernel_range_noflush - map kernel VM area with the specified pages
1878 * @addr: start of the VM area to map
1879 * @size: size of the VM area to map
1880 * @prot: page protection flags to use
1881 * @pages: pages to map
1882 *
1883 * Map PFN_UP(@size) pages at @addr. The VM area @addr and @size
1884 * specify should have been allocated using get_vm_area() and its
1885 * friends.
1886 *
1887 * NOTE:
1888 * This function does NOT do any cache flushing. The caller is
1889 * responsible for calling flush_cache_vmap() on to-be-mapped areas
1890 * before calling this function.
1891 *
1892 * RETURNS:
1893 * The number of pages mapped on success, -errno on failure.
1894 */
1895int map_kernel_range_noflush(unsigned long addr, unsigned long size,
1896 pgprot_t prot, struct page **pages)
1897{
1898 return vmap_page_range_noflush(addr, addr + size, prot, pages);
1899}
1900
1901/**
1902 * unmap_kernel_range_noflush - unmap kernel VM area
1903 * @addr: start of the VM area to unmap
1904 * @size: size of the VM area to unmap
1905 *
1906 * Unmap PFN_UP(@size) pages at @addr. The VM area @addr and @size
1907 * specify should have been allocated using get_vm_area() and its
1908 * friends.
1909 *
1910 * NOTE:
1911 * This function does NOT do any cache flushing. The caller is
1912 * responsible for calling flush_cache_vunmap() on to-be-mapped areas
1913 * before calling this function and flush_tlb_kernel_range() after.
1914 */
1915void unmap_kernel_range_noflush(unsigned long addr, unsigned long size)
1916{
1917 vunmap_page_range(addr, addr + size);
1918}
Huang Ying81e88fd2011-01-12 14:44:55 +08001919EXPORT_SYMBOL_GPL(unmap_kernel_range_noflush);
Tejun Heo8fc48982009-02-20 16:29:08 +09001920
1921/**
1922 * unmap_kernel_range - unmap kernel VM area and flush cache and TLB
1923 * @addr: start of the VM area to unmap
1924 * @size: size of the VM area to unmap
1925 *
1926 * Similar to unmap_kernel_range_noflush() but flushes vcache before
1927 * the unmapping and tlb after.
1928 */
Nick Piggindb64fe02008-10-18 20:27:03 -07001929void unmap_kernel_range(unsigned long addr, unsigned long size)
1930{
1931 unsigned long end = addr + size;
Tejun Heof6fcba72009-02-20 15:38:48 -08001932
1933 flush_cache_vunmap(addr, end);
Nick Piggindb64fe02008-10-18 20:27:03 -07001934 vunmap_page_range(addr, end);
1935 flush_tlb_kernel_range(addr, end);
1936}
Minchan Kim93ef6d6c2014-06-04 16:11:09 -07001937EXPORT_SYMBOL_GPL(unmap_kernel_range);
Nick Piggindb64fe02008-10-18 20:27:03 -07001938
WANG Chaof6f8ed42014-08-06 16:06:58 -07001939int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page **pages)
Nick Piggindb64fe02008-10-18 20:27:03 -07001940{
1941 unsigned long addr = (unsigned long)area->addr;
Wanpeng Li762216a2013-09-11 14:22:42 -07001942 unsigned long end = addr + get_vm_area_size(area);
Nick Piggindb64fe02008-10-18 20:27:03 -07001943 int err;
1944
WANG Chaof6f8ed42014-08-06 16:06:58 -07001945 err = vmap_page_range(addr, end, prot, pages);
Nick Piggindb64fe02008-10-18 20:27:03 -07001946
WANG Chaof6f8ed42014-08-06 16:06:58 -07001947 return err > 0 ? 0 : err;
Nick Piggindb64fe02008-10-18 20:27:03 -07001948}
1949EXPORT_SYMBOL_GPL(map_vm_area);
1950
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001951static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
Marek Szyprowski5e6cafc2012-04-13 12:32:09 +02001952 unsigned long flags, const void *caller)
Tejun Heocf88c792009-08-14 15:00:52 +09001953{
Joonsoo Kimc69480a2013-04-29 15:07:30 -07001954 spin_lock(&vmap_area_lock);
Tejun Heocf88c792009-08-14 15:00:52 +09001955 vm->flags = flags;
1956 vm->addr = (void *)va->va_start;
1957 vm->size = va->va_end - va->va_start;
1958 vm->caller = caller;
Minchan Kimdb1aeca2012-01-10 15:08:39 -08001959 va->vm = vm;
Tejun Heocf88c792009-08-14 15:00:52 +09001960 va->flags |= VM_VM_AREA;
Joonsoo Kimc69480a2013-04-29 15:07:30 -07001961 spin_unlock(&vmap_area_lock);
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001962}
Tejun Heocf88c792009-08-14 15:00:52 +09001963
Zhang Yanfei20fc02b2013-07-08 15:59:58 -07001964static void clear_vm_uninitialized_flag(struct vm_struct *vm)
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001965{
Joonsoo Kimd4033af2013-04-29 15:07:35 -07001966 /*
Zhang Yanfei20fc02b2013-07-08 15:59:58 -07001967 * Before removing VM_UNINITIALIZED,
Joonsoo Kimd4033af2013-04-29 15:07:35 -07001968 * we should make sure that vm has proper values.
1969 * Pair with smp_rmb() in show_numa_info().
1970 */
1971 smp_wmb();
Zhang Yanfei20fc02b2013-07-08 15:59:58 -07001972 vm->flags &= ~VM_UNINITIALIZED;
Tejun Heocf88c792009-08-14 15:00:52 +09001973}
1974
Nick Piggindb64fe02008-10-18 20:27:03 -07001975static struct vm_struct *__get_vm_area_node(unsigned long size,
David Miller2dca6992009-09-21 12:22:34 -07001976 unsigned long align, unsigned long flags, unsigned long start,
Marek Szyprowski5e6cafc2012-04-13 12:32:09 +02001977 unsigned long end, int node, gfp_t gfp_mask, const void *caller)
Nick Piggindb64fe02008-10-18 20:27:03 -07001978{
Kautuk Consul00065262011-12-19 17:12:04 -08001979 struct vmap_area *va;
Nick Piggindb64fe02008-10-18 20:27:03 -07001980 struct vm_struct *area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Giridhar Pemmasani52fd24c2006-10-28 10:38:34 -07001982 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 size = PAGE_ALIGN(size);
OGAWA Hirofumi31be8302006-11-16 01:19:29 -08001984 if (unlikely(!size))
1985 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
zijun_hu252e5c62016-10-07 16:57:26 -07001987 if (flags & VM_IOREMAP)
1988 align = 1ul << clamp_t(int, get_count_order_long(size),
1989 PAGE_SHIFT, IOREMAP_MAX_ORDER);
1990
Tejun Heocf88c792009-08-14 15:00:52 +09001991 area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 if (unlikely(!area))
1993 return NULL;
1994
Andrey Ryabinin71394fe2015-02-13 14:40:03 -08001995 if (!(flags & VM_NO_GUARD))
1996 size += PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Nick Piggindb64fe02008-10-18 20:27:03 -07001998 va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
1999 if (IS_ERR(va)) {
2000 kfree(area);
2001 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Zhang Yanfeid82b1d82013-07-03 15:04:47 -07002004 setup_vmalloc_vm(area, va, flags, caller);
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07002005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 return area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007}
2008
Christoph Lameter930fc452005-10-29 18:15:41 -07002009struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
2010 unsigned long start, unsigned long end)
2011{
David Rientjes00ef2d22013-02-22 16:35:36 -08002012 return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE,
2013 GFP_KERNEL, __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -07002014}
Rusty Russell5992b6d2007-07-19 01:49:21 -07002015EXPORT_SYMBOL_GPL(__get_vm_area);
Christoph Lameter930fc452005-10-29 18:15:41 -07002016
Benjamin Herrenschmidtc2968612009-02-18 14:48:12 -08002017struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
2018 unsigned long start, unsigned long end,
Marek Szyprowski5e6cafc2012-04-13 12:32:09 +02002019 const void *caller)
Benjamin Herrenschmidtc2968612009-02-18 14:48:12 -08002020{
David Rientjes00ef2d22013-02-22 16:35:36 -08002021 return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE,
2022 GFP_KERNEL, caller);
Benjamin Herrenschmidtc2968612009-02-18 14:48:12 -08002023}
2024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025/**
Simon Arlott183ff222007-10-20 01:27:18 +02002026 * get_vm_area - reserve a contiguous kernel virtual area
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 * @size: size of the area
2028 * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC
2029 *
2030 * Search an area of @size in the kernel virtual mapping area,
2031 * and reserved it for out purposes. Returns the area descriptor
2032 * on success or %NULL on failure.
2033 */
2034struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
2035{
David Miller2dca6992009-09-21 12:22:34 -07002036 return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
David Rientjes00ef2d22013-02-22 16:35:36 -08002037 NUMA_NO_NODE, GFP_KERNEL,
2038 __builtin_return_address(0));
Christoph Lameter23016962008-04-28 02:12:42 -07002039}
2040
2041struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
Marek Szyprowski5e6cafc2012-04-13 12:32:09 +02002042 const void *caller)
Christoph Lameter23016962008-04-28 02:12:42 -07002043{
David Miller2dca6992009-09-21 12:22:34 -07002044 return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
David Rientjes00ef2d22013-02-22 16:35:36 -08002045 NUMA_NO_NODE, GFP_KERNEL, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046}
2047
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02002048/**
2049 * find_vm_area - find a continuous kernel virtual area
2050 * @addr: base address
2051 *
2052 * Search for the kernel VM area starting at @addr, and return it.
2053 * It is up to the caller to do all required locking to keep the returned
2054 * pointer valid.
2055 */
2056struct vm_struct *find_vm_area(const void *addr)
Nick Piggin83342312006-06-23 02:03:20 -07002057{
Nick Piggindb64fe02008-10-18 20:27:03 -07002058 struct vmap_area *va;
Nick Piggin83342312006-06-23 02:03:20 -07002059
Nick Piggindb64fe02008-10-18 20:27:03 -07002060 va = find_vmap_area((unsigned long)addr);
2061 if (va && va->flags & VM_VM_AREA)
Minchan Kimdb1aeca2012-01-10 15:08:39 -08002062 return va->vm;
Nick Piggin83342312006-06-23 02:03:20 -07002063
Andi Kleen7856dfe2005-05-20 14:27:57 -07002064 return NULL;
Andi Kleen7856dfe2005-05-20 14:27:57 -07002065}
2066
Roman Gushchin48480dc2019-04-24 07:37:21 +10002067static struct vm_struct *__remove_vm_area(struct vmap_area *va)
2068{
2069 struct vm_struct *vm = va->vm;
2070
Roman Gushchin48480dc2019-04-24 07:37:21 +10002071 spin_lock(&vmap_area_lock);
2072 va->vm = NULL;
2073 va->flags &= ~VM_VM_AREA;
2074 va->flags |= VM_LAZY_FREE;
2075 spin_unlock(&vmap_area_lock);
2076
2077 kasan_free_shadow(vm);
2078 free_unmap_vmap_area(va);
2079
2080 return vm;
2081}
2082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083/**
Simon Arlott183ff222007-10-20 01:27:18 +02002084 * remove_vm_area - find and remove a continuous kernel virtual area
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 * @addr: base address
2086 *
2087 * Search for the kernel VM area starting at @addr, and remove it.
2088 * This function returns the found VM area, but using it is NOT safe
Andi Kleen7856dfe2005-05-20 14:27:57 -07002089 * on SMP machines, except for its size or flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -08002091struct vm_struct *remove_vm_area(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
Roman Gushchin48480dc2019-04-24 07:37:21 +10002093 struct vm_struct *vm = NULL;
Nick Piggindb64fe02008-10-18 20:27:03 -07002094 struct vmap_area *va;
2095
2096 va = find_vmap_area((unsigned long)addr);
Roman Gushchin48480dc2019-04-24 07:37:21 +10002097 if (va && va->flags & VM_VM_AREA)
2098 vm = __remove_vm_area(va);
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07002099
Roman Gushchin48480dc2019-04-24 07:37:21 +10002100 return vm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101}
2102
Christoph Lameterb3bdda02008-02-04 22:28:32 -08002103static void __vunmap(const void *addr, int deallocate_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
2105 struct vm_struct *area;
Roman Gushchin48480dc2019-04-24 07:37:21 +10002106 struct vmap_area *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
2108 if (!addr)
2109 return;
2110
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002111 if (WARN(!PAGE_ALIGNED(addr), "Trying to vfree() bad address (%p)\n",
Dan Carpenterab15d9b2013-07-08 15:59:53 -07002112 addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Roman Gushchin48480dc2019-04-24 07:37:21 +10002115 va = find_vmap_area((unsigned long)addr);
2116 if (unlikely(!va || !(va->flags & VM_VM_AREA))) {
Arjan van de Ven4c8573e2008-07-25 19:45:37 -07002117 WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 return;
2120 }
2121
Roman Gushchin48480dc2019-04-24 07:37:21 +10002122 area = va->vm;
2123 debug_check_no_locks_freed(addr, get_vm_area_size(area));
2124 debug_check_no_obj_freed(addr, get_vm_area_size(area));
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07002125
Roman Gushchin48480dc2019-04-24 07:37:21 +10002126 __remove_vm_area(va);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 if (deallocate_pages) {
2128 int i;
2129
2130 for (i = 0; i < area->nr_pages; i++) {
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08002131 struct page *page = area->pages[i];
2132
2133 BUG_ON(!page);
Vladimir Davydov49491482016-07-26 15:24:24 -07002134 __free_pages(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 }
Roman Gushchindb70fefd2019-02-25 12:30:37 -08002136 atomic_long_sub(area->nr_pages, &nr_vmalloc_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
David Rientjes244d63e2016-01-14 15:19:35 -08002138 kvfree(area->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 }
2140
2141 kfree(area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142}
Andrey Ryabininbf22e372016-12-12 16:44:10 -08002143
2144static inline void __vfree_deferred(const void *addr)
2145{
2146 /*
2147 * Use raw_cpu_ptr() because this can be called from preemptible
2148 * context. Preemption is absolutely fine here, because the llist_add()
2149 * implementation is lockless, so it works even if we are adding to
2150 * nother cpu's list. schedule_work() should be fine with this too.
2151 */
2152 struct vfree_deferred *p = raw_cpu_ptr(&vfree_deferred);
2153
2154 if (llist_add((struct llist_node *)addr, &p->list))
2155 schedule_work(&p->wq);
2156}
2157
2158/**
2159 * vfree_atomic - release memory allocated by vmalloc()
2160 * @addr: memory base address
2161 *
2162 * This one is just like vfree() but can be called in any atomic context
2163 * except NMIs.
2164 */
2165void vfree_atomic(const void *addr)
2166{
2167 BUG_ON(in_nmi());
2168
2169 kmemleak_free(addr);
2170
2171 if (!addr)
2172 return;
2173 __vfree_deferred(addr);
2174}
2175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176/**
2177 * vfree - release memory allocated by vmalloc()
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 * @addr: memory base address
2179 *
Simon Arlott183ff222007-10-20 01:27:18 +02002180 * Free the virtually continuous memory area starting at @addr, as
Pekka Enberg80e93ef2005-09-09 13:10:16 -07002181 * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
2182 * NULL, no operation is performed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 *
Al Viro32fcfd42013-03-10 20:14:08 -04002184 * Must not be called in NMI context (strictly speaking, only if we don't
2185 * have CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG, but making the calling
2186 * conventions for vfree() arch-depenedent would be a really bad idea)
Andrew Mortonc9fcee52013-05-07 16:18:18 -07002187 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -03002188 * NOTE: assumes that the object at @addr has a size >= sizeof(llist_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -08002190void vfree(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
Al Viro32fcfd42013-03-10 20:14:08 -04002192 BUG_ON(in_nmi());
Catalin Marinas89219d32009-06-11 13:23:19 +01002193
2194 kmemleak_free(addr);
2195
Al Viro32fcfd42013-03-10 20:14:08 -04002196 if (!addr)
2197 return;
Andrey Ryabininbf22e372016-12-12 16:44:10 -08002198 if (unlikely(in_interrupt()))
2199 __vfree_deferred(addr);
2200 else
Al Viro32fcfd42013-03-10 20:14:08 -04002201 __vunmap(addr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203EXPORT_SYMBOL(vfree);
2204
2205/**
2206 * vunmap - release virtual mapping obtained by vmap()
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 * @addr: memory base address
2208 *
2209 * Free the virtually contiguous memory area starting at @addr,
2210 * which was created from the page array passed to vmap().
2211 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -07002212 * Must not be called in interrupt context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -08002214void vunmap(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215{
2216 BUG_ON(in_interrupt());
Peter Zijlstra34754b62009-02-25 16:04:03 +01002217 might_sleep();
Al Viro32fcfd42013-03-10 20:14:08 -04002218 if (addr)
2219 __vunmap(addr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221EXPORT_SYMBOL(vunmap);
2222
2223/**
2224 * vmap - map an array of pages into virtually contiguous space
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 * @pages: array of page pointers
2226 * @count: number of pages to map
2227 * @flags: vm_area->flags
2228 * @prot: page protection for the mapping
2229 *
2230 * Maps @count pages from @pages into contiguous kernel virtual
2231 * space.
2232 */
2233void *vmap(struct page **pages, unsigned int count,
2234 unsigned long flags, pgprot_t prot)
2235{
2236 struct vm_struct *area;
Guillermo Julián Moreno65ee03c2016-06-03 14:55:33 -07002237 unsigned long size; /* In bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Peter Zijlstra34754b62009-02-25 16:04:03 +01002239 might_sleep();
2240
Jan Beulich44813742009-09-21 17:03:05 -07002241 if (count > totalram_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 return NULL;
2243
Guillermo Julián Moreno65ee03c2016-06-03 14:55:33 -07002244 size = (unsigned long)count << PAGE_SHIFT;
2245 area = get_vm_area_caller(size, flags, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 if (!area)
2247 return NULL;
Christoph Lameter23016962008-04-28 02:12:42 -07002248
WANG Chaof6f8ed42014-08-06 16:06:58 -07002249 if (map_vm_area(area, prot, pages)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 vunmap(area->addr);
2251 return NULL;
2252 }
2253
2254 return area->addr;
2255}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256EXPORT_SYMBOL(vmap);
2257
Michal Hocko8594a212017-05-12 15:46:41 -07002258static void *__vmalloc_node(unsigned long size, unsigned long align,
2259 gfp_t gfp_mask, pgprot_t prot,
2260 int node, const void *caller);
Adrian Bunke31d9eb2008-02-04 22:29:09 -08002261static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
Wanpeng Li3722e132013-11-12 15:07:29 -08002262 pgprot_t prot, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263{
2264 struct page **pages;
2265 unsigned int nr_pages, array_size, i;
David Rientjes930f0362014-08-06 16:06:28 -07002266 const gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO;
Laura Abbott704b8622017-08-18 15:16:27 -07002267 const gfp_t alloc_mask = gfp_mask | __GFP_NOWARN;
2268 const gfp_t highmem_mask = (gfp_mask & (GFP_DMA | GFP_DMA32)) ?
2269 0 :
2270 __GFP_HIGHMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
Wanpeng Li762216a2013-09-11 14:22:42 -07002272 nr_pages = get_vm_area_size(area) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 array_size = (nr_pages * sizeof(struct page *));
2274
2275 area->nr_pages = nr_pages;
2276 /* Please note that the recursion is strictly bounded. */
Jan Kiszka8757d5f2006-07-14 00:23:56 -07002277 if (array_size > PAGE_SIZE) {
Laura Abbott704b8622017-08-18 15:16:27 -07002278 pages = __vmalloc_node(array_size, 1, nested_gfp|highmem_mask,
Wanpeng Li3722e132013-11-12 15:07:29 -08002279 PAGE_KERNEL, node, area->caller);
Andrew Morton286e1ea2006-10-17 00:09:57 -07002280 } else {
Jan Beulich976d6df2009-12-14 17:58:39 -08002281 pages = kmalloc_node(array_size, nested_gfp, node);
Andrew Morton286e1ea2006-10-17 00:09:57 -07002282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 area->pages = pages;
2284 if (!area->pages) {
2285 remove_vm_area(area->addr);
2286 kfree(area);
2287 return NULL;
2288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
2290 for (i = 0; i < area->nr_pages; i++) {
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08002291 struct page *page;
2292
Jianguo Wu4b909512013-11-12 15:07:11 -08002293 if (node == NUMA_NO_NODE)
Laura Abbott704b8622017-08-18 15:16:27 -07002294 page = alloc_page(alloc_mask|highmem_mask);
Christoph Lameter930fc452005-10-29 18:15:41 -07002295 else
Laura Abbott704b8622017-08-18 15:16:27 -07002296 page = alloc_pages_node(node, alloc_mask|highmem_mask, 0);
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08002297
2298 if (unlikely(!page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 /* Successfully allocated i pages, free them in __vunmap() */
2300 area->nr_pages = i;
Roman Gushchindb70fefd2019-02-25 12:30:37 -08002301 atomic_long_add(area->nr_pages, &nr_vmalloc_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 goto fail;
2303 }
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08002304 area->pages[i] = page;
Laura Abbott704b8622017-08-18 15:16:27 -07002305 if (gfpflags_allow_blocking(gfp_mask|highmem_mask))
Eric Dumazet660654f2014-08-06 16:06:25 -07002306 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 }
Roman Gushchindb70fefd2019-02-25 12:30:37 -08002308 atomic_long_add(area->nr_pages, &nr_vmalloc_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
WANG Chaof6f8ed42014-08-06 16:06:58 -07002310 if (map_vm_area(area, prot, pages))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 goto fail;
2312 return area->addr;
2313
2314fail:
Michal Hockoa8e99252017-02-22 15:46:10 -08002315 warn_alloc(gfp_mask, NULL,
Michal Hocko7877cdc2016-10-07 17:01:55 -07002316 "vmalloc: allocation failure, allocated %ld of %ld bytes",
Dave Hansen22943ab2011-05-24 17:12:18 -07002317 (area->nr_pages*PAGE_SIZE), area->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 vfree(area->addr);
2319 return NULL;
2320}
2321
David Rientjesd0a21262011-01-13 15:46:02 -08002322/**
2323 * __vmalloc_node_range - allocate virtually contiguous memory
2324 * @size: allocation size
2325 * @align: desired alignment
2326 * @start: vm area range start
2327 * @end: vm area range end
2328 * @gfp_mask: flags for the page level allocator
2329 * @prot: protection mask for the allocated pages
Andrey Ryabinincb9e3c22015-02-13 14:40:07 -08002330 * @vm_flags: additional vm area flags (e.g. %VM_NO_GUARD)
David Rientjes00ef2d22013-02-22 16:35:36 -08002331 * @node: node to use for allocation or NUMA_NO_NODE
David Rientjesd0a21262011-01-13 15:46:02 -08002332 * @caller: caller's return address
2333 *
2334 * Allocate enough pages to cover @size from the page level
2335 * allocator with @gfp_mask flags. Map them into contiguous
2336 * kernel virtual space, using a pagetable protection of @prot.
2337 */
2338void *__vmalloc_node_range(unsigned long size, unsigned long align,
2339 unsigned long start, unsigned long end, gfp_t gfp_mask,
Andrey Ryabinincb9e3c22015-02-13 14:40:07 -08002340 pgprot_t prot, unsigned long vm_flags, int node,
2341 const void *caller)
Christoph Lameter930fc452005-10-29 18:15:41 -07002342{
David Rientjesd0a21262011-01-13 15:46:02 -08002343 struct vm_struct *area;
2344 void *addr;
2345 unsigned long real_size = size;
2346
2347 size = PAGE_ALIGN(size);
2348 if (!size || (size >> PAGE_SHIFT) > totalram_pages)
Joe Perchesde7d2b52011-10-31 17:08:48 -07002349 goto fail;
David Rientjesd0a21262011-01-13 15:46:02 -08002350
Andrey Ryabinincb9e3c22015-02-13 14:40:07 -08002351 area = __get_vm_area_node(size, align, VM_ALLOC | VM_UNINITIALIZED |
2352 vm_flags, start, end, node, gfp_mask, caller);
David Rientjesd0a21262011-01-13 15:46:02 -08002353 if (!area)
Joe Perchesde7d2b52011-10-31 17:08:48 -07002354 goto fail;
David Rientjesd0a21262011-01-13 15:46:02 -08002355
Wanpeng Li3722e132013-11-12 15:07:29 -08002356 addr = __vmalloc_area_node(area, gfp_mask, prot, node);
Mel Gorman1368edf2011-12-08 14:34:30 -08002357 if (!addr)
Wanpeng Lib82225f32013-11-12 15:07:33 -08002358 return NULL;
Catalin Marinas89219d32009-06-11 13:23:19 +01002359
2360 /*
Zhang Yanfei20fc02b2013-07-08 15:59:58 -07002361 * In this function, newly allocated vm_struct has VM_UNINITIALIZED
2362 * flag. It means that vm_struct is not fully initialized.
Joonsoo Kim4341fa42013-04-29 15:07:39 -07002363 * Now, it is fully initialized, so remove this flag here.
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07002364 */
Zhang Yanfei20fc02b2013-07-08 15:59:58 -07002365 clear_vm_uninitialized_flag(area);
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07002366
Catalin Marinas94f4a162017-07-06 15:40:22 -07002367 kmemleak_vmalloc(area, size, gfp_mask);
Catalin Marinas89219d32009-06-11 13:23:19 +01002368
2369 return addr;
Joe Perchesde7d2b52011-10-31 17:08:48 -07002370
2371fail:
Michal Hockoa8e99252017-02-22 15:46:10 -08002372 warn_alloc(gfp_mask, NULL,
Michal Hocko7877cdc2016-10-07 17:01:55 -07002373 "vmalloc: allocation failure: %lu bytes", real_size);
Joe Perchesde7d2b52011-10-31 17:08:48 -07002374 return NULL;
Christoph Lameter930fc452005-10-29 18:15:41 -07002375}
2376
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377/**
Christoph Lameter930fc452005-10-29 18:15:41 -07002378 * __vmalloc_node - allocate virtually contiguous memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 * @size: allocation size
David Miller2dca6992009-09-21 12:22:34 -07002380 * @align: desired alignment
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 * @gfp_mask: flags for the page level allocator
2382 * @prot: protection mask for the allocated pages
David Rientjes00ef2d22013-02-22 16:35:36 -08002383 * @node: node to use for allocation or NUMA_NO_NODE
Randy Dunlapc85d1942008-05-01 04:34:48 -07002384 * @caller: caller's return address
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 *
2386 * Allocate enough pages to cover @size from the page level
2387 * allocator with @gfp_mask flags. Map them into contiguous
2388 * kernel virtual space, using a pagetable protection of @prot.
Michal Hockoa7c3e902017-05-08 15:57:09 -07002389 *
Michal Hockodcda9b02017-07-12 14:36:45 -07002390 * Reclaim modifiers in @gfp_mask - __GFP_NORETRY, __GFP_RETRY_MAYFAIL
Michal Hockoa7c3e902017-05-08 15:57:09 -07002391 * and __GFP_NOFAIL are not supported
2392 *
2393 * Any use of gfp flags outside of GFP_KERNEL should be consulted
2394 * with mm people.
2395 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 */
Michal Hocko8594a212017-05-12 15:46:41 -07002397static void *__vmalloc_node(unsigned long size, unsigned long align,
David Miller2dca6992009-09-21 12:22:34 -07002398 gfp_t gfp_mask, pgprot_t prot,
Marek Szyprowski5e6cafc2012-04-13 12:32:09 +02002399 int node, const void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400{
David Rientjesd0a21262011-01-13 15:46:02 -08002401 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
Andrey Ryabinincb9e3c22015-02-13 14:40:07 -08002402 gfp_mask, prot, 0, node, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403}
2404
Christoph Lameter930fc452005-10-29 18:15:41 -07002405void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
2406{
David Rientjes00ef2d22013-02-22 16:35:36 -08002407 return __vmalloc_node(size, 1, gfp_mask, prot, NUMA_NO_NODE,
Christoph Lameter23016962008-04-28 02:12:42 -07002408 __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -07002409}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410EXPORT_SYMBOL(__vmalloc);
2411
Michal Hocko8594a212017-05-12 15:46:41 -07002412static inline void *__vmalloc_node_flags(unsigned long size,
2413 int node, gfp_t flags)
2414{
2415 return __vmalloc_node(size, 1, flags, PAGE_KERNEL,
2416 node, __builtin_return_address(0));
2417}
2418
2419
2420void *__vmalloc_node_flags_caller(unsigned long size, int node, gfp_t flags,
2421 void *caller)
2422{
2423 return __vmalloc_node(size, 1, flags, PAGE_KERNEL, node, caller);
2424}
2425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426/**
2427 * vmalloc - allocate virtually contiguous memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 * @size: allocation size
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 * Allocate enough pages to cover @size from the page level
2430 * allocator and map them into contiguous kernel virtual space.
2431 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +02002432 * For tight control over page level allocator and protection flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 * use __vmalloc() instead.
2434 */
2435void *vmalloc(unsigned long size)
2436{
David Rientjes00ef2d22013-02-22 16:35:36 -08002437 return __vmalloc_node_flags(size, NUMA_NO_NODE,
Michal Hocko19809c22017-05-08 15:57:44 -07002438 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440EXPORT_SYMBOL(vmalloc);
2441
Christoph Lameter930fc452005-10-29 18:15:41 -07002442/**
Dave Younge1ca7782010-10-26 14:22:06 -07002443 * vzalloc - allocate virtually contiguous memory with zero fill
2444 * @size: allocation size
2445 * Allocate enough pages to cover @size from the page level
2446 * allocator and map them into contiguous kernel virtual space.
2447 * The memory allocated is set to zero.
2448 *
2449 * For tight control over page level allocator and protection flags
2450 * use __vmalloc() instead.
2451 */
2452void *vzalloc(unsigned long size)
2453{
David Rientjes00ef2d22013-02-22 16:35:36 -08002454 return __vmalloc_node_flags(size, NUMA_NO_NODE,
Michal Hocko19809c22017-05-08 15:57:44 -07002455 GFP_KERNEL | __GFP_ZERO);
Dave Younge1ca7782010-10-26 14:22:06 -07002456}
2457EXPORT_SYMBOL(vzalloc);
2458
2459/**
Rolf Eike Beeread04082006-09-27 01:50:13 -07002460 * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
2461 * @size: allocation size
Nick Piggin83342312006-06-23 02:03:20 -07002462 *
Rolf Eike Beeread04082006-09-27 01:50:13 -07002463 * The resulting memory area is zeroed so it can be mapped to userspace
2464 * without leaking data.
Nick Piggin83342312006-06-23 02:03:20 -07002465 */
2466void *vmalloc_user(unsigned long size)
2467{
2468 struct vm_struct *area;
2469 void *ret;
2470
David Miller2dca6992009-09-21 12:22:34 -07002471 ret = __vmalloc_node(size, SHMLBA,
Michal Hocko19809c22017-05-08 15:57:44 -07002472 GFP_KERNEL | __GFP_ZERO,
David Rientjes00ef2d22013-02-22 16:35:36 -08002473 PAGE_KERNEL, NUMA_NO_NODE,
2474 __builtin_return_address(0));
Eric Dumazet2b4ac442006-11-10 12:27:48 -08002475 if (ret) {
Nick Piggindb64fe02008-10-18 20:27:03 -07002476 area = find_vm_area(ret);
Eric Dumazet2b4ac442006-11-10 12:27:48 -08002477 area->flags |= VM_USERMAP;
Eric Dumazet2b4ac442006-11-10 12:27:48 -08002478 }
Nick Piggin83342312006-06-23 02:03:20 -07002479 return ret;
2480}
2481EXPORT_SYMBOL(vmalloc_user);
2482
2483/**
Christoph Lameter930fc452005-10-29 18:15:41 -07002484 * vmalloc_node - allocate memory on a specific node
Christoph Lameter930fc452005-10-29 18:15:41 -07002485 * @size: allocation size
Randy Dunlapd44e0782005-11-07 01:01:10 -08002486 * @node: numa node
Christoph Lameter930fc452005-10-29 18:15:41 -07002487 *
2488 * Allocate enough pages to cover @size from the page level
2489 * allocator and map them into contiguous kernel virtual space.
2490 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +02002491 * For tight control over page level allocator and protection flags
Christoph Lameter930fc452005-10-29 18:15:41 -07002492 * use __vmalloc() instead.
2493 */
2494void *vmalloc_node(unsigned long size, int node)
2495{
Michal Hocko19809c22017-05-08 15:57:44 -07002496 return __vmalloc_node(size, 1, GFP_KERNEL, PAGE_KERNEL,
Christoph Lameter23016962008-04-28 02:12:42 -07002497 node, __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -07002498}
2499EXPORT_SYMBOL(vmalloc_node);
2500
Dave Younge1ca7782010-10-26 14:22:06 -07002501/**
2502 * vzalloc_node - allocate memory on a specific node with zero fill
2503 * @size: allocation size
2504 * @node: numa node
2505 *
2506 * Allocate enough pages to cover @size from the page level
2507 * allocator and map them into contiguous kernel virtual space.
2508 * The memory allocated is set to zero.
2509 *
2510 * For tight control over page level allocator and protection flags
2511 * use __vmalloc_node() instead.
2512 */
2513void *vzalloc_node(unsigned long size, int node)
2514{
2515 return __vmalloc_node_flags(size, node,
Michal Hocko19809c22017-05-08 15:57:44 -07002516 GFP_KERNEL | __GFP_ZERO);
Dave Younge1ca7782010-10-26 14:22:06 -07002517}
2518EXPORT_SYMBOL(vzalloc_node);
2519
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520/**
2521 * vmalloc_exec - allocate virtually contiguous, executable memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 * @size: allocation size
2523 *
2524 * Kernel-internal function to allocate enough pages to cover @size
2525 * the page level allocator and map them into contiguous and
2526 * executable kernel virtual space.
2527 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +02002528 * For tight control over page level allocator and protection flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 * use __vmalloc() instead.
2530 */
2531
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532void *vmalloc_exec(unsigned long size)
2533{
Michal Hocko19809c22017-05-08 15:57:44 -07002534 return __vmalloc_node(size, 1, GFP_KERNEL, PAGE_KERNEL_EXEC,
David Rientjes00ef2d22013-02-22 16:35:36 -08002535 NUMA_NO_NODE, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536}
2537
Andi Kleen0d08e0d2007-05-02 19:27:12 +02002538#if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
Michal Hocko698d0832018-02-21 14:46:01 -08002539#define GFP_VMALLOC32 (GFP_DMA32 | GFP_KERNEL)
Andi Kleen0d08e0d2007-05-02 19:27:12 +02002540#elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
Michal Hocko698d0832018-02-21 14:46:01 -08002541#define GFP_VMALLOC32 (GFP_DMA | GFP_KERNEL)
Andi Kleen0d08e0d2007-05-02 19:27:12 +02002542#else
Michal Hocko698d0832018-02-21 14:46:01 -08002543/*
2544 * 64b systems should always have either DMA or DMA32 zones. For others
2545 * GFP_DMA32 should do the right thing and use the normal zone.
2546 */
2547#define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
Andi Kleen0d08e0d2007-05-02 19:27:12 +02002548#endif
2549
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550/**
2551 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 * @size: allocation size
2553 *
2554 * Allocate enough 32bit PA addressable pages to cover @size from the
2555 * page level allocator and map them into contiguous kernel virtual space.
2556 */
2557void *vmalloc_32(unsigned long size)
2558{
David Miller2dca6992009-09-21 12:22:34 -07002559 return __vmalloc_node(size, 1, GFP_VMALLOC32, PAGE_KERNEL,
David Rientjes00ef2d22013-02-22 16:35:36 -08002560 NUMA_NO_NODE, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562EXPORT_SYMBOL(vmalloc_32);
2563
Nick Piggin83342312006-06-23 02:03:20 -07002564/**
Rolf Eike Beeread04082006-09-27 01:50:13 -07002565 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
Nick Piggin83342312006-06-23 02:03:20 -07002566 * @size: allocation size
Rolf Eike Beeread04082006-09-27 01:50:13 -07002567 *
2568 * The resulting memory area is 32bit addressable and zeroed so it can be
2569 * mapped to userspace without leaking data.
Nick Piggin83342312006-06-23 02:03:20 -07002570 */
2571void *vmalloc_32_user(unsigned long size)
2572{
2573 struct vm_struct *area;
2574 void *ret;
2575
David Miller2dca6992009-09-21 12:22:34 -07002576 ret = __vmalloc_node(size, 1, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
David Rientjes00ef2d22013-02-22 16:35:36 -08002577 NUMA_NO_NODE, __builtin_return_address(0));
Eric Dumazet2b4ac442006-11-10 12:27:48 -08002578 if (ret) {
Nick Piggindb64fe02008-10-18 20:27:03 -07002579 area = find_vm_area(ret);
Eric Dumazet2b4ac442006-11-10 12:27:48 -08002580 area->flags |= VM_USERMAP;
Eric Dumazet2b4ac442006-11-10 12:27:48 -08002581 }
Nick Piggin83342312006-06-23 02:03:20 -07002582 return ret;
2583}
2584EXPORT_SYMBOL(vmalloc_32_user);
2585
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002586/*
2587 * small helper routine , copy contents to buf from addr.
2588 * If the page is not present, fill zero.
2589 */
2590
2591static int aligned_vread(char *buf, char *addr, unsigned long count)
2592{
2593 struct page *p;
2594 int copied = 0;
2595
2596 while (count) {
2597 unsigned long offset, length;
2598
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08002599 offset = offset_in_page(addr);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002600 length = PAGE_SIZE - offset;
2601 if (length > count)
2602 length = count;
2603 p = vmalloc_to_page(addr);
2604 /*
2605 * To do safe access to this _mapped_ area, we need
2606 * lock. But adding lock here means that we need to add
2607 * overhead of vmalloc()/vfree() calles for this _debug_
2608 * interface, rarely used. Instead of that, we'll use
2609 * kmap() and get small overhead in this access function.
2610 */
2611 if (p) {
2612 /*
2613 * we can expect USER0 is not used (see vread/vwrite's
2614 * function description)
2615 */
Cong Wang9b04c5f2011-11-25 23:14:39 +08002616 void *map = kmap_atomic(p);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002617 memcpy(buf, map + offset, length);
Cong Wang9b04c5f2011-11-25 23:14:39 +08002618 kunmap_atomic(map);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002619 } else
2620 memset(buf, 0, length);
2621
2622 addr += length;
2623 buf += length;
2624 copied += length;
2625 count -= length;
2626 }
2627 return copied;
2628}
2629
2630static int aligned_vwrite(char *buf, char *addr, unsigned long count)
2631{
2632 struct page *p;
2633 int copied = 0;
2634
2635 while (count) {
2636 unsigned long offset, length;
2637
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08002638 offset = offset_in_page(addr);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002639 length = PAGE_SIZE - offset;
2640 if (length > count)
2641 length = count;
2642 p = vmalloc_to_page(addr);
2643 /*
2644 * To do safe access to this _mapped_ area, we need
2645 * lock. But adding lock here means that we need to add
2646 * overhead of vmalloc()/vfree() calles for this _debug_
2647 * interface, rarely used. Instead of that, we'll use
2648 * kmap() and get small overhead in this access function.
2649 */
2650 if (p) {
2651 /*
2652 * we can expect USER0 is not used (see vread/vwrite's
2653 * function description)
2654 */
Cong Wang9b04c5f2011-11-25 23:14:39 +08002655 void *map = kmap_atomic(p);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002656 memcpy(map + offset, buf, length);
Cong Wang9b04c5f2011-11-25 23:14:39 +08002657 kunmap_atomic(map);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002658 }
2659 addr += length;
2660 buf += length;
2661 copied += length;
2662 count -= length;
2663 }
2664 return copied;
2665}
2666
2667/**
2668 * vread() - read vmalloc area in a safe way.
2669 * @buf: buffer for reading data
2670 * @addr: vm address.
2671 * @count: number of bytes to be read.
2672 *
2673 * Returns # of bytes which addr and buf should be increased.
2674 * (same number to @count). Returns 0 if [addr...addr+count) doesn't
2675 * includes any intersect with alive vmalloc area.
2676 *
2677 * This function checks that addr is a valid vmalloc'ed area, and
2678 * copy data from that area to a given buffer. If the given memory range
2679 * of [addr...addr+count) includes some valid address, data is copied to
2680 * proper area of @buf. If there are memory holes, they'll be zero-filled.
2681 * IOREMAP area is treated as memory hole and no copy is done.
2682 *
2683 * If [addr...addr+count) doesn't includes any intersects with alive
Cong Wanga8e52022012-06-23 11:30:16 +08002684 * vm_struct area, returns 0. @buf should be kernel's buffer.
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002685 *
2686 * Note: In usual ops, vread() is never necessary because the caller
2687 * should know vmalloc() area is valid and can use memcpy().
2688 * This is for routines which have to access vmalloc area without
2689 * any informaion, as /dev/kmem.
2690 *
2691 */
2692
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693long vread(char *buf, char *addr, unsigned long count)
2694{
Joonsoo Kime81ce852013-04-29 15:07:32 -07002695 struct vmap_area *va;
2696 struct vm_struct *vm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 char *vaddr, *buf_start = buf;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002698 unsigned long buflen = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 unsigned long n;
2700
2701 /* Don't allow overflow */
2702 if ((unsigned long) addr + count < count)
2703 count = -(unsigned long) addr;
2704
Joonsoo Kime81ce852013-04-29 15:07:32 -07002705 spin_lock(&vmap_area_lock);
2706 list_for_each_entry(va, &vmap_area_list, list) {
2707 if (!count)
2708 break;
2709
2710 if (!(va->flags & VM_VM_AREA))
2711 continue;
2712
2713 vm = va->vm;
2714 vaddr = (char *) vm->addr;
Wanpeng Li762216a2013-09-11 14:22:42 -07002715 if (addr >= vaddr + get_vm_area_size(vm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 continue;
2717 while (addr < vaddr) {
2718 if (count == 0)
2719 goto finished;
2720 *buf = '\0';
2721 buf++;
2722 addr++;
2723 count--;
2724 }
Wanpeng Li762216a2013-09-11 14:22:42 -07002725 n = vaddr + get_vm_area_size(vm) - addr;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002726 if (n > count)
2727 n = count;
Joonsoo Kime81ce852013-04-29 15:07:32 -07002728 if (!(vm->flags & VM_IOREMAP))
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002729 aligned_vread(buf, addr, n);
2730 else /* IOREMAP area is treated as memory hole */
2731 memset(buf, 0, n);
2732 buf += n;
2733 addr += n;
2734 count -= n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 }
2736finished:
Joonsoo Kime81ce852013-04-29 15:07:32 -07002737 spin_unlock(&vmap_area_lock);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002738
2739 if (buf == buf_start)
2740 return 0;
2741 /* zero-fill memory holes */
2742 if (buf != buf_start + buflen)
2743 memset(buf, 0, buflen - (buf - buf_start));
2744
2745 return buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746}
2747
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002748/**
2749 * vwrite() - write vmalloc area in a safe way.
2750 * @buf: buffer for source data
2751 * @addr: vm address.
2752 * @count: number of bytes to be read.
2753 *
2754 * Returns # of bytes which addr and buf should be incresed.
2755 * (same number to @count).
2756 * If [addr...addr+count) doesn't includes any intersect with valid
2757 * vmalloc area, returns 0.
2758 *
2759 * This function checks that addr is a valid vmalloc'ed area, and
2760 * copy data from a buffer to the given addr. If specified range of
2761 * [addr...addr+count) includes some valid address, data is copied from
2762 * proper area of @buf. If there are memory holes, no copy to hole.
2763 * IOREMAP area is treated as memory hole and no copy is done.
2764 *
2765 * If [addr...addr+count) doesn't includes any intersects with alive
Cong Wanga8e52022012-06-23 11:30:16 +08002766 * vm_struct area, returns 0. @buf should be kernel's buffer.
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002767 *
2768 * Note: In usual ops, vwrite() is never necessary because the caller
2769 * should know vmalloc() area is valid and can use memcpy().
2770 * This is for routines which have to access vmalloc area without
2771 * any informaion, as /dev/kmem.
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002772 */
2773
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774long vwrite(char *buf, char *addr, unsigned long count)
2775{
Joonsoo Kime81ce852013-04-29 15:07:32 -07002776 struct vmap_area *va;
2777 struct vm_struct *vm;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002778 char *vaddr;
2779 unsigned long n, buflen;
2780 int copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
2782 /* Don't allow overflow */
2783 if ((unsigned long) addr + count < count)
2784 count = -(unsigned long) addr;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002785 buflen = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
Joonsoo Kime81ce852013-04-29 15:07:32 -07002787 spin_lock(&vmap_area_lock);
2788 list_for_each_entry(va, &vmap_area_list, list) {
2789 if (!count)
2790 break;
2791
2792 if (!(va->flags & VM_VM_AREA))
2793 continue;
2794
2795 vm = va->vm;
2796 vaddr = (char *) vm->addr;
Wanpeng Li762216a2013-09-11 14:22:42 -07002797 if (addr >= vaddr + get_vm_area_size(vm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 continue;
2799 while (addr < vaddr) {
2800 if (count == 0)
2801 goto finished;
2802 buf++;
2803 addr++;
2804 count--;
2805 }
Wanpeng Li762216a2013-09-11 14:22:42 -07002806 n = vaddr + get_vm_area_size(vm) - addr;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002807 if (n > count)
2808 n = count;
Joonsoo Kime81ce852013-04-29 15:07:32 -07002809 if (!(vm->flags & VM_IOREMAP)) {
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002810 aligned_vwrite(buf, addr, n);
2811 copied++;
2812 }
2813 buf += n;
2814 addr += n;
2815 count -= n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 }
2817finished:
Joonsoo Kime81ce852013-04-29 15:07:32 -07002818 spin_unlock(&vmap_area_lock);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002819 if (!copied)
2820 return 0;
2821 return buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822}
Nick Piggin83342312006-06-23 02:03:20 -07002823
2824/**
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002825 * remap_vmalloc_range_partial - map vmalloc pages to userspace
2826 * @vma: vma to cover
2827 * @uaddr: target user address to start at
2828 * @kaddr: virtual address of vmalloc kernel memory
2829 * @size: size of map area
2830 *
2831 * Returns: 0 for success, -Exxx on failure
2832 *
2833 * This function checks that @kaddr is a valid vmalloc'ed area,
2834 * and that it is big enough to cover the range starting at
2835 * @uaddr in @vma. Will return failure if that criteria isn't
2836 * met.
2837 *
2838 * Similar to remap_pfn_range() (see mm/memory.c)
2839 */
2840int remap_vmalloc_range_partial(struct vm_area_struct *vma, unsigned long uaddr,
2841 void *kaddr, unsigned long size)
2842{
2843 struct vm_struct *area;
2844
2845 size = PAGE_ALIGN(size);
2846
2847 if (!PAGE_ALIGNED(uaddr) || !PAGE_ALIGNED(kaddr))
2848 return -EINVAL;
2849
2850 area = find_vm_area(kaddr);
2851 if (!area)
2852 return -EINVAL;
2853
2854 if (!(area->flags & VM_USERMAP))
2855 return -EINVAL;
2856
Roman Penyaevc1ddc7b2019-03-05 15:43:20 -08002857 if (kaddr + size > area->addr + get_vm_area_size(area))
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002858 return -EINVAL;
2859
2860 do {
2861 struct page *page = vmalloc_to_page(kaddr);
2862 int ret;
2863
2864 ret = vm_insert_page(vma, uaddr, page);
2865 if (ret)
2866 return ret;
2867
2868 uaddr += PAGE_SIZE;
2869 kaddr += PAGE_SIZE;
2870 size -= PAGE_SIZE;
2871 } while (size > 0);
2872
2873 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
2874
2875 return 0;
2876}
2877EXPORT_SYMBOL(remap_vmalloc_range_partial);
2878
2879/**
Nick Piggin83342312006-06-23 02:03:20 -07002880 * remap_vmalloc_range - map vmalloc pages to userspace
Nick Piggin83342312006-06-23 02:03:20 -07002881 * @vma: vma to cover (map full range of vma)
2882 * @addr: vmalloc memory
2883 * @pgoff: number of pages into addr before first page to map
Randy Dunlap76824862008-03-19 17:00:40 -07002884 *
2885 * Returns: 0 for success, -Exxx on failure
Nick Piggin83342312006-06-23 02:03:20 -07002886 *
2887 * This function checks that addr is a valid vmalloc'ed area, and
2888 * that it is big enough to cover the vma. Will return failure if
2889 * that criteria isn't met.
2890 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002891 * Similar to remap_pfn_range() (see mm/memory.c)
Nick Piggin83342312006-06-23 02:03:20 -07002892 */
2893int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
2894 unsigned long pgoff)
2895{
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002896 return remap_vmalloc_range_partial(vma, vma->vm_start,
2897 addr + (pgoff << PAGE_SHIFT),
2898 vma->vm_end - vma->vm_start);
Nick Piggin83342312006-06-23 02:03:20 -07002899}
2900EXPORT_SYMBOL(remap_vmalloc_range);
2901
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -07002902/*
2903 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
2904 * have one.
2905 */
Gideon Israel Dsouza3b321232014-04-07 15:37:26 -07002906void __weak vmalloc_sync_all(void)
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -07002907{
2908}
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002909
2910
Martin Schwidefsky2f569af2008-02-08 04:22:04 -08002911static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002912{
David Vrabelcd129092011-09-29 16:53:32 +01002913 pte_t ***p = data;
2914
2915 if (p) {
2916 *(*p) = pte;
2917 (*p)++;
2918 }
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002919 return 0;
2920}
2921
2922/**
2923 * alloc_vm_area - allocate a range of kernel address space
2924 * @size: size of the area
David Vrabelcd129092011-09-29 16:53:32 +01002925 * @ptes: returns the PTEs for the address space
Randy Dunlap76824862008-03-19 17:00:40 -07002926 *
2927 * Returns: NULL on failure, vm_struct on success
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002928 *
2929 * This function reserves a range of kernel address space, and
2930 * allocates pagetables to map that range. No actual mappings
David Vrabelcd129092011-09-29 16:53:32 +01002931 * are created.
2932 *
2933 * If @ptes is non-NULL, pointers to the PTEs (in init_mm)
2934 * allocated for the VM area are returned.
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002935 */
David Vrabelcd129092011-09-29 16:53:32 +01002936struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002937{
2938 struct vm_struct *area;
2939
Christoph Lameter23016962008-04-28 02:12:42 -07002940 area = get_vm_area_caller(size, VM_IOREMAP,
2941 __builtin_return_address(0));
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002942 if (area == NULL)
2943 return NULL;
2944
2945 /*
2946 * This ensures that page tables are constructed for this region
2947 * of kernel virtual address space and mapped into init_mm.
2948 */
2949 if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
David Vrabelcd129092011-09-29 16:53:32 +01002950 size, f, ptes ? &ptes : NULL)) {
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002951 free_vm_area(area);
2952 return NULL;
2953 }
2954
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002955 return area;
2956}
2957EXPORT_SYMBOL_GPL(alloc_vm_area);
2958
2959void free_vm_area(struct vm_struct *area)
2960{
2961 struct vm_struct *ret;
2962 ret = remove_vm_area(area->addr);
2963 BUG_ON(ret != area);
2964 kfree(area);
2965}
2966EXPORT_SYMBOL_GPL(free_vm_area);
Christoph Lametera10aa572008-04-28 02:12:40 -07002967
Tejun Heo4f8b02b2010-09-03 18:22:47 +02002968#ifdef CONFIG_SMP
Tejun Heoca23e402009-08-14 15:00:52 +09002969static struct vmap_area *node_to_va(struct rb_node *n)
2970{
Geliang Tang4583e772017-02-22 15:41:54 -08002971 return rb_entry_safe(n, struct vmap_area, rb_node);
Tejun Heoca23e402009-08-14 15:00:52 +09002972}
2973
2974/**
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07002975 * pvm_find_va_enclose_addr - find the vmap_area @addr belongs to
2976 * @addr: target address
Tejun Heoca23e402009-08-14 15:00:52 +09002977 *
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07002978 * Returns: vmap_area if it is found. If there is no such area
2979 * the first highest(reverse order) vmap_area is returned
2980 * i.e. va->va_start < addr && va->va_end < addr or NULL
2981 * if there are no any areas before @addr.
Tejun Heoca23e402009-08-14 15:00:52 +09002982 */
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07002983static struct vmap_area *
2984pvm_find_va_enclose_addr(unsigned long addr)
Tejun Heoca23e402009-08-14 15:00:52 +09002985{
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07002986 struct vmap_area *va, *tmp;
2987 struct rb_node *n;
2988
2989 n = free_vmap_area_root.rb_node;
2990 va = NULL;
Tejun Heoca23e402009-08-14 15:00:52 +09002991
2992 while (n) {
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07002993 tmp = rb_entry(n, struct vmap_area, rb_node);
2994 if (tmp->va_start <= addr) {
2995 va = tmp;
2996 if (tmp->va_end >= addr)
2997 break;
2998
Tejun Heoca23e402009-08-14 15:00:52 +09002999 n = n->rb_right;
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003000 } else {
3001 n = n->rb_left;
3002 }
Tejun Heoca23e402009-08-14 15:00:52 +09003003 }
3004
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003005 return va;
Tejun Heoca23e402009-08-14 15:00:52 +09003006}
3007
3008/**
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003009 * pvm_determine_end_from_reverse - find the highest aligned address
3010 * of free block below VMALLOC_END
3011 * @va:
3012 * in - the VA we start the search(reverse order);
3013 * out - the VA with the highest aligned end address.
Tejun Heoca23e402009-08-14 15:00:52 +09003014 *
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003015 * Returns: determined end address within vmap_area
Tejun Heoca23e402009-08-14 15:00:52 +09003016 */
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003017static unsigned long
3018pvm_determine_end_from_reverse(struct vmap_area **va, unsigned long align)
Tejun Heoca23e402009-08-14 15:00:52 +09003019{
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003020 unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
Tejun Heoca23e402009-08-14 15:00:52 +09003021 unsigned long addr;
3022
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003023 if (likely(*va)) {
3024 list_for_each_entry_from_reverse((*va),
3025 &free_vmap_area_list, list) {
3026 addr = min((*va)->va_end & ~(align - 1), vmalloc_end);
3027 if ((*va)->va_start < addr)
3028 return addr;
3029 }
Tejun Heoca23e402009-08-14 15:00:52 +09003030 }
3031
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003032 return 0;
Tejun Heoca23e402009-08-14 15:00:52 +09003033}
3034
3035/**
3036 * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator
3037 * @offsets: array containing offset of each area
3038 * @sizes: array containing size of each area
3039 * @nr_vms: the number of areas to allocate
3040 * @align: alignment, all entries in @offsets and @sizes must be aligned to this
Tejun Heoca23e402009-08-14 15:00:52 +09003041 *
3042 * Returns: kmalloc'd vm_struct pointer array pointing to allocated
3043 * vm_structs on success, %NULL on failure
3044 *
3045 * Percpu allocator wants to use congruent vm areas so that it can
3046 * maintain the offsets among percpu areas. This function allocates
David Rientjesec3f64f2011-01-13 15:46:01 -08003047 * congruent vmalloc areas for it with GFP_KERNEL. These areas tend to
3048 * be scattered pretty far, distance between two areas easily going up
3049 * to gigabytes. To avoid interacting with regular vmallocs, these
3050 * areas are allocated from top.
Tejun Heoca23e402009-08-14 15:00:52 +09003051 *
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003052 * Despite its complicated look, this allocator is rather simple. It
3053 * does everything top-down and scans free blocks from the end looking
3054 * for matching base. While scanning, if any of the areas do not fit the
3055 * base address is pulled down to fit the area. Scanning is repeated till
3056 * all the areas fit and then all necessary data structures are inserted
3057 * and the result is returned.
Tejun Heoca23e402009-08-14 15:00:52 +09003058 */
3059struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
3060 const size_t *sizes, int nr_vms,
David Rientjesec3f64f2011-01-13 15:46:01 -08003061 size_t align)
Tejun Heoca23e402009-08-14 15:00:52 +09003062{
3063 const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align);
3064 const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003065 struct vmap_area **vas, *va;
Tejun Heoca23e402009-08-14 15:00:52 +09003066 struct vm_struct **vms;
3067 int area, area2, last_area, term_area;
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003068 unsigned long base, start, size, end, last_end;
Tejun Heoca23e402009-08-14 15:00:52 +09003069 bool purged = false;
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003070 enum fit_type type;
Tejun Heoca23e402009-08-14 15:00:52 +09003071
Tejun Heoca23e402009-08-14 15:00:52 +09003072 /* verify parameters and allocate data structures */
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08003073 BUG_ON(offset_in_page(align) || !is_power_of_2(align));
Tejun Heoca23e402009-08-14 15:00:52 +09003074 for (last_area = 0, area = 0; area < nr_vms; area++) {
3075 start = offsets[area];
3076 end = start + sizes[area];
3077
3078 /* is everything aligned properly? */
3079 BUG_ON(!IS_ALIGNED(offsets[area], align));
3080 BUG_ON(!IS_ALIGNED(sizes[area], align));
3081
3082 /* detect the area with the highest address */
3083 if (start > offsets[last_area])
3084 last_area = area;
3085
Wei Yangc568da22017-09-06 16:24:09 -07003086 for (area2 = area + 1; area2 < nr_vms; area2++) {
Tejun Heoca23e402009-08-14 15:00:52 +09003087 unsigned long start2 = offsets[area2];
3088 unsigned long end2 = start2 + sizes[area2];
3089
Wei Yangc568da22017-09-06 16:24:09 -07003090 BUG_ON(start2 < end && start < end2);
Tejun Heoca23e402009-08-14 15:00:52 +09003091 }
3092 }
3093 last_end = offsets[last_area] + sizes[last_area];
3094
3095 if (vmalloc_end - vmalloc_start < last_end) {
3096 WARN_ON(true);
3097 return NULL;
3098 }
3099
Thomas Meyer4d67d862012-05-29 15:06:21 -07003100 vms = kcalloc(nr_vms, sizeof(vms[0]), GFP_KERNEL);
3101 vas = kcalloc(nr_vms, sizeof(vas[0]), GFP_KERNEL);
Tejun Heoca23e402009-08-14 15:00:52 +09003102 if (!vas || !vms)
Kautuk Consulf1db7af2012-01-12 17:20:08 -08003103 goto err_free2;
Tejun Heoca23e402009-08-14 15:00:52 +09003104
3105 for (area = 0; area < nr_vms; area++) {
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003106 vas[area] = kmem_cache_zalloc(vmap_area_cachep, GFP_KERNEL);
David Rientjesec3f64f2011-01-13 15:46:01 -08003107 vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL);
Tejun Heoca23e402009-08-14 15:00:52 +09003108 if (!vas[area] || !vms[area])
3109 goto err_free;
3110 }
3111retry:
3112 spin_lock(&vmap_area_lock);
3113
3114 /* start scanning - we scan from the top, begin with the last area */
3115 area = term_area = last_area;
3116 start = offsets[area];
3117 end = start + sizes[area];
3118
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003119 va = pvm_find_va_enclose_addr(vmalloc_end);
3120 base = pvm_determine_end_from_reverse(&va, align) - end;
Tejun Heoca23e402009-08-14 15:00:52 +09003121
3122 while (true) {
Tejun Heoca23e402009-08-14 15:00:52 +09003123 /*
3124 * base might have underflowed, add last_end before
3125 * comparing.
3126 */
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003127 if (base + last_end < vmalloc_start + last_end)
3128 goto overflow;
Tejun Heoca23e402009-08-14 15:00:52 +09003129
3130 /*
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003131 * Fitting base has not been found.
Tejun Heoca23e402009-08-14 15:00:52 +09003132 */
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003133 if (va == NULL)
3134 goto overflow;
Tejun Heoca23e402009-08-14 15:00:52 +09003135
3136 /*
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003137 * If this VA does not fit, move base downwards and recheck.
Tejun Heoca23e402009-08-14 15:00:52 +09003138 */
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003139 if (base + start < va->va_start || base + end > va->va_end) {
3140 va = node_to_va(rb_prev(&va->rb_node));
3141 base = pvm_determine_end_from_reverse(&va, align) - end;
Tejun Heoca23e402009-08-14 15:00:52 +09003142 term_area = area;
3143 continue;
3144 }
3145
3146 /*
3147 * This area fits, move on to the previous one. If
3148 * the previous one is the terminal one, we're done.
3149 */
3150 area = (area + nr_vms - 1) % nr_vms;
3151 if (area == term_area)
3152 break;
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003153
Tejun Heoca23e402009-08-14 15:00:52 +09003154 start = offsets[area];
3155 end = start + sizes[area];
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003156 va = pvm_find_va_enclose_addr(base + end);
Tejun Heoca23e402009-08-14 15:00:52 +09003157 }
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003158
Tejun Heoca23e402009-08-14 15:00:52 +09003159 /* we've found a fitting base, insert all va's */
3160 for (area = 0; area < nr_vms; area++) {
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003161 int ret;
Tejun Heoca23e402009-08-14 15:00:52 +09003162
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003163 start = base + offsets[area];
3164 size = sizes[area];
3165
3166 va = pvm_find_va_enclose_addr(start);
3167 if (WARN_ON_ONCE(va == NULL))
3168 /* It is a BUG(), but trigger recovery instead. */
3169 goto recovery;
3170
3171 type = classify_va_fit_type(va, start, size);
3172 if (WARN_ON_ONCE(type == NOTHING_FIT))
3173 /* It is a BUG(), but trigger recovery instead. */
3174 goto recovery;
3175
3176 ret = adjust_va_to_fit_type(va, start, size, type);
3177 if (unlikely(ret))
3178 goto recovery;
3179
3180 /* Allocated area. */
3181 va = vas[area];
3182 va->va_start = start;
3183 va->va_end = start + size;
3184
3185 insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
Tejun Heoca23e402009-08-14 15:00:52 +09003186 }
3187
Tejun Heoca23e402009-08-14 15:00:52 +09003188 spin_unlock(&vmap_area_lock);
3189
3190 /* insert all vm's */
3191 for (area = 0; area < nr_vms; area++)
Zhang Yanfei3645cb42013-07-03 15:04:48 -07003192 setup_vmalloc_vm(vms[area], vas[area], VM_ALLOC,
3193 pcpu_get_vm_areas);
Tejun Heoca23e402009-08-14 15:00:52 +09003194
3195 kfree(vas);
3196 return vms;
3197
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003198recovery:
3199 /* Remove previously inserted areas. */
3200 while (area--) {
3201 __free_vmap_area(vas[area]);
3202 vas[area] = NULL;
3203 }
3204
3205overflow:
3206 spin_unlock(&vmap_area_lock);
3207 if (!purged) {
3208 purge_vmap_area_lazy();
3209 purged = true;
3210
3211 /* Before "retry", check if we recover. */
3212 for (area = 0; area < nr_vms; area++) {
3213 if (vas[area])
3214 continue;
3215
3216 vas[area] = kmem_cache_zalloc(
3217 vmap_area_cachep, GFP_KERNEL);
3218 if (!vas[area])
3219 goto err_free;
3220 }
3221
3222 goto retry;
3223 }
3224
Tejun Heoca23e402009-08-14 15:00:52 +09003225err_free:
3226 for (area = 0; area < nr_vms; area++) {
Uladzislau Rezki (Sony)674e1202019-05-17 14:31:31 -07003227 if (vas[area])
3228 kmem_cache_free(vmap_area_cachep, vas[area]);
3229
Kautuk Consulf1db7af2012-01-12 17:20:08 -08003230 kfree(vms[area]);
Tejun Heoca23e402009-08-14 15:00:52 +09003231 }
Kautuk Consulf1db7af2012-01-12 17:20:08 -08003232err_free2:
Tejun Heoca23e402009-08-14 15:00:52 +09003233 kfree(vas);
3234 kfree(vms);
3235 return NULL;
3236}
3237
3238/**
3239 * pcpu_free_vm_areas - free vmalloc areas for percpu allocator
3240 * @vms: vm_struct pointer array returned by pcpu_get_vm_areas()
3241 * @nr_vms: the number of allocated areas
3242 *
3243 * Free vm_structs and the array allocated by pcpu_get_vm_areas().
3244 */
3245void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
3246{
3247 int i;
3248
3249 for (i = 0; i < nr_vms; i++)
3250 free_vm_area(vms[i]);
3251 kfree(vms);
3252}
Tejun Heo4f8b02b2010-09-03 18:22:47 +02003253#endif /* CONFIG_SMP */
Christoph Lametera10aa572008-04-28 02:12:40 -07003254
3255#ifdef CONFIG_PROC_FS
3256static void *s_start(struct seq_file *m, loff_t *pos)
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003257 __acquires(&vmap_area_lock)
Christoph Lametera10aa572008-04-28 02:12:40 -07003258{
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003259 spin_lock(&vmap_area_lock);
zijun_hu3f500062016-12-12 16:42:17 -08003260 return seq_list_start(&vmap_area_list, *pos);
Christoph Lametera10aa572008-04-28 02:12:40 -07003261}
3262
3263static void *s_next(struct seq_file *m, void *p, loff_t *pos)
3264{
zijun_hu3f500062016-12-12 16:42:17 -08003265 return seq_list_next(p, &vmap_area_list, pos);
Christoph Lametera10aa572008-04-28 02:12:40 -07003266}
3267
3268static void s_stop(struct seq_file *m, void *p)
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003269 __releases(&vmap_area_lock)
Christoph Lametera10aa572008-04-28 02:12:40 -07003270{
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003271 spin_unlock(&vmap_area_lock);
Christoph Lametera10aa572008-04-28 02:12:40 -07003272}
3273
Eric Dumazeta47a1262008-07-23 21:27:38 -07003274static void show_numa_info(struct seq_file *m, struct vm_struct *v)
3275{
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08003276 if (IS_ENABLED(CONFIG_NUMA)) {
Eric Dumazeta47a1262008-07-23 21:27:38 -07003277 unsigned int nr, *counters = m->private;
3278
3279 if (!counters)
3280 return;
3281
Wanpeng Liaf123462013-11-12 15:07:32 -08003282 if (v->flags & VM_UNINITIALIZED)
3283 return;
Dmitry Vyukov7e5b5282014-12-12 16:56:30 -08003284 /* Pair with smp_wmb() in clear_vm_uninitialized_flag() */
3285 smp_rmb();
Wanpeng Liaf123462013-11-12 15:07:32 -08003286
Eric Dumazeta47a1262008-07-23 21:27:38 -07003287 memset(counters, 0, nr_node_ids * sizeof(unsigned int));
3288
3289 for (nr = 0; nr < v->nr_pages; nr++)
3290 counters[page_to_nid(v->pages[nr])]++;
3291
3292 for_each_node_state(nr, N_HIGH_MEMORY)
3293 if (counters[nr])
3294 seq_printf(m, " N%u=%u", nr, counters[nr]);
3295 }
3296}
3297
Christoph Lametera10aa572008-04-28 02:12:40 -07003298static int s_show(struct seq_file *m, void *p)
3299{
zijun_hu3f500062016-12-12 16:42:17 -08003300 struct vmap_area *va;
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003301 struct vm_struct *v;
3302
zijun_hu3f500062016-12-12 16:42:17 -08003303 va = list_entry(p, struct vmap_area, list);
3304
Wanpeng Lic2ce8c12013-11-12 15:07:31 -08003305 /*
3306 * s_show can encounter race with remove_vm_area, !VM_VM_AREA on
3307 * behalf of vmap area is being tear down or vm_map_ram allocation.
3308 */
Yisheng Xie78c72742017-07-10 15:48:09 -07003309 if (!(va->flags & VM_VM_AREA)) {
3310 seq_printf(m, "0x%pK-0x%pK %7ld %s\n",
3311 (void *)va->va_start, (void *)va->va_end,
3312 va->va_end - va->va_start,
3313 va->flags & VM_LAZY_FREE ? "unpurged vm_area" : "vm_map_ram");
3314
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003315 return 0;
Yisheng Xie78c72742017-07-10 15:48:09 -07003316 }
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003317
Joonsoo Kimd4033af2013-04-29 15:07:35 -07003318 v = va->vm;
Christoph Lametera10aa572008-04-28 02:12:40 -07003319
Kees Cook45ec1692012-10-08 16:34:09 -07003320 seq_printf(m, "0x%pK-0x%pK %7ld",
Christoph Lametera10aa572008-04-28 02:12:40 -07003321 v->addr, v->addr + v->size, v->size);
3322
Joe Perches62c70bc2011-01-13 15:45:52 -08003323 if (v->caller)
3324 seq_printf(m, " %pS", v->caller);
Christoph Lameter23016962008-04-28 02:12:42 -07003325
Christoph Lametera10aa572008-04-28 02:12:40 -07003326 if (v->nr_pages)
3327 seq_printf(m, " pages=%d", v->nr_pages);
3328
3329 if (v->phys_addr)
Miles Chen199eaa02017-02-24 14:59:51 -08003330 seq_printf(m, " phys=%pa", &v->phys_addr);
Christoph Lametera10aa572008-04-28 02:12:40 -07003331
3332 if (v->flags & VM_IOREMAP)
Fabian Frederickf4527c92014-06-04 16:08:09 -07003333 seq_puts(m, " ioremap");
Christoph Lametera10aa572008-04-28 02:12:40 -07003334
3335 if (v->flags & VM_ALLOC)
Fabian Frederickf4527c92014-06-04 16:08:09 -07003336 seq_puts(m, " vmalloc");
Christoph Lametera10aa572008-04-28 02:12:40 -07003337
3338 if (v->flags & VM_MAP)
Fabian Frederickf4527c92014-06-04 16:08:09 -07003339 seq_puts(m, " vmap");
Christoph Lametera10aa572008-04-28 02:12:40 -07003340
3341 if (v->flags & VM_USERMAP)
Fabian Frederickf4527c92014-06-04 16:08:09 -07003342 seq_puts(m, " user");
Christoph Lametera10aa572008-04-28 02:12:40 -07003343
David Rientjes244d63e2016-01-14 15:19:35 -08003344 if (is_vmalloc_addr(v->pages))
Fabian Frederickf4527c92014-06-04 16:08:09 -07003345 seq_puts(m, " vpages");
Christoph Lametera10aa572008-04-28 02:12:40 -07003346
Eric Dumazeta47a1262008-07-23 21:27:38 -07003347 show_numa_info(m, v);
Christoph Lametera10aa572008-04-28 02:12:40 -07003348 seq_putc(m, '\n');
3349 return 0;
3350}
3351
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04003352static const struct seq_operations vmalloc_op = {
Christoph Lametera10aa572008-04-28 02:12:40 -07003353 .start = s_start,
3354 .next = s_next,
3355 .stop = s_stop,
3356 .show = s_show,
3357};
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04003358
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04003359static int __init proc_vmalloc_init(void)
3360{
Christoph Hellwigfddda2b2018-04-13 19:44:18 +02003361 if (IS_ENABLED(CONFIG_NUMA))
Joe Perches0825a6f2018-06-14 15:27:58 -07003362 proc_create_seq_private("vmallocinfo", 0400, NULL,
Christoph Hellwig44414d82018-04-24 17:05:17 +02003363 &vmalloc_op,
3364 nr_node_ids * sizeof(unsigned int), NULL);
Christoph Hellwigfddda2b2018-04-13 19:44:18 +02003365 else
Joe Perches0825a6f2018-06-14 15:27:58 -07003366 proc_create_seq("vmallocinfo", 0400, NULL, &vmalloc_op);
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04003367 return 0;
3368}
3369module_init(proc_vmalloc_init);
Joonsoo Kimdb3808c2013-04-29 15:07:28 -07003370
Christoph Lametera10aa572008-04-28 02:12:40 -07003371#endif
3372