hugetlb.c 118 KB

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