vmalloc.c 61 KB

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