vmalloc.c 70 KB

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