blob: c74a087fcb7d1cd003e8ccc6bc88a4b015966241 [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>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040015#include <linux/sched.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>
Jann Hornf8e84d72020-04-20 18:14:11 -070034#include <linux/overflow.h>
Gideon Israel Dsouza3b321232014-04-07 15:37:26 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/uaccess.h>
37#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);
53 struct llist_node *llnode = llist_del_all(&p->list);
54 while (llnode) {
55 void *p = llnode;
56 llnode = llist_next(llnode);
57 __vunmap(p, 1);
58 }
59}
60
Nick Piggindb64fe02008-10-18 20:27:03 -070061/*** Page table manipulation functions ***/
Adrian Bunkb2213852006-09-25 23:31:02 -070062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end)
64{
65 pte_t *pte;
66
67 pte = pte_offset_kernel(pmd, addr);
68 do {
69 pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte);
70 WARN_ON(!pte_none(ptent) && !pte_present(ptent));
71 } while (pte++, addr += PAGE_SIZE, addr != end);
72}
73
Nick Piggindb64fe02008-10-18 20:27:03 -070074static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
76 pmd_t *pmd;
77 unsigned long next;
78
79 pmd = pmd_offset(pud, addr);
80 do {
81 next = pmd_addr_end(addr, end);
Toshi Kanib9820d82015-04-14 15:47:26 -070082 if (pmd_clear_huge(pmd))
83 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (pmd_none_or_clear_bad(pmd))
85 continue;
86 vunmap_pte_range(pmd, addr, next);
87 } while (pmd++, addr = next, addr != end);
88}
89
Nick Piggindb64fe02008-10-18 20:27:03 -070090static void vunmap_pud_range(pgd_t *pgd, unsigned long addr, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 pud_t *pud;
93 unsigned long next;
94
95 pud = pud_offset(pgd, addr);
96 do {
97 next = pud_addr_end(addr, end);
Toshi Kanib9820d82015-04-14 15:47:26 -070098 if (pud_clear_huge(pud))
99 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (pud_none_or_clear_bad(pud))
101 continue;
102 vunmap_pmd_range(pud, addr, next);
103 } while (pud++, addr = next, addr != end);
104}
105
Nick Piggindb64fe02008-10-18 20:27:03 -0700106static void vunmap_page_range(unsigned long addr, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 pgd_t *pgd;
109 unsigned long next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 BUG_ON(addr >= end);
112 pgd = pgd_offset_k(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 do {
114 next = pgd_addr_end(addr, end);
115 if (pgd_none_or_clear_bad(pgd))
116 continue;
117 vunmap_pud_range(pgd, addr, next);
118 } while (pgd++, addr = next, addr != end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
121static int vmap_pte_range(pmd_t *pmd, unsigned long addr,
Nick Piggindb64fe02008-10-18 20:27:03 -0700122 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 pte_t *pte;
125
Nick Piggindb64fe02008-10-18 20:27:03 -0700126 /*
127 * nr is a running index into the array which helps higher level
128 * callers keep track of where we're up to.
129 */
130
Hugh Dickins872fec12005-10-29 18:16:21 -0700131 pte = pte_alloc_kernel(pmd, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (!pte)
133 return -ENOMEM;
134 do {
Nick Piggindb64fe02008-10-18 20:27:03 -0700135 struct page *page = pages[*nr];
136
137 if (WARN_ON(!pte_none(*pte)))
138 return -EBUSY;
139 if (WARN_ON(!page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return -ENOMEM;
141 set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
Nick Piggindb64fe02008-10-18 20:27:03 -0700142 (*nr)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 } while (pte++, addr += PAGE_SIZE, addr != end);
144 return 0;
145}
146
Nick Piggindb64fe02008-10-18 20:27:03 -0700147static int vmap_pmd_range(pud_t *pud, unsigned long addr,
148 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 pmd_t *pmd;
151 unsigned long next;
152
153 pmd = pmd_alloc(&init_mm, pud, addr);
154 if (!pmd)
155 return -ENOMEM;
156 do {
157 next = pmd_addr_end(addr, end);
Nick Piggindb64fe02008-10-18 20:27:03 -0700158 if (vmap_pte_range(pmd, addr, next, prot, pages, nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return -ENOMEM;
160 } while (pmd++, addr = next, addr != end);
161 return 0;
162}
163
Nick Piggindb64fe02008-10-18 20:27:03 -0700164static int vmap_pud_range(pgd_t *pgd, unsigned long addr,
165 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 pud_t *pud;
168 unsigned long next;
169
170 pud = pud_alloc(&init_mm, pgd, addr);
171 if (!pud)
172 return -ENOMEM;
173 do {
174 next = pud_addr_end(addr, end);
Nick Piggindb64fe02008-10-18 20:27:03 -0700175 if (vmap_pmd_range(pud, addr, next, prot, pages, nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return -ENOMEM;
177 } while (pud++, addr = next, addr != end);
178 return 0;
179}
180
Nick Piggindb64fe02008-10-18 20:27:03 -0700181/*
182 * Set up page tables in kva (addr, end). The ptes shall have prot "prot", and
183 * will have pfns corresponding to the "pages" array.
184 *
185 * Ie. pte at addr+N*PAGE_SIZE shall point to pfn corresponding to pages[N]
186 */
Tejun Heo8fc48982009-02-20 16:29:08 +0900187static int vmap_page_range_noflush(unsigned long start, unsigned long end,
188 pgprot_t prot, struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 pgd_t *pgd;
191 unsigned long next;
Adam Lackorzynski2e4e27c2009-01-04 12:00:46 -0800192 unsigned long addr = start;
Nick Piggindb64fe02008-10-18 20:27:03 -0700193 int err = 0;
194 int nr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 BUG_ON(addr >= end);
197 pgd = pgd_offset_k(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 do {
199 next = pgd_addr_end(addr, end);
Nick Piggindb64fe02008-10-18 20:27:03 -0700200 err = vmap_pud_range(pgd, addr, next, prot, pages, &nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (err)
Figo.zhangbf88c8c2009-09-21 17:01:47 -0700202 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 } while (pgd++, addr = next, addr != end);
Nick Piggindb64fe02008-10-18 20:27:03 -0700204
Nick Piggindb64fe02008-10-18 20:27:03 -0700205 return nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Tejun Heo8fc48982009-02-20 16:29:08 +0900208static int vmap_page_range(unsigned long start, unsigned long end,
209 pgprot_t prot, struct page **pages)
210{
211 int ret;
212
213 ret = vmap_page_range_noflush(start, end, prot, pages);
214 flush_cache_vmap(start, end);
215 return ret;
216}
217
KAMEZAWA Hiroyuki81ac3ad2009-09-22 16:45:49 -0700218int is_vmalloc_or_module_addr(const void *x)
Linus Torvalds73bdf0a2008-10-15 08:35:12 -0700219{
220 /*
Russell Kingab4f2ee2008-11-06 17:11:07 +0000221 * ARM, x86-64 and sparc64 put modules in a special place,
Linus Torvalds73bdf0a2008-10-15 08:35:12 -0700222 * and fall back on vmalloc() if that fails. Others
223 * just put it in the vmalloc space.
224 */
225#if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
226 unsigned long addr = (unsigned long)x;
227 if (addr >= MODULES_VADDR && addr < MODULES_END)
228 return 1;
229#endif
230 return is_vmalloc_addr(x);
231}
232
Christoph Lameter48667e72008-02-04 22:28:31 -0800233/*
malcadd688f2014-01-27 17:06:53 -0800234 * Walk a vmap address to the struct page it maps.
Christoph Lameter48667e72008-02-04 22:28:31 -0800235 */
malcadd688f2014-01-27 17:06:53 -0800236struct page *vmalloc_to_page(const void *vmalloc_addr)
Christoph Lameter48667e72008-02-04 22:28:31 -0800237{
238 unsigned long addr = (unsigned long) vmalloc_addr;
malcadd688f2014-01-27 17:06:53 -0800239 struct page *page = NULL;
Christoph Lameter48667e72008-02-04 22:28:31 -0800240 pgd_t *pgd = pgd_offset_k(addr);
Christoph Lameter48667e72008-02-04 22:28:31 -0800241
Ingo Molnar7aa413d2008-06-19 13:28:11 +0200242 /*
243 * XXX we might need to change this if we add VIRTUAL_BUG_ON for
244 * architectures that do not vmalloc module space
245 */
Linus Torvalds73bdf0a2008-10-15 08:35:12 -0700246 VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr));
Jiri Slaby59ea7462008-06-12 13:56:40 +0200247
Ard Biesheuvel647f6052017-06-23 15:08:41 -0700248 /*
249 * Don't dereference bad PUD or PMD (below) entries. This will also
250 * identify huge mappings, which we may encounter on architectures
251 * that define CONFIG_HAVE_ARCH_HUGE_VMAP=y. Such regions will be
252 * identified as vmalloc addresses by is_vmalloc_addr(), but are
253 * not [unambiguously] associated with a struct page, so there is
254 * no correct value to return for them.
255 */
Christoph Lameter48667e72008-02-04 22:28:31 -0800256 if (!pgd_none(*pgd)) {
Nick Piggindb64fe02008-10-18 20:27:03 -0700257 pud_t *pud = pud_offset(pgd, addr);
Ard Biesheuvel647f6052017-06-23 15:08:41 -0700258 WARN_ON_ONCE(pud_bad(*pud));
259 if (!pud_none(*pud) && !pud_bad(*pud)) {
Nick Piggindb64fe02008-10-18 20:27:03 -0700260 pmd_t *pmd = pmd_offset(pud, addr);
Ard Biesheuvel647f6052017-06-23 15:08:41 -0700261 WARN_ON_ONCE(pmd_bad(*pmd));
262 if (!pmd_none(*pmd) && !pmd_bad(*pmd)) {
Nick Piggindb64fe02008-10-18 20:27:03 -0700263 pte_t *ptep, pte;
264
Christoph Lameter48667e72008-02-04 22:28:31 -0800265 ptep = pte_offset_map(pmd, addr);
266 pte = *ptep;
267 if (pte_present(pte))
malcadd688f2014-01-27 17:06:53 -0800268 page = pte_page(pte);
Christoph Lameter48667e72008-02-04 22:28:31 -0800269 pte_unmap(ptep);
270 }
271 }
272 }
malcadd688f2014-01-27 17:06:53 -0800273 return page;
Jianyu Zhanece86e222014-01-21 15:49:12 -0800274}
275EXPORT_SYMBOL(vmalloc_to_page);
276
malcadd688f2014-01-27 17:06:53 -0800277/*
278 * Map a vmalloc()-space virtual address to the physical page frame number.
279 */
280unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
281{
282 return page_to_pfn(vmalloc_to_page(vmalloc_addr));
283}
284EXPORT_SYMBOL(vmalloc_to_pfn);
285
Nick Piggindb64fe02008-10-18 20:27:03 -0700286
287/*** Global kva allocator ***/
288
Nick Piggindb64fe02008-10-18 20:27:03 -0700289#define VM_VM_AREA 0x04
290
Nick Piggindb64fe02008-10-18 20:27:03 -0700291static DEFINE_SPINLOCK(vmap_area_lock);
Joonsoo Kimf1c40692013-04-29 15:07:37 -0700292/* Export for kexec only */
293LIST_HEAD(vmap_area_list);
Chris Wilson80c4bd72016-05-20 16:57:38 -0700294static LLIST_HEAD(vmap_purge_list);
Nick Piggin89699602011-03-22 16:30:36 -0700295static struct rb_root vmap_area_root = RB_ROOT;
296
297/* The vmap cache globals are protected by vmap_area_lock */
298static struct rb_node *free_vmap_cache;
299static unsigned long cached_hole_size;
300static unsigned long cached_vstart;
301static unsigned long cached_align;
302
Tejun Heoca23e402009-08-14 15:00:52 +0900303static unsigned long vmap_area_pcpu_hole;
Nick Piggindb64fe02008-10-18 20:27:03 -0700304
305static struct vmap_area *__find_vmap_area(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Nick Piggindb64fe02008-10-18 20:27:03 -0700307 struct rb_node *n = vmap_area_root.rb_node;
308
309 while (n) {
310 struct vmap_area *va;
311
312 va = rb_entry(n, struct vmap_area, rb_node);
313 if (addr < va->va_start)
314 n = n->rb_left;
HATAYAMA Daisukecef2ac32013-07-03 15:02:17 -0700315 else if (addr >= va->va_end)
Nick Piggindb64fe02008-10-18 20:27:03 -0700316 n = n->rb_right;
317 else
318 return va;
319 }
320
321 return NULL;
322}
323
324static void __insert_vmap_area(struct vmap_area *va)
325{
326 struct rb_node **p = &vmap_area_root.rb_node;
327 struct rb_node *parent = NULL;
328 struct rb_node *tmp;
329
330 while (*p) {
Namhyung Kim170168d2010-10-26 14:22:02 -0700331 struct vmap_area *tmp_va;
Nick Piggindb64fe02008-10-18 20:27:03 -0700332
333 parent = *p;
Namhyung Kim170168d2010-10-26 14:22:02 -0700334 tmp_va = rb_entry(parent, struct vmap_area, rb_node);
335 if (va->va_start < tmp_va->va_end)
Nick Piggindb64fe02008-10-18 20:27:03 -0700336 p = &(*p)->rb_left;
Namhyung Kim170168d2010-10-26 14:22:02 -0700337 else if (va->va_end > tmp_va->va_start)
Nick Piggindb64fe02008-10-18 20:27:03 -0700338 p = &(*p)->rb_right;
339 else
340 BUG();
341 }
342
343 rb_link_node(&va->rb_node, parent, p);
344 rb_insert_color(&va->rb_node, &vmap_area_root);
345
Joonsoo Kim4341fa42013-04-29 15:07:39 -0700346 /* address-sort this list */
Nick Piggindb64fe02008-10-18 20:27:03 -0700347 tmp = rb_prev(&va->rb_node);
348 if (tmp) {
349 struct vmap_area *prev;
350 prev = rb_entry(tmp, struct vmap_area, rb_node);
351 list_add_rcu(&va->list, &prev->list);
352 } else
353 list_add_rcu(&va->list, &vmap_area_list);
354}
355
356static void purge_vmap_area_lazy(void);
357
Chris Wilson4da56b92016-04-04 14:46:42 +0100358static BLOCKING_NOTIFIER_HEAD(vmap_notify_list);
359
Nick Piggindb64fe02008-10-18 20:27:03 -0700360/*
361 * Allocate a region of KVA of the specified size and alignment, within the
362 * vstart and vend.
363 */
364static struct vmap_area *alloc_vmap_area(unsigned long size,
365 unsigned long align,
366 unsigned long vstart, unsigned long vend,
367 int node, gfp_t gfp_mask)
368{
369 struct vmap_area *va;
370 struct rb_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 unsigned long addr;
Nick Piggindb64fe02008-10-18 20:27:03 -0700372 int purged = 0;
Nick Piggin89699602011-03-22 16:30:36 -0700373 struct vmap_area *first;
Nick Piggindb64fe02008-10-18 20:27:03 -0700374
Nick Piggin77669702009-02-27 14:03:03 -0800375 BUG_ON(!size);
Alexander Kuleshov891c49a2015-11-05 18:46:51 -0800376 BUG_ON(offset_in_page(size));
Nick Piggin89699602011-03-22 16:30:36 -0700377 BUG_ON(!is_power_of_2(align));
Nick Piggindb64fe02008-10-18 20:27:03 -0700378
Chris Wilson4da56b92016-04-04 14:46:42 +0100379 might_sleep_if(gfpflags_allow_blocking(gfp_mask));
380
Nick Piggindb64fe02008-10-18 20:27:03 -0700381 va = kmalloc_node(sizeof(struct vmap_area),
382 gfp_mask & GFP_RECLAIM_MASK, node);
383 if (unlikely(!va))
384 return ERR_PTR(-ENOMEM);
385
Catalin Marinas7f88f882013-11-12 15:07:45 -0800386 /*
387 * Only scan the relevant parts containing pointers to other objects
388 * to avoid false negatives.
389 */
390 kmemleak_scan_area(&va->rb_node, SIZE_MAX, gfp_mask & GFP_RECLAIM_MASK);
391
Nick Piggindb64fe02008-10-18 20:27:03 -0700392retry:
393 spin_lock(&vmap_area_lock);
Nick Piggin89699602011-03-22 16:30:36 -0700394 /*
395 * Invalidate cache if we have more permissive parameters.
396 * cached_hole_size notes the largest hole noticed _below_
397 * the vmap_area cached in free_vmap_cache: if size fits
398 * into that hole, we want to scan from vstart to reuse
399 * the hole instead of allocating above free_vmap_cache.
400 * Note that __free_vmap_area may update free_vmap_cache
401 * without updating cached_hole_size or cached_align.
402 */
403 if (!free_vmap_cache ||
404 size < cached_hole_size ||
405 vstart < cached_vstart ||
406 align < cached_align) {
407nocache:
408 cached_hole_size = 0;
409 free_vmap_cache = NULL;
410 }
411 /* record if we encounter less permissive parameters */
412 cached_vstart = vstart;
413 cached_align = align;
Nick Piggin77669702009-02-27 14:03:03 -0800414
Nick Piggin89699602011-03-22 16:30:36 -0700415 /* find starting point for our search */
416 if (free_vmap_cache) {
417 first = rb_entry(free_vmap_cache, struct vmap_area, rb_node);
Johannes Weiner248ac0e2011-05-24 17:11:43 -0700418 addr = ALIGN(first->va_end, align);
Nick Piggin89699602011-03-22 16:30:36 -0700419 if (addr < vstart)
420 goto nocache;
Zhang Yanfeibcb615a2013-07-08 16:00:19 -0700421 if (addr + size < addr)
Nick Piggin89699602011-03-22 16:30:36 -0700422 goto overflow;
Nick Piggindb64fe02008-10-18 20:27:03 -0700423
Nick Piggin89699602011-03-22 16:30:36 -0700424 } else {
425 addr = ALIGN(vstart, align);
Zhang Yanfeibcb615a2013-07-08 16:00:19 -0700426 if (addr + size < addr)
Nick Piggin89699602011-03-22 16:30:36 -0700427 goto overflow;
428
429 n = vmap_area_root.rb_node;
430 first = NULL;
431
432 while (n) {
Nick Piggindb64fe02008-10-18 20:27:03 -0700433 struct vmap_area *tmp;
434 tmp = rb_entry(n, struct vmap_area, rb_node);
435 if (tmp->va_end >= addr) {
Nick Piggindb64fe02008-10-18 20:27:03 -0700436 first = tmp;
Nick Piggin89699602011-03-22 16:30:36 -0700437 if (tmp->va_start <= addr)
438 break;
439 n = n->rb_left;
440 } else
Nick Piggindb64fe02008-10-18 20:27:03 -0700441 n = n->rb_right;
Nick Piggin89699602011-03-22 16:30:36 -0700442 }
Nick Piggindb64fe02008-10-18 20:27:03 -0700443
444 if (!first)
445 goto found;
Nick Piggindb64fe02008-10-18 20:27:03 -0700446 }
Nick Piggin89699602011-03-22 16:30:36 -0700447
448 /* from the starting point, walk areas until a suitable hole is found */
Johannes Weiner248ac0e2011-05-24 17:11:43 -0700449 while (addr + size > first->va_start && addr + size <= vend) {
Nick Piggin89699602011-03-22 16:30:36 -0700450 if (addr + cached_hole_size < first->va_start)
451 cached_hole_size = first->va_start - addr;
Johannes Weiner248ac0e2011-05-24 17:11:43 -0700452 addr = ALIGN(first->va_end, align);
Zhang Yanfeibcb615a2013-07-08 16:00:19 -0700453 if (addr + size < addr)
Nick Piggin89699602011-03-22 16:30:36 -0700454 goto overflow;
455
Hong zhi guo92ca9222012-07-31 16:41:35 -0700456 if (list_is_last(&first->list, &vmap_area_list))
Nick Piggin89699602011-03-22 16:30:36 -0700457 goto found;
Hong zhi guo92ca9222012-07-31 16:41:35 -0700458
Geliang Tang6219c2a2016-01-14 15:19:08 -0800459 first = list_next_entry(first, list);
Nick Piggin89699602011-03-22 16:30:36 -0700460 }
461
Nick Piggindb64fe02008-10-18 20:27:03 -0700462found:
Uladzislau Rezki (Sony)e6bd3ed2019-03-05 15:45:59 -0800463 /*
464 * Check also calculated address against the vstart,
465 * because it can be 0 because of big align request.
466 */
467 if (addr + size > vend || addr < vstart)
Nick Piggin89699602011-03-22 16:30:36 -0700468 goto overflow;
Nick Piggindb64fe02008-10-18 20:27:03 -0700469
470 va->va_start = addr;
471 va->va_end = addr + size;
472 va->flags = 0;
473 __insert_vmap_area(va);
Nick Piggin89699602011-03-22 16:30:36 -0700474 free_vmap_cache = &va->rb_node;
Nick Piggindb64fe02008-10-18 20:27:03 -0700475 spin_unlock(&vmap_area_lock);
476
Wang Xiaoqiang61e16552016-01-15 16:57:19 -0800477 BUG_ON(!IS_ALIGNED(va->va_start, align));
Nick Piggin89699602011-03-22 16:30:36 -0700478 BUG_ON(va->va_start < vstart);
479 BUG_ON(va->va_end > vend);
480
Nick Piggindb64fe02008-10-18 20:27:03 -0700481 return va;
Nick Piggin89699602011-03-22 16:30:36 -0700482
483overflow:
484 spin_unlock(&vmap_area_lock);
485 if (!purged) {
486 purge_vmap_area_lazy();
487 purged = 1;
488 goto retry;
489 }
Chris Wilson4da56b92016-04-04 14:46:42 +0100490
491 if (gfpflags_allow_blocking(gfp_mask)) {
492 unsigned long freed = 0;
493 blocking_notifier_call_chain(&vmap_notify_list, 0, &freed);
494 if (freed > 0) {
495 purged = 0;
496 goto retry;
497 }
498 }
499
Nick Piggin89699602011-03-22 16:30:36 -0700500 if (printk_ratelimit())
Joe Perches756a025f02016-03-17 14:19:47 -0700501 pr_warn("vmap allocation for size %lu failed: use vmalloc=<size> to increase size\n",
502 size);
Nick Piggin89699602011-03-22 16:30:36 -0700503 kfree(va);
504 return ERR_PTR(-EBUSY);
Nick Piggindb64fe02008-10-18 20:27:03 -0700505}
506
Chris Wilson4da56b92016-04-04 14:46:42 +0100507int register_vmap_purge_notifier(struct notifier_block *nb)
508{
509 return blocking_notifier_chain_register(&vmap_notify_list, nb);
510}
511EXPORT_SYMBOL_GPL(register_vmap_purge_notifier);
512
513int unregister_vmap_purge_notifier(struct notifier_block *nb)
514{
515 return blocking_notifier_chain_unregister(&vmap_notify_list, nb);
516}
517EXPORT_SYMBOL_GPL(unregister_vmap_purge_notifier);
518
Nick Piggindb64fe02008-10-18 20:27:03 -0700519static void __free_vmap_area(struct vmap_area *va)
520{
521 BUG_ON(RB_EMPTY_NODE(&va->rb_node));
Nick Piggin89699602011-03-22 16:30:36 -0700522
523 if (free_vmap_cache) {
524 if (va->va_end < cached_vstart) {
525 free_vmap_cache = NULL;
526 } else {
527 struct vmap_area *cache;
528 cache = rb_entry(free_vmap_cache, struct vmap_area, rb_node);
529 if (va->va_start <= cache->va_start) {
530 free_vmap_cache = rb_prev(&va->rb_node);
531 /*
532 * We don't try to update cached_hole_size or
533 * cached_align, but it won't go very wrong.
534 */
535 }
536 }
537 }
Nick Piggindb64fe02008-10-18 20:27:03 -0700538 rb_erase(&va->rb_node, &vmap_area_root);
539 RB_CLEAR_NODE(&va->rb_node);
540 list_del_rcu(&va->list);
541
Tejun Heoca23e402009-08-14 15:00:52 +0900542 /*
543 * Track the highest possible candidate for pcpu area
544 * allocation. Areas outside of vmalloc area can be returned
545 * here too, consider only end addresses which fall inside
546 * vmalloc area proper.
547 */
548 if (va->va_end > VMALLOC_START && va->va_end <= VMALLOC_END)
549 vmap_area_pcpu_hole = max(vmap_area_pcpu_hole, va->va_end);
550
Lai Jiangshan14769de2011-03-18 12:12:19 +0800551 kfree_rcu(va, rcu_head);
Nick Piggindb64fe02008-10-18 20:27:03 -0700552}
553
554/*
555 * Free a region of KVA allocated by alloc_vmap_area
556 */
557static void free_vmap_area(struct vmap_area *va)
558{
559 spin_lock(&vmap_area_lock);
560 __free_vmap_area(va);
561 spin_unlock(&vmap_area_lock);
562}
563
564/*
565 * Clear the pagetable entries of a given vmap_area
566 */
567static void unmap_vmap_area(struct vmap_area *va)
568{
569 vunmap_page_range(va->va_start, va->va_end);
570}
571
Nick Piggincd528582009-01-06 14:39:20 -0800572static void vmap_debug_free_range(unsigned long start, unsigned long end)
573{
574 /*
Joonsoo Kimf48d97f2016-03-17 14:17:49 -0700575 * Unmap page tables and force a TLB flush immediately if pagealloc
576 * debugging is enabled. This catches use after free bugs similarly to
577 * those in linear kernel virtual address space after a page has been
578 * freed.
Nick Piggincd528582009-01-06 14:39:20 -0800579 *
Joonsoo Kimf48d97f2016-03-17 14:17:49 -0700580 * All the lazy freeing logic is still retained, in order to minimise
581 * intrusiveness of this debugging feature.
Nick Piggincd528582009-01-06 14:39:20 -0800582 *
Joonsoo Kimf48d97f2016-03-17 14:17:49 -0700583 * This is going to be *slow* (linear kernel virtual address debugging
584 * doesn't do a broadcast TLB flush so it is a lot faster).
Nick Piggincd528582009-01-06 14:39:20 -0800585 */
Joonsoo Kimf48d97f2016-03-17 14:17:49 -0700586 if (debug_pagealloc_enabled()) {
587 vunmap_page_range(start, end);
588 flush_tlb_kernel_range(start, end);
589 }
Nick Piggincd528582009-01-06 14:39:20 -0800590}
591
Nick Piggindb64fe02008-10-18 20:27:03 -0700592/*
593 * lazy_max_pages is the maximum amount of virtual address space we gather up
594 * before attempting to purge with a TLB flush.
595 *
596 * There is a tradeoff here: a larger number will cover more kernel page tables
597 * and take slightly longer to purge, but it will linearly reduce the number of
598 * global TLB flushes that must be performed. It would seem natural to scale
599 * this number up linearly with the number of CPUs (because vmapping activity
600 * could also scale linearly with the number of CPUs), however it is likely
601 * that in practice, workloads might be constrained in other ways that mean
602 * vmap activity will not scale linearly with CPUs. Also, I want to be
603 * conservative and not introduce a big latency on huge systems, so go with
604 * a less aggressive log scale. It will still be an improvement over the old
605 * code, and it will be simple to change the scale factor if we find that it
606 * becomes a problem on bigger systems.
607 */
608static unsigned long lazy_max_pages(void)
609{
610 unsigned int log;
611
612 log = fls(num_online_cpus());
613
614 return log * (32UL * 1024 * 1024 / PAGE_SIZE);
615}
616
617static atomic_t vmap_lazy_nr = ATOMIC_INIT(0);
618
Nick Piggin02b709d2010-02-01 22:25:57 +1100619/* for per-CPU blocks */
620static void purge_fragmented_blocks_allcpus(void);
621
Nick Piggindb64fe02008-10-18 20:27:03 -0700622/*
Cliff Wickman3ee48b62010-09-16 11:44:02 -0500623 * called before a call to iounmap() if the caller wants vm_area_struct's
624 * immediately freed.
625 */
626void set_iounmap_nonlazy(void)
627{
628 atomic_set(&vmap_lazy_nr, lazy_max_pages()+1);
629}
630
631/*
Nick Piggindb64fe02008-10-18 20:27:03 -0700632 * Purges all lazily-freed vmap areas.
633 *
634 * If sync is 0 then don't purge if there is already a purge in progress.
635 * If force_flush is 1, then flush kernel TLBs between *start and *end even
636 * if we found no lazy vmap areas to unmap (callers can use this to optimise
637 * their own TLB flushing).
638 * Returns with *start = min(*start, lowest purged address)
639 * *end = max(*end, highest purged address)
640 */
641static void __purge_vmap_area_lazy(unsigned long *start, unsigned long *end,
642 int sync, int force_flush)
643{
Andrew Morton46666d82009-01-15 13:51:15 -0800644 static DEFINE_SPINLOCK(purge_lock);
Chris Wilson80c4bd72016-05-20 16:57:38 -0700645 struct llist_node *valist;
Nick Piggindb64fe02008-10-18 20:27:03 -0700646 struct vmap_area *va;
Vegard Nossumcbb76672009-02-27 14:03:04 -0800647 struct vmap_area *n_va;
Nick Piggindb64fe02008-10-18 20:27:03 -0700648 int nr = 0;
649
650 /*
651 * If sync is 0 but force_flush is 1, we'll go sync anyway but callers
652 * should not expect such behaviour. This just simplifies locking for
653 * the case that isn't actually used at the moment anyway.
654 */
655 if (!sync && !force_flush) {
Andrew Morton46666d82009-01-15 13:51:15 -0800656 if (!spin_trylock(&purge_lock))
Nick Piggindb64fe02008-10-18 20:27:03 -0700657 return;
658 } else
Andrew Morton46666d82009-01-15 13:51:15 -0800659 spin_lock(&purge_lock);
Nick Piggindb64fe02008-10-18 20:27:03 -0700660
Nick Piggin02b709d2010-02-01 22:25:57 +1100661 if (sync)
662 purge_fragmented_blocks_allcpus();
663
Chris Wilson80c4bd72016-05-20 16:57:38 -0700664 valist = llist_del_all(&vmap_purge_list);
665 llist_for_each_entry(va, valist, purge_list) {
666 if (va->va_start < *start)
667 *start = va->va_start;
668 if (va->va_end > *end)
669 *end = va->va_end;
670 nr += (va->va_end - va->va_start) >> PAGE_SHIFT;
Nick Piggindb64fe02008-10-18 20:27:03 -0700671 }
Nick Piggindb64fe02008-10-18 20:27:03 -0700672
Yongseok Koh88f50042010-01-19 17:33:49 +0900673 if (nr)
Nick Piggindb64fe02008-10-18 20:27:03 -0700674 atomic_sub(nr, &vmap_lazy_nr);
Nick Piggindb64fe02008-10-18 20:27:03 -0700675
676 if (nr || force_flush)
677 flush_tlb_kernel_range(*start, *end);
678
679 if (nr) {
680 spin_lock(&vmap_area_lock);
Chris Wilson80c4bd72016-05-20 16:57:38 -0700681 llist_for_each_entry_safe(va, n_va, valist, purge_list)
Nick Piggindb64fe02008-10-18 20:27:03 -0700682 __free_vmap_area(va);
683 spin_unlock(&vmap_area_lock);
684 }
Andrew Morton46666d82009-01-15 13:51:15 -0800685 spin_unlock(&purge_lock);
Nick Piggindb64fe02008-10-18 20:27:03 -0700686}
687
688/*
Nick Piggin496850e2008-11-19 15:36:33 -0800689 * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
690 * is already purging.
691 */
692static void try_purge_vmap_area_lazy(void)
693{
694 unsigned long start = ULONG_MAX, end = 0;
695
696 __purge_vmap_area_lazy(&start, &end, 0, 0);
697}
698
699/*
Nick Piggindb64fe02008-10-18 20:27:03 -0700700 * Kick off a purge of the outstanding lazy areas.
701 */
702static void purge_vmap_area_lazy(void)
703{
704 unsigned long start = ULONG_MAX, end = 0;
705
Nick Piggin496850e2008-11-19 15:36:33 -0800706 __purge_vmap_area_lazy(&start, &end, 1, 0);
Nick Piggindb64fe02008-10-18 20:27:03 -0700707}
708
709/*
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -0800710 * Free a vmap area, caller ensuring that the area has been unmapped
711 * and flush_cache_vunmap had been called for the correct range
712 * previously.
Nick Piggindb64fe02008-10-18 20:27:03 -0700713 */
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -0800714static void free_vmap_area_noflush(struct vmap_area *va)
Nick Piggindb64fe02008-10-18 20:27:03 -0700715{
Chris Wilson80c4bd72016-05-20 16:57:38 -0700716 int nr_lazy;
717
718 nr_lazy = atomic_add_return((va->va_end - va->va_start) >> PAGE_SHIFT,
719 &vmap_lazy_nr);
720
721 /* After this point, we may free va at any time */
722 llist_add(&va->purge_list, &vmap_purge_list);
723
724 if (unlikely(nr_lazy > lazy_max_pages()))
Nick Piggin496850e2008-11-19 15:36:33 -0800725 try_purge_vmap_area_lazy();
Nick Piggindb64fe02008-10-18 20:27:03 -0700726}
727
Nick Pigginb29acbd2008-12-01 13:13:47 -0800728/*
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -0800729 * Free and unmap a vmap area, caller ensuring flush_cache_vunmap had been
730 * called for the correct range previously.
731 */
732static void free_unmap_vmap_area_noflush(struct vmap_area *va)
733{
734 unmap_vmap_area(va);
735 free_vmap_area_noflush(va);
736}
737
738/*
Nick Pigginb29acbd2008-12-01 13:13:47 -0800739 * Free and unmap a vmap area
740 */
741static void free_unmap_vmap_area(struct vmap_area *va)
742{
743 flush_cache_vunmap(va->va_start, va->va_end);
744 free_unmap_vmap_area_noflush(va);
745}
746
Nick Piggindb64fe02008-10-18 20:27:03 -0700747static struct vmap_area *find_vmap_area(unsigned long addr)
748{
749 struct vmap_area *va;
750
751 spin_lock(&vmap_area_lock);
752 va = __find_vmap_area(addr);
753 spin_unlock(&vmap_area_lock);
754
755 return va;
756}
757
758static void free_unmap_vmap_area_addr(unsigned long addr)
759{
760 struct vmap_area *va;
761
762 va = find_vmap_area(addr);
763 BUG_ON(!va);
764 free_unmap_vmap_area(va);
765}
766
767
768/*** Per cpu kva allocator ***/
769
770/*
771 * vmap space is limited especially on 32 bit architectures. Ensure there is
772 * room for at least 16 percpu vmap blocks per CPU.
773 */
774/*
775 * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
776 * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess
777 * instead (we just need a rough idea)
778 */
779#if BITS_PER_LONG == 32
780#define VMALLOC_SPACE (128UL*1024*1024)
781#else
782#define VMALLOC_SPACE (128UL*1024*1024*1024)
783#endif
784
785#define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE)
786#define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */
787#define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */
788#define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2)
789#define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */
790#define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */
Clemens Ladischf982f912011-06-21 22:09:50 +0200791#define VMAP_BBMAP_BITS \
792 VMAP_MIN(VMAP_BBMAP_BITS_MAX, \
793 VMAP_MAX(VMAP_BBMAP_BITS_MIN, \
794 VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16))
Nick Piggindb64fe02008-10-18 20:27:03 -0700795
796#define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE)
797
Jeremy Fitzhardinge9b463332008-10-28 19:22:34 +1100798static bool vmap_initialized __read_mostly = false;
799
Nick Piggindb64fe02008-10-18 20:27:03 -0700800struct vmap_block_queue {
801 spinlock_t lock;
802 struct list_head free;
Nick Piggindb64fe02008-10-18 20:27:03 -0700803};
804
805struct vmap_block {
806 spinlock_t lock;
807 struct vmap_area *va;
Nick Piggindb64fe02008-10-18 20:27:03 -0700808 unsigned long free, dirty;
Roman Pen7d61bfe2015-04-15 16:13:55 -0700809 unsigned long dirty_min, dirty_max; /*< dirty range */
Nick Pigginde560422010-02-01 22:24:18 +1100810 struct list_head free_list;
811 struct rcu_head rcu_head;
Nick Piggin02b709d2010-02-01 22:25:57 +1100812 struct list_head purge;
Nick Piggindb64fe02008-10-18 20:27:03 -0700813};
814
815/* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
816static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
817
818/*
819 * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block
820 * in the free path. Could get rid of this if we change the API to return a
821 * "cookie" from alloc, to be passed to free. But no big deal yet.
822 */
823static DEFINE_SPINLOCK(vmap_block_tree_lock);
824static RADIX_TREE(vmap_block_tree, GFP_ATOMIC);
825
826/*
827 * We should probably have a fallback mechanism to allocate virtual memory
828 * out of partially filled vmap blocks. However vmap block sizing should be
829 * fairly reasonable according to the vmalloc size, so it shouldn't be a
830 * big problem.
831 */
832
833static unsigned long addr_to_vb_idx(unsigned long addr)
834{
835 addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
836 addr /= VMAP_BLOCK_SIZE;
837 return addr;
838}
839
Roman Pencf725ce2015-04-15 16:13:52 -0700840static void *vmap_block_vaddr(unsigned long va_start, unsigned long pages_off)
841{
842 unsigned long addr;
843
844 addr = va_start + (pages_off << PAGE_SHIFT);
845 BUG_ON(addr_to_vb_idx(addr) != addr_to_vb_idx(va_start));
846 return (void *)addr;
847}
848
849/**
850 * new_vmap_block - allocates new vmap_block and occupies 2^order pages in this
851 * block. Of course pages number can't exceed VMAP_BBMAP_BITS
852 * @order: how many 2^order pages should be occupied in newly allocated block
853 * @gfp_mask: flags for the page level allocator
854 *
855 * Returns: virtual address in a newly allocated block or ERR_PTR(-errno)
856 */
857static void *new_vmap_block(unsigned int order, gfp_t gfp_mask)
Nick Piggindb64fe02008-10-18 20:27:03 -0700858{
859 struct vmap_block_queue *vbq;
860 struct vmap_block *vb;
861 struct vmap_area *va;
862 unsigned long vb_idx;
863 int node, err;
Roman Pencf725ce2015-04-15 16:13:52 -0700864 void *vaddr;
Nick Piggindb64fe02008-10-18 20:27:03 -0700865
866 node = numa_node_id();
867
868 vb = kmalloc_node(sizeof(struct vmap_block),
869 gfp_mask & GFP_RECLAIM_MASK, node);
870 if (unlikely(!vb))
871 return ERR_PTR(-ENOMEM);
872
873 va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
874 VMALLOC_START, VMALLOC_END,
875 node, gfp_mask);
Tobias Klauserddf9c6d2011-01-13 15:46:15 -0800876 if (IS_ERR(va)) {
Nick Piggindb64fe02008-10-18 20:27:03 -0700877 kfree(vb);
Julia Lawalle7d86342010-08-09 17:18:28 -0700878 return ERR_CAST(va);
Nick Piggindb64fe02008-10-18 20:27:03 -0700879 }
880
881 err = radix_tree_preload(gfp_mask);
882 if (unlikely(err)) {
883 kfree(vb);
884 free_vmap_area(va);
885 return ERR_PTR(err);
886 }
887
Roman Pencf725ce2015-04-15 16:13:52 -0700888 vaddr = vmap_block_vaddr(va->va_start, 0);
Nick Piggindb64fe02008-10-18 20:27:03 -0700889 spin_lock_init(&vb->lock);
890 vb->va = va;
Roman Pencf725ce2015-04-15 16:13:52 -0700891 /* At least something should be left free */
892 BUG_ON(VMAP_BBMAP_BITS <= (1UL << order));
893 vb->free = VMAP_BBMAP_BITS - (1UL << order);
Nick Piggindb64fe02008-10-18 20:27:03 -0700894 vb->dirty = 0;
Roman Pen7d61bfe2015-04-15 16:13:55 -0700895 vb->dirty_min = VMAP_BBMAP_BITS;
896 vb->dirty_max = 0;
Nick Piggindb64fe02008-10-18 20:27:03 -0700897 INIT_LIST_HEAD(&vb->free_list);
Nick Piggindb64fe02008-10-18 20:27:03 -0700898
899 vb_idx = addr_to_vb_idx(va->va_start);
900 spin_lock(&vmap_block_tree_lock);
901 err = radix_tree_insert(&vmap_block_tree, vb_idx, vb);
902 spin_unlock(&vmap_block_tree_lock);
903 BUG_ON(err);
904 radix_tree_preload_end();
905
906 vbq = &get_cpu_var(vmap_block_queue);
Nick Piggindb64fe02008-10-18 20:27:03 -0700907 spin_lock(&vbq->lock);
Roman Pen68ac5462015-04-15 16:13:48 -0700908 list_add_tail_rcu(&vb->free_list, &vbq->free);
Nick Piggindb64fe02008-10-18 20:27:03 -0700909 spin_unlock(&vbq->lock);
Tejun Heo3f04ba82009-10-29 22:34:12 +0900910 put_cpu_var(vmap_block_queue);
Nick Piggindb64fe02008-10-18 20:27:03 -0700911
Roman Pencf725ce2015-04-15 16:13:52 -0700912 return vaddr;
Nick Piggindb64fe02008-10-18 20:27:03 -0700913}
914
Nick Piggindb64fe02008-10-18 20:27:03 -0700915static void free_vmap_block(struct vmap_block *vb)
916{
917 struct vmap_block *tmp;
918 unsigned long vb_idx;
919
Nick Piggindb64fe02008-10-18 20:27:03 -0700920 vb_idx = addr_to_vb_idx(vb->va->va_start);
921 spin_lock(&vmap_block_tree_lock);
922 tmp = radix_tree_delete(&vmap_block_tree, vb_idx);
923 spin_unlock(&vmap_block_tree_lock);
924 BUG_ON(tmp != vb);
925
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -0800926 free_vmap_area_noflush(vb->va);
Lai Jiangshan22a3c7d2011-03-18 12:13:08 +0800927 kfree_rcu(vb, rcu_head);
Nick Piggindb64fe02008-10-18 20:27:03 -0700928}
929
Nick Piggin02b709d2010-02-01 22:25:57 +1100930static void purge_fragmented_blocks(int cpu)
931{
932 LIST_HEAD(purge);
933 struct vmap_block *vb;
934 struct vmap_block *n_vb;
935 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
936
937 rcu_read_lock();
938 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
939
940 if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS))
941 continue;
942
943 spin_lock(&vb->lock);
944 if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) {
945 vb->free = 0; /* prevent further allocs after releasing lock */
946 vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */
Roman Pen7d61bfe2015-04-15 16:13:55 -0700947 vb->dirty_min = 0;
948 vb->dirty_max = VMAP_BBMAP_BITS;
Nick Piggin02b709d2010-02-01 22:25:57 +1100949 spin_lock(&vbq->lock);
950 list_del_rcu(&vb->free_list);
951 spin_unlock(&vbq->lock);
952 spin_unlock(&vb->lock);
953 list_add_tail(&vb->purge, &purge);
954 } else
955 spin_unlock(&vb->lock);
956 }
957 rcu_read_unlock();
958
959 list_for_each_entry_safe(vb, n_vb, &purge, purge) {
960 list_del(&vb->purge);
961 free_vmap_block(vb);
962 }
963}
964
Nick Piggin02b709d2010-02-01 22:25:57 +1100965static void purge_fragmented_blocks_allcpus(void)
966{
967 int cpu;
968
969 for_each_possible_cpu(cpu)
970 purge_fragmented_blocks(cpu);
971}
972
Nick Piggindb64fe02008-10-18 20:27:03 -0700973static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
974{
975 struct vmap_block_queue *vbq;
976 struct vmap_block *vb;
Roman Pencf725ce2015-04-15 16:13:52 -0700977 void *vaddr = NULL;
Nick Piggindb64fe02008-10-18 20:27:03 -0700978 unsigned int order;
979
Alexander Kuleshov891c49a2015-11-05 18:46:51 -0800980 BUG_ON(offset_in_page(size));
Nick Piggindb64fe02008-10-18 20:27:03 -0700981 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
Jan Karaaa91c4d2012-07-31 16:41:37 -0700982 if (WARN_ON(size == 0)) {
983 /*
984 * Allocating 0 bytes isn't what caller wants since
985 * get_order(0) returns funny result. Just warn and terminate
986 * early.
987 */
988 return NULL;
989 }
Nick Piggindb64fe02008-10-18 20:27:03 -0700990 order = get_order(size);
991
Nick Piggindb64fe02008-10-18 20:27:03 -0700992 rcu_read_lock();
993 vbq = &get_cpu_var(vmap_block_queue);
994 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
Roman Pencf725ce2015-04-15 16:13:52 -0700995 unsigned long pages_off;
Nick Piggindb64fe02008-10-18 20:27:03 -0700996
997 spin_lock(&vb->lock);
Roman Pencf725ce2015-04-15 16:13:52 -0700998 if (vb->free < (1UL << order)) {
999 spin_unlock(&vb->lock);
1000 continue;
1001 }
Nick Piggin02b709d2010-02-01 22:25:57 +11001002
Roman Pencf725ce2015-04-15 16:13:52 -07001003 pages_off = VMAP_BBMAP_BITS - vb->free;
1004 vaddr = vmap_block_vaddr(vb->va->va_start, pages_off);
Nick Piggin02b709d2010-02-01 22:25:57 +11001005 vb->free -= 1UL << order;
1006 if (vb->free == 0) {
1007 spin_lock(&vbq->lock);
1008 list_del_rcu(&vb->free_list);
1009 spin_unlock(&vbq->lock);
Nick Piggindb64fe02008-10-18 20:27:03 -07001010 }
Roman Pencf725ce2015-04-15 16:13:52 -07001011
Nick Piggindb64fe02008-10-18 20:27:03 -07001012 spin_unlock(&vb->lock);
Nick Piggin02b709d2010-02-01 22:25:57 +11001013 break;
Nick Piggindb64fe02008-10-18 20:27:03 -07001014 }
Nick Piggin02b709d2010-02-01 22:25:57 +11001015
Tejun Heo3f04ba82009-10-29 22:34:12 +09001016 put_cpu_var(vmap_block_queue);
Nick Piggindb64fe02008-10-18 20:27:03 -07001017 rcu_read_unlock();
1018
Roman Pencf725ce2015-04-15 16:13:52 -07001019 /* Allocate new block if nothing was found */
1020 if (!vaddr)
1021 vaddr = new_vmap_block(order, gfp_mask);
Nick Piggindb64fe02008-10-18 20:27:03 -07001022
Roman Pencf725ce2015-04-15 16:13:52 -07001023 return vaddr;
Nick Piggindb64fe02008-10-18 20:27:03 -07001024}
1025
1026static void vb_free(const void *addr, unsigned long size)
1027{
1028 unsigned long offset;
1029 unsigned long vb_idx;
1030 unsigned int order;
1031 struct vmap_block *vb;
1032
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08001033 BUG_ON(offset_in_page(size));
Nick Piggindb64fe02008-10-18 20:27:03 -07001034 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
Nick Pigginb29acbd2008-12-01 13:13:47 -08001035
1036 flush_cache_vunmap((unsigned long)addr, (unsigned long)addr + size);
1037
Nick Piggindb64fe02008-10-18 20:27:03 -07001038 order = get_order(size);
1039
1040 offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1);
Roman Pen7d61bfe2015-04-15 16:13:55 -07001041 offset >>= PAGE_SHIFT;
Nick Piggindb64fe02008-10-18 20:27:03 -07001042
1043 vb_idx = addr_to_vb_idx((unsigned long)addr);
1044 rcu_read_lock();
1045 vb = radix_tree_lookup(&vmap_block_tree, vb_idx);
1046 rcu_read_unlock();
1047 BUG_ON(!vb);
1048
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -08001049 vunmap_page_range((unsigned long)addr, (unsigned long)addr + size);
1050
Nick Piggindb64fe02008-10-18 20:27:03 -07001051 spin_lock(&vb->lock);
Roman Pen7d61bfe2015-04-15 16:13:55 -07001052
1053 /* Expand dirty range */
1054 vb->dirty_min = min(vb->dirty_min, offset);
1055 vb->dirty_max = max(vb->dirty_max, offset + (1UL << order));
MinChan Kimd0868172009-03-31 15:19:26 -07001056
Nick Piggindb64fe02008-10-18 20:27:03 -07001057 vb->dirty += 1UL << order;
1058 if (vb->dirty == VMAP_BBMAP_BITS) {
Nick Pigginde560422010-02-01 22:24:18 +11001059 BUG_ON(vb->free);
Nick Piggindb64fe02008-10-18 20:27:03 -07001060 spin_unlock(&vb->lock);
1061 free_vmap_block(vb);
1062 } else
1063 spin_unlock(&vb->lock);
1064}
1065
1066/**
1067 * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
1068 *
1069 * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
1070 * to amortize TLB flushing overheads. What this means is that any page you
1071 * have now, may, in a former life, have been mapped into kernel virtual
1072 * address by the vmap layer and so there might be some CPUs with TLB entries
1073 * still referencing that page (additional to the regular 1:1 kernel mapping).
1074 *
1075 * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
1076 * be sure that none of the pages we have control over will have any aliases
1077 * from the vmap layer.
1078 */
1079void vm_unmap_aliases(void)
1080{
1081 unsigned long start = ULONG_MAX, end = 0;
1082 int cpu;
1083 int flush = 0;
1084
Jeremy Fitzhardinge9b463332008-10-28 19:22:34 +11001085 if (unlikely(!vmap_initialized))
1086 return;
1087
Nick Piggindb64fe02008-10-18 20:27:03 -07001088 for_each_possible_cpu(cpu) {
1089 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
1090 struct vmap_block *vb;
1091
1092 rcu_read_lock();
1093 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
Nick Piggindb64fe02008-10-18 20:27:03 -07001094 spin_lock(&vb->lock);
Roman Pen7d61bfe2015-04-15 16:13:55 -07001095 if (vb->dirty) {
1096 unsigned long va_start = vb->va->va_start;
Nick Piggindb64fe02008-10-18 20:27:03 -07001097 unsigned long s, e;
Joonsoo Kimb136be5e2013-09-11 14:21:40 -07001098
Roman Pen7d61bfe2015-04-15 16:13:55 -07001099 s = va_start + (vb->dirty_min << PAGE_SHIFT);
1100 e = va_start + (vb->dirty_max << PAGE_SHIFT);
Nick Piggindb64fe02008-10-18 20:27:03 -07001101
Roman Pen7d61bfe2015-04-15 16:13:55 -07001102 start = min(s, start);
1103 end = max(e, end);
1104
Nick Piggindb64fe02008-10-18 20:27:03 -07001105 flush = 1;
Nick Piggindb64fe02008-10-18 20:27:03 -07001106 }
1107 spin_unlock(&vb->lock);
1108 }
1109 rcu_read_unlock();
1110 }
1111
1112 __purge_vmap_area_lazy(&start, &end, 1, flush);
1113}
1114EXPORT_SYMBOL_GPL(vm_unmap_aliases);
1115
1116/**
1117 * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
1118 * @mem: the pointer returned by vm_map_ram
1119 * @count: the count passed to that vm_map_ram call (cannot unmap partial)
1120 */
1121void vm_unmap_ram(const void *mem, unsigned int count)
1122{
Guillermo Julián Moreno65ee03c2016-06-03 14:55:33 -07001123 unsigned long size = (unsigned long)count << PAGE_SHIFT;
Nick Piggindb64fe02008-10-18 20:27:03 -07001124 unsigned long addr = (unsigned long)mem;
1125
1126 BUG_ON(!addr);
1127 BUG_ON(addr < VMALLOC_START);
1128 BUG_ON(addr > VMALLOC_END);
Shawn Lina1c0b1a2016-03-17 14:20:37 -07001129 BUG_ON(!PAGE_ALIGNED(addr));
Nick Piggindb64fe02008-10-18 20:27:03 -07001130
1131 debug_check_no_locks_freed(mem, size);
Nick Piggincd528582009-01-06 14:39:20 -08001132 vmap_debug_free_range(addr, addr+size);
Nick Piggindb64fe02008-10-18 20:27:03 -07001133
1134 if (likely(count <= VMAP_MAX_ALLOC))
1135 vb_free(mem, size);
1136 else
1137 free_unmap_vmap_area_addr(addr);
1138}
1139EXPORT_SYMBOL(vm_unmap_ram);
1140
1141/**
1142 * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
1143 * @pages: an array of pointers to the pages to be mapped
1144 * @count: number of pages
1145 * @node: prefer to allocate data structures on this node
1146 * @prot: memory protection to use. PAGE_KERNEL for regular RAM
Randy Dunlape99c97a2008-10-29 14:01:09 -07001147 *
Gioh Kim36437632014-04-07 15:37:37 -07001148 * If you use this function for less than VMAP_MAX_ALLOC pages, it could be
1149 * faster than vmap so it's good. But if you mix long-life and short-life
1150 * objects with vm_map_ram(), it could consume lots of address space through
1151 * fragmentation (especially on a 32bit machine). You could see failures in
1152 * the end. Please use this function for short-lived objects.
1153 *
Randy Dunlape99c97a2008-10-29 14:01:09 -07001154 * Returns: a pointer to the address that has been mapped, or %NULL on failure
Nick Piggindb64fe02008-10-18 20:27:03 -07001155 */
1156void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
1157{
Guillermo Julián Moreno65ee03c2016-06-03 14:55:33 -07001158 unsigned long size = (unsigned long)count << PAGE_SHIFT;
Nick Piggindb64fe02008-10-18 20:27:03 -07001159 unsigned long addr;
1160 void *mem;
1161
1162 if (likely(count <= VMAP_MAX_ALLOC)) {
1163 mem = vb_alloc(size, GFP_KERNEL);
1164 if (IS_ERR(mem))
1165 return NULL;
1166 addr = (unsigned long)mem;
1167 } else {
1168 struct vmap_area *va;
1169 va = alloc_vmap_area(size, PAGE_SIZE,
1170 VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
1171 if (IS_ERR(va))
1172 return NULL;
1173
1174 addr = va->va_start;
1175 mem = (void *)addr;
1176 }
1177 if (vmap_page_range(addr, addr + size, prot, pages) < 0) {
1178 vm_unmap_ram(mem, count);
1179 return NULL;
1180 }
1181 return mem;
1182}
1183EXPORT_SYMBOL(vm_map_ram);
1184
Joonsoo Kim4341fa42013-04-29 15:07:39 -07001185static struct vm_struct *vmlist __initdata;
Tejun Heof0aa6612009-02-20 16:29:08 +09001186/**
Nicolas Pitrebe9b7332011-08-25 00:24:21 -04001187 * vm_area_add_early - add vmap area early during boot
1188 * @vm: vm_struct to add
1189 *
1190 * This function is used to add fixed kernel vm area to vmlist before
1191 * vmalloc_init() is called. @vm->addr, @vm->size, and @vm->flags
1192 * should contain proper values and the other fields should be zero.
1193 *
1194 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1195 */
1196void __init vm_area_add_early(struct vm_struct *vm)
1197{
1198 struct vm_struct *tmp, **p;
1199
1200 BUG_ON(vmap_initialized);
1201 for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
1202 if (tmp->addr >= vm->addr) {
1203 BUG_ON(tmp->addr < vm->addr + vm->size);
1204 break;
1205 } else
1206 BUG_ON(tmp->addr + tmp->size > vm->addr);
1207 }
1208 vm->next = *p;
1209 *p = vm;
1210}
1211
1212/**
Tejun Heof0aa6612009-02-20 16:29:08 +09001213 * vm_area_register_early - register vmap area early during boot
1214 * @vm: vm_struct to register
Tejun Heoc0c0a292009-02-24 11:57:21 +09001215 * @align: requested alignment
Tejun Heof0aa6612009-02-20 16:29:08 +09001216 *
1217 * This function is used to register kernel vm area before
1218 * vmalloc_init() is called. @vm->size and @vm->flags should contain
1219 * proper values on entry and other fields should be zero. On return,
1220 * vm->addr contains the allocated address.
1221 *
1222 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1223 */
Tejun Heoc0c0a292009-02-24 11:57:21 +09001224void __init vm_area_register_early(struct vm_struct *vm, size_t align)
Tejun Heof0aa6612009-02-20 16:29:08 +09001225{
1226 static size_t vm_init_off __initdata;
Tejun Heoc0c0a292009-02-24 11:57:21 +09001227 unsigned long addr;
Tejun Heof0aa6612009-02-20 16:29:08 +09001228
Tejun Heoc0c0a292009-02-24 11:57:21 +09001229 addr = ALIGN(VMALLOC_START + vm_init_off, align);
1230 vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START;
1231
1232 vm->addr = (void *)addr;
Tejun Heof0aa6612009-02-20 16:29:08 +09001233
Nicolas Pitrebe9b7332011-08-25 00:24:21 -04001234 vm_area_add_early(vm);
Tejun Heof0aa6612009-02-20 16:29:08 +09001235}
1236
Nick Piggindb64fe02008-10-18 20:27:03 -07001237void __init vmalloc_init(void)
1238{
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08001239 struct vmap_area *va;
1240 struct vm_struct *tmp;
Nick Piggindb64fe02008-10-18 20:27:03 -07001241 int i;
1242
1243 for_each_possible_cpu(i) {
1244 struct vmap_block_queue *vbq;
Al Viro32fcfd42013-03-10 20:14:08 -04001245 struct vfree_deferred *p;
Nick Piggindb64fe02008-10-18 20:27:03 -07001246
1247 vbq = &per_cpu(vmap_block_queue, i);
1248 spin_lock_init(&vbq->lock);
1249 INIT_LIST_HEAD(&vbq->free);
Al Viro32fcfd42013-03-10 20:14:08 -04001250 p = &per_cpu(vfree_deferred, i);
1251 init_llist_head(&p->list);
1252 INIT_WORK(&p->wq, free_work);
Nick Piggindb64fe02008-10-18 20:27:03 -07001253 }
Jeremy Fitzhardinge9b463332008-10-28 19:22:34 +11001254
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08001255 /* Import existing vmlist entries. */
1256 for (tmp = vmlist; tmp; tmp = tmp->next) {
Pekka Enberg43ebdac2009-05-25 15:01:35 +03001257 va = kzalloc(sizeof(struct vmap_area), GFP_NOWAIT);
KyongHodbda5912012-05-29 15:06:49 -07001258 va->flags = VM_VM_AREA;
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08001259 va->va_start = (unsigned long)tmp->addr;
1260 va->va_end = va->va_start + tmp->size;
KyongHodbda5912012-05-29 15:06:49 -07001261 va->vm = tmp;
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08001262 __insert_vmap_area(va);
1263 }
Tejun Heoca23e402009-08-14 15:00:52 +09001264
1265 vmap_area_pcpu_hole = VMALLOC_END;
1266
Jeremy Fitzhardinge9b463332008-10-28 19:22:34 +11001267 vmap_initialized = true;
Nick Piggindb64fe02008-10-18 20:27:03 -07001268}
1269
Tejun Heo8fc48982009-02-20 16:29:08 +09001270/**
1271 * map_kernel_range_noflush - map kernel VM area with the specified pages
1272 * @addr: start of the VM area to map
1273 * @size: size of the VM area to map
1274 * @prot: page protection flags to use
1275 * @pages: pages to map
1276 *
1277 * Map PFN_UP(@size) pages at @addr. The VM area @addr and @size
1278 * specify should have been allocated using get_vm_area() and its
1279 * friends.
1280 *
1281 * NOTE:
1282 * This function does NOT do any cache flushing. The caller is
1283 * responsible for calling flush_cache_vmap() on to-be-mapped areas
1284 * before calling this function.
1285 *
1286 * RETURNS:
1287 * The number of pages mapped on success, -errno on failure.
1288 */
1289int map_kernel_range_noflush(unsigned long addr, unsigned long size,
1290 pgprot_t prot, struct page **pages)
1291{
1292 return vmap_page_range_noflush(addr, addr + size, prot, pages);
1293}
1294
1295/**
1296 * unmap_kernel_range_noflush - unmap kernel VM area
1297 * @addr: start of the VM area to unmap
1298 * @size: size of the VM area to unmap
1299 *
1300 * Unmap PFN_UP(@size) pages at @addr. The VM area @addr and @size
1301 * specify should have been allocated using get_vm_area() and its
1302 * friends.
1303 *
1304 * NOTE:
1305 * This function does NOT do any cache flushing. The caller is
1306 * responsible for calling flush_cache_vunmap() on to-be-mapped areas
1307 * before calling this function and flush_tlb_kernel_range() after.
1308 */
1309void unmap_kernel_range_noflush(unsigned long addr, unsigned long size)
1310{
1311 vunmap_page_range(addr, addr + size);
1312}
Huang Ying81e88fd2011-01-12 14:44:55 +08001313EXPORT_SYMBOL_GPL(unmap_kernel_range_noflush);
Tejun Heo8fc48982009-02-20 16:29:08 +09001314
1315/**
1316 * unmap_kernel_range - unmap kernel VM area and flush cache and TLB
1317 * @addr: start of the VM area to unmap
1318 * @size: size of the VM area to unmap
1319 *
1320 * Similar to unmap_kernel_range_noflush() but flushes vcache before
1321 * the unmapping and tlb after.
1322 */
Nick Piggindb64fe02008-10-18 20:27:03 -07001323void unmap_kernel_range(unsigned long addr, unsigned long size)
1324{
1325 unsigned long end = addr + size;
Tejun Heof6fcba72009-02-20 15:38:48 -08001326
1327 flush_cache_vunmap(addr, end);
Nick Piggindb64fe02008-10-18 20:27:03 -07001328 vunmap_page_range(addr, end);
1329 flush_tlb_kernel_range(addr, end);
1330}
Minchan Kim93ef6d62014-06-04 16:11:09 -07001331EXPORT_SYMBOL_GPL(unmap_kernel_range);
Nick Piggindb64fe02008-10-18 20:27:03 -07001332
WANG Chaof6f8ed42014-08-06 16:06:58 -07001333int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page **pages)
Nick Piggindb64fe02008-10-18 20:27:03 -07001334{
1335 unsigned long addr = (unsigned long)area->addr;
Wanpeng Li762216a2013-09-11 14:22:42 -07001336 unsigned long end = addr + get_vm_area_size(area);
Nick Piggindb64fe02008-10-18 20:27:03 -07001337 int err;
1338
WANG Chaof6f8ed42014-08-06 16:06:58 -07001339 err = vmap_page_range(addr, end, prot, pages);
Nick Piggindb64fe02008-10-18 20:27:03 -07001340
WANG Chaof6f8ed42014-08-06 16:06:58 -07001341 return err > 0 ? 0 : err;
Nick Piggindb64fe02008-10-18 20:27:03 -07001342}
1343EXPORT_SYMBOL_GPL(map_vm_area);
1344
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001345static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
Marek Szyprowski5e6cafc2012-04-13 12:32:09 +02001346 unsigned long flags, const void *caller)
Tejun Heocf88c792009-08-14 15:00:52 +09001347{
Joonsoo Kimc69480a2013-04-29 15:07:30 -07001348 spin_lock(&vmap_area_lock);
Tejun Heocf88c792009-08-14 15:00:52 +09001349 vm->flags = flags;
1350 vm->addr = (void *)va->va_start;
1351 vm->size = va->va_end - va->va_start;
1352 vm->caller = caller;
Minchan Kimdb1aeca2012-01-10 15:08:39 -08001353 va->vm = vm;
Tejun Heocf88c792009-08-14 15:00:52 +09001354 va->flags |= VM_VM_AREA;
Joonsoo Kimc69480a2013-04-29 15:07:30 -07001355 spin_unlock(&vmap_area_lock);
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001356}
Tejun Heocf88c792009-08-14 15:00:52 +09001357
Zhang Yanfei20fc02b2013-07-08 15:59:58 -07001358static void clear_vm_uninitialized_flag(struct vm_struct *vm)
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001359{
Joonsoo Kimd4033af2013-04-29 15:07:35 -07001360 /*
Zhang Yanfei20fc02b2013-07-08 15:59:58 -07001361 * Before removing VM_UNINITIALIZED,
Joonsoo Kimd4033af2013-04-29 15:07:35 -07001362 * we should make sure that vm has proper values.
1363 * Pair with smp_rmb() in show_numa_info().
1364 */
1365 smp_wmb();
Zhang Yanfei20fc02b2013-07-08 15:59:58 -07001366 vm->flags &= ~VM_UNINITIALIZED;
Tejun Heocf88c792009-08-14 15:00:52 +09001367}
1368
Nick Piggindb64fe02008-10-18 20:27:03 -07001369static struct vm_struct *__get_vm_area_node(unsigned long size,
David Miller2dca6992009-09-21 12:22:34 -07001370 unsigned long align, unsigned long flags, unsigned long start,
Marek Szyprowski5e6cafc2012-04-13 12:32:09 +02001371 unsigned long end, int node, gfp_t gfp_mask, const void *caller)
Nick Piggindb64fe02008-10-18 20:27:03 -07001372{
Kautuk Consul00065262011-12-19 17:12:04 -08001373 struct vmap_area *va;
Nick Piggindb64fe02008-10-18 20:27:03 -07001374 struct vm_struct *area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Giridhar Pemmasani52fd24c2006-10-28 10:38:34 -07001376 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 size = PAGE_ALIGN(size);
OGAWA Hirofumi31be8302006-11-16 01:19:29 -08001378 if (unlikely(!size))
1379 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
zijun_hu252e5c62016-10-07 16:57:26 -07001381 if (flags & VM_IOREMAP)
1382 align = 1ul << clamp_t(int, get_count_order_long(size),
1383 PAGE_SHIFT, IOREMAP_MAX_ORDER);
1384
Tejun Heocf88c792009-08-14 15:00:52 +09001385 area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 if (unlikely(!area))
1387 return NULL;
1388
Andrey Ryabinin71394fe2015-02-13 14:40:03 -08001389 if (!(flags & VM_NO_GUARD))
1390 size += PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Nick Piggindb64fe02008-10-18 20:27:03 -07001392 va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
1393 if (IS_ERR(va)) {
1394 kfree(area);
1395 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Zhang Yanfeid82b1d82013-07-03 15:04:47 -07001398 setup_vmalloc_vm(area, va, flags, caller);
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 return area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
1402
Christoph Lameter930fc452005-10-29 18:15:41 -07001403struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
1404 unsigned long start, unsigned long end)
1405{
David Rientjes00ef2d22013-02-22 16:35:36 -08001406 return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE,
1407 GFP_KERNEL, __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -07001408}
Rusty Russell5992b6d2007-07-19 01:49:21 -07001409EXPORT_SYMBOL_GPL(__get_vm_area);
Christoph Lameter930fc452005-10-29 18:15:41 -07001410
Benjamin Herrenschmidtc2968612009-02-18 14:48:12 -08001411struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
1412 unsigned long start, unsigned long end,
Marek Szyprowski5e6cafc2012-04-13 12:32:09 +02001413 const void *caller)
Benjamin Herrenschmidtc2968612009-02-18 14:48:12 -08001414{
David Rientjes00ef2d22013-02-22 16:35:36 -08001415 return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE,
1416 GFP_KERNEL, caller);
Benjamin Herrenschmidtc2968612009-02-18 14:48:12 -08001417}
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419/**
Simon Arlott183ff222007-10-20 01:27:18 +02001420 * get_vm_area - reserve a contiguous kernel virtual area
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 * @size: size of the area
1422 * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC
1423 *
1424 * Search an area of @size in the kernel virtual mapping area,
1425 * and reserved it for out purposes. Returns the area descriptor
1426 * on success or %NULL on failure.
1427 */
1428struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
1429{
David Miller2dca6992009-09-21 12:22:34 -07001430 return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
David Rientjes00ef2d22013-02-22 16:35:36 -08001431 NUMA_NO_NODE, GFP_KERNEL,
1432 __builtin_return_address(0));
Christoph Lameter23016962008-04-28 02:12:42 -07001433}
1434
1435struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
Marek Szyprowski5e6cafc2012-04-13 12:32:09 +02001436 const void *caller)
Christoph Lameter23016962008-04-28 02:12:42 -07001437{
David Miller2dca6992009-09-21 12:22:34 -07001438 return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
David Rientjes00ef2d22013-02-22 16:35:36 -08001439 NUMA_NO_NODE, GFP_KERNEL, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440}
1441
Marek Szyprowskie9da6e92012-07-30 09:11:33 +02001442/**
1443 * find_vm_area - find a continuous kernel virtual area
1444 * @addr: base address
1445 *
1446 * Search for the kernel VM area starting at @addr, and return it.
1447 * It is up to the caller to do all required locking to keep the returned
1448 * pointer valid.
1449 */
1450struct vm_struct *find_vm_area(const void *addr)
Nick Piggin83342312006-06-23 02:03:20 -07001451{
Nick Piggindb64fe02008-10-18 20:27:03 -07001452 struct vmap_area *va;
Nick Piggin83342312006-06-23 02:03:20 -07001453
Nick Piggindb64fe02008-10-18 20:27:03 -07001454 va = find_vmap_area((unsigned long)addr);
1455 if (va && va->flags & VM_VM_AREA)
Minchan Kimdb1aeca2012-01-10 15:08:39 -08001456 return va->vm;
Nick Piggin83342312006-06-23 02:03:20 -07001457
Andi Kleen7856dfe2005-05-20 14:27:57 -07001458 return NULL;
Andi Kleen7856dfe2005-05-20 14:27:57 -07001459}
1460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461/**
Simon Arlott183ff222007-10-20 01:27:18 +02001462 * remove_vm_area - find and remove a continuous kernel virtual area
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 * @addr: base address
1464 *
1465 * Search for the kernel VM area starting at @addr, and remove it.
1466 * This function returns the found VM area, but using it is NOT safe
Andi Kleen7856dfe2005-05-20 14:27:57 -07001467 * on SMP machines, except for its size or flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -08001469struct vm_struct *remove_vm_area(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
Nick Piggindb64fe02008-10-18 20:27:03 -07001471 struct vmap_area *va;
1472
1473 va = find_vmap_area((unsigned long)addr);
1474 if (va && va->flags & VM_VM_AREA) {
Minchan Kimdb1aeca2012-01-10 15:08:39 -08001475 struct vm_struct *vm = va->vm;
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001476
Joonsoo Kimc69480a2013-04-29 15:07:30 -07001477 spin_lock(&vmap_area_lock);
1478 va->vm = NULL;
1479 va->flags &= ~VM_VM_AREA;
1480 spin_unlock(&vmap_area_lock);
1481
KAMEZAWA Hiroyukidd32c272009-09-21 17:02:32 -07001482 vmap_debug_free_range(va->va_start, va->va_end);
Andrey Ryabinina5af5aa2015-03-12 16:26:11 -07001483 kasan_free_shadow(vm);
KAMEZAWA Hiroyukidd32c272009-09-21 17:02:32 -07001484 free_unmap_vmap_area(va);
KAMEZAWA Hiroyukidd32c272009-09-21 17:02:32 -07001485
Nick Piggindb64fe02008-10-18 20:27:03 -07001486 return vm;
1487 }
1488 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489}
1490
Christoph Lameterb3bdda02008-02-04 22:28:32 -08001491static void __vunmap(const void *addr, int deallocate_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
1493 struct vm_struct *area;
1494
1495 if (!addr)
1496 return;
1497
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07001498 if (WARN(!PAGE_ALIGNED(addr), "Trying to vfree() bad address (%p)\n",
Dan Carpenterab15d9b2013-07-08 15:59:53 -07001499 addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Chintan Pandyae18d3282018-06-07 17:06:50 -07001502 area = find_vmap_area((unsigned long)addr)->vm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 if (unlikely(!area)) {
Arjan van de Ven4c8573e2008-07-25 19:45:37 -07001504 WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return;
1507 }
1508
Jerome Marchand7511c3e2015-11-20 15:57:02 -08001509 debug_check_no_locks_freed(addr, get_vm_area_size(area));
1510 debug_check_no_obj_freed(addr, get_vm_area_size(area));
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001511
Chintan Pandyae18d3282018-06-07 17:06:50 -07001512 remove_vm_area(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 if (deallocate_pages) {
1514 int i;
1515
1516 for (i = 0; i < area->nr_pages; i++) {
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08001517 struct page *page = area->pages[i];
1518
1519 BUG_ON(!page);
Vladimir Davydov49491482016-07-26 15:24:24 -07001520 __free_pages(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
1522
David Rientjes244d63e2016-01-14 15:19:35 -08001523 kvfree(area->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 }
1525
1526 kfree(area);
1527 return;
1528}
Al Viro32fcfd42013-03-10 20:14:08 -04001529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530/**
1531 * vfree - release memory allocated by vmalloc()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 * @addr: memory base address
1533 *
Simon Arlott183ff222007-10-20 01:27:18 +02001534 * Free the virtually continuous memory area starting at @addr, as
Pekka Enberg80e93ef2005-09-09 13:10:16 -07001535 * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
1536 * NULL, no operation is performed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 *
Al Viro32fcfd42013-03-10 20:14:08 -04001538 * Must not be called in NMI context (strictly speaking, only if we don't
1539 * have CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG, but making the calling
1540 * conventions for vfree() arch-depenedent would be a really bad idea)
Andrew Mortonc9fcee52013-05-07 16:18:18 -07001541 *
1542 * NOTE: assumes that the object at *addr has a size >= sizeof(llist_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -08001544void vfree(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
Al Viro32fcfd42013-03-10 20:14:08 -04001546 BUG_ON(in_nmi());
Catalin Marinas89219d32009-06-11 13:23:19 +01001547
1548 kmemleak_free(addr);
1549
Al Viro32fcfd42013-03-10 20:14:08 -04001550 if (!addr)
1551 return;
1552 if (unlikely(in_interrupt())) {
Christoph Lameter7c8e0182014-06-04 16:07:56 -07001553 struct vfree_deferred *p = this_cpu_ptr(&vfree_deferred);
Oleg Nesterov59d31322013-07-08 16:00:08 -07001554 if (llist_add((struct llist_node *)addr, &p->list))
1555 schedule_work(&p->wq);
Al Viro32fcfd42013-03-10 20:14:08 -04001556 } else
1557 __vunmap(addr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559EXPORT_SYMBOL(vfree);
1560
1561/**
1562 * vunmap - release virtual mapping obtained by vmap()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 * @addr: memory base address
1564 *
1565 * Free the virtually contiguous memory area starting at @addr,
1566 * which was created from the page array passed to vmap().
1567 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -07001568 * Must not be called in interrupt context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -08001570void vunmap(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
1572 BUG_ON(in_interrupt());
Peter Zijlstra34754b62009-02-25 16:04:03 +01001573 might_sleep();
Al Viro32fcfd42013-03-10 20:14:08 -04001574 if (addr)
1575 __vunmap(addr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577EXPORT_SYMBOL(vunmap);
1578
1579/**
1580 * vmap - map an array of pages into virtually contiguous space
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 * @pages: array of page pointers
1582 * @count: number of pages to map
1583 * @flags: vm_area->flags
1584 * @prot: page protection for the mapping
1585 *
1586 * Maps @count pages from @pages into contiguous kernel virtual
1587 * space.
1588 */
1589void *vmap(struct page **pages, unsigned int count,
1590 unsigned long flags, pgprot_t prot)
1591{
1592 struct vm_struct *area;
Guillermo Julián Moreno65ee03c2016-06-03 14:55:33 -07001593 unsigned long size; /* In bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Peter Zijlstra34754b62009-02-25 16:04:03 +01001595 might_sleep();
1596
Jan Beulich44813742009-09-21 17:03:05 -07001597 if (count > totalram_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 return NULL;
1599
Guillermo Julián Moreno65ee03c2016-06-03 14:55:33 -07001600 size = (unsigned long)count << PAGE_SHIFT;
1601 area = get_vm_area_caller(size, flags, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 if (!area)
1603 return NULL;
Christoph Lameter23016962008-04-28 02:12:42 -07001604
WANG Chaof6f8ed42014-08-06 16:06:58 -07001605 if (map_vm_area(area, prot, pages)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 vunmap(area->addr);
1607 return NULL;
1608 }
1609
1610 return area->addr;
1611}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612EXPORT_SYMBOL(vmap);
1613
David Miller2dca6992009-09-21 12:22:34 -07001614static void *__vmalloc_node(unsigned long size, unsigned long align,
1615 gfp_t gfp_mask, pgprot_t prot,
Marek Szyprowski5e6cafc2012-04-13 12:32:09 +02001616 int node, const void *caller);
Adrian Bunke31d9eb2008-02-04 22:29:09 -08001617static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
Wanpeng Li3722e132013-11-12 15:07:29 -08001618 pgprot_t prot, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
1620 struct page **pages;
1621 unsigned int nr_pages, array_size, i;
David Rientjes930f0362014-08-06 16:06:28 -07001622 const gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO;
1623 const gfp_t alloc_mask = gfp_mask | __GFP_NOWARN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Wanpeng Li762216a2013-09-11 14:22:42 -07001625 nr_pages = get_vm_area_size(area) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 array_size = (nr_pages * sizeof(struct page *));
1627
1628 area->nr_pages = nr_pages;
1629 /* Please note that the recursion is strictly bounded. */
Jan Kiszka8757d5f2006-07-14 00:23:56 -07001630 if (array_size > PAGE_SIZE) {
Jan Beulich976d6df2009-12-14 17:58:39 -08001631 pages = __vmalloc_node(array_size, 1, nested_gfp|__GFP_HIGHMEM,
Wanpeng Li3722e132013-11-12 15:07:29 -08001632 PAGE_KERNEL, node, area->caller);
Andrew Morton286e1ea2006-10-17 00:09:57 -07001633 } else {
Jan Beulich976d6df2009-12-14 17:58:39 -08001634 pages = kmalloc_node(array_size, nested_gfp, node);
Andrew Morton286e1ea2006-10-17 00:09:57 -07001635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 area->pages = pages;
1637 if (!area->pages) {
1638 remove_vm_area(area->addr);
1639 kfree(area);
1640 return NULL;
1641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643 for (i = 0; i < area->nr_pages; i++) {
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08001644 struct page *page;
1645
Jianguo Wu4b909512013-11-12 15:07:11 -08001646 if (node == NUMA_NO_NODE)
Michal Hocko7877cdc2016-10-07 17:01:55 -07001647 page = alloc_page(alloc_mask);
Christoph Lameter930fc452005-10-29 18:15:41 -07001648 else
Michal Hocko7877cdc2016-10-07 17:01:55 -07001649 page = alloc_pages_node(node, alloc_mask, 0);
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08001650
1651 if (unlikely(!page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 /* Successfully allocated i pages, free them in __vunmap() */
1653 area->nr_pages = i;
1654 goto fail;
1655 }
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08001656 area->pages[i] = page;
Mel Gormand0164ad2015-11-06 16:28:21 -08001657 if (gfpflags_allow_blocking(gfp_mask))
Eric Dumazet660654f2014-08-06 16:06:25 -07001658 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 }
1660
WANG Chaof6f8ed42014-08-06 16:06:58 -07001661 if (map_vm_area(area, prot, pages))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 goto fail;
1663 return area->addr;
1664
1665fail:
Michal Hocko7877cdc2016-10-07 17:01:55 -07001666 warn_alloc(gfp_mask,
1667 "vmalloc: allocation failure, allocated %ld of %ld bytes",
Dave Hansen22943ab2011-05-24 17:12:18 -07001668 (area->nr_pages*PAGE_SIZE), area->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 vfree(area->addr);
1670 return NULL;
1671}
1672
David Rientjesd0a21262011-01-13 15:46:02 -08001673/**
1674 * __vmalloc_node_range - allocate virtually contiguous memory
1675 * @size: allocation size
1676 * @align: desired alignment
1677 * @start: vm area range start
1678 * @end: vm area range end
1679 * @gfp_mask: flags for the page level allocator
1680 * @prot: protection mask for the allocated pages
Andrey Ryabinincb9e3c22015-02-13 14:40:07 -08001681 * @vm_flags: additional vm area flags (e.g. %VM_NO_GUARD)
David Rientjes00ef2d22013-02-22 16:35:36 -08001682 * @node: node to use for allocation or NUMA_NO_NODE
David Rientjesd0a21262011-01-13 15:46:02 -08001683 * @caller: caller's return address
1684 *
1685 * Allocate enough pages to cover @size from the page level
1686 * allocator with @gfp_mask flags. Map them into contiguous
1687 * kernel virtual space, using a pagetable protection of @prot.
1688 */
1689void *__vmalloc_node_range(unsigned long size, unsigned long align,
1690 unsigned long start, unsigned long end, gfp_t gfp_mask,
Andrey Ryabinincb9e3c22015-02-13 14:40:07 -08001691 pgprot_t prot, unsigned long vm_flags, int node,
1692 const void *caller)
Christoph Lameter930fc452005-10-29 18:15:41 -07001693{
David Rientjesd0a21262011-01-13 15:46:02 -08001694 struct vm_struct *area;
1695 void *addr;
1696 unsigned long real_size = size;
1697
1698 size = PAGE_ALIGN(size);
1699 if (!size || (size >> PAGE_SHIFT) > totalram_pages)
Joe Perchesde7d2b52011-10-31 17:08:48 -07001700 goto fail;
David Rientjesd0a21262011-01-13 15:46:02 -08001701
Andrey Ryabinincb9e3c22015-02-13 14:40:07 -08001702 area = __get_vm_area_node(size, align, VM_ALLOC | VM_UNINITIALIZED |
1703 vm_flags, start, end, node, gfp_mask, caller);
David Rientjesd0a21262011-01-13 15:46:02 -08001704 if (!area)
Joe Perchesde7d2b52011-10-31 17:08:48 -07001705 goto fail;
David Rientjesd0a21262011-01-13 15:46:02 -08001706
Wanpeng Li3722e132013-11-12 15:07:29 -08001707 addr = __vmalloc_area_node(area, gfp_mask, prot, node);
Mel Gorman1368edf2011-12-08 14:34:30 -08001708 if (!addr)
Wanpeng Lib82225f32013-11-12 15:07:33 -08001709 return NULL;
Catalin Marinas89219d32009-06-11 13:23:19 +01001710
1711 /*
Joerg Roedel6c8f40d2019-07-19 20:46:52 +02001712 * First make sure the mappings are removed from all page-tables
1713 * before they are freed.
1714 */
Joerg Roedel8c59bdc2020-03-21 18:22:41 -07001715 vmalloc_sync_unmappings();
Joerg Roedel6c8f40d2019-07-19 20:46:52 +02001716
1717 /*
Zhang Yanfei20fc02b2013-07-08 15:59:58 -07001718 * In this function, newly allocated vm_struct has VM_UNINITIALIZED
1719 * flag. It means that vm_struct is not fully initialized.
Joonsoo Kim4341fa42013-04-29 15:07:39 -07001720 * Now, it is fully initialized, so remove this flag here.
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001721 */
Zhang Yanfei20fc02b2013-07-08 15:59:58 -07001722 clear_vm_uninitialized_flag(area);
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001723
1724 /*
Catalin Marinas7f88f882013-11-12 15:07:45 -08001725 * A ref_count = 2 is needed because vm_struct allocated in
1726 * __get_vm_area_node() contains a reference to the virtual address of
1727 * the vmalloc'ed block.
Catalin Marinas89219d32009-06-11 13:23:19 +01001728 */
Catalin Marinas7f88f882013-11-12 15:07:45 -08001729 kmemleak_alloc(addr, real_size, 2, gfp_mask);
Catalin Marinas89219d32009-06-11 13:23:19 +01001730
1731 return addr;
Joe Perchesde7d2b52011-10-31 17:08:48 -07001732
1733fail:
Michal Hocko7877cdc2016-10-07 17:01:55 -07001734 warn_alloc(gfp_mask,
1735 "vmalloc: allocation failure: %lu bytes", real_size);
Joe Perchesde7d2b52011-10-31 17:08:48 -07001736 return NULL;
Christoph Lameter930fc452005-10-29 18:15:41 -07001737}
1738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739/**
Christoph Lameter930fc452005-10-29 18:15:41 -07001740 * __vmalloc_node - allocate virtually contiguous memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 * @size: allocation size
David Miller2dca6992009-09-21 12:22:34 -07001742 * @align: desired alignment
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 * @gfp_mask: flags for the page level allocator
1744 * @prot: protection mask for the allocated pages
David Rientjes00ef2d22013-02-22 16:35:36 -08001745 * @node: node to use for allocation or NUMA_NO_NODE
Randy Dunlapc85d1942008-05-01 04:34:48 -07001746 * @caller: caller's return address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 *
1748 * Allocate enough pages to cover @size from the page level
1749 * allocator with @gfp_mask flags. Map them into contiguous
1750 * kernel virtual space, using a pagetable protection of @prot.
1751 */
David Miller2dca6992009-09-21 12:22:34 -07001752static void *__vmalloc_node(unsigned long size, unsigned long align,
1753 gfp_t gfp_mask, pgprot_t prot,
Marek Szyprowski5e6cafc2012-04-13 12:32:09 +02001754 int node, const void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
David Rientjesd0a21262011-01-13 15:46:02 -08001756 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
Andrey Ryabinincb9e3c22015-02-13 14:40:07 -08001757 gfp_mask, prot, 0, node, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758}
1759
Christoph Lameter930fc452005-10-29 18:15:41 -07001760void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
1761{
David Rientjes00ef2d22013-02-22 16:35:36 -08001762 return __vmalloc_node(size, 1, gfp_mask, prot, NUMA_NO_NODE,
Christoph Lameter23016962008-04-28 02:12:42 -07001763 __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -07001764}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765EXPORT_SYMBOL(__vmalloc);
1766
Dave Younge1ca7782010-10-26 14:22:06 -07001767static inline void *__vmalloc_node_flags(unsigned long size,
1768 int node, gfp_t flags)
1769{
1770 return __vmalloc_node(size, 1, flags, PAGE_KERNEL,
1771 node, __builtin_return_address(0));
1772}
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774/**
1775 * vmalloc - allocate virtually contiguous memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 * @size: allocation size
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 * Allocate enough pages to cover @size from the page level
1778 * allocator and map them into contiguous kernel virtual space.
1779 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +02001780 * For tight control over page level allocator and protection flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 * use __vmalloc() instead.
1782 */
1783void *vmalloc(unsigned long size)
1784{
David Rientjes00ef2d22013-02-22 16:35:36 -08001785 return __vmalloc_node_flags(size, NUMA_NO_NODE,
1786 GFP_KERNEL | __GFP_HIGHMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788EXPORT_SYMBOL(vmalloc);
1789
Christoph Lameter930fc452005-10-29 18:15:41 -07001790/**
Dave Younge1ca7782010-10-26 14:22:06 -07001791 * vzalloc - allocate virtually contiguous memory with zero fill
1792 * @size: allocation size
1793 * Allocate enough pages to cover @size from the page level
1794 * allocator and map them into contiguous kernel virtual space.
1795 * The memory allocated is set to zero.
1796 *
1797 * For tight control over page level allocator and protection flags
1798 * use __vmalloc() instead.
1799 */
1800void *vzalloc(unsigned long size)
1801{
David Rientjes00ef2d22013-02-22 16:35:36 -08001802 return __vmalloc_node_flags(size, NUMA_NO_NODE,
Dave Younge1ca7782010-10-26 14:22:06 -07001803 GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
1804}
1805EXPORT_SYMBOL(vzalloc);
1806
1807/**
Rolf Eike Beeread04082006-09-27 01:50:13 -07001808 * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
1809 * @size: allocation size
Nick Piggin83342312006-06-23 02:03:20 -07001810 *
Rolf Eike Beeread04082006-09-27 01:50:13 -07001811 * The resulting memory area is zeroed so it can be mapped to userspace
1812 * without leaking data.
Nick Piggin83342312006-06-23 02:03:20 -07001813 */
1814void *vmalloc_user(unsigned long size)
1815{
1816 struct vm_struct *area;
1817 void *ret;
1818
David Miller2dca6992009-09-21 12:22:34 -07001819 ret = __vmalloc_node(size, SHMLBA,
1820 GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
David Rientjes00ef2d22013-02-22 16:35:36 -08001821 PAGE_KERNEL, NUMA_NO_NODE,
1822 __builtin_return_address(0));
Eric Dumazet2b4ac442006-11-10 12:27:48 -08001823 if (ret) {
Nick Piggindb64fe02008-10-18 20:27:03 -07001824 area = find_vm_area(ret);
Eric Dumazet2b4ac442006-11-10 12:27:48 -08001825 area->flags |= VM_USERMAP;
Eric Dumazet2b4ac442006-11-10 12:27:48 -08001826 }
Nick Piggin83342312006-06-23 02:03:20 -07001827 return ret;
1828}
1829EXPORT_SYMBOL(vmalloc_user);
1830
1831/**
Christoph Lameter930fc452005-10-29 18:15:41 -07001832 * vmalloc_node - allocate memory on a specific node
Christoph Lameter930fc452005-10-29 18:15:41 -07001833 * @size: allocation size
Randy Dunlapd44e0782005-11-07 01:01:10 -08001834 * @node: numa node
Christoph Lameter930fc452005-10-29 18:15:41 -07001835 *
1836 * Allocate enough pages to cover @size from the page level
1837 * allocator and map them into contiguous kernel virtual space.
1838 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +02001839 * For tight control over page level allocator and protection flags
Christoph Lameter930fc452005-10-29 18:15:41 -07001840 * use __vmalloc() instead.
1841 */
1842void *vmalloc_node(unsigned long size, int node)
1843{
David Miller2dca6992009-09-21 12:22:34 -07001844 return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
Christoph Lameter23016962008-04-28 02:12:42 -07001845 node, __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -07001846}
1847EXPORT_SYMBOL(vmalloc_node);
1848
Dave Younge1ca7782010-10-26 14:22:06 -07001849/**
1850 * vzalloc_node - allocate memory on a specific node with zero fill
1851 * @size: allocation size
1852 * @node: numa node
1853 *
1854 * Allocate enough pages to cover @size from the page level
1855 * allocator and map them into contiguous kernel virtual space.
1856 * The memory allocated is set to zero.
1857 *
1858 * For tight control over page level allocator and protection flags
1859 * use __vmalloc_node() instead.
1860 */
1861void *vzalloc_node(unsigned long size, int node)
1862{
1863 return __vmalloc_node_flags(size, node,
1864 GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
1865}
1866EXPORT_SYMBOL(vzalloc_node);
1867
Pavel Pisa4dc3b162005-05-01 08:59:25 -07001868#ifndef PAGE_KERNEL_EXEC
1869# define PAGE_KERNEL_EXEC PAGE_KERNEL
1870#endif
1871
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872/**
1873 * vmalloc_exec - allocate virtually contiguous, executable memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 * @size: allocation size
1875 *
1876 * Kernel-internal function to allocate enough pages to cover @size
1877 * the page level allocator and map them into contiguous and
1878 * executable kernel virtual space.
1879 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +02001880 * For tight control over page level allocator and protection flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 * use __vmalloc() instead.
1882 */
1883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884void *vmalloc_exec(unsigned long size)
1885{
David Miller2dca6992009-09-21 12:22:34 -07001886 return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC,
David Rientjes00ef2d22013-02-22 16:35:36 -08001887 NUMA_NO_NODE, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888}
1889
Andi Kleen0d08e0d2007-05-02 19:27:12 +02001890#if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
Benjamin Herrenschmidt7ac674f2007-07-19 01:49:10 -07001891#define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
Andi Kleen0d08e0d2007-05-02 19:27:12 +02001892#elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
Benjamin Herrenschmidt7ac674f2007-07-19 01:49:10 -07001893#define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL
Andi Kleen0d08e0d2007-05-02 19:27:12 +02001894#else
1895#define GFP_VMALLOC32 GFP_KERNEL
1896#endif
1897
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898/**
1899 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 * @size: allocation size
1901 *
1902 * Allocate enough 32bit PA addressable pages to cover @size from the
1903 * page level allocator and map them into contiguous kernel virtual space.
1904 */
1905void *vmalloc_32(unsigned long size)
1906{
David Miller2dca6992009-09-21 12:22:34 -07001907 return __vmalloc_node(size, 1, GFP_VMALLOC32, PAGE_KERNEL,
David Rientjes00ef2d22013-02-22 16:35:36 -08001908 NUMA_NO_NODE, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910EXPORT_SYMBOL(vmalloc_32);
1911
Nick Piggin83342312006-06-23 02:03:20 -07001912/**
Rolf Eike Beeread04082006-09-27 01:50:13 -07001913 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
Nick Piggin83342312006-06-23 02:03:20 -07001914 * @size: allocation size
Rolf Eike Beeread04082006-09-27 01:50:13 -07001915 *
1916 * The resulting memory area is 32bit addressable and zeroed so it can be
1917 * mapped to userspace without leaking data.
Nick Piggin83342312006-06-23 02:03:20 -07001918 */
1919void *vmalloc_32_user(unsigned long size)
1920{
1921 struct vm_struct *area;
1922 void *ret;
1923
David Miller2dca6992009-09-21 12:22:34 -07001924 ret = __vmalloc_node(size, 1, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
David Rientjes00ef2d22013-02-22 16:35:36 -08001925 NUMA_NO_NODE, __builtin_return_address(0));
Eric Dumazet2b4ac442006-11-10 12:27:48 -08001926 if (ret) {
Nick Piggindb64fe02008-10-18 20:27:03 -07001927 area = find_vm_area(ret);
Eric Dumazet2b4ac442006-11-10 12:27:48 -08001928 area->flags |= VM_USERMAP;
Eric Dumazet2b4ac442006-11-10 12:27:48 -08001929 }
Nick Piggin83342312006-06-23 02:03:20 -07001930 return ret;
1931}
1932EXPORT_SYMBOL(vmalloc_32_user);
1933
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07001934/*
1935 * small helper routine , copy contents to buf from addr.
1936 * If the page is not present, fill zero.
1937 */
1938
1939static int aligned_vread(char *buf, char *addr, unsigned long count)
1940{
1941 struct page *p;
1942 int copied = 0;
1943
1944 while (count) {
1945 unsigned long offset, length;
1946
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08001947 offset = offset_in_page(addr);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07001948 length = PAGE_SIZE - offset;
1949 if (length > count)
1950 length = count;
1951 p = vmalloc_to_page(addr);
1952 /*
1953 * To do safe access to this _mapped_ area, we need
1954 * lock. But adding lock here means that we need to add
1955 * overhead of vmalloc()/vfree() calles for this _debug_
1956 * interface, rarely used. Instead of that, we'll use
1957 * kmap() and get small overhead in this access function.
1958 */
1959 if (p) {
1960 /*
1961 * we can expect USER0 is not used (see vread/vwrite's
1962 * function description)
1963 */
Cong Wang9b04c5f2011-11-25 23:14:39 +08001964 void *map = kmap_atomic(p);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07001965 memcpy(buf, map + offset, length);
Cong Wang9b04c5f2011-11-25 23:14:39 +08001966 kunmap_atomic(map);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07001967 } else
1968 memset(buf, 0, length);
1969
1970 addr += length;
1971 buf += length;
1972 copied += length;
1973 count -= length;
1974 }
1975 return copied;
1976}
1977
1978static int aligned_vwrite(char *buf, char *addr, unsigned long count)
1979{
1980 struct page *p;
1981 int copied = 0;
1982
1983 while (count) {
1984 unsigned long offset, length;
1985
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08001986 offset = offset_in_page(addr);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07001987 length = PAGE_SIZE - offset;
1988 if (length > count)
1989 length = count;
1990 p = vmalloc_to_page(addr);
1991 /*
1992 * To do safe access to this _mapped_ area, we need
1993 * lock. But adding lock here means that we need to add
1994 * overhead of vmalloc()/vfree() calles for this _debug_
1995 * interface, rarely used. Instead of that, we'll use
1996 * kmap() and get small overhead in this access function.
1997 */
1998 if (p) {
1999 /*
2000 * we can expect USER0 is not used (see vread/vwrite's
2001 * function description)
2002 */
Cong Wang9b04c5f2011-11-25 23:14:39 +08002003 void *map = kmap_atomic(p);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002004 memcpy(map + offset, buf, length);
Cong Wang9b04c5f2011-11-25 23:14:39 +08002005 kunmap_atomic(map);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002006 }
2007 addr += length;
2008 buf += length;
2009 copied += length;
2010 count -= length;
2011 }
2012 return copied;
2013}
2014
2015/**
2016 * vread() - read vmalloc area in a safe way.
2017 * @buf: buffer for reading data
2018 * @addr: vm address.
2019 * @count: number of bytes to be read.
2020 *
2021 * Returns # of bytes which addr and buf should be increased.
2022 * (same number to @count). Returns 0 if [addr...addr+count) doesn't
2023 * includes any intersect with alive vmalloc area.
2024 *
2025 * This function checks that addr is a valid vmalloc'ed area, and
2026 * copy data from that area to a given buffer. If the given memory range
2027 * of [addr...addr+count) includes some valid address, data is copied to
2028 * proper area of @buf. If there are memory holes, they'll be zero-filled.
2029 * IOREMAP area is treated as memory hole and no copy is done.
2030 *
2031 * If [addr...addr+count) doesn't includes any intersects with alive
Cong Wanga8e52022012-06-23 11:30:16 +08002032 * vm_struct area, returns 0. @buf should be kernel's buffer.
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002033 *
2034 * Note: In usual ops, vread() is never necessary because the caller
2035 * should know vmalloc() area is valid and can use memcpy().
2036 * This is for routines which have to access vmalloc area without
2037 * any informaion, as /dev/kmem.
2038 *
2039 */
2040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041long vread(char *buf, char *addr, unsigned long count)
2042{
Joonsoo Kime81ce852013-04-29 15:07:32 -07002043 struct vmap_area *va;
2044 struct vm_struct *vm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 char *vaddr, *buf_start = buf;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002046 unsigned long buflen = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 unsigned long n;
2048
2049 /* Don't allow overflow */
2050 if ((unsigned long) addr + count < count)
2051 count = -(unsigned long) addr;
2052
Joonsoo Kime81ce852013-04-29 15:07:32 -07002053 spin_lock(&vmap_area_lock);
2054 list_for_each_entry(va, &vmap_area_list, list) {
2055 if (!count)
2056 break;
2057
2058 if (!(va->flags & VM_VM_AREA))
2059 continue;
2060
2061 vm = va->vm;
2062 vaddr = (char *) vm->addr;
Wanpeng Li762216a2013-09-11 14:22:42 -07002063 if (addr >= vaddr + get_vm_area_size(vm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 continue;
2065 while (addr < vaddr) {
2066 if (count == 0)
2067 goto finished;
2068 *buf = '\0';
2069 buf++;
2070 addr++;
2071 count--;
2072 }
Wanpeng Li762216a2013-09-11 14:22:42 -07002073 n = vaddr + get_vm_area_size(vm) - addr;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002074 if (n > count)
2075 n = count;
Joonsoo Kime81ce852013-04-29 15:07:32 -07002076 if (!(vm->flags & VM_IOREMAP))
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002077 aligned_vread(buf, addr, n);
2078 else /* IOREMAP area is treated as memory hole */
2079 memset(buf, 0, n);
2080 buf += n;
2081 addr += n;
2082 count -= n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 }
2084finished:
Joonsoo Kime81ce852013-04-29 15:07:32 -07002085 spin_unlock(&vmap_area_lock);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002086
2087 if (buf == buf_start)
2088 return 0;
2089 /* zero-fill memory holes */
2090 if (buf != buf_start + buflen)
2091 memset(buf, 0, buflen - (buf - buf_start));
2092
2093 return buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094}
2095
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002096/**
2097 * vwrite() - write vmalloc area in a safe way.
2098 * @buf: buffer for source data
2099 * @addr: vm address.
2100 * @count: number of bytes to be read.
2101 *
2102 * Returns # of bytes which addr and buf should be incresed.
2103 * (same number to @count).
2104 * If [addr...addr+count) doesn't includes any intersect with valid
2105 * vmalloc area, returns 0.
2106 *
2107 * This function checks that addr is a valid vmalloc'ed area, and
2108 * copy data from a buffer to the given addr. If specified range of
2109 * [addr...addr+count) includes some valid address, data is copied from
2110 * proper area of @buf. If there are memory holes, no copy to hole.
2111 * IOREMAP area is treated as memory hole and no copy is done.
2112 *
2113 * If [addr...addr+count) doesn't includes any intersects with alive
Cong Wanga8e52022012-06-23 11:30:16 +08002114 * vm_struct area, returns 0. @buf should be kernel's buffer.
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002115 *
2116 * Note: In usual ops, vwrite() is never necessary because the caller
2117 * should know vmalloc() area is valid and can use memcpy().
2118 * This is for routines which have to access vmalloc area without
2119 * any informaion, as /dev/kmem.
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002120 */
2121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122long vwrite(char *buf, char *addr, unsigned long count)
2123{
Joonsoo Kime81ce852013-04-29 15:07:32 -07002124 struct vmap_area *va;
2125 struct vm_struct *vm;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002126 char *vaddr;
2127 unsigned long n, buflen;
2128 int copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
2130 /* Don't allow overflow */
2131 if ((unsigned long) addr + count < count)
2132 count = -(unsigned long) addr;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002133 buflen = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Joonsoo Kime81ce852013-04-29 15:07:32 -07002135 spin_lock(&vmap_area_lock);
2136 list_for_each_entry(va, &vmap_area_list, list) {
2137 if (!count)
2138 break;
2139
2140 if (!(va->flags & VM_VM_AREA))
2141 continue;
2142
2143 vm = va->vm;
2144 vaddr = (char *) vm->addr;
Wanpeng Li762216a2013-09-11 14:22:42 -07002145 if (addr >= vaddr + get_vm_area_size(vm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 continue;
2147 while (addr < vaddr) {
2148 if (count == 0)
2149 goto finished;
2150 buf++;
2151 addr++;
2152 count--;
2153 }
Wanpeng Li762216a2013-09-11 14:22:42 -07002154 n = vaddr + get_vm_area_size(vm) - addr;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002155 if (n > count)
2156 n = count;
Joonsoo Kime81ce852013-04-29 15:07:32 -07002157 if (!(vm->flags & VM_IOREMAP)) {
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002158 aligned_vwrite(buf, addr, n);
2159 copied++;
2160 }
2161 buf += n;
2162 addr += n;
2163 count -= n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 }
2165finished:
Joonsoo Kime81ce852013-04-29 15:07:32 -07002166 spin_unlock(&vmap_area_lock);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002167 if (!copied)
2168 return 0;
2169 return buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170}
Nick Piggin83342312006-06-23 02:03:20 -07002171
2172/**
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002173 * remap_vmalloc_range_partial - map vmalloc pages to userspace
2174 * @vma: vma to cover
2175 * @uaddr: target user address to start at
2176 * @kaddr: virtual address of vmalloc kernel memory
Jann Hornf8e84d72020-04-20 18:14:11 -07002177 * @pgoff: offset from @kaddr to start at
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002178 * @size: size of map area
2179 *
2180 * Returns: 0 for success, -Exxx on failure
2181 *
2182 * This function checks that @kaddr is a valid vmalloc'ed area,
2183 * and that it is big enough to cover the range starting at
2184 * @uaddr in @vma. Will return failure if that criteria isn't
2185 * met.
2186 *
2187 * Similar to remap_pfn_range() (see mm/memory.c)
2188 */
2189int remap_vmalloc_range_partial(struct vm_area_struct *vma, unsigned long uaddr,
Jann Hornf8e84d72020-04-20 18:14:11 -07002190 void *kaddr, unsigned long pgoff,
2191 unsigned long size)
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002192{
2193 struct vm_struct *area;
Jann Hornf8e84d72020-04-20 18:14:11 -07002194 unsigned long off;
2195 unsigned long end_index;
2196
2197 if (check_shl_overflow(pgoff, PAGE_SHIFT, &off))
2198 return -EINVAL;
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002199
2200 size = PAGE_ALIGN(size);
2201
2202 if (!PAGE_ALIGNED(uaddr) || !PAGE_ALIGNED(kaddr))
2203 return -EINVAL;
2204
2205 area = find_vm_area(kaddr);
2206 if (!area)
2207 return -EINVAL;
2208
2209 if (!(area->flags & VM_USERMAP))
2210 return -EINVAL;
2211
Jann Hornf8e84d72020-04-20 18:14:11 -07002212 if (check_add_overflow(size, off, &end_index) ||
2213 end_index > get_vm_area_size(area))
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002214 return -EINVAL;
Jann Hornf8e84d72020-04-20 18:14:11 -07002215 kaddr += off;
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002216
2217 do {
2218 struct page *page = vmalloc_to_page(kaddr);
2219 int ret;
2220
2221 ret = vm_insert_page(vma, uaddr, page);
2222 if (ret)
2223 return ret;
2224
2225 uaddr += PAGE_SIZE;
2226 kaddr += PAGE_SIZE;
2227 size -= PAGE_SIZE;
2228 } while (size > 0);
2229
2230 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
2231
2232 return 0;
2233}
2234EXPORT_SYMBOL(remap_vmalloc_range_partial);
2235
2236/**
Nick Piggin83342312006-06-23 02:03:20 -07002237 * remap_vmalloc_range - map vmalloc pages to userspace
Nick Piggin83342312006-06-23 02:03:20 -07002238 * @vma: vma to cover (map full range of vma)
2239 * @addr: vmalloc memory
2240 * @pgoff: number of pages into addr before first page to map
Randy Dunlap76824862008-03-19 17:00:40 -07002241 *
2242 * Returns: 0 for success, -Exxx on failure
Nick Piggin83342312006-06-23 02:03:20 -07002243 *
2244 * This function checks that addr is a valid vmalloc'ed area, and
2245 * that it is big enough to cover the vma. Will return failure if
2246 * that criteria isn't met.
2247 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002248 * Similar to remap_pfn_range() (see mm/memory.c)
Nick Piggin83342312006-06-23 02:03:20 -07002249 */
2250int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
2251 unsigned long pgoff)
2252{
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002253 return remap_vmalloc_range_partial(vma, vma->vm_start,
Jann Hornf8e84d72020-04-20 18:14:11 -07002254 addr, pgoff,
HATAYAMA Daisukee69e9d4a2013-07-03 15:02:18 -07002255 vma->vm_end - vma->vm_start);
Nick Piggin83342312006-06-23 02:03:20 -07002256}
2257EXPORT_SYMBOL(remap_vmalloc_range);
2258
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -07002259/*
Joerg Roedel8c59bdc2020-03-21 18:22:41 -07002260 * Implement stubs for vmalloc_sync_[un]mappings () if the architecture chose
2261 * not to have one.
Joerg Roedel6c8f40d2019-07-19 20:46:52 +02002262 *
2263 * The purpose of this function is to make sure the vmalloc area
2264 * mappings are identical in all page-tables in the system.
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -07002265 */
Joerg Roedel8c59bdc2020-03-21 18:22:41 -07002266void __weak vmalloc_sync_mappings(void)
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -07002267{
2268}
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002269
Joerg Roedel8c59bdc2020-03-21 18:22:41 -07002270void __weak vmalloc_sync_unmappings(void)
2271{
2272}
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002273
Martin Schwidefsky2f569af2008-02-08 04:22:04 -08002274static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002275{
David Vrabelcd129092011-09-29 16:53:32 +01002276 pte_t ***p = data;
2277
2278 if (p) {
2279 *(*p) = pte;
2280 (*p)++;
2281 }
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002282 return 0;
2283}
2284
2285/**
2286 * alloc_vm_area - allocate a range of kernel address space
2287 * @size: size of the area
David Vrabelcd129092011-09-29 16:53:32 +01002288 * @ptes: returns the PTEs for the address space
Randy Dunlap76824862008-03-19 17:00:40 -07002289 *
2290 * Returns: NULL on failure, vm_struct on success
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002291 *
2292 * This function reserves a range of kernel address space, and
2293 * allocates pagetables to map that range. No actual mappings
David Vrabelcd129092011-09-29 16:53:32 +01002294 * are created.
2295 *
2296 * If @ptes is non-NULL, pointers to the PTEs (in init_mm)
2297 * allocated for the VM area are returned.
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002298 */
David Vrabelcd129092011-09-29 16:53:32 +01002299struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002300{
2301 struct vm_struct *area;
2302
Christoph Lameter23016962008-04-28 02:12:42 -07002303 area = get_vm_area_caller(size, VM_IOREMAP,
2304 __builtin_return_address(0));
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002305 if (area == NULL)
2306 return NULL;
2307
2308 /*
2309 * This ensures that page tables are constructed for this region
2310 * of kernel virtual address space and mapped into init_mm.
2311 */
2312 if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
David Vrabelcd129092011-09-29 16:53:32 +01002313 size, f, ptes ? &ptes : NULL)) {
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002314 free_vm_area(area);
2315 return NULL;
2316 }
2317
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002318 return area;
2319}
2320EXPORT_SYMBOL_GPL(alloc_vm_area);
2321
2322void free_vm_area(struct vm_struct *area)
2323{
2324 struct vm_struct *ret;
2325 ret = remove_vm_area(area->addr);
2326 BUG_ON(ret != area);
2327 kfree(area);
2328}
2329EXPORT_SYMBOL_GPL(free_vm_area);
Christoph Lametera10aa572008-04-28 02:12:40 -07002330
Tejun Heo4f8b02b2010-09-03 18:22:47 +02002331#ifdef CONFIG_SMP
Tejun Heoca23e402009-08-14 15:00:52 +09002332static struct vmap_area *node_to_va(struct rb_node *n)
2333{
2334 return n ? rb_entry(n, struct vmap_area, rb_node) : NULL;
2335}
2336
2337/**
2338 * pvm_find_next_prev - find the next and prev vmap_area surrounding @end
2339 * @end: target address
2340 * @pnext: out arg for the next vmap_area
2341 * @pprev: out arg for the previous vmap_area
2342 *
2343 * Returns: %true if either or both of next and prev are found,
2344 * %false if no vmap_area exists
2345 *
2346 * Find vmap_areas end addresses of which enclose @end. ie. if not
2347 * NULL, *pnext->va_end > @end and *pprev->va_end <= @end.
2348 */
2349static bool pvm_find_next_prev(unsigned long end,
2350 struct vmap_area **pnext,
2351 struct vmap_area **pprev)
2352{
2353 struct rb_node *n = vmap_area_root.rb_node;
2354 struct vmap_area *va = NULL;
2355
2356 while (n) {
2357 va = rb_entry(n, struct vmap_area, rb_node);
2358 if (end < va->va_end)
2359 n = n->rb_left;
2360 else if (end > va->va_end)
2361 n = n->rb_right;
2362 else
2363 break;
2364 }
2365
2366 if (!va)
2367 return false;
2368
2369 if (va->va_end > end) {
2370 *pnext = va;
2371 *pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
2372 } else {
2373 *pprev = va;
2374 *pnext = node_to_va(rb_next(&(*pprev)->rb_node));
2375 }
2376 return true;
2377}
2378
2379/**
2380 * pvm_determine_end - find the highest aligned address between two vmap_areas
2381 * @pnext: in/out arg for the next vmap_area
2382 * @pprev: in/out arg for the previous vmap_area
2383 * @align: alignment
2384 *
2385 * Returns: determined end address
2386 *
2387 * Find the highest aligned address between *@pnext and *@pprev below
2388 * VMALLOC_END. *@pnext and *@pprev are adjusted so that the aligned
2389 * down address is between the end addresses of the two vmap_areas.
2390 *
2391 * Please note that the address returned by this function may fall
2392 * inside *@pnext vmap_area. The caller is responsible for checking
2393 * that.
2394 */
2395static unsigned long pvm_determine_end(struct vmap_area **pnext,
2396 struct vmap_area **pprev,
2397 unsigned long align)
2398{
2399 const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
2400 unsigned long addr;
2401
2402 if (*pnext)
2403 addr = min((*pnext)->va_start & ~(align - 1), vmalloc_end);
2404 else
2405 addr = vmalloc_end;
2406
2407 while (*pprev && (*pprev)->va_end > addr) {
2408 *pnext = *pprev;
2409 *pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
2410 }
2411
2412 return addr;
2413}
2414
2415/**
2416 * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator
2417 * @offsets: array containing offset of each area
2418 * @sizes: array containing size of each area
2419 * @nr_vms: the number of areas to allocate
2420 * @align: alignment, all entries in @offsets and @sizes must be aligned to this
Tejun Heoca23e402009-08-14 15:00:52 +09002421 *
2422 * Returns: kmalloc'd vm_struct pointer array pointing to allocated
2423 * vm_structs on success, %NULL on failure
2424 *
2425 * Percpu allocator wants to use congruent vm areas so that it can
2426 * maintain the offsets among percpu areas. This function allocates
David Rientjesec3f64f2011-01-13 15:46:01 -08002427 * congruent vmalloc areas for it with GFP_KERNEL. These areas tend to
2428 * be scattered pretty far, distance between two areas easily going up
2429 * to gigabytes. To avoid interacting with regular vmallocs, these
2430 * areas are allocated from top.
Tejun Heoca23e402009-08-14 15:00:52 +09002431 *
2432 * Despite its complicated look, this allocator is rather simple. It
2433 * does everything top-down and scans areas from the end looking for
2434 * matching slot. While scanning, if any of the areas overlaps with
2435 * existing vmap_area, the base address is pulled down to fit the
2436 * area. Scanning is repeated till all the areas fit and then all
2437 * necessary data structres are inserted and the result is returned.
2438 */
2439struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
2440 const size_t *sizes, int nr_vms,
David Rientjesec3f64f2011-01-13 15:46:01 -08002441 size_t align)
Tejun Heoca23e402009-08-14 15:00:52 +09002442{
2443 const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align);
2444 const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
2445 struct vmap_area **vas, *prev, *next;
2446 struct vm_struct **vms;
2447 int area, area2, last_area, term_area;
2448 unsigned long base, start, end, last_end;
2449 bool purged = false;
2450
Tejun Heoca23e402009-08-14 15:00:52 +09002451 /* verify parameters and allocate data structures */
Alexander Kuleshov891c49a2015-11-05 18:46:51 -08002452 BUG_ON(offset_in_page(align) || !is_power_of_2(align));
Tejun Heoca23e402009-08-14 15:00:52 +09002453 for (last_area = 0, area = 0; area < nr_vms; area++) {
2454 start = offsets[area];
2455 end = start + sizes[area];
2456
2457 /* is everything aligned properly? */
2458 BUG_ON(!IS_ALIGNED(offsets[area], align));
2459 BUG_ON(!IS_ALIGNED(sizes[area], align));
2460
2461 /* detect the area with the highest address */
2462 if (start > offsets[last_area])
2463 last_area = area;
2464
2465 for (area2 = 0; area2 < nr_vms; area2++) {
2466 unsigned long start2 = offsets[area2];
2467 unsigned long end2 = start2 + sizes[area2];
2468
2469 if (area2 == area)
2470 continue;
2471
2472 BUG_ON(start2 >= start && start2 < end);
2473 BUG_ON(end2 <= end && end2 > start);
2474 }
2475 }
2476 last_end = offsets[last_area] + sizes[last_area];
2477
2478 if (vmalloc_end - vmalloc_start < last_end) {
2479 WARN_ON(true);
2480 return NULL;
2481 }
2482
Thomas Meyer4d67d862012-05-29 15:06:21 -07002483 vms = kcalloc(nr_vms, sizeof(vms[0]), GFP_KERNEL);
2484 vas = kcalloc(nr_vms, sizeof(vas[0]), GFP_KERNEL);
Tejun Heoca23e402009-08-14 15:00:52 +09002485 if (!vas || !vms)
Kautuk Consulf1db7af2012-01-12 17:20:08 -08002486 goto err_free2;
Tejun Heoca23e402009-08-14 15:00:52 +09002487
2488 for (area = 0; area < nr_vms; area++) {
David Rientjesec3f64f2011-01-13 15:46:01 -08002489 vas[area] = kzalloc(sizeof(struct vmap_area), GFP_KERNEL);
2490 vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL);
Tejun Heoca23e402009-08-14 15:00:52 +09002491 if (!vas[area] || !vms[area])
2492 goto err_free;
2493 }
2494retry:
2495 spin_lock(&vmap_area_lock);
2496
2497 /* start scanning - we scan from the top, begin with the last area */
2498 area = term_area = last_area;
2499 start = offsets[area];
2500 end = start + sizes[area];
2501
2502 if (!pvm_find_next_prev(vmap_area_pcpu_hole, &next, &prev)) {
2503 base = vmalloc_end - last_end;
2504 goto found;
2505 }
2506 base = pvm_determine_end(&next, &prev, align) - end;
2507
2508 while (true) {
2509 BUG_ON(next && next->va_end <= base + end);
2510 BUG_ON(prev && prev->va_end > base + end);
2511
2512 /*
2513 * base might have underflowed, add last_end before
2514 * comparing.
2515 */
2516 if (base + last_end < vmalloc_start + last_end) {
2517 spin_unlock(&vmap_area_lock);
2518 if (!purged) {
2519 purge_vmap_area_lazy();
2520 purged = true;
2521 goto retry;
2522 }
2523 goto err_free;
2524 }
2525
2526 /*
2527 * If next overlaps, move base downwards so that it's
2528 * right below next and then recheck.
2529 */
2530 if (next && next->va_start < base + end) {
2531 base = pvm_determine_end(&next, &prev, align) - end;
2532 term_area = area;
2533 continue;
2534 }
2535
2536 /*
2537 * If prev overlaps, shift down next and prev and move
2538 * base so that it's right below new next and then
2539 * recheck.
2540 */
2541 if (prev && prev->va_end > base + start) {
2542 next = prev;
2543 prev = node_to_va(rb_prev(&next->rb_node));
2544 base = pvm_determine_end(&next, &prev, align) - end;
2545 term_area = area;
2546 continue;
2547 }
2548
2549 /*
2550 * This area fits, move on to the previous one. If
2551 * the previous one is the terminal one, we're done.
2552 */
2553 area = (area + nr_vms - 1) % nr_vms;
2554 if (area == term_area)
2555 break;
2556 start = offsets[area];
2557 end = start + sizes[area];
2558 pvm_find_next_prev(base + end, &next, &prev);
2559 }
2560found:
2561 /* we've found a fitting base, insert all va's */
2562 for (area = 0; area < nr_vms; area++) {
2563 struct vmap_area *va = vas[area];
2564
2565 va->va_start = base + offsets[area];
2566 va->va_end = va->va_start + sizes[area];
2567 __insert_vmap_area(va);
2568 }
2569
2570 vmap_area_pcpu_hole = base + offsets[last_area];
2571
2572 spin_unlock(&vmap_area_lock);
2573
2574 /* insert all vm's */
2575 for (area = 0; area < nr_vms; area++)
Zhang Yanfei3645cb42013-07-03 15:04:48 -07002576 setup_vmalloc_vm(vms[area], vas[area], VM_ALLOC,
2577 pcpu_get_vm_areas);
Tejun Heoca23e402009-08-14 15:00:52 +09002578
2579 kfree(vas);
2580 return vms;
2581
2582err_free:
2583 for (area = 0; area < nr_vms; area++) {
Kautuk Consulf1db7af2012-01-12 17:20:08 -08002584 kfree(vas[area]);
2585 kfree(vms[area]);
Tejun Heoca23e402009-08-14 15:00:52 +09002586 }
Kautuk Consulf1db7af2012-01-12 17:20:08 -08002587err_free2:
Tejun Heoca23e402009-08-14 15:00:52 +09002588 kfree(vas);
2589 kfree(vms);
2590 return NULL;
2591}
2592
2593/**
2594 * pcpu_free_vm_areas - free vmalloc areas for percpu allocator
2595 * @vms: vm_struct pointer array returned by pcpu_get_vm_areas()
2596 * @nr_vms: the number of allocated areas
2597 *
2598 * Free vm_structs and the array allocated by pcpu_get_vm_areas().
2599 */
2600void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
2601{
2602 int i;
2603
2604 for (i = 0; i < nr_vms; i++)
2605 free_vm_area(vms[i]);
2606 kfree(vms);
2607}
Tejun Heo4f8b02b2010-09-03 18:22:47 +02002608#endif /* CONFIG_SMP */
Christoph Lametera10aa572008-04-28 02:12:40 -07002609
2610#ifdef CONFIG_PROC_FS
2611static void *s_start(struct seq_file *m, loff_t *pos)
Joonsoo Kimd4033af2013-04-29 15:07:35 -07002612 __acquires(&vmap_area_lock)
Christoph Lametera10aa572008-04-28 02:12:40 -07002613{
2614 loff_t n = *pos;
Joonsoo Kimd4033af2013-04-29 15:07:35 -07002615 struct vmap_area *va;
Christoph Lametera10aa572008-04-28 02:12:40 -07002616
Joonsoo Kimd4033af2013-04-29 15:07:35 -07002617 spin_lock(&vmap_area_lock);
Geliang Tang6219c2a2016-01-14 15:19:08 -08002618 va = list_first_entry(&vmap_area_list, typeof(*va), list);
Joonsoo Kimd4033af2013-04-29 15:07:35 -07002619 while (n > 0 && &va->list != &vmap_area_list) {
Christoph Lametera10aa572008-04-28 02:12:40 -07002620 n--;
Geliang Tang6219c2a2016-01-14 15:19:08 -08002621 va = list_next_entry(va, list);
Christoph Lametera10aa572008-04-28 02:12:40 -07002622 }
Joonsoo Kimd4033af2013-04-29 15:07:35 -07002623 if (!n && &va->list != &vmap_area_list)
2624 return va;
Christoph Lametera10aa572008-04-28 02:12:40 -07002625
2626 return NULL;
2627
2628}
2629
2630static void *s_next(struct seq_file *m, void *p, loff_t *pos)
2631{
Joonsoo Kimd4033af2013-04-29 15:07:35 -07002632 struct vmap_area *va = p, *next;
Christoph Lametera10aa572008-04-28 02:12:40 -07002633
2634 ++*pos;
Geliang Tang6219c2a2016-01-14 15:19:08 -08002635 next = list_next_entry(va, list);
Joonsoo Kimd4033af2013-04-29 15:07:35 -07002636 if (&next->list != &vmap_area_list)
2637 return next;
2638
2639 return NULL;
Christoph Lametera10aa572008-04-28 02:12:40 -07002640}
2641
2642static void s_stop(struct seq_file *m, void *p)
Joonsoo Kimd4033af2013-04-29 15:07:35 -07002643 __releases(&vmap_area_lock)
Christoph Lametera10aa572008-04-28 02:12:40 -07002644{
Joonsoo Kimd4033af2013-04-29 15:07:35 -07002645 spin_unlock(&vmap_area_lock);
Christoph Lametera10aa572008-04-28 02:12:40 -07002646}
2647
Eric Dumazeta47a1262008-07-23 21:27:38 -07002648static void show_numa_info(struct seq_file *m, struct vm_struct *v)
2649{
Kirill A. Shutemove5adfff2012-12-11 16:00:29 -08002650 if (IS_ENABLED(CONFIG_NUMA)) {
Eric Dumazeta47a1262008-07-23 21:27:38 -07002651 unsigned int nr, *counters = m->private;
2652
2653 if (!counters)
2654 return;
2655
Wanpeng Liaf123462013-11-12 15:07:32 -08002656 if (v->flags & VM_UNINITIALIZED)
2657 return;
Dmitry Vyukov7e5b5282014-12-12 16:56:30 -08002658 /* Pair with smp_wmb() in clear_vm_uninitialized_flag() */
2659 smp_rmb();
Wanpeng Liaf123462013-11-12 15:07:32 -08002660
Eric Dumazeta47a1262008-07-23 21:27:38 -07002661 memset(counters, 0, nr_node_ids * sizeof(unsigned int));
2662
2663 for (nr = 0; nr < v->nr_pages; nr++)
2664 counters[page_to_nid(v->pages[nr])]++;
2665
2666 for_each_node_state(nr, N_HIGH_MEMORY)
2667 if (counters[nr])
2668 seq_printf(m, " N%u=%u", nr, counters[nr]);
2669 }
2670}
2671
Christoph Lametera10aa572008-04-28 02:12:40 -07002672static int s_show(struct seq_file *m, void *p)
2673{
Joonsoo Kimd4033af2013-04-29 15:07:35 -07002674 struct vmap_area *va = p;
2675 struct vm_struct *v;
2676
Wanpeng Lic2ce8c12013-11-12 15:07:31 -08002677 /*
2678 * s_show can encounter race with remove_vm_area, !VM_VM_AREA on
2679 * behalf of vmap area is being tear down or vm_map_ram allocation.
2680 */
2681 if (!(va->flags & VM_VM_AREA))
Joonsoo Kimd4033af2013-04-29 15:07:35 -07002682 return 0;
2683
Joonsoo Kimd4033af2013-04-29 15:07:35 -07002684 v = va->vm;
Christoph Lametera10aa572008-04-28 02:12:40 -07002685
Kees Cook45ec1692012-10-08 16:34:09 -07002686 seq_printf(m, "0x%pK-0x%pK %7ld",
Christoph Lametera10aa572008-04-28 02:12:40 -07002687 v->addr, v->addr + v->size, v->size);
2688
Joe Perches62c70bc2011-01-13 15:45:52 -08002689 if (v->caller)
2690 seq_printf(m, " %pS", v->caller);
Christoph Lameter23016962008-04-28 02:12:42 -07002691
Christoph Lametera10aa572008-04-28 02:12:40 -07002692 if (v->nr_pages)
2693 seq_printf(m, " pages=%d", v->nr_pages);
2694
2695 if (v->phys_addr)
Kenji Kaneshigeffa71f32010-06-18 12:22:40 +09002696 seq_printf(m, " phys=%llx", (unsigned long long)v->phys_addr);
Christoph Lametera10aa572008-04-28 02:12:40 -07002697
2698 if (v->flags & VM_IOREMAP)
Fabian Frederickf4527c92014-06-04 16:08:09 -07002699 seq_puts(m, " ioremap");
Christoph Lametera10aa572008-04-28 02:12:40 -07002700
2701 if (v->flags & VM_ALLOC)
Fabian Frederickf4527c92014-06-04 16:08:09 -07002702 seq_puts(m, " vmalloc");
Christoph Lametera10aa572008-04-28 02:12:40 -07002703
2704 if (v->flags & VM_MAP)
Fabian Frederickf4527c92014-06-04 16:08:09 -07002705 seq_puts(m, " vmap");
Christoph Lametera10aa572008-04-28 02:12:40 -07002706
2707 if (v->flags & VM_USERMAP)
Fabian Frederickf4527c92014-06-04 16:08:09 -07002708 seq_puts(m, " user");
Christoph Lametera10aa572008-04-28 02:12:40 -07002709
David Rientjes244d63e2016-01-14 15:19:35 -08002710 if (is_vmalloc_addr(v->pages))
Fabian Frederickf4527c92014-06-04 16:08:09 -07002711 seq_puts(m, " vpages");
Christoph Lametera10aa572008-04-28 02:12:40 -07002712
Eric Dumazeta47a1262008-07-23 21:27:38 -07002713 show_numa_info(m, v);
Christoph Lametera10aa572008-04-28 02:12:40 -07002714 seq_putc(m, '\n');
2715 return 0;
2716}
2717
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04002718static const struct seq_operations vmalloc_op = {
Christoph Lametera10aa572008-04-28 02:12:40 -07002719 .start = s_start,
2720 .next = s_next,
2721 .stop = s_stop,
2722 .show = s_show,
2723};
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04002724
2725static int vmalloc_open(struct inode *inode, struct file *file)
2726{
Rob Jones703394c2014-10-09 15:28:01 -07002727 if (IS_ENABLED(CONFIG_NUMA))
2728 return seq_open_private(file, &vmalloc_op,
2729 nr_node_ids * sizeof(unsigned int));
2730 else
2731 return seq_open(file, &vmalloc_op);
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04002732}
2733
2734static const struct file_operations proc_vmalloc_operations = {
2735 .open = vmalloc_open,
2736 .read = seq_read,
2737 .llseek = seq_lseek,
2738 .release = seq_release_private,
2739};
2740
2741static int __init proc_vmalloc_init(void)
2742{
2743 proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
2744 return 0;
2745}
2746module_init(proc_vmalloc_init);
Joonsoo Kimdb3808c2013-04-29 15:07:28 -07002747
Christoph Lametera10aa572008-04-28 02:12:40 -07002748#endif
2749