hugetlb.c 62 KB

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