hugetlb.c 89 KB

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