blob: f568b19645517b77fb054b20fdbfc952ad7bd628 [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>
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070036#include <linux/page_cgroup.h>
Balbir Singh8cdea7c2008-02-07 00:13:50 -080037
Balbir Singh8697d332008-02-07 00:13:59 -080038#include <asm/uaccess.h>
39
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070040struct cgroup_subsys mem_cgroup_subsys __read_mostly;
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070041#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
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800138enum charge_type {
139 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
140 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700141 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700142 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
143 NR_CHARGE_TYPE,
144};
145
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700146/* only for here (for easy reading.) */
147#define PCGF_CACHE (1UL << PCG_CACHE)
148#define PCGF_USED (1UL << PCG_USED)
149#define PCGF_ACTIVE (1UL << PCG_ACTIVE)
150#define PCGF_LOCK (1UL << PCG_LOCK)
151#define PCGF_FILE (1UL << PCG_FILE)
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700152static const unsigned long
153pcg_default_flags[NR_CHARGE_TYPE] = {
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700154 PCGF_CACHE | PCGF_FILE | PCGF_USED | PCGF_LOCK, /* File Cache */
155 PCGF_ACTIVE | PCGF_USED | PCGF_LOCK, /* Anon */
156 PCGF_ACTIVE | PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* Shmem */
157 0, /* FORCE */
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800158};
159
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800160/*
161 * Always modified under lru lock. Then, not necessary to preempt_disable()
162 */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700163static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
164 struct page_cgroup *pc,
165 bool charge)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800166{
167 int val = (charge)? 1 : -1;
168 struct mem_cgroup_stat *stat = &mem->stat;
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700169 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800170
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800171 VM_BUG_ON(!irqs_disabled());
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700172
173 cpustat = &stat->cpustat[smp_processor_id()];
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700174 if (PageCgroupCache(pc))
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700175 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800176 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700177 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700178
179 if (charge)
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700180 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700181 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
182 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700183 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700184 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800185}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800186
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800187static struct mem_cgroup_per_zone *
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800188mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
189{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800190 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
191}
192
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800193static struct mem_cgroup_per_zone *
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800194page_cgroup_zoneinfo(struct page_cgroup *pc)
195{
196 struct mem_cgroup *mem = pc->mem_cgroup;
197 int nid = page_cgroup_nid(pc);
198 int zid = page_cgroup_zid(pc);
199
200 return mem_cgroup_zoneinfo(mem, nid, zid);
201}
202
203static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700204 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800205{
206 int nid, zid;
207 struct mem_cgroup_per_zone *mz;
208 u64 total = 0;
209
210 for_each_online_node(nid)
211 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
212 mz = mem_cgroup_zoneinfo(mem, nid, zid);
213 total += MEM_CGROUP_ZSTAT(mz, idx);
214 }
215 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800216}
217
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800218static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800219{
220 return container_of(cgroup_subsys_state(cont,
221 mem_cgroup_subsys_id), struct mem_cgroup,
222 css);
223}
224
Balbir Singhcf475ad2008-04-29 01:00:16 -0700225struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800226{
Balbir Singh31a78f22008-09-28 23:09:31 +0100227 /*
228 * mm_update_next_owner() may clear mm->owner to NULL
229 * if it races with swapoff, page migration, etc.
230 * So this can be called with p == NULL.
231 */
232 if (unlikely(!p))
233 return NULL;
234
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800235 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
236 struct mem_cgroup, css);
237}
238
KAMEZAWA Hiroyuki3eae90c2008-04-29 01:00:22 -0700239static void __mem_cgroup_remove_list(struct mem_cgroup_per_zone *mz,
240 struct page_cgroup *pc)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800241{
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700242 int lru = LRU_BASE;
243
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700244 if (PageCgroupUnevictable(pc))
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700245 lru = LRU_UNEVICTABLE;
246 else {
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700247 if (PageCgroupActive(pc))
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700248 lru += LRU_ACTIVE;
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700249 if (PageCgroupFile(pc))
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700250 lru += LRU_FILE;
251 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800252
Christoph Lameterb69408e2008-10-18 20:26:14 -0700253 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800254
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700255 mem_cgroup_charge_statistics(pc->mem_cgroup, pc, false);
KAMEZAWA Hiroyuki508b7be2008-07-25 01:47:09 -0700256 list_del(&pc->lru);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800257}
258
KAMEZAWA Hiroyuki3eae90c2008-04-29 01:00:22 -0700259static void __mem_cgroup_add_list(struct mem_cgroup_per_zone *mz,
260 struct page_cgroup *pc)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800261{
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700262 int lru = LRU_BASE;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800263
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700264 if (PageCgroupUnevictable(pc))
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700265 lru = LRU_UNEVICTABLE;
266 else {
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700267 if (PageCgroupActive(pc))
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700268 lru += LRU_ACTIVE;
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700269 if (PageCgroupFile(pc))
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700270 lru += LRU_FILE;
271 }
Christoph Lameterb69408e2008-10-18 20:26:14 -0700272
273 MEM_CGROUP_ZSTAT(mz, lru) += 1;
274 list_add(&pc->lru, &mz->lists[lru]);
275
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700276 mem_cgroup_charge_statistics(pc->mem_cgroup, pc, true);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800277}
278
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700279static void __mem_cgroup_move_lists(struct page_cgroup *pc, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800280{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800281 struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700282 int active = PageCgroupActive(pc);
283 int file = PageCgroupFile(pc);
284 int unevictable = PageCgroupUnevictable(pc);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700285 enum lru_list from = unevictable ? LRU_UNEVICTABLE :
286 (LRU_FILE * !!file + !!active);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800287
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700288 if (lru == from)
289 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700290
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700291 MEM_CGROUP_ZSTAT(mz, from) -= 1;
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700292 /*
293 * However this is done under mz->lru_lock, another flags, which
294 * are not related to LRU, will be modified from out-of-lock.
295 * We have to use atomic set/clear flags.
296 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700297 if (is_unevictable_lru(lru)) {
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700298 ClearPageCgroupActive(pc);
299 SetPageCgroupUnevictable(pc);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700300 } else {
301 if (is_active_lru(lru))
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700302 SetPageCgroupActive(pc);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700303 else
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700304 ClearPageCgroupActive(pc);
305 ClearPageCgroupUnevictable(pc);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700306 }
Christoph Lameterb69408e2008-10-18 20:26:14 -0700307
Christoph Lameterb69408e2008-10-18 20:26:14 -0700308 MEM_CGROUP_ZSTAT(mz, lru) += 1;
309 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800310}
311
David Rientjes4c4a2212008-02-07 00:14:06 -0800312int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
313{
314 int ret;
315
316 task_lock(task);
Hugh Dickinsbd845e32008-03-04 14:29:01 -0800317 ret = task->mm && mm_match_cgroup(task->mm, mem);
David Rientjes4c4a2212008-02-07 00:14:06 -0800318 task_unlock(task);
319 return ret;
320}
321
Balbir Singh66e17072008-02-07 00:13:56 -0800322/*
323 * This routine assumes that the appropriate zone's lru lock is already held
324 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700325void mem_cgroup_move_lists(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800326{
Hugh Dickins427d5412008-03-04 14:29:03 -0800327 struct page_cgroup *pc;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800328 struct mem_cgroup_per_zone *mz;
329 unsigned long flags;
330
Li Zefancede86a2008-07-25 01:47:18 -0700331 if (mem_cgroup_subsys.disabled)
332 return;
333
Hugh Dickins2680eed2008-03-04 14:29:13 -0800334 /*
335 * We cannot lock_page_cgroup while holding zone's lru_lock,
336 * because other holders of lock_page_cgroup can be interrupted
337 * with an attempt to rotate_reclaimable_page. But we cannot
338 * safely get to page_cgroup without it, so just try_lock it:
339 * mem_cgroup_isolate_pages allows for page left on wrong list.
340 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700341 pc = lookup_page_cgroup(page);
342 if (!trylock_page_cgroup(pc))
Balbir Singh66e17072008-02-07 00:13:56 -0800343 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700344 if (pc && PageCgroupUsed(pc)) {
Hugh Dickins2680eed2008-03-04 14:29:13 -0800345 mz = page_cgroup_zoneinfo(pc);
Hugh Dickins2680eed2008-03-04 14:29:13 -0800346 spin_lock_irqsave(&mz->lru_lock, flags);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700347 __mem_cgroup_move_lists(pc, lru);
Hugh Dickins2680eed2008-03-04 14:29:13 -0800348 spin_unlock_irqrestore(&mz->lru_lock, flags);
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800349 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700350 unlock_page_cgroup(pc);
Balbir Singh66e17072008-02-07 00:13:56 -0800351}
352
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800353/*
354 * Calculate mapped_ratio under memory controller. This will be used in
355 * vmscan.c for deteremining we have to reclaim mapped pages.
356 */
357int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
358{
359 long total, rss;
360
361 /*
362 * usage is recorded in bytes. But, here, we assume the number of
363 * physical pages can be represented by "long" on any arch.
364 */
365 total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
366 rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
367 return (int)((rss * 100L) / total);
368}
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800369
KAMEZAWA Hiroyuki5932f362008-02-07 00:14:33 -0800370/*
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800371 * prev_priority control...this will be used in memory reclaim path.
372 */
373int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
374{
375 return mem->prev_priority;
376}
377
378void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
379{
380 if (priority < mem->prev_priority)
381 mem->prev_priority = priority;
382}
383
384void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
385{
386 mem->prev_priority = priority;
387}
388
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800389/*
390 * Calculate # of pages to be scanned in this priority/zone.
391 * See also vmscan.c
392 *
393 * priority starts from "DEF_PRIORITY" and decremented in each loop.
394 * (see include/linux/mmzone.h)
395 */
396
Christoph Lameterb69408e2008-10-18 20:26:14 -0700397long mem_cgroup_calc_reclaim(struct mem_cgroup *mem, struct zone *zone,
398 int priority, enum lru_list lru)
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800399{
Christoph Lameterb69408e2008-10-18 20:26:14 -0700400 long nr_pages;
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800401 int nid = zone->zone_pgdat->node_id;
402 int zid = zone_idx(zone);
403 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
404
Christoph Lameterb69408e2008-10-18 20:26:14 -0700405 nr_pages = MEM_CGROUP_ZSTAT(mz, lru);
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800406
Christoph Lameterb69408e2008-10-18 20:26:14 -0700407 return (nr_pages >> priority);
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800408}
409
Balbir Singh66e17072008-02-07 00:13:56 -0800410unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
411 struct list_head *dst,
412 unsigned long *scanned, int order,
413 int mode, struct zone *z,
414 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700415 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -0800416{
417 unsigned long nr_taken = 0;
418 struct page *page;
419 unsigned long scan;
420 LIST_HEAD(pc_list);
421 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800422 struct page_cgroup *pc, *tmp;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800423 int nid = z->zone_pgdat->node_id;
424 int zid = zone_idx(z);
425 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700426 int lru = LRU_FILE * !!file + !!active;
Balbir Singh66e17072008-02-07 00:13:56 -0800427
Balbir Singhcf475ad2008-04-29 01:00:16 -0700428 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800429 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700430 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -0800431
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800432 spin_lock(&mz->lru_lock);
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800433 scan = 0;
434 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -0800435 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800436 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700437 if (unlikely(!PageCgroupUsed(pc)))
438 continue;
Balbir Singh66e17072008-02-07 00:13:56 -0800439 page = pc->page;
Balbir Singh66e17072008-02-07 00:13:56 -0800440
Hugh Dickins436c65412008-02-07 00:14:12 -0800441 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800442 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800443
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700444 /*
445 * TODO: play better with lumpy reclaim, grabbing anything.
446 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700447 if (PageUnevictable(page) ||
448 (PageActive(page) && !active) ||
449 (!PageActive(page) && active)) {
450 __mem_cgroup_move_lists(pc, page_lru(page));
Balbir Singh66e17072008-02-07 00:13:56 -0800451 continue;
452 }
453
Hugh Dickins436c65412008-02-07 00:14:12 -0800454 scan++;
455 list_move(&pc->lru, &pc_list);
Balbir Singh66e17072008-02-07 00:13:56 -0800456
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700457 if (__isolate_lru_page(page, mode, file) == 0) {
Balbir Singh66e17072008-02-07 00:13:56 -0800458 list_move(&page->lru, dst);
459 nr_taken++;
460 }
461 }
462
463 list_splice(&pc_list, src);
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800464 spin_unlock(&mz->lru_lock);
Balbir Singh66e17072008-02-07 00:13:56 -0800465
466 *scanned = scan;
467 return nr_taken;
468}
469
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800470
471/**
472 * mem_cgroup_try_charge - get charge of PAGE_SIZE.
473 * @mm: an mm_struct which is charged against. (when *memcg is NULL)
474 * @gfp_mask: gfp_mask for reclaim.
475 * @memcg: a pointer to memory cgroup which is charged against.
476 *
477 * charge against memory cgroup pointed by *memcg. if *memcg == NULL, estimated
478 * memory cgroup from @mm is got and stored in *memcg.
479 *
480 * Returns 0 if success. -ENOMEM at failure.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800481 */
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800482
483int mem_cgroup_try_charge(struct mm_struct *mm,
484 gfp_t gfp_mask, struct mem_cgroup **memcg)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800485{
486 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800487 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800488 /*
Hugh Dickins3be91272008-02-07 00:14:19 -0800489 * We always charge the cgroup the mm_struct belongs to.
490 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800491 * thread group leader migrates. It's possible that mm is not
492 * set, if so charge the init_mm (happens for pagecache usage).
493 */
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800494 if (likely(!*memcg)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700495 rcu_read_lock();
496 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
Balbir Singh31a78f22008-09-28 23:09:31 +0100497 if (unlikely(!mem)) {
498 rcu_read_unlock();
Balbir Singh31a78f22008-09-28 23:09:31 +0100499 return 0;
500 }
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700501 /*
502 * For every charge from the cgroup, increment reference count
503 */
504 css_get(&mem->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800505 *memcg = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700506 rcu_read_unlock();
507 } else {
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800508 mem = *memcg;
509 css_get(&mem->css);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700510 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800511
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800512
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700513 while (unlikely(res_counter_charge(&mem->res, PAGE_SIZE))) {
Hugh Dickins3be91272008-02-07 00:14:19 -0800514 if (!(gfp_mask & __GFP_WAIT))
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800515 goto nomem;
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800516
517 if (try_to_free_mem_cgroup_pages(mem, gfp_mask))
Balbir Singh66e17072008-02-07 00:13:56 -0800518 continue;
519
520 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800521 * try_to_free_mem_cgroup_pages() might not give us a full
522 * picture of reclaim. Some pages are reclaimed and might be
523 * moved to swap cache or just unmapped from the cgroup.
524 * Check the limit again to see if the reclaim reduced the
525 * current usage of the cgroup before giving up
526 */
Balbir Singh66e17072008-02-07 00:13:56 -0800527 if (res_counter_check_under_limit(&mem->res))
528 continue;
Hugh Dickins3be91272008-02-07 00:14:19 -0800529
530 if (!nr_retries--) {
531 mem_cgroup_out_of_memory(mem, gfp_mask);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800532 goto nomem;
Balbir Singh66e17072008-02-07 00:13:56 -0800533 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800534 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800535 return 0;
536nomem:
537 css_put(&mem->css);
538 return -ENOMEM;
539}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800540
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800541/*
542 * commit a charge got by mem_cgroup_try_charge() and makes page_cgroup to be
543 * USED state. If already USED, uncharge and return.
544 */
545
546static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
547 struct page_cgroup *pc,
548 enum charge_type ctype)
549{
550 struct mem_cgroup_per_zone *mz;
551 unsigned long flags;
552
553 /* try_charge() can return NULL to *memcg, taking care of it. */
554 if (!mem)
555 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700556
557 lock_page_cgroup(pc);
558 if (unlikely(PageCgroupUsed(pc))) {
559 unlock_page_cgroup(pc);
560 res_counter_uncharge(&mem->res, PAGE_SIZE);
561 css_put(&mem->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800562 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700563 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800564 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki508b7be2008-07-25 01:47:09 -0700565 /*
566 * If a page is accounted as a page cache, insert to inactive list.
567 * If anon, insert to active list.
568 */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700569 pc->flags = pcg_default_flags[ctype];
Hugh Dickins3be91272008-02-07 00:14:19 -0800570
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800571 mz = page_cgroup_zoneinfo(pc);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700572
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800573 spin_lock_irqsave(&mz->lru_lock, flags);
KAMEZAWA Hiroyuki3eae90c2008-04-29 01:00:22 -0700574 __mem_cgroup_add_list(mz, pc);
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800575 spin_unlock_irqrestore(&mz->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700576 unlock_page_cgroup(pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800577}
578
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800579/*
580 * Charge the memory controller for page usage.
581 * Return
582 * 0 if the charge was successful
583 * < 0 if the cgroup is over its limit
584 */
585static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
586 gfp_t gfp_mask, enum charge_type ctype,
587 struct mem_cgroup *memcg)
588{
589 struct mem_cgroup *mem;
590 struct page_cgroup *pc;
591 int ret;
592
593 pc = lookup_page_cgroup(page);
594 /* can happen at boot */
595 if (unlikely(!pc))
596 return 0;
597 prefetchw(pc);
598
599 mem = memcg;
600 ret = mem_cgroup_try_charge(mm, gfp_mask, &mem);
601 if (ret)
602 return ret;
603
604 __mem_cgroup_commit_charge(mem, pc, ctype);
605 return 0;
606}
607
608int mem_cgroup_newpage_charge(struct page *page,
609 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800610{
Li Zefancede86a2008-07-25 01:47:18 -0700611 if (mem_cgroup_subsys.disabled)
612 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700613 if (PageCompound(page))
614 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700615 /*
616 * If already mapped, we don't have to account.
617 * If page cache, page->mapping has address_space.
618 * But page->mapping may have out-of-use anon_vma pointer,
619 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
620 * is NULL.
621 */
622 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
623 return 0;
624 if (unlikely(!mm))
625 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800626 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700627 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800628}
629
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800630/*
631 * same as mem_cgroup_newpage_charge(), now.
632 * But what we assume is different from newpage, and this is special case.
633 * treat this in special function. easy for maintenance.
634 */
635
636int mem_cgroup_charge_migrate_fixup(struct page *page,
637 struct mm_struct *mm, gfp_t gfp_mask)
638{
639 if (mem_cgroup_subsys.disabled)
640 return 0;
641
642 if (PageCompound(page))
643 return 0;
644
645 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
646 return 0;
647
648 if (unlikely(!mm))
649 mm = &init_mm;
650
651 return mem_cgroup_charge_common(page, mm, gfp_mask,
652 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
653}
654
655
656
657
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800658int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
659 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -0800660{
Li Zefancede86a2008-07-25 01:47:18 -0700661 if (mem_cgroup_subsys.disabled)
662 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700663 if (PageCompound(page))
664 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700665 /*
666 * Corner case handling. This is called from add_to_page_cache()
667 * in usual. But some FS (shmem) precharges this page before calling it
668 * and call add_to_page_cache() with GFP_NOWAIT.
669 *
670 * For GFP_NOWAIT case, the page may be pre-charged before calling
671 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
672 * charge twice. (It works but has to pay a bit larger cost.)
673 */
674 if (!(gfp_mask & __GFP_WAIT)) {
675 struct page_cgroup *pc;
676
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700677
678 pc = lookup_page_cgroup(page);
679 if (!pc)
680 return 0;
681 lock_page_cgroup(pc);
682 if (PageCgroupUsed(pc)) {
683 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700684 return 0;
685 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700686 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700687 }
688
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700689 if (unlikely(!mm))
Balbir Singh8697d332008-02-07 00:13:59 -0800690 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700691
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700692 if (page_is_file_cache(page))
693 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700694 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700695 else
696 return mem_cgroup_charge_common(page, mm, gfp_mask,
697 MEM_CGROUP_CHARGE_TYPE_SHMEM, NULL);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700698}
699
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800700
701void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
702{
703 struct page_cgroup *pc;
704
705 if (mem_cgroup_subsys.disabled)
706 return;
707 if (!ptr)
708 return;
709 pc = lookup_page_cgroup(page);
710 __mem_cgroup_commit_charge(ptr, pc, MEM_CGROUP_CHARGE_TYPE_MAPPED);
711}
712
713void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
714{
715 if (mem_cgroup_subsys.disabled)
716 return;
717 if (!mem)
718 return;
719 res_counter_uncharge(&mem->res, PAGE_SIZE);
720 css_put(&mem->css);
721}
722
723
Balbir Singh8697d332008-02-07 00:13:59 -0800724/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700725 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800726 */
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700727static void
728__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800729{
Hugh Dickins82895462008-03-04 14:29:08 -0800730 struct page_cgroup *pc;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800731 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800732 struct mem_cgroup_per_zone *mz;
Balbir Singh66e17072008-02-07 00:13:56 -0800733 unsigned long flags;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800734
Balbir Singh40779602008-04-04 14:29:59 -0700735 if (mem_cgroup_subsys.disabled)
736 return;
737
Balbir Singh8697d332008-02-07 00:13:59 -0800738 /*
Balbir Singh3c541e12008-02-07 00:14:41 -0800739 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -0800740 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700741 pc = lookup_page_cgroup(page);
742 if (unlikely(!pc || !PageCgroupUsed(pc)))
743 return;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800744
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700745 lock_page_cgroup(pc);
746 if ((ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED && page_mapped(page))
747 || !PageCgroupUsed(pc)) {
748 /* This happens at race in zap_pte_range() and do_swap_page()*/
749 unlock_page_cgroup(pc);
750 return;
751 }
752 ClearPageCgroupUsed(pc);
753 mem = pc->mem_cgroup;
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);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700759 unlock_page_cgroup(pc);
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -0800760
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700761 res_counter_uncharge(&mem->res, PAGE_SIZE);
762 css_put(&mem->css);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800763
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700764 return;
Balbir Singh3c541e12008-02-07 00:14:41 -0800765}
766
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700767void mem_cgroup_uncharge_page(struct page *page)
768{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700769 /* early check. */
770 if (page_mapped(page))
771 return;
772 if (page->mapping && !PageAnon(page))
773 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700774 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
775}
776
777void mem_cgroup_uncharge_cache_page(struct page *page)
778{
779 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -0700780 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700781 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
782}
783
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800784/*
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700785 * Before starting migration, account against new page.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800786 */
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700787int mem_cgroup_prepare_migration(struct page *page, struct page *newpage)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800788{
789 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700790 struct mem_cgroup *mem = NULL;
791 enum charge_type ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
792 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800793
Balbir Singh40779602008-04-04 14:29:59 -0700794 if (mem_cgroup_subsys.disabled)
795 return 0;
796
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700797 pc = lookup_page_cgroup(page);
798 lock_page_cgroup(pc);
799 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700800 mem = pc->mem_cgroup;
801 css_get(&mem->css);
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700802 if (PageCgroupCache(pc)) {
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700803 if (page_is_file_cache(page))
804 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
805 else
806 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
807 }
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800808 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700809 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700810 if (mem) {
811 ret = mem_cgroup_charge_common(newpage, NULL, GFP_KERNEL,
812 ctype, mem);
813 css_put(&mem->css);
814 }
815 return ret;
816}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -0800817
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700818/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700819void mem_cgroup_end_migration(struct page *newpage)
820{
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700821 /*
822 * At success, page->mapping is not NULL.
823 * special rollback care is necessary when
824 * 1. at migration failure. (newpage->mapping is cleared in this case)
825 * 2. the newpage was moved but not remapped again because the task
826 * exits and the newpage is obsolete. In this case, the new page
827 * may be a swapcache. So, we just call mem_cgroup_uncharge_page()
828 * always for avoiding mess. The page_cgroup will be removed if
829 * unnecessary. File cache pages is still on radix-tree. Don't
830 * care it.
831 */
832 if (!newpage->mapping)
833 __mem_cgroup_uncharge_common(newpage,
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700834 MEM_CGROUP_CHARGE_TYPE_FORCE);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700835 else if (PageAnon(newpage))
836 mem_cgroup_uncharge_page(newpage);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800837}
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800838
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800839/*
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -0700840 * A call to try to shrink memory usage under specified resource controller.
841 * This is typically used for page reclaiming for shmem for reducing side
842 * effect of page allocation from shmem, which is used by some mem_cgroup.
843 */
844int mem_cgroup_shrink_usage(struct mm_struct *mm, gfp_t gfp_mask)
845{
846 struct mem_cgroup *mem;
847 int progress = 0;
848 int retry = MEM_CGROUP_RECLAIM_RETRIES;
849
Li Zefancede86a2008-07-25 01:47:18 -0700850 if (mem_cgroup_subsys.disabled)
851 return 0;
Hugh Dickins9623e072008-08-12 15:08:41 -0700852 if (!mm)
853 return 0;
Li Zefancede86a2008-07-25 01:47:18 -0700854
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -0700855 rcu_read_lock();
856 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
Balbir Singh31a78f22008-09-28 23:09:31 +0100857 if (unlikely(!mem)) {
858 rcu_read_unlock();
859 return 0;
860 }
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -0700861 css_get(&mem->css);
862 rcu_read_unlock();
863
864 do {
865 progress = try_to_free_mem_cgroup_pages(mem, gfp_mask);
Daisuke Nishimuraa10cebf2008-09-22 13:57:52 -0700866 progress += res_counter_check_under_limit(&mem->res);
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -0700867 } while (!progress && --retry);
868
869 css_put(&mem->css);
870 if (!retry)
871 return -ENOMEM;
872 return 0;
873}
874
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -0800875static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
876 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -0700877{
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;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700921 if (!PageCgroupUsed(pc))
922 break;
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800923 get_page(page);
924 spin_unlock_irqrestore(&mz->lru_lock, flags);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700925 /*
926 * Check if this page is on LRU. !LRU page can be found
927 * if it's under page migration.
928 */
929 if (PageLRU(page)) {
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700930 __mem_cgroup_uncharge_common(page,
931 MEM_CGROUP_CHARGE_TYPE_FORCE);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700932 put_page(page);
933 if (--count <= 0) {
934 count = FORCE_UNCHARGE_BATCH;
935 cond_resched();
936 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700937 } else {
938 spin_lock_irqsave(&mz->lru_lock, flags);
939 break;
940 }
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800941 spin_lock_irqsave(&mz->lru_lock, flags);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800942 }
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800943 spin_unlock_irqrestore(&mz->lru_lock, flags);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800944}
945
946/*
947 * make mem_cgroup's charge to be 0 if there is no task.
948 * This enables deleting this mem_cgroup.
949 */
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800950static int mem_cgroup_force_empty(struct mem_cgroup *mem)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800951{
952 int ret = -EBUSY;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800953 int node, zid;
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800954
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800955 css_get(&mem->css);
956 /*
957 * page reclaim code (kswapd etc..) will move pages between
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800958 * active_list <-> inactive_list while we don't take a lock.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800959 * So, we have to do loop here until all lists are empty.
960 */
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800961 while (mem->res.usage > 0) {
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800962 if (atomic_read(&mem->css.cgroup->count) > 0)
963 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700964 /* This is for making all *used* pages to be on LRU. */
965 lru_add_drain_all();
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800966 for_each_node_state(node, N_POSSIBLE)
967 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
968 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700969 enum lru_list l;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800970 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700971 for_each_lru(l)
972 mem_cgroup_force_empty_list(mem, mz, l);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800973 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700974 cond_resched();
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800975 }
976 ret = 0;
977out:
978 css_put(&mem->css);
979 return ret;
980}
981
Paul Menage2c3daa72008-04-29 00:59:58 -0700982static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800983{
Paul Menage2c3daa72008-04-29 00:59:58 -0700984 return res_counter_read_u64(&mem_cgroup_from_cont(cont)->res,
985 cft->private);
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800986}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -0700987/*
988 * The user of this function is...
989 * RES_LIMIT.
990 */
Paul Menage856c13a2008-07-25 01:47:04 -0700991static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
992 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800993{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -0700994 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
995 unsigned long long val;
996 int ret;
997
998 switch (cft->private) {
999 case RES_LIMIT:
1000 /* This function does all necessary parse...reuse it */
1001 ret = res_counter_memparse_write_strategy(buffer, &val);
1002 if (!ret)
1003 ret = mem_cgroup_resize_limit(memcg, val);
1004 break;
1005 default:
1006 ret = -EINVAL; /* should be BUG() ? */
1007 break;
1008 }
1009 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001010}
1011
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001012static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001013{
1014 struct mem_cgroup *mem;
1015
1016 mem = mem_cgroup_from_cont(cont);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001017 switch (event) {
1018 case RES_MAX_USAGE:
1019 res_counter_reset_max(&mem->res);
1020 break;
1021 case RES_FAILCNT:
1022 res_counter_reset_failcnt(&mem->res);
1023 break;
1024 }
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07001025 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001026}
1027
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07001028static int mem_force_empty_write(struct cgroup *cont, unsigned int event)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001029{
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07001030 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont));
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001031}
1032
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001033static const struct mem_cgroup_stat_desc {
1034 const char *msg;
1035 u64 unit;
1036} mem_cgroup_stat_desc[] = {
1037 [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
1038 [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
Balaji Rao55e462b2008-05-01 04:35:12 -07001039 [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, },
1040 [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, },
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001041};
1042
Paul Menagec64745c2008-04-29 01:00:02 -07001043static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
1044 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001045{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001046 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
1047 struct mem_cgroup_stat *stat = &mem_cont->stat;
1048 int i;
1049
1050 for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
1051 s64 val;
1052
1053 val = mem_cgroup_read_stat(stat, i);
1054 val *= mem_cgroup_stat_desc[i].unit;
Paul Menagec64745c2008-04-29 01:00:02 -07001055 cb->fill(cb, mem_cgroup_stat_desc[i].msg, val);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001056 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001057 /* showing # of active pages */
1058 {
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001059 unsigned long active_anon, inactive_anon;
1060 unsigned long active_file, inactive_file;
Lee Schermerhorn7b854122008-10-18 20:26:40 -07001061 unsigned long unevictable;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001062
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001063 inactive_anon = mem_cgroup_get_all_zonestat(mem_cont,
1064 LRU_INACTIVE_ANON);
1065 active_anon = mem_cgroup_get_all_zonestat(mem_cont,
1066 LRU_ACTIVE_ANON);
1067 inactive_file = mem_cgroup_get_all_zonestat(mem_cont,
1068 LRU_INACTIVE_FILE);
1069 active_file = mem_cgroup_get_all_zonestat(mem_cont,
1070 LRU_ACTIVE_FILE);
Lee Schermerhorn7b854122008-10-18 20:26:40 -07001071 unevictable = mem_cgroup_get_all_zonestat(mem_cont,
1072 LRU_UNEVICTABLE);
1073
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001074 cb->fill(cb, "active_anon", (active_anon) * PAGE_SIZE);
1075 cb->fill(cb, "inactive_anon", (inactive_anon) * PAGE_SIZE);
1076 cb->fill(cb, "active_file", (active_file) * PAGE_SIZE);
1077 cb->fill(cb, "inactive_file", (inactive_file) * PAGE_SIZE);
Lee Schermerhorn7b854122008-10-18 20:26:40 -07001078 cb->fill(cb, "unevictable", unevictable * PAGE_SIZE);
1079
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001080 }
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001081 return 0;
1082}
1083
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001084static struct cftype mem_cgroup_files[] = {
1085 {
Balbir Singh0eea1032008-02-07 00:13:57 -08001086 .name = "usage_in_bytes",
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001087 .private = RES_USAGE,
Paul Menage2c3daa72008-04-29 00:59:58 -07001088 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001089 },
1090 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001091 .name = "max_usage_in_bytes",
1092 .private = RES_MAX_USAGE,
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001093 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001094 .read_u64 = mem_cgroup_read,
1095 },
1096 {
Balbir Singh0eea1032008-02-07 00:13:57 -08001097 .name = "limit_in_bytes",
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001098 .private = RES_LIMIT,
Paul Menage856c13a2008-07-25 01:47:04 -07001099 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07001100 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001101 },
1102 {
1103 .name = "failcnt",
1104 .private = RES_FAILCNT,
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001105 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07001106 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001107 },
Balbir Singh8697d332008-02-07 00:13:59 -08001108 {
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001109 .name = "force_empty",
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07001110 .trigger = mem_force_empty_write,
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001111 },
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001112 {
1113 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07001114 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001115 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001116};
1117
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001118static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1119{
1120 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001121 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07001122 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07001123 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001124 /*
1125 * This routine is called against possible nodes.
1126 * But it's BUG to call kmalloc() against offline node.
1127 *
1128 * TODO: this routine can waste much memory for nodes which will
1129 * never be onlined. It's better to use memory hotplug callback
1130 * function.
1131 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07001132 if (!node_state(node, N_NORMAL_MEMORY))
1133 tmp = -1;
1134 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001135 if (!pn)
1136 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001137
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001138 mem->info.nodeinfo[node] = pn;
1139 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001140
1141 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
1142 mz = &pn->zoneinfo[zone];
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001143 spin_lock_init(&mz->lru_lock);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001144 for_each_lru(l)
1145 INIT_LIST_HEAD(&mz->lists[l]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001146 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001147 return 0;
1148}
1149
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001150static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1151{
1152 kfree(mem->info.nodeinfo[node]);
1153}
1154
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001155static struct mem_cgroup *mem_cgroup_alloc(void)
1156{
1157 struct mem_cgroup *mem;
1158
1159 if (sizeof(*mem) < PAGE_SIZE)
1160 mem = kmalloc(sizeof(*mem), GFP_KERNEL);
1161 else
1162 mem = vmalloc(sizeof(*mem));
1163
1164 if (mem)
1165 memset(mem, 0, sizeof(*mem));
1166 return mem;
1167}
1168
1169static void mem_cgroup_free(struct mem_cgroup *mem)
1170{
1171 if (sizeof(*mem) < PAGE_SIZE)
1172 kfree(mem);
1173 else
1174 vfree(mem);
1175}
1176
1177
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001178static struct cgroup_subsys_state *
1179mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
1180{
1181 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001182 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001183
Balbir Singhb6ac57d2008-04-29 01:00:19 -07001184 if (unlikely((cont->parent) == NULL)) {
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001185 mem = &init_mem_cgroup;
Balbir Singhb6ac57d2008-04-29 01:00:19 -07001186 } else {
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001187 mem = mem_cgroup_alloc();
1188 if (!mem)
1189 return ERR_PTR(-ENOMEM);
Balbir Singhb6ac57d2008-04-29 01:00:19 -07001190 }
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001191
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001192 res_counter_init(&mem->res);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001193
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001194 for_each_node_state(node, N_POSSIBLE)
1195 if (alloc_mem_cgroup_per_zone_info(mem, node))
1196 goto free_out;
1197
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001198 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001199free_out:
1200 for_each_node_state(node, N_POSSIBLE)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001201 free_mem_cgroup_per_zone_info(mem, node);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001202 if (cont->parent != NULL)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001203 mem_cgroup_free(mem);
Li Zefan2dda81c2008-02-23 15:24:14 -08001204 return ERR_PTR(-ENOMEM);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001205}
1206
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08001207static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
1208 struct cgroup *cont)
1209{
1210 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1211 mem_cgroup_force_empty(mem);
1212}
1213
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001214static void mem_cgroup_destroy(struct cgroup_subsys *ss,
1215 struct cgroup *cont)
1216{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001217 int node;
1218 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1219
1220 for_each_node_state(node, N_POSSIBLE)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001221 free_mem_cgroup_per_zone_info(mem, node);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001222
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001223 mem_cgroup_free(mem_cgroup_from_cont(cont));
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001224}
1225
1226static int mem_cgroup_populate(struct cgroup_subsys *ss,
1227 struct cgroup *cont)
1228{
1229 return cgroup_add_files(cont, ss, mem_cgroup_files,
1230 ARRAY_SIZE(mem_cgroup_files));
1231}
1232
Balbir Singh67e465a2008-02-07 00:13:54 -08001233static void mem_cgroup_move_task(struct cgroup_subsys *ss,
1234 struct cgroup *cont,
1235 struct cgroup *old_cont,
1236 struct task_struct *p)
1237{
1238 struct mm_struct *mm;
1239 struct mem_cgroup *mem, *old_mem;
1240
1241 mm = get_task_mm(p);
1242 if (mm == NULL)
1243 return;
1244
1245 mem = mem_cgroup_from_cont(cont);
1246 old_mem = mem_cgroup_from_cont(old_cont);
1247
Balbir Singh67e465a2008-02-07 00:13:54 -08001248 /*
1249 * Only thread group leaders are allowed to migrate, the mm_struct is
1250 * in effect owned by the leader
1251 */
Pavel Emelyanov52ea27e2008-03-19 17:00:45 -07001252 if (!thread_group_leader(p))
Balbir Singh67e465a2008-02-07 00:13:54 -08001253 goto out;
1254
Balbir Singh67e465a2008-02-07 00:13:54 -08001255out:
1256 mmput(mm);
Balbir Singh67e465a2008-02-07 00:13:54 -08001257}
1258
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001259struct cgroup_subsys mem_cgroup_subsys = {
1260 .name = "memory",
1261 .subsys_id = mem_cgroup_subsys_id,
1262 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08001263 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001264 .destroy = mem_cgroup_destroy,
1265 .populate = mem_cgroup_populate,
Balbir Singh67e465a2008-02-07 00:13:54 -08001266 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001267 .early_init = 0,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001268};