page_cgroup.c 6.1 KB

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