hugetlb.c 98 KB

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