hugetlb.c 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459
  1. /*
  2. * Generic hugetlb support.
  3. * (C) Nadia Yvette Chambers, April 2004
  4. */
  5. #include <linux/list.h>
  6. #include <linux/init.h>
  7. #include <linux/module.h>
  8. #include <linux/mm.h>
  9. #include <linux/seq_file.h>
  10. #include <linux/sysctl.h>
  11. #include <linux/highmem.h>
  12. #include <linux/mmu_notifier.h>
  13. #include <linux/nodemask.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/mempolicy.h>
  16. #include <linux/cpuset.h>
  17. #include <linux/mutex.h>
  18. #include <linux/bootmem.h>
  19. #include <linux/sysfs.h>
  20. #include <linux/slab.h>
  21. #include <linux/rmap.h>
  22. #include <linux/swap.h>
  23. #include <linux/swapops.h>
  24. #include <asm/page.h>
  25. #include <asm/pgtable.h>
  26. #include <asm/tlb.h>
  27. #include <linux/io.h>
  28. #include <linux/hugetlb.h>
  29. #include <linux/hugetlb_cgroup.h>
  30. #include <linux/node.h>
  31. #include "internal.h"
  32. const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL;
  33. static gfp_t htlb_alloc_mask = GFP_HIGHUSER;
  34. unsigned long hugepages_treat_as_movable;
  35. int hugetlb_max_hstate __read_mostly;
  36. unsigned int default_hstate_idx;
  37. struct hstate hstates[HUGE_MAX_HSTATE];
  38. __initdata LIST_HEAD(huge_boot_pages);
  39. /* for command line parsing */
  40. static struct hstate * __initdata parsed_hstate;
  41. static unsigned long __initdata default_hstate_max_huge_pages;
  42. static unsigned long __initdata default_hstate_size;
  43. /*
  44. * Protects updates to hugepage_freelists, hugepage_activelist, nr_huge_pages,
  45. * free_huge_pages, and surplus_huge_pages.
  46. */
  47. DEFINE_SPINLOCK(hugetlb_lock);
  48. static inline void unlock_or_release_subpool(struct hugepage_subpool *spool)
  49. {
  50. bool free = (spool->count == 0) && (spool->used_hpages == 0);
  51. spin_unlock(&spool->lock);
  52. /* If no pages are used, and no other handles to the subpool
  53. * remain, free the subpool the subpool remain */
  54. if (free)
  55. kfree(spool);
  56. }
  57. struct hugepage_subpool *hugepage_new_subpool(long nr_blocks)
  58. {
  59. struct hugepage_subpool *spool;
  60. spool = kmalloc(sizeof(*spool), GFP_KERNEL);
  61. if (!spool)
  62. return NULL;
  63. spin_lock_init(&spool->lock);
  64. spool->count = 1;
  65. spool->max_hpages = nr_blocks;
  66. spool->used_hpages = 0;
  67. return spool;
  68. }
  69. void hugepage_put_subpool(struct hugepage_subpool *spool)
  70. {
  71. spin_lock(&spool->lock);
  72. BUG_ON(!spool->count);
  73. spool->count--;
  74. unlock_or_release_subpool(spool);
  75. }
  76. static int hugepage_subpool_get_pages(struct hugepage_subpool *spool,
  77. long delta)
  78. {
  79. int ret = 0;
  80. if (!spool)
  81. return 0;
  82. spin_lock(&spool->lock);
  83. if ((spool->used_hpages + delta) <= spool->max_hpages) {
  84. spool->used_hpages += delta;
  85. } else {
  86. ret = -ENOMEM;
  87. }
  88. spin_unlock(&spool->lock);
  89. return ret;
  90. }
  91. static void hugepage_subpool_put_pages(struct hugepage_subpool *spool,
  92. long delta)
  93. {
  94. if (!spool)
  95. return;
  96. spin_lock(&spool->lock);
  97. spool->used_hpages -= delta;
  98. /* If hugetlbfs_put_super couldn't free spool due to
  99. * an outstanding quota reference, free it now. */
  100. unlock_or_release_subpool(spool);
  101. }
  102. static inline struct hugepage_subpool *subpool_inode(struct inode *inode)
  103. {
  104. return HUGETLBFS_SB(inode->i_sb)->spool;
  105. }
  106. static inline struct hugepage_subpool *subpool_vma(struct vm_area_struct *vma)
  107. {
  108. return subpool_inode(file_inode(vma->vm_file));
  109. }
  110. /*
  111. * Region tracking -- allows tracking of reservations and instantiated pages
  112. * across the pages in a mapping.
  113. *
  114. * The region data structures are protected by a combination of the mmap_sem
  115. * and the hugetlb_instantiation_mutex. To access or modify a region the caller
  116. * must either hold the mmap_sem for write, or the mmap_sem for read and
  117. * the hugetlb_instantiation_mutex:
  118. *
  119. * down_write(&mm->mmap_sem);
  120. * or
  121. * down_read(&mm->mmap_sem);
  122. * mutex_lock(&hugetlb_instantiation_mutex);
  123. */
  124. struct file_region {
  125. struct list_head link;
  126. long from;
  127. long to;
  128. };
  129. static long region_add(struct list_head *head, long f, long t)
  130. {
  131. struct file_region *rg, *nrg, *trg;
  132. /* Locate the region we are either in or before. */
  133. list_for_each_entry(rg, head, link)
  134. if (f <= rg->to)
  135. break;
  136. /* Round our left edge to the current segment if it encloses us. */
  137. if (f > rg->from)
  138. f = rg->from;
  139. /* Check for and consume any regions we now overlap with. */
  140. nrg = rg;
  141. list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
  142. if (&rg->link == head)
  143. break;
  144. if (rg->from > t)
  145. break;
  146. /* If this area reaches higher then extend our area to
  147. * include it completely. If this is not the first area
  148. * which we intend to reuse, free it. */
  149. if (rg->to > t)
  150. t = rg->to;
  151. if (rg != nrg) {
  152. list_del(&rg->link);
  153. kfree(rg);
  154. }
  155. }
  156. nrg->from = f;
  157. nrg->to = t;
  158. return 0;
  159. }
  160. static long region_chg(struct list_head *head, long f, long t)
  161. {
  162. struct file_region *rg, *nrg;
  163. long chg = 0;
  164. /* Locate the region we are before or in. */
  165. list_for_each_entry(rg, head, link)
  166. if (f <= rg->to)
  167. break;
  168. /* If we are below the current region then a new region is required.
  169. * Subtle, allocate a new region at the position but make it zero
  170. * size such that we can guarantee to record the reservation. */
  171. if (&rg->link == head || t < rg->from) {
  172. nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
  173. if (!nrg)
  174. return -ENOMEM;
  175. nrg->from = f;
  176. nrg->to = f;
  177. INIT_LIST_HEAD(&nrg->link);
  178. list_add(&nrg->link, rg->link.prev);
  179. return t - f;
  180. }
  181. /* Round our left edge to the current segment if it encloses us. */
  182. if (f > rg->from)
  183. f = rg->from;
  184. chg = t - f;
  185. /* Check for and consume any regions we now overlap with. */
  186. list_for_each_entry(rg, rg->link.prev, link) {
  187. if (&rg->link == head)
  188. break;
  189. if (rg->from > t)
  190. return chg;
  191. /* We overlap with this area, if it extends further than
  192. * us then we must extend ourselves. Account for its
  193. * existing reservation. */
  194. if (rg->to > t) {
  195. chg += rg->to - t;
  196. t = rg->to;
  197. }
  198. chg -= rg->to - rg->from;
  199. }
  200. return chg;
  201. }
  202. static long region_truncate(struct list_head *head, long end)
  203. {
  204. struct file_region *rg, *trg;
  205. long chg = 0;
  206. /* Locate the region we are either in or before. */
  207. list_for_each_entry(rg, head, link)
  208. if (end <= rg->to)
  209. break;
  210. if (&rg->link == head)
  211. return 0;
  212. /* If we are in the middle of a region then adjust it. */
  213. if (end > rg->from) {
  214. chg = rg->to - end;
  215. rg->to = end;
  216. rg = list_entry(rg->link.next, typeof(*rg), link);
  217. }
  218. /* Drop any remaining regions. */
  219. list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
  220. if (&rg->link == head)
  221. break;
  222. chg += rg->to - rg->from;
  223. list_del(&rg->link);
  224. kfree(rg);
  225. }
  226. return chg;
  227. }
  228. static long region_count(struct list_head *head, long f, long t)
  229. {
  230. struct file_region *rg;
  231. long chg = 0;
  232. /* Locate each segment we overlap with, and count that overlap. */
  233. list_for_each_entry(rg, head, link) {
  234. long seg_from;
  235. long seg_to;
  236. if (rg->to <= f)
  237. continue;
  238. if (rg->from >= t)
  239. break;
  240. seg_from = max(rg->from, f);
  241. seg_to = min(rg->to, t);
  242. chg += seg_to - seg_from;
  243. }
  244. return chg;
  245. }
  246. /*
  247. * Convert the address within this vma to the page offset within
  248. * the mapping, in pagecache page units; huge pages here.
  249. */
  250. static pgoff_t vma_hugecache_offset(struct hstate *h,
  251. struct vm_area_struct *vma, unsigned long address)
  252. {
  253. return ((address - vma->vm_start) >> huge_page_shift(h)) +
  254. (vma->vm_pgoff >> huge_page_order(h));
  255. }
  256. pgoff_t linear_hugepage_index(struct vm_area_struct *vma,
  257. unsigned long address)
  258. {
  259. return vma_hugecache_offset(hstate_vma(vma), vma, address);
  260. }
  261. /*
  262. * Return the size of the pages allocated when backing a VMA. In the majority
  263. * cases this will be same size as used by the page table entries.
  264. */
  265. unsigned long vma_kernel_pagesize(struct vm_area_struct *vma)
  266. {
  267. struct hstate *hstate;
  268. if (!is_vm_hugetlb_page(vma))
  269. return PAGE_SIZE;
  270. hstate = hstate_vma(vma);
  271. return 1UL << huge_page_shift(hstate);
  272. }
  273. EXPORT_SYMBOL_GPL(vma_kernel_pagesize);
  274. /*
  275. * Return the page size being used by the MMU to back a VMA. In the majority
  276. * of cases, the page size used by the kernel matches the MMU size. On
  277. * architectures where it differs, an architecture-specific version of this
  278. * function is required.
  279. */
  280. #ifndef vma_mmu_pagesize
  281. unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
  282. {
  283. return vma_kernel_pagesize(vma);
  284. }
  285. #endif
  286. /*
  287. * Flags for MAP_PRIVATE reservations. These are stored in the bottom
  288. * bits of the reservation map pointer, which are always clear due to
  289. * alignment.
  290. */
  291. #define HPAGE_RESV_OWNER (1UL << 0)
  292. #define HPAGE_RESV_UNMAPPED (1UL << 1)
  293. #define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
  294. /*
  295. * These helpers are used to track how many pages are reserved for
  296. * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
  297. * is guaranteed to have their future faults succeed.
  298. *
  299. * With the exception of reset_vma_resv_huge_pages() which is called at fork(),
  300. * the reserve counters are updated with the hugetlb_lock held. It is safe
  301. * to reset the VMA at fork() time as it is not in use yet and there is no
  302. * chance of the global counters getting corrupted as a result of the values.
  303. *
  304. * The private mapping reservation is represented in a subtly different
  305. * manner to a shared mapping. A shared mapping has a region map associated
  306. * with the underlying file, this region map represents the backing file
  307. * pages which have ever had a reservation assigned which this persists even
  308. * after the page is instantiated. A private mapping has a region map
  309. * associated with the original mmap which is attached to all VMAs which
  310. * reference it, this region map represents those offsets which have consumed
  311. * reservation ie. where pages have been instantiated.
  312. */
  313. static unsigned long get_vma_private_data(struct vm_area_struct *vma)
  314. {
  315. return (unsigned long)vma->vm_private_data;
  316. }
  317. static void set_vma_private_data(struct vm_area_struct *vma,
  318. unsigned long value)
  319. {
  320. vma->vm_private_data = (void *)value;
  321. }
  322. struct resv_map {
  323. struct kref refs;
  324. struct list_head regions;
  325. };
  326. static struct resv_map *resv_map_alloc(void)
  327. {
  328. struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL);
  329. if (!resv_map)
  330. return NULL;
  331. kref_init(&resv_map->refs);
  332. INIT_LIST_HEAD(&resv_map->regions);
  333. return resv_map;
  334. }
  335. static void resv_map_release(struct kref *ref)
  336. {
  337. struct resv_map *resv_map = container_of(ref, struct resv_map, refs);
  338. /* Clear out any active regions before we release the map. */
  339. region_truncate(&resv_map->regions, 0);
  340. kfree(resv_map);
  341. }
  342. static struct resv_map *vma_resv_map(struct vm_area_struct *vma)
  343. {
  344. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  345. if (!(vma->vm_flags & VM_MAYSHARE))
  346. return (struct resv_map *)(get_vma_private_data(vma) &
  347. ~HPAGE_RESV_MASK);
  348. return NULL;
  349. }
  350. static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map)
  351. {
  352. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  353. VM_BUG_ON(vma->vm_flags & VM_MAYSHARE);
  354. set_vma_private_data(vma, (get_vma_private_data(vma) &
  355. HPAGE_RESV_MASK) | (unsigned long)map);
  356. }
  357. static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
  358. {
  359. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  360. VM_BUG_ON(vma->vm_flags & VM_MAYSHARE);
  361. set_vma_private_data(vma, get_vma_private_data(vma) | flags);
  362. }
  363. static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
  364. {
  365. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  366. return (get_vma_private_data(vma) & flag) != 0;
  367. }
  368. /* Reset counters to 0 and clear all HPAGE_RESV_* flags */
  369. void reset_vma_resv_huge_pages(struct vm_area_struct *vma)
  370. {
  371. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  372. if (!(vma->vm_flags & VM_MAYSHARE))
  373. vma->vm_private_data = (void *)0;
  374. }
  375. /* Returns true if the VMA has associated reserve pages */
  376. static int vma_has_reserves(struct vm_area_struct *vma, long chg)
  377. {
  378. if (vma->vm_flags & VM_NORESERVE) {
  379. /*
  380. * This address is already reserved by other process(chg == 0),
  381. * so, we should decrement reserved count. Without decrementing,
  382. * reserve count remains after releasing inode, because this
  383. * allocated page will go into page cache and is regarded as
  384. * coming from reserved pool in releasing step. Currently, we
  385. * don't have any other solution to deal with this situation
  386. * properly, so add work-around here.
  387. */
  388. if (vma->vm_flags & VM_MAYSHARE && chg == 0)
  389. return 1;
  390. else
  391. return 0;
  392. }
  393. /* Shared mappings always use reserves */
  394. if (vma->vm_flags & VM_MAYSHARE)
  395. return 1;
  396. /*
  397. * Only the process that called mmap() has reserves for
  398. * private mappings.
  399. */
  400. if (is_vma_resv_set(vma, HPAGE_RESV_OWNER))
  401. return 1;
  402. return 0;
  403. }
  404. static void copy_gigantic_page(struct page *dst, struct page *src)
  405. {
  406. int i;
  407. struct hstate *h = page_hstate(src);
  408. struct page *dst_base = dst;
  409. struct page *src_base = src;
  410. for (i = 0; i < pages_per_huge_page(h); ) {
  411. cond_resched();
  412. copy_highpage(dst, src);
  413. i++;
  414. dst = mem_map_next(dst, dst_base, i);
  415. src = mem_map_next(src, src_base, i);
  416. }
  417. }
  418. void copy_huge_page(struct page *dst, struct page *src)
  419. {
  420. int i;
  421. struct hstate *h = page_hstate(src);
  422. if (unlikely(pages_per_huge_page(h) > MAX_ORDER_NR_PAGES)) {
  423. copy_gigantic_page(dst, src);
  424. return;
  425. }
  426. might_sleep();
  427. for (i = 0; i < pages_per_huge_page(h); i++) {
  428. cond_resched();
  429. copy_highpage(dst + i, src + i);
  430. }
  431. }
  432. static void enqueue_huge_page(struct hstate *h, struct page *page)
  433. {
  434. int nid = page_to_nid(page);
  435. list_move(&page->lru, &h->hugepage_freelists[nid]);
  436. h->free_huge_pages++;
  437. h->free_huge_pages_node[nid]++;
  438. }
  439. static struct page *dequeue_huge_page_node(struct hstate *h, int nid)
  440. {
  441. struct page *page;
  442. if (list_empty(&h->hugepage_freelists[nid]))
  443. return NULL;
  444. page = list_entry(h->hugepage_freelists[nid].next, struct page, lru);
  445. list_move(&page->lru, &h->hugepage_activelist);
  446. set_page_refcounted(page);
  447. h->free_huge_pages--;
  448. h->free_huge_pages_node[nid]--;
  449. return page;
  450. }
  451. static struct page *dequeue_huge_page_vma(struct hstate *h,
  452. struct vm_area_struct *vma,
  453. unsigned long address, int avoid_reserve,
  454. long chg)
  455. {
  456. struct page *page = NULL;
  457. struct mempolicy *mpol;
  458. nodemask_t *nodemask;
  459. struct zonelist *zonelist;
  460. struct zone *zone;
  461. struct zoneref *z;
  462. unsigned int cpuset_mems_cookie;
  463. /*
  464. * A child process with MAP_PRIVATE mappings created by their parent
  465. * have no page reserves. This check ensures that reservations are
  466. * not "stolen". The child may still get SIGKILLed
  467. */
  468. if (!vma_has_reserves(vma, chg) &&
  469. h->free_huge_pages - h->resv_huge_pages == 0)
  470. goto err;
  471. /* If reserves cannot be used, ensure enough pages are in the pool */
  472. if (avoid_reserve && h->free_huge_pages - h->resv_huge_pages == 0)
  473. goto err;
  474. retry_cpuset:
  475. cpuset_mems_cookie = get_mems_allowed();
  476. zonelist = huge_zonelist(vma, address,
  477. htlb_alloc_mask, &mpol, &nodemask);
  478. for_each_zone_zonelist_nodemask(zone, z, zonelist,
  479. MAX_NR_ZONES - 1, nodemask) {
  480. if (cpuset_zone_allowed_softwall(zone, htlb_alloc_mask)) {
  481. page = dequeue_huge_page_node(h, zone_to_nid(zone));
  482. if (page) {
  483. if (avoid_reserve)
  484. break;
  485. if (!vma_has_reserves(vma, chg))
  486. break;
  487. SetPagePrivate(page);
  488. h->resv_huge_pages--;
  489. break;
  490. }
  491. }
  492. }
  493. mpol_cond_put(mpol);
  494. if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !page))
  495. goto retry_cpuset;
  496. return page;
  497. err:
  498. return NULL;
  499. }
  500. static void update_and_free_page(struct hstate *h, struct page *page)
  501. {
  502. int i;
  503. VM_BUG_ON(h->order >= MAX_ORDER);
  504. h->nr_huge_pages--;
  505. h->nr_huge_pages_node[page_to_nid(page)]--;
  506. for (i = 0; i < pages_per_huge_page(h); i++) {
  507. page[i].flags &= ~(1 << PG_locked | 1 << PG_error |
  508. 1 << PG_referenced | 1 << PG_dirty |
  509. 1 << PG_active | 1 << PG_reserved |
  510. 1 << PG_private | 1 << PG_writeback);
  511. }
  512. VM_BUG_ON(hugetlb_cgroup_from_page(page));
  513. set_compound_page_dtor(page, NULL);
  514. set_page_refcounted(page);
  515. arch_release_hugepage(page);
  516. __free_pages(page, huge_page_order(h));
  517. }
  518. struct hstate *size_to_hstate(unsigned long size)
  519. {
  520. struct hstate *h;
  521. for_each_hstate(h) {
  522. if (huge_page_size(h) == size)
  523. return h;
  524. }
  525. return NULL;
  526. }
  527. static void free_huge_page(struct page *page)
  528. {
  529. /*
  530. * Can't pass hstate in here because it is called from the
  531. * compound page destructor.
  532. */
  533. struct hstate *h = page_hstate(page);
  534. int nid = page_to_nid(page);
  535. struct hugepage_subpool *spool =
  536. (struct hugepage_subpool *)page_private(page);
  537. bool restore_reserve;
  538. set_page_private(page, 0);
  539. page->mapping = NULL;
  540. BUG_ON(page_count(page));
  541. BUG_ON(page_mapcount(page));
  542. restore_reserve = PagePrivate(page);
  543. spin_lock(&hugetlb_lock);
  544. hugetlb_cgroup_uncharge_page(hstate_index(h),
  545. pages_per_huge_page(h), page);
  546. if (restore_reserve)
  547. h->resv_huge_pages++;
  548. if (h->surplus_huge_pages_node[nid] && huge_page_order(h) < MAX_ORDER) {
  549. /* remove the page from active list */
  550. list_del(&page->lru);
  551. update_and_free_page(h, page);
  552. h->surplus_huge_pages--;
  553. h->surplus_huge_pages_node[nid]--;
  554. } else {
  555. arch_clear_hugepage_flags(page);
  556. enqueue_huge_page(h, page);
  557. }
  558. spin_unlock(&hugetlb_lock);
  559. hugepage_subpool_put_pages(spool, 1);
  560. }
  561. static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)
  562. {
  563. INIT_LIST_HEAD(&page->lru);
  564. set_compound_page_dtor(page, free_huge_page);
  565. spin_lock(&hugetlb_lock);
  566. set_hugetlb_cgroup(page, NULL);
  567. h->nr_huge_pages++;
  568. h->nr_huge_pages_node[nid]++;
  569. spin_unlock(&hugetlb_lock);
  570. put_page(page); /* free it into the hugepage allocator */
  571. }
  572. static void prep_compound_gigantic_page(struct page *page, unsigned long order)
  573. {
  574. int i;
  575. int nr_pages = 1 << order;
  576. struct page *p = page + 1;
  577. /* we rely on prep_new_huge_page to set the destructor */
  578. set_compound_order(page, order);
  579. __SetPageHead(page);
  580. for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) {
  581. __SetPageTail(p);
  582. set_page_count(p, 0);
  583. p->first_page = page;
  584. }
  585. }
  586. /*
  587. * PageHuge() only returns true for hugetlbfs pages, but not for normal or
  588. * transparent huge pages. See the PageTransHuge() documentation for more
  589. * details.
  590. */
  591. int PageHuge(struct page *page)
  592. {
  593. compound_page_dtor *dtor;
  594. if (!PageCompound(page))
  595. return 0;
  596. page = compound_head(page);
  597. dtor = get_compound_page_dtor(page);
  598. return dtor == free_huge_page;
  599. }
  600. EXPORT_SYMBOL_GPL(PageHuge);
  601. pgoff_t __basepage_index(struct page *page)
  602. {
  603. struct page *page_head = compound_head(page);
  604. pgoff_t index = page_index(page_head);
  605. unsigned long compound_idx;
  606. if (!PageHuge(page_head))
  607. return page_index(page);
  608. if (compound_order(page_head) >= MAX_ORDER)
  609. compound_idx = page_to_pfn(page) - page_to_pfn(page_head);
  610. else
  611. compound_idx = page - page_head;
  612. return (index << compound_order(page_head)) + compound_idx;
  613. }
  614. static struct page *alloc_fresh_huge_page_node(struct hstate *h, int nid)
  615. {
  616. struct page *page;
  617. if (h->order >= MAX_ORDER)
  618. return NULL;
  619. page = alloc_pages_exact_node(nid,
  620. htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|
  621. __GFP_REPEAT|__GFP_NOWARN,
  622. huge_page_order(h));
  623. if (page) {
  624. if (arch_prepare_hugepage(page)) {
  625. __free_pages(page, huge_page_order(h));
  626. return NULL;
  627. }
  628. prep_new_huge_page(h, page, nid);
  629. }
  630. return page;
  631. }
  632. /*
  633. * common helper functions for hstate_next_node_to_{alloc|free}.
  634. * We may have allocated or freed a huge page based on a different
  635. * nodes_allowed previously, so h->next_node_to_{alloc|free} might
  636. * be outside of *nodes_allowed. Ensure that we use an allowed
  637. * node for alloc or free.
  638. */
  639. static int next_node_allowed(int nid, nodemask_t *nodes_allowed)
  640. {
  641. nid = next_node(nid, *nodes_allowed);
  642. if (nid == MAX_NUMNODES)
  643. nid = first_node(*nodes_allowed);
  644. VM_BUG_ON(nid >= MAX_NUMNODES);
  645. return nid;
  646. }
  647. static int get_valid_node_allowed(int nid, nodemask_t *nodes_allowed)
  648. {
  649. if (!node_isset(nid, *nodes_allowed))
  650. nid = next_node_allowed(nid, nodes_allowed);
  651. return nid;
  652. }
  653. /*
  654. * returns the previously saved node ["this node"] from which to
  655. * allocate a persistent huge page for the pool and advance the
  656. * next node from which to allocate, handling wrap at end of node
  657. * mask.
  658. */
  659. static int hstate_next_node_to_alloc(struct hstate *h,
  660. nodemask_t *nodes_allowed)
  661. {
  662. int nid;
  663. VM_BUG_ON(!nodes_allowed);
  664. nid = get_valid_node_allowed(h->next_nid_to_alloc, nodes_allowed);
  665. h->next_nid_to_alloc = next_node_allowed(nid, nodes_allowed);
  666. return nid;
  667. }
  668. /*
  669. * helper for free_pool_huge_page() - return the previously saved
  670. * node ["this node"] from which to free a huge page. Advance the
  671. * next node id whether or not we find a free huge page to free so
  672. * that the next attempt to free addresses the next node.
  673. */
  674. static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed)
  675. {
  676. int nid;
  677. VM_BUG_ON(!nodes_allowed);
  678. nid = get_valid_node_allowed(h->next_nid_to_free, nodes_allowed);
  679. h->next_nid_to_free = next_node_allowed(nid, nodes_allowed);
  680. return nid;
  681. }
  682. #define for_each_node_mask_to_alloc(hs, nr_nodes, node, mask) \
  683. for (nr_nodes = nodes_weight(*mask); \
  684. nr_nodes > 0 && \
  685. ((node = hstate_next_node_to_alloc(hs, mask)) || 1); \
  686. nr_nodes--)
  687. #define for_each_node_mask_to_free(hs, nr_nodes, node, mask) \
  688. for (nr_nodes = nodes_weight(*mask); \
  689. nr_nodes > 0 && \
  690. ((node = hstate_next_node_to_free(hs, mask)) || 1); \
  691. nr_nodes--)
  692. static int alloc_fresh_huge_page(struct hstate *h, nodemask_t *nodes_allowed)
  693. {
  694. struct page *page;
  695. int nr_nodes, node;
  696. int ret = 0;
  697. for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) {
  698. page = alloc_fresh_huge_page_node(h, node);
  699. if (page) {
  700. ret = 1;
  701. break;
  702. }
  703. }
  704. if (ret)
  705. count_vm_event(HTLB_BUDDY_PGALLOC);
  706. else
  707. count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
  708. return ret;
  709. }
  710. /*
  711. * Free huge page from pool from next node to free.
  712. * Attempt to keep persistent huge pages more or less
  713. * balanced over allowed nodes.
  714. * Called with hugetlb_lock locked.
  715. */
  716. static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,
  717. bool acct_surplus)
  718. {
  719. int nr_nodes, node;
  720. int ret = 0;
  721. for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) {
  722. /*
  723. * If we're returning unused surplus pages, only examine
  724. * nodes with surplus pages.
  725. */
  726. if ((!acct_surplus || h->surplus_huge_pages_node[node]) &&
  727. !list_empty(&h->hugepage_freelists[node])) {
  728. struct page *page =
  729. list_entry(h->hugepage_freelists[node].next,
  730. struct page, lru);
  731. list_del(&page->lru);
  732. h->free_huge_pages--;
  733. h->free_huge_pages_node[node]--;
  734. if (acct_surplus) {
  735. h->surplus_huge_pages--;
  736. h->surplus_huge_pages_node[node]--;
  737. }
  738. update_and_free_page(h, page);
  739. ret = 1;
  740. break;
  741. }
  742. }
  743. return ret;
  744. }
  745. static struct page *alloc_buddy_huge_page(struct hstate *h, int nid)
  746. {
  747. struct page *page;
  748. unsigned int r_nid;
  749. if (h->order >= MAX_ORDER)
  750. return NULL;
  751. /*
  752. * Assume we will successfully allocate the surplus page to
  753. * prevent racing processes from causing the surplus to exceed
  754. * overcommit
  755. *
  756. * This however introduces a different race, where a process B
  757. * tries to grow the static hugepage pool while alloc_pages() is
  758. * called by process A. B will only examine the per-node
  759. * counters in determining if surplus huge pages can be
  760. * converted to normal huge pages in adjust_pool_surplus(). A
  761. * won't be able to increment the per-node counter, until the
  762. * lock is dropped by B, but B doesn't drop hugetlb_lock until
  763. * no more huge pages can be converted from surplus to normal
  764. * state (and doesn't try to convert again). Thus, we have a
  765. * case where a surplus huge page exists, the pool is grown, and
  766. * the surplus huge page still exists after, even though it
  767. * should just have been converted to a normal huge page. This
  768. * does not leak memory, though, as the hugepage will be freed
  769. * once it is out of use. It also does not allow the counters to
  770. * go out of whack in adjust_pool_surplus() as we don't modify
  771. * the node values until we've gotten the hugepage and only the
  772. * per-node value is checked there.
  773. */
  774. spin_lock(&hugetlb_lock);
  775. if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) {
  776. spin_unlock(&hugetlb_lock);
  777. return NULL;
  778. } else {
  779. h->nr_huge_pages++;
  780. h->surplus_huge_pages++;
  781. }
  782. spin_unlock(&hugetlb_lock);
  783. if (nid == NUMA_NO_NODE)
  784. page = alloc_pages(htlb_alloc_mask|__GFP_COMP|
  785. __GFP_REPEAT|__GFP_NOWARN,
  786. huge_page_order(h));
  787. else
  788. page = alloc_pages_exact_node(nid,
  789. htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|
  790. __GFP_REPEAT|__GFP_NOWARN, huge_page_order(h));
  791. if (page && arch_prepare_hugepage(page)) {
  792. __free_pages(page, huge_page_order(h));
  793. page = NULL;
  794. }
  795. spin_lock(&hugetlb_lock);
  796. if (page) {
  797. INIT_LIST_HEAD(&page->lru);
  798. r_nid = page_to_nid(page);
  799. set_compound_page_dtor(page, free_huge_page);
  800. set_hugetlb_cgroup(page, NULL);
  801. /*
  802. * We incremented the global counters already
  803. */
  804. h->nr_huge_pages_node[r_nid]++;
  805. h->surplus_huge_pages_node[r_nid]++;
  806. __count_vm_event(HTLB_BUDDY_PGALLOC);
  807. } else {
  808. h->nr_huge_pages--;
  809. h->surplus_huge_pages--;
  810. __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
  811. }
  812. spin_unlock(&hugetlb_lock);
  813. return page;
  814. }
  815. /*
  816. * This allocation function is useful in the context where vma is irrelevant.
  817. * E.g. soft-offlining uses this function because it only cares physical
  818. * address of error page.
  819. */
  820. struct page *alloc_huge_page_node(struct hstate *h, int nid)
  821. {
  822. struct page *page = NULL;
  823. spin_lock(&hugetlb_lock);
  824. if (h->free_huge_pages - h->resv_huge_pages > 0)
  825. page = dequeue_huge_page_node(h, nid);
  826. spin_unlock(&hugetlb_lock);
  827. if (!page)
  828. page = alloc_buddy_huge_page(h, nid);
  829. return page;
  830. }
  831. /*
  832. * Increase the hugetlb pool such that it can accommodate a reservation
  833. * of size 'delta'.
  834. */
  835. static int gather_surplus_pages(struct hstate *h, int delta)
  836. {
  837. struct list_head surplus_list;
  838. struct page *page, *tmp;
  839. int ret, i;
  840. int needed, allocated;
  841. bool alloc_ok = true;
  842. needed = (h->resv_huge_pages + delta) - h->free_huge_pages;
  843. if (needed <= 0) {
  844. h->resv_huge_pages += delta;
  845. return 0;
  846. }
  847. allocated = 0;
  848. INIT_LIST_HEAD(&surplus_list);
  849. ret = -ENOMEM;
  850. retry:
  851. spin_unlock(&hugetlb_lock);
  852. for (i = 0; i < needed; i++) {
  853. page = alloc_buddy_huge_page(h, NUMA_NO_NODE);
  854. if (!page) {
  855. alloc_ok = false;
  856. break;
  857. }
  858. list_add(&page->lru, &surplus_list);
  859. }
  860. allocated += i;
  861. /*
  862. * After retaking hugetlb_lock, we need to recalculate 'needed'
  863. * because either resv_huge_pages or free_huge_pages may have changed.
  864. */
  865. spin_lock(&hugetlb_lock);
  866. needed = (h->resv_huge_pages + delta) -
  867. (h->free_huge_pages + allocated);
  868. if (needed > 0) {
  869. if (alloc_ok)
  870. goto retry;
  871. /*
  872. * We were not able to allocate enough pages to
  873. * satisfy the entire reservation so we free what
  874. * we've allocated so far.
  875. */
  876. goto free;
  877. }
  878. /*
  879. * The surplus_list now contains _at_least_ the number of extra pages
  880. * needed to accommodate the reservation. Add the appropriate number
  881. * of pages to the hugetlb pool and free the extras back to the buddy
  882. * allocator. Commit the entire reservation here to prevent another
  883. * process from stealing the pages as they are added to the pool but
  884. * before they are reserved.
  885. */
  886. needed += allocated;
  887. h->resv_huge_pages += delta;
  888. ret = 0;
  889. /* Free the needed pages to the hugetlb pool */
  890. list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
  891. if ((--needed) < 0)
  892. break;
  893. /*
  894. * This page is now managed by the hugetlb allocator and has
  895. * no users -- drop the buddy allocator's reference.
  896. */
  897. put_page_testzero(page);
  898. VM_BUG_ON(page_count(page));
  899. enqueue_huge_page(h, page);
  900. }
  901. free:
  902. spin_unlock(&hugetlb_lock);
  903. /* Free unnecessary surplus pages to the buddy allocator */
  904. list_for_each_entry_safe(page, tmp, &surplus_list, lru)
  905. put_page(page);
  906. spin_lock(&hugetlb_lock);
  907. return ret;
  908. }
  909. /*
  910. * When releasing a hugetlb pool reservation, any surplus pages that were
  911. * allocated to satisfy the reservation must be explicitly freed if they were
  912. * never used.
  913. * Called with hugetlb_lock held.
  914. */
  915. static void return_unused_surplus_pages(struct hstate *h,
  916. unsigned long unused_resv_pages)
  917. {
  918. unsigned long nr_pages;
  919. /* Uncommit the reservation */
  920. h->resv_huge_pages -= unused_resv_pages;
  921. /* Cannot return gigantic pages currently */
  922. if (h->order >= MAX_ORDER)
  923. return;
  924. nr_pages = min(unused_resv_pages, h->surplus_huge_pages);
  925. /*
  926. * We want to release as many surplus pages as possible, spread
  927. * evenly across all nodes with memory. Iterate across these nodes
  928. * until we can no longer free unreserved surplus pages. This occurs
  929. * when the nodes with surplus pages have no free pages.
  930. * free_pool_huge_page() will balance the the freed pages across the
  931. * on-line nodes with memory and will handle the hstate accounting.
  932. */
  933. while (nr_pages--) {
  934. if (!free_pool_huge_page(h, &node_states[N_MEMORY], 1))
  935. break;
  936. }
  937. }
  938. /*
  939. * Determine if the huge page at addr within the vma has an associated
  940. * reservation. Where it does not we will need to logically increase
  941. * reservation and actually increase subpool usage before an allocation
  942. * can occur. Where any new reservation would be required the
  943. * reservation change is prepared, but not committed. Once the page
  944. * has been allocated from the subpool and instantiated the change should
  945. * be committed via vma_commit_reservation. No action is required on
  946. * failure.
  947. */
  948. static long vma_needs_reservation(struct hstate *h,
  949. struct vm_area_struct *vma, unsigned long addr)
  950. {
  951. struct address_space *mapping = vma->vm_file->f_mapping;
  952. struct inode *inode = mapping->host;
  953. if (vma->vm_flags & VM_MAYSHARE) {
  954. pgoff_t idx = vma_hugecache_offset(h, vma, addr);
  955. return region_chg(&inode->i_mapping->private_list,
  956. idx, idx + 1);
  957. } else if (!is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
  958. return 1;
  959. } else {
  960. long err;
  961. pgoff_t idx = vma_hugecache_offset(h, vma, addr);
  962. struct resv_map *resv = vma_resv_map(vma);
  963. err = region_chg(&resv->regions, idx, idx + 1);
  964. if (err < 0)
  965. return err;
  966. return 0;
  967. }
  968. }
  969. static void vma_commit_reservation(struct hstate *h,
  970. struct vm_area_struct *vma, unsigned long addr)
  971. {
  972. struct address_space *mapping = vma->vm_file->f_mapping;
  973. struct inode *inode = mapping->host;
  974. if (vma->vm_flags & VM_MAYSHARE) {
  975. pgoff_t idx = vma_hugecache_offset(h, vma, addr);
  976. region_add(&inode->i_mapping->private_list, idx, idx + 1);
  977. } else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
  978. pgoff_t idx = vma_hugecache_offset(h, vma, addr);
  979. struct resv_map *resv = vma_resv_map(vma);
  980. /* Mark this page used in the map. */
  981. region_add(&resv->regions, idx, idx + 1);
  982. }
  983. }
  984. static struct page *alloc_huge_page(struct vm_area_struct *vma,
  985. unsigned long addr, int avoid_reserve)
  986. {
  987. struct hugepage_subpool *spool = subpool_vma(vma);
  988. struct hstate *h = hstate_vma(vma);
  989. struct page *page;
  990. long chg;
  991. int ret, idx;
  992. struct hugetlb_cgroup *h_cg;
  993. idx = hstate_index(h);
  994. /*
  995. * Processes that did not create the mapping will have no
  996. * reserves and will not have accounted against subpool
  997. * limit. Check that the subpool limit can be made before
  998. * satisfying the allocation MAP_NORESERVE mappings may also
  999. * need pages and subpool limit allocated allocated if no reserve
  1000. * mapping overlaps.
  1001. */
  1002. chg = vma_needs_reservation(h, vma, addr);
  1003. if (chg < 0)
  1004. return ERR_PTR(-ENOMEM);
  1005. if (chg || avoid_reserve)
  1006. if (hugepage_subpool_get_pages(spool, 1))
  1007. return ERR_PTR(-ENOSPC);
  1008. ret = hugetlb_cgroup_charge_cgroup(idx, pages_per_huge_page(h), &h_cg);
  1009. if (ret) {
  1010. if (chg || avoid_reserve)
  1011. hugepage_subpool_put_pages(spool, 1);
  1012. return ERR_PTR(-ENOSPC);
  1013. }
  1014. spin_lock(&hugetlb_lock);
  1015. page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve, chg);
  1016. if (!page) {
  1017. spin_unlock(&hugetlb_lock);
  1018. page = alloc_buddy_huge_page(h, NUMA_NO_NODE);
  1019. if (!page) {
  1020. hugetlb_cgroup_uncharge_cgroup(idx,
  1021. pages_per_huge_page(h),
  1022. h_cg);
  1023. if (chg || avoid_reserve)
  1024. hugepage_subpool_put_pages(spool, 1);
  1025. return ERR_PTR(-ENOSPC);
  1026. }
  1027. spin_lock(&hugetlb_lock);
  1028. list_move(&page->lru, &h->hugepage_activelist);
  1029. /* Fall through */
  1030. }
  1031. hugetlb_cgroup_commit_charge(idx, pages_per_huge_page(h), h_cg, page);
  1032. spin_unlock(&hugetlb_lock);
  1033. set_page_private(page, (unsigned long)spool);
  1034. vma_commit_reservation(h, vma, addr);
  1035. return page;
  1036. }
  1037. /*
  1038. * alloc_huge_page()'s wrapper which simply returns the page if allocation
  1039. * succeeds, otherwise NULL. This function is called from new_vma_page(),
  1040. * where no ERR_VALUE is expected to be returned.
  1041. */
  1042. struct page *alloc_huge_page_noerr(struct vm_area_struct *vma,
  1043. unsigned long addr, int avoid_reserve)
  1044. {
  1045. struct page *page = alloc_huge_page(vma, addr, avoid_reserve);
  1046. if (IS_ERR(page))
  1047. page = NULL;
  1048. return page;
  1049. }
  1050. int __weak alloc_bootmem_huge_page(struct hstate *h)
  1051. {
  1052. struct huge_bootmem_page *m;
  1053. int nr_nodes, node;
  1054. for_each_node_mask_to_alloc(h, nr_nodes, node, &node_states[N_MEMORY]) {
  1055. void *addr;
  1056. addr = __alloc_bootmem_node_nopanic(NODE_DATA(node),
  1057. huge_page_size(h), huge_page_size(h), 0);
  1058. if (addr) {
  1059. /*
  1060. * Use the beginning of the huge page to store the
  1061. * huge_bootmem_page struct (until gather_bootmem
  1062. * puts them into the mem_map).
  1063. */
  1064. m = addr;
  1065. goto found;
  1066. }
  1067. }
  1068. return 0;
  1069. found:
  1070. BUG_ON((unsigned long)virt_to_phys(m) & (huge_page_size(h) - 1));
  1071. /* Put them into a private list first because mem_map is not up yet */
  1072. list_add(&m->list, &huge_boot_pages);
  1073. m->hstate = h;
  1074. return 1;
  1075. }
  1076. static void prep_compound_huge_page(struct page *page, int order)
  1077. {
  1078. if (unlikely(order > (MAX_ORDER - 1)))
  1079. prep_compound_gigantic_page(page, order);
  1080. else
  1081. prep_compound_page(page, order);
  1082. }
  1083. /* Put bootmem huge pages into the standard lists after mem_map is up */
  1084. static void __init gather_bootmem_prealloc(void)
  1085. {
  1086. struct huge_bootmem_page *m;
  1087. list_for_each_entry(m, &huge_boot_pages, list) {
  1088. struct hstate *h = m->hstate;
  1089. struct page *page;
  1090. #ifdef CONFIG_HIGHMEM
  1091. page = pfn_to_page(m->phys >> PAGE_SHIFT);
  1092. free_bootmem_late((unsigned long)m,
  1093. sizeof(struct huge_bootmem_page));
  1094. #else
  1095. page = virt_to_page(m);
  1096. #endif
  1097. __ClearPageReserved(page);
  1098. WARN_ON(page_count(page) != 1);
  1099. prep_compound_huge_page(page, h->order);
  1100. prep_new_huge_page(h, page, page_to_nid(page));
  1101. /*
  1102. * If we had gigantic hugepages allocated at boot time, we need
  1103. * to restore the 'stolen' pages to totalram_pages in order to
  1104. * fix confusing memory reports from free(1) and another
  1105. * side-effects, like CommitLimit going negative.
  1106. */
  1107. if (h->order > (MAX_ORDER - 1))
  1108. adjust_managed_page_count(page, 1 << h->order);
  1109. }
  1110. }
  1111. static void __init hugetlb_hstate_alloc_pages(struct hstate *h)
  1112. {
  1113. unsigned long i;
  1114. for (i = 0; i < h->max_huge_pages; ++i) {
  1115. if (h->order >= MAX_ORDER) {
  1116. if (!alloc_bootmem_huge_page(h))
  1117. break;
  1118. } else if (!alloc_fresh_huge_page(h,
  1119. &node_states[N_MEMORY]))
  1120. break;
  1121. }
  1122. h->max_huge_pages = i;
  1123. }
  1124. static void __init hugetlb_init_hstates(void)
  1125. {
  1126. struct hstate *h;
  1127. for_each_hstate(h) {
  1128. /* oversize hugepages were init'ed in early boot */
  1129. if (h->order < MAX_ORDER)
  1130. hugetlb_hstate_alloc_pages(h);
  1131. }
  1132. }
  1133. static char * __init memfmt(char *buf, unsigned long n)
  1134. {
  1135. if (n >= (1UL << 30))
  1136. sprintf(buf, "%lu GB", n >> 30);
  1137. else if (n >= (1UL << 20))
  1138. sprintf(buf, "%lu MB", n >> 20);
  1139. else
  1140. sprintf(buf, "%lu KB", n >> 10);
  1141. return buf;
  1142. }
  1143. static void __init report_hugepages(void)
  1144. {
  1145. struct hstate *h;
  1146. for_each_hstate(h) {
  1147. char buf[32];
  1148. pr_info("HugeTLB registered %s page size, pre-allocated %ld pages\n",
  1149. memfmt(buf, huge_page_size(h)),
  1150. h->free_huge_pages);
  1151. }
  1152. }
  1153. #ifdef CONFIG_HIGHMEM
  1154. static void try_to_free_low(struct hstate *h, unsigned long count,
  1155. nodemask_t *nodes_allowed)
  1156. {
  1157. int i;
  1158. if (h->order >= MAX_ORDER)
  1159. return;
  1160. for_each_node_mask(i, *nodes_allowed) {
  1161. struct page *page, *next;
  1162. struct list_head *freel = &h->hugepage_freelists[i];
  1163. list_for_each_entry_safe(page, next, freel, lru) {
  1164. if (count >= h->nr_huge_pages)
  1165. return;
  1166. if (PageHighMem(page))
  1167. continue;
  1168. list_del(&page->lru);
  1169. update_and_free_page(h, page);
  1170. h->free_huge_pages--;
  1171. h->free_huge_pages_node[page_to_nid(page)]--;
  1172. }
  1173. }
  1174. }
  1175. #else
  1176. static inline void try_to_free_low(struct hstate *h, unsigned long count,
  1177. nodemask_t *nodes_allowed)
  1178. {
  1179. }
  1180. #endif
  1181. /*
  1182. * Increment or decrement surplus_huge_pages. Keep node-specific counters
  1183. * balanced by operating on them in a round-robin fashion.
  1184. * Returns 1 if an adjustment was made.
  1185. */
  1186. static int adjust_pool_surplus(struct hstate *h, nodemask_t *nodes_allowed,
  1187. int delta)
  1188. {
  1189. int nr_nodes, node;
  1190. VM_BUG_ON(delta != -1 && delta != 1);
  1191. if (delta < 0) {
  1192. for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) {
  1193. if (h->surplus_huge_pages_node[node])
  1194. goto found;
  1195. }
  1196. } else {
  1197. for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) {
  1198. if (h->surplus_huge_pages_node[node] <
  1199. h->nr_huge_pages_node[node])
  1200. goto found;
  1201. }
  1202. }
  1203. return 0;
  1204. found:
  1205. h->surplus_huge_pages += delta;
  1206. h->surplus_huge_pages_node[node] += delta;
  1207. return 1;
  1208. }
  1209. #define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages)
  1210. static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count,
  1211. nodemask_t *nodes_allowed)
  1212. {
  1213. unsigned long min_count, ret;
  1214. if (h->order >= MAX_ORDER)
  1215. return h->max_huge_pages;
  1216. /*
  1217. * Increase the pool size
  1218. * First take pages out of surplus state. Then make up the
  1219. * remaining difference by allocating fresh huge pages.
  1220. *
  1221. * We might race with alloc_buddy_huge_page() here and be unable
  1222. * to convert a surplus huge page to a normal huge page. That is
  1223. * not critical, though, it just means the overall size of the
  1224. * pool might be one hugepage larger than it needs to be, but
  1225. * within all the constraints specified by the sysctls.
  1226. */
  1227. spin_lock(&hugetlb_lock);
  1228. while (h->surplus_huge_pages && count > persistent_huge_pages(h)) {
  1229. if (!adjust_pool_surplus(h, nodes_allowed, -1))
  1230. break;
  1231. }
  1232. while (count > persistent_huge_pages(h)) {
  1233. /*
  1234. * If this allocation races such that we no longer need the
  1235. * page, free_huge_page will handle it by freeing the page
  1236. * and reducing the surplus.
  1237. */
  1238. spin_unlock(&hugetlb_lock);
  1239. ret = alloc_fresh_huge_page(h, nodes_allowed);
  1240. spin_lock(&hugetlb_lock);
  1241. if (!ret)
  1242. goto out;
  1243. /* Bail for signals. Probably ctrl-c from user */
  1244. if (signal_pending(current))
  1245. goto out;
  1246. }
  1247. /*
  1248. * Decrease the pool size
  1249. * First return free pages to the buddy allocator (being careful
  1250. * to keep enough around to satisfy reservations). Then place
  1251. * pages into surplus state as needed so the pool will shrink
  1252. * to the desired size as pages become free.
  1253. *
  1254. * By placing pages into the surplus state independent of the
  1255. * overcommit value, we are allowing the surplus pool size to
  1256. * exceed overcommit. There are few sane options here. Since
  1257. * alloc_buddy_huge_page() is checking the global counter,
  1258. * though, we'll note that we're not allowed to exceed surplus
  1259. * and won't grow the pool anywhere else. Not until one of the
  1260. * sysctls are changed, or the surplus pages go out of use.
  1261. */
  1262. min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages;
  1263. min_count = max(count, min_count);
  1264. try_to_free_low(h, min_count, nodes_allowed);
  1265. while (min_count < persistent_huge_pages(h)) {
  1266. if (!free_pool_huge_page(h, nodes_allowed, 0))
  1267. break;
  1268. }
  1269. while (count < persistent_huge_pages(h)) {
  1270. if (!adjust_pool_surplus(h, nodes_allowed, 1))
  1271. break;
  1272. }
  1273. out:
  1274. ret = persistent_huge_pages(h);
  1275. spin_unlock(&hugetlb_lock);
  1276. return ret;
  1277. }
  1278. #define HSTATE_ATTR_RO(_name) \
  1279. static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
  1280. #define HSTATE_ATTR(_name) \
  1281. static struct kobj_attribute _name##_attr = \
  1282. __ATTR(_name, 0644, _name##_show, _name##_store)
  1283. static struct kobject *hugepages_kobj;
  1284. static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
  1285. static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp);
  1286. static struct hstate *kobj_to_hstate(struct kobject *kobj, int *nidp)
  1287. {
  1288. int i;
  1289. for (i = 0; i < HUGE_MAX_HSTATE; i++)
  1290. if (hstate_kobjs[i] == kobj) {
  1291. if (nidp)
  1292. *nidp = NUMA_NO_NODE;
  1293. return &hstates[i];
  1294. }
  1295. return kobj_to_node_hstate(kobj, nidp);
  1296. }
  1297. static ssize_t nr_hugepages_show_common(struct kobject *kobj,
  1298. struct kobj_attribute *attr, char *buf)
  1299. {
  1300. struct hstate *h;
  1301. unsigned long nr_huge_pages;
  1302. int nid;
  1303. h = kobj_to_hstate(kobj, &nid);
  1304. if (nid == NUMA_NO_NODE)
  1305. nr_huge_pages = h->nr_huge_pages;
  1306. else
  1307. nr_huge_pages = h->nr_huge_pages_node[nid];
  1308. return sprintf(buf, "%lu\n", nr_huge_pages);
  1309. }
  1310. static ssize_t nr_hugepages_store_common(bool obey_mempolicy,
  1311. struct kobject *kobj, struct kobj_attribute *attr,
  1312. const char *buf, size_t len)
  1313. {
  1314. int err;
  1315. int nid;
  1316. unsigned long count;
  1317. struct hstate *h;
  1318. NODEMASK_ALLOC(nodemask_t, nodes_allowed, GFP_KERNEL | __GFP_NORETRY);
  1319. err = kstrtoul(buf, 10, &count);
  1320. if (err)
  1321. goto out;
  1322. h = kobj_to_hstate(kobj, &nid);
  1323. if (h->order >= MAX_ORDER) {
  1324. err = -EINVAL;
  1325. goto out;
  1326. }
  1327. if (nid == NUMA_NO_NODE) {
  1328. /*
  1329. * global hstate attribute
  1330. */
  1331. if (!(obey_mempolicy &&
  1332. init_nodemask_of_mempolicy(nodes_allowed))) {
  1333. NODEMASK_FREE(nodes_allowed);
  1334. nodes_allowed = &node_states[N_MEMORY];
  1335. }
  1336. } else if (nodes_allowed) {
  1337. /*
  1338. * per node hstate attribute: adjust count to global,
  1339. * but restrict alloc/free to the specified node.
  1340. */
  1341. count += h->nr_huge_pages - h->nr_huge_pages_node[nid];
  1342. init_nodemask_of_node(nodes_allowed, nid);
  1343. } else
  1344. nodes_allowed = &node_states[N_MEMORY];
  1345. h->max_huge_pages = set_max_huge_pages(h, count, nodes_allowed);
  1346. if (nodes_allowed != &node_states[N_MEMORY])
  1347. NODEMASK_FREE(nodes_allowed);
  1348. return len;
  1349. out:
  1350. NODEMASK_FREE(nodes_allowed);
  1351. return err;
  1352. }
  1353. static ssize_t nr_hugepages_show(struct kobject *kobj,
  1354. struct kobj_attribute *attr, char *buf)
  1355. {
  1356. return nr_hugepages_show_common(kobj, attr, buf);
  1357. }
  1358. static ssize_t nr_hugepages_store(struct kobject *kobj,
  1359. struct kobj_attribute *attr, const char *buf, size_t len)
  1360. {
  1361. return nr_hugepages_store_common(false, kobj, attr, buf, len);
  1362. }
  1363. HSTATE_ATTR(nr_hugepages);
  1364. #ifdef CONFIG_NUMA
  1365. /*
  1366. * hstate attribute for optionally mempolicy-based constraint on persistent
  1367. * huge page alloc/free.
  1368. */
  1369. static ssize_t nr_hugepages_mempolicy_show(struct kobject *kobj,
  1370. struct kobj_attribute *attr, char *buf)
  1371. {
  1372. return nr_hugepages_show_common(kobj, attr, buf);
  1373. }
  1374. static ssize_t nr_hugepages_mempolicy_store(struct kobject *kobj,
  1375. struct kobj_attribute *attr, const char *buf, size_t len)
  1376. {
  1377. return nr_hugepages_store_common(true, kobj, attr, buf, len);
  1378. }
  1379. HSTATE_ATTR(nr_hugepages_mempolicy);
  1380. #endif
  1381. static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj,
  1382. struct kobj_attribute *attr, char *buf)
  1383. {
  1384. struct hstate *h = kobj_to_hstate(kobj, NULL);
  1385. return sprintf(buf, "%lu\n", h->nr_overcommit_huge_pages);
  1386. }
  1387. static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj,
  1388. struct kobj_attribute *attr, const char *buf, size_t count)
  1389. {
  1390. int err;
  1391. unsigned long input;
  1392. struct hstate *h = kobj_to_hstate(kobj, NULL);
  1393. if (h->order >= MAX_ORDER)
  1394. return -EINVAL;
  1395. err = kstrtoul(buf, 10, &input);
  1396. if (err)
  1397. return err;
  1398. spin_lock(&hugetlb_lock);
  1399. h->nr_overcommit_huge_pages = input;
  1400. spin_unlock(&hugetlb_lock);
  1401. return count;
  1402. }
  1403. HSTATE_ATTR(nr_overcommit_hugepages);
  1404. static ssize_t free_hugepages_show(struct kobject *kobj,
  1405. struct kobj_attribute *attr, char *buf)
  1406. {
  1407. struct hstate *h;
  1408. unsigned long free_huge_pages;
  1409. int nid;
  1410. h = kobj_to_hstate(kobj, &nid);
  1411. if (nid == NUMA_NO_NODE)
  1412. free_huge_pages = h->free_huge_pages;
  1413. else
  1414. free_huge_pages = h->free_huge_pages_node[nid];
  1415. return sprintf(buf, "%lu\n", free_huge_pages);
  1416. }
  1417. HSTATE_ATTR_RO(free_hugepages);
  1418. static ssize_t resv_hugepages_show(struct kobject *kobj,
  1419. struct kobj_attribute *attr, char *buf)
  1420. {
  1421. struct hstate *h = kobj_to_hstate(kobj, NULL);
  1422. return sprintf(buf, "%lu\n", h->resv_huge_pages);
  1423. }
  1424. HSTATE_ATTR_RO(resv_hugepages);
  1425. static ssize_t surplus_hugepages_show(struct kobject *kobj,
  1426. struct kobj_attribute *attr, char *buf)
  1427. {
  1428. struct hstate *h;
  1429. unsigned long surplus_huge_pages;
  1430. int nid;
  1431. h = kobj_to_hstate(kobj, &nid);
  1432. if (nid == NUMA_NO_NODE)
  1433. surplus_huge_pages = h->surplus_huge_pages;
  1434. else
  1435. surplus_huge_pages = h->surplus_huge_pages_node[nid];
  1436. return sprintf(buf, "%lu\n", surplus_huge_pages);
  1437. }
  1438. HSTATE_ATTR_RO(surplus_hugepages);
  1439. static struct attribute *hstate_attrs[] = {
  1440. &nr_hugepages_attr.attr,
  1441. &nr_overcommit_hugepages_attr.attr,
  1442. &free_hugepages_attr.attr,
  1443. &resv_hugepages_attr.attr,
  1444. &surplus_hugepages_attr.attr,
  1445. #ifdef CONFIG_NUMA
  1446. &nr_hugepages_mempolicy_attr.attr,
  1447. #endif
  1448. NULL,
  1449. };
  1450. static struct attribute_group hstate_attr_group = {
  1451. .attrs = hstate_attrs,
  1452. };
  1453. static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent,
  1454. struct kobject **hstate_kobjs,
  1455. struct attribute_group *hstate_attr_group)
  1456. {
  1457. int retval;
  1458. int hi = hstate_index(h);
  1459. hstate_kobjs[hi] = kobject_create_and_add(h->name, parent);
  1460. if (!hstate_kobjs[hi])
  1461. return -ENOMEM;
  1462. retval = sysfs_create_group(hstate_kobjs[hi], hstate_attr_group);
  1463. if (retval)
  1464. kobject_put(hstate_kobjs[hi]);
  1465. return retval;
  1466. }
  1467. static void __init hugetlb_sysfs_init(void)
  1468. {
  1469. struct hstate *h;
  1470. int err;
  1471. hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj);
  1472. if (!hugepages_kobj)
  1473. return;
  1474. for_each_hstate(h) {
  1475. err = hugetlb_sysfs_add_hstate(h, hugepages_kobj,
  1476. hstate_kobjs, &hstate_attr_group);
  1477. if (err)
  1478. pr_err("Hugetlb: Unable to add hstate %s", h->name);
  1479. }
  1480. }
  1481. #ifdef CONFIG_NUMA
  1482. /*
  1483. * node_hstate/s - associate per node hstate attributes, via their kobjects,
  1484. * with node devices in node_devices[] using a parallel array. The array
  1485. * index of a node device or _hstate == node id.
  1486. * This is here to avoid any static dependency of the node device driver, in
  1487. * the base kernel, on the hugetlb module.
  1488. */
  1489. struct node_hstate {
  1490. struct kobject *hugepages_kobj;
  1491. struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
  1492. };
  1493. struct node_hstate node_hstates[MAX_NUMNODES];
  1494. /*
  1495. * A subset of global hstate attributes for node devices
  1496. */
  1497. static struct attribute *per_node_hstate_attrs[] = {
  1498. &nr_hugepages_attr.attr,
  1499. &free_hugepages_attr.attr,
  1500. &surplus_hugepages_attr.attr,
  1501. NULL,
  1502. };
  1503. static struct attribute_group per_node_hstate_attr_group = {
  1504. .attrs = per_node_hstate_attrs,
  1505. };
  1506. /*
  1507. * kobj_to_node_hstate - lookup global hstate for node device hstate attr kobj.
  1508. * Returns node id via non-NULL nidp.
  1509. */
  1510. static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
  1511. {
  1512. int nid;
  1513. for (nid = 0; nid < nr_node_ids; nid++) {
  1514. struct node_hstate *nhs = &node_hstates[nid];
  1515. int i;
  1516. for (i = 0; i < HUGE_MAX_HSTATE; i++)
  1517. if (nhs->hstate_kobjs[i] == kobj) {
  1518. if (nidp)
  1519. *nidp = nid;
  1520. return &hstates[i];
  1521. }
  1522. }
  1523. BUG();
  1524. return NULL;
  1525. }
  1526. /*
  1527. * Unregister hstate attributes from a single node device.
  1528. * No-op if no hstate attributes attached.
  1529. */
  1530. static void hugetlb_unregister_node(struct node *node)
  1531. {
  1532. struct hstate *h;
  1533. struct node_hstate *nhs = &node_hstates[node->dev.id];
  1534. if (!nhs->hugepages_kobj)
  1535. return; /* no hstate attributes */
  1536. for_each_hstate(h) {
  1537. int idx = hstate_index(h);
  1538. if (nhs->hstate_kobjs[idx]) {
  1539. kobject_put(nhs->hstate_kobjs[idx]);
  1540. nhs->hstate_kobjs[idx] = NULL;
  1541. }
  1542. }
  1543. kobject_put(nhs->hugepages_kobj);
  1544. nhs->hugepages_kobj = NULL;
  1545. }
  1546. /*
  1547. * hugetlb module exit: unregister hstate attributes from node devices
  1548. * that have them.
  1549. */
  1550. static void hugetlb_unregister_all_nodes(void)
  1551. {
  1552. int nid;
  1553. /*
  1554. * disable node device registrations.
  1555. */
  1556. register_hugetlbfs_with_node(NULL, NULL);
  1557. /*
  1558. * remove hstate attributes from any nodes that have them.
  1559. */
  1560. for (nid = 0; nid < nr_node_ids; nid++)
  1561. hugetlb_unregister_node(node_devices[nid]);
  1562. }
  1563. /*
  1564. * Register hstate attributes for a single node device.
  1565. * No-op if attributes already registered.
  1566. */
  1567. static void hugetlb_register_node(struct node *node)
  1568. {
  1569. struct hstate *h;
  1570. struct node_hstate *nhs = &node_hstates[node->dev.id];
  1571. int err;
  1572. if (nhs->hugepages_kobj)
  1573. return; /* already allocated */
  1574. nhs->hugepages_kobj = kobject_create_and_add("hugepages",
  1575. &node->dev.kobj);
  1576. if (!nhs->hugepages_kobj)
  1577. return;
  1578. for_each_hstate(h) {
  1579. err = hugetlb_sysfs_add_hstate(h, nhs->hugepages_kobj,
  1580. nhs->hstate_kobjs,
  1581. &per_node_hstate_attr_group);
  1582. if (err) {
  1583. pr_err("Hugetlb: Unable to add hstate %s for node %d\n",
  1584. h->name, node->dev.id);
  1585. hugetlb_unregister_node(node);
  1586. break;
  1587. }
  1588. }
  1589. }
  1590. /*
  1591. * hugetlb init time: register hstate attributes for all registered node
  1592. * devices of nodes that have memory. All on-line nodes should have
  1593. * registered their associated device by this time.
  1594. */
  1595. static void hugetlb_register_all_nodes(void)
  1596. {
  1597. int nid;
  1598. for_each_node_state(nid, N_MEMORY) {
  1599. struct node *node = node_devices[nid];
  1600. if (node->dev.id == nid)
  1601. hugetlb_register_node(node);
  1602. }
  1603. /*
  1604. * Let the node device driver know we're here so it can
  1605. * [un]register hstate attributes on node hotplug.
  1606. */
  1607. register_hugetlbfs_with_node(hugetlb_register_node,
  1608. hugetlb_unregister_node);
  1609. }
  1610. #else /* !CONFIG_NUMA */
  1611. static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
  1612. {
  1613. BUG();
  1614. if (nidp)
  1615. *nidp = -1;
  1616. return NULL;
  1617. }
  1618. static void hugetlb_unregister_all_nodes(void) { }
  1619. static void hugetlb_register_all_nodes(void) { }
  1620. #endif
  1621. static void __exit hugetlb_exit(void)
  1622. {
  1623. struct hstate *h;
  1624. hugetlb_unregister_all_nodes();
  1625. for_each_hstate(h) {
  1626. kobject_put(hstate_kobjs[hstate_index(h)]);
  1627. }
  1628. kobject_put(hugepages_kobj);
  1629. }
  1630. module_exit(hugetlb_exit);
  1631. static int __init hugetlb_init(void)
  1632. {
  1633. /* Some platform decide whether they support huge pages at boot
  1634. * time. On these, such as powerpc, HPAGE_SHIFT is set to 0 when
  1635. * there is no such support
  1636. */
  1637. if (HPAGE_SHIFT == 0)
  1638. return 0;
  1639. if (!size_to_hstate(default_hstate_size)) {
  1640. default_hstate_size = HPAGE_SIZE;
  1641. if (!size_to_hstate(default_hstate_size))
  1642. hugetlb_add_hstate(HUGETLB_PAGE_ORDER);
  1643. }
  1644. default_hstate_idx = hstate_index(size_to_hstate(default_hstate_size));
  1645. if (default_hstate_max_huge_pages)
  1646. default_hstate.max_huge_pages = default_hstate_max_huge_pages;
  1647. hugetlb_init_hstates();
  1648. gather_bootmem_prealloc();
  1649. report_hugepages();
  1650. hugetlb_sysfs_init();
  1651. hugetlb_register_all_nodes();
  1652. hugetlb_cgroup_file_init();
  1653. return 0;
  1654. }
  1655. module_init(hugetlb_init);
  1656. /* Should be called on processing a hugepagesz=... option */
  1657. void __init hugetlb_add_hstate(unsigned order)
  1658. {
  1659. struct hstate *h;
  1660. unsigned long i;
  1661. if (size_to_hstate(PAGE_SIZE << order)) {
  1662. pr_warning("hugepagesz= specified twice, ignoring\n");
  1663. return;
  1664. }
  1665. BUG_ON(hugetlb_max_hstate >= HUGE_MAX_HSTATE);
  1666. BUG_ON(order == 0);
  1667. h = &hstates[hugetlb_max_hstate++];
  1668. h->order = order;
  1669. h->mask = ~((1ULL << (order + PAGE_SHIFT)) - 1);
  1670. h->nr_huge_pages = 0;
  1671. h->free_huge_pages = 0;
  1672. for (i = 0; i < MAX_NUMNODES; ++i)
  1673. INIT_LIST_HEAD(&h->hugepage_freelists[i]);
  1674. INIT_LIST_HEAD(&h->hugepage_activelist);
  1675. h->next_nid_to_alloc = first_node(node_states[N_MEMORY]);
  1676. h->next_nid_to_free = first_node(node_states[N_MEMORY]);
  1677. snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB",
  1678. huge_page_size(h)/1024);
  1679. parsed_hstate = h;
  1680. }
  1681. static int __init hugetlb_nrpages_setup(char *s)
  1682. {
  1683. unsigned long *mhp;
  1684. static unsigned long *last_mhp;
  1685. /*
  1686. * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter yet,
  1687. * so this hugepages= parameter goes to the "default hstate".
  1688. */
  1689. if (!hugetlb_max_hstate)
  1690. mhp = &default_hstate_max_huge_pages;
  1691. else
  1692. mhp = &parsed_hstate->max_huge_pages;
  1693. if (mhp == last_mhp) {
  1694. pr_warning("hugepages= specified twice without "
  1695. "interleaving hugepagesz=, ignoring\n");
  1696. return 1;
  1697. }
  1698. if (sscanf(s, "%lu", mhp) <= 0)
  1699. *mhp = 0;
  1700. /*
  1701. * Global state is always initialized later in hugetlb_init.
  1702. * But we need to allocate >= MAX_ORDER hstates here early to still
  1703. * use the bootmem allocator.
  1704. */
  1705. if (hugetlb_max_hstate && parsed_hstate->order >= MAX_ORDER)
  1706. hugetlb_hstate_alloc_pages(parsed_hstate);
  1707. last_mhp = mhp;
  1708. return 1;
  1709. }
  1710. __setup("hugepages=", hugetlb_nrpages_setup);
  1711. static int __init hugetlb_default_setup(char *s)
  1712. {
  1713. default_hstate_size = memparse(s, &s);
  1714. return 1;
  1715. }
  1716. __setup("default_hugepagesz=", hugetlb_default_setup);
  1717. static unsigned int cpuset_mems_nr(unsigned int *array)
  1718. {
  1719. int node;
  1720. unsigned int nr = 0;
  1721. for_each_node_mask(node, cpuset_current_mems_allowed)
  1722. nr += array[node];
  1723. return nr;
  1724. }
  1725. #ifdef CONFIG_SYSCTL
  1726. static int hugetlb_sysctl_handler_common(bool obey_mempolicy,
  1727. struct ctl_table *table, int write,
  1728. void __user *buffer, size_t *length, loff_t *ppos)
  1729. {
  1730. struct hstate *h = &default_hstate;
  1731. unsigned long tmp;
  1732. int ret;
  1733. tmp = h->max_huge_pages;
  1734. if (write && h->order >= MAX_ORDER)
  1735. return -EINVAL;
  1736. table->data = &tmp;
  1737. table->maxlen = sizeof(unsigned long);
  1738. ret = proc_doulongvec_minmax(table, write, buffer, length, ppos);
  1739. if (ret)
  1740. goto out;
  1741. if (write) {
  1742. NODEMASK_ALLOC(nodemask_t, nodes_allowed,
  1743. GFP_KERNEL | __GFP_NORETRY);
  1744. if (!(obey_mempolicy &&
  1745. init_nodemask_of_mempolicy(nodes_allowed))) {
  1746. NODEMASK_FREE(nodes_allowed);
  1747. nodes_allowed = &node_states[N_MEMORY];
  1748. }
  1749. h->max_huge_pages = set_max_huge_pages(h, tmp, nodes_allowed);
  1750. if (nodes_allowed != &node_states[N_MEMORY])
  1751. NODEMASK_FREE(nodes_allowed);
  1752. }
  1753. out:
  1754. return ret;
  1755. }
  1756. int hugetlb_sysctl_handler(struct ctl_table *table, int write,
  1757. void __user *buffer, size_t *length, loff_t *ppos)
  1758. {
  1759. return hugetlb_sysctl_handler_common(false, table, write,
  1760. buffer, length, ppos);
  1761. }
  1762. #ifdef CONFIG_NUMA
  1763. int hugetlb_mempolicy_sysctl_handler(struct ctl_table *table, int write,
  1764. void __user *buffer, size_t *length, loff_t *ppos)
  1765. {
  1766. return hugetlb_sysctl_handler_common(true, table, write,
  1767. buffer, length, ppos);
  1768. }
  1769. #endif /* CONFIG_NUMA */
  1770. int hugetlb_treat_movable_handler(struct ctl_table *table, int write,
  1771. void __user *buffer,
  1772. size_t *length, loff_t *ppos)
  1773. {
  1774. proc_dointvec(table, write, buffer, length, ppos);
  1775. if (hugepages_treat_as_movable)
  1776. htlb_alloc_mask = GFP_HIGHUSER_MOVABLE;
  1777. else
  1778. htlb_alloc_mask = GFP_HIGHUSER;
  1779. return 0;
  1780. }
  1781. int hugetlb_overcommit_handler(struct ctl_table *table, int write,
  1782. void __user *buffer,
  1783. size_t *length, loff_t *ppos)
  1784. {
  1785. struct hstate *h = &default_hstate;
  1786. unsigned long tmp;
  1787. int ret;
  1788. tmp = h->nr_overcommit_huge_pages;
  1789. if (write && h->order >= MAX_ORDER)
  1790. return -EINVAL;
  1791. table->data = &tmp;
  1792. table->maxlen = sizeof(unsigned long);
  1793. ret = proc_doulongvec_minmax(table, write, buffer, length, ppos);
  1794. if (ret)
  1795. goto out;
  1796. if (write) {
  1797. spin_lock(&hugetlb_lock);
  1798. h->nr_overcommit_huge_pages = tmp;
  1799. spin_unlock(&hugetlb_lock);
  1800. }
  1801. out:
  1802. return ret;
  1803. }
  1804. #endif /* CONFIG_SYSCTL */
  1805. void hugetlb_report_meminfo(struct seq_file *m)
  1806. {
  1807. struct hstate *h = &default_hstate;
  1808. seq_printf(m,
  1809. "HugePages_Total: %5lu\n"
  1810. "HugePages_Free: %5lu\n"
  1811. "HugePages_Rsvd: %5lu\n"
  1812. "HugePages_Surp: %5lu\n"
  1813. "Hugepagesize: %8lu kB\n",
  1814. h->nr_huge_pages,
  1815. h->free_huge_pages,
  1816. h->resv_huge_pages,
  1817. h->surplus_huge_pages,
  1818. 1UL << (huge_page_order(h) + PAGE_SHIFT - 10));
  1819. }
  1820. int hugetlb_report_node_meminfo(int nid, char *buf)
  1821. {
  1822. struct hstate *h = &default_hstate;
  1823. return sprintf(buf,
  1824. "Node %d HugePages_Total: %5u\n"
  1825. "Node %d HugePages_Free: %5u\n"
  1826. "Node %d HugePages_Surp: %5u\n",
  1827. nid, h->nr_huge_pages_node[nid],
  1828. nid, h->free_huge_pages_node[nid],
  1829. nid, h->surplus_huge_pages_node[nid]);
  1830. }
  1831. void hugetlb_show_meminfo(void)
  1832. {
  1833. struct hstate *h;
  1834. int nid;
  1835. for_each_node_state(nid, N_MEMORY)
  1836. for_each_hstate(h)
  1837. pr_info("Node %d hugepages_total=%u hugepages_free=%u hugepages_surp=%u hugepages_size=%lukB\n",
  1838. nid,
  1839. h->nr_huge_pages_node[nid],
  1840. h->free_huge_pages_node[nid],
  1841. h->surplus_huge_pages_node[nid],
  1842. 1UL << (huge_page_order(h) + PAGE_SHIFT - 10));
  1843. }
  1844. /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
  1845. unsigned long hugetlb_total_pages(void)
  1846. {
  1847. struct hstate *h;
  1848. unsigned long nr_total_pages = 0;
  1849. for_each_hstate(h)
  1850. nr_total_pages += h->nr_huge_pages * pages_per_huge_page(h);
  1851. return nr_total_pages;
  1852. }
  1853. static int hugetlb_acct_memory(struct hstate *h, long delta)
  1854. {
  1855. int ret = -ENOMEM;
  1856. spin_lock(&hugetlb_lock);
  1857. /*
  1858. * When cpuset is configured, it breaks the strict hugetlb page
  1859. * reservation as the accounting is done on a global variable. Such
  1860. * reservation is completely rubbish in the presence of cpuset because
  1861. * the reservation is not checked against page availability for the
  1862. * current cpuset. Application can still potentially OOM'ed by kernel
  1863. * with lack of free htlb page in cpuset that the task is in.
  1864. * Attempt to enforce strict accounting with cpuset is almost
  1865. * impossible (or too ugly) because cpuset is too fluid that
  1866. * task or memory node can be dynamically moved between cpusets.
  1867. *
  1868. * The change of semantics for shared hugetlb mapping with cpuset is
  1869. * undesirable. However, in order to preserve some of the semantics,
  1870. * we fall back to check against current free page availability as
  1871. * a best attempt and hopefully to minimize the impact of changing
  1872. * semantics that cpuset has.
  1873. */
  1874. if (delta > 0) {
  1875. if (gather_surplus_pages(h, delta) < 0)
  1876. goto out;
  1877. if (delta > cpuset_mems_nr(h->free_huge_pages_node)) {
  1878. return_unused_surplus_pages(h, delta);
  1879. goto out;
  1880. }
  1881. }
  1882. ret = 0;
  1883. if (delta < 0)
  1884. return_unused_surplus_pages(h, (unsigned long) -delta);
  1885. out:
  1886. spin_unlock(&hugetlb_lock);
  1887. return ret;
  1888. }
  1889. static void hugetlb_vm_op_open(struct vm_area_struct *vma)
  1890. {
  1891. struct resv_map *resv = vma_resv_map(vma);
  1892. /*
  1893. * This new VMA should share its siblings reservation map if present.
  1894. * The VMA will only ever have a valid reservation map pointer where
  1895. * it is being copied for another still existing VMA. As that VMA
  1896. * has a reference to the reservation map it cannot disappear until
  1897. * after this open call completes. It is therefore safe to take a
  1898. * new reference here without additional locking.
  1899. */
  1900. if (resv)
  1901. kref_get(&resv->refs);
  1902. }
  1903. static void resv_map_put(struct vm_area_struct *vma)
  1904. {
  1905. struct resv_map *resv = vma_resv_map(vma);
  1906. if (!resv)
  1907. return;
  1908. kref_put(&resv->refs, resv_map_release);
  1909. }
  1910. static void hugetlb_vm_op_close(struct vm_area_struct *vma)
  1911. {
  1912. struct hstate *h = hstate_vma(vma);
  1913. struct resv_map *resv = vma_resv_map(vma);
  1914. struct hugepage_subpool *spool = subpool_vma(vma);
  1915. unsigned long reserve;
  1916. unsigned long start;
  1917. unsigned long end;
  1918. if (resv) {
  1919. start = vma_hugecache_offset(h, vma, vma->vm_start);
  1920. end = vma_hugecache_offset(h, vma, vma->vm_end);
  1921. reserve = (end - start) -
  1922. region_count(&resv->regions, start, end);
  1923. resv_map_put(vma);
  1924. if (reserve) {
  1925. hugetlb_acct_memory(h, -reserve);
  1926. hugepage_subpool_put_pages(spool, reserve);
  1927. }
  1928. }
  1929. }
  1930. /*
  1931. * We cannot handle pagefaults against hugetlb pages at all. They cause
  1932. * handle_mm_fault() to try to instantiate regular-sized pages in the
  1933. * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
  1934. * this far.
  1935. */
  1936. static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1937. {
  1938. BUG();
  1939. return 0;
  1940. }
  1941. const struct vm_operations_struct hugetlb_vm_ops = {
  1942. .fault = hugetlb_vm_op_fault,
  1943. .open = hugetlb_vm_op_open,
  1944. .close = hugetlb_vm_op_close,
  1945. };
  1946. static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
  1947. int writable)
  1948. {
  1949. pte_t entry;
  1950. if (writable) {
  1951. entry = huge_pte_mkwrite(huge_pte_mkdirty(mk_huge_pte(page,
  1952. vma->vm_page_prot)));
  1953. } else {
  1954. entry = huge_pte_wrprotect(mk_huge_pte(page,
  1955. vma->vm_page_prot));
  1956. }
  1957. entry = pte_mkyoung(entry);
  1958. entry = pte_mkhuge(entry);
  1959. entry = arch_make_huge_pte(entry, vma, page, writable);
  1960. return entry;
  1961. }
  1962. static void set_huge_ptep_writable(struct vm_area_struct *vma,
  1963. unsigned long address, pte_t *ptep)
  1964. {
  1965. pte_t entry;
  1966. entry = huge_pte_mkwrite(huge_pte_mkdirty(huge_ptep_get(ptep)));
  1967. if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1))
  1968. update_mmu_cache(vma, address, ptep);
  1969. }
  1970. int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
  1971. struct vm_area_struct *vma)
  1972. {
  1973. pte_t *src_pte, *dst_pte, entry;
  1974. struct page *ptepage;
  1975. unsigned long addr;
  1976. int cow;
  1977. struct hstate *h = hstate_vma(vma);
  1978. unsigned long sz = huge_page_size(h);
  1979. cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
  1980. for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) {
  1981. src_pte = huge_pte_offset(src, addr);
  1982. if (!src_pte)
  1983. continue;
  1984. dst_pte = huge_pte_alloc(dst, addr, sz);
  1985. if (!dst_pte)
  1986. goto nomem;
  1987. /* If the pagetables are shared don't copy or take references */
  1988. if (dst_pte == src_pte)
  1989. continue;
  1990. spin_lock(&dst->page_table_lock);
  1991. spin_lock_nested(&src->page_table_lock, SINGLE_DEPTH_NESTING);
  1992. if (!huge_pte_none(huge_ptep_get(src_pte))) {
  1993. if (cow)
  1994. huge_ptep_set_wrprotect(src, addr, src_pte);
  1995. entry = huge_ptep_get(src_pte);
  1996. ptepage = pte_page(entry);
  1997. get_page(ptepage);
  1998. page_dup_rmap(ptepage);
  1999. set_huge_pte_at(dst, addr, dst_pte, entry);
  2000. }
  2001. spin_unlock(&src->page_table_lock);
  2002. spin_unlock(&dst->page_table_lock);
  2003. }
  2004. return 0;
  2005. nomem:
  2006. return -ENOMEM;
  2007. }
  2008. static int is_hugetlb_entry_migration(pte_t pte)
  2009. {
  2010. swp_entry_t swp;
  2011. if (huge_pte_none(pte) || pte_present(pte))
  2012. return 0;
  2013. swp = pte_to_swp_entry(pte);
  2014. if (non_swap_entry(swp) && is_migration_entry(swp))
  2015. return 1;
  2016. else
  2017. return 0;
  2018. }
  2019. static int is_hugetlb_entry_hwpoisoned(pte_t pte)
  2020. {
  2021. swp_entry_t swp;
  2022. if (huge_pte_none(pte) || pte_present(pte))
  2023. return 0;
  2024. swp = pte_to_swp_entry(pte);
  2025. if (non_swap_entry(swp) && is_hwpoison_entry(swp))
  2026. return 1;
  2027. else
  2028. return 0;
  2029. }
  2030. void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
  2031. unsigned long start, unsigned long end,
  2032. struct page *ref_page)
  2033. {
  2034. int force_flush = 0;
  2035. struct mm_struct *mm = vma->vm_mm;
  2036. unsigned long address;
  2037. pte_t *ptep;
  2038. pte_t pte;
  2039. struct page *page;
  2040. struct hstate *h = hstate_vma(vma);
  2041. unsigned long sz = huge_page_size(h);
  2042. const unsigned long mmun_start = start; /* For mmu_notifiers */
  2043. const unsigned long mmun_end = end; /* For mmu_notifiers */
  2044. WARN_ON(!is_vm_hugetlb_page(vma));
  2045. BUG_ON(start & ~huge_page_mask(h));
  2046. BUG_ON(end & ~huge_page_mask(h));
  2047. tlb_start_vma(tlb, vma);
  2048. mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
  2049. again:
  2050. spin_lock(&mm->page_table_lock);
  2051. for (address = start; address < end; address += sz) {
  2052. ptep = huge_pte_offset(mm, address);
  2053. if (!ptep)
  2054. continue;
  2055. if (huge_pmd_unshare(mm, &address, ptep))
  2056. continue;
  2057. pte = huge_ptep_get(ptep);
  2058. if (huge_pte_none(pte))
  2059. continue;
  2060. /*
  2061. * HWPoisoned hugepage is already unmapped and dropped reference
  2062. */
  2063. if (unlikely(is_hugetlb_entry_hwpoisoned(pte))) {
  2064. huge_pte_clear(mm, address, ptep);
  2065. continue;
  2066. }
  2067. page = pte_page(pte);
  2068. /*
  2069. * If a reference page is supplied, it is because a specific
  2070. * page is being unmapped, not a range. Ensure the page we
  2071. * are about to unmap is the actual page of interest.
  2072. */
  2073. if (ref_page) {
  2074. if (page != ref_page)
  2075. continue;
  2076. /*
  2077. * Mark the VMA as having unmapped its page so that
  2078. * future faults in this VMA will fail rather than
  2079. * looking like data was lost
  2080. */
  2081. set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
  2082. }
  2083. pte = huge_ptep_get_and_clear(mm, address, ptep);
  2084. tlb_remove_tlb_entry(tlb, ptep, address);
  2085. if (huge_pte_dirty(pte))
  2086. set_page_dirty(page);
  2087. page_remove_rmap(page);
  2088. force_flush = !__tlb_remove_page(tlb, page);
  2089. if (force_flush)
  2090. break;
  2091. /* Bail out after unmapping reference page if supplied */
  2092. if (ref_page)
  2093. break;
  2094. }
  2095. spin_unlock(&mm->page_table_lock);
  2096. /*
  2097. * mmu_gather ran out of room to batch pages, we break out of
  2098. * the PTE lock to avoid doing the potential expensive TLB invalidate
  2099. * and page-free while holding it.
  2100. */
  2101. if (force_flush) {
  2102. force_flush = 0;
  2103. tlb_flush_mmu(tlb);
  2104. if (address < end && !ref_page)
  2105. goto again;
  2106. }
  2107. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  2108. tlb_end_vma(tlb, vma);
  2109. }
  2110. void __unmap_hugepage_range_final(struct mmu_gather *tlb,
  2111. struct vm_area_struct *vma, unsigned long start,
  2112. unsigned long end, struct page *ref_page)
  2113. {
  2114. __unmap_hugepage_range(tlb, vma, start, end, ref_page);
  2115. /*
  2116. * Clear this flag so that x86's huge_pmd_share page_table_shareable
  2117. * test will fail on a vma being torn down, and not grab a page table
  2118. * on its way out. We're lucky that the flag has such an appropriate
  2119. * name, and can in fact be safely cleared here. We could clear it
  2120. * before the __unmap_hugepage_range above, but all that's necessary
  2121. * is to clear it before releasing the i_mmap_mutex. This works
  2122. * because in the context this is called, the VMA is about to be
  2123. * destroyed and the i_mmap_mutex is held.
  2124. */
  2125. vma->vm_flags &= ~VM_MAYSHARE;
  2126. }
  2127. void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
  2128. unsigned long end, struct page *ref_page)
  2129. {
  2130. struct mm_struct *mm;
  2131. struct mmu_gather tlb;
  2132. mm = vma->vm_mm;
  2133. tlb_gather_mmu(&tlb, mm, start, end);
  2134. __unmap_hugepage_range(&tlb, vma, start, end, ref_page);
  2135. tlb_finish_mmu(&tlb, start, end);
  2136. }
  2137. /*
  2138. * This is called when the original mapper is failing to COW a MAP_PRIVATE
  2139. * mappping it owns the reserve page for. The intention is to unmap the page
  2140. * from other VMAs and let the children be SIGKILLed if they are faulting the
  2141. * same region.
  2142. */
  2143. static int unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,
  2144. struct page *page, unsigned long address)
  2145. {
  2146. struct hstate *h = hstate_vma(vma);
  2147. struct vm_area_struct *iter_vma;
  2148. struct address_space *mapping;
  2149. pgoff_t pgoff;
  2150. /*
  2151. * vm_pgoff is in PAGE_SIZE units, hence the different calculation
  2152. * from page cache lookup which is in HPAGE_SIZE units.
  2153. */
  2154. address = address & huge_page_mask(h);
  2155. pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) +
  2156. vma->vm_pgoff;
  2157. mapping = file_inode(vma->vm_file)->i_mapping;
  2158. /*
  2159. * Take the mapping lock for the duration of the table walk. As
  2160. * this mapping should be shared between all the VMAs,
  2161. * __unmap_hugepage_range() is called as the lock is already held
  2162. */
  2163. mutex_lock(&mapping->i_mmap_mutex);
  2164. vma_interval_tree_foreach(iter_vma, &mapping->i_mmap, pgoff, pgoff) {
  2165. /* Do not unmap the current VMA */
  2166. if (iter_vma == vma)
  2167. continue;
  2168. /*
  2169. * Unmap the page from other VMAs without their own reserves.
  2170. * They get marked to be SIGKILLed if they fault in these
  2171. * areas. This is because a future no-page fault on this VMA
  2172. * could insert a zeroed page instead of the data existing
  2173. * from the time of fork. This would look like data corruption
  2174. */
  2175. if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
  2176. unmap_hugepage_range(iter_vma, address,
  2177. address + huge_page_size(h), page);
  2178. }
  2179. mutex_unlock(&mapping->i_mmap_mutex);
  2180. return 1;
  2181. }
  2182. /*
  2183. * Hugetlb_cow() should be called with page lock of the original hugepage held.
  2184. * Called with hugetlb_instantiation_mutex held and pte_page locked so we
  2185. * cannot race with other handlers or page migration.
  2186. * Keep the pte_same checks anyway to make transition from the mutex easier.
  2187. */
  2188. static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
  2189. unsigned long address, pte_t *ptep, pte_t pte,
  2190. struct page *pagecache_page)
  2191. {
  2192. struct hstate *h = hstate_vma(vma);
  2193. struct page *old_page, *new_page;
  2194. int outside_reserve = 0;
  2195. unsigned long mmun_start; /* For mmu_notifiers */
  2196. unsigned long mmun_end; /* For mmu_notifiers */
  2197. old_page = pte_page(pte);
  2198. retry_avoidcopy:
  2199. /* If no-one else is actually using this page, avoid the copy
  2200. * and just make the page writable */
  2201. if (page_mapcount(old_page) == 1 && PageAnon(old_page)) {
  2202. page_move_anon_rmap(old_page, vma, address);
  2203. set_huge_ptep_writable(vma, address, ptep);
  2204. return 0;
  2205. }
  2206. /*
  2207. * If the process that created a MAP_PRIVATE mapping is about to
  2208. * perform a COW due to a shared page count, attempt to satisfy
  2209. * the allocation without using the existing reserves. The pagecache
  2210. * page is used to determine if the reserve at this address was
  2211. * consumed or not. If reserves were used, a partial faulted mapping
  2212. * at the time of fork() could consume its reserves on COW instead
  2213. * of the full address range.
  2214. */
  2215. if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
  2216. old_page != pagecache_page)
  2217. outside_reserve = 1;
  2218. page_cache_get(old_page);
  2219. /* Drop page_table_lock as buddy allocator may be called */
  2220. spin_unlock(&mm->page_table_lock);
  2221. new_page = alloc_huge_page(vma, address, outside_reserve);
  2222. if (IS_ERR(new_page)) {
  2223. long err = PTR_ERR(new_page);
  2224. page_cache_release(old_page);
  2225. /*
  2226. * If a process owning a MAP_PRIVATE mapping fails to COW,
  2227. * it is due to references held by a child and an insufficient
  2228. * huge page pool. To guarantee the original mappers
  2229. * reliability, unmap the page from child processes. The child
  2230. * may get SIGKILLed if it later faults.
  2231. */
  2232. if (outside_reserve) {
  2233. BUG_ON(huge_pte_none(pte));
  2234. if (unmap_ref_private(mm, vma, old_page, address)) {
  2235. BUG_ON(huge_pte_none(pte));
  2236. spin_lock(&mm->page_table_lock);
  2237. ptep = huge_pte_offset(mm, address & huge_page_mask(h));
  2238. if (likely(pte_same(huge_ptep_get(ptep), pte)))
  2239. goto retry_avoidcopy;
  2240. /*
  2241. * race occurs while re-acquiring page_table_lock, and
  2242. * our job is done.
  2243. */
  2244. return 0;
  2245. }
  2246. WARN_ON_ONCE(1);
  2247. }
  2248. /* Caller expects lock to be held */
  2249. spin_lock(&mm->page_table_lock);
  2250. if (err == -ENOMEM)
  2251. return VM_FAULT_OOM;
  2252. else
  2253. return VM_FAULT_SIGBUS;
  2254. }
  2255. /*
  2256. * When the original hugepage is shared one, it does not have
  2257. * anon_vma prepared.
  2258. */
  2259. if (unlikely(anon_vma_prepare(vma))) {
  2260. page_cache_release(new_page);
  2261. page_cache_release(old_page);
  2262. /* Caller expects lock to be held */
  2263. spin_lock(&mm->page_table_lock);
  2264. return VM_FAULT_OOM;
  2265. }
  2266. copy_user_huge_page(new_page, old_page, address, vma,
  2267. pages_per_huge_page(h));
  2268. __SetPageUptodate(new_page);
  2269. mmun_start = address & huge_page_mask(h);
  2270. mmun_end = mmun_start + huge_page_size(h);
  2271. mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
  2272. /*
  2273. * Retake the page_table_lock to check for racing updates
  2274. * before the page tables are altered
  2275. */
  2276. spin_lock(&mm->page_table_lock);
  2277. ptep = huge_pte_offset(mm, address & huge_page_mask(h));
  2278. if (likely(pte_same(huge_ptep_get(ptep), pte))) {
  2279. ClearPagePrivate(new_page);
  2280. /* Break COW */
  2281. huge_ptep_clear_flush(vma, address, ptep);
  2282. set_huge_pte_at(mm, address, ptep,
  2283. make_huge_pte(vma, new_page, 1));
  2284. page_remove_rmap(old_page);
  2285. hugepage_add_new_anon_rmap(new_page, vma, address);
  2286. /* Make the old page be freed below */
  2287. new_page = old_page;
  2288. }
  2289. spin_unlock(&mm->page_table_lock);
  2290. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  2291. page_cache_release(new_page);
  2292. page_cache_release(old_page);
  2293. /* Caller expects lock to be held */
  2294. spin_lock(&mm->page_table_lock);
  2295. return 0;
  2296. }
  2297. /* Return the pagecache page at a given address within a VMA */
  2298. static struct page *hugetlbfs_pagecache_page(struct hstate *h,
  2299. struct vm_area_struct *vma, unsigned long address)
  2300. {
  2301. struct address_space *mapping;
  2302. pgoff_t idx;
  2303. mapping = vma->vm_file->f_mapping;
  2304. idx = vma_hugecache_offset(h, vma, address);
  2305. return find_lock_page(mapping, idx);
  2306. }
  2307. /*
  2308. * Return whether there is a pagecache page to back given address within VMA.
  2309. * Caller follow_hugetlb_page() holds page_table_lock so we cannot lock_page.
  2310. */
  2311. static bool hugetlbfs_pagecache_present(struct hstate *h,
  2312. struct vm_area_struct *vma, unsigned long address)
  2313. {
  2314. struct address_space *mapping;
  2315. pgoff_t idx;
  2316. struct page *page;
  2317. mapping = vma->vm_file->f_mapping;
  2318. idx = vma_hugecache_offset(h, vma, address);
  2319. page = find_get_page(mapping, idx);
  2320. if (page)
  2321. put_page(page);
  2322. return page != NULL;
  2323. }
  2324. static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
  2325. unsigned long address, pte_t *ptep, unsigned int flags)
  2326. {
  2327. struct hstate *h = hstate_vma(vma);
  2328. int ret = VM_FAULT_SIGBUS;
  2329. int anon_rmap = 0;
  2330. pgoff_t idx;
  2331. unsigned long size;
  2332. struct page *page;
  2333. struct address_space *mapping;
  2334. pte_t new_pte;
  2335. /*
  2336. * Currently, we are forced to kill the process in the event the
  2337. * original mapper has unmapped pages from the child due to a failed
  2338. * COW. Warn that such a situation has occurred as it may not be obvious
  2339. */
  2340. if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
  2341. pr_warning("PID %d killed due to inadequate hugepage pool\n",
  2342. current->pid);
  2343. return ret;
  2344. }
  2345. mapping = vma->vm_file->f_mapping;
  2346. idx = vma_hugecache_offset(h, vma, address);
  2347. /*
  2348. * Use page lock to guard against racing truncation
  2349. * before we get page_table_lock.
  2350. */
  2351. retry:
  2352. page = find_lock_page(mapping, idx);
  2353. if (!page) {
  2354. size = i_size_read(mapping->host) >> huge_page_shift(h);
  2355. if (idx >= size)
  2356. goto out;
  2357. page = alloc_huge_page(vma, address, 0);
  2358. if (IS_ERR(page)) {
  2359. ret = PTR_ERR(page);
  2360. if (ret == -ENOMEM)
  2361. ret = VM_FAULT_OOM;
  2362. else
  2363. ret = VM_FAULT_SIGBUS;
  2364. goto out;
  2365. }
  2366. clear_huge_page(page, address, pages_per_huge_page(h));
  2367. __SetPageUptodate(page);
  2368. if (vma->vm_flags & VM_MAYSHARE) {
  2369. int err;
  2370. struct inode *inode = mapping->host;
  2371. err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
  2372. if (err) {
  2373. put_page(page);
  2374. if (err == -EEXIST)
  2375. goto retry;
  2376. goto out;
  2377. }
  2378. ClearPagePrivate(page);
  2379. spin_lock(&inode->i_lock);
  2380. inode->i_blocks += blocks_per_huge_page(h);
  2381. spin_unlock(&inode->i_lock);
  2382. } else {
  2383. lock_page(page);
  2384. if (unlikely(anon_vma_prepare(vma))) {
  2385. ret = VM_FAULT_OOM;
  2386. goto backout_unlocked;
  2387. }
  2388. anon_rmap = 1;
  2389. }
  2390. } else {
  2391. /*
  2392. * If memory error occurs between mmap() and fault, some process
  2393. * don't have hwpoisoned swap entry for errored virtual address.
  2394. * So we need to block hugepage fault by PG_hwpoison bit check.
  2395. */
  2396. if (unlikely(PageHWPoison(page))) {
  2397. ret = VM_FAULT_HWPOISON |
  2398. VM_FAULT_SET_HINDEX(hstate_index(h));
  2399. goto backout_unlocked;
  2400. }
  2401. }
  2402. /*
  2403. * If we are going to COW a private mapping later, we examine the
  2404. * pending reservations for this page now. This will ensure that
  2405. * any allocations necessary to record that reservation occur outside
  2406. * the spinlock.
  2407. */
  2408. if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED))
  2409. if (vma_needs_reservation(h, vma, address) < 0) {
  2410. ret = VM_FAULT_OOM;
  2411. goto backout_unlocked;
  2412. }
  2413. spin_lock(&mm->page_table_lock);
  2414. size = i_size_read(mapping->host) >> huge_page_shift(h);
  2415. if (idx >= size)
  2416. goto backout;
  2417. ret = 0;
  2418. if (!huge_pte_none(huge_ptep_get(ptep)))
  2419. goto backout;
  2420. if (anon_rmap) {
  2421. ClearPagePrivate(page);
  2422. hugepage_add_new_anon_rmap(page, vma, address);
  2423. }
  2424. else
  2425. page_dup_rmap(page);
  2426. new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
  2427. && (vma->vm_flags & VM_SHARED)));
  2428. set_huge_pte_at(mm, address, ptep, new_pte);
  2429. if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
  2430. /* Optimization, do the COW without a second fault */
  2431. ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page);
  2432. }
  2433. spin_unlock(&mm->page_table_lock);
  2434. unlock_page(page);
  2435. out:
  2436. return ret;
  2437. backout:
  2438. spin_unlock(&mm->page_table_lock);
  2439. backout_unlocked:
  2440. unlock_page(page);
  2441. put_page(page);
  2442. goto out;
  2443. }
  2444. int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  2445. unsigned long address, unsigned int flags)
  2446. {
  2447. pte_t *ptep;
  2448. pte_t entry;
  2449. int ret;
  2450. struct page *page = NULL;
  2451. struct page *pagecache_page = NULL;
  2452. static DEFINE_MUTEX(hugetlb_instantiation_mutex);
  2453. struct hstate *h = hstate_vma(vma);
  2454. address &= huge_page_mask(h);
  2455. ptep = huge_pte_offset(mm, address);
  2456. if (ptep) {
  2457. entry = huge_ptep_get(ptep);
  2458. if (unlikely(is_hugetlb_entry_migration(entry))) {
  2459. migration_entry_wait_huge(mm, ptep);
  2460. return 0;
  2461. } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry)))
  2462. return VM_FAULT_HWPOISON_LARGE |
  2463. VM_FAULT_SET_HINDEX(hstate_index(h));
  2464. }
  2465. ptep = huge_pte_alloc(mm, address, huge_page_size(h));
  2466. if (!ptep)
  2467. return VM_FAULT_OOM;
  2468. /*
  2469. * Serialize hugepage allocation and instantiation, so that we don't
  2470. * get spurious allocation failures if two CPUs race to instantiate
  2471. * the same page in the page cache.
  2472. */
  2473. mutex_lock(&hugetlb_instantiation_mutex);
  2474. entry = huge_ptep_get(ptep);
  2475. if (huge_pte_none(entry)) {
  2476. ret = hugetlb_no_page(mm, vma, address, ptep, flags);
  2477. goto out_mutex;
  2478. }
  2479. ret = 0;
  2480. /*
  2481. * If we are going to COW the mapping later, we examine the pending
  2482. * reservations for this page now. This will ensure that any
  2483. * allocations necessary to record that reservation occur outside the
  2484. * spinlock. For private mappings, we also lookup the pagecache
  2485. * page now as it is used to determine if a reservation has been
  2486. * consumed.
  2487. */
  2488. if ((flags & FAULT_FLAG_WRITE) && !huge_pte_write(entry)) {
  2489. if (vma_needs_reservation(h, vma, address) < 0) {
  2490. ret = VM_FAULT_OOM;
  2491. goto out_mutex;
  2492. }
  2493. if (!(vma->vm_flags & VM_MAYSHARE))
  2494. pagecache_page = hugetlbfs_pagecache_page(h,
  2495. vma, address);
  2496. }
  2497. /*
  2498. * hugetlb_cow() requires page locks of pte_page(entry) and
  2499. * pagecache_page, so here we need take the former one
  2500. * when page != pagecache_page or !pagecache_page.
  2501. * Note that locking order is always pagecache_page -> page,
  2502. * so no worry about deadlock.
  2503. */
  2504. page = pte_page(entry);
  2505. get_page(page);
  2506. if (page != pagecache_page)
  2507. lock_page(page);
  2508. spin_lock(&mm->page_table_lock);
  2509. /* Check for a racing update before calling hugetlb_cow */
  2510. if (unlikely(!pte_same(entry, huge_ptep_get(ptep))))
  2511. goto out_page_table_lock;
  2512. if (flags & FAULT_FLAG_WRITE) {
  2513. if (!huge_pte_write(entry)) {
  2514. ret = hugetlb_cow(mm, vma, address, ptep, entry,
  2515. pagecache_page);
  2516. goto out_page_table_lock;
  2517. }
  2518. entry = huge_pte_mkdirty(entry);
  2519. }
  2520. entry = pte_mkyoung(entry);
  2521. if (huge_ptep_set_access_flags(vma, address, ptep, entry,
  2522. flags & FAULT_FLAG_WRITE))
  2523. update_mmu_cache(vma, address, ptep);
  2524. out_page_table_lock:
  2525. spin_unlock(&mm->page_table_lock);
  2526. if (pagecache_page) {
  2527. unlock_page(pagecache_page);
  2528. put_page(pagecache_page);
  2529. }
  2530. if (page != pagecache_page)
  2531. unlock_page(page);
  2532. put_page(page);
  2533. out_mutex:
  2534. mutex_unlock(&hugetlb_instantiation_mutex);
  2535. return ret;
  2536. }
  2537. long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
  2538. struct page **pages, struct vm_area_struct **vmas,
  2539. unsigned long *position, unsigned long *nr_pages,
  2540. long i, unsigned int flags)
  2541. {
  2542. unsigned long pfn_offset;
  2543. unsigned long vaddr = *position;
  2544. unsigned long remainder = *nr_pages;
  2545. struct hstate *h = hstate_vma(vma);
  2546. spin_lock(&mm->page_table_lock);
  2547. while (vaddr < vma->vm_end && remainder) {
  2548. pte_t *pte;
  2549. int absent;
  2550. struct page *page;
  2551. /*
  2552. * Some archs (sparc64, sh*) have multiple pte_ts to
  2553. * each hugepage. We have to make sure we get the
  2554. * first, for the page indexing below to work.
  2555. */
  2556. pte = huge_pte_offset(mm, vaddr & huge_page_mask(h));
  2557. absent = !pte || huge_pte_none(huge_ptep_get(pte));
  2558. /*
  2559. * When coredumping, it suits get_dump_page if we just return
  2560. * an error where there's an empty slot with no huge pagecache
  2561. * to back it. This way, we avoid allocating a hugepage, and
  2562. * the sparse dumpfile avoids allocating disk blocks, but its
  2563. * huge holes still show up with zeroes where they need to be.
  2564. */
  2565. if (absent && (flags & FOLL_DUMP) &&
  2566. !hugetlbfs_pagecache_present(h, vma, vaddr)) {
  2567. remainder = 0;
  2568. break;
  2569. }
  2570. /*
  2571. * We need call hugetlb_fault for both hugepages under migration
  2572. * (in which case hugetlb_fault waits for the migration,) and
  2573. * hwpoisoned hugepages (in which case we need to prevent the
  2574. * caller from accessing to them.) In order to do this, we use
  2575. * here is_swap_pte instead of is_hugetlb_entry_migration and
  2576. * is_hugetlb_entry_hwpoisoned. This is because it simply covers
  2577. * both cases, and because we can't follow correct pages
  2578. * directly from any kind of swap entries.
  2579. */
  2580. if (absent || is_swap_pte(huge_ptep_get(pte)) ||
  2581. ((flags & FOLL_WRITE) &&
  2582. !huge_pte_write(huge_ptep_get(pte)))) {
  2583. int ret;
  2584. spin_unlock(&mm->page_table_lock);
  2585. ret = hugetlb_fault(mm, vma, vaddr,
  2586. (flags & FOLL_WRITE) ? FAULT_FLAG_WRITE : 0);
  2587. spin_lock(&mm->page_table_lock);
  2588. if (!(ret & VM_FAULT_ERROR))
  2589. continue;
  2590. remainder = 0;
  2591. break;
  2592. }
  2593. pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT;
  2594. page = pte_page(huge_ptep_get(pte));
  2595. same_page:
  2596. if (pages) {
  2597. pages[i] = mem_map_offset(page, pfn_offset);
  2598. get_page(pages[i]);
  2599. }
  2600. if (vmas)
  2601. vmas[i] = vma;
  2602. vaddr += PAGE_SIZE;
  2603. ++pfn_offset;
  2604. --remainder;
  2605. ++i;
  2606. if (vaddr < vma->vm_end && remainder &&
  2607. pfn_offset < pages_per_huge_page(h)) {
  2608. /*
  2609. * We use pfn_offset to avoid touching the pageframes
  2610. * of this compound page.
  2611. */
  2612. goto same_page;
  2613. }
  2614. }
  2615. spin_unlock(&mm->page_table_lock);
  2616. *nr_pages = remainder;
  2617. *position = vaddr;
  2618. return i ? i : -EFAULT;
  2619. }
  2620. unsigned long hugetlb_change_protection(struct vm_area_struct *vma,
  2621. unsigned long address, unsigned long end, pgprot_t newprot)
  2622. {
  2623. struct mm_struct *mm = vma->vm_mm;
  2624. unsigned long start = address;
  2625. pte_t *ptep;
  2626. pte_t pte;
  2627. struct hstate *h = hstate_vma(vma);
  2628. unsigned long pages = 0;
  2629. BUG_ON(address >= end);
  2630. flush_cache_range(vma, address, end);
  2631. mutex_lock(&vma->vm_file->f_mapping->i_mmap_mutex);
  2632. spin_lock(&mm->page_table_lock);
  2633. for (; address < end; address += huge_page_size(h)) {
  2634. ptep = huge_pte_offset(mm, address);
  2635. if (!ptep)
  2636. continue;
  2637. if (huge_pmd_unshare(mm, &address, ptep)) {
  2638. pages++;
  2639. continue;
  2640. }
  2641. if (!huge_pte_none(huge_ptep_get(ptep))) {
  2642. pte = huge_ptep_get_and_clear(mm, address, ptep);
  2643. pte = pte_mkhuge(huge_pte_modify(pte, newprot));
  2644. pte = arch_make_huge_pte(pte, vma, NULL, 0);
  2645. set_huge_pte_at(mm, address, ptep, pte);
  2646. pages++;
  2647. }
  2648. }
  2649. spin_unlock(&mm->page_table_lock);
  2650. /*
  2651. * Must flush TLB before releasing i_mmap_mutex: x86's huge_pmd_unshare
  2652. * may have cleared our pud entry and done put_page on the page table:
  2653. * once we release i_mmap_mutex, another task can do the final put_page
  2654. * and that page table be reused and filled with junk.
  2655. */
  2656. flush_tlb_range(vma, start, end);
  2657. mutex_unlock(&vma->vm_file->f_mapping->i_mmap_mutex);
  2658. return pages << h->order;
  2659. }
  2660. int hugetlb_reserve_pages(struct inode *inode,
  2661. long from, long to,
  2662. struct vm_area_struct *vma,
  2663. vm_flags_t vm_flags)
  2664. {
  2665. long ret, chg;
  2666. struct hstate *h = hstate_inode(inode);
  2667. struct hugepage_subpool *spool = subpool_inode(inode);
  2668. /*
  2669. * Only apply hugepage reservation if asked. At fault time, an
  2670. * attempt will be made for VM_NORESERVE to allocate a page
  2671. * without using reserves
  2672. */
  2673. if (vm_flags & VM_NORESERVE)
  2674. return 0;
  2675. /*
  2676. * Shared mappings base their reservation on the number of pages that
  2677. * are already allocated on behalf of the file. Private mappings need
  2678. * to reserve the full area even if read-only as mprotect() may be
  2679. * called to make the mapping read-write. Assume !vma is a shm mapping
  2680. */
  2681. if (!vma || vma->vm_flags & VM_MAYSHARE)
  2682. chg = region_chg(&inode->i_mapping->private_list, from, to);
  2683. else {
  2684. struct resv_map *resv_map = resv_map_alloc();
  2685. if (!resv_map)
  2686. return -ENOMEM;
  2687. chg = to - from;
  2688. set_vma_resv_map(vma, resv_map);
  2689. set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
  2690. }
  2691. if (chg < 0) {
  2692. ret = chg;
  2693. goto out_err;
  2694. }
  2695. /* There must be enough pages in the subpool for the mapping */
  2696. if (hugepage_subpool_get_pages(spool, chg)) {
  2697. ret = -ENOSPC;
  2698. goto out_err;
  2699. }
  2700. /*
  2701. * Check enough hugepages are available for the reservation.
  2702. * Hand the pages back to the subpool if there are not
  2703. */
  2704. ret = hugetlb_acct_memory(h, chg);
  2705. if (ret < 0) {
  2706. hugepage_subpool_put_pages(spool, chg);
  2707. goto out_err;
  2708. }
  2709. /*
  2710. * Account for the reservations made. Shared mappings record regions
  2711. * that have reservations as they are shared by multiple VMAs.
  2712. * When the last VMA disappears, the region map says how much
  2713. * the reservation was and the page cache tells how much of
  2714. * the reservation was consumed. Private mappings are per-VMA and
  2715. * only the consumed reservations are tracked. When the VMA
  2716. * disappears, the original reservation is the VMA size and the
  2717. * consumed reservations are stored in the map. Hence, nothing
  2718. * else has to be done for private mappings here
  2719. */
  2720. if (!vma || vma->vm_flags & VM_MAYSHARE)
  2721. region_add(&inode->i_mapping->private_list, from, to);
  2722. return 0;
  2723. out_err:
  2724. if (vma)
  2725. resv_map_put(vma);
  2726. return ret;
  2727. }
  2728. void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
  2729. {
  2730. struct hstate *h = hstate_inode(inode);
  2731. long chg = region_truncate(&inode->i_mapping->private_list, offset);
  2732. struct hugepage_subpool *spool = subpool_inode(inode);
  2733. spin_lock(&inode->i_lock);
  2734. inode->i_blocks -= (blocks_per_huge_page(h) * freed);
  2735. spin_unlock(&inode->i_lock);
  2736. hugepage_subpool_put_pages(spool, (chg - freed));
  2737. hugetlb_acct_memory(h, -(chg - freed));
  2738. }
  2739. #ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE
  2740. static unsigned long page_table_shareable(struct vm_area_struct *svma,
  2741. struct vm_area_struct *vma,
  2742. unsigned long addr, pgoff_t idx)
  2743. {
  2744. unsigned long saddr = ((idx - svma->vm_pgoff) << PAGE_SHIFT) +
  2745. svma->vm_start;
  2746. unsigned long sbase = saddr & PUD_MASK;
  2747. unsigned long s_end = sbase + PUD_SIZE;
  2748. /* Allow segments to share if only one is marked locked */
  2749. unsigned long vm_flags = vma->vm_flags & ~VM_LOCKED;
  2750. unsigned long svm_flags = svma->vm_flags & ~VM_LOCKED;
  2751. /*
  2752. * match the virtual addresses, permission and the alignment of the
  2753. * page table page.
  2754. */
  2755. if (pmd_index(addr) != pmd_index(saddr) ||
  2756. vm_flags != svm_flags ||
  2757. sbase < svma->vm_start || svma->vm_end < s_end)
  2758. return 0;
  2759. return saddr;
  2760. }
  2761. static int vma_shareable(struct vm_area_struct *vma, unsigned long addr)
  2762. {
  2763. unsigned long base = addr & PUD_MASK;
  2764. unsigned long end = base + PUD_SIZE;
  2765. /*
  2766. * check on proper vm_flags and page table alignment
  2767. */
  2768. if (vma->vm_flags & VM_MAYSHARE &&
  2769. vma->vm_start <= base && end <= vma->vm_end)
  2770. return 1;
  2771. return 0;
  2772. }
  2773. /*
  2774. * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc()
  2775. * and returns the corresponding pte. While this is not necessary for the
  2776. * !shared pmd case because we can allocate the pmd later as well, it makes the
  2777. * code much cleaner. pmd allocation is essential for the shared case because
  2778. * pud has to be populated inside the same i_mmap_mutex section - otherwise
  2779. * racing tasks could either miss the sharing (see huge_pte_offset) or select a
  2780. * bad pmd for sharing.
  2781. */
  2782. pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)
  2783. {
  2784. struct vm_area_struct *vma = find_vma(mm, addr);
  2785. struct address_space *mapping = vma->vm_file->f_mapping;
  2786. pgoff_t idx = ((addr - vma->vm_start) >> PAGE_SHIFT) +
  2787. vma->vm_pgoff;
  2788. struct vm_area_struct *svma;
  2789. unsigned long saddr;
  2790. pte_t *spte = NULL;
  2791. pte_t *pte;
  2792. if (!vma_shareable(vma, addr))
  2793. return (pte_t *)pmd_alloc(mm, pud, addr);
  2794. mutex_lock(&mapping->i_mmap_mutex);
  2795. vma_interval_tree_foreach(svma, &mapping->i_mmap, idx, idx) {
  2796. if (svma == vma)
  2797. continue;
  2798. saddr = page_table_shareable(svma, vma, addr, idx);
  2799. if (saddr) {
  2800. spte = huge_pte_offset(svma->vm_mm, saddr);
  2801. if (spte) {
  2802. get_page(virt_to_page(spte));
  2803. break;
  2804. }
  2805. }
  2806. }
  2807. if (!spte)
  2808. goto out;
  2809. spin_lock(&mm->page_table_lock);
  2810. if (pud_none(*pud))
  2811. pud_populate(mm, pud,
  2812. (pmd_t *)((unsigned long)spte & PAGE_MASK));
  2813. else
  2814. put_page(virt_to_page(spte));
  2815. spin_unlock(&mm->page_table_lock);
  2816. out:
  2817. pte = (pte_t *)pmd_alloc(mm, pud, addr);
  2818. mutex_unlock(&mapping->i_mmap_mutex);
  2819. return pte;
  2820. }
  2821. /*
  2822. * unmap huge page backed by shared pte.
  2823. *
  2824. * Hugetlb pte page is ref counted at the time of mapping. If pte is shared
  2825. * indicated by page_count > 1, unmap is achieved by clearing pud and
  2826. * decrementing the ref count. If count == 1, the pte page is not shared.
  2827. *
  2828. * called with vma->vm_mm->page_table_lock held.
  2829. *
  2830. * returns: 1 successfully unmapped a shared pte page
  2831. * 0 the underlying pte page is not shared, or it is the last user
  2832. */
  2833. int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
  2834. {
  2835. pgd_t *pgd = pgd_offset(mm, *addr);
  2836. pud_t *pud = pud_offset(pgd, *addr);
  2837. BUG_ON(page_count(virt_to_page(ptep)) == 0);
  2838. if (page_count(virt_to_page(ptep)) == 1)
  2839. return 0;
  2840. pud_clear(pud);
  2841. put_page(virt_to_page(ptep));
  2842. *addr = ALIGN(*addr, HPAGE_SIZE * PTRS_PER_PTE) - HPAGE_SIZE;
  2843. return 1;
  2844. }
  2845. #define want_pmd_share() (1)
  2846. #else /* !CONFIG_ARCH_WANT_HUGE_PMD_SHARE */
  2847. pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)
  2848. {
  2849. return NULL;
  2850. }
  2851. #define want_pmd_share() (0)
  2852. #endif /* CONFIG_ARCH_WANT_HUGE_PMD_SHARE */
  2853. #ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB
  2854. pte_t *huge_pte_alloc(struct mm_struct *mm,
  2855. unsigned long addr, unsigned long sz)
  2856. {
  2857. pgd_t *pgd;
  2858. pud_t *pud;
  2859. pte_t *pte = NULL;
  2860. pgd = pgd_offset(mm, addr);
  2861. pud = pud_alloc(mm, pgd, addr);
  2862. if (pud) {
  2863. if (sz == PUD_SIZE) {
  2864. pte = (pte_t *)pud;
  2865. } else {
  2866. BUG_ON(sz != PMD_SIZE);
  2867. if (want_pmd_share() && pud_none(*pud))
  2868. pte = huge_pmd_share(mm, addr, pud);
  2869. else
  2870. pte = (pte_t *)pmd_alloc(mm, pud, addr);
  2871. }
  2872. }
  2873. BUG_ON(pte && !pte_none(*pte) && !pte_huge(*pte));
  2874. return pte;
  2875. }
  2876. pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
  2877. {
  2878. pgd_t *pgd;
  2879. pud_t *pud;
  2880. pmd_t *pmd = NULL;
  2881. pgd = pgd_offset(mm, addr);
  2882. if (pgd_present(*pgd)) {
  2883. pud = pud_offset(pgd, addr);
  2884. if (pud_present(*pud)) {
  2885. if (pud_huge(*pud))
  2886. return (pte_t *)pud;
  2887. pmd = pmd_offset(pud, addr);
  2888. }
  2889. }
  2890. return (pte_t *) pmd;
  2891. }
  2892. struct page *
  2893. follow_huge_pmd(struct mm_struct *mm, unsigned long address,
  2894. pmd_t *pmd, int write)
  2895. {
  2896. struct page *page;
  2897. page = pte_page(*(pte_t *)pmd);
  2898. if (page)
  2899. page += ((address & ~PMD_MASK) >> PAGE_SHIFT);
  2900. return page;
  2901. }
  2902. struct page *
  2903. follow_huge_pud(struct mm_struct *mm, unsigned long address,
  2904. pud_t *pud, int write)
  2905. {
  2906. struct page *page;
  2907. page = pte_page(*(pte_t *)pud);
  2908. if (page)
  2909. page += ((address & ~PUD_MASK) >> PAGE_SHIFT);
  2910. return page;
  2911. }
  2912. #else /* !CONFIG_ARCH_WANT_GENERAL_HUGETLB */
  2913. /* Can be overriden by architectures */
  2914. __attribute__((weak)) struct page *
  2915. follow_huge_pud(struct mm_struct *mm, unsigned long address,
  2916. pud_t *pud, int write)
  2917. {
  2918. BUG();
  2919. return NULL;
  2920. }
  2921. #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */
  2922. #ifdef CONFIG_MEMORY_FAILURE
  2923. /* Should be called in hugetlb_lock */
  2924. static int is_hugepage_on_freelist(struct page *hpage)
  2925. {
  2926. struct page *page;
  2927. struct page *tmp;
  2928. struct hstate *h = page_hstate(hpage);
  2929. int nid = page_to_nid(hpage);
  2930. list_for_each_entry_safe(page, tmp, &h->hugepage_freelists[nid], lru)
  2931. if (page == hpage)
  2932. return 1;
  2933. return 0;
  2934. }
  2935. /*
  2936. * This function is called from memory failure code.
  2937. * Assume the caller holds page lock of the head page.
  2938. */
  2939. int dequeue_hwpoisoned_huge_page(struct page *hpage)
  2940. {
  2941. struct hstate *h = page_hstate(hpage);
  2942. int nid = page_to_nid(hpage);
  2943. int ret = -EBUSY;
  2944. spin_lock(&hugetlb_lock);
  2945. if (is_hugepage_on_freelist(hpage)) {
  2946. /*
  2947. * Hwpoisoned hugepage isn't linked to activelist or freelist,
  2948. * but dangling hpage->lru can trigger list-debug warnings
  2949. * (this happens when we call unpoison_memory() on it),
  2950. * so let it point to itself with list_del_init().
  2951. */
  2952. list_del_init(&hpage->lru);
  2953. set_page_refcounted(hpage);
  2954. h->free_huge_pages--;
  2955. h->free_huge_pages_node[nid]--;
  2956. ret = 0;
  2957. }
  2958. spin_unlock(&hugetlb_lock);
  2959. return ret;
  2960. }
  2961. #endif
  2962. bool isolate_huge_page(struct page *page, struct list_head *list)
  2963. {
  2964. VM_BUG_ON(!PageHead(page));
  2965. if (!get_page_unless_zero(page))
  2966. return false;
  2967. spin_lock(&hugetlb_lock);
  2968. list_move_tail(&page->lru, list);
  2969. spin_unlock(&hugetlb_lock);
  2970. return true;
  2971. }
  2972. void putback_active_hugepage(struct page *page)
  2973. {
  2974. VM_BUG_ON(!PageHead(page));
  2975. spin_lock(&hugetlb_lock);
  2976. list_move_tail(&page->lru, &(page_hstate(page))->hugepage_activelist);
  2977. spin_unlock(&hugetlb_lock);
  2978. put_page(page);
  2979. }