hugetlb.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  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/nodemask.h>
  13. #include <linux/pagemap.h>
  14. #include <linux/mempolicy.h>
  15. #include <linux/cpuset.h>
  16. #include <linux/mutex.h>
  17. #include <asm/page.h>
  18. #include <asm/pgtable.h>
  19. #include <linux/hugetlb.h>
  20. #include "internal.h"
  21. const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL;
  22. static unsigned long nr_huge_pages, free_huge_pages, resv_huge_pages;
  23. static unsigned long surplus_huge_pages;
  24. static unsigned long nr_overcommit_huge_pages;
  25. unsigned long max_huge_pages;
  26. unsigned long sysctl_overcommit_huge_pages;
  27. static struct list_head hugepage_freelists[MAX_NUMNODES];
  28. static unsigned int nr_huge_pages_node[MAX_NUMNODES];
  29. static unsigned int free_huge_pages_node[MAX_NUMNODES];
  30. static unsigned int surplus_huge_pages_node[MAX_NUMNODES];
  31. static gfp_t htlb_alloc_mask = GFP_HIGHUSER;
  32. unsigned long hugepages_treat_as_movable;
  33. static int hugetlb_next_nid;
  34. /*
  35. * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages
  36. */
  37. static DEFINE_SPINLOCK(hugetlb_lock);
  38. /*
  39. * Region tracking -- allows tracking of reservations and instantiated pages
  40. * across the pages in a mapping.
  41. */
  42. struct file_region {
  43. struct list_head link;
  44. long from;
  45. long to;
  46. };
  47. static long region_add(struct list_head *head, long f, long t)
  48. {
  49. struct file_region *rg, *nrg, *trg;
  50. /* Locate the region we are either in or before. */
  51. list_for_each_entry(rg, head, link)
  52. if (f <= rg->to)
  53. break;
  54. /* Round our left edge to the current segment if it encloses us. */
  55. if (f > rg->from)
  56. f = rg->from;
  57. /* Check for and consume any regions we now overlap with. */
  58. nrg = rg;
  59. list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
  60. if (&rg->link == head)
  61. break;
  62. if (rg->from > t)
  63. break;
  64. /* If this area reaches higher then extend our area to
  65. * include it completely. If this is not the first area
  66. * which we intend to reuse, free it. */
  67. if (rg->to > t)
  68. t = rg->to;
  69. if (rg != nrg) {
  70. list_del(&rg->link);
  71. kfree(rg);
  72. }
  73. }
  74. nrg->from = f;
  75. nrg->to = t;
  76. return 0;
  77. }
  78. static long region_chg(struct list_head *head, long f, long t)
  79. {
  80. struct file_region *rg, *nrg;
  81. long chg = 0;
  82. /* Locate the region we are before or in. */
  83. list_for_each_entry(rg, head, link)
  84. if (f <= rg->to)
  85. break;
  86. /* If we are below the current region then a new region is required.
  87. * Subtle, allocate a new region at the position but make it zero
  88. * size such that we can guarantee to record the reservation. */
  89. if (&rg->link == head || t < rg->from) {
  90. nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
  91. if (!nrg)
  92. return -ENOMEM;
  93. nrg->from = f;
  94. nrg->to = f;
  95. INIT_LIST_HEAD(&nrg->link);
  96. list_add(&nrg->link, rg->link.prev);
  97. return t - f;
  98. }
  99. /* Round our left edge to the current segment if it encloses us. */
  100. if (f > rg->from)
  101. f = rg->from;
  102. chg = t - f;
  103. /* Check for and consume any regions we now overlap with. */
  104. list_for_each_entry(rg, rg->link.prev, link) {
  105. if (&rg->link == head)
  106. break;
  107. if (rg->from > t)
  108. return chg;
  109. /* We overlap with this area, if it extends futher than
  110. * us then we must extend ourselves. Account for its
  111. * existing reservation. */
  112. if (rg->to > t) {
  113. chg += rg->to - t;
  114. t = rg->to;
  115. }
  116. chg -= rg->to - rg->from;
  117. }
  118. return chg;
  119. }
  120. static long region_truncate(struct list_head *head, long end)
  121. {
  122. struct file_region *rg, *trg;
  123. long chg = 0;
  124. /* Locate the region we are either in or before. */
  125. list_for_each_entry(rg, head, link)
  126. if (end <= rg->to)
  127. break;
  128. if (&rg->link == head)
  129. return 0;
  130. /* If we are in the middle of a region then adjust it. */
  131. if (end > rg->from) {
  132. chg = rg->to - end;
  133. rg->to = end;
  134. rg = list_entry(rg->link.next, typeof(*rg), link);
  135. }
  136. /* Drop any remaining regions. */
  137. list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
  138. if (&rg->link == head)
  139. break;
  140. chg += rg->to - rg->from;
  141. list_del(&rg->link);
  142. kfree(rg);
  143. }
  144. return chg;
  145. }
  146. /*
  147. * Convert the address within this vma to the page offset within
  148. * the mapping, in base page units.
  149. */
  150. static pgoff_t vma_page_offset(struct vm_area_struct *vma,
  151. unsigned long address)
  152. {
  153. return ((address - vma->vm_start) >> PAGE_SHIFT) +
  154. (vma->vm_pgoff >> PAGE_SHIFT);
  155. }
  156. /*
  157. * Convert the address within this vma to the page offset within
  158. * the mapping, in pagecache page units; huge pages here.
  159. */
  160. static pgoff_t vma_pagecache_offset(struct vm_area_struct *vma,
  161. unsigned long address)
  162. {
  163. return ((address - vma->vm_start) >> HPAGE_SHIFT) +
  164. (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
  165. }
  166. #define HPAGE_RESV_OWNER (1UL << (BITS_PER_LONG - 1))
  167. #define HPAGE_RESV_UNMAPPED (1UL << (BITS_PER_LONG - 2))
  168. #define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
  169. /*
  170. * These helpers are used to track how many pages are reserved for
  171. * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
  172. * is guaranteed to have their future faults succeed.
  173. *
  174. * With the exception of reset_vma_resv_huge_pages() which is called at fork(),
  175. * the reserve counters are updated with the hugetlb_lock held. It is safe
  176. * to reset the VMA at fork() time as it is not in use yet and there is no
  177. * chance of the global counters getting corrupted as a result of the values.
  178. */
  179. static unsigned long get_vma_private_data(struct vm_area_struct *vma)
  180. {
  181. return (unsigned long)vma->vm_private_data;
  182. }
  183. static void set_vma_private_data(struct vm_area_struct *vma,
  184. unsigned long value)
  185. {
  186. vma->vm_private_data = (void *)value;
  187. }
  188. static unsigned long vma_resv_huge_pages(struct vm_area_struct *vma)
  189. {
  190. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  191. if (!(vma->vm_flags & VM_SHARED))
  192. return get_vma_private_data(vma) & ~HPAGE_RESV_MASK;
  193. return 0;
  194. }
  195. static void set_vma_resv_huge_pages(struct vm_area_struct *vma,
  196. unsigned long reserve)
  197. {
  198. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  199. VM_BUG_ON(vma->vm_flags & VM_SHARED);
  200. set_vma_private_data(vma,
  201. (get_vma_private_data(vma) & HPAGE_RESV_MASK) | reserve);
  202. }
  203. static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
  204. {
  205. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  206. VM_BUG_ON(vma->vm_flags & VM_SHARED);
  207. set_vma_private_data(vma, get_vma_private_data(vma) | flags);
  208. }
  209. static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
  210. {
  211. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  212. return (get_vma_private_data(vma) & flag) != 0;
  213. }
  214. /* Decrement the reserved pages in the hugepage pool by one */
  215. static void decrement_hugepage_resv_vma(struct vm_area_struct *vma)
  216. {
  217. if (vma->vm_flags & VM_SHARED) {
  218. /* Shared mappings always use reserves */
  219. resv_huge_pages--;
  220. } else {
  221. /*
  222. * Only the process that called mmap() has reserves for
  223. * private mappings.
  224. */
  225. if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
  226. unsigned long flags, reserve;
  227. resv_huge_pages--;
  228. flags = (unsigned long)vma->vm_private_data &
  229. HPAGE_RESV_MASK;
  230. reserve = (unsigned long)vma->vm_private_data - 1;
  231. vma->vm_private_data = (void *)(reserve | flags);
  232. }
  233. }
  234. }
  235. /* Reset counters to 0 and clear all HPAGE_RESV_* flags */
  236. void reset_vma_resv_huge_pages(struct vm_area_struct *vma)
  237. {
  238. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  239. if (!(vma->vm_flags & VM_SHARED))
  240. vma->vm_private_data = (void *)0;
  241. }
  242. /* Returns true if the VMA has associated reserve pages */
  243. static int vma_has_private_reserves(struct vm_area_struct *vma)
  244. {
  245. if (vma->vm_flags & VM_SHARED)
  246. return 0;
  247. if (!vma_resv_huge_pages(vma))
  248. return 0;
  249. return 1;
  250. }
  251. static void clear_huge_page(struct page *page, unsigned long addr)
  252. {
  253. int i;
  254. might_sleep();
  255. for (i = 0; i < (HPAGE_SIZE/PAGE_SIZE); i++) {
  256. cond_resched();
  257. clear_user_highpage(page + i, addr + i * PAGE_SIZE);
  258. }
  259. }
  260. static void copy_huge_page(struct page *dst, struct page *src,
  261. unsigned long addr, struct vm_area_struct *vma)
  262. {
  263. int i;
  264. might_sleep();
  265. for (i = 0; i < HPAGE_SIZE/PAGE_SIZE; i++) {
  266. cond_resched();
  267. copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
  268. }
  269. }
  270. static void enqueue_huge_page(struct page *page)
  271. {
  272. int nid = page_to_nid(page);
  273. list_add(&page->lru, &hugepage_freelists[nid]);
  274. free_huge_pages++;
  275. free_huge_pages_node[nid]++;
  276. }
  277. static struct page *dequeue_huge_page(void)
  278. {
  279. int nid;
  280. struct page *page = NULL;
  281. for (nid = 0; nid < MAX_NUMNODES; ++nid) {
  282. if (!list_empty(&hugepage_freelists[nid])) {
  283. page = list_entry(hugepage_freelists[nid].next,
  284. struct page, lru);
  285. list_del(&page->lru);
  286. free_huge_pages--;
  287. free_huge_pages_node[nid]--;
  288. break;
  289. }
  290. }
  291. return page;
  292. }
  293. static struct page *dequeue_huge_page_vma(struct vm_area_struct *vma,
  294. unsigned long address, int avoid_reserve)
  295. {
  296. int nid;
  297. struct page *page = NULL;
  298. struct mempolicy *mpol;
  299. nodemask_t *nodemask;
  300. struct zonelist *zonelist = huge_zonelist(vma, address,
  301. htlb_alloc_mask, &mpol, &nodemask);
  302. struct zone *zone;
  303. struct zoneref *z;
  304. /*
  305. * A child process with MAP_PRIVATE mappings created by their parent
  306. * have no page reserves. This check ensures that reservations are
  307. * not "stolen". The child may still get SIGKILLed
  308. */
  309. if (!vma_has_private_reserves(vma) &&
  310. free_huge_pages - resv_huge_pages == 0)
  311. return NULL;
  312. /* If reserves cannot be used, ensure enough pages are in the pool */
  313. if (avoid_reserve && free_huge_pages - resv_huge_pages == 0)
  314. return NULL;
  315. for_each_zone_zonelist_nodemask(zone, z, zonelist,
  316. MAX_NR_ZONES - 1, nodemask) {
  317. nid = zone_to_nid(zone);
  318. if (cpuset_zone_allowed_softwall(zone, htlb_alloc_mask) &&
  319. !list_empty(&hugepage_freelists[nid])) {
  320. page = list_entry(hugepage_freelists[nid].next,
  321. struct page, lru);
  322. list_del(&page->lru);
  323. free_huge_pages--;
  324. free_huge_pages_node[nid]--;
  325. if (!avoid_reserve)
  326. decrement_hugepage_resv_vma(vma);
  327. break;
  328. }
  329. }
  330. mpol_cond_put(mpol);
  331. return page;
  332. }
  333. static void update_and_free_page(struct page *page)
  334. {
  335. int i;
  336. nr_huge_pages--;
  337. nr_huge_pages_node[page_to_nid(page)]--;
  338. for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++) {
  339. page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
  340. 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
  341. 1 << PG_private | 1<< PG_writeback);
  342. }
  343. set_compound_page_dtor(page, NULL);
  344. set_page_refcounted(page);
  345. arch_release_hugepage(page);
  346. __free_pages(page, HUGETLB_PAGE_ORDER);
  347. }
  348. static void free_huge_page(struct page *page)
  349. {
  350. int nid = page_to_nid(page);
  351. struct address_space *mapping;
  352. mapping = (struct address_space *) page_private(page);
  353. set_page_private(page, 0);
  354. BUG_ON(page_count(page));
  355. INIT_LIST_HEAD(&page->lru);
  356. spin_lock(&hugetlb_lock);
  357. if (surplus_huge_pages_node[nid]) {
  358. update_and_free_page(page);
  359. surplus_huge_pages--;
  360. surplus_huge_pages_node[nid]--;
  361. } else {
  362. enqueue_huge_page(page);
  363. }
  364. spin_unlock(&hugetlb_lock);
  365. if (mapping)
  366. hugetlb_put_quota(mapping, 1);
  367. }
  368. /*
  369. * Increment or decrement surplus_huge_pages. Keep node-specific counters
  370. * balanced by operating on them in a round-robin fashion.
  371. * Returns 1 if an adjustment was made.
  372. */
  373. static int adjust_pool_surplus(int delta)
  374. {
  375. static int prev_nid;
  376. int nid = prev_nid;
  377. int ret = 0;
  378. VM_BUG_ON(delta != -1 && delta != 1);
  379. do {
  380. nid = next_node(nid, node_online_map);
  381. if (nid == MAX_NUMNODES)
  382. nid = first_node(node_online_map);
  383. /* To shrink on this node, there must be a surplus page */
  384. if (delta < 0 && !surplus_huge_pages_node[nid])
  385. continue;
  386. /* Surplus cannot exceed the total number of pages */
  387. if (delta > 0 && surplus_huge_pages_node[nid] >=
  388. nr_huge_pages_node[nid])
  389. continue;
  390. surplus_huge_pages += delta;
  391. surplus_huge_pages_node[nid] += delta;
  392. ret = 1;
  393. break;
  394. } while (nid != prev_nid);
  395. prev_nid = nid;
  396. return ret;
  397. }
  398. static struct page *alloc_fresh_huge_page_node(int nid)
  399. {
  400. struct page *page;
  401. page = alloc_pages_node(nid,
  402. htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|
  403. __GFP_REPEAT|__GFP_NOWARN,
  404. HUGETLB_PAGE_ORDER);
  405. if (page) {
  406. if (arch_prepare_hugepage(page)) {
  407. __free_pages(page, HUGETLB_PAGE_ORDER);
  408. return NULL;
  409. }
  410. set_compound_page_dtor(page, free_huge_page);
  411. spin_lock(&hugetlb_lock);
  412. nr_huge_pages++;
  413. nr_huge_pages_node[nid]++;
  414. spin_unlock(&hugetlb_lock);
  415. put_page(page); /* free it into the hugepage allocator */
  416. }
  417. return page;
  418. }
  419. static int alloc_fresh_huge_page(void)
  420. {
  421. struct page *page;
  422. int start_nid;
  423. int next_nid;
  424. int ret = 0;
  425. start_nid = hugetlb_next_nid;
  426. do {
  427. page = alloc_fresh_huge_page_node(hugetlb_next_nid);
  428. if (page)
  429. ret = 1;
  430. /*
  431. * Use a helper variable to find the next node and then
  432. * copy it back to hugetlb_next_nid afterwards:
  433. * otherwise there's a window in which a racer might
  434. * pass invalid nid MAX_NUMNODES to alloc_pages_node.
  435. * But we don't need to use a spin_lock here: it really
  436. * doesn't matter if occasionally a racer chooses the
  437. * same nid as we do. Move nid forward in the mask even
  438. * if we just successfully allocated a hugepage so that
  439. * the next caller gets hugepages on the next node.
  440. */
  441. next_nid = next_node(hugetlb_next_nid, node_online_map);
  442. if (next_nid == MAX_NUMNODES)
  443. next_nid = first_node(node_online_map);
  444. hugetlb_next_nid = next_nid;
  445. } while (!page && hugetlb_next_nid != start_nid);
  446. if (ret)
  447. count_vm_event(HTLB_BUDDY_PGALLOC);
  448. else
  449. count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
  450. return ret;
  451. }
  452. static struct page *alloc_buddy_huge_page(struct vm_area_struct *vma,
  453. unsigned long address)
  454. {
  455. struct page *page;
  456. unsigned int nid;
  457. /*
  458. * Assume we will successfully allocate the surplus page to
  459. * prevent racing processes from causing the surplus to exceed
  460. * overcommit
  461. *
  462. * This however introduces a different race, where a process B
  463. * tries to grow the static hugepage pool while alloc_pages() is
  464. * called by process A. B will only examine the per-node
  465. * counters in determining if surplus huge pages can be
  466. * converted to normal huge pages in adjust_pool_surplus(). A
  467. * won't be able to increment the per-node counter, until the
  468. * lock is dropped by B, but B doesn't drop hugetlb_lock until
  469. * no more huge pages can be converted from surplus to normal
  470. * state (and doesn't try to convert again). Thus, we have a
  471. * case where a surplus huge page exists, the pool is grown, and
  472. * the surplus huge page still exists after, even though it
  473. * should just have been converted to a normal huge page. This
  474. * does not leak memory, though, as the hugepage will be freed
  475. * once it is out of use. It also does not allow the counters to
  476. * go out of whack in adjust_pool_surplus() as we don't modify
  477. * the node values until we've gotten the hugepage and only the
  478. * per-node value is checked there.
  479. */
  480. spin_lock(&hugetlb_lock);
  481. if (surplus_huge_pages >= nr_overcommit_huge_pages) {
  482. spin_unlock(&hugetlb_lock);
  483. return NULL;
  484. } else {
  485. nr_huge_pages++;
  486. surplus_huge_pages++;
  487. }
  488. spin_unlock(&hugetlb_lock);
  489. page = alloc_pages(htlb_alloc_mask|__GFP_COMP|
  490. __GFP_REPEAT|__GFP_NOWARN,
  491. HUGETLB_PAGE_ORDER);
  492. spin_lock(&hugetlb_lock);
  493. if (page) {
  494. /*
  495. * This page is now managed by the hugetlb allocator and has
  496. * no users -- drop the buddy allocator's reference.
  497. */
  498. put_page_testzero(page);
  499. VM_BUG_ON(page_count(page));
  500. nid = page_to_nid(page);
  501. set_compound_page_dtor(page, free_huge_page);
  502. /*
  503. * We incremented the global counters already
  504. */
  505. nr_huge_pages_node[nid]++;
  506. surplus_huge_pages_node[nid]++;
  507. __count_vm_event(HTLB_BUDDY_PGALLOC);
  508. } else {
  509. nr_huge_pages--;
  510. surplus_huge_pages--;
  511. __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
  512. }
  513. spin_unlock(&hugetlb_lock);
  514. return page;
  515. }
  516. /*
  517. * Increase the hugetlb pool such that it can accomodate a reservation
  518. * of size 'delta'.
  519. */
  520. static int gather_surplus_pages(int delta)
  521. {
  522. struct list_head surplus_list;
  523. struct page *page, *tmp;
  524. int ret, i;
  525. int needed, allocated;
  526. needed = (resv_huge_pages + delta) - free_huge_pages;
  527. if (needed <= 0) {
  528. resv_huge_pages += delta;
  529. return 0;
  530. }
  531. allocated = 0;
  532. INIT_LIST_HEAD(&surplus_list);
  533. ret = -ENOMEM;
  534. retry:
  535. spin_unlock(&hugetlb_lock);
  536. for (i = 0; i < needed; i++) {
  537. page = alloc_buddy_huge_page(NULL, 0);
  538. if (!page) {
  539. /*
  540. * We were not able to allocate enough pages to
  541. * satisfy the entire reservation so we free what
  542. * we've allocated so far.
  543. */
  544. spin_lock(&hugetlb_lock);
  545. needed = 0;
  546. goto free;
  547. }
  548. list_add(&page->lru, &surplus_list);
  549. }
  550. allocated += needed;
  551. /*
  552. * After retaking hugetlb_lock, we need to recalculate 'needed'
  553. * because either resv_huge_pages or free_huge_pages may have changed.
  554. */
  555. spin_lock(&hugetlb_lock);
  556. needed = (resv_huge_pages + delta) - (free_huge_pages + allocated);
  557. if (needed > 0)
  558. goto retry;
  559. /*
  560. * The surplus_list now contains _at_least_ the number of extra pages
  561. * needed to accomodate the reservation. Add the appropriate number
  562. * of pages to the hugetlb pool and free the extras back to the buddy
  563. * allocator. Commit the entire reservation here to prevent another
  564. * process from stealing the pages as they are added to the pool but
  565. * before they are reserved.
  566. */
  567. needed += allocated;
  568. resv_huge_pages += delta;
  569. ret = 0;
  570. free:
  571. /* Free the needed pages to the hugetlb pool */
  572. list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
  573. if ((--needed) < 0)
  574. break;
  575. list_del(&page->lru);
  576. enqueue_huge_page(page);
  577. }
  578. /* Free unnecessary surplus pages to the buddy allocator */
  579. if (!list_empty(&surplus_list)) {
  580. spin_unlock(&hugetlb_lock);
  581. list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
  582. list_del(&page->lru);
  583. /*
  584. * The page has a reference count of zero already, so
  585. * call free_huge_page directly instead of using
  586. * put_page. This must be done with hugetlb_lock
  587. * unlocked which is safe because free_huge_page takes
  588. * hugetlb_lock before deciding how to free the page.
  589. */
  590. free_huge_page(page);
  591. }
  592. spin_lock(&hugetlb_lock);
  593. }
  594. return ret;
  595. }
  596. /*
  597. * When releasing a hugetlb pool reservation, any surplus pages that were
  598. * allocated to satisfy the reservation must be explicitly freed if they were
  599. * never used.
  600. */
  601. static void return_unused_surplus_pages(unsigned long unused_resv_pages)
  602. {
  603. static int nid = -1;
  604. struct page *page;
  605. unsigned long nr_pages;
  606. /*
  607. * We want to release as many surplus pages as possible, spread
  608. * evenly across all nodes. Iterate across all nodes until we
  609. * can no longer free unreserved surplus pages. This occurs when
  610. * the nodes with surplus pages have no free pages.
  611. */
  612. unsigned long remaining_iterations = num_online_nodes();
  613. /* Uncommit the reservation */
  614. resv_huge_pages -= unused_resv_pages;
  615. nr_pages = min(unused_resv_pages, surplus_huge_pages);
  616. while (remaining_iterations-- && nr_pages) {
  617. nid = next_node(nid, node_online_map);
  618. if (nid == MAX_NUMNODES)
  619. nid = first_node(node_online_map);
  620. if (!surplus_huge_pages_node[nid])
  621. continue;
  622. if (!list_empty(&hugepage_freelists[nid])) {
  623. page = list_entry(hugepage_freelists[nid].next,
  624. struct page, lru);
  625. list_del(&page->lru);
  626. update_and_free_page(page);
  627. free_huge_pages--;
  628. free_huge_pages_node[nid]--;
  629. surplus_huge_pages--;
  630. surplus_huge_pages_node[nid]--;
  631. nr_pages--;
  632. remaining_iterations = num_online_nodes();
  633. }
  634. }
  635. }
  636. static struct page *alloc_huge_page(struct vm_area_struct *vma,
  637. unsigned long addr, int avoid_reserve)
  638. {
  639. struct page *page;
  640. struct address_space *mapping = vma->vm_file->f_mapping;
  641. struct inode *inode = mapping->host;
  642. unsigned int chg = 0;
  643. /*
  644. * Processes that did not create the mapping will have no reserves and
  645. * will not have accounted against quota. Check that the quota can be
  646. * made before satisfying the allocation
  647. */
  648. if (!(vma->vm_flags & VM_SHARED) &&
  649. !is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
  650. chg = 1;
  651. if (hugetlb_get_quota(inode->i_mapping, chg))
  652. return ERR_PTR(-ENOSPC);
  653. }
  654. spin_lock(&hugetlb_lock);
  655. page = dequeue_huge_page_vma(vma, addr, avoid_reserve);
  656. spin_unlock(&hugetlb_lock);
  657. if (!page) {
  658. page = alloc_buddy_huge_page(vma, addr);
  659. if (!page) {
  660. hugetlb_put_quota(inode->i_mapping, chg);
  661. return ERR_PTR(-VM_FAULT_OOM);
  662. }
  663. }
  664. set_page_refcounted(page);
  665. set_page_private(page, (unsigned long) mapping);
  666. return page;
  667. }
  668. static int __init hugetlb_init(void)
  669. {
  670. unsigned long i;
  671. if (HPAGE_SHIFT == 0)
  672. return 0;
  673. for (i = 0; i < MAX_NUMNODES; ++i)
  674. INIT_LIST_HEAD(&hugepage_freelists[i]);
  675. hugetlb_next_nid = first_node(node_online_map);
  676. for (i = 0; i < max_huge_pages; ++i) {
  677. if (!alloc_fresh_huge_page())
  678. break;
  679. }
  680. max_huge_pages = free_huge_pages = nr_huge_pages = i;
  681. printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages);
  682. return 0;
  683. }
  684. module_init(hugetlb_init);
  685. static int __init hugetlb_setup(char *s)
  686. {
  687. if (sscanf(s, "%lu", &max_huge_pages) <= 0)
  688. max_huge_pages = 0;
  689. return 1;
  690. }
  691. __setup("hugepages=", hugetlb_setup);
  692. static unsigned int cpuset_mems_nr(unsigned int *array)
  693. {
  694. int node;
  695. unsigned int nr = 0;
  696. for_each_node_mask(node, cpuset_current_mems_allowed)
  697. nr += array[node];
  698. return nr;
  699. }
  700. #ifdef CONFIG_SYSCTL
  701. #ifdef CONFIG_HIGHMEM
  702. static void try_to_free_low(unsigned long count)
  703. {
  704. int i;
  705. for (i = 0; i < MAX_NUMNODES; ++i) {
  706. struct page *page, *next;
  707. list_for_each_entry_safe(page, next, &hugepage_freelists[i], lru) {
  708. if (count >= nr_huge_pages)
  709. return;
  710. if (PageHighMem(page))
  711. continue;
  712. list_del(&page->lru);
  713. update_and_free_page(page);
  714. free_huge_pages--;
  715. free_huge_pages_node[page_to_nid(page)]--;
  716. }
  717. }
  718. }
  719. #else
  720. static inline void try_to_free_low(unsigned long count)
  721. {
  722. }
  723. #endif
  724. #define persistent_huge_pages (nr_huge_pages - surplus_huge_pages)
  725. static unsigned long set_max_huge_pages(unsigned long count)
  726. {
  727. unsigned long min_count, ret;
  728. /*
  729. * Increase the pool size
  730. * First take pages out of surplus state. Then make up the
  731. * remaining difference by allocating fresh huge pages.
  732. *
  733. * We might race with alloc_buddy_huge_page() here and be unable
  734. * to convert a surplus huge page to a normal huge page. That is
  735. * not critical, though, it just means the overall size of the
  736. * pool might be one hugepage larger than it needs to be, but
  737. * within all the constraints specified by the sysctls.
  738. */
  739. spin_lock(&hugetlb_lock);
  740. while (surplus_huge_pages && count > persistent_huge_pages) {
  741. if (!adjust_pool_surplus(-1))
  742. break;
  743. }
  744. while (count > persistent_huge_pages) {
  745. /*
  746. * If this allocation races such that we no longer need the
  747. * page, free_huge_page will handle it by freeing the page
  748. * and reducing the surplus.
  749. */
  750. spin_unlock(&hugetlb_lock);
  751. ret = alloc_fresh_huge_page();
  752. spin_lock(&hugetlb_lock);
  753. if (!ret)
  754. goto out;
  755. }
  756. /*
  757. * Decrease the pool size
  758. * First return free pages to the buddy allocator (being careful
  759. * to keep enough around to satisfy reservations). Then place
  760. * pages into surplus state as needed so the pool will shrink
  761. * to the desired size as pages become free.
  762. *
  763. * By placing pages into the surplus state independent of the
  764. * overcommit value, we are allowing the surplus pool size to
  765. * exceed overcommit. There are few sane options here. Since
  766. * alloc_buddy_huge_page() is checking the global counter,
  767. * though, we'll note that we're not allowed to exceed surplus
  768. * and won't grow the pool anywhere else. Not until one of the
  769. * sysctls are changed, or the surplus pages go out of use.
  770. */
  771. min_count = resv_huge_pages + nr_huge_pages - free_huge_pages;
  772. min_count = max(count, min_count);
  773. try_to_free_low(min_count);
  774. while (min_count < persistent_huge_pages) {
  775. struct page *page = dequeue_huge_page();
  776. if (!page)
  777. break;
  778. update_and_free_page(page);
  779. }
  780. while (count < persistent_huge_pages) {
  781. if (!adjust_pool_surplus(1))
  782. break;
  783. }
  784. out:
  785. ret = persistent_huge_pages;
  786. spin_unlock(&hugetlb_lock);
  787. return ret;
  788. }
  789. int hugetlb_sysctl_handler(struct ctl_table *table, int write,
  790. struct file *file, void __user *buffer,
  791. size_t *length, loff_t *ppos)
  792. {
  793. proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
  794. max_huge_pages = set_max_huge_pages(max_huge_pages);
  795. return 0;
  796. }
  797. int hugetlb_treat_movable_handler(struct ctl_table *table, int write,
  798. struct file *file, void __user *buffer,
  799. size_t *length, loff_t *ppos)
  800. {
  801. proc_dointvec(table, write, file, buffer, length, ppos);
  802. if (hugepages_treat_as_movable)
  803. htlb_alloc_mask = GFP_HIGHUSER_MOVABLE;
  804. else
  805. htlb_alloc_mask = GFP_HIGHUSER;
  806. return 0;
  807. }
  808. int hugetlb_overcommit_handler(struct ctl_table *table, int write,
  809. struct file *file, void __user *buffer,
  810. size_t *length, loff_t *ppos)
  811. {
  812. proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
  813. spin_lock(&hugetlb_lock);
  814. nr_overcommit_huge_pages = sysctl_overcommit_huge_pages;
  815. spin_unlock(&hugetlb_lock);
  816. return 0;
  817. }
  818. #endif /* CONFIG_SYSCTL */
  819. int hugetlb_report_meminfo(char *buf)
  820. {
  821. return sprintf(buf,
  822. "HugePages_Total: %5lu\n"
  823. "HugePages_Free: %5lu\n"
  824. "HugePages_Rsvd: %5lu\n"
  825. "HugePages_Surp: %5lu\n"
  826. "Hugepagesize: %5lu kB\n",
  827. nr_huge_pages,
  828. free_huge_pages,
  829. resv_huge_pages,
  830. surplus_huge_pages,
  831. HPAGE_SIZE/1024);
  832. }
  833. int hugetlb_report_node_meminfo(int nid, char *buf)
  834. {
  835. return sprintf(buf,
  836. "Node %d HugePages_Total: %5u\n"
  837. "Node %d HugePages_Free: %5u\n"
  838. "Node %d HugePages_Surp: %5u\n",
  839. nid, nr_huge_pages_node[nid],
  840. nid, free_huge_pages_node[nid],
  841. nid, surplus_huge_pages_node[nid]);
  842. }
  843. /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
  844. unsigned long hugetlb_total_pages(void)
  845. {
  846. return nr_huge_pages * (HPAGE_SIZE / PAGE_SIZE);
  847. }
  848. static int hugetlb_acct_memory(long delta)
  849. {
  850. int ret = -ENOMEM;
  851. spin_lock(&hugetlb_lock);
  852. /*
  853. * When cpuset is configured, it breaks the strict hugetlb page
  854. * reservation as the accounting is done on a global variable. Such
  855. * reservation is completely rubbish in the presence of cpuset because
  856. * the reservation is not checked against page availability for the
  857. * current cpuset. Application can still potentially OOM'ed by kernel
  858. * with lack of free htlb page in cpuset that the task is in.
  859. * Attempt to enforce strict accounting with cpuset is almost
  860. * impossible (or too ugly) because cpuset is too fluid that
  861. * task or memory node can be dynamically moved between cpusets.
  862. *
  863. * The change of semantics for shared hugetlb mapping with cpuset is
  864. * undesirable. However, in order to preserve some of the semantics,
  865. * we fall back to check against current free page availability as
  866. * a best attempt and hopefully to minimize the impact of changing
  867. * semantics that cpuset has.
  868. */
  869. if (delta > 0) {
  870. if (gather_surplus_pages(delta) < 0)
  871. goto out;
  872. if (delta > cpuset_mems_nr(free_huge_pages_node)) {
  873. return_unused_surplus_pages(delta);
  874. goto out;
  875. }
  876. }
  877. ret = 0;
  878. if (delta < 0)
  879. return_unused_surplus_pages((unsigned long) -delta);
  880. out:
  881. spin_unlock(&hugetlb_lock);
  882. return ret;
  883. }
  884. static void hugetlb_vm_op_close(struct vm_area_struct *vma)
  885. {
  886. unsigned long reserve = vma_resv_huge_pages(vma);
  887. if (reserve)
  888. hugetlb_acct_memory(-reserve);
  889. }
  890. /*
  891. * We cannot handle pagefaults against hugetlb pages at all. They cause
  892. * handle_mm_fault() to try to instantiate regular-sized pages in the
  893. * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
  894. * this far.
  895. */
  896. static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  897. {
  898. BUG();
  899. return 0;
  900. }
  901. struct vm_operations_struct hugetlb_vm_ops = {
  902. .fault = hugetlb_vm_op_fault,
  903. .close = hugetlb_vm_op_close,
  904. };
  905. static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
  906. int writable)
  907. {
  908. pte_t entry;
  909. if (writable) {
  910. entry =
  911. pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
  912. } else {
  913. entry = huge_pte_wrprotect(mk_pte(page, vma->vm_page_prot));
  914. }
  915. entry = pte_mkyoung(entry);
  916. entry = pte_mkhuge(entry);
  917. return entry;
  918. }
  919. static void set_huge_ptep_writable(struct vm_area_struct *vma,
  920. unsigned long address, pte_t *ptep)
  921. {
  922. pte_t entry;
  923. entry = pte_mkwrite(pte_mkdirty(huge_ptep_get(ptep)));
  924. if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1)) {
  925. update_mmu_cache(vma, address, entry);
  926. }
  927. }
  928. int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
  929. struct vm_area_struct *vma)
  930. {
  931. pte_t *src_pte, *dst_pte, entry;
  932. struct page *ptepage;
  933. unsigned long addr;
  934. int cow;
  935. cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
  936. for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
  937. src_pte = huge_pte_offset(src, addr);
  938. if (!src_pte)
  939. continue;
  940. dst_pte = huge_pte_alloc(dst, addr);
  941. if (!dst_pte)
  942. goto nomem;
  943. /* If the pagetables are shared don't copy or take references */
  944. if (dst_pte == src_pte)
  945. continue;
  946. spin_lock(&dst->page_table_lock);
  947. spin_lock_nested(&src->page_table_lock, SINGLE_DEPTH_NESTING);
  948. if (!huge_pte_none(huge_ptep_get(src_pte))) {
  949. if (cow)
  950. huge_ptep_set_wrprotect(src, addr, src_pte);
  951. entry = huge_ptep_get(src_pte);
  952. ptepage = pte_page(entry);
  953. get_page(ptepage);
  954. set_huge_pte_at(dst, addr, dst_pte, entry);
  955. }
  956. spin_unlock(&src->page_table_lock);
  957. spin_unlock(&dst->page_table_lock);
  958. }
  959. return 0;
  960. nomem:
  961. return -ENOMEM;
  962. }
  963. void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
  964. unsigned long end, struct page *ref_page)
  965. {
  966. struct mm_struct *mm = vma->vm_mm;
  967. unsigned long address;
  968. pte_t *ptep;
  969. pte_t pte;
  970. struct page *page;
  971. struct page *tmp;
  972. /*
  973. * A page gathering list, protected by per file i_mmap_lock. The
  974. * lock is used to avoid list corruption from multiple unmapping
  975. * of the same page since we are using page->lru.
  976. */
  977. LIST_HEAD(page_list);
  978. WARN_ON(!is_vm_hugetlb_page(vma));
  979. BUG_ON(start & ~HPAGE_MASK);
  980. BUG_ON(end & ~HPAGE_MASK);
  981. spin_lock(&mm->page_table_lock);
  982. for (address = start; address < end; address += HPAGE_SIZE) {
  983. ptep = huge_pte_offset(mm, address);
  984. if (!ptep)
  985. continue;
  986. if (huge_pmd_unshare(mm, &address, ptep))
  987. continue;
  988. /*
  989. * If a reference page is supplied, it is because a specific
  990. * page is being unmapped, not a range. Ensure the page we
  991. * are about to unmap is the actual page of interest.
  992. */
  993. if (ref_page) {
  994. pte = huge_ptep_get(ptep);
  995. if (huge_pte_none(pte))
  996. continue;
  997. page = pte_page(pte);
  998. if (page != ref_page)
  999. continue;
  1000. /*
  1001. * Mark the VMA as having unmapped its page so that
  1002. * future faults in this VMA will fail rather than
  1003. * looking like data was lost
  1004. */
  1005. set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
  1006. }
  1007. pte = huge_ptep_get_and_clear(mm, address, ptep);
  1008. if (huge_pte_none(pte))
  1009. continue;
  1010. page = pte_page(pte);
  1011. if (pte_dirty(pte))
  1012. set_page_dirty(page);
  1013. list_add(&page->lru, &page_list);
  1014. }
  1015. spin_unlock(&mm->page_table_lock);
  1016. flush_tlb_range(vma, start, end);
  1017. list_for_each_entry_safe(page, tmp, &page_list, lru) {
  1018. list_del(&page->lru);
  1019. put_page(page);
  1020. }
  1021. }
  1022. void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
  1023. unsigned long end, struct page *ref_page)
  1024. {
  1025. /*
  1026. * It is undesirable to test vma->vm_file as it should be non-null
  1027. * for valid hugetlb area. However, vm_file will be NULL in the error
  1028. * cleanup path of do_mmap_pgoff. When hugetlbfs ->mmap method fails,
  1029. * do_mmap_pgoff() nullifies vma->vm_file before calling this function
  1030. * to clean up. Since no pte has actually been setup, it is safe to
  1031. * do nothing in this case.
  1032. */
  1033. if (vma->vm_file) {
  1034. spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
  1035. __unmap_hugepage_range(vma, start, end, ref_page);
  1036. spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
  1037. }
  1038. }
  1039. /*
  1040. * This is called when the original mapper is failing to COW a MAP_PRIVATE
  1041. * mappping it owns the reserve page for. The intention is to unmap the page
  1042. * from other VMAs and let the children be SIGKILLed if they are faulting the
  1043. * same region.
  1044. */
  1045. int unmap_ref_private(struct mm_struct *mm,
  1046. struct vm_area_struct *vma,
  1047. struct page *page,
  1048. unsigned long address)
  1049. {
  1050. struct vm_area_struct *iter_vma;
  1051. struct address_space *mapping;
  1052. struct prio_tree_iter iter;
  1053. pgoff_t pgoff;
  1054. /*
  1055. * vm_pgoff is in PAGE_SIZE units, hence the different calculation
  1056. * from page cache lookup which is in HPAGE_SIZE units.
  1057. */
  1058. address = address & huge_page_mask(hstate_vma(vma));
  1059. pgoff = ((address - vma->vm_start) >> PAGE_SHIFT)
  1060. + (vma->vm_pgoff >> PAGE_SHIFT);
  1061. mapping = (struct address_space *)page_private(page);
  1062. vma_prio_tree_foreach(iter_vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  1063. /* Do not unmap the current VMA */
  1064. if (iter_vma == vma)
  1065. continue;
  1066. /*
  1067. * Unmap the page from other VMAs without their own reserves.
  1068. * They get marked to be SIGKILLed if they fault in these
  1069. * areas. This is because a future no-page fault on this VMA
  1070. * could insert a zeroed page instead of the data existing
  1071. * from the time of fork. This would look like data corruption
  1072. */
  1073. if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
  1074. unmap_hugepage_range(iter_vma,
  1075. address, address + HPAGE_SIZE,
  1076. page);
  1077. }
  1078. return 1;
  1079. }
  1080. static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
  1081. unsigned long address, pte_t *ptep, pte_t pte,
  1082. struct page *pagecache_page)
  1083. {
  1084. struct page *old_page, *new_page;
  1085. int avoidcopy;
  1086. int outside_reserve = 0;
  1087. old_page = pte_page(pte);
  1088. retry_avoidcopy:
  1089. /* If no-one else is actually using this page, avoid the copy
  1090. * and just make the page writable */
  1091. avoidcopy = (page_count(old_page) == 1);
  1092. if (avoidcopy) {
  1093. set_huge_ptep_writable(vma, address, ptep);
  1094. return 0;
  1095. }
  1096. /*
  1097. * If the process that created a MAP_PRIVATE mapping is about to
  1098. * perform a COW due to a shared page count, attempt to satisfy
  1099. * the allocation without using the existing reserves. The pagecache
  1100. * page is used to determine if the reserve at this address was
  1101. * consumed or not. If reserves were used, a partial faulted mapping
  1102. * at the time of fork() could consume its reserves on COW instead
  1103. * of the full address range.
  1104. */
  1105. if (!(vma->vm_flags & VM_SHARED) &&
  1106. is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
  1107. old_page != pagecache_page)
  1108. outside_reserve = 1;
  1109. page_cache_get(old_page);
  1110. new_page = alloc_huge_page(vma, address, outside_reserve);
  1111. if (IS_ERR(new_page)) {
  1112. page_cache_release(old_page);
  1113. /*
  1114. * If a process owning a MAP_PRIVATE mapping fails to COW,
  1115. * it is due to references held by a child and an insufficient
  1116. * huge page pool. To guarantee the original mappers
  1117. * reliability, unmap the page from child processes. The child
  1118. * may get SIGKILLed if it later faults.
  1119. */
  1120. if (outside_reserve) {
  1121. BUG_ON(huge_pte_none(pte));
  1122. if (unmap_ref_private(mm, vma, old_page, address)) {
  1123. BUG_ON(page_count(old_page) != 1);
  1124. BUG_ON(huge_pte_none(pte));
  1125. goto retry_avoidcopy;
  1126. }
  1127. WARN_ON_ONCE(1);
  1128. }
  1129. return -PTR_ERR(new_page);
  1130. }
  1131. spin_unlock(&mm->page_table_lock);
  1132. copy_huge_page(new_page, old_page, address, vma);
  1133. __SetPageUptodate(new_page);
  1134. spin_lock(&mm->page_table_lock);
  1135. ptep = huge_pte_offset(mm, address & HPAGE_MASK);
  1136. if (likely(pte_same(huge_ptep_get(ptep), pte))) {
  1137. /* Break COW */
  1138. huge_ptep_clear_flush(vma, address, ptep);
  1139. set_huge_pte_at(mm, address, ptep,
  1140. make_huge_pte(vma, new_page, 1));
  1141. /* Make the old page be freed below */
  1142. new_page = old_page;
  1143. }
  1144. page_cache_release(new_page);
  1145. page_cache_release(old_page);
  1146. return 0;
  1147. }
  1148. /* Return the pagecache page at a given address within a VMA */
  1149. static struct page *hugetlbfs_pagecache_page(struct vm_area_struct *vma,
  1150. unsigned long address)
  1151. {
  1152. struct address_space *mapping;
  1153. pgoff_t idx;
  1154. mapping = vma->vm_file->f_mapping;
  1155. idx = vma_pagecache_offset(vma, address);
  1156. return find_lock_page(mapping, idx);
  1157. }
  1158. static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1159. unsigned long address, pte_t *ptep, int write_access)
  1160. {
  1161. int ret = VM_FAULT_SIGBUS;
  1162. pgoff_t idx;
  1163. unsigned long size;
  1164. struct page *page;
  1165. struct address_space *mapping;
  1166. pte_t new_pte;
  1167. /*
  1168. * Currently, we are forced to kill the process in the event the
  1169. * original mapper has unmapped pages from the child due to a failed
  1170. * COW. Warn that such a situation has occured as it may not be obvious
  1171. */
  1172. if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
  1173. printk(KERN_WARNING
  1174. "PID %d killed due to inadequate hugepage pool\n",
  1175. current->pid);
  1176. return ret;
  1177. }
  1178. mapping = vma->vm_file->f_mapping;
  1179. idx = vma_pagecache_offset(vma, address);
  1180. /*
  1181. * Use page lock to guard against racing truncation
  1182. * before we get page_table_lock.
  1183. */
  1184. retry:
  1185. page = find_lock_page(mapping, idx);
  1186. if (!page) {
  1187. size = i_size_read(mapping->host) >> HPAGE_SHIFT;
  1188. if (idx >= size)
  1189. goto out;
  1190. page = alloc_huge_page(vma, address, 0);
  1191. if (IS_ERR(page)) {
  1192. ret = -PTR_ERR(page);
  1193. goto out;
  1194. }
  1195. clear_huge_page(page, address);
  1196. __SetPageUptodate(page);
  1197. if (vma->vm_flags & VM_SHARED) {
  1198. int err;
  1199. struct inode *inode = mapping->host;
  1200. err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
  1201. if (err) {
  1202. put_page(page);
  1203. if (err == -EEXIST)
  1204. goto retry;
  1205. goto out;
  1206. }
  1207. spin_lock(&inode->i_lock);
  1208. inode->i_blocks += BLOCKS_PER_HUGEPAGE;
  1209. spin_unlock(&inode->i_lock);
  1210. } else
  1211. lock_page(page);
  1212. }
  1213. spin_lock(&mm->page_table_lock);
  1214. size = i_size_read(mapping->host) >> HPAGE_SHIFT;
  1215. if (idx >= size)
  1216. goto backout;
  1217. ret = 0;
  1218. if (!huge_pte_none(huge_ptep_get(ptep)))
  1219. goto backout;
  1220. new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
  1221. && (vma->vm_flags & VM_SHARED)));
  1222. set_huge_pte_at(mm, address, ptep, new_pte);
  1223. if (write_access && !(vma->vm_flags & VM_SHARED)) {
  1224. /* Optimization, do the COW without a second fault */
  1225. ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page);
  1226. }
  1227. spin_unlock(&mm->page_table_lock);
  1228. unlock_page(page);
  1229. out:
  1230. return ret;
  1231. backout:
  1232. spin_unlock(&mm->page_table_lock);
  1233. unlock_page(page);
  1234. put_page(page);
  1235. goto out;
  1236. }
  1237. int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  1238. unsigned long address, int write_access)
  1239. {
  1240. pte_t *ptep;
  1241. pte_t entry;
  1242. int ret;
  1243. static DEFINE_MUTEX(hugetlb_instantiation_mutex);
  1244. ptep = huge_pte_alloc(mm, address);
  1245. if (!ptep)
  1246. return VM_FAULT_OOM;
  1247. /*
  1248. * Serialize hugepage allocation and instantiation, so that we don't
  1249. * get spurious allocation failures if two CPUs race to instantiate
  1250. * the same page in the page cache.
  1251. */
  1252. mutex_lock(&hugetlb_instantiation_mutex);
  1253. entry = huge_ptep_get(ptep);
  1254. if (huge_pte_none(entry)) {
  1255. ret = hugetlb_no_page(mm, vma, address, ptep, write_access);
  1256. mutex_unlock(&hugetlb_instantiation_mutex);
  1257. return ret;
  1258. }
  1259. ret = 0;
  1260. spin_lock(&mm->page_table_lock);
  1261. /* Check for a racing update before calling hugetlb_cow */
  1262. if (likely(pte_same(entry, huge_ptep_get(ptep))))
  1263. if (write_access && !pte_write(entry)) {
  1264. struct page *page;
  1265. page = hugetlbfs_pagecache_page(vma, address);
  1266. ret = hugetlb_cow(mm, vma, address, ptep, entry, page);
  1267. if (page) {
  1268. unlock_page(page);
  1269. put_page(page);
  1270. }
  1271. }
  1272. spin_unlock(&mm->page_table_lock);
  1273. mutex_unlock(&hugetlb_instantiation_mutex);
  1274. return ret;
  1275. }
  1276. int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1277. struct page **pages, struct vm_area_struct **vmas,
  1278. unsigned long *position, int *length, int i,
  1279. int write)
  1280. {
  1281. unsigned long pfn_offset;
  1282. unsigned long vaddr = *position;
  1283. int remainder = *length;
  1284. spin_lock(&mm->page_table_lock);
  1285. while (vaddr < vma->vm_end && remainder) {
  1286. pte_t *pte;
  1287. struct page *page;
  1288. /*
  1289. * Some archs (sparc64, sh*) have multiple pte_ts to
  1290. * each hugepage. We have to make * sure we get the
  1291. * first, for the page indexing below to work.
  1292. */
  1293. pte = huge_pte_offset(mm, vaddr & HPAGE_MASK);
  1294. if (!pte || huge_pte_none(huge_ptep_get(pte)) ||
  1295. (write && !pte_write(huge_ptep_get(pte)))) {
  1296. int ret;
  1297. spin_unlock(&mm->page_table_lock);
  1298. ret = hugetlb_fault(mm, vma, vaddr, write);
  1299. spin_lock(&mm->page_table_lock);
  1300. if (!(ret & VM_FAULT_ERROR))
  1301. continue;
  1302. remainder = 0;
  1303. if (!i)
  1304. i = -EFAULT;
  1305. break;
  1306. }
  1307. pfn_offset = (vaddr & ~HPAGE_MASK) >> PAGE_SHIFT;
  1308. page = pte_page(huge_ptep_get(pte));
  1309. same_page:
  1310. if (pages) {
  1311. get_page(page);
  1312. pages[i] = page + pfn_offset;
  1313. }
  1314. if (vmas)
  1315. vmas[i] = vma;
  1316. vaddr += PAGE_SIZE;
  1317. ++pfn_offset;
  1318. --remainder;
  1319. ++i;
  1320. if (vaddr < vma->vm_end && remainder &&
  1321. pfn_offset < HPAGE_SIZE/PAGE_SIZE) {
  1322. /*
  1323. * We use pfn_offset to avoid touching the pageframes
  1324. * of this compound page.
  1325. */
  1326. goto same_page;
  1327. }
  1328. }
  1329. spin_unlock(&mm->page_table_lock);
  1330. *length = remainder;
  1331. *position = vaddr;
  1332. return i;
  1333. }
  1334. void hugetlb_change_protection(struct vm_area_struct *vma,
  1335. unsigned long address, unsigned long end, pgprot_t newprot)
  1336. {
  1337. struct mm_struct *mm = vma->vm_mm;
  1338. unsigned long start = address;
  1339. pte_t *ptep;
  1340. pte_t pte;
  1341. BUG_ON(address >= end);
  1342. flush_cache_range(vma, address, end);
  1343. spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
  1344. spin_lock(&mm->page_table_lock);
  1345. for (; address < end; address += HPAGE_SIZE) {
  1346. ptep = huge_pte_offset(mm, address);
  1347. if (!ptep)
  1348. continue;
  1349. if (huge_pmd_unshare(mm, &address, ptep))
  1350. continue;
  1351. if (!huge_pte_none(huge_ptep_get(ptep))) {
  1352. pte = huge_ptep_get_and_clear(mm, address, ptep);
  1353. pte = pte_mkhuge(pte_modify(pte, newprot));
  1354. set_huge_pte_at(mm, address, ptep, pte);
  1355. }
  1356. }
  1357. spin_unlock(&mm->page_table_lock);
  1358. spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
  1359. flush_tlb_range(vma, start, end);
  1360. }
  1361. int hugetlb_reserve_pages(struct inode *inode,
  1362. long from, long to,
  1363. struct vm_area_struct *vma)
  1364. {
  1365. long ret, chg;
  1366. /*
  1367. * Shared mappings base their reservation on the number of pages that
  1368. * are already allocated on behalf of the file. Private mappings need
  1369. * to reserve the full area even if read-only as mprotect() may be
  1370. * called to make the mapping read-write. Assume !vma is a shm mapping
  1371. */
  1372. if (!vma || vma->vm_flags & VM_SHARED)
  1373. chg = region_chg(&inode->i_mapping->private_list, from, to);
  1374. else {
  1375. chg = to - from;
  1376. set_vma_resv_huge_pages(vma, chg);
  1377. set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
  1378. }
  1379. if (chg < 0)
  1380. return chg;
  1381. if (hugetlb_get_quota(inode->i_mapping, chg))
  1382. return -ENOSPC;
  1383. ret = hugetlb_acct_memory(chg);
  1384. if (ret < 0) {
  1385. hugetlb_put_quota(inode->i_mapping, chg);
  1386. return ret;
  1387. }
  1388. if (!vma || vma->vm_flags & VM_SHARED)
  1389. region_add(&inode->i_mapping->private_list, from, to);
  1390. return 0;
  1391. }
  1392. void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
  1393. {
  1394. long chg = region_truncate(&inode->i_mapping->private_list, offset);
  1395. spin_lock(&inode->i_lock);
  1396. inode->i_blocks -= BLOCKS_PER_HUGEPAGE * freed;
  1397. spin_unlock(&inode->i_lock);
  1398. hugetlb_put_quota(inode->i_mapping, (chg - freed));
  1399. hugetlb_acct_memory(-(chg - freed));
  1400. }