hugetlb.c 32 KB

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