page_cgroup.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #include <linux/mm.h>
  2. #include <linux/mmzone.h>
  3. #include <linux/bootmem.h>
  4. #include <linux/bit_spinlock.h>
  5. #include <linux/page_cgroup.h>
  6. #include <linux/hash.h>
  7. #include <linux/memory.h>
  8. static void __meminit
  9. __init_page_cgroup(struct page_cgroup *pc, unsigned long pfn)
  10. {
  11. pc->flags = 0;
  12. pc->mem_cgroup = NULL;
  13. pc->page = pfn_to_page(pfn);
  14. }
  15. static unsigned long total_usage;
  16. #if !defined(CONFIG_SPARSEMEM)
  17. void __init pgdat_page_cgroup_init(struct pglist_data *pgdat)
  18. {
  19. pgdat->node_page_cgroup = NULL;
  20. }
  21. struct page_cgroup *lookup_page_cgroup(struct page *page)
  22. {
  23. unsigned long pfn = page_to_pfn(page);
  24. unsigned long offset;
  25. struct page_cgroup *base;
  26. base = NODE_DATA(page_to_nid(page))->node_page_cgroup;
  27. if (unlikely(!base))
  28. return NULL;
  29. offset = pfn - NODE_DATA(page_to_nid(page))->node_start_pfn;
  30. return base + offset;
  31. }
  32. static int __init alloc_node_page_cgroup(int nid)
  33. {
  34. struct page_cgroup *base, *pc;
  35. unsigned long table_size;
  36. unsigned long start_pfn, nr_pages, index;
  37. start_pfn = NODE_DATA(nid)->node_start_pfn;
  38. nr_pages = NODE_DATA(nid)->node_spanned_pages;
  39. table_size = sizeof(struct page_cgroup) * nr_pages;
  40. base = __alloc_bootmem_node_nopanic(NODE_DATA(nid),
  41. table_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS));
  42. if (!base)
  43. return -ENOMEM;
  44. for (index = 0; index < nr_pages; index++) {
  45. pc = base + index;
  46. __init_page_cgroup(pc, start_pfn + index);
  47. }
  48. NODE_DATA(nid)->node_page_cgroup = base;
  49. total_usage += table_size;
  50. return 0;
  51. }
  52. void __init page_cgroup_init(void)
  53. {
  54. int nid, fail;
  55. for_each_online_node(nid) {
  56. fail = alloc_node_page_cgroup(nid);
  57. if (fail)
  58. goto fail;
  59. }
  60. printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
  61. printk(KERN_INFO "please try cgroup_disable=memory option if you"
  62. " don't want\n");
  63. return;
  64. fail:
  65. printk(KERN_CRIT "allocation of page_cgroup was failed.\n");
  66. printk(KERN_CRIT "please try cgroup_disable=memory boot option\n");
  67. panic("Out of memory");
  68. }
  69. #else /* CONFIG_FLAT_NODE_MEM_MAP */
  70. struct page_cgroup *lookup_page_cgroup(struct page *page)
  71. {
  72. unsigned long pfn = page_to_pfn(page);
  73. struct mem_section *section = __pfn_to_section(pfn);
  74. return section->page_cgroup + pfn;
  75. }
  76. int __meminit init_section_page_cgroup(unsigned long pfn)
  77. {
  78. struct mem_section *section;
  79. struct page_cgroup *base, *pc;
  80. unsigned long table_size;
  81. int nid, index;
  82. section = __pfn_to_section(pfn);
  83. if (section->page_cgroup)
  84. return 0;
  85. nid = page_to_nid(pfn_to_page(pfn));
  86. table_size = sizeof(struct page_cgroup) * PAGES_PER_SECTION;
  87. base = kmalloc_node(table_size, GFP_KERNEL, nid);
  88. if (!base)
  89. base = vmalloc_node(table_size, nid);
  90. if (!base) {
  91. printk(KERN_ERR "page cgroup allocation failure\n");
  92. return -ENOMEM;
  93. }
  94. for (index = 0; index < PAGES_PER_SECTION; index++) {
  95. pc = base + index;
  96. __init_page_cgroup(pc, pfn + index);
  97. }
  98. section = __pfn_to_section(pfn);
  99. section->page_cgroup = base - pfn;
  100. total_usage += table_size;
  101. return 0;
  102. }
  103. #ifdef CONFIG_MEMORY_HOTPLUG
  104. void __free_page_cgroup(unsigned long pfn)
  105. {
  106. struct mem_section *ms;
  107. struct page_cgroup *base;
  108. ms = __pfn_to_section(pfn);
  109. if (!ms || !ms->page_cgroup)
  110. return;
  111. base = ms->page_cgroup + pfn;
  112. ms->page_cgroup = NULL;
  113. if (is_vmalloc_addr(base))
  114. vfree(base);
  115. else
  116. kfree(base);
  117. }
  118. int online_page_cgroup(unsigned long start_pfn,
  119. unsigned long nr_pages,
  120. int nid)
  121. {
  122. unsigned long start, end, pfn;
  123. int fail = 0;
  124. start = start_pfn & (PAGES_PER_SECTION - 1);
  125. end = ALIGN(start_pfn + nr_pages, PAGES_PER_SECTION);
  126. for (pfn = start; !fail && pfn < end; pfn += PAGES_PER_SECTION) {
  127. if (!pfn_present(pfn))
  128. continue;
  129. fail = init_section_page_cgroup(pfn);
  130. }
  131. if (!fail)
  132. return 0;
  133. /* rollback */
  134. for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
  135. __free_page_cgroup(pfn);
  136. return -ENOMEM;
  137. }
  138. int offline_page_cgroup(unsigned long start_pfn,
  139. unsigned long nr_pages, int nid)
  140. {
  141. unsigned long start, end, pfn;
  142. start = start_pfn & (PAGES_PER_SECTION - 1);
  143. end = ALIGN(start_pfn + nr_pages, PAGES_PER_SECTION);
  144. for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
  145. __free_page_cgroup(pfn);
  146. return 0;
  147. }
  148. static int page_cgroup_callback(struct notifier_block *self,
  149. unsigned long action, void *arg)
  150. {
  151. struct memory_notify *mn = arg;
  152. int ret = 0;
  153. switch (action) {
  154. case MEM_GOING_ONLINE:
  155. ret = online_page_cgroup(mn->start_pfn,
  156. mn->nr_pages, mn->status_change_nid);
  157. break;
  158. case MEM_CANCEL_ONLINE:
  159. case MEM_OFFLINE:
  160. offline_page_cgroup(mn->start_pfn,
  161. mn->nr_pages, mn->status_change_nid);
  162. break;
  163. case MEM_GOING_OFFLINE:
  164. break;
  165. case MEM_ONLINE:
  166. case MEM_CANCEL_OFFLINE:
  167. break;
  168. }
  169. ret = notifier_from_errno(ret);
  170. return ret;
  171. }
  172. #endif
  173. void __init page_cgroup_init(void)
  174. {
  175. unsigned long pfn;
  176. int fail = 0;
  177. for (pfn = 0; !fail && pfn < max_pfn; pfn += PAGES_PER_SECTION) {
  178. if (!pfn_present(pfn))
  179. continue;
  180. fail = init_section_page_cgroup(pfn);
  181. }
  182. if (fail) {
  183. printk(KERN_CRIT "try cgroup_disable=memory boot option\n");
  184. panic("Out of memory");
  185. } else {
  186. hotplug_memory_notifier(page_cgroup_callback, 0);
  187. }
  188. printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
  189. printk(KERN_INFO "please try cgroup_disable=memory option if you don't"
  190. " want\n");
  191. }
  192. void __init pgdat_page_cgroup_init(struct pglist_data *pgdat)
  193. {
  194. return;
  195. }
  196. #endif