]> bbs.cooldavid.org Git - net-next-2.6.git/blame - mm/memcontrol.c
memcg: coalesce charging via percpu storage
[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>
f64c3f54 32#include <linux/rbtree.h>
b6ac57d5 33#include <linux/slab.h>
66e1707b
BS
34#include <linux/swap.h>
35#include <linux/spinlock.h>
36#include <linux/fs.h>
d2ceb9b7 37#include <linux/seq_file.h>
33327948 38#include <linux/vmalloc.h>
b69408e8 39#include <linux/mm_inline.h>
52d4b9ac 40#include <linux/page_cgroup.h>
cdec2e42 41#include <linux/cpu.h>
08e552c6 42#include "internal.h"
8cdea7c0 43
8697d331
BS
44#include <asm/uaccess.h>
45
a181b0e8 46struct cgroup_subsys mem_cgroup_subsys __read_mostly;
a181b0e8 47#define MEM_CGROUP_RECLAIM_RETRIES 5
4b3bde4c 48struct mem_cgroup *root_mem_cgroup __read_mostly;
8cdea7c0 49
c077719b 50#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
338c8431 51/* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
c077719b
KH
52int do_swap_account __read_mostly;
53static int really_do_swap_account __initdata = 1; /* for remember boot option*/
54#else
55#define do_swap_account (0)
56#endif
57
7f4d454d 58static DEFINE_MUTEX(memcg_tasklist); /* can be hold under cgroup_mutex */
f64c3f54 59#define SOFTLIMIT_EVENTS_THRESH (1000)
c077719b 60
d52aa412
KH
61/*
62 * Statistics for memory cgroup.
63 */
64enum mem_cgroup_stat_index {
65 /*
66 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
67 */
68 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
d69b042f
BS
69 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
70 MEM_CGROUP_STAT_MAPPED_FILE, /* # of pages charged as file rss */
55e462b0
BR
71 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
72 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
f64c3f54 73 MEM_CGROUP_STAT_EVENTS, /* sum of pagein + pageout for internal use */
0c3e73e8 74 MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */
d52aa412
KH
75
76 MEM_CGROUP_STAT_NSTATS,
77};
78
79struct mem_cgroup_stat_cpu {
80 s64 count[MEM_CGROUP_STAT_NSTATS];
81} ____cacheline_aligned_in_smp;
82
83struct mem_cgroup_stat {
c8dad2bb 84 struct mem_cgroup_stat_cpu cpustat[0];
d52aa412
KH
85};
86
f64c3f54
BS
87static inline void
88__mem_cgroup_stat_reset_safe(struct mem_cgroup_stat_cpu *stat,
89 enum mem_cgroup_stat_index idx)
90{
91 stat->count[idx] = 0;
92}
93
94static inline s64
95__mem_cgroup_stat_read_local(struct mem_cgroup_stat_cpu *stat,
96 enum mem_cgroup_stat_index idx)
97{
98 return stat->count[idx];
99}
100
d52aa412
KH
101/*
102 * For accounting under irq disable, no need for increment preempt count.
103 */
addb9efe 104static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
d52aa412
KH
105 enum mem_cgroup_stat_index idx, int val)
106{
addb9efe 107 stat->count[idx] += val;
d52aa412
KH
108}
109
110static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
111 enum mem_cgroup_stat_index idx)
112{
113 int cpu;
114 s64 ret = 0;
115 for_each_possible_cpu(cpu)
116 ret += stat->cpustat[cpu].count[idx];
117 return ret;
118}
119
04046e1a
KH
120static s64 mem_cgroup_local_usage(struct mem_cgroup_stat *stat)
121{
122 s64 ret;
123
124 ret = mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_CACHE);
125 ret += mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_RSS);
126 return ret;
127}
128
6d12e2d8
KH
129/*
130 * per-zone information in memory controller.
131 */
6d12e2d8 132struct mem_cgroup_per_zone {
072c56c1
KH
133 /*
134 * spin_lock to protect the per cgroup LRU
135 */
b69408e8
CL
136 struct list_head lists[NR_LRU_LISTS];
137 unsigned long count[NR_LRU_LISTS];
3e2f41f1
KM
138
139 struct zone_reclaim_stat reclaim_stat;
f64c3f54
BS
140 struct rb_node tree_node; /* RB tree node */
141 unsigned long long usage_in_excess;/* Set to the value by which */
142 /* the soft limit is exceeded*/
143 bool on_tree;
4e416953
BS
144 struct mem_cgroup *mem; /* Back pointer, we cannot */
145 /* use container_of */
6d12e2d8
KH
146};
147/* Macro for accessing counter */
148#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
149
150struct mem_cgroup_per_node {
151 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
152};
153
154struct mem_cgroup_lru_info {
155 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
156};
157
f64c3f54
BS
158/*
159 * Cgroups above their limits are maintained in a RB-Tree, independent of
160 * their hierarchy representation
161 */
162
163struct mem_cgroup_tree_per_zone {
164 struct rb_root rb_root;
165 spinlock_t lock;
166};
167
168struct mem_cgroup_tree_per_node {
169 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
170};
171
172struct mem_cgroup_tree {
173 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
174};
175
176static struct mem_cgroup_tree soft_limit_tree __read_mostly;
177
8cdea7c0
BS
178/*
179 * The memory controller data structure. The memory controller controls both
180 * page cache and RSS per cgroup. We would eventually like to provide
181 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
182 * to help the administrator determine what knobs to tune.
183 *
184 * TODO: Add a water mark for the memory controller. Reclaim will begin when
8a9f3ccd
BS
185 * we hit the water mark. May be even add a low water mark, such that
186 * no reclaim occurs from a cgroup at it's low water mark, this is
187 * a feature that will be implemented much later in the future.
8cdea7c0
BS
188 */
189struct mem_cgroup {
190 struct cgroup_subsys_state css;
191 /*
192 * the counter to account for memory usage
193 */
194 struct res_counter res;
8c7c6e34
KH
195 /*
196 * the counter to account for mem+swap usage.
197 */
198 struct res_counter memsw;
78fb7466
PE
199 /*
200 * Per cgroup active and inactive list, similar to the
201 * per zone LRU lists.
78fb7466 202 */
6d12e2d8 203 struct mem_cgroup_lru_info info;
072c56c1 204
2733c06a
KM
205 /*
206 protect against reclaim related member.
207 */
208 spinlock_t reclaim_param_lock;
209
6c48a1d0 210 int prev_priority; /* for recording reclaim priority */
6d61ef40
BS
211
212 /*
af901ca1 213 * While reclaiming in a hierarchy, we cache the last child we
04046e1a 214 * reclaimed from.
6d61ef40 215 */
04046e1a 216 int last_scanned_child;
18f59ea7
BS
217 /*
218 * Should the accounting and control be hierarchical, per subtree?
219 */
220 bool use_hierarchy;
a636b327 221 unsigned long last_oom_jiffies;
8c7c6e34 222 atomic_t refcnt;
14797e23 223
a7885eb8
KM
224 unsigned int swappiness;
225
22a668d7
KH
226 /* set when res.limit == memsw.limit */
227 bool memsw_is_minimum;
228
d52aa412 229 /*
c8dad2bb 230 * statistics. This must be placed at the end of memcg.
d52aa412
KH
231 */
232 struct mem_cgroup_stat stat;
8cdea7c0
BS
233};
234
4e416953
BS
235/*
236 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
237 * limit reclaim to prevent infinite loops, if they ever occur.
238 */
239#define MEM_CGROUP_MAX_RECLAIM_LOOPS (100)
240#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2)
241
217bc319
KH
242enum charge_type {
243 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
244 MEM_CGROUP_CHARGE_TYPE_MAPPED,
4f98a2fe 245 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
c05555b5 246 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
d13d1443 247 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
8a9478ca 248 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
c05555b5
KH
249 NR_CHARGE_TYPE,
250};
251
52d4b9ac
KH
252/* only for here (for easy reading.) */
253#define PCGF_CACHE (1UL << PCG_CACHE)
254#define PCGF_USED (1UL << PCG_USED)
52d4b9ac 255#define PCGF_LOCK (1UL << PCG_LOCK)
4b3bde4c
BS
256/* Not used, but added here for completeness */
257#define PCGF_ACCT (1UL << PCG_ACCT)
217bc319 258
8c7c6e34
KH
259/* for encoding cft->private value on file */
260#define _MEM (0)
261#define _MEMSWAP (1)
262#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
263#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
264#define MEMFILE_ATTR(val) ((val) & 0xffff)
265
75822b44
BS
266/*
267 * Reclaim flags for mem_cgroup_hierarchical_reclaim
268 */
269#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
270#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
271#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
272#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
4e416953
BS
273#define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
274#define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
75822b44 275
8c7c6e34
KH
276static void mem_cgroup_get(struct mem_cgroup *mem);
277static void mem_cgroup_put(struct mem_cgroup *mem);
7bcc1bb1 278static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
cdec2e42 279static void drain_all_stock_async(void);
8c7c6e34 280
f64c3f54
BS
281static struct mem_cgroup_per_zone *
282mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
283{
284 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
285}
286
287static struct mem_cgroup_per_zone *
288page_cgroup_zoneinfo(struct page_cgroup *pc)
289{
290 struct mem_cgroup *mem = pc->mem_cgroup;
291 int nid = page_cgroup_nid(pc);
292 int zid = page_cgroup_zid(pc);
293
294 if (!mem)
295 return NULL;
296
297 return mem_cgroup_zoneinfo(mem, nid, zid);
298}
299
300static struct mem_cgroup_tree_per_zone *
301soft_limit_tree_node_zone(int nid, int zid)
302{
303 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
304}
305
306static struct mem_cgroup_tree_per_zone *
307soft_limit_tree_from_page(struct page *page)
308{
309 int nid = page_to_nid(page);
310 int zid = page_zonenum(page);
311
312 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
313}
314
315static void
4e416953 316__mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
f64c3f54 317 struct mem_cgroup_per_zone *mz,
ef8745c1
KH
318 struct mem_cgroup_tree_per_zone *mctz,
319 unsigned long long new_usage_in_excess)
f64c3f54
BS
320{
321 struct rb_node **p = &mctz->rb_root.rb_node;
322 struct rb_node *parent = NULL;
323 struct mem_cgroup_per_zone *mz_node;
324
325 if (mz->on_tree)
326 return;
327
ef8745c1
KH
328 mz->usage_in_excess = new_usage_in_excess;
329 if (!mz->usage_in_excess)
330 return;
f64c3f54
BS
331 while (*p) {
332 parent = *p;
333 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
334 tree_node);
335 if (mz->usage_in_excess < mz_node->usage_in_excess)
336 p = &(*p)->rb_left;
337 /*
338 * We can't avoid mem cgroups that are over their soft
339 * limit by the same amount
340 */
341 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
342 p = &(*p)->rb_right;
343 }
344 rb_link_node(&mz->tree_node, parent, p);
345 rb_insert_color(&mz->tree_node, &mctz->rb_root);
346 mz->on_tree = true;
4e416953
BS
347}
348
349static void
350__mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
351 struct mem_cgroup_per_zone *mz,
352 struct mem_cgroup_tree_per_zone *mctz)
353{
354 if (!mz->on_tree)
355 return;
356 rb_erase(&mz->tree_node, &mctz->rb_root);
357 mz->on_tree = false;
358}
359
f64c3f54
BS
360static void
361mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
362 struct mem_cgroup_per_zone *mz,
363 struct mem_cgroup_tree_per_zone *mctz)
364{
365 spin_lock(&mctz->lock);
4e416953 366 __mem_cgroup_remove_exceeded(mem, mz, mctz);
f64c3f54
BS
367 spin_unlock(&mctz->lock);
368}
369
370static bool mem_cgroup_soft_limit_check(struct mem_cgroup *mem)
371{
372 bool ret = false;
373 int cpu;
374 s64 val;
375 struct mem_cgroup_stat_cpu *cpustat;
376
377 cpu = get_cpu();
378 cpustat = &mem->stat.cpustat[cpu];
379 val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_EVENTS);
380 if (unlikely(val > SOFTLIMIT_EVENTS_THRESH)) {
381 __mem_cgroup_stat_reset_safe(cpustat, MEM_CGROUP_STAT_EVENTS);
382 ret = true;
383 }
384 put_cpu();
385 return ret;
386}
387
388static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
389{
ef8745c1 390 unsigned long long excess;
f64c3f54
BS
391 struct mem_cgroup_per_zone *mz;
392 struct mem_cgroup_tree_per_zone *mctz;
4e649152
KH
393 int nid = page_to_nid(page);
394 int zid = page_zonenum(page);
f64c3f54
BS
395 mctz = soft_limit_tree_from_page(page);
396
397 /*
4e649152
KH
398 * Necessary to update all ancestors when hierarchy is used.
399 * because their event counter is not touched.
f64c3f54 400 */
4e649152
KH
401 for (; mem; mem = parent_mem_cgroup(mem)) {
402 mz = mem_cgroup_zoneinfo(mem, nid, zid);
ef8745c1 403 excess = res_counter_soft_limit_excess(&mem->res);
4e649152
KH
404 /*
405 * We have to update the tree if mz is on RB-tree or
406 * mem is over its softlimit.
407 */
ef8745c1 408 if (excess || mz->on_tree) {
4e649152
KH
409 spin_lock(&mctz->lock);
410 /* if on-tree, remove it */
411 if (mz->on_tree)
412 __mem_cgroup_remove_exceeded(mem, mz, mctz);
413 /*
ef8745c1
KH
414 * Insert again. mz->usage_in_excess will be updated.
415 * If excess is 0, no tree ops.
4e649152 416 */
ef8745c1 417 __mem_cgroup_insert_exceeded(mem, mz, mctz, excess);
4e649152
KH
418 spin_unlock(&mctz->lock);
419 }
f64c3f54
BS
420 }
421}
422
423static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
424{
425 int node, zone;
426 struct mem_cgroup_per_zone *mz;
427 struct mem_cgroup_tree_per_zone *mctz;
428
429 for_each_node_state(node, N_POSSIBLE) {
430 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
431 mz = mem_cgroup_zoneinfo(mem, node, zone);
432 mctz = soft_limit_tree_node_zone(node, zone);
433 mem_cgroup_remove_exceeded(mem, mz, mctz);
434 }
435 }
436}
437
4e416953
BS
438static inline unsigned long mem_cgroup_get_excess(struct mem_cgroup *mem)
439{
440 return res_counter_soft_limit_excess(&mem->res) >> PAGE_SHIFT;
441}
442
443static struct mem_cgroup_per_zone *
444__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
445{
446 struct rb_node *rightmost = NULL;
26251eaf 447 struct mem_cgroup_per_zone *mz;
4e416953
BS
448
449retry:
26251eaf 450 mz = NULL;
4e416953
BS
451 rightmost = rb_last(&mctz->rb_root);
452 if (!rightmost)
453 goto done; /* Nothing to reclaim from */
454
455 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
456 /*
457 * Remove the node now but someone else can add it back,
458 * we will to add it back at the end of reclaim to its correct
459 * position in the tree.
460 */
461 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
462 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
463 !css_tryget(&mz->mem->css))
464 goto retry;
465done:
466 return mz;
467}
468
469static struct mem_cgroup_per_zone *
470mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
471{
472 struct mem_cgroup_per_zone *mz;
473
474 spin_lock(&mctz->lock);
475 mz = __mem_cgroup_largest_soft_limit_node(mctz);
476 spin_unlock(&mctz->lock);
477 return mz;
478}
479
0c3e73e8
BS
480static void mem_cgroup_swap_statistics(struct mem_cgroup *mem,
481 bool charge)
482{
483 int val = (charge) ? 1 : -1;
484 struct mem_cgroup_stat *stat = &mem->stat;
485 struct mem_cgroup_stat_cpu *cpustat;
486 int cpu = get_cpu();
487
488 cpustat = &stat->cpustat[cpu];
489 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_SWAPOUT, val);
490 put_cpu();
491}
492
c05555b5
KH
493static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
494 struct page_cgroup *pc,
495 bool charge)
d52aa412 496{
0c3e73e8 497 int val = (charge) ? 1 : -1;
d52aa412 498 struct mem_cgroup_stat *stat = &mem->stat;
addb9efe 499 struct mem_cgroup_stat_cpu *cpustat;
08e552c6 500 int cpu = get_cpu();
d52aa412 501
08e552c6 502 cpustat = &stat->cpustat[cpu];
c05555b5 503 if (PageCgroupCache(pc))
addb9efe 504 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
d52aa412 505 else
addb9efe 506 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
55e462b0
BR
507
508 if (charge)
addb9efe 509 __mem_cgroup_stat_add_safe(cpustat,
55e462b0
BR
510 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
511 else
addb9efe 512 __mem_cgroup_stat_add_safe(cpustat,
55e462b0 513 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
f64c3f54 514 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_EVENTS, 1);
08e552c6 515 put_cpu();
6d12e2d8
KH
516}
517
14067bb3 518static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
b69408e8 519 enum lru_list idx)
6d12e2d8
KH
520{
521 int nid, zid;
522 struct mem_cgroup_per_zone *mz;
523 u64 total = 0;
524
525 for_each_online_node(nid)
526 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
527 mz = mem_cgroup_zoneinfo(mem, nid, zid);
528 total += MEM_CGROUP_ZSTAT(mz, idx);
529 }
530 return total;
d52aa412
KH
531}
532
d5b69e38 533static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
8cdea7c0
BS
534{
535 return container_of(cgroup_subsys_state(cont,
536 mem_cgroup_subsys_id), struct mem_cgroup,
537 css);
538}
539
cf475ad2 540struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
78fb7466 541{
31a78f23
BS
542 /*
543 * mm_update_next_owner() may clear mm->owner to NULL
544 * if it races with swapoff, page migration, etc.
545 * So this can be called with p == NULL.
546 */
547 if (unlikely(!p))
548 return NULL;
549
78fb7466
PE
550 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
551 struct mem_cgroup, css);
552}
553
54595fe2
KH
554static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
555{
556 struct mem_cgroup *mem = NULL;
0b7f569e
KH
557
558 if (!mm)
559 return NULL;
54595fe2
KH
560 /*
561 * Because we have no locks, mm->owner's may be being moved to other
562 * cgroup. We use css_tryget() here even if this looks
563 * pessimistic (rather than adding locks here).
564 */
565 rcu_read_lock();
566 do {
567 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
568 if (unlikely(!mem))
569 break;
570 } while (!css_tryget(&mem->css));
571 rcu_read_unlock();
572 return mem;
573}
574
14067bb3
KH
575/*
576 * Call callback function against all cgroup under hierarchy tree.
577 */
578static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
579 int (*func)(struct mem_cgroup *, void *))
580{
581 int found, ret, nextid;
582 struct cgroup_subsys_state *css;
583 struct mem_cgroup *mem;
584
585 if (!root->use_hierarchy)
586 return (*func)(root, data);
587
588 nextid = 1;
589 do {
590 ret = 0;
591 mem = NULL;
592
593 rcu_read_lock();
594 css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
595 &found);
596 if (css && css_tryget(css))
597 mem = container_of(css, struct mem_cgroup, css);
598 rcu_read_unlock();
599
600 if (mem) {
601 ret = (*func)(mem, data);
602 css_put(&mem->css);
603 }
604 nextid = found + 1;
605 } while (!ret && css);
606
607 return ret;
608}
609
4b3bde4c
BS
610static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
611{
612 return (mem == root_mem_cgroup);
613}
614
08e552c6
KH
615/*
616 * Following LRU functions are allowed to be used without PCG_LOCK.
617 * Operations are called by routine of global LRU independently from memcg.
618 * What we have to take care of here is validness of pc->mem_cgroup.
619 *
620 * Changes to pc->mem_cgroup happens when
621 * 1. charge
622 * 2. moving account
623 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
624 * It is added to LRU before charge.
625 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
626 * When moving account, the page is not on LRU. It's isolated.
627 */
4f98a2fe 628
08e552c6
KH
629void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
630{
631 struct page_cgroup *pc;
08e552c6 632 struct mem_cgroup_per_zone *mz;
6d12e2d8 633
f8d66542 634 if (mem_cgroup_disabled())
08e552c6
KH
635 return;
636 pc = lookup_page_cgroup(page);
637 /* can happen while we handle swapcache. */
4b3bde4c 638 if (!TestClearPageCgroupAcctLRU(pc))
08e552c6 639 return;
4b3bde4c 640 VM_BUG_ON(!pc->mem_cgroup);
544122e5
KH
641 /*
642 * We don't check PCG_USED bit. It's cleared when the "page" is finally
643 * removed from global LRU.
644 */
08e552c6 645 mz = page_cgroup_zoneinfo(pc);
b69408e8 646 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
4b3bde4c
BS
647 if (mem_cgroup_is_root(pc->mem_cgroup))
648 return;
649 VM_BUG_ON(list_empty(&pc->lru));
08e552c6
KH
650 list_del_init(&pc->lru);
651 return;
6d12e2d8
KH
652}
653
08e552c6 654void mem_cgroup_del_lru(struct page *page)
6d12e2d8 655{
08e552c6
KH
656 mem_cgroup_del_lru_list(page, page_lru(page));
657}
b69408e8 658
08e552c6
KH
659void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
660{
661 struct mem_cgroup_per_zone *mz;
662 struct page_cgroup *pc;
b69408e8 663
f8d66542 664 if (mem_cgroup_disabled())
08e552c6 665 return;
6d12e2d8 666
08e552c6 667 pc = lookup_page_cgroup(page);
bd112db8
DN
668 /*
669 * Used bit is set without atomic ops but after smp_wmb().
670 * For making pc->mem_cgroup visible, insert smp_rmb() here.
671 */
08e552c6 672 smp_rmb();
4b3bde4c
BS
673 /* unused or root page is not rotated. */
674 if (!PageCgroupUsed(pc) || mem_cgroup_is_root(pc->mem_cgroup))
08e552c6
KH
675 return;
676 mz = page_cgroup_zoneinfo(pc);
677 list_move(&pc->lru, &mz->lists[lru]);
6d12e2d8
KH
678}
679
08e552c6 680void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
66e1707b 681{
08e552c6
KH
682 struct page_cgroup *pc;
683 struct mem_cgroup_per_zone *mz;
6d12e2d8 684
f8d66542 685 if (mem_cgroup_disabled())
08e552c6
KH
686 return;
687 pc = lookup_page_cgroup(page);
4b3bde4c 688 VM_BUG_ON(PageCgroupAcctLRU(pc));
bd112db8
DN
689 /*
690 * Used bit is set without atomic ops but after smp_wmb().
691 * For making pc->mem_cgroup visible, insert smp_rmb() here.
692 */
08e552c6
KH
693 smp_rmb();
694 if (!PageCgroupUsed(pc))
894bc310 695 return;
b69408e8 696
08e552c6 697 mz = page_cgroup_zoneinfo(pc);
b69408e8 698 MEM_CGROUP_ZSTAT(mz, lru) += 1;
4b3bde4c
BS
699 SetPageCgroupAcctLRU(pc);
700 if (mem_cgroup_is_root(pc->mem_cgroup))
701 return;
08e552c6
KH
702 list_add(&pc->lru, &mz->lists[lru]);
703}
544122e5 704
08e552c6 705/*
544122e5
KH
706 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
707 * lru because the page may.be reused after it's fully uncharged (because of
708 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
709 * it again. This function is only used to charge SwapCache. It's done under
710 * lock_page and expected that zone->lru_lock is never held.
08e552c6 711 */
544122e5 712static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
08e552c6 713{
544122e5
KH
714 unsigned long flags;
715 struct zone *zone = page_zone(page);
716 struct page_cgroup *pc = lookup_page_cgroup(page);
717
718 spin_lock_irqsave(&zone->lru_lock, flags);
719 /*
720 * Forget old LRU when this page_cgroup is *not* used. This Used bit
721 * is guarded by lock_page() because the page is SwapCache.
722 */
723 if (!PageCgroupUsed(pc))
724 mem_cgroup_del_lru_list(page, page_lru(page));
725 spin_unlock_irqrestore(&zone->lru_lock, flags);
08e552c6
KH
726}
727
544122e5
KH
728static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
729{
730 unsigned long flags;
731 struct zone *zone = page_zone(page);
732 struct page_cgroup *pc = lookup_page_cgroup(page);
733
734 spin_lock_irqsave(&zone->lru_lock, flags);
735 /* link when the page is linked to LRU but page_cgroup isn't */
4b3bde4c 736 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
544122e5
KH
737 mem_cgroup_add_lru_list(page, page_lru(page));
738 spin_unlock_irqrestore(&zone->lru_lock, flags);
739}
740
741
08e552c6
KH
742void mem_cgroup_move_lists(struct page *page,
743 enum lru_list from, enum lru_list to)
744{
f8d66542 745 if (mem_cgroup_disabled())
08e552c6
KH
746 return;
747 mem_cgroup_del_lru_list(page, from);
748 mem_cgroup_add_lru_list(page, to);
66e1707b
BS
749}
750
4c4a2214
DR
751int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
752{
753 int ret;
0b7f569e 754 struct mem_cgroup *curr = NULL;
4c4a2214
DR
755
756 task_lock(task);
0b7f569e
KH
757 rcu_read_lock();
758 curr = try_get_mem_cgroup_from_mm(task->mm);
759 rcu_read_unlock();
4c4a2214 760 task_unlock(task);
0b7f569e
KH
761 if (!curr)
762 return 0;
763 if (curr->use_hierarchy)
764 ret = css_is_ancestor(&curr->css, &mem->css);
765 else
766 ret = (curr == mem);
767 css_put(&curr->css);
4c4a2214
DR
768 return ret;
769}
770
6c48a1d0
KH
771/*
772 * prev_priority control...this will be used in memory reclaim path.
773 */
774int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
775{
2733c06a
KM
776 int prev_priority;
777
778 spin_lock(&mem->reclaim_param_lock);
779 prev_priority = mem->prev_priority;
780 spin_unlock(&mem->reclaim_param_lock);
781
782 return prev_priority;
6c48a1d0
KH
783}
784
785void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
786{
2733c06a 787 spin_lock(&mem->reclaim_param_lock);
6c48a1d0
KH
788 if (priority < mem->prev_priority)
789 mem->prev_priority = priority;
2733c06a 790 spin_unlock(&mem->reclaim_param_lock);
6c48a1d0
KH
791}
792
793void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
794{
2733c06a 795 spin_lock(&mem->reclaim_param_lock);
6c48a1d0 796 mem->prev_priority = priority;
2733c06a 797 spin_unlock(&mem->reclaim_param_lock);
6c48a1d0
KH
798}
799
c772be93 800static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
14797e23
KM
801{
802 unsigned long active;
803 unsigned long inactive;
c772be93
KM
804 unsigned long gb;
805 unsigned long inactive_ratio;
14797e23 806
14067bb3
KH
807 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
808 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
14797e23 809
c772be93
KM
810 gb = (inactive + active) >> (30 - PAGE_SHIFT);
811 if (gb)
812 inactive_ratio = int_sqrt(10 * gb);
813 else
814 inactive_ratio = 1;
815
816 if (present_pages) {
817 present_pages[0] = inactive;
818 present_pages[1] = active;
819 }
820
821 return inactive_ratio;
822}
823
824int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
825{
826 unsigned long active;
827 unsigned long inactive;
828 unsigned long present_pages[2];
829 unsigned long inactive_ratio;
830
831 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
832
833 inactive = present_pages[0];
834 active = present_pages[1];
835
836 if (inactive * inactive_ratio < active)
14797e23
KM
837 return 1;
838
839 return 0;
840}
841
56e49d21
RR
842int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
843{
844 unsigned long active;
845 unsigned long inactive;
846
847 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
848 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
849
850 return (active > inactive);
851}
852
a3d8e054
KM
853unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
854 struct zone *zone,
855 enum lru_list lru)
856{
857 int nid = zone->zone_pgdat->node_id;
858 int zid = zone_idx(zone);
859 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
860
861 return MEM_CGROUP_ZSTAT(mz, lru);
862}
863
3e2f41f1
KM
864struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
865 struct zone *zone)
866{
867 int nid = zone->zone_pgdat->node_id;
868 int zid = zone_idx(zone);
869 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
870
871 return &mz->reclaim_stat;
872}
873
874struct zone_reclaim_stat *
875mem_cgroup_get_reclaim_stat_from_page(struct page *page)
876{
877 struct page_cgroup *pc;
878 struct mem_cgroup_per_zone *mz;
879
880 if (mem_cgroup_disabled())
881 return NULL;
882
883 pc = lookup_page_cgroup(page);
bd112db8
DN
884 /*
885 * Used bit is set without atomic ops but after smp_wmb().
886 * For making pc->mem_cgroup visible, insert smp_rmb() here.
887 */
888 smp_rmb();
889 if (!PageCgroupUsed(pc))
890 return NULL;
891
3e2f41f1
KM
892 mz = page_cgroup_zoneinfo(pc);
893 if (!mz)
894 return NULL;
895
896 return &mz->reclaim_stat;
897}
898
66e1707b
BS
899unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
900 struct list_head *dst,
901 unsigned long *scanned, int order,
902 int mode, struct zone *z,
903 struct mem_cgroup *mem_cont,
4f98a2fe 904 int active, int file)
66e1707b
BS
905{
906 unsigned long nr_taken = 0;
907 struct page *page;
908 unsigned long scan;
909 LIST_HEAD(pc_list);
910 struct list_head *src;
ff7283fa 911 struct page_cgroup *pc, *tmp;
1ecaab2b
KH
912 int nid = z->zone_pgdat->node_id;
913 int zid = zone_idx(z);
914 struct mem_cgroup_per_zone *mz;
b7c46d15 915 int lru = LRU_FILE * file + active;
2ffebca6 916 int ret;
66e1707b 917
cf475ad2 918 BUG_ON(!mem_cont);
1ecaab2b 919 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
b69408e8 920 src = &mz->lists[lru];
66e1707b 921
ff7283fa
KH
922 scan = 0;
923 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
436c6541 924 if (scan >= nr_to_scan)
ff7283fa 925 break;
08e552c6
KH
926
927 page = pc->page;
52d4b9ac
KH
928 if (unlikely(!PageCgroupUsed(pc)))
929 continue;
436c6541 930 if (unlikely(!PageLRU(page)))
ff7283fa 931 continue;
ff7283fa 932
436c6541 933 scan++;
2ffebca6
KH
934 ret = __isolate_lru_page(page, mode, file);
935 switch (ret) {
936 case 0:
66e1707b 937 list_move(&page->lru, dst);
2ffebca6 938 mem_cgroup_del_lru(page);
66e1707b 939 nr_taken++;
2ffebca6
KH
940 break;
941 case -EBUSY:
942 /* we don't affect global LRU but rotate in our LRU */
943 mem_cgroup_rotate_lru_list(page, page_lru(page));
944 break;
945 default:
946 break;
66e1707b
BS
947 }
948 }
949
66e1707b
BS
950 *scanned = scan;
951 return nr_taken;
952}
953
6d61ef40
BS
954#define mem_cgroup_from_res_counter(counter, member) \
955 container_of(counter, struct mem_cgroup, member)
956
b85a96c0
DN
957static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
958{
959 if (do_swap_account) {
960 if (res_counter_check_under_limit(&mem->res) &&
961 res_counter_check_under_limit(&mem->memsw))
962 return true;
963 } else
964 if (res_counter_check_under_limit(&mem->res))
965 return true;
966 return false;
967}
968
a7885eb8
KM
969static unsigned int get_swappiness(struct mem_cgroup *memcg)
970{
971 struct cgroup *cgrp = memcg->css.cgroup;
972 unsigned int swappiness;
973
974 /* root ? */
975 if (cgrp->parent == NULL)
976 return vm_swappiness;
977
978 spin_lock(&memcg->reclaim_param_lock);
979 swappiness = memcg->swappiness;
980 spin_unlock(&memcg->reclaim_param_lock);
981
982 return swappiness;
983}
984
81d39c20
KH
985static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
986{
987 int *val = data;
988 (*val)++;
989 return 0;
990}
e222432b
BS
991
992/**
993 * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode.
994 * @memcg: The memory cgroup that went over limit
995 * @p: Task that is going to be killed
996 *
997 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
998 * enabled
999 */
1000void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1001{
1002 struct cgroup *task_cgrp;
1003 struct cgroup *mem_cgrp;
1004 /*
1005 * Need a buffer in BSS, can't rely on allocations. The code relies
1006 * on the assumption that OOM is serialized for memory controller.
1007 * If this assumption is broken, revisit this code.
1008 */
1009 static char memcg_name[PATH_MAX];
1010 int ret;
1011
1012 if (!memcg)
1013 return;
1014
1015
1016 rcu_read_lock();
1017
1018 mem_cgrp = memcg->css.cgroup;
1019 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1020
1021 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1022 if (ret < 0) {
1023 /*
1024 * Unfortunately, we are unable to convert to a useful name
1025 * But we'll still print out the usage information
1026 */
1027 rcu_read_unlock();
1028 goto done;
1029 }
1030 rcu_read_unlock();
1031
1032 printk(KERN_INFO "Task in %s killed", memcg_name);
1033
1034 rcu_read_lock();
1035 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1036 if (ret < 0) {
1037 rcu_read_unlock();
1038 goto done;
1039 }
1040 rcu_read_unlock();
1041
1042 /*
1043 * Continues from above, so we don't need an KERN_ level
1044 */
1045 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1046done:
1047
1048 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1049 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1050 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1051 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1052 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1053 "failcnt %llu\n",
1054 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1055 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1056 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1057}
1058
81d39c20
KH
1059/*
1060 * This function returns the number of memcg under hierarchy tree. Returns
1061 * 1(self count) if no children.
1062 */
1063static int mem_cgroup_count_children(struct mem_cgroup *mem)
1064{
1065 int num = 0;
1066 mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
1067 return num;
1068}
1069
6d61ef40 1070/*
04046e1a
KH
1071 * Visit the first child (need not be the first child as per the ordering
1072 * of the cgroup list, since we track last_scanned_child) of @mem and use
1073 * that to reclaim free pages from.
1074 */
1075static struct mem_cgroup *
1076mem_cgroup_select_victim(struct mem_cgroup *root_mem)
1077{
1078 struct mem_cgroup *ret = NULL;
1079 struct cgroup_subsys_state *css;
1080 int nextid, found;
1081
1082 if (!root_mem->use_hierarchy) {
1083 css_get(&root_mem->css);
1084 ret = root_mem;
1085 }
1086
1087 while (!ret) {
1088 rcu_read_lock();
1089 nextid = root_mem->last_scanned_child + 1;
1090 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
1091 &found);
1092 if (css && css_tryget(css))
1093 ret = container_of(css, struct mem_cgroup, css);
1094
1095 rcu_read_unlock();
1096 /* Updates scanning parameter */
1097 spin_lock(&root_mem->reclaim_param_lock);
1098 if (!css) {
1099 /* this means start scan from ID:1 */
1100 root_mem->last_scanned_child = 0;
1101 } else
1102 root_mem->last_scanned_child = found;
1103 spin_unlock(&root_mem->reclaim_param_lock);
1104 }
1105
1106 return ret;
1107}
1108
1109/*
1110 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1111 * we reclaimed from, so that we don't end up penalizing one child extensively
1112 * based on its position in the children list.
6d61ef40
BS
1113 *
1114 * root_mem is the original ancestor that we've been reclaim from.
04046e1a
KH
1115 *
1116 * We give up and return to the caller when we visit root_mem twice.
1117 * (other groups can be removed while we're walking....)
81d39c20
KH
1118 *
1119 * If shrink==true, for avoiding to free too much, this returns immedieately.
6d61ef40
BS
1120 */
1121static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
4e416953 1122 struct zone *zone,
75822b44
BS
1123 gfp_t gfp_mask,
1124 unsigned long reclaim_options)
6d61ef40 1125{
04046e1a
KH
1126 struct mem_cgroup *victim;
1127 int ret, total = 0;
1128 int loop = 0;
75822b44
BS
1129 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1130 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
4e416953
BS
1131 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
1132 unsigned long excess = mem_cgroup_get_excess(root_mem);
04046e1a 1133
22a668d7
KH
1134 /* If memsw_is_minimum==1, swap-out is of-no-use. */
1135 if (root_mem->memsw_is_minimum)
1136 noswap = true;
1137
4e416953 1138 while (1) {
04046e1a 1139 victim = mem_cgroup_select_victim(root_mem);
4e416953 1140 if (victim == root_mem) {
04046e1a 1141 loop++;
cdec2e42
KH
1142 if (loop >= 1)
1143 drain_all_stock_async();
4e416953
BS
1144 if (loop >= 2) {
1145 /*
1146 * If we have not been able to reclaim
1147 * anything, it might because there are
1148 * no reclaimable pages under this hierarchy
1149 */
1150 if (!check_soft || !total) {
1151 css_put(&victim->css);
1152 break;
1153 }
1154 /*
1155 * We want to do more targetted reclaim.
1156 * excess >> 2 is not to excessive so as to
1157 * reclaim too much, nor too less that we keep
1158 * coming back to reclaim from this cgroup
1159 */
1160 if (total >= (excess >> 2) ||
1161 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1162 css_put(&victim->css);
1163 break;
1164 }
1165 }
1166 }
04046e1a
KH
1167 if (!mem_cgroup_local_usage(&victim->stat)) {
1168 /* this cgroup's local usage == 0 */
1169 css_put(&victim->css);
6d61ef40
BS
1170 continue;
1171 }
04046e1a 1172 /* we use swappiness of local cgroup */
4e416953
BS
1173 if (check_soft)
1174 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
1175 noswap, get_swappiness(victim), zone,
1176 zone->zone_pgdat->node_id);
1177 else
1178 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
1179 noswap, get_swappiness(victim));
04046e1a 1180 css_put(&victim->css);
81d39c20
KH
1181 /*
1182 * At shrinking usage, we can't check we should stop here or
1183 * reclaim more. It's depends on callers. last_scanned_child
1184 * will work enough for keeping fairness under tree.
1185 */
1186 if (shrink)
1187 return ret;
04046e1a 1188 total += ret;
4e416953
BS
1189 if (check_soft) {
1190 if (res_counter_check_under_soft_limit(&root_mem->res))
1191 return total;
1192 } else if (mem_cgroup_check_under_limit(root_mem))
04046e1a 1193 return 1 + total;
6d61ef40 1194 }
04046e1a 1195 return total;
6d61ef40
BS
1196}
1197
a636b327
KH
1198bool mem_cgroup_oom_called(struct task_struct *task)
1199{
1200 bool ret = false;
1201 struct mem_cgroup *mem;
1202 struct mm_struct *mm;
1203
1204 rcu_read_lock();
1205 mm = task->mm;
1206 if (!mm)
1207 mm = &init_mm;
1208 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
1209 if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
1210 ret = true;
1211 rcu_read_unlock();
1212 return ret;
1213}
0b7f569e
KH
1214
1215static int record_last_oom_cb(struct mem_cgroup *mem, void *data)
1216{
1217 mem->last_oom_jiffies = jiffies;
1218 return 0;
1219}
1220
1221static void record_last_oom(struct mem_cgroup *mem)
1222{
1223 mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb);
1224}
1225
d69b042f
BS
1226/*
1227 * Currently used to update mapped file statistics, but the routine can be
1228 * generalized to update other statistics as well.
1229 */
1230void mem_cgroup_update_mapped_file_stat(struct page *page, int val)
1231{
1232 struct mem_cgroup *mem;
1233 struct mem_cgroup_stat *stat;
1234 struct mem_cgroup_stat_cpu *cpustat;
1235 int cpu;
1236 struct page_cgroup *pc;
1237
1238 if (!page_is_file_cache(page))
1239 return;
1240
1241 pc = lookup_page_cgroup(page);
1242 if (unlikely(!pc))
1243 return;
1244
1245 lock_page_cgroup(pc);
1246 mem = pc->mem_cgroup;
1247 if (!mem)
1248 goto done;
1249
1250 if (!PageCgroupUsed(pc))
1251 goto done;
1252
1253 /*
1254 * Preemption is already disabled, we don't need get_cpu()
1255 */
1256 cpu = smp_processor_id();
1257 stat = &mem->stat;
1258 cpustat = &stat->cpustat[cpu];
1259
1260 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE, val);
1261done:
1262 unlock_page_cgroup(pc);
1263}
0b7f569e 1264
cdec2e42
KH
1265/*
1266 * size of first charge trial. "32" comes from vmscan.c's magic value.
1267 * TODO: maybe necessary to use big numbers in big irons.
1268 */
1269#define CHARGE_SIZE (32 * PAGE_SIZE)
1270struct memcg_stock_pcp {
1271 struct mem_cgroup *cached; /* this never be root cgroup */
1272 int charge;
1273 struct work_struct work;
1274};
1275static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
1276static atomic_t memcg_drain_count;
1277
1278/*
1279 * Try to consume stocked charge on this cpu. If success, PAGE_SIZE is consumed
1280 * from local stock and true is returned. If the stock is 0 or charges from a
1281 * cgroup which is not current target, returns false. This stock will be
1282 * refilled.
1283 */
1284static bool consume_stock(struct mem_cgroup *mem)
1285{
1286 struct memcg_stock_pcp *stock;
1287 bool ret = true;
1288
1289 stock = &get_cpu_var(memcg_stock);
1290 if (mem == stock->cached && stock->charge)
1291 stock->charge -= PAGE_SIZE;
1292 else /* need to call res_counter_charge */
1293 ret = false;
1294 put_cpu_var(memcg_stock);
1295 return ret;
1296}
1297
1298/*
1299 * Returns stocks cached in percpu to res_counter and reset cached information.
1300 */
1301static void drain_stock(struct memcg_stock_pcp *stock)
1302{
1303 struct mem_cgroup *old = stock->cached;
1304
1305 if (stock->charge) {
1306 res_counter_uncharge(&old->res, stock->charge);
1307 if (do_swap_account)
1308 res_counter_uncharge(&old->memsw, stock->charge);
1309 }
1310 stock->cached = NULL;
1311 stock->charge = 0;
1312}
1313
1314/*
1315 * This must be called under preempt disabled or must be called by
1316 * a thread which is pinned to local cpu.
1317 */
1318static void drain_local_stock(struct work_struct *dummy)
1319{
1320 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
1321 drain_stock(stock);
1322}
1323
1324/*
1325 * Cache charges(val) which is from res_counter, to local per_cpu area.
1326 * This will be consumed by consumt_stock() function, later.
1327 */
1328static void refill_stock(struct mem_cgroup *mem, int val)
1329{
1330 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
1331
1332 if (stock->cached != mem) { /* reset if necessary */
1333 drain_stock(stock);
1334 stock->cached = mem;
1335 }
1336 stock->charge += val;
1337 put_cpu_var(memcg_stock);
1338}
1339
1340/*
1341 * Tries to drain stocked charges in other cpus. This function is asynchronous
1342 * and just put a work per cpu for draining localy on each cpu. Caller can
1343 * expects some charges will be back to res_counter later but cannot wait for
1344 * it.
1345 */
1346static void drain_all_stock_async(void)
1347{
1348 int cpu;
1349 /* This function is for scheduling "drain" in asynchronous way.
1350 * The result of "drain" is not directly handled by callers. Then,
1351 * if someone is calling drain, we don't have to call drain more.
1352 * Anyway, WORK_STRUCT_PENDING check in queue_work_on() will catch if
1353 * there is a race. We just do loose check here.
1354 */
1355 if (atomic_read(&memcg_drain_count))
1356 return;
1357 /* Notify other cpus that system-wide "drain" is running */
1358 atomic_inc(&memcg_drain_count);
1359 get_online_cpus();
1360 for_each_online_cpu(cpu) {
1361 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
1362 schedule_work_on(cpu, &stock->work);
1363 }
1364 put_online_cpus();
1365 atomic_dec(&memcg_drain_count);
1366 /* We don't wait for flush_work */
1367}
1368
1369/* This is a synchronous drain interface. */
1370static void drain_all_stock_sync(void)
1371{
1372 /* called when force_empty is called */
1373 atomic_inc(&memcg_drain_count);
1374 schedule_on_each_cpu(drain_local_stock);
1375 atomic_dec(&memcg_drain_count);
1376}
1377
1378static int __cpuinit memcg_stock_cpu_callback(struct notifier_block *nb,
1379 unsigned long action,
1380 void *hcpu)
1381{
1382 int cpu = (unsigned long)hcpu;
1383 struct memcg_stock_pcp *stock;
1384
1385 if (action != CPU_DEAD)
1386 return NOTIFY_OK;
1387 stock = &per_cpu(memcg_stock, cpu);
1388 drain_stock(stock);
1389 return NOTIFY_OK;
1390}
1391
f817ed48
KH
1392/*
1393 * Unlike exported interface, "oom" parameter is added. if oom==true,
1394 * oom-killer can be invoked.
8a9f3ccd 1395 */
f817ed48 1396static int __mem_cgroup_try_charge(struct mm_struct *mm,
8c7c6e34 1397 gfp_t gfp_mask, struct mem_cgroup **memcg,
f64c3f54 1398 bool oom, struct page *page)
8a9f3ccd 1399{
4e649152 1400 struct mem_cgroup *mem, *mem_over_limit;
7a81b88c 1401 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
4e649152 1402 struct res_counter *fail_res;
cdec2e42 1403 int csize = CHARGE_SIZE;
a636b327
KH
1404
1405 if (unlikely(test_thread_flag(TIF_MEMDIE))) {
1406 /* Don't account this! */
1407 *memcg = NULL;
1408 return 0;
1409 }
1410
8a9f3ccd 1411 /*
3be91277
HD
1412 * We always charge the cgroup the mm_struct belongs to.
1413 * The mm_struct's mem_cgroup changes on task migration if the
8a9f3ccd
BS
1414 * thread group leader migrates. It's possible that mm is not
1415 * set, if so charge the init_mm (happens for pagecache usage).
1416 */
54595fe2
KH
1417 mem = *memcg;
1418 if (likely(!mem)) {
1419 mem = try_get_mem_cgroup_from_mm(mm);
7a81b88c 1420 *memcg = mem;
e8589cc1 1421 } else {
7a81b88c 1422 css_get(&mem->css);
e8589cc1 1423 }
54595fe2
KH
1424 if (unlikely(!mem))
1425 return 0;
1426
46f7e602 1427 VM_BUG_ON(css_is_removed(&mem->css));
cdec2e42
KH
1428 if (mem_cgroup_is_root(mem))
1429 goto done;
8a9f3ccd 1430
8c7c6e34 1431 while (1) {
0c3e73e8 1432 int ret = 0;
75822b44 1433 unsigned long flags = 0;
7a81b88c 1434
cdec2e42
KH
1435 if (consume_stock(mem))
1436 goto charged;
1437
1438 ret = res_counter_charge(&mem->res, csize, &fail_res);
8c7c6e34
KH
1439 if (likely(!ret)) {
1440 if (!do_swap_account)
1441 break;
cdec2e42 1442 ret = res_counter_charge(&mem->memsw, csize, &fail_res);
8c7c6e34
KH
1443 if (likely(!ret))
1444 break;
1445 /* mem+swap counter fails */
cdec2e42 1446 res_counter_uncharge(&mem->res, csize);
75822b44 1447 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
6d61ef40
BS
1448 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1449 memsw);
1450 } else
1451 /* mem counter fails */
1452 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1453 res);
1454
cdec2e42
KH
1455 /* reduce request size and retry */
1456 if (csize > PAGE_SIZE) {
1457 csize = PAGE_SIZE;
1458 continue;
1459 }
3be91277 1460 if (!(gfp_mask & __GFP_WAIT))
7a81b88c 1461 goto nomem;
e1a1cd59 1462
4e416953
BS
1463 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
1464 gfp_mask, flags);
4d1c6273
DN
1465 if (ret)
1466 continue;
66e1707b
BS
1467
1468 /*
8869b8f6
HD
1469 * try_to_free_mem_cgroup_pages() might not give us a full
1470 * picture of reclaim. Some pages are reclaimed and might be
1471 * moved to swap cache or just unmapped from the cgroup.
1472 * Check the limit again to see if the reclaim reduced the
1473 * current usage of the cgroup before giving up
8c7c6e34 1474 *
8869b8f6 1475 */
b85a96c0
DN
1476 if (mem_cgroup_check_under_limit(mem_over_limit))
1477 continue;
3be91277
HD
1478
1479 if (!nr_retries--) {
a636b327 1480 if (oom) {
7f4d454d 1481 mutex_lock(&memcg_tasklist);
88700756 1482 mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
7f4d454d 1483 mutex_unlock(&memcg_tasklist);
0b7f569e 1484 record_last_oom(mem_over_limit);
a636b327 1485 }
7a81b88c 1486 goto nomem;
66e1707b 1487 }
8a9f3ccd 1488 }
cdec2e42
KH
1489 if (csize > PAGE_SIZE)
1490 refill_stock(mem, csize - PAGE_SIZE);
1491charged:
f64c3f54 1492 /*
4e649152
KH
1493 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
1494 * if they exceeds softlimit.
f64c3f54 1495 */
4e649152
KH
1496 if (mem_cgroup_soft_limit_check(mem))
1497 mem_cgroup_update_tree(mem, page);
0c3e73e8 1498done:
7a81b88c
KH
1499 return 0;
1500nomem:
1501 css_put(&mem->css);
1502 return -ENOMEM;
1503}
8a9f3ccd 1504
a3b2d692
KH
1505/*
1506 * A helper function to get mem_cgroup from ID. must be called under
1507 * rcu_read_lock(). The caller must check css_is_removed() or some if
1508 * it's concern. (dropping refcnt from swap can be called against removed
1509 * memcg.)
1510 */
1511static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
1512{
1513 struct cgroup_subsys_state *css;
1514
1515 /* ID 0 is unused ID */
1516 if (!id)
1517 return NULL;
1518 css = css_lookup(&mem_cgroup_subsys, id);
1519 if (!css)
1520 return NULL;
1521 return container_of(css, struct mem_cgroup, css);
1522}
1523
b5a84319
KH
1524static struct mem_cgroup *try_get_mem_cgroup_from_swapcache(struct page *page)
1525{
1526 struct mem_cgroup *mem;
3c776e64 1527 struct page_cgroup *pc;
a3b2d692 1528 unsigned short id;
b5a84319
KH
1529 swp_entry_t ent;
1530
3c776e64
DN
1531 VM_BUG_ON(!PageLocked(page));
1532
b5a84319
KH
1533 if (!PageSwapCache(page))
1534 return NULL;
1535
3c776e64 1536 pc = lookup_page_cgroup(page);
c0bd3f63 1537 lock_page_cgroup(pc);
a3b2d692 1538 if (PageCgroupUsed(pc)) {
3c776e64 1539 mem = pc->mem_cgroup;
a3b2d692
KH
1540 if (mem && !css_tryget(&mem->css))
1541 mem = NULL;
1542 } else {
3c776e64 1543 ent.val = page_private(page);
a3b2d692
KH
1544 id = lookup_swap_cgroup(ent);
1545 rcu_read_lock();
1546 mem = mem_cgroup_lookup(id);
1547 if (mem && !css_tryget(&mem->css))
1548 mem = NULL;
1549 rcu_read_unlock();
3c776e64 1550 }
c0bd3f63 1551 unlock_page_cgroup(pc);
b5a84319
KH
1552 return mem;
1553}
1554
7a81b88c 1555/*
a5e924f5 1556 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
7a81b88c
KH
1557 * USED state. If already USED, uncharge and return.
1558 */
1559
1560static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1561 struct page_cgroup *pc,
1562 enum charge_type ctype)
1563{
7a81b88c
KH
1564 /* try_charge() can return NULL to *memcg, taking care of it. */
1565 if (!mem)
1566 return;
52d4b9ac
KH
1567
1568 lock_page_cgroup(pc);
1569 if (unlikely(PageCgroupUsed(pc))) {
1570 unlock_page_cgroup(pc);
0c3e73e8 1571 if (!mem_cgroup_is_root(mem)) {
4e649152 1572 res_counter_uncharge(&mem->res, PAGE_SIZE);
0c3e73e8 1573 if (do_swap_account)
4e649152 1574 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
0c3e73e8 1575 }
52d4b9ac 1576 css_put(&mem->css);
7a81b88c 1577 return;
52d4b9ac 1578 }
4b3bde4c 1579
8a9f3ccd 1580 pc->mem_cgroup = mem;
261fb61a
KH
1581 /*
1582 * We access a page_cgroup asynchronously without lock_page_cgroup().
1583 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
1584 * is accessed after testing USED bit. To make pc->mem_cgroup visible
1585 * before USED bit, we need memory barrier here.
1586 * See mem_cgroup_add_lru_list(), etc.
1587 */
08e552c6 1588 smp_wmb();
4b3bde4c
BS
1589 switch (ctype) {
1590 case MEM_CGROUP_CHARGE_TYPE_CACHE:
1591 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
1592 SetPageCgroupCache(pc);
1593 SetPageCgroupUsed(pc);
1594 break;
1595 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1596 ClearPageCgroupCache(pc);
1597 SetPageCgroupUsed(pc);
1598 break;
1599 default:
1600 break;
1601 }
3be91277 1602
08e552c6 1603 mem_cgroup_charge_statistics(mem, pc, true);
52d4b9ac 1604
52d4b9ac 1605 unlock_page_cgroup(pc);
7a81b88c 1606}
66e1707b 1607
f817ed48
KH
1608/**
1609 * mem_cgroup_move_account - move account of the page
1610 * @pc: page_cgroup of the page.
1611 * @from: mem_cgroup which the page is moved from.
1612 * @to: mem_cgroup which the page is moved to. @from != @to.
1613 *
1614 * The caller must confirm following.
08e552c6 1615 * - page is not on LRU (isolate_page() is useful.)
f817ed48
KH
1616 *
1617 * returns 0 at success,
1618 * returns -EBUSY when lock is busy or "pc" is unstable.
1619 *
1620 * This function does "uncharge" from old cgroup but doesn't do "charge" to
1621 * new cgroup. It should be done by a caller.
1622 */
1623
1624static int mem_cgroup_move_account(struct page_cgroup *pc,
1625 struct mem_cgroup *from, struct mem_cgroup *to)
1626{
1627 struct mem_cgroup_per_zone *from_mz, *to_mz;
1628 int nid, zid;
1629 int ret = -EBUSY;
d69b042f
BS
1630 struct page *page;
1631 int cpu;
1632 struct mem_cgroup_stat *stat;
1633 struct mem_cgroup_stat_cpu *cpustat;
f817ed48 1634
f817ed48 1635 VM_BUG_ON(from == to);
08e552c6 1636 VM_BUG_ON(PageLRU(pc->page));
f817ed48
KH
1637
1638 nid = page_cgroup_nid(pc);
1639 zid = page_cgroup_zid(pc);
1640 from_mz = mem_cgroup_zoneinfo(from, nid, zid);
1641 to_mz = mem_cgroup_zoneinfo(to, nid, zid);
1642
f817ed48
KH
1643 if (!trylock_page_cgroup(pc))
1644 return ret;
1645
1646 if (!PageCgroupUsed(pc))
1647 goto out;
1648
1649 if (pc->mem_cgroup != from)
1650 goto out;
1651
0c3e73e8 1652 if (!mem_cgroup_is_root(from))
4e649152 1653 res_counter_uncharge(&from->res, PAGE_SIZE);
08e552c6 1654 mem_cgroup_charge_statistics(from, pc, false);
d69b042f
BS
1655
1656 page = pc->page;
1657 if (page_is_file_cache(page) && page_mapped(page)) {
1658 cpu = smp_processor_id();
1659 /* Update mapped_file data for mem_cgroup "from" */
1660 stat = &from->stat;
1661 cpustat = &stat->cpustat[cpu];
1662 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1663 -1);
1664
1665 /* Update mapped_file data for mem_cgroup "to" */
1666 stat = &to->stat;
1667 cpustat = &stat->cpustat[cpu];
1668 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1669 1);
1670 }
1671
0c3e73e8 1672 if (do_swap_account && !mem_cgroup_is_root(from))
4e649152 1673 res_counter_uncharge(&from->memsw, PAGE_SIZE);
40d58138
DN
1674 css_put(&from->css);
1675
1676 css_get(&to->css);
08e552c6
KH
1677 pc->mem_cgroup = to;
1678 mem_cgroup_charge_statistics(to, pc, true);
08e552c6 1679 ret = 0;
f817ed48
KH
1680out:
1681 unlock_page_cgroup(pc);
88703267
KH
1682 /*
1683 * We charges against "to" which may not have any tasks. Then, "to"
1684 * can be under rmdir(). But in current implementation, caller of
1685 * this function is just force_empty() and it's garanteed that
1686 * "to" is never removed. So, we don't check rmdir status here.
1687 */
f817ed48
KH
1688 return ret;
1689}
1690
1691/*
1692 * move charges to its parent.
1693 */
1694
1695static int mem_cgroup_move_parent(struct page_cgroup *pc,
1696 struct mem_cgroup *child,
1697 gfp_t gfp_mask)
1698{
08e552c6 1699 struct page *page = pc->page;
f817ed48
KH
1700 struct cgroup *cg = child->css.cgroup;
1701 struct cgroup *pcg = cg->parent;
1702 struct mem_cgroup *parent;
f817ed48
KH
1703 int ret;
1704
1705 /* Is ROOT ? */
1706 if (!pcg)
1707 return -EINVAL;
1708
08e552c6 1709
f817ed48
KH
1710 parent = mem_cgroup_from_cont(pcg);
1711
08e552c6 1712
f64c3f54 1713 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false, page);
a636b327 1714 if (ret || !parent)
f817ed48
KH
1715 return ret;
1716
40d58138
DN
1717 if (!get_page_unless_zero(page)) {
1718 ret = -EBUSY;
1719 goto uncharge;
1720 }
08e552c6
KH
1721
1722 ret = isolate_lru_page(page);
1723
1724 if (ret)
1725 goto cancel;
f817ed48 1726
f817ed48 1727 ret = mem_cgroup_move_account(pc, child, parent);
f817ed48 1728
08e552c6
KH
1729 putback_lru_page(page);
1730 if (!ret) {
1731 put_page(page);
40d58138
DN
1732 /* drop extra refcnt by try_charge() */
1733 css_put(&parent->css);
08e552c6 1734 return 0;
8c7c6e34 1735 }
40d58138 1736
08e552c6 1737cancel:
40d58138
DN
1738 put_page(page);
1739uncharge:
1740 /* drop extra refcnt by try_charge() */
1741 css_put(&parent->css);
1742 /* uncharge if move fails */
0c3e73e8 1743 if (!mem_cgroup_is_root(parent)) {
4e649152 1744 res_counter_uncharge(&parent->res, PAGE_SIZE);
0c3e73e8 1745 if (do_swap_account)
4e649152 1746 res_counter_uncharge(&parent->memsw, PAGE_SIZE);
0c3e73e8 1747 }
f817ed48
KH
1748 return ret;
1749}
1750
7a81b88c
KH
1751/*
1752 * Charge the memory controller for page usage.
1753 * Return
1754 * 0 if the charge was successful
1755 * < 0 if the cgroup is over its limit
1756 */
1757static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
1758 gfp_t gfp_mask, enum charge_type ctype,
1759 struct mem_cgroup *memcg)
1760{
1761 struct mem_cgroup *mem;
1762 struct page_cgroup *pc;
1763 int ret;
1764
1765 pc = lookup_page_cgroup(page);
1766 /* can happen at boot */
1767 if (unlikely(!pc))
1768 return 0;
1769 prefetchw(pc);
1770
1771 mem = memcg;
f64c3f54 1772 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true, page);
a636b327 1773 if (ret || !mem)
7a81b88c
KH
1774 return ret;
1775
1776 __mem_cgroup_commit_charge(mem, pc, ctype);
8a9f3ccd 1777 return 0;
8a9f3ccd
BS
1778}
1779
7a81b88c
KH
1780int mem_cgroup_newpage_charge(struct page *page,
1781 struct mm_struct *mm, gfp_t gfp_mask)
217bc319 1782{
f8d66542 1783 if (mem_cgroup_disabled())
cede86ac 1784 return 0;
52d4b9ac
KH
1785 if (PageCompound(page))
1786 return 0;
69029cd5
KH
1787 /*
1788 * If already mapped, we don't have to account.
1789 * If page cache, page->mapping has address_space.
1790 * But page->mapping may have out-of-use anon_vma pointer,
1791 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
1792 * is NULL.
1793 */
1794 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
1795 return 0;
1796 if (unlikely(!mm))
1797 mm = &init_mm;
217bc319 1798 return mem_cgroup_charge_common(page, mm, gfp_mask,
e8589cc1 1799 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
217bc319
KH
1800}
1801
83aae4c7
DN
1802static void
1803__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1804 enum charge_type ctype);
1805
e1a1cd59
BS
1806int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
1807 gfp_t gfp_mask)
8697d331 1808{
b5a84319
KH
1809 struct mem_cgroup *mem = NULL;
1810 int ret;
1811
f8d66542 1812 if (mem_cgroup_disabled())
cede86ac 1813 return 0;
52d4b9ac
KH
1814 if (PageCompound(page))
1815 return 0;
accf163e
KH
1816 /*
1817 * Corner case handling. This is called from add_to_page_cache()
1818 * in usual. But some FS (shmem) precharges this page before calling it
1819 * and call add_to_page_cache() with GFP_NOWAIT.
1820 *
1821 * For GFP_NOWAIT case, the page may be pre-charged before calling
1822 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
1823 * charge twice. (It works but has to pay a bit larger cost.)
b5a84319
KH
1824 * And when the page is SwapCache, it should take swap information
1825 * into account. This is under lock_page() now.
accf163e
KH
1826 */
1827 if (!(gfp_mask & __GFP_WAIT)) {
1828 struct page_cgroup *pc;
1829
52d4b9ac
KH
1830
1831 pc = lookup_page_cgroup(page);
1832 if (!pc)
1833 return 0;
1834 lock_page_cgroup(pc);
1835 if (PageCgroupUsed(pc)) {
1836 unlock_page_cgroup(pc);
accf163e
KH
1837 return 0;
1838 }
52d4b9ac 1839 unlock_page_cgroup(pc);
accf163e
KH
1840 }
1841
b5a84319 1842 if (unlikely(!mm && !mem))
8697d331 1843 mm = &init_mm;
accf163e 1844
c05555b5
KH
1845 if (page_is_file_cache(page))
1846 return mem_cgroup_charge_common(page, mm, gfp_mask,
e8589cc1 1847 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
b5a84319 1848
83aae4c7
DN
1849 /* shmem */
1850 if (PageSwapCache(page)) {
1851 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1852 if (!ret)
1853 __mem_cgroup_commit_charge_swapin(page, mem,
1854 MEM_CGROUP_CHARGE_TYPE_SHMEM);
1855 } else
1856 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
1857 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
b5a84319 1858
b5a84319 1859 return ret;
e8589cc1
KH
1860}
1861
54595fe2
KH
1862/*
1863 * While swap-in, try_charge -> commit or cancel, the page is locked.
1864 * And when try_charge() successfully returns, one refcnt to memcg without
21ae2956 1865 * struct page_cgroup is acquired. This refcnt will be consumed by
54595fe2
KH
1866 * "commit()" or removed by "cancel()"
1867 */
8c7c6e34
KH
1868int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
1869 struct page *page,
1870 gfp_t mask, struct mem_cgroup **ptr)
1871{
1872 struct mem_cgroup *mem;
54595fe2 1873 int ret;
8c7c6e34 1874
f8d66542 1875 if (mem_cgroup_disabled())
8c7c6e34
KH
1876 return 0;
1877
1878 if (!do_swap_account)
1879 goto charge_cur_mm;
8c7c6e34
KH
1880 /*
1881 * A racing thread's fault, or swapoff, may have already updated
407f9c8b
HD
1882 * the pte, and even removed page from swap cache: in those cases
1883 * do_swap_page()'s pte_same() test will fail; but there's also a
1884 * KSM case which does need to charge the page.
8c7c6e34
KH
1885 */
1886 if (!PageSwapCache(page))
407f9c8b 1887 goto charge_cur_mm;
b5a84319 1888 mem = try_get_mem_cgroup_from_swapcache(page);
54595fe2
KH
1889 if (!mem)
1890 goto charge_cur_mm;
8c7c6e34 1891 *ptr = mem;
f64c3f54 1892 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true, page);
54595fe2
KH
1893 /* drop extra refcnt from tryget */
1894 css_put(&mem->css);
1895 return ret;
8c7c6e34
KH
1896charge_cur_mm:
1897 if (unlikely(!mm))
1898 mm = &init_mm;
f64c3f54 1899 return __mem_cgroup_try_charge(mm, mask, ptr, true, page);
8c7c6e34
KH
1900}
1901
83aae4c7
DN
1902static void
1903__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1904 enum charge_type ctype)
7a81b88c
KH
1905{
1906 struct page_cgroup *pc;
1907
f8d66542 1908 if (mem_cgroup_disabled())
7a81b88c
KH
1909 return;
1910 if (!ptr)
1911 return;
88703267 1912 cgroup_exclude_rmdir(&ptr->css);
7a81b88c 1913 pc = lookup_page_cgroup(page);
544122e5 1914 mem_cgroup_lru_del_before_commit_swapcache(page);
83aae4c7 1915 __mem_cgroup_commit_charge(ptr, pc, ctype);
544122e5 1916 mem_cgroup_lru_add_after_commit_swapcache(page);
8c7c6e34
KH
1917 /*
1918 * Now swap is on-memory. This means this page may be
1919 * counted both as mem and swap....double count.
03f3c433
KH
1920 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
1921 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
1922 * may call delete_from_swap_cache() before reach here.
8c7c6e34 1923 */
03f3c433 1924 if (do_swap_account && PageSwapCache(page)) {
8c7c6e34 1925 swp_entry_t ent = {.val = page_private(page)};
a3b2d692 1926 unsigned short id;
8c7c6e34 1927 struct mem_cgroup *memcg;
a3b2d692
KH
1928
1929 id = swap_cgroup_record(ent, 0);
1930 rcu_read_lock();
1931 memcg = mem_cgroup_lookup(id);
8c7c6e34 1932 if (memcg) {
a3b2d692
KH
1933 /*
1934 * This recorded memcg can be obsolete one. So, avoid
1935 * calling css_tryget
1936 */
0c3e73e8 1937 if (!mem_cgroup_is_root(memcg))
4e649152 1938 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
0c3e73e8 1939 mem_cgroup_swap_statistics(memcg, false);
8c7c6e34
KH
1940 mem_cgroup_put(memcg);
1941 }
a3b2d692 1942 rcu_read_unlock();
8c7c6e34 1943 }
88703267
KH
1944 /*
1945 * At swapin, we may charge account against cgroup which has no tasks.
1946 * So, rmdir()->pre_destroy() can be called while we do this charge.
1947 * In that case, we need to call pre_destroy() again. check it here.
1948 */
1949 cgroup_release_and_wakeup_rmdir(&ptr->css);
7a81b88c
KH
1950}
1951
83aae4c7
DN
1952void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
1953{
1954 __mem_cgroup_commit_charge_swapin(page, ptr,
1955 MEM_CGROUP_CHARGE_TYPE_MAPPED);
1956}
1957
7a81b88c
KH
1958void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
1959{
f8d66542 1960 if (mem_cgroup_disabled())
7a81b88c
KH
1961 return;
1962 if (!mem)
1963 return;
0c3e73e8 1964 if (!mem_cgroup_is_root(mem)) {
4e649152 1965 res_counter_uncharge(&mem->res, PAGE_SIZE);
0c3e73e8 1966 if (do_swap_account)
4e649152 1967 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
0c3e73e8 1968 }
7a81b88c
KH
1969 css_put(&mem->css);
1970}
1971
569b846d
KH
1972static void
1973__do_uncharge(struct mem_cgroup *mem, const enum charge_type ctype)
1974{
1975 struct memcg_batch_info *batch = NULL;
1976 bool uncharge_memsw = true;
1977 /* If swapout, usage of swap doesn't decrease */
1978 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1979 uncharge_memsw = false;
1980 /*
1981 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
1982 * In those cases, all pages freed continously can be expected to be in
1983 * the same cgroup and we have chance to coalesce uncharges.
1984 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
1985 * because we want to do uncharge as soon as possible.
1986 */
1987 if (!current->memcg_batch.do_batch || test_thread_flag(TIF_MEMDIE))
1988 goto direct_uncharge;
1989
1990 batch = &current->memcg_batch;
1991 /*
1992 * In usual, we do css_get() when we remember memcg pointer.
1993 * But in this case, we keep res->usage until end of a series of
1994 * uncharges. Then, it's ok to ignore memcg's refcnt.
1995 */
1996 if (!batch->memcg)
1997 batch->memcg = mem;
1998 /*
1999 * In typical case, batch->memcg == mem. This means we can
2000 * merge a series of uncharges to an uncharge of res_counter.
2001 * If not, we uncharge res_counter ony by one.
2002 */
2003 if (batch->memcg != mem)
2004 goto direct_uncharge;
2005 /* remember freed charge and uncharge it later */
2006 batch->bytes += PAGE_SIZE;
2007 if (uncharge_memsw)
2008 batch->memsw_bytes += PAGE_SIZE;
2009 return;
2010direct_uncharge:
2011 res_counter_uncharge(&mem->res, PAGE_SIZE);
2012 if (uncharge_memsw)
2013 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
2014 return;
2015}
7a81b88c 2016
8a9f3ccd 2017/*
69029cd5 2018 * uncharge if !page_mapped(page)
8a9f3ccd 2019 */
8c7c6e34 2020static struct mem_cgroup *
69029cd5 2021__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
8a9f3ccd 2022{
8289546e 2023 struct page_cgroup *pc;
8c7c6e34 2024 struct mem_cgroup *mem = NULL;
072c56c1 2025 struct mem_cgroup_per_zone *mz;
8a9f3ccd 2026
f8d66542 2027 if (mem_cgroup_disabled())
8c7c6e34 2028 return NULL;
4077960e 2029
d13d1443 2030 if (PageSwapCache(page))
8c7c6e34 2031 return NULL;
d13d1443 2032
8697d331 2033 /*
3c541e14 2034 * Check if our page_cgroup is valid
8697d331 2035 */
52d4b9ac
KH
2036 pc = lookup_page_cgroup(page);
2037 if (unlikely(!pc || !PageCgroupUsed(pc)))
8c7c6e34 2038 return NULL;
b9c565d5 2039
52d4b9ac 2040 lock_page_cgroup(pc);
d13d1443 2041
8c7c6e34
KH
2042 mem = pc->mem_cgroup;
2043
d13d1443
KH
2044 if (!PageCgroupUsed(pc))
2045 goto unlock_out;
2046
2047 switch (ctype) {
2048 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
8a9478ca 2049 case MEM_CGROUP_CHARGE_TYPE_DROP:
d13d1443
KH
2050 if (page_mapped(page))
2051 goto unlock_out;
2052 break;
2053 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
2054 if (!PageAnon(page)) { /* Shared memory */
2055 if (page->mapping && !page_is_file_cache(page))
2056 goto unlock_out;
2057 } else if (page_mapped(page)) /* Anon */
2058 goto unlock_out;
2059 break;
2060 default:
2061 break;
52d4b9ac 2062 }
d13d1443 2063
569b846d
KH
2064 if (!mem_cgroup_is_root(mem))
2065 __do_uncharge(mem, ctype);
0c3e73e8
BS
2066 if (ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2067 mem_cgroup_swap_statistics(mem, true);
08e552c6 2068 mem_cgroup_charge_statistics(mem, pc, false);
04046e1a 2069
52d4b9ac 2070 ClearPageCgroupUsed(pc);
544122e5
KH
2071 /*
2072 * pc->mem_cgroup is not cleared here. It will be accessed when it's
2073 * freed from LRU. This is safe because uncharged page is expected not
2074 * to be reused (freed soon). Exception is SwapCache, it's handled by
2075 * special functions.
2076 */
b9c565d5 2077
69029cd5 2078 mz = page_cgroup_zoneinfo(pc);
52d4b9ac 2079 unlock_page_cgroup(pc);
fb59e9f1 2080
4e649152 2081 if (mem_cgroup_soft_limit_check(mem))
f64c3f54 2082 mem_cgroup_update_tree(mem, page);
a7fe942e
KH
2083 /* at swapout, this memcg will be accessed to record to swap */
2084 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2085 css_put(&mem->css);
6d12e2d8 2086
8c7c6e34 2087 return mem;
d13d1443
KH
2088
2089unlock_out:
2090 unlock_page_cgroup(pc);
8c7c6e34 2091 return NULL;
3c541e14
BS
2092}
2093
69029cd5
KH
2094void mem_cgroup_uncharge_page(struct page *page)
2095{
52d4b9ac
KH
2096 /* early check. */
2097 if (page_mapped(page))
2098 return;
2099 if (page->mapping && !PageAnon(page))
2100 return;
69029cd5
KH
2101 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
2102}
2103
2104void mem_cgroup_uncharge_cache_page(struct page *page)
2105{
2106 VM_BUG_ON(page_mapped(page));
b7abea96 2107 VM_BUG_ON(page->mapping);
69029cd5
KH
2108 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
2109}
2110
569b846d
KH
2111/*
2112 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
2113 * In that cases, pages are freed continuously and we can expect pages
2114 * are in the same memcg. All these calls itself limits the number of
2115 * pages freed at once, then uncharge_start/end() is called properly.
2116 * This may be called prural(2) times in a context,
2117 */
2118
2119void mem_cgroup_uncharge_start(void)
2120{
2121 current->memcg_batch.do_batch++;
2122 /* We can do nest. */
2123 if (current->memcg_batch.do_batch == 1) {
2124 current->memcg_batch.memcg = NULL;
2125 current->memcg_batch.bytes = 0;
2126 current->memcg_batch.memsw_bytes = 0;
2127 }
2128}
2129
2130void mem_cgroup_uncharge_end(void)
2131{
2132 struct memcg_batch_info *batch = &current->memcg_batch;
2133
2134 if (!batch->do_batch)
2135 return;
2136
2137 batch->do_batch--;
2138 if (batch->do_batch) /* If stacked, do nothing. */
2139 return;
2140
2141 if (!batch->memcg)
2142 return;
2143 /*
2144 * This "batch->memcg" is valid without any css_get/put etc...
2145 * bacause we hide charges behind us.
2146 */
2147 if (batch->bytes)
2148 res_counter_uncharge(&batch->memcg->res, batch->bytes);
2149 if (batch->memsw_bytes)
2150 res_counter_uncharge(&batch->memcg->memsw, batch->memsw_bytes);
2151 /* forget this pointer (for sanity check) */
2152 batch->memcg = NULL;
2153}
2154
e767e056 2155#ifdef CONFIG_SWAP
8c7c6e34 2156/*
e767e056 2157 * called after __delete_from_swap_cache() and drop "page" account.
8c7c6e34
KH
2158 * memcg information is recorded to swap_cgroup of "ent"
2159 */
8a9478ca
KH
2160void
2161mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
8c7c6e34
KH
2162{
2163 struct mem_cgroup *memcg;
8a9478ca
KH
2164 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
2165
2166 if (!swapout) /* this was a swap cache but the swap is unused ! */
2167 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
2168
2169 memcg = __mem_cgroup_uncharge_common(page, ctype);
8c7c6e34 2170
8c7c6e34 2171 /* record memcg information */
8a9478ca 2172 if (do_swap_account && swapout && memcg) {
a3b2d692 2173 swap_cgroup_record(ent, css_id(&memcg->css));
8c7c6e34
KH
2174 mem_cgroup_get(memcg);
2175 }
8a9478ca 2176 if (swapout && memcg)
a7fe942e 2177 css_put(&memcg->css);
8c7c6e34 2178}
e767e056 2179#endif
8c7c6e34
KH
2180
2181#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2182/*
2183 * called from swap_entry_free(). remove record in swap_cgroup and
2184 * uncharge "memsw" account.
2185 */
2186void mem_cgroup_uncharge_swap(swp_entry_t ent)
d13d1443 2187{
8c7c6e34 2188 struct mem_cgroup *memcg;
a3b2d692 2189 unsigned short id;
8c7c6e34
KH
2190
2191 if (!do_swap_account)
2192 return;
2193
a3b2d692
KH
2194 id = swap_cgroup_record(ent, 0);
2195 rcu_read_lock();
2196 memcg = mem_cgroup_lookup(id);
8c7c6e34 2197 if (memcg) {
a3b2d692
KH
2198 /*
2199 * We uncharge this because swap is freed.
2200 * This memcg can be obsolete one. We avoid calling css_tryget
2201 */
0c3e73e8 2202 if (!mem_cgroup_is_root(memcg))
4e649152 2203 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
0c3e73e8 2204 mem_cgroup_swap_statistics(memcg, false);
8c7c6e34
KH
2205 mem_cgroup_put(memcg);
2206 }
a3b2d692 2207 rcu_read_unlock();
d13d1443 2208}
8c7c6e34 2209#endif
d13d1443 2210
ae41be37 2211/*
01b1ae63
KH
2212 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
2213 * page belongs to.
ae41be37 2214 */
01b1ae63 2215int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
ae41be37
KH
2216{
2217 struct page_cgroup *pc;
e8589cc1 2218 struct mem_cgroup *mem = NULL;
e8589cc1 2219 int ret = 0;
8869b8f6 2220
f8d66542 2221 if (mem_cgroup_disabled())
4077960e
BS
2222 return 0;
2223
52d4b9ac
KH
2224 pc = lookup_page_cgroup(page);
2225 lock_page_cgroup(pc);
2226 if (PageCgroupUsed(pc)) {
e8589cc1
KH
2227 mem = pc->mem_cgroup;
2228 css_get(&mem->css);
e8589cc1 2229 }
52d4b9ac 2230 unlock_page_cgroup(pc);
01b1ae63 2231
e8589cc1 2232 if (mem) {
f64c3f54
BS
2233 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false,
2234 page);
e8589cc1
KH
2235 css_put(&mem->css);
2236 }
01b1ae63 2237 *ptr = mem;
e8589cc1 2238 return ret;
ae41be37 2239}
8869b8f6 2240
69029cd5 2241/* remove redundant charge if migration failed*/
01b1ae63
KH
2242void mem_cgroup_end_migration(struct mem_cgroup *mem,
2243 struct page *oldpage, struct page *newpage)
ae41be37 2244{
01b1ae63
KH
2245 struct page *target, *unused;
2246 struct page_cgroup *pc;
2247 enum charge_type ctype;
2248
2249 if (!mem)
2250 return;
88703267 2251 cgroup_exclude_rmdir(&mem->css);
01b1ae63
KH
2252 /* at migration success, oldpage->mapping is NULL. */
2253 if (oldpage->mapping) {
2254 target = oldpage;
2255 unused = NULL;
2256 } else {
2257 target = newpage;
2258 unused = oldpage;
2259 }
2260
2261 if (PageAnon(target))
2262 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
2263 else if (page_is_file_cache(target))
2264 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
2265 else
2266 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
2267
2268 /* unused page is not on radix-tree now. */
d13d1443 2269 if (unused)
01b1ae63
KH
2270 __mem_cgroup_uncharge_common(unused, ctype);
2271
2272 pc = lookup_page_cgroup(target);
69029cd5 2273 /*
01b1ae63
KH
2274 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
2275 * So, double-counting is effectively avoided.
2276 */
2277 __mem_cgroup_commit_charge(mem, pc, ctype);
2278
2279 /*
2280 * Both of oldpage and newpage are still under lock_page().
2281 * Then, we don't have to care about race in radix-tree.
2282 * But we have to be careful that this page is unmapped or not.
2283 *
2284 * There is a case for !page_mapped(). At the start of
2285 * migration, oldpage was mapped. But now, it's zapped.
2286 * But we know *target* page is not freed/reused under us.
2287 * mem_cgroup_uncharge_page() does all necessary checks.
69029cd5 2288 */
01b1ae63
KH
2289 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
2290 mem_cgroup_uncharge_page(target);
88703267
KH
2291 /*
2292 * At migration, we may charge account against cgroup which has no tasks
2293 * So, rmdir()->pre_destroy() can be called while we do this charge.
2294 * In that case, we need to call pre_destroy() again. check it here.
2295 */
2296 cgroup_release_and_wakeup_rmdir(&mem->css);
ae41be37 2297}
78fb7466 2298
c9b0ed51 2299/*
ae3abae6
DN
2300 * A call to try to shrink memory usage on charge failure at shmem's swapin.
2301 * Calling hierarchical_reclaim is not enough because we should update
2302 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
2303 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
2304 * not from the memcg which this page would be charged to.
2305 * try_charge_swapin does all of these works properly.
c9b0ed51 2306 */
ae3abae6 2307int mem_cgroup_shmem_charge_fallback(struct page *page,
b5a84319
KH
2308 struct mm_struct *mm,
2309 gfp_t gfp_mask)
c9b0ed51 2310{
b5a84319 2311 struct mem_cgroup *mem = NULL;
ae3abae6 2312 int ret;
c9b0ed51 2313
f8d66542 2314 if (mem_cgroup_disabled())
cede86ac 2315 return 0;
c9b0ed51 2316
ae3abae6
DN
2317 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2318 if (!ret)
2319 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
c9b0ed51 2320
ae3abae6 2321 return ret;
c9b0ed51
KH
2322}
2323
8c7c6e34
KH
2324static DEFINE_MUTEX(set_limit_mutex);
2325
d38d2a75 2326static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
8c7c6e34 2327 unsigned long long val)
628f4235 2328{
81d39c20 2329 int retry_count;
628f4235 2330 int progress;
8c7c6e34 2331 u64 memswlimit;
628f4235 2332 int ret = 0;
81d39c20
KH
2333 int children = mem_cgroup_count_children(memcg);
2334 u64 curusage, oldusage;
2335
2336 /*
2337 * For keeping hierarchical_reclaim simple, how long we should retry
2338 * is depends on callers. We set our retry-count to be function
2339 * of # of children which we should visit in this loop.
2340 */
2341 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
2342
2343 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
628f4235 2344
8c7c6e34 2345 while (retry_count) {
628f4235
KH
2346 if (signal_pending(current)) {
2347 ret = -EINTR;
2348 break;
2349 }
8c7c6e34
KH
2350 /*
2351 * Rather than hide all in some function, I do this in
2352 * open coded manner. You see what this really does.
2353 * We have to guarantee mem->res.limit < mem->memsw.limit.
2354 */
2355 mutex_lock(&set_limit_mutex);
2356 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2357 if (memswlimit < val) {
2358 ret = -EINVAL;
2359 mutex_unlock(&set_limit_mutex);
628f4235
KH
2360 break;
2361 }
8c7c6e34 2362 ret = res_counter_set_limit(&memcg->res, val);
22a668d7
KH
2363 if (!ret) {
2364 if (memswlimit == val)
2365 memcg->memsw_is_minimum = true;
2366 else
2367 memcg->memsw_is_minimum = false;
2368 }
8c7c6e34
KH
2369 mutex_unlock(&set_limit_mutex);
2370
2371 if (!ret)
2372 break;
2373
4e416953
BS
2374 progress = mem_cgroup_hierarchical_reclaim(memcg, NULL,
2375 GFP_KERNEL,
2376 MEM_CGROUP_RECLAIM_SHRINK);
81d39c20
KH
2377 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
2378 /* Usage is reduced ? */
2379 if (curusage >= oldusage)
2380 retry_count--;
2381 else
2382 oldusage = curusage;
8c7c6e34 2383 }
14797e23 2384
8c7c6e34
KH
2385 return ret;
2386}
2387
338c8431
LZ
2388static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
2389 unsigned long long val)
8c7c6e34 2390{
81d39c20 2391 int retry_count;
8c7c6e34 2392 u64 memlimit, oldusage, curusage;
81d39c20
KH
2393 int children = mem_cgroup_count_children(memcg);
2394 int ret = -EBUSY;
8c7c6e34 2395
81d39c20
KH
2396 /* see mem_cgroup_resize_res_limit */
2397 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
2398 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
8c7c6e34
KH
2399 while (retry_count) {
2400 if (signal_pending(current)) {
2401 ret = -EINTR;
2402 break;
2403 }
2404 /*
2405 * Rather than hide all in some function, I do this in
2406 * open coded manner. You see what this really does.
2407 * We have to guarantee mem->res.limit < mem->memsw.limit.
2408 */
2409 mutex_lock(&set_limit_mutex);
2410 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2411 if (memlimit > val) {
2412 ret = -EINVAL;
2413 mutex_unlock(&set_limit_mutex);
2414 break;
2415 }
2416 ret = res_counter_set_limit(&memcg->memsw, val);
22a668d7
KH
2417 if (!ret) {
2418 if (memlimit == val)
2419 memcg->memsw_is_minimum = true;
2420 else
2421 memcg->memsw_is_minimum = false;
2422 }
8c7c6e34
KH
2423 mutex_unlock(&set_limit_mutex);
2424
2425 if (!ret)
2426 break;
2427
4e416953 2428 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
75822b44
BS
2429 MEM_CGROUP_RECLAIM_NOSWAP |
2430 MEM_CGROUP_RECLAIM_SHRINK);
8c7c6e34 2431 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
81d39c20 2432 /* Usage is reduced ? */
8c7c6e34 2433 if (curusage >= oldusage)
628f4235 2434 retry_count--;
81d39c20
KH
2435 else
2436 oldusage = curusage;
628f4235
KH
2437 }
2438 return ret;
2439}
2440
4e416953
BS
2441unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
2442 gfp_t gfp_mask, int nid,
2443 int zid)
2444{
2445 unsigned long nr_reclaimed = 0;
2446 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
2447 unsigned long reclaimed;
2448 int loop = 0;
2449 struct mem_cgroup_tree_per_zone *mctz;
ef8745c1 2450 unsigned long long excess;
4e416953
BS
2451
2452 if (order > 0)
2453 return 0;
2454
2455 mctz = soft_limit_tree_node_zone(nid, zid);
2456 /*
2457 * This loop can run a while, specially if mem_cgroup's continuously
2458 * keep exceeding their soft limit and putting the system under
2459 * pressure
2460 */
2461 do {
2462 if (next_mz)
2463 mz = next_mz;
2464 else
2465 mz = mem_cgroup_largest_soft_limit_node(mctz);
2466 if (!mz)
2467 break;
2468
2469 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
2470 gfp_mask,
2471 MEM_CGROUP_RECLAIM_SOFT);
2472 nr_reclaimed += reclaimed;
2473 spin_lock(&mctz->lock);
2474
2475 /*
2476 * If we failed to reclaim anything from this memory cgroup
2477 * it is time to move on to the next cgroup
2478 */
2479 next_mz = NULL;
2480 if (!reclaimed) {
2481 do {
2482 /*
2483 * Loop until we find yet another one.
2484 *
2485 * By the time we get the soft_limit lock
2486 * again, someone might have aded the
2487 * group back on the RB tree. Iterate to
2488 * make sure we get a different mem.
2489 * mem_cgroup_largest_soft_limit_node returns
2490 * NULL if no other cgroup is present on
2491 * the tree
2492 */
2493 next_mz =
2494 __mem_cgroup_largest_soft_limit_node(mctz);
2495 if (next_mz == mz) {
2496 css_put(&next_mz->mem->css);
2497 next_mz = NULL;
2498 } else /* next_mz == NULL or other memcg */
2499 break;
2500 } while (1);
2501 }
4e416953 2502 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
ef8745c1 2503 excess = res_counter_soft_limit_excess(&mz->mem->res);
4e416953
BS
2504 /*
2505 * One school of thought says that we should not add
2506 * back the node to the tree if reclaim returns 0.
2507 * But our reclaim could return 0, simply because due
2508 * to priority we are exposing a smaller subset of
2509 * memory to reclaim from. Consider this as a longer
2510 * term TODO.
2511 */
ef8745c1
KH
2512 /* If excess == 0, no tree ops */
2513 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
4e416953
BS
2514 spin_unlock(&mctz->lock);
2515 css_put(&mz->mem->css);
2516 loop++;
2517 /*
2518 * Could not reclaim anything and there are no more
2519 * mem cgroups to try or we seem to be looping without
2520 * reclaiming anything.
2521 */
2522 if (!nr_reclaimed &&
2523 (next_mz == NULL ||
2524 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
2525 break;
2526 } while (!nr_reclaimed);
2527 if (next_mz)
2528 css_put(&next_mz->mem->css);
2529 return nr_reclaimed;
2530}
2531
cc847582
KH
2532/*
2533 * This routine traverse page_cgroup in given list and drop them all.
cc847582
KH
2534 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
2535 */
f817ed48 2536static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
08e552c6 2537 int node, int zid, enum lru_list lru)
cc847582 2538{
08e552c6
KH
2539 struct zone *zone;
2540 struct mem_cgroup_per_zone *mz;
f817ed48 2541 struct page_cgroup *pc, *busy;
08e552c6 2542 unsigned long flags, loop;
072c56c1 2543 struct list_head *list;
f817ed48 2544 int ret = 0;
072c56c1 2545
08e552c6
KH
2546 zone = &NODE_DATA(node)->node_zones[zid];
2547 mz = mem_cgroup_zoneinfo(mem, node, zid);
b69408e8 2548 list = &mz->lists[lru];
cc847582 2549
f817ed48
KH
2550 loop = MEM_CGROUP_ZSTAT(mz, lru);
2551 /* give some margin against EBUSY etc...*/
2552 loop += 256;
2553 busy = NULL;
2554 while (loop--) {
2555 ret = 0;
08e552c6 2556 spin_lock_irqsave(&zone->lru_lock, flags);
f817ed48 2557 if (list_empty(list)) {
08e552c6 2558 spin_unlock_irqrestore(&zone->lru_lock, flags);
52d4b9ac 2559 break;
f817ed48
KH
2560 }
2561 pc = list_entry(list->prev, struct page_cgroup, lru);
2562 if (busy == pc) {
2563 list_move(&pc->lru, list);
2564 busy = 0;
08e552c6 2565 spin_unlock_irqrestore(&zone->lru_lock, flags);
f817ed48
KH
2566 continue;
2567 }
08e552c6 2568 spin_unlock_irqrestore(&zone->lru_lock, flags);
f817ed48 2569
2c26fdd7 2570 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
f817ed48 2571 if (ret == -ENOMEM)
52d4b9ac 2572 break;
f817ed48
KH
2573
2574 if (ret == -EBUSY || ret == -EINVAL) {
2575 /* found lock contention or "pc" is obsolete. */
2576 busy = pc;
2577 cond_resched();
2578 } else
2579 busy = NULL;
cc847582 2580 }
08e552c6 2581
f817ed48
KH
2582 if (!ret && !list_empty(list))
2583 return -EBUSY;
2584 return ret;
cc847582
KH
2585}
2586
2587/*
2588 * make mem_cgroup's charge to be 0 if there is no task.
2589 * This enables deleting this mem_cgroup.
2590 */
c1e862c1 2591static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
cc847582 2592{
f817ed48
KH
2593 int ret;
2594 int node, zid, shrink;
2595 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
c1e862c1 2596 struct cgroup *cgrp = mem->css.cgroup;
8869b8f6 2597
cc847582 2598 css_get(&mem->css);
f817ed48
KH
2599
2600 shrink = 0;
c1e862c1
KH
2601 /* should free all ? */
2602 if (free_all)
2603 goto try_to_free;
f817ed48 2604move_account:
1ecaab2b 2605 while (mem->res.usage > 0) {
f817ed48 2606 ret = -EBUSY;
c1e862c1
KH
2607 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
2608 goto out;
2609 ret = -EINTR;
2610 if (signal_pending(current))
cc847582 2611 goto out;
52d4b9ac
KH
2612 /* This is for making all *used* pages to be on LRU. */
2613 lru_add_drain_all();
cdec2e42 2614 drain_all_stock_sync();
f817ed48 2615 ret = 0;
299b4eaa 2616 for_each_node_state(node, N_HIGH_MEMORY) {
f817ed48 2617 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
b69408e8 2618 enum lru_list l;
f817ed48
KH
2619 for_each_lru(l) {
2620 ret = mem_cgroup_force_empty_list(mem,
08e552c6 2621 node, zid, l);
f817ed48
KH
2622 if (ret)
2623 break;
2624 }
1ecaab2b 2625 }
f817ed48
KH
2626 if (ret)
2627 break;
2628 }
2629 /* it seems parent cgroup doesn't have enough mem */
2630 if (ret == -ENOMEM)
2631 goto try_to_free;
52d4b9ac 2632 cond_resched();
cc847582
KH
2633 }
2634 ret = 0;
2635out:
2636 css_put(&mem->css);
2637 return ret;
f817ed48
KH
2638
2639try_to_free:
c1e862c1
KH
2640 /* returns EBUSY if there is a task or if we come here twice. */
2641 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
f817ed48
KH
2642 ret = -EBUSY;
2643 goto out;
2644 }
c1e862c1
KH
2645 /* we call try-to-free pages for make this cgroup empty */
2646 lru_add_drain_all();
f817ed48
KH
2647 /* try to free all pages in this cgroup */
2648 shrink = 1;
2649 while (nr_retries && mem->res.usage > 0) {
2650 int progress;
c1e862c1
KH
2651
2652 if (signal_pending(current)) {
2653 ret = -EINTR;
2654 goto out;
2655 }
a7885eb8
KM
2656 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
2657 false, get_swappiness(mem));
c1e862c1 2658 if (!progress) {
f817ed48 2659 nr_retries--;
c1e862c1 2660 /* maybe some writeback is necessary */
8aa7e847 2661 congestion_wait(BLK_RW_ASYNC, HZ/10);
c1e862c1 2662 }
f817ed48
KH
2663
2664 }
08e552c6 2665 lru_add_drain();
f817ed48
KH
2666 /* try move_account...there may be some *locked* pages. */
2667 if (mem->res.usage)
2668 goto move_account;
2669 ret = 0;
2670 goto out;
cc847582
KH
2671}
2672
c1e862c1
KH
2673int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
2674{
2675 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
2676}
2677
2678
18f59ea7
BS
2679static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
2680{
2681 return mem_cgroup_from_cont(cont)->use_hierarchy;
2682}
2683
2684static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
2685 u64 val)
2686{
2687 int retval = 0;
2688 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2689 struct cgroup *parent = cont->parent;
2690 struct mem_cgroup *parent_mem = NULL;
2691
2692 if (parent)
2693 parent_mem = mem_cgroup_from_cont(parent);
2694
2695 cgroup_lock();
2696 /*
af901ca1 2697 * If parent's use_hierarchy is set, we can't make any modifications
18f59ea7
BS
2698 * in the child subtrees. If it is unset, then the change can
2699 * occur, provided the current cgroup has no children.
2700 *
2701 * For the root cgroup, parent_mem is NULL, we allow value to be
2702 * set if there are no children.
2703 */
2704 if ((!parent_mem || !parent_mem->use_hierarchy) &&
2705 (val == 1 || val == 0)) {
2706 if (list_empty(&cont->children))
2707 mem->use_hierarchy = val;
2708 else
2709 retval = -EBUSY;
2710 } else
2711 retval = -EINVAL;
2712 cgroup_unlock();
2713
2714 return retval;
2715}
2716
0c3e73e8
BS
2717struct mem_cgroup_idx_data {
2718 s64 val;
2719 enum mem_cgroup_stat_index idx;
2720};
2721
2722static int
2723mem_cgroup_get_idx_stat(struct mem_cgroup *mem, void *data)
2724{
2725 struct mem_cgroup_idx_data *d = data;
2726 d->val += mem_cgroup_read_stat(&mem->stat, d->idx);
2727 return 0;
2728}
2729
2730static void
2731mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem,
2732 enum mem_cgroup_stat_index idx, s64 *val)
2733{
2734 struct mem_cgroup_idx_data d;
2735 d.idx = idx;
2736 d.val = 0;
2737 mem_cgroup_walk_tree(mem, &d, mem_cgroup_get_idx_stat);
2738 *val = d.val;
2739}
2740
2c3daa72 2741static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
8cdea7c0 2742{
8c7c6e34 2743 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
0c3e73e8 2744 u64 idx_val, val;
8c7c6e34
KH
2745 int type, name;
2746
2747 type = MEMFILE_TYPE(cft->private);
2748 name = MEMFILE_ATTR(cft->private);
2749 switch (type) {
2750 case _MEM:
0c3e73e8
BS
2751 if (name == RES_USAGE && mem_cgroup_is_root(mem)) {
2752 mem_cgroup_get_recursive_idx_stat(mem,
2753 MEM_CGROUP_STAT_CACHE, &idx_val);
2754 val = idx_val;
2755 mem_cgroup_get_recursive_idx_stat(mem,
2756 MEM_CGROUP_STAT_RSS, &idx_val);
2757 val += idx_val;
2758 val <<= PAGE_SHIFT;
2759 } else
2760 val = res_counter_read_u64(&mem->res, name);
8c7c6e34
KH
2761 break;
2762 case _MEMSWAP:
0c3e73e8
BS
2763 if (name == RES_USAGE && mem_cgroup_is_root(mem)) {
2764 mem_cgroup_get_recursive_idx_stat(mem,
2765 MEM_CGROUP_STAT_CACHE, &idx_val);
2766 val = idx_val;
2767 mem_cgroup_get_recursive_idx_stat(mem,
2768 MEM_CGROUP_STAT_RSS, &idx_val);
2769 val += idx_val;
2770 mem_cgroup_get_recursive_idx_stat(mem,
2771 MEM_CGROUP_STAT_SWAPOUT, &idx_val);
cd9b45b7 2772 val += idx_val;
0c3e73e8
BS
2773 val <<= PAGE_SHIFT;
2774 } else
2775 val = res_counter_read_u64(&mem->memsw, name);
8c7c6e34
KH
2776 break;
2777 default:
2778 BUG();
2779 break;
2780 }
2781 return val;
8cdea7c0 2782}
628f4235
KH
2783/*
2784 * The user of this function is...
2785 * RES_LIMIT.
2786 */
856c13aa
PM
2787static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
2788 const char *buffer)
8cdea7c0 2789{
628f4235 2790 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
8c7c6e34 2791 int type, name;
628f4235
KH
2792 unsigned long long val;
2793 int ret;
2794
8c7c6e34
KH
2795 type = MEMFILE_TYPE(cft->private);
2796 name = MEMFILE_ATTR(cft->private);
2797 switch (name) {
628f4235 2798 case RES_LIMIT:
4b3bde4c
BS
2799 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
2800 ret = -EINVAL;
2801 break;
2802 }
628f4235
KH
2803 /* This function does all necessary parse...reuse it */
2804 ret = res_counter_memparse_write_strategy(buffer, &val);
8c7c6e34
KH
2805 if (ret)
2806 break;
2807 if (type == _MEM)
628f4235 2808 ret = mem_cgroup_resize_limit(memcg, val);
8c7c6e34
KH
2809 else
2810 ret = mem_cgroup_resize_memsw_limit(memcg, val);
628f4235 2811 break;
296c81d8
BS
2812 case RES_SOFT_LIMIT:
2813 ret = res_counter_memparse_write_strategy(buffer, &val);
2814 if (ret)
2815 break;
2816 /*
2817 * For memsw, soft limits are hard to implement in terms
2818 * of semantics, for now, we support soft limits for
2819 * control without swap
2820 */
2821 if (type == _MEM)
2822 ret = res_counter_set_soft_limit(&memcg->res, val);
2823 else
2824 ret = -EINVAL;
2825 break;
628f4235
KH
2826 default:
2827 ret = -EINVAL; /* should be BUG() ? */
2828 break;
2829 }
2830 return ret;
8cdea7c0
BS
2831}
2832
fee7b548
KH
2833static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
2834 unsigned long long *mem_limit, unsigned long long *memsw_limit)
2835{
2836 struct cgroup *cgroup;
2837 unsigned long long min_limit, min_memsw_limit, tmp;
2838
2839 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2840 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2841 cgroup = memcg->css.cgroup;
2842 if (!memcg->use_hierarchy)
2843 goto out;
2844
2845 while (cgroup->parent) {
2846 cgroup = cgroup->parent;
2847 memcg = mem_cgroup_from_cont(cgroup);
2848 if (!memcg->use_hierarchy)
2849 break;
2850 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
2851 min_limit = min(min_limit, tmp);
2852 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2853 min_memsw_limit = min(min_memsw_limit, tmp);
2854 }
2855out:
2856 *mem_limit = min_limit;
2857 *memsw_limit = min_memsw_limit;
2858 return;
2859}
2860
29f2a4da 2861static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
c84872e1
PE
2862{
2863 struct mem_cgroup *mem;
8c7c6e34 2864 int type, name;
c84872e1
PE
2865
2866 mem = mem_cgroup_from_cont(cont);
8c7c6e34
KH
2867 type = MEMFILE_TYPE(event);
2868 name = MEMFILE_ATTR(event);
2869 switch (name) {
29f2a4da 2870 case RES_MAX_USAGE:
8c7c6e34
KH
2871 if (type == _MEM)
2872 res_counter_reset_max(&mem->res);
2873 else
2874 res_counter_reset_max(&mem->memsw);
29f2a4da
PE
2875 break;
2876 case RES_FAILCNT:
8c7c6e34
KH
2877 if (type == _MEM)
2878 res_counter_reset_failcnt(&mem->res);
2879 else
2880 res_counter_reset_failcnt(&mem->memsw);
29f2a4da
PE
2881 break;
2882 }
f64c3f54 2883
85cc59db 2884 return 0;
c84872e1
PE
2885}
2886
14067bb3
KH
2887
2888/* For read statistics */
2889enum {
2890 MCS_CACHE,
2891 MCS_RSS,
d69b042f 2892 MCS_MAPPED_FILE,
14067bb3
KH
2893 MCS_PGPGIN,
2894 MCS_PGPGOUT,
1dd3a273 2895 MCS_SWAP,
14067bb3
KH
2896 MCS_INACTIVE_ANON,
2897 MCS_ACTIVE_ANON,
2898 MCS_INACTIVE_FILE,
2899 MCS_ACTIVE_FILE,
2900 MCS_UNEVICTABLE,
2901 NR_MCS_STAT,
2902};
2903
2904struct mcs_total_stat {
2905 s64 stat[NR_MCS_STAT];
d2ceb9b7
KH
2906};
2907
14067bb3
KH
2908struct {
2909 char *local_name;
2910 char *total_name;
2911} memcg_stat_strings[NR_MCS_STAT] = {
2912 {"cache", "total_cache"},
2913 {"rss", "total_rss"},
d69b042f 2914 {"mapped_file", "total_mapped_file"},
14067bb3
KH
2915 {"pgpgin", "total_pgpgin"},
2916 {"pgpgout", "total_pgpgout"},
1dd3a273 2917 {"swap", "total_swap"},
14067bb3
KH
2918 {"inactive_anon", "total_inactive_anon"},
2919 {"active_anon", "total_active_anon"},
2920 {"inactive_file", "total_inactive_file"},
2921 {"active_file", "total_active_file"},
2922 {"unevictable", "total_unevictable"}
2923};
2924
2925
2926static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
2927{
2928 struct mcs_total_stat *s = data;
2929 s64 val;
2930
2931 /* per cpu stat */
2932 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE);
2933 s->stat[MCS_CACHE] += val * PAGE_SIZE;
2934 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
2935 s->stat[MCS_RSS] += val * PAGE_SIZE;
d69b042f
BS
2936 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_MAPPED_FILE);
2937 s->stat[MCS_MAPPED_FILE] += val * PAGE_SIZE;
14067bb3
KH
2938 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT);
2939 s->stat[MCS_PGPGIN] += val;
2940 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT);
2941 s->stat[MCS_PGPGOUT] += val;
1dd3a273
DN
2942 if (do_swap_account) {
2943 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_SWAPOUT);
2944 s->stat[MCS_SWAP] += val * PAGE_SIZE;
2945 }
14067bb3
KH
2946
2947 /* per zone stat */
2948 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
2949 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
2950 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
2951 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
2952 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
2953 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
2954 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
2955 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
2956 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
2957 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
2958 return 0;
2959}
2960
2961static void
2962mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
2963{
2964 mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
2965}
2966
c64745cf
PM
2967static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
2968 struct cgroup_map_cb *cb)
d2ceb9b7 2969{
d2ceb9b7 2970 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
14067bb3 2971 struct mcs_total_stat mystat;
d2ceb9b7
KH
2972 int i;
2973
14067bb3
KH
2974 memset(&mystat, 0, sizeof(mystat));
2975 mem_cgroup_get_local_stat(mem_cont, &mystat);
d2ceb9b7 2976
1dd3a273
DN
2977 for (i = 0; i < NR_MCS_STAT; i++) {
2978 if (i == MCS_SWAP && !do_swap_account)
2979 continue;
14067bb3 2980 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
1dd3a273 2981 }
7b854121 2982
14067bb3 2983 /* Hierarchical information */
fee7b548
KH
2984 {
2985 unsigned long long limit, memsw_limit;
2986 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
2987 cb->fill(cb, "hierarchical_memory_limit", limit);
2988 if (do_swap_account)
2989 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
2990 }
7f016ee8 2991
14067bb3
KH
2992 memset(&mystat, 0, sizeof(mystat));
2993 mem_cgroup_get_total_stat(mem_cont, &mystat);
1dd3a273
DN
2994 for (i = 0; i < NR_MCS_STAT; i++) {
2995 if (i == MCS_SWAP && !do_swap_account)
2996 continue;
14067bb3 2997 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
1dd3a273 2998 }
14067bb3 2999
7f016ee8 3000#ifdef CONFIG_DEBUG_VM
c772be93 3001 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
7f016ee8
KM
3002
3003 {
3004 int nid, zid;
3005 struct mem_cgroup_per_zone *mz;
3006 unsigned long recent_rotated[2] = {0, 0};
3007 unsigned long recent_scanned[2] = {0, 0};
3008
3009 for_each_online_node(nid)
3010 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
3011 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
3012
3013 recent_rotated[0] +=
3014 mz->reclaim_stat.recent_rotated[0];
3015 recent_rotated[1] +=
3016 mz->reclaim_stat.recent_rotated[1];
3017 recent_scanned[0] +=
3018 mz->reclaim_stat.recent_scanned[0];
3019 recent_scanned[1] +=
3020 mz->reclaim_stat.recent_scanned[1];
3021 }
3022 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
3023 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
3024 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
3025 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
3026 }
3027#endif
3028
d2ceb9b7
KH
3029 return 0;
3030}
3031
a7885eb8
KM
3032static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
3033{
3034 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3035
3036 return get_swappiness(memcg);
3037}
3038
3039static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
3040 u64 val)
3041{
3042 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3043 struct mem_cgroup *parent;
068b38c1 3044
a7885eb8
KM
3045 if (val > 100)
3046 return -EINVAL;
3047
3048 if (cgrp->parent == NULL)
3049 return -EINVAL;
3050
3051 parent = mem_cgroup_from_cont(cgrp->parent);
068b38c1
LZ
3052
3053 cgroup_lock();
3054
a7885eb8
KM
3055 /* If under hierarchy, only empty-root can set this value */
3056 if ((parent->use_hierarchy) ||
068b38c1
LZ
3057 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
3058 cgroup_unlock();
a7885eb8 3059 return -EINVAL;
068b38c1 3060 }
a7885eb8
KM
3061
3062 spin_lock(&memcg->reclaim_param_lock);
3063 memcg->swappiness = val;
3064 spin_unlock(&memcg->reclaim_param_lock);
3065
068b38c1
LZ
3066 cgroup_unlock();
3067
a7885eb8
KM
3068 return 0;
3069}
3070
c1e862c1 3071
8cdea7c0
BS
3072static struct cftype mem_cgroup_files[] = {
3073 {
0eea1030 3074 .name = "usage_in_bytes",
8c7c6e34 3075 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
2c3daa72 3076 .read_u64 = mem_cgroup_read,
8cdea7c0 3077 },
c84872e1
PE
3078 {
3079 .name = "max_usage_in_bytes",
8c7c6e34 3080 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
29f2a4da 3081 .trigger = mem_cgroup_reset,
c84872e1
PE
3082 .read_u64 = mem_cgroup_read,
3083 },
8cdea7c0 3084 {
0eea1030 3085 .name = "limit_in_bytes",
8c7c6e34 3086 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
856c13aa 3087 .write_string = mem_cgroup_write,
2c3daa72 3088 .read_u64 = mem_cgroup_read,
8cdea7c0 3089 },
296c81d8
BS
3090 {
3091 .name = "soft_limit_in_bytes",
3092 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
3093 .write_string = mem_cgroup_write,
3094 .read_u64 = mem_cgroup_read,
3095 },
8cdea7c0
BS
3096 {
3097 .name = "failcnt",
8c7c6e34 3098 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
29f2a4da 3099 .trigger = mem_cgroup_reset,
2c3daa72 3100 .read_u64 = mem_cgroup_read,
8cdea7c0 3101 },
d2ceb9b7
KH
3102 {
3103 .name = "stat",
c64745cf 3104 .read_map = mem_control_stat_show,
d2ceb9b7 3105 },
c1e862c1
KH
3106 {
3107 .name = "force_empty",
3108 .trigger = mem_cgroup_force_empty_write,
3109 },
18f59ea7
BS
3110 {
3111 .name = "use_hierarchy",
3112 .write_u64 = mem_cgroup_hierarchy_write,
3113 .read_u64 = mem_cgroup_hierarchy_read,
3114 },
a7885eb8
KM
3115 {
3116 .name = "swappiness",
3117 .read_u64 = mem_cgroup_swappiness_read,
3118 .write_u64 = mem_cgroup_swappiness_write,
3119 },
8cdea7c0
BS
3120};
3121
8c7c6e34
KH
3122#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3123static struct cftype memsw_cgroup_files[] = {
3124 {
3125 .name = "memsw.usage_in_bytes",
3126 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
3127 .read_u64 = mem_cgroup_read,
3128 },
3129 {
3130 .name = "memsw.max_usage_in_bytes",
3131 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
3132 .trigger = mem_cgroup_reset,
3133 .read_u64 = mem_cgroup_read,
3134 },
3135 {
3136 .name = "memsw.limit_in_bytes",
3137 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
3138 .write_string = mem_cgroup_write,
3139 .read_u64 = mem_cgroup_read,
3140 },
3141 {
3142 .name = "memsw.failcnt",
3143 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
3144 .trigger = mem_cgroup_reset,
3145 .read_u64 = mem_cgroup_read,
3146 },
3147};
3148
3149static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
3150{
3151 if (!do_swap_account)
3152 return 0;
3153 return cgroup_add_files(cont, ss, memsw_cgroup_files,
3154 ARRAY_SIZE(memsw_cgroup_files));
3155};
3156#else
3157static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
3158{
3159 return 0;
3160}
3161#endif
3162
6d12e2d8
KH
3163static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
3164{
3165 struct mem_cgroup_per_node *pn;
1ecaab2b 3166 struct mem_cgroup_per_zone *mz;
b69408e8 3167 enum lru_list l;
41e3355d 3168 int zone, tmp = node;
1ecaab2b
KH
3169 /*
3170 * This routine is called against possible nodes.
3171 * But it's BUG to call kmalloc() against offline node.
3172 *
3173 * TODO: this routine can waste much memory for nodes which will
3174 * never be onlined. It's better to use memory hotplug callback
3175 * function.
3176 */
41e3355d
KH
3177 if (!node_state(node, N_NORMAL_MEMORY))
3178 tmp = -1;
3179 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
6d12e2d8
KH
3180 if (!pn)
3181 return 1;
1ecaab2b 3182
6d12e2d8
KH
3183 mem->info.nodeinfo[node] = pn;
3184 memset(pn, 0, sizeof(*pn));
1ecaab2b
KH
3185
3186 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
3187 mz = &pn->zoneinfo[zone];
b69408e8
CL
3188 for_each_lru(l)
3189 INIT_LIST_HEAD(&mz->lists[l]);
f64c3f54 3190 mz->usage_in_excess = 0;
4e416953
BS
3191 mz->on_tree = false;
3192 mz->mem = mem;
1ecaab2b 3193 }
6d12e2d8
KH
3194 return 0;
3195}
3196
1ecaab2b
KH
3197static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
3198{
3199 kfree(mem->info.nodeinfo[node]);
3200}
3201
c8dad2bb
JB
3202static int mem_cgroup_size(void)
3203{
3204 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
3205 return sizeof(struct mem_cgroup) + cpustat_size;
3206}
3207
33327948
KH
3208static struct mem_cgroup *mem_cgroup_alloc(void)
3209{
3210 struct mem_cgroup *mem;
c8dad2bb 3211 int size = mem_cgroup_size();
33327948 3212
c8dad2bb
JB
3213 if (size < PAGE_SIZE)
3214 mem = kmalloc(size, GFP_KERNEL);
33327948 3215 else
c8dad2bb 3216 mem = vmalloc(size);
33327948
KH
3217
3218 if (mem)
c8dad2bb 3219 memset(mem, 0, size);
33327948
KH
3220 return mem;
3221}
3222
8c7c6e34
KH
3223/*
3224 * At destroying mem_cgroup, references from swap_cgroup can remain.
3225 * (scanning all at force_empty is too costly...)
3226 *
3227 * Instead of clearing all references at force_empty, we remember
3228 * the number of reference from swap_cgroup and free mem_cgroup when
3229 * it goes down to 0.
3230 *
8c7c6e34
KH
3231 * Removal of cgroup itself succeeds regardless of refs from swap.
3232 */
3233
a7ba0eef 3234static void __mem_cgroup_free(struct mem_cgroup *mem)
33327948 3235{
08e552c6
KH
3236 int node;
3237
f64c3f54 3238 mem_cgroup_remove_from_trees(mem);
04046e1a
KH
3239 free_css_id(&mem_cgroup_subsys, &mem->css);
3240
08e552c6
KH
3241 for_each_node_state(node, N_POSSIBLE)
3242 free_mem_cgroup_per_zone_info(mem, node);
3243
c8dad2bb 3244 if (mem_cgroup_size() < PAGE_SIZE)
33327948
KH
3245 kfree(mem);
3246 else
3247 vfree(mem);
3248}
3249
8c7c6e34
KH
3250static void mem_cgroup_get(struct mem_cgroup *mem)
3251{
3252 atomic_inc(&mem->refcnt);
3253}
3254
3255static void mem_cgroup_put(struct mem_cgroup *mem)
3256{
7bcc1bb1
DN
3257 if (atomic_dec_and_test(&mem->refcnt)) {
3258 struct mem_cgroup *parent = parent_mem_cgroup(mem);
a7ba0eef 3259 __mem_cgroup_free(mem);
7bcc1bb1
DN
3260 if (parent)
3261 mem_cgroup_put(parent);
3262 }
8c7c6e34
KH
3263}
3264
7bcc1bb1
DN
3265/*
3266 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
3267 */
3268static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
3269{
3270 if (!mem->res.parent)
3271 return NULL;
3272 return mem_cgroup_from_res_counter(mem->res.parent, res);
3273}
33327948 3274
c077719b
KH
3275#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3276static void __init enable_swap_cgroup(void)
3277{
f8d66542 3278 if (!mem_cgroup_disabled() && really_do_swap_account)
c077719b
KH
3279 do_swap_account = 1;
3280}
3281#else
3282static void __init enable_swap_cgroup(void)
3283{
3284}
3285#endif
3286
f64c3f54
BS
3287static int mem_cgroup_soft_limit_tree_init(void)
3288{
3289 struct mem_cgroup_tree_per_node *rtpn;
3290 struct mem_cgroup_tree_per_zone *rtpz;
3291 int tmp, node, zone;
3292
3293 for_each_node_state(node, N_POSSIBLE) {
3294 tmp = node;
3295 if (!node_state(node, N_NORMAL_MEMORY))
3296 tmp = -1;
3297 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
3298 if (!rtpn)
3299 return 1;
3300
3301 soft_limit_tree.rb_tree_per_node[node] = rtpn;
3302
3303 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
3304 rtpz = &rtpn->rb_tree_per_zone[zone];
3305 rtpz->rb_root = RB_ROOT;
3306 spin_lock_init(&rtpz->lock);
3307 }
3308 }
3309 return 0;
3310}
3311
0eb253e2 3312static struct cgroup_subsys_state * __ref
8cdea7c0
BS
3313mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
3314{
28dbc4b6 3315 struct mem_cgroup *mem, *parent;
04046e1a 3316 long error = -ENOMEM;
6d12e2d8 3317 int node;
8cdea7c0 3318
c8dad2bb
JB
3319 mem = mem_cgroup_alloc();
3320 if (!mem)
04046e1a 3321 return ERR_PTR(error);
78fb7466 3322
6d12e2d8
KH
3323 for_each_node_state(node, N_POSSIBLE)
3324 if (alloc_mem_cgroup_per_zone_info(mem, node))
3325 goto free_out;
f64c3f54 3326
c077719b 3327 /* root ? */
28dbc4b6 3328 if (cont->parent == NULL) {
cdec2e42 3329 int cpu;
c077719b 3330 enable_swap_cgroup();
28dbc4b6 3331 parent = NULL;
4b3bde4c 3332 root_mem_cgroup = mem;
f64c3f54
BS
3333 if (mem_cgroup_soft_limit_tree_init())
3334 goto free_out;
cdec2e42
KH
3335 for_each_possible_cpu(cpu) {
3336 struct memcg_stock_pcp *stock =
3337 &per_cpu(memcg_stock, cpu);
3338 INIT_WORK(&stock->work, drain_local_stock);
3339 }
3340 hotcpu_notifier(memcg_stock_cpu_callback, 0);
f64c3f54 3341
18f59ea7 3342 } else {
28dbc4b6 3343 parent = mem_cgroup_from_cont(cont->parent);
18f59ea7
BS
3344 mem->use_hierarchy = parent->use_hierarchy;
3345 }
28dbc4b6 3346
18f59ea7
BS
3347 if (parent && parent->use_hierarchy) {
3348 res_counter_init(&mem->res, &parent->res);
3349 res_counter_init(&mem->memsw, &parent->memsw);
7bcc1bb1
DN
3350 /*
3351 * We increment refcnt of the parent to ensure that we can
3352 * safely access it on res_counter_charge/uncharge.
3353 * This refcnt will be decremented when freeing this
3354 * mem_cgroup(see mem_cgroup_put).
3355 */
3356 mem_cgroup_get(parent);
18f59ea7
BS
3357 } else {
3358 res_counter_init(&mem->res, NULL);
3359 res_counter_init(&mem->memsw, NULL);
3360 }
04046e1a 3361 mem->last_scanned_child = 0;
2733c06a 3362 spin_lock_init(&mem->reclaim_param_lock);
6d61ef40 3363
a7885eb8
KM
3364 if (parent)
3365 mem->swappiness = get_swappiness(parent);
a7ba0eef 3366 atomic_set(&mem->refcnt, 1);
8cdea7c0 3367 return &mem->css;
6d12e2d8 3368free_out:
a7ba0eef 3369 __mem_cgroup_free(mem);
4b3bde4c 3370 root_mem_cgroup = NULL;
04046e1a 3371 return ERR_PTR(error);
8cdea7c0
BS
3372}
3373
ec64f515 3374static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
df878fb0
KH
3375 struct cgroup *cont)
3376{
3377 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
ec64f515
KH
3378
3379 return mem_cgroup_force_empty(mem, false);
df878fb0
KH
3380}
3381
8cdea7c0
BS
3382static void mem_cgroup_destroy(struct cgroup_subsys *ss,
3383 struct cgroup *cont)
3384{
c268e994 3385 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
c268e994 3386
c268e994 3387 mem_cgroup_put(mem);
8cdea7c0
BS
3388}
3389
3390static int mem_cgroup_populate(struct cgroup_subsys *ss,
3391 struct cgroup *cont)
3392{
8c7c6e34
KH
3393 int ret;
3394
3395 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
3396 ARRAY_SIZE(mem_cgroup_files));
3397
3398 if (!ret)
3399 ret = register_memsw_files(cont, ss);
3400 return ret;
8cdea7c0
BS
3401}
3402
67e465a7
BS
3403static void mem_cgroup_move_task(struct cgroup_subsys *ss,
3404 struct cgroup *cont,
3405 struct cgroup *old_cont,
be367d09
BB
3406 struct task_struct *p,
3407 bool threadgroup)
67e465a7 3408{
7f4d454d 3409 mutex_lock(&memcg_tasklist);
67e465a7 3410 /*
f9717d28
NK
3411 * FIXME: It's better to move charges of this process from old
3412 * memcg to new memcg. But it's just on TODO-List now.
67e465a7 3413 */
7f4d454d 3414 mutex_unlock(&memcg_tasklist);
67e465a7
BS
3415}
3416
8cdea7c0
BS
3417struct cgroup_subsys mem_cgroup_subsys = {
3418 .name = "memory",
3419 .subsys_id = mem_cgroup_subsys_id,
3420 .create = mem_cgroup_create,
df878fb0 3421 .pre_destroy = mem_cgroup_pre_destroy,
8cdea7c0
BS
3422 .destroy = mem_cgroup_destroy,
3423 .populate = mem_cgroup_populate,
67e465a7 3424 .attach = mem_cgroup_move_task,
6d12e2d8 3425 .early_init = 0,
04046e1a 3426 .use_id = 1,
8cdea7c0 3427};
c077719b
KH
3428
3429#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3430
3431static int __init disable_swap_account(char *s)
3432{
3433 really_do_swap_account = 0;
3434 return 1;
3435}
3436__setup("noswapaccount", disable_swap_account);
3437#endif