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 #include <linux/vmalloc.h>
11 __init_page_cgroup(struct page_cgroup *pc, unsigned long pfn)
14 pc->mem_cgroup = NULL;
15 pc->page = pfn_to_page(pfn);
17 static unsigned long total_usage;
19 #if !defined(CONFIG_SPARSEMEM)
22 void __init pgdat_page_cgroup_init(struct pglist_data *pgdat)
24 pgdat->node_page_cgroup = NULL;
27 struct page_cgroup *lookup_page_cgroup(struct page *page)
29 unsigned long pfn = page_to_pfn(page);
31 struct page_cgroup *base;
33 base = NODE_DATA(page_to_nid(page))->node_page_cgroup;
37 offset = pfn - NODE_DATA(page_to_nid(page))->node_start_pfn;
41 static int __init alloc_node_page_cgroup(int nid)
43 struct page_cgroup *base, *pc;
44 unsigned long table_size;
45 unsigned long start_pfn, nr_pages, index;
47 start_pfn = NODE_DATA(nid)->node_start_pfn;
48 nr_pages = NODE_DATA(nid)->node_spanned_pages;
50 table_size = sizeof(struct page_cgroup) * nr_pages;
52 base = __alloc_bootmem_node_nopanic(NODE_DATA(nid),
53 table_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS));
56 for (index = 0; index < nr_pages; index++) {
58 __init_page_cgroup(pc, start_pfn + index);
60 NODE_DATA(nid)->node_page_cgroup = base;
61 total_usage += table_size;
65 void __init page_cgroup_init(void)
70 for_each_online_node(nid) {
71 fail = alloc_node_page_cgroup(nid);
75 printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
76 printk(KERN_INFO "please try cgroup_disable=memory option if you"
80 printk(KERN_CRIT "allocation of page_cgroup was failed.\n");
81 printk(KERN_CRIT "please try cgroup_disable=memory boot option\n");
82 panic("Out of memory");
85 #else /* CONFIG_FLAT_NODE_MEM_MAP */
87 struct page_cgroup *lookup_page_cgroup(struct page *page)
89 unsigned long pfn = page_to_pfn(page);
90 struct mem_section *section = __pfn_to_section(pfn);
92 return section->page_cgroup + pfn;
95 int __meminit init_section_page_cgroup(unsigned long pfn)
97 struct mem_section *section;
98 struct page_cgroup *base, *pc;
99 unsigned long table_size;
102 section = __pfn_to_section(pfn);
104 if (section->page_cgroup)
107 nid = page_to_nid(pfn_to_page(pfn));
109 table_size = sizeof(struct page_cgroup) * PAGES_PER_SECTION;
110 base = kmalloc_node(table_size, GFP_KERNEL, nid);
112 base = vmalloc_node(table_size, nid);
115 printk(KERN_ERR "page cgroup allocation failure\n");
119 for (index = 0; index < PAGES_PER_SECTION; index++) {
121 __init_page_cgroup(pc, pfn + index);
124 section = __pfn_to_section(pfn);
125 section->page_cgroup = base - pfn;
126 total_usage += table_size;
129 #ifdef CONFIG_MEMORY_HOTPLUG
130 void __free_page_cgroup(unsigned long pfn)
132 struct mem_section *ms;
133 struct page_cgroup *base;
135 ms = __pfn_to_section(pfn);
136 if (!ms || !ms->page_cgroup)
138 base = ms->page_cgroup + pfn;
139 ms->page_cgroup = NULL;
140 if (is_vmalloc_addr(base))
146 int online_page_cgroup(unsigned long start_pfn,
147 unsigned long nr_pages,
150 unsigned long start, end, pfn;
153 start = start_pfn & (PAGES_PER_SECTION - 1);
154 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))
159 fail = init_section_page_cgroup(pfn);
165 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
166 __free_page_cgroup(pfn);
171 int offline_page_cgroup(unsigned long start_pfn,
172 unsigned long nr_pages, int nid)
174 unsigned long start, end, pfn;
176 start = start_pfn & (PAGES_PER_SECTION - 1);
177 end = ALIGN(start_pfn + nr_pages, PAGES_PER_SECTION);
179 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
180 __free_page_cgroup(pfn);
185 static int page_cgroup_callback(struct notifier_block *self,
186 unsigned long action, void *arg)
188 struct memory_notify *mn = arg;
191 case MEM_GOING_ONLINE:
192 ret = online_page_cgroup(mn->start_pfn,
193 mn->nr_pages, mn->status_change_nid);
195 case MEM_CANCEL_ONLINE:
197 offline_page_cgroup(mn->start_pfn,
198 mn->nr_pages, mn->status_change_nid);
200 case MEM_GOING_OFFLINE:
203 case MEM_CANCEL_OFFLINE:
206 ret = notifier_from_errno(ret);
212 void __init page_cgroup_init(void)
217 for (pfn = 0; !fail && pfn < max_pfn; pfn += PAGES_PER_SECTION) {
218 if (!pfn_present(pfn))
220 fail = init_section_page_cgroup(pfn);
223 printk(KERN_CRIT "try cgroup_disable=memory boot option\n");
224 panic("Out of memory");
226 hotplug_memory_notifier(page_cgroup_callback, 0);
228 printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
229 printk(KERN_INFO "please try cgroup_disable=memory option if you don't"
233 void __init pgdat_page_cgroup_init(struct pglist_data *pgdat)