vmalloc.c 70 KB

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