blob: 031682e7ef0c8fbafe2d18ee397925b2335a25af [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 */
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -070069static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080070 enum mem_cgroup_stat_index idx, int val)
71{
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -070072 stat->count[idx] += val;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080073}
74
75static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
76 enum mem_cgroup_stat_index idx)
77{
78 int cpu;
79 s64 ret = 0;
80 for_each_possible_cpu(cpu)
81 ret += stat->cpustat[cpu].count[idx];
82 return ret;
83}
84
85/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -080086 * per-zone information in memory controller.
87 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -080088struct mem_cgroup_per_zone {
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -080089 /*
90 * spin_lock to protect the per cgroup LRU
91 */
92 spinlock_t lru_lock;
Christoph Lameterb69408e2008-10-18 20:26:14 -070093 struct list_head lists[NR_LRU_LISTS];
94 unsigned long count[NR_LRU_LISTS];
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -080095};
96/* Macro for accessing counter */
97#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
98
99struct mem_cgroup_per_node {
100 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
101};
102
103struct mem_cgroup_lru_info {
104 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
105};
106
107/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800108 * The memory controller data structure. The memory controller controls both
109 * page cache and RSS per cgroup. We would eventually like to provide
110 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
111 * to help the administrator determine what knobs to tune.
112 *
113 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800114 * we hit the water mark. May be even add a low water mark, such that
115 * no reclaim occurs from a cgroup at it's low water mark, this is
116 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800117 */
118struct mem_cgroup {
119 struct cgroup_subsys_state css;
120 /*
121 * the counter to account for memory usage
122 */
123 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800124 /*
125 * Per cgroup active and inactive list, similar to the
126 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800127 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800128 struct mem_cgroup_lru_info info;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800129
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800130 int prev_priority; /* for recording reclaim priority */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800131 /*
132 * statistics.
133 */
134 struct mem_cgroup_stat stat;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800135};
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800136static struct mem_cgroup init_mem_cgroup;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800137
138/*
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800139 * We use the lower bit of the page->page_cgroup pointer as a bit spin
Hugh Dickins9442ec92008-03-04 14:29:07 -0800140 * lock. We need to ensure that page->page_cgroup is at least two
141 * byte aligned (based on comments from Nick Piggin). But since
142 * bit_spin_lock doesn't actually set that lock bit in a non-debug
143 * uniprocessor kernel, we should avoid setting it here too.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800144 */
145#define PAGE_CGROUP_LOCK_BIT 0x0
Hugh Dickins9442ec92008-03-04 14:29:07 -0800146#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
147#define PAGE_CGROUP_LOCK (1 << PAGE_CGROUP_LOCK_BIT)
148#else
149#define PAGE_CGROUP_LOCK 0x0
150#endif
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800151
152/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800153 * A page_cgroup page is associated with every page descriptor. The
154 * page_cgroup helps us identify information about the cgroup
155 */
156struct page_cgroup {
157 struct list_head lru; /* per cgroup LRU list */
158 struct page *page;
159 struct mem_cgroup *mem_cgroup;
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700160 unsigned long flags;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800161};
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700162
163enum {
164 /* flags for mem_cgroup */
165 PCG_CACHE, /* charged as cache */
166 /* flags for LRU placement */
167 PCG_ACTIVE, /* page is active in this cgroup */
168 PCG_FILE, /* page is file system backed */
169 PCG_UNEVICTABLE, /* page is unevictableable */
170};
171
172#define TESTPCGFLAG(uname, lname) \
173static inline int PageCgroup##uname(struct page_cgroup *pc) \
174 { return test_bit(PCG_##lname, &pc->flags); }
175
176#define SETPCGFLAG(uname, lname) \
177static inline void SetPageCgroup##uname(struct page_cgroup *pc)\
178 { set_bit(PCG_##lname, &pc->flags); }
179
180#define CLEARPCGFLAG(uname, lname) \
181static inline void ClearPageCgroup##uname(struct page_cgroup *pc) \
182 { clear_bit(PCG_##lname, &pc->flags); }
183
184
185/* Cache flag is set only once (at allocation) */
186TESTPCGFLAG(Cache, CACHE)
187
188/* LRU management flags (from global-lru definition) */
189TESTPCGFLAG(File, FILE)
190SETPCGFLAG(File, FILE)
191CLEARPCGFLAG(File, FILE)
192
193TESTPCGFLAG(Active, ACTIVE)
194SETPCGFLAG(Active, ACTIVE)
195CLEARPCGFLAG(Active, ACTIVE)
196
197TESTPCGFLAG(Unevictable, UNEVICTABLE)
198SETPCGFLAG(Unevictable, UNEVICTABLE)
199CLEARPCGFLAG(Unevictable, UNEVICTABLE)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800200
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800201static int page_cgroup_nid(struct page_cgroup *pc)
KAMEZAWA Hiroyukic01495302008-02-07 00:14:30 -0800202{
203 return page_to_nid(pc->page);
204}
205
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800206static enum zone_type page_cgroup_zid(struct page_cgroup *pc)
KAMEZAWA Hiroyukic01495302008-02-07 00:14:30 -0800207{
208 return page_zonenum(pc->page);
209}
210
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800211enum charge_type {
212 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
213 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700214 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700215 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
216 NR_CHARGE_TYPE,
217};
218
219static const unsigned long
220pcg_default_flags[NR_CHARGE_TYPE] = {
221 ((1 << PCG_CACHE) | (1 << PCG_FILE)),
222 ((1 << PCG_ACTIVE)),
223 ((1 << PCG_ACTIVE) | (1 << PCG_CACHE)),
224 0,
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800225};
226
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800227/*
228 * Always modified under lru lock. Then, not necessary to preempt_disable()
229 */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700230static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
231 struct page_cgroup *pc,
232 bool charge)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800233{
234 int val = (charge)? 1 : -1;
235 struct mem_cgroup_stat *stat = &mem->stat;
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700236 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800237
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800238 VM_BUG_ON(!irqs_disabled());
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700239
240 cpustat = &stat->cpustat[smp_processor_id()];
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700241 if (PageCgroupCache(pc))
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700242 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800243 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700244 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700245
246 if (charge)
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700247 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700248 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
249 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700250 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700251 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800252}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800253
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800254static struct mem_cgroup_per_zone *
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800255mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
256{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800257 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
258}
259
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800260static struct mem_cgroup_per_zone *
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800261page_cgroup_zoneinfo(struct page_cgroup *pc)
262{
263 struct mem_cgroup *mem = pc->mem_cgroup;
264 int nid = page_cgroup_nid(pc);
265 int zid = page_cgroup_zid(pc);
266
267 return mem_cgroup_zoneinfo(mem, nid, zid);
268}
269
270static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700271 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800272{
273 int nid, zid;
274 struct mem_cgroup_per_zone *mz;
275 u64 total = 0;
276
277 for_each_online_node(nid)
278 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
279 mz = mem_cgroup_zoneinfo(mem, nid, zid);
280 total += MEM_CGROUP_ZSTAT(mz, idx);
281 }
282 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800283}
284
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800285static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800286{
287 return container_of(cgroup_subsys_state(cont,
288 mem_cgroup_subsys_id), struct mem_cgroup,
289 css);
290}
291
Balbir Singhcf475ad2008-04-29 01:00:16 -0700292struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800293{
Balbir Singh31a78f22008-09-28 23:09:31 +0100294 /*
295 * mm_update_next_owner() may clear mm->owner to NULL
296 * if it races with swapoff, page migration, etc.
297 * So this can be called with p == NULL.
298 */
299 if (unlikely(!p))
300 return NULL;
301
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800302 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
303 struct mem_cgroup, css);
304}
305
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800306static inline int page_cgroup_locked(struct page *page)
307{
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800308 return bit_spin_is_locked(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800309}
310
Hugh Dickins9442ec92008-03-04 14:29:07 -0800311static void page_assign_page_cgroup(struct page *page, struct page_cgroup *pc)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800312{
Hugh Dickins9442ec92008-03-04 14:29:07 -0800313 VM_BUG_ON(!page_cgroup_locked(page));
314 page->page_cgroup = ((unsigned long)pc | PAGE_CGROUP_LOCK);
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800315}
316
317struct page_cgroup *page_get_page_cgroup(struct page *page)
318{
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800319 return (struct page_cgroup *) (page->page_cgroup & ~PAGE_CGROUP_LOCK);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800320}
321
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800322static void lock_page_cgroup(struct page *page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800323{
324 bit_spin_lock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800325}
326
Hugh Dickins2680eed2008-03-04 14:29:13 -0800327static int try_lock_page_cgroup(struct page *page)
328{
329 return bit_spin_trylock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
330}
331
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800332static void unlock_page_cgroup(struct page *page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800333{
334 bit_spin_unlock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
335}
336
KAMEZAWA Hiroyuki3eae90c2008-04-29 01:00:22 -0700337static void __mem_cgroup_remove_list(struct mem_cgroup_per_zone *mz,
338 struct page_cgroup *pc)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800339{
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700340 int lru = LRU_BASE;
341
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700342 if (PageCgroupUnevictable(pc))
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700343 lru = LRU_UNEVICTABLE;
344 else {
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700345 if (PageCgroupActive(pc))
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700346 lru += LRU_ACTIVE;
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700347 if (PageCgroupFile(pc))
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700348 lru += LRU_FILE;
349 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800350
Christoph Lameterb69408e2008-10-18 20:26:14 -0700351 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800352
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700353 mem_cgroup_charge_statistics(pc->mem_cgroup, pc, false);
KAMEZAWA Hiroyuki508b7be2008-07-25 01:47:09 -0700354 list_del(&pc->lru);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800355}
356
KAMEZAWA Hiroyuki3eae90c2008-04-29 01:00:22 -0700357static void __mem_cgroup_add_list(struct mem_cgroup_per_zone *mz,
358 struct page_cgroup *pc)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800359{
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700360 int lru = LRU_BASE;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800361
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700362 if (PageCgroupUnevictable(pc))
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700363 lru = LRU_UNEVICTABLE;
364 else {
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700365 if (PageCgroupActive(pc))
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700366 lru += LRU_ACTIVE;
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700367 if (PageCgroupFile(pc))
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700368 lru += LRU_FILE;
369 }
Christoph Lameterb69408e2008-10-18 20:26:14 -0700370
371 MEM_CGROUP_ZSTAT(mz, lru) += 1;
372 list_add(&pc->lru, &mz->lists[lru]);
373
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700374 mem_cgroup_charge_statistics(pc->mem_cgroup, pc, true);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800375}
376
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700377static void __mem_cgroup_move_lists(struct page_cgroup *pc, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800378{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800379 struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700380 int active = PageCgroupActive(pc);
381 int file = PageCgroupFile(pc);
382 int unevictable = PageCgroupUnevictable(pc);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700383 enum lru_list from = unevictable ? LRU_UNEVICTABLE :
384 (LRU_FILE * !!file + !!active);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800385
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700386 if (lru == from)
387 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700388
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700389 MEM_CGROUP_ZSTAT(mz, from) -= 1;
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700390 /*
391 * However this is done under mz->lru_lock, another flags, which
392 * are not related to LRU, will be modified from out-of-lock.
393 * We have to use atomic set/clear flags.
394 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700395 if (is_unevictable_lru(lru)) {
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700396 ClearPageCgroupActive(pc);
397 SetPageCgroupUnevictable(pc);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700398 } else {
399 if (is_active_lru(lru))
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700400 SetPageCgroupActive(pc);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700401 else
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700402 ClearPageCgroupActive(pc);
403 ClearPageCgroupUnevictable(pc);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700404 }
Christoph Lameterb69408e2008-10-18 20:26:14 -0700405
Christoph Lameterb69408e2008-10-18 20:26:14 -0700406 MEM_CGROUP_ZSTAT(mz, lru) += 1;
407 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800408}
409
David Rientjes4c4a2212008-02-07 00:14:06 -0800410int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
411{
412 int ret;
413
414 task_lock(task);
Hugh Dickinsbd845e32008-03-04 14:29:01 -0800415 ret = task->mm && mm_match_cgroup(task->mm, mem);
David Rientjes4c4a2212008-02-07 00:14:06 -0800416 task_unlock(task);
417 return ret;
418}
419
Balbir Singh66e17072008-02-07 00:13:56 -0800420/*
421 * This routine assumes that the appropriate zone's lru lock is already held
422 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700423void mem_cgroup_move_lists(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800424{
Hugh Dickins427d5412008-03-04 14:29:03 -0800425 struct page_cgroup *pc;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800426 struct mem_cgroup_per_zone *mz;
427 unsigned long flags;
428
Li Zefancede86a2008-07-25 01:47:18 -0700429 if (mem_cgroup_subsys.disabled)
430 return;
431
Hugh Dickins2680eed2008-03-04 14:29:13 -0800432 /*
433 * We cannot lock_page_cgroup while holding zone's lru_lock,
434 * because other holders of lock_page_cgroup can be interrupted
435 * with an attempt to rotate_reclaimable_page. But we cannot
436 * safely get to page_cgroup without it, so just try_lock it:
437 * mem_cgroup_isolate_pages allows for page left on wrong list.
438 */
439 if (!try_lock_page_cgroup(page))
Balbir Singh66e17072008-02-07 00:13:56 -0800440 return;
441
Hugh Dickins2680eed2008-03-04 14:29:13 -0800442 pc = page_get_page_cgroup(page);
443 if (pc) {
Hugh Dickins2680eed2008-03-04 14:29:13 -0800444 mz = page_cgroup_zoneinfo(pc);
Hugh Dickins2680eed2008-03-04 14:29:13 -0800445 spin_lock_irqsave(&mz->lru_lock, flags);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700446 __mem_cgroup_move_lists(pc, lru);
Hugh Dickins2680eed2008-03-04 14:29:13 -0800447 spin_unlock_irqrestore(&mz->lru_lock, flags);
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800448 }
449 unlock_page_cgroup(page);
Balbir Singh66e17072008-02-07 00:13:56 -0800450}
451
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800452/*
453 * Calculate mapped_ratio under memory controller. This will be used in
454 * vmscan.c for deteremining we have to reclaim mapped pages.
455 */
456int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
457{
458 long total, rss;
459
460 /*
461 * usage is recorded in bytes. But, here, we assume the number of
462 * physical pages can be represented by "long" on any arch.
463 */
464 total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
465 rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
466 return (int)((rss * 100L) / total);
467}
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800468
KAMEZAWA Hiroyuki5932f362008-02-07 00:14:33 -0800469/*
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800470 * prev_priority control...this will be used in memory reclaim path.
471 */
472int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
473{
474 return mem->prev_priority;
475}
476
477void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
478{
479 if (priority < mem->prev_priority)
480 mem->prev_priority = priority;
481}
482
483void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
484{
485 mem->prev_priority = priority;
486}
487
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800488/*
489 * Calculate # of pages to be scanned in this priority/zone.
490 * See also vmscan.c
491 *
492 * priority starts from "DEF_PRIORITY" and decremented in each loop.
493 * (see include/linux/mmzone.h)
494 */
495
Christoph Lameterb69408e2008-10-18 20:26:14 -0700496long mem_cgroup_calc_reclaim(struct mem_cgroup *mem, struct zone *zone,
497 int priority, enum lru_list lru)
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800498{
Christoph Lameterb69408e2008-10-18 20:26:14 -0700499 long nr_pages;
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800500 int nid = zone->zone_pgdat->node_id;
501 int zid = zone_idx(zone);
502 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
503
Christoph Lameterb69408e2008-10-18 20:26:14 -0700504 nr_pages = MEM_CGROUP_ZSTAT(mz, lru);
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800505
Christoph Lameterb69408e2008-10-18 20:26:14 -0700506 return (nr_pages >> priority);
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800507}
508
Balbir Singh66e17072008-02-07 00:13:56 -0800509unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
510 struct list_head *dst,
511 unsigned long *scanned, int order,
512 int mode, struct zone *z,
513 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700514 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -0800515{
516 unsigned long nr_taken = 0;
517 struct page *page;
518 unsigned long scan;
519 LIST_HEAD(pc_list);
520 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800521 struct page_cgroup *pc, *tmp;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800522 int nid = z->zone_pgdat->node_id;
523 int zid = zone_idx(z);
524 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700525 int lru = LRU_FILE * !!file + !!active;
Balbir Singh66e17072008-02-07 00:13:56 -0800526
Balbir Singhcf475ad2008-04-29 01:00:16 -0700527 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800528 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700529 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -0800530
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800531 spin_lock(&mz->lru_lock);
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800532 scan = 0;
533 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -0800534 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800535 break;
Balbir Singh66e17072008-02-07 00:13:56 -0800536 page = pc->page;
Balbir Singh66e17072008-02-07 00:13:56 -0800537
Hugh Dickins436c65412008-02-07 00:14:12 -0800538 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800539 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800540
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700541 /*
542 * TODO: play better with lumpy reclaim, grabbing anything.
543 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700544 if (PageUnevictable(page) ||
545 (PageActive(page) && !active) ||
546 (!PageActive(page) && active)) {
547 __mem_cgroup_move_lists(pc, page_lru(page));
Balbir Singh66e17072008-02-07 00:13:56 -0800548 continue;
549 }
550
Hugh Dickins436c65412008-02-07 00:14:12 -0800551 scan++;
552 list_move(&pc->lru, &pc_list);
Balbir Singh66e17072008-02-07 00:13:56 -0800553
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700554 if (__isolate_lru_page(page, mode, file) == 0) {
Balbir Singh66e17072008-02-07 00:13:56 -0800555 list_move(&page->lru, dst);
556 nr_taken++;
557 }
558 }
559
560 list_splice(&pc_list, src);
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800561 spin_unlock(&mz->lru_lock);
Balbir Singh66e17072008-02-07 00:13:56 -0800562
563 *scanned = scan;
564 return nr_taken;
565}
566
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800567/*
568 * Charge the memory controller for page usage.
569 * Return
570 * 0 if the charge was successful
571 * < 0 if the cgroup is over its limit
572 */
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800573static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700574 gfp_t gfp_mask, enum charge_type ctype,
575 struct mem_cgroup *memcg)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800576{
577 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800578 struct page_cgroup *pc;
Balbir Singh66e17072008-02-07 00:13:56 -0800579 unsigned long flags;
580 unsigned long nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800581 struct mem_cgroup_per_zone *mz;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800582
KAMEZAWA Hiroyuki508b7be2008-07-25 01:47:09 -0700583 pc = kmem_cache_alloc(page_cgroup_cache, gfp_mask);
KAMEZAWA Hiroyukib76734e2008-07-25 01:47:16 -0700584 if (unlikely(pc == NULL))
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800585 goto err;
586
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800587 /*
Hugh Dickins3be912772008-02-07 00:14:19 -0800588 * We always charge the cgroup the mm_struct belongs to.
589 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800590 * thread group leader migrates. It's possible that mm is not
591 * set, if so charge the init_mm (happens for pagecache usage).
592 */
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700593 if (likely(!memcg)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700594 rcu_read_lock();
595 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
Balbir Singh31a78f22008-09-28 23:09:31 +0100596 if (unlikely(!mem)) {
597 rcu_read_unlock();
598 kmem_cache_free(page_cgroup_cache, pc);
599 return 0;
600 }
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700601 /*
602 * For every charge from the cgroup, increment reference count
603 */
604 css_get(&mem->css);
605 rcu_read_unlock();
606 } else {
607 mem = memcg;
608 css_get(&memcg->css);
609 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800610
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700611 while (unlikely(res_counter_charge(&mem->res, PAGE_SIZE))) {
Hugh Dickins3be912772008-02-07 00:14:19 -0800612 if (!(gfp_mask & __GFP_WAIT))
613 goto out;
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800614
615 if (try_to_free_mem_cgroup_pages(mem, gfp_mask))
Balbir Singh66e17072008-02-07 00:13:56 -0800616 continue;
617
618 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800619 * try_to_free_mem_cgroup_pages() might not give us a full
620 * picture of reclaim. Some pages are reclaimed and might be
621 * moved to swap cache or just unmapped from the cgroup.
622 * Check the limit again to see if the reclaim reduced the
623 * current usage of the cgroup before giving up
624 */
Balbir Singh66e17072008-02-07 00:13:56 -0800625 if (res_counter_check_under_limit(&mem->res))
626 continue;
Hugh Dickins3be912772008-02-07 00:14:19 -0800627
628 if (!nr_retries--) {
629 mem_cgroup_out_of_memory(mem, gfp_mask);
630 goto out;
Balbir Singh66e17072008-02-07 00:13:56 -0800631 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800632 }
633
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800634 pc->mem_cgroup = mem;
635 pc->page = page;
KAMEZAWA Hiroyuki508b7be2008-07-25 01:47:09 -0700636 /*
637 * If a page is accounted as a page cache, insert to inactive list.
638 * If anon, insert to active list.
639 */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700640 pc->flags = pcg_default_flags[ctype];
Hugh Dickins3be912772008-02-07 00:14:19 -0800641
Hugh Dickins7e924aa2008-03-04 14:29:08 -0800642 lock_page_cgroup(page);
KAMEZAWA Hiroyukib76734e2008-07-25 01:47:16 -0700643 if (unlikely(page_get_page_cgroup(page))) {
Hugh Dickins7e924aa2008-03-04 14:29:08 -0800644 unlock_page_cgroup(page);
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800645 res_counter_uncharge(&mem->res, PAGE_SIZE);
646 css_put(&mem->css);
Balbir Singhb6ac57d2008-04-29 01:00:19 -0700647 kmem_cache_free(page_cgroup_cache, pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700648 goto done;
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800649 }
Hugh Dickins7e924aa2008-03-04 14:29:08 -0800650 page_assign_page_cgroup(page, pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800651
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800652 mz = page_cgroup_zoneinfo(pc);
653 spin_lock_irqsave(&mz->lru_lock, flags);
KAMEZAWA Hiroyuki3eae90c2008-04-29 01:00:22 -0700654 __mem_cgroup_add_list(mz, pc);
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800655 spin_unlock_irqrestore(&mz->lru_lock, flags);
Balbir Singh66e17072008-02-07 00:13:56 -0800656
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -0800657 unlock_page_cgroup(page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800658done:
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800659 return 0;
Hugh Dickins3be912772008-02-07 00:14:19 -0800660out:
661 css_put(&mem->css);
Balbir Singhb6ac57d2008-04-29 01:00:19 -0700662 kmem_cache_free(page_cgroup_cache, pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800663err:
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800664 return -ENOMEM;
665}
666
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800667int mem_cgroup_charge(struct page *page, struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800668{
Li Zefancede86a2008-07-25 01:47:18 -0700669 if (mem_cgroup_subsys.disabled)
670 return 0;
671
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700672 /*
673 * If already mapped, we don't have to account.
674 * If page cache, page->mapping has address_space.
675 * But page->mapping may have out-of-use anon_vma pointer,
676 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
677 * is NULL.
678 */
679 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
680 return 0;
681 if (unlikely(!mm))
682 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800683 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700684 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800685}
686
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800687int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
688 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -0800689{
Li Zefancede86a2008-07-25 01:47:18 -0700690 if (mem_cgroup_subsys.disabled)
691 return 0;
692
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700693 /*
694 * Corner case handling. This is called from add_to_page_cache()
695 * in usual. But some FS (shmem) precharges this page before calling it
696 * and call add_to_page_cache() with GFP_NOWAIT.
697 *
698 * For GFP_NOWAIT case, the page may be pre-charged before calling
699 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
700 * charge twice. (It works but has to pay a bit larger cost.)
701 */
702 if (!(gfp_mask & __GFP_WAIT)) {
703 struct page_cgroup *pc;
704
705 lock_page_cgroup(page);
706 pc = page_get_page_cgroup(page);
707 if (pc) {
708 VM_BUG_ON(pc->page != page);
709 VM_BUG_ON(!pc->mem_cgroup);
710 unlock_page_cgroup(page);
711 return 0;
712 }
713 unlock_page_cgroup(page);
714 }
715
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700716 if (unlikely(!mm))
Balbir Singh8697d332008-02-07 00:13:59 -0800717 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700718
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700719 if (page_is_file_cache(page))
720 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700721 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700722 else
723 return mem_cgroup_charge_common(page, mm, gfp_mask,
724 MEM_CGROUP_CHARGE_TYPE_SHMEM, NULL);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700725}
726
Balbir Singh8697d332008-02-07 00:13:59 -0800727/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700728 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800729 */
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700730static void
731__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800732{
Hugh Dickins82895462008-03-04 14:29:08 -0800733 struct page_cgroup *pc;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800734 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800735 struct mem_cgroup_per_zone *mz;
Balbir Singh66e17072008-02-07 00:13:56 -0800736 unsigned long flags;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800737
Balbir Singh40779602008-04-04 14:29:59 -0700738 if (mem_cgroup_subsys.disabled)
739 return;
740
Balbir Singh8697d332008-02-07 00:13:59 -0800741 /*
Balbir Singh3c541e12008-02-07 00:14:41 -0800742 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -0800743 */
Hugh Dickins82895462008-03-04 14:29:08 -0800744 lock_page_cgroup(page);
745 pc = page_get_page_cgroup(page);
KAMEZAWA Hiroyukib76734e2008-07-25 01:47:16 -0700746 if (unlikely(!pc))
Hugh Dickins82895462008-03-04 14:29:08 -0800747 goto unlock;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800748
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800749 VM_BUG_ON(pc->page != page);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800750
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700751 if ((ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700752 && ((PageCgroupCache(pc) || page_mapped(page))))
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700753 goto unlock;
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800754
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700755 mz = page_cgroup_zoneinfo(pc);
756 spin_lock_irqsave(&mz->lru_lock, flags);
757 __mem_cgroup_remove_list(mz, pc);
758 spin_unlock_irqrestore(&mz->lru_lock, flags);
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -0800759
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700760 page_assign_page_cgroup(page, NULL);
761 unlock_page_cgroup(page);
Hugh Dickins6d48ff82008-03-04 14:29:12 -0800762
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700763 mem = pc->mem_cgroup;
764 res_counter_uncharge(&mem->res, PAGE_SIZE);
765 css_put(&mem->css);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800766
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700767 kmem_cache_free(page_cgroup_cache, pc);
768 return;
Hugh Dickins82895462008-03-04 14:29:08 -0800769unlock:
Balbir Singh3c541e12008-02-07 00:14:41 -0800770 unlock_page_cgroup(page);
771}
772
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700773void mem_cgroup_uncharge_page(struct page *page)
774{
775 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
776}
777
778void mem_cgroup_uncharge_cache_page(struct page *page)
779{
780 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -0700781 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700782 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
783}
784
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800785/*
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700786 * Before starting migration, account against new page.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800787 */
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700788int mem_cgroup_prepare_migration(struct page *page, struct page *newpage)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800789{
790 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700791 struct mem_cgroup *mem = NULL;
792 enum charge_type ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
793 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800794
Balbir Singh40779602008-04-04 14:29:59 -0700795 if (mem_cgroup_subsys.disabled)
796 return 0;
797
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800798 lock_page_cgroup(page);
799 pc = page_get_page_cgroup(page);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700800 if (pc) {
801 mem = pc->mem_cgroup;
802 css_get(&mem->css);
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700803 if (PageCgroupCache(pc)) {
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700804 if (page_is_file_cache(page))
805 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
806 else
807 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
808 }
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800809 }
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -0800810 unlock_page_cgroup(page);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700811 if (mem) {
812 ret = mem_cgroup_charge_common(newpage, NULL, GFP_KERNEL,
813 ctype, mem);
814 css_put(&mem->css);
815 }
816 return ret;
817}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -0800818
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700819/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700820void mem_cgroup_end_migration(struct page *newpage)
821{
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700822 /*
823 * At success, page->mapping is not NULL.
824 * special rollback care is necessary when
825 * 1. at migration failure. (newpage->mapping is cleared in this case)
826 * 2. the newpage was moved but not remapped again because the task
827 * exits and the newpage is obsolete. In this case, the new page
828 * may be a swapcache. So, we just call mem_cgroup_uncharge_page()
829 * always for avoiding mess. The page_cgroup will be removed if
830 * unnecessary. File cache pages is still on radix-tree. Don't
831 * care it.
832 */
833 if (!newpage->mapping)
834 __mem_cgroup_uncharge_common(newpage,
835 MEM_CGROUP_CHARGE_TYPE_FORCE);
836 else if (PageAnon(newpage))
837 mem_cgroup_uncharge_page(newpage);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800838}
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800839
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800840/*
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -0700841 * A call to try to shrink memory usage under specified resource controller.
842 * This is typically used for page reclaiming for shmem for reducing side
843 * effect of page allocation from shmem, which is used by some mem_cgroup.
844 */
845int mem_cgroup_shrink_usage(struct mm_struct *mm, gfp_t gfp_mask)
846{
847 struct mem_cgroup *mem;
848 int progress = 0;
849 int retry = MEM_CGROUP_RECLAIM_RETRIES;
850
Li Zefancede86a2008-07-25 01:47:18 -0700851 if (mem_cgroup_subsys.disabled)
852 return 0;
Hugh Dickins9623e072008-08-12 15:08:41 -0700853 if (!mm)
854 return 0;
Li Zefancede86a2008-07-25 01:47:18 -0700855
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -0700856 rcu_read_lock();
857 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
Balbir Singh31a78f22008-09-28 23:09:31 +0100858 if (unlikely(!mem)) {
859 rcu_read_unlock();
860 return 0;
861 }
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -0700862 css_get(&mem->css);
863 rcu_read_unlock();
864
865 do {
866 progress = try_to_free_mem_cgroup_pages(mem, gfp_mask);
Daisuke Nishimuraa10cebf2008-09-22 13:57:52 -0700867 progress += res_counter_check_under_limit(&mem->res);
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -0700868 } while (!progress && --retry);
869
870 css_put(&mem->css);
871 if (!retry)
872 return -ENOMEM;
873 return 0;
874}
875
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -0700876int mem_cgroup_resize_limit(struct mem_cgroup *memcg, unsigned long long val)
877{
878
879 int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
880 int progress;
881 int ret = 0;
882
883 while (res_counter_set_limit(&memcg->res, val)) {
884 if (signal_pending(current)) {
885 ret = -EINTR;
886 break;
887 }
888 if (!retry_count) {
889 ret = -EBUSY;
890 break;
891 }
892 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL);
893 if (!progress)
894 retry_count--;
895 }
896 return ret;
897}
898
899
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -0700900/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800901 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800902 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
903 */
904#define FORCE_UNCHARGE_BATCH (128)
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800905static void mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800906 struct mem_cgroup_per_zone *mz,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700907 enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800908{
909 struct page_cgroup *pc;
910 struct page *page;
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800911 int count = FORCE_UNCHARGE_BATCH;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800912 unsigned long flags;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800913 struct list_head *list;
914
Christoph Lameterb69408e2008-10-18 20:26:14 -0700915 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800916
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800917 spin_lock_irqsave(&mz->lru_lock, flags);
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800918 while (!list_empty(list)) {
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800919 pc = list_entry(list->prev, struct page_cgroup, lru);
920 page = pc->page;
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800921 get_page(page);
922 spin_unlock_irqrestore(&mz->lru_lock, flags);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700923 /*
924 * Check if this page is on LRU. !LRU page can be found
925 * if it's under page migration.
926 */
927 if (PageLRU(page)) {
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700928 __mem_cgroup_uncharge_common(page,
929 MEM_CGROUP_CHARGE_TYPE_FORCE);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700930 put_page(page);
931 if (--count <= 0) {
932 count = FORCE_UNCHARGE_BATCH;
933 cond_resched();
934 }
935 } else
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800936 cond_resched();
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800937 spin_lock_irqsave(&mz->lru_lock, flags);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800938 }
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800939 spin_unlock_irqrestore(&mz->lru_lock, flags);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800940}
941
942/*
943 * make mem_cgroup's charge to be 0 if there is no task.
944 * This enables deleting this mem_cgroup.
945 */
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800946static int mem_cgroup_force_empty(struct mem_cgroup *mem)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800947{
948 int ret = -EBUSY;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800949 int node, zid;
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800950
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800951 css_get(&mem->css);
952 /*
953 * page reclaim code (kswapd etc..) will move pages between
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800954 * active_list <-> inactive_list while we don't take a lock.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800955 * So, we have to do loop here until all lists are empty.
956 */
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800957 while (mem->res.usage > 0) {
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800958 if (atomic_read(&mem->css.cgroup->count) > 0)
959 goto out;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800960 for_each_node_state(node, N_POSSIBLE)
961 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
962 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700963 enum lru_list l;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800964 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700965 for_each_lru(l)
966 mem_cgroup_force_empty_list(mem, mz, l);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800967 }
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800968 }
969 ret = 0;
970out:
971 css_put(&mem->css);
972 return ret;
973}
974
Paul Menage2c3daa72008-04-29 00:59:58 -0700975static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800976{
Paul Menage2c3daa72008-04-29 00:59:58 -0700977 return res_counter_read_u64(&mem_cgroup_from_cont(cont)->res,
978 cft->private);
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800979}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -0700980/*
981 * The user of this function is...
982 * RES_LIMIT.
983 */
Paul Menage856c13a2008-07-25 01:47:04 -0700984static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
985 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800986{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -0700987 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
988 unsigned long long val;
989 int ret;
990
991 switch (cft->private) {
992 case RES_LIMIT:
993 /* This function does all necessary parse...reuse it */
994 ret = res_counter_memparse_write_strategy(buffer, &val);
995 if (!ret)
996 ret = mem_cgroup_resize_limit(memcg, val);
997 break;
998 default:
999 ret = -EINVAL; /* should be BUG() ? */
1000 break;
1001 }
1002 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001003}
1004
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001005static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001006{
1007 struct mem_cgroup *mem;
1008
1009 mem = mem_cgroup_from_cont(cont);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001010 switch (event) {
1011 case RES_MAX_USAGE:
1012 res_counter_reset_max(&mem->res);
1013 break;
1014 case RES_FAILCNT:
1015 res_counter_reset_failcnt(&mem->res);
1016 break;
1017 }
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07001018 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001019}
1020
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07001021static int mem_force_empty_write(struct cgroup *cont, unsigned int event)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001022{
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07001023 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont));
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001024}
1025
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001026static const struct mem_cgroup_stat_desc {
1027 const char *msg;
1028 u64 unit;
1029} mem_cgroup_stat_desc[] = {
1030 [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
1031 [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
Balaji Rao55e462b2008-05-01 04:35:12 -07001032 [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, },
1033 [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, },
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001034};
1035
Paul Menagec64745c2008-04-29 01:00:02 -07001036static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
1037 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001038{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001039 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
1040 struct mem_cgroup_stat *stat = &mem_cont->stat;
1041 int i;
1042
1043 for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
1044 s64 val;
1045
1046 val = mem_cgroup_read_stat(stat, i);
1047 val *= mem_cgroup_stat_desc[i].unit;
Paul Menagec64745c2008-04-29 01:00:02 -07001048 cb->fill(cb, mem_cgroup_stat_desc[i].msg, val);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001049 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001050 /* showing # of active pages */
1051 {
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001052 unsigned long active_anon, inactive_anon;
1053 unsigned long active_file, inactive_file;
Lee Schermerhorn7b854122008-10-18 20:26:40 -07001054 unsigned long unevictable;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001055
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001056 inactive_anon = mem_cgroup_get_all_zonestat(mem_cont,
1057 LRU_INACTIVE_ANON);
1058 active_anon = mem_cgroup_get_all_zonestat(mem_cont,
1059 LRU_ACTIVE_ANON);
1060 inactive_file = mem_cgroup_get_all_zonestat(mem_cont,
1061 LRU_INACTIVE_FILE);
1062 active_file = mem_cgroup_get_all_zonestat(mem_cont,
1063 LRU_ACTIVE_FILE);
Lee Schermerhorn7b854122008-10-18 20:26:40 -07001064 unevictable = mem_cgroup_get_all_zonestat(mem_cont,
1065 LRU_UNEVICTABLE);
1066
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001067 cb->fill(cb, "active_anon", (active_anon) * PAGE_SIZE);
1068 cb->fill(cb, "inactive_anon", (inactive_anon) * PAGE_SIZE);
1069 cb->fill(cb, "active_file", (active_file) * PAGE_SIZE);
1070 cb->fill(cb, "inactive_file", (inactive_file) * PAGE_SIZE);
Lee Schermerhorn7b854122008-10-18 20:26:40 -07001071 cb->fill(cb, "unevictable", unevictable * PAGE_SIZE);
1072
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001073 }
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001074 return 0;
1075}
1076
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001077static struct cftype mem_cgroup_files[] = {
1078 {
Balbir Singh0eea1032008-02-07 00:13:57 -08001079 .name = "usage_in_bytes",
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001080 .private = RES_USAGE,
Paul Menage2c3daa72008-04-29 00:59:58 -07001081 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001082 },
1083 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001084 .name = "max_usage_in_bytes",
1085 .private = RES_MAX_USAGE,
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001086 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001087 .read_u64 = mem_cgroup_read,
1088 },
1089 {
Balbir Singh0eea1032008-02-07 00:13:57 -08001090 .name = "limit_in_bytes",
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001091 .private = RES_LIMIT,
Paul Menage856c13a2008-07-25 01:47:04 -07001092 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07001093 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001094 },
1095 {
1096 .name = "failcnt",
1097 .private = RES_FAILCNT,
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001098 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07001099 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001100 },
Balbir Singh8697d332008-02-07 00:13:59 -08001101 {
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001102 .name = "force_empty",
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07001103 .trigger = mem_force_empty_write,
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001104 },
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001105 {
1106 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07001107 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001108 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001109};
1110
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001111static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1112{
1113 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001114 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07001115 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07001116 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001117 /*
1118 * This routine is called against possible nodes.
1119 * But it's BUG to call kmalloc() against offline node.
1120 *
1121 * TODO: this routine can waste much memory for nodes which will
1122 * never be onlined. It's better to use memory hotplug callback
1123 * function.
1124 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07001125 if (!node_state(node, N_NORMAL_MEMORY))
1126 tmp = -1;
1127 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001128 if (!pn)
1129 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001130
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001131 mem->info.nodeinfo[node] = pn;
1132 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001133
1134 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
1135 mz = &pn->zoneinfo[zone];
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001136 spin_lock_init(&mz->lru_lock);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001137 for_each_lru(l)
1138 INIT_LIST_HEAD(&mz->lists[l]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001139 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001140 return 0;
1141}
1142
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001143static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1144{
1145 kfree(mem->info.nodeinfo[node]);
1146}
1147
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001148static struct mem_cgroup *mem_cgroup_alloc(void)
1149{
1150 struct mem_cgroup *mem;
1151
1152 if (sizeof(*mem) < PAGE_SIZE)
1153 mem = kmalloc(sizeof(*mem), GFP_KERNEL);
1154 else
1155 mem = vmalloc(sizeof(*mem));
1156
1157 if (mem)
1158 memset(mem, 0, sizeof(*mem));
1159 return mem;
1160}
1161
1162static void mem_cgroup_free(struct mem_cgroup *mem)
1163{
1164 if (sizeof(*mem) < PAGE_SIZE)
1165 kfree(mem);
1166 else
1167 vfree(mem);
1168}
1169
1170
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001171static struct cgroup_subsys_state *
1172mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
1173{
1174 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001175 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001176
Balbir Singhb6ac57d2008-04-29 01:00:19 -07001177 if (unlikely((cont->parent) == NULL)) {
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001178 mem = &init_mem_cgroup;
Balbir Singhb6ac57d2008-04-29 01:00:19 -07001179 page_cgroup_cache = KMEM_CACHE(page_cgroup, SLAB_PANIC);
1180 } else {
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001181 mem = mem_cgroup_alloc();
1182 if (!mem)
1183 return ERR_PTR(-ENOMEM);
Balbir Singhb6ac57d2008-04-29 01:00:19 -07001184 }
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001185
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001186 res_counter_init(&mem->res);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001187
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001188 for_each_node_state(node, N_POSSIBLE)
1189 if (alloc_mem_cgroup_per_zone_info(mem, node))
1190 goto free_out;
1191
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001192 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001193free_out:
1194 for_each_node_state(node, N_POSSIBLE)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001195 free_mem_cgroup_per_zone_info(mem, node);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001196 if (cont->parent != NULL)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001197 mem_cgroup_free(mem);
Li Zefan2dda81c2008-02-23 15:24:14 -08001198 return ERR_PTR(-ENOMEM);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001199}
1200
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08001201static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
1202 struct cgroup *cont)
1203{
1204 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1205 mem_cgroup_force_empty(mem);
1206}
1207
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001208static void mem_cgroup_destroy(struct cgroup_subsys *ss,
1209 struct cgroup *cont)
1210{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001211 int node;
1212 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1213
1214 for_each_node_state(node, N_POSSIBLE)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001215 free_mem_cgroup_per_zone_info(mem, node);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001216
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001217 mem_cgroup_free(mem_cgroup_from_cont(cont));
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001218}
1219
1220static int mem_cgroup_populate(struct cgroup_subsys *ss,
1221 struct cgroup *cont)
1222{
1223 return cgroup_add_files(cont, ss, mem_cgroup_files,
1224 ARRAY_SIZE(mem_cgroup_files));
1225}
1226
Balbir Singh67e465a2008-02-07 00:13:54 -08001227static void mem_cgroup_move_task(struct cgroup_subsys *ss,
1228 struct cgroup *cont,
1229 struct cgroup *old_cont,
1230 struct task_struct *p)
1231{
1232 struct mm_struct *mm;
1233 struct mem_cgroup *mem, *old_mem;
1234
1235 mm = get_task_mm(p);
1236 if (mm == NULL)
1237 return;
1238
1239 mem = mem_cgroup_from_cont(cont);
1240 old_mem = mem_cgroup_from_cont(old_cont);
1241
Balbir Singh67e465a2008-02-07 00:13:54 -08001242 /*
1243 * Only thread group leaders are allowed to migrate, the mm_struct is
1244 * in effect owned by the leader
1245 */
Pavel Emelyanov52ea27e2008-03-19 17:00:45 -07001246 if (!thread_group_leader(p))
Balbir Singh67e465a2008-02-07 00:13:54 -08001247 goto out;
1248
Balbir Singh67e465a2008-02-07 00:13:54 -08001249out:
1250 mmput(mm);
Balbir Singh67e465a2008-02-07 00:13:54 -08001251}
1252
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001253struct cgroup_subsys mem_cgroup_subsys = {
1254 .name = "memory",
1255 .subsys_id = mem_cgroup_subsys_id,
1256 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08001257 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001258 .destroy = mem_cgroup_destroy,
1259 .populate = mem_cgroup_populate,
Balbir Singh67e465a2008-02-07 00:13:54 -08001260 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001261 .early_init = 0,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001262};