]> bbs.cooldavid.org Git - net-next-2.6.git/blame - mm/memcontrol.c
eeprom/at25: bugfix "not ready" timeout after write
[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>
e222432b 30#include <linux/limits.h>
8c7c6e34 31#include <linux/mutex.h>
b6ac57d5 32#include <linux/slab.h>
66e1707b
BS
33#include <linux/swap.h>
34#include <linux/spinlock.h>
35#include <linux/fs.h>
d2ceb9b7 36#include <linux/seq_file.h>
33327948 37#include <linux/vmalloc.h>
b69408e8 38#include <linux/mm_inline.h>
52d4b9ac 39#include <linux/page_cgroup.h>
08e552c6 40#include "internal.h"
8cdea7c0 41
8697d331
BS
42#include <asm/uaccess.h>
43
a181b0e8 44struct cgroup_subsys mem_cgroup_subsys __read_mostly;
a181b0e8 45#define MEM_CGROUP_RECLAIM_RETRIES 5
8cdea7c0 46
c077719b 47#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
338c8431 48/* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
c077719b
KH
49int do_swap_account __read_mostly;
50static int really_do_swap_account __initdata = 1; /* for remember boot option*/
51#else
52#define do_swap_account (0)
53#endif
54
7f4d454d 55static DEFINE_MUTEX(memcg_tasklist); /* can be hold under cgroup_mutex */
c077719b 56
d52aa412
KH
57/*
58 * Statistics for memory cgroup.
59 */
60enum mem_cgroup_stat_index {
61 /*
62 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
63 */
64 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
d69b042f
BS
65 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
66 MEM_CGROUP_STAT_MAPPED_FILE, /* # of pages charged as file rss */
55e462b0
BR
67 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
68 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
d52aa412
KH
69
70 MEM_CGROUP_STAT_NSTATS,
71};
72
73struct mem_cgroup_stat_cpu {
74 s64 count[MEM_CGROUP_STAT_NSTATS];
75} ____cacheline_aligned_in_smp;
76
77struct mem_cgroup_stat {
c8dad2bb 78 struct mem_cgroup_stat_cpu cpustat[0];
d52aa412
KH
79};
80
81/*
82 * For accounting under irq disable, no need for increment preempt count.
83 */
addb9efe 84static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
d52aa412
KH
85 enum mem_cgroup_stat_index idx, int val)
86{
addb9efe 87 stat->count[idx] += val;
d52aa412
KH
88}
89
90static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
91 enum mem_cgroup_stat_index idx)
92{
93 int cpu;
94 s64 ret = 0;
95 for_each_possible_cpu(cpu)
96 ret += stat->cpustat[cpu].count[idx];
97 return ret;
98}
99
04046e1a
KH
100static s64 mem_cgroup_local_usage(struct mem_cgroup_stat *stat)
101{
102 s64 ret;
103
104 ret = mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_CACHE);
105 ret += mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_RSS);
106 return ret;
107}
108
6d12e2d8
KH
109/*
110 * per-zone information in memory controller.
111 */
6d12e2d8 112struct mem_cgroup_per_zone {
072c56c1
KH
113 /*
114 * spin_lock to protect the per cgroup LRU
115 */
b69408e8
CL
116 struct list_head lists[NR_LRU_LISTS];
117 unsigned long count[NR_LRU_LISTS];
3e2f41f1
KM
118
119 struct zone_reclaim_stat reclaim_stat;
6d12e2d8
KH
120};
121/* Macro for accessing counter */
122#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
123
124struct mem_cgroup_per_node {
125 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
126};
127
128struct mem_cgroup_lru_info {
129 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
130};
131
8cdea7c0
BS
132/*
133 * The memory controller data structure. The memory controller controls both
134 * page cache and RSS per cgroup. We would eventually like to provide
135 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
136 * to help the administrator determine what knobs to tune.
137 *
138 * TODO: Add a water mark for the memory controller. Reclaim will begin when
8a9f3ccd
BS
139 * we hit the water mark. May be even add a low water mark, such that
140 * no reclaim occurs from a cgroup at it's low water mark, this is
141 * a feature that will be implemented much later in the future.
8cdea7c0
BS
142 */
143struct mem_cgroup {
144 struct cgroup_subsys_state css;
145 /*
146 * the counter to account for memory usage
147 */
148 struct res_counter res;
8c7c6e34
KH
149 /*
150 * the counter to account for mem+swap usage.
151 */
152 struct res_counter memsw;
78fb7466
PE
153 /*
154 * Per cgroup active and inactive list, similar to the
155 * per zone LRU lists.
78fb7466 156 */
6d12e2d8 157 struct mem_cgroup_lru_info info;
072c56c1 158
2733c06a
KM
159 /*
160 protect against reclaim related member.
161 */
162 spinlock_t reclaim_param_lock;
163
6c48a1d0 164 int prev_priority; /* for recording reclaim priority */
6d61ef40
BS
165
166 /*
167 * While reclaiming in a hiearchy, we cache the last child we
04046e1a 168 * reclaimed from.
6d61ef40 169 */
04046e1a 170 int last_scanned_child;
18f59ea7
BS
171 /*
172 * Should the accounting and control be hierarchical, per subtree?
173 */
174 bool use_hierarchy;
a636b327 175 unsigned long last_oom_jiffies;
8c7c6e34 176 atomic_t refcnt;
14797e23 177
a7885eb8
KM
178 unsigned int swappiness;
179
22a668d7
KH
180 /* set when res.limit == memsw.limit */
181 bool memsw_is_minimum;
182
d52aa412 183 /*
c8dad2bb 184 * statistics. This must be placed at the end of memcg.
d52aa412
KH
185 */
186 struct mem_cgroup_stat stat;
8cdea7c0
BS
187};
188
217bc319
KH
189enum charge_type {
190 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
191 MEM_CGROUP_CHARGE_TYPE_MAPPED,
4f98a2fe 192 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
c05555b5 193 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
d13d1443 194 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
8a9478ca 195 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
c05555b5
KH
196 NR_CHARGE_TYPE,
197};
198
52d4b9ac
KH
199/* only for here (for easy reading.) */
200#define PCGF_CACHE (1UL << PCG_CACHE)
201#define PCGF_USED (1UL << PCG_USED)
52d4b9ac 202#define PCGF_LOCK (1UL << PCG_LOCK)
c05555b5
KH
203static const unsigned long
204pcg_default_flags[NR_CHARGE_TYPE] = {
08e552c6
KH
205 PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* File Cache */
206 PCGF_USED | PCGF_LOCK, /* Anon */
207 PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* Shmem */
52d4b9ac 208 0, /* FORCE */
217bc319
KH
209};
210
8c7c6e34
KH
211/* for encoding cft->private value on file */
212#define _MEM (0)
213#define _MEMSWAP (1)
214#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
215#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
216#define MEMFILE_ATTR(val) ((val) & 0xffff)
217
218static void mem_cgroup_get(struct mem_cgroup *mem);
219static void mem_cgroup_put(struct mem_cgroup *mem);
7bcc1bb1 220static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
8c7c6e34 221
c05555b5
KH
222static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
223 struct page_cgroup *pc,
224 bool charge)
d52aa412
KH
225{
226 int val = (charge)? 1 : -1;
227 struct mem_cgroup_stat *stat = &mem->stat;
addb9efe 228 struct mem_cgroup_stat_cpu *cpustat;
08e552c6 229 int cpu = get_cpu();
d52aa412 230
08e552c6 231 cpustat = &stat->cpustat[cpu];
c05555b5 232 if (PageCgroupCache(pc))
addb9efe 233 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
d52aa412 234 else
addb9efe 235 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
55e462b0
BR
236
237 if (charge)
addb9efe 238 __mem_cgroup_stat_add_safe(cpustat,
55e462b0
BR
239 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
240 else
addb9efe 241 __mem_cgroup_stat_add_safe(cpustat,
55e462b0 242 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
08e552c6 243 put_cpu();
6d12e2d8
KH
244}
245
d5b69e38 246static struct mem_cgroup_per_zone *
6d12e2d8
KH
247mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
248{
6d12e2d8
KH
249 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
250}
251
d5b69e38 252static struct mem_cgroup_per_zone *
6d12e2d8
KH
253page_cgroup_zoneinfo(struct page_cgroup *pc)
254{
255 struct mem_cgroup *mem = pc->mem_cgroup;
256 int nid = page_cgroup_nid(pc);
257 int zid = page_cgroup_zid(pc);
d52aa412 258
54992762
KM
259 if (!mem)
260 return NULL;
261
6d12e2d8
KH
262 return mem_cgroup_zoneinfo(mem, nid, zid);
263}
264
14067bb3 265static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
b69408e8 266 enum lru_list idx)
6d12e2d8
KH
267{
268 int nid, zid;
269 struct mem_cgroup_per_zone *mz;
270 u64 total = 0;
271
272 for_each_online_node(nid)
273 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
274 mz = mem_cgroup_zoneinfo(mem, nid, zid);
275 total += MEM_CGROUP_ZSTAT(mz, idx);
276 }
277 return total;
d52aa412
KH
278}
279
d5b69e38 280static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
8cdea7c0
BS
281{
282 return container_of(cgroup_subsys_state(cont,
283 mem_cgroup_subsys_id), struct mem_cgroup,
284 css);
285}
286
cf475ad2 287struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
78fb7466 288{
31a78f23
BS
289 /*
290 * mm_update_next_owner() may clear mm->owner to NULL
291 * if it races with swapoff, page migration, etc.
292 * So this can be called with p == NULL.
293 */
294 if (unlikely(!p))
295 return NULL;
296
78fb7466
PE
297 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
298 struct mem_cgroup, css);
299}
300
54595fe2
KH
301static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
302{
303 struct mem_cgroup *mem = NULL;
0b7f569e
KH
304
305 if (!mm)
306 return NULL;
54595fe2
KH
307 /*
308 * Because we have no locks, mm->owner's may be being moved to other
309 * cgroup. We use css_tryget() here even if this looks
310 * pessimistic (rather than adding locks here).
311 */
312 rcu_read_lock();
313 do {
314 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
315 if (unlikely(!mem))
316 break;
317 } while (!css_tryget(&mem->css));
318 rcu_read_unlock();
319 return mem;
320}
321
14067bb3
KH
322/*
323 * Call callback function against all cgroup under hierarchy tree.
324 */
325static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
326 int (*func)(struct mem_cgroup *, void *))
327{
328 int found, ret, nextid;
329 struct cgroup_subsys_state *css;
330 struct mem_cgroup *mem;
331
332 if (!root->use_hierarchy)
333 return (*func)(root, data);
334
335 nextid = 1;
336 do {
337 ret = 0;
338 mem = NULL;
339
340 rcu_read_lock();
341 css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
342 &found);
343 if (css && css_tryget(css))
344 mem = container_of(css, struct mem_cgroup, css);
345 rcu_read_unlock();
346
347 if (mem) {
348 ret = (*func)(mem, data);
349 css_put(&mem->css);
350 }
351 nextid = found + 1;
352 } while (!ret && css);
353
354 return ret;
355}
356
08e552c6
KH
357/*
358 * Following LRU functions are allowed to be used without PCG_LOCK.
359 * Operations are called by routine of global LRU independently from memcg.
360 * What we have to take care of here is validness of pc->mem_cgroup.
361 *
362 * Changes to pc->mem_cgroup happens when
363 * 1. charge
364 * 2. moving account
365 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
366 * It is added to LRU before charge.
367 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
368 * When moving account, the page is not on LRU. It's isolated.
369 */
4f98a2fe 370
08e552c6
KH
371void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
372{
373 struct page_cgroup *pc;
374 struct mem_cgroup *mem;
375 struct mem_cgroup_per_zone *mz;
6d12e2d8 376
f8d66542 377 if (mem_cgroup_disabled())
08e552c6
KH
378 return;
379 pc = lookup_page_cgroup(page);
380 /* can happen while we handle swapcache. */
544122e5 381 if (list_empty(&pc->lru) || !pc->mem_cgroup)
08e552c6 382 return;
544122e5
KH
383 /*
384 * We don't check PCG_USED bit. It's cleared when the "page" is finally
385 * removed from global LRU.
386 */
08e552c6
KH
387 mz = page_cgroup_zoneinfo(pc);
388 mem = pc->mem_cgroup;
b69408e8 389 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
08e552c6
KH
390 list_del_init(&pc->lru);
391 return;
6d12e2d8
KH
392}
393
08e552c6 394void mem_cgroup_del_lru(struct page *page)
6d12e2d8 395{
08e552c6
KH
396 mem_cgroup_del_lru_list(page, page_lru(page));
397}
b69408e8 398
08e552c6
KH
399void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
400{
401 struct mem_cgroup_per_zone *mz;
402 struct page_cgroup *pc;
b69408e8 403
f8d66542 404 if (mem_cgroup_disabled())
08e552c6 405 return;
6d12e2d8 406
08e552c6 407 pc = lookup_page_cgroup(page);
bd112db8
DN
408 /*
409 * Used bit is set without atomic ops but after smp_wmb().
410 * For making pc->mem_cgroup visible, insert smp_rmb() here.
411 */
08e552c6
KH
412 smp_rmb();
413 /* unused page is not rotated. */
414 if (!PageCgroupUsed(pc))
415 return;
416 mz = page_cgroup_zoneinfo(pc);
417 list_move(&pc->lru, &mz->lists[lru]);
6d12e2d8
KH
418}
419
08e552c6 420void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
66e1707b 421{
08e552c6
KH
422 struct page_cgroup *pc;
423 struct mem_cgroup_per_zone *mz;
6d12e2d8 424
f8d66542 425 if (mem_cgroup_disabled())
08e552c6
KH
426 return;
427 pc = lookup_page_cgroup(page);
bd112db8
DN
428 /*
429 * Used bit is set without atomic ops but after smp_wmb().
430 * For making pc->mem_cgroup visible, insert smp_rmb() here.
431 */
08e552c6
KH
432 smp_rmb();
433 if (!PageCgroupUsed(pc))
894bc310 434 return;
b69408e8 435
08e552c6 436 mz = page_cgroup_zoneinfo(pc);
b69408e8 437 MEM_CGROUP_ZSTAT(mz, lru) += 1;
08e552c6
KH
438 list_add(&pc->lru, &mz->lists[lru]);
439}
544122e5 440
08e552c6 441/*
544122e5
KH
442 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
443 * lru because the page may.be reused after it's fully uncharged (because of
444 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
445 * it again. This function is only used to charge SwapCache. It's done under
446 * lock_page and expected that zone->lru_lock is never held.
08e552c6 447 */
544122e5 448static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
08e552c6 449{
544122e5
KH
450 unsigned long flags;
451 struct zone *zone = page_zone(page);
452 struct page_cgroup *pc = lookup_page_cgroup(page);
453
454 spin_lock_irqsave(&zone->lru_lock, flags);
455 /*
456 * Forget old LRU when this page_cgroup is *not* used. This Used bit
457 * is guarded by lock_page() because the page is SwapCache.
458 */
459 if (!PageCgroupUsed(pc))
460 mem_cgroup_del_lru_list(page, page_lru(page));
461 spin_unlock_irqrestore(&zone->lru_lock, flags);
08e552c6
KH
462}
463
544122e5
KH
464static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
465{
466 unsigned long flags;
467 struct zone *zone = page_zone(page);
468 struct page_cgroup *pc = lookup_page_cgroup(page);
469
470 spin_lock_irqsave(&zone->lru_lock, flags);
471 /* link when the page is linked to LRU but page_cgroup isn't */
472 if (PageLRU(page) && list_empty(&pc->lru))
473 mem_cgroup_add_lru_list(page, page_lru(page));
474 spin_unlock_irqrestore(&zone->lru_lock, flags);
475}
476
477
08e552c6
KH
478void mem_cgroup_move_lists(struct page *page,
479 enum lru_list from, enum lru_list to)
480{
f8d66542 481 if (mem_cgroup_disabled())
08e552c6
KH
482 return;
483 mem_cgroup_del_lru_list(page, from);
484 mem_cgroup_add_lru_list(page, to);
66e1707b
BS
485}
486
4c4a2214
DR
487int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
488{
489 int ret;
0b7f569e 490 struct mem_cgroup *curr = NULL;
4c4a2214
DR
491
492 task_lock(task);
0b7f569e
KH
493 rcu_read_lock();
494 curr = try_get_mem_cgroup_from_mm(task->mm);
495 rcu_read_unlock();
4c4a2214 496 task_unlock(task);
0b7f569e
KH
497 if (!curr)
498 return 0;
499 if (curr->use_hierarchy)
500 ret = css_is_ancestor(&curr->css, &mem->css);
501 else
502 ret = (curr == mem);
503 css_put(&curr->css);
4c4a2214
DR
504 return ret;
505}
506
6c48a1d0
KH
507/*
508 * prev_priority control...this will be used in memory reclaim path.
509 */
510int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
511{
2733c06a
KM
512 int prev_priority;
513
514 spin_lock(&mem->reclaim_param_lock);
515 prev_priority = mem->prev_priority;
516 spin_unlock(&mem->reclaim_param_lock);
517
518 return prev_priority;
6c48a1d0
KH
519}
520
521void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
522{
2733c06a 523 spin_lock(&mem->reclaim_param_lock);
6c48a1d0
KH
524 if (priority < mem->prev_priority)
525 mem->prev_priority = priority;
2733c06a 526 spin_unlock(&mem->reclaim_param_lock);
6c48a1d0
KH
527}
528
529void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
530{
2733c06a 531 spin_lock(&mem->reclaim_param_lock);
6c48a1d0 532 mem->prev_priority = priority;
2733c06a 533 spin_unlock(&mem->reclaim_param_lock);
6c48a1d0
KH
534}
535
c772be93 536static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
14797e23
KM
537{
538 unsigned long active;
539 unsigned long inactive;
c772be93
KM
540 unsigned long gb;
541 unsigned long inactive_ratio;
14797e23 542
14067bb3
KH
543 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
544 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
14797e23 545
c772be93
KM
546 gb = (inactive + active) >> (30 - PAGE_SHIFT);
547 if (gb)
548 inactive_ratio = int_sqrt(10 * gb);
549 else
550 inactive_ratio = 1;
551
552 if (present_pages) {
553 present_pages[0] = inactive;
554 present_pages[1] = active;
555 }
556
557 return inactive_ratio;
558}
559
560int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
561{
562 unsigned long active;
563 unsigned long inactive;
564 unsigned long present_pages[2];
565 unsigned long inactive_ratio;
566
567 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
568
569 inactive = present_pages[0];
570 active = present_pages[1];
571
572 if (inactive * inactive_ratio < active)
14797e23
KM
573 return 1;
574
575 return 0;
576}
577
56e49d21
RR
578int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
579{
580 unsigned long active;
581 unsigned long inactive;
582
583 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
584 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
585
586 return (active > inactive);
587}
588
a3d8e054
KM
589unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
590 struct zone *zone,
591 enum lru_list lru)
592{
593 int nid = zone->zone_pgdat->node_id;
594 int zid = zone_idx(zone);
595 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
596
597 return MEM_CGROUP_ZSTAT(mz, lru);
598}
599
3e2f41f1
KM
600struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
601 struct zone *zone)
602{
603 int nid = zone->zone_pgdat->node_id;
604 int zid = zone_idx(zone);
605 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
606
607 return &mz->reclaim_stat;
608}
609
610struct zone_reclaim_stat *
611mem_cgroup_get_reclaim_stat_from_page(struct page *page)
612{
613 struct page_cgroup *pc;
614 struct mem_cgroup_per_zone *mz;
615
616 if (mem_cgroup_disabled())
617 return NULL;
618
619 pc = lookup_page_cgroup(page);
bd112db8
DN
620 /*
621 * Used bit is set without atomic ops but after smp_wmb().
622 * For making pc->mem_cgroup visible, insert smp_rmb() here.
623 */
624 smp_rmb();
625 if (!PageCgroupUsed(pc))
626 return NULL;
627
3e2f41f1
KM
628 mz = page_cgroup_zoneinfo(pc);
629 if (!mz)
630 return NULL;
631
632 return &mz->reclaim_stat;
633}
634
66e1707b
BS
635unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
636 struct list_head *dst,
637 unsigned long *scanned, int order,
638 int mode, struct zone *z,
639 struct mem_cgroup *mem_cont,
4f98a2fe 640 int active, int file)
66e1707b
BS
641{
642 unsigned long nr_taken = 0;
643 struct page *page;
644 unsigned long scan;
645 LIST_HEAD(pc_list);
646 struct list_head *src;
ff7283fa 647 struct page_cgroup *pc, *tmp;
1ecaab2b
KH
648 int nid = z->zone_pgdat->node_id;
649 int zid = zone_idx(z);
650 struct mem_cgroup_per_zone *mz;
4f98a2fe 651 int lru = LRU_FILE * !!file + !!active;
2ffebca6 652 int ret;
66e1707b 653
cf475ad2 654 BUG_ON(!mem_cont);
1ecaab2b 655 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
b69408e8 656 src = &mz->lists[lru];
66e1707b 657
ff7283fa
KH
658 scan = 0;
659 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
436c6541 660 if (scan >= nr_to_scan)
ff7283fa 661 break;
08e552c6
KH
662
663 page = pc->page;
52d4b9ac
KH
664 if (unlikely(!PageCgroupUsed(pc)))
665 continue;
436c6541 666 if (unlikely(!PageLRU(page)))
ff7283fa 667 continue;
ff7283fa 668
436c6541 669 scan++;
2ffebca6
KH
670 ret = __isolate_lru_page(page, mode, file);
671 switch (ret) {
672 case 0:
66e1707b 673 list_move(&page->lru, dst);
2ffebca6 674 mem_cgroup_del_lru(page);
66e1707b 675 nr_taken++;
2ffebca6
KH
676 break;
677 case -EBUSY:
678 /* we don't affect global LRU but rotate in our LRU */
679 mem_cgroup_rotate_lru_list(page, page_lru(page));
680 break;
681 default:
682 break;
66e1707b
BS
683 }
684 }
685
66e1707b
BS
686 *scanned = scan;
687 return nr_taken;
688}
689
6d61ef40
BS
690#define mem_cgroup_from_res_counter(counter, member) \
691 container_of(counter, struct mem_cgroup, member)
692
b85a96c0
DN
693static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
694{
695 if (do_swap_account) {
696 if (res_counter_check_under_limit(&mem->res) &&
697 res_counter_check_under_limit(&mem->memsw))
698 return true;
699 } else
700 if (res_counter_check_under_limit(&mem->res))
701 return true;
702 return false;
703}
704
a7885eb8
KM
705static unsigned int get_swappiness(struct mem_cgroup *memcg)
706{
707 struct cgroup *cgrp = memcg->css.cgroup;
708 unsigned int swappiness;
709
710 /* root ? */
711 if (cgrp->parent == NULL)
712 return vm_swappiness;
713
714 spin_lock(&memcg->reclaim_param_lock);
715 swappiness = memcg->swappiness;
716 spin_unlock(&memcg->reclaim_param_lock);
717
718 return swappiness;
719}
720
81d39c20
KH
721static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
722{
723 int *val = data;
724 (*val)++;
725 return 0;
726}
e222432b
BS
727
728/**
729 * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode.
730 * @memcg: The memory cgroup that went over limit
731 * @p: Task that is going to be killed
732 *
733 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
734 * enabled
735 */
736void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
737{
738 struct cgroup *task_cgrp;
739 struct cgroup *mem_cgrp;
740 /*
741 * Need a buffer in BSS, can't rely on allocations. The code relies
742 * on the assumption that OOM is serialized for memory controller.
743 * If this assumption is broken, revisit this code.
744 */
745 static char memcg_name[PATH_MAX];
746 int ret;
747
748 if (!memcg)
749 return;
750
751
752 rcu_read_lock();
753
754 mem_cgrp = memcg->css.cgroup;
755 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
756
757 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
758 if (ret < 0) {
759 /*
760 * Unfortunately, we are unable to convert to a useful name
761 * But we'll still print out the usage information
762 */
763 rcu_read_unlock();
764 goto done;
765 }
766 rcu_read_unlock();
767
768 printk(KERN_INFO "Task in %s killed", memcg_name);
769
770 rcu_read_lock();
771 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
772 if (ret < 0) {
773 rcu_read_unlock();
774 goto done;
775 }
776 rcu_read_unlock();
777
778 /*
779 * Continues from above, so we don't need an KERN_ level
780 */
781 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
782done:
783
784 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
785 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
786 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
787 res_counter_read_u64(&memcg->res, RES_FAILCNT));
788 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
789 "failcnt %llu\n",
790 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
791 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
792 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
793}
794
81d39c20
KH
795/*
796 * This function returns the number of memcg under hierarchy tree. Returns
797 * 1(self count) if no children.
798 */
799static int mem_cgroup_count_children(struct mem_cgroup *mem)
800{
801 int num = 0;
802 mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
803 return num;
804}
805
6d61ef40 806/*
04046e1a
KH
807 * Visit the first child (need not be the first child as per the ordering
808 * of the cgroup list, since we track last_scanned_child) of @mem and use
809 * that to reclaim free pages from.
810 */
811static struct mem_cgroup *
812mem_cgroup_select_victim(struct mem_cgroup *root_mem)
813{
814 struct mem_cgroup *ret = NULL;
815 struct cgroup_subsys_state *css;
816 int nextid, found;
817
818 if (!root_mem->use_hierarchy) {
819 css_get(&root_mem->css);
820 ret = root_mem;
821 }
822
823 while (!ret) {
824 rcu_read_lock();
825 nextid = root_mem->last_scanned_child + 1;
826 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
827 &found);
828 if (css && css_tryget(css))
829 ret = container_of(css, struct mem_cgroup, css);
830
831 rcu_read_unlock();
832 /* Updates scanning parameter */
833 spin_lock(&root_mem->reclaim_param_lock);
834 if (!css) {
835 /* this means start scan from ID:1 */
836 root_mem->last_scanned_child = 0;
837 } else
838 root_mem->last_scanned_child = found;
839 spin_unlock(&root_mem->reclaim_param_lock);
840 }
841
842 return ret;
843}
844
845/*
846 * Scan the hierarchy if needed to reclaim memory. We remember the last child
847 * we reclaimed from, so that we don't end up penalizing one child extensively
848 * based on its position in the children list.
6d61ef40
BS
849 *
850 * root_mem is the original ancestor that we've been reclaim from.
04046e1a
KH
851 *
852 * We give up and return to the caller when we visit root_mem twice.
853 * (other groups can be removed while we're walking....)
81d39c20
KH
854 *
855 * If shrink==true, for avoiding to free too much, this returns immedieately.
6d61ef40
BS
856 */
857static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
81d39c20 858 gfp_t gfp_mask, bool noswap, bool shrink)
6d61ef40 859{
04046e1a
KH
860 struct mem_cgroup *victim;
861 int ret, total = 0;
862 int loop = 0;
863
22a668d7
KH
864 /* If memsw_is_minimum==1, swap-out is of-no-use. */
865 if (root_mem->memsw_is_minimum)
866 noswap = true;
867
04046e1a
KH
868 while (loop < 2) {
869 victim = mem_cgroup_select_victim(root_mem);
870 if (victim == root_mem)
871 loop++;
872 if (!mem_cgroup_local_usage(&victim->stat)) {
873 /* this cgroup's local usage == 0 */
874 css_put(&victim->css);
6d61ef40
BS
875 continue;
876 }
04046e1a
KH
877 /* we use swappiness of local cgroup */
878 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask, noswap,
879 get_swappiness(victim));
880 css_put(&victim->css);
81d39c20
KH
881 /*
882 * At shrinking usage, we can't check we should stop here or
883 * reclaim more. It's depends on callers. last_scanned_child
884 * will work enough for keeping fairness under tree.
885 */
886 if (shrink)
887 return ret;
04046e1a 888 total += ret;
b85a96c0 889 if (mem_cgroup_check_under_limit(root_mem))
04046e1a 890 return 1 + total;
6d61ef40 891 }
04046e1a 892 return total;
6d61ef40
BS
893}
894
a636b327
KH
895bool mem_cgroup_oom_called(struct task_struct *task)
896{
897 bool ret = false;
898 struct mem_cgroup *mem;
899 struct mm_struct *mm;
900
901 rcu_read_lock();
902 mm = task->mm;
903 if (!mm)
904 mm = &init_mm;
905 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
906 if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
907 ret = true;
908 rcu_read_unlock();
909 return ret;
910}
0b7f569e
KH
911
912static int record_last_oom_cb(struct mem_cgroup *mem, void *data)
913{
914 mem->last_oom_jiffies = jiffies;
915 return 0;
916}
917
918static void record_last_oom(struct mem_cgroup *mem)
919{
920 mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb);
921}
922
d69b042f
BS
923/*
924 * Currently used to update mapped file statistics, but the routine can be
925 * generalized to update other statistics as well.
926 */
927void mem_cgroup_update_mapped_file_stat(struct page *page, int val)
928{
929 struct mem_cgroup *mem;
930 struct mem_cgroup_stat *stat;
931 struct mem_cgroup_stat_cpu *cpustat;
932 int cpu;
933 struct page_cgroup *pc;
934
935 if (!page_is_file_cache(page))
936 return;
937
938 pc = lookup_page_cgroup(page);
939 if (unlikely(!pc))
940 return;
941
942 lock_page_cgroup(pc);
943 mem = pc->mem_cgroup;
944 if (!mem)
945 goto done;
946
947 if (!PageCgroupUsed(pc))
948 goto done;
949
950 /*
951 * Preemption is already disabled, we don't need get_cpu()
952 */
953 cpu = smp_processor_id();
954 stat = &mem->stat;
955 cpustat = &stat->cpustat[cpu];
956
957 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE, val);
958done:
959 unlock_page_cgroup(pc);
960}
0b7f569e 961
f817ed48
KH
962/*
963 * Unlike exported interface, "oom" parameter is added. if oom==true,
964 * oom-killer can be invoked.
8a9f3ccd 965 */
f817ed48 966static int __mem_cgroup_try_charge(struct mm_struct *mm,
8c7c6e34
KH
967 gfp_t gfp_mask, struct mem_cgroup **memcg,
968 bool oom)
8a9f3ccd 969{
6d61ef40 970 struct mem_cgroup *mem, *mem_over_limit;
7a81b88c 971 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
28dbc4b6 972 struct res_counter *fail_res;
a636b327
KH
973
974 if (unlikely(test_thread_flag(TIF_MEMDIE))) {
975 /* Don't account this! */
976 *memcg = NULL;
977 return 0;
978 }
979
8a9f3ccd 980 /*
3be91277
HD
981 * We always charge the cgroup the mm_struct belongs to.
982 * The mm_struct's mem_cgroup changes on task migration if the
8a9f3ccd
BS
983 * thread group leader migrates. It's possible that mm is not
984 * set, if so charge the init_mm (happens for pagecache usage).
985 */
54595fe2
KH
986 mem = *memcg;
987 if (likely(!mem)) {
988 mem = try_get_mem_cgroup_from_mm(mm);
7a81b88c 989 *memcg = mem;
e8589cc1 990 } else {
7a81b88c 991 css_get(&mem->css);
e8589cc1 992 }
54595fe2
KH
993 if (unlikely(!mem))
994 return 0;
995
46f7e602 996 VM_BUG_ON(css_is_removed(&mem->css));
8a9f3ccd 997
8c7c6e34
KH
998 while (1) {
999 int ret;
1000 bool noswap = false;
7a81b88c 1001
28dbc4b6 1002 ret = res_counter_charge(&mem->res, PAGE_SIZE, &fail_res);
8c7c6e34
KH
1003 if (likely(!ret)) {
1004 if (!do_swap_account)
1005 break;
28dbc4b6
BS
1006 ret = res_counter_charge(&mem->memsw, PAGE_SIZE,
1007 &fail_res);
8c7c6e34
KH
1008 if (likely(!ret))
1009 break;
1010 /* mem+swap counter fails */
1011 res_counter_uncharge(&mem->res, PAGE_SIZE);
1012 noswap = true;
6d61ef40
BS
1013 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1014 memsw);
1015 } else
1016 /* mem counter fails */
1017 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1018 res);
1019
3be91277 1020 if (!(gfp_mask & __GFP_WAIT))
7a81b88c 1021 goto nomem;
e1a1cd59 1022
6d61ef40 1023 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, gfp_mask,
81d39c20 1024 noswap, false);
4d1c6273
DN
1025 if (ret)
1026 continue;
66e1707b
BS
1027
1028 /*
8869b8f6
HD
1029 * try_to_free_mem_cgroup_pages() might not give us a full
1030 * picture of reclaim. Some pages are reclaimed and might be
1031 * moved to swap cache or just unmapped from the cgroup.
1032 * Check the limit again to see if the reclaim reduced the
1033 * current usage of the cgroup before giving up
8c7c6e34 1034 *
8869b8f6 1035 */
b85a96c0
DN
1036 if (mem_cgroup_check_under_limit(mem_over_limit))
1037 continue;
3be91277
HD
1038
1039 if (!nr_retries--) {
a636b327 1040 if (oom) {
7f4d454d 1041 mutex_lock(&memcg_tasklist);
88700756 1042 mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
7f4d454d 1043 mutex_unlock(&memcg_tasklist);
0b7f569e 1044 record_last_oom(mem_over_limit);
a636b327 1045 }
7a81b88c 1046 goto nomem;
66e1707b 1047 }
8a9f3ccd 1048 }
7a81b88c
KH
1049 return 0;
1050nomem:
1051 css_put(&mem->css);
1052 return -ENOMEM;
1053}
8a9f3ccd 1054
a3b2d692
KH
1055
1056/*
1057 * A helper function to get mem_cgroup from ID. must be called under
1058 * rcu_read_lock(). The caller must check css_is_removed() or some if
1059 * it's concern. (dropping refcnt from swap can be called against removed
1060 * memcg.)
1061 */
1062static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
1063{
1064 struct cgroup_subsys_state *css;
1065
1066 /* ID 0 is unused ID */
1067 if (!id)
1068 return NULL;
1069 css = css_lookup(&mem_cgroup_subsys, id);
1070 if (!css)
1071 return NULL;
1072 return container_of(css, struct mem_cgroup, css);
1073}
1074
b5a84319
KH
1075static struct mem_cgroup *try_get_mem_cgroup_from_swapcache(struct page *page)
1076{
1077 struct mem_cgroup *mem;
3c776e64 1078 struct page_cgroup *pc;
a3b2d692 1079 unsigned short id;
b5a84319
KH
1080 swp_entry_t ent;
1081
3c776e64
DN
1082 VM_BUG_ON(!PageLocked(page));
1083
b5a84319
KH
1084 if (!PageSwapCache(page))
1085 return NULL;
1086
3c776e64 1087 pc = lookup_page_cgroup(page);
c0bd3f63 1088 lock_page_cgroup(pc);
a3b2d692 1089 if (PageCgroupUsed(pc)) {
3c776e64 1090 mem = pc->mem_cgroup;
a3b2d692
KH
1091 if (mem && !css_tryget(&mem->css))
1092 mem = NULL;
1093 } else {
3c776e64 1094 ent.val = page_private(page);
a3b2d692
KH
1095 id = lookup_swap_cgroup(ent);
1096 rcu_read_lock();
1097 mem = mem_cgroup_lookup(id);
1098 if (mem && !css_tryget(&mem->css))
1099 mem = NULL;
1100 rcu_read_unlock();
3c776e64 1101 }
c0bd3f63 1102 unlock_page_cgroup(pc);
b5a84319
KH
1103 return mem;
1104}
1105
7a81b88c 1106/*
a5e924f5 1107 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
7a81b88c
KH
1108 * USED state. If already USED, uncharge and return.
1109 */
1110
1111static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1112 struct page_cgroup *pc,
1113 enum charge_type ctype)
1114{
7a81b88c
KH
1115 /* try_charge() can return NULL to *memcg, taking care of it. */
1116 if (!mem)
1117 return;
52d4b9ac
KH
1118
1119 lock_page_cgroup(pc);
1120 if (unlikely(PageCgroupUsed(pc))) {
1121 unlock_page_cgroup(pc);
1122 res_counter_uncharge(&mem->res, PAGE_SIZE);
8c7c6e34
KH
1123 if (do_swap_account)
1124 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
52d4b9ac 1125 css_put(&mem->css);
7a81b88c 1126 return;
52d4b9ac 1127 }
8a9f3ccd 1128 pc->mem_cgroup = mem;
08e552c6 1129 smp_wmb();
c05555b5 1130 pc->flags = pcg_default_flags[ctype];
3be91277 1131
08e552c6 1132 mem_cgroup_charge_statistics(mem, pc, true);
52d4b9ac 1133
52d4b9ac 1134 unlock_page_cgroup(pc);
7a81b88c 1135}
66e1707b 1136
f817ed48
KH
1137/**
1138 * mem_cgroup_move_account - move account of the page
1139 * @pc: page_cgroup of the page.
1140 * @from: mem_cgroup which the page is moved from.
1141 * @to: mem_cgroup which the page is moved to. @from != @to.
1142 *
1143 * The caller must confirm following.
08e552c6 1144 * - page is not on LRU (isolate_page() is useful.)
f817ed48
KH
1145 *
1146 * returns 0 at success,
1147 * returns -EBUSY when lock is busy or "pc" is unstable.
1148 *
1149 * This function does "uncharge" from old cgroup but doesn't do "charge" to
1150 * new cgroup. It should be done by a caller.
1151 */
1152
1153static int mem_cgroup_move_account(struct page_cgroup *pc,
1154 struct mem_cgroup *from, struct mem_cgroup *to)
1155{
1156 struct mem_cgroup_per_zone *from_mz, *to_mz;
1157 int nid, zid;
1158 int ret = -EBUSY;
d69b042f
BS
1159 struct page *page;
1160 int cpu;
1161 struct mem_cgroup_stat *stat;
1162 struct mem_cgroup_stat_cpu *cpustat;
f817ed48 1163
f817ed48 1164 VM_BUG_ON(from == to);
08e552c6 1165 VM_BUG_ON(PageLRU(pc->page));
f817ed48
KH
1166
1167 nid = page_cgroup_nid(pc);
1168 zid = page_cgroup_zid(pc);
1169 from_mz = mem_cgroup_zoneinfo(from, nid, zid);
1170 to_mz = mem_cgroup_zoneinfo(to, nid, zid);
1171
f817ed48
KH
1172 if (!trylock_page_cgroup(pc))
1173 return ret;
1174
1175 if (!PageCgroupUsed(pc))
1176 goto out;
1177
1178 if (pc->mem_cgroup != from)
1179 goto out;
1180
08e552c6
KH
1181 res_counter_uncharge(&from->res, PAGE_SIZE);
1182 mem_cgroup_charge_statistics(from, pc, false);
d69b042f
BS
1183
1184 page = pc->page;
1185 if (page_is_file_cache(page) && page_mapped(page)) {
1186 cpu = smp_processor_id();
1187 /* Update mapped_file data for mem_cgroup "from" */
1188 stat = &from->stat;
1189 cpustat = &stat->cpustat[cpu];
1190 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1191 -1);
1192
1193 /* Update mapped_file data for mem_cgroup "to" */
1194 stat = &to->stat;
1195 cpustat = &stat->cpustat[cpu];
1196 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1197 1);
1198 }
1199
08e552c6
KH
1200 if (do_swap_account)
1201 res_counter_uncharge(&from->memsw, PAGE_SIZE);
40d58138
DN
1202 css_put(&from->css);
1203
1204 css_get(&to->css);
08e552c6
KH
1205 pc->mem_cgroup = to;
1206 mem_cgroup_charge_statistics(to, pc, true);
08e552c6 1207 ret = 0;
f817ed48
KH
1208out:
1209 unlock_page_cgroup(pc);
1210 return ret;
1211}
1212
1213/*
1214 * move charges to its parent.
1215 */
1216
1217static int mem_cgroup_move_parent(struct page_cgroup *pc,
1218 struct mem_cgroup *child,
1219 gfp_t gfp_mask)
1220{
08e552c6 1221 struct page *page = pc->page;
f817ed48
KH
1222 struct cgroup *cg = child->css.cgroup;
1223 struct cgroup *pcg = cg->parent;
1224 struct mem_cgroup *parent;
f817ed48
KH
1225 int ret;
1226
1227 /* Is ROOT ? */
1228 if (!pcg)
1229 return -EINVAL;
1230
08e552c6 1231
f817ed48
KH
1232 parent = mem_cgroup_from_cont(pcg);
1233
08e552c6 1234
f817ed48 1235 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
a636b327 1236 if (ret || !parent)
f817ed48
KH
1237 return ret;
1238
40d58138
DN
1239 if (!get_page_unless_zero(page)) {
1240 ret = -EBUSY;
1241 goto uncharge;
1242 }
08e552c6
KH
1243
1244 ret = isolate_lru_page(page);
1245
1246 if (ret)
1247 goto cancel;
f817ed48 1248
f817ed48 1249 ret = mem_cgroup_move_account(pc, child, parent);
f817ed48 1250
08e552c6
KH
1251 putback_lru_page(page);
1252 if (!ret) {
1253 put_page(page);
40d58138
DN
1254 /* drop extra refcnt by try_charge() */
1255 css_put(&parent->css);
08e552c6 1256 return 0;
8c7c6e34 1257 }
40d58138 1258
08e552c6 1259cancel:
40d58138
DN
1260 put_page(page);
1261uncharge:
1262 /* drop extra refcnt by try_charge() */
1263 css_put(&parent->css);
1264 /* uncharge if move fails */
08e552c6
KH
1265 res_counter_uncharge(&parent->res, PAGE_SIZE);
1266 if (do_swap_account)
1267 res_counter_uncharge(&parent->memsw, PAGE_SIZE);
f817ed48
KH
1268 return ret;
1269}
1270
7a81b88c
KH
1271/*
1272 * Charge the memory controller for page usage.
1273 * Return
1274 * 0 if the charge was successful
1275 * < 0 if the cgroup is over its limit
1276 */
1277static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
1278 gfp_t gfp_mask, enum charge_type ctype,
1279 struct mem_cgroup *memcg)
1280{
1281 struct mem_cgroup *mem;
1282 struct page_cgroup *pc;
1283 int ret;
1284
1285 pc = lookup_page_cgroup(page);
1286 /* can happen at boot */
1287 if (unlikely(!pc))
1288 return 0;
1289 prefetchw(pc);
1290
1291 mem = memcg;
f817ed48 1292 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
a636b327 1293 if (ret || !mem)
7a81b88c
KH
1294 return ret;
1295
1296 __mem_cgroup_commit_charge(mem, pc, ctype);
8a9f3ccd 1297 return 0;
8a9f3ccd
BS
1298}
1299
7a81b88c
KH
1300int mem_cgroup_newpage_charge(struct page *page,
1301 struct mm_struct *mm, gfp_t gfp_mask)
217bc319 1302{
f8d66542 1303 if (mem_cgroup_disabled())
cede86ac 1304 return 0;
52d4b9ac
KH
1305 if (PageCompound(page))
1306 return 0;
69029cd5
KH
1307 /*
1308 * If already mapped, we don't have to account.
1309 * If page cache, page->mapping has address_space.
1310 * But page->mapping may have out-of-use anon_vma pointer,
1311 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
1312 * is NULL.
1313 */
1314 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
1315 return 0;
1316 if (unlikely(!mm))
1317 mm = &init_mm;
217bc319 1318 return mem_cgroup_charge_common(page, mm, gfp_mask,
e8589cc1 1319 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
217bc319
KH
1320}
1321
83aae4c7
DN
1322static void
1323__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1324 enum charge_type ctype);
1325
e1a1cd59
BS
1326int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
1327 gfp_t gfp_mask)
8697d331 1328{
b5a84319
KH
1329 struct mem_cgroup *mem = NULL;
1330 int ret;
1331
f8d66542 1332 if (mem_cgroup_disabled())
cede86ac 1333 return 0;
52d4b9ac
KH
1334 if (PageCompound(page))
1335 return 0;
accf163e
KH
1336 /*
1337 * Corner case handling. This is called from add_to_page_cache()
1338 * in usual. But some FS (shmem) precharges this page before calling it
1339 * and call add_to_page_cache() with GFP_NOWAIT.
1340 *
1341 * For GFP_NOWAIT case, the page may be pre-charged before calling
1342 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
1343 * charge twice. (It works but has to pay a bit larger cost.)
b5a84319
KH
1344 * And when the page is SwapCache, it should take swap information
1345 * into account. This is under lock_page() now.
accf163e
KH
1346 */
1347 if (!(gfp_mask & __GFP_WAIT)) {
1348 struct page_cgroup *pc;
1349
52d4b9ac
KH
1350
1351 pc = lookup_page_cgroup(page);
1352 if (!pc)
1353 return 0;
1354 lock_page_cgroup(pc);
1355 if (PageCgroupUsed(pc)) {
1356 unlock_page_cgroup(pc);
accf163e
KH
1357 return 0;
1358 }
52d4b9ac 1359 unlock_page_cgroup(pc);
accf163e
KH
1360 }
1361
b5a84319 1362 if (unlikely(!mm && !mem))
8697d331 1363 mm = &init_mm;
accf163e 1364
c05555b5
KH
1365 if (page_is_file_cache(page))
1366 return mem_cgroup_charge_common(page, mm, gfp_mask,
e8589cc1 1367 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
b5a84319 1368
83aae4c7
DN
1369 /* shmem */
1370 if (PageSwapCache(page)) {
1371 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1372 if (!ret)
1373 __mem_cgroup_commit_charge_swapin(page, mem,
1374 MEM_CGROUP_CHARGE_TYPE_SHMEM);
1375 } else
1376 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
1377 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
b5a84319 1378
b5a84319 1379 return ret;
e8589cc1
KH
1380}
1381
54595fe2
KH
1382/*
1383 * While swap-in, try_charge -> commit or cancel, the page is locked.
1384 * And when try_charge() successfully returns, one refcnt to memcg without
1385 * struct page_cgroup is aquired. This refcnt will be cumsumed by
1386 * "commit()" or removed by "cancel()"
1387 */
8c7c6e34
KH
1388int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
1389 struct page *page,
1390 gfp_t mask, struct mem_cgroup **ptr)
1391{
1392 struct mem_cgroup *mem;
54595fe2 1393 int ret;
8c7c6e34 1394
f8d66542 1395 if (mem_cgroup_disabled())
8c7c6e34
KH
1396 return 0;
1397
1398 if (!do_swap_account)
1399 goto charge_cur_mm;
8c7c6e34
KH
1400 /*
1401 * A racing thread's fault, or swapoff, may have already updated
1402 * the pte, and even removed page from swap cache: return success
1403 * to go on to do_swap_page()'s pte_same() test, which should fail.
1404 */
1405 if (!PageSwapCache(page))
1406 return 0;
b5a84319 1407 mem = try_get_mem_cgroup_from_swapcache(page);
54595fe2
KH
1408 if (!mem)
1409 goto charge_cur_mm;
8c7c6e34 1410 *ptr = mem;
54595fe2
KH
1411 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true);
1412 /* drop extra refcnt from tryget */
1413 css_put(&mem->css);
1414 return ret;
8c7c6e34
KH
1415charge_cur_mm:
1416 if (unlikely(!mm))
1417 mm = &init_mm;
1418 return __mem_cgroup_try_charge(mm, mask, ptr, true);
1419}
1420
83aae4c7
DN
1421static void
1422__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1423 enum charge_type ctype)
7a81b88c
KH
1424{
1425 struct page_cgroup *pc;
1426
f8d66542 1427 if (mem_cgroup_disabled())
7a81b88c
KH
1428 return;
1429 if (!ptr)
1430 return;
1431 pc = lookup_page_cgroup(page);
544122e5 1432 mem_cgroup_lru_del_before_commit_swapcache(page);
83aae4c7 1433 __mem_cgroup_commit_charge(ptr, pc, ctype);
544122e5 1434 mem_cgroup_lru_add_after_commit_swapcache(page);
8c7c6e34
KH
1435 /*
1436 * Now swap is on-memory. This means this page may be
1437 * counted both as mem and swap....double count.
03f3c433
KH
1438 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
1439 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
1440 * may call delete_from_swap_cache() before reach here.
8c7c6e34 1441 */
03f3c433 1442 if (do_swap_account && PageSwapCache(page)) {
8c7c6e34 1443 swp_entry_t ent = {.val = page_private(page)};
a3b2d692 1444 unsigned short id;
8c7c6e34 1445 struct mem_cgroup *memcg;
a3b2d692
KH
1446
1447 id = swap_cgroup_record(ent, 0);
1448 rcu_read_lock();
1449 memcg = mem_cgroup_lookup(id);
8c7c6e34 1450 if (memcg) {
a3b2d692
KH
1451 /*
1452 * This recorded memcg can be obsolete one. So, avoid
1453 * calling css_tryget
1454 */
8c7c6e34
KH
1455 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1456 mem_cgroup_put(memcg);
1457 }
a3b2d692 1458 rcu_read_unlock();
8c7c6e34 1459 }
08e552c6 1460 /* add this page(page_cgroup) to the LRU we want. */
544122e5 1461
7a81b88c
KH
1462}
1463
83aae4c7
DN
1464void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
1465{
1466 __mem_cgroup_commit_charge_swapin(page, ptr,
1467 MEM_CGROUP_CHARGE_TYPE_MAPPED);
1468}
1469
7a81b88c
KH
1470void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
1471{
f8d66542 1472 if (mem_cgroup_disabled())
7a81b88c
KH
1473 return;
1474 if (!mem)
1475 return;
1476 res_counter_uncharge(&mem->res, PAGE_SIZE);
8c7c6e34
KH
1477 if (do_swap_account)
1478 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
7a81b88c
KH
1479 css_put(&mem->css);
1480}
1481
1482
8a9f3ccd 1483/*
69029cd5 1484 * uncharge if !page_mapped(page)
8a9f3ccd 1485 */
8c7c6e34 1486static struct mem_cgroup *
69029cd5 1487__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
8a9f3ccd 1488{
8289546e 1489 struct page_cgroup *pc;
8c7c6e34 1490 struct mem_cgroup *mem = NULL;
072c56c1 1491 struct mem_cgroup_per_zone *mz;
8a9f3ccd 1492
f8d66542 1493 if (mem_cgroup_disabled())
8c7c6e34 1494 return NULL;
4077960e 1495
d13d1443 1496 if (PageSwapCache(page))
8c7c6e34 1497 return NULL;
d13d1443 1498
8697d331 1499 /*
3c541e14 1500 * Check if our page_cgroup is valid
8697d331 1501 */
52d4b9ac
KH
1502 pc = lookup_page_cgroup(page);
1503 if (unlikely(!pc || !PageCgroupUsed(pc)))
8c7c6e34 1504 return NULL;
b9c565d5 1505
52d4b9ac 1506 lock_page_cgroup(pc);
d13d1443 1507
8c7c6e34
KH
1508 mem = pc->mem_cgroup;
1509
d13d1443
KH
1510 if (!PageCgroupUsed(pc))
1511 goto unlock_out;
1512
1513 switch (ctype) {
1514 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
8a9478ca 1515 case MEM_CGROUP_CHARGE_TYPE_DROP:
d13d1443
KH
1516 if (page_mapped(page))
1517 goto unlock_out;
1518 break;
1519 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
1520 if (!PageAnon(page)) { /* Shared memory */
1521 if (page->mapping && !page_is_file_cache(page))
1522 goto unlock_out;
1523 } else if (page_mapped(page)) /* Anon */
1524 goto unlock_out;
1525 break;
1526 default:
1527 break;
52d4b9ac 1528 }
d13d1443 1529
8c7c6e34
KH
1530 res_counter_uncharge(&mem->res, PAGE_SIZE);
1531 if (do_swap_account && (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT))
1532 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
08e552c6 1533 mem_cgroup_charge_statistics(mem, pc, false);
04046e1a 1534
52d4b9ac 1535 ClearPageCgroupUsed(pc);
544122e5
KH
1536 /*
1537 * pc->mem_cgroup is not cleared here. It will be accessed when it's
1538 * freed from LRU. This is safe because uncharged page is expected not
1539 * to be reused (freed soon). Exception is SwapCache, it's handled by
1540 * special functions.
1541 */
b9c565d5 1542
69029cd5 1543 mz = page_cgroup_zoneinfo(pc);
52d4b9ac 1544 unlock_page_cgroup(pc);
fb59e9f1 1545
a7fe942e
KH
1546 /* at swapout, this memcg will be accessed to record to swap */
1547 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1548 css_put(&mem->css);
6d12e2d8 1549
8c7c6e34 1550 return mem;
d13d1443
KH
1551
1552unlock_out:
1553 unlock_page_cgroup(pc);
8c7c6e34 1554 return NULL;
3c541e14
BS
1555}
1556
69029cd5
KH
1557void mem_cgroup_uncharge_page(struct page *page)
1558{
52d4b9ac
KH
1559 /* early check. */
1560 if (page_mapped(page))
1561 return;
1562 if (page->mapping && !PageAnon(page))
1563 return;
69029cd5
KH
1564 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
1565}
1566
1567void mem_cgroup_uncharge_cache_page(struct page *page)
1568{
1569 VM_BUG_ON(page_mapped(page));
b7abea96 1570 VM_BUG_ON(page->mapping);
69029cd5
KH
1571 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
1572}
1573
e767e056 1574#ifdef CONFIG_SWAP
8c7c6e34 1575/*
e767e056 1576 * called after __delete_from_swap_cache() and drop "page" account.
8c7c6e34
KH
1577 * memcg information is recorded to swap_cgroup of "ent"
1578 */
8a9478ca
KH
1579void
1580mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
8c7c6e34
KH
1581{
1582 struct mem_cgroup *memcg;
8a9478ca
KH
1583 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
1584
1585 if (!swapout) /* this was a swap cache but the swap is unused ! */
1586 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
1587
1588 memcg = __mem_cgroup_uncharge_common(page, ctype);
8c7c6e34 1589
8c7c6e34 1590 /* record memcg information */
8a9478ca 1591 if (do_swap_account && swapout && memcg) {
a3b2d692 1592 swap_cgroup_record(ent, css_id(&memcg->css));
8c7c6e34
KH
1593 mem_cgroup_get(memcg);
1594 }
8a9478ca 1595 if (swapout && memcg)
a7fe942e 1596 css_put(&memcg->css);
8c7c6e34 1597}
e767e056 1598#endif
8c7c6e34
KH
1599
1600#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1601/*
1602 * called from swap_entry_free(). remove record in swap_cgroup and
1603 * uncharge "memsw" account.
1604 */
1605void mem_cgroup_uncharge_swap(swp_entry_t ent)
d13d1443 1606{
8c7c6e34 1607 struct mem_cgroup *memcg;
a3b2d692 1608 unsigned short id;
8c7c6e34
KH
1609
1610 if (!do_swap_account)
1611 return;
1612
a3b2d692
KH
1613 id = swap_cgroup_record(ent, 0);
1614 rcu_read_lock();
1615 memcg = mem_cgroup_lookup(id);
8c7c6e34 1616 if (memcg) {
a3b2d692
KH
1617 /*
1618 * We uncharge this because swap is freed.
1619 * This memcg can be obsolete one. We avoid calling css_tryget
1620 */
8c7c6e34
KH
1621 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1622 mem_cgroup_put(memcg);
1623 }
a3b2d692 1624 rcu_read_unlock();
d13d1443 1625}
8c7c6e34 1626#endif
d13d1443 1627
ae41be37 1628/*
01b1ae63
KH
1629 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
1630 * page belongs to.
ae41be37 1631 */
01b1ae63 1632int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
ae41be37
KH
1633{
1634 struct page_cgroup *pc;
e8589cc1 1635 struct mem_cgroup *mem = NULL;
e8589cc1 1636 int ret = 0;
8869b8f6 1637
f8d66542 1638 if (mem_cgroup_disabled())
4077960e
BS
1639 return 0;
1640
52d4b9ac
KH
1641 pc = lookup_page_cgroup(page);
1642 lock_page_cgroup(pc);
1643 if (PageCgroupUsed(pc)) {
e8589cc1
KH
1644 mem = pc->mem_cgroup;
1645 css_get(&mem->css);
e8589cc1 1646 }
52d4b9ac 1647 unlock_page_cgroup(pc);
01b1ae63 1648
e8589cc1 1649 if (mem) {
3bb4edf2 1650 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false);
e8589cc1
KH
1651 css_put(&mem->css);
1652 }
01b1ae63 1653 *ptr = mem;
e8589cc1 1654 return ret;
ae41be37 1655}
8869b8f6 1656
69029cd5 1657/* remove redundant charge if migration failed*/
01b1ae63
KH
1658void mem_cgroup_end_migration(struct mem_cgroup *mem,
1659 struct page *oldpage, struct page *newpage)
ae41be37 1660{
01b1ae63
KH
1661 struct page *target, *unused;
1662 struct page_cgroup *pc;
1663 enum charge_type ctype;
1664
1665 if (!mem)
1666 return;
1667
1668 /* at migration success, oldpage->mapping is NULL. */
1669 if (oldpage->mapping) {
1670 target = oldpage;
1671 unused = NULL;
1672 } else {
1673 target = newpage;
1674 unused = oldpage;
1675 }
1676
1677 if (PageAnon(target))
1678 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
1679 else if (page_is_file_cache(target))
1680 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
1681 else
1682 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
1683
1684 /* unused page is not on radix-tree now. */
d13d1443 1685 if (unused)
01b1ae63
KH
1686 __mem_cgroup_uncharge_common(unused, ctype);
1687
1688 pc = lookup_page_cgroup(target);
69029cd5 1689 /*
01b1ae63
KH
1690 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
1691 * So, double-counting is effectively avoided.
1692 */
1693 __mem_cgroup_commit_charge(mem, pc, ctype);
1694
1695 /*
1696 * Both of oldpage and newpage are still under lock_page().
1697 * Then, we don't have to care about race in radix-tree.
1698 * But we have to be careful that this page is unmapped or not.
1699 *
1700 * There is a case for !page_mapped(). At the start of
1701 * migration, oldpage was mapped. But now, it's zapped.
1702 * But we know *target* page is not freed/reused under us.
1703 * mem_cgroup_uncharge_page() does all necessary checks.
69029cd5 1704 */
01b1ae63
KH
1705 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
1706 mem_cgroup_uncharge_page(target);
ae41be37 1707}
78fb7466 1708
c9b0ed51 1709/*
ae3abae6
DN
1710 * A call to try to shrink memory usage on charge failure at shmem's swapin.
1711 * Calling hierarchical_reclaim is not enough because we should update
1712 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
1713 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
1714 * not from the memcg which this page would be charged to.
1715 * try_charge_swapin does all of these works properly.
c9b0ed51 1716 */
ae3abae6 1717int mem_cgroup_shmem_charge_fallback(struct page *page,
b5a84319
KH
1718 struct mm_struct *mm,
1719 gfp_t gfp_mask)
c9b0ed51 1720{
b5a84319 1721 struct mem_cgroup *mem = NULL;
ae3abae6 1722 int ret;
c9b0ed51 1723
f8d66542 1724 if (mem_cgroup_disabled())
cede86ac 1725 return 0;
c9b0ed51 1726
ae3abae6
DN
1727 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1728 if (!ret)
1729 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
c9b0ed51 1730
ae3abae6 1731 return ret;
c9b0ed51
KH
1732}
1733
8c7c6e34
KH
1734static DEFINE_MUTEX(set_limit_mutex);
1735
d38d2a75 1736static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
8c7c6e34 1737 unsigned long long val)
628f4235 1738{
81d39c20 1739 int retry_count;
628f4235 1740 int progress;
8c7c6e34 1741 u64 memswlimit;
628f4235 1742 int ret = 0;
81d39c20
KH
1743 int children = mem_cgroup_count_children(memcg);
1744 u64 curusage, oldusage;
1745
1746 /*
1747 * For keeping hierarchical_reclaim simple, how long we should retry
1748 * is depends on callers. We set our retry-count to be function
1749 * of # of children which we should visit in this loop.
1750 */
1751 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
1752
1753 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
628f4235 1754
8c7c6e34 1755 while (retry_count) {
628f4235
KH
1756 if (signal_pending(current)) {
1757 ret = -EINTR;
1758 break;
1759 }
8c7c6e34
KH
1760 /*
1761 * Rather than hide all in some function, I do this in
1762 * open coded manner. You see what this really does.
1763 * We have to guarantee mem->res.limit < mem->memsw.limit.
1764 */
1765 mutex_lock(&set_limit_mutex);
1766 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1767 if (memswlimit < val) {
1768 ret = -EINVAL;
1769 mutex_unlock(&set_limit_mutex);
628f4235
KH
1770 break;
1771 }
8c7c6e34 1772 ret = res_counter_set_limit(&memcg->res, val);
22a668d7
KH
1773 if (!ret) {
1774 if (memswlimit == val)
1775 memcg->memsw_is_minimum = true;
1776 else
1777 memcg->memsw_is_minimum = false;
1778 }
8c7c6e34
KH
1779 mutex_unlock(&set_limit_mutex);
1780
1781 if (!ret)
1782 break;
1783
42e9abb6 1784 progress = mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL,
81d39c20
KH
1785 false, true);
1786 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
1787 /* Usage is reduced ? */
1788 if (curusage >= oldusage)
1789 retry_count--;
1790 else
1791 oldusage = curusage;
8c7c6e34 1792 }
14797e23 1793
8c7c6e34
KH
1794 return ret;
1795}
1796
338c8431
LZ
1797static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
1798 unsigned long long val)
8c7c6e34 1799{
81d39c20 1800 int retry_count;
8c7c6e34 1801 u64 memlimit, oldusage, curusage;
81d39c20
KH
1802 int children = mem_cgroup_count_children(memcg);
1803 int ret = -EBUSY;
8c7c6e34 1804
81d39c20
KH
1805 /* see mem_cgroup_resize_res_limit */
1806 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
1807 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
8c7c6e34
KH
1808 while (retry_count) {
1809 if (signal_pending(current)) {
1810 ret = -EINTR;
1811 break;
1812 }
1813 /*
1814 * Rather than hide all in some function, I do this in
1815 * open coded manner. You see what this really does.
1816 * We have to guarantee mem->res.limit < mem->memsw.limit.
1817 */
1818 mutex_lock(&set_limit_mutex);
1819 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
1820 if (memlimit > val) {
1821 ret = -EINVAL;
1822 mutex_unlock(&set_limit_mutex);
1823 break;
1824 }
1825 ret = res_counter_set_limit(&memcg->memsw, val);
22a668d7
KH
1826 if (!ret) {
1827 if (memlimit == val)
1828 memcg->memsw_is_minimum = true;
1829 else
1830 memcg->memsw_is_minimum = false;
1831 }
8c7c6e34
KH
1832 mutex_unlock(&set_limit_mutex);
1833
1834 if (!ret)
1835 break;
1836
81d39c20 1837 mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL, true, true);
8c7c6e34 1838 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
81d39c20 1839 /* Usage is reduced ? */
8c7c6e34 1840 if (curusage >= oldusage)
628f4235 1841 retry_count--;
81d39c20
KH
1842 else
1843 oldusage = curusage;
628f4235
KH
1844 }
1845 return ret;
1846}
1847
cc847582
KH
1848/*
1849 * This routine traverse page_cgroup in given list and drop them all.
cc847582
KH
1850 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
1851 */
f817ed48 1852static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
08e552c6 1853 int node, int zid, enum lru_list lru)
cc847582 1854{
08e552c6
KH
1855 struct zone *zone;
1856 struct mem_cgroup_per_zone *mz;
f817ed48 1857 struct page_cgroup *pc, *busy;
08e552c6 1858 unsigned long flags, loop;
072c56c1 1859 struct list_head *list;
f817ed48 1860 int ret = 0;
072c56c1 1861
08e552c6
KH
1862 zone = &NODE_DATA(node)->node_zones[zid];
1863 mz = mem_cgroup_zoneinfo(mem, node, zid);
b69408e8 1864 list = &mz->lists[lru];
cc847582 1865
f817ed48
KH
1866 loop = MEM_CGROUP_ZSTAT(mz, lru);
1867 /* give some margin against EBUSY etc...*/
1868 loop += 256;
1869 busy = NULL;
1870 while (loop--) {
1871 ret = 0;
08e552c6 1872 spin_lock_irqsave(&zone->lru_lock, flags);
f817ed48 1873 if (list_empty(list)) {
08e552c6 1874 spin_unlock_irqrestore(&zone->lru_lock, flags);
52d4b9ac 1875 break;
f817ed48
KH
1876 }
1877 pc = list_entry(list->prev, struct page_cgroup, lru);
1878 if (busy == pc) {
1879 list_move(&pc->lru, list);
1880 busy = 0;
08e552c6 1881 spin_unlock_irqrestore(&zone->lru_lock, flags);
f817ed48
KH
1882 continue;
1883 }
08e552c6 1884 spin_unlock_irqrestore(&zone->lru_lock, flags);
f817ed48 1885
2c26fdd7 1886 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
f817ed48 1887 if (ret == -ENOMEM)
52d4b9ac 1888 break;
f817ed48
KH
1889
1890 if (ret == -EBUSY || ret == -EINVAL) {
1891 /* found lock contention or "pc" is obsolete. */
1892 busy = pc;
1893 cond_resched();
1894 } else
1895 busy = NULL;
cc847582 1896 }
08e552c6 1897
f817ed48
KH
1898 if (!ret && !list_empty(list))
1899 return -EBUSY;
1900 return ret;
cc847582
KH
1901}
1902
1903/*
1904 * make mem_cgroup's charge to be 0 if there is no task.
1905 * This enables deleting this mem_cgroup.
1906 */
c1e862c1 1907static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
cc847582 1908{
f817ed48
KH
1909 int ret;
1910 int node, zid, shrink;
1911 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
c1e862c1 1912 struct cgroup *cgrp = mem->css.cgroup;
8869b8f6 1913
cc847582 1914 css_get(&mem->css);
f817ed48
KH
1915
1916 shrink = 0;
c1e862c1
KH
1917 /* should free all ? */
1918 if (free_all)
1919 goto try_to_free;
f817ed48 1920move_account:
1ecaab2b 1921 while (mem->res.usage > 0) {
f817ed48 1922 ret = -EBUSY;
c1e862c1
KH
1923 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
1924 goto out;
1925 ret = -EINTR;
1926 if (signal_pending(current))
cc847582 1927 goto out;
52d4b9ac
KH
1928 /* This is for making all *used* pages to be on LRU. */
1929 lru_add_drain_all();
f817ed48 1930 ret = 0;
299b4eaa 1931 for_each_node_state(node, N_HIGH_MEMORY) {
f817ed48 1932 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
b69408e8 1933 enum lru_list l;
f817ed48
KH
1934 for_each_lru(l) {
1935 ret = mem_cgroup_force_empty_list(mem,
08e552c6 1936 node, zid, l);
f817ed48
KH
1937 if (ret)
1938 break;
1939 }
1ecaab2b 1940 }
f817ed48
KH
1941 if (ret)
1942 break;
1943 }
1944 /* it seems parent cgroup doesn't have enough mem */
1945 if (ret == -ENOMEM)
1946 goto try_to_free;
52d4b9ac 1947 cond_resched();
cc847582
KH
1948 }
1949 ret = 0;
1950out:
1951 css_put(&mem->css);
1952 return ret;
f817ed48
KH
1953
1954try_to_free:
c1e862c1
KH
1955 /* returns EBUSY if there is a task or if we come here twice. */
1956 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
f817ed48
KH
1957 ret = -EBUSY;
1958 goto out;
1959 }
c1e862c1
KH
1960 /* we call try-to-free pages for make this cgroup empty */
1961 lru_add_drain_all();
f817ed48
KH
1962 /* try to free all pages in this cgroup */
1963 shrink = 1;
1964 while (nr_retries && mem->res.usage > 0) {
1965 int progress;
c1e862c1
KH
1966
1967 if (signal_pending(current)) {
1968 ret = -EINTR;
1969 goto out;
1970 }
a7885eb8
KM
1971 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
1972 false, get_swappiness(mem));
c1e862c1 1973 if (!progress) {
f817ed48 1974 nr_retries--;
c1e862c1 1975 /* maybe some writeback is necessary */
8aa7e847 1976 congestion_wait(BLK_RW_ASYNC, HZ/10);
c1e862c1 1977 }
f817ed48
KH
1978
1979 }
08e552c6 1980 lru_add_drain();
f817ed48
KH
1981 /* try move_account...there may be some *locked* pages. */
1982 if (mem->res.usage)
1983 goto move_account;
1984 ret = 0;
1985 goto out;
cc847582
KH
1986}
1987
c1e862c1
KH
1988int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
1989{
1990 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
1991}
1992
1993
18f59ea7
BS
1994static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
1995{
1996 return mem_cgroup_from_cont(cont)->use_hierarchy;
1997}
1998
1999static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
2000 u64 val)
2001{
2002 int retval = 0;
2003 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2004 struct cgroup *parent = cont->parent;
2005 struct mem_cgroup *parent_mem = NULL;
2006
2007 if (parent)
2008 parent_mem = mem_cgroup_from_cont(parent);
2009
2010 cgroup_lock();
2011 /*
2012 * If parent's use_hiearchy is set, we can't make any modifications
2013 * in the child subtrees. If it is unset, then the change can
2014 * occur, provided the current cgroup has no children.
2015 *
2016 * For the root cgroup, parent_mem is NULL, we allow value to be
2017 * set if there are no children.
2018 */
2019 if ((!parent_mem || !parent_mem->use_hierarchy) &&
2020 (val == 1 || val == 0)) {
2021 if (list_empty(&cont->children))
2022 mem->use_hierarchy = val;
2023 else
2024 retval = -EBUSY;
2025 } else
2026 retval = -EINVAL;
2027 cgroup_unlock();
2028
2029 return retval;
2030}
2031
2c3daa72 2032static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
8cdea7c0 2033{
8c7c6e34
KH
2034 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2035 u64 val = 0;
2036 int type, name;
2037
2038 type = MEMFILE_TYPE(cft->private);
2039 name = MEMFILE_ATTR(cft->private);
2040 switch (type) {
2041 case _MEM:
2042 val = res_counter_read_u64(&mem->res, name);
2043 break;
2044 case _MEMSWAP:
338c8431 2045 val = res_counter_read_u64(&mem->memsw, name);
8c7c6e34
KH
2046 break;
2047 default:
2048 BUG();
2049 break;
2050 }
2051 return val;
8cdea7c0 2052}
628f4235
KH
2053/*
2054 * The user of this function is...
2055 * RES_LIMIT.
2056 */
856c13aa
PM
2057static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
2058 const char *buffer)
8cdea7c0 2059{
628f4235 2060 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
8c7c6e34 2061 int type, name;
628f4235
KH
2062 unsigned long long val;
2063 int ret;
2064
8c7c6e34
KH
2065 type = MEMFILE_TYPE(cft->private);
2066 name = MEMFILE_ATTR(cft->private);
2067 switch (name) {
628f4235
KH
2068 case RES_LIMIT:
2069 /* This function does all necessary parse...reuse it */
2070 ret = res_counter_memparse_write_strategy(buffer, &val);
8c7c6e34
KH
2071 if (ret)
2072 break;
2073 if (type == _MEM)
628f4235 2074 ret = mem_cgroup_resize_limit(memcg, val);
8c7c6e34
KH
2075 else
2076 ret = mem_cgroup_resize_memsw_limit(memcg, val);
628f4235
KH
2077 break;
2078 default:
2079 ret = -EINVAL; /* should be BUG() ? */
2080 break;
2081 }
2082 return ret;
8cdea7c0
BS
2083}
2084
fee7b548
KH
2085static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
2086 unsigned long long *mem_limit, unsigned long long *memsw_limit)
2087{
2088 struct cgroup *cgroup;
2089 unsigned long long min_limit, min_memsw_limit, tmp;
2090
2091 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2092 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2093 cgroup = memcg->css.cgroup;
2094 if (!memcg->use_hierarchy)
2095 goto out;
2096
2097 while (cgroup->parent) {
2098 cgroup = cgroup->parent;
2099 memcg = mem_cgroup_from_cont(cgroup);
2100 if (!memcg->use_hierarchy)
2101 break;
2102 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
2103 min_limit = min(min_limit, tmp);
2104 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2105 min_memsw_limit = min(min_memsw_limit, tmp);
2106 }
2107out:
2108 *mem_limit = min_limit;
2109 *memsw_limit = min_memsw_limit;
2110 return;
2111}
2112
29f2a4da 2113static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
c84872e1
PE
2114{
2115 struct mem_cgroup *mem;
8c7c6e34 2116 int type, name;
c84872e1
PE
2117
2118 mem = mem_cgroup_from_cont(cont);
8c7c6e34
KH
2119 type = MEMFILE_TYPE(event);
2120 name = MEMFILE_ATTR(event);
2121 switch (name) {
29f2a4da 2122 case RES_MAX_USAGE:
8c7c6e34
KH
2123 if (type == _MEM)
2124 res_counter_reset_max(&mem->res);
2125 else
2126 res_counter_reset_max(&mem->memsw);
29f2a4da
PE
2127 break;
2128 case RES_FAILCNT:
8c7c6e34
KH
2129 if (type == _MEM)
2130 res_counter_reset_failcnt(&mem->res);
2131 else
2132 res_counter_reset_failcnt(&mem->memsw);
29f2a4da
PE
2133 break;
2134 }
85cc59db 2135 return 0;
c84872e1
PE
2136}
2137
14067bb3
KH
2138
2139/* For read statistics */
2140enum {
2141 MCS_CACHE,
2142 MCS_RSS,
d69b042f 2143 MCS_MAPPED_FILE,
14067bb3
KH
2144 MCS_PGPGIN,
2145 MCS_PGPGOUT,
2146 MCS_INACTIVE_ANON,
2147 MCS_ACTIVE_ANON,
2148 MCS_INACTIVE_FILE,
2149 MCS_ACTIVE_FILE,
2150 MCS_UNEVICTABLE,
2151 NR_MCS_STAT,
2152};
2153
2154struct mcs_total_stat {
2155 s64 stat[NR_MCS_STAT];
d2ceb9b7
KH
2156};
2157
14067bb3
KH
2158struct {
2159 char *local_name;
2160 char *total_name;
2161} memcg_stat_strings[NR_MCS_STAT] = {
2162 {"cache", "total_cache"},
2163 {"rss", "total_rss"},
d69b042f 2164 {"mapped_file", "total_mapped_file"},
14067bb3
KH
2165 {"pgpgin", "total_pgpgin"},
2166 {"pgpgout", "total_pgpgout"},
2167 {"inactive_anon", "total_inactive_anon"},
2168 {"active_anon", "total_active_anon"},
2169 {"inactive_file", "total_inactive_file"},
2170 {"active_file", "total_active_file"},
2171 {"unevictable", "total_unevictable"}
2172};
2173
2174
2175static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
2176{
2177 struct mcs_total_stat *s = data;
2178 s64 val;
2179
2180 /* per cpu stat */
2181 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE);
2182 s->stat[MCS_CACHE] += val * PAGE_SIZE;
2183 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
2184 s->stat[MCS_RSS] += val * PAGE_SIZE;
d69b042f
BS
2185 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_MAPPED_FILE);
2186 s->stat[MCS_MAPPED_FILE] += val * PAGE_SIZE;
14067bb3
KH
2187 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT);
2188 s->stat[MCS_PGPGIN] += val;
2189 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT);
2190 s->stat[MCS_PGPGOUT] += val;
2191
2192 /* per zone stat */
2193 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
2194 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
2195 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
2196 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
2197 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
2198 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
2199 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
2200 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
2201 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
2202 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
2203 return 0;
2204}
2205
2206static void
2207mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
2208{
2209 mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
2210}
2211
c64745cf
PM
2212static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
2213 struct cgroup_map_cb *cb)
d2ceb9b7 2214{
d2ceb9b7 2215 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
14067bb3 2216 struct mcs_total_stat mystat;
d2ceb9b7
KH
2217 int i;
2218
14067bb3
KH
2219 memset(&mystat, 0, sizeof(mystat));
2220 mem_cgroup_get_local_stat(mem_cont, &mystat);
d2ceb9b7 2221
14067bb3
KH
2222 for (i = 0; i < NR_MCS_STAT; i++)
2223 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
7b854121 2224
14067bb3 2225 /* Hierarchical information */
fee7b548
KH
2226 {
2227 unsigned long long limit, memsw_limit;
2228 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
2229 cb->fill(cb, "hierarchical_memory_limit", limit);
2230 if (do_swap_account)
2231 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
2232 }
7f016ee8 2233
14067bb3
KH
2234 memset(&mystat, 0, sizeof(mystat));
2235 mem_cgroup_get_total_stat(mem_cont, &mystat);
2236 for (i = 0; i < NR_MCS_STAT; i++)
2237 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
2238
2239
7f016ee8 2240#ifdef CONFIG_DEBUG_VM
c772be93 2241 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
7f016ee8
KM
2242
2243 {
2244 int nid, zid;
2245 struct mem_cgroup_per_zone *mz;
2246 unsigned long recent_rotated[2] = {0, 0};
2247 unsigned long recent_scanned[2] = {0, 0};
2248
2249 for_each_online_node(nid)
2250 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
2251 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
2252
2253 recent_rotated[0] +=
2254 mz->reclaim_stat.recent_rotated[0];
2255 recent_rotated[1] +=
2256 mz->reclaim_stat.recent_rotated[1];
2257 recent_scanned[0] +=
2258 mz->reclaim_stat.recent_scanned[0];
2259 recent_scanned[1] +=
2260 mz->reclaim_stat.recent_scanned[1];
2261 }
2262 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
2263 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
2264 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
2265 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
2266 }
2267#endif
2268
d2ceb9b7
KH
2269 return 0;
2270}
2271
a7885eb8
KM
2272static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
2273{
2274 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2275
2276 return get_swappiness(memcg);
2277}
2278
2279static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
2280 u64 val)
2281{
2282 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2283 struct mem_cgroup *parent;
068b38c1 2284
a7885eb8
KM
2285 if (val > 100)
2286 return -EINVAL;
2287
2288 if (cgrp->parent == NULL)
2289 return -EINVAL;
2290
2291 parent = mem_cgroup_from_cont(cgrp->parent);
068b38c1
LZ
2292
2293 cgroup_lock();
2294
a7885eb8
KM
2295 /* If under hierarchy, only empty-root can set this value */
2296 if ((parent->use_hierarchy) ||
068b38c1
LZ
2297 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
2298 cgroup_unlock();
a7885eb8 2299 return -EINVAL;
068b38c1 2300 }
a7885eb8
KM
2301
2302 spin_lock(&memcg->reclaim_param_lock);
2303 memcg->swappiness = val;
2304 spin_unlock(&memcg->reclaim_param_lock);
2305
068b38c1
LZ
2306 cgroup_unlock();
2307
a7885eb8
KM
2308 return 0;
2309}
2310
c1e862c1 2311
8cdea7c0
BS
2312static struct cftype mem_cgroup_files[] = {
2313 {
0eea1030 2314 .name = "usage_in_bytes",
8c7c6e34 2315 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
2c3daa72 2316 .read_u64 = mem_cgroup_read,
8cdea7c0 2317 },
c84872e1
PE
2318 {
2319 .name = "max_usage_in_bytes",
8c7c6e34 2320 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
29f2a4da 2321 .trigger = mem_cgroup_reset,
c84872e1
PE
2322 .read_u64 = mem_cgroup_read,
2323 },
8cdea7c0 2324 {
0eea1030 2325 .name = "limit_in_bytes",
8c7c6e34 2326 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
856c13aa 2327 .write_string = mem_cgroup_write,
2c3daa72 2328 .read_u64 = mem_cgroup_read,
8cdea7c0
BS
2329 },
2330 {
2331 .name = "failcnt",
8c7c6e34 2332 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
29f2a4da 2333 .trigger = mem_cgroup_reset,
2c3daa72 2334 .read_u64 = mem_cgroup_read,
8cdea7c0 2335 },
d2ceb9b7
KH
2336 {
2337 .name = "stat",
c64745cf 2338 .read_map = mem_control_stat_show,
d2ceb9b7 2339 },
c1e862c1
KH
2340 {
2341 .name = "force_empty",
2342 .trigger = mem_cgroup_force_empty_write,
2343 },
18f59ea7
BS
2344 {
2345 .name = "use_hierarchy",
2346 .write_u64 = mem_cgroup_hierarchy_write,
2347 .read_u64 = mem_cgroup_hierarchy_read,
2348 },
a7885eb8
KM
2349 {
2350 .name = "swappiness",
2351 .read_u64 = mem_cgroup_swappiness_read,
2352 .write_u64 = mem_cgroup_swappiness_write,
2353 },
8cdea7c0
BS
2354};
2355
8c7c6e34
KH
2356#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2357static struct cftype memsw_cgroup_files[] = {
2358 {
2359 .name = "memsw.usage_in_bytes",
2360 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
2361 .read_u64 = mem_cgroup_read,
2362 },
2363 {
2364 .name = "memsw.max_usage_in_bytes",
2365 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
2366 .trigger = mem_cgroup_reset,
2367 .read_u64 = mem_cgroup_read,
2368 },
2369 {
2370 .name = "memsw.limit_in_bytes",
2371 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
2372 .write_string = mem_cgroup_write,
2373 .read_u64 = mem_cgroup_read,
2374 },
2375 {
2376 .name = "memsw.failcnt",
2377 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
2378 .trigger = mem_cgroup_reset,
2379 .read_u64 = mem_cgroup_read,
2380 },
2381};
2382
2383static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2384{
2385 if (!do_swap_account)
2386 return 0;
2387 return cgroup_add_files(cont, ss, memsw_cgroup_files,
2388 ARRAY_SIZE(memsw_cgroup_files));
2389};
2390#else
2391static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2392{
2393 return 0;
2394}
2395#endif
2396
6d12e2d8
KH
2397static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2398{
2399 struct mem_cgroup_per_node *pn;
1ecaab2b 2400 struct mem_cgroup_per_zone *mz;
b69408e8 2401 enum lru_list l;
41e3355d 2402 int zone, tmp = node;
1ecaab2b
KH
2403 /*
2404 * This routine is called against possible nodes.
2405 * But it's BUG to call kmalloc() against offline node.
2406 *
2407 * TODO: this routine can waste much memory for nodes which will
2408 * never be onlined. It's better to use memory hotplug callback
2409 * function.
2410 */
41e3355d
KH
2411 if (!node_state(node, N_NORMAL_MEMORY))
2412 tmp = -1;
2413 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
6d12e2d8
KH
2414 if (!pn)
2415 return 1;
1ecaab2b 2416
6d12e2d8
KH
2417 mem->info.nodeinfo[node] = pn;
2418 memset(pn, 0, sizeof(*pn));
1ecaab2b
KH
2419
2420 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
2421 mz = &pn->zoneinfo[zone];
b69408e8
CL
2422 for_each_lru(l)
2423 INIT_LIST_HEAD(&mz->lists[l]);
1ecaab2b 2424 }
6d12e2d8
KH
2425 return 0;
2426}
2427
1ecaab2b
KH
2428static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2429{
2430 kfree(mem->info.nodeinfo[node]);
2431}
2432
c8dad2bb
JB
2433static int mem_cgroup_size(void)
2434{
2435 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
2436 return sizeof(struct mem_cgroup) + cpustat_size;
2437}
2438
33327948
KH
2439static struct mem_cgroup *mem_cgroup_alloc(void)
2440{
2441 struct mem_cgroup *mem;
c8dad2bb 2442 int size = mem_cgroup_size();
33327948 2443
c8dad2bb
JB
2444 if (size < PAGE_SIZE)
2445 mem = kmalloc(size, GFP_KERNEL);
33327948 2446 else
c8dad2bb 2447 mem = vmalloc(size);
33327948
KH
2448
2449 if (mem)
c8dad2bb 2450 memset(mem, 0, size);
33327948
KH
2451 return mem;
2452}
2453
8c7c6e34
KH
2454/*
2455 * At destroying mem_cgroup, references from swap_cgroup can remain.
2456 * (scanning all at force_empty is too costly...)
2457 *
2458 * Instead of clearing all references at force_empty, we remember
2459 * the number of reference from swap_cgroup and free mem_cgroup when
2460 * it goes down to 0.
2461 *
8c7c6e34
KH
2462 * Removal of cgroup itself succeeds regardless of refs from swap.
2463 */
2464
a7ba0eef 2465static void __mem_cgroup_free(struct mem_cgroup *mem)
33327948 2466{
08e552c6
KH
2467 int node;
2468
04046e1a
KH
2469 free_css_id(&mem_cgroup_subsys, &mem->css);
2470
08e552c6
KH
2471 for_each_node_state(node, N_POSSIBLE)
2472 free_mem_cgroup_per_zone_info(mem, node);
2473
c8dad2bb 2474 if (mem_cgroup_size() < PAGE_SIZE)
33327948
KH
2475 kfree(mem);
2476 else
2477 vfree(mem);
2478}
2479
8c7c6e34
KH
2480static void mem_cgroup_get(struct mem_cgroup *mem)
2481{
2482 atomic_inc(&mem->refcnt);
2483}
2484
2485static void mem_cgroup_put(struct mem_cgroup *mem)
2486{
7bcc1bb1
DN
2487 if (atomic_dec_and_test(&mem->refcnt)) {
2488 struct mem_cgroup *parent = parent_mem_cgroup(mem);
a7ba0eef 2489 __mem_cgroup_free(mem);
7bcc1bb1
DN
2490 if (parent)
2491 mem_cgroup_put(parent);
2492 }
8c7c6e34
KH
2493}
2494
7bcc1bb1
DN
2495/*
2496 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
2497 */
2498static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
2499{
2500 if (!mem->res.parent)
2501 return NULL;
2502 return mem_cgroup_from_res_counter(mem->res.parent, res);
2503}
33327948 2504
c077719b
KH
2505#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2506static void __init enable_swap_cgroup(void)
2507{
f8d66542 2508 if (!mem_cgroup_disabled() && really_do_swap_account)
c077719b
KH
2509 do_swap_account = 1;
2510}
2511#else
2512static void __init enable_swap_cgroup(void)
2513{
2514}
2515#endif
2516
0eb253e2 2517static struct cgroup_subsys_state * __ref
8cdea7c0
BS
2518mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
2519{
28dbc4b6 2520 struct mem_cgroup *mem, *parent;
04046e1a 2521 long error = -ENOMEM;
6d12e2d8 2522 int node;
8cdea7c0 2523
c8dad2bb
JB
2524 mem = mem_cgroup_alloc();
2525 if (!mem)
04046e1a 2526 return ERR_PTR(error);
78fb7466 2527
6d12e2d8
KH
2528 for_each_node_state(node, N_POSSIBLE)
2529 if (alloc_mem_cgroup_per_zone_info(mem, node))
2530 goto free_out;
c077719b 2531 /* root ? */
28dbc4b6 2532 if (cont->parent == NULL) {
c077719b 2533 enable_swap_cgroup();
28dbc4b6 2534 parent = NULL;
18f59ea7 2535 } else {
28dbc4b6 2536 parent = mem_cgroup_from_cont(cont->parent);
18f59ea7
BS
2537 mem->use_hierarchy = parent->use_hierarchy;
2538 }
28dbc4b6 2539
18f59ea7
BS
2540 if (parent && parent->use_hierarchy) {
2541 res_counter_init(&mem->res, &parent->res);
2542 res_counter_init(&mem->memsw, &parent->memsw);
7bcc1bb1
DN
2543 /*
2544 * We increment refcnt of the parent to ensure that we can
2545 * safely access it on res_counter_charge/uncharge.
2546 * This refcnt will be decremented when freeing this
2547 * mem_cgroup(see mem_cgroup_put).
2548 */
2549 mem_cgroup_get(parent);
18f59ea7
BS
2550 } else {
2551 res_counter_init(&mem->res, NULL);
2552 res_counter_init(&mem->memsw, NULL);
2553 }
04046e1a 2554 mem->last_scanned_child = 0;
2733c06a 2555 spin_lock_init(&mem->reclaim_param_lock);
6d61ef40 2556
a7885eb8
KM
2557 if (parent)
2558 mem->swappiness = get_swappiness(parent);
a7ba0eef 2559 atomic_set(&mem->refcnt, 1);
8cdea7c0 2560 return &mem->css;
6d12e2d8 2561free_out:
a7ba0eef 2562 __mem_cgroup_free(mem);
04046e1a 2563 return ERR_PTR(error);
8cdea7c0
BS
2564}
2565
ec64f515 2566static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
df878fb0
KH
2567 struct cgroup *cont)
2568{
2569 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
ec64f515
KH
2570
2571 return mem_cgroup_force_empty(mem, false);
df878fb0
KH
2572}
2573
8cdea7c0
BS
2574static void mem_cgroup_destroy(struct cgroup_subsys *ss,
2575 struct cgroup *cont)
2576{
c268e994 2577 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
c268e994 2578
c268e994 2579 mem_cgroup_put(mem);
8cdea7c0
BS
2580}
2581
2582static int mem_cgroup_populate(struct cgroup_subsys *ss,
2583 struct cgroup *cont)
2584{
8c7c6e34
KH
2585 int ret;
2586
2587 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
2588 ARRAY_SIZE(mem_cgroup_files));
2589
2590 if (!ret)
2591 ret = register_memsw_files(cont, ss);
2592 return ret;
8cdea7c0
BS
2593}
2594
67e465a7
BS
2595static void mem_cgroup_move_task(struct cgroup_subsys *ss,
2596 struct cgroup *cont,
2597 struct cgroup *old_cont,
2598 struct task_struct *p)
2599{
7f4d454d 2600 mutex_lock(&memcg_tasklist);
67e465a7 2601 /*
f9717d28
NK
2602 * FIXME: It's better to move charges of this process from old
2603 * memcg to new memcg. But it's just on TODO-List now.
67e465a7 2604 */
7f4d454d 2605 mutex_unlock(&memcg_tasklist);
67e465a7
BS
2606}
2607
8cdea7c0
BS
2608struct cgroup_subsys mem_cgroup_subsys = {
2609 .name = "memory",
2610 .subsys_id = mem_cgroup_subsys_id,
2611 .create = mem_cgroup_create,
df878fb0 2612 .pre_destroy = mem_cgroup_pre_destroy,
8cdea7c0
BS
2613 .destroy = mem_cgroup_destroy,
2614 .populate = mem_cgroup_populate,
67e465a7 2615 .attach = mem_cgroup_move_task,
6d12e2d8 2616 .early_init = 0,
04046e1a 2617 .use_id = 1,
8cdea7c0 2618};
c077719b
KH
2619
2620#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2621
2622static int __init disable_swap_account(char *s)
2623{
2624 really_do_swap_account = 0;
2625 return 1;
2626}
2627__setup("noswapaccount", disable_swap_account);
2628#endif