hugetlb.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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/hugetlb.h>
  11. #include <linux/sysctl.h>
  12. #include <linux/highmem.h>
  13. #include <linux/nodemask.h>
  14. const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL;
  15. static unsigned long nr_huge_pages, free_huge_pages;
  16. unsigned long max_huge_pages;
  17. static struct list_head hugepage_freelists[MAX_NUMNODES];
  18. static unsigned int nr_huge_pages_node[MAX_NUMNODES];
  19. static unsigned int free_huge_pages_node[MAX_NUMNODES];
  20. static DEFINE_SPINLOCK(hugetlb_lock);
  21. static void enqueue_huge_page(struct page *page)
  22. {
  23. int nid = page_to_nid(page);
  24. list_add(&page->lru, &hugepage_freelists[nid]);
  25. free_huge_pages++;
  26. free_huge_pages_node[nid]++;
  27. }
  28. static struct page *dequeue_huge_page(void)
  29. {
  30. int nid = numa_node_id();
  31. struct page *page = NULL;
  32. if (list_empty(&hugepage_freelists[nid])) {
  33. for (nid = 0; nid < MAX_NUMNODES; ++nid)
  34. if (!list_empty(&hugepage_freelists[nid]))
  35. break;
  36. }
  37. if (nid >= 0 && nid < MAX_NUMNODES &&
  38. !list_empty(&hugepage_freelists[nid])) {
  39. page = list_entry(hugepage_freelists[nid].next,
  40. struct page, lru);
  41. list_del(&page->lru);
  42. free_huge_pages--;
  43. free_huge_pages_node[nid]--;
  44. }
  45. return page;
  46. }
  47. static struct page *alloc_fresh_huge_page(void)
  48. {
  49. static int nid = 0;
  50. struct page *page;
  51. page = alloc_pages_node(nid, GFP_HIGHUSER|__GFP_COMP|__GFP_NOWARN,
  52. HUGETLB_PAGE_ORDER);
  53. nid = (nid + 1) % num_online_nodes();
  54. if (page) {
  55. nr_huge_pages++;
  56. nr_huge_pages_node[page_to_nid(page)]++;
  57. }
  58. return page;
  59. }
  60. void free_huge_page(struct page *page)
  61. {
  62. BUG_ON(page_count(page));
  63. INIT_LIST_HEAD(&page->lru);
  64. page[1].mapping = NULL;
  65. spin_lock(&hugetlb_lock);
  66. enqueue_huge_page(page);
  67. spin_unlock(&hugetlb_lock);
  68. }
  69. struct page *alloc_huge_page(void)
  70. {
  71. struct page *page;
  72. int i;
  73. spin_lock(&hugetlb_lock);
  74. page = dequeue_huge_page();
  75. if (!page) {
  76. spin_unlock(&hugetlb_lock);
  77. return NULL;
  78. }
  79. spin_unlock(&hugetlb_lock);
  80. set_page_count(page, 1);
  81. page[1].mapping = (void *)free_huge_page;
  82. for (i = 0; i < (HPAGE_SIZE/PAGE_SIZE); ++i)
  83. clear_highpage(&page[i]);
  84. return page;
  85. }
  86. static int __init hugetlb_init(void)
  87. {
  88. unsigned long i;
  89. struct page *page;
  90. for (i = 0; i < MAX_NUMNODES; ++i)
  91. INIT_LIST_HEAD(&hugepage_freelists[i]);
  92. for (i = 0; i < max_huge_pages; ++i) {
  93. page = alloc_fresh_huge_page();
  94. if (!page)
  95. break;
  96. spin_lock(&hugetlb_lock);
  97. enqueue_huge_page(page);
  98. spin_unlock(&hugetlb_lock);
  99. }
  100. max_huge_pages = free_huge_pages = nr_huge_pages = i;
  101. printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages);
  102. return 0;
  103. }
  104. module_init(hugetlb_init);
  105. static int __init hugetlb_setup(char *s)
  106. {
  107. if (sscanf(s, "%lu", &max_huge_pages) <= 0)
  108. max_huge_pages = 0;
  109. return 1;
  110. }
  111. __setup("hugepages=", hugetlb_setup);
  112. #ifdef CONFIG_SYSCTL
  113. static void update_and_free_page(struct page *page)
  114. {
  115. int i;
  116. nr_huge_pages--;
  117. nr_huge_pages_node[page_zone(page)->zone_pgdat->node_id]--;
  118. for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++) {
  119. page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
  120. 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
  121. 1 << PG_private | 1<< PG_writeback);
  122. set_page_count(&page[i], 0);
  123. }
  124. set_page_count(page, 1);
  125. __free_pages(page, HUGETLB_PAGE_ORDER);
  126. }
  127. #ifdef CONFIG_HIGHMEM
  128. static void try_to_free_low(unsigned long count)
  129. {
  130. int i, nid;
  131. for (i = 0; i < MAX_NUMNODES; ++i) {
  132. struct page *page, *next;
  133. list_for_each_entry_safe(page, next, &hugepage_freelists[i], lru) {
  134. if (PageHighMem(page))
  135. continue;
  136. list_del(&page->lru);
  137. update_and_free_page(page);
  138. nid = page_zone(page)->zone_pgdat->node_id;
  139. free_huge_pages--;
  140. free_huge_pages_node[nid]--;
  141. if (count >= nr_huge_pages)
  142. return;
  143. }
  144. }
  145. }
  146. #else
  147. static inline void try_to_free_low(unsigned long count)
  148. {
  149. }
  150. #endif
  151. static unsigned long set_max_huge_pages(unsigned long count)
  152. {
  153. while (count > nr_huge_pages) {
  154. struct page *page = alloc_fresh_huge_page();
  155. if (!page)
  156. return nr_huge_pages;
  157. spin_lock(&hugetlb_lock);
  158. enqueue_huge_page(page);
  159. spin_unlock(&hugetlb_lock);
  160. }
  161. if (count >= nr_huge_pages)
  162. return nr_huge_pages;
  163. spin_lock(&hugetlb_lock);
  164. try_to_free_low(count);
  165. while (count < nr_huge_pages) {
  166. struct page *page = dequeue_huge_page();
  167. if (!page)
  168. break;
  169. update_and_free_page(page);
  170. }
  171. spin_unlock(&hugetlb_lock);
  172. return nr_huge_pages;
  173. }
  174. int hugetlb_sysctl_handler(struct ctl_table *table, int write,
  175. struct file *file, void __user *buffer,
  176. size_t *length, loff_t *ppos)
  177. {
  178. proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
  179. max_huge_pages = set_max_huge_pages(max_huge_pages);
  180. return 0;
  181. }
  182. #endif /* CONFIG_SYSCTL */
  183. int hugetlb_report_meminfo(char *buf)
  184. {
  185. return sprintf(buf,
  186. "HugePages_Total: %5lu\n"
  187. "HugePages_Free: %5lu\n"
  188. "Hugepagesize: %5lu kB\n",
  189. nr_huge_pages,
  190. free_huge_pages,
  191. HPAGE_SIZE/1024);
  192. }
  193. int hugetlb_report_node_meminfo(int nid, char *buf)
  194. {
  195. return sprintf(buf,
  196. "Node %d HugePages_Total: %5u\n"
  197. "Node %d HugePages_Free: %5u\n",
  198. nid, nr_huge_pages_node[nid],
  199. nid, free_huge_pages_node[nid]);
  200. }
  201. int is_hugepage_mem_enough(size_t size)
  202. {
  203. return (size + ~HPAGE_MASK)/HPAGE_SIZE <= free_huge_pages;
  204. }
  205. /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
  206. unsigned long hugetlb_total_pages(void)
  207. {
  208. return nr_huge_pages * (HPAGE_SIZE / PAGE_SIZE);
  209. }
  210. EXPORT_SYMBOL(hugetlb_total_pages);
  211. /*
  212. * We cannot handle pagefaults against hugetlb pages at all. They cause
  213. * handle_mm_fault() to try to instantiate regular-sized pages in the
  214. * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
  215. * this far.
  216. */
  217. static struct page *hugetlb_nopage(struct vm_area_struct *vma,
  218. unsigned long address, int *unused)
  219. {
  220. BUG();
  221. return NULL;
  222. }
  223. struct vm_operations_struct hugetlb_vm_ops = {
  224. .nopage = hugetlb_nopage,
  225. };
  226. void zap_hugepage_range(struct vm_area_struct *vma,
  227. unsigned long start, unsigned long length)
  228. {
  229. struct mm_struct *mm = vma->vm_mm;
  230. spin_lock(&mm->page_table_lock);
  231. unmap_hugepage_range(vma, start, start + length);
  232. spin_unlock(&mm->page_table_lock);
  233. }