vmalloc.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247
  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 <linux/pfn.h>
  26. #include <linux/kmemleak.h>
  27. #include <asm/atomic.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/tlbflush.h>
  30. /*** Page table manipulation functions ***/
  31. static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end)
  32. {
  33. pte_t *pte;
  34. pte = pte_offset_kernel(pmd, addr);
  35. do {
  36. pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte);
  37. WARN_ON(!pte_none(ptent) && !pte_present(ptent));
  38. } while (pte++, addr += PAGE_SIZE, addr != end);
  39. }
  40. static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end)
  41. {
  42. pmd_t *pmd;
  43. unsigned long next;
  44. pmd = pmd_offset(pud, addr);
  45. do {
  46. next = pmd_addr_end(addr, end);
  47. if (pmd_none_or_clear_bad(pmd))
  48. continue;
  49. vunmap_pte_range(pmd, addr, next);
  50. } while (pmd++, addr = next, addr != end);
  51. }
  52. static void vunmap_pud_range(pgd_t *pgd, unsigned long addr, unsigned long end)
  53. {
  54. pud_t *pud;
  55. unsigned long next;
  56. pud = pud_offset(pgd, addr);
  57. do {
  58. next = pud_addr_end(addr, end);
  59. if (pud_none_or_clear_bad(pud))
  60. continue;
  61. vunmap_pmd_range(pud, addr, next);
  62. } while (pud++, addr = next, addr != end);
  63. }
  64. static void vunmap_page_range(unsigned long addr, unsigned long end)
  65. {
  66. pgd_t *pgd;
  67. unsigned long next;
  68. BUG_ON(addr >= end);
  69. pgd = pgd_offset_k(addr);
  70. do {
  71. next = pgd_addr_end(addr, end);
  72. if (pgd_none_or_clear_bad(pgd))
  73. continue;
  74. vunmap_pud_range(pgd, addr, next);
  75. } while (pgd++, addr = next, addr != end);
  76. }
  77. static int vmap_pte_range(pmd_t *pmd, unsigned long addr,
  78. unsigned long end, pgprot_t prot, struct page **pages, int *nr)
  79. {
  80. pte_t *pte;
  81. /*
  82. * nr is a running index into the array which helps higher level
  83. * callers keep track of where we're up to.
  84. */
  85. pte = pte_alloc_kernel(pmd, addr);
  86. if (!pte)
  87. return -ENOMEM;
  88. do {
  89. struct page *page = pages[*nr];
  90. if (WARN_ON(!pte_none(*pte)))
  91. return -EBUSY;
  92. if (WARN_ON(!page))
  93. return -ENOMEM;
  94. set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
  95. (*nr)++;
  96. } while (pte++, addr += PAGE_SIZE, addr != end);
  97. return 0;
  98. }
  99. static int vmap_pmd_range(pud_t *pud, unsigned long addr,
  100. unsigned long end, pgprot_t prot, struct page **pages, int *nr)
  101. {
  102. pmd_t *pmd;
  103. unsigned long next;
  104. pmd = pmd_alloc(&init_mm, pud, addr);
  105. if (!pmd)
  106. return -ENOMEM;
  107. do {
  108. next = pmd_addr_end(addr, end);
  109. if (vmap_pte_range(pmd, addr, next, prot, pages, nr))
  110. return -ENOMEM;
  111. } while (pmd++, addr = next, addr != end);
  112. return 0;
  113. }
  114. static int vmap_pud_range(pgd_t *pgd, unsigned long addr,
  115. unsigned long end, pgprot_t prot, struct page **pages, int *nr)
  116. {
  117. pud_t *pud;
  118. unsigned long next;
  119. pud = pud_alloc(&init_mm, pgd, addr);
  120. if (!pud)
  121. return -ENOMEM;
  122. do {
  123. next = pud_addr_end(addr, end);
  124. if (vmap_pmd_range(pud, addr, next, prot, pages, nr))
  125. return -ENOMEM;
  126. } while (pud++, addr = next, addr != end);
  127. return 0;
  128. }
  129. /*
  130. * Set up page tables in kva (addr, end). The ptes shall have prot "prot", and
  131. * will have pfns corresponding to the "pages" array.
  132. *
  133. * Ie. pte at addr+N*PAGE_SIZE shall point to pfn corresponding to pages[N]
  134. */
  135. static int vmap_page_range_noflush(unsigned long start, unsigned long end,
  136. pgprot_t prot, struct page **pages)
  137. {
  138. pgd_t *pgd;
  139. unsigned long next;
  140. unsigned long addr = start;
  141. int err = 0;
  142. int nr = 0;
  143. BUG_ON(addr >= end);
  144. pgd = pgd_offset_k(addr);
  145. do {
  146. next = pgd_addr_end(addr, end);
  147. err = vmap_pud_range(pgd, addr, next, prot, pages, &nr);
  148. if (err)
  149. break;
  150. } while (pgd++, addr = next, addr != end);
  151. if (unlikely(err))
  152. return err;
  153. return nr;
  154. }
  155. static int vmap_page_range(unsigned long start, unsigned long end,
  156. pgprot_t prot, struct page **pages)
  157. {
  158. int ret;
  159. ret = vmap_page_range_noflush(start, end, prot, pages);
  160. flush_cache_vmap(start, end);
  161. return ret;
  162. }
  163. static inline int is_vmalloc_or_module_addr(const void *x)
  164. {
  165. /*
  166. * ARM, x86-64 and sparc64 put modules in a special place,
  167. * and fall back on vmalloc() if that fails. Others
  168. * just put it in the vmalloc space.
  169. */
  170. #if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
  171. unsigned long addr = (unsigned long)x;
  172. if (addr >= MODULES_VADDR && addr < MODULES_END)
  173. return 1;
  174. #endif
  175. return is_vmalloc_addr(x);
  176. }
  177. /*
  178. * Walk a vmap address to the struct page it maps.
  179. */
  180. struct page *vmalloc_to_page(const void *vmalloc_addr)
  181. {
  182. unsigned long addr = (unsigned long) vmalloc_addr;
  183. struct page *page = NULL;
  184. pgd_t *pgd = pgd_offset_k(addr);
  185. /*
  186. * XXX we might need to change this if we add VIRTUAL_BUG_ON for
  187. * architectures that do not vmalloc module space
  188. */
  189. VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr));
  190. if (!pgd_none(*pgd)) {
  191. pud_t *pud = pud_offset(pgd, addr);
  192. if (!pud_none(*pud)) {
  193. pmd_t *pmd = pmd_offset(pud, addr);
  194. if (!pmd_none(*pmd)) {
  195. pte_t *ptep, pte;
  196. ptep = pte_offset_map(pmd, addr);
  197. pte = *ptep;
  198. if (pte_present(pte))
  199. page = pte_page(pte);
  200. pte_unmap(ptep);
  201. }
  202. }
  203. }
  204. return page;
  205. }
  206. EXPORT_SYMBOL(vmalloc_to_page);
  207. /*
  208. * Map a vmalloc()-space virtual address to the physical page frame number.
  209. */
  210. unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
  211. {
  212. return page_to_pfn(vmalloc_to_page(vmalloc_addr));
  213. }
  214. EXPORT_SYMBOL(vmalloc_to_pfn);
  215. /*** Global kva allocator ***/
  216. #define VM_LAZY_FREE 0x01
  217. #define VM_LAZY_FREEING 0x02
  218. #define VM_VM_AREA 0x04
  219. struct vmap_area {
  220. unsigned long va_start;
  221. unsigned long va_end;
  222. unsigned long flags;
  223. struct rb_node rb_node; /* address sorted rbtree */
  224. struct list_head list; /* address sorted list */
  225. struct list_head purge_list; /* "lazy purge" list */
  226. void *private;
  227. struct rcu_head rcu_head;
  228. };
  229. static DEFINE_SPINLOCK(vmap_area_lock);
  230. static struct rb_root vmap_area_root = RB_ROOT;
  231. static LIST_HEAD(vmap_area_list);
  232. static unsigned long vmap_area_pcpu_hole;
  233. static struct vmap_area *__find_vmap_area(unsigned long addr)
  234. {
  235. struct rb_node *n = vmap_area_root.rb_node;
  236. while (n) {
  237. struct vmap_area *va;
  238. va = rb_entry(n, struct vmap_area, rb_node);
  239. if (addr < va->va_start)
  240. n = n->rb_left;
  241. else if (addr > va->va_start)
  242. n = n->rb_right;
  243. else
  244. return va;
  245. }
  246. return NULL;
  247. }
  248. static void __insert_vmap_area(struct vmap_area *va)
  249. {
  250. struct rb_node **p = &vmap_area_root.rb_node;
  251. struct rb_node *parent = NULL;
  252. struct rb_node *tmp;
  253. while (*p) {
  254. struct vmap_area *tmp;
  255. parent = *p;
  256. tmp = rb_entry(parent, struct vmap_area, rb_node);
  257. if (va->va_start < tmp->va_end)
  258. p = &(*p)->rb_left;
  259. else if (va->va_end > tmp->va_start)
  260. p = &(*p)->rb_right;
  261. else
  262. BUG();
  263. }
  264. rb_link_node(&va->rb_node, parent, p);
  265. rb_insert_color(&va->rb_node, &vmap_area_root);
  266. /* address-sort this list so it is usable like the vmlist */
  267. tmp = rb_prev(&va->rb_node);
  268. if (tmp) {
  269. struct vmap_area *prev;
  270. prev = rb_entry(tmp, struct vmap_area, rb_node);
  271. list_add_rcu(&va->list, &prev->list);
  272. } else
  273. list_add_rcu(&va->list, &vmap_area_list);
  274. }
  275. static void purge_vmap_area_lazy(void);
  276. /*
  277. * Allocate a region of KVA of the specified size and alignment, within the
  278. * vstart and vend.
  279. */
  280. static struct vmap_area *alloc_vmap_area(unsigned long size,
  281. unsigned long align,
  282. unsigned long vstart, unsigned long vend,
  283. int node, gfp_t gfp_mask)
  284. {
  285. struct vmap_area *va;
  286. struct rb_node *n;
  287. unsigned long addr;
  288. int purged = 0;
  289. BUG_ON(!size);
  290. BUG_ON(size & ~PAGE_MASK);
  291. va = kmalloc_node(sizeof(struct vmap_area),
  292. gfp_mask & GFP_RECLAIM_MASK, node);
  293. if (unlikely(!va))
  294. return ERR_PTR(-ENOMEM);
  295. retry:
  296. addr = ALIGN(vstart, align);
  297. spin_lock(&vmap_area_lock);
  298. if (addr + size - 1 < addr)
  299. goto overflow;
  300. /* XXX: could have a last_hole cache */
  301. n = vmap_area_root.rb_node;
  302. if (n) {
  303. struct vmap_area *first = NULL;
  304. do {
  305. struct vmap_area *tmp;
  306. tmp = rb_entry(n, struct vmap_area, rb_node);
  307. if (tmp->va_end >= addr) {
  308. if (!first && tmp->va_start < addr + size)
  309. first = tmp;
  310. n = n->rb_left;
  311. } else {
  312. first = tmp;
  313. n = n->rb_right;
  314. }
  315. } while (n);
  316. if (!first)
  317. goto found;
  318. if (first->va_end < addr) {
  319. n = rb_next(&first->rb_node);
  320. if (n)
  321. first = rb_entry(n, struct vmap_area, rb_node);
  322. else
  323. goto found;
  324. }
  325. while (addr + size > first->va_start && addr + size <= vend) {
  326. addr = ALIGN(first->va_end + PAGE_SIZE, align);
  327. if (addr + size - 1 < addr)
  328. goto overflow;
  329. n = rb_next(&first->rb_node);
  330. if (n)
  331. first = rb_entry(n, struct vmap_area, rb_node);
  332. else
  333. goto found;
  334. }
  335. }
  336. found:
  337. if (addr + size > vend) {
  338. overflow:
  339. spin_unlock(&vmap_area_lock);
  340. if (!purged) {
  341. purge_vmap_area_lazy();
  342. purged = 1;
  343. goto retry;
  344. }
  345. if (printk_ratelimit())
  346. printk(KERN_WARNING
  347. "vmap allocation for size %lu failed: "
  348. "use vmalloc=<size> to increase size.\n", size);
  349. kfree(va);
  350. return ERR_PTR(-EBUSY);
  351. }
  352. BUG_ON(addr & (align-1));
  353. va->va_start = addr;
  354. va->va_end = addr + size;
  355. va->flags = 0;
  356. __insert_vmap_area(va);
  357. spin_unlock(&vmap_area_lock);
  358. return va;
  359. }
  360. static void rcu_free_va(struct rcu_head *head)
  361. {
  362. struct vmap_area *va = container_of(head, struct vmap_area, rcu_head);
  363. kfree(va);
  364. }
  365. static void __free_vmap_area(struct vmap_area *va)
  366. {
  367. BUG_ON(RB_EMPTY_NODE(&va->rb_node));
  368. rb_erase(&va->rb_node, &vmap_area_root);
  369. RB_CLEAR_NODE(&va->rb_node);
  370. list_del_rcu(&va->list);
  371. /*
  372. * Track the highest possible candidate for pcpu area
  373. * allocation. Areas outside of vmalloc area can be returned
  374. * here too, consider only end addresses which fall inside
  375. * vmalloc area proper.
  376. */
  377. if (va->va_end > VMALLOC_START && va->va_end <= VMALLOC_END)
  378. vmap_area_pcpu_hole = max(vmap_area_pcpu_hole, va->va_end);
  379. call_rcu(&va->rcu_head, rcu_free_va);
  380. }
  381. /*
  382. * Free a region of KVA allocated by alloc_vmap_area
  383. */
  384. static void free_vmap_area(struct vmap_area *va)
  385. {
  386. spin_lock(&vmap_area_lock);
  387. __free_vmap_area(va);
  388. spin_unlock(&vmap_area_lock);
  389. }
  390. /*
  391. * Clear the pagetable entries of a given vmap_area
  392. */
  393. static void unmap_vmap_area(struct vmap_area *va)
  394. {
  395. vunmap_page_range(va->va_start, va->va_end);
  396. }
  397. static void vmap_debug_free_range(unsigned long start, unsigned long end)
  398. {
  399. /*
  400. * Unmap page tables and force a TLB flush immediately if
  401. * CONFIG_DEBUG_PAGEALLOC is set. This catches use after free
  402. * bugs similarly to those in linear kernel virtual address
  403. * space after a page has been freed.
  404. *
  405. * All the lazy freeing logic is still retained, in order to
  406. * minimise intrusiveness of this debugging feature.
  407. *
  408. * This is going to be *slow* (linear kernel virtual address
  409. * debugging doesn't do a broadcast TLB flush so it is a lot
  410. * faster).
  411. */
  412. #ifdef CONFIG_DEBUG_PAGEALLOC
  413. vunmap_page_range(start, end);
  414. flush_tlb_kernel_range(start, end);
  415. #endif
  416. }
  417. /*
  418. * lazy_max_pages is the maximum amount of virtual address space we gather up
  419. * before attempting to purge with a TLB flush.
  420. *
  421. * There is a tradeoff here: a larger number will cover more kernel page tables
  422. * and take slightly longer to purge, but it will linearly reduce the number of
  423. * global TLB flushes that must be performed. It would seem natural to scale
  424. * this number up linearly with the number of CPUs (because vmapping activity
  425. * could also scale linearly with the number of CPUs), however it is likely
  426. * that in practice, workloads might be constrained in other ways that mean
  427. * vmap activity will not scale linearly with CPUs. Also, I want to be
  428. * conservative and not introduce a big latency on huge systems, so go with
  429. * a less aggressive log scale. It will still be an improvement over the old
  430. * code, and it will be simple to change the scale factor if we find that it
  431. * becomes a problem on bigger systems.
  432. */
  433. static unsigned long lazy_max_pages(void)
  434. {
  435. unsigned int log;
  436. log = fls(num_online_cpus());
  437. return log * (32UL * 1024 * 1024 / PAGE_SIZE);
  438. }
  439. static atomic_t vmap_lazy_nr = ATOMIC_INIT(0);
  440. /*
  441. * Purges all lazily-freed vmap areas.
  442. *
  443. * If sync is 0 then don't purge if there is already a purge in progress.
  444. * If force_flush is 1, then flush kernel TLBs between *start and *end even
  445. * if we found no lazy vmap areas to unmap (callers can use this to optimise
  446. * their own TLB flushing).
  447. * Returns with *start = min(*start, lowest purged address)
  448. * *end = max(*end, highest purged address)
  449. */
  450. static void __purge_vmap_area_lazy(unsigned long *start, unsigned long *end,
  451. int sync, int force_flush)
  452. {
  453. static DEFINE_SPINLOCK(purge_lock);
  454. LIST_HEAD(valist);
  455. struct vmap_area *va;
  456. struct vmap_area *n_va;
  457. int nr = 0;
  458. /*
  459. * If sync is 0 but force_flush is 1, we'll go sync anyway but callers
  460. * should not expect such behaviour. This just simplifies locking for
  461. * the case that isn't actually used at the moment anyway.
  462. */
  463. if (!sync && !force_flush) {
  464. if (!spin_trylock(&purge_lock))
  465. return;
  466. } else
  467. spin_lock(&purge_lock);
  468. rcu_read_lock();
  469. list_for_each_entry_rcu(va, &vmap_area_list, list) {
  470. if (va->flags & VM_LAZY_FREE) {
  471. if (va->va_start < *start)
  472. *start = va->va_start;
  473. if (va->va_end > *end)
  474. *end = va->va_end;
  475. nr += (va->va_end - va->va_start) >> PAGE_SHIFT;
  476. unmap_vmap_area(va);
  477. list_add_tail(&va->purge_list, &valist);
  478. va->flags |= VM_LAZY_FREEING;
  479. va->flags &= ~VM_LAZY_FREE;
  480. }
  481. }
  482. rcu_read_unlock();
  483. if (nr) {
  484. BUG_ON(nr > atomic_read(&vmap_lazy_nr));
  485. atomic_sub(nr, &vmap_lazy_nr);
  486. }
  487. if (nr || force_flush)
  488. flush_tlb_kernel_range(*start, *end);
  489. if (nr) {
  490. spin_lock(&vmap_area_lock);
  491. list_for_each_entry_safe(va, n_va, &valist, purge_list)
  492. __free_vmap_area(va);
  493. spin_unlock(&vmap_area_lock);
  494. }
  495. spin_unlock(&purge_lock);
  496. }
  497. /*
  498. * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
  499. * is already purging.
  500. */
  501. static void try_purge_vmap_area_lazy(void)
  502. {
  503. unsigned long start = ULONG_MAX, end = 0;
  504. __purge_vmap_area_lazy(&start, &end, 0, 0);
  505. }
  506. /*
  507. * Kick off a purge of the outstanding lazy areas.
  508. */
  509. static void purge_vmap_area_lazy(void)
  510. {
  511. unsigned long start = ULONG_MAX, end = 0;
  512. __purge_vmap_area_lazy(&start, &end, 1, 0);
  513. }
  514. /*
  515. * Free and unmap a vmap area, caller ensuring flush_cache_vunmap had been
  516. * called for the correct range previously.
  517. */
  518. static void free_unmap_vmap_area_noflush(struct vmap_area *va)
  519. {
  520. va->flags |= VM_LAZY_FREE;
  521. atomic_add((va->va_end - va->va_start) >> PAGE_SHIFT, &vmap_lazy_nr);
  522. if (unlikely(atomic_read(&vmap_lazy_nr) > lazy_max_pages()))
  523. try_purge_vmap_area_lazy();
  524. }
  525. /*
  526. * Free and unmap a vmap area
  527. */
  528. static void free_unmap_vmap_area(struct vmap_area *va)
  529. {
  530. flush_cache_vunmap(va->va_start, va->va_end);
  531. free_unmap_vmap_area_noflush(va);
  532. }
  533. static struct vmap_area *find_vmap_area(unsigned long addr)
  534. {
  535. struct vmap_area *va;
  536. spin_lock(&vmap_area_lock);
  537. va = __find_vmap_area(addr);
  538. spin_unlock(&vmap_area_lock);
  539. return va;
  540. }
  541. static void free_unmap_vmap_area_addr(unsigned long addr)
  542. {
  543. struct vmap_area *va;
  544. va = find_vmap_area(addr);
  545. BUG_ON(!va);
  546. free_unmap_vmap_area(va);
  547. }
  548. /*** Per cpu kva allocator ***/
  549. /*
  550. * vmap space is limited especially on 32 bit architectures. Ensure there is
  551. * room for at least 16 percpu vmap blocks per CPU.
  552. */
  553. /*
  554. * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
  555. * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess
  556. * instead (we just need a rough idea)
  557. */
  558. #if BITS_PER_LONG == 32
  559. #define VMALLOC_SPACE (128UL*1024*1024)
  560. #else
  561. #define VMALLOC_SPACE (128UL*1024*1024*1024)
  562. #endif
  563. #define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE)
  564. #define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */
  565. #define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */
  566. #define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2)
  567. #define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */
  568. #define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */
  569. #define VMAP_BBMAP_BITS VMAP_MIN(VMAP_BBMAP_BITS_MAX, \
  570. VMAP_MAX(VMAP_BBMAP_BITS_MIN, \
  571. VMALLOC_PAGES / NR_CPUS / 16))
  572. #define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE)
  573. static bool vmap_initialized __read_mostly = false;
  574. struct vmap_block_queue {
  575. spinlock_t lock;
  576. struct list_head free;
  577. struct list_head dirty;
  578. unsigned int nr_dirty;
  579. };
  580. struct vmap_block {
  581. spinlock_t lock;
  582. struct vmap_area *va;
  583. struct vmap_block_queue *vbq;
  584. unsigned long free, dirty;
  585. DECLARE_BITMAP(alloc_map, VMAP_BBMAP_BITS);
  586. DECLARE_BITMAP(dirty_map, VMAP_BBMAP_BITS);
  587. union {
  588. struct list_head free_list;
  589. struct rcu_head rcu_head;
  590. };
  591. };
  592. /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
  593. static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
  594. /*
  595. * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block
  596. * in the free path. Could get rid of this if we change the API to return a
  597. * "cookie" from alloc, to be passed to free. But no big deal yet.
  598. */
  599. static DEFINE_SPINLOCK(vmap_block_tree_lock);
  600. static RADIX_TREE(vmap_block_tree, GFP_ATOMIC);
  601. /*
  602. * We should probably have a fallback mechanism to allocate virtual memory
  603. * out of partially filled vmap blocks. However vmap block sizing should be
  604. * fairly reasonable according to the vmalloc size, so it shouldn't be a
  605. * big problem.
  606. */
  607. static unsigned long addr_to_vb_idx(unsigned long addr)
  608. {
  609. addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
  610. addr /= VMAP_BLOCK_SIZE;
  611. return addr;
  612. }
  613. static struct vmap_block *new_vmap_block(gfp_t gfp_mask)
  614. {
  615. struct vmap_block_queue *vbq;
  616. struct vmap_block *vb;
  617. struct vmap_area *va;
  618. unsigned long vb_idx;
  619. int node, err;
  620. node = numa_node_id();
  621. vb = kmalloc_node(sizeof(struct vmap_block),
  622. gfp_mask & GFP_RECLAIM_MASK, node);
  623. if (unlikely(!vb))
  624. return ERR_PTR(-ENOMEM);
  625. va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
  626. VMALLOC_START, VMALLOC_END,
  627. node, gfp_mask);
  628. if (unlikely(IS_ERR(va))) {
  629. kfree(vb);
  630. return ERR_PTR(PTR_ERR(va));
  631. }
  632. err = radix_tree_preload(gfp_mask);
  633. if (unlikely(err)) {
  634. kfree(vb);
  635. free_vmap_area(va);
  636. return ERR_PTR(err);
  637. }
  638. spin_lock_init(&vb->lock);
  639. vb->va = va;
  640. vb->free = VMAP_BBMAP_BITS;
  641. vb->dirty = 0;
  642. bitmap_zero(vb->alloc_map, VMAP_BBMAP_BITS);
  643. bitmap_zero(vb->dirty_map, VMAP_BBMAP_BITS);
  644. INIT_LIST_HEAD(&vb->free_list);
  645. vb_idx = addr_to_vb_idx(va->va_start);
  646. spin_lock(&vmap_block_tree_lock);
  647. err = radix_tree_insert(&vmap_block_tree, vb_idx, vb);
  648. spin_unlock(&vmap_block_tree_lock);
  649. BUG_ON(err);
  650. radix_tree_preload_end();
  651. vbq = &get_cpu_var(vmap_block_queue);
  652. vb->vbq = vbq;
  653. spin_lock(&vbq->lock);
  654. list_add(&vb->free_list, &vbq->free);
  655. spin_unlock(&vbq->lock);
  656. put_cpu_var(vmap_cpu_blocks);
  657. return vb;
  658. }
  659. static void rcu_free_vb(struct rcu_head *head)
  660. {
  661. struct vmap_block *vb = container_of(head, struct vmap_block, rcu_head);
  662. kfree(vb);
  663. }
  664. static void free_vmap_block(struct vmap_block *vb)
  665. {
  666. struct vmap_block *tmp;
  667. unsigned long vb_idx;
  668. BUG_ON(!list_empty(&vb->free_list));
  669. vb_idx = addr_to_vb_idx(vb->va->va_start);
  670. spin_lock(&vmap_block_tree_lock);
  671. tmp = radix_tree_delete(&vmap_block_tree, vb_idx);
  672. spin_unlock(&vmap_block_tree_lock);
  673. BUG_ON(tmp != vb);
  674. free_unmap_vmap_area_noflush(vb->va);
  675. call_rcu(&vb->rcu_head, rcu_free_vb);
  676. }
  677. static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
  678. {
  679. struct vmap_block_queue *vbq;
  680. struct vmap_block *vb;
  681. unsigned long addr = 0;
  682. unsigned int order;
  683. BUG_ON(size & ~PAGE_MASK);
  684. BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
  685. order = get_order(size);
  686. again:
  687. rcu_read_lock();
  688. vbq = &get_cpu_var(vmap_block_queue);
  689. list_for_each_entry_rcu(vb, &vbq->free, free_list) {
  690. int i;
  691. spin_lock(&vb->lock);
  692. i = bitmap_find_free_region(vb->alloc_map,
  693. VMAP_BBMAP_BITS, order);
  694. if (i >= 0) {
  695. addr = vb->va->va_start + (i << PAGE_SHIFT);
  696. BUG_ON(addr_to_vb_idx(addr) !=
  697. addr_to_vb_idx(vb->va->va_start));
  698. vb->free -= 1UL << order;
  699. if (vb->free == 0) {
  700. spin_lock(&vbq->lock);
  701. list_del_init(&vb->free_list);
  702. spin_unlock(&vbq->lock);
  703. }
  704. spin_unlock(&vb->lock);
  705. break;
  706. }
  707. spin_unlock(&vb->lock);
  708. }
  709. put_cpu_var(vmap_cpu_blocks);
  710. rcu_read_unlock();
  711. if (!addr) {
  712. vb = new_vmap_block(gfp_mask);
  713. if (IS_ERR(vb))
  714. return vb;
  715. goto again;
  716. }
  717. return (void *)addr;
  718. }
  719. static void vb_free(const void *addr, unsigned long size)
  720. {
  721. unsigned long offset;
  722. unsigned long vb_idx;
  723. unsigned int order;
  724. struct vmap_block *vb;
  725. BUG_ON(size & ~PAGE_MASK);
  726. BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
  727. flush_cache_vunmap((unsigned long)addr, (unsigned long)addr + size);
  728. order = get_order(size);
  729. offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1);
  730. vb_idx = addr_to_vb_idx((unsigned long)addr);
  731. rcu_read_lock();
  732. vb = radix_tree_lookup(&vmap_block_tree, vb_idx);
  733. rcu_read_unlock();
  734. BUG_ON(!vb);
  735. spin_lock(&vb->lock);
  736. bitmap_allocate_region(vb->dirty_map, offset >> PAGE_SHIFT, order);
  737. vb->dirty += 1UL << order;
  738. if (vb->dirty == VMAP_BBMAP_BITS) {
  739. BUG_ON(vb->free || !list_empty(&vb->free_list));
  740. spin_unlock(&vb->lock);
  741. free_vmap_block(vb);
  742. } else
  743. spin_unlock(&vb->lock);
  744. }
  745. /**
  746. * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
  747. *
  748. * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
  749. * to amortize TLB flushing overheads. What this means is that any page you
  750. * have now, may, in a former life, have been mapped into kernel virtual
  751. * address by the vmap layer and so there might be some CPUs with TLB entries
  752. * still referencing that page (additional to the regular 1:1 kernel mapping).
  753. *
  754. * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
  755. * be sure that none of the pages we have control over will have any aliases
  756. * from the vmap layer.
  757. */
  758. void vm_unmap_aliases(void)
  759. {
  760. unsigned long start = ULONG_MAX, end = 0;
  761. int cpu;
  762. int flush = 0;
  763. if (unlikely(!vmap_initialized))
  764. return;
  765. for_each_possible_cpu(cpu) {
  766. struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
  767. struct vmap_block *vb;
  768. rcu_read_lock();
  769. list_for_each_entry_rcu(vb, &vbq->free, free_list) {
  770. int i;
  771. spin_lock(&vb->lock);
  772. i = find_first_bit(vb->dirty_map, VMAP_BBMAP_BITS);
  773. while (i < VMAP_BBMAP_BITS) {
  774. unsigned long s, e;
  775. int j;
  776. j = find_next_zero_bit(vb->dirty_map,
  777. VMAP_BBMAP_BITS, i);
  778. s = vb->va->va_start + (i << PAGE_SHIFT);
  779. e = vb->va->va_start + (j << PAGE_SHIFT);
  780. vunmap_page_range(s, e);
  781. flush = 1;
  782. if (s < start)
  783. start = s;
  784. if (e > end)
  785. end = e;
  786. i = j;
  787. i = find_next_bit(vb->dirty_map,
  788. VMAP_BBMAP_BITS, i);
  789. }
  790. spin_unlock(&vb->lock);
  791. }
  792. rcu_read_unlock();
  793. }
  794. __purge_vmap_area_lazy(&start, &end, 1, flush);
  795. }
  796. EXPORT_SYMBOL_GPL(vm_unmap_aliases);
  797. /**
  798. * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
  799. * @mem: the pointer returned by vm_map_ram
  800. * @count: the count passed to that vm_map_ram call (cannot unmap partial)
  801. */
  802. void vm_unmap_ram(const void *mem, unsigned int count)
  803. {
  804. unsigned long size = count << PAGE_SHIFT;
  805. unsigned long addr = (unsigned long)mem;
  806. BUG_ON(!addr);
  807. BUG_ON(addr < VMALLOC_START);
  808. BUG_ON(addr > VMALLOC_END);
  809. BUG_ON(addr & (PAGE_SIZE-1));
  810. debug_check_no_locks_freed(mem, size);
  811. vmap_debug_free_range(addr, addr+size);
  812. if (likely(count <= VMAP_MAX_ALLOC))
  813. vb_free(mem, size);
  814. else
  815. free_unmap_vmap_area_addr(addr);
  816. }
  817. EXPORT_SYMBOL(vm_unmap_ram);
  818. /**
  819. * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
  820. * @pages: an array of pointers to the pages to be mapped
  821. * @count: number of pages
  822. * @node: prefer to allocate data structures on this node
  823. * @prot: memory protection to use. PAGE_KERNEL for regular RAM
  824. *
  825. * Returns: a pointer to the address that has been mapped, or %NULL on failure
  826. */
  827. void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
  828. {
  829. unsigned long size = count << PAGE_SHIFT;
  830. unsigned long addr;
  831. void *mem;
  832. if (likely(count <= VMAP_MAX_ALLOC)) {
  833. mem = vb_alloc(size, GFP_KERNEL);
  834. if (IS_ERR(mem))
  835. return NULL;
  836. addr = (unsigned long)mem;
  837. } else {
  838. struct vmap_area *va;
  839. va = alloc_vmap_area(size, PAGE_SIZE,
  840. VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
  841. if (IS_ERR(va))
  842. return NULL;
  843. addr = va->va_start;
  844. mem = (void *)addr;
  845. }
  846. if (vmap_page_range(addr, addr + size, prot, pages) < 0) {
  847. vm_unmap_ram(mem, count);
  848. return NULL;
  849. }
  850. return mem;
  851. }
  852. EXPORT_SYMBOL(vm_map_ram);
  853. /**
  854. * vm_area_register_early - register vmap area early during boot
  855. * @vm: vm_struct to register
  856. * @align: requested alignment
  857. *
  858. * This function is used to register kernel vm area before
  859. * vmalloc_init() is called. @vm->size and @vm->flags should contain
  860. * proper values on entry and other fields should be zero. On return,
  861. * vm->addr contains the allocated address.
  862. *
  863. * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
  864. */
  865. void __init vm_area_register_early(struct vm_struct *vm, size_t align)
  866. {
  867. static size_t vm_init_off __initdata;
  868. unsigned long addr;
  869. addr = ALIGN(VMALLOC_START + vm_init_off, align);
  870. vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START;
  871. vm->addr = (void *)addr;
  872. vm->next = vmlist;
  873. vmlist = vm;
  874. }
  875. void __init vmalloc_init(void)
  876. {
  877. struct vmap_area *va;
  878. struct vm_struct *tmp;
  879. int i;
  880. for_each_possible_cpu(i) {
  881. struct vmap_block_queue *vbq;
  882. vbq = &per_cpu(vmap_block_queue, i);
  883. spin_lock_init(&vbq->lock);
  884. INIT_LIST_HEAD(&vbq->free);
  885. INIT_LIST_HEAD(&vbq->dirty);
  886. vbq->nr_dirty = 0;
  887. }
  888. /* Import existing vmlist entries. */
  889. for (tmp = vmlist; tmp; tmp = tmp->next) {
  890. va = kzalloc(sizeof(struct vmap_area), GFP_NOWAIT);
  891. va->flags = tmp->flags | VM_VM_AREA;
  892. va->va_start = (unsigned long)tmp->addr;
  893. va->va_end = va->va_start + tmp->size;
  894. __insert_vmap_area(va);
  895. }
  896. vmap_area_pcpu_hole = VMALLOC_END;
  897. vmap_initialized = true;
  898. }
  899. /**
  900. * map_kernel_range_noflush - map kernel VM area with the specified pages
  901. * @addr: start of the VM area to map
  902. * @size: size of the VM area to map
  903. * @prot: page protection flags to use
  904. * @pages: pages to map
  905. *
  906. * Map PFN_UP(@size) pages at @addr. The VM area @addr and @size
  907. * specify should have been allocated using get_vm_area() and its
  908. * friends.
  909. *
  910. * NOTE:
  911. * This function does NOT do any cache flushing. The caller is
  912. * responsible for calling flush_cache_vmap() on to-be-mapped areas
  913. * before calling this function.
  914. *
  915. * RETURNS:
  916. * The number of pages mapped on success, -errno on failure.
  917. */
  918. int map_kernel_range_noflush(unsigned long addr, unsigned long size,
  919. pgprot_t prot, struct page **pages)
  920. {
  921. return vmap_page_range_noflush(addr, addr + size, prot, pages);
  922. }
  923. /**
  924. * unmap_kernel_range_noflush - unmap kernel VM area
  925. * @addr: start of the VM area to unmap
  926. * @size: size of the VM area to unmap
  927. *
  928. * Unmap PFN_UP(@size) pages at @addr. The VM area @addr and @size
  929. * specify should have been allocated using get_vm_area() and its
  930. * friends.
  931. *
  932. * NOTE:
  933. * This function does NOT do any cache flushing. The caller is
  934. * responsible for calling flush_cache_vunmap() on to-be-mapped areas
  935. * before calling this function and flush_tlb_kernel_range() after.
  936. */
  937. void unmap_kernel_range_noflush(unsigned long addr, unsigned long size)
  938. {
  939. vunmap_page_range(addr, addr + size);
  940. }
  941. /**
  942. * unmap_kernel_range - unmap kernel VM area and flush cache and TLB
  943. * @addr: start of the VM area to unmap
  944. * @size: size of the VM area to unmap
  945. *
  946. * Similar to unmap_kernel_range_noflush() but flushes vcache before
  947. * the unmapping and tlb after.
  948. */
  949. void unmap_kernel_range(unsigned long addr, unsigned long size)
  950. {
  951. unsigned long end = addr + size;
  952. flush_cache_vunmap(addr, end);
  953. vunmap_page_range(addr, end);
  954. flush_tlb_kernel_range(addr, end);
  955. }
  956. int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page ***pages)
  957. {
  958. unsigned long addr = (unsigned long)area->addr;
  959. unsigned long end = addr + area->size - PAGE_SIZE;
  960. int err;
  961. err = vmap_page_range(addr, end, prot, *pages);
  962. if (err > 0) {
  963. *pages += err;
  964. err = 0;
  965. }
  966. return err;
  967. }
  968. EXPORT_SYMBOL_GPL(map_vm_area);
  969. /*** Old vmalloc interfaces ***/
  970. DEFINE_RWLOCK(vmlist_lock);
  971. struct vm_struct *vmlist;
  972. static void insert_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
  973. unsigned long flags, void *caller)
  974. {
  975. struct vm_struct *tmp, **p;
  976. vm->flags = flags;
  977. vm->addr = (void *)va->va_start;
  978. vm->size = va->va_end - va->va_start;
  979. vm->caller = caller;
  980. va->private = vm;
  981. va->flags |= VM_VM_AREA;
  982. write_lock(&vmlist_lock);
  983. for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
  984. if (tmp->addr >= vm->addr)
  985. break;
  986. }
  987. vm->next = *p;
  988. *p = vm;
  989. write_unlock(&vmlist_lock);
  990. }
  991. static struct vm_struct *__get_vm_area_node(unsigned long size,
  992. unsigned long flags, unsigned long start, unsigned long end,
  993. int node, gfp_t gfp_mask, void *caller)
  994. {
  995. static struct vmap_area *va;
  996. struct vm_struct *area;
  997. unsigned long align = 1;
  998. BUG_ON(in_interrupt());
  999. if (flags & VM_IOREMAP) {
  1000. int bit = fls(size);
  1001. if (bit > IOREMAP_MAX_ORDER)
  1002. bit = IOREMAP_MAX_ORDER;
  1003. else if (bit < PAGE_SHIFT)
  1004. bit = PAGE_SHIFT;
  1005. align = 1ul << bit;
  1006. }
  1007. size = PAGE_ALIGN(size);
  1008. if (unlikely(!size))
  1009. return NULL;
  1010. area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
  1011. if (unlikely(!area))
  1012. return NULL;
  1013. /*
  1014. * We always allocate a guard page.
  1015. */
  1016. size += PAGE_SIZE;
  1017. va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
  1018. if (IS_ERR(va)) {
  1019. kfree(area);
  1020. return NULL;
  1021. }
  1022. insert_vmalloc_vm(area, va, flags, caller);
  1023. return area;
  1024. }
  1025. struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
  1026. unsigned long start, unsigned long end)
  1027. {
  1028. return __get_vm_area_node(size, flags, start, end, -1, GFP_KERNEL,
  1029. __builtin_return_address(0));
  1030. }
  1031. EXPORT_SYMBOL_GPL(__get_vm_area);
  1032. struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
  1033. unsigned long start, unsigned long end,
  1034. void *caller)
  1035. {
  1036. return __get_vm_area_node(size, flags, start, end, -1, GFP_KERNEL,
  1037. caller);
  1038. }
  1039. /**
  1040. * get_vm_area - reserve a contiguous kernel virtual area
  1041. * @size: size of the area
  1042. * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC
  1043. *
  1044. * Search an area of @size in the kernel virtual mapping area,
  1045. * and reserved it for out purposes. Returns the area descriptor
  1046. * on success or %NULL on failure.
  1047. */
  1048. struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
  1049. {
  1050. return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
  1051. -1, GFP_KERNEL, __builtin_return_address(0));
  1052. }
  1053. struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
  1054. void *caller)
  1055. {
  1056. return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
  1057. -1, GFP_KERNEL, caller);
  1058. }
  1059. struct vm_struct *get_vm_area_node(unsigned long size, unsigned long flags,
  1060. int node, gfp_t gfp_mask)
  1061. {
  1062. return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END, node,
  1063. gfp_mask, __builtin_return_address(0));
  1064. }
  1065. static struct vm_struct *find_vm_area(const void *addr)
  1066. {
  1067. struct vmap_area *va;
  1068. va = find_vmap_area((unsigned long)addr);
  1069. if (va && va->flags & VM_VM_AREA)
  1070. return va->private;
  1071. return NULL;
  1072. }
  1073. /**
  1074. * remove_vm_area - find and remove a continuous kernel virtual area
  1075. * @addr: base address
  1076. *
  1077. * Search for the kernel VM area starting at @addr, and remove it.
  1078. * This function returns the found VM area, but using it is NOT safe
  1079. * on SMP machines, except for its size or flags.
  1080. */
  1081. struct vm_struct *remove_vm_area(const void *addr)
  1082. {
  1083. struct vmap_area *va;
  1084. va = find_vmap_area((unsigned long)addr);
  1085. if (va && va->flags & VM_VM_AREA) {
  1086. struct vm_struct *vm = va->private;
  1087. struct vm_struct *tmp, **p;
  1088. vmap_debug_free_range(va->va_start, va->va_end);
  1089. free_unmap_vmap_area(va);
  1090. vm->size -= PAGE_SIZE;
  1091. write_lock(&vmlist_lock);
  1092. for (p = &vmlist; (tmp = *p) != vm; p = &tmp->next)
  1093. ;
  1094. *p = tmp->next;
  1095. write_unlock(&vmlist_lock);
  1096. return vm;
  1097. }
  1098. return NULL;
  1099. }
  1100. static void __vunmap(const void *addr, int deallocate_pages)
  1101. {
  1102. struct vm_struct *area;
  1103. if (!addr)
  1104. return;
  1105. if ((PAGE_SIZE-1) & (unsigned long)addr) {
  1106. WARN(1, KERN_ERR "Trying to vfree() bad address (%p)\n", addr);
  1107. return;
  1108. }
  1109. area = remove_vm_area(addr);
  1110. if (unlikely(!area)) {
  1111. WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
  1112. addr);
  1113. return;
  1114. }
  1115. debug_check_no_locks_freed(addr, area->size);
  1116. debug_check_no_obj_freed(addr, area->size);
  1117. if (deallocate_pages) {
  1118. int i;
  1119. for (i = 0; i < area->nr_pages; i++) {
  1120. struct page *page = area->pages[i];
  1121. BUG_ON(!page);
  1122. __free_page(page);
  1123. }
  1124. if (area->flags & VM_VPAGES)
  1125. vfree(area->pages);
  1126. else
  1127. kfree(area->pages);
  1128. }
  1129. kfree(area);
  1130. return;
  1131. }
  1132. /**
  1133. * vfree - release memory allocated by vmalloc()
  1134. * @addr: memory base address
  1135. *
  1136. * Free the virtually continuous memory area starting at @addr, as
  1137. * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
  1138. * NULL, no operation is performed.
  1139. *
  1140. * Must not be called in interrupt context.
  1141. */
  1142. void vfree(const void *addr)
  1143. {
  1144. BUG_ON(in_interrupt());
  1145. kmemleak_free(addr);
  1146. __vunmap(addr, 1);
  1147. }
  1148. EXPORT_SYMBOL(vfree);
  1149. /**
  1150. * vunmap - release virtual mapping obtained by vmap()
  1151. * @addr: memory base address
  1152. *
  1153. * Free the virtually contiguous memory area starting at @addr,
  1154. * which was created from the page array passed to vmap().
  1155. *
  1156. * Must not be called in interrupt context.
  1157. */
  1158. void vunmap(const void *addr)
  1159. {
  1160. BUG_ON(in_interrupt());
  1161. might_sleep();
  1162. __vunmap(addr, 0);
  1163. }
  1164. EXPORT_SYMBOL(vunmap);
  1165. /**
  1166. * vmap - map an array of pages into virtually contiguous space
  1167. * @pages: array of page pointers
  1168. * @count: number of pages to map
  1169. * @flags: vm_area->flags
  1170. * @prot: page protection for the mapping
  1171. *
  1172. * Maps @count pages from @pages into contiguous kernel virtual
  1173. * space.
  1174. */
  1175. void *vmap(struct page **pages, unsigned int count,
  1176. unsigned long flags, pgprot_t prot)
  1177. {
  1178. struct vm_struct *area;
  1179. might_sleep();
  1180. if (count > num_physpages)
  1181. return NULL;
  1182. area = get_vm_area_caller((count << PAGE_SHIFT), flags,
  1183. __builtin_return_address(0));
  1184. if (!area)
  1185. return NULL;
  1186. if (map_vm_area(area, prot, &pages)) {
  1187. vunmap(area->addr);
  1188. return NULL;
  1189. }
  1190. return area->addr;
  1191. }
  1192. EXPORT_SYMBOL(vmap);
  1193. static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
  1194. int node, void *caller);
  1195. static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
  1196. pgprot_t prot, int node, void *caller)
  1197. {
  1198. struct page **pages;
  1199. unsigned int nr_pages, array_size, i;
  1200. nr_pages = (area->size - PAGE_SIZE) >> PAGE_SHIFT;
  1201. array_size = (nr_pages * sizeof(struct page *));
  1202. area->nr_pages = nr_pages;
  1203. /* Please note that the recursion is strictly bounded. */
  1204. if (array_size > PAGE_SIZE) {
  1205. pages = __vmalloc_node(array_size, gfp_mask | __GFP_ZERO,
  1206. PAGE_KERNEL, node, caller);
  1207. area->flags |= VM_VPAGES;
  1208. } else {
  1209. pages = kmalloc_node(array_size,
  1210. (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO,
  1211. node);
  1212. }
  1213. area->pages = pages;
  1214. area->caller = caller;
  1215. if (!area->pages) {
  1216. remove_vm_area(area->addr);
  1217. kfree(area);
  1218. return NULL;
  1219. }
  1220. for (i = 0; i < area->nr_pages; i++) {
  1221. struct page *page;
  1222. if (node < 0)
  1223. page = alloc_page(gfp_mask);
  1224. else
  1225. page = alloc_pages_node(node, gfp_mask, 0);
  1226. if (unlikely(!page)) {
  1227. /* Successfully allocated i pages, free them in __vunmap() */
  1228. area->nr_pages = i;
  1229. goto fail;
  1230. }
  1231. area->pages[i] = page;
  1232. }
  1233. if (map_vm_area(area, prot, &pages))
  1234. goto fail;
  1235. return area->addr;
  1236. fail:
  1237. vfree(area->addr);
  1238. return NULL;
  1239. }
  1240. void *__vmalloc_area(struct vm_struct *area, gfp_t gfp_mask, pgprot_t prot)
  1241. {
  1242. void *addr = __vmalloc_area_node(area, gfp_mask, prot, -1,
  1243. __builtin_return_address(0));
  1244. /*
  1245. * A ref_count = 3 is needed because the vm_struct and vmap_area
  1246. * structures allocated in the __get_vm_area_node() function contain
  1247. * references to the virtual address of the vmalloc'ed block.
  1248. */
  1249. kmemleak_alloc(addr, area->size - PAGE_SIZE, 3, gfp_mask);
  1250. return addr;
  1251. }
  1252. /**
  1253. * __vmalloc_node - allocate virtually contiguous memory
  1254. * @size: allocation size
  1255. * @gfp_mask: flags for the page level allocator
  1256. * @prot: protection mask for the allocated pages
  1257. * @node: node to use for allocation or -1
  1258. * @caller: caller's return address
  1259. *
  1260. * Allocate enough pages to cover @size from the page level
  1261. * allocator with @gfp_mask flags. Map them into contiguous
  1262. * kernel virtual space, using a pagetable protection of @prot.
  1263. */
  1264. static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
  1265. int node, void *caller)
  1266. {
  1267. struct vm_struct *area;
  1268. void *addr;
  1269. unsigned long real_size = size;
  1270. size = PAGE_ALIGN(size);
  1271. if (!size || (size >> PAGE_SHIFT) > num_physpages)
  1272. return NULL;
  1273. area = __get_vm_area_node(size, VM_ALLOC, VMALLOC_START, VMALLOC_END,
  1274. node, gfp_mask, caller);
  1275. if (!area)
  1276. return NULL;
  1277. addr = __vmalloc_area_node(area, gfp_mask, prot, node, caller);
  1278. /*
  1279. * A ref_count = 3 is needed because the vm_struct and vmap_area
  1280. * structures allocated in the __get_vm_area_node() function contain
  1281. * references to the virtual address of the vmalloc'ed block.
  1282. */
  1283. kmemleak_alloc(addr, real_size, 3, gfp_mask);
  1284. return addr;
  1285. }
  1286. void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
  1287. {
  1288. return __vmalloc_node(size, gfp_mask, prot, -1,
  1289. __builtin_return_address(0));
  1290. }
  1291. EXPORT_SYMBOL(__vmalloc);
  1292. /**
  1293. * vmalloc - allocate virtually contiguous memory
  1294. * @size: allocation size
  1295. * Allocate enough pages to cover @size from the page level
  1296. * allocator and map them into contiguous kernel virtual space.
  1297. *
  1298. * For tight control over page level allocator and protection flags
  1299. * use __vmalloc() instead.
  1300. */
  1301. void *vmalloc(unsigned long size)
  1302. {
  1303. return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
  1304. -1, __builtin_return_address(0));
  1305. }
  1306. EXPORT_SYMBOL(vmalloc);
  1307. /**
  1308. * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
  1309. * @size: allocation size
  1310. *
  1311. * The resulting memory area is zeroed so it can be mapped to userspace
  1312. * without leaking data.
  1313. */
  1314. void *vmalloc_user(unsigned long size)
  1315. {
  1316. struct vm_struct *area;
  1317. void *ret;
  1318. ret = __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
  1319. PAGE_KERNEL, -1, __builtin_return_address(0));
  1320. if (ret) {
  1321. area = find_vm_area(ret);
  1322. area->flags |= VM_USERMAP;
  1323. }
  1324. return ret;
  1325. }
  1326. EXPORT_SYMBOL(vmalloc_user);
  1327. /**
  1328. * vmalloc_node - allocate memory on a specific node
  1329. * @size: allocation size
  1330. * @node: numa node
  1331. *
  1332. * Allocate enough pages to cover @size from the page level
  1333. * allocator and map them into contiguous kernel virtual space.
  1334. *
  1335. * For tight control over page level allocator and protection flags
  1336. * use __vmalloc() instead.
  1337. */
  1338. void *vmalloc_node(unsigned long size, int node)
  1339. {
  1340. return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
  1341. node, __builtin_return_address(0));
  1342. }
  1343. EXPORT_SYMBOL(vmalloc_node);
  1344. #ifndef PAGE_KERNEL_EXEC
  1345. # define PAGE_KERNEL_EXEC PAGE_KERNEL
  1346. #endif
  1347. /**
  1348. * vmalloc_exec - allocate virtually contiguous, executable memory
  1349. * @size: allocation size
  1350. *
  1351. * Kernel-internal function to allocate enough pages to cover @size
  1352. * the page level allocator and map them into contiguous and
  1353. * executable kernel virtual space.
  1354. *
  1355. * For tight control over page level allocator and protection flags
  1356. * use __vmalloc() instead.
  1357. */
  1358. void *vmalloc_exec(unsigned long size)
  1359. {
  1360. return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC,
  1361. -1, __builtin_return_address(0));
  1362. }
  1363. #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
  1364. #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
  1365. #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
  1366. #define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL
  1367. #else
  1368. #define GFP_VMALLOC32 GFP_KERNEL
  1369. #endif
  1370. /**
  1371. * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
  1372. * @size: allocation size
  1373. *
  1374. * Allocate enough 32bit PA addressable pages to cover @size from the
  1375. * page level allocator and map them into contiguous kernel virtual space.
  1376. */
  1377. void *vmalloc_32(unsigned long size)
  1378. {
  1379. return __vmalloc_node(size, GFP_VMALLOC32, PAGE_KERNEL,
  1380. -1, __builtin_return_address(0));
  1381. }
  1382. EXPORT_SYMBOL(vmalloc_32);
  1383. /**
  1384. * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
  1385. * @size: allocation size
  1386. *
  1387. * The resulting memory area is 32bit addressable and zeroed so it can be
  1388. * mapped to userspace without leaking data.
  1389. */
  1390. void *vmalloc_32_user(unsigned long size)
  1391. {
  1392. struct vm_struct *area;
  1393. void *ret;
  1394. ret = __vmalloc_node(size, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
  1395. -1, __builtin_return_address(0));
  1396. if (ret) {
  1397. area = find_vm_area(ret);
  1398. area->flags |= VM_USERMAP;
  1399. }
  1400. return ret;
  1401. }
  1402. EXPORT_SYMBOL(vmalloc_32_user);
  1403. long vread(char *buf, char *addr, unsigned long count)
  1404. {
  1405. struct vm_struct *tmp;
  1406. char *vaddr, *buf_start = buf;
  1407. unsigned long n;
  1408. /* Don't allow overflow */
  1409. if ((unsigned long) addr + count < count)
  1410. count = -(unsigned long) addr;
  1411. read_lock(&vmlist_lock);
  1412. for (tmp = vmlist; tmp; tmp = tmp->next) {
  1413. vaddr = (char *) tmp->addr;
  1414. if (addr >= vaddr + tmp->size - PAGE_SIZE)
  1415. continue;
  1416. while (addr < vaddr) {
  1417. if (count == 0)
  1418. goto finished;
  1419. *buf = '\0';
  1420. buf++;
  1421. addr++;
  1422. count--;
  1423. }
  1424. n = vaddr + tmp->size - PAGE_SIZE - addr;
  1425. do {
  1426. if (count == 0)
  1427. goto finished;
  1428. *buf = *addr;
  1429. buf++;
  1430. addr++;
  1431. count--;
  1432. } while (--n > 0);
  1433. }
  1434. finished:
  1435. read_unlock(&vmlist_lock);
  1436. return buf - buf_start;
  1437. }
  1438. long vwrite(char *buf, char *addr, unsigned long count)
  1439. {
  1440. struct vm_struct *tmp;
  1441. char *vaddr, *buf_start = buf;
  1442. unsigned long n;
  1443. /* Don't allow overflow */
  1444. if ((unsigned long) addr + count < count)
  1445. count = -(unsigned long) addr;
  1446. read_lock(&vmlist_lock);
  1447. for (tmp = vmlist; tmp; tmp = tmp->next) {
  1448. vaddr = (char *) tmp->addr;
  1449. if (addr >= vaddr + tmp->size - PAGE_SIZE)
  1450. continue;
  1451. while (addr < vaddr) {
  1452. if (count == 0)
  1453. goto finished;
  1454. buf++;
  1455. addr++;
  1456. count--;
  1457. }
  1458. n = vaddr + tmp->size - PAGE_SIZE - addr;
  1459. do {
  1460. if (count == 0)
  1461. goto finished;
  1462. *addr = *buf;
  1463. buf++;
  1464. addr++;
  1465. count--;
  1466. } while (--n > 0);
  1467. }
  1468. finished:
  1469. read_unlock(&vmlist_lock);
  1470. return buf - buf_start;
  1471. }
  1472. /**
  1473. * remap_vmalloc_range - map vmalloc pages to userspace
  1474. * @vma: vma to cover (map full range of vma)
  1475. * @addr: vmalloc memory
  1476. * @pgoff: number of pages into addr before first page to map
  1477. *
  1478. * Returns: 0 for success, -Exxx on failure
  1479. *
  1480. * This function checks that addr is a valid vmalloc'ed area, and
  1481. * that it is big enough to cover the vma. Will return failure if
  1482. * that criteria isn't met.
  1483. *
  1484. * Similar to remap_pfn_range() (see mm/memory.c)
  1485. */
  1486. int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
  1487. unsigned long pgoff)
  1488. {
  1489. struct vm_struct *area;
  1490. unsigned long uaddr = vma->vm_start;
  1491. unsigned long usize = vma->vm_end - vma->vm_start;
  1492. if ((PAGE_SIZE-1) & (unsigned long)addr)
  1493. return -EINVAL;
  1494. area = find_vm_area(addr);
  1495. if (!area)
  1496. return -EINVAL;
  1497. if (!(area->flags & VM_USERMAP))
  1498. return -EINVAL;
  1499. if (usize + (pgoff << PAGE_SHIFT) > area->size - PAGE_SIZE)
  1500. return -EINVAL;
  1501. addr += pgoff << PAGE_SHIFT;
  1502. do {
  1503. struct page *page = vmalloc_to_page(addr);
  1504. int ret;
  1505. ret = vm_insert_page(vma, uaddr, page);
  1506. if (ret)
  1507. return ret;
  1508. uaddr += PAGE_SIZE;
  1509. addr += PAGE_SIZE;
  1510. usize -= PAGE_SIZE;
  1511. } while (usize > 0);
  1512. /* Prevent "things" like memory migration? VM_flags need a cleanup... */
  1513. vma->vm_flags |= VM_RESERVED;
  1514. return 0;
  1515. }
  1516. EXPORT_SYMBOL(remap_vmalloc_range);
  1517. /*
  1518. * Implement a stub for vmalloc_sync_all() if the architecture chose not to
  1519. * have one.
  1520. */
  1521. void __attribute__((weak)) vmalloc_sync_all(void)
  1522. {
  1523. }
  1524. static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
  1525. {
  1526. /* apply_to_page_range() does all the hard work. */
  1527. return 0;
  1528. }
  1529. /**
  1530. * alloc_vm_area - allocate a range of kernel address space
  1531. * @size: size of the area
  1532. *
  1533. * Returns: NULL on failure, vm_struct on success
  1534. *
  1535. * This function reserves a range of kernel address space, and
  1536. * allocates pagetables to map that range. No actual mappings
  1537. * are created. If the kernel address space is not shared
  1538. * between processes, it syncs the pagetable across all
  1539. * processes.
  1540. */
  1541. struct vm_struct *alloc_vm_area(size_t size)
  1542. {
  1543. struct vm_struct *area;
  1544. area = get_vm_area_caller(size, VM_IOREMAP,
  1545. __builtin_return_address(0));
  1546. if (area == NULL)
  1547. return NULL;
  1548. /*
  1549. * This ensures that page tables are constructed for this region
  1550. * of kernel virtual address space and mapped into init_mm.
  1551. */
  1552. if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
  1553. area->size, f, NULL)) {
  1554. free_vm_area(area);
  1555. return NULL;
  1556. }
  1557. /* Make sure the pagetables are constructed in process kernel
  1558. mappings */
  1559. vmalloc_sync_all();
  1560. return area;
  1561. }
  1562. EXPORT_SYMBOL_GPL(alloc_vm_area);
  1563. void free_vm_area(struct vm_struct *area)
  1564. {
  1565. struct vm_struct *ret;
  1566. ret = remove_vm_area(area->addr);
  1567. BUG_ON(ret != area);
  1568. kfree(area);
  1569. }
  1570. EXPORT_SYMBOL_GPL(free_vm_area);
  1571. static struct vmap_area *node_to_va(struct rb_node *n)
  1572. {
  1573. return n ? rb_entry(n, struct vmap_area, rb_node) : NULL;
  1574. }
  1575. /**
  1576. * pvm_find_next_prev - find the next and prev vmap_area surrounding @end
  1577. * @end: target address
  1578. * @pnext: out arg for the next vmap_area
  1579. * @pprev: out arg for the previous vmap_area
  1580. *
  1581. * Returns: %true if either or both of next and prev are found,
  1582. * %false if no vmap_area exists
  1583. *
  1584. * Find vmap_areas end addresses of which enclose @end. ie. if not
  1585. * NULL, *pnext->va_end > @end and *pprev->va_end <= @end.
  1586. */
  1587. static bool pvm_find_next_prev(unsigned long end,
  1588. struct vmap_area **pnext,
  1589. struct vmap_area **pprev)
  1590. {
  1591. struct rb_node *n = vmap_area_root.rb_node;
  1592. struct vmap_area *va = NULL;
  1593. while (n) {
  1594. va = rb_entry(n, struct vmap_area, rb_node);
  1595. if (end < va->va_end)
  1596. n = n->rb_left;
  1597. else if (end > va->va_end)
  1598. n = n->rb_right;
  1599. else
  1600. break;
  1601. }
  1602. if (!va)
  1603. return false;
  1604. if (va->va_end > end) {
  1605. *pnext = va;
  1606. *pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
  1607. } else {
  1608. *pprev = va;
  1609. *pnext = node_to_va(rb_next(&(*pprev)->rb_node));
  1610. }
  1611. return true;
  1612. }
  1613. /**
  1614. * pvm_determine_end - find the highest aligned address between two vmap_areas
  1615. * @pnext: in/out arg for the next vmap_area
  1616. * @pprev: in/out arg for the previous vmap_area
  1617. * @align: alignment
  1618. *
  1619. * Returns: determined end address
  1620. *
  1621. * Find the highest aligned address between *@pnext and *@pprev below
  1622. * VMALLOC_END. *@pnext and *@pprev are adjusted so that the aligned
  1623. * down address is between the end addresses of the two vmap_areas.
  1624. *
  1625. * Please note that the address returned by this function may fall
  1626. * inside *@pnext vmap_area. The caller is responsible for checking
  1627. * that.
  1628. */
  1629. static unsigned long pvm_determine_end(struct vmap_area **pnext,
  1630. struct vmap_area **pprev,
  1631. unsigned long align)
  1632. {
  1633. const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
  1634. unsigned long addr;
  1635. if (*pnext)
  1636. addr = min((*pnext)->va_start & ~(align - 1), vmalloc_end);
  1637. else
  1638. addr = vmalloc_end;
  1639. while (*pprev && (*pprev)->va_end > addr) {
  1640. *pnext = *pprev;
  1641. *pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
  1642. }
  1643. return addr;
  1644. }
  1645. /**
  1646. * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator
  1647. * @offsets: array containing offset of each area
  1648. * @sizes: array containing size of each area
  1649. * @nr_vms: the number of areas to allocate
  1650. * @align: alignment, all entries in @offsets and @sizes must be aligned to this
  1651. * @gfp_mask: allocation mask
  1652. *
  1653. * Returns: kmalloc'd vm_struct pointer array pointing to allocated
  1654. * vm_structs on success, %NULL on failure
  1655. *
  1656. * Percpu allocator wants to use congruent vm areas so that it can
  1657. * maintain the offsets among percpu areas. This function allocates
  1658. * congruent vmalloc areas for it. These areas tend to be scattered
  1659. * pretty far, distance between two areas easily going up to
  1660. * gigabytes. To avoid interacting with regular vmallocs, these areas
  1661. * are allocated from top.
  1662. *
  1663. * Despite its complicated look, this allocator is rather simple. It
  1664. * does everything top-down and scans areas from the end looking for
  1665. * matching slot. While scanning, if any of the areas overlaps with
  1666. * existing vmap_area, the base address is pulled down to fit the
  1667. * area. Scanning is repeated till all the areas fit and then all
  1668. * necessary data structres are inserted and the result is returned.
  1669. */
  1670. struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
  1671. const size_t *sizes, int nr_vms,
  1672. size_t align, gfp_t gfp_mask)
  1673. {
  1674. const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align);
  1675. const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
  1676. struct vmap_area **vas, *prev, *next;
  1677. struct vm_struct **vms;
  1678. int area, area2, last_area, term_area;
  1679. unsigned long base, start, end, last_end;
  1680. bool purged = false;
  1681. gfp_mask &= GFP_RECLAIM_MASK;
  1682. /* verify parameters and allocate data structures */
  1683. BUG_ON(align & ~PAGE_MASK || !is_power_of_2(align));
  1684. for (last_area = 0, area = 0; area < nr_vms; area++) {
  1685. start = offsets[area];
  1686. end = start + sizes[area];
  1687. /* is everything aligned properly? */
  1688. BUG_ON(!IS_ALIGNED(offsets[area], align));
  1689. BUG_ON(!IS_ALIGNED(sizes[area], align));
  1690. /* detect the area with the highest address */
  1691. if (start > offsets[last_area])
  1692. last_area = area;
  1693. for (area2 = 0; area2 < nr_vms; area2++) {
  1694. unsigned long start2 = offsets[area2];
  1695. unsigned long end2 = start2 + sizes[area2];
  1696. if (area2 == area)
  1697. continue;
  1698. BUG_ON(start2 >= start && start2 < end);
  1699. BUG_ON(end2 <= end && end2 > start);
  1700. }
  1701. }
  1702. last_end = offsets[last_area] + sizes[last_area];
  1703. if (vmalloc_end - vmalloc_start < last_end) {
  1704. WARN_ON(true);
  1705. return NULL;
  1706. }
  1707. vms = kzalloc(sizeof(vms[0]) * nr_vms, gfp_mask);
  1708. vas = kzalloc(sizeof(vas[0]) * nr_vms, gfp_mask);
  1709. if (!vas || !vms)
  1710. goto err_free;
  1711. for (area = 0; area < nr_vms; area++) {
  1712. vas[area] = kzalloc(sizeof(struct vmap_area), gfp_mask);
  1713. vms[area] = kzalloc(sizeof(struct vm_struct), gfp_mask);
  1714. if (!vas[area] || !vms[area])
  1715. goto err_free;
  1716. }
  1717. retry:
  1718. spin_lock(&vmap_area_lock);
  1719. /* start scanning - we scan from the top, begin with the last area */
  1720. area = term_area = last_area;
  1721. start = offsets[area];
  1722. end = start + sizes[area];
  1723. if (!pvm_find_next_prev(vmap_area_pcpu_hole, &next, &prev)) {
  1724. base = vmalloc_end - last_end;
  1725. goto found;
  1726. }
  1727. base = pvm_determine_end(&next, &prev, align) - end;
  1728. while (true) {
  1729. BUG_ON(next && next->va_end <= base + end);
  1730. BUG_ON(prev && prev->va_end > base + end);
  1731. /*
  1732. * base might have underflowed, add last_end before
  1733. * comparing.
  1734. */
  1735. if (base + last_end < vmalloc_start + last_end) {
  1736. spin_unlock(&vmap_area_lock);
  1737. if (!purged) {
  1738. purge_vmap_area_lazy();
  1739. purged = true;
  1740. goto retry;
  1741. }
  1742. goto err_free;
  1743. }
  1744. /*
  1745. * If next overlaps, move base downwards so that it's
  1746. * right below next and then recheck.
  1747. */
  1748. if (next && next->va_start < base + end) {
  1749. base = pvm_determine_end(&next, &prev, align) - end;
  1750. term_area = area;
  1751. continue;
  1752. }
  1753. /*
  1754. * If prev overlaps, shift down next and prev and move
  1755. * base so that it's right below new next and then
  1756. * recheck.
  1757. */
  1758. if (prev && prev->va_end > base + start) {
  1759. next = prev;
  1760. prev = node_to_va(rb_prev(&next->rb_node));
  1761. base = pvm_determine_end(&next, &prev, align) - end;
  1762. term_area = area;
  1763. continue;
  1764. }
  1765. /*
  1766. * This area fits, move on to the previous one. If
  1767. * the previous one is the terminal one, we're done.
  1768. */
  1769. area = (area + nr_vms - 1) % nr_vms;
  1770. if (area == term_area)
  1771. break;
  1772. start = offsets[area];
  1773. end = start + sizes[area];
  1774. pvm_find_next_prev(base + end, &next, &prev);
  1775. }
  1776. found:
  1777. /* we've found a fitting base, insert all va's */
  1778. for (area = 0; area < nr_vms; area++) {
  1779. struct vmap_area *va = vas[area];
  1780. va->va_start = base + offsets[area];
  1781. va->va_end = va->va_start + sizes[area];
  1782. __insert_vmap_area(va);
  1783. }
  1784. vmap_area_pcpu_hole = base + offsets[last_area];
  1785. spin_unlock(&vmap_area_lock);
  1786. /* insert all vm's */
  1787. for (area = 0; area < nr_vms; area++)
  1788. insert_vmalloc_vm(vms[area], vas[area], VM_ALLOC,
  1789. pcpu_get_vm_areas);
  1790. kfree(vas);
  1791. return vms;
  1792. err_free:
  1793. for (area = 0; area < nr_vms; area++) {
  1794. if (vas)
  1795. kfree(vas[area]);
  1796. if (vms)
  1797. kfree(vms[area]);
  1798. }
  1799. kfree(vas);
  1800. kfree(vms);
  1801. return NULL;
  1802. }
  1803. /**
  1804. * pcpu_free_vm_areas - free vmalloc areas for percpu allocator
  1805. * @vms: vm_struct pointer array returned by pcpu_get_vm_areas()
  1806. * @nr_vms: the number of allocated areas
  1807. *
  1808. * Free vm_structs and the array allocated by pcpu_get_vm_areas().
  1809. */
  1810. void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
  1811. {
  1812. int i;
  1813. for (i = 0; i < nr_vms; i++)
  1814. free_vm_area(vms[i]);
  1815. kfree(vms);
  1816. }
  1817. #ifdef CONFIG_PROC_FS
  1818. static void *s_start(struct seq_file *m, loff_t *pos)
  1819. {
  1820. loff_t n = *pos;
  1821. struct vm_struct *v;
  1822. read_lock(&vmlist_lock);
  1823. v = vmlist;
  1824. while (n > 0 && v) {
  1825. n--;
  1826. v = v->next;
  1827. }
  1828. if (!n)
  1829. return v;
  1830. return NULL;
  1831. }
  1832. static void *s_next(struct seq_file *m, void *p, loff_t *pos)
  1833. {
  1834. struct vm_struct *v = p;
  1835. ++*pos;
  1836. return v->next;
  1837. }
  1838. static void s_stop(struct seq_file *m, void *p)
  1839. {
  1840. read_unlock(&vmlist_lock);
  1841. }
  1842. static void show_numa_info(struct seq_file *m, struct vm_struct *v)
  1843. {
  1844. if (NUMA_BUILD) {
  1845. unsigned int nr, *counters = m->private;
  1846. if (!counters)
  1847. return;
  1848. memset(counters, 0, nr_node_ids * sizeof(unsigned int));
  1849. for (nr = 0; nr < v->nr_pages; nr++)
  1850. counters[page_to_nid(v->pages[nr])]++;
  1851. for_each_node_state(nr, N_HIGH_MEMORY)
  1852. if (counters[nr])
  1853. seq_printf(m, " N%u=%u", nr, counters[nr]);
  1854. }
  1855. }
  1856. static int s_show(struct seq_file *m, void *p)
  1857. {
  1858. struct vm_struct *v = p;
  1859. seq_printf(m, "0x%p-0x%p %7ld",
  1860. v->addr, v->addr + v->size, v->size);
  1861. if (v->caller) {
  1862. char buff[KSYM_SYMBOL_LEN];
  1863. seq_putc(m, ' ');
  1864. sprint_symbol(buff, (unsigned long)v->caller);
  1865. seq_puts(m, buff);
  1866. }
  1867. if (v->nr_pages)
  1868. seq_printf(m, " pages=%d", v->nr_pages);
  1869. if (v->phys_addr)
  1870. seq_printf(m, " phys=%lx", v->phys_addr);
  1871. if (v->flags & VM_IOREMAP)
  1872. seq_printf(m, " ioremap");
  1873. if (v->flags & VM_ALLOC)
  1874. seq_printf(m, " vmalloc");
  1875. if (v->flags & VM_MAP)
  1876. seq_printf(m, " vmap");
  1877. if (v->flags & VM_USERMAP)
  1878. seq_printf(m, " user");
  1879. if (v->flags & VM_VPAGES)
  1880. seq_printf(m, " vpages");
  1881. show_numa_info(m, v);
  1882. seq_putc(m, '\n');
  1883. return 0;
  1884. }
  1885. static const struct seq_operations vmalloc_op = {
  1886. .start = s_start,
  1887. .next = s_next,
  1888. .stop = s_stop,
  1889. .show = s_show,
  1890. };
  1891. static int vmalloc_open(struct inode *inode, struct file *file)
  1892. {
  1893. unsigned int *ptr = NULL;
  1894. int ret;
  1895. if (NUMA_BUILD)
  1896. ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
  1897. ret = seq_open(file, &vmalloc_op);
  1898. if (!ret) {
  1899. struct seq_file *m = file->private_data;
  1900. m->private = ptr;
  1901. } else
  1902. kfree(ptr);
  1903. return ret;
  1904. }
  1905. static const struct file_operations proc_vmalloc_operations = {
  1906. .open = vmalloc_open,
  1907. .read = seq_read,
  1908. .llseek = seq_lseek,
  1909. .release = seq_release_private,
  1910. };
  1911. static int __init proc_vmalloc_init(void)
  1912. {
  1913. proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
  1914. return 0;
  1915. }
  1916. module_init(proc_vmalloc_init);
  1917. #endif