blob: c0cbd7790c51916a06d30fb904df95cad1f2561b [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};
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800163#define PAGE_CGROUP_FLAG_CACHE (0x1) /* charged as cache */
KAMEZAWA Hiroyuki3564c7c2008-02-07 00:14:23 -0800164#define PAGE_CGROUP_FLAG_ACTIVE (0x2) /* page is active in this cgroup */
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800165
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800166static int page_cgroup_nid(struct page_cgroup *pc)
KAMEZAWA Hiroyukic01495302008-02-07 00:14:30 -0800167{
168 return page_to_nid(pc->page);
169}
170
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800171static enum zone_type page_cgroup_zid(struct page_cgroup *pc)
KAMEZAWA Hiroyukic01495302008-02-07 00:14:30 -0800172{
173 return page_zonenum(pc->page);
174}
175
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800176enum charge_type {
177 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
178 MEM_CGROUP_CHARGE_TYPE_MAPPED,
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700179 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800180};
181
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800182/*
183 * Always modified under lru lock. Then, not necessary to preempt_disable()
184 */
185static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, int flags,
186 bool charge)
187{
188 int val = (charge)? 1 : -1;
189 struct mem_cgroup_stat *stat = &mem->stat;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800190
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800191 VM_BUG_ON(!irqs_disabled());
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800192 if (flags & PAGE_CGROUP_FLAG_CACHE)
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800193 __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_CACHE, val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800194 else
195 __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_RSS, val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700196
197 if (charge)
198 __mem_cgroup_stat_add_safe(stat,
199 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
200 else
201 __mem_cgroup_stat_add_safe(stat,
202 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800203}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800204
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800205static struct mem_cgroup_per_zone *
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800206mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
207{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800208 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
209}
210
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800211static struct mem_cgroup_per_zone *
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800212page_cgroup_zoneinfo(struct page_cgroup *pc)
213{
214 struct mem_cgroup *mem = pc->mem_cgroup;
215 int nid = page_cgroup_nid(pc);
216 int zid = page_cgroup_zid(pc);
217
218 return mem_cgroup_zoneinfo(mem, nid, zid);
219}
220
221static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700222 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800223{
224 int nid, zid;
225 struct mem_cgroup_per_zone *mz;
226 u64 total = 0;
227
228 for_each_online_node(nid)
229 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
230 mz = mem_cgroup_zoneinfo(mem, nid, zid);
231 total += MEM_CGROUP_ZSTAT(mz, idx);
232 }
233 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800234}
235
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800236static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800237{
238 return container_of(cgroup_subsys_state(cont,
239 mem_cgroup_subsys_id), struct mem_cgroup,
240 css);
241}
242
Balbir Singhcf475ad2008-04-29 01:00:16 -0700243struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800244{
Balbir Singh31a78f22008-09-28 23:09:31 +0100245 /*
246 * mm_update_next_owner() may clear mm->owner to NULL
247 * if it races with swapoff, page migration, etc.
248 * So this can be called with p == NULL.
249 */
250 if (unlikely(!p))
251 return NULL;
252
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800253 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
254 struct mem_cgroup, css);
255}
256
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800257static inline int page_cgroup_locked(struct page *page)
258{
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800259 return bit_spin_is_locked(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800260}
261
Hugh Dickins9442ec92008-03-04 14:29:07 -0800262static void page_assign_page_cgroup(struct page *page, struct page_cgroup *pc)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800263{
Hugh Dickins9442ec92008-03-04 14:29:07 -0800264 VM_BUG_ON(!page_cgroup_locked(page));
265 page->page_cgroup = ((unsigned long)pc | PAGE_CGROUP_LOCK);
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800266}
267
268struct page_cgroup *page_get_page_cgroup(struct page *page)
269{
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800270 return (struct page_cgroup *) (page->page_cgroup & ~PAGE_CGROUP_LOCK);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800271}
272
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800273static void lock_page_cgroup(struct page *page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800274{
275 bit_spin_lock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800276}
277
Hugh Dickins2680eed2008-03-04 14:29:13 -0800278static int try_lock_page_cgroup(struct page *page)
279{
280 return bit_spin_trylock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
281}
282
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800283static void unlock_page_cgroup(struct page *page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800284{
285 bit_spin_unlock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
286}
287
KAMEZAWA Hiroyuki3eae90c2008-04-29 01:00:22 -0700288static void __mem_cgroup_remove_list(struct mem_cgroup_per_zone *mz,
289 struct page_cgroup *pc)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800290{
291 int from = pc->flags & PAGE_CGROUP_FLAG_ACTIVE;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700292 int lru = !!from;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800293
Christoph Lameterb69408e2008-10-18 20:26:14 -0700294 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800295
296 mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, false);
KAMEZAWA Hiroyuki508b7be2008-07-25 01:47:09 -0700297 list_del(&pc->lru);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800298}
299
KAMEZAWA Hiroyuki3eae90c2008-04-29 01:00:22 -0700300static void __mem_cgroup_add_list(struct mem_cgroup_per_zone *mz,
301 struct page_cgroup *pc)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800302{
Christoph Lameterb69408e2008-10-18 20:26:14 -0700303 int lru = LRU_INACTIVE;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800304
Christoph Lameterb69408e2008-10-18 20:26:14 -0700305 if (pc->flags & PAGE_CGROUP_FLAG_ACTIVE)
306 lru += LRU_ACTIVE;
307
308 MEM_CGROUP_ZSTAT(mz, lru) += 1;
309 list_add(&pc->lru, &mz->lists[lru]);
310
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800311 mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, true);
312}
313
Balbir Singh8697d332008-02-07 00:13:59 -0800314static void __mem_cgroup_move_lists(struct page_cgroup *pc, bool active)
Balbir Singh66e17072008-02-07 00:13:56 -0800315{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800316 struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700317 int lru = LRU_INACTIVE;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800318
Christoph Lameterb69408e2008-10-18 20:26:14 -0700319 if (pc->flags & PAGE_CGROUP_FLAG_ACTIVE)
320 lru += LRU_ACTIVE;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800321
Christoph Lameterb69408e2008-10-18 20:26:14 -0700322 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
323
324 if (active)
KAMEZAWA Hiroyuki3564c7c2008-02-07 00:14:23 -0800325 pc->flags |= PAGE_CGROUP_FLAG_ACTIVE;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700326 else
KAMEZAWA Hiroyuki3564c7c2008-02-07 00:14:23 -0800327 pc->flags &= ~PAGE_CGROUP_FLAG_ACTIVE;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700328
329 lru = !!active;
330 MEM_CGROUP_ZSTAT(mz, lru) += 1;
331 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800332}
333
David Rientjes4c4a2212008-02-07 00:14:06 -0800334int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
335{
336 int ret;
337
338 task_lock(task);
Hugh Dickinsbd845e32008-03-04 14:29:01 -0800339 ret = task->mm && mm_match_cgroup(task->mm, mem);
David Rientjes4c4a2212008-02-07 00:14:06 -0800340 task_unlock(task);
341 return ret;
342}
343
Balbir Singh66e17072008-02-07 00:13:56 -0800344/*
345 * This routine assumes that the appropriate zone's lru lock is already held
346 */
Hugh Dickins427d5412008-03-04 14:29:03 -0800347void mem_cgroup_move_lists(struct page *page, bool active)
Balbir Singh66e17072008-02-07 00:13:56 -0800348{
Hugh Dickins427d5412008-03-04 14:29:03 -0800349 struct page_cgroup *pc;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800350 struct mem_cgroup_per_zone *mz;
351 unsigned long flags;
352
Li Zefancede86a2008-07-25 01:47:18 -0700353 if (mem_cgroup_subsys.disabled)
354 return;
355
Hugh Dickins2680eed2008-03-04 14:29:13 -0800356 /*
357 * We cannot lock_page_cgroup while holding zone's lru_lock,
358 * because other holders of lock_page_cgroup can be interrupted
359 * with an attempt to rotate_reclaimable_page. But we cannot
360 * safely get to page_cgroup without it, so just try_lock it:
361 * mem_cgroup_isolate_pages allows for page left on wrong list.
362 */
363 if (!try_lock_page_cgroup(page))
Balbir Singh66e17072008-02-07 00:13:56 -0800364 return;
365
Hugh Dickins2680eed2008-03-04 14:29:13 -0800366 pc = page_get_page_cgroup(page);
367 if (pc) {
Hugh Dickins2680eed2008-03-04 14:29:13 -0800368 mz = page_cgroup_zoneinfo(pc);
Hugh Dickins2680eed2008-03-04 14:29:13 -0800369 spin_lock_irqsave(&mz->lru_lock, flags);
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800370 __mem_cgroup_move_lists(pc, active);
Hugh Dickins2680eed2008-03-04 14:29:13 -0800371 spin_unlock_irqrestore(&mz->lru_lock, flags);
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800372 }
373 unlock_page_cgroup(page);
Balbir Singh66e17072008-02-07 00:13:56 -0800374}
375
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800376/*
377 * Calculate mapped_ratio under memory controller. This will be used in
378 * vmscan.c for deteremining we have to reclaim mapped pages.
379 */
380int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
381{
382 long total, rss;
383
384 /*
385 * usage is recorded in bytes. But, here, we assume the number of
386 * physical pages can be represented by "long" on any arch.
387 */
388 total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
389 rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
390 return (int)((rss * 100L) / total);
391}
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800392
KAMEZAWA Hiroyuki5932f362008-02-07 00:14:33 -0800393/*
394 * This function is called from vmscan.c. In page reclaiming loop. balance
395 * between active and inactive list is calculated. For memory controller
396 * page reclaiming, we should use using mem_cgroup's imbalance rather than
397 * zone's global lru imbalance.
398 */
399long mem_cgroup_reclaim_imbalance(struct mem_cgroup *mem)
400{
401 unsigned long active, inactive;
402 /* active and inactive are the number of pages. 'long' is ok.*/
Christoph Lameterb69408e2008-10-18 20:26:14 -0700403 active = mem_cgroup_get_all_zonestat(mem, LRU_ACTIVE);
404 inactive = mem_cgroup_get_all_zonestat(mem, LRU_INACTIVE);
KAMEZAWA Hiroyuki5932f362008-02-07 00:14:33 -0800405 return (long) (active / (inactive + 1));
406}
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800407
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800408/*
409 * prev_priority control...this will be used in memory reclaim path.
410 */
411int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
412{
413 return mem->prev_priority;
414}
415
416void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
417{
418 if (priority < mem->prev_priority)
419 mem->prev_priority = priority;
420}
421
422void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
423{
424 mem->prev_priority = priority;
425}
426
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800427/*
428 * Calculate # of pages to be scanned in this priority/zone.
429 * See also vmscan.c
430 *
431 * priority starts from "DEF_PRIORITY" and decremented in each loop.
432 * (see include/linux/mmzone.h)
433 */
434
Christoph Lameterb69408e2008-10-18 20:26:14 -0700435long mem_cgroup_calc_reclaim(struct mem_cgroup *mem, struct zone *zone,
436 int priority, enum lru_list lru)
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800437{
Christoph Lameterb69408e2008-10-18 20:26:14 -0700438 long nr_pages;
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800439 int nid = zone->zone_pgdat->node_id;
440 int zid = zone_idx(zone);
441 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
442
Christoph Lameterb69408e2008-10-18 20:26:14 -0700443 nr_pages = MEM_CGROUP_ZSTAT(mz, lru);
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800444
Christoph Lameterb69408e2008-10-18 20:26:14 -0700445 return (nr_pages >> priority);
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800446}
447
Balbir Singh66e17072008-02-07 00:13:56 -0800448unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
449 struct list_head *dst,
450 unsigned long *scanned, int order,
451 int mode, struct zone *z,
452 struct mem_cgroup *mem_cont,
453 int active)
454{
455 unsigned long nr_taken = 0;
456 struct page *page;
457 unsigned long scan;
458 LIST_HEAD(pc_list);
459 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800460 struct page_cgroup *pc, *tmp;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800461 int nid = z->zone_pgdat->node_id;
462 int zid = zone_idx(z);
463 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700464 int lru = !!active;
Balbir Singh66e17072008-02-07 00:13:56 -0800465
Balbir Singhcf475ad2008-04-29 01:00:16 -0700466 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800467 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700468 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -0800469
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800470 spin_lock(&mz->lru_lock);
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800471 scan = 0;
472 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -0800473 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800474 break;
Balbir Singh66e17072008-02-07 00:13:56 -0800475 page = pc->page;
Balbir Singh66e17072008-02-07 00:13:56 -0800476
Hugh Dickins436c65412008-02-07 00:14:12 -0800477 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800478 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800479
Balbir Singh66e17072008-02-07 00:13:56 -0800480 if (PageActive(page) && !active) {
481 __mem_cgroup_move_lists(pc, true);
Balbir Singh66e17072008-02-07 00:13:56 -0800482 continue;
483 }
484 if (!PageActive(page) && active) {
485 __mem_cgroup_move_lists(pc, false);
Balbir Singh66e17072008-02-07 00:13:56 -0800486 continue;
487 }
488
Hugh Dickins436c65412008-02-07 00:14:12 -0800489 scan++;
490 list_move(&pc->lru, &pc_list);
Balbir Singh66e17072008-02-07 00:13:56 -0800491
492 if (__isolate_lru_page(page, mode) == 0) {
493 list_move(&page->lru, dst);
494 nr_taken++;
495 }
496 }
497
498 list_splice(&pc_list, src);
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800499 spin_unlock(&mz->lru_lock);
Balbir Singh66e17072008-02-07 00:13:56 -0800500
501 *scanned = scan;
502 return nr_taken;
503}
504
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800505/*
506 * Charge the memory controller for page usage.
507 * Return
508 * 0 if the charge was successful
509 * < 0 if the cgroup is over its limit
510 */
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800511static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700512 gfp_t gfp_mask, enum charge_type ctype,
513 struct mem_cgroup *memcg)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800514{
515 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800516 struct page_cgroup *pc;
Balbir Singh66e17072008-02-07 00:13:56 -0800517 unsigned long flags;
518 unsigned long nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800519 struct mem_cgroup_per_zone *mz;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800520
KAMEZAWA Hiroyuki508b7be2008-07-25 01:47:09 -0700521 pc = kmem_cache_alloc(page_cgroup_cache, gfp_mask);
KAMEZAWA Hiroyukib76734e2008-07-25 01:47:16 -0700522 if (unlikely(pc == NULL))
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800523 goto err;
524
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800525 /*
Hugh Dickins3be91272008-02-07 00:14:19 -0800526 * We always charge the cgroup the mm_struct belongs to.
527 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800528 * thread group leader migrates. It's possible that mm is not
529 * set, if so charge the init_mm (happens for pagecache usage).
530 */
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700531 if (likely(!memcg)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700532 rcu_read_lock();
533 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
Balbir Singh31a78f22008-09-28 23:09:31 +0100534 if (unlikely(!mem)) {
535 rcu_read_unlock();
536 kmem_cache_free(page_cgroup_cache, pc);
537 return 0;
538 }
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700539 /*
540 * For every charge from the cgroup, increment reference count
541 */
542 css_get(&mem->css);
543 rcu_read_unlock();
544 } else {
545 mem = memcg;
546 css_get(&memcg->css);
547 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800548
Balbir Singh0eea1032008-02-07 00:13:57 -0800549 while (res_counter_charge(&mem->res, PAGE_SIZE)) {
Hugh Dickins3be91272008-02-07 00:14:19 -0800550 if (!(gfp_mask & __GFP_WAIT))
551 goto out;
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800552
553 if (try_to_free_mem_cgroup_pages(mem, gfp_mask))
Balbir Singh66e17072008-02-07 00:13:56 -0800554 continue;
555
556 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800557 * try_to_free_mem_cgroup_pages() might not give us a full
558 * picture of reclaim. Some pages are reclaimed and might be
559 * moved to swap cache or just unmapped from the cgroup.
560 * Check the limit again to see if the reclaim reduced the
561 * current usage of the cgroup before giving up
562 */
Balbir Singh66e17072008-02-07 00:13:56 -0800563 if (res_counter_check_under_limit(&mem->res))
564 continue;
Hugh Dickins3be91272008-02-07 00:14:19 -0800565
566 if (!nr_retries--) {
567 mem_cgroup_out_of_memory(mem, gfp_mask);
568 goto out;
Balbir Singh66e17072008-02-07 00:13:56 -0800569 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800570 }
571
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800572 pc->mem_cgroup = mem;
573 pc->page = page;
KAMEZAWA Hiroyuki508b7be2008-07-25 01:47:09 -0700574 /*
575 * If a page is accounted as a page cache, insert to inactive list.
576 * If anon, insert to active list.
577 */
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800578 if (ctype == MEM_CGROUP_CHARGE_TYPE_CACHE)
Balbir Singh4a56d022008-04-29 01:00:23 -0700579 pc->flags = PAGE_CGROUP_FLAG_CACHE;
KAMEZAWA Hiroyuki508b7be2008-07-25 01:47:09 -0700580 else
581 pc->flags = PAGE_CGROUP_FLAG_ACTIVE;
Hugh Dickins3be91272008-02-07 00:14:19 -0800582
Hugh Dickins7e924aa2008-03-04 14:29:08 -0800583 lock_page_cgroup(page);
KAMEZAWA Hiroyukib76734e2008-07-25 01:47:16 -0700584 if (unlikely(page_get_page_cgroup(page))) {
Hugh Dickins7e924aa2008-03-04 14:29:08 -0800585 unlock_page_cgroup(page);
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800586 res_counter_uncharge(&mem->res, PAGE_SIZE);
587 css_put(&mem->css);
Balbir Singhb6ac57d2008-04-29 01:00:19 -0700588 kmem_cache_free(page_cgroup_cache, pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700589 goto done;
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800590 }
Hugh Dickins7e924aa2008-03-04 14:29:08 -0800591 page_assign_page_cgroup(page, pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800592
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800593 mz = page_cgroup_zoneinfo(pc);
594 spin_lock_irqsave(&mz->lru_lock, flags);
KAMEZAWA Hiroyuki3eae90c2008-04-29 01:00:22 -0700595 __mem_cgroup_add_list(mz, pc);
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800596 spin_unlock_irqrestore(&mz->lru_lock, flags);
Balbir Singh66e17072008-02-07 00:13:56 -0800597
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -0800598 unlock_page_cgroup(page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800599done:
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800600 return 0;
Hugh Dickins3be91272008-02-07 00:14:19 -0800601out:
602 css_put(&mem->css);
Balbir Singhb6ac57d2008-04-29 01:00:19 -0700603 kmem_cache_free(page_cgroup_cache, pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800604err:
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800605 return -ENOMEM;
606}
607
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800608int mem_cgroup_charge(struct page *page, struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800609{
Li Zefancede86a2008-07-25 01:47:18 -0700610 if (mem_cgroup_subsys.disabled)
611 return 0;
612
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700613 /*
614 * If already mapped, we don't have to account.
615 * If page cache, page->mapping has address_space.
616 * But page->mapping may have out-of-use anon_vma pointer,
617 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
618 * is NULL.
619 */
620 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
621 return 0;
622 if (unlikely(!mm))
623 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800624 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700625 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800626}
627
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800628int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
629 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -0800630{
Li Zefancede86a2008-07-25 01:47:18 -0700631 if (mem_cgroup_subsys.disabled)
632 return 0;
633
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700634 /*
635 * Corner case handling. This is called from add_to_page_cache()
636 * in usual. But some FS (shmem) precharges this page before calling it
637 * and call add_to_page_cache() with GFP_NOWAIT.
638 *
639 * For GFP_NOWAIT case, the page may be pre-charged before calling
640 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
641 * charge twice. (It works but has to pay a bit larger cost.)
642 */
643 if (!(gfp_mask & __GFP_WAIT)) {
644 struct page_cgroup *pc;
645
646 lock_page_cgroup(page);
647 pc = page_get_page_cgroup(page);
648 if (pc) {
649 VM_BUG_ON(pc->page != page);
650 VM_BUG_ON(!pc->mem_cgroup);
651 unlock_page_cgroup(page);
652 return 0;
653 }
654 unlock_page_cgroup(page);
655 }
656
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700657 if (unlikely(!mm))
Balbir Singh8697d332008-02-07 00:13:59 -0800658 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700659
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800660 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700661 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
662}
663
Balbir Singh8697d332008-02-07 00:13:59 -0800664/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700665 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800666 */
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700667static void
668__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800669{
Hugh Dickins82895462008-03-04 14:29:08 -0800670 struct page_cgroup *pc;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800671 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800672 struct mem_cgroup_per_zone *mz;
Balbir Singh66e17072008-02-07 00:13:56 -0800673 unsigned long flags;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800674
Balbir Singh40779602008-04-04 14:29:59 -0700675 if (mem_cgroup_subsys.disabled)
676 return;
677
Balbir Singh8697d332008-02-07 00:13:59 -0800678 /*
Balbir Singh3c541e12008-02-07 00:14:41 -0800679 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -0800680 */
Hugh Dickins82895462008-03-04 14:29:08 -0800681 lock_page_cgroup(page);
682 pc = page_get_page_cgroup(page);
KAMEZAWA Hiroyukib76734e2008-07-25 01:47:16 -0700683 if (unlikely(!pc))
Hugh Dickins82895462008-03-04 14:29:08 -0800684 goto unlock;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800685
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800686 VM_BUG_ON(pc->page != page);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800687
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700688 if ((ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
689 && ((pc->flags & PAGE_CGROUP_FLAG_CACHE)
690 || page_mapped(page)))
691 goto unlock;
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800692
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700693 mz = page_cgroup_zoneinfo(pc);
694 spin_lock_irqsave(&mz->lru_lock, flags);
695 __mem_cgroup_remove_list(mz, pc);
696 spin_unlock_irqrestore(&mz->lru_lock, flags);
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -0800697
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700698 page_assign_page_cgroup(page, NULL);
699 unlock_page_cgroup(page);
Hugh Dickins6d48ff82008-03-04 14:29:12 -0800700
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700701 mem = pc->mem_cgroup;
702 res_counter_uncharge(&mem->res, PAGE_SIZE);
703 css_put(&mem->css);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800704
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700705 kmem_cache_free(page_cgroup_cache, pc);
706 return;
Hugh Dickins82895462008-03-04 14:29:08 -0800707unlock:
Balbir Singh3c541e12008-02-07 00:14:41 -0800708 unlock_page_cgroup(page);
709}
710
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700711void mem_cgroup_uncharge_page(struct page *page)
712{
713 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
714}
715
716void mem_cgroup_uncharge_cache_page(struct page *page)
717{
718 VM_BUG_ON(page_mapped(page));
719 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
720}
721
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800722/*
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700723 * Before starting migration, account against new page.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800724 */
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700725int mem_cgroup_prepare_migration(struct page *page, struct page *newpage)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800726{
727 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700728 struct mem_cgroup *mem = NULL;
729 enum charge_type ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
730 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800731
Balbir Singh40779602008-04-04 14:29:59 -0700732 if (mem_cgroup_subsys.disabled)
733 return 0;
734
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800735 lock_page_cgroup(page);
736 pc = page_get_page_cgroup(page);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700737 if (pc) {
738 mem = pc->mem_cgroup;
739 css_get(&mem->css);
740 if (pc->flags & PAGE_CGROUP_FLAG_CACHE)
741 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800742 }
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -0800743 unlock_page_cgroup(page);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700744 if (mem) {
745 ret = mem_cgroup_charge_common(newpage, NULL, GFP_KERNEL,
746 ctype, mem);
747 css_put(&mem->css);
748 }
749 return ret;
750}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -0800751
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700752/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700753void mem_cgroup_end_migration(struct page *newpage)
754{
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700755 /*
756 * At success, page->mapping is not NULL.
757 * special rollback care is necessary when
758 * 1. at migration failure. (newpage->mapping is cleared in this case)
759 * 2. the newpage was moved but not remapped again because the task
760 * exits and the newpage is obsolete. In this case, the new page
761 * may be a swapcache. So, we just call mem_cgroup_uncharge_page()
762 * always for avoiding mess. The page_cgroup will be removed if
763 * unnecessary. File cache pages is still on radix-tree. Don't
764 * care it.
765 */
766 if (!newpage->mapping)
767 __mem_cgroup_uncharge_common(newpage,
768 MEM_CGROUP_CHARGE_TYPE_FORCE);
769 else if (PageAnon(newpage))
770 mem_cgroup_uncharge_page(newpage);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800771}
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800772
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800773/*
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -0700774 * A call to try to shrink memory usage under specified resource controller.
775 * This is typically used for page reclaiming for shmem for reducing side
776 * effect of page allocation from shmem, which is used by some mem_cgroup.
777 */
778int mem_cgroup_shrink_usage(struct mm_struct *mm, gfp_t gfp_mask)
779{
780 struct mem_cgroup *mem;
781 int progress = 0;
782 int retry = MEM_CGROUP_RECLAIM_RETRIES;
783
Li Zefancede86a2008-07-25 01:47:18 -0700784 if (mem_cgroup_subsys.disabled)
785 return 0;
Hugh Dickins9623e072008-08-12 15:08:41 -0700786 if (!mm)
787 return 0;
Li Zefancede86a2008-07-25 01:47:18 -0700788
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -0700789 rcu_read_lock();
790 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
Balbir Singh31a78f22008-09-28 23:09:31 +0100791 if (unlikely(!mem)) {
792 rcu_read_unlock();
793 return 0;
794 }
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -0700795 css_get(&mem->css);
796 rcu_read_unlock();
797
798 do {
799 progress = try_to_free_mem_cgroup_pages(mem, gfp_mask);
Daisuke Nishimuraa10cebf2008-09-22 13:57:52 -0700800 progress += res_counter_check_under_limit(&mem->res);
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -0700801 } while (!progress && --retry);
802
803 css_put(&mem->css);
804 if (!retry)
805 return -ENOMEM;
806 return 0;
807}
808
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -0700809int mem_cgroup_resize_limit(struct mem_cgroup *memcg, unsigned long long val)
810{
811
812 int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
813 int progress;
814 int ret = 0;
815
816 while (res_counter_set_limit(&memcg->res, val)) {
817 if (signal_pending(current)) {
818 ret = -EINTR;
819 break;
820 }
821 if (!retry_count) {
822 ret = -EBUSY;
823 break;
824 }
825 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL);
826 if (!progress)
827 retry_count--;
828 }
829 return ret;
830}
831
832
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -0700833/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800834 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800835 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
836 */
837#define FORCE_UNCHARGE_BATCH (128)
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800838static void mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800839 struct mem_cgroup_per_zone *mz,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700840 enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800841{
842 struct page_cgroup *pc;
843 struct page *page;
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800844 int count = FORCE_UNCHARGE_BATCH;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800845 unsigned long flags;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800846 struct list_head *list;
847
Christoph Lameterb69408e2008-10-18 20:26:14 -0700848 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800849
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800850 spin_lock_irqsave(&mz->lru_lock, flags);
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800851 while (!list_empty(list)) {
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800852 pc = list_entry(list->prev, struct page_cgroup, lru);
853 page = pc->page;
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800854 get_page(page);
855 spin_unlock_irqrestore(&mz->lru_lock, flags);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700856 /*
857 * Check if this page is on LRU. !LRU page can be found
858 * if it's under page migration.
859 */
860 if (PageLRU(page)) {
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700861 __mem_cgroup_uncharge_common(page,
862 MEM_CGROUP_CHARGE_TYPE_FORCE);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700863 put_page(page);
864 if (--count <= 0) {
865 count = FORCE_UNCHARGE_BATCH;
866 cond_resched();
867 }
868 } else
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800869 cond_resched();
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800870 spin_lock_irqsave(&mz->lru_lock, flags);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800871 }
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800872 spin_unlock_irqrestore(&mz->lru_lock, flags);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800873}
874
875/*
876 * make mem_cgroup's charge to be 0 if there is no task.
877 * This enables deleting this mem_cgroup.
878 */
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800879static int mem_cgroup_force_empty(struct mem_cgroup *mem)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800880{
881 int ret = -EBUSY;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800882 int node, zid;
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800883
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800884 css_get(&mem->css);
885 /*
886 * page reclaim code (kswapd etc..) will move pages between
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800887 * active_list <-> inactive_list while we don't take a lock.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800888 * So, we have to do loop here until all lists are empty.
889 */
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800890 while (mem->res.usage > 0) {
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800891 if (atomic_read(&mem->css.cgroup->count) > 0)
892 goto out;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800893 for_each_node_state(node, N_POSSIBLE)
894 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
895 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700896 enum lru_list l;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800897 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700898 for_each_lru(l)
899 mem_cgroup_force_empty_list(mem, mz, l);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800900 }
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800901 }
902 ret = 0;
903out:
904 css_put(&mem->css);
905 return ret;
906}
907
Paul Menage2c3daa72008-04-29 00:59:58 -0700908static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800909{
Paul Menage2c3daa72008-04-29 00:59:58 -0700910 return res_counter_read_u64(&mem_cgroup_from_cont(cont)->res,
911 cft->private);
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800912}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -0700913/*
914 * The user of this function is...
915 * RES_LIMIT.
916 */
Paul Menage856c13a2008-07-25 01:47:04 -0700917static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
918 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800919{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -0700920 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
921 unsigned long long val;
922 int ret;
923
924 switch (cft->private) {
925 case RES_LIMIT:
926 /* This function does all necessary parse...reuse it */
927 ret = res_counter_memparse_write_strategy(buffer, &val);
928 if (!ret)
929 ret = mem_cgroup_resize_limit(memcg, val);
930 break;
931 default:
932 ret = -EINVAL; /* should be BUG() ? */
933 break;
934 }
935 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800936}
937
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -0700938static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -0700939{
940 struct mem_cgroup *mem;
941
942 mem = mem_cgroup_from_cont(cont);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -0700943 switch (event) {
944 case RES_MAX_USAGE:
945 res_counter_reset_max(&mem->res);
946 break;
947 case RES_FAILCNT:
948 res_counter_reset_failcnt(&mem->res);
949 break;
950 }
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -0700951 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -0700952}
953
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -0700954static int mem_force_empty_write(struct cgroup *cont, unsigned int event)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800955{
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -0700956 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont));
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800957}
958
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -0800959static const struct mem_cgroup_stat_desc {
960 const char *msg;
961 u64 unit;
962} mem_cgroup_stat_desc[] = {
963 [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
964 [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
Balaji Rao55e462b2008-05-01 04:35:12 -0700965 [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, },
966 [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, },
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -0800967};
968
Paul Menagec64745c2008-04-29 01:00:02 -0700969static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
970 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -0800971{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -0800972 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
973 struct mem_cgroup_stat *stat = &mem_cont->stat;
974 int i;
975
976 for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
977 s64 val;
978
979 val = mem_cgroup_read_stat(stat, i);
980 val *= mem_cgroup_stat_desc[i].unit;
Paul Menagec64745c2008-04-29 01:00:02 -0700981 cb->fill(cb, mem_cgroup_stat_desc[i].msg, val);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -0800982 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800983 /* showing # of active pages */
984 {
985 unsigned long active, inactive;
986
987 inactive = mem_cgroup_get_all_zonestat(mem_cont,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700988 LRU_INACTIVE);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800989 active = mem_cgroup_get_all_zonestat(mem_cont,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700990 LRU_ACTIVE);
Paul Menagec64745c2008-04-29 01:00:02 -0700991 cb->fill(cb, "active", (active) * PAGE_SIZE);
992 cb->fill(cb, "inactive", (inactive) * PAGE_SIZE);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800993 }
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -0800994 return 0;
995}
996
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800997static struct cftype mem_cgroup_files[] = {
998 {
Balbir Singh0eea1032008-02-07 00:13:57 -0800999 .name = "usage_in_bytes",
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001000 .private = RES_USAGE,
Paul Menage2c3daa72008-04-29 00:59:58 -07001001 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001002 },
1003 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001004 .name = "max_usage_in_bytes",
1005 .private = RES_MAX_USAGE,
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001006 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001007 .read_u64 = mem_cgroup_read,
1008 },
1009 {
Balbir Singh0eea1032008-02-07 00:13:57 -08001010 .name = "limit_in_bytes",
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001011 .private = RES_LIMIT,
Paul Menage856c13a2008-07-25 01:47:04 -07001012 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07001013 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001014 },
1015 {
1016 .name = "failcnt",
1017 .private = RES_FAILCNT,
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001018 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07001019 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001020 },
Balbir Singh8697d332008-02-07 00:13:59 -08001021 {
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001022 .name = "force_empty",
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07001023 .trigger = mem_force_empty_write,
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001024 },
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001025 {
1026 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07001027 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001028 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001029};
1030
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001031static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1032{
1033 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001034 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07001035 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07001036 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001037 /*
1038 * This routine is called against possible nodes.
1039 * But it's BUG to call kmalloc() against offline node.
1040 *
1041 * TODO: this routine can waste much memory for nodes which will
1042 * never be onlined. It's better to use memory hotplug callback
1043 * function.
1044 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07001045 if (!node_state(node, N_NORMAL_MEMORY))
1046 tmp = -1;
1047 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001048 if (!pn)
1049 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001050
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001051 mem->info.nodeinfo[node] = pn;
1052 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001053
1054 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
1055 mz = &pn->zoneinfo[zone];
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001056 spin_lock_init(&mz->lru_lock);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001057 for_each_lru(l)
1058 INIT_LIST_HEAD(&mz->lists[l]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001059 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001060 return 0;
1061}
1062
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001063static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1064{
1065 kfree(mem->info.nodeinfo[node]);
1066}
1067
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001068static struct mem_cgroup *mem_cgroup_alloc(void)
1069{
1070 struct mem_cgroup *mem;
1071
1072 if (sizeof(*mem) < PAGE_SIZE)
1073 mem = kmalloc(sizeof(*mem), GFP_KERNEL);
1074 else
1075 mem = vmalloc(sizeof(*mem));
1076
1077 if (mem)
1078 memset(mem, 0, sizeof(*mem));
1079 return mem;
1080}
1081
1082static void mem_cgroup_free(struct mem_cgroup *mem)
1083{
1084 if (sizeof(*mem) < PAGE_SIZE)
1085 kfree(mem);
1086 else
1087 vfree(mem);
1088}
1089
1090
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001091static struct cgroup_subsys_state *
1092mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
1093{
1094 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001095 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001096
Balbir Singhb6ac57d2008-04-29 01:00:19 -07001097 if (unlikely((cont->parent) == NULL)) {
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001098 mem = &init_mem_cgroup;
Balbir Singhb6ac57d2008-04-29 01:00:19 -07001099 page_cgroup_cache = KMEM_CACHE(page_cgroup, SLAB_PANIC);
1100 } else {
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001101 mem = mem_cgroup_alloc();
1102 if (!mem)
1103 return ERR_PTR(-ENOMEM);
Balbir Singhb6ac57d2008-04-29 01:00:19 -07001104 }
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001105
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001106 res_counter_init(&mem->res);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001107
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001108 for_each_node_state(node, N_POSSIBLE)
1109 if (alloc_mem_cgroup_per_zone_info(mem, node))
1110 goto free_out;
1111
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001112 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001113free_out:
1114 for_each_node_state(node, N_POSSIBLE)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001115 free_mem_cgroup_per_zone_info(mem, node);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001116 if (cont->parent != NULL)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001117 mem_cgroup_free(mem);
Li Zefan2dda81c2008-02-23 15:24:14 -08001118 return ERR_PTR(-ENOMEM);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001119}
1120
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08001121static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
1122 struct cgroup *cont)
1123{
1124 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1125 mem_cgroup_force_empty(mem);
1126}
1127
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001128static void mem_cgroup_destroy(struct cgroup_subsys *ss,
1129 struct cgroup *cont)
1130{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001131 int node;
1132 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1133
1134 for_each_node_state(node, N_POSSIBLE)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001135 free_mem_cgroup_per_zone_info(mem, node);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001136
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001137 mem_cgroup_free(mem_cgroup_from_cont(cont));
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001138}
1139
1140static int mem_cgroup_populate(struct cgroup_subsys *ss,
1141 struct cgroup *cont)
1142{
1143 return cgroup_add_files(cont, ss, mem_cgroup_files,
1144 ARRAY_SIZE(mem_cgroup_files));
1145}
1146
Balbir Singh67e465a2008-02-07 00:13:54 -08001147static void mem_cgroup_move_task(struct cgroup_subsys *ss,
1148 struct cgroup *cont,
1149 struct cgroup *old_cont,
1150 struct task_struct *p)
1151{
1152 struct mm_struct *mm;
1153 struct mem_cgroup *mem, *old_mem;
1154
1155 mm = get_task_mm(p);
1156 if (mm == NULL)
1157 return;
1158
1159 mem = mem_cgroup_from_cont(cont);
1160 old_mem = mem_cgroup_from_cont(old_cont);
1161
Balbir Singh67e465a2008-02-07 00:13:54 -08001162 /*
1163 * Only thread group leaders are allowed to migrate, the mm_struct is
1164 * in effect owned by the leader
1165 */
Pavel Emelyanov52ea27e2008-03-19 17:00:45 -07001166 if (!thread_group_leader(p))
Balbir Singh67e465a2008-02-07 00:13:54 -08001167 goto out;
1168
Balbir Singh67e465a2008-02-07 00:13:54 -08001169out:
1170 mmput(mm);
Balbir Singh67e465a2008-02-07 00:13:54 -08001171}
1172
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001173struct cgroup_subsys mem_cgroup_subsys = {
1174 .name = "memory",
1175 .subsys_id = mem_cgroup_subsys_id,
1176 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08001177 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001178 .destroy = mem_cgroup_destroy,
1179 .populate = mem_cgroup_populate,
Balbir Singh67e465a2008-02-07 00:13:54 -08001180 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001181 .early_init = 0,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001182};