hugetlb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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 <asm/page.h>
  15. #include <asm/pgtable.h>
  16. #include <linux/hugetlb.h>
  17. const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL;
  18. static unsigned long nr_huge_pages, free_huge_pages;
  19. unsigned long max_huge_pages;
  20. static struct list_head hugepage_freelists[MAX_NUMNODES];
  21. static unsigned int nr_huge_pages_node[MAX_NUMNODES];
  22. static unsigned int free_huge_pages_node[MAX_NUMNODES];
  23. /*
  24. * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages
  25. */
  26. static DEFINE_SPINLOCK(hugetlb_lock);
  27. static void enqueue_huge_page(struct page *page)
  28. {
  29. int nid = page_to_nid(page);
  30. list_add(&page->lru, &hugepage_freelists[nid]);
  31. free_huge_pages++;
  32. free_huge_pages_node[nid]++;
  33. }
  34. static struct page *dequeue_huge_page(void)
  35. {
  36. int nid = numa_node_id();
  37. struct page *page = NULL;
  38. if (list_empty(&hugepage_freelists[nid])) {
  39. for (nid = 0; nid < MAX_NUMNODES; ++nid)
  40. if (!list_empty(&hugepage_freelists[nid]))
  41. break;
  42. }
  43. if (nid >= 0 && nid < MAX_NUMNODES &&
  44. !list_empty(&hugepage_freelists[nid])) {
  45. page = list_entry(hugepage_freelists[nid].next,
  46. struct page, lru);
  47. list_del(&page->lru);
  48. free_huge_pages--;
  49. free_huge_pages_node[nid]--;
  50. }
  51. return page;
  52. }
  53. static struct page *alloc_fresh_huge_page(void)
  54. {
  55. static int nid = 0;
  56. struct page *page;
  57. page = alloc_pages_node(nid, GFP_HIGHUSER|__GFP_COMP|__GFP_NOWARN,
  58. HUGETLB_PAGE_ORDER);
  59. nid = (nid + 1) % num_online_nodes();
  60. if (page) {
  61. spin_lock(&hugetlb_lock);
  62. nr_huge_pages++;
  63. nr_huge_pages_node[page_to_nid(page)]++;
  64. spin_unlock(&hugetlb_lock);
  65. }
  66. return page;
  67. }
  68. void free_huge_page(struct page *page)
  69. {
  70. BUG_ON(page_count(page));
  71. INIT_LIST_HEAD(&page->lru);
  72. page[1].mapping = NULL;
  73. spin_lock(&hugetlb_lock);
  74. enqueue_huge_page(page);
  75. spin_unlock(&hugetlb_lock);
  76. }
  77. struct page *alloc_huge_page(void)
  78. {
  79. struct page *page;
  80. int i;
  81. spin_lock(&hugetlb_lock);
  82. page = dequeue_huge_page();
  83. if (!page) {
  84. spin_unlock(&hugetlb_lock);
  85. return NULL;
  86. }
  87. spin_unlock(&hugetlb_lock);
  88. set_page_count(page, 1);
  89. page[1].mapping = (void *)free_huge_page;
  90. for (i = 0; i < (HPAGE_SIZE/PAGE_SIZE); ++i)
  91. clear_highpage(&page[i]);
  92. return page;
  93. }
  94. static int __init hugetlb_init(void)
  95. {
  96. unsigned long i;
  97. struct page *page;
  98. if (HPAGE_SHIFT == 0)
  99. return 0;
  100. for (i = 0; i < MAX_NUMNODES; ++i)
  101. INIT_LIST_HEAD(&hugepage_freelists[i]);
  102. for (i = 0; i < max_huge_pages; ++i) {
  103. page = alloc_fresh_huge_page();
  104. if (!page)
  105. break;
  106. spin_lock(&hugetlb_lock);
  107. enqueue_huge_page(page);
  108. spin_unlock(&hugetlb_lock);
  109. }
  110. max_huge_pages = free_huge_pages = nr_huge_pages = i;
  111. printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages);
  112. return 0;
  113. }
  114. module_init(hugetlb_init);
  115. static int __init hugetlb_setup(char *s)
  116. {
  117. if (sscanf(s, "%lu", &max_huge_pages) <= 0)
  118. max_huge_pages = 0;
  119. return 1;
  120. }
  121. __setup("hugepages=", hugetlb_setup);
  122. #ifdef CONFIG_SYSCTL
  123. static void update_and_free_page(struct page *page)
  124. {
  125. int i;
  126. nr_huge_pages--;
  127. nr_huge_pages_node[page_zone(page)->zone_pgdat->node_id]--;
  128. for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++) {
  129. page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
  130. 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
  131. 1 << PG_private | 1<< PG_writeback);
  132. set_page_count(&page[i], 0);
  133. }
  134. set_page_count(page, 1);
  135. __free_pages(page, HUGETLB_PAGE_ORDER);
  136. }
  137. #ifdef CONFIG_HIGHMEM
  138. static void try_to_free_low(unsigned long count)
  139. {
  140. int i, nid;
  141. for (i = 0; i < MAX_NUMNODES; ++i) {
  142. struct page *page, *next;
  143. list_for_each_entry_safe(page, next, &hugepage_freelists[i], lru) {
  144. if (PageHighMem(page))
  145. continue;
  146. list_del(&page->lru);
  147. update_and_free_page(page);
  148. nid = page_zone(page)->zone_pgdat->node_id;
  149. free_huge_pages--;
  150. free_huge_pages_node[nid]--;
  151. if (count >= nr_huge_pages)
  152. return;
  153. }
  154. }
  155. }
  156. #else
  157. static inline void try_to_free_low(unsigned long count)
  158. {
  159. }
  160. #endif
  161. static unsigned long set_max_huge_pages(unsigned long count)
  162. {
  163. while (count > nr_huge_pages) {
  164. struct page *page = alloc_fresh_huge_page();
  165. if (!page)
  166. return nr_huge_pages;
  167. spin_lock(&hugetlb_lock);
  168. enqueue_huge_page(page);
  169. spin_unlock(&hugetlb_lock);
  170. }
  171. if (count >= nr_huge_pages)
  172. return nr_huge_pages;
  173. spin_lock(&hugetlb_lock);
  174. try_to_free_low(count);
  175. while (count < nr_huge_pages) {
  176. struct page *page = dequeue_huge_page();
  177. if (!page)
  178. break;
  179. update_and_free_page(page);
  180. }
  181. spin_unlock(&hugetlb_lock);
  182. return nr_huge_pages;
  183. }
  184. int hugetlb_sysctl_handler(struct ctl_table *table, int write,
  185. struct file *file, void __user *buffer,
  186. size_t *length, loff_t *ppos)
  187. {
  188. proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
  189. max_huge_pages = set_max_huge_pages(max_huge_pages);
  190. return 0;
  191. }
  192. #endif /* CONFIG_SYSCTL */
  193. int hugetlb_report_meminfo(char *buf)
  194. {
  195. return sprintf(buf,
  196. "HugePages_Total: %5lu\n"
  197. "HugePages_Free: %5lu\n"
  198. "Hugepagesize: %5lu kB\n",
  199. nr_huge_pages,
  200. free_huge_pages,
  201. HPAGE_SIZE/1024);
  202. }
  203. int hugetlb_report_node_meminfo(int nid, char *buf)
  204. {
  205. return sprintf(buf,
  206. "Node %d HugePages_Total: %5u\n"
  207. "Node %d HugePages_Free: %5u\n",
  208. nid, nr_huge_pages_node[nid],
  209. nid, free_huge_pages_node[nid]);
  210. }
  211. int is_hugepage_mem_enough(size_t size)
  212. {
  213. return (size + ~HPAGE_MASK)/HPAGE_SIZE <= free_huge_pages;
  214. }
  215. /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
  216. unsigned long hugetlb_total_pages(void)
  217. {
  218. return nr_huge_pages * (HPAGE_SIZE / PAGE_SIZE);
  219. }
  220. /*
  221. * We cannot handle pagefaults against hugetlb pages at all. They cause
  222. * handle_mm_fault() to try to instantiate regular-sized pages in the
  223. * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
  224. * this far.
  225. */
  226. static struct page *hugetlb_nopage(struct vm_area_struct *vma,
  227. unsigned long address, int *unused)
  228. {
  229. BUG();
  230. return NULL;
  231. }
  232. struct vm_operations_struct hugetlb_vm_ops = {
  233. .nopage = hugetlb_nopage,
  234. };
  235. static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
  236. int writable)
  237. {
  238. pte_t entry;
  239. if (writable) {
  240. entry =
  241. pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
  242. } else {
  243. entry = pte_wrprotect(mk_pte(page, vma->vm_page_prot));
  244. }
  245. entry = pte_mkyoung(entry);
  246. entry = pte_mkhuge(entry);
  247. return entry;
  248. }
  249. static void set_huge_ptep_writable(struct vm_area_struct *vma,
  250. unsigned long address, pte_t *ptep)
  251. {
  252. pte_t entry;
  253. entry = pte_mkwrite(pte_mkdirty(*ptep));
  254. ptep_set_access_flags(vma, address, ptep, entry, 1);
  255. update_mmu_cache(vma, address, entry);
  256. lazy_mmu_prot_update(entry);
  257. }
  258. int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
  259. struct vm_area_struct *vma)
  260. {
  261. pte_t *src_pte, *dst_pte, entry;
  262. struct page *ptepage;
  263. unsigned long addr;
  264. int cow;
  265. cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
  266. for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
  267. src_pte = huge_pte_offset(src, addr);
  268. if (!src_pte)
  269. continue;
  270. dst_pte = huge_pte_alloc(dst, addr);
  271. if (!dst_pte)
  272. goto nomem;
  273. spin_lock(&dst->page_table_lock);
  274. spin_lock(&src->page_table_lock);
  275. if (!pte_none(*src_pte)) {
  276. if (cow)
  277. ptep_set_wrprotect(src, addr, src_pte);
  278. entry = *src_pte;
  279. ptepage = pte_page(entry);
  280. get_page(ptepage);
  281. add_mm_counter(dst, file_rss, HPAGE_SIZE / PAGE_SIZE);
  282. set_huge_pte_at(dst, addr, dst_pte, entry);
  283. }
  284. spin_unlock(&src->page_table_lock);
  285. spin_unlock(&dst->page_table_lock);
  286. }
  287. return 0;
  288. nomem:
  289. return -ENOMEM;
  290. }
  291. void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
  292. unsigned long end)
  293. {
  294. struct mm_struct *mm = vma->vm_mm;
  295. unsigned long address;
  296. pte_t *ptep;
  297. pte_t pte;
  298. struct page *page;
  299. WARN_ON(!is_vm_hugetlb_page(vma));
  300. BUG_ON(start & ~HPAGE_MASK);
  301. BUG_ON(end & ~HPAGE_MASK);
  302. spin_lock(&mm->page_table_lock);
  303. /* Update high watermark before we lower rss */
  304. update_hiwater_rss(mm);
  305. for (address = start; address < end; address += HPAGE_SIZE) {
  306. ptep = huge_pte_offset(mm, address);
  307. if (!ptep)
  308. continue;
  309. pte = huge_ptep_get_and_clear(mm, address, ptep);
  310. if (pte_none(pte))
  311. continue;
  312. page = pte_page(pte);
  313. put_page(page);
  314. add_mm_counter(mm, file_rss, (int) -(HPAGE_SIZE / PAGE_SIZE));
  315. }
  316. spin_unlock(&mm->page_table_lock);
  317. flush_tlb_range(vma, start, end);
  318. }
  319. static struct page *find_or_alloc_huge_page(struct address_space *mapping,
  320. unsigned long idx, int shared)
  321. {
  322. struct page *page;
  323. int err;
  324. retry:
  325. page = find_lock_page(mapping, idx);
  326. if (page)
  327. goto out;
  328. if (hugetlb_get_quota(mapping))
  329. goto out;
  330. page = alloc_huge_page();
  331. if (!page) {
  332. hugetlb_put_quota(mapping);
  333. goto out;
  334. }
  335. if (shared) {
  336. err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
  337. if (err) {
  338. put_page(page);
  339. hugetlb_put_quota(mapping);
  340. if (err == -EEXIST)
  341. goto retry;
  342. page = NULL;
  343. }
  344. } else {
  345. /* Caller expects a locked page */
  346. lock_page(page);
  347. }
  348. out:
  349. return page;
  350. }
  351. static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
  352. unsigned long address, pte_t *ptep, pte_t pte)
  353. {
  354. struct page *old_page, *new_page;
  355. int i, avoidcopy;
  356. old_page = pte_page(pte);
  357. /* If no-one else is actually using this page, avoid the copy
  358. * and just make the page writable */
  359. avoidcopy = (page_count(old_page) == 1);
  360. if (avoidcopy) {
  361. set_huge_ptep_writable(vma, address, ptep);
  362. return VM_FAULT_MINOR;
  363. }
  364. page_cache_get(old_page);
  365. new_page = alloc_huge_page();
  366. if (!new_page) {
  367. page_cache_release(old_page);
  368. /* Logically this is OOM, not a SIGBUS, but an OOM
  369. * could cause the kernel to go killing other
  370. * processes which won't help the hugepage situation
  371. * at all (?) */
  372. return VM_FAULT_SIGBUS;
  373. }
  374. spin_unlock(&mm->page_table_lock);
  375. for (i = 0; i < HPAGE_SIZE/PAGE_SIZE; i++)
  376. copy_user_highpage(new_page + i, old_page + i,
  377. address + i*PAGE_SIZE);
  378. spin_lock(&mm->page_table_lock);
  379. ptep = huge_pte_offset(mm, address & HPAGE_MASK);
  380. if (likely(pte_same(*ptep, pte))) {
  381. /* Break COW */
  382. set_huge_pte_at(mm, address, ptep,
  383. make_huge_pte(vma, new_page, 1));
  384. /* Make the old page be freed below */
  385. new_page = old_page;
  386. }
  387. page_cache_release(new_page);
  388. page_cache_release(old_page);
  389. return VM_FAULT_MINOR;
  390. }
  391. int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
  392. unsigned long address, pte_t *ptep, int write_access)
  393. {
  394. int ret = VM_FAULT_SIGBUS;
  395. unsigned long idx;
  396. unsigned long size;
  397. struct page *page;
  398. struct address_space *mapping;
  399. pte_t new_pte;
  400. mapping = vma->vm_file->f_mapping;
  401. idx = ((address - vma->vm_start) >> HPAGE_SHIFT)
  402. + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
  403. /*
  404. * Use page lock to guard against racing truncation
  405. * before we get page_table_lock.
  406. */
  407. page = find_or_alloc_huge_page(mapping, idx,
  408. vma->vm_flags & VM_SHARED);
  409. if (!page)
  410. goto out;
  411. BUG_ON(!PageLocked(page));
  412. spin_lock(&mm->page_table_lock);
  413. size = i_size_read(mapping->host) >> HPAGE_SHIFT;
  414. if (idx >= size)
  415. goto backout;
  416. ret = VM_FAULT_MINOR;
  417. if (!pte_none(*ptep))
  418. goto backout;
  419. add_mm_counter(mm, file_rss, HPAGE_SIZE / PAGE_SIZE);
  420. new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
  421. && (vma->vm_flags & VM_SHARED)));
  422. set_huge_pte_at(mm, address, ptep, new_pte);
  423. if (write_access && !(vma->vm_flags & VM_SHARED)) {
  424. /* Optimization, do the COW without a second fault */
  425. ret = hugetlb_cow(mm, vma, address, ptep, new_pte);
  426. }
  427. spin_unlock(&mm->page_table_lock);
  428. unlock_page(page);
  429. out:
  430. return ret;
  431. backout:
  432. spin_unlock(&mm->page_table_lock);
  433. hugetlb_put_quota(mapping);
  434. unlock_page(page);
  435. put_page(page);
  436. goto out;
  437. }
  438. int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  439. unsigned long address, int write_access)
  440. {
  441. pte_t *ptep;
  442. pte_t entry;
  443. int ret;
  444. ptep = huge_pte_alloc(mm, address);
  445. if (!ptep)
  446. return VM_FAULT_OOM;
  447. entry = *ptep;
  448. if (pte_none(entry))
  449. return hugetlb_no_page(mm, vma, address, ptep, write_access);
  450. ret = VM_FAULT_MINOR;
  451. spin_lock(&mm->page_table_lock);
  452. /* Check for a racing update before calling hugetlb_cow */
  453. if (likely(pte_same(entry, *ptep)))
  454. if (write_access && !pte_write(entry))
  455. ret = hugetlb_cow(mm, vma, address, ptep, entry);
  456. spin_unlock(&mm->page_table_lock);
  457. return ret;
  458. }
  459. int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
  460. struct page **pages, struct vm_area_struct **vmas,
  461. unsigned long *position, int *length, int i)
  462. {
  463. unsigned long vpfn, vaddr = *position;
  464. int remainder = *length;
  465. vpfn = vaddr/PAGE_SIZE;
  466. spin_lock(&mm->page_table_lock);
  467. while (vaddr < vma->vm_end && remainder) {
  468. pte_t *pte;
  469. struct page *page;
  470. /*
  471. * Some archs (sparc64, sh*) have multiple pte_ts to
  472. * each hugepage. We have to make * sure we get the
  473. * first, for the page indexing below to work.
  474. */
  475. pte = huge_pte_offset(mm, vaddr & HPAGE_MASK);
  476. if (!pte || pte_none(*pte)) {
  477. int ret;
  478. spin_unlock(&mm->page_table_lock);
  479. ret = hugetlb_fault(mm, vma, vaddr, 0);
  480. spin_lock(&mm->page_table_lock);
  481. if (ret == VM_FAULT_MINOR)
  482. continue;
  483. remainder = 0;
  484. if (!i)
  485. i = -EFAULT;
  486. break;
  487. }
  488. if (pages) {
  489. page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)];
  490. get_page(page);
  491. pages[i] = page;
  492. }
  493. if (vmas)
  494. vmas[i] = vma;
  495. vaddr += PAGE_SIZE;
  496. ++vpfn;
  497. --remainder;
  498. ++i;
  499. }
  500. spin_unlock(&mm->page_table_lock);
  501. *length = remainder;
  502. *position = vaddr;
  503. return i;
  504. }