hugetlb.c 63 KB

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