Merge tag 'v3.10.88' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / mm / page_cgroup.c
CommitLineData
52d4b9ac
KH
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>
94b6da5a 7#include <linux/slab.h>
52d4b9ac 8#include <linux/memory.h>
4c821042 9#include <linux/vmalloc.h>
94b6da5a 10#include <linux/cgroup.h>
27a7faa0 11#include <linux/swapops.h>
7952f988 12#include <linux/kmemleak.h>
52d4b9ac 13
52d4b9ac
KH
14static unsigned long total_usage;
15
16#if !defined(CONFIG_SPARSEMEM)
17
18
31168481 19void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat)
52d4b9ac
KH
20{
21 pgdat->node_page_cgroup = NULL;
22}
23
24struct 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
30 base = NODE_DATA(page_to_nid(page))->node_page_cgroup;
00c54c0b
JW
31#ifdef CONFIG_DEBUG_VM
32 /*
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.
37 */
52d4b9ac
KH
38 if (unlikely(!base))
39 return NULL;
00c54c0b 40#endif
52d4b9ac
KH
41 offset = pfn - NODE_DATA(page_to_nid(page))->node_start_pfn;
42 return base + offset;
43}
44
45static int __init alloc_node_page_cgroup(int nid)
46{
6b208e3f 47 struct page_cgroup *base;
52d4b9ac 48 unsigned long table_size;
6b208e3f 49 unsigned long nr_pages;
52d4b9ac 50
52d4b9ac 51 nr_pages = NODE_DATA(nid)->node_spanned_pages;
653d22c0
KH
52 if (!nr_pages)
53 return 0;
54
52d4b9ac 55 table_size = sizeof(struct page_cgroup) * nr_pages;
ca371c0d
KH
56
57 base = __alloc_bootmem_node_nopanic(NODE_DATA(nid),
58 table_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS));
59 if (!base)
52d4b9ac 60 return -ENOMEM;
52d4b9ac
KH
61 NODE_DATA(nid)->node_page_cgroup = base;
62 total_usage += table_size;
63 return 0;
64}
65
ca371c0d 66void __init page_cgroup_init_flatmem(void)
52d4b9ac
KH
67{
68
69 int nid, fail;
70
f8d66542 71 if (mem_cgroup_disabled())
94b6da5a
KH
72 return;
73
52d4b9ac
KH
74 for_each_online_node(nid) {
75 fail = alloc_node_page_cgroup(nid);
76 if (fail)
77 goto fail;
78 }
79 printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
8ca739e3
RD
80 printk(KERN_INFO "please try 'cgroup_disable=memory' option if you"
81 " don't want memory cgroups\n");
52d4b9ac
KH
82 return;
83fail:
8ca739e3
RD
84 printk(KERN_CRIT "allocation of page_cgroup failed.\n");
85 printk(KERN_CRIT "please try 'cgroup_disable=memory' boot option\n");
52d4b9ac
KH
86 panic("Out of memory");
87}
88
89#else /* CONFIG_FLAT_NODE_MEM_MAP */
90
91struct page_cgroup *lookup_page_cgroup(struct page *page)
92{
93 unsigned long pfn = page_to_pfn(page);
94 struct mem_section *section = __pfn_to_section(pfn);
00c54c0b
JW
95#ifdef CONFIG_DEBUG_VM
96 /*
97 * The sanity checks the page allocator does upon freeing a
98 * page can reach here before the page_cgroup arrays are
99 * allocated when feeding a range of pages to the allocator
100 * for the first time during bootup or memory hotplug.
101 */
d69b042f
BS
102 if (!section->page_cgroup)
103 return NULL;
00c54c0b 104#endif
52d4b9ac
KH
105 return section->page_cgroup + pfn;
106}
107
268433b8 108static void *__meminit alloc_page_cgroup(size_t size, int nid)
dde79e00 109{
6b208e3f 110 gfp_t flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN;
dde79e00
MH
111 void *addr = NULL;
112
ff7ee93f
SR
113 addr = alloc_pages_exact_nid(nid, size, flags);
114 if (addr) {
115 kmemleak_alloc(addr, size, 1, flags);
dde79e00 116 return addr;
ff7ee93f 117 }
dde79e00
MH
118
119 if (node_state(nid, N_HIGH_MEMORY))
6b208e3f 120 addr = vzalloc_node(size, nid);
dde79e00 121 else
6b208e3f 122 addr = vzalloc(size);
dde79e00
MH
123
124 return addr;
125}
126
37573e8c 127static int __meminit init_section_page_cgroup(unsigned long pfn, int nid)
52d4b9ac 128{
6b3ae58e 129 struct mem_section *section;
6b208e3f 130 struct page_cgroup *base;
52d4b9ac 131 unsigned long table_size;
52d4b9ac 132
6b208e3f 133 section = __pfn_to_section(pfn);
6b3ae58e
JW
134
135 if (section->page_cgroup)
136 return 0;
137
6b3ae58e 138 table_size = sizeof(struct page_cgroup) * PAGES_PER_SECTION;
dde79e00
MH
139 base = alloc_page_cgroup(table_size, nid);
140
6b3ae58e
JW
141 /*
142 * The value stored in section->page_cgroup is (base - pfn)
143 * and it does not point to the memory block allocated above,
144 * causing kmemleak false positives.
145 */
146 kmemleak_not_leak(base);
52d4b9ac
KH
147
148 if (!base) {
149 printk(KERN_ERR "page cgroup allocation failure\n");
150 return -ENOMEM;
151 }
152
37573e8c
KH
153 /*
154 * The passed "pfn" may not be aligned to SECTION. For the calculation
155 * we need to apply a mask.
156 */
157 pfn &= PAGE_SECTION_MASK;
52d4b9ac
KH
158 section->page_cgroup = base - pfn;
159 total_usage += table_size;
160 return 0;
161}
162#ifdef CONFIG_MEMORY_HOTPLUG
0efc8eb9
BL
163static void free_page_cgroup(void *addr)
164{
165 if (is_vmalloc_addr(addr)) {
166 vfree(addr);
167 } else {
168 struct page *page = virt_to_page(addr);
169 size_t table_size =
170 sizeof(struct page_cgroup) * PAGES_PER_SECTION;
171
172 BUG_ON(PageReserved(page));
6c8ad60f 173 kmemleak_free(addr);
0efc8eb9
BL
174 free_pages_exact(addr, table_size);
175 }
176}
177
52d4b9ac
KH
178void __free_page_cgroup(unsigned long pfn)
179{
180 struct mem_section *ms;
181 struct page_cgroup *base;
182
183 ms = __pfn_to_section(pfn);
184 if (!ms || !ms->page_cgroup)
185 return;
186 base = ms->page_cgroup + pfn;
dde79e00
MH
187 free_page_cgroup(base);
188 ms->page_cgroup = NULL;
52d4b9ac
KH
189}
190
31168481 191int __meminit online_page_cgroup(unsigned long start_pfn,
52d4b9ac
KH
192 unsigned long nr_pages,
193 int nid)
194{
195 unsigned long start, end, pfn;
196 int fail = 0;
197
1bb36fbd
DK
198 start = SECTION_ALIGN_DOWN(start_pfn);
199 end = SECTION_ALIGN_UP(start_pfn + nr_pages);
52d4b9ac 200
37573e8c
KH
201 if (nid == -1) {
202 /*
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.
206 */
207 nid = pfn_to_nid(start_pfn);
208 VM_BUG_ON(!node_state(nid, N_ONLINE));
209 }
210
52d4b9ac
KH
211 for (pfn = start; !fail && pfn < end; pfn += PAGES_PER_SECTION) {
212 if (!pfn_present(pfn))
213 continue;
37573e8c 214 fail = init_section_page_cgroup(pfn, nid);
52d4b9ac
KH
215 }
216 if (!fail)
217 return 0;
218
219 /* rollback */
220 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
221 __free_page_cgroup(pfn);
222
223 return -ENOMEM;
224}
225
31168481 226int __meminit offline_page_cgroup(unsigned long start_pfn,
52d4b9ac
KH
227 unsigned long nr_pages, int nid)
228{
229 unsigned long start, end, pfn;
230
1bb36fbd
DK
231 start = SECTION_ALIGN_DOWN(start_pfn);
232 end = SECTION_ALIGN_UP(start_pfn + nr_pages);
52d4b9ac
KH
233
234 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
235 __free_page_cgroup(pfn);
236 return 0;
237
238}
239
31168481 240static int __meminit page_cgroup_callback(struct notifier_block *self,
52d4b9ac
KH
241 unsigned long action, void *arg)
242{
243 struct memory_notify *mn = arg;
244 int ret = 0;
245 switch (action) {
246 case MEM_GOING_ONLINE:
247 ret = online_page_cgroup(mn->start_pfn,
248 mn->nr_pages, mn->status_change_nid);
249 break;
52d4b9ac
KH
250 case MEM_OFFLINE:
251 offline_page_cgroup(mn->start_pfn,
252 mn->nr_pages, mn->status_change_nid);
253 break;
dc19f9db 254 case MEM_CANCEL_ONLINE:
7c72eb32
WC
255 offline_page_cgroup(mn->start_pfn,
256 mn->nr_pages, mn->status_change_nid);
257 break;
52d4b9ac
KH
258 case MEM_GOING_OFFLINE:
259 break;
260 case MEM_ONLINE:
261 case MEM_CANCEL_OFFLINE:
262 break;
263 }
dc19f9db 264
5fda1bd5 265 return notifier_from_errno(ret);
52d4b9ac
KH
266}
267
268#endif
269
270void __init page_cgroup_init(void)
271{
272 unsigned long pfn;
37573e8c 273 int nid;
52d4b9ac 274
f8d66542 275 if (mem_cgroup_disabled())
94b6da5a
KH
276 return;
277
31aaea4a 278 for_each_node_state(nid, N_MEMORY) {
37573e8c
KH
279 unsigned long start_pfn, end_pfn;
280
281 start_pfn = node_start_pfn(nid);
282 end_pfn = node_end_pfn(nid);
283 /*
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.
287 */
288 for (pfn = start_pfn;
289 pfn < end_pfn;
290 pfn = ALIGN(pfn + 1, PAGES_PER_SECTION)) {
291
292 if (!pfn_valid(pfn))
293 continue;
294 /*
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|....
299 */
300 if (pfn_to_nid(pfn) != nid)
301 continue;
302 if (init_section_page_cgroup(pfn, nid))
303 goto oom;
304 }
52d4b9ac 305 }
37573e8c 306 hotplug_memory_notifier(page_cgroup_callback, 0);
52d4b9ac 307 printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
37573e8c
KH
308 printk(KERN_INFO "please try 'cgroup_disable=memory' option if you "
309 "don't want memory cgroups\n");
310 return;
311oom:
312 printk(KERN_CRIT "try 'cgroup_disable=memory' boot option\n");
313 panic("Out of memory");
52d4b9ac
KH
314}
315
31168481 316void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat)
52d4b9ac
KH
317{
318 return;
319}
320
321#endif
27a7faa0
KH
322
323
c255a458 324#ifdef CONFIG_MEMCG_SWAP
27a7faa0
KH
325
326static DEFINE_MUTEX(swap_cgroup_mutex);
327struct swap_cgroup_ctrl {
328 struct page **map;
329 unsigned long length;
e9e58a4e 330 spinlock_t lock;
27a7faa0
KH
331};
332
61600f57 333static struct swap_cgroup_ctrl swap_cgroup_ctrl[MAX_SWAPFILES];
27a7faa0 334
27a7faa0 335struct swap_cgroup {
a3b2d692 336 unsigned short id;
27a7faa0
KH
337};
338#define SC_PER_PAGE (PAGE_SIZE/sizeof(struct swap_cgroup))
27a7faa0
KH
339
340/*
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.
344 *
345 * This means,
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".
350 *
351 * TODO: we can push these buffers out to HIGHMEM.
352 */
353
354/*
355 * allocate buffer for swap_cgroup.
356 */
357static int swap_cgroup_prepare(int type)
358{
359 struct page *page;
360 struct swap_cgroup_ctrl *ctrl;
361 unsigned long idx, max;
362
27a7faa0
KH
363 ctrl = &swap_cgroup_ctrl[type];
364
365 for (idx = 0; idx < ctrl->length; idx++) {
366 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
367 if (!page)
368 goto not_enough_page;
369 ctrl->map[idx] = page;
370 }
371 return 0;
372not_enough_page:
373 max = idx;
374 for (idx = 0; idx < max; idx++)
375 __free_page(ctrl->map[idx]);
376
377 return -ENOMEM;
378}
379
9fb4b7cc
BL
380static struct swap_cgroup *lookup_swap_cgroup(swp_entry_t ent,
381 struct swap_cgroup_ctrl **ctrlp)
382{
383 pgoff_t offset = swp_offset(ent);
384 struct swap_cgroup_ctrl *ctrl;
385 struct page *mappage;
c09ff089 386 struct swap_cgroup *sc;
9fb4b7cc
BL
387
388 ctrl = &swap_cgroup_ctrl[swp_type(ent)];
389 if (ctrlp)
390 *ctrlp = ctrl;
391
392 mappage = ctrl->map[offset / SC_PER_PAGE];
c09ff089
HD
393 sc = page_address(mappage);
394 return sc + offset % SC_PER_PAGE;
9fb4b7cc
BL
395}
396
02491447
DN
397/**
398 * swap_cgroup_cmpxchg - cmpxchg mem_cgroup's id for this swp_entry.
dad7557e 399 * @ent: swap entry to be cmpxchged
02491447
DN
400 * @old: old id
401 * @new: new id
402 *
403 * Returns old id at success, 0 at failure.
25985edc 404 * (There is no mem_cgroup using 0 as its id)
02491447
DN
405 */
406unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
407 unsigned short old, unsigned short new)
408{
02491447 409 struct swap_cgroup_ctrl *ctrl;
02491447 410 struct swap_cgroup *sc;
e9e58a4e
KH
411 unsigned long flags;
412 unsigned short retval;
02491447 413
9fb4b7cc 414 sc = lookup_swap_cgroup(ent, &ctrl);
02491447 415
e9e58a4e
KH
416 spin_lock_irqsave(&ctrl->lock, flags);
417 retval = sc->id;
418 if (retval == old)
419 sc->id = new;
02491447 420 else
e9e58a4e
KH
421 retval = 0;
422 spin_unlock_irqrestore(&ctrl->lock, flags);
423 return retval;
02491447
DN
424}
425
27a7faa0
KH
426/**
427 * swap_cgroup_record - record mem_cgroup for this swp_entry.
428 * @ent: swap entry to be recorded into
dad7557e 429 * @id: mem_cgroup to be recorded
27a7faa0 430 *
a3b2d692
KH
431 * Returns old value at success, 0 at failure.
432 * (Of course, old value can be 0.)
27a7faa0 433 */
a3b2d692 434unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
27a7faa0 435{
27a7faa0 436 struct swap_cgroup_ctrl *ctrl;
27a7faa0 437 struct swap_cgroup *sc;
a3b2d692 438 unsigned short old;
e9e58a4e 439 unsigned long flags;
27a7faa0 440
9fb4b7cc 441 sc = lookup_swap_cgroup(ent, &ctrl);
27a7faa0 442
e9e58a4e
KH
443 spin_lock_irqsave(&ctrl->lock, flags);
444 old = sc->id;
445 sc->id = id;
446 spin_unlock_irqrestore(&ctrl->lock, flags);
27a7faa0
KH
447
448 return old;
449}
450
451/**
9fb4b7cc 452 * lookup_swap_cgroup_id - lookup mem_cgroup id tied to swap entry
27a7faa0
KH
453 * @ent: swap entry to be looked up.
454 *
a3b2d692 455 * Returns CSS ID of mem_cgroup at success. 0 at failure. (0 is invalid ID)
27a7faa0 456 */
9fb4b7cc 457unsigned short lookup_swap_cgroup_id(swp_entry_t ent)
27a7faa0 458{
9fb4b7cc 459 return lookup_swap_cgroup(ent, NULL)->id;
27a7faa0
KH
460}
461
462int swap_cgroup_swapon(int type, unsigned long max_pages)
463{
464 void *array;
465 unsigned long array_size;
466 unsigned long length;
467 struct swap_cgroup_ctrl *ctrl;
468
469 if (!do_swap_account)
470 return 0;
471
33278f7f 472 length = DIV_ROUND_UP(max_pages, SC_PER_PAGE);
27a7faa0
KH
473 array_size = length * sizeof(void *);
474
8c1fec1b 475 array = vzalloc(array_size);
27a7faa0
KH
476 if (!array)
477 goto nomem;
478
27a7faa0
KH
479 ctrl = &swap_cgroup_ctrl[type];
480 mutex_lock(&swap_cgroup_mutex);
481 ctrl->length = length;
482 ctrl->map = array;
e9e58a4e 483 spin_lock_init(&ctrl->lock);
27a7faa0
KH
484 if (swap_cgroup_prepare(type)) {
485 /* memory shortage */
486 ctrl->map = NULL;
487 ctrl->length = 0;
27a7faa0 488 mutex_unlock(&swap_cgroup_mutex);
6a5b18d2 489 vfree(array);
27a7faa0
KH
490 goto nomem;
491 }
492 mutex_unlock(&swap_cgroup_mutex);
493
27a7faa0
KH
494 return 0;
495nomem:
496 printk(KERN_INFO "couldn't allocate enough memory for swap_cgroup.\n");
497 printk(KERN_INFO
00a66d29 498 "swap_cgroup can be disabled by swapaccount=0 boot option\n");
27a7faa0
KH
499 return -ENOMEM;
500}
501
502void swap_cgroup_swapoff(int type)
503{
6a5b18d2
NK
504 struct page **map;
505 unsigned long i, length;
27a7faa0
KH
506 struct swap_cgroup_ctrl *ctrl;
507
508 if (!do_swap_account)
509 return;
510
511 mutex_lock(&swap_cgroup_mutex);
512 ctrl = &swap_cgroup_ctrl[type];
6a5b18d2
NK
513 map = ctrl->map;
514 length = ctrl->length;
515 ctrl->map = NULL;
516 ctrl->length = 0;
517 mutex_unlock(&swap_cgroup_mutex);
518
519 if (map) {
520 for (i = 0; i < length; i++) {
521 struct page *page = map[i];
27a7faa0
KH
522 if (page)
523 __free_page(page);
524 }
6a5b18d2 525 vfree(map);
27a7faa0 526 }
27a7faa0
KH
527}
528
529#endif