vmalloc.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760
  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. flush_cache_vunmap(addr, end);
  69. do {
  70. next = pgd_addr_end(addr, end);
  71. if (pgd_none_or_clear_bad(pgd))
  72. continue;
  73. vunmap_pud_range(pgd, addr, next);
  74. } while (pgd++, addr = next, addr != end);
  75. }
  76. static int vmap_pte_range(pmd_t *pmd, unsigned long addr,
  77. unsigned long end, pgprot_t prot, struct page **pages, int *nr)
  78. {
  79. pte_t *pte;
  80. /*
  81. * nr is a running index into the array which helps higher level
  82. * callers keep track of where we're up to.
  83. */
  84. pte = pte_alloc_kernel(pmd, addr);
  85. if (!pte)
  86. return -ENOMEM;
  87. do {
  88. struct page *page = pages[*nr];
  89. if (WARN_ON(!pte_none(*pte)))
  90. return -EBUSY;
  91. if (WARN_ON(!page))
  92. return -ENOMEM;
  93. set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
  94. (*nr)++;
  95. } while (pte++, addr += PAGE_SIZE, addr != end);
  96. return 0;
  97. }
  98. static int vmap_pmd_range(pud_t *pud, unsigned long addr,
  99. unsigned long end, pgprot_t prot, struct page **pages, int *nr)
  100. {
  101. pmd_t *pmd;
  102. unsigned long next;
  103. pmd = pmd_alloc(&init_mm, pud, addr);
  104. if (!pmd)
  105. return -ENOMEM;
  106. do {
  107. next = pmd_addr_end(addr, end);
  108. if (vmap_pte_range(pmd, addr, next, prot, pages, nr))
  109. return -ENOMEM;
  110. } while (pmd++, addr = next, addr != end);
  111. return 0;
  112. }
  113. static int vmap_pud_range(pgd_t *pgd, unsigned long addr,
  114. unsigned long end, pgprot_t prot, struct page **pages, int *nr)
  115. {
  116. pud_t *pud;
  117. unsigned long next;
  118. pud = pud_alloc(&init_mm, pgd, addr);
  119. if (!pud)
  120. return -ENOMEM;
  121. do {
  122. next = pud_addr_end(addr, end);
  123. if (vmap_pmd_range(pud, addr, next, prot, pages, nr))
  124. return -ENOMEM;
  125. } while (pud++, addr = next, addr != end);
  126. return 0;
  127. }
  128. /*
  129. * Set up page tables in kva (addr, end). The ptes shall have prot "prot", and
  130. * will have pfns corresponding to the "pages" array.
  131. *
  132. * Ie. pte at addr+N*PAGE_SIZE shall point to pfn corresponding to pages[N]
  133. */
  134. static int vmap_page_range(unsigned long addr, unsigned long end,
  135. pgprot_t prot, struct page **pages)
  136. {
  137. pgd_t *pgd;
  138. unsigned long next;
  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(addr, 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. * 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. addr = ALIGN(vstart, align);
  281. va = kmalloc_node(sizeof(struct vmap_area),
  282. gfp_mask & GFP_RECLAIM_MASK, node);
  283. if (unlikely(!va))
  284. return ERR_PTR(-ENOMEM);
  285. retry:
  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.
  452. */
  453. static void 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. * Free and unmap a vmap area
  460. */
  461. static void free_unmap_vmap_area(struct vmap_area *va)
  462. {
  463. va->flags |= VM_LAZY_FREE;
  464. atomic_add((va->va_end - va->va_start) >> PAGE_SHIFT, &vmap_lazy_nr);
  465. if (unlikely(atomic_read(&vmap_lazy_nr) > lazy_max_pages()))
  466. purge_vmap_area_lazy();
  467. }
  468. static struct vmap_area *find_vmap_area(unsigned long addr)
  469. {
  470. struct vmap_area *va;
  471. spin_lock(&vmap_area_lock);
  472. va = __find_vmap_area(addr);
  473. spin_unlock(&vmap_area_lock);
  474. return va;
  475. }
  476. static void free_unmap_vmap_area_addr(unsigned long addr)
  477. {
  478. struct vmap_area *va;
  479. va = find_vmap_area(addr);
  480. BUG_ON(!va);
  481. free_unmap_vmap_area(va);
  482. }
  483. /*** Per cpu kva allocator ***/
  484. /*
  485. * vmap space is limited especially on 32 bit architectures. Ensure there is
  486. * room for at least 16 percpu vmap blocks per CPU.
  487. */
  488. /*
  489. * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
  490. * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess
  491. * instead (we just need a rough idea)
  492. */
  493. #if BITS_PER_LONG == 32
  494. #define VMALLOC_SPACE (128UL*1024*1024)
  495. #else
  496. #define VMALLOC_SPACE (128UL*1024*1024*1024)
  497. #endif
  498. #define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE)
  499. #define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */
  500. #define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */
  501. #define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2)
  502. #define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */
  503. #define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */
  504. #define VMAP_BBMAP_BITS VMAP_MIN(VMAP_BBMAP_BITS_MAX, \
  505. VMAP_MAX(VMAP_BBMAP_BITS_MIN, \
  506. VMALLOC_PAGES / NR_CPUS / 16))
  507. #define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE)
  508. struct vmap_block_queue {
  509. spinlock_t lock;
  510. struct list_head free;
  511. struct list_head dirty;
  512. unsigned int nr_dirty;
  513. };
  514. struct vmap_block {
  515. spinlock_t lock;
  516. struct vmap_area *va;
  517. struct vmap_block_queue *vbq;
  518. unsigned long free, dirty;
  519. DECLARE_BITMAP(alloc_map, VMAP_BBMAP_BITS);
  520. DECLARE_BITMAP(dirty_map, VMAP_BBMAP_BITS);
  521. union {
  522. struct {
  523. struct list_head free_list;
  524. struct list_head dirty_list;
  525. };
  526. struct rcu_head rcu_head;
  527. };
  528. };
  529. /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
  530. static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
  531. /*
  532. * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block
  533. * in the free path. Could get rid of this if we change the API to return a
  534. * "cookie" from alloc, to be passed to free. But no big deal yet.
  535. */
  536. static DEFINE_SPINLOCK(vmap_block_tree_lock);
  537. static RADIX_TREE(vmap_block_tree, GFP_ATOMIC);
  538. /*
  539. * We should probably have a fallback mechanism to allocate virtual memory
  540. * out of partially filled vmap blocks. However vmap block sizing should be
  541. * fairly reasonable according to the vmalloc size, so it shouldn't be a
  542. * big problem.
  543. */
  544. static unsigned long addr_to_vb_idx(unsigned long addr)
  545. {
  546. addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
  547. addr /= VMAP_BLOCK_SIZE;
  548. return addr;
  549. }
  550. static struct vmap_block *new_vmap_block(gfp_t gfp_mask)
  551. {
  552. struct vmap_block_queue *vbq;
  553. struct vmap_block *vb;
  554. struct vmap_area *va;
  555. unsigned long vb_idx;
  556. int node, err;
  557. node = numa_node_id();
  558. vb = kmalloc_node(sizeof(struct vmap_block),
  559. gfp_mask & GFP_RECLAIM_MASK, node);
  560. if (unlikely(!vb))
  561. return ERR_PTR(-ENOMEM);
  562. va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
  563. VMALLOC_START, VMALLOC_END,
  564. node, gfp_mask);
  565. if (unlikely(IS_ERR(va))) {
  566. kfree(vb);
  567. return ERR_PTR(PTR_ERR(va));
  568. }
  569. err = radix_tree_preload(gfp_mask);
  570. if (unlikely(err)) {
  571. kfree(vb);
  572. free_vmap_area(va);
  573. return ERR_PTR(err);
  574. }
  575. spin_lock_init(&vb->lock);
  576. vb->va = va;
  577. vb->free = VMAP_BBMAP_BITS;
  578. vb->dirty = 0;
  579. bitmap_zero(vb->alloc_map, VMAP_BBMAP_BITS);
  580. bitmap_zero(vb->dirty_map, VMAP_BBMAP_BITS);
  581. INIT_LIST_HEAD(&vb->free_list);
  582. INIT_LIST_HEAD(&vb->dirty_list);
  583. vb_idx = addr_to_vb_idx(va->va_start);
  584. spin_lock(&vmap_block_tree_lock);
  585. err = radix_tree_insert(&vmap_block_tree, vb_idx, vb);
  586. spin_unlock(&vmap_block_tree_lock);
  587. BUG_ON(err);
  588. radix_tree_preload_end();
  589. vbq = &get_cpu_var(vmap_block_queue);
  590. vb->vbq = vbq;
  591. spin_lock(&vbq->lock);
  592. list_add(&vb->free_list, &vbq->free);
  593. spin_unlock(&vbq->lock);
  594. put_cpu_var(vmap_cpu_blocks);
  595. return vb;
  596. }
  597. static void rcu_free_vb(struct rcu_head *head)
  598. {
  599. struct vmap_block *vb = container_of(head, struct vmap_block, rcu_head);
  600. kfree(vb);
  601. }
  602. static void free_vmap_block(struct vmap_block *vb)
  603. {
  604. struct vmap_block *tmp;
  605. unsigned long vb_idx;
  606. spin_lock(&vb->vbq->lock);
  607. if (!list_empty(&vb->free_list))
  608. list_del(&vb->free_list);
  609. if (!list_empty(&vb->dirty_list))
  610. list_del(&vb->dirty_list);
  611. spin_unlock(&vb->vbq->lock);
  612. vb_idx = addr_to_vb_idx(vb->va->va_start);
  613. spin_lock(&vmap_block_tree_lock);
  614. tmp = radix_tree_delete(&vmap_block_tree, vb_idx);
  615. spin_unlock(&vmap_block_tree_lock);
  616. BUG_ON(tmp != vb);
  617. free_unmap_vmap_area(vb->va);
  618. call_rcu(&vb->rcu_head, rcu_free_vb);
  619. }
  620. static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
  621. {
  622. struct vmap_block_queue *vbq;
  623. struct vmap_block *vb;
  624. unsigned long addr = 0;
  625. unsigned int order;
  626. BUG_ON(size & ~PAGE_MASK);
  627. BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
  628. order = get_order(size);
  629. again:
  630. rcu_read_lock();
  631. vbq = &get_cpu_var(vmap_block_queue);
  632. list_for_each_entry_rcu(vb, &vbq->free, free_list) {
  633. int i;
  634. spin_lock(&vb->lock);
  635. i = bitmap_find_free_region(vb->alloc_map,
  636. VMAP_BBMAP_BITS, order);
  637. if (i >= 0) {
  638. addr = vb->va->va_start + (i << PAGE_SHIFT);
  639. BUG_ON(addr_to_vb_idx(addr) !=
  640. addr_to_vb_idx(vb->va->va_start));
  641. vb->free -= 1UL << order;
  642. if (vb->free == 0) {
  643. spin_lock(&vbq->lock);
  644. list_del_init(&vb->free_list);
  645. spin_unlock(&vbq->lock);
  646. }
  647. spin_unlock(&vb->lock);
  648. break;
  649. }
  650. spin_unlock(&vb->lock);
  651. }
  652. put_cpu_var(vmap_cpu_blocks);
  653. rcu_read_unlock();
  654. if (!addr) {
  655. vb = new_vmap_block(gfp_mask);
  656. if (IS_ERR(vb))
  657. return vb;
  658. goto again;
  659. }
  660. return (void *)addr;
  661. }
  662. static void vb_free(const void *addr, unsigned long size)
  663. {
  664. unsigned long offset;
  665. unsigned long vb_idx;
  666. unsigned int order;
  667. struct vmap_block *vb;
  668. BUG_ON(size & ~PAGE_MASK);
  669. BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
  670. order = get_order(size);
  671. offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1);
  672. vb_idx = addr_to_vb_idx((unsigned long)addr);
  673. rcu_read_lock();
  674. vb = radix_tree_lookup(&vmap_block_tree, vb_idx);
  675. rcu_read_unlock();
  676. BUG_ON(!vb);
  677. spin_lock(&vb->lock);
  678. bitmap_allocate_region(vb->dirty_map, offset >> PAGE_SHIFT, order);
  679. if (!vb->dirty) {
  680. spin_lock(&vb->vbq->lock);
  681. list_add(&vb->dirty_list, &vb->vbq->dirty);
  682. spin_unlock(&vb->vbq->lock);
  683. }
  684. vb->dirty += 1UL << order;
  685. if (vb->dirty == VMAP_BBMAP_BITS) {
  686. BUG_ON(vb->free || !list_empty(&vb->free_list));
  687. spin_unlock(&vb->lock);
  688. free_vmap_block(vb);
  689. } else
  690. spin_unlock(&vb->lock);
  691. }
  692. /**
  693. * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
  694. *
  695. * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
  696. * to amortize TLB flushing overheads. What this means is that any page you
  697. * have now, may, in a former life, have been mapped into kernel virtual
  698. * address by the vmap layer and so there might be some CPUs with TLB entries
  699. * still referencing that page (additional to the regular 1:1 kernel mapping).
  700. *
  701. * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
  702. * be sure that none of the pages we have control over will have any aliases
  703. * from the vmap layer.
  704. */
  705. void vm_unmap_aliases(void)
  706. {
  707. unsigned long start = ULONG_MAX, end = 0;
  708. int cpu;
  709. int flush = 0;
  710. for_each_possible_cpu(cpu) {
  711. struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
  712. struct vmap_block *vb;
  713. rcu_read_lock();
  714. list_for_each_entry_rcu(vb, &vbq->free, free_list) {
  715. int i;
  716. spin_lock(&vb->lock);
  717. i = find_first_bit(vb->dirty_map, VMAP_BBMAP_BITS);
  718. while (i < VMAP_BBMAP_BITS) {
  719. unsigned long s, e;
  720. int j;
  721. j = find_next_zero_bit(vb->dirty_map,
  722. VMAP_BBMAP_BITS, i);
  723. s = vb->va->va_start + (i << PAGE_SHIFT);
  724. e = vb->va->va_start + (j << PAGE_SHIFT);
  725. vunmap_page_range(s, e);
  726. flush = 1;
  727. if (s < start)
  728. start = s;
  729. if (e > end)
  730. end = e;
  731. i = j;
  732. i = find_next_bit(vb->dirty_map,
  733. VMAP_BBMAP_BITS, i);
  734. }
  735. spin_unlock(&vb->lock);
  736. }
  737. rcu_read_unlock();
  738. }
  739. __purge_vmap_area_lazy(&start, &end, 1, flush);
  740. }
  741. EXPORT_SYMBOL_GPL(vm_unmap_aliases);
  742. /**
  743. * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
  744. * @mem: the pointer returned by vm_map_ram
  745. * @count: the count passed to that vm_map_ram call (cannot unmap partial)
  746. */
  747. void vm_unmap_ram(const void *mem, unsigned int count)
  748. {
  749. unsigned long size = count << PAGE_SHIFT;
  750. unsigned long addr = (unsigned long)mem;
  751. BUG_ON(!addr);
  752. BUG_ON(addr < VMALLOC_START);
  753. BUG_ON(addr > VMALLOC_END);
  754. BUG_ON(addr & (PAGE_SIZE-1));
  755. debug_check_no_locks_freed(mem, size);
  756. if (likely(count <= VMAP_MAX_ALLOC))
  757. vb_free(mem, size);
  758. else
  759. free_unmap_vmap_area_addr(addr);
  760. }
  761. EXPORT_SYMBOL(vm_unmap_ram);
  762. /**
  763. * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
  764. * @pages: an array of pointers to the pages to be mapped
  765. * @count: number of pages
  766. * @node: prefer to allocate data structures on this node
  767. * @prot: memory protection to use. PAGE_KERNEL for regular RAM
  768. *
  769. * Returns: a pointer to the address that has been mapped, or %NULL on failure
  770. */
  771. void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
  772. {
  773. unsigned long size = count << PAGE_SHIFT;
  774. unsigned long addr;
  775. void *mem;
  776. if (likely(count <= VMAP_MAX_ALLOC)) {
  777. mem = vb_alloc(size, GFP_KERNEL);
  778. if (IS_ERR(mem))
  779. return NULL;
  780. addr = (unsigned long)mem;
  781. } else {
  782. struct vmap_area *va;
  783. va = alloc_vmap_area(size, PAGE_SIZE,
  784. VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
  785. if (IS_ERR(va))
  786. return NULL;
  787. addr = va->va_start;
  788. mem = (void *)addr;
  789. }
  790. if (vmap_page_range(addr, addr + size, prot, pages) < 0) {
  791. vm_unmap_ram(mem, count);
  792. return NULL;
  793. }
  794. return mem;
  795. }
  796. EXPORT_SYMBOL(vm_map_ram);
  797. void __init vmalloc_init(void)
  798. {
  799. int i;
  800. for_each_possible_cpu(i) {
  801. struct vmap_block_queue *vbq;
  802. vbq = &per_cpu(vmap_block_queue, i);
  803. spin_lock_init(&vbq->lock);
  804. INIT_LIST_HEAD(&vbq->free);
  805. INIT_LIST_HEAD(&vbq->dirty);
  806. vbq->nr_dirty = 0;
  807. }
  808. }
  809. void unmap_kernel_range(unsigned long addr, unsigned long size)
  810. {
  811. unsigned long end = addr + size;
  812. vunmap_page_range(addr, end);
  813. flush_tlb_kernel_range(addr, end);
  814. }
  815. int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page ***pages)
  816. {
  817. unsigned long addr = (unsigned long)area->addr;
  818. unsigned long end = addr + area->size - PAGE_SIZE;
  819. int err;
  820. err = vmap_page_range(addr, end, prot, *pages);
  821. if (err > 0) {
  822. *pages += err;
  823. err = 0;
  824. }
  825. return err;
  826. }
  827. EXPORT_SYMBOL_GPL(map_vm_area);
  828. /*** Old vmalloc interfaces ***/
  829. DEFINE_RWLOCK(vmlist_lock);
  830. struct vm_struct *vmlist;
  831. static struct vm_struct *__get_vm_area_node(unsigned long size,
  832. unsigned long flags, unsigned long start, unsigned long end,
  833. int node, gfp_t gfp_mask, void *caller)
  834. {
  835. static struct vmap_area *va;
  836. struct vm_struct *area;
  837. struct vm_struct *tmp, **p;
  838. unsigned long align = 1;
  839. BUG_ON(in_interrupt());
  840. if (flags & VM_IOREMAP) {
  841. int bit = fls(size);
  842. if (bit > IOREMAP_MAX_ORDER)
  843. bit = IOREMAP_MAX_ORDER;
  844. else if (bit < PAGE_SHIFT)
  845. bit = PAGE_SHIFT;
  846. align = 1ul << bit;
  847. }
  848. size = PAGE_ALIGN(size);
  849. if (unlikely(!size))
  850. return NULL;
  851. area = kmalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
  852. if (unlikely(!area))
  853. return NULL;
  854. /*
  855. * We always allocate a guard page.
  856. */
  857. size += PAGE_SIZE;
  858. va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
  859. if (IS_ERR(va)) {
  860. kfree(area);
  861. return NULL;
  862. }
  863. area->flags = flags;
  864. area->addr = (void *)va->va_start;
  865. area->size = size;
  866. area->pages = NULL;
  867. area->nr_pages = 0;
  868. area->phys_addr = 0;
  869. area->caller = caller;
  870. va->private = area;
  871. va->flags |= VM_VM_AREA;
  872. write_lock(&vmlist_lock);
  873. for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
  874. if (tmp->addr >= area->addr)
  875. break;
  876. }
  877. area->next = *p;
  878. *p = area;
  879. write_unlock(&vmlist_lock);
  880. return area;
  881. }
  882. struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
  883. unsigned long start, unsigned long end)
  884. {
  885. return __get_vm_area_node(size, flags, start, end, -1, GFP_KERNEL,
  886. __builtin_return_address(0));
  887. }
  888. EXPORT_SYMBOL_GPL(__get_vm_area);
  889. /**
  890. * get_vm_area - reserve a contiguous kernel virtual area
  891. * @size: size of the area
  892. * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC
  893. *
  894. * Search an area of @size in the kernel virtual mapping area,
  895. * and reserved it for out purposes. Returns the area descriptor
  896. * on success or %NULL on failure.
  897. */
  898. struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
  899. {
  900. return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
  901. -1, GFP_KERNEL, __builtin_return_address(0));
  902. }
  903. struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
  904. void *caller)
  905. {
  906. return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
  907. -1, GFP_KERNEL, caller);
  908. }
  909. struct vm_struct *get_vm_area_node(unsigned long size, unsigned long flags,
  910. int node, gfp_t gfp_mask)
  911. {
  912. return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END, node,
  913. gfp_mask, __builtin_return_address(0));
  914. }
  915. static struct vm_struct *find_vm_area(const void *addr)
  916. {
  917. struct vmap_area *va;
  918. va = find_vmap_area((unsigned long)addr);
  919. if (va && va->flags & VM_VM_AREA)
  920. return va->private;
  921. return NULL;
  922. }
  923. /**
  924. * remove_vm_area - find and remove a continuous kernel virtual area
  925. * @addr: base address
  926. *
  927. * Search for the kernel VM area starting at @addr, and remove it.
  928. * This function returns the found VM area, but using it is NOT safe
  929. * on SMP machines, except for its size or flags.
  930. */
  931. struct vm_struct *remove_vm_area(const void *addr)
  932. {
  933. struct vmap_area *va;
  934. va = find_vmap_area((unsigned long)addr);
  935. if (va && va->flags & VM_VM_AREA) {
  936. struct vm_struct *vm = va->private;
  937. struct vm_struct *tmp, **p;
  938. free_unmap_vmap_area(va);
  939. vm->size -= PAGE_SIZE;
  940. write_lock(&vmlist_lock);
  941. for (p = &vmlist; (tmp = *p) != vm; p = &tmp->next)
  942. ;
  943. *p = tmp->next;
  944. write_unlock(&vmlist_lock);
  945. return vm;
  946. }
  947. return NULL;
  948. }
  949. static void __vunmap(const void *addr, int deallocate_pages)
  950. {
  951. struct vm_struct *area;
  952. if (!addr)
  953. return;
  954. if ((PAGE_SIZE-1) & (unsigned long)addr) {
  955. WARN(1, KERN_ERR "Trying to vfree() bad address (%p)\n", addr);
  956. return;
  957. }
  958. area = remove_vm_area(addr);
  959. if (unlikely(!area)) {
  960. WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
  961. addr);
  962. return;
  963. }
  964. debug_check_no_locks_freed(addr, area->size);
  965. debug_check_no_obj_freed(addr, area->size);
  966. if (deallocate_pages) {
  967. int i;
  968. for (i = 0; i < area->nr_pages; i++) {
  969. struct page *page = area->pages[i];
  970. BUG_ON(!page);
  971. __free_page(page);
  972. }
  973. if (area->flags & VM_VPAGES)
  974. vfree(area->pages);
  975. else
  976. kfree(area->pages);
  977. }
  978. kfree(area);
  979. return;
  980. }
  981. /**
  982. * vfree - release memory allocated by vmalloc()
  983. * @addr: memory base address
  984. *
  985. * Free the virtually continuous memory area starting at @addr, as
  986. * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
  987. * NULL, no operation is performed.
  988. *
  989. * Must not be called in interrupt context.
  990. */
  991. void vfree(const void *addr)
  992. {
  993. BUG_ON(in_interrupt());
  994. __vunmap(addr, 1);
  995. }
  996. EXPORT_SYMBOL(vfree);
  997. /**
  998. * vunmap - release virtual mapping obtained by vmap()
  999. * @addr: memory base address
  1000. *
  1001. * Free the virtually contiguous memory area starting at @addr,
  1002. * which was created from the page array passed to vmap().
  1003. *
  1004. * Must not be called in interrupt context.
  1005. */
  1006. void vunmap(const void *addr)
  1007. {
  1008. BUG_ON(in_interrupt());
  1009. __vunmap(addr, 0);
  1010. }
  1011. EXPORT_SYMBOL(vunmap);
  1012. /**
  1013. * vmap - map an array of pages into virtually contiguous space
  1014. * @pages: array of page pointers
  1015. * @count: number of pages to map
  1016. * @flags: vm_area->flags
  1017. * @prot: page protection for the mapping
  1018. *
  1019. * Maps @count pages from @pages into contiguous kernel virtual
  1020. * space.
  1021. */
  1022. void *vmap(struct page **pages, unsigned int count,
  1023. unsigned long flags, pgprot_t prot)
  1024. {
  1025. struct vm_struct *area;
  1026. if (count > num_physpages)
  1027. return NULL;
  1028. area = get_vm_area_caller((count << PAGE_SHIFT), flags,
  1029. __builtin_return_address(0));
  1030. if (!area)
  1031. return NULL;
  1032. if (map_vm_area(area, prot, &pages)) {
  1033. vunmap(area->addr);
  1034. return NULL;
  1035. }
  1036. return area->addr;
  1037. }
  1038. EXPORT_SYMBOL(vmap);
  1039. static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
  1040. int node, void *caller);
  1041. static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
  1042. pgprot_t prot, int node, void *caller)
  1043. {
  1044. struct page **pages;
  1045. unsigned int nr_pages, array_size, i;
  1046. nr_pages = (area->size - PAGE_SIZE) >> PAGE_SHIFT;
  1047. array_size = (nr_pages * sizeof(struct page *));
  1048. area->nr_pages = nr_pages;
  1049. /* Please note that the recursion is strictly bounded. */
  1050. if (array_size > PAGE_SIZE) {
  1051. pages = __vmalloc_node(array_size, gfp_mask | __GFP_ZERO,
  1052. PAGE_KERNEL, node, caller);
  1053. area->flags |= VM_VPAGES;
  1054. } else {
  1055. pages = kmalloc_node(array_size,
  1056. (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO,
  1057. node);
  1058. }
  1059. area->pages = pages;
  1060. area->caller = caller;
  1061. if (!area->pages) {
  1062. remove_vm_area(area->addr);
  1063. kfree(area);
  1064. return NULL;
  1065. }
  1066. for (i = 0; i < area->nr_pages; i++) {
  1067. struct page *page;
  1068. if (node < 0)
  1069. page = alloc_page(gfp_mask);
  1070. else
  1071. page = alloc_pages_node(node, gfp_mask, 0);
  1072. if (unlikely(!page)) {
  1073. /* Successfully allocated i pages, free them in __vunmap() */
  1074. area->nr_pages = i;
  1075. goto fail;
  1076. }
  1077. area->pages[i] = page;
  1078. }
  1079. if (map_vm_area(area, prot, &pages))
  1080. goto fail;
  1081. return area->addr;
  1082. fail:
  1083. vfree(area->addr);
  1084. return NULL;
  1085. }
  1086. void *__vmalloc_area(struct vm_struct *area, gfp_t gfp_mask, pgprot_t prot)
  1087. {
  1088. return __vmalloc_area_node(area, gfp_mask, prot, -1,
  1089. __builtin_return_address(0));
  1090. }
  1091. /**
  1092. * __vmalloc_node - allocate virtually contiguous memory
  1093. * @size: allocation size
  1094. * @gfp_mask: flags for the page level allocator
  1095. * @prot: protection mask for the allocated pages
  1096. * @node: node to use for allocation or -1
  1097. * @caller: caller's return address
  1098. *
  1099. * Allocate enough pages to cover @size from the page level
  1100. * allocator with @gfp_mask flags. Map them into contiguous
  1101. * kernel virtual space, using a pagetable protection of @prot.
  1102. */
  1103. static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
  1104. int node, void *caller)
  1105. {
  1106. struct vm_struct *area;
  1107. size = PAGE_ALIGN(size);
  1108. if (!size || (size >> PAGE_SHIFT) > num_physpages)
  1109. return NULL;
  1110. area = __get_vm_area_node(size, VM_ALLOC, VMALLOC_START, VMALLOC_END,
  1111. node, gfp_mask, caller);
  1112. if (!area)
  1113. return NULL;
  1114. return __vmalloc_area_node(area, gfp_mask, prot, node, caller);
  1115. }
  1116. void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
  1117. {
  1118. return __vmalloc_node(size, gfp_mask, prot, -1,
  1119. __builtin_return_address(0));
  1120. }
  1121. EXPORT_SYMBOL(__vmalloc);
  1122. /**
  1123. * vmalloc - allocate virtually contiguous memory
  1124. * @size: allocation size
  1125. * Allocate enough pages to cover @size from the page level
  1126. * allocator and map them into contiguous kernel virtual space.
  1127. *
  1128. * For tight control over page level allocator and protection flags
  1129. * use __vmalloc() instead.
  1130. */
  1131. void *vmalloc(unsigned long size)
  1132. {
  1133. return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
  1134. -1, __builtin_return_address(0));
  1135. }
  1136. EXPORT_SYMBOL(vmalloc);
  1137. /**
  1138. * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
  1139. * @size: allocation size
  1140. *
  1141. * The resulting memory area is zeroed so it can be mapped to userspace
  1142. * without leaking data.
  1143. */
  1144. void *vmalloc_user(unsigned long size)
  1145. {
  1146. struct vm_struct *area;
  1147. void *ret;
  1148. ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO, PAGE_KERNEL);
  1149. if (ret) {
  1150. area = find_vm_area(ret);
  1151. area->flags |= VM_USERMAP;
  1152. }
  1153. return ret;
  1154. }
  1155. EXPORT_SYMBOL(vmalloc_user);
  1156. /**
  1157. * vmalloc_node - allocate memory on a specific node
  1158. * @size: allocation size
  1159. * @node: numa node
  1160. *
  1161. * Allocate enough pages to cover @size from the page level
  1162. * allocator and map them into contiguous kernel virtual space.
  1163. *
  1164. * For tight control over page level allocator and protection flags
  1165. * use __vmalloc() instead.
  1166. */
  1167. void *vmalloc_node(unsigned long size, int node)
  1168. {
  1169. return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
  1170. node, __builtin_return_address(0));
  1171. }
  1172. EXPORT_SYMBOL(vmalloc_node);
  1173. #ifndef PAGE_KERNEL_EXEC
  1174. # define PAGE_KERNEL_EXEC PAGE_KERNEL
  1175. #endif
  1176. /**
  1177. * vmalloc_exec - allocate virtually contiguous, executable memory
  1178. * @size: allocation size
  1179. *
  1180. * Kernel-internal function to allocate enough pages to cover @size
  1181. * the page level allocator and map them into contiguous and
  1182. * executable kernel virtual space.
  1183. *
  1184. * For tight control over page level allocator and protection flags
  1185. * use __vmalloc() instead.
  1186. */
  1187. void *vmalloc_exec(unsigned long size)
  1188. {
  1189. return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
  1190. }
  1191. #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
  1192. #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
  1193. #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
  1194. #define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL
  1195. #else
  1196. #define GFP_VMALLOC32 GFP_KERNEL
  1197. #endif
  1198. /**
  1199. * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
  1200. * @size: allocation size
  1201. *
  1202. * Allocate enough 32bit PA addressable pages to cover @size from the
  1203. * page level allocator and map them into contiguous kernel virtual space.
  1204. */
  1205. void *vmalloc_32(unsigned long size)
  1206. {
  1207. return __vmalloc(size, GFP_VMALLOC32, PAGE_KERNEL);
  1208. }
  1209. EXPORT_SYMBOL(vmalloc_32);
  1210. /**
  1211. * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
  1212. * @size: allocation size
  1213. *
  1214. * The resulting memory area is 32bit addressable and zeroed so it can be
  1215. * mapped to userspace without leaking data.
  1216. */
  1217. void *vmalloc_32_user(unsigned long size)
  1218. {
  1219. struct vm_struct *area;
  1220. void *ret;
  1221. ret = __vmalloc(size, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL);
  1222. if (ret) {
  1223. area = find_vm_area(ret);
  1224. area->flags |= VM_USERMAP;
  1225. }
  1226. return ret;
  1227. }
  1228. EXPORT_SYMBOL(vmalloc_32_user);
  1229. long vread(char *buf, char *addr, unsigned long count)
  1230. {
  1231. struct vm_struct *tmp;
  1232. char *vaddr, *buf_start = buf;
  1233. unsigned long n;
  1234. /* Don't allow overflow */
  1235. if ((unsigned long) addr + count < count)
  1236. count = -(unsigned long) addr;
  1237. read_lock(&vmlist_lock);
  1238. for (tmp = vmlist; tmp; tmp = tmp->next) {
  1239. vaddr = (char *) tmp->addr;
  1240. if (addr >= vaddr + tmp->size - PAGE_SIZE)
  1241. continue;
  1242. while (addr < vaddr) {
  1243. if (count == 0)
  1244. goto finished;
  1245. *buf = '\0';
  1246. buf++;
  1247. addr++;
  1248. count--;
  1249. }
  1250. n = vaddr + tmp->size - PAGE_SIZE - addr;
  1251. do {
  1252. if (count == 0)
  1253. goto finished;
  1254. *buf = *addr;
  1255. buf++;
  1256. addr++;
  1257. count--;
  1258. } while (--n > 0);
  1259. }
  1260. finished:
  1261. read_unlock(&vmlist_lock);
  1262. return buf - buf_start;
  1263. }
  1264. long vwrite(char *buf, char *addr, unsigned long count)
  1265. {
  1266. struct vm_struct *tmp;
  1267. char *vaddr, *buf_start = buf;
  1268. unsigned long n;
  1269. /* Don't allow overflow */
  1270. if ((unsigned long) addr + count < count)
  1271. count = -(unsigned long) addr;
  1272. read_lock(&vmlist_lock);
  1273. for (tmp = vmlist; tmp; tmp = tmp->next) {
  1274. vaddr = (char *) tmp->addr;
  1275. if (addr >= vaddr + tmp->size - PAGE_SIZE)
  1276. continue;
  1277. while (addr < vaddr) {
  1278. if (count == 0)
  1279. goto finished;
  1280. buf++;
  1281. addr++;
  1282. count--;
  1283. }
  1284. n = vaddr + tmp->size - PAGE_SIZE - addr;
  1285. do {
  1286. if (count == 0)
  1287. goto finished;
  1288. *addr = *buf;
  1289. buf++;
  1290. addr++;
  1291. count--;
  1292. } while (--n > 0);
  1293. }
  1294. finished:
  1295. read_unlock(&vmlist_lock);
  1296. return buf - buf_start;
  1297. }
  1298. /**
  1299. * remap_vmalloc_range - map vmalloc pages to userspace
  1300. * @vma: vma to cover (map full range of vma)
  1301. * @addr: vmalloc memory
  1302. * @pgoff: number of pages into addr before first page to map
  1303. *
  1304. * Returns: 0 for success, -Exxx on failure
  1305. *
  1306. * This function checks that addr is a valid vmalloc'ed area, and
  1307. * that it is big enough to cover the vma. Will return failure if
  1308. * that criteria isn't met.
  1309. *
  1310. * Similar to remap_pfn_range() (see mm/memory.c)
  1311. */
  1312. int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
  1313. unsigned long pgoff)
  1314. {
  1315. struct vm_struct *area;
  1316. unsigned long uaddr = vma->vm_start;
  1317. unsigned long usize = vma->vm_end - vma->vm_start;
  1318. if ((PAGE_SIZE-1) & (unsigned long)addr)
  1319. return -EINVAL;
  1320. area = find_vm_area(addr);
  1321. if (!area)
  1322. return -EINVAL;
  1323. if (!(area->flags & VM_USERMAP))
  1324. return -EINVAL;
  1325. if (usize + (pgoff << PAGE_SHIFT) > area->size - PAGE_SIZE)
  1326. return -EINVAL;
  1327. addr += pgoff << PAGE_SHIFT;
  1328. do {
  1329. struct page *page = vmalloc_to_page(addr);
  1330. int ret;
  1331. ret = vm_insert_page(vma, uaddr, page);
  1332. if (ret)
  1333. return ret;
  1334. uaddr += PAGE_SIZE;
  1335. addr += PAGE_SIZE;
  1336. usize -= PAGE_SIZE;
  1337. } while (usize > 0);
  1338. /* Prevent "things" like memory migration? VM_flags need a cleanup... */
  1339. vma->vm_flags |= VM_RESERVED;
  1340. return 0;
  1341. }
  1342. EXPORT_SYMBOL(remap_vmalloc_range);
  1343. /*
  1344. * Implement a stub for vmalloc_sync_all() if the architecture chose not to
  1345. * have one.
  1346. */
  1347. void __attribute__((weak)) vmalloc_sync_all(void)
  1348. {
  1349. }
  1350. static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
  1351. {
  1352. /* apply_to_page_range() does all the hard work. */
  1353. return 0;
  1354. }
  1355. /**
  1356. * alloc_vm_area - allocate a range of kernel address space
  1357. * @size: size of the area
  1358. *
  1359. * Returns: NULL on failure, vm_struct on success
  1360. *
  1361. * This function reserves a range of kernel address space, and
  1362. * allocates pagetables to map that range. No actual mappings
  1363. * are created. If the kernel address space is not shared
  1364. * between processes, it syncs the pagetable across all
  1365. * processes.
  1366. */
  1367. struct vm_struct *alloc_vm_area(size_t size)
  1368. {
  1369. struct vm_struct *area;
  1370. area = get_vm_area_caller(size, VM_IOREMAP,
  1371. __builtin_return_address(0));
  1372. if (area == NULL)
  1373. return NULL;
  1374. /*
  1375. * This ensures that page tables are constructed for this region
  1376. * of kernel virtual address space and mapped into init_mm.
  1377. */
  1378. if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
  1379. area->size, f, NULL)) {
  1380. free_vm_area(area);
  1381. return NULL;
  1382. }
  1383. /* Make sure the pagetables are constructed in process kernel
  1384. mappings */
  1385. vmalloc_sync_all();
  1386. return area;
  1387. }
  1388. EXPORT_SYMBOL_GPL(alloc_vm_area);
  1389. void free_vm_area(struct vm_struct *area)
  1390. {
  1391. struct vm_struct *ret;
  1392. ret = remove_vm_area(area->addr);
  1393. BUG_ON(ret != area);
  1394. kfree(area);
  1395. }
  1396. EXPORT_SYMBOL_GPL(free_vm_area);
  1397. #ifdef CONFIG_PROC_FS
  1398. static void *s_start(struct seq_file *m, loff_t *pos)
  1399. {
  1400. loff_t n = *pos;
  1401. struct vm_struct *v;
  1402. read_lock(&vmlist_lock);
  1403. v = vmlist;
  1404. while (n > 0 && v) {
  1405. n--;
  1406. v = v->next;
  1407. }
  1408. if (!n)
  1409. return v;
  1410. return NULL;
  1411. }
  1412. static void *s_next(struct seq_file *m, void *p, loff_t *pos)
  1413. {
  1414. struct vm_struct *v = p;
  1415. ++*pos;
  1416. return v->next;
  1417. }
  1418. static void s_stop(struct seq_file *m, void *p)
  1419. {
  1420. read_unlock(&vmlist_lock);
  1421. }
  1422. static void show_numa_info(struct seq_file *m, struct vm_struct *v)
  1423. {
  1424. if (NUMA_BUILD) {
  1425. unsigned int nr, *counters = m->private;
  1426. if (!counters)
  1427. return;
  1428. memset(counters, 0, nr_node_ids * sizeof(unsigned int));
  1429. for (nr = 0; nr < v->nr_pages; nr++)
  1430. counters[page_to_nid(v->pages[nr])]++;
  1431. for_each_node_state(nr, N_HIGH_MEMORY)
  1432. if (counters[nr])
  1433. seq_printf(m, " N%u=%u", nr, counters[nr]);
  1434. }
  1435. }
  1436. static int s_show(struct seq_file *m, void *p)
  1437. {
  1438. struct vm_struct *v = p;
  1439. seq_printf(m, "0x%p-0x%p %7ld",
  1440. v->addr, v->addr + v->size, v->size);
  1441. if (v->caller) {
  1442. char buff[2 * KSYM_NAME_LEN];
  1443. seq_putc(m, ' ');
  1444. sprint_symbol(buff, (unsigned long)v->caller);
  1445. seq_puts(m, buff);
  1446. }
  1447. if (v->nr_pages)
  1448. seq_printf(m, " pages=%d", v->nr_pages);
  1449. if (v->phys_addr)
  1450. seq_printf(m, " phys=%lx", v->phys_addr);
  1451. if (v->flags & VM_IOREMAP)
  1452. seq_printf(m, " ioremap");
  1453. if (v->flags & VM_ALLOC)
  1454. seq_printf(m, " vmalloc");
  1455. if (v->flags & VM_MAP)
  1456. seq_printf(m, " vmap");
  1457. if (v->flags & VM_USERMAP)
  1458. seq_printf(m, " user");
  1459. if (v->flags & VM_VPAGES)
  1460. seq_printf(m, " vpages");
  1461. show_numa_info(m, v);
  1462. seq_putc(m, '\n');
  1463. return 0;
  1464. }
  1465. static const struct seq_operations vmalloc_op = {
  1466. .start = s_start,
  1467. .next = s_next,
  1468. .stop = s_stop,
  1469. .show = s_show,
  1470. };
  1471. static int vmalloc_open(struct inode *inode, struct file *file)
  1472. {
  1473. unsigned int *ptr = NULL;
  1474. int ret;
  1475. if (NUMA_BUILD)
  1476. ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
  1477. ret = seq_open(file, &vmalloc_op);
  1478. if (!ret) {
  1479. struct seq_file *m = file->private_data;
  1480. m->private = ptr;
  1481. } else
  1482. kfree(ptr);
  1483. return ret;
  1484. }
  1485. static const struct file_operations proc_vmalloc_operations = {
  1486. .open = vmalloc_open,
  1487. .read = seq_read,
  1488. .llseek = seq_lseek,
  1489. .release = seq_release_private,
  1490. };
  1491. static int __init proc_vmalloc_init(void)
  1492. {
  1493. proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
  1494. return 0;
  1495. }
  1496. module_init(proc_vmalloc_init);
  1497. #endif