vmalloc.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791
  1. /*
  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
  8. * Numa awareness, Christoph Lameter, SGI, June 2005
  9. */
  10. #include <linux/vmalloc.h>
  11. #include <linux/mm.h>
  12. #include <linux/module.h>
  13. #include <linux/highmem.h>
  14. #include <linux/slab.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/proc_fs.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/debugobjects.h>
  20. #include <linux/kallsyms.h>
  21. #include <linux/list.h>
  22. #include <linux/rbtree.h>
  23. #include <linux/radix-tree.h>
  24. #include <linux/rcupdate.h>
  25. #include <asm/atomic.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/tlbflush.h>
  28. /*** Page table manipulation functions ***/
  29. static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end)
  30. {
  31. pte_t *pte;
  32. pte = pte_offset_kernel(pmd, addr);
  33. do {
  34. pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte);
  35. WARN_ON(!pte_none(ptent) && !pte_present(ptent));
  36. } while (pte++, addr += PAGE_SIZE, addr != end);
  37. }
  38. static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end)
  39. {
  40. pmd_t *pmd;
  41. unsigned long next;
  42. pmd = pmd_offset(pud, addr);
  43. do {
  44. next = pmd_addr_end(addr, end);
  45. if (pmd_none_or_clear_bad(pmd))
  46. continue;
  47. vunmap_pte_range(pmd, addr, next);
  48. } while (pmd++, addr = next, addr != end);
  49. }
  50. static void vunmap_pud_range(pgd_t *pgd, unsigned long addr, unsigned long end)
  51. {
  52. pud_t *pud;
  53. unsigned long next;
  54. pud = pud_offset(pgd, addr);
  55. do {
  56. next = pud_addr_end(addr, end);
  57. if (pud_none_or_clear_bad(pud))
  58. continue;
  59. vunmap_pmd_range(pud, addr, next);
  60. } while (pud++, addr = next, addr != end);
  61. }
  62. static void vunmap_page_range(unsigned long addr, unsigned long end)
  63. {
  64. pgd_t *pgd;
  65. unsigned long next;
  66. BUG_ON(addr >= end);
  67. pgd = pgd_offset_k(addr);
  68. do {
  69. next = pgd_addr_end(addr, end);
  70. if (pgd_none_or_clear_bad(pgd))
  71. continue;
  72. vunmap_pud_range(pgd, addr, next);
  73. } while (pgd++, addr = next, addr != end);
  74. }
  75. static int vmap_pte_range(pmd_t *pmd, unsigned long addr,
  76. unsigned long end, pgprot_t prot, struct page **pages, int *nr)
  77. {
  78. pte_t *pte;
  79. /*
  80. * nr is a running index into the array which helps higher level
  81. * callers keep track of where we're up to.
  82. */
  83. pte = pte_alloc_kernel(pmd, addr);
  84. if (!pte)
  85. return -ENOMEM;
  86. do {
  87. struct page *page = pages[*nr];
  88. if (WARN_ON(!pte_none(*pte)))
  89. return -EBUSY;
  90. if (WARN_ON(!page))
  91. return -ENOMEM;
  92. set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
  93. (*nr)++;
  94. } while (pte++, addr += PAGE_SIZE, addr != end);
  95. return 0;
  96. }
  97. static int vmap_pmd_range(pud_t *pud, unsigned long addr,
  98. unsigned long end, pgprot_t prot, struct page **pages, int *nr)
  99. {
  100. pmd_t *pmd;
  101. unsigned long next;
  102. pmd = pmd_alloc(&init_mm, pud, addr);
  103. if (!pmd)
  104. return -ENOMEM;
  105. do {
  106. next = pmd_addr_end(addr, end);
  107. if (vmap_pte_range(pmd, addr, next, prot, pages, nr))
  108. return -ENOMEM;
  109. } while (pmd++, addr = next, addr != end);
  110. return 0;
  111. }
  112. static int vmap_pud_range(pgd_t *pgd, unsigned long addr,
  113. unsigned long end, pgprot_t prot, struct page **pages, int *nr)
  114. {
  115. pud_t *pud;
  116. unsigned long next;
  117. pud = pud_alloc(&init_mm, pgd, addr);
  118. if (!pud)
  119. return -ENOMEM;
  120. do {
  121. next = pud_addr_end(addr, end);
  122. if (vmap_pmd_range(pud, addr, next, prot, pages, nr))
  123. return -ENOMEM;
  124. } while (pud++, addr = next, addr != end);
  125. return 0;
  126. }
  127. /*
  128. * Set up page tables in kva (addr, end). The ptes shall have prot "prot", and
  129. * will have pfns corresponding to the "pages" array.
  130. *
  131. * Ie. pte at addr+N*PAGE_SIZE shall point to pfn corresponding to pages[N]
  132. */
  133. static int vmap_page_range(unsigned long start, unsigned long end,
  134. pgprot_t prot, struct page **pages)
  135. {
  136. pgd_t *pgd;
  137. unsigned long next;
  138. unsigned long addr = start;
  139. int err = 0;
  140. int nr = 0;
  141. BUG_ON(addr >= end);
  142. pgd = pgd_offset_k(addr);
  143. do {
  144. next = pgd_addr_end(addr, end);
  145. err = vmap_pud_range(pgd, addr, next, prot, pages, &nr);
  146. if (err)
  147. break;
  148. } while (pgd++, addr = next, addr != end);
  149. flush_cache_vmap(start, end);
  150. if (unlikely(err))
  151. return err;
  152. return nr;
  153. }
  154. static inline int is_vmalloc_or_module_addr(const void *x)
  155. {
  156. /*
  157. * ARM, x86-64 and sparc64 put modules in a special place,
  158. * and fall back on vmalloc() if that fails. Others
  159. * just put it in the vmalloc space.
  160. */
  161. #if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
  162. unsigned long addr = (unsigned long)x;
  163. if (addr >= MODULES_VADDR && addr < MODULES_END)
  164. return 1;
  165. #endif
  166. return is_vmalloc_addr(x);
  167. }
  168. /*
  169. * Walk a vmap address to the struct page it maps.
  170. */
  171. struct page *vmalloc_to_page(const void *vmalloc_addr)
  172. {
  173. unsigned long addr = (unsigned long) vmalloc_addr;
  174. struct page *page = NULL;
  175. pgd_t *pgd = pgd_offset_k(addr);
  176. /*
  177. * XXX we might need to change this if we add VIRTUAL_BUG_ON for
  178. * architectures that do not vmalloc module space
  179. */
  180. VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr));
  181. if (!pgd_none(*pgd)) {
  182. pud_t *pud = pud_offset(pgd, addr);
  183. if (!pud_none(*pud)) {
  184. pmd_t *pmd = pmd_offset(pud, addr);
  185. if (!pmd_none(*pmd)) {
  186. pte_t *ptep, pte;
  187. ptep = pte_offset_map(pmd, addr);
  188. pte = *ptep;
  189. if (pte_present(pte))
  190. page = pte_page(pte);
  191. pte_unmap(ptep);
  192. }
  193. }
  194. }
  195. return page;
  196. }
  197. EXPORT_SYMBOL(vmalloc_to_page);
  198. /*
  199. * Map a vmalloc()-space virtual address to the physical page frame number.
  200. */
  201. unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
  202. {
  203. return page_to_pfn(vmalloc_to_page(vmalloc_addr));
  204. }
  205. EXPORT_SYMBOL(vmalloc_to_pfn);
  206. /*** Global kva allocator ***/
  207. #define VM_LAZY_FREE 0x01
  208. #define VM_LAZY_FREEING 0x02
  209. #define VM_VM_AREA 0x04
  210. struct vmap_area {
  211. unsigned long va_start;
  212. unsigned long va_end;
  213. unsigned long flags;
  214. struct rb_node rb_node; /* address sorted rbtree */
  215. struct list_head list; /* address sorted list */
  216. struct list_head purge_list; /* "lazy purge" list */
  217. void *private;
  218. struct rcu_head rcu_head;
  219. };
  220. static DEFINE_SPINLOCK(vmap_area_lock);
  221. static struct rb_root vmap_area_root = RB_ROOT;
  222. static LIST_HEAD(vmap_area_list);
  223. static struct vmap_area *__find_vmap_area(unsigned long addr)
  224. {
  225. struct rb_node *n = vmap_area_root.rb_node;
  226. while (n) {
  227. struct vmap_area *va;
  228. va = rb_entry(n, struct vmap_area, rb_node);
  229. if (addr < va->va_start)
  230. n = n->rb_left;
  231. else if (addr > va->va_start)
  232. n = n->rb_right;
  233. else
  234. return va;
  235. }
  236. return NULL;
  237. }
  238. static void __insert_vmap_area(struct vmap_area *va)
  239. {
  240. struct rb_node **p = &vmap_area_root.rb_node;
  241. struct rb_node *parent = NULL;
  242. struct rb_node *tmp;
  243. while (*p) {
  244. struct vmap_area *tmp;
  245. parent = *p;
  246. tmp = rb_entry(parent, struct vmap_area, rb_node);
  247. if (va->va_start < tmp->va_end)
  248. p = &(*p)->rb_left;
  249. else if (va->va_end > tmp->va_start)
  250. p = &(*p)->rb_right;
  251. else
  252. BUG();
  253. }
  254. rb_link_node(&va->rb_node, parent, p);
  255. rb_insert_color(&va->rb_node, &vmap_area_root);
  256. /* address-sort this list so it is usable like the vmlist */
  257. tmp = rb_prev(&va->rb_node);
  258. if (tmp) {
  259. struct vmap_area *prev;
  260. prev = rb_entry(tmp, struct vmap_area, rb_node);
  261. list_add_rcu(&va->list, &prev->list);
  262. } else
  263. list_add_rcu(&va->list, &vmap_area_list);
  264. }
  265. static void purge_vmap_area_lazy(void);
  266. /*
  267. * Allocate a region of KVA of the specified size and alignment, within the
  268. * vstart and vend.
  269. */
  270. static struct vmap_area *alloc_vmap_area(unsigned long size,
  271. unsigned long align,
  272. unsigned long vstart, unsigned long vend,
  273. int node, gfp_t gfp_mask)
  274. {
  275. struct vmap_area *va;
  276. struct rb_node *n;
  277. unsigned long addr;
  278. int purged = 0;
  279. BUG_ON(size & ~PAGE_MASK);
  280. va = kmalloc_node(sizeof(struct vmap_area),
  281. gfp_mask & GFP_RECLAIM_MASK, node);
  282. if (unlikely(!va))
  283. return ERR_PTR(-ENOMEM);
  284. retry:
  285. addr = ALIGN(vstart, align);
  286. spin_lock(&vmap_area_lock);
  287. /* XXX: could have a last_hole cache */
  288. n = vmap_area_root.rb_node;
  289. if (n) {
  290. struct vmap_area *first = NULL;
  291. do {
  292. struct vmap_area *tmp;
  293. tmp = rb_entry(n, struct vmap_area, rb_node);
  294. if (tmp->va_end >= addr) {
  295. if (!first && tmp->va_start < addr + size)
  296. first = tmp;
  297. n = n->rb_left;
  298. } else {
  299. first = tmp;
  300. n = n->rb_right;
  301. }
  302. } while (n);
  303. if (!first)
  304. goto found;
  305. if (first->va_end < addr) {
  306. n = rb_next(&first->rb_node);
  307. if (n)
  308. first = rb_entry(n, struct vmap_area, rb_node);
  309. else
  310. goto found;
  311. }
  312. while (addr + size > first->va_start && addr + size <= vend) {
  313. addr = ALIGN(first->va_end + PAGE_SIZE, align);
  314. n = rb_next(&first->rb_node);
  315. if (n)
  316. first = rb_entry(n, struct vmap_area, rb_node);
  317. else
  318. goto found;
  319. }
  320. }
  321. found:
  322. if (addr + size > vend) {
  323. spin_unlock(&vmap_area_lock);
  324. if (!purged) {
  325. purge_vmap_area_lazy();
  326. purged = 1;
  327. goto retry;
  328. }
  329. if (printk_ratelimit())
  330. printk(KERN_WARNING "vmap allocation failed: "
  331. "use vmalloc=<size> to increase size.\n");
  332. return ERR_PTR(-EBUSY);
  333. }
  334. BUG_ON(addr & (align-1));
  335. va->va_start = addr;
  336. va->va_end = addr + size;
  337. va->flags = 0;
  338. __insert_vmap_area(va);
  339. spin_unlock(&vmap_area_lock);
  340. return va;
  341. }
  342. static void rcu_free_va(struct rcu_head *head)
  343. {
  344. struct vmap_area *va = container_of(head, struct vmap_area, rcu_head);
  345. kfree(va);
  346. }
  347. static void __free_vmap_area(struct vmap_area *va)
  348. {
  349. BUG_ON(RB_EMPTY_NODE(&va->rb_node));
  350. rb_erase(&va->rb_node, &vmap_area_root);
  351. RB_CLEAR_NODE(&va->rb_node);
  352. list_del_rcu(&va->list);
  353. call_rcu(&va->rcu_head, rcu_free_va);
  354. }
  355. /*
  356. * Free a region of KVA allocated by alloc_vmap_area
  357. */
  358. static void free_vmap_area(struct vmap_area *va)
  359. {
  360. spin_lock(&vmap_area_lock);
  361. __free_vmap_area(va);
  362. spin_unlock(&vmap_area_lock);
  363. }
  364. /*
  365. * Clear the pagetable entries of a given vmap_area
  366. */
  367. static void unmap_vmap_area(struct vmap_area *va)
  368. {
  369. vunmap_page_range(va->va_start, va->va_end);
  370. }
  371. /*
  372. * lazy_max_pages is the maximum amount of virtual address space we gather up
  373. * before attempting to purge with a TLB flush.
  374. *
  375. * There is a tradeoff here: a larger number will cover more kernel page tables
  376. * and take slightly longer to purge, but it will linearly reduce the number of
  377. * global TLB flushes that must be performed. It would seem natural to scale
  378. * this number up linearly with the number of CPUs (because vmapping activity
  379. * could also scale linearly with the number of CPUs), however it is likely
  380. * that in practice, workloads might be constrained in other ways that mean
  381. * vmap activity will not scale linearly with CPUs. Also, I want to be
  382. * conservative and not introduce a big latency on huge systems, so go with
  383. * a less aggressive log scale. It will still be an improvement over the old
  384. * code, and it will be simple to change the scale factor if we find that it
  385. * becomes a problem on bigger systems.
  386. */
  387. static unsigned long lazy_max_pages(void)
  388. {
  389. unsigned int log;
  390. log = fls(num_online_cpus());
  391. return log * (32UL * 1024 * 1024 / PAGE_SIZE);
  392. }
  393. static atomic_t vmap_lazy_nr = ATOMIC_INIT(0);
  394. /*
  395. * Purges all lazily-freed vmap areas.
  396. *
  397. * If sync is 0 then don't purge if there is already a purge in progress.
  398. * If force_flush is 1, then flush kernel TLBs between *start and *end even
  399. * if we found no lazy vmap areas to unmap (callers can use this to optimise
  400. * their own TLB flushing).
  401. * Returns with *start = min(*start, lowest purged address)
  402. * *end = max(*end, highest purged address)
  403. */
  404. static void __purge_vmap_area_lazy(unsigned long *start, unsigned long *end,
  405. int sync, int force_flush)
  406. {
  407. static DEFINE_SPINLOCK(purge_lock);
  408. LIST_HEAD(valist);
  409. struct vmap_area *va;
  410. int nr = 0;
  411. /*
  412. * If sync is 0 but force_flush is 1, we'll go sync anyway but callers
  413. * should not expect such behaviour. This just simplifies locking for
  414. * the case that isn't actually used at the moment anyway.
  415. */
  416. if (!sync && !force_flush) {
  417. if (!spin_trylock(&purge_lock))
  418. return;
  419. } else
  420. spin_lock(&purge_lock);
  421. rcu_read_lock();
  422. list_for_each_entry_rcu(va, &vmap_area_list, list) {
  423. if (va->flags & VM_LAZY_FREE) {
  424. if (va->va_start < *start)
  425. *start = va->va_start;
  426. if (va->va_end > *end)
  427. *end = va->va_end;
  428. nr += (va->va_end - va->va_start) >> PAGE_SHIFT;
  429. unmap_vmap_area(va);
  430. list_add_tail(&va->purge_list, &valist);
  431. va->flags |= VM_LAZY_FREEING;
  432. va->flags &= ~VM_LAZY_FREE;
  433. }
  434. }
  435. rcu_read_unlock();
  436. if (nr) {
  437. BUG_ON(nr > atomic_read(&vmap_lazy_nr));
  438. atomic_sub(nr, &vmap_lazy_nr);
  439. }
  440. if (nr || force_flush)
  441. flush_tlb_kernel_range(*start, *end);
  442. if (nr) {
  443. spin_lock(&vmap_area_lock);
  444. list_for_each_entry(va, &valist, purge_list)
  445. __free_vmap_area(va);
  446. spin_unlock(&vmap_area_lock);
  447. }
  448. spin_unlock(&purge_lock);
  449. }
  450. /*
  451. * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
  452. * is already purging.
  453. */
  454. static void try_purge_vmap_area_lazy(void)
  455. {
  456. unsigned long start = ULONG_MAX, end = 0;
  457. __purge_vmap_area_lazy(&start, &end, 0, 0);
  458. }
  459. /*
  460. * Kick off a purge of the outstanding lazy areas.
  461. */
  462. static void purge_vmap_area_lazy(void)
  463. {
  464. unsigned long start = ULONG_MAX, end = 0;
  465. __purge_vmap_area_lazy(&start, &end, 1, 0);
  466. }
  467. /*
  468. * Free and unmap a vmap area, caller ensuring flush_cache_vunmap had been
  469. * called for the correct range previously.
  470. */
  471. static void free_unmap_vmap_area_noflush(struct vmap_area *va)
  472. {
  473. va->flags |= VM_LAZY_FREE;
  474. atomic_add((va->va_end - va->va_start) >> PAGE_SHIFT, &vmap_lazy_nr);
  475. if (unlikely(atomic_read(&vmap_lazy_nr) > lazy_max_pages()))
  476. try_purge_vmap_area_lazy();
  477. }
  478. /*
  479. * Free and unmap a vmap area
  480. */
  481. static void free_unmap_vmap_area(struct vmap_area *va)
  482. {
  483. flush_cache_vunmap(va->va_start, va->va_end);
  484. free_unmap_vmap_area_noflush(va);
  485. }
  486. static struct vmap_area *find_vmap_area(unsigned long addr)
  487. {
  488. struct vmap_area *va;
  489. spin_lock(&vmap_area_lock);
  490. va = __find_vmap_area(addr);
  491. spin_unlock(&vmap_area_lock);
  492. return va;
  493. }
  494. static void free_unmap_vmap_area_addr(unsigned long addr)
  495. {
  496. struct vmap_area *va;
  497. va = find_vmap_area(addr);
  498. BUG_ON(!va);
  499. free_unmap_vmap_area(va);
  500. }
  501. /*** Per cpu kva allocator ***/
  502. /*
  503. * vmap space is limited especially on 32 bit architectures. Ensure there is
  504. * room for at least 16 percpu vmap blocks per CPU.
  505. */
  506. /*
  507. * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
  508. * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess
  509. * instead (we just need a rough idea)
  510. */
  511. #if BITS_PER_LONG == 32
  512. #define VMALLOC_SPACE (128UL*1024*1024)
  513. #else
  514. #define VMALLOC_SPACE (128UL*1024*1024*1024)
  515. #endif
  516. #define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE)
  517. #define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */
  518. #define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */
  519. #define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2)
  520. #define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */
  521. #define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */
  522. #define VMAP_BBMAP_BITS VMAP_MIN(VMAP_BBMAP_BITS_MAX, \
  523. VMAP_MAX(VMAP_BBMAP_BITS_MIN, \
  524. VMALLOC_PAGES / NR_CPUS / 16))
  525. #define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE)
  526. static bool vmap_initialized __read_mostly = false;
  527. struct vmap_block_queue {
  528. spinlock_t lock;
  529. struct list_head free;
  530. struct list_head dirty;
  531. unsigned int nr_dirty;
  532. };
  533. struct vmap_block {
  534. spinlock_t lock;
  535. struct vmap_area *va;
  536. struct vmap_block_queue *vbq;
  537. unsigned long free, dirty;
  538. DECLARE_BITMAP(alloc_map, VMAP_BBMAP_BITS);
  539. DECLARE_BITMAP(dirty_map, VMAP_BBMAP_BITS);
  540. union {
  541. struct {
  542. struct list_head free_list;
  543. struct list_head dirty_list;
  544. };
  545. struct rcu_head rcu_head;
  546. };
  547. };
  548. /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
  549. static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
  550. /*
  551. * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block
  552. * in the free path. Could get rid of this if we change the API to return a
  553. * "cookie" from alloc, to be passed to free. But no big deal yet.
  554. */
  555. static DEFINE_SPINLOCK(vmap_block_tree_lock);
  556. static RADIX_TREE(vmap_block_tree, GFP_ATOMIC);
  557. /*
  558. * We should probably have a fallback mechanism to allocate virtual memory
  559. * out of partially filled vmap blocks. However vmap block sizing should be
  560. * fairly reasonable according to the vmalloc size, so it shouldn't be a
  561. * big problem.
  562. */
  563. static unsigned long addr_to_vb_idx(unsigned long addr)
  564. {
  565. addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
  566. addr /= VMAP_BLOCK_SIZE;
  567. return addr;
  568. }
  569. static struct vmap_block *new_vmap_block(gfp_t gfp_mask)
  570. {
  571. struct vmap_block_queue *vbq;
  572. struct vmap_block *vb;
  573. struct vmap_area *va;
  574. unsigned long vb_idx;
  575. int node, err;
  576. node = numa_node_id();
  577. vb = kmalloc_node(sizeof(struct vmap_block),
  578. gfp_mask & GFP_RECLAIM_MASK, node);
  579. if (unlikely(!vb))
  580. return ERR_PTR(-ENOMEM);
  581. va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
  582. VMALLOC_START, VMALLOC_END,
  583. node, gfp_mask);
  584. if (unlikely(IS_ERR(va))) {
  585. kfree(vb);
  586. return ERR_PTR(PTR_ERR(va));
  587. }
  588. err = radix_tree_preload(gfp_mask);
  589. if (unlikely(err)) {
  590. kfree(vb);
  591. free_vmap_area(va);
  592. return ERR_PTR(err);
  593. }
  594. spin_lock_init(&vb->lock);
  595. vb->va = va;
  596. vb->free = VMAP_BBMAP_BITS;
  597. vb->dirty = 0;
  598. bitmap_zero(vb->alloc_map, VMAP_BBMAP_BITS);
  599. bitmap_zero(vb->dirty_map, VMAP_BBMAP_BITS);
  600. INIT_LIST_HEAD(&vb->free_list);
  601. INIT_LIST_HEAD(&vb->dirty_list);
  602. vb_idx = addr_to_vb_idx(va->va_start);
  603. spin_lock(&vmap_block_tree_lock);
  604. err = radix_tree_insert(&vmap_block_tree, vb_idx, vb);
  605. spin_unlock(&vmap_block_tree_lock);
  606. BUG_ON(err);
  607. radix_tree_preload_end();
  608. vbq = &get_cpu_var(vmap_block_queue);
  609. vb->vbq = vbq;
  610. spin_lock(&vbq->lock);
  611. list_add(&vb->free_list, &vbq->free);
  612. spin_unlock(&vbq->lock);
  613. put_cpu_var(vmap_cpu_blocks);
  614. return vb;
  615. }
  616. static void rcu_free_vb(struct rcu_head *head)
  617. {
  618. struct vmap_block *vb = container_of(head, struct vmap_block, rcu_head);
  619. kfree(vb);
  620. }
  621. static void free_vmap_block(struct vmap_block *vb)
  622. {
  623. struct vmap_block *tmp;
  624. unsigned long vb_idx;
  625. spin_lock(&vb->vbq->lock);
  626. if (!list_empty(&vb->free_list))
  627. list_del(&vb->free_list);
  628. if (!list_empty(&vb->dirty_list))
  629. list_del(&vb->dirty_list);
  630. spin_unlock(&vb->vbq->lock);
  631. vb_idx = addr_to_vb_idx(vb->va->va_start);
  632. spin_lock(&vmap_block_tree_lock);
  633. tmp = radix_tree_delete(&vmap_block_tree, vb_idx);
  634. spin_unlock(&vmap_block_tree_lock);
  635. BUG_ON(tmp != vb);
  636. free_unmap_vmap_area_noflush(vb->va);
  637. call_rcu(&vb->rcu_head, rcu_free_vb);
  638. }
  639. static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
  640. {
  641. struct vmap_block_queue *vbq;
  642. struct vmap_block *vb;
  643. unsigned long addr = 0;
  644. unsigned int order;
  645. BUG_ON(size & ~PAGE_MASK);
  646. BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
  647. order = get_order(size);
  648. again:
  649. rcu_read_lock();
  650. vbq = &get_cpu_var(vmap_block_queue);
  651. list_for_each_entry_rcu(vb, &vbq->free, free_list) {
  652. int i;
  653. spin_lock(&vb->lock);
  654. i = bitmap_find_free_region(vb->alloc_map,
  655. VMAP_BBMAP_BITS, order);
  656. if (i >= 0) {
  657. addr = vb->va->va_start + (i << PAGE_SHIFT);
  658. BUG_ON(addr_to_vb_idx(addr) !=
  659. addr_to_vb_idx(vb->va->va_start));
  660. vb->free -= 1UL << order;
  661. if (vb->free == 0) {
  662. spin_lock(&vbq->lock);
  663. list_del_init(&vb->free_list);
  664. spin_unlock(&vbq->lock);
  665. }
  666. spin_unlock(&vb->lock);
  667. break;
  668. }
  669. spin_unlock(&vb->lock);
  670. }
  671. put_cpu_var(vmap_cpu_blocks);
  672. rcu_read_unlock();
  673. if (!addr) {
  674. vb = new_vmap_block(gfp_mask);
  675. if (IS_ERR(vb))
  676. return vb;
  677. goto again;
  678. }
  679. return (void *)addr;
  680. }
  681. static void vb_free(const void *addr, unsigned long size)
  682. {
  683. unsigned long offset;
  684. unsigned long vb_idx;
  685. unsigned int order;
  686. struct vmap_block *vb;
  687. BUG_ON(size & ~PAGE_MASK);
  688. BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
  689. flush_cache_vunmap((unsigned long)addr, (unsigned long)addr + size);
  690. order = get_order(size);
  691. offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1);
  692. vb_idx = addr_to_vb_idx((unsigned long)addr);
  693. rcu_read_lock();
  694. vb = radix_tree_lookup(&vmap_block_tree, vb_idx);
  695. rcu_read_unlock();
  696. BUG_ON(!vb);
  697. spin_lock(&vb->lock);
  698. bitmap_allocate_region(vb->dirty_map, offset >> PAGE_SHIFT, order);
  699. if (!vb->dirty) {
  700. spin_lock(&vb->vbq->lock);
  701. list_add(&vb->dirty_list, &vb->vbq->dirty);
  702. spin_unlock(&vb->vbq->lock);
  703. }
  704. vb->dirty += 1UL << order;
  705. if (vb->dirty == VMAP_BBMAP_BITS) {
  706. BUG_ON(vb->free || !list_empty(&vb->free_list));
  707. spin_unlock(&vb->lock);
  708. free_vmap_block(vb);
  709. } else
  710. spin_unlock(&vb->lock);
  711. }
  712. /**
  713. * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
  714. *
  715. * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
  716. * to amortize TLB flushing overheads. What this means is that any page you
  717. * have now, may, in a former life, have been mapped into kernel virtual
  718. * address by the vmap layer and so there might be some CPUs with TLB entries
  719. * still referencing that page (additional to the regular 1:1 kernel mapping).
  720. *
  721. * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
  722. * be sure that none of the pages we have control over will have any aliases
  723. * from the vmap layer.
  724. */
  725. void vm_unmap_aliases(void)
  726. {
  727. unsigned long start = ULONG_MAX, end = 0;
  728. int cpu;
  729. int flush = 0;
  730. if (unlikely(!vmap_initialized))
  731. return;
  732. for_each_possible_cpu(cpu) {
  733. struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
  734. struct vmap_block *vb;
  735. rcu_read_lock();
  736. list_for_each_entry_rcu(vb, &vbq->free, free_list) {
  737. int i;
  738. spin_lock(&vb->lock);
  739. i = find_first_bit(vb->dirty_map, VMAP_BBMAP_BITS);
  740. while (i < VMAP_BBMAP_BITS) {
  741. unsigned long s, e;
  742. int j;
  743. j = find_next_zero_bit(vb->dirty_map,
  744. VMAP_BBMAP_BITS, i);
  745. s = vb->va->va_start + (i << PAGE_SHIFT);
  746. e = vb->va->va_start + (j << PAGE_SHIFT);
  747. vunmap_page_range(s, e);
  748. flush = 1;
  749. if (s < start)
  750. start = s;
  751. if (e > end)
  752. end = e;
  753. i = j;
  754. i = find_next_bit(vb->dirty_map,
  755. VMAP_BBMAP_BITS, i);
  756. }
  757. spin_unlock(&vb->lock);
  758. }
  759. rcu_read_unlock();
  760. }
  761. __purge_vmap_area_lazy(&start, &end, 1, flush);
  762. }
  763. EXPORT_SYMBOL_GPL(vm_unmap_aliases);
  764. /**
  765. * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
  766. * @mem: the pointer returned by vm_map_ram
  767. * @count: the count passed to that vm_map_ram call (cannot unmap partial)
  768. */
  769. void vm_unmap_ram(const void *mem, unsigned int count)
  770. {
  771. unsigned long size = count << PAGE_SHIFT;
  772. unsigned long addr = (unsigned long)mem;
  773. BUG_ON(!addr);
  774. BUG_ON(addr < VMALLOC_START);
  775. BUG_ON(addr > VMALLOC_END);
  776. BUG_ON(addr & (PAGE_SIZE-1));
  777. debug_check_no_locks_freed(mem, size);
  778. if (likely(count <= VMAP_MAX_ALLOC))
  779. vb_free(mem, size);
  780. else
  781. free_unmap_vmap_area_addr(addr);
  782. }
  783. EXPORT_SYMBOL(vm_unmap_ram);
  784. /**
  785. * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
  786. * @pages: an array of pointers to the pages to be mapped
  787. * @count: number of pages
  788. * @node: prefer to allocate data structures on this node
  789. * @prot: memory protection to use. PAGE_KERNEL for regular RAM
  790. *
  791. * Returns: a pointer to the address that has been mapped, or %NULL on failure
  792. */
  793. void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
  794. {
  795. unsigned long size = count << PAGE_SHIFT;
  796. unsigned long addr;
  797. void *mem;
  798. if (likely(count <= VMAP_MAX_ALLOC)) {
  799. mem = vb_alloc(size, GFP_KERNEL);
  800. if (IS_ERR(mem))
  801. return NULL;
  802. addr = (unsigned long)mem;
  803. } else {
  804. struct vmap_area *va;
  805. va = alloc_vmap_area(size, PAGE_SIZE,
  806. VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
  807. if (IS_ERR(va))
  808. return NULL;
  809. addr = va->va_start;
  810. mem = (void *)addr;
  811. }
  812. if (vmap_page_range(addr, addr + size, prot, pages) < 0) {
  813. vm_unmap_ram(mem, count);
  814. return NULL;
  815. }
  816. return mem;
  817. }
  818. EXPORT_SYMBOL(vm_map_ram);
  819. void __init vmalloc_init(void)
  820. {
  821. int i;
  822. for_each_possible_cpu(i) {
  823. struct vmap_block_queue *vbq;
  824. vbq = &per_cpu(vmap_block_queue, i);
  825. spin_lock_init(&vbq->lock);
  826. INIT_LIST_HEAD(&vbq->free);
  827. INIT_LIST_HEAD(&vbq->dirty);
  828. vbq->nr_dirty = 0;
  829. }
  830. vmap_initialized = true;
  831. }
  832. void unmap_kernel_range(unsigned long addr, unsigned long size)
  833. {
  834. unsigned long end = addr + size;
  835. vunmap_page_range(addr, end);
  836. flush_tlb_kernel_range(addr, end);
  837. }
  838. int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page ***pages)
  839. {
  840. unsigned long addr = (unsigned long)area->addr;
  841. unsigned long end = addr + area->size - PAGE_SIZE;
  842. int err;
  843. err = vmap_page_range(addr, end, prot, *pages);
  844. if (err > 0) {
  845. *pages += err;
  846. err = 0;
  847. }
  848. return err;
  849. }
  850. EXPORT_SYMBOL_GPL(map_vm_area);
  851. /*** Old vmalloc interfaces ***/
  852. DEFINE_RWLOCK(vmlist_lock);
  853. struct vm_struct *vmlist;
  854. static struct vm_struct *__get_vm_area_node(unsigned long size,
  855. unsigned long flags, unsigned long start, unsigned long end,
  856. int node, gfp_t gfp_mask, void *caller)
  857. {
  858. static struct vmap_area *va;
  859. struct vm_struct *area;
  860. struct vm_struct *tmp, **p;
  861. unsigned long align = 1;
  862. BUG_ON(in_interrupt());
  863. if (flags & VM_IOREMAP) {
  864. int bit = fls(size);
  865. if (bit > IOREMAP_MAX_ORDER)
  866. bit = IOREMAP_MAX_ORDER;
  867. else if (bit < PAGE_SHIFT)
  868. bit = PAGE_SHIFT;
  869. align = 1ul << bit;
  870. }
  871. size = PAGE_ALIGN(size);
  872. if (unlikely(!size))
  873. return NULL;
  874. area = kmalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
  875. if (unlikely(!area))
  876. return NULL;
  877. /*
  878. * We always allocate a guard page.
  879. */
  880. size += PAGE_SIZE;
  881. va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
  882. if (IS_ERR(va)) {
  883. kfree(area);
  884. return NULL;
  885. }
  886. area->flags = flags;
  887. area->addr = (void *)va->va_start;
  888. area->size = size;
  889. area->pages = NULL;
  890. area->nr_pages = 0;
  891. area->phys_addr = 0;
  892. area->caller = caller;
  893. va->private = area;
  894. va->flags |= VM_VM_AREA;
  895. write_lock(&vmlist_lock);
  896. for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
  897. if (tmp->addr >= area->addr)
  898. break;
  899. }
  900. area->next = *p;
  901. *p = area;
  902. write_unlock(&vmlist_lock);
  903. return area;
  904. }
  905. struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
  906. unsigned long start, unsigned long end)
  907. {
  908. return __get_vm_area_node(size, flags, start, end, -1, GFP_KERNEL,
  909. __builtin_return_address(0));
  910. }
  911. EXPORT_SYMBOL_GPL(__get_vm_area);
  912. /**
  913. * get_vm_area - reserve a contiguous kernel virtual area
  914. * @size: size of the area
  915. * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC
  916. *
  917. * Search an area of @size in the kernel virtual mapping area,
  918. * and reserved it for out purposes. Returns the area descriptor
  919. * on success or %NULL on failure.
  920. */
  921. struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
  922. {
  923. return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
  924. -1, GFP_KERNEL, __builtin_return_address(0));
  925. }
  926. struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
  927. void *caller)
  928. {
  929. return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
  930. -1, GFP_KERNEL, caller);
  931. }
  932. struct vm_struct *get_vm_area_node(unsigned long size, unsigned long flags,
  933. int node, gfp_t gfp_mask)
  934. {
  935. return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END, node,
  936. gfp_mask, __builtin_return_address(0));
  937. }
  938. static struct vm_struct *find_vm_area(const void *addr)
  939. {
  940. struct vmap_area *va;
  941. va = find_vmap_area((unsigned long)addr);
  942. if (va && va->flags & VM_VM_AREA)
  943. return va->private;
  944. return NULL;
  945. }
  946. /**
  947. * remove_vm_area - find and remove a continuous kernel virtual area
  948. * @addr: base address
  949. *
  950. * Search for the kernel VM area starting at @addr, and remove it.
  951. * This function returns the found VM area, but using it is NOT safe
  952. * on SMP machines, except for its size or flags.
  953. */
  954. struct vm_struct *remove_vm_area(const void *addr)
  955. {
  956. struct vmap_area *va;
  957. va = find_vmap_area((unsigned long)addr);
  958. if (va && va->flags & VM_VM_AREA) {
  959. struct vm_struct *vm = va->private;
  960. struct vm_struct *tmp, **p;
  961. free_unmap_vmap_area(va);
  962. vm->size -= PAGE_SIZE;
  963. write_lock(&vmlist_lock);
  964. for (p = &vmlist; (tmp = *p) != vm; p = &tmp->next)
  965. ;
  966. *p = tmp->next;
  967. write_unlock(&vmlist_lock);
  968. return vm;
  969. }
  970. return NULL;
  971. }
  972. static void __vunmap(const void *addr, int deallocate_pages)
  973. {
  974. struct vm_struct *area;
  975. if (!addr)
  976. return;
  977. if ((PAGE_SIZE-1) & (unsigned long)addr) {
  978. WARN(1, KERN_ERR "Trying to vfree() bad address (%p)\n", addr);
  979. return;
  980. }
  981. area = remove_vm_area(addr);
  982. if (unlikely(!area)) {
  983. WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
  984. addr);
  985. return;
  986. }
  987. debug_check_no_locks_freed(addr, area->size);
  988. debug_check_no_obj_freed(addr, area->size);
  989. if (deallocate_pages) {
  990. int i;
  991. for (i = 0; i < area->nr_pages; i++) {
  992. struct page *page = area->pages[i];
  993. BUG_ON(!page);
  994. __free_page(page);
  995. }
  996. if (area->flags & VM_VPAGES)
  997. vfree(area->pages);
  998. else
  999. kfree(area->pages);
  1000. }
  1001. kfree(area);
  1002. return;
  1003. }
  1004. /**
  1005. * vfree - release memory allocated by vmalloc()
  1006. * @addr: memory base address
  1007. *
  1008. * Free the virtually continuous memory area starting at @addr, as
  1009. * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
  1010. * NULL, no operation is performed.
  1011. *
  1012. * Must not be called in interrupt context.
  1013. */
  1014. void vfree(const void *addr)
  1015. {
  1016. BUG_ON(in_interrupt());
  1017. __vunmap(addr, 1);
  1018. }
  1019. EXPORT_SYMBOL(vfree);
  1020. /**
  1021. * vunmap - release virtual mapping obtained by vmap()
  1022. * @addr: memory base address
  1023. *
  1024. * Free the virtually contiguous memory area starting at @addr,
  1025. * which was created from the page array passed to vmap().
  1026. *
  1027. * Must not be called in interrupt context.
  1028. */
  1029. void vunmap(const void *addr)
  1030. {
  1031. BUG_ON(in_interrupt());
  1032. __vunmap(addr, 0);
  1033. }
  1034. EXPORT_SYMBOL(vunmap);
  1035. /**
  1036. * vmap - map an array of pages into virtually contiguous space
  1037. * @pages: array of page pointers
  1038. * @count: number of pages to map
  1039. * @flags: vm_area->flags
  1040. * @prot: page protection for the mapping
  1041. *
  1042. * Maps @count pages from @pages into contiguous kernel virtual
  1043. * space.
  1044. */
  1045. void *vmap(struct page **pages, unsigned int count,
  1046. unsigned long flags, pgprot_t prot)
  1047. {
  1048. struct vm_struct *area;
  1049. if (count > num_physpages)
  1050. return NULL;
  1051. area = get_vm_area_caller((count << PAGE_SHIFT), flags,
  1052. __builtin_return_address(0));
  1053. if (!area)
  1054. return NULL;
  1055. if (map_vm_area(area, prot, &pages)) {
  1056. vunmap(area->addr);
  1057. return NULL;
  1058. }
  1059. return area->addr;
  1060. }
  1061. EXPORT_SYMBOL(vmap);
  1062. static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
  1063. int node, void *caller);
  1064. static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
  1065. pgprot_t prot, int node, void *caller)
  1066. {
  1067. struct page **pages;
  1068. unsigned int nr_pages, array_size, i;
  1069. nr_pages = (area->size - PAGE_SIZE) >> PAGE_SHIFT;
  1070. array_size = (nr_pages * sizeof(struct page *));
  1071. area->nr_pages = nr_pages;
  1072. /* Please note that the recursion is strictly bounded. */
  1073. if (array_size > PAGE_SIZE) {
  1074. pages = __vmalloc_node(array_size, gfp_mask | __GFP_ZERO,
  1075. PAGE_KERNEL, node, caller);
  1076. area->flags |= VM_VPAGES;
  1077. } else {
  1078. pages = kmalloc_node(array_size,
  1079. (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO,
  1080. node);
  1081. }
  1082. area->pages = pages;
  1083. area->caller = caller;
  1084. if (!area->pages) {
  1085. remove_vm_area(area->addr);
  1086. kfree(area);
  1087. return NULL;
  1088. }
  1089. for (i = 0; i < area->nr_pages; i++) {
  1090. struct page *page;
  1091. if (node < 0)
  1092. page = alloc_page(gfp_mask);
  1093. else
  1094. page = alloc_pages_node(node, gfp_mask, 0);
  1095. if (unlikely(!page)) {
  1096. /* Successfully allocated i pages, free them in __vunmap() */
  1097. area->nr_pages = i;
  1098. goto fail;
  1099. }
  1100. area->pages[i] = page;
  1101. }
  1102. if (map_vm_area(area, prot, &pages))
  1103. goto fail;
  1104. return area->addr;
  1105. fail:
  1106. vfree(area->addr);
  1107. return NULL;
  1108. }
  1109. void *__vmalloc_area(struct vm_struct *area, gfp_t gfp_mask, pgprot_t prot)
  1110. {
  1111. return __vmalloc_area_node(area, gfp_mask, prot, -1,
  1112. __builtin_return_address(0));
  1113. }
  1114. /**
  1115. * __vmalloc_node - allocate virtually contiguous memory
  1116. * @size: allocation size
  1117. * @gfp_mask: flags for the page level allocator
  1118. * @prot: protection mask for the allocated pages
  1119. * @node: node to use for allocation or -1
  1120. * @caller: caller's return address
  1121. *
  1122. * Allocate enough pages to cover @size from the page level
  1123. * allocator with @gfp_mask flags. Map them into contiguous
  1124. * kernel virtual space, using a pagetable protection of @prot.
  1125. */
  1126. static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
  1127. int node, void *caller)
  1128. {
  1129. struct vm_struct *area;
  1130. size = PAGE_ALIGN(size);
  1131. if (!size || (size >> PAGE_SHIFT) > num_physpages)
  1132. return NULL;
  1133. area = __get_vm_area_node(size, VM_ALLOC, VMALLOC_START, VMALLOC_END,
  1134. node, gfp_mask, caller);
  1135. if (!area)
  1136. return NULL;
  1137. return __vmalloc_area_node(area, gfp_mask, prot, node, caller);
  1138. }
  1139. void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
  1140. {
  1141. return __vmalloc_node(size, gfp_mask, prot, -1,
  1142. __builtin_return_address(0));
  1143. }
  1144. EXPORT_SYMBOL(__vmalloc);
  1145. /**
  1146. * vmalloc - allocate virtually contiguous memory
  1147. * @size: allocation size
  1148. * Allocate enough pages to cover @size from the page level
  1149. * allocator and map them into contiguous kernel virtual space.
  1150. *
  1151. * For tight control over page level allocator and protection flags
  1152. * use __vmalloc() instead.
  1153. */
  1154. void *vmalloc(unsigned long size)
  1155. {
  1156. return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
  1157. -1, __builtin_return_address(0));
  1158. }
  1159. EXPORT_SYMBOL(vmalloc);
  1160. /**
  1161. * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
  1162. * @size: allocation size
  1163. *
  1164. * The resulting memory area is zeroed so it can be mapped to userspace
  1165. * without leaking data.
  1166. */
  1167. void *vmalloc_user(unsigned long size)
  1168. {
  1169. struct vm_struct *area;
  1170. void *ret;
  1171. ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO, PAGE_KERNEL);
  1172. if (ret) {
  1173. area = find_vm_area(ret);
  1174. area->flags |= VM_USERMAP;
  1175. }
  1176. return ret;
  1177. }
  1178. EXPORT_SYMBOL(vmalloc_user);
  1179. /**
  1180. * vmalloc_node - allocate memory on a specific node
  1181. * @size: allocation size
  1182. * @node: numa node
  1183. *
  1184. * Allocate enough pages to cover @size from the page level
  1185. * allocator and map them into contiguous kernel virtual space.
  1186. *
  1187. * For tight control over page level allocator and protection flags
  1188. * use __vmalloc() instead.
  1189. */
  1190. void *vmalloc_node(unsigned long size, int node)
  1191. {
  1192. return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
  1193. node, __builtin_return_address(0));
  1194. }
  1195. EXPORT_SYMBOL(vmalloc_node);
  1196. #ifndef PAGE_KERNEL_EXEC
  1197. # define PAGE_KERNEL_EXEC PAGE_KERNEL
  1198. #endif
  1199. /**
  1200. * vmalloc_exec - allocate virtually contiguous, executable memory
  1201. * @size: allocation size
  1202. *
  1203. * Kernel-internal function to allocate enough pages to cover @size
  1204. * the page level allocator and map them into contiguous and
  1205. * executable kernel virtual space.
  1206. *
  1207. * For tight control over page level allocator and protection flags
  1208. * use __vmalloc() instead.
  1209. */
  1210. void *vmalloc_exec(unsigned long size)
  1211. {
  1212. return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
  1213. }
  1214. #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
  1215. #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
  1216. #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
  1217. #define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL
  1218. #else
  1219. #define GFP_VMALLOC32 GFP_KERNEL
  1220. #endif
  1221. /**
  1222. * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
  1223. * @size: allocation size
  1224. *
  1225. * Allocate enough 32bit PA addressable pages to cover @size from the
  1226. * page level allocator and map them into contiguous kernel virtual space.
  1227. */
  1228. void *vmalloc_32(unsigned long size)
  1229. {
  1230. return __vmalloc(size, GFP_VMALLOC32, PAGE_KERNEL);
  1231. }
  1232. EXPORT_SYMBOL(vmalloc_32);
  1233. /**
  1234. * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
  1235. * @size: allocation size
  1236. *
  1237. * The resulting memory area is 32bit addressable and zeroed so it can be
  1238. * mapped to userspace without leaking data.
  1239. */
  1240. void *vmalloc_32_user(unsigned long size)
  1241. {
  1242. struct vm_struct *area;
  1243. void *ret;
  1244. ret = __vmalloc(size, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL);
  1245. if (ret) {
  1246. area = find_vm_area(ret);
  1247. area->flags |= VM_USERMAP;
  1248. }
  1249. return ret;
  1250. }
  1251. EXPORT_SYMBOL(vmalloc_32_user);
  1252. long vread(char *buf, char *addr, unsigned long count)
  1253. {
  1254. struct vm_struct *tmp;
  1255. char *vaddr, *buf_start = buf;
  1256. unsigned long n;
  1257. /* Don't allow overflow */
  1258. if ((unsigned long) addr + count < count)
  1259. count = -(unsigned long) addr;
  1260. read_lock(&vmlist_lock);
  1261. for (tmp = vmlist; tmp; tmp = tmp->next) {
  1262. vaddr = (char *) tmp->addr;
  1263. if (addr >= vaddr + tmp->size - PAGE_SIZE)
  1264. continue;
  1265. while (addr < vaddr) {
  1266. if (count == 0)
  1267. goto finished;
  1268. *buf = '\0';
  1269. buf++;
  1270. addr++;
  1271. count--;
  1272. }
  1273. n = vaddr + tmp->size - PAGE_SIZE - addr;
  1274. do {
  1275. if (count == 0)
  1276. goto finished;
  1277. *buf = *addr;
  1278. buf++;
  1279. addr++;
  1280. count--;
  1281. } while (--n > 0);
  1282. }
  1283. finished:
  1284. read_unlock(&vmlist_lock);
  1285. return buf - buf_start;
  1286. }
  1287. long vwrite(char *buf, char *addr, unsigned long count)
  1288. {
  1289. struct vm_struct *tmp;
  1290. char *vaddr, *buf_start = buf;
  1291. unsigned long n;
  1292. /* Don't allow overflow */
  1293. if ((unsigned long) addr + count < count)
  1294. count = -(unsigned long) addr;
  1295. read_lock(&vmlist_lock);
  1296. for (tmp = vmlist; tmp; tmp = tmp->next) {
  1297. vaddr = (char *) tmp->addr;
  1298. if (addr >= vaddr + tmp->size - PAGE_SIZE)
  1299. continue;
  1300. while (addr < vaddr) {
  1301. if (count == 0)
  1302. goto finished;
  1303. buf++;
  1304. addr++;
  1305. count--;
  1306. }
  1307. n = vaddr + tmp->size - PAGE_SIZE - addr;
  1308. do {
  1309. if (count == 0)
  1310. goto finished;
  1311. *addr = *buf;
  1312. buf++;
  1313. addr++;
  1314. count--;
  1315. } while (--n > 0);
  1316. }
  1317. finished:
  1318. read_unlock(&vmlist_lock);
  1319. return buf - buf_start;
  1320. }
  1321. /**
  1322. * remap_vmalloc_range - map vmalloc pages to userspace
  1323. * @vma: vma to cover (map full range of vma)
  1324. * @addr: vmalloc memory
  1325. * @pgoff: number of pages into addr before first page to map
  1326. *
  1327. * Returns: 0 for success, -Exxx on failure
  1328. *
  1329. * This function checks that addr is a valid vmalloc'ed area, and
  1330. * that it is big enough to cover the vma. Will return failure if
  1331. * that criteria isn't met.
  1332. *
  1333. * Similar to remap_pfn_range() (see mm/memory.c)
  1334. */
  1335. int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
  1336. unsigned long pgoff)
  1337. {
  1338. struct vm_struct *area;
  1339. unsigned long uaddr = vma->vm_start;
  1340. unsigned long usize = vma->vm_end - vma->vm_start;
  1341. if ((PAGE_SIZE-1) & (unsigned long)addr)
  1342. return -EINVAL;
  1343. area = find_vm_area(addr);
  1344. if (!area)
  1345. return -EINVAL;
  1346. if (!(area->flags & VM_USERMAP))
  1347. return -EINVAL;
  1348. if (usize + (pgoff << PAGE_SHIFT) > area->size - PAGE_SIZE)
  1349. return -EINVAL;
  1350. addr += pgoff << PAGE_SHIFT;
  1351. do {
  1352. struct page *page = vmalloc_to_page(addr);
  1353. int ret;
  1354. ret = vm_insert_page(vma, uaddr, page);
  1355. if (ret)
  1356. return ret;
  1357. uaddr += PAGE_SIZE;
  1358. addr += PAGE_SIZE;
  1359. usize -= PAGE_SIZE;
  1360. } while (usize > 0);
  1361. /* Prevent "things" like memory migration? VM_flags need a cleanup... */
  1362. vma->vm_flags |= VM_RESERVED;
  1363. return 0;
  1364. }
  1365. EXPORT_SYMBOL(remap_vmalloc_range);
  1366. /*
  1367. * Implement a stub for vmalloc_sync_all() if the architecture chose not to
  1368. * have one.
  1369. */
  1370. void __attribute__((weak)) vmalloc_sync_all(void)
  1371. {
  1372. }
  1373. static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
  1374. {
  1375. /* apply_to_page_range() does all the hard work. */
  1376. return 0;
  1377. }
  1378. /**
  1379. * alloc_vm_area - allocate a range of kernel address space
  1380. * @size: size of the area
  1381. *
  1382. * Returns: NULL on failure, vm_struct on success
  1383. *
  1384. * This function reserves a range of kernel address space, and
  1385. * allocates pagetables to map that range. No actual mappings
  1386. * are created. If the kernel address space is not shared
  1387. * between processes, it syncs the pagetable across all
  1388. * processes.
  1389. */
  1390. struct vm_struct *alloc_vm_area(size_t size)
  1391. {
  1392. struct vm_struct *area;
  1393. area = get_vm_area_caller(size, VM_IOREMAP,
  1394. __builtin_return_address(0));
  1395. if (area == NULL)
  1396. return NULL;
  1397. /*
  1398. * This ensures that page tables are constructed for this region
  1399. * of kernel virtual address space and mapped into init_mm.
  1400. */
  1401. if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
  1402. area->size, f, NULL)) {
  1403. free_vm_area(area);
  1404. return NULL;
  1405. }
  1406. /* Make sure the pagetables are constructed in process kernel
  1407. mappings */
  1408. vmalloc_sync_all();
  1409. return area;
  1410. }
  1411. EXPORT_SYMBOL_GPL(alloc_vm_area);
  1412. void free_vm_area(struct vm_struct *area)
  1413. {
  1414. struct vm_struct *ret;
  1415. ret = remove_vm_area(area->addr);
  1416. BUG_ON(ret != area);
  1417. kfree(area);
  1418. }
  1419. EXPORT_SYMBOL_GPL(free_vm_area);
  1420. #ifdef CONFIG_PROC_FS
  1421. static void *s_start(struct seq_file *m, loff_t *pos)
  1422. {
  1423. loff_t n = *pos;
  1424. struct vm_struct *v;
  1425. read_lock(&vmlist_lock);
  1426. v = vmlist;
  1427. while (n > 0 && v) {
  1428. n--;
  1429. v = v->next;
  1430. }
  1431. if (!n)
  1432. return v;
  1433. return NULL;
  1434. }
  1435. static void *s_next(struct seq_file *m, void *p, loff_t *pos)
  1436. {
  1437. struct vm_struct *v = p;
  1438. ++*pos;
  1439. return v->next;
  1440. }
  1441. static void s_stop(struct seq_file *m, void *p)
  1442. {
  1443. read_unlock(&vmlist_lock);
  1444. }
  1445. static void show_numa_info(struct seq_file *m, struct vm_struct *v)
  1446. {
  1447. if (NUMA_BUILD) {
  1448. unsigned int nr, *counters = m->private;
  1449. if (!counters)
  1450. return;
  1451. memset(counters, 0, nr_node_ids * sizeof(unsigned int));
  1452. for (nr = 0; nr < v->nr_pages; nr++)
  1453. counters[page_to_nid(v->pages[nr])]++;
  1454. for_each_node_state(nr, N_HIGH_MEMORY)
  1455. if (counters[nr])
  1456. seq_printf(m, " N%u=%u", nr, counters[nr]);
  1457. }
  1458. }
  1459. static int s_show(struct seq_file *m, void *p)
  1460. {
  1461. struct vm_struct *v = p;
  1462. seq_printf(m, "0x%p-0x%p %7ld",
  1463. v->addr, v->addr + v->size, v->size);
  1464. if (v->caller) {
  1465. char buff[KSYM_SYMBOL_LEN];
  1466. seq_putc(m, ' ');
  1467. sprint_symbol(buff, (unsigned long)v->caller);
  1468. seq_puts(m, buff);
  1469. }
  1470. if (v->nr_pages)
  1471. seq_printf(m, " pages=%d", v->nr_pages);
  1472. if (v->phys_addr)
  1473. seq_printf(m, " phys=%lx", v->phys_addr);
  1474. if (v->flags & VM_IOREMAP)
  1475. seq_printf(m, " ioremap");
  1476. if (v->flags & VM_ALLOC)
  1477. seq_printf(m, " vmalloc");
  1478. if (v->flags & VM_MAP)
  1479. seq_printf(m, " vmap");
  1480. if (v->flags & VM_USERMAP)
  1481. seq_printf(m, " user");
  1482. if (v->flags & VM_VPAGES)
  1483. seq_printf(m, " vpages");
  1484. show_numa_info(m, v);
  1485. seq_putc(m, '\n');
  1486. return 0;
  1487. }
  1488. static const struct seq_operations vmalloc_op = {
  1489. .start = s_start,
  1490. .next = s_next,
  1491. .stop = s_stop,
  1492. .show = s_show,
  1493. };
  1494. static int vmalloc_open(struct inode *inode, struct file *file)
  1495. {
  1496. unsigned int *ptr = NULL;
  1497. int ret;
  1498. if (NUMA_BUILD)
  1499. ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
  1500. ret = seq_open(file, &vmalloc_op);
  1501. if (!ret) {
  1502. struct seq_file *m = file->private_data;
  1503. m->private = ptr;
  1504. } else
  1505. kfree(ptr);
  1506. return ret;
  1507. }
  1508. static const struct file_operations proc_vmalloc_operations = {
  1509. .open = vmalloc_open,
  1510. .read = seq_read,
  1511. .llseek = seq_lseek,
  1512. .release = seq_release_private,
  1513. };
  1514. static int __init proc_vmalloc_init(void)
  1515. {
  1516. proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
  1517. return 0;
  1518. }
  1519. module_init(proc_vmalloc_init);
  1520. #endif