hugetlb.c 118 KB

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