hugetlb.c 94 KB

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