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 #include <linux/swapops.h>
12 #include <linux/kmemleak.h>
14 static void __meminit init_page_cgroup(struct page_cgroup *pc, unsigned long id)
17 set_page_cgroup_array_id(pc, id);
18 pc->mem_cgroup = NULL;
20 static unsigned long total_usage;
22 #if !defined(CONFIG_SPARSEMEM)
25 void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat)
27 pgdat->node_page_cgroup = NULL;
30 struct page_cgroup *lookup_page_cgroup(struct page *page)
32 unsigned long pfn = page_to_pfn(page);
34 struct page_cgroup *base;
36 base = NODE_DATA(page_to_nid(page))->node_page_cgroup;
40 offset = pfn - NODE_DATA(page_to_nid(page))->node_start_pfn;
44 struct page *lookup_cgroup_page(struct page_cgroup *pc)
50 pgdat = NODE_DATA(page_cgroup_array_id(pc));
51 pfn = pc - pgdat->node_page_cgroup + pgdat->node_start_pfn;
52 page = pfn_to_page(pfn);
53 VM_BUG_ON(pc != lookup_page_cgroup(page));
57 static int __init alloc_node_page_cgroup(int nid)
59 struct page_cgroup *base, *pc;
60 unsigned long table_size;
61 unsigned long start_pfn, nr_pages, index;
63 start_pfn = NODE_DATA(nid)->node_start_pfn;
64 nr_pages = NODE_DATA(nid)->node_spanned_pages;
69 table_size = sizeof(struct page_cgroup) * nr_pages;
71 base = __alloc_bootmem_node_nopanic(NODE_DATA(nid),
72 table_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS));
75 for (index = 0; index < nr_pages; index++) {
77 init_page_cgroup(pc, nid);
79 NODE_DATA(nid)->node_page_cgroup = base;
80 total_usage += table_size;
84 void __init page_cgroup_init_flatmem(void)
89 if (mem_cgroup_disabled())
92 for_each_online_node(nid) {
93 fail = alloc_node_page_cgroup(nid);
97 printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
98 printk(KERN_INFO "please try 'cgroup_disable=memory' option if you"
99 " don't want memory cgroups\n");
102 printk(KERN_CRIT "allocation of page_cgroup failed.\n");
103 printk(KERN_CRIT "please try 'cgroup_disable=memory' boot option\n");
104 panic("Out of memory");
107 #else /* CONFIG_FLAT_NODE_MEM_MAP */
109 struct page_cgroup *lookup_page_cgroup(struct page *page)
111 unsigned long pfn = page_to_pfn(page);
112 struct mem_section *section = __pfn_to_section(pfn);
114 if (!section->page_cgroup)
116 return section->page_cgroup + pfn;
119 struct page *lookup_cgroup_page(struct page_cgroup *pc)
121 struct mem_section *section;
125 nr = page_cgroup_array_id(pc);
126 section = __nr_to_section(nr);
127 page = pfn_to_page(pc - section->page_cgroup);
128 VM_BUG_ON(pc != lookup_page_cgroup(page));
132 static void *__meminit alloc_page_cgroup(size_t size, int nid)
135 gfp_t flags = GFP_KERNEL | __GFP_NOWARN;
137 addr = alloc_pages_exact_nid(nid, size, flags);
139 kmemleak_alloc(addr, size, 1, flags);
143 if (node_state(nid, N_HIGH_MEMORY))
144 addr = vmalloc_node(size, nid);
146 addr = vmalloc(size);
151 #ifdef CONFIG_MEMORY_HOTPLUG
152 static void free_page_cgroup(void *addr)
154 if (is_vmalloc_addr(addr)) {
157 struct page *page = virt_to_page(addr);
159 sizeof(struct page_cgroup) * PAGES_PER_SECTION;
161 BUG_ON(PageReserved(page));
162 free_pages_exact(addr, table_size);
167 static int __meminit init_section_page_cgroup(unsigned long pfn, int nid)
169 struct page_cgroup *base, *pc;
170 struct mem_section *section;
171 unsigned long table_size;
175 nr = pfn_to_section_nr(pfn);
176 section = __nr_to_section(nr);
178 if (section->page_cgroup)
181 table_size = sizeof(struct page_cgroup) * PAGES_PER_SECTION;
182 base = alloc_page_cgroup(table_size, nid);
185 * The value stored in section->page_cgroup is (base - pfn)
186 * and it does not point to the memory block allocated above,
187 * causing kmemleak false positives.
189 kmemleak_not_leak(base);
192 printk(KERN_ERR "page cgroup allocation failure\n");
196 for (index = 0; index < PAGES_PER_SECTION; index++) {
198 init_page_cgroup(pc, nr);
201 * The passed "pfn" may not be aligned to SECTION. For the calculation
202 * we need to apply a mask.
204 pfn &= PAGE_SECTION_MASK;
205 section->page_cgroup = base - pfn;
206 total_usage += table_size;
209 #ifdef CONFIG_MEMORY_HOTPLUG
210 void __free_page_cgroup(unsigned long pfn)
212 struct mem_section *ms;
213 struct page_cgroup *base;
215 ms = __pfn_to_section(pfn);
216 if (!ms || !ms->page_cgroup)
218 base = ms->page_cgroup + pfn;
219 free_page_cgroup(base);
220 ms->page_cgroup = NULL;
223 int __meminit online_page_cgroup(unsigned long start_pfn,
224 unsigned long nr_pages,
227 unsigned long start, end, pfn;
230 start = SECTION_ALIGN_DOWN(start_pfn);
231 end = SECTION_ALIGN_UP(start_pfn + nr_pages);
235 * In this case, "nid" already exists and contains valid memory.
236 * "start_pfn" passed to us is a pfn which is an arg for
237 * online__pages(), and start_pfn should exist.
239 nid = pfn_to_nid(start_pfn);
240 VM_BUG_ON(!node_state(nid, N_ONLINE));
243 for (pfn = start; !fail && pfn < end; pfn += PAGES_PER_SECTION) {
244 if (!pfn_present(pfn))
246 fail = init_section_page_cgroup(pfn, nid);
252 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
253 __free_page_cgroup(pfn);
258 int __meminit offline_page_cgroup(unsigned long start_pfn,
259 unsigned long nr_pages, int nid)
261 unsigned long start, end, pfn;
263 start = SECTION_ALIGN_DOWN(start_pfn);
264 end = SECTION_ALIGN_UP(start_pfn + nr_pages);
266 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
267 __free_page_cgroup(pfn);
272 static int __meminit page_cgroup_callback(struct notifier_block *self,
273 unsigned long action, void *arg)
275 struct memory_notify *mn = arg;
278 case MEM_GOING_ONLINE:
279 ret = online_page_cgroup(mn->start_pfn,
280 mn->nr_pages, mn->status_change_nid);
283 offline_page_cgroup(mn->start_pfn,
284 mn->nr_pages, mn->status_change_nid);
286 case MEM_CANCEL_ONLINE:
287 case MEM_GOING_OFFLINE:
290 case MEM_CANCEL_OFFLINE:
294 return notifier_from_errno(ret);
299 void __init page_cgroup_init(void)
304 if (mem_cgroup_disabled())
307 for_each_node_state(nid, N_HIGH_MEMORY) {
308 unsigned long start_pfn, end_pfn;
310 start_pfn = node_start_pfn(nid);
311 end_pfn = node_end_pfn(nid);
313 * start_pfn and end_pfn may not be aligned to SECTION and the
314 * page->flags of out of node pages are not initialized. So we
315 * scan [start_pfn, the biggest section's pfn < end_pfn) here.
317 for (pfn = start_pfn;
319 pfn = ALIGN(pfn + 1, PAGES_PER_SECTION)) {
324 * Nodes's pfns can be overlapping.
325 * We know some arch can have a nodes layout such as
326 * -------------pfn-------------->
327 * N0 | N1 | N2 | N0 | N1 | N2|....
329 if (pfn_to_nid(pfn) != nid)
331 if (init_section_page_cgroup(pfn, nid))
335 hotplug_memory_notifier(page_cgroup_callback, 0);
336 printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
337 printk(KERN_INFO "please try 'cgroup_disable=memory' option if you "
338 "don't want memory cgroups\n");
341 printk(KERN_CRIT "try 'cgroup_disable=memory' boot option\n");
342 panic("Out of memory");
345 void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat)
353 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
355 static DEFINE_MUTEX(swap_cgroup_mutex);
356 struct swap_cgroup_ctrl {
358 unsigned long length;
362 static struct swap_cgroup_ctrl swap_cgroup_ctrl[MAX_SWAPFILES];
367 #define SC_PER_PAGE (PAGE_SIZE/sizeof(struct swap_cgroup))
368 #define SC_POS_MASK (SC_PER_PAGE - 1)
371 * SwapCgroup implements "lookup" and "exchange" operations.
372 * In typical usage, this swap_cgroup is accessed via memcg's charge/uncharge
373 * against SwapCache. At swap_free(), this is accessed directly from swap.
376 * - we have no race in "exchange" when we're accessed via SwapCache because
377 * SwapCache(and its swp_entry) is under lock.
378 * - When called via swap_free(), there is no user of this entry and no race.
379 * Then, we don't need lock around "exchange".
381 * TODO: we can push these buffers out to HIGHMEM.
385 * allocate buffer for swap_cgroup.
387 static int swap_cgroup_prepare(int type)
390 struct swap_cgroup_ctrl *ctrl;
391 unsigned long idx, max;
393 ctrl = &swap_cgroup_ctrl[type];
395 for (idx = 0; idx < ctrl->length; idx++) {
396 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
398 goto not_enough_page;
399 ctrl->map[idx] = page;
404 for (idx = 0; idx < max; idx++)
405 __free_page(ctrl->map[idx]);
411 * swap_cgroup_cmpxchg - cmpxchg mem_cgroup's id for this swp_entry.
412 * @end: swap entry to be cmpxchged
416 * Returns old id at success, 0 at failure.
417 * (There is no mem_cgroup using 0 as its id)
419 unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
420 unsigned short old, unsigned short new)
422 int type = swp_type(ent);
423 unsigned long offset = swp_offset(ent);
424 unsigned long idx = offset / SC_PER_PAGE;
425 unsigned long pos = offset & SC_POS_MASK;
426 struct swap_cgroup_ctrl *ctrl;
427 struct page *mappage;
428 struct swap_cgroup *sc;
430 unsigned short retval;
432 ctrl = &swap_cgroup_ctrl[type];
434 mappage = ctrl->map[idx];
435 sc = page_address(mappage);
437 spin_lock_irqsave(&ctrl->lock, flags);
443 spin_unlock_irqrestore(&ctrl->lock, flags);
448 * swap_cgroup_record - record mem_cgroup for this swp_entry.
449 * @ent: swap entry to be recorded into
450 * @mem: mem_cgroup to be recorded
452 * Returns old value at success, 0 at failure.
453 * (Of course, old value can be 0.)
455 unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
457 int type = swp_type(ent);
458 unsigned long offset = swp_offset(ent);
459 unsigned long idx = offset / SC_PER_PAGE;
460 unsigned long pos = offset & SC_POS_MASK;
461 struct swap_cgroup_ctrl *ctrl;
462 struct page *mappage;
463 struct swap_cgroup *sc;
467 ctrl = &swap_cgroup_ctrl[type];
469 mappage = ctrl->map[idx];
470 sc = page_address(mappage);
472 spin_lock_irqsave(&ctrl->lock, flags);
475 spin_unlock_irqrestore(&ctrl->lock, flags);
481 * lookup_swap_cgroup - lookup mem_cgroup tied to swap entry
482 * @ent: swap entry to be looked up.
484 * Returns CSS ID of mem_cgroup at success. 0 at failure. (0 is invalid ID)
486 unsigned short lookup_swap_cgroup(swp_entry_t ent)
488 int type = swp_type(ent);
489 unsigned long offset = swp_offset(ent);
490 unsigned long idx = offset / SC_PER_PAGE;
491 unsigned long pos = offset & SC_POS_MASK;
492 struct swap_cgroup_ctrl *ctrl;
493 struct page *mappage;
494 struct swap_cgroup *sc;
497 ctrl = &swap_cgroup_ctrl[type];
498 mappage = ctrl->map[idx];
499 sc = page_address(mappage);
505 int swap_cgroup_swapon(int type, unsigned long max_pages)
508 unsigned long array_size;
509 unsigned long length;
510 struct swap_cgroup_ctrl *ctrl;
512 if (!do_swap_account)
515 length = DIV_ROUND_UP(max_pages, SC_PER_PAGE);
516 array_size = length * sizeof(void *);
518 array = vzalloc(array_size);
522 ctrl = &swap_cgroup_ctrl[type];
523 mutex_lock(&swap_cgroup_mutex);
524 ctrl->length = length;
526 spin_lock_init(&ctrl->lock);
527 if (swap_cgroup_prepare(type)) {
528 /* memory shortage */
531 mutex_unlock(&swap_cgroup_mutex);
535 mutex_unlock(&swap_cgroup_mutex);
539 printk(KERN_INFO "couldn't allocate enough memory for swap_cgroup.\n");
541 "swap_cgroup can be disabled by swapaccount=0 boot option\n");
545 void swap_cgroup_swapoff(int type)
548 unsigned long i, length;
549 struct swap_cgroup_ctrl *ctrl;
551 if (!do_swap_account)
554 mutex_lock(&swap_cgroup_mutex);
555 ctrl = &swap_cgroup_ctrl[type];
557 length = ctrl->length;
560 mutex_unlock(&swap_cgroup_mutex);
563 for (i = 0; i < length; i++) {
564 struct page *page = map[i];