blob: 82c065e7551e060ad476e4650d102687491e8533 [file] [log] [blame]
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001/* memcontrol.c - Memory Controller
2 *
3 * Copyright IBM Corporation, 2007
4 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
5 *
Pavel Emelianov78fb7462008-02-07 00:13:51 -08006 * Copyright 2007 OpenVZ SWsoft Inc
7 * Author: Pavel Emelianov <xemul@openvz.org>
8 *
Balbir Singh8cdea7c2008-02-07 00:13:50 -08009 * 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>
Pavel Emelianov78fb7462008-02-07 00:13:51 -080023#include <linux/mm.h>
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080024#include <linux/smp.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080025#include <linux/page-flags.h>
Balbir Singh66e17072008-02-07 00:13:56 -080026#include <linux/backing-dev.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080027#include <linux/bit_spinlock.h>
28#include <linux/rcupdate.h>
Balbir Singhb6ac57d2008-04-29 01:00:19 -070029#include <linux/slab.h>
Balbir Singh66e17072008-02-07 00:13:56 -080030#include <linux/swap.h>
31#include <linux/spinlock.h>
32#include <linux/fs.h>
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -080033#include <linux/seq_file.h>
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -070034#include <linux/vmalloc.h>
Christoph Lameterb69408e2008-10-18 20:26:14 -070035#include <linux/mm_inline.h>
Balbir Singh8cdea7c2008-02-07 00:13:50 -080036
Balbir Singh8697d332008-02-07 00:13:59 -080037#include <asm/uaccess.h>
38
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070039struct cgroup_subsys mem_cgroup_subsys __read_mostly;
40static struct kmem_cache *page_cgroup_cache __read_mostly;
41#define MEM_CGROUP_RECLAIM_RETRIES 5
Balbir Singh8cdea7c2008-02-07 00:13:50 -080042
43/*
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080044 * 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 */
Balaji Rao55e462b2008-05-01 04:35:12 -070052 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
53 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080054
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 */
69static void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat *stat,
70 enum mem_cgroup_stat_index idx, int val)
71{
72 int cpu = smp_processor_id();
73 stat->cpustat[cpu].count[idx] += val;
74}
75
76static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
77 enum mem_cgroup_stat_index idx)
78{
79 int cpu;
80 s64 ret = 0;
81 for_each_possible_cpu(cpu)
82 ret += stat->cpustat[cpu].count[idx];
83 return ret;
84}
85
86/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -080087 * per-zone information in memory controller.
88 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -080089struct mem_cgroup_per_zone {
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -080090 /*
91 * spin_lock to protect the per cgroup LRU
92 */
93 spinlock_t lru_lock;
Christoph Lameterb69408e2008-10-18 20:26:14 -070094 struct list_head lists[NR_LRU_LISTS];
95 unsigned long count[NR_LRU_LISTS];
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -080096};
97/* Macro for accessing counter */
98#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
99
100struct mem_cgroup_per_node {
101 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
102};
103
104struct mem_cgroup_lru_info {
105 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
106};
107
108/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800109 * The memory controller data structure. The memory controller controls both
110 * page cache and RSS per cgroup. We would eventually like to provide
111 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
112 * to help the administrator determine what knobs to tune.
113 *
114 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800115 * we hit the water mark. May be even add a low water mark, such that
116 * no reclaim occurs from a cgroup at it's low water mark, this is
117 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800118 */
119struct mem_cgroup {
120 struct cgroup_subsys_state css;
121 /*
122 * the counter to account for memory usage
123 */
124 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800125 /*
126 * Per cgroup active and inactive list, similar to the
127 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800128 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800129 struct mem_cgroup_lru_info info;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800130
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800131 int prev_priority; /* for recording reclaim priority */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800132 /*
133 * statistics.
134 */
135 struct mem_cgroup_stat stat;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800136};
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800137static struct mem_cgroup init_mem_cgroup;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800138
139/*
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800140 * We use the lower bit of the page->page_cgroup pointer as a bit spin
Hugh Dickins9442ec92008-03-04 14:29:07 -0800141 * lock. We need to ensure that page->page_cgroup is at least two
142 * byte aligned (based on comments from Nick Piggin). But since
143 * bit_spin_lock doesn't actually set that lock bit in a non-debug
144 * uniprocessor kernel, we should avoid setting it here too.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800145 */
146#define PAGE_CGROUP_LOCK_BIT 0x0
Hugh Dickins9442ec92008-03-04 14:29:07 -0800147#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
148#define PAGE_CGROUP_LOCK (1 << PAGE_CGROUP_LOCK_BIT)
149#else
150#define PAGE_CGROUP_LOCK 0x0
151#endif
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800152
153/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800154 * A page_cgroup page is associated with every page descriptor. The
155 * page_cgroup helps us identify information about the cgroup
156 */
157struct page_cgroup {
158 struct list_head lru; /* per cgroup LRU list */
159 struct page *page;
160 struct mem_cgroup *mem_cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800161 int flags;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800162};
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700163#define PAGE_CGROUP_FLAG_CACHE (0x1) /* charged as cache */
164#define PAGE_CGROUP_FLAG_ACTIVE (0x2) /* page is active in this cgroup */
165#define PAGE_CGROUP_FLAG_FILE (0x4) /* page is file system backed */
166#define PAGE_CGROUP_FLAG_UNEVICTABLE (0x8) /* page is unevictableable */
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800167
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800168static int page_cgroup_nid(struct page_cgroup *pc)
KAMEZAWA Hiroyukic01495302008-02-07 00:14:30 -0800169{
170 return page_to_nid(pc->page);
171}
172
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800173static enum zone_type page_cgroup_zid(struct page_cgroup *pc)
KAMEZAWA Hiroyukic01495302008-02-07 00:14:30 -0800174{
175 return page_zonenum(pc->page);
176}
177
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800178enum charge_type {
179 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
180 MEM_CGROUP_CHARGE_TYPE_MAPPED,
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700181 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700182 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800183};
184
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800185/*
186 * Always modified under lru lock. Then, not necessary to preempt_disable()
187 */
188static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, int flags,
189 bool charge)
190{
191 int val = (charge)? 1 : -1;
192 struct mem_cgroup_stat *stat = &mem->stat;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800193
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800194 VM_BUG_ON(!irqs_disabled());
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800195 if (flags & PAGE_CGROUP_FLAG_CACHE)
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800196 __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_CACHE, val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800197 else
198 __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_RSS, val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700199
200 if (charge)
201 __mem_cgroup_stat_add_safe(stat,
202 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
203 else
204 __mem_cgroup_stat_add_safe(stat,
205 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800206}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800207
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800208static struct mem_cgroup_per_zone *
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800209mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
210{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800211 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
212}
213
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800214static struct mem_cgroup_per_zone *
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800215page_cgroup_zoneinfo(struct page_cgroup *pc)
216{
217 struct mem_cgroup *mem = pc->mem_cgroup;
218 int nid = page_cgroup_nid(pc);
219 int zid = page_cgroup_zid(pc);
220
221 return mem_cgroup_zoneinfo(mem, nid, zid);
222}
223
224static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700225 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800226{
227 int nid, zid;
228 struct mem_cgroup_per_zone *mz;
229 u64 total = 0;
230
231 for_each_online_node(nid)
232 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
233 mz = mem_cgroup_zoneinfo(mem, nid, zid);
234 total += MEM_CGROUP_ZSTAT(mz, idx);
235 }
236 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800237}
238
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800239static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800240{
241 return container_of(cgroup_subsys_state(cont,
242 mem_cgroup_subsys_id), struct mem_cgroup,
243 css);
244}
245
Balbir Singhcf475ad2008-04-29 01:00:16 -0700246struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800247{
Balbir Singh31a78f22008-09-28 23:09:31 +0100248 /*
249 * mm_update_next_owner() may clear mm->owner to NULL
250 * if it races with swapoff, page migration, etc.
251 * So this can be called with p == NULL.
252 */
253 if (unlikely(!p))
254 return NULL;
255
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800256 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
257 struct mem_cgroup, css);
258}
259
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800260static inline int page_cgroup_locked(struct page *page)
261{
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800262 return bit_spin_is_locked(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800263}
264
Hugh Dickins9442ec92008-03-04 14:29:07 -0800265static void page_assign_page_cgroup(struct page *page, struct page_cgroup *pc)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800266{
Hugh Dickins9442ec92008-03-04 14:29:07 -0800267 VM_BUG_ON(!page_cgroup_locked(page));
268 page->page_cgroup = ((unsigned long)pc | PAGE_CGROUP_LOCK);
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800269}
270
271struct page_cgroup *page_get_page_cgroup(struct page *page)
272{
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800273 return (struct page_cgroup *) (page->page_cgroup & ~PAGE_CGROUP_LOCK);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800274}
275
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800276static void lock_page_cgroup(struct page *page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800277{
278 bit_spin_lock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800279}
280
Hugh Dickins2680eed2008-03-04 14:29:13 -0800281static int try_lock_page_cgroup(struct page *page)
282{
283 return bit_spin_trylock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
284}
285
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800286static void unlock_page_cgroup(struct page *page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800287{
288 bit_spin_unlock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
289}
290
KAMEZAWA Hiroyuki3eae90c2008-04-29 01:00:22 -0700291static void __mem_cgroup_remove_list(struct mem_cgroup_per_zone *mz,
292 struct page_cgroup *pc)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800293{
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700294 int lru = LRU_BASE;
295
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700296 if (pc->flags & PAGE_CGROUP_FLAG_UNEVICTABLE)
297 lru = LRU_UNEVICTABLE;
298 else {
299 if (pc->flags & PAGE_CGROUP_FLAG_ACTIVE)
300 lru += LRU_ACTIVE;
301 if (pc->flags & PAGE_CGROUP_FLAG_FILE)
302 lru += LRU_FILE;
303 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800304
Christoph Lameterb69408e2008-10-18 20:26:14 -0700305 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800306
307 mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, false);
KAMEZAWA Hiroyuki508b7be2008-07-25 01:47:09 -0700308 list_del(&pc->lru);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800309}
310
KAMEZAWA Hiroyuki3eae90c2008-04-29 01:00:22 -0700311static void __mem_cgroup_add_list(struct mem_cgroup_per_zone *mz,
312 struct page_cgroup *pc)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800313{
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700314 int lru = LRU_BASE;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800315
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700316 if (pc->flags & PAGE_CGROUP_FLAG_UNEVICTABLE)
317 lru = LRU_UNEVICTABLE;
318 else {
319 if (pc->flags & PAGE_CGROUP_FLAG_ACTIVE)
320 lru += LRU_ACTIVE;
321 if (pc->flags & PAGE_CGROUP_FLAG_FILE)
322 lru += LRU_FILE;
323 }
Christoph Lameterb69408e2008-10-18 20:26:14 -0700324
325 MEM_CGROUP_ZSTAT(mz, lru) += 1;
326 list_add(&pc->lru, &mz->lists[lru]);
327
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800328 mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, true);
329}
330
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700331static void __mem_cgroup_move_lists(struct page_cgroup *pc, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800332{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800333 struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700334 int active = pc->flags & PAGE_CGROUP_FLAG_ACTIVE;
335 int file = pc->flags & PAGE_CGROUP_FLAG_FILE;
336 int unevictable = pc->flags & PAGE_CGROUP_FLAG_UNEVICTABLE;
337 enum lru_list from = unevictable ? LRU_UNEVICTABLE :
338 (LRU_FILE * !!file + !!active);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800339
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700340 if (lru == from)
341 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700342
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700343 MEM_CGROUP_ZSTAT(mz, from) -= 1;
344
345 if (is_unevictable_lru(lru)) {
KAMEZAWA Hiroyuki3564c7c2008-02-07 00:14:23 -0800346 pc->flags &= ~PAGE_CGROUP_FLAG_ACTIVE;
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700347 pc->flags |= PAGE_CGROUP_FLAG_UNEVICTABLE;
348 } else {
349 if (is_active_lru(lru))
350 pc->flags |= PAGE_CGROUP_FLAG_ACTIVE;
351 else
352 pc->flags &= ~PAGE_CGROUP_FLAG_ACTIVE;
353 pc->flags &= ~PAGE_CGROUP_FLAG_UNEVICTABLE;
354 }
Christoph Lameterb69408e2008-10-18 20:26:14 -0700355
Christoph Lameterb69408e2008-10-18 20:26:14 -0700356 MEM_CGROUP_ZSTAT(mz, lru) += 1;
357 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800358}
359
David Rientjes4c4a2212008-02-07 00:14:06 -0800360int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
361{
362 int ret;
363
364 task_lock(task);
Hugh Dickinsbd845e32008-03-04 14:29:01 -0800365 ret = task->mm && mm_match_cgroup(task->mm, mem);
David Rientjes4c4a2212008-02-07 00:14:06 -0800366 task_unlock(task);
367 return ret;
368}
369
Balbir Singh66e17072008-02-07 00:13:56 -0800370/*
371 * This routine assumes that the appropriate zone's lru lock is already held
372 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700373void mem_cgroup_move_lists(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800374{
Hugh Dickins427d5412008-03-04 14:29:03 -0800375 struct page_cgroup *pc;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800376 struct mem_cgroup_per_zone *mz;
377 unsigned long flags;
378
Li Zefancede86a2008-07-25 01:47:18 -0700379 if (mem_cgroup_subsys.disabled)
380 return;
381
Hugh Dickins2680eed2008-03-04 14:29:13 -0800382 /*
383 * We cannot lock_page_cgroup while holding zone's lru_lock,
384 * because other holders of lock_page_cgroup can be interrupted
385 * with an attempt to rotate_reclaimable_page. But we cannot
386 * safely get to page_cgroup without it, so just try_lock it:
387 * mem_cgroup_isolate_pages allows for page left on wrong list.
388 */
389 if (!try_lock_page_cgroup(page))
Balbir Singh66e17072008-02-07 00:13:56 -0800390 return;
391
Hugh Dickins2680eed2008-03-04 14:29:13 -0800392 pc = page_get_page_cgroup(page);
393 if (pc) {
Hugh Dickins2680eed2008-03-04 14:29:13 -0800394 mz = page_cgroup_zoneinfo(pc);
Hugh Dickins2680eed2008-03-04 14:29:13 -0800395 spin_lock_irqsave(&mz->lru_lock, flags);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700396 __mem_cgroup_move_lists(pc, lru);
Hugh Dickins2680eed2008-03-04 14:29:13 -0800397 spin_unlock_irqrestore(&mz->lru_lock, flags);
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800398 }
399 unlock_page_cgroup(page);
Balbir Singh66e17072008-02-07 00:13:56 -0800400}
401
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800402/*
403 * Calculate mapped_ratio under memory controller. This will be used in
404 * vmscan.c for deteremining we have to reclaim mapped pages.
405 */
406int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
407{
408 long total, rss;
409
410 /*
411 * usage is recorded in bytes. But, here, we assume the number of
412 * physical pages can be represented by "long" on any arch.
413 */
414 total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
415 rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
416 return (int)((rss * 100L) / total);
417}
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800418
KAMEZAWA Hiroyuki5932f362008-02-07 00:14:33 -0800419/*
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800420 * prev_priority control...this will be used in memory reclaim path.
421 */
422int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
423{
424 return mem->prev_priority;
425}
426
427void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
428{
429 if (priority < mem->prev_priority)
430 mem->prev_priority = priority;
431}
432
433void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
434{
435 mem->prev_priority = priority;
436}
437
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800438/*
439 * Calculate # of pages to be scanned in this priority/zone.
440 * See also vmscan.c
441 *
442 * priority starts from "DEF_PRIORITY" and decremented in each loop.
443 * (see include/linux/mmzone.h)
444 */
445
Christoph Lameterb69408e2008-10-18 20:26:14 -0700446long mem_cgroup_calc_reclaim(struct mem_cgroup *mem, struct zone *zone,
447 int priority, enum lru_list lru)
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800448{
Christoph Lameterb69408e2008-10-18 20:26:14 -0700449 long nr_pages;
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800450 int nid = zone->zone_pgdat->node_id;
451 int zid = zone_idx(zone);
452 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
453
Christoph Lameterb69408e2008-10-18 20:26:14 -0700454 nr_pages = MEM_CGROUP_ZSTAT(mz, lru);
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800455
Christoph Lameterb69408e2008-10-18 20:26:14 -0700456 return (nr_pages >> priority);
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800457}
458
Balbir Singh66e17072008-02-07 00:13:56 -0800459unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
460 struct list_head *dst,
461 unsigned long *scanned, int order,
462 int mode, struct zone *z,
463 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700464 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -0800465{
466 unsigned long nr_taken = 0;
467 struct page *page;
468 unsigned long scan;
469 LIST_HEAD(pc_list);
470 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800471 struct page_cgroup *pc, *tmp;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800472 int nid = z->zone_pgdat->node_id;
473 int zid = zone_idx(z);
474 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700475 int lru = LRU_FILE * !!file + !!active;
Balbir Singh66e17072008-02-07 00:13:56 -0800476
Balbir Singhcf475ad2008-04-29 01:00:16 -0700477 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800478 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700479 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -0800480
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800481 spin_lock(&mz->lru_lock);
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800482 scan = 0;
483 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -0800484 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800485 break;
Balbir Singh66e17072008-02-07 00:13:56 -0800486 page = pc->page;
Balbir Singh66e17072008-02-07 00:13:56 -0800487
Hugh Dickins436c65412008-02-07 00:14:12 -0800488 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800489 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800490
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700491 /*
492 * TODO: play better with lumpy reclaim, grabbing anything.
493 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700494 if (PageUnevictable(page) ||
495 (PageActive(page) && !active) ||
496 (!PageActive(page) && active)) {
497 __mem_cgroup_move_lists(pc, page_lru(page));
Balbir Singh66e17072008-02-07 00:13:56 -0800498 continue;
499 }
500
Hugh Dickins436c65412008-02-07 00:14:12 -0800501 scan++;
502 list_move(&pc->lru, &pc_list);
Balbir Singh66e17072008-02-07 00:13:56 -0800503
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700504 if (__isolate_lru_page(page, mode, file) == 0) {
Balbir Singh66e17072008-02-07 00:13:56 -0800505 list_move(&page->lru, dst);
506 nr_taken++;
507 }
508 }
509
510 list_splice(&pc_list, src);
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800511 spin_unlock(&mz->lru_lock);
Balbir Singh66e17072008-02-07 00:13:56 -0800512
513 *scanned = scan;
514 return nr_taken;
515}
516
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800517/*
518 * Charge the memory controller for page usage.
519 * Return
520 * 0 if the charge was successful
521 * < 0 if the cgroup is over its limit
522 */
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800523static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700524 gfp_t gfp_mask, enum charge_type ctype,
525 struct mem_cgroup *memcg)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800526{
527 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800528 struct page_cgroup *pc;
Balbir Singh66e17072008-02-07 00:13:56 -0800529 unsigned long flags;
530 unsigned long nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800531 struct mem_cgroup_per_zone *mz;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800532
KAMEZAWA Hiroyuki508b7be2008-07-25 01:47:09 -0700533 pc = kmem_cache_alloc(page_cgroup_cache, gfp_mask);
KAMEZAWA Hiroyukib76734e2008-07-25 01:47:16 -0700534 if (unlikely(pc == NULL))
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800535 goto err;
536
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800537 /*
Hugh Dickins3be912772008-02-07 00:14:19 -0800538 * We always charge the cgroup the mm_struct belongs to.
539 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800540 * thread group leader migrates. It's possible that mm is not
541 * set, if so charge the init_mm (happens for pagecache usage).
542 */
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700543 if (likely(!memcg)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700544 rcu_read_lock();
545 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
Balbir Singh31a78f22008-09-28 23:09:31 +0100546 if (unlikely(!mem)) {
547 rcu_read_unlock();
548 kmem_cache_free(page_cgroup_cache, pc);
549 return 0;
550 }
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700551 /*
552 * For every charge from the cgroup, increment reference count
553 */
554 css_get(&mem->css);
555 rcu_read_unlock();
556 } else {
557 mem = memcg;
558 css_get(&memcg->css);
559 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800560
Balbir Singh0eea1032008-02-07 00:13:57 -0800561 while (res_counter_charge(&mem->res, PAGE_SIZE)) {
Hugh Dickins3be912772008-02-07 00:14:19 -0800562 if (!(gfp_mask & __GFP_WAIT))
563 goto out;
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800564
565 if (try_to_free_mem_cgroup_pages(mem, gfp_mask))
Balbir Singh66e17072008-02-07 00:13:56 -0800566 continue;
567
568 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800569 * try_to_free_mem_cgroup_pages() might not give us a full
570 * picture of reclaim. Some pages are reclaimed and might be
571 * moved to swap cache or just unmapped from the cgroup.
572 * Check the limit again to see if the reclaim reduced the
573 * current usage of the cgroup before giving up
574 */
Balbir Singh66e17072008-02-07 00:13:56 -0800575 if (res_counter_check_under_limit(&mem->res))
576 continue;
Hugh Dickins3be912772008-02-07 00:14:19 -0800577
578 if (!nr_retries--) {
579 mem_cgroup_out_of_memory(mem, gfp_mask);
580 goto out;
Balbir Singh66e17072008-02-07 00:13:56 -0800581 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800582 }
583
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800584 pc->mem_cgroup = mem;
585 pc->page = page;
KAMEZAWA Hiroyuki508b7be2008-07-25 01:47:09 -0700586 /*
587 * If a page is accounted as a page cache, insert to inactive list.
588 * If anon, insert to active list.
589 */
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700590 if (ctype == MEM_CGROUP_CHARGE_TYPE_CACHE) {
Balbir Singh4a56d022008-04-29 01:00:23 -0700591 pc->flags = PAGE_CGROUP_FLAG_CACHE;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700592 if (page_is_file_cache(page))
593 pc->flags |= PAGE_CGROUP_FLAG_FILE;
594 else
595 pc->flags |= PAGE_CGROUP_FLAG_ACTIVE;
596 } else if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
KAMEZAWA Hiroyuki508b7be2008-07-25 01:47:09 -0700597 pc->flags = PAGE_CGROUP_FLAG_ACTIVE;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700598 else /* MEM_CGROUP_CHARGE_TYPE_SHMEM */
599 pc->flags = PAGE_CGROUP_FLAG_CACHE | PAGE_CGROUP_FLAG_ACTIVE;
Hugh Dickins3be912772008-02-07 00:14:19 -0800600
Hugh Dickins7e924aa2008-03-04 14:29:08 -0800601 lock_page_cgroup(page);
KAMEZAWA Hiroyukib76734e2008-07-25 01:47:16 -0700602 if (unlikely(page_get_page_cgroup(page))) {
Hugh Dickins7e924aa2008-03-04 14:29:08 -0800603 unlock_page_cgroup(page);
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800604 res_counter_uncharge(&mem->res, PAGE_SIZE);
605 css_put(&mem->css);
Balbir Singhb6ac57d2008-04-29 01:00:19 -0700606 kmem_cache_free(page_cgroup_cache, pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700607 goto done;
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800608 }
Hugh Dickins7e924aa2008-03-04 14:29:08 -0800609 page_assign_page_cgroup(page, pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800610
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800611 mz = page_cgroup_zoneinfo(pc);
612 spin_lock_irqsave(&mz->lru_lock, flags);
KAMEZAWA Hiroyuki3eae90c2008-04-29 01:00:22 -0700613 __mem_cgroup_add_list(mz, pc);
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800614 spin_unlock_irqrestore(&mz->lru_lock, flags);
Balbir Singh66e17072008-02-07 00:13:56 -0800615
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -0800616 unlock_page_cgroup(page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800617done:
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800618 return 0;
Hugh Dickins3be912772008-02-07 00:14:19 -0800619out:
620 css_put(&mem->css);
Balbir Singhb6ac57d2008-04-29 01:00:19 -0700621 kmem_cache_free(page_cgroup_cache, pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800622err:
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800623 return -ENOMEM;
624}
625
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800626int mem_cgroup_charge(struct page *page, struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800627{
Li Zefancede86a2008-07-25 01:47:18 -0700628 if (mem_cgroup_subsys.disabled)
629 return 0;
630
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700631 /*
632 * If already mapped, we don't have to account.
633 * If page cache, page->mapping has address_space.
634 * But page->mapping may have out-of-use anon_vma pointer,
635 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
636 * is NULL.
637 */
638 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
639 return 0;
640 if (unlikely(!mm))
641 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800642 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700643 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800644}
645
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800646int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
647 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -0800648{
Li Zefancede86a2008-07-25 01:47:18 -0700649 if (mem_cgroup_subsys.disabled)
650 return 0;
651
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700652 /*
653 * Corner case handling. This is called from add_to_page_cache()
654 * in usual. But some FS (shmem) precharges this page before calling it
655 * and call add_to_page_cache() with GFP_NOWAIT.
656 *
657 * For GFP_NOWAIT case, the page may be pre-charged before calling
658 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
659 * charge twice. (It works but has to pay a bit larger cost.)
660 */
661 if (!(gfp_mask & __GFP_WAIT)) {
662 struct page_cgroup *pc;
663
664 lock_page_cgroup(page);
665 pc = page_get_page_cgroup(page);
666 if (pc) {
667 VM_BUG_ON(pc->page != page);
668 VM_BUG_ON(!pc->mem_cgroup);
669 unlock_page_cgroup(page);
670 return 0;
671 }
672 unlock_page_cgroup(page);
673 }
674
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700675 if (unlikely(!mm))
Balbir Singh8697d332008-02-07 00:13:59 -0800676 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700677
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800678 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700679 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
680}
681
Balbir Singh8697d332008-02-07 00:13:59 -0800682/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700683 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800684 */
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700685static void
686__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800687{
Hugh Dickins82895462008-03-04 14:29:08 -0800688 struct page_cgroup *pc;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800689 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800690 struct mem_cgroup_per_zone *mz;
Balbir Singh66e17072008-02-07 00:13:56 -0800691 unsigned long flags;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800692
Balbir Singh40779602008-04-04 14:29:59 -0700693 if (mem_cgroup_subsys.disabled)
694 return;
695
Balbir Singh8697d332008-02-07 00:13:59 -0800696 /*
Balbir Singh3c541e12008-02-07 00:14:41 -0800697 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -0800698 */
Hugh Dickins82895462008-03-04 14:29:08 -0800699 lock_page_cgroup(page);
700 pc = page_get_page_cgroup(page);
KAMEZAWA Hiroyukib76734e2008-07-25 01:47:16 -0700701 if (unlikely(!pc))
Hugh Dickins82895462008-03-04 14:29:08 -0800702 goto unlock;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800703
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800704 VM_BUG_ON(pc->page != page);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800705
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700706 if ((ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
707 && ((pc->flags & PAGE_CGROUP_FLAG_CACHE)
708 || page_mapped(page)))
709 goto unlock;
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800710
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700711 mz = page_cgroup_zoneinfo(pc);
712 spin_lock_irqsave(&mz->lru_lock, flags);
713 __mem_cgroup_remove_list(mz, pc);
714 spin_unlock_irqrestore(&mz->lru_lock, flags);
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -0800715
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700716 page_assign_page_cgroup(page, NULL);
717 unlock_page_cgroup(page);
Hugh Dickins6d48ff82008-03-04 14:29:12 -0800718
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700719 mem = pc->mem_cgroup;
720 res_counter_uncharge(&mem->res, PAGE_SIZE);
721 css_put(&mem->css);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800722
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700723 kmem_cache_free(page_cgroup_cache, pc);
724 return;
Hugh Dickins82895462008-03-04 14:29:08 -0800725unlock:
Balbir Singh3c541e12008-02-07 00:14:41 -0800726 unlock_page_cgroup(page);
727}
728
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700729void mem_cgroup_uncharge_page(struct page *page)
730{
731 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
732}
733
734void mem_cgroup_uncharge_cache_page(struct page *page)
735{
736 VM_BUG_ON(page_mapped(page));
737 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
738}
739
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800740/*
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700741 * Before starting migration, account against new page.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800742 */
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700743int mem_cgroup_prepare_migration(struct page *page, struct page *newpage)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800744{
745 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700746 struct mem_cgroup *mem = NULL;
747 enum charge_type ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
748 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800749
Balbir Singh40779602008-04-04 14:29:59 -0700750 if (mem_cgroup_subsys.disabled)
751 return 0;
752
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800753 lock_page_cgroup(page);
754 pc = page_get_page_cgroup(page);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700755 if (pc) {
756 mem = pc->mem_cgroup;
757 css_get(&mem->css);
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700758 if (pc->flags & PAGE_CGROUP_FLAG_CACHE) {
759 if (page_is_file_cache(page))
760 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
761 else
762 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
763 }
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800764 }
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -0800765 unlock_page_cgroup(page);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700766 if (mem) {
767 ret = mem_cgroup_charge_common(newpage, NULL, GFP_KERNEL,
768 ctype, mem);
769 css_put(&mem->css);
770 }
771 return ret;
772}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -0800773
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700774/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700775void mem_cgroup_end_migration(struct page *newpage)
776{
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700777 /*
778 * At success, page->mapping is not NULL.
779 * special rollback care is necessary when
780 * 1. at migration failure. (newpage->mapping is cleared in this case)
781 * 2. the newpage was moved but not remapped again because the task
782 * exits and the newpage is obsolete. In this case, the new page
783 * may be a swapcache. So, we just call mem_cgroup_uncharge_page()
784 * always for avoiding mess. The page_cgroup will be removed if
785 * unnecessary. File cache pages is still on radix-tree. Don't
786 * care it.
787 */
788 if (!newpage->mapping)
789 __mem_cgroup_uncharge_common(newpage,
790 MEM_CGROUP_CHARGE_TYPE_FORCE);
791 else if (PageAnon(newpage))
792 mem_cgroup_uncharge_page(newpage);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800793}
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800794
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800795/*
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -0700796 * A call to try to shrink memory usage under specified resource controller.
797 * This is typically used for page reclaiming for shmem for reducing side
798 * effect of page allocation from shmem, which is used by some mem_cgroup.
799 */
800int mem_cgroup_shrink_usage(struct mm_struct *mm, gfp_t gfp_mask)
801{
802 struct mem_cgroup *mem;
803 int progress = 0;
804 int retry = MEM_CGROUP_RECLAIM_RETRIES;
805
Li Zefancede86a2008-07-25 01:47:18 -0700806 if (mem_cgroup_subsys.disabled)
807 return 0;
Hugh Dickins9623e072008-08-12 15:08:41 -0700808 if (!mm)
809 return 0;
Li Zefancede86a2008-07-25 01:47:18 -0700810
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -0700811 rcu_read_lock();
812 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
Balbir Singh31a78f22008-09-28 23:09:31 +0100813 if (unlikely(!mem)) {
814 rcu_read_unlock();
815 return 0;
816 }
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -0700817 css_get(&mem->css);
818 rcu_read_unlock();
819
820 do {
821 progress = try_to_free_mem_cgroup_pages(mem, gfp_mask);
Daisuke Nishimuraa10cebf2008-09-22 13:57:52 -0700822 progress += res_counter_check_under_limit(&mem->res);
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -0700823 } while (!progress && --retry);
824
825 css_put(&mem->css);
826 if (!retry)
827 return -ENOMEM;
828 return 0;
829}
830
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -0700831int mem_cgroup_resize_limit(struct mem_cgroup *memcg, unsigned long long val)
832{
833
834 int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
835 int progress;
836 int ret = 0;
837
838 while (res_counter_set_limit(&memcg->res, val)) {
839 if (signal_pending(current)) {
840 ret = -EINTR;
841 break;
842 }
843 if (!retry_count) {
844 ret = -EBUSY;
845 break;
846 }
847 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL);
848 if (!progress)
849 retry_count--;
850 }
851 return ret;
852}
853
854
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -0700855/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800856 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800857 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
858 */
859#define FORCE_UNCHARGE_BATCH (128)
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800860static void mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800861 struct mem_cgroup_per_zone *mz,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700862 enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800863{
864 struct page_cgroup *pc;
865 struct page *page;
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800866 int count = FORCE_UNCHARGE_BATCH;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800867 unsigned long flags;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800868 struct list_head *list;
869
Christoph Lameterb69408e2008-10-18 20:26:14 -0700870 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800871
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800872 spin_lock_irqsave(&mz->lru_lock, flags);
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800873 while (!list_empty(list)) {
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800874 pc = list_entry(list->prev, struct page_cgroup, lru);
875 page = pc->page;
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800876 get_page(page);
877 spin_unlock_irqrestore(&mz->lru_lock, flags);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700878 /*
879 * Check if this page is on LRU. !LRU page can be found
880 * if it's under page migration.
881 */
882 if (PageLRU(page)) {
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700883 __mem_cgroup_uncharge_common(page,
884 MEM_CGROUP_CHARGE_TYPE_FORCE);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700885 put_page(page);
886 if (--count <= 0) {
887 count = FORCE_UNCHARGE_BATCH;
888 cond_resched();
889 }
890 } else
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800891 cond_resched();
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800892 spin_lock_irqsave(&mz->lru_lock, flags);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800893 }
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800894 spin_unlock_irqrestore(&mz->lru_lock, flags);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800895}
896
897/*
898 * make mem_cgroup's charge to be 0 if there is no task.
899 * This enables deleting this mem_cgroup.
900 */
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800901static int mem_cgroup_force_empty(struct mem_cgroup *mem)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800902{
903 int ret = -EBUSY;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800904 int node, zid;
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800905
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800906 css_get(&mem->css);
907 /*
908 * page reclaim code (kswapd etc..) will move pages between
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800909 * active_list <-> inactive_list while we don't take a lock.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800910 * So, we have to do loop here until all lists are empty.
911 */
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800912 while (mem->res.usage > 0) {
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800913 if (atomic_read(&mem->css.cgroup->count) > 0)
914 goto out;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800915 for_each_node_state(node, N_POSSIBLE)
916 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
917 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700918 enum lru_list l;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800919 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700920 for_each_lru(l)
921 mem_cgroup_force_empty_list(mem, mz, l);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800922 }
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800923 }
924 ret = 0;
925out:
926 css_put(&mem->css);
927 return ret;
928}
929
Paul Menage2c3daa72008-04-29 00:59:58 -0700930static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800931{
Paul Menage2c3daa72008-04-29 00:59:58 -0700932 return res_counter_read_u64(&mem_cgroup_from_cont(cont)->res,
933 cft->private);
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800934}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -0700935/*
936 * The user of this function is...
937 * RES_LIMIT.
938 */
Paul Menage856c13a2008-07-25 01:47:04 -0700939static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
940 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800941{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -0700942 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
943 unsigned long long val;
944 int ret;
945
946 switch (cft->private) {
947 case RES_LIMIT:
948 /* This function does all necessary parse...reuse it */
949 ret = res_counter_memparse_write_strategy(buffer, &val);
950 if (!ret)
951 ret = mem_cgroup_resize_limit(memcg, val);
952 break;
953 default:
954 ret = -EINVAL; /* should be BUG() ? */
955 break;
956 }
957 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800958}
959
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -0700960static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -0700961{
962 struct mem_cgroup *mem;
963
964 mem = mem_cgroup_from_cont(cont);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -0700965 switch (event) {
966 case RES_MAX_USAGE:
967 res_counter_reset_max(&mem->res);
968 break;
969 case RES_FAILCNT:
970 res_counter_reset_failcnt(&mem->res);
971 break;
972 }
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -0700973 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -0700974}
975
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -0700976static int mem_force_empty_write(struct cgroup *cont, unsigned int event)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800977{
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -0700978 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont));
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800979}
980
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -0800981static const struct mem_cgroup_stat_desc {
982 const char *msg;
983 u64 unit;
984} mem_cgroup_stat_desc[] = {
985 [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
986 [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
Balaji Rao55e462b2008-05-01 04:35:12 -0700987 [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, },
988 [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, },
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -0800989};
990
Paul Menagec64745c2008-04-29 01:00:02 -0700991static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
992 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -0800993{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -0800994 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
995 struct mem_cgroup_stat *stat = &mem_cont->stat;
996 int i;
997
998 for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
999 s64 val;
1000
1001 val = mem_cgroup_read_stat(stat, i);
1002 val *= mem_cgroup_stat_desc[i].unit;
Paul Menagec64745c2008-04-29 01:00:02 -07001003 cb->fill(cb, mem_cgroup_stat_desc[i].msg, val);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001004 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001005 /* showing # of active pages */
1006 {
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001007 unsigned long active_anon, inactive_anon;
1008 unsigned long active_file, inactive_file;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001009
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001010 inactive_anon = mem_cgroup_get_all_zonestat(mem_cont,
1011 LRU_INACTIVE_ANON);
1012 active_anon = mem_cgroup_get_all_zonestat(mem_cont,
1013 LRU_ACTIVE_ANON);
1014 inactive_file = mem_cgroup_get_all_zonestat(mem_cont,
1015 LRU_INACTIVE_FILE);
1016 active_file = mem_cgroup_get_all_zonestat(mem_cont,
1017 LRU_ACTIVE_FILE);
1018 cb->fill(cb, "active_anon", (active_anon) * PAGE_SIZE);
1019 cb->fill(cb, "inactive_anon", (inactive_anon) * PAGE_SIZE);
1020 cb->fill(cb, "active_file", (active_file) * PAGE_SIZE);
1021 cb->fill(cb, "inactive_file", (inactive_file) * PAGE_SIZE);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001022 }
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001023 return 0;
1024}
1025
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001026static struct cftype mem_cgroup_files[] = {
1027 {
Balbir Singh0eea1032008-02-07 00:13:57 -08001028 .name = "usage_in_bytes",
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001029 .private = RES_USAGE,
Paul Menage2c3daa72008-04-29 00:59:58 -07001030 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001031 },
1032 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001033 .name = "max_usage_in_bytes",
1034 .private = RES_MAX_USAGE,
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001035 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001036 .read_u64 = mem_cgroup_read,
1037 },
1038 {
Balbir Singh0eea1032008-02-07 00:13:57 -08001039 .name = "limit_in_bytes",
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001040 .private = RES_LIMIT,
Paul Menage856c13a2008-07-25 01:47:04 -07001041 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07001042 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001043 },
1044 {
1045 .name = "failcnt",
1046 .private = RES_FAILCNT,
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001047 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07001048 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001049 },
Balbir Singh8697d332008-02-07 00:13:59 -08001050 {
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001051 .name = "force_empty",
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07001052 .trigger = mem_force_empty_write,
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001053 },
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001054 {
1055 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07001056 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001057 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001058};
1059
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001060static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1061{
1062 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001063 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07001064 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07001065 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001066 /*
1067 * This routine is called against possible nodes.
1068 * But it's BUG to call kmalloc() against offline node.
1069 *
1070 * TODO: this routine can waste much memory for nodes which will
1071 * never be onlined. It's better to use memory hotplug callback
1072 * function.
1073 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07001074 if (!node_state(node, N_NORMAL_MEMORY))
1075 tmp = -1;
1076 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001077 if (!pn)
1078 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001079
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001080 mem->info.nodeinfo[node] = pn;
1081 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001082
1083 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
1084 mz = &pn->zoneinfo[zone];
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001085 spin_lock_init(&mz->lru_lock);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001086 for_each_lru(l)
1087 INIT_LIST_HEAD(&mz->lists[l]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001088 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001089 return 0;
1090}
1091
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001092static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1093{
1094 kfree(mem->info.nodeinfo[node]);
1095}
1096
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001097static struct mem_cgroup *mem_cgroup_alloc(void)
1098{
1099 struct mem_cgroup *mem;
1100
1101 if (sizeof(*mem) < PAGE_SIZE)
1102 mem = kmalloc(sizeof(*mem), GFP_KERNEL);
1103 else
1104 mem = vmalloc(sizeof(*mem));
1105
1106 if (mem)
1107 memset(mem, 0, sizeof(*mem));
1108 return mem;
1109}
1110
1111static void mem_cgroup_free(struct mem_cgroup *mem)
1112{
1113 if (sizeof(*mem) < PAGE_SIZE)
1114 kfree(mem);
1115 else
1116 vfree(mem);
1117}
1118
1119
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001120static struct cgroup_subsys_state *
1121mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
1122{
1123 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001124 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001125
Balbir Singhb6ac57d2008-04-29 01:00:19 -07001126 if (unlikely((cont->parent) == NULL)) {
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001127 mem = &init_mem_cgroup;
Balbir Singhb6ac57d2008-04-29 01:00:19 -07001128 page_cgroup_cache = KMEM_CACHE(page_cgroup, SLAB_PANIC);
1129 } else {
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001130 mem = mem_cgroup_alloc();
1131 if (!mem)
1132 return ERR_PTR(-ENOMEM);
Balbir Singhb6ac57d2008-04-29 01:00:19 -07001133 }
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001134
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001135 res_counter_init(&mem->res);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001136
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001137 for_each_node_state(node, N_POSSIBLE)
1138 if (alloc_mem_cgroup_per_zone_info(mem, node))
1139 goto free_out;
1140
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001141 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001142free_out:
1143 for_each_node_state(node, N_POSSIBLE)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001144 free_mem_cgroup_per_zone_info(mem, node);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001145 if (cont->parent != NULL)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001146 mem_cgroup_free(mem);
Li Zefan2dda81c2008-02-23 15:24:14 -08001147 return ERR_PTR(-ENOMEM);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001148}
1149
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08001150static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
1151 struct cgroup *cont)
1152{
1153 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1154 mem_cgroup_force_empty(mem);
1155}
1156
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001157static void mem_cgroup_destroy(struct cgroup_subsys *ss,
1158 struct cgroup *cont)
1159{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001160 int node;
1161 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1162
1163 for_each_node_state(node, N_POSSIBLE)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001164 free_mem_cgroup_per_zone_info(mem, node);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001165
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001166 mem_cgroup_free(mem_cgroup_from_cont(cont));
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001167}
1168
1169static int mem_cgroup_populate(struct cgroup_subsys *ss,
1170 struct cgroup *cont)
1171{
1172 return cgroup_add_files(cont, ss, mem_cgroup_files,
1173 ARRAY_SIZE(mem_cgroup_files));
1174}
1175
Balbir Singh67e465a2008-02-07 00:13:54 -08001176static void mem_cgroup_move_task(struct cgroup_subsys *ss,
1177 struct cgroup *cont,
1178 struct cgroup *old_cont,
1179 struct task_struct *p)
1180{
1181 struct mm_struct *mm;
1182 struct mem_cgroup *mem, *old_mem;
1183
1184 mm = get_task_mm(p);
1185 if (mm == NULL)
1186 return;
1187
1188 mem = mem_cgroup_from_cont(cont);
1189 old_mem = mem_cgroup_from_cont(old_cont);
1190
Balbir Singh67e465a2008-02-07 00:13:54 -08001191 /*
1192 * Only thread group leaders are allowed to migrate, the mm_struct is
1193 * in effect owned by the leader
1194 */
Pavel Emelyanov52ea27e2008-03-19 17:00:45 -07001195 if (!thread_group_leader(p))
Balbir Singh67e465a2008-02-07 00:13:54 -08001196 goto out;
1197
Balbir Singh67e465a2008-02-07 00:13:54 -08001198out:
1199 mmput(mm);
Balbir Singh67e465a2008-02-07 00:13:54 -08001200}
1201
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001202struct cgroup_subsys mem_cgroup_subsys = {
1203 .name = "memory",
1204 .subsys_id = mem_cgroup_subsys_id,
1205 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08001206 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001207 .destroy = mem_cgroup_destroy,
1208 .populate = mem_cgroup_populate,
Balbir Singh67e465a2008-02-07 00:13:54 -08001209 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001210 .early_init = 0,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001211};