page_cgroup.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. int __init_refok init_section_page_cgroup(unsigned long pfn)
  85. {
  86. struct mem_section *section;
  87. struct page_cgroup *base, *pc;
  88. unsigned long table_size;
  89. int nid, index;
  90. section = __pfn_to_section(pfn);
  91. if (!section->page_cgroup) {
  92. nid = page_to_nid(pfn_to_page(pfn));
  93. table_size = sizeof(struct page_cgroup) * PAGES_PER_SECTION;
  94. if (slab_is_available()) {
  95. base = kmalloc_node(table_size, GFP_KERNEL, nid);
  96. if (!base)
  97. base = vmalloc_node(table_size, nid);
  98. } else {
  99. base = __alloc_bootmem_node_nopanic(NODE_DATA(nid),
  100. table_size,
  101. PAGE_SIZE, __pa(MAX_DMA_ADDRESS));
  102. }
  103. } else {
  104. /*
  105. * We don't have to allocate page_cgroup again, but
  106. * address of memmap may be changed. So, we have to initialize
  107. * again.
  108. */
  109. base = section->page_cgroup + pfn;
  110. table_size = 0;
  111. /* check address of memmap is changed or not. */
  112. if (base->page == pfn_to_page(pfn))
  113. return 0;
  114. }
  115. if (!base) {
  116. printk(KERN_ERR "page cgroup allocation failure\n");
  117. return -ENOMEM;
  118. }
  119. for (index = 0; index < PAGES_PER_SECTION; index++) {
  120. pc = base + index;
  121. __init_page_cgroup(pc, pfn + index);
  122. }
  123. section = __pfn_to_section(pfn);
  124. section->page_cgroup = base - pfn;
  125. total_usage += table_size;
  126. return 0;
  127. }
  128. #ifdef CONFIG_MEMORY_HOTPLUG
  129. void __free_page_cgroup(unsigned long pfn)
  130. {
  131. struct mem_section *ms;
  132. struct page_cgroup *base;
  133. ms = __pfn_to_section(pfn);
  134. if (!ms || !ms->page_cgroup)
  135. return;
  136. base = ms->page_cgroup + pfn;
  137. if (is_vmalloc_addr(base)) {
  138. vfree(base);
  139. ms->page_cgroup = NULL;
  140. } else {
  141. struct page *page = virt_to_page(base);
  142. if (!PageReserved(page)) { /* Is bootmem ? */
  143. kfree(base);
  144. ms->page_cgroup = NULL;
  145. }
  146. }
  147. }
  148. int __meminit online_page_cgroup(unsigned long start_pfn,
  149. unsigned long nr_pages,
  150. int nid)
  151. {
  152. unsigned long start, end, pfn;
  153. int fail = 0;
  154. start = start_pfn & ~(PAGES_PER_SECTION - 1);
  155. end = ALIGN(start_pfn + nr_pages, PAGES_PER_SECTION);
  156. for (pfn = start; !fail && pfn < end; pfn += PAGES_PER_SECTION) {
  157. if (!pfn_present(pfn))
  158. continue;
  159. fail = init_section_page_cgroup(pfn);
  160. }
  161. if (!fail)
  162. return 0;
  163. /* rollback */
  164. for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
  165. __free_page_cgroup(pfn);
  166. return -ENOMEM;
  167. }
  168. int __meminit offline_page_cgroup(unsigned long start_pfn,
  169. unsigned long nr_pages, int nid)
  170. {
  171. unsigned long start, end, pfn;
  172. start = start_pfn & ~(PAGES_PER_SECTION - 1);
  173. end = ALIGN(start_pfn + nr_pages, PAGES_PER_SECTION);
  174. for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
  175. __free_page_cgroup(pfn);
  176. return 0;
  177. }
  178. static int __meminit page_cgroup_callback(struct notifier_block *self,
  179. unsigned long action, void *arg)
  180. {
  181. struct memory_notify *mn = arg;
  182. int ret = 0;
  183. switch (action) {
  184. case MEM_GOING_ONLINE:
  185. ret = online_page_cgroup(mn->start_pfn,
  186. mn->nr_pages, mn->status_change_nid);
  187. break;
  188. case MEM_OFFLINE:
  189. offline_page_cgroup(mn->start_pfn,
  190. mn->nr_pages, mn->status_change_nid);
  191. break;
  192. case MEM_CANCEL_ONLINE:
  193. case MEM_GOING_OFFLINE:
  194. break;
  195. case MEM_ONLINE:
  196. case MEM_CANCEL_OFFLINE:
  197. break;
  198. }
  199. if (ret)
  200. ret = notifier_from_errno(ret);
  201. else
  202. ret = NOTIFY_OK;
  203. return ret;
  204. }
  205. #endif
  206. void __init page_cgroup_init(void)
  207. {
  208. unsigned long pfn;
  209. int fail = 0;
  210. if (mem_cgroup_subsys.disabled)
  211. return;
  212. for (pfn = 0; !fail && pfn < max_pfn; pfn += PAGES_PER_SECTION) {
  213. if (!pfn_present(pfn))
  214. continue;
  215. fail = init_section_page_cgroup(pfn);
  216. }
  217. if (fail) {
  218. printk(KERN_CRIT "try cgroup_disable=memory boot option\n");
  219. panic("Out of memory");
  220. } else {
  221. hotplug_memory_notifier(page_cgroup_callback, 0);
  222. }
  223. printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
  224. printk(KERN_INFO "please try cgroup_disable=memory option if you don't"
  225. " want\n");
  226. }
  227. void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat)
  228. {
  229. return;
  230. }
  231. #endif