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