hugetlb.c 107 KB

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