hugetlb.c 77 KB

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