hugetlb.c 31 KB

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