hugetlb.c 33 KB

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