page_cgroup.c 5.7 KB

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