]> bbs.cooldavid.org Git - net-next-2.6.git/blame - mm/memcontrol.c
memcg: mem+swap controller core
[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>
8cdea7c0 39
8697d331
BS
40#include <asm/uaccess.h>
41
a181b0e8 42struct cgroup_subsys mem_cgroup_subsys __read_mostly;
a181b0e8 43#define MEM_CGROUP_RECLAIM_RETRIES 5
8cdea7c0 44
c077719b
KH
45#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
46/* Turned on only when memory cgroup is enabled && really_do_swap_account = 0 */
47int do_swap_account __read_mostly;
48static int really_do_swap_account __initdata = 1; /* for remember boot option*/
49#else
50#define do_swap_account (0)
51#endif
52
53
d52aa412
KH
54/*
55 * Statistics for memory cgroup.
56 */
57enum mem_cgroup_stat_index {
58 /*
59 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
60 */
61 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
62 MEM_CGROUP_STAT_RSS, /* # of pages charged as rss */
55e462b0
BR
63 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
64 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
d52aa412
KH
65
66 MEM_CGROUP_STAT_NSTATS,
67};
68
69struct mem_cgroup_stat_cpu {
70 s64 count[MEM_CGROUP_STAT_NSTATS];
71} ____cacheline_aligned_in_smp;
72
73struct mem_cgroup_stat {
c8dad2bb 74 struct mem_cgroup_stat_cpu cpustat[0];
d52aa412
KH
75};
76
77/*
78 * For accounting under irq disable, no need for increment preempt count.
79 */
addb9efe 80static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
d52aa412
KH
81 enum mem_cgroup_stat_index idx, int val)
82{
addb9efe 83 stat->count[idx] += val;
d52aa412
KH
84}
85
86static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
87 enum mem_cgroup_stat_index idx)
88{
89 int cpu;
90 s64 ret = 0;
91 for_each_possible_cpu(cpu)
92 ret += stat->cpustat[cpu].count[idx];
93 return ret;
94}
95
6d12e2d8
KH
96/*
97 * per-zone information in memory controller.
98 */
6d12e2d8 99struct mem_cgroup_per_zone {
072c56c1
KH
100 /*
101 * spin_lock to protect the per cgroup LRU
102 */
103 spinlock_t lru_lock;
b69408e8
CL
104 struct list_head lists[NR_LRU_LISTS];
105 unsigned long count[NR_LRU_LISTS];
6d12e2d8
KH
106};
107/* Macro for accessing counter */
108#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
109
110struct mem_cgroup_per_node {
111 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
112};
113
114struct mem_cgroup_lru_info {
115 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
116};
117
8cdea7c0
BS
118/*
119 * The memory controller data structure. The memory controller controls both
120 * page cache and RSS per cgroup. We would eventually like to provide
121 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
122 * to help the administrator determine what knobs to tune.
123 *
124 * TODO: Add a water mark for the memory controller. Reclaim will begin when
8a9f3ccd
BS
125 * we hit the water mark. May be even add a low water mark, such that
126 * no reclaim occurs from a cgroup at it's low water mark, this is
127 * a feature that will be implemented much later in the future.
8cdea7c0
BS
128 */
129struct mem_cgroup {
130 struct cgroup_subsys_state css;
131 /*
132 * the counter to account for memory usage
133 */
134 struct res_counter res;
8c7c6e34
KH
135 /*
136 * the counter to account for mem+swap usage.
137 */
138 struct res_counter memsw;
78fb7466
PE
139 /*
140 * Per cgroup active and inactive list, similar to the
141 * per zone LRU lists.
78fb7466 142 */
6d12e2d8 143 struct mem_cgroup_lru_info info;
072c56c1 144
6c48a1d0 145 int prev_priority; /* for recording reclaim priority */
8c7c6e34
KH
146 int obsolete;
147 atomic_t refcnt;
d52aa412 148 /*
c8dad2bb 149 * statistics. This must be placed at the end of memcg.
d52aa412
KH
150 */
151 struct mem_cgroup_stat stat;
8cdea7c0
BS
152};
153
217bc319
KH
154enum charge_type {
155 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
156 MEM_CGROUP_CHARGE_TYPE_MAPPED,
4f98a2fe 157 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
c05555b5 158 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
d13d1443 159 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
c05555b5
KH
160 NR_CHARGE_TYPE,
161};
162
52d4b9ac
KH
163/* only for here (for easy reading.) */
164#define PCGF_CACHE (1UL << PCG_CACHE)
165#define PCGF_USED (1UL << PCG_USED)
166#define PCGF_ACTIVE (1UL << PCG_ACTIVE)
167#define PCGF_LOCK (1UL << PCG_LOCK)
168#define PCGF_FILE (1UL << PCG_FILE)
c05555b5
KH
169static const unsigned long
170pcg_default_flags[NR_CHARGE_TYPE] = {
52d4b9ac
KH
171 PCGF_CACHE | PCGF_FILE | PCGF_USED | PCGF_LOCK, /* File Cache */
172 PCGF_ACTIVE | PCGF_USED | PCGF_LOCK, /* Anon */
173 PCGF_ACTIVE | PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* Shmem */
174 0, /* FORCE */
217bc319
KH
175};
176
8c7c6e34
KH
177
178/* for encoding cft->private value on file */
179#define _MEM (0)
180#define _MEMSWAP (1)
181#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
182#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
183#define MEMFILE_ATTR(val) ((val) & 0xffff)
184
185static void mem_cgroup_get(struct mem_cgroup *mem);
186static void mem_cgroup_put(struct mem_cgroup *mem);
187
d52aa412
KH
188/*
189 * Always modified under lru lock. Then, not necessary to preempt_disable()
190 */
c05555b5
KH
191static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
192 struct page_cgroup *pc,
193 bool charge)
d52aa412
KH
194{
195 int val = (charge)? 1 : -1;
196 struct mem_cgroup_stat *stat = &mem->stat;
addb9efe 197 struct mem_cgroup_stat_cpu *cpustat;
d52aa412 198
8869b8f6 199 VM_BUG_ON(!irqs_disabled());
addb9efe
KH
200
201 cpustat = &stat->cpustat[smp_processor_id()];
c05555b5 202 if (PageCgroupCache(pc))
addb9efe 203 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
d52aa412 204 else
addb9efe 205 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
55e462b0
BR
206
207 if (charge)
addb9efe 208 __mem_cgroup_stat_add_safe(cpustat,
55e462b0
BR
209 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
210 else
addb9efe 211 __mem_cgroup_stat_add_safe(cpustat,
55e462b0 212 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
6d12e2d8
KH
213}
214
d5b69e38 215static struct mem_cgroup_per_zone *
6d12e2d8
KH
216mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
217{
6d12e2d8
KH
218 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
219}
220
d5b69e38 221static struct mem_cgroup_per_zone *
6d12e2d8
KH
222page_cgroup_zoneinfo(struct page_cgroup *pc)
223{
224 struct mem_cgroup *mem = pc->mem_cgroup;
225 int nid = page_cgroup_nid(pc);
226 int zid = page_cgroup_zid(pc);
d52aa412 227
6d12e2d8
KH
228 return mem_cgroup_zoneinfo(mem, nid, zid);
229}
230
231static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem,
b69408e8 232 enum lru_list idx)
6d12e2d8
KH
233{
234 int nid, zid;
235 struct mem_cgroup_per_zone *mz;
236 u64 total = 0;
237
238 for_each_online_node(nid)
239 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
240 mz = mem_cgroup_zoneinfo(mem, nid, zid);
241 total += MEM_CGROUP_ZSTAT(mz, idx);
242 }
243 return total;
d52aa412
KH
244}
245
d5b69e38 246static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
8cdea7c0
BS
247{
248 return container_of(cgroup_subsys_state(cont,
249 mem_cgroup_subsys_id), struct mem_cgroup,
250 css);
251}
252
cf475ad2 253struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
78fb7466 254{
31a78f23
BS
255 /*
256 * mm_update_next_owner() may clear mm->owner to NULL
257 * if it races with swapoff, page migration, etc.
258 * So this can be called with p == NULL.
259 */
260 if (unlikely(!p))
261 return NULL;
262
78fb7466
PE
263 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
264 struct mem_cgroup, css);
265}
266
3eae90c3
KH
267static void __mem_cgroup_remove_list(struct mem_cgroup_per_zone *mz,
268 struct page_cgroup *pc)
6d12e2d8 269{
4f98a2fe
RR
270 int lru = LRU_BASE;
271
c05555b5 272 if (PageCgroupUnevictable(pc))
894bc310
LS
273 lru = LRU_UNEVICTABLE;
274 else {
c05555b5 275 if (PageCgroupActive(pc))
894bc310 276 lru += LRU_ACTIVE;
c05555b5 277 if (PageCgroupFile(pc))
894bc310
LS
278 lru += LRU_FILE;
279 }
6d12e2d8 280
b69408e8 281 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
6d12e2d8 282
c05555b5 283 mem_cgroup_charge_statistics(pc->mem_cgroup, pc, false);
508b7be0 284 list_del(&pc->lru);
6d12e2d8
KH
285}
286
3eae90c3 287static void __mem_cgroup_add_list(struct mem_cgroup_per_zone *mz,
f817ed48 288 struct page_cgroup *pc, bool hot)
6d12e2d8 289{
4f98a2fe 290 int lru = LRU_BASE;
b69408e8 291
c05555b5 292 if (PageCgroupUnevictable(pc))
894bc310
LS
293 lru = LRU_UNEVICTABLE;
294 else {
c05555b5 295 if (PageCgroupActive(pc))
894bc310 296 lru += LRU_ACTIVE;
c05555b5 297 if (PageCgroupFile(pc))
894bc310
LS
298 lru += LRU_FILE;
299 }
b69408e8
CL
300
301 MEM_CGROUP_ZSTAT(mz, lru) += 1;
f817ed48
KH
302 if (hot)
303 list_add(&pc->lru, &mz->lists[lru]);
304 else
305 list_add_tail(&pc->lru, &mz->lists[lru]);
6d12e2d8 306
c05555b5 307 mem_cgroup_charge_statistics(pc->mem_cgroup, pc, true);
6d12e2d8
KH
308}
309
894bc310 310static void __mem_cgroup_move_lists(struct page_cgroup *pc, enum lru_list lru)
66e1707b 311{
6d12e2d8 312 struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
c05555b5
KH
313 int active = PageCgroupActive(pc);
314 int file = PageCgroupFile(pc);
315 int unevictable = PageCgroupUnevictable(pc);
894bc310
LS
316 enum lru_list from = unevictable ? LRU_UNEVICTABLE :
317 (LRU_FILE * !!file + !!active);
6d12e2d8 318
894bc310
LS
319 if (lru == from)
320 return;
b69408e8 321
894bc310 322 MEM_CGROUP_ZSTAT(mz, from) -= 1;
c05555b5
KH
323 /*
324 * However this is done under mz->lru_lock, another flags, which
325 * are not related to LRU, will be modified from out-of-lock.
326 * We have to use atomic set/clear flags.
327 */
894bc310 328 if (is_unevictable_lru(lru)) {
c05555b5
KH
329 ClearPageCgroupActive(pc);
330 SetPageCgroupUnevictable(pc);
894bc310
LS
331 } else {
332 if (is_active_lru(lru))
c05555b5 333 SetPageCgroupActive(pc);
894bc310 334 else
c05555b5
KH
335 ClearPageCgroupActive(pc);
336 ClearPageCgroupUnevictable(pc);
894bc310 337 }
b69408e8 338
b69408e8
CL
339 MEM_CGROUP_ZSTAT(mz, lru) += 1;
340 list_move(&pc->lru, &mz->lists[lru]);
66e1707b
BS
341}
342
4c4a2214
DR
343int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
344{
345 int ret;
346
347 task_lock(task);
bd845e38 348 ret = task->mm && mm_match_cgroup(task->mm, mem);
4c4a2214
DR
349 task_unlock(task);
350 return ret;
351}
352
66e1707b
BS
353/*
354 * This routine assumes that the appropriate zone's lru lock is already held
355 */
894bc310 356void mem_cgroup_move_lists(struct page *page, enum lru_list lru)
66e1707b 357{
427d5416 358 struct page_cgroup *pc;
072c56c1
KH
359 struct mem_cgroup_per_zone *mz;
360 unsigned long flags;
361
cede86ac
LZ
362 if (mem_cgroup_subsys.disabled)
363 return;
364
2680eed7
HD
365 /*
366 * We cannot lock_page_cgroup while holding zone's lru_lock,
367 * because other holders of lock_page_cgroup can be interrupted
368 * with an attempt to rotate_reclaimable_page. But we cannot
369 * safely get to page_cgroup without it, so just try_lock it:
370 * mem_cgroup_isolate_pages allows for page left on wrong list.
371 */
52d4b9ac
KH
372 pc = lookup_page_cgroup(page);
373 if (!trylock_page_cgroup(pc))
66e1707b 374 return;
52d4b9ac 375 if (pc && PageCgroupUsed(pc)) {
2680eed7 376 mz = page_cgroup_zoneinfo(pc);
2680eed7 377 spin_lock_irqsave(&mz->lru_lock, flags);
894bc310 378 __mem_cgroup_move_lists(pc, lru);
2680eed7 379 spin_unlock_irqrestore(&mz->lru_lock, flags);
9b3c0a07 380 }
52d4b9ac 381 unlock_page_cgroup(pc);
66e1707b
BS
382}
383
58ae83db
KH
384/*
385 * Calculate mapped_ratio under memory controller. This will be used in
386 * vmscan.c for deteremining we have to reclaim mapped pages.
387 */
388int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
389{
390 long total, rss;
391
392 /*
393 * usage is recorded in bytes. But, here, we assume the number of
394 * physical pages can be represented by "long" on any arch.
395 */
396 total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
397 rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
398 return (int)((rss * 100L) / total);
399}
8869b8f6 400
6c48a1d0
KH
401/*
402 * prev_priority control...this will be used in memory reclaim path.
403 */
404int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
405{
406 return mem->prev_priority;
407}
408
409void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
410{
411 if (priority < mem->prev_priority)
412 mem->prev_priority = priority;
413}
414
415void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
416{
417 mem->prev_priority = priority;
418}
419
cc38108e
KH
420/*
421 * Calculate # of pages to be scanned in this priority/zone.
422 * See also vmscan.c
423 *
424 * priority starts from "DEF_PRIORITY" and decremented in each loop.
425 * (see include/linux/mmzone.h)
426 */
427
b69408e8
CL
428long mem_cgroup_calc_reclaim(struct mem_cgroup *mem, struct zone *zone,
429 int priority, enum lru_list lru)
cc38108e 430{
b69408e8 431 long nr_pages;
cc38108e
KH
432 int nid = zone->zone_pgdat->node_id;
433 int zid = zone_idx(zone);
434 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
435
b69408e8 436 nr_pages = MEM_CGROUP_ZSTAT(mz, lru);
cc38108e 437
b69408e8 438 return (nr_pages >> priority);
cc38108e
KH
439}
440
66e1707b
BS
441unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
442 struct list_head *dst,
443 unsigned long *scanned, int order,
444 int mode, struct zone *z,
445 struct mem_cgroup *mem_cont,
4f98a2fe 446 int active, int file)
66e1707b
BS
447{
448 unsigned long nr_taken = 0;
449 struct page *page;
450 unsigned long scan;
451 LIST_HEAD(pc_list);
452 struct list_head *src;
ff7283fa 453 struct page_cgroup *pc, *tmp;
1ecaab2b
KH
454 int nid = z->zone_pgdat->node_id;
455 int zid = zone_idx(z);
456 struct mem_cgroup_per_zone *mz;
4f98a2fe 457 int lru = LRU_FILE * !!file + !!active;
66e1707b 458
cf475ad2 459 BUG_ON(!mem_cont);
1ecaab2b 460 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
b69408e8 461 src = &mz->lists[lru];
66e1707b 462
072c56c1 463 spin_lock(&mz->lru_lock);
ff7283fa
KH
464 scan = 0;
465 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
436c6541 466 if (scan >= nr_to_scan)
ff7283fa 467 break;
52d4b9ac
KH
468 if (unlikely(!PageCgroupUsed(pc)))
469 continue;
66e1707b 470 page = pc->page;
66e1707b 471
436c6541 472 if (unlikely(!PageLRU(page)))
ff7283fa 473 continue;
ff7283fa 474
4f98a2fe
RR
475 /*
476 * TODO: play better with lumpy reclaim, grabbing anything.
477 */
894bc310
LS
478 if (PageUnevictable(page) ||
479 (PageActive(page) && !active) ||
480 (!PageActive(page) && active)) {
481 __mem_cgroup_move_lists(pc, page_lru(page));
66e1707b
BS
482 continue;
483 }
484
436c6541
HD
485 scan++;
486 list_move(&pc->lru, &pc_list);
66e1707b 487
4f98a2fe 488 if (__isolate_lru_page(page, mode, file) == 0) {
66e1707b
BS
489 list_move(&page->lru, dst);
490 nr_taken++;
491 }
492 }
493
494 list_splice(&pc_list, src);
072c56c1 495 spin_unlock(&mz->lru_lock);
66e1707b
BS
496
497 *scanned = scan;
498 return nr_taken;
499}
500
f817ed48
KH
501/*
502 * Unlike exported interface, "oom" parameter is added. if oom==true,
503 * oom-killer can be invoked.
8a9f3ccd 504 */
f817ed48 505static int __mem_cgroup_try_charge(struct mm_struct *mm,
8c7c6e34
KH
506 gfp_t gfp_mask, struct mem_cgroup **memcg,
507 bool oom)
8a9f3ccd
BS
508{
509 struct mem_cgroup *mem;
7a81b88c 510 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
8a9f3ccd 511 /*
3be91277
HD
512 * We always charge the cgroup the mm_struct belongs to.
513 * The mm_struct's mem_cgroup changes on task migration if the
8a9f3ccd
BS
514 * thread group leader migrates. It's possible that mm is not
515 * set, if so charge the init_mm (happens for pagecache usage).
516 */
7a81b88c 517 if (likely(!*memcg)) {
e8589cc1
KH
518 rcu_read_lock();
519 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
31a78f23
BS
520 if (unlikely(!mem)) {
521 rcu_read_unlock();
31a78f23
BS
522 return 0;
523 }
e8589cc1
KH
524 /*
525 * For every charge from the cgroup, increment reference count
526 */
527 css_get(&mem->css);
7a81b88c 528 *memcg = mem;
e8589cc1
KH
529 rcu_read_unlock();
530 } else {
7a81b88c
KH
531 mem = *memcg;
532 css_get(&mem->css);
e8589cc1 533 }
8a9f3ccd 534
8c7c6e34
KH
535 while (1) {
536 int ret;
537 bool noswap = false;
7a81b88c 538
8c7c6e34
KH
539 ret = res_counter_charge(&mem->res, PAGE_SIZE);
540 if (likely(!ret)) {
541 if (!do_swap_account)
542 break;
543 ret = res_counter_charge(&mem->memsw, PAGE_SIZE);
544 if (likely(!ret))
545 break;
546 /* mem+swap counter fails */
547 res_counter_uncharge(&mem->res, PAGE_SIZE);
548 noswap = true;
549 }
3be91277 550 if (!(gfp_mask & __GFP_WAIT))
7a81b88c 551 goto nomem;
e1a1cd59 552
8c7c6e34 553 if (try_to_free_mem_cgroup_pages(mem, gfp_mask, noswap))
66e1707b
BS
554 continue;
555
556 /*
8869b8f6
HD
557 * try_to_free_mem_cgroup_pages() might not give us a full
558 * picture of reclaim. Some pages are reclaimed and might be
559 * moved to swap cache or just unmapped from the cgroup.
560 * Check the limit again to see if the reclaim reduced the
561 * current usage of the cgroup before giving up
8c7c6e34 562 *
8869b8f6 563 */
8c7c6e34
KH
564 if (!do_swap_account &&
565 res_counter_check_under_limit(&mem->res))
566 continue;
567 if (do_swap_account &&
568 res_counter_check_under_limit(&mem->memsw))
66e1707b 569 continue;
3be91277
HD
570
571 if (!nr_retries--) {
f817ed48
KH
572 if (oom)
573 mem_cgroup_out_of_memory(mem, gfp_mask);
7a81b88c 574 goto nomem;
66e1707b 575 }
8a9f3ccd 576 }
7a81b88c
KH
577 return 0;
578nomem:
579 css_put(&mem->css);
580 return -ENOMEM;
581}
8a9f3ccd 582
f817ed48
KH
583/**
584 * mem_cgroup_try_charge - get charge of PAGE_SIZE.
585 * @mm: an mm_struct which is charged against. (when *memcg is NULL)
586 * @gfp_mask: gfp_mask for reclaim.
587 * @memcg: a pointer to memory cgroup which is charged against.
588 *
589 * charge against memory cgroup pointed by *memcg. if *memcg == NULL, estimated
590 * memory cgroup from @mm is got and stored in *memcg.
591 *
592 * Returns 0 if success. -ENOMEM at failure.
593 * This call can invoke OOM-Killer.
594 */
595
596int mem_cgroup_try_charge(struct mm_struct *mm,
597 gfp_t mask, struct mem_cgroup **memcg)
598{
599 return __mem_cgroup_try_charge(mm, mask, memcg, true);
600}
601
7a81b88c
KH
602/*
603 * commit a charge got by mem_cgroup_try_charge() and makes page_cgroup to be
604 * USED state. If already USED, uncharge and return.
605 */
606
607static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
608 struct page_cgroup *pc,
609 enum charge_type ctype)
610{
611 struct mem_cgroup_per_zone *mz;
612 unsigned long flags;
613
614 /* try_charge() can return NULL to *memcg, taking care of it. */
615 if (!mem)
616 return;
52d4b9ac
KH
617
618 lock_page_cgroup(pc);
619 if (unlikely(PageCgroupUsed(pc))) {
620 unlock_page_cgroup(pc);
621 res_counter_uncharge(&mem->res, PAGE_SIZE);
8c7c6e34
KH
622 if (do_swap_account)
623 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
52d4b9ac 624 css_put(&mem->css);
7a81b88c 625 return;
52d4b9ac 626 }
8a9f3ccd 627 pc->mem_cgroup = mem;
508b7be0
KH
628 /*
629 * If a page is accounted as a page cache, insert to inactive list.
630 * If anon, insert to active list.
631 */
c05555b5 632 pc->flags = pcg_default_flags[ctype];
3be91277 633
072c56c1 634 mz = page_cgroup_zoneinfo(pc);
52d4b9ac 635
072c56c1 636 spin_lock_irqsave(&mz->lru_lock, flags);
f817ed48 637 __mem_cgroup_add_list(mz, pc, true);
072c56c1 638 spin_unlock_irqrestore(&mz->lru_lock, flags);
52d4b9ac 639 unlock_page_cgroup(pc);
7a81b88c 640}
66e1707b 641
f817ed48
KH
642/**
643 * mem_cgroup_move_account - move account of the page
644 * @pc: page_cgroup of the page.
645 * @from: mem_cgroup which the page is moved from.
646 * @to: mem_cgroup which the page is moved to. @from != @to.
647 *
648 * The caller must confirm following.
649 * 1. disable irq.
650 * 2. lru_lock of old mem_cgroup(@from) should be held.
651 *
652 * returns 0 at success,
653 * returns -EBUSY when lock is busy or "pc" is unstable.
654 *
655 * This function does "uncharge" from old cgroup but doesn't do "charge" to
656 * new cgroup. It should be done by a caller.
657 */
658
659static int mem_cgroup_move_account(struct page_cgroup *pc,
660 struct mem_cgroup *from, struct mem_cgroup *to)
661{
662 struct mem_cgroup_per_zone *from_mz, *to_mz;
663 int nid, zid;
664 int ret = -EBUSY;
665
666 VM_BUG_ON(!irqs_disabled());
667 VM_BUG_ON(from == to);
668
669 nid = page_cgroup_nid(pc);
670 zid = page_cgroup_zid(pc);
671 from_mz = mem_cgroup_zoneinfo(from, nid, zid);
672 to_mz = mem_cgroup_zoneinfo(to, nid, zid);
673
674
675 if (!trylock_page_cgroup(pc))
676 return ret;
677
678 if (!PageCgroupUsed(pc))
679 goto out;
680
681 if (pc->mem_cgroup != from)
682 goto out;
683
684 if (spin_trylock(&to_mz->lru_lock)) {
685 __mem_cgroup_remove_list(from_mz, pc);
686 css_put(&from->css);
687 res_counter_uncharge(&from->res, PAGE_SIZE);
8c7c6e34
KH
688 if (do_swap_account)
689 res_counter_uncharge(&from->memsw, PAGE_SIZE);
f817ed48
KH
690 pc->mem_cgroup = to;
691 css_get(&to->css);
692 __mem_cgroup_add_list(to_mz, pc, false);
693 ret = 0;
694 spin_unlock(&to_mz->lru_lock);
695 }
696out:
697 unlock_page_cgroup(pc);
698 return ret;
699}
700
701/*
702 * move charges to its parent.
703 */
704
705static int mem_cgroup_move_parent(struct page_cgroup *pc,
706 struct mem_cgroup *child,
707 gfp_t gfp_mask)
708{
709 struct cgroup *cg = child->css.cgroup;
710 struct cgroup *pcg = cg->parent;
711 struct mem_cgroup *parent;
712 struct mem_cgroup_per_zone *mz;
713 unsigned long flags;
714 int ret;
715
716 /* Is ROOT ? */
717 if (!pcg)
718 return -EINVAL;
719
720 parent = mem_cgroup_from_cont(pcg);
721
722 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
723 if (ret)
724 return ret;
725
726 mz = mem_cgroup_zoneinfo(child,
727 page_cgroup_nid(pc), page_cgroup_zid(pc));
728
729 spin_lock_irqsave(&mz->lru_lock, flags);
730 ret = mem_cgroup_move_account(pc, child, parent);
731 spin_unlock_irqrestore(&mz->lru_lock, flags);
732
733 /* drop extra refcnt */
734 css_put(&parent->css);
735 /* uncharge if move fails */
8c7c6e34 736 if (ret) {
f817ed48 737 res_counter_uncharge(&parent->res, PAGE_SIZE);
8c7c6e34
KH
738 if (do_swap_account)
739 res_counter_uncharge(&parent->memsw, PAGE_SIZE);
740 }
f817ed48
KH
741
742 return ret;
743}
744
7a81b88c
KH
745/*
746 * Charge the memory controller for page usage.
747 * Return
748 * 0 if the charge was successful
749 * < 0 if the cgroup is over its limit
750 */
751static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
752 gfp_t gfp_mask, enum charge_type ctype,
753 struct mem_cgroup *memcg)
754{
755 struct mem_cgroup *mem;
756 struct page_cgroup *pc;
757 int ret;
758
759 pc = lookup_page_cgroup(page);
760 /* can happen at boot */
761 if (unlikely(!pc))
762 return 0;
763 prefetchw(pc);
764
765 mem = memcg;
f817ed48 766 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
7a81b88c
KH
767 if (ret)
768 return ret;
769
770 __mem_cgroup_commit_charge(mem, pc, ctype);
8a9f3ccd 771 return 0;
8a9f3ccd
BS
772}
773
7a81b88c
KH
774int mem_cgroup_newpage_charge(struct page *page,
775 struct mm_struct *mm, gfp_t gfp_mask)
217bc319 776{
cede86ac
LZ
777 if (mem_cgroup_subsys.disabled)
778 return 0;
52d4b9ac
KH
779 if (PageCompound(page))
780 return 0;
69029cd5
KH
781 /*
782 * If already mapped, we don't have to account.
783 * If page cache, page->mapping has address_space.
784 * But page->mapping may have out-of-use anon_vma pointer,
785 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
786 * is NULL.
787 */
788 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
789 return 0;
790 if (unlikely(!mm))
791 mm = &init_mm;
217bc319 792 return mem_cgroup_charge_common(page, mm, gfp_mask,
e8589cc1 793 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
217bc319
KH
794}
795
e1a1cd59
BS
796int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
797 gfp_t gfp_mask)
8697d331 798{
cede86ac
LZ
799 if (mem_cgroup_subsys.disabled)
800 return 0;
52d4b9ac
KH
801 if (PageCompound(page))
802 return 0;
accf163e
KH
803 /*
804 * Corner case handling. This is called from add_to_page_cache()
805 * in usual. But some FS (shmem) precharges this page before calling it
806 * and call add_to_page_cache() with GFP_NOWAIT.
807 *
808 * For GFP_NOWAIT case, the page may be pre-charged before calling
809 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
810 * charge twice. (It works but has to pay a bit larger cost.)
811 */
812 if (!(gfp_mask & __GFP_WAIT)) {
813 struct page_cgroup *pc;
814
52d4b9ac
KH
815
816 pc = lookup_page_cgroup(page);
817 if (!pc)
818 return 0;
819 lock_page_cgroup(pc);
820 if (PageCgroupUsed(pc)) {
821 unlock_page_cgroup(pc);
accf163e
KH
822 return 0;
823 }
52d4b9ac 824 unlock_page_cgroup(pc);
accf163e
KH
825 }
826
69029cd5 827 if (unlikely(!mm))
8697d331 828 mm = &init_mm;
accf163e 829
c05555b5
KH
830 if (page_is_file_cache(page))
831 return mem_cgroup_charge_common(page, mm, gfp_mask,
e8589cc1 832 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
c05555b5
KH
833 else
834 return mem_cgroup_charge_common(page, mm, gfp_mask,
835 MEM_CGROUP_CHARGE_TYPE_SHMEM, NULL);
e8589cc1
KH
836}
837
8c7c6e34
KH
838int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
839 struct page *page,
840 gfp_t mask, struct mem_cgroup **ptr)
841{
842 struct mem_cgroup *mem;
843 swp_entry_t ent;
844
845 if (mem_cgroup_subsys.disabled)
846 return 0;
847
848 if (!do_swap_account)
849 goto charge_cur_mm;
850
851 /*
852 * A racing thread's fault, or swapoff, may have already updated
853 * the pte, and even removed page from swap cache: return success
854 * to go on to do_swap_page()'s pte_same() test, which should fail.
855 */
856 if (!PageSwapCache(page))
857 return 0;
858
859 ent.val = page_private(page);
860
861 mem = lookup_swap_cgroup(ent);
862 if (!mem || mem->obsolete)
863 goto charge_cur_mm;
864 *ptr = mem;
865 return __mem_cgroup_try_charge(NULL, mask, ptr, true);
866charge_cur_mm:
867 if (unlikely(!mm))
868 mm = &init_mm;
869 return __mem_cgroup_try_charge(mm, mask, ptr, true);
870}
871
d13d1443 872#ifdef CONFIG_SWAP
8c7c6e34 873
d13d1443
KH
874int mem_cgroup_cache_charge_swapin(struct page *page,
875 struct mm_struct *mm, gfp_t mask, bool locked)
876{
877 int ret = 0;
878
879 if (mem_cgroup_subsys.disabled)
880 return 0;
881 if (unlikely(!mm))
882 mm = &init_mm;
883 if (!locked)
884 lock_page(page);
885 /*
886 * If not locked, the page can be dropped from SwapCache until
887 * we reach here.
888 */
889 if (PageSwapCache(page)) {
8c7c6e34
KH
890 struct mem_cgroup *mem = NULL;
891 swp_entry_t ent;
892
893 ent.val = page_private(page);
894 if (do_swap_account) {
895 mem = lookup_swap_cgroup(ent);
896 if (mem && mem->obsolete)
897 mem = NULL;
898 if (mem)
899 mm = NULL;
900 }
d13d1443 901 ret = mem_cgroup_charge_common(page, mm, mask,
8c7c6e34
KH
902 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
903
904 if (!ret && do_swap_account) {
905 /* avoid double counting */
906 mem = swap_cgroup_record(ent, NULL);
907 if (mem) {
908 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
909 mem_cgroup_put(mem);
910 }
911 }
d13d1443
KH
912 }
913 if (!locked)
914 unlock_page(page);
915
916 return ret;
917}
918#endif
919
7a81b88c
KH
920void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
921{
922 struct page_cgroup *pc;
923
924 if (mem_cgroup_subsys.disabled)
925 return;
926 if (!ptr)
927 return;
928 pc = lookup_page_cgroup(page);
929 __mem_cgroup_commit_charge(ptr, pc, MEM_CGROUP_CHARGE_TYPE_MAPPED);
8c7c6e34
KH
930 /*
931 * Now swap is on-memory. This means this page may be
932 * counted both as mem and swap....double count.
933 * Fix it by uncharging from memsw. This SwapCache is stable
934 * because we're still under lock_page().
935 */
936 if (do_swap_account) {
937 swp_entry_t ent = {.val = page_private(page)};
938 struct mem_cgroup *memcg;
939 memcg = swap_cgroup_record(ent, NULL);
940 if (memcg) {
941 /* If memcg is obsolete, memcg can be != ptr */
942 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
943 mem_cgroup_put(memcg);
944 }
945
946 }
7a81b88c
KH
947}
948
949void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
950{
951 if (mem_cgroup_subsys.disabled)
952 return;
953 if (!mem)
954 return;
955 res_counter_uncharge(&mem->res, PAGE_SIZE);
8c7c6e34
KH
956 if (do_swap_account)
957 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
7a81b88c
KH
958 css_put(&mem->css);
959}
960
961
8a9f3ccd 962/*
69029cd5 963 * uncharge if !page_mapped(page)
8a9f3ccd 964 */
8c7c6e34 965static struct mem_cgroup *
69029cd5 966__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
8a9f3ccd 967{
8289546e 968 struct page_cgroup *pc;
8c7c6e34 969 struct mem_cgroup *mem = NULL;
072c56c1 970 struct mem_cgroup_per_zone *mz;
66e1707b 971 unsigned long flags;
8a9f3ccd 972
4077960e 973 if (mem_cgroup_subsys.disabled)
8c7c6e34 974 return NULL;
4077960e 975
d13d1443 976 if (PageSwapCache(page))
8c7c6e34 977 return NULL;
d13d1443 978
8697d331 979 /*
3c541e14 980 * Check if our page_cgroup is valid
8697d331 981 */
52d4b9ac
KH
982 pc = lookup_page_cgroup(page);
983 if (unlikely(!pc || !PageCgroupUsed(pc)))
8c7c6e34 984 return NULL;
b9c565d5 985
52d4b9ac 986 lock_page_cgroup(pc);
d13d1443 987
8c7c6e34
KH
988 mem = pc->mem_cgroup;
989
d13d1443
KH
990 if (!PageCgroupUsed(pc))
991 goto unlock_out;
992
993 switch (ctype) {
994 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
995 if (page_mapped(page))
996 goto unlock_out;
997 break;
998 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
999 if (!PageAnon(page)) { /* Shared memory */
1000 if (page->mapping && !page_is_file_cache(page))
1001 goto unlock_out;
1002 } else if (page_mapped(page)) /* Anon */
1003 goto unlock_out;
1004 break;
1005 default:
1006 break;
52d4b9ac 1007 }
d13d1443 1008
8c7c6e34
KH
1009 res_counter_uncharge(&mem->res, PAGE_SIZE);
1010 if (do_swap_account && (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT))
1011 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1012
52d4b9ac 1013 ClearPageCgroupUsed(pc);
b9c565d5 1014
69029cd5
KH
1015 mz = page_cgroup_zoneinfo(pc);
1016 spin_lock_irqsave(&mz->lru_lock, flags);
1017 __mem_cgroup_remove_list(mz, pc);
1018 spin_unlock_irqrestore(&mz->lru_lock, flags);
52d4b9ac 1019 unlock_page_cgroup(pc);
fb59e9f1 1020
69029cd5 1021 css_put(&mem->css);
6d12e2d8 1022
8c7c6e34 1023 return mem;
d13d1443
KH
1024
1025unlock_out:
1026 unlock_page_cgroup(pc);
8c7c6e34 1027 return NULL;
3c541e14
BS
1028}
1029
69029cd5
KH
1030void mem_cgroup_uncharge_page(struct page *page)
1031{
52d4b9ac
KH
1032 /* early check. */
1033 if (page_mapped(page))
1034 return;
1035 if (page->mapping && !PageAnon(page))
1036 return;
69029cd5
KH
1037 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
1038}
1039
1040void mem_cgroup_uncharge_cache_page(struct page *page)
1041{
1042 VM_BUG_ON(page_mapped(page));
b7abea96 1043 VM_BUG_ON(page->mapping);
69029cd5
KH
1044 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
1045}
1046
8c7c6e34
KH
1047/*
1048 * called from __delete_from_swap_cache() and drop "page" account.
1049 * memcg information is recorded to swap_cgroup of "ent"
1050 */
1051void mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent)
1052{
1053 struct mem_cgroup *memcg;
1054
1055 memcg = __mem_cgroup_uncharge_common(page,
1056 MEM_CGROUP_CHARGE_TYPE_SWAPOUT);
1057 /* record memcg information */
1058 if (do_swap_account && memcg) {
1059 swap_cgroup_record(ent, memcg);
1060 mem_cgroup_get(memcg);
1061 }
1062}
1063
1064#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1065/*
1066 * called from swap_entry_free(). remove record in swap_cgroup and
1067 * uncharge "memsw" account.
1068 */
1069void mem_cgroup_uncharge_swap(swp_entry_t ent)
d13d1443 1070{
8c7c6e34
KH
1071 struct mem_cgroup *memcg;
1072
1073 if (!do_swap_account)
1074 return;
1075
1076 memcg = swap_cgroup_record(ent, NULL);
1077 if (memcg) {
1078 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1079 mem_cgroup_put(memcg);
1080 }
d13d1443 1081}
8c7c6e34 1082#endif
d13d1443 1083
ae41be37 1084/*
01b1ae63
KH
1085 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
1086 * page belongs to.
ae41be37 1087 */
01b1ae63 1088int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
ae41be37
KH
1089{
1090 struct page_cgroup *pc;
e8589cc1 1091 struct mem_cgroup *mem = NULL;
e8589cc1 1092 int ret = 0;
8869b8f6 1093
4077960e
BS
1094 if (mem_cgroup_subsys.disabled)
1095 return 0;
1096
52d4b9ac
KH
1097 pc = lookup_page_cgroup(page);
1098 lock_page_cgroup(pc);
1099 if (PageCgroupUsed(pc)) {
e8589cc1
KH
1100 mem = pc->mem_cgroup;
1101 css_get(&mem->css);
e8589cc1 1102 }
52d4b9ac 1103 unlock_page_cgroup(pc);
01b1ae63 1104
e8589cc1 1105 if (mem) {
01b1ae63 1106 ret = mem_cgroup_try_charge(NULL, GFP_HIGHUSER_MOVABLE, &mem);
e8589cc1
KH
1107 css_put(&mem->css);
1108 }
01b1ae63 1109 *ptr = mem;
e8589cc1 1110 return ret;
ae41be37 1111}
8869b8f6 1112
69029cd5 1113/* remove redundant charge if migration failed*/
01b1ae63
KH
1114void mem_cgroup_end_migration(struct mem_cgroup *mem,
1115 struct page *oldpage, struct page *newpage)
ae41be37 1116{
01b1ae63
KH
1117 struct page *target, *unused;
1118 struct page_cgroup *pc;
1119 enum charge_type ctype;
1120
1121 if (!mem)
1122 return;
1123
1124 /* at migration success, oldpage->mapping is NULL. */
1125 if (oldpage->mapping) {
1126 target = oldpage;
1127 unused = NULL;
1128 } else {
1129 target = newpage;
1130 unused = oldpage;
1131 }
1132
1133 if (PageAnon(target))
1134 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
1135 else if (page_is_file_cache(target))
1136 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
1137 else
1138 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
1139
1140 /* unused page is not on radix-tree now. */
d13d1443 1141 if (unused)
01b1ae63
KH
1142 __mem_cgroup_uncharge_common(unused, ctype);
1143
1144 pc = lookup_page_cgroup(target);
69029cd5 1145 /*
01b1ae63
KH
1146 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
1147 * So, double-counting is effectively avoided.
1148 */
1149 __mem_cgroup_commit_charge(mem, pc, ctype);
1150
1151 /*
1152 * Both of oldpage and newpage are still under lock_page().
1153 * Then, we don't have to care about race in radix-tree.
1154 * But we have to be careful that this page is unmapped or not.
1155 *
1156 * There is a case for !page_mapped(). At the start of
1157 * migration, oldpage was mapped. But now, it's zapped.
1158 * But we know *target* page is not freed/reused under us.
1159 * mem_cgroup_uncharge_page() does all necessary checks.
69029cd5 1160 */
01b1ae63
KH
1161 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
1162 mem_cgroup_uncharge_page(target);
ae41be37 1163}
78fb7466 1164
c9b0ed51
KH
1165/*
1166 * A call to try to shrink memory usage under specified resource controller.
1167 * This is typically used for page reclaiming for shmem for reducing side
1168 * effect of page allocation from shmem, which is used by some mem_cgroup.
1169 */
1170int mem_cgroup_shrink_usage(struct mm_struct *mm, gfp_t gfp_mask)
1171{
1172 struct mem_cgroup *mem;
1173 int progress = 0;
1174 int retry = MEM_CGROUP_RECLAIM_RETRIES;
1175
cede86ac
LZ
1176 if (mem_cgroup_subsys.disabled)
1177 return 0;
9623e078
HD
1178 if (!mm)
1179 return 0;
cede86ac 1180
c9b0ed51
KH
1181 rcu_read_lock();
1182 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
31a78f23
BS
1183 if (unlikely(!mem)) {
1184 rcu_read_unlock();
1185 return 0;
1186 }
c9b0ed51
KH
1187 css_get(&mem->css);
1188 rcu_read_unlock();
1189
1190 do {
8c7c6e34 1191 progress = try_to_free_mem_cgroup_pages(mem, gfp_mask, true);
a10cebf5 1192 progress += res_counter_check_under_limit(&mem->res);
c9b0ed51
KH
1193 } while (!progress && --retry);
1194
1195 css_put(&mem->css);
1196 if (!retry)
1197 return -ENOMEM;
1198 return 0;
1199}
1200
8c7c6e34
KH
1201static DEFINE_MUTEX(set_limit_mutex);
1202
d38d2a75 1203static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
8c7c6e34 1204 unsigned long long val)
628f4235
KH
1205{
1206
1207 int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
1208 int progress;
8c7c6e34 1209 u64 memswlimit;
628f4235
KH
1210 int ret = 0;
1211
8c7c6e34 1212 while (retry_count) {
628f4235
KH
1213 if (signal_pending(current)) {
1214 ret = -EINTR;
1215 break;
1216 }
8c7c6e34
KH
1217 /*
1218 * Rather than hide all in some function, I do this in
1219 * open coded manner. You see what this really does.
1220 * We have to guarantee mem->res.limit < mem->memsw.limit.
1221 */
1222 mutex_lock(&set_limit_mutex);
1223 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1224 if (memswlimit < val) {
1225 ret = -EINVAL;
1226 mutex_unlock(&set_limit_mutex);
628f4235
KH
1227 break;
1228 }
8c7c6e34
KH
1229 ret = res_counter_set_limit(&memcg->res, val);
1230 mutex_unlock(&set_limit_mutex);
1231
1232 if (!ret)
1233 break;
1234
bced0520 1235 progress = try_to_free_mem_cgroup_pages(memcg,
8c7c6e34
KH
1236 GFP_HIGHUSER_MOVABLE, false);
1237 if (!progress) retry_count--;
1238 }
1239 return ret;
1240}
1241
1242int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
1243 unsigned long long val)
1244{
1245 int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
1246 u64 memlimit, oldusage, curusage;
1247 int ret;
1248
1249 if (!do_swap_account)
1250 return -EINVAL;
1251
1252 while (retry_count) {
1253 if (signal_pending(current)) {
1254 ret = -EINTR;
1255 break;
1256 }
1257 /*
1258 * Rather than hide all in some function, I do this in
1259 * open coded manner. You see what this really does.
1260 * We have to guarantee mem->res.limit < mem->memsw.limit.
1261 */
1262 mutex_lock(&set_limit_mutex);
1263 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
1264 if (memlimit > val) {
1265 ret = -EINVAL;
1266 mutex_unlock(&set_limit_mutex);
1267 break;
1268 }
1269 ret = res_counter_set_limit(&memcg->memsw, val);
1270 mutex_unlock(&set_limit_mutex);
1271
1272 if (!ret)
1273 break;
1274
1275 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
1276 try_to_free_mem_cgroup_pages(memcg, GFP_HIGHUSER_MOVABLE, true);
1277 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
1278 if (curusage >= oldusage)
628f4235
KH
1279 retry_count--;
1280 }
1281 return ret;
1282}
1283
1284
cc847582
KH
1285/*
1286 * This routine traverse page_cgroup in given list and drop them all.
cc847582
KH
1287 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
1288 */
f817ed48 1289static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
072c56c1 1290 struct mem_cgroup_per_zone *mz,
b69408e8 1291 enum lru_list lru)
cc847582 1292{
f817ed48 1293 struct page_cgroup *pc, *busy;
cc847582 1294 unsigned long flags;
f817ed48 1295 unsigned long loop;
072c56c1 1296 struct list_head *list;
f817ed48 1297 int ret = 0;
072c56c1 1298
b69408e8 1299 list = &mz->lists[lru];
cc847582 1300
f817ed48
KH
1301 loop = MEM_CGROUP_ZSTAT(mz, lru);
1302 /* give some margin against EBUSY etc...*/
1303 loop += 256;
1304 busy = NULL;
1305 while (loop--) {
1306 ret = 0;
1307 spin_lock_irqsave(&mz->lru_lock, flags);
1308 if (list_empty(list)) {
1309 spin_unlock_irqrestore(&mz->lru_lock, flags);
52d4b9ac 1310 break;
f817ed48
KH
1311 }
1312 pc = list_entry(list->prev, struct page_cgroup, lru);
1313 if (busy == pc) {
1314 list_move(&pc->lru, list);
1315 busy = 0;
1316 spin_unlock_irqrestore(&mz->lru_lock, flags);
1317 continue;
1318 }
9b3c0a07 1319 spin_unlock_irqrestore(&mz->lru_lock, flags);
f817ed48
KH
1320
1321 ret = mem_cgroup_move_parent(pc, mem, GFP_HIGHUSER_MOVABLE);
1322 if (ret == -ENOMEM)
52d4b9ac 1323 break;
f817ed48
KH
1324
1325 if (ret == -EBUSY || ret == -EINVAL) {
1326 /* found lock contention or "pc" is obsolete. */
1327 busy = pc;
1328 cond_resched();
1329 } else
1330 busy = NULL;
cc847582 1331 }
f817ed48
KH
1332 if (!ret && !list_empty(list))
1333 return -EBUSY;
1334 return ret;
cc847582
KH
1335}
1336
1337/*
1338 * make mem_cgroup's charge to be 0 if there is no task.
1339 * This enables deleting this mem_cgroup.
1340 */
c1e862c1 1341static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
cc847582 1342{
f817ed48
KH
1343 int ret;
1344 int node, zid, shrink;
1345 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
c1e862c1 1346 struct cgroup *cgrp = mem->css.cgroup;
8869b8f6 1347
cc847582 1348 css_get(&mem->css);
f817ed48
KH
1349
1350 shrink = 0;
c1e862c1
KH
1351 /* should free all ? */
1352 if (free_all)
1353 goto try_to_free;
f817ed48 1354move_account:
1ecaab2b 1355 while (mem->res.usage > 0) {
f817ed48 1356 ret = -EBUSY;
c1e862c1
KH
1357 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
1358 goto out;
1359 ret = -EINTR;
1360 if (signal_pending(current))
cc847582 1361 goto out;
52d4b9ac
KH
1362 /* This is for making all *used* pages to be on LRU. */
1363 lru_add_drain_all();
f817ed48
KH
1364 ret = 0;
1365 for_each_node_state(node, N_POSSIBLE) {
1366 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
1ecaab2b 1367 struct mem_cgroup_per_zone *mz;
b69408e8 1368 enum lru_list l;
1ecaab2b 1369 mz = mem_cgroup_zoneinfo(mem, node, zid);
f817ed48
KH
1370 for_each_lru(l) {
1371 ret = mem_cgroup_force_empty_list(mem,
1372 mz, l);
1373 if (ret)
1374 break;
1375 }
1ecaab2b 1376 }
f817ed48
KH
1377 if (ret)
1378 break;
1379 }
1380 /* it seems parent cgroup doesn't have enough mem */
1381 if (ret == -ENOMEM)
1382 goto try_to_free;
52d4b9ac 1383 cond_resched();
cc847582
KH
1384 }
1385 ret = 0;
1386out:
1387 css_put(&mem->css);
1388 return ret;
f817ed48
KH
1389
1390try_to_free:
c1e862c1
KH
1391 /* returns EBUSY if there is a task or if we come here twice. */
1392 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
f817ed48
KH
1393 ret = -EBUSY;
1394 goto out;
1395 }
c1e862c1
KH
1396 /* we call try-to-free pages for make this cgroup empty */
1397 lru_add_drain_all();
f817ed48
KH
1398 /* try to free all pages in this cgroup */
1399 shrink = 1;
1400 while (nr_retries && mem->res.usage > 0) {
1401 int progress;
c1e862c1
KH
1402
1403 if (signal_pending(current)) {
1404 ret = -EINTR;
1405 goto out;
1406 }
f817ed48 1407 progress = try_to_free_mem_cgroup_pages(mem,
8c7c6e34 1408 GFP_HIGHUSER_MOVABLE, false);
c1e862c1 1409 if (!progress) {
f817ed48 1410 nr_retries--;
c1e862c1
KH
1411 /* maybe some writeback is necessary */
1412 congestion_wait(WRITE, HZ/10);
1413 }
f817ed48
KH
1414
1415 }
1416 /* try move_account...there may be some *locked* pages. */
1417 if (mem->res.usage)
1418 goto move_account;
1419 ret = 0;
1420 goto out;
cc847582
KH
1421}
1422
c1e862c1
KH
1423int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
1424{
1425 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
1426}
1427
1428
2c3daa72 1429static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
8cdea7c0 1430{
8c7c6e34
KH
1431 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1432 u64 val = 0;
1433 int type, name;
1434
1435 type = MEMFILE_TYPE(cft->private);
1436 name = MEMFILE_ATTR(cft->private);
1437 switch (type) {
1438 case _MEM:
1439 val = res_counter_read_u64(&mem->res, name);
1440 break;
1441 case _MEMSWAP:
1442 if (do_swap_account)
1443 val = res_counter_read_u64(&mem->memsw, name);
1444 break;
1445 default:
1446 BUG();
1447 break;
1448 }
1449 return val;
8cdea7c0 1450}
628f4235
KH
1451/*
1452 * The user of this function is...
1453 * RES_LIMIT.
1454 */
856c13aa
PM
1455static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
1456 const char *buffer)
8cdea7c0 1457{
628f4235 1458 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
8c7c6e34 1459 int type, name;
628f4235
KH
1460 unsigned long long val;
1461 int ret;
1462
8c7c6e34
KH
1463 type = MEMFILE_TYPE(cft->private);
1464 name = MEMFILE_ATTR(cft->private);
1465 switch (name) {
628f4235
KH
1466 case RES_LIMIT:
1467 /* This function does all necessary parse...reuse it */
1468 ret = res_counter_memparse_write_strategy(buffer, &val);
8c7c6e34
KH
1469 if (ret)
1470 break;
1471 if (type == _MEM)
628f4235 1472 ret = mem_cgroup_resize_limit(memcg, val);
8c7c6e34
KH
1473 else
1474 ret = mem_cgroup_resize_memsw_limit(memcg, val);
628f4235
KH
1475 break;
1476 default:
1477 ret = -EINVAL; /* should be BUG() ? */
1478 break;
1479 }
1480 return ret;
8cdea7c0
BS
1481}
1482
29f2a4da 1483static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
c84872e1
PE
1484{
1485 struct mem_cgroup *mem;
8c7c6e34 1486 int type, name;
c84872e1
PE
1487
1488 mem = mem_cgroup_from_cont(cont);
8c7c6e34
KH
1489 type = MEMFILE_TYPE(event);
1490 name = MEMFILE_ATTR(event);
1491 switch (name) {
29f2a4da 1492 case RES_MAX_USAGE:
8c7c6e34
KH
1493 if (type == _MEM)
1494 res_counter_reset_max(&mem->res);
1495 else
1496 res_counter_reset_max(&mem->memsw);
29f2a4da
PE
1497 break;
1498 case RES_FAILCNT:
8c7c6e34
KH
1499 if (type == _MEM)
1500 res_counter_reset_failcnt(&mem->res);
1501 else
1502 res_counter_reset_failcnt(&mem->memsw);
29f2a4da
PE
1503 break;
1504 }
85cc59db 1505 return 0;
c84872e1
PE
1506}
1507
d2ceb9b7
KH
1508static const struct mem_cgroup_stat_desc {
1509 const char *msg;
1510 u64 unit;
1511} mem_cgroup_stat_desc[] = {
1512 [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
1513 [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
55e462b0
BR
1514 [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, },
1515 [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, },
d2ceb9b7
KH
1516};
1517
c64745cf
PM
1518static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
1519 struct cgroup_map_cb *cb)
d2ceb9b7 1520{
d2ceb9b7
KH
1521 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
1522 struct mem_cgroup_stat *stat = &mem_cont->stat;
1523 int i;
1524
1525 for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
1526 s64 val;
1527
1528 val = mem_cgroup_read_stat(stat, i);
1529 val *= mem_cgroup_stat_desc[i].unit;
c64745cf 1530 cb->fill(cb, mem_cgroup_stat_desc[i].msg, val);
d2ceb9b7 1531 }
6d12e2d8
KH
1532 /* showing # of active pages */
1533 {
4f98a2fe
RR
1534 unsigned long active_anon, inactive_anon;
1535 unsigned long active_file, inactive_file;
7b854121 1536 unsigned long unevictable;
4f98a2fe
RR
1537
1538 inactive_anon = mem_cgroup_get_all_zonestat(mem_cont,
1539 LRU_INACTIVE_ANON);
1540 active_anon = mem_cgroup_get_all_zonestat(mem_cont,
1541 LRU_ACTIVE_ANON);
1542 inactive_file = mem_cgroup_get_all_zonestat(mem_cont,
1543 LRU_INACTIVE_FILE);
1544 active_file = mem_cgroup_get_all_zonestat(mem_cont,
1545 LRU_ACTIVE_FILE);
7b854121
LS
1546 unevictable = mem_cgroup_get_all_zonestat(mem_cont,
1547 LRU_UNEVICTABLE);
1548
4f98a2fe
RR
1549 cb->fill(cb, "active_anon", (active_anon) * PAGE_SIZE);
1550 cb->fill(cb, "inactive_anon", (inactive_anon) * PAGE_SIZE);
1551 cb->fill(cb, "active_file", (active_file) * PAGE_SIZE);
1552 cb->fill(cb, "inactive_file", (inactive_file) * PAGE_SIZE);
7b854121
LS
1553 cb->fill(cb, "unevictable", unevictable * PAGE_SIZE);
1554
6d12e2d8 1555 }
d2ceb9b7
KH
1556 return 0;
1557}
1558
c1e862c1 1559
8cdea7c0
BS
1560static struct cftype mem_cgroup_files[] = {
1561 {
0eea1030 1562 .name = "usage_in_bytes",
8c7c6e34 1563 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
2c3daa72 1564 .read_u64 = mem_cgroup_read,
8cdea7c0 1565 },
c84872e1
PE
1566 {
1567 .name = "max_usage_in_bytes",
8c7c6e34 1568 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
29f2a4da 1569 .trigger = mem_cgroup_reset,
c84872e1
PE
1570 .read_u64 = mem_cgroup_read,
1571 },
8cdea7c0 1572 {
0eea1030 1573 .name = "limit_in_bytes",
8c7c6e34 1574 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
856c13aa 1575 .write_string = mem_cgroup_write,
2c3daa72 1576 .read_u64 = mem_cgroup_read,
8cdea7c0
BS
1577 },
1578 {
1579 .name = "failcnt",
8c7c6e34 1580 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
29f2a4da 1581 .trigger = mem_cgroup_reset,
2c3daa72 1582 .read_u64 = mem_cgroup_read,
8cdea7c0 1583 },
d2ceb9b7
KH
1584 {
1585 .name = "stat",
c64745cf 1586 .read_map = mem_control_stat_show,
d2ceb9b7 1587 },
c1e862c1
KH
1588 {
1589 .name = "force_empty",
1590 .trigger = mem_cgroup_force_empty_write,
1591 },
8cdea7c0
BS
1592};
1593
8c7c6e34
KH
1594#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1595static struct cftype memsw_cgroup_files[] = {
1596 {
1597 .name = "memsw.usage_in_bytes",
1598 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
1599 .read_u64 = mem_cgroup_read,
1600 },
1601 {
1602 .name = "memsw.max_usage_in_bytes",
1603 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
1604 .trigger = mem_cgroup_reset,
1605 .read_u64 = mem_cgroup_read,
1606 },
1607 {
1608 .name = "memsw.limit_in_bytes",
1609 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
1610 .write_string = mem_cgroup_write,
1611 .read_u64 = mem_cgroup_read,
1612 },
1613 {
1614 .name = "memsw.failcnt",
1615 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
1616 .trigger = mem_cgroup_reset,
1617 .read_u64 = mem_cgroup_read,
1618 },
1619};
1620
1621static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
1622{
1623 if (!do_swap_account)
1624 return 0;
1625 return cgroup_add_files(cont, ss, memsw_cgroup_files,
1626 ARRAY_SIZE(memsw_cgroup_files));
1627};
1628#else
1629static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
1630{
1631 return 0;
1632}
1633#endif
1634
6d12e2d8
KH
1635static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1636{
1637 struct mem_cgroup_per_node *pn;
1ecaab2b 1638 struct mem_cgroup_per_zone *mz;
b69408e8 1639 enum lru_list l;
41e3355d 1640 int zone, tmp = node;
1ecaab2b
KH
1641 /*
1642 * This routine is called against possible nodes.
1643 * But it's BUG to call kmalloc() against offline node.
1644 *
1645 * TODO: this routine can waste much memory for nodes which will
1646 * never be onlined. It's better to use memory hotplug callback
1647 * function.
1648 */
41e3355d
KH
1649 if (!node_state(node, N_NORMAL_MEMORY))
1650 tmp = -1;
1651 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
6d12e2d8
KH
1652 if (!pn)
1653 return 1;
1ecaab2b 1654
6d12e2d8
KH
1655 mem->info.nodeinfo[node] = pn;
1656 memset(pn, 0, sizeof(*pn));
1ecaab2b
KH
1657
1658 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
1659 mz = &pn->zoneinfo[zone];
072c56c1 1660 spin_lock_init(&mz->lru_lock);
b69408e8
CL
1661 for_each_lru(l)
1662 INIT_LIST_HEAD(&mz->lists[l]);
1ecaab2b 1663 }
6d12e2d8
KH
1664 return 0;
1665}
1666
1ecaab2b
KH
1667static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1668{
1669 kfree(mem->info.nodeinfo[node]);
1670}
1671
c8dad2bb
JB
1672static int mem_cgroup_size(void)
1673{
1674 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
1675 return sizeof(struct mem_cgroup) + cpustat_size;
1676}
1677
33327948
KH
1678static struct mem_cgroup *mem_cgroup_alloc(void)
1679{
1680 struct mem_cgroup *mem;
c8dad2bb 1681 int size = mem_cgroup_size();
33327948 1682
c8dad2bb
JB
1683 if (size < PAGE_SIZE)
1684 mem = kmalloc(size, GFP_KERNEL);
33327948 1685 else
c8dad2bb 1686 mem = vmalloc(size);
33327948
KH
1687
1688 if (mem)
c8dad2bb 1689 memset(mem, 0, size);
33327948
KH
1690 return mem;
1691}
1692
8c7c6e34
KH
1693/*
1694 * At destroying mem_cgroup, references from swap_cgroup can remain.
1695 * (scanning all at force_empty is too costly...)
1696 *
1697 * Instead of clearing all references at force_empty, we remember
1698 * the number of reference from swap_cgroup and free mem_cgroup when
1699 * it goes down to 0.
1700 *
1701 * When mem_cgroup is destroyed, mem->obsolete will be set to 0 and
1702 * entry which points to this memcg will be ignore at swapin.
1703 *
1704 * Removal of cgroup itself succeeds regardless of refs from swap.
1705 */
1706
33327948
KH
1707static void mem_cgroup_free(struct mem_cgroup *mem)
1708{
8c7c6e34
KH
1709 if (atomic_read(&mem->refcnt) > 0)
1710 return;
c8dad2bb 1711 if (mem_cgroup_size() < PAGE_SIZE)
33327948
KH
1712 kfree(mem);
1713 else
1714 vfree(mem);
1715}
1716
8c7c6e34
KH
1717static void mem_cgroup_get(struct mem_cgroup *mem)
1718{
1719 atomic_inc(&mem->refcnt);
1720}
1721
1722static void mem_cgroup_put(struct mem_cgroup *mem)
1723{
1724 if (atomic_dec_and_test(&mem->refcnt)) {
1725 if (!mem->obsolete)
1726 return;
1727 mem_cgroup_free(mem);
1728 }
1729}
1730
33327948 1731
c077719b
KH
1732#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1733static void __init enable_swap_cgroup(void)
1734{
1735 if (!mem_cgroup_subsys.disabled && really_do_swap_account)
1736 do_swap_account = 1;
1737}
1738#else
1739static void __init enable_swap_cgroup(void)
1740{
1741}
1742#endif
1743
8cdea7c0
BS
1744static struct cgroup_subsys_state *
1745mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
1746{
1747 struct mem_cgroup *mem;
6d12e2d8 1748 int node;
8cdea7c0 1749
c8dad2bb
JB
1750 mem = mem_cgroup_alloc();
1751 if (!mem)
1752 return ERR_PTR(-ENOMEM);
78fb7466 1753
8cdea7c0 1754 res_counter_init(&mem->res);
8c7c6e34 1755 res_counter_init(&mem->memsw);
1ecaab2b 1756
6d12e2d8
KH
1757 for_each_node_state(node, N_POSSIBLE)
1758 if (alloc_mem_cgroup_per_zone_info(mem, node))
1759 goto free_out;
c077719b
KH
1760 /* root ? */
1761 if (cont->parent == NULL)
1762 enable_swap_cgroup();
6d12e2d8 1763
8cdea7c0 1764 return &mem->css;
6d12e2d8
KH
1765free_out:
1766 for_each_node_state(node, N_POSSIBLE)
1ecaab2b 1767 free_mem_cgroup_per_zone_info(mem, node);
c8dad2bb 1768 mem_cgroup_free(mem);
2dda81ca 1769 return ERR_PTR(-ENOMEM);
8cdea7c0
BS
1770}
1771
df878fb0
KH
1772static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
1773 struct cgroup *cont)
1774{
1775 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
8c7c6e34 1776 mem->obsolete = 1;
c1e862c1 1777 mem_cgroup_force_empty(mem, false);
df878fb0
KH
1778}
1779
8cdea7c0
BS
1780static void mem_cgroup_destroy(struct cgroup_subsys *ss,
1781 struct cgroup *cont)
1782{
6d12e2d8
KH
1783 int node;
1784 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1785
1786 for_each_node_state(node, N_POSSIBLE)
1ecaab2b 1787 free_mem_cgroup_per_zone_info(mem, node);
6d12e2d8 1788
33327948 1789 mem_cgroup_free(mem_cgroup_from_cont(cont));
8cdea7c0
BS
1790}
1791
1792static int mem_cgroup_populate(struct cgroup_subsys *ss,
1793 struct cgroup *cont)
1794{
8c7c6e34
KH
1795 int ret;
1796
1797 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
1798 ARRAY_SIZE(mem_cgroup_files));
1799
1800 if (!ret)
1801 ret = register_memsw_files(cont, ss);
1802 return ret;
8cdea7c0
BS
1803}
1804
67e465a7
BS
1805static void mem_cgroup_move_task(struct cgroup_subsys *ss,
1806 struct cgroup *cont,
1807 struct cgroup *old_cont,
1808 struct task_struct *p)
1809{
1810 struct mm_struct *mm;
1811 struct mem_cgroup *mem, *old_mem;
1812
1813 mm = get_task_mm(p);
1814 if (mm == NULL)
1815 return;
1816
1817 mem = mem_cgroup_from_cont(cont);
1818 old_mem = mem_cgroup_from_cont(old_cont);
1819
67e465a7
BS
1820 /*
1821 * Only thread group leaders are allowed to migrate, the mm_struct is
1822 * in effect owned by the leader
1823 */
52ea27eb 1824 if (!thread_group_leader(p))
67e465a7
BS
1825 goto out;
1826
67e465a7
BS
1827out:
1828 mmput(mm);
67e465a7
BS
1829}
1830
8cdea7c0
BS
1831struct cgroup_subsys mem_cgroup_subsys = {
1832 .name = "memory",
1833 .subsys_id = mem_cgroup_subsys_id,
1834 .create = mem_cgroup_create,
df878fb0 1835 .pre_destroy = mem_cgroup_pre_destroy,
8cdea7c0
BS
1836 .destroy = mem_cgroup_destroy,
1837 .populate = mem_cgroup_populate,
67e465a7 1838 .attach = mem_cgroup_move_task,
6d12e2d8 1839 .early_init = 0,
8cdea7c0 1840};
c077719b
KH
1841
1842#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1843
1844static int __init disable_swap_account(char *s)
1845{
1846 really_do_swap_account = 0;
1847 return 1;
1848}
1849__setup("noswapaccount", disable_swap_account);
1850#endif