hugetlb.c 79 KB

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