hugetlb.c 71 KB

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