hugetlb.c 103 KB

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