hugetlb.c 84 KB

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