vmalloc.c 70 KB

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