page_cgroup.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. table_size = sizeof(struct page_cgroup) * nr_pages;
  43. base = __alloc_bootmem_node_nopanic(NODE_DATA(nid),
  44. table_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS));
  45. if (!base)
  46. return -ENOMEM;
  47. for (index = 0; index < nr_pages; index++) {
  48. pc = base + index;
  49. __init_page_cgroup(pc, start_pfn + index);
  50. }
  51. NODE_DATA(nid)->node_page_cgroup = base;
  52. total_usage += table_size;
  53. return 0;
  54. }
  55. void __init page_cgroup_init(void)
  56. {
  57. int nid, fail;
  58. if (mem_cgroup_subsys.disabled)
  59. return;
  60. for_each_online_node(nid) {
  61. fail = alloc_node_page_cgroup(nid);
  62. if (fail)
  63. goto fail;
  64. }
  65. printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
  66. printk(KERN_INFO "please try cgroup_disable=memory option if you"
  67. " don't want\n");
  68. return;
  69. fail:
  70. printk(KERN_CRIT "allocation of page_cgroup was failed.\n");
  71. printk(KERN_CRIT "please try cgroup_disable=memory boot option\n");
  72. panic("Out of memory");
  73. }
  74. #else /* CONFIG_FLAT_NODE_MEM_MAP */
  75. struct page_cgroup *lookup_page_cgroup(struct page *page)
  76. {
  77. unsigned long pfn = page_to_pfn(page);
  78. struct mem_section *section = __pfn_to_section(pfn);
  79. return section->page_cgroup + pfn;
  80. }
  81. /* __alloc_bootmem...() is protected by !slab_available() */
  82. int __init_refok init_section_page_cgroup(unsigned long pfn)
  83. {
  84. struct mem_section *section;
  85. struct page_cgroup *base, *pc;
  86. unsigned long table_size;
  87. int nid, index;
  88. section = __pfn_to_section(pfn);
  89. if (section->page_cgroup)
  90. return 0;
  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), table_size,
  99. PAGE_SIZE, __pa(MAX_DMA_ADDRESS));
  100. }
  101. if (!base) {
  102. printk(KERN_ERR "page cgroup allocation failure\n");
  103. return -ENOMEM;
  104. }
  105. for (index = 0; index < PAGES_PER_SECTION; index++) {
  106. pc = base + index;
  107. __init_page_cgroup(pc, pfn + index);
  108. }
  109. section = __pfn_to_section(pfn);
  110. section->page_cgroup = base - pfn;
  111. total_usage += table_size;
  112. return 0;
  113. }
  114. #ifdef CONFIG_MEMORY_HOTPLUG
  115. void __free_page_cgroup(unsigned long pfn)
  116. {
  117. struct mem_section *ms;
  118. struct page_cgroup *base;
  119. ms = __pfn_to_section(pfn);
  120. if (!ms || !ms->page_cgroup)
  121. return;
  122. base = ms->page_cgroup + pfn;
  123. if (is_vmalloc_addr(base)) {
  124. vfree(base);
  125. ms->page_cgroup = NULL;
  126. } else {
  127. struct page *page = virt_to_page(base);
  128. if (!PageReserved(page)) { /* Is bootmem ? */
  129. kfree(base);
  130. ms->page_cgroup = NULL;
  131. }
  132. }
  133. }
  134. int __meminit online_page_cgroup(unsigned long start_pfn,
  135. unsigned long nr_pages,
  136. int nid)
  137. {
  138. unsigned long start, end, pfn;
  139. int fail = 0;
  140. start = start_pfn & ~(PAGES_PER_SECTION - 1);
  141. end = ALIGN(start_pfn + nr_pages, PAGES_PER_SECTION);
  142. for (pfn = start; !fail && pfn < end; pfn += PAGES_PER_SECTION) {
  143. if (!pfn_present(pfn))
  144. continue;
  145. fail = init_section_page_cgroup(pfn);
  146. }
  147. if (!fail)
  148. return 0;
  149. /* rollback */
  150. for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
  151. __free_page_cgroup(pfn);
  152. return -ENOMEM;
  153. }
  154. int __meminit offline_page_cgroup(unsigned long start_pfn,
  155. unsigned long nr_pages, int nid)
  156. {
  157. unsigned long start, end, pfn;
  158. start = start_pfn & ~(PAGES_PER_SECTION - 1);
  159. end = ALIGN(start_pfn + nr_pages, PAGES_PER_SECTION);
  160. for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
  161. __free_page_cgroup(pfn);
  162. return 0;
  163. }
  164. static int __meminit page_cgroup_callback(struct notifier_block *self,
  165. unsigned long action, void *arg)
  166. {
  167. struct memory_notify *mn = arg;
  168. int ret = 0;
  169. switch (action) {
  170. case MEM_GOING_ONLINE:
  171. ret = online_page_cgroup(mn->start_pfn,
  172. mn->nr_pages, mn->status_change_nid);
  173. break;
  174. case MEM_CANCEL_ONLINE:
  175. case MEM_OFFLINE:
  176. offline_page_cgroup(mn->start_pfn,
  177. mn->nr_pages, mn->status_change_nid);
  178. break;
  179. case MEM_GOING_OFFLINE:
  180. break;
  181. case MEM_ONLINE:
  182. case MEM_CANCEL_OFFLINE:
  183. break;
  184. }
  185. ret = notifier_from_errno(ret);
  186. return ret;
  187. }
  188. #endif
  189. void __init page_cgroup_init(void)
  190. {
  191. unsigned long pfn;
  192. int fail = 0;
  193. if (mem_cgroup_subsys.disabled)
  194. return;
  195. for (pfn = 0; !fail && pfn < max_pfn; pfn += PAGES_PER_SECTION) {
  196. if (!pfn_present(pfn))
  197. continue;
  198. fail = init_section_page_cgroup(pfn);
  199. }
  200. if (fail) {
  201. printk(KERN_CRIT "try cgroup_disable=memory boot option\n");
  202. panic("Out of memory");
  203. } else {
  204. hotplug_memory_notifier(page_cgroup_callback, 0);
  205. }
  206. printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
  207. printk(KERN_INFO "please try cgroup_disable=memory option if you don't"
  208. " want\n");
  209. }
  210. void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat)
  211. {
  212. return;
  213. }
  214. #endif