vmalloc.c 42 KB

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