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