hugetlb.c 129 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794
  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/mm.h>
  8. #include <linux/seq_file.h>
  9. #include <linux/sysctl.h>
  10. #include <linux/highmem.h>
  11. #include <linux/mmu_notifier.h>
  12. #include <linux/nodemask.h>
  13. #include <linux/pagemap.h>
  14. #include <linux/mempolicy.h>
  15. #include <linux/compiler.h>
  16. #include <linux/cpuset.h>
  17. #include <linux/mutex.h>
  18. #include <linux/bootmem.h>
  19. #include <linux/sysfs.h>
  20. #include <linux/slab.h>
  21. #include <linux/sched/signal.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 <linux/userfaultfd_k.h>
  35. #include "internal.h"
  36. int 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. /*
  41. * Minimum page order among possible hugepage sizes, set to a proper value
  42. * at boot time.
  43. */
  44. static unsigned int minimum_order __read_mostly = UINT_MAX;
  45. __initdata LIST_HEAD(huge_boot_pages);
  46. /* for command line parsing */
  47. static struct hstate * __initdata parsed_hstate;
  48. static unsigned long __initdata default_hstate_max_huge_pages;
  49. static unsigned long __initdata default_hstate_size;
  50. static bool __initdata parsed_valid_hugepagesz = true;
  51. /*
  52. * Protects updates to hugepage_freelists, hugepage_activelist, nr_huge_pages,
  53. * free_huge_pages, and surplus_huge_pages.
  54. */
  55. DEFINE_SPINLOCK(hugetlb_lock);
  56. /*
  57. * Serializes faults on the same logical page. This is used to
  58. * prevent spurious OOMs when the hugepage pool is fully utilized.
  59. */
  60. static int num_fault_mutexes;
  61. struct mutex *hugetlb_fault_mutex_table ____cacheline_aligned_in_smp;
  62. /* Forward declaration */
  63. static int hugetlb_acct_memory(struct hstate *h, long delta);
  64. static inline void unlock_or_release_subpool(struct hugepage_subpool *spool)
  65. {
  66. bool free = (spool->count == 0) && (spool->used_hpages == 0);
  67. spin_unlock(&spool->lock);
  68. /* If no pages are used, and no other handles to the subpool
  69. * remain, give up any reservations mased on minimum size and
  70. * free the subpool */
  71. if (free) {
  72. if (spool->min_hpages != -1)
  73. hugetlb_acct_memory(spool->hstate,
  74. -spool->min_hpages);
  75. kfree(spool);
  76. }
  77. }
  78. struct hugepage_subpool *hugepage_new_subpool(struct hstate *h, long max_hpages,
  79. long min_hpages)
  80. {
  81. struct hugepage_subpool *spool;
  82. spool = kzalloc(sizeof(*spool), GFP_KERNEL);
  83. if (!spool)
  84. return NULL;
  85. spin_lock_init(&spool->lock);
  86. spool->count = 1;
  87. spool->max_hpages = max_hpages;
  88. spool->hstate = h;
  89. spool->min_hpages = min_hpages;
  90. if (min_hpages != -1 && hugetlb_acct_memory(h, min_hpages)) {
  91. kfree(spool);
  92. return NULL;
  93. }
  94. spool->rsv_hpages = min_hpages;
  95. return spool;
  96. }
  97. void hugepage_put_subpool(struct hugepage_subpool *spool)
  98. {
  99. spin_lock(&spool->lock);
  100. BUG_ON(!spool->count);
  101. spool->count--;
  102. unlock_or_release_subpool(spool);
  103. }
  104. /*
  105. * Subpool accounting for allocating and reserving pages.
  106. * Return -ENOMEM if there are not enough resources to satisfy the
  107. * the request. Otherwise, return the number of pages by which the
  108. * global pools must be adjusted (upward). The returned value may
  109. * only be different than the passed value (delta) in the case where
  110. * a subpool minimum size must be manitained.
  111. */
  112. static long hugepage_subpool_get_pages(struct hugepage_subpool *spool,
  113. long delta)
  114. {
  115. long ret = delta;
  116. if (!spool)
  117. return ret;
  118. spin_lock(&spool->lock);
  119. if (spool->max_hpages != -1) { /* maximum size accounting */
  120. if ((spool->used_hpages + delta) <= spool->max_hpages)
  121. spool->used_hpages += delta;
  122. else {
  123. ret = -ENOMEM;
  124. goto unlock_ret;
  125. }
  126. }
  127. /* minimum size accounting */
  128. if (spool->min_hpages != -1 && spool->rsv_hpages) {
  129. if (delta > spool->rsv_hpages) {
  130. /*
  131. * Asking for more reserves than those already taken on
  132. * behalf of subpool. Return difference.
  133. */
  134. ret = delta - spool->rsv_hpages;
  135. spool->rsv_hpages = 0;
  136. } else {
  137. ret = 0; /* reserves already accounted for */
  138. spool->rsv_hpages -= delta;
  139. }
  140. }
  141. unlock_ret:
  142. spin_unlock(&spool->lock);
  143. return ret;
  144. }
  145. /*
  146. * Subpool accounting for freeing and unreserving pages.
  147. * Return the number of global page reservations that must be dropped.
  148. * The return value may only be different than the passed value (delta)
  149. * in the case where a subpool minimum size must be maintained.
  150. */
  151. static long hugepage_subpool_put_pages(struct hugepage_subpool *spool,
  152. long delta)
  153. {
  154. long ret = delta;
  155. if (!spool)
  156. return delta;
  157. spin_lock(&spool->lock);
  158. if (spool->max_hpages != -1) /* maximum size accounting */
  159. spool->used_hpages -= delta;
  160. /* minimum size accounting */
  161. if (spool->min_hpages != -1 && spool->used_hpages < spool->min_hpages) {
  162. if (spool->rsv_hpages + delta <= spool->min_hpages)
  163. ret = 0;
  164. else
  165. ret = spool->rsv_hpages + delta - spool->min_hpages;
  166. spool->rsv_hpages += delta;
  167. if (spool->rsv_hpages > spool->min_hpages)
  168. spool->rsv_hpages = spool->min_hpages;
  169. }
  170. /*
  171. * If hugetlbfs_put_super couldn't free spool due to an outstanding
  172. * quota reference, free it now.
  173. */
  174. unlock_or_release_subpool(spool);
  175. return ret;
  176. }
  177. static inline struct hugepage_subpool *subpool_inode(struct inode *inode)
  178. {
  179. return HUGETLBFS_SB(inode->i_sb)->spool;
  180. }
  181. static inline struct hugepage_subpool *subpool_vma(struct vm_area_struct *vma)
  182. {
  183. return subpool_inode(file_inode(vma->vm_file));
  184. }
  185. /*
  186. * Region tracking -- allows tracking of reservations and instantiated pages
  187. * across the pages in a mapping.
  188. *
  189. * The region data structures are embedded into a resv_map and protected
  190. * by a resv_map's lock. The set of regions within the resv_map represent
  191. * reservations for huge pages, or huge pages that have already been
  192. * instantiated within the map. The from and to elements are huge page
  193. * indicies into the associated mapping. from indicates the starting index
  194. * of the region. to represents the first index past the end of the region.
  195. *
  196. * For example, a file region structure with from == 0 and to == 4 represents
  197. * four huge pages in a mapping. It is important to note that the to element
  198. * represents the first element past the end of the region. This is used in
  199. * arithmetic as 4(to) - 0(from) = 4 huge pages in the region.
  200. *
  201. * Interval notation of the form [from, to) will be used to indicate that
  202. * the endpoint from is inclusive and to is exclusive.
  203. */
  204. struct file_region {
  205. struct list_head link;
  206. long from;
  207. long to;
  208. };
  209. /*
  210. * Add the huge page range represented by [f, t) to the reserve
  211. * map. In the normal case, existing regions will be expanded
  212. * to accommodate the specified range. Sufficient regions should
  213. * exist for expansion due to the previous call to region_chg
  214. * with the same range. However, it is possible that region_del
  215. * could have been called after region_chg and modifed the map
  216. * in such a way that no region exists to be expanded. In this
  217. * case, pull a region descriptor from the cache associated with
  218. * the map and use that for the new range.
  219. *
  220. * Return the number of new huge pages added to the map. This
  221. * number is greater than or equal to zero.
  222. */
  223. static long region_add(struct resv_map *resv, long f, long t)
  224. {
  225. struct list_head *head = &resv->regions;
  226. struct file_region *rg, *nrg, *trg;
  227. long add = 0;
  228. spin_lock(&resv->lock);
  229. /* Locate the region we are either in or before. */
  230. list_for_each_entry(rg, head, link)
  231. if (f <= rg->to)
  232. break;
  233. /*
  234. * If no region exists which can be expanded to include the
  235. * specified range, the list must have been modified by an
  236. * interleving call to region_del(). Pull a region descriptor
  237. * from the cache and use it for this range.
  238. */
  239. if (&rg->link == head || t < rg->from) {
  240. VM_BUG_ON(resv->region_cache_count <= 0);
  241. resv->region_cache_count--;
  242. nrg = list_first_entry(&resv->region_cache, struct file_region,
  243. link);
  244. list_del(&nrg->link);
  245. nrg->from = f;
  246. nrg->to = t;
  247. list_add(&nrg->link, rg->link.prev);
  248. add += t - f;
  249. goto out_locked;
  250. }
  251. /* Round our left edge to the current segment if it encloses us. */
  252. if (f > rg->from)
  253. f = rg->from;
  254. /* Check for and consume any regions we now overlap with. */
  255. nrg = rg;
  256. list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
  257. if (&rg->link == head)
  258. break;
  259. if (rg->from > t)
  260. break;
  261. /* If this area reaches higher then extend our area to
  262. * include it completely. If this is not the first area
  263. * which we intend to reuse, free it. */
  264. if (rg->to > t)
  265. t = rg->to;
  266. if (rg != nrg) {
  267. /* Decrement return value by the deleted range.
  268. * Another range will span this area so that by
  269. * end of routine add will be >= zero
  270. */
  271. add -= (rg->to - rg->from);
  272. list_del(&rg->link);
  273. kfree(rg);
  274. }
  275. }
  276. add += (nrg->from - f); /* Added to beginning of region */
  277. nrg->from = f;
  278. add += t - nrg->to; /* Added to end of region */
  279. nrg->to = t;
  280. out_locked:
  281. resv->adds_in_progress--;
  282. spin_unlock(&resv->lock);
  283. VM_BUG_ON(add < 0);
  284. return add;
  285. }
  286. /*
  287. * Examine the existing reserve map and determine how many
  288. * huge pages in the specified range [f, t) are NOT currently
  289. * represented. This routine is called before a subsequent
  290. * call to region_add that will actually modify the reserve
  291. * map to add the specified range [f, t). region_chg does
  292. * not change the number of huge pages represented by the
  293. * map. However, if the existing regions in the map can not
  294. * be expanded to represent the new range, a new file_region
  295. * structure is added to the map as a placeholder. This is
  296. * so that the subsequent region_add call will have all the
  297. * regions it needs and will not fail.
  298. *
  299. * Upon entry, region_chg will also examine the cache of region descriptors
  300. * associated with the map. If there are not enough descriptors cached, one
  301. * will be allocated for the in progress add operation.
  302. *
  303. * Returns the number of huge pages that need to be added to the existing
  304. * reservation map for the range [f, t). This number is greater or equal to
  305. * zero. -ENOMEM is returned if a new file_region structure or cache entry
  306. * is needed and can not be allocated.
  307. */
  308. static long region_chg(struct resv_map *resv, long f, long t)
  309. {
  310. struct list_head *head = &resv->regions;
  311. struct file_region *rg, *nrg = NULL;
  312. long chg = 0;
  313. retry:
  314. spin_lock(&resv->lock);
  315. retry_locked:
  316. resv->adds_in_progress++;
  317. /*
  318. * Check for sufficient descriptors in the cache to accommodate
  319. * the number of in progress add operations.
  320. */
  321. if (resv->adds_in_progress > resv->region_cache_count) {
  322. struct file_region *trg;
  323. VM_BUG_ON(resv->adds_in_progress - resv->region_cache_count > 1);
  324. /* Must drop lock to allocate a new descriptor. */
  325. resv->adds_in_progress--;
  326. spin_unlock(&resv->lock);
  327. trg = kmalloc(sizeof(*trg), GFP_KERNEL);
  328. if (!trg) {
  329. kfree(nrg);
  330. return -ENOMEM;
  331. }
  332. spin_lock(&resv->lock);
  333. list_add(&trg->link, &resv->region_cache);
  334. resv->region_cache_count++;
  335. goto retry_locked;
  336. }
  337. /* Locate the region we are before or in. */
  338. list_for_each_entry(rg, head, link)
  339. if (f <= rg->to)
  340. break;
  341. /* If we are below the current region then a new region is required.
  342. * Subtle, allocate a new region at the position but make it zero
  343. * size such that we can guarantee to record the reservation. */
  344. if (&rg->link == head || t < rg->from) {
  345. if (!nrg) {
  346. resv->adds_in_progress--;
  347. spin_unlock(&resv->lock);
  348. nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
  349. if (!nrg)
  350. return -ENOMEM;
  351. nrg->from = f;
  352. nrg->to = f;
  353. INIT_LIST_HEAD(&nrg->link);
  354. goto retry;
  355. }
  356. list_add(&nrg->link, rg->link.prev);
  357. chg = t - f;
  358. goto out_nrg;
  359. }
  360. /* Round our left edge to the current segment if it encloses us. */
  361. if (f > rg->from)
  362. f = rg->from;
  363. chg = t - f;
  364. /* Check for and consume any regions we now overlap with. */
  365. list_for_each_entry(rg, rg->link.prev, link) {
  366. if (&rg->link == head)
  367. break;
  368. if (rg->from > t)
  369. goto out;
  370. /* We overlap with this area, if it extends further than
  371. * us then we must extend ourselves. Account for its
  372. * existing reservation. */
  373. if (rg->to > t) {
  374. chg += rg->to - t;
  375. t = rg->to;
  376. }
  377. chg -= rg->to - rg->from;
  378. }
  379. out:
  380. spin_unlock(&resv->lock);
  381. /* We already know we raced and no longer need the new region */
  382. kfree(nrg);
  383. return chg;
  384. out_nrg:
  385. spin_unlock(&resv->lock);
  386. return chg;
  387. }
  388. /*
  389. * Abort the in progress add operation. The adds_in_progress field
  390. * of the resv_map keeps track of the operations in progress between
  391. * calls to region_chg and region_add. Operations are sometimes
  392. * aborted after the call to region_chg. In such cases, region_abort
  393. * is called to decrement the adds_in_progress counter.
  394. *
  395. * NOTE: The range arguments [f, t) are not needed or used in this
  396. * routine. They are kept to make reading the calling code easier as
  397. * arguments will match the associated region_chg call.
  398. */
  399. static void region_abort(struct resv_map *resv, long f, long t)
  400. {
  401. spin_lock(&resv->lock);
  402. VM_BUG_ON(!resv->region_cache_count);
  403. resv->adds_in_progress--;
  404. spin_unlock(&resv->lock);
  405. }
  406. /*
  407. * Delete the specified range [f, t) from the reserve map. If the
  408. * t parameter is LONG_MAX, this indicates that ALL regions after f
  409. * should be deleted. Locate the regions which intersect [f, t)
  410. * and either trim, delete or split the existing regions.
  411. *
  412. * Returns the number of huge pages deleted from the reserve map.
  413. * In the normal case, the return value is zero or more. In the
  414. * case where a region must be split, a new region descriptor must
  415. * be allocated. If the allocation fails, -ENOMEM will be returned.
  416. * NOTE: If the parameter t == LONG_MAX, then we will never split
  417. * a region and possibly return -ENOMEM. Callers specifying
  418. * t == LONG_MAX do not need to check for -ENOMEM error.
  419. */
  420. static long region_del(struct resv_map *resv, long f, long t)
  421. {
  422. struct list_head *head = &resv->regions;
  423. struct file_region *rg, *trg;
  424. struct file_region *nrg = NULL;
  425. long del = 0;
  426. retry:
  427. spin_lock(&resv->lock);
  428. list_for_each_entry_safe(rg, trg, head, link) {
  429. /*
  430. * Skip regions before the range to be deleted. file_region
  431. * ranges are normally of the form [from, to). However, there
  432. * may be a "placeholder" entry in the map which is of the form
  433. * (from, to) with from == to. Check for placeholder entries
  434. * at the beginning of the range to be deleted.
  435. */
  436. if (rg->to <= f && (rg->to != rg->from || rg->to != f))
  437. continue;
  438. if (rg->from >= t)
  439. break;
  440. if (f > rg->from && t < rg->to) { /* Must split region */
  441. /*
  442. * Check for an entry in the cache before dropping
  443. * lock and attempting allocation.
  444. */
  445. if (!nrg &&
  446. resv->region_cache_count > resv->adds_in_progress) {
  447. nrg = list_first_entry(&resv->region_cache,
  448. struct file_region,
  449. link);
  450. list_del(&nrg->link);
  451. resv->region_cache_count--;
  452. }
  453. if (!nrg) {
  454. spin_unlock(&resv->lock);
  455. nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
  456. if (!nrg)
  457. return -ENOMEM;
  458. goto retry;
  459. }
  460. del += t - f;
  461. /* New entry for end of split region */
  462. nrg->from = t;
  463. nrg->to = rg->to;
  464. INIT_LIST_HEAD(&nrg->link);
  465. /* Original entry is trimmed */
  466. rg->to = f;
  467. list_add(&nrg->link, &rg->link);
  468. nrg = NULL;
  469. break;
  470. }
  471. if (f <= rg->from && t >= rg->to) { /* Remove entire region */
  472. del += rg->to - rg->from;
  473. list_del(&rg->link);
  474. kfree(rg);
  475. continue;
  476. }
  477. if (f <= rg->from) { /* Trim beginning of region */
  478. del += t - rg->from;
  479. rg->from = t;
  480. } else { /* Trim end of region */
  481. del += rg->to - f;
  482. rg->to = f;
  483. }
  484. }
  485. spin_unlock(&resv->lock);
  486. kfree(nrg);
  487. return del;
  488. }
  489. /*
  490. * A rare out of memory error was encountered which prevented removal of
  491. * the reserve map region for a page. The huge page itself was free'ed
  492. * and removed from the page cache. This routine will adjust the subpool
  493. * usage count, and the global reserve count if needed. By incrementing
  494. * these counts, the reserve map entry which could not be deleted will
  495. * appear as a "reserved" entry instead of simply dangling with incorrect
  496. * counts.
  497. */
  498. void hugetlb_fix_reserve_counts(struct inode *inode)
  499. {
  500. struct hugepage_subpool *spool = subpool_inode(inode);
  501. long rsv_adjust;
  502. rsv_adjust = hugepage_subpool_get_pages(spool, 1);
  503. if (rsv_adjust) {
  504. struct hstate *h = hstate_inode(inode);
  505. hugetlb_acct_memory(h, 1);
  506. }
  507. }
  508. /*
  509. * Count and return the number of huge pages in the reserve map
  510. * that intersect with the range [f, t).
  511. */
  512. static long region_count(struct resv_map *resv, long f, long t)
  513. {
  514. struct list_head *head = &resv->regions;
  515. struct file_region *rg;
  516. long chg = 0;
  517. spin_lock(&resv->lock);
  518. /* Locate each segment we overlap with, and count that overlap. */
  519. list_for_each_entry(rg, head, link) {
  520. long seg_from;
  521. long seg_to;
  522. if (rg->to <= f)
  523. continue;
  524. if (rg->from >= t)
  525. break;
  526. seg_from = max(rg->from, f);
  527. seg_to = min(rg->to, t);
  528. chg += seg_to - seg_from;
  529. }
  530. spin_unlock(&resv->lock);
  531. return chg;
  532. }
  533. /*
  534. * Convert the address within this vma to the page offset within
  535. * the mapping, in pagecache page units; huge pages here.
  536. */
  537. static pgoff_t vma_hugecache_offset(struct hstate *h,
  538. struct vm_area_struct *vma, unsigned long address)
  539. {
  540. return ((address - vma->vm_start) >> huge_page_shift(h)) +
  541. (vma->vm_pgoff >> huge_page_order(h));
  542. }
  543. pgoff_t linear_hugepage_index(struct vm_area_struct *vma,
  544. unsigned long address)
  545. {
  546. return vma_hugecache_offset(hstate_vma(vma), vma, address);
  547. }
  548. EXPORT_SYMBOL_GPL(linear_hugepage_index);
  549. /*
  550. * Return the size of the pages allocated when backing a VMA. In the majority
  551. * cases this will be same size as used by the page table entries.
  552. */
  553. unsigned long vma_kernel_pagesize(struct vm_area_struct *vma)
  554. {
  555. struct hstate *hstate;
  556. if (!is_vm_hugetlb_page(vma))
  557. return PAGE_SIZE;
  558. hstate = hstate_vma(vma);
  559. return 1UL << huge_page_shift(hstate);
  560. }
  561. EXPORT_SYMBOL_GPL(vma_kernel_pagesize);
  562. /*
  563. * Return the page size being used by the MMU to back a VMA. In the majority
  564. * of cases, the page size used by the kernel matches the MMU size. On
  565. * architectures where it differs, an architecture-specific version of this
  566. * function is required.
  567. */
  568. #ifndef vma_mmu_pagesize
  569. unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
  570. {
  571. return vma_kernel_pagesize(vma);
  572. }
  573. #endif
  574. /*
  575. * Flags for MAP_PRIVATE reservations. These are stored in the bottom
  576. * bits of the reservation map pointer, which are always clear due to
  577. * alignment.
  578. */
  579. #define HPAGE_RESV_OWNER (1UL << 0)
  580. #define HPAGE_RESV_UNMAPPED (1UL << 1)
  581. #define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
  582. /*
  583. * These helpers are used to track how many pages are reserved for
  584. * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
  585. * is guaranteed to have their future faults succeed.
  586. *
  587. * With the exception of reset_vma_resv_huge_pages() which is called at fork(),
  588. * the reserve counters are updated with the hugetlb_lock held. It is safe
  589. * to reset the VMA at fork() time as it is not in use yet and there is no
  590. * chance of the global counters getting corrupted as a result of the values.
  591. *
  592. * The private mapping reservation is represented in a subtly different
  593. * manner to a shared mapping. A shared mapping has a region map associated
  594. * with the underlying file, this region map represents the backing file
  595. * pages which have ever had a reservation assigned which this persists even
  596. * after the page is instantiated. A private mapping has a region map
  597. * associated with the original mmap which is attached to all VMAs which
  598. * reference it, this region map represents those offsets which have consumed
  599. * reservation ie. where pages have been instantiated.
  600. */
  601. static unsigned long get_vma_private_data(struct vm_area_struct *vma)
  602. {
  603. return (unsigned long)vma->vm_private_data;
  604. }
  605. static void set_vma_private_data(struct vm_area_struct *vma,
  606. unsigned long value)
  607. {
  608. vma->vm_private_data = (void *)value;
  609. }
  610. struct resv_map *resv_map_alloc(void)
  611. {
  612. struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL);
  613. struct file_region *rg = kmalloc(sizeof(*rg), GFP_KERNEL);
  614. if (!resv_map || !rg) {
  615. kfree(resv_map);
  616. kfree(rg);
  617. return NULL;
  618. }
  619. kref_init(&resv_map->refs);
  620. spin_lock_init(&resv_map->lock);
  621. INIT_LIST_HEAD(&resv_map->regions);
  622. resv_map->adds_in_progress = 0;
  623. INIT_LIST_HEAD(&resv_map->region_cache);
  624. list_add(&rg->link, &resv_map->region_cache);
  625. resv_map->region_cache_count = 1;
  626. return resv_map;
  627. }
  628. void resv_map_release(struct kref *ref)
  629. {
  630. struct resv_map *resv_map = container_of(ref, struct resv_map, refs);
  631. struct list_head *head = &resv_map->region_cache;
  632. struct file_region *rg, *trg;
  633. /* Clear out any active regions before we release the map. */
  634. region_del(resv_map, 0, LONG_MAX);
  635. /* ... and any entries left in the cache */
  636. list_for_each_entry_safe(rg, trg, head, link) {
  637. list_del(&rg->link);
  638. kfree(rg);
  639. }
  640. VM_BUG_ON(resv_map->adds_in_progress);
  641. kfree(resv_map);
  642. }
  643. static inline struct resv_map *inode_resv_map(struct inode *inode)
  644. {
  645. return inode->i_mapping->private_data;
  646. }
  647. static struct resv_map *vma_resv_map(struct vm_area_struct *vma)
  648. {
  649. VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
  650. if (vma->vm_flags & VM_MAYSHARE) {
  651. struct address_space *mapping = vma->vm_file->f_mapping;
  652. struct inode *inode = mapping->host;
  653. return inode_resv_map(inode);
  654. } else {
  655. return (struct resv_map *)(get_vma_private_data(vma) &
  656. ~HPAGE_RESV_MASK);
  657. }
  658. }
  659. static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map)
  660. {
  661. VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
  662. VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma);
  663. set_vma_private_data(vma, (get_vma_private_data(vma) &
  664. HPAGE_RESV_MASK) | (unsigned long)map);
  665. }
  666. static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
  667. {
  668. VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
  669. VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma);
  670. set_vma_private_data(vma, get_vma_private_data(vma) | flags);
  671. }
  672. static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
  673. {
  674. VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
  675. return (get_vma_private_data(vma) & flag) != 0;
  676. }
  677. /* Reset counters to 0 and clear all HPAGE_RESV_* flags */
  678. void reset_vma_resv_huge_pages(struct vm_area_struct *vma)
  679. {
  680. VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
  681. if (!(vma->vm_flags & VM_MAYSHARE))
  682. vma->vm_private_data = (void *)0;
  683. }
  684. /* Returns true if the VMA has associated reserve pages */
  685. static bool vma_has_reserves(struct vm_area_struct *vma, long chg)
  686. {
  687. if (vma->vm_flags & VM_NORESERVE) {
  688. /*
  689. * This address is already reserved by other process(chg == 0),
  690. * so, we should decrement reserved count. Without decrementing,
  691. * reserve count remains after releasing inode, because this
  692. * allocated page will go into page cache and is regarded as
  693. * coming from reserved pool in releasing step. Currently, we
  694. * don't have any other solution to deal with this situation
  695. * properly, so add work-around here.
  696. */
  697. if (vma->vm_flags & VM_MAYSHARE && chg == 0)
  698. return true;
  699. else
  700. return false;
  701. }
  702. /* Shared mappings always use reserves */
  703. if (vma->vm_flags & VM_MAYSHARE) {
  704. /*
  705. * We know VM_NORESERVE is not set. Therefore, there SHOULD
  706. * be a region map for all pages. The only situation where
  707. * there is no region map is if a hole was punched via
  708. * fallocate. In this case, there really are no reverves to
  709. * use. This situation is indicated if chg != 0.
  710. */
  711. if (chg)
  712. return false;
  713. else
  714. return true;
  715. }
  716. /*
  717. * Only the process that called mmap() has reserves for
  718. * private mappings.
  719. */
  720. if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
  721. /*
  722. * Like the shared case above, a hole punch or truncate
  723. * could have been performed on the private mapping.
  724. * Examine the value of chg to determine if reserves
  725. * actually exist or were previously consumed.
  726. * Very Subtle - The value of chg comes from a previous
  727. * call to vma_needs_reserves(). The reserve map for
  728. * private mappings has different (opposite) semantics
  729. * than that of shared mappings. vma_needs_reserves()
  730. * has already taken this difference in semantics into
  731. * account. Therefore, the meaning of chg is the same
  732. * as in the shared case above. Code could easily be
  733. * combined, but keeping it separate draws attention to
  734. * subtle differences.
  735. */
  736. if (chg)
  737. return false;
  738. else
  739. return true;
  740. }
  741. return false;
  742. }
  743. static void enqueue_huge_page(struct hstate *h, struct page *page)
  744. {
  745. int nid = page_to_nid(page);
  746. list_move(&page->lru, &h->hugepage_freelists[nid]);
  747. h->free_huge_pages++;
  748. h->free_huge_pages_node[nid]++;
  749. }
  750. static struct page *dequeue_huge_page_node_exact(struct hstate *h, int nid)
  751. {
  752. struct page *page;
  753. list_for_each_entry(page, &h->hugepage_freelists[nid], lru)
  754. if (!is_migrate_isolate_page(page))
  755. break;
  756. /*
  757. * if 'non-isolated free hugepage' not found on the list,
  758. * the allocation fails.
  759. */
  760. if (&h->hugepage_freelists[nid] == &page->lru)
  761. return NULL;
  762. list_move(&page->lru, &h->hugepage_activelist);
  763. set_page_refcounted(page);
  764. h->free_huge_pages--;
  765. h->free_huge_pages_node[nid]--;
  766. return page;
  767. }
  768. static struct page *dequeue_huge_page_node(struct hstate *h, int nid)
  769. {
  770. struct page *page;
  771. int node;
  772. if (nid != NUMA_NO_NODE)
  773. return dequeue_huge_page_node_exact(h, nid);
  774. for_each_online_node(node) {
  775. page = dequeue_huge_page_node_exact(h, node);
  776. if (page)
  777. return page;
  778. }
  779. return NULL;
  780. }
  781. /* Movability of hugepages depends on migration support. */
  782. static inline gfp_t htlb_alloc_mask(struct hstate *h)
  783. {
  784. if (hugepages_treat_as_movable || hugepage_migration_supported(h))
  785. return GFP_HIGHUSER_MOVABLE;
  786. else
  787. return GFP_HIGHUSER;
  788. }
  789. static struct page *dequeue_huge_page_vma(struct hstate *h,
  790. struct vm_area_struct *vma,
  791. unsigned long address, int avoid_reserve,
  792. long chg)
  793. {
  794. struct page *page = NULL;
  795. struct mempolicy *mpol;
  796. nodemask_t *nodemask;
  797. struct zonelist *zonelist;
  798. struct zone *zone;
  799. struct zoneref *z;
  800. unsigned int cpuset_mems_cookie;
  801. /*
  802. * A child process with MAP_PRIVATE mappings created by their parent
  803. * have no page reserves. This check ensures that reservations are
  804. * not "stolen". The child may still get SIGKILLed
  805. */
  806. if (!vma_has_reserves(vma, chg) &&
  807. h->free_huge_pages - h->resv_huge_pages == 0)
  808. goto err;
  809. /* If reserves cannot be used, ensure enough pages are in the pool */
  810. if (avoid_reserve && h->free_huge_pages - h->resv_huge_pages == 0)
  811. goto err;
  812. retry_cpuset:
  813. cpuset_mems_cookie = read_mems_allowed_begin();
  814. zonelist = huge_zonelist(vma, address,
  815. htlb_alloc_mask(h), &mpol, &nodemask);
  816. for_each_zone_zonelist_nodemask(zone, z, zonelist,
  817. MAX_NR_ZONES - 1, nodemask) {
  818. if (cpuset_zone_allowed(zone, htlb_alloc_mask(h))) {
  819. page = dequeue_huge_page_node(h, zone_to_nid(zone));
  820. if (page) {
  821. if (avoid_reserve)
  822. break;
  823. if (!vma_has_reserves(vma, chg))
  824. break;
  825. SetPagePrivate(page);
  826. h->resv_huge_pages--;
  827. break;
  828. }
  829. }
  830. }
  831. mpol_cond_put(mpol);
  832. if (unlikely(!page && read_mems_allowed_retry(cpuset_mems_cookie)))
  833. goto retry_cpuset;
  834. return page;
  835. err:
  836. return NULL;
  837. }
  838. /*
  839. * common helper functions for hstate_next_node_to_{alloc|free}.
  840. * We may have allocated or freed a huge page based on a different
  841. * nodes_allowed previously, so h->next_node_to_{alloc|free} might
  842. * be outside of *nodes_allowed. Ensure that we use an allowed
  843. * node for alloc or free.
  844. */
  845. static int next_node_allowed(int nid, nodemask_t *nodes_allowed)
  846. {
  847. nid = next_node_in(nid, *nodes_allowed);
  848. VM_BUG_ON(nid >= MAX_NUMNODES);
  849. return nid;
  850. }
  851. static int get_valid_node_allowed(int nid, nodemask_t *nodes_allowed)
  852. {
  853. if (!node_isset(nid, *nodes_allowed))
  854. nid = next_node_allowed(nid, nodes_allowed);
  855. return nid;
  856. }
  857. /*
  858. * returns the previously saved node ["this node"] from which to
  859. * allocate a persistent huge page for the pool and advance the
  860. * next node from which to allocate, handling wrap at end of node
  861. * mask.
  862. */
  863. static int hstate_next_node_to_alloc(struct hstate *h,
  864. nodemask_t *nodes_allowed)
  865. {
  866. int nid;
  867. VM_BUG_ON(!nodes_allowed);
  868. nid = get_valid_node_allowed(h->next_nid_to_alloc, nodes_allowed);
  869. h->next_nid_to_alloc = next_node_allowed(nid, nodes_allowed);
  870. return nid;
  871. }
  872. /*
  873. * helper for free_pool_huge_page() - return the previously saved
  874. * node ["this node"] from which to free a huge page. Advance the
  875. * next node id whether or not we find a free huge page to free so
  876. * that the next attempt to free addresses the next node.
  877. */
  878. static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed)
  879. {
  880. int nid;
  881. VM_BUG_ON(!nodes_allowed);
  882. nid = get_valid_node_allowed(h->next_nid_to_free, nodes_allowed);
  883. h->next_nid_to_free = next_node_allowed(nid, nodes_allowed);
  884. return nid;
  885. }
  886. #define for_each_node_mask_to_alloc(hs, nr_nodes, node, mask) \
  887. for (nr_nodes = nodes_weight(*mask); \
  888. nr_nodes > 0 && \
  889. ((node = hstate_next_node_to_alloc(hs, mask)) || 1); \
  890. nr_nodes--)
  891. #define for_each_node_mask_to_free(hs, nr_nodes, node, mask) \
  892. for (nr_nodes = nodes_weight(*mask); \
  893. nr_nodes > 0 && \
  894. ((node = hstate_next_node_to_free(hs, mask)) || 1); \
  895. nr_nodes--)
  896. #if defined(CONFIG_ARCH_HAS_GIGANTIC_PAGE) && \
  897. ((defined(CONFIG_MEMORY_ISOLATION) && defined(CONFIG_COMPACTION)) || \
  898. defined(CONFIG_CMA))
  899. static void destroy_compound_gigantic_page(struct page *page,
  900. unsigned int order)
  901. {
  902. int i;
  903. int nr_pages = 1 << order;
  904. struct page *p = page + 1;
  905. atomic_set(compound_mapcount_ptr(page), 0);
  906. for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) {
  907. clear_compound_head(p);
  908. set_page_refcounted(p);
  909. }
  910. set_compound_order(page, 0);
  911. __ClearPageHead(page);
  912. }
  913. static void free_gigantic_page(struct page *page, unsigned int order)
  914. {
  915. free_contig_range(page_to_pfn(page), 1 << order);
  916. }
  917. static int __alloc_gigantic_page(unsigned long start_pfn,
  918. unsigned long nr_pages)
  919. {
  920. unsigned long end_pfn = start_pfn + nr_pages;
  921. return alloc_contig_range(start_pfn, end_pfn, MIGRATE_MOVABLE,
  922. GFP_KERNEL);
  923. }
  924. static bool pfn_range_valid_gigantic(struct zone *z,
  925. unsigned long start_pfn, unsigned long nr_pages)
  926. {
  927. unsigned long i, end_pfn = start_pfn + nr_pages;
  928. struct page *page;
  929. for (i = start_pfn; i < end_pfn; i++) {
  930. if (!pfn_valid(i))
  931. return false;
  932. page = pfn_to_page(i);
  933. if (page_zone(page) != z)
  934. return false;
  935. if (PageReserved(page))
  936. return false;
  937. if (page_count(page) > 0)
  938. return false;
  939. if (PageHuge(page))
  940. return false;
  941. }
  942. return true;
  943. }
  944. static bool zone_spans_last_pfn(const struct zone *zone,
  945. unsigned long start_pfn, unsigned long nr_pages)
  946. {
  947. unsigned long last_pfn = start_pfn + nr_pages - 1;
  948. return zone_spans_pfn(zone, last_pfn);
  949. }
  950. static struct page *alloc_gigantic_page(int nid, unsigned int order)
  951. {
  952. unsigned long nr_pages = 1 << order;
  953. unsigned long ret, pfn, flags;
  954. struct zone *z;
  955. z = NODE_DATA(nid)->node_zones;
  956. for (; z - NODE_DATA(nid)->node_zones < MAX_NR_ZONES; z++) {
  957. spin_lock_irqsave(&z->lock, flags);
  958. pfn = ALIGN(z->zone_start_pfn, nr_pages);
  959. while (zone_spans_last_pfn(z, pfn, nr_pages)) {
  960. if (pfn_range_valid_gigantic(z, pfn, nr_pages)) {
  961. /*
  962. * We release the zone lock here because
  963. * alloc_contig_range() will also lock the zone
  964. * at some point. If there's an allocation
  965. * spinning on this lock, it may win the race
  966. * and cause alloc_contig_range() to fail...
  967. */
  968. spin_unlock_irqrestore(&z->lock, flags);
  969. ret = __alloc_gigantic_page(pfn, nr_pages);
  970. if (!ret)
  971. return pfn_to_page(pfn);
  972. spin_lock_irqsave(&z->lock, flags);
  973. }
  974. pfn += nr_pages;
  975. }
  976. spin_unlock_irqrestore(&z->lock, flags);
  977. }
  978. return NULL;
  979. }
  980. static void prep_new_huge_page(struct hstate *h, struct page *page, int nid);
  981. static void prep_compound_gigantic_page(struct page *page, unsigned int order);
  982. static struct page *alloc_fresh_gigantic_page_node(struct hstate *h, int nid)
  983. {
  984. struct page *page;
  985. page = alloc_gigantic_page(nid, huge_page_order(h));
  986. if (page) {
  987. prep_compound_gigantic_page(page, huge_page_order(h));
  988. prep_new_huge_page(h, page, nid);
  989. }
  990. return page;
  991. }
  992. static int alloc_fresh_gigantic_page(struct hstate *h,
  993. nodemask_t *nodes_allowed)
  994. {
  995. struct page *page = NULL;
  996. int nr_nodes, node;
  997. for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) {
  998. page = alloc_fresh_gigantic_page_node(h, node);
  999. if (page)
  1000. return 1;
  1001. }
  1002. return 0;
  1003. }
  1004. static inline bool gigantic_page_supported(void) { return true; }
  1005. #else
  1006. static inline bool gigantic_page_supported(void) { return false; }
  1007. static inline void free_gigantic_page(struct page *page, unsigned int order) { }
  1008. static inline void destroy_compound_gigantic_page(struct page *page,
  1009. unsigned int order) { }
  1010. static inline int alloc_fresh_gigantic_page(struct hstate *h,
  1011. nodemask_t *nodes_allowed) { return 0; }
  1012. #endif
  1013. static void update_and_free_page(struct hstate *h, struct page *page)
  1014. {
  1015. int i;
  1016. if (hstate_is_gigantic(h) && !gigantic_page_supported())
  1017. return;
  1018. h->nr_huge_pages--;
  1019. h->nr_huge_pages_node[page_to_nid(page)]--;
  1020. for (i = 0; i < pages_per_huge_page(h); i++) {
  1021. page[i].flags &= ~(1 << PG_locked | 1 << PG_error |
  1022. 1 << PG_referenced | 1 << PG_dirty |
  1023. 1 << PG_active | 1 << PG_private |
  1024. 1 << PG_writeback);
  1025. }
  1026. VM_BUG_ON_PAGE(hugetlb_cgroup_from_page(page), page);
  1027. set_compound_page_dtor(page, NULL_COMPOUND_DTOR);
  1028. set_page_refcounted(page);
  1029. if (hstate_is_gigantic(h)) {
  1030. destroy_compound_gigantic_page(page, huge_page_order(h));
  1031. free_gigantic_page(page, huge_page_order(h));
  1032. } else {
  1033. __free_pages(page, huge_page_order(h));
  1034. }
  1035. }
  1036. struct hstate *size_to_hstate(unsigned long size)
  1037. {
  1038. struct hstate *h;
  1039. for_each_hstate(h) {
  1040. if (huge_page_size(h) == size)
  1041. return h;
  1042. }
  1043. return NULL;
  1044. }
  1045. /*
  1046. * Test to determine whether the hugepage is "active/in-use" (i.e. being linked
  1047. * to hstate->hugepage_activelist.)
  1048. *
  1049. * This function can be called for tail pages, but never returns true for them.
  1050. */
  1051. bool page_huge_active(struct page *page)
  1052. {
  1053. VM_BUG_ON_PAGE(!PageHuge(page), page);
  1054. return PageHead(page) && PagePrivate(&page[1]);
  1055. }
  1056. /* never called for tail page */
  1057. static void set_page_huge_active(struct page *page)
  1058. {
  1059. VM_BUG_ON_PAGE(!PageHeadHuge(page), page);
  1060. SetPagePrivate(&page[1]);
  1061. }
  1062. static void clear_page_huge_active(struct page *page)
  1063. {
  1064. VM_BUG_ON_PAGE(!PageHeadHuge(page), page);
  1065. ClearPagePrivate(&page[1]);
  1066. }
  1067. void free_huge_page(struct page *page)
  1068. {
  1069. /*
  1070. * Can't pass hstate in here because it is called from the
  1071. * compound page destructor.
  1072. */
  1073. struct hstate *h = page_hstate(page);
  1074. int nid = page_to_nid(page);
  1075. struct hugepage_subpool *spool =
  1076. (struct hugepage_subpool *)page_private(page);
  1077. bool restore_reserve;
  1078. set_page_private(page, 0);
  1079. page->mapping = NULL;
  1080. VM_BUG_ON_PAGE(page_count(page), page);
  1081. VM_BUG_ON_PAGE(page_mapcount(page), page);
  1082. restore_reserve = PagePrivate(page);
  1083. ClearPagePrivate(page);
  1084. /*
  1085. * A return code of zero implies that the subpool will be under its
  1086. * minimum size if the reservation is not restored after page is free.
  1087. * Therefore, force restore_reserve operation.
  1088. */
  1089. if (hugepage_subpool_put_pages(spool, 1) == 0)
  1090. restore_reserve = true;
  1091. spin_lock(&hugetlb_lock);
  1092. clear_page_huge_active(page);
  1093. hugetlb_cgroup_uncharge_page(hstate_index(h),
  1094. pages_per_huge_page(h), page);
  1095. if (restore_reserve)
  1096. h->resv_huge_pages++;
  1097. if (h->surplus_huge_pages_node[nid]) {
  1098. /* remove the page from active list */
  1099. list_del(&page->lru);
  1100. update_and_free_page(h, page);
  1101. h->surplus_huge_pages--;
  1102. h->surplus_huge_pages_node[nid]--;
  1103. } else {
  1104. arch_clear_hugepage_flags(page);
  1105. enqueue_huge_page(h, page);
  1106. }
  1107. spin_unlock(&hugetlb_lock);
  1108. }
  1109. static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)
  1110. {
  1111. INIT_LIST_HEAD(&page->lru);
  1112. set_compound_page_dtor(page, HUGETLB_PAGE_DTOR);
  1113. spin_lock(&hugetlb_lock);
  1114. set_hugetlb_cgroup(page, NULL);
  1115. h->nr_huge_pages++;
  1116. h->nr_huge_pages_node[nid]++;
  1117. spin_unlock(&hugetlb_lock);
  1118. put_page(page); /* free it into the hugepage allocator */
  1119. }
  1120. static void prep_compound_gigantic_page(struct page *page, unsigned int order)
  1121. {
  1122. int i;
  1123. int nr_pages = 1 << order;
  1124. struct page *p = page + 1;
  1125. /* we rely on prep_new_huge_page to set the destructor */
  1126. set_compound_order(page, order);
  1127. __ClearPageReserved(page);
  1128. __SetPageHead(page);
  1129. for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) {
  1130. /*
  1131. * For gigantic hugepages allocated through bootmem at
  1132. * boot, it's safer to be consistent with the not-gigantic
  1133. * hugepages and clear the PG_reserved bit from all tail pages
  1134. * too. Otherwse drivers using get_user_pages() to access tail
  1135. * pages may get the reference counting wrong if they see
  1136. * PG_reserved set on a tail page (despite the head page not
  1137. * having PG_reserved set). Enforcing this consistency between
  1138. * head and tail pages allows drivers to optimize away a check
  1139. * on the head page when they need know if put_page() is needed
  1140. * after get_user_pages().
  1141. */
  1142. __ClearPageReserved(p);
  1143. set_page_count(p, 0);
  1144. set_compound_head(p, page);
  1145. }
  1146. atomic_set(compound_mapcount_ptr(page), -1);
  1147. }
  1148. /*
  1149. * PageHuge() only returns true for hugetlbfs pages, but not for normal or
  1150. * transparent huge pages. See the PageTransHuge() documentation for more
  1151. * details.
  1152. */
  1153. int PageHuge(struct page *page)
  1154. {
  1155. if (!PageCompound(page))
  1156. return 0;
  1157. page = compound_head(page);
  1158. return page[1].compound_dtor == HUGETLB_PAGE_DTOR;
  1159. }
  1160. EXPORT_SYMBOL_GPL(PageHuge);
  1161. /*
  1162. * PageHeadHuge() only returns true for hugetlbfs head page, but not for
  1163. * normal or transparent huge pages.
  1164. */
  1165. int PageHeadHuge(struct page *page_head)
  1166. {
  1167. if (!PageHead(page_head))
  1168. return 0;
  1169. return get_compound_page_dtor(page_head) == free_huge_page;
  1170. }
  1171. pgoff_t __basepage_index(struct page *page)
  1172. {
  1173. struct page *page_head = compound_head(page);
  1174. pgoff_t index = page_index(page_head);
  1175. unsigned long compound_idx;
  1176. if (!PageHuge(page_head))
  1177. return page_index(page);
  1178. if (compound_order(page_head) >= MAX_ORDER)
  1179. compound_idx = page_to_pfn(page) - page_to_pfn(page_head);
  1180. else
  1181. compound_idx = page - page_head;
  1182. return (index << compound_order(page_head)) + compound_idx;
  1183. }
  1184. static struct page *alloc_fresh_huge_page_node(struct hstate *h, int nid)
  1185. {
  1186. struct page *page;
  1187. page = __alloc_pages_node(nid,
  1188. htlb_alloc_mask(h)|__GFP_COMP|__GFP_THISNODE|
  1189. __GFP_REPEAT|__GFP_NOWARN,
  1190. huge_page_order(h));
  1191. if (page) {
  1192. prep_new_huge_page(h, page, nid);
  1193. }
  1194. return page;
  1195. }
  1196. static int alloc_fresh_huge_page(struct hstate *h, nodemask_t *nodes_allowed)
  1197. {
  1198. struct page *page;
  1199. int nr_nodes, node;
  1200. int ret = 0;
  1201. for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) {
  1202. page = alloc_fresh_huge_page_node(h, node);
  1203. if (page) {
  1204. ret = 1;
  1205. break;
  1206. }
  1207. }
  1208. if (ret)
  1209. count_vm_event(HTLB_BUDDY_PGALLOC);
  1210. else
  1211. count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
  1212. return ret;
  1213. }
  1214. /*
  1215. * Free huge page from pool from next node to free.
  1216. * Attempt to keep persistent huge pages more or less
  1217. * balanced over allowed nodes.
  1218. * Called with hugetlb_lock locked.
  1219. */
  1220. static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,
  1221. bool acct_surplus)
  1222. {
  1223. int nr_nodes, node;
  1224. int ret = 0;
  1225. for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) {
  1226. /*
  1227. * If we're returning unused surplus pages, only examine
  1228. * nodes with surplus pages.
  1229. */
  1230. if ((!acct_surplus || h->surplus_huge_pages_node[node]) &&
  1231. !list_empty(&h->hugepage_freelists[node])) {
  1232. struct page *page =
  1233. list_entry(h->hugepage_freelists[node].next,
  1234. struct page, lru);
  1235. list_del(&page->lru);
  1236. h->free_huge_pages--;
  1237. h->free_huge_pages_node[node]--;
  1238. if (acct_surplus) {
  1239. h->surplus_huge_pages--;
  1240. h->surplus_huge_pages_node[node]--;
  1241. }
  1242. update_and_free_page(h, page);
  1243. ret = 1;
  1244. break;
  1245. }
  1246. }
  1247. return ret;
  1248. }
  1249. /*
  1250. * Dissolve a given free hugepage into free buddy pages. This function does
  1251. * nothing for in-use (including surplus) hugepages. Returns -EBUSY if the
  1252. * number of free hugepages would be reduced below the number of reserved
  1253. * hugepages.
  1254. */
  1255. static int dissolve_free_huge_page(struct page *page)
  1256. {
  1257. int rc = 0;
  1258. spin_lock(&hugetlb_lock);
  1259. if (PageHuge(page) && !page_count(page)) {
  1260. struct page *head = compound_head(page);
  1261. struct hstate *h = page_hstate(head);
  1262. int nid = page_to_nid(head);
  1263. if (h->free_huge_pages - h->resv_huge_pages == 0) {
  1264. rc = -EBUSY;
  1265. goto out;
  1266. }
  1267. list_del(&head->lru);
  1268. h->free_huge_pages--;
  1269. h->free_huge_pages_node[nid]--;
  1270. h->max_huge_pages--;
  1271. update_and_free_page(h, head);
  1272. }
  1273. out:
  1274. spin_unlock(&hugetlb_lock);
  1275. return rc;
  1276. }
  1277. /*
  1278. * Dissolve free hugepages in a given pfn range. Used by memory hotplug to
  1279. * make specified memory blocks removable from the system.
  1280. * Note that this will dissolve a free gigantic hugepage completely, if any
  1281. * part of it lies within the given range.
  1282. * Also note that if dissolve_free_huge_page() returns with an error, all
  1283. * free hugepages that were dissolved before that error are lost.
  1284. */
  1285. int dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn)
  1286. {
  1287. unsigned long pfn;
  1288. struct page *page;
  1289. int rc = 0;
  1290. if (!hugepages_supported())
  1291. return rc;
  1292. for (pfn = start_pfn; pfn < end_pfn; pfn += 1 << minimum_order) {
  1293. page = pfn_to_page(pfn);
  1294. if (PageHuge(page) && !page_count(page)) {
  1295. rc = dissolve_free_huge_page(page);
  1296. if (rc)
  1297. break;
  1298. }
  1299. }
  1300. return rc;
  1301. }
  1302. /*
  1303. * There are 3 ways this can get called:
  1304. * 1. With vma+addr: we use the VMA's memory policy
  1305. * 2. With !vma, but nid=NUMA_NO_NODE: We try to allocate a huge
  1306. * page from any node, and let the buddy allocator itself figure
  1307. * it out.
  1308. * 3. With !vma, but nid!=NUMA_NO_NODE. We allocate a huge page
  1309. * strictly from 'nid'
  1310. */
  1311. static struct page *__hugetlb_alloc_buddy_huge_page(struct hstate *h,
  1312. struct vm_area_struct *vma, unsigned long addr, int nid)
  1313. {
  1314. int order = huge_page_order(h);
  1315. gfp_t gfp = htlb_alloc_mask(h)|__GFP_COMP|__GFP_REPEAT|__GFP_NOWARN;
  1316. unsigned int cpuset_mems_cookie;
  1317. /*
  1318. * We need a VMA to get a memory policy. If we do not
  1319. * have one, we use the 'nid' argument.
  1320. *
  1321. * The mempolicy stuff below has some non-inlined bits
  1322. * and calls ->vm_ops. That makes it hard to optimize at
  1323. * compile-time, even when NUMA is off and it does
  1324. * nothing. This helps the compiler optimize it out.
  1325. */
  1326. if (!IS_ENABLED(CONFIG_NUMA) || !vma) {
  1327. /*
  1328. * If a specific node is requested, make sure to
  1329. * get memory from there, but only when a node
  1330. * is explicitly specified.
  1331. */
  1332. if (nid != NUMA_NO_NODE)
  1333. gfp |= __GFP_THISNODE;
  1334. /*
  1335. * Make sure to call something that can handle
  1336. * nid=NUMA_NO_NODE
  1337. */
  1338. return alloc_pages_node(nid, gfp, order);
  1339. }
  1340. /*
  1341. * OK, so we have a VMA. Fetch the mempolicy and try to
  1342. * allocate a huge page with it. We will only reach this
  1343. * when CONFIG_NUMA=y.
  1344. */
  1345. do {
  1346. struct page *page;
  1347. struct mempolicy *mpol;
  1348. struct zonelist *zl;
  1349. nodemask_t *nodemask;
  1350. cpuset_mems_cookie = read_mems_allowed_begin();
  1351. zl = huge_zonelist(vma, addr, gfp, &mpol, &nodemask);
  1352. mpol_cond_put(mpol);
  1353. page = __alloc_pages_nodemask(gfp, order, zl, nodemask);
  1354. if (page)
  1355. return page;
  1356. } while (read_mems_allowed_retry(cpuset_mems_cookie));
  1357. return NULL;
  1358. }
  1359. /*
  1360. * There are two ways to allocate a huge page:
  1361. * 1. When you have a VMA and an address (like a fault)
  1362. * 2. When you have no VMA (like when setting /proc/.../nr_hugepages)
  1363. *
  1364. * 'vma' and 'addr' are only for (1). 'nid' is always NUMA_NO_NODE in
  1365. * this case which signifies that the allocation should be done with
  1366. * respect for the VMA's memory policy.
  1367. *
  1368. * For (2), we ignore 'vma' and 'addr' and use 'nid' exclusively. This
  1369. * implies that memory policies will not be taken in to account.
  1370. */
  1371. static struct page *__alloc_buddy_huge_page(struct hstate *h,
  1372. struct vm_area_struct *vma, unsigned long addr, int nid)
  1373. {
  1374. struct page *page;
  1375. unsigned int r_nid;
  1376. if (hstate_is_gigantic(h))
  1377. return NULL;
  1378. /*
  1379. * Make sure that anyone specifying 'nid' is not also specifying a VMA.
  1380. * This makes sure the caller is picking _one_ of the modes with which
  1381. * we can call this function, not both.
  1382. */
  1383. if (vma || (addr != -1)) {
  1384. VM_WARN_ON_ONCE(addr == -1);
  1385. VM_WARN_ON_ONCE(nid != NUMA_NO_NODE);
  1386. }
  1387. /*
  1388. * Assume we will successfully allocate the surplus page to
  1389. * prevent racing processes from causing the surplus to exceed
  1390. * overcommit
  1391. *
  1392. * This however introduces a different race, where a process B
  1393. * tries to grow the static hugepage pool while alloc_pages() is
  1394. * called by process A. B will only examine the per-node
  1395. * counters in determining if surplus huge pages can be
  1396. * converted to normal huge pages in adjust_pool_surplus(). A
  1397. * won't be able to increment the per-node counter, until the
  1398. * lock is dropped by B, but B doesn't drop hugetlb_lock until
  1399. * no more huge pages can be converted from surplus to normal
  1400. * state (and doesn't try to convert again). Thus, we have a
  1401. * case where a surplus huge page exists, the pool is grown, and
  1402. * the surplus huge page still exists after, even though it
  1403. * should just have been converted to a normal huge page. This
  1404. * does not leak memory, though, as the hugepage will be freed
  1405. * once it is out of use. It also does not allow the counters to
  1406. * go out of whack in adjust_pool_surplus() as we don't modify
  1407. * the node values until we've gotten the hugepage and only the
  1408. * per-node value is checked there.
  1409. */
  1410. spin_lock(&hugetlb_lock);
  1411. if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) {
  1412. spin_unlock(&hugetlb_lock);
  1413. return NULL;
  1414. } else {
  1415. h->nr_huge_pages++;
  1416. h->surplus_huge_pages++;
  1417. }
  1418. spin_unlock(&hugetlb_lock);
  1419. page = __hugetlb_alloc_buddy_huge_page(h, vma, addr, nid);
  1420. spin_lock(&hugetlb_lock);
  1421. if (page) {
  1422. INIT_LIST_HEAD(&page->lru);
  1423. r_nid = page_to_nid(page);
  1424. set_compound_page_dtor(page, HUGETLB_PAGE_DTOR);
  1425. set_hugetlb_cgroup(page, NULL);
  1426. /*
  1427. * We incremented the global counters already
  1428. */
  1429. h->nr_huge_pages_node[r_nid]++;
  1430. h->surplus_huge_pages_node[r_nid]++;
  1431. __count_vm_event(HTLB_BUDDY_PGALLOC);
  1432. } else {
  1433. h->nr_huge_pages--;
  1434. h->surplus_huge_pages--;
  1435. __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
  1436. }
  1437. spin_unlock(&hugetlb_lock);
  1438. return page;
  1439. }
  1440. /*
  1441. * Allocate a huge page from 'nid'. Note, 'nid' may be
  1442. * NUMA_NO_NODE, which means that it may be allocated
  1443. * anywhere.
  1444. */
  1445. static
  1446. struct page *__alloc_buddy_huge_page_no_mpol(struct hstate *h, int nid)
  1447. {
  1448. unsigned long addr = -1;
  1449. return __alloc_buddy_huge_page(h, NULL, addr, nid);
  1450. }
  1451. /*
  1452. * Use the VMA's mpolicy to allocate a huge page from the buddy.
  1453. */
  1454. static
  1455. struct page *__alloc_buddy_huge_page_with_mpol(struct hstate *h,
  1456. struct vm_area_struct *vma, unsigned long addr)
  1457. {
  1458. return __alloc_buddy_huge_page(h, vma, addr, NUMA_NO_NODE);
  1459. }
  1460. /*
  1461. * This allocation function is useful in the context where vma is irrelevant.
  1462. * E.g. soft-offlining uses this function because it only cares physical
  1463. * address of error page.
  1464. */
  1465. struct page *alloc_huge_page_node(struct hstate *h, int nid)
  1466. {
  1467. struct page *page = NULL;
  1468. spin_lock(&hugetlb_lock);
  1469. if (h->free_huge_pages - h->resv_huge_pages > 0)
  1470. page = dequeue_huge_page_node(h, nid);
  1471. spin_unlock(&hugetlb_lock);
  1472. if (!page)
  1473. page = __alloc_buddy_huge_page_no_mpol(h, nid);
  1474. return page;
  1475. }
  1476. /*
  1477. * Increase the hugetlb pool such that it can accommodate a reservation
  1478. * of size 'delta'.
  1479. */
  1480. static int gather_surplus_pages(struct hstate *h, int delta)
  1481. {
  1482. struct list_head surplus_list;
  1483. struct page *page, *tmp;
  1484. int ret, i;
  1485. int needed, allocated;
  1486. bool alloc_ok = true;
  1487. needed = (h->resv_huge_pages + delta) - h->free_huge_pages;
  1488. if (needed <= 0) {
  1489. h->resv_huge_pages += delta;
  1490. return 0;
  1491. }
  1492. allocated = 0;
  1493. INIT_LIST_HEAD(&surplus_list);
  1494. ret = -ENOMEM;
  1495. retry:
  1496. spin_unlock(&hugetlb_lock);
  1497. for (i = 0; i < needed; i++) {
  1498. page = __alloc_buddy_huge_page_no_mpol(h, NUMA_NO_NODE);
  1499. if (!page) {
  1500. alloc_ok = false;
  1501. break;
  1502. }
  1503. list_add(&page->lru, &surplus_list);
  1504. }
  1505. allocated += i;
  1506. /*
  1507. * After retaking hugetlb_lock, we need to recalculate 'needed'
  1508. * because either resv_huge_pages or free_huge_pages may have changed.
  1509. */
  1510. spin_lock(&hugetlb_lock);
  1511. needed = (h->resv_huge_pages + delta) -
  1512. (h->free_huge_pages + allocated);
  1513. if (needed > 0) {
  1514. if (alloc_ok)
  1515. goto retry;
  1516. /*
  1517. * We were not able to allocate enough pages to
  1518. * satisfy the entire reservation so we free what
  1519. * we've allocated so far.
  1520. */
  1521. goto free;
  1522. }
  1523. /*
  1524. * The surplus_list now contains _at_least_ the number of extra pages
  1525. * needed to accommodate the reservation. Add the appropriate number
  1526. * of pages to the hugetlb pool and free the extras back to the buddy
  1527. * allocator. Commit the entire reservation here to prevent another
  1528. * process from stealing the pages as they are added to the pool but
  1529. * before they are reserved.
  1530. */
  1531. needed += allocated;
  1532. h->resv_huge_pages += delta;
  1533. ret = 0;
  1534. /* Free the needed pages to the hugetlb pool */
  1535. list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
  1536. if ((--needed) < 0)
  1537. break;
  1538. /*
  1539. * This page is now managed by the hugetlb allocator and has
  1540. * no users -- drop the buddy allocator's reference.
  1541. */
  1542. put_page_testzero(page);
  1543. VM_BUG_ON_PAGE(page_count(page), page);
  1544. enqueue_huge_page(h, page);
  1545. }
  1546. free:
  1547. spin_unlock(&hugetlb_lock);
  1548. /* Free unnecessary surplus pages to the buddy allocator */
  1549. list_for_each_entry_safe(page, tmp, &surplus_list, lru)
  1550. put_page(page);
  1551. spin_lock(&hugetlb_lock);
  1552. return ret;
  1553. }
  1554. /*
  1555. * This routine has two main purposes:
  1556. * 1) Decrement the reservation count (resv_huge_pages) by the value passed
  1557. * in unused_resv_pages. This corresponds to the prior adjustments made
  1558. * to the associated reservation map.
  1559. * 2) Free any unused surplus pages that may have been allocated to satisfy
  1560. * the reservation. As many as unused_resv_pages may be freed.
  1561. *
  1562. * Called with hugetlb_lock held. However, the lock could be dropped (and
  1563. * reacquired) during calls to cond_resched_lock. Whenever dropping the lock,
  1564. * we must make sure nobody else can claim pages we are in the process of
  1565. * freeing. Do this by ensuring resv_huge_page always is greater than the
  1566. * number of huge pages we plan to free when dropping the lock.
  1567. */
  1568. static void return_unused_surplus_pages(struct hstate *h,
  1569. unsigned long unused_resv_pages)
  1570. {
  1571. unsigned long nr_pages;
  1572. /* Cannot return gigantic pages currently */
  1573. if (hstate_is_gigantic(h))
  1574. goto out;
  1575. /*
  1576. * Part (or even all) of the reservation could have been backed
  1577. * by pre-allocated pages. Only free surplus pages.
  1578. */
  1579. nr_pages = min(unused_resv_pages, h->surplus_huge_pages);
  1580. /*
  1581. * We want to release as many surplus pages as possible, spread
  1582. * evenly across all nodes with memory. Iterate across these nodes
  1583. * until we can no longer free unreserved surplus pages. This occurs
  1584. * when the nodes with surplus pages have no free pages.
  1585. * free_pool_huge_page() will balance the the freed pages across the
  1586. * on-line nodes with memory and will handle the hstate accounting.
  1587. *
  1588. * Note that we decrement resv_huge_pages as we free the pages. If
  1589. * we drop the lock, resv_huge_pages will still be sufficiently large
  1590. * to cover subsequent pages we may free.
  1591. */
  1592. while (nr_pages--) {
  1593. h->resv_huge_pages--;
  1594. unused_resv_pages--;
  1595. if (!free_pool_huge_page(h, &node_states[N_MEMORY], 1))
  1596. goto out;
  1597. cond_resched_lock(&hugetlb_lock);
  1598. }
  1599. out:
  1600. /* Fully uncommit the reservation */
  1601. h->resv_huge_pages -= unused_resv_pages;
  1602. }
  1603. /*
  1604. * vma_needs_reservation, vma_commit_reservation and vma_end_reservation
  1605. * are used by the huge page allocation routines to manage reservations.
  1606. *
  1607. * vma_needs_reservation is called to determine if the huge page at addr
  1608. * within the vma has an associated reservation. If a reservation is
  1609. * needed, the value 1 is returned. The caller is then responsible for
  1610. * managing the global reservation and subpool usage counts. After
  1611. * the huge page has been allocated, vma_commit_reservation is called
  1612. * to add the page to the reservation map. If the page allocation fails,
  1613. * the reservation must be ended instead of committed. vma_end_reservation
  1614. * is called in such cases.
  1615. *
  1616. * In the normal case, vma_commit_reservation returns the same value
  1617. * as the preceding vma_needs_reservation call. The only time this
  1618. * is not the case is if a reserve map was changed between calls. It
  1619. * is the responsibility of the caller to notice the difference and
  1620. * take appropriate action.
  1621. *
  1622. * vma_add_reservation is used in error paths where a reservation must
  1623. * be restored when a newly allocated huge page must be freed. It is
  1624. * to be called after calling vma_needs_reservation to determine if a
  1625. * reservation exists.
  1626. */
  1627. enum vma_resv_mode {
  1628. VMA_NEEDS_RESV,
  1629. VMA_COMMIT_RESV,
  1630. VMA_END_RESV,
  1631. VMA_ADD_RESV,
  1632. };
  1633. static long __vma_reservation_common(struct hstate *h,
  1634. struct vm_area_struct *vma, unsigned long addr,
  1635. enum vma_resv_mode mode)
  1636. {
  1637. struct resv_map *resv;
  1638. pgoff_t idx;
  1639. long ret;
  1640. resv = vma_resv_map(vma);
  1641. if (!resv)
  1642. return 1;
  1643. idx = vma_hugecache_offset(h, vma, addr);
  1644. switch (mode) {
  1645. case VMA_NEEDS_RESV:
  1646. ret = region_chg(resv, idx, idx + 1);
  1647. break;
  1648. case VMA_COMMIT_RESV:
  1649. ret = region_add(resv, idx, idx + 1);
  1650. break;
  1651. case VMA_END_RESV:
  1652. region_abort(resv, idx, idx + 1);
  1653. ret = 0;
  1654. break;
  1655. case VMA_ADD_RESV:
  1656. if (vma->vm_flags & VM_MAYSHARE)
  1657. ret = region_add(resv, idx, idx + 1);
  1658. else {
  1659. region_abort(resv, idx, idx + 1);
  1660. ret = region_del(resv, idx, idx + 1);
  1661. }
  1662. break;
  1663. default:
  1664. BUG();
  1665. }
  1666. if (vma->vm_flags & VM_MAYSHARE)
  1667. return ret;
  1668. else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) && ret >= 0) {
  1669. /*
  1670. * In most cases, reserves always exist for private mappings.
  1671. * However, a file associated with mapping could have been
  1672. * hole punched or truncated after reserves were consumed.
  1673. * As subsequent fault on such a range will not use reserves.
  1674. * Subtle - The reserve map for private mappings has the
  1675. * opposite meaning than that of shared mappings. If NO
  1676. * entry is in the reserve map, it means a reservation exists.
  1677. * If an entry exists in the reserve map, it means the
  1678. * reservation has already been consumed. As a result, the
  1679. * return value of this routine is the opposite of the
  1680. * value returned from reserve map manipulation routines above.
  1681. */
  1682. if (ret)
  1683. return 0;
  1684. else
  1685. return 1;
  1686. }
  1687. else
  1688. return ret < 0 ? ret : 0;
  1689. }
  1690. static long vma_needs_reservation(struct hstate *h,
  1691. struct vm_area_struct *vma, unsigned long addr)
  1692. {
  1693. return __vma_reservation_common(h, vma, addr, VMA_NEEDS_RESV);
  1694. }
  1695. static long vma_commit_reservation(struct hstate *h,
  1696. struct vm_area_struct *vma, unsigned long addr)
  1697. {
  1698. return __vma_reservation_common(h, vma, addr, VMA_COMMIT_RESV);
  1699. }
  1700. static void vma_end_reservation(struct hstate *h,
  1701. struct vm_area_struct *vma, unsigned long addr)
  1702. {
  1703. (void)__vma_reservation_common(h, vma, addr, VMA_END_RESV);
  1704. }
  1705. static long vma_add_reservation(struct hstate *h,
  1706. struct vm_area_struct *vma, unsigned long addr)
  1707. {
  1708. return __vma_reservation_common(h, vma, addr, VMA_ADD_RESV);
  1709. }
  1710. /*
  1711. * This routine is called to restore a reservation on error paths. In the
  1712. * specific error paths, a huge page was allocated (via alloc_huge_page)
  1713. * and is about to be freed. If a reservation for the page existed,
  1714. * alloc_huge_page would have consumed the reservation and set PagePrivate
  1715. * in the newly allocated page. When the page is freed via free_huge_page,
  1716. * the global reservation count will be incremented if PagePrivate is set.
  1717. * However, free_huge_page can not adjust the reserve map. Adjust the
  1718. * reserve map here to be consistent with global reserve count adjustments
  1719. * to be made by free_huge_page.
  1720. */
  1721. static void restore_reserve_on_error(struct hstate *h,
  1722. struct vm_area_struct *vma, unsigned long address,
  1723. struct page *page)
  1724. {
  1725. if (unlikely(PagePrivate(page))) {
  1726. long rc = vma_needs_reservation(h, vma, address);
  1727. if (unlikely(rc < 0)) {
  1728. /*
  1729. * Rare out of memory condition in reserve map
  1730. * manipulation. Clear PagePrivate so that
  1731. * global reserve count will not be incremented
  1732. * by free_huge_page. This will make it appear
  1733. * as though the reservation for this page was
  1734. * consumed. This may prevent the task from
  1735. * faulting in the page at a later time. This
  1736. * is better than inconsistent global huge page
  1737. * accounting of reserve counts.
  1738. */
  1739. ClearPagePrivate(page);
  1740. } else if (rc) {
  1741. rc = vma_add_reservation(h, vma, address);
  1742. if (unlikely(rc < 0))
  1743. /*
  1744. * See above comment about rare out of
  1745. * memory condition.
  1746. */
  1747. ClearPagePrivate(page);
  1748. } else
  1749. vma_end_reservation(h, vma, address);
  1750. }
  1751. }
  1752. struct page *alloc_huge_page(struct vm_area_struct *vma,
  1753. unsigned long addr, int avoid_reserve)
  1754. {
  1755. struct hugepage_subpool *spool = subpool_vma(vma);
  1756. struct hstate *h = hstate_vma(vma);
  1757. struct page *page;
  1758. long map_chg, map_commit;
  1759. long gbl_chg;
  1760. int ret, idx;
  1761. struct hugetlb_cgroup *h_cg;
  1762. idx = hstate_index(h);
  1763. /*
  1764. * Examine the region/reserve map to determine if the process
  1765. * has a reservation for the page to be allocated. A return
  1766. * code of zero indicates a reservation exists (no change).
  1767. */
  1768. map_chg = gbl_chg = vma_needs_reservation(h, vma, addr);
  1769. if (map_chg < 0)
  1770. return ERR_PTR(-ENOMEM);
  1771. /*
  1772. * Processes that did not create the mapping will have no
  1773. * reserves as indicated by the region/reserve map. Check
  1774. * that the allocation will not exceed the subpool limit.
  1775. * Allocations for MAP_NORESERVE mappings also need to be
  1776. * checked against any subpool limit.
  1777. */
  1778. if (map_chg || avoid_reserve) {
  1779. gbl_chg = hugepage_subpool_get_pages(spool, 1);
  1780. if (gbl_chg < 0) {
  1781. vma_end_reservation(h, vma, addr);
  1782. return ERR_PTR(-ENOSPC);
  1783. }
  1784. /*
  1785. * Even though there was no reservation in the region/reserve
  1786. * map, there could be reservations associated with the
  1787. * subpool that can be used. This would be indicated if the
  1788. * return value of hugepage_subpool_get_pages() is zero.
  1789. * However, if avoid_reserve is specified we still avoid even
  1790. * the subpool reservations.
  1791. */
  1792. if (avoid_reserve)
  1793. gbl_chg = 1;
  1794. }
  1795. ret = hugetlb_cgroup_charge_cgroup(idx, pages_per_huge_page(h), &h_cg);
  1796. if (ret)
  1797. goto out_subpool_put;
  1798. spin_lock(&hugetlb_lock);
  1799. /*
  1800. * glb_chg is passed to indicate whether or not a page must be taken
  1801. * from the global free pool (global change). gbl_chg == 0 indicates
  1802. * a reservation exists for the allocation.
  1803. */
  1804. page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve, gbl_chg);
  1805. if (!page) {
  1806. spin_unlock(&hugetlb_lock);
  1807. page = __alloc_buddy_huge_page_with_mpol(h, vma, addr);
  1808. if (!page)
  1809. goto out_uncharge_cgroup;
  1810. if (!avoid_reserve && vma_has_reserves(vma, gbl_chg)) {
  1811. SetPagePrivate(page);
  1812. h->resv_huge_pages--;
  1813. }
  1814. spin_lock(&hugetlb_lock);
  1815. list_move(&page->lru, &h->hugepage_activelist);
  1816. /* Fall through */
  1817. }
  1818. hugetlb_cgroup_commit_charge(idx, pages_per_huge_page(h), h_cg, page);
  1819. spin_unlock(&hugetlb_lock);
  1820. set_page_private(page, (unsigned long)spool);
  1821. map_commit = vma_commit_reservation(h, vma, addr);
  1822. if (unlikely(map_chg > map_commit)) {
  1823. /*
  1824. * The page was added to the reservation map between
  1825. * vma_needs_reservation and vma_commit_reservation.
  1826. * This indicates a race with hugetlb_reserve_pages.
  1827. * Adjust for the subpool count incremented above AND
  1828. * in hugetlb_reserve_pages for the same page. Also,
  1829. * the reservation count added in hugetlb_reserve_pages
  1830. * no longer applies.
  1831. */
  1832. long rsv_adjust;
  1833. rsv_adjust = hugepage_subpool_put_pages(spool, 1);
  1834. hugetlb_acct_memory(h, -rsv_adjust);
  1835. }
  1836. return page;
  1837. out_uncharge_cgroup:
  1838. hugetlb_cgroup_uncharge_cgroup(idx, pages_per_huge_page(h), h_cg);
  1839. out_subpool_put:
  1840. if (map_chg || avoid_reserve)
  1841. hugepage_subpool_put_pages(spool, 1);
  1842. vma_end_reservation(h, vma, addr);
  1843. return ERR_PTR(-ENOSPC);
  1844. }
  1845. /*
  1846. * alloc_huge_page()'s wrapper which simply returns the page if allocation
  1847. * succeeds, otherwise NULL. This function is called from new_vma_page(),
  1848. * where no ERR_VALUE is expected to be returned.
  1849. */
  1850. struct page *alloc_huge_page_noerr(struct vm_area_struct *vma,
  1851. unsigned long addr, int avoid_reserve)
  1852. {
  1853. struct page *page = alloc_huge_page(vma, addr, avoid_reserve);
  1854. if (IS_ERR(page))
  1855. page = NULL;
  1856. return page;
  1857. }
  1858. int __weak alloc_bootmem_huge_page(struct hstate *h)
  1859. {
  1860. struct huge_bootmem_page *m;
  1861. int nr_nodes, node;
  1862. for_each_node_mask_to_alloc(h, nr_nodes, node, &node_states[N_MEMORY]) {
  1863. void *addr;
  1864. addr = memblock_virt_alloc_try_nid_nopanic(
  1865. huge_page_size(h), huge_page_size(h),
  1866. 0, BOOTMEM_ALLOC_ACCESSIBLE, node);
  1867. if (addr) {
  1868. /*
  1869. * Use the beginning of the huge page to store the
  1870. * huge_bootmem_page struct (until gather_bootmem
  1871. * puts them into the mem_map).
  1872. */
  1873. m = addr;
  1874. goto found;
  1875. }
  1876. }
  1877. return 0;
  1878. found:
  1879. BUG_ON(!IS_ALIGNED(virt_to_phys(m), huge_page_size(h)));
  1880. /* Put them into a private list first because mem_map is not up yet */
  1881. list_add(&m->list, &huge_boot_pages);
  1882. m->hstate = h;
  1883. return 1;
  1884. }
  1885. static void __init prep_compound_huge_page(struct page *page,
  1886. unsigned int order)
  1887. {
  1888. if (unlikely(order > (MAX_ORDER - 1)))
  1889. prep_compound_gigantic_page(page, order);
  1890. else
  1891. prep_compound_page(page, order);
  1892. }
  1893. /* Put bootmem huge pages into the standard lists after mem_map is up */
  1894. static void __init gather_bootmem_prealloc(void)
  1895. {
  1896. struct huge_bootmem_page *m;
  1897. list_for_each_entry(m, &huge_boot_pages, list) {
  1898. struct hstate *h = m->hstate;
  1899. struct page *page;
  1900. #ifdef CONFIG_HIGHMEM
  1901. page = pfn_to_page(m->phys >> PAGE_SHIFT);
  1902. memblock_free_late(__pa(m),
  1903. sizeof(struct huge_bootmem_page));
  1904. #else
  1905. page = virt_to_page(m);
  1906. #endif
  1907. WARN_ON(page_count(page) != 1);
  1908. prep_compound_huge_page(page, h->order);
  1909. WARN_ON(PageReserved(page));
  1910. prep_new_huge_page(h, page, page_to_nid(page));
  1911. /*
  1912. * If we had gigantic hugepages allocated at boot time, we need
  1913. * to restore the 'stolen' pages to totalram_pages in order to
  1914. * fix confusing memory reports from free(1) and another
  1915. * side-effects, like CommitLimit going negative.
  1916. */
  1917. if (hstate_is_gigantic(h))
  1918. adjust_managed_page_count(page, 1 << h->order);
  1919. }
  1920. }
  1921. static void __init hugetlb_hstate_alloc_pages(struct hstate *h)
  1922. {
  1923. unsigned long i;
  1924. for (i = 0; i < h->max_huge_pages; ++i) {
  1925. if (hstate_is_gigantic(h)) {
  1926. if (!alloc_bootmem_huge_page(h))
  1927. break;
  1928. } else if (!alloc_fresh_huge_page(h,
  1929. &node_states[N_MEMORY]))
  1930. break;
  1931. }
  1932. h->max_huge_pages = i;
  1933. }
  1934. static void __init hugetlb_init_hstates(void)
  1935. {
  1936. struct hstate *h;
  1937. for_each_hstate(h) {
  1938. if (minimum_order > huge_page_order(h))
  1939. minimum_order = huge_page_order(h);
  1940. /* oversize hugepages were init'ed in early boot */
  1941. if (!hstate_is_gigantic(h))
  1942. hugetlb_hstate_alloc_pages(h);
  1943. }
  1944. VM_BUG_ON(minimum_order == UINT_MAX);
  1945. }
  1946. static char * __init memfmt(char *buf, unsigned long n)
  1947. {
  1948. if (n >= (1UL << 30))
  1949. sprintf(buf, "%lu GB", n >> 30);
  1950. else if (n >= (1UL << 20))
  1951. sprintf(buf, "%lu MB", n >> 20);
  1952. else
  1953. sprintf(buf, "%lu KB", n >> 10);
  1954. return buf;
  1955. }
  1956. static void __init report_hugepages(void)
  1957. {
  1958. struct hstate *h;
  1959. for_each_hstate(h) {
  1960. char buf[32];
  1961. pr_info("HugeTLB registered %s page size, pre-allocated %ld pages\n",
  1962. memfmt(buf, huge_page_size(h)),
  1963. h->free_huge_pages);
  1964. }
  1965. }
  1966. #ifdef CONFIG_HIGHMEM
  1967. static void try_to_free_low(struct hstate *h, unsigned long count,
  1968. nodemask_t *nodes_allowed)
  1969. {
  1970. int i;
  1971. if (hstate_is_gigantic(h))
  1972. return;
  1973. for_each_node_mask(i, *nodes_allowed) {
  1974. struct page *page, *next;
  1975. struct list_head *freel = &h->hugepage_freelists[i];
  1976. list_for_each_entry_safe(page, next, freel, lru) {
  1977. if (count >= h->nr_huge_pages)
  1978. return;
  1979. if (PageHighMem(page))
  1980. continue;
  1981. list_del(&page->lru);
  1982. update_and_free_page(h, page);
  1983. h->free_huge_pages--;
  1984. h->free_huge_pages_node[page_to_nid(page)]--;
  1985. }
  1986. }
  1987. }
  1988. #else
  1989. static inline void try_to_free_low(struct hstate *h, unsigned long count,
  1990. nodemask_t *nodes_allowed)
  1991. {
  1992. }
  1993. #endif
  1994. /*
  1995. * Increment or decrement surplus_huge_pages. Keep node-specific counters
  1996. * balanced by operating on them in a round-robin fashion.
  1997. * Returns 1 if an adjustment was made.
  1998. */
  1999. static int adjust_pool_surplus(struct hstate *h, nodemask_t *nodes_allowed,
  2000. int delta)
  2001. {
  2002. int nr_nodes, node;
  2003. VM_BUG_ON(delta != -1 && delta != 1);
  2004. if (delta < 0) {
  2005. for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) {
  2006. if (h->surplus_huge_pages_node[node])
  2007. goto found;
  2008. }
  2009. } else {
  2010. for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) {
  2011. if (h->surplus_huge_pages_node[node] <
  2012. h->nr_huge_pages_node[node])
  2013. goto found;
  2014. }
  2015. }
  2016. return 0;
  2017. found:
  2018. h->surplus_huge_pages += delta;
  2019. h->surplus_huge_pages_node[node] += delta;
  2020. return 1;
  2021. }
  2022. #define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages)
  2023. static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count,
  2024. nodemask_t *nodes_allowed)
  2025. {
  2026. unsigned long min_count, ret;
  2027. if (hstate_is_gigantic(h) && !gigantic_page_supported())
  2028. return h->max_huge_pages;
  2029. /*
  2030. * Increase the pool size
  2031. * First take pages out of surplus state. Then make up the
  2032. * remaining difference by allocating fresh huge pages.
  2033. *
  2034. * We might race with __alloc_buddy_huge_page() here and be unable
  2035. * to convert a surplus huge page to a normal huge page. That is
  2036. * not critical, though, it just means the overall size of the
  2037. * pool might be one hugepage larger than it needs to be, but
  2038. * within all the constraints specified by the sysctls.
  2039. */
  2040. spin_lock(&hugetlb_lock);
  2041. while (h->surplus_huge_pages && count > persistent_huge_pages(h)) {
  2042. if (!adjust_pool_surplus(h, nodes_allowed, -1))
  2043. break;
  2044. }
  2045. while (count > persistent_huge_pages(h)) {
  2046. /*
  2047. * If this allocation races such that we no longer need the
  2048. * page, free_huge_page will handle it by freeing the page
  2049. * and reducing the surplus.
  2050. */
  2051. spin_unlock(&hugetlb_lock);
  2052. /* yield cpu to avoid soft lockup */
  2053. cond_resched();
  2054. if (hstate_is_gigantic(h))
  2055. ret = alloc_fresh_gigantic_page(h, nodes_allowed);
  2056. else
  2057. ret = alloc_fresh_huge_page(h, nodes_allowed);
  2058. spin_lock(&hugetlb_lock);
  2059. if (!ret)
  2060. goto out;
  2061. /* Bail for signals. Probably ctrl-c from user */
  2062. if (signal_pending(current))
  2063. goto out;
  2064. }
  2065. /*
  2066. * Decrease the pool size
  2067. * First return free pages to the buddy allocator (being careful
  2068. * to keep enough around to satisfy reservations). Then place
  2069. * pages into surplus state as needed so the pool will shrink
  2070. * to the desired size as pages become free.
  2071. *
  2072. * By placing pages into the surplus state independent of the
  2073. * overcommit value, we are allowing the surplus pool size to
  2074. * exceed overcommit. There are few sane options here. Since
  2075. * __alloc_buddy_huge_page() is checking the global counter,
  2076. * though, we'll note that we're not allowed to exceed surplus
  2077. * and won't grow the pool anywhere else. Not until one of the
  2078. * sysctls are changed, or the surplus pages go out of use.
  2079. */
  2080. min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages;
  2081. min_count = max(count, min_count);
  2082. try_to_free_low(h, min_count, nodes_allowed);
  2083. while (min_count < persistent_huge_pages(h)) {
  2084. if (!free_pool_huge_page(h, nodes_allowed, 0))
  2085. break;
  2086. cond_resched_lock(&hugetlb_lock);
  2087. }
  2088. while (count < persistent_huge_pages(h)) {
  2089. if (!adjust_pool_surplus(h, nodes_allowed, 1))
  2090. break;
  2091. }
  2092. out:
  2093. ret = persistent_huge_pages(h);
  2094. spin_unlock(&hugetlb_lock);
  2095. return ret;
  2096. }
  2097. #define HSTATE_ATTR_RO(_name) \
  2098. static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
  2099. #define HSTATE_ATTR(_name) \
  2100. static struct kobj_attribute _name##_attr = \
  2101. __ATTR(_name, 0644, _name##_show, _name##_store)
  2102. static struct kobject *hugepages_kobj;
  2103. static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
  2104. static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp);
  2105. static struct hstate *kobj_to_hstate(struct kobject *kobj, int *nidp)
  2106. {
  2107. int i;
  2108. for (i = 0; i < HUGE_MAX_HSTATE; i++)
  2109. if (hstate_kobjs[i] == kobj) {
  2110. if (nidp)
  2111. *nidp = NUMA_NO_NODE;
  2112. return &hstates[i];
  2113. }
  2114. return kobj_to_node_hstate(kobj, nidp);
  2115. }
  2116. static ssize_t nr_hugepages_show_common(struct kobject *kobj,
  2117. struct kobj_attribute *attr, char *buf)
  2118. {
  2119. struct hstate *h;
  2120. unsigned long nr_huge_pages;
  2121. int nid;
  2122. h = kobj_to_hstate(kobj, &nid);
  2123. if (nid == NUMA_NO_NODE)
  2124. nr_huge_pages = h->nr_huge_pages;
  2125. else
  2126. nr_huge_pages = h->nr_huge_pages_node[nid];
  2127. return sprintf(buf, "%lu\n", nr_huge_pages);
  2128. }
  2129. static ssize_t __nr_hugepages_store_common(bool obey_mempolicy,
  2130. struct hstate *h, int nid,
  2131. unsigned long count, size_t len)
  2132. {
  2133. int err;
  2134. NODEMASK_ALLOC(nodemask_t, nodes_allowed, GFP_KERNEL | __GFP_NORETRY);
  2135. if (hstate_is_gigantic(h) && !gigantic_page_supported()) {
  2136. err = -EINVAL;
  2137. goto out;
  2138. }
  2139. if (nid == NUMA_NO_NODE) {
  2140. /*
  2141. * global hstate attribute
  2142. */
  2143. if (!(obey_mempolicy &&
  2144. init_nodemask_of_mempolicy(nodes_allowed))) {
  2145. NODEMASK_FREE(nodes_allowed);
  2146. nodes_allowed = &node_states[N_MEMORY];
  2147. }
  2148. } else if (nodes_allowed) {
  2149. /*
  2150. * per node hstate attribute: adjust count to global,
  2151. * but restrict alloc/free to the specified node.
  2152. */
  2153. count += h->nr_huge_pages - h->nr_huge_pages_node[nid];
  2154. init_nodemask_of_node(nodes_allowed, nid);
  2155. } else
  2156. nodes_allowed = &node_states[N_MEMORY];
  2157. h->max_huge_pages = set_max_huge_pages(h, count, nodes_allowed);
  2158. if (nodes_allowed != &node_states[N_MEMORY])
  2159. NODEMASK_FREE(nodes_allowed);
  2160. return len;
  2161. out:
  2162. NODEMASK_FREE(nodes_allowed);
  2163. return err;
  2164. }
  2165. static ssize_t nr_hugepages_store_common(bool obey_mempolicy,
  2166. struct kobject *kobj, const char *buf,
  2167. size_t len)
  2168. {
  2169. struct hstate *h;
  2170. unsigned long count;
  2171. int nid;
  2172. int err;
  2173. err = kstrtoul(buf, 10, &count);
  2174. if (err)
  2175. return err;
  2176. h = kobj_to_hstate(kobj, &nid);
  2177. return __nr_hugepages_store_common(obey_mempolicy, h, nid, count, len);
  2178. }
  2179. static ssize_t nr_hugepages_show(struct kobject *kobj,
  2180. struct kobj_attribute *attr, char *buf)
  2181. {
  2182. return nr_hugepages_show_common(kobj, attr, buf);
  2183. }
  2184. static ssize_t nr_hugepages_store(struct kobject *kobj,
  2185. struct kobj_attribute *attr, const char *buf, size_t len)
  2186. {
  2187. return nr_hugepages_store_common(false, kobj, buf, len);
  2188. }
  2189. HSTATE_ATTR(nr_hugepages);
  2190. #ifdef CONFIG_NUMA
  2191. /*
  2192. * hstate attribute for optionally mempolicy-based constraint on persistent
  2193. * huge page alloc/free.
  2194. */
  2195. static ssize_t nr_hugepages_mempolicy_show(struct kobject *kobj,
  2196. struct kobj_attribute *attr, char *buf)
  2197. {
  2198. return nr_hugepages_show_common(kobj, attr, buf);
  2199. }
  2200. static ssize_t nr_hugepages_mempolicy_store(struct kobject *kobj,
  2201. struct kobj_attribute *attr, const char *buf, size_t len)
  2202. {
  2203. return nr_hugepages_store_common(true, kobj, buf, len);
  2204. }
  2205. HSTATE_ATTR(nr_hugepages_mempolicy);
  2206. #endif
  2207. static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj,
  2208. struct kobj_attribute *attr, char *buf)
  2209. {
  2210. struct hstate *h = kobj_to_hstate(kobj, NULL);
  2211. return sprintf(buf, "%lu\n", h->nr_overcommit_huge_pages);
  2212. }
  2213. static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj,
  2214. struct kobj_attribute *attr, const char *buf, size_t count)
  2215. {
  2216. int err;
  2217. unsigned long input;
  2218. struct hstate *h = kobj_to_hstate(kobj, NULL);
  2219. if (hstate_is_gigantic(h))
  2220. return -EINVAL;
  2221. err = kstrtoul(buf, 10, &input);
  2222. if (err)
  2223. return err;
  2224. spin_lock(&hugetlb_lock);
  2225. h->nr_overcommit_huge_pages = input;
  2226. spin_unlock(&hugetlb_lock);
  2227. return count;
  2228. }
  2229. HSTATE_ATTR(nr_overcommit_hugepages);
  2230. static ssize_t free_hugepages_show(struct kobject *kobj,
  2231. struct kobj_attribute *attr, char *buf)
  2232. {
  2233. struct hstate *h;
  2234. unsigned long free_huge_pages;
  2235. int nid;
  2236. h = kobj_to_hstate(kobj, &nid);
  2237. if (nid == NUMA_NO_NODE)
  2238. free_huge_pages = h->free_huge_pages;
  2239. else
  2240. free_huge_pages = h->free_huge_pages_node[nid];
  2241. return sprintf(buf, "%lu\n", free_huge_pages);
  2242. }
  2243. HSTATE_ATTR_RO(free_hugepages);
  2244. static ssize_t resv_hugepages_show(struct kobject *kobj,
  2245. struct kobj_attribute *attr, char *buf)
  2246. {
  2247. struct hstate *h = kobj_to_hstate(kobj, NULL);
  2248. return sprintf(buf, "%lu\n", h->resv_huge_pages);
  2249. }
  2250. HSTATE_ATTR_RO(resv_hugepages);
  2251. static ssize_t surplus_hugepages_show(struct kobject *kobj,
  2252. struct kobj_attribute *attr, char *buf)
  2253. {
  2254. struct hstate *h;
  2255. unsigned long surplus_huge_pages;
  2256. int nid;
  2257. h = kobj_to_hstate(kobj, &nid);
  2258. if (nid == NUMA_NO_NODE)
  2259. surplus_huge_pages = h->surplus_huge_pages;
  2260. else
  2261. surplus_huge_pages = h->surplus_huge_pages_node[nid];
  2262. return sprintf(buf, "%lu\n", surplus_huge_pages);
  2263. }
  2264. HSTATE_ATTR_RO(surplus_hugepages);
  2265. static struct attribute *hstate_attrs[] = {
  2266. &nr_hugepages_attr.attr,
  2267. &nr_overcommit_hugepages_attr.attr,
  2268. &free_hugepages_attr.attr,
  2269. &resv_hugepages_attr.attr,
  2270. &surplus_hugepages_attr.attr,
  2271. #ifdef CONFIG_NUMA
  2272. &nr_hugepages_mempolicy_attr.attr,
  2273. #endif
  2274. NULL,
  2275. };
  2276. static struct attribute_group hstate_attr_group = {
  2277. .attrs = hstate_attrs,
  2278. };
  2279. static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent,
  2280. struct kobject **hstate_kobjs,
  2281. struct attribute_group *hstate_attr_group)
  2282. {
  2283. int retval;
  2284. int hi = hstate_index(h);
  2285. hstate_kobjs[hi] = kobject_create_and_add(h->name, parent);
  2286. if (!hstate_kobjs[hi])
  2287. return -ENOMEM;
  2288. retval = sysfs_create_group(hstate_kobjs[hi], hstate_attr_group);
  2289. if (retval)
  2290. kobject_put(hstate_kobjs[hi]);
  2291. return retval;
  2292. }
  2293. static void __init hugetlb_sysfs_init(void)
  2294. {
  2295. struct hstate *h;
  2296. int err;
  2297. hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj);
  2298. if (!hugepages_kobj)
  2299. return;
  2300. for_each_hstate(h) {
  2301. err = hugetlb_sysfs_add_hstate(h, hugepages_kobj,
  2302. hstate_kobjs, &hstate_attr_group);
  2303. if (err)
  2304. pr_err("Hugetlb: Unable to add hstate %s", h->name);
  2305. }
  2306. }
  2307. #ifdef CONFIG_NUMA
  2308. /*
  2309. * node_hstate/s - associate per node hstate attributes, via their kobjects,
  2310. * with node devices in node_devices[] using a parallel array. The array
  2311. * index of a node device or _hstate == node id.
  2312. * This is here to avoid any static dependency of the node device driver, in
  2313. * the base kernel, on the hugetlb module.
  2314. */
  2315. struct node_hstate {
  2316. struct kobject *hugepages_kobj;
  2317. struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
  2318. };
  2319. static struct node_hstate node_hstates[MAX_NUMNODES];
  2320. /*
  2321. * A subset of global hstate attributes for node devices
  2322. */
  2323. static struct attribute *per_node_hstate_attrs[] = {
  2324. &nr_hugepages_attr.attr,
  2325. &free_hugepages_attr.attr,
  2326. &surplus_hugepages_attr.attr,
  2327. NULL,
  2328. };
  2329. static struct attribute_group per_node_hstate_attr_group = {
  2330. .attrs = per_node_hstate_attrs,
  2331. };
  2332. /*
  2333. * kobj_to_node_hstate - lookup global hstate for node device hstate attr kobj.
  2334. * Returns node id via non-NULL nidp.
  2335. */
  2336. static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
  2337. {
  2338. int nid;
  2339. for (nid = 0; nid < nr_node_ids; nid++) {
  2340. struct node_hstate *nhs = &node_hstates[nid];
  2341. int i;
  2342. for (i = 0; i < HUGE_MAX_HSTATE; i++)
  2343. if (nhs->hstate_kobjs[i] == kobj) {
  2344. if (nidp)
  2345. *nidp = nid;
  2346. return &hstates[i];
  2347. }
  2348. }
  2349. BUG();
  2350. return NULL;
  2351. }
  2352. /*
  2353. * Unregister hstate attributes from a single node device.
  2354. * No-op if no hstate attributes attached.
  2355. */
  2356. static void hugetlb_unregister_node(struct node *node)
  2357. {
  2358. struct hstate *h;
  2359. struct node_hstate *nhs = &node_hstates[node->dev.id];
  2360. if (!nhs->hugepages_kobj)
  2361. return; /* no hstate attributes */
  2362. for_each_hstate(h) {
  2363. int idx = hstate_index(h);
  2364. if (nhs->hstate_kobjs[idx]) {
  2365. kobject_put(nhs->hstate_kobjs[idx]);
  2366. nhs->hstate_kobjs[idx] = NULL;
  2367. }
  2368. }
  2369. kobject_put(nhs->hugepages_kobj);
  2370. nhs->hugepages_kobj = NULL;
  2371. }
  2372. /*
  2373. * Register hstate attributes for a single node device.
  2374. * No-op if attributes already registered.
  2375. */
  2376. static void hugetlb_register_node(struct node *node)
  2377. {
  2378. struct hstate *h;
  2379. struct node_hstate *nhs = &node_hstates[node->dev.id];
  2380. int err;
  2381. if (nhs->hugepages_kobj)
  2382. return; /* already allocated */
  2383. nhs->hugepages_kobj = kobject_create_and_add("hugepages",
  2384. &node->dev.kobj);
  2385. if (!nhs->hugepages_kobj)
  2386. return;
  2387. for_each_hstate(h) {
  2388. err = hugetlb_sysfs_add_hstate(h, nhs->hugepages_kobj,
  2389. nhs->hstate_kobjs,
  2390. &per_node_hstate_attr_group);
  2391. if (err) {
  2392. pr_err("Hugetlb: Unable to add hstate %s for node %d\n",
  2393. h->name, node->dev.id);
  2394. hugetlb_unregister_node(node);
  2395. break;
  2396. }
  2397. }
  2398. }
  2399. /*
  2400. * hugetlb init time: register hstate attributes for all registered node
  2401. * devices of nodes that have memory. All on-line nodes should have
  2402. * registered their associated device by this time.
  2403. */
  2404. static void __init hugetlb_register_all_nodes(void)
  2405. {
  2406. int nid;
  2407. for_each_node_state(nid, N_MEMORY) {
  2408. struct node *node = node_devices[nid];
  2409. if (node->dev.id == nid)
  2410. hugetlb_register_node(node);
  2411. }
  2412. /*
  2413. * Let the node device driver know we're here so it can
  2414. * [un]register hstate attributes on node hotplug.
  2415. */
  2416. register_hugetlbfs_with_node(hugetlb_register_node,
  2417. hugetlb_unregister_node);
  2418. }
  2419. #else /* !CONFIG_NUMA */
  2420. static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
  2421. {
  2422. BUG();
  2423. if (nidp)
  2424. *nidp = -1;
  2425. return NULL;
  2426. }
  2427. static void hugetlb_register_all_nodes(void) { }
  2428. #endif
  2429. static int __init hugetlb_init(void)
  2430. {
  2431. int i;
  2432. if (!hugepages_supported())
  2433. return 0;
  2434. if (!size_to_hstate(default_hstate_size)) {
  2435. default_hstate_size = HPAGE_SIZE;
  2436. if (!size_to_hstate(default_hstate_size))
  2437. hugetlb_add_hstate(HUGETLB_PAGE_ORDER);
  2438. }
  2439. default_hstate_idx = hstate_index(size_to_hstate(default_hstate_size));
  2440. if (default_hstate_max_huge_pages) {
  2441. if (!default_hstate.max_huge_pages)
  2442. default_hstate.max_huge_pages = default_hstate_max_huge_pages;
  2443. }
  2444. hugetlb_init_hstates();
  2445. gather_bootmem_prealloc();
  2446. report_hugepages();
  2447. hugetlb_sysfs_init();
  2448. hugetlb_register_all_nodes();
  2449. hugetlb_cgroup_file_init();
  2450. #ifdef CONFIG_SMP
  2451. num_fault_mutexes = roundup_pow_of_two(8 * num_possible_cpus());
  2452. #else
  2453. num_fault_mutexes = 1;
  2454. #endif
  2455. hugetlb_fault_mutex_table =
  2456. kmalloc(sizeof(struct mutex) * num_fault_mutexes, GFP_KERNEL);
  2457. BUG_ON(!hugetlb_fault_mutex_table);
  2458. for (i = 0; i < num_fault_mutexes; i++)
  2459. mutex_init(&hugetlb_fault_mutex_table[i]);
  2460. return 0;
  2461. }
  2462. subsys_initcall(hugetlb_init);
  2463. /* Should be called on processing a hugepagesz=... option */
  2464. void __init hugetlb_bad_size(void)
  2465. {
  2466. parsed_valid_hugepagesz = false;
  2467. }
  2468. void __init hugetlb_add_hstate(unsigned int order)
  2469. {
  2470. struct hstate *h;
  2471. unsigned long i;
  2472. if (size_to_hstate(PAGE_SIZE << order)) {
  2473. pr_warn("hugepagesz= specified twice, ignoring\n");
  2474. return;
  2475. }
  2476. BUG_ON(hugetlb_max_hstate >= HUGE_MAX_HSTATE);
  2477. BUG_ON(order == 0);
  2478. h = &hstates[hugetlb_max_hstate++];
  2479. h->order = order;
  2480. h->mask = ~((1ULL << (order + PAGE_SHIFT)) - 1);
  2481. h->nr_huge_pages = 0;
  2482. h->free_huge_pages = 0;
  2483. for (i = 0; i < MAX_NUMNODES; ++i)
  2484. INIT_LIST_HEAD(&h->hugepage_freelists[i]);
  2485. INIT_LIST_HEAD(&h->hugepage_activelist);
  2486. h->next_nid_to_alloc = first_memory_node;
  2487. h->next_nid_to_free = first_memory_node;
  2488. snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB",
  2489. huge_page_size(h)/1024);
  2490. parsed_hstate = h;
  2491. }
  2492. static int __init hugetlb_nrpages_setup(char *s)
  2493. {
  2494. unsigned long *mhp;
  2495. static unsigned long *last_mhp;
  2496. if (!parsed_valid_hugepagesz) {
  2497. pr_warn("hugepages = %s preceded by "
  2498. "an unsupported hugepagesz, ignoring\n", s);
  2499. parsed_valid_hugepagesz = true;
  2500. return 1;
  2501. }
  2502. /*
  2503. * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter yet,
  2504. * so this hugepages= parameter goes to the "default hstate".
  2505. */
  2506. else if (!hugetlb_max_hstate)
  2507. mhp = &default_hstate_max_huge_pages;
  2508. else
  2509. mhp = &parsed_hstate->max_huge_pages;
  2510. if (mhp == last_mhp) {
  2511. pr_warn("hugepages= specified twice without interleaving hugepagesz=, ignoring\n");
  2512. return 1;
  2513. }
  2514. if (sscanf(s, "%lu", mhp) <= 0)
  2515. *mhp = 0;
  2516. /*
  2517. * Global state is always initialized later in hugetlb_init.
  2518. * But we need to allocate >= MAX_ORDER hstates here early to still
  2519. * use the bootmem allocator.
  2520. */
  2521. if (hugetlb_max_hstate && parsed_hstate->order >= MAX_ORDER)
  2522. hugetlb_hstate_alloc_pages(parsed_hstate);
  2523. last_mhp = mhp;
  2524. return 1;
  2525. }
  2526. __setup("hugepages=", hugetlb_nrpages_setup);
  2527. static int __init hugetlb_default_setup(char *s)
  2528. {
  2529. default_hstate_size = memparse(s, &s);
  2530. return 1;
  2531. }
  2532. __setup("default_hugepagesz=", hugetlb_default_setup);
  2533. static unsigned int cpuset_mems_nr(unsigned int *array)
  2534. {
  2535. int node;
  2536. unsigned int nr = 0;
  2537. for_each_node_mask(node, cpuset_current_mems_allowed)
  2538. nr += array[node];
  2539. return nr;
  2540. }
  2541. #ifdef CONFIG_SYSCTL
  2542. static int hugetlb_sysctl_handler_common(bool obey_mempolicy,
  2543. struct ctl_table *table, int write,
  2544. void __user *buffer, size_t *length, loff_t *ppos)
  2545. {
  2546. struct hstate *h = &default_hstate;
  2547. unsigned long tmp = h->max_huge_pages;
  2548. int ret;
  2549. if (!hugepages_supported())
  2550. return -EOPNOTSUPP;
  2551. table->data = &tmp;
  2552. table->maxlen = sizeof(unsigned long);
  2553. ret = proc_doulongvec_minmax(table, write, buffer, length, ppos);
  2554. if (ret)
  2555. goto out;
  2556. if (write)
  2557. ret = __nr_hugepages_store_common(obey_mempolicy, h,
  2558. NUMA_NO_NODE, tmp, *length);
  2559. out:
  2560. return ret;
  2561. }
  2562. int hugetlb_sysctl_handler(struct ctl_table *table, int write,
  2563. void __user *buffer, size_t *length, loff_t *ppos)
  2564. {
  2565. return hugetlb_sysctl_handler_common(false, table, write,
  2566. buffer, length, ppos);
  2567. }
  2568. #ifdef CONFIG_NUMA
  2569. int hugetlb_mempolicy_sysctl_handler(struct ctl_table *table, int write,
  2570. void __user *buffer, size_t *length, loff_t *ppos)
  2571. {
  2572. return hugetlb_sysctl_handler_common(true, table, write,
  2573. buffer, length, ppos);
  2574. }
  2575. #endif /* CONFIG_NUMA */
  2576. int hugetlb_overcommit_handler(struct ctl_table *table, int write,
  2577. void __user *buffer,
  2578. size_t *length, loff_t *ppos)
  2579. {
  2580. struct hstate *h = &default_hstate;
  2581. unsigned long tmp;
  2582. int ret;
  2583. if (!hugepages_supported())
  2584. return -EOPNOTSUPP;
  2585. tmp = h->nr_overcommit_huge_pages;
  2586. if (write && hstate_is_gigantic(h))
  2587. return -EINVAL;
  2588. table->data = &tmp;
  2589. table->maxlen = sizeof(unsigned long);
  2590. ret = proc_doulongvec_minmax(table, write, buffer, length, ppos);
  2591. if (ret)
  2592. goto out;
  2593. if (write) {
  2594. spin_lock(&hugetlb_lock);
  2595. h->nr_overcommit_huge_pages = tmp;
  2596. spin_unlock(&hugetlb_lock);
  2597. }
  2598. out:
  2599. return ret;
  2600. }
  2601. #endif /* CONFIG_SYSCTL */
  2602. void hugetlb_report_meminfo(struct seq_file *m)
  2603. {
  2604. struct hstate *h = &default_hstate;
  2605. if (!hugepages_supported())
  2606. return;
  2607. seq_printf(m,
  2608. "HugePages_Total: %5lu\n"
  2609. "HugePages_Free: %5lu\n"
  2610. "HugePages_Rsvd: %5lu\n"
  2611. "HugePages_Surp: %5lu\n"
  2612. "Hugepagesize: %8lu kB\n",
  2613. h->nr_huge_pages,
  2614. h->free_huge_pages,
  2615. h->resv_huge_pages,
  2616. h->surplus_huge_pages,
  2617. 1UL << (huge_page_order(h) + PAGE_SHIFT - 10));
  2618. }
  2619. int hugetlb_report_node_meminfo(int nid, char *buf)
  2620. {
  2621. struct hstate *h = &default_hstate;
  2622. if (!hugepages_supported())
  2623. return 0;
  2624. return sprintf(buf,
  2625. "Node %d HugePages_Total: %5u\n"
  2626. "Node %d HugePages_Free: %5u\n"
  2627. "Node %d HugePages_Surp: %5u\n",
  2628. nid, h->nr_huge_pages_node[nid],
  2629. nid, h->free_huge_pages_node[nid],
  2630. nid, h->surplus_huge_pages_node[nid]);
  2631. }
  2632. void hugetlb_show_meminfo(void)
  2633. {
  2634. struct hstate *h;
  2635. int nid;
  2636. if (!hugepages_supported())
  2637. return;
  2638. for_each_node_state(nid, N_MEMORY)
  2639. for_each_hstate(h)
  2640. pr_info("Node %d hugepages_total=%u hugepages_free=%u hugepages_surp=%u hugepages_size=%lukB\n",
  2641. nid,
  2642. h->nr_huge_pages_node[nid],
  2643. h->free_huge_pages_node[nid],
  2644. h->surplus_huge_pages_node[nid],
  2645. 1UL << (huge_page_order(h) + PAGE_SHIFT - 10));
  2646. }
  2647. void hugetlb_report_usage(struct seq_file *m, struct mm_struct *mm)
  2648. {
  2649. seq_printf(m, "HugetlbPages:\t%8lu kB\n",
  2650. atomic_long_read(&mm->hugetlb_usage) << (PAGE_SHIFT - 10));
  2651. }
  2652. /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
  2653. unsigned long hugetlb_total_pages(void)
  2654. {
  2655. struct hstate *h;
  2656. unsigned long nr_total_pages = 0;
  2657. for_each_hstate(h)
  2658. nr_total_pages += h->nr_huge_pages * pages_per_huge_page(h);
  2659. return nr_total_pages;
  2660. }
  2661. static int hugetlb_acct_memory(struct hstate *h, long delta)
  2662. {
  2663. int ret = -ENOMEM;
  2664. spin_lock(&hugetlb_lock);
  2665. /*
  2666. * When cpuset is configured, it breaks the strict hugetlb page
  2667. * reservation as the accounting is done on a global variable. Such
  2668. * reservation is completely rubbish in the presence of cpuset because
  2669. * the reservation is not checked against page availability for the
  2670. * current cpuset. Application can still potentially OOM'ed by kernel
  2671. * with lack of free htlb page in cpuset that the task is in.
  2672. * Attempt to enforce strict accounting with cpuset is almost
  2673. * impossible (or too ugly) because cpuset is too fluid that
  2674. * task or memory node can be dynamically moved between cpusets.
  2675. *
  2676. * The change of semantics for shared hugetlb mapping with cpuset is
  2677. * undesirable. However, in order to preserve some of the semantics,
  2678. * we fall back to check against current free page availability as
  2679. * a best attempt and hopefully to minimize the impact of changing
  2680. * semantics that cpuset has.
  2681. */
  2682. if (delta > 0) {
  2683. if (gather_surplus_pages(h, delta) < 0)
  2684. goto out;
  2685. if (delta > cpuset_mems_nr(h->free_huge_pages_node)) {
  2686. return_unused_surplus_pages(h, delta);
  2687. goto out;
  2688. }
  2689. }
  2690. ret = 0;
  2691. if (delta < 0)
  2692. return_unused_surplus_pages(h, (unsigned long) -delta);
  2693. out:
  2694. spin_unlock(&hugetlb_lock);
  2695. return ret;
  2696. }
  2697. static void hugetlb_vm_op_open(struct vm_area_struct *vma)
  2698. {
  2699. struct resv_map *resv = vma_resv_map(vma);
  2700. /*
  2701. * This new VMA should share its siblings reservation map if present.
  2702. * The VMA will only ever have a valid reservation map pointer where
  2703. * it is being copied for another still existing VMA. As that VMA
  2704. * has a reference to the reservation map it cannot disappear until
  2705. * after this open call completes. It is therefore safe to take a
  2706. * new reference here without additional locking.
  2707. */
  2708. if (resv && is_vma_resv_set(vma, HPAGE_RESV_OWNER))
  2709. kref_get(&resv->refs);
  2710. }
  2711. static void hugetlb_vm_op_close(struct vm_area_struct *vma)
  2712. {
  2713. struct hstate *h = hstate_vma(vma);
  2714. struct resv_map *resv = vma_resv_map(vma);
  2715. struct hugepage_subpool *spool = subpool_vma(vma);
  2716. unsigned long reserve, start, end;
  2717. long gbl_reserve;
  2718. if (!resv || !is_vma_resv_set(vma, HPAGE_RESV_OWNER))
  2719. return;
  2720. start = vma_hugecache_offset(h, vma, vma->vm_start);
  2721. end = vma_hugecache_offset(h, vma, vma->vm_end);
  2722. reserve = (end - start) - region_count(resv, start, end);
  2723. kref_put(&resv->refs, resv_map_release);
  2724. if (reserve) {
  2725. /*
  2726. * Decrement reserve counts. The global reserve count may be
  2727. * adjusted if the subpool has a minimum size.
  2728. */
  2729. gbl_reserve = hugepage_subpool_put_pages(spool, reserve);
  2730. hugetlb_acct_memory(h, -gbl_reserve);
  2731. }
  2732. }
  2733. /*
  2734. * We cannot handle pagefaults against hugetlb pages at all. They cause
  2735. * handle_mm_fault() to try to instantiate regular-sized pages in the
  2736. * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
  2737. * this far.
  2738. */
  2739. static int hugetlb_vm_op_fault(struct vm_fault *vmf)
  2740. {
  2741. BUG();
  2742. return 0;
  2743. }
  2744. const struct vm_operations_struct hugetlb_vm_ops = {
  2745. .fault = hugetlb_vm_op_fault,
  2746. .open = hugetlb_vm_op_open,
  2747. .close = hugetlb_vm_op_close,
  2748. };
  2749. static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
  2750. int writable)
  2751. {
  2752. pte_t entry;
  2753. if (writable) {
  2754. entry = huge_pte_mkwrite(huge_pte_mkdirty(mk_huge_pte(page,
  2755. vma->vm_page_prot)));
  2756. } else {
  2757. entry = huge_pte_wrprotect(mk_huge_pte(page,
  2758. vma->vm_page_prot));
  2759. }
  2760. entry = pte_mkyoung(entry);
  2761. entry = pte_mkhuge(entry);
  2762. entry = arch_make_huge_pte(entry, vma, page, writable);
  2763. return entry;
  2764. }
  2765. static void set_huge_ptep_writable(struct vm_area_struct *vma,
  2766. unsigned long address, pte_t *ptep)
  2767. {
  2768. pte_t entry;
  2769. entry = huge_pte_mkwrite(huge_pte_mkdirty(huge_ptep_get(ptep)));
  2770. if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1))
  2771. update_mmu_cache(vma, address, ptep);
  2772. }
  2773. bool is_hugetlb_entry_migration(pte_t pte)
  2774. {
  2775. swp_entry_t swp;
  2776. if (huge_pte_none(pte) || pte_present(pte))
  2777. return false;
  2778. swp = pte_to_swp_entry(pte);
  2779. if (non_swap_entry(swp) && is_migration_entry(swp))
  2780. return true;
  2781. else
  2782. return false;
  2783. }
  2784. static int is_hugetlb_entry_hwpoisoned(pte_t pte)
  2785. {
  2786. swp_entry_t swp;
  2787. if (huge_pte_none(pte) || pte_present(pte))
  2788. return 0;
  2789. swp = pte_to_swp_entry(pte);
  2790. if (non_swap_entry(swp) && is_hwpoison_entry(swp))
  2791. return 1;
  2792. else
  2793. return 0;
  2794. }
  2795. int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
  2796. struct vm_area_struct *vma)
  2797. {
  2798. pte_t *src_pte, *dst_pte, entry;
  2799. struct page *ptepage;
  2800. unsigned long addr;
  2801. int cow;
  2802. struct hstate *h = hstate_vma(vma);
  2803. unsigned long sz = huge_page_size(h);
  2804. unsigned long mmun_start; /* For mmu_notifiers */
  2805. unsigned long mmun_end; /* For mmu_notifiers */
  2806. int ret = 0;
  2807. cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
  2808. mmun_start = vma->vm_start;
  2809. mmun_end = vma->vm_end;
  2810. if (cow)
  2811. mmu_notifier_invalidate_range_start(src, mmun_start, mmun_end);
  2812. for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) {
  2813. spinlock_t *src_ptl, *dst_ptl;
  2814. src_pte = huge_pte_offset(src, addr);
  2815. if (!src_pte)
  2816. continue;
  2817. dst_pte = huge_pte_alloc(dst, addr, sz);
  2818. if (!dst_pte) {
  2819. ret = -ENOMEM;
  2820. break;
  2821. }
  2822. /* If the pagetables are shared don't copy or take references */
  2823. if (dst_pte == src_pte)
  2824. continue;
  2825. dst_ptl = huge_pte_lock(h, dst, dst_pte);
  2826. src_ptl = huge_pte_lockptr(h, src, src_pte);
  2827. spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
  2828. entry = huge_ptep_get(src_pte);
  2829. if (huge_pte_none(entry)) { /* skip none entry */
  2830. ;
  2831. } else if (unlikely(is_hugetlb_entry_migration(entry) ||
  2832. is_hugetlb_entry_hwpoisoned(entry))) {
  2833. swp_entry_t swp_entry = pte_to_swp_entry(entry);
  2834. if (is_write_migration_entry(swp_entry) && cow) {
  2835. /*
  2836. * COW mappings require pages in both
  2837. * parent and child to be set to read.
  2838. */
  2839. make_migration_entry_read(&swp_entry);
  2840. entry = swp_entry_to_pte(swp_entry);
  2841. set_huge_pte_at(src, addr, src_pte, entry);
  2842. }
  2843. set_huge_pte_at(dst, addr, dst_pte, entry);
  2844. } else {
  2845. if (cow) {
  2846. huge_ptep_set_wrprotect(src, addr, src_pte);
  2847. mmu_notifier_invalidate_range(src, mmun_start,
  2848. mmun_end);
  2849. }
  2850. entry = huge_ptep_get(src_pte);
  2851. ptepage = pte_page(entry);
  2852. get_page(ptepage);
  2853. page_dup_rmap(ptepage, true);
  2854. set_huge_pte_at(dst, addr, dst_pte, entry);
  2855. hugetlb_count_add(pages_per_huge_page(h), dst);
  2856. }
  2857. spin_unlock(src_ptl);
  2858. spin_unlock(dst_ptl);
  2859. }
  2860. if (cow)
  2861. mmu_notifier_invalidate_range_end(src, mmun_start, mmun_end);
  2862. return ret;
  2863. }
  2864. void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
  2865. unsigned long start, unsigned long end,
  2866. struct page *ref_page)
  2867. {
  2868. struct mm_struct *mm = vma->vm_mm;
  2869. unsigned long address;
  2870. pte_t *ptep;
  2871. pte_t pte;
  2872. spinlock_t *ptl;
  2873. struct page *page;
  2874. struct hstate *h = hstate_vma(vma);
  2875. unsigned long sz = huge_page_size(h);
  2876. const unsigned long mmun_start = start; /* For mmu_notifiers */
  2877. const unsigned long mmun_end = end; /* For mmu_notifiers */
  2878. WARN_ON(!is_vm_hugetlb_page(vma));
  2879. BUG_ON(start & ~huge_page_mask(h));
  2880. BUG_ON(end & ~huge_page_mask(h));
  2881. /*
  2882. * This is a hugetlb vma, all the pte entries should point
  2883. * to huge page.
  2884. */
  2885. tlb_remove_check_page_size_change(tlb, sz);
  2886. tlb_start_vma(tlb, vma);
  2887. mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
  2888. address = start;
  2889. for (; address < end; address += sz) {
  2890. ptep = huge_pte_offset(mm, address);
  2891. if (!ptep)
  2892. continue;
  2893. ptl = huge_pte_lock(h, mm, ptep);
  2894. if (huge_pmd_unshare(mm, &address, ptep)) {
  2895. spin_unlock(ptl);
  2896. continue;
  2897. }
  2898. pte = huge_ptep_get(ptep);
  2899. if (huge_pte_none(pte)) {
  2900. spin_unlock(ptl);
  2901. continue;
  2902. }
  2903. /*
  2904. * Migrating hugepage or HWPoisoned hugepage is already
  2905. * unmapped and its refcount is dropped, so just clear pte here.
  2906. */
  2907. if (unlikely(!pte_present(pte))) {
  2908. huge_pte_clear(mm, address, ptep);
  2909. spin_unlock(ptl);
  2910. continue;
  2911. }
  2912. page = pte_page(pte);
  2913. /*
  2914. * If a reference page is supplied, it is because a specific
  2915. * page is being unmapped, not a range. Ensure the page we
  2916. * are about to unmap is the actual page of interest.
  2917. */
  2918. if (ref_page) {
  2919. if (page != ref_page) {
  2920. spin_unlock(ptl);
  2921. continue;
  2922. }
  2923. /*
  2924. * Mark the VMA as having unmapped its page so that
  2925. * future faults in this VMA will fail rather than
  2926. * looking like data was lost
  2927. */
  2928. set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
  2929. }
  2930. pte = huge_ptep_get_and_clear(mm, address, ptep);
  2931. tlb_remove_huge_tlb_entry(h, tlb, ptep, address);
  2932. if (huge_pte_dirty(pte))
  2933. set_page_dirty(page);
  2934. hugetlb_count_sub(pages_per_huge_page(h), mm);
  2935. page_remove_rmap(page, true);
  2936. spin_unlock(ptl);
  2937. tlb_remove_page_size(tlb, page, huge_page_size(h));
  2938. /*
  2939. * Bail out after unmapping reference page if supplied
  2940. */
  2941. if (ref_page)
  2942. break;
  2943. }
  2944. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  2945. tlb_end_vma(tlb, vma);
  2946. }
  2947. void __unmap_hugepage_range_final(struct mmu_gather *tlb,
  2948. struct vm_area_struct *vma, unsigned long start,
  2949. unsigned long end, struct page *ref_page)
  2950. {
  2951. __unmap_hugepage_range(tlb, vma, start, end, ref_page);
  2952. /*
  2953. * Clear this flag so that x86's huge_pmd_share page_table_shareable
  2954. * test will fail on a vma being torn down, and not grab a page table
  2955. * on its way out. We're lucky that the flag has such an appropriate
  2956. * name, and can in fact be safely cleared here. We could clear it
  2957. * before the __unmap_hugepage_range above, but all that's necessary
  2958. * is to clear it before releasing the i_mmap_rwsem. This works
  2959. * because in the context this is called, the VMA is about to be
  2960. * destroyed and the i_mmap_rwsem is held.
  2961. */
  2962. vma->vm_flags &= ~VM_MAYSHARE;
  2963. }
  2964. void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
  2965. unsigned long end, struct page *ref_page)
  2966. {
  2967. struct mm_struct *mm;
  2968. struct mmu_gather tlb;
  2969. mm = vma->vm_mm;
  2970. tlb_gather_mmu(&tlb, mm, start, end);
  2971. __unmap_hugepage_range(&tlb, vma, start, end, ref_page);
  2972. tlb_finish_mmu(&tlb, start, end);
  2973. }
  2974. /*
  2975. * This is called when the original mapper is failing to COW a MAP_PRIVATE
  2976. * mappping it owns the reserve page for. The intention is to unmap the page
  2977. * from other VMAs and let the children be SIGKILLed if they are faulting the
  2978. * same region.
  2979. */
  2980. static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,
  2981. struct page *page, unsigned long address)
  2982. {
  2983. struct hstate *h = hstate_vma(vma);
  2984. struct vm_area_struct *iter_vma;
  2985. struct address_space *mapping;
  2986. pgoff_t pgoff;
  2987. /*
  2988. * vm_pgoff is in PAGE_SIZE units, hence the different calculation
  2989. * from page cache lookup which is in HPAGE_SIZE units.
  2990. */
  2991. address = address & huge_page_mask(h);
  2992. pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) +
  2993. vma->vm_pgoff;
  2994. mapping = vma->vm_file->f_mapping;
  2995. /*
  2996. * Take the mapping lock for the duration of the table walk. As
  2997. * this mapping should be shared between all the VMAs,
  2998. * __unmap_hugepage_range() is called as the lock is already held
  2999. */
  3000. i_mmap_lock_write(mapping);
  3001. vma_interval_tree_foreach(iter_vma, &mapping->i_mmap, pgoff, pgoff) {
  3002. /* Do not unmap the current VMA */
  3003. if (iter_vma == vma)
  3004. continue;
  3005. /*
  3006. * Shared VMAs have their own reserves and do not affect
  3007. * MAP_PRIVATE accounting but it is possible that a shared
  3008. * VMA is using the same page so check and skip such VMAs.
  3009. */
  3010. if (iter_vma->vm_flags & VM_MAYSHARE)
  3011. continue;
  3012. /*
  3013. * Unmap the page from other VMAs without their own reserves.
  3014. * They get marked to be SIGKILLed if they fault in these
  3015. * areas. This is because a future no-page fault on this VMA
  3016. * could insert a zeroed page instead of the data existing
  3017. * from the time of fork. This would look like data corruption
  3018. */
  3019. if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
  3020. unmap_hugepage_range(iter_vma, address,
  3021. address + huge_page_size(h), page);
  3022. }
  3023. i_mmap_unlock_write(mapping);
  3024. }
  3025. /*
  3026. * Hugetlb_cow() should be called with page lock of the original hugepage held.
  3027. * Called with hugetlb_instantiation_mutex held and pte_page locked so we
  3028. * cannot race with other handlers or page migration.
  3029. * Keep the pte_same checks anyway to make transition from the mutex easier.
  3030. */
  3031. static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
  3032. unsigned long address, pte_t *ptep,
  3033. struct page *pagecache_page, spinlock_t *ptl)
  3034. {
  3035. pte_t pte;
  3036. struct hstate *h = hstate_vma(vma);
  3037. struct page *old_page, *new_page;
  3038. int ret = 0, outside_reserve = 0;
  3039. unsigned long mmun_start; /* For mmu_notifiers */
  3040. unsigned long mmun_end; /* For mmu_notifiers */
  3041. pte = huge_ptep_get(ptep);
  3042. old_page = pte_page(pte);
  3043. retry_avoidcopy:
  3044. /* If no-one else is actually using this page, avoid the copy
  3045. * and just make the page writable */
  3046. if (page_mapcount(old_page) == 1 && PageAnon(old_page)) {
  3047. page_move_anon_rmap(old_page, vma);
  3048. set_huge_ptep_writable(vma, address, ptep);
  3049. return 0;
  3050. }
  3051. /*
  3052. * If the process that created a MAP_PRIVATE mapping is about to
  3053. * perform a COW due to a shared page count, attempt to satisfy
  3054. * the allocation without using the existing reserves. The pagecache
  3055. * page is used to determine if the reserve at this address was
  3056. * consumed or not. If reserves were used, a partial faulted mapping
  3057. * at the time of fork() could consume its reserves on COW instead
  3058. * of the full address range.
  3059. */
  3060. if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
  3061. old_page != pagecache_page)
  3062. outside_reserve = 1;
  3063. get_page(old_page);
  3064. /*
  3065. * Drop page table lock as buddy allocator may be called. It will
  3066. * be acquired again before returning to the caller, as expected.
  3067. */
  3068. spin_unlock(ptl);
  3069. new_page = alloc_huge_page(vma, address, outside_reserve);
  3070. if (IS_ERR(new_page)) {
  3071. /*
  3072. * If a process owning a MAP_PRIVATE mapping fails to COW,
  3073. * it is due to references held by a child and an insufficient
  3074. * huge page pool. To guarantee the original mappers
  3075. * reliability, unmap the page from child processes. The child
  3076. * may get SIGKILLed if it later faults.
  3077. */
  3078. if (outside_reserve) {
  3079. put_page(old_page);
  3080. BUG_ON(huge_pte_none(pte));
  3081. unmap_ref_private(mm, vma, old_page, address);
  3082. BUG_ON(huge_pte_none(pte));
  3083. spin_lock(ptl);
  3084. ptep = huge_pte_offset(mm, address & huge_page_mask(h));
  3085. if (likely(ptep &&
  3086. pte_same(huge_ptep_get(ptep), pte)))
  3087. goto retry_avoidcopy;
  3088. /*
  3089. * race occurs while re-acquiring page table
  3090. * lock, and our job is done.
  3091. */
  3092. return 0;
  3093. }
  3094. ret = (PTR_ERR(new_page) == -ENOMEM) ?
  3095. VM_FAULT_OOM : VM_FAULT_SIGBUS;
  3096. goto out_release_old;
  3097. }
  3098. /*
  3099. * When the original hugepage is shared one, it does not have
  3100. * anon_vma prepared.
  3101. */
  3102. if (unlikely(anon_vma_prepare(vma))) {
  3103. ret = VM_FAULT_OOM;
  3104. goto out_release_all;
  3105. }
  3106. copy_user_huge_page(new_page, old_page, address, vma,
  3107. pages_per_huge_page(h));
  3108. __SetPageUptodate(new_page);
  3109. set_page_huge_active(new_page);
  3110. mmun_start = address & huge_page_mask(h);
  3111. mmun_end = mmun_start + huge_page_size(h);
  3112. mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
  3113. /*
  3114. * Retake the page table lock to check for racing updates
  3115. * before the page tables are altered
  3116. */
  3117. spin_lock(ptl);
  3118. ptep = huge_pte_offset(mm, address & huge_page_mask(h));
  3119. if (likely(ptep && pte_same(huge_ptep_get(ptep), pte))) {
  3120. ClearPagePrivate(new_page);
  3121. /* Break COW */
  3122. huge_ptep_clear_flush(vma, address, ptep);
  3123. mmu_notifier_invalidate_range(mm, mmun_start, mmun_end);
  3124. set_huge_pte_at(mm, address, ptep,
  3125. make_huge_pte(vma, new_page, 1));
  3126. page_remove_rmap(old_page, true);
  3127. hugepage_add_new_anon_rmap(new_page, vma, address);
  3128. /* Make the old page be freed below */
  3129. new_page = old_page;
  3130. }
  3131. spin_unlock(ptl);
  3132. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  3133. out_release_all:
  3134. restore_reserve_on_error(h, vma, address, new_page);
  3135. put_page(new_page);
  3136. out_release_old:
  3137. put_page(old_page);
  3138. spin_lock(ptl); /* Caller expects lock to be held */
  3139. return ret;
  3140. }
  3141. /* Return the pagecache page at a given address within a VMA */
  3142. static struct page *hugetlbfs_pagecache_page(struct hstate *h,
  3143. struct vm_area_struct *vma, unsigned long address)
  3144. {
  3145. struct address_space *mapping;
  3146. pgoff_t idx;
  3147. mapping = vma->vm_file->f_mapping;
  3148. idx = vma_hugecache_offset(h, vma, address);
  3149. return find_lock_page(mapping, idx);
  3150. }
  3151. /*
  3152. * Return whether there is a pagecache page to back given address within VMA.
  3153. * Caller follow_hugetlb_page() holds page_table_lock so we cannot lock_page.
  3154. */
  3155. static bool hugetlbfs_pagecache_present(struct hstate *h,
  3156. struct vm_area_struct *vma, unsigned long address)
  3157. {
  3158. struct address_space *mapping;
  3159. pgoff_t idx;
  3160. struct page *page;
  3161. mapping = vma->vm_file->f_mapping;
  3162. idx = vma_hugecache_offset(h, vma, address);
  3163. page = find_get_page(mapping, idx);
  3164. if (page)
  3165. put_page(page);
  3166. return page != NULL;
  3167. }
  3168. int huge_add_to_page_cache(struct page *page, struct address_space *mapping,
  3169. pgoff_t idx)
  3170. {
  3171. struct inode *inode = mapping->host;
  3172. struct hstate *h = hstate_inode(inode);
  3173. int err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
  3174. if (err)
  3175. return err;
  3176. ClearPagePrivate(page);
  3177. spin_lock(&inode->i_lock);
  3178. inode->i_blocks += blocks_per_huge_page(h);
  3179. spin_unlock(&inode->i_lock);
  3180. return 0;
  3181. }
  3182. static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
  3183. struct address_space *mapping, pgoff_t idx,
  3184. unsigned long address, pte_t *ptep, unsigned int flags)
  3185. {
  3186. struct hstate *h = hstate_vma(vma);
  3187. int ret = VM_FAULT_SIGBUS;
  3188. int anon_rmap = 0;
  3189. unsigned long size;
  3190. struct page *page;
  3191. pte_t new_pte;
  3192. spinlock_t *ptl;
  3193. /*
  3194. * Currently, we are forced to kill the process in the event the
  3195. * original mapper has unmapped pages from the child due to a failed
  3196. * COW. Warn that such a situation has occurred as it may not be obvious
  3197. */
  3198. if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
  3199. pr_warn_ratelimited("PID %d killed due to inadequate hugepage pool\n",
  3200. current->pid);
  3201. return ret;
  3202. }
  3203. /*
  3204. * Use page lock to guard against racing truncation
  3205. * before we get page_table_lock.
  3206. */
  3207. retry:
  3208. page = find_lock_page(mapping, idx);
  3209. if (!page) {
  3210. size = i_size_read(mapping->host) >> huge_page_shift(h);
  3211. if (idx >= size)
  3212. goto out;
  3213. /*
  3214. * Check for page in userfault range
  3215. */
  3216. if (userfaultfd_missing(vma)) {
  3217. u32 hash;
  3218. struct vm_fault vmf = {
  3219. .vma = vma,
  3220. .address = address,
  3221. .flags = flags,
  3222. /*
  3223. * Hard to debug if it ends up being
  3224. * used by a callee that assumes
  3225. * something about the other
  3226. * uninitialized fields... same as in
  3227. * memory.c
  3228. */
  3229. };
  3230. /*
  3231. * hugetlb_fault_mutex must be dropped before
  3232. * handling userfault. Reacquire after handling
  3233. * fault to make calling code simpler.
  3234. */
  3235. hash = hugetlb_fault_mutex_hash(h, mm, vma, mapping,
  3236. idx, address);
  3237. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  3238. ret = handle_userfault(&vmf, VM_UFFD_MISSING);
  3239. mutex_lock(&hugetlb_fault_mutex_table[hash]);
  3240. goto out;
  3241. }
  3242. page = alloc_huge_page(vma, address, 0);
  3243. if (IS_ERR(page)) {
  3244. ret = PTR_ERR(page);
  3245. if (ret == -ENOMEM)
  3246. ret = VM_FAULT_OOM;
  3247. else
  3248. ret = VM_FAULT_SIGBUS;
  3249. goto out;
  3250. }
  3251. clear_huge_page(page, address, pages_per_huge_page(h));
  3252. __SetPageUptodate(page);
  3253. set_page_huge_active(page);
  3254. if (vma->vm_flags & VM_MAYSHARE) {
  3255. int err = huge_add_to_page_cache(page, mapping, idx);
  3256. if (err) {
  3257. put_page(page);
  3258. if (err == -EEXIST)
  3259. goto retry;
  3260. goto out;
  3261. }
  3262. } else {
  3263. lock_page(page);
  3264. if (unlikely(anon_vma_prepare(vma))) {
  3265. ret = VM_FAULT_OOM;
  3266. goto backout_unlocked;
  3267. }
  3268. anon_rmap = 1;
  3269. }
  3270. } else {
  3271. /*
  3272. * If memory error occurs between mmap() and fault, some process
  3273. * don't have hwpoisoned swap entry for errored virtual address.
  3274. * So we need to block hugepage fault by PG_hwpoison bit check.
  3275. */
  3276. if (unlikely(PageHWPoison(page))) {
  3277. ret = VM_FAULT_HWPOISON |
  3278. VM_FAULT_SET_HINDEX(hstate_index(h));
  3279. goto backout_unlocked;
  3280. }
  3281. }
  3282. /*
  3283. * If we are going to COW a private mapping later, we examine the
  3284. * pending reservations for this page now. This will ensure that
  3285. * any allocations necessary to record that reservation occur outside
  3286. * the spinlock.
  3287. */
  3288. if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
  3289. if (vma_needs_reservation(h, vma, address) < 0) {
  3290. ret = VM_FAULT_OOM;
  3291. goto backout_unlocked;
  3292. }
  3293. /* Just decrements count, does not deallocate */
  3294. vma_end_reservation(h, vma, address);
  3295. }
  3296. ptl = huge_pte_lock(h, mm, ptep);
  3297. size = i_size_read(mapping->host) >> huge_page_shift(h);
  3298. if (idx >= size)
  3299. goto backout;
  3300. ret = 0;
  3301. if (!huge_pte_none(huge_ptep_get(ptep)))
  3302. goto backout;
  3303. if (anon_rmap) {
  3304. ClearPagePrivate(page);
  3305. hugepage_add_new_anon_rmap(page, vma, address);
  3306. } else
  3307. page_dup_rmap(page, true);
  3308. new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
  3309. && (vma->vm_flags & VM_SHARED)));
  3310. set_huge_pte_at(mm, address, ptep, new_pte);
  3311. hugetlb_count_add(pages_per_huge_page(h), mm);
  3312. if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
  3313. /* Optimization, do the COW without a second fault */
  3314. ret = hugetlb_cow(mm, vma, address, ptep, page, ptl);
  3315. }
  3316. spin_unlock(ptl);
  3317. unlock_page(page);
  3318. out:
  3319. return ret;
  3320. backout:
  3321. spin_unlock(ptl);
  3322. backout_unlocked:
  3323. unlock_page(page);
  3324. restore_reserve_on_error(h, vma, address, page);
  3325. put_page(page);
  3326. goto out;
  3327. }
  3328. #ifdef CONFIG_SMP
  3329. u32 hugetlb_fault_mutex_hash(struct hstate *h, struct mm_struct *mm,
  3330. struct vm_area_struct *vma,
  3331. struct address_space *mapping,
  3332. pgoff_t idx, unsigned long address)
  3333. {
  3334. unsigned long key[2];
  3335. u32 hash;
  3336. if (vma->vm_flags & VM_SHARED) {
  3337. key[0] = (unsigned long) mapping;
  3338. key[1] = idx;
  3339. } else {
  3340. key[0] = (unsigned long) mm;
  3341. key[1] = address >> huge_page_shift(h);
  3342. }
  3343. hash = jhash2((u32 *)&key, sizeof(key)/sizeof(u32), 0);
  3344. return hash & (num_fault_mutexes - 1);
  3345. }
  3346. #else
  3347. /*
  3348. * For uniprocesor systems we always use a single mutex, so just
  3349. * return 0 and avoid the hashing overhead.
  3350. */
  3351. u32 hugetlb_fault_mutex_hash(struct hstate *h, struct mm_struct *mm,
  3352. struct vm_area_struct *vma,
  3353. struct address_space *mapping,
  3354. pgoff_t idx, unsigned long address)
  3355. {
  3356. return 0;
  3357. }
  3358. #endif
  3359. int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  3360. unsigned long address, unsigned int flags)
  3361. {
  3362. pte_t *ptep, entry;
  3363. spinlock_t *ptl;
  3364. int ret;
  3365. u32 hash;
  3366. pgoff_t idx;
  3367. struct page *page = NULL;
  3368. struct page *pagecache_page = NULL;
  3369. struct hstate *h = hstate_vma(vma);
  3370. struct address_space *mapping;
  3371. int need_wait_lock = 0;
  3372. address &= huge_page_mask(h);
  3373. ptep = huge_pte_offset(mm, address);
  3374. if (ptep) {
  3375. entry = huge_ptep_get(ptep);
  3376. if (unlikely(is_hugetlb_entry_migration(entry))) {
  3377. migration_entry_wait_huge(vma, mm, ptep);
  3378. return 0;
  3379. } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry)))
  3380. return VM_FAULT_HWPOISON_LARGE |
  3381. VM_FAULT_SET_HINDEX(hstate_index(h));
  3382. } else {
  3383. ptep = huge_pte_alloc(mm, address, huge_page_size(h));
  3384. if (!ptep)
  3385. return VM_FAULT_OOM;
  3386. }
  3387. mapping = vma->vm_file->f_mapping;
  3388. idx = vma_hugecache_offset(h, vma, address);
  3389. /*
  3390. * Serialize hugepage allocation and instantiation, so that we don't
  3391. * get spurious allocation failures if two CPUs race to instantiate
  3392. * the same page in the page cache.
  3393. */
  3394. hash = hugetlb_fault_mutex_hash(h, mm, vma, mapping, idx, address);
  3395. mutex_lock(&hugetlb_fault_mutex_table[hash]);
  3396. entry = huge_ptep_get(ptep);
  3397. if (huge_pte_none(entry)) {
  3398. ret = hugetlb_no_page(mm, vma, mapping, idx, address, ptep, flags);
  3399. goto out_mutex;
  3400. }
  3401. ret = 0;
  3402. /*
  3403. * entry could be a migration/hwpoison entry at this point, so this
  3404. * check prevents the kernel from going below assuming that we have
  3405. * a active hugepage in pagecache. This goto expects the 2nd page fault,
  3406. * and is_hugetlb_entry_(migration|hwpoisoned) check will properly
  3407. * handle it.
  3408. */
  3409. if (!pte_present(entry))
  3410. goto out_mutex;
  3411. /*
  3412. * If we are going to COW the mapping later, we examine the pending
  3413. * reservations for this page now. This will ensure that any
  3414. * allocations necessary to record that reservation occur outside the
  3415. * spinlock. For private mappings, we also lookup the pagecache
  3416. * page now as it is used to determine if a reservation has been
  3417. * consumed.
  3418. */
  3419. if ((flags & FAULT_FLAG_WRITE) && !huge_pte_write(entry)) {
  3420. if (vma_needs_reservation(h, vma, address) < 0) {
  3421. ret = VM_FAULT_OOM;
  3422. goto out_mutex;
  3423. }
  3424. /* Just decrements count, does not deallocate */
  3425. vma_end_reservation(h, vma, address);
  3426. if (!(vma->vm_flags & VM_MAYSHARE))
  3427. pagecache_page = hugetlbfs_pagecache_page(h,
  3428. vma, address);
  3429. }
  3430. ptl = huge_pte_lock(h, mm, ptep);
  3431. /* Check for a racing update before calling hugetlb_cow */
  3432. if (unlikely(!pte_same(entry, huge_ptep_get(ptep))))
  3433. goto out_ptl;
  3434. /*
  3435. * hugetlb_cow() requires page locks of pte_page(entry) and
  3436. * pagecache_page, so here we need take the former one
  3437. * when page != pagecache_page or !pagecache_page.
  3438. */
  3439. page = pte_page(entry);
  3440. if (page != pagecache_page)
  3441. if (!trylock_page(page)) {
  3442. need_wait_lock = 1;
  3443. goto out_ptl;
  3444. }
  3445. get_page(page);
  3446. if (flags & FAULT_FLAG_WRITE) {
  3447. if (!huge_pte_write(entry)) {
  3448. ret = hugetlb_cow(mm, vma, address, ptep,
  3449. pagecache_page, ptl);
  3450. goto out_put_page;
  3451. }
  3452. entry = huge_pte_mkdirty(entry);
  3453. }
  3454. entry = pte_mkyoung(entry);
  3455. if (huge_ptep_set_access_flags(vma, address, ptep, entry,
  3456. flags & FAULT_FLAG_WRITE))
  3457. update_mmu_cache(vma, address, ptep);
  3458. out_put_page:
  3459. if (page != pagecache_page)
  3460. unlock_page(page);
  3461. put_page(page);
  3462. out_ptl:
  3463. spin_unlock(ptl);
  3464. if (pagecache_page) {
  3465. unlock_page(pagecache_page);
  3466. put_page(pagecache_page);
  3467. }
  3468. out_mutex:
  3469. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  3470. /*
  3471. * Generally it's safe to hold refcount during waiting page lock. But
  3472. * here we just wait to defer the next page fault to avoid busy loop and
  3473. * the page is not used after unlocked before returning from the current
  3474. * page fault. So we are safe from accessing freed page, even if we wait
  3475. * here without taking refcount.
  3476. */
  3477. if (need_wait_lock)
  3478. wait_on_page_locked(page);
  3479. return ret;
  3480. }
  3481. /*
  3482. * Used by userfaultfd UFFDIO_COPY. Based on mcopy_atomic_pte with
  3483. * modifications for huge pages.
  3484. */
  3485. int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm,
  3486. pte_t *dst_pte,
  3487. struct vm_area_struct *dst_vma,
  3488. unsigned long dst_addr,
  3489. unsigned long src_addr,
  3490. struct page **pagep)
  3491. {
  3492. int vm_shared = dst_vma->vm_flags & VM_SHARED;
  3493. struct hstate *h = hstate_vma(dst_vma);
  3494. pte_t _dst_pte;
  3495. spinlock_t *ptl;
  3496. int ret;
  3497. struct page *page;
  3498. if (!*pagep) {
  3499. ret = -ENOMEM;
  3500. page = alloc_huge_page(dst_vma, dst_addr, 0);
  3501. if (IS_ERR(page))
  3502. goto out;
  3503. ret = copy_huge_page_from_user(page,
  3504. (const void __user *) src_addr,
  3505. pages_per_huge_page(h), false);
  3506. /* fallback to copy_from_user outside mmap_sem */
  3507. if (unlikely(ret)) {
  3508. ret = -EFAULT;
  3509. *pagep = page;
  3510. /* don't free the page */
  3511. goto out;
  3512. }
  3513. } else {
  3514. page = *pagep;
  3515. *pagep = NULL;
  3516. }
  3517. /*
  3518. * The memory barrier inside __SetPageUptodate makes sure that
  3519. * preceding stores to the page contents become visible before
  3520. * the set_pte_at() write.
  3521. */
  3522. __SetPageUptodate(page);
  3523. set_page_huge_active(page);
  3524. /*
  3525. * If shared, add to page cache
  3526. */
  3527. if (vm_shared) {
  3528. struct address_space *mapping = dst_vma->vm_file->f_mapping;
  3529. pgoff_t idx = vma_hugecache_offset(h, dst_vma, dst_addr);
  3530. ret = huge_add_to_page_cache(page, mapping, idx);
  3531. if (ret)
  3532. goto out_release_nounlock;
  3533. }
  3534. ptl = huge_pte_lockptr(h, dst_mm, dst_pte);
  3535. spin_lock(ptl);
  3536. ret = -EEXIST;
  3537. if (!huge_pte_none(huge_ptep_get(dst_pte)))
  3538. goto out_release_unlock;
  3539. if (vm_shared) {
  3540. page_dup_rmap(page, true);
  3541. } else {
  3542. ClearPagePrivate(page);
  3543. hugepage_add_new_anon_rmap(page, dst_vma, dst_addr);
  3544. }
  3545. _dst_pte = make_huge_pte(dst_vma, page, dst_vma->vm_flags & VM_WRITE);
  3546. if (dst_vma->vm_flags & VM_WRITE)
  3547. _dst_pte = huge_pte_mkdirty(_dst_pte);
  3548. _dst_pte = pte_mkyoung(_dst_pte);
  3549. set_huge_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
  3550. (void)huge_ptep_set_access_flags(dst_vma, dst_addr, dst_pte, _dst_pte,
  3551. dst_vma->vm_flags & VM_WRITE);
  3552. hugetlb_count_add(pages_per_huge_page(h), dst_mm);
  3553. /* No need to invalidate - it was non-present before */
  3554. update_mmu_cache(dst_vma, dst_addr, dst_pte);
  3555. spin_unlock(ptl);
  3556. if (vm_shared)
  3557. unlock_page(page);
  3558. ret = 0;
  3559. out:
  3560. return ret;
  3561. out_release_unlock:
  3562. spin_unlock(ptl);
  3563. out_release_nounlock:
  3564. if (vm_shared)
  3565. unlock_page(page);
  3566. put_page(page);
  3567. goto out;
  3568. }
  3569. long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
  3570. struct page **pages, struct vm_area_struct **vmas,
  3571. unsigned long *position, unsigned long *nr_pages,
  3572. long i, unsigned int flags, int *nonblocking)
  3573. {
  3574. unsigned long pfn_offset;
  3575. unsigned long vaddr = *position;
  3576. unsigned long remainder = *nr_pages;
  3577. struct hstate *h = hstate_vma(vma);
  3578. while (vaddr < vma->vm_end && remainder) {
  3579. pte_t *pte;
  3580. spinlock_t *ptl = NULL;
  3581. int absent;
  3582. struct page *page;
  3583. /*
  3584. * If we have a pending SIGKILL, don't keep faulting pages and
  3585. * potentially allocating memory.
  3586. */
  3587. if (unlikely(fatal_signal_pending(current))) {
  3588. remainder = 0;
  3589. break;
  3590. }
  3591. /*
  3592. * Some archs (sparc64, sh*) have multiple pte_ts to
  3593. * each hugepage. We have to make sure we get the
  3594. * first, for the page indexing below to work.
  3595. *
  3596. * Note that page table lock is not held when pte is null.
  3597. */
  3598. pte = huge_pte_offset(mm, vaddr & huge_page_mask(h));
  3599. if (pte)
  3600. ptl = huge_pte_lock(h, mm, pte);
  3601. absent = !pte || huge_pte_none(huge_ptep_get(pte));
  3602. /*
  3603. * When coredumping, it suits get_dump_page if we just return
  3604. * an error where there's an empty slot with no huge pagecache
  3605. * to back it. This way, we avoid allocating a hugepage, and
  3606. * the sparse dumpfile avoids allocating disk blocks, but its
  3607. * huge holes still show up with zeroes where they need to be.
  3608. */
  3609. if (absent && (flags & FOLL_DUMP) &&
  3610. !hugetlbfs_pagecache_present(h, vma, vaddr)) {
  3611. if (pte)
  3612. spin_unlock(ptl);
  3613. remainder = 0;
  3614. break;
  3615. }
  3616. /*
  3617. * We need call hugetlb_fault for both hugepages under migration
  3618. * (in which case hugetlb_fault waits for the migration,) and
  3619. * hwpoisoned hugepages (in which case we need to prevent the
  3620. * caller from accessing to them.) In order to do this, we use
  3621. * here is_swap_pte instead of is_hugetlb_entry_migration and
  3622. * is_hugetlb_entry_hwpoisoned. This is because it simply covers
  3623. * both cases, and because we can't follow correct pages
  3624. * directly from any kind of swap entries.
  3625. */
  3626. if (absent || is_swap_pte(huge_ptep_get(pte)) ||
  3627. ((flags & FOLL_WRITE) &&
  3628. !huge_pte_write(huge_ptep_get(pte)))) {
  3629. int ret;
  3630. unsigned int fault_flags = 0;
  3631. if (pte)
  3632. spin_unlock(ptl);
  3633. if (flags & FOLL_WRITE)
  3634. fault_flags |= FAULT_FLAG_WRITE;
  3635. if (nonblocking)
  3636. fault_flags |= FAULT_FLAG_ALLOW_RETRY;
  3637. if (flags & FOLL_NOWAIT)
  3638. fault_flags |= FAULT_FLAG_ALLOW_RETRY |
  3639. FAULT_FLAG_RETRY_NOWAIT;
  3640. if (flags & FOLL_TRIED) {
  3641. VM_WARN_ON_ONCE(fault_flags &
  3642. FAULT_FLAG_ALLOW_RETRY);
  3643. fault_flags |= FAULT_FLAG_TRIED;
  3644. }
  3645. ret = hugetlb_fault(mm, vma, vaddr, fault_flags);
  3646. if (ret & VM_FAULT_ERROR) {
  3647. int err = vm_fault_to_errno(ret, flags);
  3648. if (err)
  3649. return err;
  3650. remainder = 0;
  3651. break;
  3652. }
  3653. if (ret & VM_FAULT_RETRY) {
  3654. if (nonblocking)
  3655. *nonblocking = 0;
  3656. *nr_pages = 0;
  3657. /*
  3658. * VM_FAULT_RETRY must not return an
  3659. * error, it will return zero
  3660. * instead.
  3661. *
  3662. * No need to update "position" as the
  3663. * caller will not check it after
  3664. * *nr_pages is set to 0.
  3665. */
  3666. return i;
  3667. }
  3668. continue;
  3669. }
  3670. pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT;
  3671. page = pte_page(huge_ptep_get(pte));
  3672. same_page:
  3673. if (pages) {
  3674. pages[i] = mem_map_offset(page, pfn_offset);
  3675. get_page(pages[i]);
  3676. }
  3677. if (vmas)
  3678. vmas[i] = vma;
  3679. vaddr += PAGE_SIZE;
  3680. ++pfn_offset;
  3681. --remainder;
  3682. ++i;
  3683. if (vaddr < vma->vm_end && remainder &&
  3684. pfn_offset < pages_per_huge_page(h)) {
  3685. /*
  3686. * We use pfn_offset to avoid touching the pageframes
  3687. * of this compound page.
  3688. */
  3689. goto same_page;
  3690. }
  3691. spin_unlock(ptl);
  3692. }
  3693. *nr_pages = remainder;
  3694. /*
  3695. * setting position is actually required only if remainder is
  3696. * not zero but it's faster not to add a "if (remainder)"
  3697. * branch.
  3698. */
  3699. *position = vaddr;
  3700. return i ? i : -EFAULT;
  3701. }
  3702. #ifndef __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE
  3703. /*
  3704. * ARCHes with special requirements for evicting HUGETLB backing TLB entries can
  3705. * implement this.
  3706. */
  3707. #define flush_hugetlb_tlb_range(vma, addr, end) flush_tlb_range(vma, addr, end)
  3708. #endif
  3709. unsigned long hugetlb_change_protection(struct vm_area_struct *vma,
  3710. unsigned long address, unsigned long end, pgprot_t newprot)
  3711. {
  3712. struct mm_struct *mm = vma->vm_mm;
  3713. unsigned long start = address;
  3714. pte_t *ptep;
  3715. pte_t pte;
  3716. struct hstate *h = hstate_vma(vma);
  3717. unsigned long pages = 0;
  3718. BUG_ON(address >= end);
  3719. flush_cache_range(vma, address, end);
  3720. mmu_notifier_invalidate_range_start(mm, start, end);
  3721. i_mmap_lock_write(vma->vm_file->f_mapping);
  3722. for (; address < end; address += huge_page_size(h)) {
  3723. spinlock_t *ptl;
  3724. ptep = huge_pte_offset(mm, address);
  3725. if (!ptep)
  3726. continue;
  3727. ptl = huge_pte_lock(h, mm, ptep);
  3728. if (huge_pmd_unshare(mm, &address, ptep)) {
  3729. pages++;
  3730. spin_unlock(ptl);
  3731. continue;
  3732. }
  3733. pte = huge_ptep_get(ptep);
  3734. if (unlikely(is_hugetlb_entry_hwpoisoned(pte))) {
  3735. spin_unlock(ptl);
  3736. continue;
  3737. }
  3738. if (unlikely(is_hugetlb_entry_migration(pte))) {
  3739. swp_entry_t entry = pte_to_swp_entry(pte);
  3740. if (is_write_migration_entry(entry)) {
  3741. pte_t newpte;
  3742. make_migration_entry_read(&entry);
  3743. newpte = swp_entry_to_pte(entry);
  3744. set_huge_pte_at(mm, address, ptep, newpte);
  3745. pages++;
  3746. }
  3747. spin_unlock(ptl);
  3748. continue;
  3749. }
  3750. if (!huge_pte_none(pte)) {
  3751. pte = huge_ptep_get_and_clear(mm, address, ptep);
  3752. pte = pte_mkhuge(huge_pte_modify(pte, newprot));
  3753. pte = arch_make_huge_pte(pte, vma, NULL, 0);
  3754. set_huge_pte_at(mm, address, ptep, pte);
  3755. pages++;
  3756. }
  3757. spin_unlock(ptl);
  3758. }
  3759. /*
  3760. * Must flush TLB before releasing i_mmap_rwsem: x86's huge_pmd_unshare
  3761. * may have cleared our pud entry and done put_page on the page table:
  3762. * once we release i_mmap_rwsem, another task can do the final put_page
  3763. * and that page table be reused and filled with junk.
  3764. */
  3765. flush_hugetlb_tlb_range(vma, start, end);
  3766. mmu_notifier_invalidate_range(mm, start, end);
  3767. i_mmap_unlock_write(vma->vm_file->f_mapping);
  3768. mmu_notifier_invalidate_range_end(mm, start, end);
  3769. return pages << h->order;
  3770. }
  3771. int hugetlb_reserve_pages(struct inode *inode,
  3772. long from, long to,
  3773. struct vm_area_struct *vma,
  3774. vm_flags_t vm_flags)
  3775. {
  3776. long ret, chg;
  3777. struct hstate *h = hstate_inode(inode);
  3778. struct hugepage_subpool *spool = subpool_inode(inode);
  3779. struct resv_map *resv_map;
  3780. long gbl_reserve;
  3781. /*
  3782. * Only apply hugepage reservation if asked. At fault time, an
  3783. * attempt will be made for VM_NORESERVE to allocate a page
  3784. * without using reserves
  3785. */
  3786. if (vm_flags & VM_NORESERVE)
  3787. return 0;
  3788. /*
  3789. * Shared mappings base their reservation on the number of pages that
  3790. * are already allocated on behalf of the file. Private mappings need
  3791. * to reserve the full area even if read-only as mprotect() may be
  3792. * called to make the mapping read-write. Assume !vma is a shm mapping
  3793. */
  3794. if (!vma || vma->vm_flags & VM_MAYSHARE) {
  3795. resv_map = inode_resv_map(inode);
  3796. chg = region_chg(resv_map, from, to);
  3797. } else {
  3798. resv_map = resv_map_alloc();
  3799. if (!resv_map)
  3800. return -ENOMEM;
  3801. chg = to - from;
  3802. set_vma_resv_map(vma, resv_map);
  3803. set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
  3804. }
  3805. if (chg < 0) {
  3806. ret = chg;
  3807. goto out_err;
  3808. }
  3809. /*
  3810. * There must be enough pages in the subpool for the mapping. If
  3811. * the subpool has a minimum size, there may be some global
  3812. * reservations already in place (gbl_reserve).
  3813. */
  3814. gbl_reserve = hugepage_subpool_get_pages(spool, chg);
  3815. if (gbl_reserve < 0) {
  3816. ret = -ENOSPC;
  3817. goto out_err;
  3818. }
  3819. /*
  3820. * Check enough hugepages are available for the reservation.
  3821. * Hand the pages back to the subpool if there are not
  3822. */
  3823. ret = hugetlb_acct_memory(h, gbl_reserve);
  3824. if (ret < 0) {
  3825. /* put back original number of pages, chg */
  3826. (void)hugepage_subpool_put_pages(spool, chg);
  3827. goto out_err;
  3828. }
  3829. /*
  3830. * Account for the reservations made. Shared mappings record regions
  3831. * that have reservations as they are shared by multiple VMAs.
  3832. * When the last VMA disappears, the region map says how much
  3833. * the reservation was and the page cache tells how much of
  3834. * the reservation was consumed. Private mappings are per-VMA and
  3835. * only the consumed reservations are tracked. When the VMA
  3836. * disappears, the original reservation is the VMA size and the
  3837. * consumed reservations are stored in the map. Hence, nothing
  3838. * else has to be done for private mappings here
  3839. */
  3840. if (!vma || vma->vm_flags & VM_MAYSHARE) {
  3841. long add = region_add(resv_map, from, to);
  3842. if (unlikely(chg > add)) {
  3843. /*
  3844. * pages in this range were added to the reserve
  3845. * map between region_chg and region_add. This
  3846. * indicates a race with alloc_huge_page. Adjust
  3847. * the subpool and reserve counts modified above
  3848. * based on the difference.
  3849. */
  3850. long rsv_adjust;
  3851. rsv_adjust = hugepage_subpool_put_pages(spool,
  3852. chg - add);
  3853. hugetlb_acct_memory(h, -rsv_adjust);
  3854. }
  3855. }
  3856. return 0;
  3857. out_err:
  3858. if (!vma || vma->vm_flags & VM_MAYSHARE)
  3859. /* Don't call region_abort if region_chg failed */
  3860. if (chg >= 0)
  3861. region_abort(resv_map, from, to);
  3862. if (vma && is_vma_resv_set(vma, HPAGE_RESV_OWNER))
  3863. kref_put(&resv_map->refs, resv_map_release);
  3864. return ret;
  3865. }
  3866. long hugetlb_unreserve_pages(struct inode *inode, long start, long end,
  3867. long freed)
  3868. {
  3869. struct hstate *h = hstate_inode(inode);
  3870. struct resv_map *resv_map = inode_resv_map(inode);
  3871. long chg = 0;
  3872. struct hugepage_subpool *spool = subpool_inode(inode);
  3873. long gbl_reserve;
  3874. if (resv_map) {
  3875. chg = region_del(resv_map, start, end);
  3876. /*
  3877. * region_del() can fail in the rare case where a region
  3878. * must be split and another region descriptor can not be
  3879. * allocated. If end == LONG_MAX, it will not fail.
  3880. */
  3881. if (chg < 0)
  3882. return chg;
  3883. }
  3884. spin_lock(&inode->i_lock);
  3885. inode->i_blocks -= (blocks_per_huge_page(h) * freed);
  3886. spin_unlock(&inode->i_lock);
  3887. /*
  3888. * If the subpool has a minimum size, the number of global
  3889. * reservations to be released may be adjusted.
  3890. */
  3891. gbl_reserve = hugepage_subpool_put_pages(spool, (chg - freed));
  3892. hugetlb_acct_memory(h, -gbl_reserve);
  3893. return 0;
  3894. }
  3895. #ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE
  3896. static unsigned long page_table_shareable(struct vm_area_struct *svma,
  3897. struct vm_area_struct *vma,
  3898. unsigned long addr, pgoff_t idx)
  3899. {
  3900. unsigned long saddr = ((idx - svma->vm_pgoff) << PAGE_SHIFT) +
  3901. svma->vm_start;
  3902. unsigned long sbase = saddr & PUD_MASK;
  3903. unsigned long s_end = sbase + PUD_SIZE;
  3904. /* Allow segments to share if only one is marked locked */
  3905. unsigned long vm_flags = vma->vm_flags & VM_LOCKED_CLEAR_MASK;
  3906. unsigned long svm_flags = svma->vm_flags & VM_LOCKED_CLEAR_MASK;
  3907. /*
  3908. * match the virtual addresses, permission and the alignment of the
  3909. * page table page.
  3910. */
  3911. if (pmd_index(addr) != pmd_index(saddr) ||
  3912. vm_flags != svm_flags ||
  3913. sbase < svma->vm_start || svma->vm_end < s_end)
  3914. return 0;
  3915. return saddr;
  3916. }
  3917. static bool vma_shareable(struct vm_area_struct *vma, unsigned long addr)
  3918. {
  3919. unsigned long base = addr & PUD_MASK;
  3920. unsigned long end = base + PUD_SIZE;
  3921. /*
  3922. * check on proper vm_flags and page table alignment
  3923. */
  3924. if (vma->vm_flags & VM_MAYSHARE &&
  3925. vma->vm_start <= base && end <= vma->vm_end)
  3926. return true;
  3927. return false;
  3928. }
  3929. /*
  3930. * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc()
  3931. * and returns the corresponding pte. While this is not necessary for the
  3932. * !shared pmd case because we can allocate the pmd later as well, it makes the
  3933. * code much cleaner. pmd allocation is essential for the shared case because
  3934. * pud has to be populated inside the same i_mmap_rwsem section - otherwise
  3935. * racing tasks could either miss the sharing (see huge_pte_offset) or select a
  3936. * bad pmd for sharing.
  3937. */
  3938. pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)
  3939. {
  3940. struct vm_area_struct *vma = find_vma(mm, addr);
  3941. struct address_space *mapping = vma->vm_file->f_mapping;
  3942. pgoff_t idx = ((addr - vma->vm_start) >> PAGE_SHIFT) +
  3943. vma->vm_pgoff;
  3944. struct vm_area_struct *svma;
  3945. unsigned long saddr;
  3946. pte_t *spte = NULL;
  3947. pte_t *pte;
  3948. spinlock_t *ptl;
  3949. if (!vma_shareable(vma, addr))
  3950. return (pte_t *)pmd_alloc(mm, pud, addr);
  3951. i_mmap_lock_write(mapping);
  3952. vma_interval_tree_foreach(svma, &mapping->i_mmap, idx, idx) {
  3953. if (svma == vma)
  3954. continue;
  3955. saddr = page_table_shareable(svma, vma, addr, idx);
  3956. if (saddr) {
  3957. spte = huge_pte_offset(svma->vm_mm, saddr);
  3958. if (spte) {
  3959. get_page(virt_to_page(spte));
  3960. break;
  3961. }
  3962. }
  3963. }
  3964. if (!spte)
  3965. goto out;
  3966. ptl = huge_pte_lock(hstate_vma(vma), mm, spte);
  3967. if (pud_none(*pud)) {
  3968. pud_populate(mm, pud,
  3969. (pmd_t *)((unsigned long)spte & PAGE_MASK));
  3970. mm_inc_nr_pmds(mm);
  3971. } else {
  3972. put_page(virt_to_page(spte));
  3973. }
  3974. spin_unlock(ptl);
  3975. out:
  3976. pte = (pte_t *)pmd_alloc(mm, pud, addr);
  3977. i_mmap_unlock_write(mapping);
  3978. return pte;
  3979. }
  3980. /*
  3981. * unmap huge page backed by shared pte.
  3982. *
  3983. * Hugetlb pte page is ref counted at the time of mapping. If pte is shared
  3984. * indicated by page_count > 1, unmap is achieved by clearing pud and
  3985. * decrementing the ref count. If count == 1, the pte page is not shared.
  3986. *
  3987. * called with page table lock held.
  3988. *
  3989. * returns: 1 successfully unmapped a shared pte page
  3990. * 0 the underlying pte page is not shared, or it is the last user
  3991. */
  3992. int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
  3993. {
  3994. pgd_t *pgd = pgd_offset(mm, *addr);
  3995. p4d_t *p4d = p4d_offset(pgd, *addr);
  3996. pud_t *pud = pud_offset(p4d, *addr);
  3997. BUG_ON(page_count(virt_to_page(ptep)) == 0);
  3998. if (page_count(virt_to_page(ptep)) == 1)
  3999. return 0;
  4000. pud_clear(pud);
  4001. put_page(virt_to_page(ptep));
  4002. mm_dec_nr_pmds(mm);
  4003. *addr = ALIGN(*addr, HPAGE_SIZE * PTRS_PER_PTE) - HPAGE_SIZE;
  4004. return 1;
  4005. }
  4006. #define want_pmd_share() (1)
  4007. #else /* !CONFIG_ARCH_WANT_HUGE_PMD_SHARE */
  4008. pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)
  4009. {
  4010. return NULL;
  4011. }
  4012. int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
  4013. {
  4014. return 0;
  4015. }
  4016. #define want_pmd_share() (0)
  4017. #endif /* CONFIG_ARCH_WANT_HUGE_PMD_SHARE */
  4018. #ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB
  4019. pte_t *huge_pte_alloc(struct mm_struct *mm,
  4020. unsigned long addr, unsigned long sz)
  4021. {
  4022. pgd_t *pgd;
  4023. p4d_t *p4d;
  4024. pud_t *pud;
  4025. pte_t *pte = NULL;
  4026. pgd = pgd_offset(mm, addr);
  4027. p4d = p4d_offset(pgd, addr);
  4028. pud = pud_alloc(mm, p4d, addr);
  4029. if (pud) {
  4030. if (sz == PUD_SIZE) {
  4031. pte = (pte_t *)pud;
  4032. } else {
  4033. BUG_ON(sz != PMD_SIZE);
  4034. if (want_pmd_share() && pud_none(*pud))
  4035. pte = huge_pmd_share(mm, addr, pud);
  4036. else
  4037. pte = (pte_t *)pmd_alloc(mm, pud, addr);
  4038. }
  4039. }
  4040. BUG_ON(pte && pte_present(*pte) && !pte_huge(*pte));
  4041. return pte;
  4042. }
  4043. pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
  4044. {
  4045. pgd_t *pgd;
  4046. p4d_t *p4d;
  4047. pud_t *pud;
  4048. pmd_t *pmd;
  4049. pgd = pgd_offset(mm, addr);
  4050. if (!pgd_present(*pgd))
  4051. return NULL;
  4052. p4d = p4d_offset(pgd, addr);
  4053. if (!p4d_present(*p4d))
  4054. return NULL;
  4055. pud = pud_offset(p4d, addr);
  4056. if (!pud_present(*pud))
  4057. return NULL;
  4058. if (pud_huge(*pud))
  4059. return (pte_t *)pud;
  4060. pmd = pmd_offset(pud, addr);
  4061. return (pte_t *) pmd;
  4062. }
  4063. #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */
  4064. /*
  4065. * These functions are overwritable if your architecture needs its own
  4066. * behavior.
  4067. */
  4068. struct page * __weak
  4069. follow_huge_addr(struct mm_struct *mm, unsigned long address,
  4070. int write)
  4071. {
  4072. return ERR_PTR(-EINVAL);
  4073. }
  4074. struct page * __weak
  4075. follow_huge_pd(struct vm_area_struct *vma,
  4076. unsigned long address, hugepd_t hpd, int flags, int pdshift)
  4077. {
  4078. WARN(1, "hugepd follow called with no support for hugepage directory format\n");
  4079. return NULL;
  4080. }
  4081. struct page * __weak
  4082. follow_huge_pmd(struct mm_struct *mm, unsigned long address,
  4083. pmd_t *pmd, int flags)
  4084. {
  4085. struct page *page = NULL;
  4086. spinlock_t *ptl;
  4087. pte_t pte;
  4088. retry:
  4089. ptl = pmd_lockptr(mm, pmd);
  4090. spin_lock(ptl);
  4091. /*
  4092. * make sure that the address range covered by this pmd is not
  4093. * unmapped from other threads.
  4094. */
  4095. if (!pmd_huge(*pmd))
  4096. goto out;
  4097. pte = huge_ptep_get((pte_t *)pmd);
  4098. if (pte_present(pte)) {
  4099. page = pmd_page(*pmd) + ((address & ~PMD_MASK) >> PAGE_SHIFT);
  4100. if (flags & FOLL_GET)
  4101. get_page(page);
  4102. } else {
  4103. if (is_hugetlb_entry_migration(pte)) {
  4104. spin_unlock(ptl);
  4105. __migration_entry_wait(mm, (pte_t *)pmd, ptl);
  4106. goto retry;
  4107. }
  4108. /*
  4109. * hwpoisoned entry is treated as no_page_table in
  4110. * follow_page_mask().
  4111. */
  4112. }
  4113. out:
  4114. spin_unlock(ptl);
  4115. return page;
  4116. }
  4117. struct page * __weak
  4118. follow_huge_pud(struct mm_struct *mm, unsigned long address,
  4119. pud_t *pud, int flags)
  4120. {
  4121. if (flags & FOLL_GET)
  4122. return NULL;
  4123. return pte_page(*(pte_t *)pud) + ((address & ~PUD_MASK) >> PAGE_SHIFT);
  4124. }
  4125. struct page * __weak
  4126. follow_huge_pgd(struct mm_struct *mm, unsigned long address, pgd_t *pgd, int flags)
  4127. {
  4128. if (flags & FOLL_GET)
  4129. return NULL;
  4130. return pte_page(*(pte_t *)pgd) + ((address & ~PGDIR_MASK) >> PAGE_SHIFT);
  4131. }
  4132. #ifdef CONFIG_MEMORY_FAILURE
  4133. /*
  4134. * This function is called from memory failure code.
  4135. */
  4136. int dequeue_hwpoisoned_huge_page(struct page *hpage)
  4137. {
  4138. struct hstate *h = page_hstate(hpage);
  4139. int nid = page_to_nid(hpage);
  4140. int ret = -EBUSY;
  4141. spin_lock(&hugetlb_lock);
  4142. /*
  4143. * Just checking !page_huge_active is not enough, because that could be
  4144. * an isolated/hwpoisoned hugepage (which have >0 refcount).
  4145. */
  4146. if (!page_huge_active(hpage) && !page_count(hpage)) {
  4147. /*
  4148. * Hwpoisoned hugepage isn't linked to activelist or freelist,
  4149. * but dangling hpage->lru can trigger list-debug warnings
  4150. * (this happens when we call unpoison_memory() on it),
  4151. * so let it point to itself with list_del_init().
  4152. */
  4153. list_del_init(&hpage->lru);
  4154. set_page_refcounted(hpage);
  4155. h->free_huge_pages--;
  4156. h->free_huge_pages_node[nid]--;
  4157. ret = 0;
  4158. }
  4159. spin_unlock(&hugetlb_lock);
  4160. return ret;
  4161. }
  4162. #endif
  4163. bool isolate_huge_page(struct page *page, struct list_head *list)
  4164. {
  4165. bool ret = true;
  4166. VM_BUG_ON_PAGE(!PageHead(page), page);
  4167. spin_lock(&hugetlb_lock);
  4168. if (!page_huge_active(page) || !get_page_unless_zero(page)) {
  4169. ret = false;
  4170. goto unlock;
  4171. }
  4172. clear_page_huge_active(page);
  4173. list_move_tail(&page->lru, list);
  4174. unlock:
  4175. spin_unlock(&hugetlb_lock);
  4176. return ret;
  4177. }
  4178. void putback_active_hugepage(struct page *page)
  4179. {
  4180. VM_BUG_ON_PAGE(!PageHead(page), page);
  4181. spin_lock(&hugetlb_lock);
  4182. set_page_huge_active(page);
  4183. list_move_tail(&page->lru, &(page_hstate(page))->hugepage_activelist);
  4184. spin_unlock(&hugetlb_lock);
  4185. put_page(page);
  4186. }