]> bbs.cooldavid.org Git - net-next-2.6.git/blame - mm/memcontrol.c
memcg: fix gfp_mask of callers of charge
[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>
d52aa412 24#include <linux/smp.h>
8a9f3ccd 25#include <linux/page-flags.h>
66e1707b 26#include <linux/backing-dev.h>
8a9f3ccd
BS
27#include <linux/bit_spinlock.h>
28#include <linux/rcupdate.h>
b6ac57d5 29#include <linux/slab.h>
66e1707b
BS
30#include <linux/swap.h>
31#include <linux/spinlock.h>
32#include <linux/fs.h>
d2ceb9b7 33#include <linux/seq_file.h>
33327948 34#include <linux/vmalloc.h>
b69408e8 35#include <linux/mm_inline.h>
52d4b9ac 36#include <linux/page_cgroup.h>
8cdea7c0 37
8697d331
BS
38#include <asm/uaccess.h>
39
a181b0e8 40struct cgroup_subsys mem_cgroup_subsys __read_mostly;
a181b0e8 41#define MEM_CGROUP_RECLAIM_RETRIES 5
8cdea7c0 42
d52aa412
KH
43/*
44 * Statistics for memory cgroup.
45 */
46enum mem_cgroup_stat_index {
47 /*
48 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
49 */
50 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
51 MEM_CGROUP_STAT_RSS, /* # of pages charged as rss */
55e462b0
BR
52 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
53 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
d52aa412
KH
54
55 MEM_CGROUP_STAT_NSTATS,
56};
57
58struct mem_cgroup_stat_cpu {
59 s64 count[MEM_CGROUP_STAT_NSTATS];
60} ____cacheline_aligned_in_smp;
61
62struct mem_cgroup_stat {
63 struct mem_cgroup_stat_cpu cpustat[NR_CPUS];
64};
65
66/*
67 * For accounting under irq disable, no need for increment preempt count.
68 */
addb9efe 69static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
d52aa412
KH
70 enum mem_cgroup_stat_index idx, int val)
71{
addb9efe 72 stat->count[idx] += val;
d52aa412
KH
73}
74
75static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
76 enum mem_cgroup_stat_index idx)
77{
78 int cpu;
79 s64 ret = 0;
80 for_each_possible_cpu(cpu)
81 ret += stat->cpustat[cpu].count[idx];
82 return ret;
83}
84
6d12e2d8
KH
85/*
86 * per-zone information in memory controller.
87 */
6d12e2d8 88struct mem_cgroup_per_zone {
072c56c1
KH
89 /*
90 * spin_lock to protect the per cgroup LRU
91 */
92 spinlock_t lru_lock;
b69408e8
CL
93 struct list_head lists[NR_LRU_LISTS];
94 unsigned long count[NR_LRU_LISTS];
6d12e2d8
KH
95};
96/* Macro for accessing counter */
97#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
98
99struct mem_cgroup_per_node {
100 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
101};
102
103struct mem_cgroup_lru_info {
104 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
105};
106
8cdea7c0
BS
107/*
108 * The memory controller data structure. The memory controller controls both
109 * page cache and RSS per cgroup. We would eventually like to provide
110 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
111 * to help the administrator determine what knobs to tune.
112 *
113 * TODO: Add a water mark for the memory controller. Reclaim will begin when
8a9f3ccd
BS
114 * we hit the water mark. May be even add a low water mark, such that
115 * no reclaim occurs from a cgroup at it's low water mark, this is
116 * a feature that will be implemented much later in the future.
8cdea7c0
BS
117 */
118struct mem_cgroup {
119 struct cgroup_subsys_state css;
120 /*
121 * the counter to account for memory usage
122 */
123 struct res_counter res;
78fb7466
PE
124 /*
125 * Per cgroup active and inactive list, similar to the
126 * per zone LRU lists.
78fb7466 127 */
6d12e2d8 128 struct mem_cgroup_lru_info info;
072c56c1 129
6c48a1d0 130 int prev_priority; /* for recording reclaim priority */
d52aa412
KH
131 /*
132 * statistics.
133 */
134 struct mem_cgroup_stat stat;
8cdea7c0 135};
8869b8f6 136static struct mem_cgroup init_mem_cgroup;
8cdea7c0 137
217bc319
KH
138enum charge_type {
139 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
140 MEM_CGROUP_CHARGE_TYPE_MAPPED,
4f98a2fe 141 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
c05555b5
KH
142 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
143 NR_CHARGE_TYPE,
144};
145
52d4b9ac
KH
146/* only for here (for easy reading.) */
147#define PCGF_CACHE (1UL << PCG_CACHE)
148#define PCGF_USED (1UL << PCG_USED)
149#define PCGF_ACTIVE (1UL << PCG_ACTIVE)
150#define PCGF_LOCK (1UL << PCG_LOCK)
151#define PCGF_FILE (1UL << PCG_FILE)
c05555b5
KH
152static const unsigned long
153pcg_default_flags[NR_CHARGE_TYPE] = {
52d4b9ac
KH
154 PCGF_CACHE | PCGF_FILE | PCGF_USED | PCGF_LOCK, /* File Cache */
155 PCGF_ACTIVE | PCGF_USED | PCGF_LOCK, /* Anon */
156 PCGF_ACTIVE | PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* Shmem */
157 0, /* FORCE */
217bc319
KH
158};
159
d52aa412
KH
160/*
161 * Always modified under lru lock. Then, not necessary to preempt_disable()
162 */
c05555b5
KH
163static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
164 struct page_cgroup *pc,
165 bool charge)
d52aa412
KH
166{
167 int val = (charge)? 1 : -1;
168 struct mem_cgroup_stat *stat = &mem->stat;
addb9efe 169 struct mem_cgroup_stat_cpu *cpustat;
d52aa412 170
8869b8f6 171 VM_BUG_ON(!irqs_disabled());
addb9efe
KH
172
173 cpustat = &stat->cpustat[smp_processor_id()];
c05555b5 174 if (PageCgroupCache(pc))
addb9efe 175 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
d52aa412 176 else
addb9efe 177 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
55e462b0
BR
178
179 if (charge)
addb9efe 180 __mem_cgroup_stat_add_safe(cpustat,
55e462b0
BR
181 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
182 else
addb9efe 183 __mem_cgroup_stat_add_safe(cpustat,
55e462b0 184 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
6d12e2d8
KH
185}
186
d5b69e38 187static struct mem_cgroup_per_zone *
6d12e2d8
KH
188mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
189{
6d12e2d8
KH
190 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
191}
192
d5b69e38 193static struct mem_cgroup_per_zone *
6d12e2d8
KH
194page_cgroup_zoneinfo(struct page_cgroup *pc)
195{
196 struct mem_cgroup *mem = pc->mem_cgroup;
197 int nid = page_cgroup_nid(pc);
198 int zid = page_cgroup_zid(pc);
d52aa412 199
6d12e2d8
KH
200 return mem_cgroup_zoneinfo(mem, nid, zid);
201}
202
203static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem,
b69408e8 204 enum lru_list idx)
6d12e2d8
KH
205{
206 int nid, zid;
207 struct mem_cgroup_per_zone *mz;
208 u64 total = 0;
209
210 for_each_online_node(nid)
211 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
212 mz = mem_cgroup_zoneinfo(mem, nid, zid);
213 total += MEM_CGROUP_ZSTAT(mz, idx);
214 }
215 return total;
d52aa412
KH
216}
217
d5b69e38 218static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
8cdea7c0
BS
219{
220 return container_of(cgroup_subsys_state(cont,
221 mem_cgroup_subsys_id), struct mem_cgroup,
222 css);
223}
224
cf475ad2 225struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
78fb7466 226{
31a78f23
BS
227 /*
228 * mm_update_next_owner() may clear mm->owner to NULL
229 * if it races with swapoff, page migration, etc.
230 * So this can be called with p == NULL.
231 */
232 if (unlikely(!p))
233 return NULL;
234
78fb7466
PE
235 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
236 struct mem_cgroup, css);
237}
238
3eae90c3
KH
239static void __mem_cgroup_remove_list(struct mem_cgroup_per_zone *mz,
240 struct page_cgroup *pc)
6d12e2d8 241{
4f98a2fe
RR
242 int lru = LRU_BASE;
243
c05555b5 244 if (PageCgroupUnevictable(pc))
894bc310
LS
245 lru = LRU_UNEVICTABLE;
246 else {
c05555b5 247 if (PageCgroupActive(pc))
894bc310 248 lru += LRU_ACTIVE;
c05555b5 249 if (PageCgroupFile(pc))
894bc310
LS
250 lru += LRU_FILE;
251 }
6d12e2d8 252
b69408e8 253 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
6d12e2d8 254
c05555b5 255 mem_cgroup_charge_statistics(pc->mem_cgroup, pc, false);
508b7be0 256 list_del(&pc->lru);
6d12e2d8
KH
257}
258
3eae90c3
KH
259static void __mem_cgroup_add_list(struct mem_cgroup_per_zone *mz,
260 struct page_cgroup *pc)
6d12e2d8 261{
4f98a2fe 262 int lru = LRU_BASE;
b69408e8 263
c05555b5 264 if (PageCgroupUnevictable(pc))
894bc310
LS
265 lru = LRU_UNEVICTABLE;
266 else {
c05555b5 267 if (PageCgroupActive(pc))
894bc310 268 lru += LRU_ACTIVE;
c05555b5 269 if (PageCgroupFile(pc))
894bc310
LS
270 lru += LRU_FILE;
271 }
b69408e8
CL
272
273 MEM_CGROUP_ZSTAT(mz, lru) += 1;
274 list_add(&pc->lru, &mz->lists[lru]);
6d12e2d8 275
c05555b5 276 mem_cgroup_charge_statistics(pc->mem_cgroup, pc, true);
6d12e2d8
KH
277}
278
894bc310 279static void __mem_cgroup_move_lists(struct page_cgroup *pc, enum lru_list lru)
66e1707b 280{
6d12e2d8 281 struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
c05555b5
KH
282 int active = PageCgroupActive(pc);
283 int file = PageCgroupFile(pc);
284 int unevictable = PageCgroupUnevictable(pc);
894bc310
LS
285 enum lru_list from = unevictable ? LRU_UNEVICTABLE :
286 (LRU_FILE * !!file + !!active);
6d12e2d8 287
894bc310
LS
288 if (lru == from)
289 return;
b69408e8 290
894bc310 291 MEM_CGROUP_ZSTAT(mz, from) -= 1;
c05555b5
KH
292 /*
293 * However this is done under mz->lru_lock, another flags, which
294 * are not related to LRU, will be modified from out-of-lock.
295 * We have to use atomic set/clear flags.
296 */
894bc310 297 if (is_unevictable_lru(lru)) {
c05555b5
KH
298 ClearPageCgroupActive(pc);
299 SetPageCgroupUnevictable(pc);
894bc310
LS
300 } else {
301 if (is_active_lru(lru))
c05555b5 302 SetPageCgroupActive(pc);
894bc310 303 else
c05555b5
KH
304 ClearPageCgroupActive(pc);
305 ClearPageCgroupUnevictable(pc);
894bc310 306 }
b69408e8 307
b69408e8
CL
308 MEM_CGROUP_ZSTAT(mz, lru) += 1;
309 list_move(&pc->lru, &mz->lists[lru]);
66e1707b
BS
310}
311
4c4a2214
DR
312int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
313{
314 int ret;
315
316 task_lock(task);
bd845e38 317 ret = task->mm && mm_match_cgroup(task->mm, mem);
4c4a2214
DR
318 task_unlock(task);
319 return ret;
320}
321
66e1707b
BS
322/*
323 * This routine assumes that the appropriate zone's lru lock is already held
324 */
894bc310 325void mem_cgroup_move_lists(struct page *page, enum lru_list lru)
66e1707b 326{
427d5416 327 struct page_cgroup *pc;
072c56c1
KH
328 struct mem_cgroup_per_zone *mz;
329 unsigned long flags;
330
cede86ac
LZ
331 if (mem_cgroup_subsys.disabled)
332 return;
333
2680eed7
HD
334 /*
335 * We cannot lock_page_cgroup while holding zone's lru_lock,
336 * because other holders of lock_page_cgroup can be interrupted
337 * with an attempt to rotate_reclaimable_page. But we cannot
338 * safely get to page_cgroup without it, so just try_lock it:
339 * mem_cgroup_isolate_pages allows for page left on wrong list.
340 */
52d4b9ac
KH
341 pc = lookup_page_cgroup(page);
342 if (!trylock_page_cgroup(pc))
66e1707b 343 return;
52d4b9ac 344 if (pc && PageCgroupUsed(pc)) {
2680eed7 345 mz = page_cgroup_zoneinfo(pc);
2680eed7 346 spin_lock_irqsave(&mz->lru_lock, flags);
894bc310 347 __mem_cgroup_move_lists(pc, lru);
2680eed7 348 spin_unlock_irqrestore(&mz->lru_lock, flags);
9b3c0a07 349 }
52d4b9ac 350 unlock_page_cgroup(pc);
66e1707b
BS
351}
352
58ae83db
KH
353/*
354 * Calculate mapped_ratio under memory controller. This will be used in
355 * vmscan.c for deteremining we have to reclaim mapped pages.
356 */
357int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
358{
359 long total, rss;
360
361 /*
362 * usage is recorded in bytes. But, here, we assume the number of
363 * physical pages can be represented by "long" on any arch.
364 */
365 total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
366 rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
367 return (int)((rss * 100L) / total);
368}
8869b8f6 369
6c48a1d0
KH
370/*
371 * prev_priority control...this will be used in memory reclaim path.
372 */
373int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
374{
375 return mem->prev_priority;
376}
377
378void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
379{
380 if (priority < mem->prev_priority)
381 mem->prev_priority = priority;
382}
383
384void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
385{
386 mem->prev_priority = priority;
387}
388
cc38108e
KH
389/*
390 * Calculate # of pages to be scanned in this priority/zone.
391 * See also vmscan.c
392 *
393 * priority starts from "DEF_PRIORITY" and decremented in each loop.
394 * (see include/linux/mmzone.h)
395 */
396
b69408e8
CL
397long mem_cgroup_calc_reclaim(struct mem_cgroup *mem, struct zone *zone,
398 int priority, enum lru_list lru)
cc38108e 399{
b69408e8 400 long nr_pages;
cc38108e
KH
401 int nid = zone->zone_pgdat->node_id;
402 int zid = zone_idx(zone);
403 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
404
b69408e8 405 nr_pages = MEM_CGROUP_ZSTAT(mz, lru);
cc38108e 406
b69408e8 407 return (nr_pages >> priority);
cc38108e
KH
408}
409
66e1707b
BS
410unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
411 struct list_head *dst,
412 unsigned long *scanned, int order,
413 int mode, struct zone *z,
414 struct mem_cgroup *mem_cont,
4f98a2fe 415 int active, int file)
66e1707b
BS
416{
417 unsigned long nr_taken = 0;
418 struct page *page;
419 unsigned long scan;
420 LIST_HEAD(pc_list);
421 struct list_head *src;
ff7283fa 422 struct page_cgroup *pc, *tmp;
1ecaab2b
KH
423 int nid = z->zone_pgdat->node_id;
424 int zid = zone_idx(z);
425 struct mem_cgroup_per_zone *mz;
4f98a2fe 426 int lru = LRU_FILE * !!file + !!active;
66e1707b 427
cf475ad2 428 BUG_ON(!mem_cont);
1ecaab2b 429 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
b69408e8 430 src = &mz->lists[lru];
66e1707b 431
072c56c1 432 spin_lock(&mz->lru_lock);
ff7283fa
KH
433 scan = 0;
434 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
436c6541 435 if (scan >= nr_to_scan)
ff7283fa 436 break;
52d4b9ac
KH
437 if (unlikely(!PageCgroupUsed(pc)))
438 continue;
66e1707b 439 page = pc->page;
66e1707b 440
436c6541 441 if (unlikely(!PageLRU(page)))
ff7283fa 442 continue;
ff7283fa 443
4f98a2fe
RR
444 /*
445 * TODO: play better with lumpy reclaim, grabbing anything.
446 */
894bc310
LS
447 if (PageUnevictable(page) ||
448 (PageActive(page) && !active) ||
449 (!PageActive(page) && active)) {
450 __mem_cgroup_move_lists(pc, page_lru(page));
66e1707b
BS
451 continue;
452 }
453
436c6541
HD
454 scan++;
455 list_move(&pc->lru, &pc_list);
66e1707b 456
4f98a2fe 457 if (__isolate_lru_page(page, mode, file) == 0) {
66e1707b
BS
458 list_move(&page->lru, dst);
459 nr_taken++;
460 }
461 }
462
463 list_splice(&pc_list, src);
072c56c1 464 spin_unlock(&mz->lru_lock);
66e1707b
BS
465
466 *scanned = scan;
467 return nr_taken;
468}
469
7a81b88c
KH
470
471/**
472 * mem_cgroup_try_charge - get charge of PAGE_SIZE.
473 * @mm: an mm_struct which is charged against. (when *memcg is NULL)
474 * @gfp_mask: gfp_mask for reclaim.
475 * @memcg: a pointer to memory cgroup which is charged against.
476 *
477 * charge against memory cgroup pointed by *memcg. if *memcg == NULL, estimated
478 * memory cgroup from @mm is got and stored in *memcg.
479 *
480 * Returns 0 if success. -ENOMEM at failure.
8a9f3ccd 481 */
7a81b88c
KH
482
483int mem_cgroup_try_charge(struct mm_struct *mm,
484 gfp_t gfp_mask, struct mem_cgroup **memcg)
8a9f3ccd
BS
485{
486 struct mem_cgroup *mem;
7a81b88c 487 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
8a9f3ccd 488 /*
3be91277
HD
489 * We always charge the cgroup the mm_struct belongs to.
490 * The mm_struct's mem_cgroup changes on task migration if the
8a9f3ccd
BS
491 * thread group leader migrates. It's possible that mm is not
492 * set, if so charge the init_mm (happens for pagecache usage).
493 */
7a81b88c 494 if (likely(!*memcg)) {
e8589cc1
KH
495 rcu_read_lock();
496 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
31a78f23
BS
497 if (unlikely(!mem)) {
498 rcu_read_unlock();
31a78f23
BS
499 return 0;
500 }
e8589cc1
KH
501 /*
502 * For every charge from the cgroup, increment reference count
503 */
504 css_get(&mem->css);
7a81b88c 505 *memcg = mem;
e8589cc1
KH
506 rcu_read_unlock();
507 } else {
7a81b88c
KH
508 mem = *memcg;
509 css_get(&mem->css);
e8589cc1 510 }
8a9f3ccd 511
7a81b88c 512
addb9efe 513 while (unlikely(res_counter_charge(&mem->res, PAGE_SIZE))) {
3be91277 514 if (!(gfp_mask & __GFP_WAIT))
7a81b88c 515 goto nomem;
e1a1cd59
BS
516
517 if (try_to_free_mem_cgroup_pages(mem, gfp_mask))
66e1707b
BS
518 continue;
519
520 /*
8869b8f6
HD
521 * try_to_free_mem_cgroup_pages() might not give us a full
522 * picture of reclaim. Some pages are reclaimed and might be
523 * moved to swap cache or just unmapped from the cgroup.
524 * Check the limit again to see if the reclaim reduced the
525 * current usage of the cgroup before giving up
526 */
66e1707b
BS
527 if (res_counter_check_under_limit(&mem->res))
528 continue;
3be91277
HD
529
530 if (!nr_retries--) {
531 mem_cgroup_out_of_memory(mem, gfp_mask);
7a81b88c 532 goto nomem;
66e1707b 533 }
8a9f3ccd 534 }
7a81b88c
KH
535 return 0;
536nomem:
537 css_put(&mem->css);
538 return -ENOMEM;
539}
8a9f3ccd 540
7a81b88c
KH
541/*
542 * commit a charge got by mem_cgroup_try_charge() and makes page_cgroup to be
543 * USED state. If already USED, uncharge and return.
544 */
545
546static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
547 struct page_cgroup *pc,
548 enum charge_type ctype)
549{
550 struct mem_cgroup_per_zone *mz;
551 unsigned long flags;
552
553 /* try_charge() can return NULL to *memcg, taking care of it. */
554 if (!mem)
555 return;
52d4b9ac
KH
556
557 lock_page_cgroup(pc);
558 if (unlikely(PageCgroupUsed(pc))) {
559 unlock_page_cgroup(pc);
560 res_counter_uncharge(&mem->res, PAGE_SIZE);
561 css_put(&mem->css);
7a81b88c 562 return;
52d4b9ac 563 }
8a9f3ccd 564 pc->mem_cgroup = mem;
508b7be0
KH
565 /*
566 * If a page is accounted as a page cache, insert to inactive list.
567 * If anon, insert to active list.
568 */
c05555b5 569 pc->flags = pcg_default_flags[ctype];
3be91277 570
072c56c1 571 mz = page_cgroup_zoneinfo(pc);
52d4b9ac 572
072c56c1 573 spin_lock_irqsave(&mz->lru_lock, flags);
3eae90c3 574 __mem_cgroup_add_list(mz, pc);
072c56c1 575 spin_unlock_irqrestore(&mz->lru_lock, flags);
52d4b9ac 576 unlock_page_cgroup(pc);
7a81b88c 577}
66e1707b 578
7a81b88c
KH
579/*
580 * Charge the memory controller for page usage.
581 * Return
582 * 0 if the charge was successful
583 * < 0 if the cgroup is over its limit
584 */
585static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
586 gfp_t gfp_mask, enum charge_type ctype,
587 struct mem_cgroup *memcg)
588{
589 struct mem_cgroup *mem;
590 struct page_cgroup *pc;
591 int ret;
592
593 pc = lookup_page_cgroup(page);
594 /* can happen at boot */
595 if (unlikely(!pc))
596 return 0;
597 prefetchw(pc);
598
599 mem = memcg;
600 ret = mem_cgroup_try_charge(mm, gfp_mask, &mem);
601 if (ret)
602 return ret;
603
604 __mem_cgroup_commit_charge(mem, pc, ctype);
8a9f3ccd 605 return 0;
8a9f3ccd
BS
606}
607
7a81b88c
KH
608int mem_cgroup_newpage_charge(struct page *page,
609 struct mm_struct *mm, gfp_t gfp_mask)
217bc319 610{
cede86ac
LZ
611 if (mem_cgroup_subsys.disabled)
612 return 0;
52d4b9ac
KH
613 if (PageCompound(page))
614 return 0;
69029cd5
KH
615 /*
616 * If already mapped, we don't have to account.
617 * If page cache, page->mapping has address_space.
618 * But page->mapping may have out-of-use anon_vma pointer,
619 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
620 * is NULL.
621 */
622 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
623 return 0;
624 if (unlikely(!mm))
625 mm = &init_mm;
217bc319 626 return mem_cgroup_charge_common(page, mm, gfp_mask,
e8589cc1 627 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
217bc319
KH
628}
629
7a81b88c
KH
630/*
631 * same as mem_cgroup_newpage_charge(), now.
632 * But what we assume is different from newpage, and this is special case.
633 * treat this in special function. easy for maintenance.
634 */
635
636int mem_cgroup_charge_migrate_fixup(struct page *page,
637 struct mm_struct *mm, gfp_t gfp_mask)
638{
639 if (mem_cgroup_subsys.disabled)
640 return 0;
641
642 if (PageCompound(page))
643 return 0;
644
645 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
646 return 0;
647
648 if (unlikely(!mm))
649 mm = &init_mm;
650
651 return mem_cgroup_charge_common(page, mm, gfp_mask,
652 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
653}
654
655
656
657
e1a1cd59
BS
658int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
659 gfp_t gfp_mask)
8697d331 660{
cede86ac
LZ
661 if (mem_cgroup_subsys.disabled)
662 return 0;
52d4b9ac
KH
663 if (PageCompound(page))
664 return 0;
accf163e
KH
665 /*
666 * Corner case handling. This is called from add_to_page_cache()
667 * in usual. But some FS (shmem) precharges this page before calling it
668 * and call add_to_page_cache() with GFP_NOWAIT.
669 *
670 * For GFP_NOWAIT case, the page may be pre-charged before calling
671 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
672 * charge twice. (It works but has to pay a bit larger cost.)
673 */
674 if (!(gfp_mask & __GFP_WAIT)) {
675 struct page_cgroup *pc;
676
52d4b9ac
KH
677
678 pc = lookup_page_cgroup(page);
679 if (!pc)
680 return 0;
681 lock_page_cgroup(pc);
682 if (PageCgroupUsed(pc)) {
683 unlock_page_cgroup(pc);
accf163e
KH
684 return 0;
685 }
52d4b9ac 686 unlock_page_cgroup(pc);
accf163e
KH
687 }
688
69029cd5 689 if (unlikely(!mm))
8697d331 690 mm = &init_mm;
accf163e 691
c05555b5
KH
692 if (page_is_file_cache(page))
693 return mem_cgroup_charge_common(page, mm, gfp_mask,
e8589cc1 694 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
c05555b5
KH
695 else
696 return mem_cgroup_charge_common(page, mm, gfp_mask,
697 MEM_CGROUP_CHARGE_TYPE_SHMEM, NULL);
e8589cc1
KH
698}
699
7a81b88c
KH
700
701void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
702{
703 struct page_cgroup *pc;
704
705 if (mem_cgroup_subsys.disabled)
706 return;
707 if (!ptr)
708 return;
709 pc = lookup_page_cgroup(page);
710 __mem_cgroup_commit_charge(ptr, pc, MEM_CGROUP_CHARGE_TYPE_MAPPED);
711}
712
713void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
714{
715 if (mem_cgroup_subsys.disabled)
716 return;
717 if (!mem)
718 return;
719 res_counter_uncharge(&mem->res, PAGE_SIZE);
720 css_put(&mem->css);
721}
722
723
8a9f3ccd 724/*
69029cd5 725 * uncharge if !page_mapped(page)
8a9f3ccd 726 */
69029cd5
KH
727static void
728__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
8a9f3ccd 729{
8289546e 730 struct page_cgroup *pc;
8a9f3ccd 731 struct mem_cgroup *mem;
072c56c1 732 struct mem_cgroup_per_zone *mz;
66e1707b 733 unsigned long flags;
8a9f3ccd 734
4077960e
BS
735 if (mem_cgroup_subsys.disabled)
736 return;
737
8697d331 738 /*
3c541e14 739 * Check if our page_cgroup is valid
8697d331 740 */
52d4b9ac
KH
741 pc = lookup_page_cgroup(page);
742 if (unlikely(!pc || !PageCgroupUsed(pc)))
743 return;
b9c565d5 744
52d4b9ac
KH
745 lock_page_cgroup(pc);
746 if ((ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED && page_mapped(page))
747 || !PageCgroupUsed(pc)) {
748 /* This happens at race in zap_pte_range() and do_swap_page()*/
749 unlock_page_cgroup(pc);
750 return;
751 }
752 ClearPageCgroupUsed(pc);
753 mem = pc->mem_cgroup;
b9c565d5 754
69029cd5
KH
755 mz = page_cgroup_zoneinfo(pc);
756 spin_lock_irqsave(&mz->lru_lock, flags);
757 __mem_cgroup_remove_list(mz, pc);
758 spin_unlock_irqrestore(&mz->lru_lock, flags);
52d4b9ac 759 unlock_page_cgroup(pc);
fb59e9f1 760
69029cd5
KH
761 res_counter_uncharge(&mem->res, PAGE_SIZE);
762 css_put(&mem->css);
6d12e2d8 763
69029cd5 764 return;
3c541e14
BS
765}
766
69029cd5
KH
767void mem_cgroup_uncharge_page(struct page *page)
768{
52d4b9ac
KH
769 /* early check. */
770 if (page_mapped(page))
771 return;
772 if (page->mapping && !PageAnon(page))
773 return;
69029cd5
KH
774 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
775}
776
777void mem_cgroup_uncharge_cache_page(struct page *page)
778{
779 VM_BUG_ON(page_mapped(page));
b7abea96 780 VM_BUG_ON(page->mapping);
69029cd5
KH
781 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
782}
783
ae41be37 784/*
e8589cc1 785 * Before starting migration, account against new page.
ae41be37 786 */
e8589cc1 787int mem_cgroup_prepare_migration(struct page *page, struct page *newpage)
ae41be37
KH
788{
789 struct page_cgroup *pc;
e8589cc1
KH
790 struct mem_cgroup *mem = NULL;
791 enum charge_type ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
792 int ret = 0;
8869b8f6 793
4077960e
BS
794 if (mem_cgroup_subsys.disabled)
795 return 0;
796
52d4b9ac
KH
797 pc = lookup_page_cgroup(page);
798 lock_page_cgroup(pc);
799 if (PageCgroupUsed(pc)) {
e8589cc1
KH
800 mem = pc->mem_cgroup;
801 css_get(&mem->css);
c05555b5 802 if (PageCgroupCache(pc)) {
4f98a2fe
RR
803 if (page_is_file_cache(page))
804 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
805 else
806 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
807 }
e8589cc1 808 }
52d4b9ac 809 unlock_page_cgroup(pc);
e8589cc1 810 if (mem) {
bced0520
KH
811 ret = mem_cgroup_charge_common(newpage, NULL,
812 GFP_HIGHUSER_MOVABLE,
813 ctype, mem);
e8589cc1
KH
814 css_put(&mem->css);
815 }
816 return ret;
ae41be37 817}
8869b8f6 818
69029cd5 819/* remove redundant charge if migration failed*/
e8589cc1 820void mem_cgroup_end_migration(struct page *newpage)
ae41be37 821{
69029cd5
KH
822 /*
823 * At success, page->mapping is not NULL.
824 * special rollback care is necessary when
825 * 1. at migration failure. (newpage->mapping is cleared in this case)
826 * 2. the newpage was moved but not remapped again because the task
827 * exits and the newpage is obsolete. In this case, the new page
828 * may be a swapcache. So, we just call mem_cgroup_uncharge_page()
829 * always for avoiding mess. The page_cgroup will be removed if
830 * unnecessary. File cache pages is still on radix-tree. Don't
831 * care it.
832 */
833 if (!newpage->mapping)
834 __mem_cgroup_uncharge_common(newpage,
52d4b9ac 835 MEM_CGROUP_CHARGE_TYPE_FORCE);
69029cd5
KH
836 else if (PageAnon(newpage))
837 mem_cgroup_uncharge_page(newpage);
ae41be37 838}
78fb7466 839
c9b0ed51
KH
840/*
841 * A call to try to shrink memory usage under specified resource controller.
842 * This is typically used for page reclaiming for shmem for reducing side
843 * effect of page allocation from shmem, which is used by some mem_cgroup.
844 */
845int mem_cgroup_shrink_usage(struct mm_struct *mm, gfp_t gfp_mask)
846{
847 struct mem_cgroup *mem;
848 int progress = 0;
849 int retry = MEM_CGROUP_RECLAIM_RETRIES;
850
cede86ac
LZ
851 if (mem_cgroup_subsys.disabled)
852 return 0;
9623e078
HD
853 if (!mm)
854 return 0;
cede86ac 855
c9b0ed51
KH
856 rcu_read_lock();
857 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
31a78f23
BS
858 if (unlikely(!mem)) {
859 rcu_read_unlock();
860 return 0;
861 }
c9b0ed51
KH
862 css_get(&mem->css);
863 rcu_read_unlock();
864
865 do {
866 progress = try_to_free_mem_cgroup_pages(mem, gfp_mask);
a10cebf5 867 progress += res_counter_check_under_limit(&mem->res);
c9b0ed51
KH
868 } while (!progress && --retry);
869
870 css_put(&mem->css);
871 if (!retry)
872 return -ENOMEM;
873 return 0;
874}
875
d38d2a75
KM
876static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
877 unsigned long long val)
628f4235
KH
878{
879
880 int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
881 int progress;
882 int ret = 0;
883
884 while (res_counter_set_limit(&memcg->res, val)) {
885 if (signal_pending(current)) {
886 ret = -EINTR;
887 break;
888 }
889 if (!retry_count) {
890 ret = -EBUSY;
891 break;
892 }
bced0520
KH
893 progress = try_to_free_mem_cgroup_pages(memcg,
894 GFP_HIGHUSER_MOVABLE);
628f4235
KH
895 if (!progress)
896 retry_count--;
897 }
898 return ret;
899}
900
901
cc847582
KH
902/*
903 * This routine traverse page_cgroup in given list and drop them all.
cc847582
KH
904 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
905 */
906#define FORCE_UNCHARGE_BATCH (128)
8869b8f6 907static void mem_cgroup_force_empty_list(struct mem_cgroup *mem,
072c56c1 908 struct mem_cgroup_per_zone *mz,
b69408e8 909 enum lru_list lru)
cc847582
KH
910{
911 struct page_cgroup *pc;
912 struct page *page;
9b3c0a07 913 int count = FORCE_UNCHARGE_BATCH;
cc847582 914 unsigned long flags;
072c56c1
KH
915 struct list_head *list;
916
b69408e8 917 list = &mz->lists[lru];
cc847582 918
072c56c1 919 spin_lock_irqsave(&mz->lru_lock, flags);
9b3c0a07 920 while (!list_empty(list)) {
cc847582
KH
921 pc = list_entry(list->prev, struct page_cgroup, lru);
922 page = pc->page;
52d4b9ac
KH
923 if (!PageCgroupUsed(pc))
924 break;
9b3c0a07
HT
925 get_page(page);
926 spin_unlock_irqrestore(&mz->lru_lock, flags);
e8589cc1
KH
927 /*
928 * Check if this page is on LRU. !LRU page can be found
929 * if it's under page migration.
930 */
931 if (PageLRU(page)) {
69029cd5
KH
932 __mem_cgroup_uncharge_common(page,
933 MEM_CGROUP_CHARGE_TYPE_FORCE);
e8589cc1
KH
934 put_page(page);
935 if (--count <= 0) {
936 count = FORCE_UNCHARGE_BATCH;
937 cond_resched();
938 }
52d4b9ac
KH
939 } else {
940 spin_lock_irqsave(&mz->lru_lock, flags);
941 break;
942 }
9b3c0a07 943 spin_lock_irqsave(&mz->lru_lock, flags);
cc847582 944 }
072c56c1 945 spin_unlock_irqrestore(&mz->lru_lock, flags);
cc847582
KH
946}
947
948/*
949 * make mem_cgroup's charge to be 0 if there is no task.
950 * This enables deleting this mem_cgroup.
951 */
d5b69e38 952static int mem_cgroup_force_empty(struct mem_cgroup *mem)
cc847582
KH
953{
954 int ret = -EBUSY;
1ecaab2b 955 int node, zid;
8869b8f6 956
cc847582
KH
957 css_get(&mem->css);
958 /*
959 * page reclaim code (kswapd etc..) will move pages between
8869b8f6 960 * active_list <-> inactive_list while we don't take a lock.
cc847582
KH
961 * So, we have to do loop here until all lists are empty.
962 */
1ecaab2b 963 while (mem->res.usage > 0) {
cc847582
KH
964 if (atomic_read(&mem->css.cgroup->count) > 0)
965 goto out;
52d4b9ac
KH
966 /* This is for making all *used* pages to be on LRU. */
967 lru_add_drain_all();
1ecaab2b
KH
968 for_each_node_state(node, N_POSSIBLE)
969 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
970 struct mem_cgroup_per_zone *mz;
b69408e8 971 enum lru_list l;
1ecaab2b 972 mz = mem_cgroup_zoneinfo(mem, node, zid);
b69408e8
CL
973 for_each_lru(l)
974 mem_cgroup_force_empty_list(mem, mz, l);
1ecaab2b 975 }
52d4b9ac 976 cond_resched();
cc847582
KH
977 }
978 ret = 0;
979out:
980 css_put(&mem->css);
981 return ret;
982}
983
2c3daa72 984static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
8cdea7c0 985{
2c3daa72
PM
986 return res_counter_read_u64(&mem_cgroup_from_cont(cont)->res,
987 cft->private);
8cdea7c0 988}
628f4235
KH
989/*
990 * The user of this function is...
991 * RES_LIMIT.
992 */
856c13aa
PM
993static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
994 const char *buffer)
8cdea7c0 995{
628f4235
KH
996 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
997 unsigned long long val;
998 int ret;
999
1000 switch (cft->private) {
1001 case RES_LIMIT:
1002 /* This function does all necessary parse...reuse it */
1003 ret = res_counter_memparse_write_strategy(buffer, &val);
1004 if (!ret)
1005 ret = mem_cgroup_resize_limit(memcg, val);
1006 break;
1007 default:
1008 ret = -EINVAL; /* should be BUG() ? */
1009 break;
1010 }
1011 return ret;
8cdea7c0
BS
1012}
1013
29f2a4da 1014static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
c84872e1
PE
1015{
1016 struct mem_cgroup *mem;
1017
1018 mem = mem_cgroup_from_cont(cont);
29f2a4da
PE
1019 switch (event) {
1020 case RES_MAX_USAGE:
1021 res_counter_reset_max(&mem->res);
1022 break;
1023 case RES_FAILCNT:
1024 res_counter_reset_failcnt(&mem->res);
1025 break;
1026 }
85cc59db 1027 return 0;
c84872e1
PE
1028}
1029
85cc59db 1030static int mem_force_empty_write(struct cgroup *cont, unsigned int event)
cc847582 1031{
85cc59db 1032 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont));
cc847582
KH
1033}
1034
d2ceb9b7
KH
1035static const struct mem_cgroup_stat_desc {
1036 const char *msg;
1037 u64 unit;
1038} mem_cgroup_stat_desc[] = {
1039 [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
1040 [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
55e462b0
BR
1041 [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, },
1042 [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, },
d2ceb9b7
KH
1043};
1044
c64745cf
PM
1045static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
1046 struct cgroup_map_cb *cb)
d2ceb9b7 1047{
d2ceb9b7
KH
1048 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
1049 struct mem_cgroup_stat *stat = &mem_cont->stat;
1050 int i;
1051
1052 for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
1053 s64 val;
1054
1055 val = mem_cgroup_read_stat(stat, i);
1056 val *= mem_cgroup_stat_desc[i].unit;
c64745cf 1057 cb->fill(cb, mem_cgroup_stat_desc[i].msg, val);
d2ceb9b7 1058 }
6d12e2d8
KH
1059 /* showing # of active pages */
1060 {
4f98a2fe
RR
1061 unsigned long active_anon, inactive_anon;
1062 unsigned long active_file, inactive_file;
7b854121 1063 unsigned long unevictable;
4f98a2fe
RR
1064
1065 inactive_anon = mem_cgroup_get_all_zonestat(mem_cont,
1066 LRU_INACTIVE_ANON);
1067 active_anon = mem_cgroup_get_all_zonestat(mem_cont,
1068 LRU_ACTIVE_ANON);
1069 inactive_file = mem_cgroup_get_all_zonestat(mem_cont,
1070 LRU_INACTIVE_FILE);
1071 active_file = mem_cgroup_get_all_zonestat(mem_cont,
1072 LRU_ACTIVE_FILE);
7b854121
LS
1073 unevictable = mem_cgroup_get_all_zonestat(mem_cont,
1074 LRU_UNEVICTABLE);
1075
4f98a2fe
RR
1076 cb->fill(cb, "active_anon", (active_anon) * PAGE_SIZE);
1077 cb->fill(cb, "inactive_anon", (inactive_anon) * PAGE_SIZE);
1078 cb->fill(cb, "active_file", (active_file) * PAGE_SIZE);
1079 cb->fill(cb, "inactive_file", (inactive_file) * PAGE_SIZE);
7b854121
LS
1080 cb->fill(cb, "unevictable", unevictable * PAGE_SIZE);
1081
6d12e2d8 1082 }
d2ceb9b7
KH
1083 return 0;
1084}
1085
8cdea7c0
BS
1086static struct cftype mem_cgroup_files[] = {
1087 {
0eea1030 1088 .name = "usage_in_bytes",
8cdea7c0 1089 .private = RES_USAGE,
2c3daa72 1090 .read_u64 = mem_cgroup_read,
8cdea7c0 1091 },
c84872e1
PE
1092 {
1093 .name = "max_usage_in_bytes",
1094 .private = RES_MAX_USAGE,
29f2a4da 1095 .trigger = mem_cgroup_reset,
c84872e1
PE
1096 .read_u64 = mem_cgroup_read,
1097 },
8cdea7c0 1098 {
0eea1030 1099 .name = "limit_in_bytes",
8cdea7c0 1100 .private = RES_LIMIT,
856c13aa 1101 .write_string = mem_cgroup_write,
2c3daa72 1102 .read_u64 = mem_cgroup_read,
8cdea7c0
BS
1103 },
1104 {
1105 .name = "failcnt",
1106 .private = RES_FAILCNT,
29f2a4da 1107 .trigger = mem_cgroup_reset,
2c3daa72 1108 .read_u64 = mem_cgroup_read,
8cdea7c0 1109 },
cc847582
KH
1110 {
1111 .name = "force_empty",
85cc59db 1112 .trigger = mem_force_empty_write,
cc847582 1113 },
d2ceb9b7
KH
1114 {
1115 .name = "stat",
c64745cf 1116 .read_map = mem_control_stat_show,
d2ceb9b7 1117 },
8cdea7c0
BS
1118};
1119
6d12e2d8
KH
1120static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1121{
1122 struct mem_cgroup_per_node *pn;
1ecaab2b 1123 struct mem_cgroup_per_zone *mz;
b69408e8 1124 enum lru_list l;
41e3355d 1125 int zone, tmp = node;
1ecaab2b
KH
1126 /*
1127 * This routine is called against possible nodes.
1128 * But it's BUG to call kmalloc() against offline node.
1129 *
1130 * TODO: this routine can waste much memory for nodes which will
1131 * never be onlined. It's better to use memory hotplug callback
1132 * function.
1133 */
41e3355d
KH
1134 if (!node_state(node, N_NORMAL_MEMORY))
1135 tmp = -1;
1136 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
6d12e2d8
KH
1137 if (!pn)
1138 return 1;
1ecaab2b 1139
6d12e2d8
KH
1140 mem->info.nodeinfo[node] = pn;
1141 memset(pn, 0, sizeof(*pn));
1ecaab2b
KH
1142
1143 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
1144 mz = &pn->zoneinfo[zone];
072c56c1 1145 spin_lock_init(&mz->lru_lock);
b69408e8
CL
1146 for_each_lru(l)
1147 INIT_LIST_HEAD(&mz->lists[l]);
1ecaab2b 1148 }
6d12e2d8
KH
1149 return 0;
1150}
1151
1ecaab2b
KH
1152static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1153{
1154 kfree(mem->info.nodeinfo[node]);
1155}
1156
33327948
KH
1157static struct mem_cgroup *mem_cgroup_alloc(void)
1158{
1159 struct mem_cgroup *mem;
1160
1161 if (sizeof(*mem) < PAGE_SIZE)
1162 mem = kmalloc(sizeof(*mem), GFP_KERNEL);
1163 else
1164 mem = vmalloc(sizeof(*mem));
1165
1166 if (mem)
1167 memset(mem, 0, sizeof(*mem));
1168 return mem;
1169}
1170
1171static void mem_cgroup_free(struct mem_cgroup *mem)
1172{
1173 if (sizeof(*mem) < PAGE_SIZE)
1174 kfree(mem);
1175 else
1176 vfree(mem);
1177}
1178
1179
8cdea7c0
BS
1180static struct cgroup_subsys_state *
1181mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
1182{
1183 struct mem_cgroup *mem;
6d12e2d8 1184 int node;
8cdea7c0 1185
b6ac57d5 1186 if (unlikely((cont->parent) == NULL)) {
78fb7466 1187 mem = &init_mem_cgroup;
b6ac57d5 1188 } else {
33327948
KH
1189 mem = mem_cgroup_alloc();
1190 if (!mem)
1191 return ERR_PTR(-ENOMEM);
b6ac57d5 1192 }
78fb7466 1193
8cdea7c0 1194 res_counter_init(&mem->res);
1ecaab2b 1195
6d12e2d8
KH
1196 for_each_node_state(node, N_POSSIBLE)
1197 if (alloc_mem_cgroup_per_zone_info(mem, node))
1198 goto free_out;
1199
8cdea7c0 1200 return &mem->css;
6d12e2d8
KH
1201free_out:
1202 for_each_node_state(node, N_POSSIBLE)
1ecaab2b 1203 free_mem_cgroup_per_zone_info(mem, node);
6d12e2d8 1204 if (cont->parent != NULL)
33327948 1205 mem_cgroup_free(mem);
2dda81ca 1206 return ERR_PTR(-ENOMEM);
8cdea7c0
BS
1207}
1208
df878fb0
KH
1209static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
1210 struct cgroup *cont)
1211{
1212 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1213 mem_cgroup_force_empty(mem);
1214}
1215
8cdea7c0
BS
1216static void mem_cgroup_destroy(struct cgroup_subsys *ss,
1217 struct cgroup *cont)
1218{
6d12e2d8
KH
1219 int node;
1220 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1221
1222 for_each_node_state(node, N_POSSIBLE)
1ecaab2b 1223 free_mem_cgroup_per_zone_info(mem, node);
6d12e2d8 1224
33327948 1225 mem_cgroup_free(mem_cgroup_from_cont(cont));
8cdea7c0
BS
1226}
1227
1228static int mem_cgroup_populate(struct cgroup_subsys *ss,
1229 struct cgroup *cont)
1230{
1231 return cgroup_add_files(cont, ss, mem_cgroup_files,
1232 ARRAY_SIZE(mem_cgroup_files));
1233}
1234
67e465a7
BS
1235static void mem_cgroup_move_task(struct cgroup_subsys *ss,
1236 struct cgroup *cont,
1237 struct cgroup *old_cont,
1238 struct task_struct *p)
1239{
1240 struct mm_struct *mm;
1241 struct mem_cgroup *mem, *old_mem;
1242
1243 mm = get_task_mm(p);
1244 if (mm == NULL)
1245 return;
1246
1247 mem = mem_cgroup_from_cont(cont);
1248 old_mem = mem_cgroup_from_cont(old_cont);
1249
67e465a7
BS
1250 /*
1251 * Only thread group leaders are allowed to migrate, the mm_struct is
1252 * in effect owned by the leader
1253 */
52ea27eb 1254 if (!thread_group_leader(p))
67e465a7
BS
1255 goto out;
1256
67e465a7
BS
1257out:
1258 mmput(mm);
67e465a7
BS
1259}
1260
8cdea7c0
BS
1261struct cgroup_subsys mem_cgroup_subsys = {
1262 .name = "memory",
1263 .subsys_id = mem_cgroup_subsys_id,
1264 .create = mem_cgroup_create,
df878fb0 1265 .pre_destroy = mem_cgroup_pre_destroy,
8cdea7c0
BS
1266 .destroy = mem_cgroup_destroy,
1267 .populate = mem_cgroup_populate,
67e465a7 1268 .attach = mem_cgroup_move_task,
6d12e2d8 1269 .early_init = 0,
8cdea7c0 1270};