hugetlb.c 75 KB

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