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 unsigned long total_usage;
16 #if !defined(CONFIG_SPARSEMEM)
19 void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat)
21 pgdat->node_page_cgroup = NULL;
24 struct page_cgroup *lookup_page_cgroup(struct page *page)
26 unsigned long pfn = page_to_pfn(page);
28 struct page_cgroup *base;
30 base = NODE_DATA(page_to_nid(page))->node_page_cgroup;
31 #ifdef CONFIG_DEBUG_VM
33 * The sanity checks the page allocator does upon freeing a
34 * page can reach here before the page_cgroup arrays are
35 * allocated when feeding a range of pages to the allocator
36 * for the first time during bootup or memory hotplug.
41 offset = pfn - NODE_DATA(page_to_nid(page))->node_start_pfn;
45 static int __init alloc_node_page_cgroup(int nid)
47 struct page_cgroup *base;
48 unsigned long table_size;
49 unsigned long nr_pages;
51 nr_pages = NODE_DATA(nid)->node_spanned_pages;
55 table_size = sizeof(struct page_cgroup) * nr_pages;
57 base = memblock_virt_alloc_try_nid_nopanic(
58 table_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS),
59 BOOTMEM_ALLOC_ACCESSIBLE, nid);
62 NODE_DATA(nid)->node_page_cgroup = base;
63 total_usage += table_size;
67 void __init page_cgroup_init_flatmem(void)
72 if (mem_cgroup_disabled())
75 for_each_online_node(nid) {
76 fail = alloc_node_page_cgroup(nid);
80 printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
81 printk(KERN_INFO "please try 'cgroup_disable=memory' option if you"
82 " don't want memory cgroups\n");
85 printk(KERN_CRIT "allocation of page_cgroup failed.\n");
86 printk(KERN_CRIT "please try 'cgroup_disable=memory' boot option\n");
87 panic("Out of memory");
90 #else /* CONFIG_FLAT_NODE_MEM_MAP */
92 struct page_cgroup *lookup_page_cgroup(struct page *page)
94 unsigned long pfn = page_to_pfn(page);
95 struct mem_section *section = __pfn_to_section(pfn);
96 #ifdef CONFIG_DEBUG_VM
98 * The sanity checks the page allocator does upon freeing a
99 * page can reach here before the page_cgroup arrays are
100 * allocated when feeding a range of pages to the allocator
101 * for the first time during bootup or memory hotplug.
103 if (!section->page_cgroup)
106 return section->page_cgroup + pfn;
109 static void *__meminit alloc_page_cgroup(size_t size, int nid)
111 gfp_t flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN;
114 addr = alloc_pages_exact_nid(nid, size, flags);
116 kmemleak_alloc(addr, size, 1, flags);
120 if (node_state(nid, N_HIGH_MEMORY))
121 addr = vzalloc_node(size, nid);
123 addr = vzalloc(size);
128 static int __meminit init_section_page_cgroup(unsigned long pfn, int nid)
130 struct mem_section *section;
131 struct page_cgroup *base;
132 unsigned long table_size;
134 section = __pfn_to_section(pfn);
136 if (section->page_cgroup)
139 table_size = sizeof(struct page_cgroup) * PAGES_PER_SECTION;
140 base = alloc_page_cgroup(table_size, nid);
143 * The value stored in section->page_cgroup is (base - pfn)
144 * and it does not point to the memory block allocated above,
145 * causing kmemleak false positives.
147 kmemleak_not_leak(base);
150 printk(KERN_ERR "page cgroup allocation failure\n");
155 * The passed "pfn" may not be aligned to SECTION. For the calculation
156 * we need to apply a mask.
158 pfn &= PAGE_SECTION_MASK;
159 section->page_cgroup = base - pfn;
160 total_usage += table_size;
163 #ifdef CONFIG_MEMORY_HOTPLUG
164 static void free_page_cgroup(void *addr)
166 if (is_vmalloc_addr(addr)) {
169 struct page *page = virt_to_page(addr);
171 sizeof(struct page_cgroup) * PAGES_PER_SECTION;
173 BUG_ON(PageReserved(page));
174 free_pages_exact(addr, table_size);
178 void __free_page_cgroup(unsigned long pfn)
180 struct mem_section *ms;
181 struct page_cgroup *base;
183 ms = __pfn_to_section(pfn);
184 if (!ms || !ms->page_cgroup)
186 base = ms->page_cgroup + pfn;
187 free_page_cgroup(base);
188 ms->page_cgroup = NULL;
191 int __meminit online_page_cgroup(unsigned long start_pfn,
192 unsigned long nr_pages,
195 unsigned long start, end, pfn;
198 start = SECTION_ALIGN_DOWN(start_pfn);
199 end = SECTION_ALIGN_UP(start_pfn + nr_pages);
203 * In this case, "nid" already exists and contains valid memory.
204 * "start_pfn" passed to us is a pfn which is an arg for
205 * online__pages(), and start_pfn should exist.
207 nid = pfn_to_nid(start_pfn);
208 VM_BUG_ON(!node_state(nid, N_ONLINE));
211 for (pfn = start; !fail && pfn < end; pfn += PAGES_PER_SECTION) {
212 if (!pfn_present(pfn))
214 fail = init_section_page_cgroup(pfn, nid);
220 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
221 __free_page_cgroup(pfn);
226 int __meminit offline_page_cgroup(unsigned long start_pfn,
227 unsigned long nr_pages, int nid)
229 unsigned long start, end, pfn;
231 start = SECTION_ALIGN_DOWN(start_pfn);
232 end = SECTION_ALIGN_UP(start_pfn + nr_pages);
234 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
235 __free_page_cgroup(pfn);
240 static int __meminit page_cgroup_callback(struct notifier_block *self,
241 unsigned long action, void *arg)
243 struct memory_notify *mn = arg;
246 case MEM_GOING_ONLINE:
247 ret = online_page_cgroup(mn->start_pfn,
248 mn->nr_pages, mn->status_change_nid);
251 offline_page_cgroup(mn->start_pfn,
252 mn->nr_pages, mn->status_change_nid);
254 case MEM_CANCEL_ONLINE:
255 offline_page_cgroup(mn->start_pfn,
256 mn->nr_pages, mn->status_change_nid);
258 case MEM_GOING_OFFLINE:
261 case MEM_CANCEL_OFFLINE:
265 return notifier_from_errno(ret);
270 void __init page_cgroup_init(void)
275 if (mem_cgroup_disabled())
278 for_each_node_state(nid, N_MEMORY) {
279 unsigned long start_pfn, end_pfn;
281 start_pfn = node_start_pfn(nid);
282 end_pfn = node_end_pfn(nid);
284 * start_pfn and end_pfn may not be aligned to SECTION and the
285 * page->flags of out of node pages are not initialized. So we
286 * scan [start_pfn, the biggest section's pfn < end_pfn) here.
288 for (pfn = start_pfn;
290 pfn = ALIGN(pfn + 1, PAGES_PER_SECTION)) {
295 * Nodes's pfns can be overlapping.
296 * We know some arch can have a nodes layout such as
297 * -------------pfn-------------->
298 * N0 | N1 | N2 | N0 | N1 | N2|....
300 if (pfn_to_nid(pfn) != nid)
302 if (init_section_page_cgroup(pfn, nid))
306 hotplug_memory_notifier(page_cgroup_callback, 0);
307 printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
308 printk(KERN_INFO "please try 'cgroup_disable=memory' option if you "
309 "don't want memory cgroups\n");
312 printk(KERN_CRIT "try 'cgroup_disable=memory' boot option\n");
313 panic("Out of memory");
316 void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat)
324 #ifdef CONFIG_MEMCG_SWAP
326 static DEFINE_MUTEX(swap_cgroup_mutex);
327 struct swap_cgroup_ctrl {
329 unsigned long length;
333 static struct swap_cgroup_ctrl swap_cgroup_ctrl[MAX_SWAPFILES];
338 #define SC_PER_PAGE (PAGE_SIZE/sizeof(struct swap_cgroup))
341 * SwapCgroup implements "lookup" and "exchange" operations.
342 * In typical usage, this swap_cgroup is accessed via memcg's charge/uncharge
343 * against SwapCache. At swap_free(), this is accessed directly from swap.
346 * - we have no race in "exchange" when we're accessed via SwapCache because
347 * SwapCache(and its swp_entry) is under lock.
348 * - When called via swap_free(), there is no user of this entry and no race.
349 * Then, we don't need lock around "exchange".
351 * TODO: we can push these buffers out to HIGHMEM.
355 * allocate buffer for swap_cgroup.
357 static int swap_cgroup_prepare(int type)
360 struct swap_cgroup_ctrl *ctrl;
361 unsigned long idx, max;
363 ctrl = &swap_cgroup_ctrl[type];
365 for (idx = 0; idx < ctrl->length; idx++) {
366 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
368 goto not_enough_page;
369 ctrl->map[idx] = page;
374 for (idx = 0; idx < max; idx++)
375 __free_page(ctrl->map[idx]);
380 static struct swap_cgroup *lookup_swap_cgroup(swp_entry_t ent,
381 struct swap_cgroup_ctrl **ctrlp)
383 pgoff_t offset = swp_offset(ent);
384 struct swap_cgroup_ctrl *ctrl;
385 struct page *mappage;
386 struct swap_cgroup *sc;
388 ctrl = &swap_cgroup_ctrl[swp_type(ent)];
392 mappage = ctrl->map[offset / SC_PER_PAGE];
393 sc = page_address(mappage);
394 return sc + offset % SC_PER_PAGE;
398 * swap_cgroup_cmpxchg - cmpxchg mem_cgroup's id for this swp_entry.
399 * @ent: swap entry to be cmpxchged
403 * Returns old id at success, 0 at failure.
404 * (There is no mem_cgroup using 0 as its id)
406 unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
407 unsigned short old, unsigned short new)
409 struct swap_cgroup_ctrl *ctrl;
410 struct swap_cgroup *sc;
412 unsigned short retval;
414 sc = lookup_swap_cgroup(ent, &ctrl);
416 spin_lock_irqsave(&ctrl->lock, flags);
422 spin_unlock_irqrestore(&ctrl->lock, flags);
427 * swap_cgroup_record - record mem_cgroup for this swp_entry.
428 * @ent: swap entry to be recorded into
429 * @id: mem_cgroup to be recorded
431 * Returns old value at success, 0 at failure.
432 * (Of course, old value can be 0.)
434 unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
436 struct swap_cgroup_ctrl *ctrl;
437 struct swap_cgroup *sc;
441 sc = lookup_swap_cgroup(ent, &ctrl);
443 spin_lock_irqsave(&ctrl->lock, flags);
446 spin_unlock_irqrestore(&ctrl->lock, flags);
452 * lookup_swap_cgroup_id - lookup mem_cgroup id tied to swap entry
453 * @ent: swap entry to be looked up.
455 * Returns ID of mem_cgroup at success. 0 at failure. (0 is invalid ID)
457 unsigned short lookup_swap_cgroup_id(swp_entry_t ent)
459 return lookup_swap_cgroup(ent, NULL)->id;
462 int swap_cgroup_swapon(int type, unsigned long max_pages)
465 unsigned long array_size;
466 unsigned long length;
467 struct swap_cgroup_ctrl *ctrl;
469 if (!do_swap_account)
472 length = DIV_ROUND_UP(max_pages, SC_PER_PAGE);
473 array_size = length * sizeof(void *);
475 array = vzalloc(array_size);
479 ctrl = &swap_cgroup_ctrl[type];
480 mutex_lock(&swap_cgroup_mutex);
481 ctrl->length = length;
483 spin_lock_init(&ctrl->lock);
484 if (swap_cgroup_prepare(type)) {
485 /* memory shortage */
488 mutex_unlock(&swap_cgroup_mutex);
492 mutex_unlock(&swap_cgroup_mutex);
496 printk(KERN_INFO "couldn't allocate enough memory for swap_cgroup.\n");
498 "swap_cgroup can be disabled by swapaccount=0 boot option\n");
502 void swap_cgroup_swapoff(int type)
505 unsigned long i, length;
506 struct swap_cgroup_ctrl *ctrl;
508 if (!do_swap_account)
511 mutex_lock(&swap_cgroup_mutex);
512 ctrl = &swap_cgroup_ctrl[type];
514 length = ctrl->length;
517 mutex_unlock(&swap_cgroup_mutex);
520 for (i = 0; i < length; i++) {
521 struct page *page = map[i];