]> bbs.cooldavid.org Git - net-next-2.6.git/blame - mm/memcontrol.c
memcontrol: rcu_read_lock() to protect mm_match_cgroup()
[net-next-2.6.git] / mm / memcontrol.c
CommitLineData
8cdea7c0
BS
1/* memcontrol.c - Memory Controller
2 *
3 * Copyright IBM Corporation, 2007
4 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
5 *
78fb7466
PE
6 * Copyright 2007 OpenVZ SWsoft Inc
7 * Author: Pavel Emelianov <xemul@openvz.org>
8 *
8cdea7c0
BS
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 */
19
20#include <linux/res_counter.h>
21#include <linux/memcontrol.h>
22#include <linux/cgroup.h>
78fb7466 23#include <linux/mm.h>
d13d1443 24#include <linux/pagemap.h>
d52aa412 25#include <linux/smp.h>
8a9f3ccd 26#include <linux/page-flags.h>
66e1707b 27#include <linux/backing-dev.h>
8a9f3ccd
BS
28#include <linux/bit_spinlock.h>
29#include <linux/rcupdate.h>
8c7c6e34 30#include <linux/mutex.h>
b6ac57d5 31#include <linux/slab.h>
66e1707b
BS
32#include <linux/swap.h>
33#include <linux/spinlock.h>
34#include <linux/fs.h>
d2ceb9b7 35#include <linux/seq_file.h>
33327948 36#include <linux/vmalloc.h>
b69408e8 37#include <linux/mm_inline.h>
52d4b9ac 38#include <linux/page_cgroup.h>
08e552c6 39#include "internal.h"
8cdea7c0 40
8697d331
BS
41#include <asm/uaccess.h>
42
a181b0e8 43struct cgroup_subsys mem_cgroup_subsys __read_mostly;
a181b0e8 44#define MEM_CGROUP_RECLAIM_RETRIES 5
8cdea7c0 45
c077719b
KH
46#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
47/* Turned on only when memory cgroup is enabled && really_do_swap_account = 0 */
48int do_swap_account __read_mostly;
49static int really_do_swap_account __initdata = 1; /* for remember boot option*/
50#else
51#define do_swap_account (0)
52#endif
53
54
d52aa412
KH
55/*
56 * Statistics for memory cgroup.
57 */
58enum mem_cgroup_stat_index {
59 /*
60 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
61 */
62 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
63 MEM_CGROUP_STAT_RSS, /* # of pages charged as rss */
55e462b0
BR
64 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
65 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
d52aa412
KH
66
67 MEM_CGROUP_STAT_NSTATS,
68};
69
70struct mem_cgroup_stat_cpu {
71 s64 count[MEM_CGROUP_STAT_NSTATS];
72} ____cacheline_aligned_in_smp;
73
74struct mem_cgroup_stat {
c8dad2bb 75 struct mem_cgroup_stat_cpu cpustat[0];
d52aa412
KH
76};
77
78/*
79 * For accounting under irq disable, no need for increment preempt count.
80 */
addb9efe 81static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
d52aa412
KH
82 enum mem_cgroup_stat_index idx, int val)
83{
addb9efe 84 stat->count[idx] += val;
d52aa412
KH
85}
86
87static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
88 enum mem_cgroup_stat_index idx)
89{
90 int cpu;
91 s64 ret = 0;
92 for_each_possible_cpu(cpu)
93 ret += stat->cpustat[cpu].count[idx];
94 return ret;
95}
96
6d12e2d8
KH
97/*
98 * per-zone information in memory controller.
99 */
6d12e2d8 100struct mem_cgroup_per_zone {
072c56c1
KH
101 /*
102 * spin_lock to protect the per cgroup LRU
103 */
b69408e8
CL
104 struct list_head lists[NR_LRU_LISTS];
105 unsigned long count[NR_LRU_LISTS];
6d12e2d8
KH
106};
107/* Macro for accessing counter */
108#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
109
110struct mem_cgroup_per_node {
111 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
112};
113
114struct mem_cgroup_lru_info {
115 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
116};
117
8cdea7c0
BS
118/*
119 * The memory controller data structure. The memory controller controls both
120 * page cache and RSS per cgroup. We would eventually like to provide
121 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
122 * to help the administrator determine what knobs to tune.
123 *
124 * TODO: Add a water mark for the memory controller. Reclaim will begin when
8a9f3ccd
BS
125 * we hit the water mark. May be even add a low water mark, such that
126 * no reclaim occurs from a cgroup at it's low water mark, this is
127 * a feature that will be implemented much later in the future.
8cdea7c0
BS
128 */
129struct mem_cgroup {
130 struct cgroup_subsys_state css;
131 /*
132 * the counter to account for memory usage
133 */
134 struct res_counter res;
8c7c6e34
KH
135 /*
136 * the counter to account for mem+swap usage.
137 */
138 struct res_counter memsw;
78fb7466
PE
139 /*
140 * Per cgroup active and inactive list, similar to the
141 * per zone LRU lists.
78fb7466 142 */
6d12e2d8 143 struct mem_cgroup_lru_info info;
072c56c1 144
6c48a1d0 145 int prev_priority; /* for recording reclaim priority */
6d61ef40
BS
146
147 /*
148 * While reclaiming in a hiearchy, we cache the last child we
149 * reclaimed from. Protected by cgroup_lock()
150 */
151 struct mem_cgroup *last_scanned_child;
18f59ea7
BS
152 /*
153 * Should the accounting and control be hierarchical, per subtree?
154 */
155 bool use_hierarchy;
6d61ef40 156
8c7c6e34
KH
157 int obsolete;
158 atomic_t refcnt;
d52aa412 159 /*
c8dad2bb 160 * statistics. This must be placed at the end of memcg.
d52aa412
KH
161 */
162 struct mem_cgroup_stat stat;
8cdea7c0
BS
163};
164
217bc319
KH
165enum charge_type {
166 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
167 MEM_CGROUP_CHARGE_TYPE_MAPPED,
4f98a2fe 168 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
c05555b5 169 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
d13d1443 170 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
c05555b5
KH
171 NR_CHARGE_TYPE,
172};
173
52d4b9ac
KH
174/* only for here (for easy reading.) */
175#define PCGF_CACHE (1UL << PCG_CACHE)
176#define PCGF_USED (1UL << PCG_USED)
52d4b9ac 177#define PCGF_LOCK (1UL << PCG_LOCK)
c05555b5
KH
178static const unsigned long
179pcg_default_flags[NR_CHARGE_TYPE] = {
08e552c6
KH
180 PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* File Cache */
181 PCGF_USED | PCGF_LOCK, /* Anon */
182 PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* Shmem */
52d4b9ac 183 0, /* FORCE */
217bc319
KH
184};
185
8c7c6e34
KH
186
187/* for encoding cft->private value on file */
188#define _MEM (0)
189#define _MEMSWAP (1)
190#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
191#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
192#define MEMFILE_ATTR(val) ((val) & 0xffff)
193
194static void mem_cgroup_get(struct mem_cgroup *mem);
195static void mem_cgroup_put(struct mem_cgroup *mem);
196
c05555b5
KH
197static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
198 struct page_cgroup *pc,
199 bool charge)
d52aa412
KH
200{
201 int val = (charge)? 1 : -1;
202 struct mem_cgroup_stat *stat = &mem->stat;
addb9efe 203 struct mem_cgroup_stat_cpu *cpustat;
08e552c6 204 int cpu = get_cpu();
d52aa412 205
08e552c6 206 cpustat = &stat->cpustat[cpu];
c05555b5 207 if (PageCgroupCache(pc))
addb9efe 208 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
d52aa412 209 else
addb9efe 210 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
55e462b0
BR
211
212 if (charge)
addb9efe 213 __mem_cgroup_stat_add_safe(cpustat,
55e462b0
BR
214 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
215 else
addb9efe 216 __mem_cgroup_stat_add_safe(cpustat,
55e462b0 217 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
08e552c6 218 put_cpu();
6d12e2d8
KH
219}
220
d5b69e38 221static struct mem_cgroup_per_zone *
6d12e2d8
KH
222mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
223{
6d12e2d8
KH
224 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
225}
226
d5b69e38 227static struct mem_cgroup_per_zone *
6d12e2d8
KH
228page_cgroup_zoneinfo(struct page_cgroup *pc)
229{
230 struct mem_cgroup *mem = pc->mem_cgroup;
231 int nid = page_cgroup_nid(pc);
232 int zid = page_cgroup_zid(pc);
d52aa412 233
6d12e2d8
KH
234 return mem_cgroup_zoneinfo(mem, nid, zid);
235}
236
237static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem,
b69408e8 238 enum lru_list idx)
6d12e2d8
KH
239{
240 int nid, zid;
241 struct mem_cgroup_per_zone *mz;
242 u64 total = 0;
243
244 for_each_online_node(nid)
245 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
246 mz = mem_cgroup_zoneinfo(mem, nid, zid);
247 total += MEM_CGROUP_ZSTAT(mz, idx);
248 }
249 return total;
d52aa412
KH
250}
251
d5b69e38 252static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
8cdea7c0
BS
253{
254 return container_of(cgroup_subsys_state(cont,
255 mem_cgroup_subsys_id), struct mem_cgroup,
256 css);
257}
258
cf475ad2 259struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
78fb7466 260{
31a78f23
BS
261 /*
262 * mm_update_next_owner() may clear mm->owner to NULL
263 * if it races with swapoff, page migration, etc.
264 * So this can be called with p == NULL.
265 */
266 if (unlikely(!p))
267 return NULL;
268
78fb7466
PE
269 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
270 struct mem_cgroup, css);
271}
272
08e552c6
KH
273/*
274 * Following LRU functions are allowed to be used without PCG_LOCK.
275 * Operations are called by routine of global LRU independently from memcg.
276 * What we have to take care of here is validness of pc->mem_cgroup.
277 *
278 * Changes to pc->mem_cgroup happens when
279 * 1. charge
280 * 2. moving account
281 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
282 * It is added to LRU before charge.
283 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
284 * When moving account, the page is not on LRU. It's isolated.
285 */
4f98a2fe 286
08e552c6
KH
287void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
288{
289 struct page_cgroup *pc;
290 struct mem_cgroup *mem;
291 struct mem_cgroup_per_zone *mz;
6d12e2d8 292
f8d66542 293 if (mem_cgroup_disabled())
08e552c6
KH
294 return;
295 pc = lookup_page_cgroup(page);
296 /* can happen while we handle swapcache. */
297 if (list_empty(&pc->lru))
298 return;
299 mz = page_cgroup_zoneinfo(pc);
300 mem = pc->mem_cgroup;
b69408e8 301 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
08e552c6
KH
302 list_del_init(&pc->lru);
303 return;
6d12e2d8
KH
304}
305
08e552c6 306void mem_cgroup_del_lru(struct page *page)
6d12e2d8 307{
08e552c6
KH
308 mem_cgroup_del_lru_list(page, page_lru(page));
309}
b69408e8 310
08e552c6
KH
311void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
312{
313 struct mem_cgroup_per_zone *mz;
314 struct page_cgroup *pc;
b69408e8 315
f8d66542 316 if (mem_cgroup_disabled())
08e552c6 317 return;
6d12e2d8 318
08e552c6
KH
319 pc = lookup_page_cgroup(page);
320 smp_rmb();
321 /* unused page is not rotated. */
322 if (!PageCgroupUsed(pc))
323 return;
324 mz = page_cgroup_zoneinfo(pc);
325 list_move(&pc->lru, &mz->lists[lru]);
6d12e2d8
KH
326}
327
08e552c6 328void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
66e1707b 329{
08e552c6
KH
330 struct page_cgroup *pc;
331 struct mem_cgroup_per_zone *mz;
6d12e2d8 332
f8d66542 333 if (mem_cgroup_disabled())
08e552c6
KH
334 return;
335 pc = lookup_page_cgroup(page);
336 /* barrier to sync with "charge" */
337 smp_rmb();
338 if (!PageCgroupUsed(pc))
894bc310 339 return;
b69408e8 340
08e552c6 341 mz = page_cgroup_zoneinfo(pc);
b69408e8 342 MEM_CGROUP_ZSTAT(mz, lru) += 1;
08e552c6
KH
343 list_add(&pc->lru, &mz->lists[lru]);
344}
345/*
346 * To add swapcache into LRU. Be careful to all this function.
347 * zone->lru_lock shouldn't be held and irq must not be disabled.
348 */
349static void mem_cgroup_lru_fixup(struct page *page)
350{
351 if (!isolate_lru_page(page))
352 putback_lru_page(page);
353}
354
355void mem_cgroup_move_lists(struct page *page,
356 enum lru_list from, enum lru_list to)
357{
f8d66542 358 if (mem_cgroup_disabled())
08e552c6
KH
359 return;
360 mem_cgroup_del_lru_list(page, from);
361 mem_cgroup_add_lru_list(page, to);
66e1707b
BS
362}
363
4c4a2214
DR
364int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
365{
366 int ret;
367
368 task_lock(task);
bd845e38 369 ret = task->mm && mm_match_cgroup(task->mm, mem);
4c4a2214
DR
370 task_unlock(task);
371 return ret;
372}
373
58ae83db
KH
374/*
375 * Calculate mapped_ratio under memory controller. This will be used in
376 * vmscan.c for deteremining we have to reclaim mapped pages.
377 */
378int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
379{
380 long total, rss;
381
382 /*
383 * usage is recorded in bytes. But, here, we assume the number of
384 * physical pages can be represented by "long" on any arch.
385 */
386 total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
387 rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
388 return (int)((rss * 100L) / total);
389}
8869b8f6 390
6c48a1d0
KH
391/*
392 * prev_priority control...this will be used in memory reclaim path.
393 */
394int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
395{
396 return mem->prev_priority;
397}
398
399void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
400{
401 if (priority < mem->prev_priority)
402 mem->prev_priority = priority;
403}
404
405void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
406{
407 mem->prev_priority = priority;
408}
409
cc38108e
KH
410/*
411 * Calculate # of pages to be scanned in this priority/zone.
412 * See also vmscan.c
413 *
414 * priority starts from "DEF_PRIORITY" and decremented in each loop.
415 * (see include/linux/mmzone.h)
416 */
417
b69408e8
CL
418long mem_cgroup_calc_reclaim(struct mem_cgroup *mem, struct zone *zone,
419 int priority, enum lru_list lru)
cc38108e 420{
b69408e8 421 long nr_pages;
cc38108e
KH
422 int nid = zone->zone_pgdat->node_id;
423 int zid = zone_idx(zone);
424 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
425
b69408e8 426 nr_pages = MEM_CGROUP_ZSTAT(mz, lru);
cc38108e 427
b69408e8 428 return (nr_pages >> priority);
cc38108e
KH
429}
430
66e1707b
BS
431unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
432 struct list_head *dst,
433 unsigned long *scanned, int order,
434 int mode, struct zone *z,
435 struct mem_cgroup *mem_cont,
4f98a2fe 436 int active, int file)
66e1707b
BS
437{
438 unsigned long nr_taken = 0;
439 struct page *page;
440 unsigned long scan;
441 LIST_HEAD(pc_list);
442 struct list_head *src;
ff7283fa 443 struct page_cgroup *pc, *tmp;
1ecaab2b
KH
444 int nid = z->zone_pgdat->node_id;
445 int zid = zone_idx(z);
446 struct mem_cgroup_per_zone *mz;
4f98a2fe 447 int lru = LRU_FILE * !!file + !!active;
66e1707b 448
cf475ad2 449 BUG_ON(!mem_cont);
1ecaab2b 450 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
b69408e8 451 src = &mz->lists[lru];
66e1707b 452
ff7283fa
KH
453 scan = 0;
454 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
436c6541 455 if (scan >= nr_to_scan)
ff7283fa 456 break;
08e552c6
KH
457
458 page = pc->page;
52d4b9ac
KH
459 if (unlikely(!PageCgroupUsed(pc)))
460 continue;
436c6541 461 if (unlikely(!PageLRU(page)))
ff7283fa 462 continue;
ff7283fa 463
436c6541 464 scan++;
4f98a2fe 465 if (__isolate_lru_page(page, mode, file) == 0) {
66e1707b
BS
466 list_move(&page->lru, dst);
467 nr_taken++;
468 }
469 }
470
66e1707b
BS
471 *scanned = scan;
472 return nr_taken;
473}
474
6d61ef40
BS
475#define mem_cgroup_from_res_counter(counter, member) \
476 container_of(counter, struct mem_cgroup, member)
477
478/*
479 * This routine finds the DFS walk successor. This routine should be
480 * called with cgroup_mutex held
481 */
482static struct mem_cgroup *
483mem_cgroup_get_next_node(struct mem_cgroup *curr, struct mem_cgroup *root_mem)
484{
485 struct cgroup *cgroup, *curr_cgroup, *root_cgroup;
486
487 curr_cgroup = curr->css.cgroup;
488 root_cgroup = root_mem->css.cgroup;
489
490 if (!list_empty(&curr_cgroup->children)) {
491 /*
492 * Walk down to children
493 */
494 mem_cgroup_put(curr);
495 cgroup = list_entry(curr_cgroup->children.next,
496 struct cgroup, sibling);
497 curr = mem_cgroup_from_cont(cgroup);
498 mem_cgroup_get(curr);
499 goto done;
500 }
501
502visit_parent:
503 if (curr_cgroup == root_cgroup) {
504 mem_cgroup_put(curr);
505 curr = root_mem;
506 mem_cgroup_get(curr);
507 goto done;
508 }
509
510 /*
511 * Goto next sibling
512 */
513 if (curr_cgroup->sibling.next != &curr_cgroup->parent->children) {
514 mem_cgroup_put(curr);
515 cgroup = list_entry(curr_cgroup->sibling.next, struct cgroup,
516 sibling);
517 curr = mem_cgroup_from_cont(cgroup);
518 mem_cgroup_get(curr);
519 goto done;
520 }
521
522 /*
523 * Go up to next parent and next parent's sibling if need be
524 */
525 curr_cgroup = curr_cgroup->parent;
526 goto visit_parent;
527
528done:
529 root_mem->last_scanned_child = curr;
530 return curr;
531}
532
533/*
534 * Visit the first child (need not be the first child as per the ordering
535 * of the cgroup list, since we track last_scanned_child) of @mem and use
536 * that to reclaim free pages from.
537 */
538static struct mem_cgroup *
539mem_cgroup_get_first_node(struct mem_cgroup *root_mem)
540{
541 struct cgroup *cgroup;
542 struct mem_cgroup *ret;
543 bool obsolete = (root_mem->last_scanned_child &&
544 root_mem->last_scanned_child->obsolete);
545
546 /*
547 * Scan all children under the mem_cgroup mem
548 */
549 cgroup_lock();
550 if (list_empty(&root_mem->css.cgroup->children)) {
551 ret = root_mem;
552 goto done;
553 }
554
555 if (!root_mem->last_scanned_child || obsolete) {
556
557 if (obsolete)
558 mem_cgroup_put(root_mem->last_scanned_child);
559
560 cgroup = list_first_entry(&root_mem->css.cgroup->children,
561 struct cgroup, sibling);
562 ret = mem_cgroup_from_cont(cgroup);
563 mem_cgroup_get(ret);
564 } else
565 ret = mem_cgroup_get_next_node(root_mem->last_scanned_child,
566 root_mem);
567
568done:
569 root_mem->last_scanned_child = ret;
570 cgroup_unlock();
571 return ret;
572}
573
574/*
575 * Dance down the hierarchy if needed to reclaim memory. We remember the
576 * last child we reclaimed from, so that we don't end up penalizing
577 * one child extensively based on its position in the children list.
578 *
579 * root_mem is the original ancestor that we've been reclaim from.
580 */
581static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
582 gfp_t gfp_mask, bool noswap)
583{
584 struct mem_cgroup *next_mem;
585 int ret = 0;
586
587 /*
588 * Reclaim unconditionally and don't check for return value.
589 * We need to reclaim in the current group and down the tree.
590 * One might think about checking for children before reclaiming,
591 * but there might be left over accounting, even after children
592 * have left.
593 */
594 ret = try_to_free_mem_cgroup_pages(root_mem, gfp_mask, noswap);
595 if (res_counter_check_under_limit(&root_mem->res))
596 return 0;
597
598 next_mem = mem_cgroup_get_first_node(root_mem);
599
600 while (next_mem != root_mem) {
601 if (next_mem->obsolete) {
602 mem_cgroup_put(next_mem);
603 cgroup_lock();
604 next_mem = mem_cgroup_get_first_node(root_mem);
605 cgroup_unlock();
606 continue;
607 }
608 ret = try_to_free_mem_cgroup_pages(next_mem, gfp_mask, noswap);
609 if (res_counter_check_under_limit(&root_mem->res))
610 return 0;
611 cgroup_lock();
612 next_mem = mem_cgroup_get_next_node(next_mem, root_mem);
613 cgroup_unlock();
614 }
615 return ret;
616}
617
f817ed48
KH
618/*
619 * Unlike exported interface, "oom" parameter is added. if oom==true,
620 * oom-killer can be invoked.
8a9f3ccd 621 */
f817ed48 622static int __mem_cgroup_try_charge(struct mm_struct *mm,
8c7c6e34
KH
623 gfp_t gfp_mask, struct mem_cgroup **memcg,
624 bool oom)
8a9f3ccd 625{
6d61ef40 626 struct mem_cgroup *mem, *mem_over_limit;
7a81b88c 627 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
28dbc4b6 628 struct res_counter *fail_res;
8a9f3ccd 629 /*
3be91277
HD
630 * We always charge the cgroup the mm_struct belongs to.
631 * The mm_struct's mem_cgroup changes on task migration if the
8a9f3ccd
BS
632 * thread group leader migrates. It's possible that mm is not
633 * set, if so charge the init_mm (happens for pagecache usage).
634 */
7a81b88c 635 if (likely(!*memcg)) {
e8589cc1
KH
636 rcu_read_lock();
637 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
31a78f23
BS
638 if (unlikely(!mem)) {
639 rcu_read_unlock();
31a78f23
BS
640 return 0;
641 }
e8589cc1
KH
642 /*
643 * For every charge from the cgroup, increment reference count
644 */
645 css_get(&mem->css);
7a81b88c 646 *memcg = mem;
e8589cc1
KH
647 rcu_read_unlock();
648 } else {
7a81b88c
KH
649 mem = *memcg;
650 css_get(&mem->css);
e8589cc1 651 }
8a9f3ccd 652
8c7c6e34
KH
653 while (1) {
654 int ret;
655 bool noswap = false;
7a81b88c 656
28dbc4b6 657 ret = res_counter_charge(&mem->res, PAGE_SIZE, &fail_res);
8c7c6e34
KH
658 if (likely(!ret)) {
659 if (!do_swap_account)
660 break;
28dbc4b6
BS
661 ret = res_counter_charge(&mem->memsw, PAGE_SIZE,
662 &fail_res);
8c7c6e34
KH
663 if (likely(!ret))
664 break;
665 /* mem+swap counter fails */
666 res_counter_uncharge(&mem->res, PAGE_SIZE);
667 noswap = true;
6d61ef40
BS
668 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
669 memsw);
670 } else
671 /* mem counter fails */
672 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
673 res);
674
3be91277 675 if (!(gfp_mask & __GFP_WAIT))
7a81b88c 676 goto nomem;
e1a1cd59 677
6d61ef40
BS
678 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, gfp_mask,
679 noswap);
66e1707b
BS
680
681 /*
8869b8f6
HD
682 * try_to_free_mem_cgroup_pages() might not give us a full
683 * picture of reclaim. Some pages are reclaimed and might be
684 * moved to swap cache or just unmapped from the cgroup.
685 * Check the limit again to see if the reclaim reduced the
686 * current usage of the cgroup before giving up
8c7c6e34 687 *
8869b8f6 688 */
8c7c6e34
KH
689 if (!do_swap_account &&
690 res_counter_check_under_limit(&mem->res))
691 continue;
692 if (do_swap_account &&
693 res_counter_check_under_limit(&mem->memsw))
66e1707b 694 continue;
3be91277
HD
695
696 if (!nr_retries--) {
f817ed48
KH
697 if (oom)
698 mem_cgroup_out_of_memory(mem, gfp_mask);
7a81b88c 699 goto nomem;
66e1707b 700 }
8a9f3ccd 701 }
7a81b88c
KH
702 return 0;
703nomem:
704 css_put(&mem->css);
705 return -ENOMEM;
706}
8a9f3ccd 707
f817ed48
KH
708/**
709 * mem_cgroup_try_charge - get charge of PAGE_SIZE.
710 * @mm: an mm_struct which is charged against. (when *memcg is NULL)
711 * @gfp_mask: gfp_mask for reclaim.
712 * @memcg: a pointer to memory cgroup which is charged against.
713 *
714 * charge against memory cgroup pointed by *memcg. if *memcg == NULL, estimated
715 * memory cgroup from @mm is got and stored in *memcg.
716 *
717 * Returns 0 if success. -ENOMEM at failure.
718 * This call can invoke OOM-Killer.
719 */
720
721int mem_cgroup_try_charge(struct mm_struct *mm,
722 gfp_t mask, struct mem_cgroup **memcg)
723{
724 return __mem_cgroup_try_charge(mm, mask, memcg, true);
725}
726
7a81b88c
KH
727/*
728 * commit a charge got by mem_cgroup_try_charge() and makes page_cgroup to be
729 * USED state. If already USED, uncharge and return.
730 */
731
732static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
733 struct page_cgroup *pc,
734 enum charge_type ctype)
735{
7a81b88c
KH
736 /* try_charge() can return NULL to *memcg, taking care of it. */
737 if (!mem)
738 return;
52d4b9ac
KH
739
740 lock_page_cgroup(pc);
741 if (unlikely(PageCgroupUsed(pc))) {
742 unlock_page_cgroup(pc);
743 res_counter_uncharge(&mem->res, PAGE_SIZE);
8c7c6e34
KH
744 if (do_swap_account)
745 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
52d4b9ac 746 css_put(&mem->css);
7a81b88c 747 return;
52d4b9ac 748 }
8a9f3ccd 749 pc->mem_cgroup = mem;
08e552c6 750 smp_wmb();
c05555b5 751 pc->flags = pcg_default_flags[ctype];
3be91277 752
08e552c6 753 mem_cgroup_charge_statistics(mem, pc, true);
52d4b9ac 754
52d4b9ac 755 unlock_page_cgroup(pc);
7a81b88c 756}
66e1707b 757
f817ed48
KH
758/**
759 * mem_cgroup_move_account - move account of the page
760 * @pc: page_cgroup of the page.
761 * @from: mem_cgroup which the page is moved from.
762 * @to: mem_cgroup which the page is moved to. @from != @to.
763 *
764 * The caller must confirm following.
08e552c6 765 * - page is not on LRU (isolate_page() is useful.)
f817ed48
KH
766 *
767 * returns 0 at success,
768 * returns -EBUSY when lock is busy or "pc" is unstable.
769 *
770 * This function does "uncharge" from old cgroup but doesn't do "charge" to
771 * new cgroup. It should be done by a caller.
772 */
773
774static int mem_cgroup_move_account(struct page_cgroup *pc,
775 struct mem_cgroup *from, struct mem_cgroup *to)
776{
777 struct mem_cgroup_per_zone *from_mz, *to_mz;
778 int nid, zid;
779 int ret = -EBUSY;
780
f817ed48 781 VM_BUG_ON(from == to);
08e552c6 782 VM_BUG_ON(PageLRU(pc->page));
f817ed48
KH
783
784 nid = page_cgroup_nid(pc);
785 zid = page_cgroup_zid(pc);
786 from_mz = mem_cgroup_zoneinfo(from, nid, zid);
787 to_mz = mem_cgroup_zoneinfo(to, nid, zid);
788
f817ed48
KH
789 if (!trylock_page_cgroup(pc))
790 return ret;
791
792 if (!PageCgroupUsed(pc))
793 goto out;
794
795 if (pc->mem_cgroup != from)
796 goto out;
797
08e552c6
KH
798 css_put(&from->css);
799 res_counter_uncharge(&from->res, PAGE_SIZE);
800 mem_cgroup_charge_statistics(from, pc, false);
801 if (do_swap_account)
802 res_counter_uncharge(&from->memsw, PAGE_SIZE);
803 pc->mem_cgroup = to;
804 mem_cgroup_charge_statistics(to, pc, true);
805 css_get(&to->css);
806 ret = 0;
f817ed48
KH
807out:
808 unlock_page_cgroup(pc);
809 return ret;
810}
811
812/*
813 * move charges to its parent.
814 */
815
816static int mem_cgroup_move_parent(struct page_cgroup *pc,
817 struct mem_cgroup *child,
818 gfp_t gfp_mask)
819{
08e552c6 820 struct page *page = pc->page;
f817ed48
KH
821 struct cgroup *cg = child->css.cgroup;
822 struct cgroup *pcg = cg->parent;
823 struct mem_cgroup *parent;
f817ed48
KH
824 int ret;
825
826 /* Is ROOT ? */
827 if (!pcg)
828 return -EINVAL;
829
08e552c6 830
f817ed48
KH
831 parent = mem_cgroup_from_cont(pcg);
832
08e552c6 833
f817ed48
KH
834 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
835 if (ret)
836 return ret;
837
08e552c6
KH
838 if (!get_page_unless_zero(page))
839 return -EBUSY;
840
841 ret = isolate_lru_page(page);
842
843 if (ret)
844 goto cancel;
f817ed48 845
f817ed48 846 ret = mem_cgroup_move_account(pc, child, parent);
f817ed48 847
08e552c6 848 /* drop extra refcnt by try_charge() (move_account increment one) */
f817ed48 849 css_put(&parent->css);
08e552c6
KH
850 putback_lru_page(page);
851 if (!ret) {
852 put_page(page);
853 return 0;
8c7c6e34 854 }
08e552c6
KH
855 /* uncharge if move fails */
856cancel:
857 res_counter_uncharge(&parent->res, PAGE_SIZE);
858 if (do_swap_account)
859 res_counter_uncharge(&parent->memsw, PAGE_SIZE);
860 put_page(page);
f817ed48
KH
861 return ret;
862}
863
7a81b88c
KH
864/*
865 * Charge the memory controller for page usage.
866 * Return
867 * 0 if the charge was successful
868 * < 0 if the cgroup is over its limit
869 */
870static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
871 gfp_t gfp_mask, enum charge_type ctype,
872 struct mem_cgroup *memcg)
873{
874 struct mem_cgroup *mem;
875 struct page_cgroup *pc;
876 int ret;
877
878 pc = lookup_page_cgroup(page);
879 /* can happen at boot */
880 if (unlikely(!pc))
881 return 0;
882 prefetchw(pc);
883
884 mem = memcg;
f817ed48 885 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
7a81b88c
KH
886 if (ret)
887 return ret;
888
889 __mem_cgroup_commit_charge(mem, pc, ctype);
8a9f3ccd 890 return 0;
8a9f3ccd
BS
891}
892
7a81b88c
KH
893int mem_cgroup_newpage_charge(struct page *page,
894 struct mm_struct *mm, gfp_t gfp_mask)
217bc319 895{
f8d66542 896 if (mem_cgroup_disabled())
cede86ac 897 return 0;
52d4b9ac
KH
898 if (PageCompound(page))
899 return 0;
69029cd5
KH
900 /*
901 * If already mapped, we don't have to account.
902 * If page cache, page->mapping has address_space.
903 * But page->mapping may have out-of-use anon_vma pointer,
904 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
905 * is NULL.
906 */
907 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
908 return 0;
909 if (unlikely(!mm))
910 mm = &init_mm;
217bc319 911 return mem_cgroup_charge_common(page, mm, gfp_mask,
e8589cc1 912 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
217bc319
KH
913}
914
e1a1cd59
BS
915int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
916 gfp_t gfp_mask)
8697d331 917{
f8d66542 918 if (mem_cgroup_disabled())
cede86ac 919 return 0;
52d4b9ac
KH
920 if (PageCompound(page))
921 return 0;
accf163e
KH
922 /*
923 * Corner case handling. This is called from add_to_page_cache()
924 * in usual. But some FS (shmem) precharges this page before calling it
925 * and call add_to_page_cache() with GFP_NOWAIT.
926 *
927 * For GFP_NOWAIT case, the page may be pre-charged before calling
928 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
929 * charge twice. (It works but has to pay a bit larger cost.)
930 */
931 if (!(gfp_mask & __GFP_WAIT)) {
932 struct page_cgroup *pc;
933
52d4b9ac
KH
934
935 pc = lookup_page_cgroup(page);
936 if (!pc)
937 return 0;
938 lock_page_cgroup(pc);
939 if (PageCgroupUsed(pc)) {
940 unlock_page_cgroup(pc);
accf163e
KH
941 return 0;
942 }
52d4b9ac 943 unlock_page_cgroup(pc);
accf163e
KH
944 }
945
69029cd5 946 if (unlikely(!mm))
8697d331 947 mm = &init_mm;
accf163e 948
c05555b5
KH
949 if (page_is_file_cache(page))
950 return mem_cgroup_charge_common(page, mm, gfp_mask,
e8589cc1 951 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
c05555b5
KH
952 else
953 return mem_cgroup_charge_common(page, mm, gfp_mask,
954 MEM_CGROUP_CHARGE_TYPE_SHMEM, NULL);
e8589cc1
KH
955}
956
8c7c6e34
KH
957int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
958 struct page *page,
959 gfp_t mask, struct mem_cgroup **ptr)
960{
961 struct mem_cgroup *mem;
962 swp_entry_t ent;
963
f8d66542 964 if (mem_cgroup_disabled())
8c7c6e34
KH
965 return 0;
966
967 if (!do_swap_account)
968 goto charge_cur_mm;
969
970 /*
971 * A racing thread's fault, or swapoff, may have already updated
972 * the pte, and even removed page from swap cache: return success
973 * to go on to do_swap_page()'s pte_same() test, which should fail.
974 */
975 if (!PageSwapCache(page))
976 return 0;
977
978 ent.val = page_private(page);
979
980 mem = lookup_swap_cgroup(ent);
981 if (!mem || mem->obsolete)
982 goto charge_cur_mm;
983 *ptr = mem;
984 return __mem_cgroup_try_charge(NULL, mask, ptr, true);
985charge_cur_mm:
986 if (unlikely(!mm))
987 mm = &init_mm;
988 return __mem_cgroup_try_charge(mm, mask, ptr, true);
989}
990
d13d1443 991#ifdef CONFIG_SWAP
8c7c6e34 992
d13d1443
KH
993int mem_cgroup_cache_charge_swapin(struct page *page,
994 struct mm_struct *mm, gfp_t mask, bool locked)
995{
996 int ret = 0;
997
f8d66542 998 if (mem_cgroup_disabled())
d13d1443
KH
999 return 0;
1000 if (unlikely(!mm))
1001 mm = &init_mm;
1002 if (!locked)
1003 lock_page(page);
1004 /*
1005 * If not locked, the page can be dropped from SwapCache until
1006 * we reach here.
1007 */
1008 if (PageSwapCache(page)) {
8c7c6e34
KH
1009 struct mem_cgroup *mem = NULL;
1010 swp_entry_t ent;
1011
1012 ent.val = page_private(page);
1013 if (do_swap_account) {
1014 mem = lookup_swap_cgroup(ent);
1015 if (mem && mem->obsolete)
1016 mem = NULL;
1017 if (mem)
1018 mm = NULL;
1019 }
d13d1443 1020 ret = mem_cgroup_charge_common(page, mm, mask,
8c7c6e34
KH
1021 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
1022
1023 if (!ret && do_swap_account) {
1024 /* avoid double counting */
1025 mem = swap_cgroup_record(ent, NULL);
1026 if (mem) {
1027 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1028 mem_cgroup_put(mem);
1029 }
1030 }
d13d1443
KH
1031 }
1032 if (!locked)
1033 unlock_page(page);
08e552c6
KH
1034 /* add this page(page_cgroup) to the LRU we want. */
1035 mem_cgroup_lru_fixup(page);
d13d1443
KH
1036
1037 return ret;
1038}
1039#endif
1040
7a81b88c
KH
1041void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
1042{
1043 struct page_cgroup *pc;
1044
f8d66542 1045 if (mem_cgroup_disabled())
7a81b88c
KH
1046 return;
1047 if (!ptr)
1048 return;
1049 pc = lookup_page_cgroup(page);
1050 __mem_cgroup_commit_charge(ptr, pc, MEM_CGROUP_CHARGE_TYPE_MAPPED);
8c7c6e34
KH
1051 /*
1052 * Now swap is on-memory. This means this page may be
1053 * counted both as mem and swap....double count.
1054 * Fix it by uncharging from memsw. This SwapCache is stable
1055 * because we're still under lock_page().
1056 */
1057 if (do_swap_account) {
1058 swp_entry_t ent = {.val = page_private(page)};
1059 struct mem_cgroup *memcg;
1060 memcg = swap_cgroup_record(ent, NULL);
1061 if (memcg) {
1062 /* If memcg is obsolete, memcg can be != ptr */
1063 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1064 mem_cgroup_put(memcg);
1065 }
1066
1067 }
08e552c6
KH
1068 /* add this page(page_cgroup) to the LRU we want. */
1069 mem_cgroup_lru_fixup(page);
7a81b88c
KH
1070}
1071
1072void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
1073{
f8d66542 1074 if (mem_cgroup_disabled())
7a81b88c
KH
1075 return;
1076 if (!mem)
1077 return;
1078 res_counter_uncharge(&mem->res, PAGE_SIZE);
8c7c6e34
KH
1079 if (do_swap_account)
1080 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
7a81b88c
KH
1081 css_put(&mem->css);
1082}
1083
1084
8a9f3ccd 1085/*
69029cd5 1086 * uncharge if !page_mapped(page)
8a9f3ccd 1087 */
8c7c6e34 1088static struct mem_cgroup *
69029cd5 1089__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
8a9f3ccd 1090{
8289546e 1091 struct page_cgroup *pc;
8c7c6e34 1092 struct mem_cgroup *mem = NULL;
072c56c1 1093 struct mem_cgroup_per_zone *mz;
8a9f3ccd 1094
f8d66542 1095 if (mem_cgroup_disabled())
8c7c6e34 1096 return NULL;
4077960e 1097
d13d1443 1098 if (PageSwapCache(page))
8c7c6e34 1099 return NULL;
d13d1443 1100
8697d331 1101 /*
3c541e14 1102 * Check if our page_cgroup is valid
8697d331 1103 */
52d4b9ac
KH
1104 pc = lookup_page_cgroup(page);
1105 if (unlikely(!pc || !PageCgroupUsed(pc)))
8c7c6e34 1106 return NULL;
b9c565d5 1107
52d4b9ac 1108 lock_page_cgroup(pc);
d13d1443 1109
8c7c6e34
KH
1110 mem = pc->mem_cgroup;
1111
d13d1443
KH
1112 if (!PageCgroupUsed(pc))
1113 goto unlock_out;
1114
1115 switch (ctype) {
1116 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1117 if (page_mapped(page))
1118 goto unlock_out;
1119 break;
1120 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
1121 if (!PageAnon(page)) { /* Shared memory */
1122 if (page->mapping && !page_is_file_cache(page))
1123 goto unlock_out;
1124 } else if (page_mapped(page)) /* Anon */
1125 goto unlock_out;
1126 break;
1127 default:
1128 break;
52d4b9ac 1129 }
d13d1443 1130
8c7c6e34
KH
1131 res_counter_uncharge(&mem->res, PAGE_SIZE);
1132 if (do_swap_account && (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT))
1133 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1134
08e552c6 1135 mem_cgroup_charge_statistics(mem, pc, false);
52d4b9ac 1136 ClearPageCgroupUsed(pc);
b9c565d5 1137
69029cd5 1138 mz = page_cgroup_zoneinfo(pc);
52d4b9ac 1139 unlock_page_cgroup(pc);
fb59e9f1 1140
69029cd5 1141 css_put(&mem->css);
6d12e2d8 1142
8c7c6e34 1143 return mem;
d13d1443
KH
1144
1145unlock_out:
1146 unlock_page_cgroup(pc);
8c7c6e34 1147 return NULL;
3c541e14
BS
1148}
1149
69029cd5
KH
1150void mem_cgroup_uncharge_page(struct page *page)
1151{
52d4b9ac
KH
1152 /* early check. */
1153 if (page_mapped(page))
1154 return;
1155 if (page->mapping && !PageAnon(page))
1156 return;
69029cd5
KH
1157 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
1158}
1159
1160void mem_cgroup_uncharge_cache_page(struct page *page)
1161{
1162 VM_BUG_ON(page_mapped(page));
b7abea96 1163 VM_BUG_ON(page->mapping);
69029cd5
KH
1164 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
1165}
1166
8c7c6e34
KH
1167/*
1168 * called from __delete_from_swap_cache() and drop "page" account.
1169 * memcg information is recorded to swap_cgroup of "ent"
1170 */
1171void mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent)
1172{
1173 struct mem_cgroup *memcg;
1174
1175 memcg = __mem_cgroup_uncharge_common(page,
1176 MEM_CGROUP_CHARGE_TYPE_SWAPOUT);
1177 /* record memcg information */
1178 if (do_swap_account && memcg) {
1179 swap_cgroup_record(ent, memcg);
1180 mem_cgroup_get(memcg);
1181 }
1182}
1183
1184#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1185/*
1186 * called from swap_entry_free(). remove record in swap_cgroup and
1187 * uncharge "memsw" account.
1188 */
1189void mem_cgroup_uncharge_swap(swp_entry_t ent)
d13d1443 1190{
8c7c6e34
KH
1191 struct mem_cgroup *memcg;
1192
1193 if (!do_swap_account)
1194 return;
1195
1196 memcg = swap_cgroup_record(ent, NULL);
1197 if (memcg) {
1198 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1199 mem_cgroup_put(memcg);
1200 }
d13d1443 1201}
8c7c6e34 1202#endif
d13d1443 1203
ae41be37 1204/*
01b1ae63
KH
1205 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
1206 * page belongs to.
ae41be37 1207 */
01b1ae63 1208int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
ae41be37
KH
1209{
1210 struct page_cgroup *pc;
e8589cc1 1211 struct mem_cgroup *mem = NULL;
e8589cc1 1212 int ret = 0;
8869b8f6 1213
f8d66542 1214 if (mem_cgroup_disabled())
4077960e
BS
1215 return 0;
1216
52d4b9ac
KH
1217 pc = lookup_page_cgroup(page);
1218 lock_page_cgroup(pc);
1219 if (PageCgroupUsed(pc)) {
e8589cc1
KH
1220 mem = pc->mem_cgroup;
1221 css_get(&mem->css);
e8589cc1 1222 }
52d4b9ac 1223 unlock_page_cgroup(pc);
01b1ae63 1224
e8589cc1 1225 if (mem) {
01b1ae63 1226 ret = mem_cgroup_try_charge(NULL, GFP_HIGHUSER_MOVABLE, &mem);
e8589cc1
KH
1227 css_put(&mem->css);
1228 }
01b1ae63 1229 *ptr = mem;
e8589cc1 1230 return ret;
ae41be37 1231}
8869b8f6 1232
69029cd5 1233/* remove redundant charge if migration failed*/
01b1ae63
KH
1234void mem_cgroup_end_migration(struct mem_cgroup *mem,
1235 struct page *oldpage, struct page *newpage)
ae41be37 1236{
01b1ae63
KH
1237 struct page *target, *unused;
1238 struct page_cgroup *pc;
1239 enum charge_type ctype;
1240
1241 if (!mem)
1242 return;
1243
1244 /* at migration success, oldpage->mapping is NULL. */
1245 if (oldpage->mapping) {
1246 target = oldpage;
1247 unused = NULL;
1248 } else {
1249 target = newpage;
1250 unused = oldpage;
1251 }
1252
1253 if (PageAnon(target))
1254 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
1255 else if (page_is_file_cache(target))
1256 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
1257 else
1258 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
1259
1260 /* unused page is not on radix-tree now. */
d13d1443 1261 if (unused)
01b1ae63
KH
1262 __mem_cgroup_uncharge_common(unused, ctype);
1263
1264 pc = lookup_page_cgroup(target);
69029cd5 1265 /*
01b1ae63
KH
1266 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
1267 * So, double-counting is effectively avoided.
1268 */
1269 __mem_cgroup_commit_charge(mem, pc, ctype);
1270
1271 /*
1272 * Both of oldpage and newpage are still under lock_page().
1273 * Then, we don't have to care about race in radix-tree.
1274 * But we have to be careful that this page is unmapped or not.
1275 *
1276 * There is a case for !page_mapped(). At the start of
1277 * migration, oldpage was mapped. But now, it's zapped.
1278 * But we know *target* page is not freed/reused under us.
1279 * mem_cgroup_uncharge_page() does all necessary checks.
69029cd5 1280 */
01b1ae63
KH
1281 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
1282 mem_cgroup_uncharge_page(target);
ae41be37 1283}
78fb7466 1284
c9b0ed51
KH
1285/*
1286 * A call to try to shrink memory usage under specified resource controller.
1287 * This is typically used for page reclaiming for shmem for reducing side
1288 * effect of page allocation from shmem, which is used by some mem_cgroup.
1289 */
1290int mem_cgroup_shrink_usage(struct mm_struct *mm, gfp_t gfp_mask)
1291{
1292 struct mem_cgroup *mem;
1293 int progress = 0;
1294 int retry = MEM_CGROUP_RECLAIM_RETRIES;
1295
f8d66542 1296 if (mem_cgroup_disabled())
cede86ac 1297 return 0;
9623e078
HD
1298 if (!mm)
1299 return 0;
cede86ac 1300
c9b0ed51
KH
1301 rcu_read_lock();
1302 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
31a78f23
BS
1303 if (unlikely(!mem)) {
1304 rcu_read_unlock();
1305 return 0;
1306 }
c9b0ed51
KH
1307 css_get(&mem->css);
1308 rcu_read_unlock();
1309
1310 do {
8c7c6e34 1311 progress = try_to_free_mem_cgroup_pages(mem, gfp_mask, true);
a10cebf5 1312 progress += res_counter_check_under_limit(&mem->res);
c9b0ed51
KH
1313 } while (!progress && --retry);
1314
1315 css_put(&mem->css);
1316 if (!retry)
1317 return -ENOMEM;
1318 return 0;
1319}
1320
8c7c6e34
KH
1321static DEFINE_MUTEX(set_limit_mutex);
1322
d38d2a75 1323static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
8c7c6e34 1324 unsigned long long val)
628f4235
KH
1325{
1326
1327 int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
1328 int progress;
8c7c6e34 1329 u64 memswlimit;
628f4235
KH
1330 int ret = 0;
1331
8c7c6e34 1332 while (retry_count) {
628f4235
KH
1333 if (signal_pending(current)) {
1334 ret = -EINTR;
1335 break;
1336 }
8c7c6e34
KH
1337 /*
1338 * Rather than hide all in some function, I do this in
1339 * open coded manner. You see what this really does.
1340 * We have to guarantee mem->res.limit < mem->memsw.limit.
1341 */
1342 mutex_lock(&set_limit_mutex);
1343 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1344 if (memswlimit < val) {
1345 ret = -EINVAL;
1346 mutex_unlock(&set_limit_mutex);
628f4235
KH
1347 break;
1348 }
8c7c6e34
KH
1349 ret = res_counter_set_limit(&memcg->res, val);
1350 mutex_unlock(&set_limit_mutex);
1351
1352 if (!ret)
1353 break;
1354
bced0520 1355 progress = try_to_free_mem_cgroup_pages(memcg,
8c7c6e34
KH
1356 GFP_HIGHUSER_MOVABLE, false);
1357 if (!progress) retry_count--;
1358 }
1359 return ret;
1360}
1361
1362int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
1363 unsigned long long val)
1364{
1365 int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
1366 u64 memlimit, oldusage, curusage;
1367 int ret;
1368
1369 if (!do_swap_account)
1370 return -EINVAL;
1371
1372 while (retry_count) {
1373 if (signal_pending(current)) {
1374 ret = -EINTR;
1375 break;
1376 }
1377 /*
1378 * Rather than hide all in some function, I do this in
1379 * open coded manner. You see what this really does.
1380 * We have to guarantee mem->res.limit < mem->memsw.limit.
1381 */
1382 mutex_lock(&set_limit_mutex);
1383 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
1384 if (memlimit > val) {
1385 ret = -EINVAL;
1386 mutex_unlock(&set_limit_mutex);
1387 break;
1388 }
1389 ret = res_counter_set_limit(&memcg->memsw, val);
1390 mutex_unlock(&set_limit_mutex);
1391
1392 if (!ret)
1393 break;
1394
1395 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
1396 try_to_free_mem_cgroup_pages(memcg, GFP_HIGHUSER_MOVABLE, true);
1397 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
1398 if (curusage >= oldusage)
628f4235
KH
1399 retry_count--;
1400 }
1401 return ret;
1402}
1403
cc847582
KH
1404/*
1405 * This routine traverse page_cgroup in given list and drop them all.
cc847582
KH
1406 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
1407 */
f817ed48 1408static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
08e552c6 1409 int node, int zid, enum lru_list lru)
cc847582 1410{
08e552c6
KH
1411 struct zone *zone;
1412 struct mem_cgroup_per_zone *mz;
f817ed48 1413 struct page_cgroup *pc, *busy;
08e552c6 1414 unsigned long flags, loop;
072c56c1 1415 struct list_head *list;
f817ed48 1416 int ret = 0;
072c56c1 1417
08e552c6
KH
1418 zone = &NODE_DATA(node)->node_zones[zid];
1419 mz = mem_cgroup_zoneinfo(mem, node, zid);
b69408e8 1420 list = &mz->lists[lru];
cc847582 1421
f817ed48
KH
1422 loop = MEM_CGROUP_ZSTAT(mz, lru);
1423 /* give some margin against EBUSY etc...*/
1424 loop += 256;
1425 busy = NULL;
1426 while (loop--) {
1427 ret = 0;
08e552c6 1428 spin_lock_irqsave(&zone->lru_lock, flags);
f817ed48 1429 if (list_empty(list)) {
08e552c6 1430 spin_unlock_irqrestore(&zone->lru_lock, flags);
52d4b9ac 1431 break;
f817ed48
KH
1432 }
1433 pc = list_entry(list->prev, struct page_cgroup, lru);
1434 if (busy == pc) {
1435 list_move(&pc->lru, list);
1436 busy = 0;
08e552c6 1437 spin_unlock_irqrestore(&zone->lru_lock, flags);
f817ed48
KH
1438 continue;
1439 }
08e552c6 1440 spin_unlock_irqrestore(&zone->lru_lock, flags);
f817ed48
KH
1441
1442 ret = mem_cgroup_move_parent(pc, mem, GFP_HIGHUSER_MOVABLE);
1443 if (ret == -ENOMEM)
52d4b9ac 1444 break;
f817ed48
KH
1445
1446 if (ret == -EBUSY || ret == -EINVAL) {
1447 /* found lock contention or "pc" is obsolete. */
1448 busy = pc;
1449 cond_resched();
1450 } else
1451 busy = NULL;
cc847582 1452 }
08e552c6 1453
f817ed48
KH
1454 if (!ret && !list_empty(list))
1455 return -EBUSY;
1456 return ret;
cc847582
KH
1457}
1458
1459/*
1460 * make mem_cgroup's charge to be 0 if there is no task.
1461 * This enables deleting this mem_cgroup.
1462 */
c1e862c1 1463static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
cc847582 1464{
f817ed48
KH
1465 int ret;
1466 int node, zid, shrink;
1467 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
c1e862c1 1468 struct cgroup *cgrp = mem->css.cgroup;
8869b8f6 1469
cc847582 1470 css_get(&mem->css);
f817ed48
KH
1471
1472 shrink = 0;
c1e862c1
KH
1473 /* should free all ? */
1474 if (free_all)
1475 goto try_to_free;
f817ed48 1476move_account:
1ecaab2b 1477 while (mem->res.usage > 0) {
f817ed48 1478 ret = -EBUSY;
c1e862c1
KH
1479 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
1480 goto out;
1481 ret = -EINTR;
1482 if (signal_pending(current))
cc847582 1483 goto out;
52d4b9ac
KH
1484 /* This is for making all *used* pages to be on LRU. */
1485 lru_add_drain_all();
f817ed48
KH
1486 ret = 0;
1487 for_each_node_state(node, N_POSSIBLE) {
1488 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
b69408e8 1489 enum lru_list l;
f817ed48
KH
1490 for_each_lru(l) {
1491 ret = mem_cgroup_force_empty_list(mem,
08e552c6 1492 node, zid, l);
f817ed48
KH
1493 if (ret)
1494 break;
1495 }
1ecaab2b 1496 }
f817ed48
KH
1497 if (ret)
1498 break;
1499 }
1500 /* it seems parent cgroup doesn't have enough mem */
1501 if (ret == -ENOMEM)
1502 goto try_to_free;
52d4b9ac 1503 cond_resched();
cc847582
KH
1504 }
1505 ret = 0;
1506out:
1507 css_put(&mem->css);
1508 return ret;
f817ed48
KH
1509
1510try_to_free:
c1e862c1
KH
1511 /* returns EBUSY if there is a task or if we come here twice. */
1512 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
f817ed48
KH
1513 ret = -EBUSY;
1514 goto out;
1515 }
c1e862c1
KH
1516 /* we call try-to-free pages for make this cgroup empty */
1517 lru_add_drain_all();
f817ed48
KH
1518 /* try to free all pages in this cgroup */
1519 shrink = 1;
1520 while (nr_retries && mem->res.usage > 0) {
1521 int progress;
c1e862c1
KH
1522
1523 if (signal_pending(current)) {
1524 ret = -EINTR;
1525 goto out;
1526 }
f817ed48 1527 progress = try_to_free_mem_cgroup_pages(mem,
8c7c6e34 1528 GFP_HIGHUSER_MOVABLE, false);
c1e862c1 1529 if (!progress) {
f817ed48 1530 nr_retries--;
c1e862c1
KH
1531 /* maybe some writeback is necessary */
1532 congestion_wait(WRITE, HZ/10);
1533 }
f817ed48
KH
1534
1535 }
08e552c6 1536 lru_add_drain();
f817ed48
KH
1537 /* try move_account...there may be some *locked* pages. */
1538 if (mem->res.usage)
1539 goto move_account;
1540 ret = 0;
1541 goto out;
cc847582
KH
1542}
1543
c1e862c1
KH
1544int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
1545{
1546 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
1547}
1548
1549
18f59ea7
BS
1550static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
1551{
1552 return mem_cgroup_from_cont(cont)->use_hierarchy;
1553}
1554
1555static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
1556 u64 val)
1557{
1558 int retval = 0;
1559 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1560 struct cgroup *parent = cont->parent;
1561 struct mem_cgroup *parent_mem = NULL;
1562
1563 if (parent)
1564 parent_mem = mem_cgroup_from_cont(parent);
1565
1566 cgroup_lock();
1567 /*
1568 * If parent's use_hiearchy is set, we can't make any modifications
1569 * in the child subtrees. If it is unset, then the change can
1570 * occur, provided the current cgroup has no children.
1571 *
1572 * For the root cgroup, parent_mem is NULL, we allow value to be
1573 * set if there are no children.
1574 */
1575 if ((!parent_mem || !parent_mem->use_hierarchy) &&
1576 (val == 1 || val == 0)) {
1577 if (list_empty(&cont->children))
1578 mem->use_hierarchy = val;
1579 else
1580 retval = -EBUSY;
1581 } else
1582 retval = -EINVAL;
1583 cgroup_unlock();
1584
1585 return retval;
1586}
1587
2c3daa72 1588static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
8cdea7c0 1589{
8c7c6e34
KH
1590 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1591 u64 val = 0;
1592 int type, name;
1593
1594 type = MEMFILE_TYPE(cft->private);
1595 name = MEMFILE_ATTR(cft->private);
1596 switch (type) {
1597 case _MEM:
1598 val = res_counter_read_u64(&mem->res, name);
1599 break;
1600 case _MEMSWAP:
1601 if (do_swap_account)
1602 val = res_counter_read_u64(&mem->memsw, name);
1603 break;
1604 default:
1605 BUG();
1606 break;
1607 }
1608 return val;
8cdea7c0 1609}
628f4235
KH
1610/*
1611 * The user of this function is...
1612 * RES_LIMIT.
1613 */
856c13aa
PM
1614static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
1615 const char *buffer)
8cdea7c0 1616{
628f4235 1617 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
8c7c6e34 1618 int type, name;
628f4235
KH
1619 unsigned long long val;
1620 int ret;
1621
8c7c6e34
KH
1622 type = MEMFILE_TYPE(cft->private);
1623 name = MEMFILE_ATTR(cft->private);
1624 switch (name) {
628f4235
KH
1625 case RES_LIMIT:
1626 /* This function does all necessary parse...reuse it */
1627 ret = res_counter_memparse_write_strategy(buffer, &val);
8c7c6e34
KH
1628 if (ret)
1629 break;
1630 if (type == _MEM)
628f4235 1631 ret = mem_cgroup_resize_limit(memcg, val);
8c7c6e34
KH
1632 else
1633 ret = mem_cgroup_resize_memsw_limit(memcg, val);
628f4235
KH
1634 break;
1635 default:
1636 ret = -EINVAL; /* should be BUG() ? */
1637 break;
1638 }
1639 return ret;
8cdea7c0
BS
1640}
1641
29f2a4da 1642static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
c84872e1
PE
1643{
1644 struct mem_cgroup *mem;
8c7c6e34 1645 int type, name;
c84872e1
PE
1646
1647 mem = mem_cgroup_from_cont(cont);
8c7c6e34
KH
1648 type = MEMFILE_TYPE(event);
1649 name = MEMFILE_ATTR(event);
1650 switch (name) {
29f2a4da 1651 case RES_MAX_USAGE:
8c7c6e34
KH
1652 if (type == _MEM)
1653 res_counter_reset_max(&mem->res);
1654 else
1655 res_counter_reset_max(&mem->memsw);
29f2a4da
PE
1656 break;
1657 case RES_FAILCNT:
8c7c6e34
KH
1658 if (type == _MEM)
1659 res_counter_reset_failcnt(&mem->res);
1660 else
1661 res_counter_reset_failcnt(&mem->memsw);
29f2a4da
PE
1662 break;
1663 }
85cc59db 1664 return 0;
c84872e1
PE
1665}
1666
d2ceb9b7
KH
1667static const struct mem_cgroup_stat_desc {
1668 const char *msg;
1669 u64 unit;
1670} mem_cgroup_stat_desc[] = {
1671 [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
1672 [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
55e462b0
BR
1673 [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, },
1674 [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, },
d2ceb9b7
KH
1675};
1676
c64745cf
PM
1677static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
1678 struct cgroup_map_cb *cb)
d2ceb9b7 1679{
d2ceb9b7
KH
1680 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
1681 struct mem_cgroup_stat *stat = &mem_cont->stat;
1682 int i;
1683
1684 for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
1685 s64 val;
1686
1687 val = mem_cgroup_read_stat(stat, i);
1688 val *= mem_cgroup_stat_desc[i].unit;
c64745cf 1689 cb->fill(cb, mem_cgroup_stat_desc[i].msg, val);
d2ceb9b7 1690 }
6d12e2d8
KH
1691 /* showing # of active pages */
1692 {
4f98a2fe
RR
1693 unsigned long active_anon, inactive_anon;
1694 unsigned long active_file, inactive_file;
7b854121 1695 unsigned long unevictable;
4f98a2fe
RR
1696
1697 inactive_anon = mem_cgroup_get_all_zonestat(mem_cont,
1698 LRU_INACTIVE_ANON);
1699 active_anon = mem_cgroup_get_all_zonestat(mem_cont,
1700 LRU_ACTIVE_ANON);
1701 inactive_file = mem_cgroup_get_all_zonestat(mem_cont,
1702 LRU_INACTIVE_FILE);
1703 active_file = mem_cgroup_get_all_zonestat(mem_cont,
1704 LRU_ACTIVE_FILE);
7b854121
LS
1705 unevictable = mem_cgroup_get_all_zonestat(mem_cont,
1706 LRU_UNEVICTABLE);
1707
4f98a2fe
RR
1708 cb->fill(cb, "active_anon", (active_anon) * PAGE_SIZE);
1709 cb->fill(cb, "inactive_anon", (inactive_anon) * PAGE_SIZE);
1710 cb->fill(cb, "active_file", (active_file) * PAGE_SIZE);
1711 cb->fill(cb, "inactive_file", (inactive_file) * PAGE_SIZE);
7b854121
LS
1712 cb->fill(cb, "unevictable", unevictable * PAGE_SIZE);
1713
6d12e2d8 1714 }
d2ceb9b7
KH
1715 return 0;
1716}
1717
c1e862c1 1718
8cdea7c0
BS
1719static struct cftype mem_cgroup_files[] = {
1720 {
0eea1030 1721 .name = "usage_in_bytes",
8c7c6e34 1722 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
2c3daa72 1723 .read_u64 = mem_cgroup_read,
8cdea7c0 1724 },
c84872e1
PE
1725 {
1726 .name = "max_usage_in_bytes",
8c7c6e34 1727 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
29f2a4da 1728 .trigger = mem_cgroup_reset,
c84872e1
PE
1729 .read_u64 = mem_cgroup_read,
1730 },
8cdea7c0 1731 {
0eea1030 1732 .name = "limit_in_bytes",
8c7c6e34 1733 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
856c13aa 1734 .write_string = mem_cgroup_write,
2c3daa72 1735 .read_u64 = mem_cgroup_read,
8cdea7c0
BS
1736 },
1737 {
1738 .name = "failcnt",
8c7c6e34 1739 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
29f2a4da 1740 .trigger = mem_cgroup_reset,
2c3daa72 1741 .read_u64 = mem_cgroup_read,
8cdea7c0 1742 },
d2ceb9b7
KH
1743 {
1744 .name = "stat",
c64745cf 1745 .read_map = mem_control_stat_show,
d2ceb9b7 1746 },
c1e862c1
KH
1747 {
1748 .name = "force_empty",
1749 .trigger = mem_cgroup_force_empty_write,
1750 },
18f59ea7
BS
1751 {
1752 .name = "use_hierarchy",
1753 .write_u64 = mem_cgroup_hierarchy_write,
1754 .read_u64 = mem_cgroup_hierarchy_read,
1755 },
8cdea7c0
BS
1756};
1757
8c7c6e34
KH
1758#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1759static struct cftype memsw_cgroup_files[] = {
1760 {
1761 .name = "memsw.usage_in_bytes",
1762 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
1763 .read_u64 = mem_cgroup_read,
1764 },
1765 {
1766 .name = "memsw.max_usage_in_bytes",
1767 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
1768 .trigger = mem_cgroup_reset,
1769 .read_u64 = mem_cgroup_read,
1770 },
1771 {
1772 .name = "memsw.limit_in_bytes",
1773 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
1774 .write_string = mem_cgroup_write,
1775 .read_u64 = mem_cgroup_read,
1776 },
1777 {
1778 .name = "memsw.failcnt",
1779 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
1780 .trigger = mem_cgroup_reset,
1781 .read_u64 = mem_cgroup_read,
1782 },
1783};
1784
1785static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
1786{
1787 if (!do_swap_account)
1788 return 0;
1789 return cgroup_add_files(cont, ss, memsw_cgroup_files,
1790 ARRAY_SIZE(memsw_cgroup_files));
1791};
1792#else
1793static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
1794{
1795 return 0;
1796}
1797#endif
1798
6d12e2d8
KH
1799static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1800{
1801 struct mem_cgroup_per_node *pn;
1ecaab2b 1802 struct mem_cgroup_per_zone *mz;
b69408e8 1803 enum lru_list l;
41e3355d 1804 int zone, tmp = node;
1ecaab2b
KH
1805 /*
1806 * This routine is called against possible nodes.
1807 * But it's BUG to call kmalloc() against offline node.
1808 *
1809 * TODO: this routine can waste much memory for nodes which will
1810 * never be onlined. It's better to use memory hotplug callback
1811 * function.
1812 */
41e3355d
KH
1813 if (!node_state(node, N_NORMAL_MEMORY))
1814 tmp = -1;
1815 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
6d12e2d8
KH
1816 if (!pn)
1817 return 1;
1ecaab2b 1818
6d12e2d8
KH
1819 mem->info.nodeinfo[node] = pn;
1820 memset(pn, 0, sizeof(*pn));
1ecaab2b
KH
1821
1822 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
1823 mz = &pn->zoneinfo[zone];
b69408e8
CL
1824 for_each_lru(l)
1825 INIT_LIST_HEAD(&mz->lists[l]);
1ecaab2b 1826 }
6d12e2d8
KH
1827 return 0;
1828}
1829
1ecaab2b
KH
1830static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1831{
1832 kfree(mem->info.nodeinfo[node]);
1833}
1834
c8dad2bb
JB
1835static int mem_cgroup_size(void)
1836{
1837 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
1838 return sizeof(struct mem_cgroup) + cpustat_size;
1839}
1840
33327948
KH
1841static struct mem_cgroup *mem_cgroup_alloc(void)
1842{
1843 struct mem_cgroup *mem;
c8dad2bb 1844 int size = mem_cgroup_size();
33327948 1845
c8dad2bb
JB
1846 if (size < PAGE_SIZE)
1847 mem = kmalloc(size, GFP_KERNEL);
33327948 1848 else
c8dad2bb 1849 mem = vmalloc(size);
33327948
KH
1850
1851 if (mem)
c8dad2bb 1852 memset(mem, 0, size);
33327948
KH
1853 return mem;
1854}
1855
8c7c6e34
KH
1856/*
1857 * At destroying mem_cgroup, references from swap_cgroup can remain.
1858 * (scanning all at force_empty is too costly...)
1859 *
1860 * Instead of clearing all references at force_empty, we remember
1861 * the number of reference from swap_cgroup and free mem_cgroup when
1862 * it goes down to 0.
1863 *
1864 * When mem_cgroup is destroyed, mem->obsolete will be set to 0 and
1865 * entry which points to this memcg will be ignore at swapin.
1866 *
1867 * Removal of cgroup itself succeeds regardless of refs from swap.
1868 */
1869
33327948
KH
1870static void mem_cgroup_free(struct mem_cgroup *mem)
1871{
08e552c6
KH
1872 int node;
1873
8c7c6e34
KH
1874 if (atomic_read(&mem->refcnt) > 0)
1875 return;
08e552c6
KH
1876
1877
1878 for_each_node_state(node, N_POSSIBLE)
1879 free_mem_cgroup_per_zone_info(mem, node);
1880
c8dad2bb 1881 if (mem_cgroup_size() < PAGE_SIZE)
33327948
KH
1882 kfree(mem);
1883 else
1884 vfree(mem);
1885}
1886
8c7c6e34
KH
1887static void mem_cgroup_get(struct mem_cgroup *mem)
1888{
1889 atomic_inc(&mem->refcnt);
1890}
1891
1892static void mem_cgroup_put(struct mem_cgroup *mem)
1893{
1894 if (atomic_dec_and_test(&mem->refcnt)) {
1895 if (!mem->obsolete)
1896 return;
1897 mem_cgroup_free(mem);
1898 }
1899}
1900
33327948 1901
c077719b
KH
1902#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1903static void __init enable_swap_cgroup(void)
1904{
f8d66542 1905 if (!mem_cgroup_disabled() && really_do_swap_account)
c077719b
KH
1906 do_swap_account = 1;
1907}
1908#else
1909static void __init enable_swap_cgroup(void)
1910{
1911}
1912#endif
1913
8cdea7c0
BS
1914static struct cgroup_subsys_state *
1915mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
1916{
28dbc4b6 1917 struct mem_cgroup *mem, *parent;
6d12e2d8 1918 int node;
8cdea7c0 1919
c8dad2bb
JB
1920 mem = mem_cgroup_alloc();
1921 if (!mem)
1922 return ERR_PTR(-ENOMEM);
78fb7466 1923
6d12e2d8
KH
1924 for_each_node_state(node, N_POSSIBLE)
1925 if (alloc_mem_cgroup_per_zone_info(mem, node))
1926 goto free_out;
c077719b 1927 /* root ? */
28dbc4b6 1928 if (cont->parent == NULL) {
c077719b 1929 enable_swap_cgroup();
28dbc4b6 1930 parent = NULL;
18f59ea7 1931 } else {
28dbc4b6 1932 parent = mem_cgroup_from_cont(cont->parent);
18f59ea7
BS
1933 mem->use_hierarchy = parent->use_hierarchy;
1934 }
28dbc4b6 1935
18f59ea7
BS
1936 if (parent && parent->use_hierarchy) {
1937 res_counter_init(&mem->res, &parent->res);
1938 res_counter_init(&mem->memsw, &parent->memsw);
1939 } else {
1940 res_counter_init(&mem->res, NULL);
1941 res_counter_init(&mem->memsw, NULL);
1942 }
6d12e2d8 1943
6d61ef40
BS
1944 mem->last_scanned_child = NULL;
1945
8cdea7c0 1946 return &mem->css;
6d12e2d8
KH
1947free_out:
1948 for_each_node_state(node, N_POSSIBLE)
1ecaab2b 1949 free_mem_cgroup_per_zone_info(mem, node);
c8dad2bb 1950 mem_cgroup_free(mem);
2dda81ca 1951 return ERR_PTR(-ENOMEM);
8cdea7c0
BS
1952}
1953
df878fb0
KH
1954static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
1955 struct cgroup *cont)
1956{
1957 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
8c7c6e34 1958 mem->obsolete = 1;
c1e862c1 1959 mem_cgroup_force_empty(mem, false);
df878fb0
KH
1960}
1961
8cdea7c0
BS
1962static void mem_cgroup_destroy(struct cgroup_subsys *ss,
1963 struct cgroup *cont)
1964{
33327948 1965 mem_cgroup_free(mem_cgroup_from_cont(cont));
8cdea7c0
BS
1966}
1967
1968static int mem_cgroup_populate(struct cgroup_subsys *ss,
1969 struct cgroup *cont)
1970{
8c7c6e34
KH
1971 int ret;
1972
1973 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
1974 ARRAY_SIZE(mem_cgroup_files));
1975
1976 if (!ret)
1977 ret = register_memsw_files(cont, ss);
1978 return ret;
8cdea7c0
BS
1979}
1980
67e465a7
BS
1981static void mem_cgroup_move_task(struct cgroup_subsys *ss,
1982 struct cgroup *cont,
1983 struct cgroup *old_cont,
1984 struct task_struct *p)
1985{
1986 struct mm_struct *mm;
1987 struct mem_cgroup *mem, *old_mem;
1988
1989 mm = get_task_mm(p);
1990 if (mm == NULL)
1991 return;
1992
1993 mem = mem_cgroup_from_cont(cont);
1994 old_mem = mem_cgroup_from_cont(old_cont);
1995
67e465a7
BS
1996 /*
1997 * Only thread group leaders are allowed to migrate, the mm_struct is
1998 * in effect owned by the leader
1999 */
52ea27eb 2000 if (!thread_group_leader(p))
67e465a7
BS
2001 goto out;
2002
67e465a7
BS
2003out:
2004 mmput(mm);
67e465a7
BS
2005}
2006
8cdea7c0
BS
2007struct cgroup_subsys mem_cgroup_subsys = {
2008 .name = "memory",
2009 .subsys_id = mem_cgroup_subsys_id,
2010 .create = mem_cgroup_create,
df878fb0 2011 .pre_destroy = mem_cgroup_pre_destroy,
8cdea7c0
BS
2012 .destroy = mem_cgroup_destroy,
2013 .populate = mem_cgroup_populate,
67e465a7 2014 .attach = mem_cgroup_move_task,
6d12e2d8 2015 .early_init = 0,
8cdea7c0 2016};
c077719b
KH
2017
2018#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2019
2020static int __init disable_swap_account(char *s)
2021{
2022 really_do_swap_account = 0;
2023 return 1;
2024}
2025__setup("noswapaccount", disable_swap_account);
2026#endif