hugetlb.c 56 KB

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