hugetlb.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  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. static void clear_huge_page(struct page *page, unsigned long addr)
  39. {
  40. int i;
  41. might_sleep();
  42. for (i = 0; i < (HPAGE_SIZE/PAGE_SIZE); i++) {
  43. cond_resched();
  44. clear_user_highpage(page + i, addr + i * PAGE_SIZE);
  45. }
  46. }
  47. static void copy_huge_page(struct page *dst, struct page *src,
  48. unsigned long addr, struct vm_area_struct *vma)
  49. {
  50. int i;
  51. might_sleep();
  52. for (i = 0; i < HPAGE_SIZE/PAGE_SIZE; i++) {
  53. cond_resched();
  54. copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
  55. }
  56. }
  57. static void enqueue_huge_page(struct page *page)
  58. {
  59. int nid = page_to_nid(page);
  60. list_add(&page->lru, &hugepage_freelists[nid]);
  61. free_huge_pages++;
  62. free_huge_pages_node[nid]++;
  63. }
  64. static struct page *dequeue_huge_page(struct vm_area_struct *vma,
  65. unsigned long address)
  66. {
  67. int nid;
  68. struct page *page = NULL;
  69. struct mempolicy *mpol;
  70. struct zonelist *zonelist = huge_zonelist(vma, address,
  71. htlb_alloc_mask, &mpol);
  72. struct zone **z;
  73. for (z = zonelist->zones; *z; z++) {
  74. nid = zone_to_nid(*z);
  75. if (cpuset_zone_allowed_softwall(*z, htlb_alloc_mask) &&
  76. !list_empty(&hugepage_freelists[nid])) {
  77. page = list_entry(hugepage_freelists[nid].next,
  78. struct page, lru);
  79. list_del(&page->lru);
  80. free_huge_pages--;
  81. free_huge_pages_node[nid]--;
  82. if (vma && vma->vm_flags & VM_MAYSHARE)
  83. resv_huge_pages--;
  84. break;
  85. }
  86. }
  87. mpol_free(mpol); /* unref if mpol !NULL */
  88. return page;
  89. }
  90. static void update_and_free_page(struct page *page)
  91. {
  92. int i;
  93. nr_huge_pages--;
  94. nr_huge_pages_node[page_to_nid(page)]--;
  95. for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++) {
  96. page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
  97. 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
  98. 1 << PG_private | 1<< PG_writeback);
  99. }
  100. set_compound_page_dtor(page, NULL);
  101. set_page_refcounted(page);
  102. __free_pages(page, HUGETLB_PAGE_ORDER);
  103. }
  104. static void free_huge_page(struct page *page)
  105. {
  106. int nid = page_to_nid(page);
  107. struct address_space *mapping;
  108. mapping = (struct address_space *) page_private(page);
  109. set_page_private(page, 0);
  110. BUG_ON(page_count(page));
  111. INIT_LIST_HEAD(&page->lru);
  112. spin_lock(&hugetlb_lock);
  113. if (surplus_huge_pages_node[nid]) {
  114. update_and_free_page(page);
  115. surplus_huge_pages--;
  116. surplus_huge_pages_node[nid]--;
  117. } else {
  118. enqueue_huge_page(page);
  119. }
  120. spin_unlock(&hugetlb_lock);
  121. if (mapping)
  122. hugetlb_put_quota(mapping, 1);
  123. }
  124. /*
  125. * Increment or decrement surplus_huge_pages. Keep node-specific counters
  126. * balanced by operating on them in a round-robin fashion.
  127. * Returns 1 if an adjustment was made.
  128. */
  129. static int adjust_pool_surplus(int delta)
  130. {
  131. static int prev_nid;
  132. int nid = prev_nid;
  133. int ret = 0;
  134. VM_BUG_ON(delta != -1 && delta != 1);
  135. do {
  136. nid = next_node(nid, node_online_map);
  137. if (nid == MAX_NUMNODES)
  138. nid = first_node(node_online_map);
  139. /* To shrink on this node, there must be a surplus page */
  140. if (delta < 0 && !surplus_huge_pages_node[nid])
  141. continue;
  142. /* Surplus cannot exceed the total number of pages */
  143. if (delta > 0 && surplus_huge_pages_node[nid] >=
  144. nr_huge_pages_node[nid])
  145. continue;
  146. surplus_huge_pages += delta;
  147. surplus_huge_pages_node[nid] += delta;
  148. ret = 1;
  149. break;
  150. } while (nid != prev_nid);
  151. prev_nid = nid;
  152. return ret;
  153. }
  154. static struct page *alloc_fresh_huge_page_node(int nid)
  155. {
  156. struct page *page;
  157. page = alloc_pages_node(nid,
  158. htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|__GFP_NOWARN,
  159. HUGETLB_PAGE_ORDER);
  160. if (page) {
  161. set_compound_page_dtor(page, free_huge_page);
  162. spin_lock(&hugetlb_lock);
  163. nr_huge_pages++;
  164. nr_huge_pages_node[nid]++;
  165. spin_unlock(&hugetlb_lock);
  166. put_page(page); /* free it into the hugepage allocator */
  167. }
  168. return page;
  169. }
  170. static int alloc_fresh_huge_page(void)
  171. {
  172. struct page *page;
  173. int start_nid;
  174. int next_nid;
  175. int ret = 0;
  176. start_nid = hugetlb_next_nid;
  177. do {
  178. page = alloc_fresh_huge_page_node(hugetlb_next_nid);
  179. if (page)
  180. ret = 1;
  181. /*
  182. * Use a helper variable to find the next node and then
  183. * copy it back to hugetlb_next_nid afterwards:
  184. * otherwise there's a window in which a racer might
  185. * pass invalid nid MAX_NUMNODES to alloc_pages_node.
  186. * But we don't need to use a spin_lock here: it really
  187. * doesn't matter if occasionally a racer chooses the
  188. * same nid as we do. Move nid forward in the mask even
  189. * if we just successfully allocated a hugepage so that
  190. * the next caller gets hugepages on the next node.
  191. */
  192. next_nid = next_node(hugetlb_next_nid, node_online_map);
  193. if (next_nid == MAX_NUMNODES)
  194. next_nid = first_node(node_online_map);
  195. hugetlb_next_nid = next_nid;
  196. } while (!page && hugetlb_next_nid != start_nid);
  197. return ret;
  198. }
  199. static struct page *alloc_buddy_huge_page(struct vm_area_struct *vma,
  200. unsigned long address)
  201. {
  202. struct page *page;
  203. unsigned int nid;
  204. /*
  205. * Assume we will successfully allocate the surplus page to
  206. * prevent racing processes from causing the surplus to exceed
  207. * overcommit
  208. *
  209. * This however introduces a different race, where a process B
  210. * tries to grow the static hugepage pool while alloc_pages() is
  211. * called by process A. B will only examine the per-node
  212. * counters in determining if surplus huge pages can be
  213. * converted to normal huge pages in adjust_pool_surplus(). A
  214. * won't be able to increment the per-node counter, until the
  215. * lock is dropped by B, but B doesn't drop hugetlb_lock until
  216. * no more huge pages can be converted from surplus to normal
  217. * state (and doesn't try to convert again). Thus, we have a
  218. * case where a surplus huge page exists, the pool is grown, and
  219. * the surplus huge page still exists after, even though it
  220. * should just have been converted to a normal huge page. This
  221. * does not leak memory, though, as the hugepage will be freed
  222. * once it is out of use. It also does not allow the counters to
  223. * go out of whack in adjust_pool_surplus() as we don't modify
  224. * the node values until we've gotten the hugepage and only the
  225. * per-node value is checked there.
  226. */
  227. spin_lock(&hugetlb_lock);
  228. if (surplus_huge_pages >= nr_overcommit_huge_pages) {
  229. spin_unlock(&hugetlb_lock);
  230. return NULL;
  231. } else {
  232. nr_huge_pages++;
  233. surplus_huge_pages++;
  234. }
  235. spin_unlock(&hugetlb_lock);
  236. page = alloc_pages(htlb_alloc_mask|__GFP_COMP|__GFP_NOWARN,
  237. HUGETLB_PAGE_ORDER);
  238. spin_lock(&hugetlb_lock);
  239. if (page) {
  240. nid = page_to_nid(page);
  241. set_compound_page_dtor(page, free_huge_page);
  242. /*
  243. * We incremented the global counters already
  244. */
  245. nr_huge_pages_node[nid]++;
  246. surplus_huge_pages_node[nid]++;
  247. } else {
  248. nr_huge_pages--;
  249. surplus_huge_pages--;
  250. }
  251. spin_unlock(&hugetlb_lock);
  252. return page;
  253. }
  254. /*
  255. * Increase the hugetlb pool such that it can accomodate a reservation
  256. * of size 'delta'.
  257. */
  258. static int gather_surplus_pages(int delta)
  259. {
  260. struct list_head surplus_list;
  261. struct page *page, *tmp;
  262. int ret, i;
  263. int needed, allocated;
  264. needed = (resv_huge_pages + delta) - free_huge_pages;
  265. if (needed <= 0)
  266. return 0;
  267. allocated = 0;
  268. INIT_LIST_HEAD(&surplus_list);
  269. ret = -ENOMEM;
  270. retry:
  271. spin_unlock(&hugetlb_lock);
  272. for (i = 0; i < needed; i++) {
  273. page = alloc_buddy_huge_page(NULL, 0);
  274. if (!page) {
  275. /*
  276. * We were not able to allocate enough pages to
  277. * satisfy the entire reservation so we free what
  278. * we've allocated so far.
  279. */
  280. spin_lock(&hugetlb_lock);
  281. needed = 0;
  282. goto free;
  283. }
  284. list_add(&page->lru, &surplus_list);
  285. }
  286. allocated += needed;
  287. /*
  288. * After retaking hugetlb_lock, we need to recalculate 'needed'
  289. * because either resv_huge_pages or free_huge_pages may have changed.
  290. */
  291. spin_lock(&hugetlb_lock);
  292. needed = (resv_huge_pages + delta) - (free_huge_pages + allocated);
  293. if (needed > 0)
  294. goto retry;
  295. /*
  296. * The surplus_list now contains _at_least_ the number of extra pages
  297. * needed to accomodate the reservation. Add the appropriate number
  298. * of pages to the hugetlb pool and free the extras back to the buddy
  299. * allocator.
  300. */
  301. needed += allocated;
  302. ret = 0;
  303. free:
  304. list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
  305. list_del(&page->lru);
  306. if ((--needed) >= 0)
  307. enqueue_huge_page(page);
  308. else {
  309. /*
  310. * Decrement the refcount and free the page using its
  311. * destructor. This must be done with hugetlb_lock
  312. * unlocked which is safe because free_huge_page takes
  313. * hugetlb_lock before deciding how to free the page.
  314. */
  315. spin_unlock(&hugetlb_lock);
  316. put_page(page);
  317. spin_lock(&hugetlb_lock);
  318. }
  319. }
  320. return ret;
  321. }
  322. /*
  323. * When releasing a hugetlb pool reservation, any surplus pages that were
  324. * allocated to satisfy the reservation must be explicitly freed if they were
  325. * never used.
  326. */
  327. static void return_unused_surplus_pages(unsigned long unused_resv_pages)
  328. {
  329. static int nid = -1;
  330. struct page *page;
  331. unsigned long nr_pages;
  332. nr_pages = min(unused_resv_pages, surplus_huge_pages);
  333. while (nr_pages) {
  334. nid = next_node(nid, node_online_map);
  335. if (nid == MAX_NUMNODES)
  336. nid = first_node(node_online_map);
  337. if (!surplus_huge_pages_node[nid])
  338. continue;
  339. if (!list_empty(&hugepage_freelists[nid])) {
  340. page = list_entry(hugepage_freelists[nid].next,
  341. struct page, lru);
  342. list_del(&page->lru);
  343. update_and_free_page(page);
  344. free_huge_pages--;
  345. free_huge_pages_node[nid]--;
  346. surplus_huge_pages--;
  347. surplus_huge_pages_node[nid]--;
  348. nr_pages--;
  349. }
  350. }
  351. }
  352. static struct page *alloc_huge_page_shared(struct vm_area_struct *vma,
  353. unsigned long addr)
  354. {
  355. struct page *page;
  356. spin_lock(&hugetlb_lock);
  357. page = dequeue_huge_page(vma, addr);
  358. spin_unlock(&hugetlb_lock);
  359. return page ? page : ERR_PTR(-VM_FAULT_OOM);
  360. }
  361. static struct page *alloc_huge_page_private(struct vm_area_struct *vma,
  362. unsigned long addr)
  363. {
  364. struct page *page = NULL;
  365. if (hugetlb_get_quota(vma->vm_file->f_mapping, 1))
  366. return ERR_PTR(-VM_FAULT_SIGBUS);
  367. spin_lock(&hugetlb_lock);
  368. if (free_huge_pages > resv_huge_pages)
  369. page = dequeue_huge_page(vma, addr);
  370. spin_unlock(&hugetlb_lock);
  371. if (!page) {
  372. page = alloc_buddy_huge_page(vma, addr);
  373. if (!page) {
  374. hugetlb_put_quota(vma->vm_file->f_mapping, 1);
  375. return ERR_PTR(-VM_FAULT_OOM);
  376. }
  377. }
  378. return page;
  379. }
  380. static struct page *alloc_huge_page(struct vm_area_struct *vma,
  381. unsigned long addr)
  382. {
  383. struct page *page;
  384. struct address_space *mapping = vma->vm_file->f_mapping;
  385. if (vma->vm_flags & VM_MAYSHARE)
  386. page = alloc_huge_page_shared(vma, addr);
  387. else
  388. page = alloc_huge_page_private(vma, addr);
  389. if (!IS_ERR(page)) {
  390. set_page_refcounted(page);
  391. set_page_private(page, (unsigned long) mapping);
  392. }
  393. return page;
  394. }
  395. static int __init hugetlb_init(void)
  396. {
  397. unsigned long i;
  398. if (HPAGE_SHIFT == 0)
  399. return 0;
  400. for (i = 0; i < MAX_NUMNODES; ++i)
  401. INIT_LIST_HEAD(&hugepage_freelists[i]);
  402. hugetlb_next_nid = first_node(node_online_map);
  403. for (i = 0; i < max_huge_pages; ++i) {
  404. if (!alloc_fresh_huge_page())
  405. break;
  406. }
  407. max_huge_pages = free_huge_pages = nr_huge_pages = i;
  408. printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages);
  409. return 0;
  410. }
  411. module_init(hugetlb_init);
  412. static int __init hugetlb_setup(char *s)
  413. {
  414. if (sscanf(s, "%lu", &max_huge_pages) <= 0)
  415. max_huge_pages = 0;
  416. return 1;
  417. }
  418. __setup("hugepages=", hugetlb_setup);
  419. static unsigned int cpuset_mems_nr(unsigned int *array)
  420. {
  421. int node;
  422. unsigned int nr = 0;
  423. for_each_node_mask(node, cpuset_current_mems_allowed)
  424. nr += array[node];
  425. return nr;
  426. }
  427. #ifdef CONFIG_SYSCTL
  428. #ifdef CONFIG_HIGHMEM
  429. static void try_to_free_low(unsigned long count)
  430. {
  431. int i;
  432. for (i = 0; i < MAX_NUMNODES; ++i) {
  433. struct page *page, *next;
  434. list_for_each_entry_safe(page, next, &hugepage_freelists[i], lru) {
  435. if (count >= nr_huge_pages)
  436. return;
  437. if (PageHighMem(page))
  438. continue;
  439. list_del(&page->lru);
  440. update_and_free_page(page);
  441. free_huge_pages--;
  442. free_huge_pages_node[page_to_nid(page)]--;
  443. }
  444. }
  445. }
  446. #else
  447. static inline void try_to_free_low(unsigned long count)
  448. {
  449. }
  450. #endif
  451. #define persistent_huge_pages (nr_huge_pages - surplus_huge_pages)
  452. static unsigned long set_max_huge_pages(unsigned long count)
  453. {
  454. unsigned long min_count, ret;
  455. /*
  456. * Increase the pool size
  457. * First take pages out of surplus state. Then make up the
  458. * remaining difference by allocating fresh huge pages.
  459. *
  460. * We might race with alloc_buddy_huge_page() here and be unable
  461. * to convert a surplus huge page to a normal huge page. That is
  462. * not critical, though, it just means the overall size of the
  463. * pool might be one hugepage larger than it needs to be, but
  464. * within all the constraints specified by the sysctls.
  465. */
  466. spin_lock(&hugetlb_lock);
  467. while (surplus_huge_pages && count > persistent_huge_pages) {
  468. if (!adjust_pool_surplus(-1))
  469. break;
  470. }
  471. while (count > persistent_huge_pages) {
  472. int ret;
  473. /*
  474. * If this allocation races such that we no longer need the
  475. * page, free_huge_page will handle it by freeing the page
  476. * and reducing the surplus.
  477. */
  478. spin_unlock(&hugetlb_lock);
  479. ret = alloc_fresh_huge_page();
  480. spin_lock(&hugetlb_lock);
  481. if (!ret)
  482. goto out;
  483. }
  484. /*
  485. * Decrease the pool size
  486. * First return free pages to the buddy allocator (being careful
  487. * to keep enough around to satisfy reservations). Then place
  488. * pages into surplus state as needed so the pool will shrink
  489. * to the desired size as pages become free.
  490. *
  491. * By placing pages into the surplus state independent of the
  492. * overcommit value, we are allowing the surplus pool size to
  493. * exceed overcommit. There are few sane options here. Since
  494. * alloc_buddy_huge_page() is checking the global counter,
  495. * though, we'll note that we're not allowed to exceed surplus
  496. * and won't grow the pool anywhere else. Not until one of the
  497. * sysctls are changed, or the surplus pages go out of use.
  498. */
  499. min_count = resv_huge_pages + nr_huge_pages - free_huge_pages;
  500. min_count = max(count, min_count);
  501. try_to_free_low(min_count);
  502. while (min_count < persistent_huge_pages) {
  503. struct page *page = dequeue_huge_page(NULL, 0);
  504. if (!page)
  505. break;
  506. update_and_free_page(page);
  507. }
  508. while (count < persistent_huge_pages) {
  509. if (!adjust_pool_surplus(1))
  510. break;
  511. }
  512. out:
  513. ret = persistent_huge_pages;
  514. spin_unlock(&hugetlb_lock);
  515. return ret;
  516. }
  517. int hugetlb_sysctl_handler(struct ctl_table *table, int write,
  518. struct file *file, void __user *buffer,
  519. size_t *length, loff_t *ppos)
  520. {
  521. proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
  522. max_huge_pages = set_max_huge_pages(max_huge_pages);
  523. return 0;
  524. }
  525. int hugetlb_treat_movable_handler(struct ctl_table *table, int write,
  526. struct file *file, void __user *buffer,
  527. size_t *length, loff_t *ppos)
  528. {
  529. proc_dointvec(table, write, file, buffer, length, ppos);
  530. if (hugepages_treat_as_movable)
  531. htlb_alloc_mask = GFP_HIGHUSER_MOVABLE;
  532. else
  533. htlb_alloc_mask = GFP_HIGHUSER;
  534. return 0;
  535. }
  536. int hugetlb_overcommit_handler(struct ctl_table *table, int write,
  537. struct file *file, void __user *buffer,
  538. size_t *length, loff_t *ppos)
  539. {
  540. proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
  541. spin_lock(&hugetlb_lock);
  542. nr_overcommit_huge_pages = sysctl_overcommit_huge_pages;
  543. spin_unlock(&hugetlb_lock);
  544. return 0;
  545. }
  546. #endif /* CONFIG_SYSCTL */
  547. int hugetlb_report_meminfo(char *buf)
  548. {
  549. return sprintf(buf,
  550. "HugePages_Total: %5lu\n"
  551. "HugePages_Free: %5lu\n"
  552. "HugePages_Rsvd: %5lu\n"
  553. "HugePages_Surp: %5lu\n"
  554. "Hugepagesize: %5lu kB\n",
  555. nr_huge_pages,
  556. free_huge_pages,
  557. resv_huge_pages,
  558. surplus_huge_pages,
  559. HPAGE_SIZE/1024);
  560. }
  561. int hugetlb_report_node_meminfo(int nid, char *buf)
  562. {
  563. return sprintf(buf,
  564. "Node %d HugePages_Total: %5u\n"
  565. "Node %d HugePages_Free: %5u\n",
  566. nid, nr_huge_pages_node[nid],
  567. nid, free_huge_pages_node[nid]);
  568. }
  569. /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
  570. unsigned long hugetlb_total_pages(void)
  571. {
  572. return nr_huge_pages * (HPAGE_SIZE / PAGE_SIZE);
  573. }
  574. /*
  575. * We cannot handle pagefaults against hugetlb pages at all. They cause
  576. * handle_mm_fault() to try to instantiate regular-sized pages in the
  577. * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
  578. * this far.
  579. */
  580. static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  581. {
  582. BUG();
  583. return 0;
  584. }
  585. struct vm_operations_struct hugetlb_vm_ops = {
  586. .fault = hugetlb_vm_op_fault,
  587. };
  588. static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
  589. int writable)
  590. {
  591. pte_t entry;
  592. if (writable) {
  593. entry =
  594. pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
  595. } else {
  596. entry = pte_wrprotect(mk_pte(page, vma->vm_page_prot));
  597. }
  598. entry = pte_mkyoung(entry);
  599. entry = pte_mkhuge(entry);
  600. return entry;
  601. }
  602. static void set_huge_ptep_writable(struct vm_area_struct *vma,
  603. unsigned long address, pte_t *ptep)
  604. {
  605. pte_t entry;
  606. entry = pte_mkwrite(pte_mkdirty(*ptep));
  607. if (ptep_set_access_flags(vma, address, ptep, entry, 1)) {
  608. update_mmu_cache(vma, address, entry);
  609. }
  610. }
  611. int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
  612. struct vm_area_struct *vma)
  613. {
  614. pte_t *src_pte, *dst_pte, entry;
  615. struct page *ptepage;
  616. unsigned long addr;
  617. int cow;
  618. cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
  619. for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
  620. src_pte = huge_pte_offset(src, addr);
  621. if (!src_pte)
  622. continue;
  623. dst_pte = huge_pte_alloc(dst, addr);
  624. if (!dst_pte)
  625. goto nomem;
  626. /* If the pagetables are shared don't copy or take references */
  627. if (dst_pte == src_pte)
  628. continue;
  629. spin_lock(&dst->page_table_lock);
  630. spin_lock(&src->page_table_lock);
  631. if (!pte_none(*src_pte)) {
  632. if (cow)
  633. ptep_set_wrprotect(src, addr, src_pte);
  634. entry = *src_pte;
  635. ptepage = pte_page(entry);
  636. get_page(ptepage);
  637. set_huge_pte_at(dst, addr, dst_pte, entry);
  638. }
  639. spin_unlock(&src->page_table_lock);
  640. spin_unlock(&dst->page_table_lock);
  641. }
  642. return 0;
  643. nomem:
  644. return -ENOMEM;
  645. }
  646. void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
  647. unsigned long end)
  648. {
  649. struct mm_struct *mm = vma->vm_mm;
  650. unsigned long address;
  651. pte_t *ptep;
  652. pte_t pte;
  653. struct page *page;
  654. struct page *tmp;
  655. /*
  656. * A page gathering list, protected by per file i_mmap_lock. The
  657. * lock is used to avoid list corruption from multiple unmapping
  658. * of the same page since we are using page->lru.
  659. */
  660. LIST_HEAD(page_list);
  661. WARN_ON(!is_vm_hugetlb_page(vma));
  662. BUG_ON(start & ~HPAGE_MASK);
  663. BUG_ON(end & ~HPAGE_MASK);
  664. spin_lock(&mm->page_table_lock);
  665. for (address = start; address < end; address += HPAGE_SIZE) {
  666. ptep = huge_pte_offset(mm, address);
  667. if (!ptep)
  668. continue;
  669. if (huge_pmd_unshare(mm, &address, ptep))
  670. continue;
  671. pte = huge_ptep_get_and_clear(mm, address, ptep);
  672. if (pte_none(pte))
  673. continue;
  674. page = pte_page(pte);
  675. if (pte_dirty(pte))
  676. set_page_dirty(page);
  677. list_add(&page->lru, &page_list);
  678. }
  679. spin_unlock(&mm->page_table_lock);
  680. flush_tlb_range(vma, start, end);
  681. list_for_each_entry_safe(page, tmp, &page_list, lru) {
  682. list_del(&page->lru);
  683. put_page(page);
  684. }
  685. }
  686. void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
  687. unsigned long end)
  688. {
  689. /*
  690. * It is undesirable to test vma->vm_file as it should be non-null
  691. * for valid hugetlb area. However, vm_file will be NULL in the error
  692. * cleanup path of do_mmap_pgoff. When hugetlbfs ->mmap method fails,
  693. * do_mmap_pgoff() nullifies vma->vm_file before calling this function
  694. * to clean up. Since no pte has actually been setup, it is safe to
  695. * do nothing in this case.
  696. */
  697. if (vma->vm_file) {
  698. spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
  699. __unmap_hugepage_range(vma, start, end);
  700. spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
  701. }
  702. }
  703. static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
  704. unsigned long address, pte_t *ptep, pte_t pte)
  705. {
  706. struct page *old_page, *new_page;
  707. int avoidcopy;
  708. old_page = pte_page(pte);
  709. /* If no-one else is actually using this page, avoid the copy
  710. * and just make the page writable */
  711. avoidcopy = (page_count(old_page) == 1);
  712. if (avoidcopy) {
  713. set_huge_ptep_writable(vma, address, ptep);
  714. return 0;
  715. }
  716. page_cache_get(old_page);
  717. new_page = alloc_huge_page(vma, address);
  718. if (IS_ERR(new_page)) {
  719. page_cache_release(old_page);
  720. return -PTR_ERR(new_page);
  721. }
  722. spin_unlock(&mm->page_table_lock);
  723. copy_huge_page(new_page, old_page, address, vma);
  724. __SetPageUptodate(new_page);
  725. spin_lock(&mm->page_table_lock);
  726. ptep = huge_pte_offset(mm, address & HPAGE_MASK);
  727. if (likely(pte_same(*ptep, pte))) {
  728. /* Break COW */
  729. set_huge_pte_at(mm, address, ptep,
  730. make_huge_pte(vma, new_page, 1));
  731. /* Make the old page be freed below */
  732. new_page = old_page;
  733. }
  734. page_cache_release(new_page);
  735. page_cache_release(old_page);
  736. return 0;
  737. }
  738. static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
  739. unsigned long address, pte_t *ptep, int write_access)
  740. {
  741. int ret = VM_FAULT_SIGBUS;
  742. unsigned long idx;
  743. unsigned long size;
  744. struct page *page;
  745. struct address_space *mapping;
  746. pte_t new_pte;
  747. mapping = vma->vm_file->f_mapping;
  748. idx = ((address - vma->vm_start) >> HPAGE_SHIFT)
  749. + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
  750. /*
  751. * Use page lock to guard against racing truncation
  752. * before we get page_table_lock.
  753. */
  754. retry:
  755. page = find_lock_page(mapping, idx);
  756. if (!page) {
  757. size = i_size_read(mapping->host) >> HPAGE_SHIFT;
  758. if (idx >= size)
  759. goto out;
  760. page = alloc_huge_page(vma, address);
  761. if (IS_ERR(page)) {
  762. ret = -PTR_ERR(page);
  763. goto out;
  764. }
  765. clear_huge_page(page, address);
  766. __SetPageUptodate(page);
  767. if (vma->vm_flags & VM_SHARED) {
  768. int err;
  769. struct inode *inode = mapping->host;
  770. err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
  771. if (err) {
  772. put_page(page);
  773. if (err == -EEXIST)
  774. goto retry;
  775. goto out;
  776. }
  777. spin_lock(&inode->i_lock);
  778. inode->i_blocks += BLOCKS_PER_HUGEPAGE;
  779. spin_unlock(&inode->i_lock);
  780. } else
  781. lock_page(page);
  782. }
  783. spin_lock(&mm->page_table_lock);
  784. size = i_size_read(mapping->host) >> HPAGE_SHIFT;
  785. if (idx >= size)
  786. goto backout;
  787. ret = 0;
  788. if (!pte_none(*ptep))
  789. goto backout;
  790. new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
  791. && (vma->vm_flags & VM_SHARED)));
  792. set_huge_pte_at(mm, address, ptep, new_pte);
  793. if (write_access && !(vma->vm_flags & VM_SHARED)) {
  794. /* Optimization, do the COW without a second fault */
  795. ret = hugetlb_cow(mm, vma, address, ptep, new_pte);
  796. }
  797. spin_unlock(&mm->page_table_lock);
  798. unlock_page(page);
  799. out:
  800. return ret;
  801. backout:
  802. spin_unlock(&mm->page_table_lock);
  803. unlock_page(page);
  804. put_page(page);
  805. goto out;
  806. }
  807. int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  808. unsigned long address, int write_access)
  809. {
  810. pte_t *ptep;
  811. pte_t entry;
  812. int ret;
  813. static DEFINE_MUTEX(hugetlb_instantiation_mutex);
  814. ptep = huge_pte_alloc(mm, address);
  815. if (!ptep)
  816. return VM_FAULT_OOM;
  817. /*
  818. * Serialize hugepage allocation and instantiation, so that we don't
  819. * get spurious allocation failures if two CPUs race to instantiate
  820. * the same page in the page cache.
  821. */
  822. mutex_lock(&hugetlb_instantiation_mutex);
  823. entry = *ptep;
  824. if (pte_none(entry)) {
  825. ret = hugetlb_no_page(mm, vma, address, ptep, write_access);
  826. mutex_unlock(&hugetlb_instantiation_mutex);
  827. return ret;
  828. }
  829. ret = 0;
  830. spin_lock(&mm->page_table_lock);
  831. /* Check for a racing update before calling hugetlb_cow */
  832. if (likely(pte_same(entry, *ptep)))
  833. if (write_access && !pte_write(entry))
  834. ret = hugetlb_cow(mm, vma, address, ptep, entry);
  835. spin_unlock(&mm->page_table_lock);
  836. mutex_unlock(&hugetlb_instantiation_mutex);
  837. return ret;
  838. }
  839. int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
  840. struct page **pages, struct vm_area_struct **vmas,
  841. unsigned long *position, int *length, int i,
  842. int write)
  843. {
  844. unsigned long pfn_offset;
  845. unsigned long vaddr = *position;
  846. int remainder = *length;
  847. spin_lock(&mm->page_table_lock);
  848. while (vaddr < vma->vm_end && remainder) {
  849. pte_t *pte;
  850. struct page *page;
  851. /*
  852. * Some archs (sparc64, sh*) have multiple pte_ts to
  853. * each hugepage. We have to make * sure we get the
  854. * first, for the page indexing below to work.
  855. */
  856. pte = huge_pte_offset(mm, vaddr & HPAGE_MASK);
  857. if (!pte || pte_none(*pte) || (write && !pte_write(*pte))) {
  858. int ret;
  859. spin_unlock(&mm->page_table_lock);
  860. ret = hugetlb_fault(mm, vma, vaddr, write);
  861. spin_lock(&mm->page_table_lock);
  862. if (!(ret & VM_FAULT_ERROR))
  863. continue;
  864. remainder = 0;
  865. if (!i)
  866. i = -EFAULT;
  867. break;
  868. }
  869. pfn_offset = (vaddr & ~HPAGE_MASK) >> PAGE_SHIFT;
  870. page = pte_page(*pte);
  871. same_page:
  872. if (pages) {
  873. get_page(page);
  874. pages[i] = page + pfn_offset;
  875. }
  876. if (vmas)
  877. vmas[i] = vma;
  878. vaddr += PAGE_SIZE;
  879. ++pfn_offset;
  880. --remainder;
  881. ++i;
  882. if (vaddr < vma->vm_end && remainder &&
  883. pfn_offset < HPAGE_SIZE/PAGE_SIZE) {
  884. /*
  885. * We use pfn_offset to avoid touching the pageframes
  886. * of this compound page.
  887. */
  888. goto same_page;
  889. }
  890. }
  891. spin_unlock(&mm->page_table_lock);
  892. *length = remainder;
  893. *position = vaddr;
  894. return i;
  895. }
  896. void hugetlb_change_protection(struct vm_area_struct *vma,
  897. unsigned long address, unsigned long end, pgprot_t newprot)
  898. {
  899. struct mm_struct *mm = vma->vm_mm;
  900. unsigned long start = address;
  901. pte_t *ptep;
  902. pte_t pte;
  903. BUG_ON(address >= end);
  904. flush_cache_range(vma, address, end);
  905. spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
  906. spin_lock(&mm->page_table_lock);
  907. for (; address < end; address += HPAGE_SIZE) {
  908. ptep = huge_pte_offset(mm, address);
  909. if (!ptep)
  910. continue;
  911. if (huge_pmd_unshare(mm, &address, ptep))
  912. continue;
  913. if (!pte_none(*ptep)) {
  914. pte = huge_ptep_get_and_clear(mm, address, ptep);
  915. pte = pte_mkhuge(pte_modify(pte, newprot));
  916. set_huge_pte_at(mm, address, ptep, pte);
  917. }
  918. }
  919. spin_unlock(&mm->page_table_lock);
  920. spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
  921. flush_tlb_range(vma, start, end);
  922. }
  923. struct file_region {
  924. struct list_head link;
  925. long from;
  926. long to;
  927. };
  928. static long region_add(struct list_head *head, long f, long t)
  929. {
  930. struct file_region *rg, *nrg, *trg;
  931. /* Locate the region we are either in or before. */
  932. list_for_each_entry(rg, head, link)
  933. if (f <= rg->to)
  934. break;
  935. /* Round our left edge to the current segment if it encloses us. */
  936. if (f > rg->from)
  937. f = rg->from;
  938. /* Check for and consume any regions we now overlap with. */
  939. nrg = rg;
  940. list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
  941. if (&rg->link == head)
  942. break;
  943. if (rg->from > t)
  944. break;
  945. /* If this area reaches higher then extend our area to
  946. * include it completely. If this is not the first area
  947. * which we intend to reuse, free it. */
  948. if (rg->to > t)
  949. t = rg->to;
  950. if (rg != nrg) {
  951. list_del(&rg->link);
  952. kfree(rg);
  953. }
  954. }
  955. nrg->from = f;
  956. nrg->to = t;
  957. return 0;
  958. }
  959. static long region_chg(struct list_head *head, long f, long t)
  960. {
  961. struct file_region *rg, *nrg;
  962. long chg = 0;
  963. /* Locate the region we are before or in. */
  964. list_for_each_entry(rg, head, link)
  965. if (f <= rg->to)
  966. break;
  967. /* If we are below the current region then a new region is required.
  968. * Subtle, allocate a new region at the position but make it zero
  969. * size such that we can guarantee to record the reservation. */
  970. if (&rg->link == head || t < rg->from) {
  971. nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
  972. if (!nrg)
  973. return -ENOMEM;
  974. nrg->from = f;
  975. nrg->to = f;
  976. INIT_LIST_HEAD(&nrg->link);
  977. list_add(&nrg->link, rg->link.prev);
  978. return t - f;
  979. }
  980. /* Round our left edge to the current segment if it encloses us. */
  981. if (f > rg->from)
  982. f = rg->from;
  983. chg = t - f;
  984. /* Check for and consume any regions we now overlap with. */
  985. list_for_each_entry(rg, rg->link.prev, link) {
  986. if (&rg->link == head)
  987. break;
  988. if (rg->from > t)
  989. return chg;
  990. /* We overlap with this area, if it extends futher than
  991. * us then we must extend ourselves. Account for its
  992. * existing reservation. */
  993. if (rg->to > t) {
  994. chg += rg->to - t;
  995. t = rg->to;
  996. }
  997. chg -= rg->to - rg->from;
  998. }
  999. return chg;
  1000. }
  1001. static long region_truncate(struct list_head *head, long end)
  1002. {
  1003. struct file_region *rg, *trg;
  1004. long chg = 0;
  1005. /* Locate the region we are either in or before. */
  1006. list_for_each_entry(rg, head, link)
  1007. if (end <= rg->to)
  1008. break;
  1009. if (&rg->link == head)
  1010. return 0;
  1011. /* If we are in the middle of a region then adjust it. */
  1012. if (end > rg->from) {
  1013. chg = rg->to - end;
  1014. rg->to = end;
  1015. rg = list_entry(rg->link.next, typeof(*rg), link);
  1016. }
  1017. /* Drop any remaining regions. */
  1018. list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
  1019. if (&rg->link == head)
  1020. break;
  1021. chg += rg->to - rg->from;
  1022. list_del(&rg->link);
  1023. kfree(rg);
  1024. }
  1025. return chg;
  1026. }
  1027. static int hugetlb_acct_memory(long delta)
  1028. {
  1029. int ret = -ENOMEM;
  1030. spin_lock(&hugetlb_lock);
  1031. /*
  1032. * When cpuset is configured, it breaks the strict hugetlb page
  1033. * reservation as the accounting is done on a global variable. Such
  1034. * reservation is completely rubbish in the presence of cpuset because
  1035. * the reservation is not checked against page availability for the
  1036. * current cpuset. Application can still potentially OOM'ed by kernel
  1037. * with lack of free htlb page in cpuset that the task is in.
  1038. * Attempt to enforce strict accounting with cpuset is almost
  1039. * impossible (or too ugly) because cpuset is too fluid that
  1040. * task or memory node can be dynamically moved between cpusets.
  1041. *
  1042. * The change of semantics for shared hugetlb mapping with cpuset is
  1043. * undesirable. However, in order to preserve some of the semantics,
  1044. * we fall back to check against current free page availability as
  1045. * a best attempt and hopefully to minimize the impact of changing
  1046. * semantics that cpuset has.
  1047. */
  1048. if (delta > 0) {
  1049. if (gather_surplus_pages(delta) < 0)
  1050. goto out;
  1051. if (delta > cpuset_mems_nr(free_huge_pages_node))
  1052. goto out;
  1053. }
  1054. ret = 0;
  1055. resv_huge_pages += delta;
  1056. if (delta < 0)
  1057. return_unused_surplus_pages((unsigned long) -delta);
  1058. out:
  1059. spin_unlock(&hugetlb_lock);
  1060. return ret;
  1061. }
  1062. int hugetlb_reserve_pages(struct inode *inode, long from, long to)
  1063. {
  1064. long ret, chg;
  1065. chg = region_chg(&inode->i_mapping->private_list, from, to);
  1066. if (chg < 0)
  1067. return chg;
  1068. if (hugetlb_get_quota(inode->i_mapping, chg))
  1069. return -ENOSPC;
  1070. ret = hugetlb_acct_memory(chg);
  1071. if (ret < 0) {
  1072. hugetlb_put_quota(inode->i_mapping, chg);
  1073. return ret;
  1074. }
  1075. region_add(&inode->i_mapping->private_list, from, to);
  1076. return 0;
  1077. }
  1078. void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
  1079. {
  1080. long chg = region_truncate(&inode->i_mapping->private_list, offset);
  1081. spin_lock(&inode->i_lock);
  1082. inode->i_blocks -= BLOCKS_PER_HUGEPAGE * freed;
  1083. spin_unlock(&inode->i_lock);
  1084. hugetlb_put_quota(inode->i_mapping, (chg - freed));
  1085. hugetlb_acct_memory(-(chg - freed));
  1086. }