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