hugetlb.c 77 KB

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