blob: 3468c38adde69ef9415a7ee5a4317e82d1575b9b [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 Hiroyukid13d1442009-01-07 18:07:56 -080024#include <linux/pagemap.h>
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080025#include <linux/smp.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080026#include <linux/page-flags.h>
Balbir Singh66e17072008-02-07 00:13:56 -080027#include <linux/backing-dev.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080028#include <linux/bit_spinlock.h>
29#include <linux/rcupdate.h>
Balbir Singhe2224322009-04-02 16:57:39 -070030#include <linux/limits.h>
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -080031#include <linux/mutex.h>
Balbir Singhb6ac57d2008-04-29 01:00:19 -070032#include <linux/slab.h>
Balbir Singh66e17072008-02-07 00:13:56 -080033#include <linux/swap.h>
34#include <linux/spinlock.h>
35#include <linux/fs.h>
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -080036#include <linux/seq_file.h>
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -070037#include <linux/vmalloc.h>
Christoph Lameterb69408e2008-10-18 20:26:14 -070038#include <linux/mm_inline.h>
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070039#include <linux/page_cgroup.h>
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -080040#include "internal.h"
Balbir Singh8cdea7c2008-02-07 00:13:50 -080041
Balbir Singh8697d332008-02-07 00:13:59 -080042#include <asm/uaccess.h>
43
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070044struct cgroup_subsys mem_cgroup_subsys __read_mostly;
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070045#define MEM_CGROUP_RECLAIM_RETRIES 5
Balbir Singh8cdea7c2008-02-07 00:13:50 -080046
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080047#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
Li Zefan338c8432009-06-17 16:27:15 -070048/* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080049int do_swap_account __read_mostly;
50static int really_do_swap_account __initdata = 1; /* for remember boot option*/
51#else
52#define do_swap_account (0)
53#endif
54
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -080055static DEFINE_MUTEX(memcg_tasklist); /* can be hold under cgroup_mutex */
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080056
Balbir Singh8cdea7c2008-02-07 00:13:50 -080057/*
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080058 * Statistics for memory cgroup.
59 */
60enum mem_cgroup_stat_index {
61 /*
62 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
63 */
64 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
Balbir Singhd69b0422009-06-17 16:26:34 -070065 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
66 MEM_CGROUP_STAT_MAPPED_FILE, /* # of pages charged as file rss */
Balaji Rao55e462b2008-05-01 04:35:12 -070067 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
68 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080069
70 MEM_CGROUP_STAT_NSTATS,
71};
72
73struct mem_cgroup_stat_cpu {
74 s64 count[MEM_CGROUP_STAT_NSTATS];
75} ____cacheline_aligned_in_smp;
76
77struct mem_cgroup_stat {
Jan Blunckc8dad2b2009-01-07 18:07:53 -080078 struct mem_cgroup_stat_cpu cpustat[0];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080079};
80
81/*
82 * For accounting under irq disable, no need for increment preempt count.
83 */
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -070084static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080085 enum mem_cgroup_stat_index idx, int val)
86{
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -070087 stat->count[idx] += val;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080088}
89
90static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
91 enum mem_cgroup_stat_index idx)
92{
93 int cpu;
94 s64 ret = 0;
95 for_each_possible_cpu(cpu)
96 ret += stat->cpustat[cpu].count[idx];
97 return ret;
98}
99
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700100static s64 mem_cgroup_local_usage(struct mem_cgroup_stat *stat)
101{
102 s64 ret;
103
104 ret = mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_CACHE);
105 ret += mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_RSS);
106 return ret;
107}
108
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800109/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800110 * per-zone information in memory controller.
111 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800112struct mem_cgroup_per_zone {
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800113 /*
114 * spin_lock to protect the per cgroup LRU
115 */
Christoph Lameterb69408e2008-10-18 20:26:14 -0700116 struct list_head lists[NR_LRU_LISTS];
117 unsigned long count[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800118
119 struct zone_reclaim_stat reclaim_stat;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800120};
121/* Macro for accessing counter */
122#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
123
124struct mem_cgroup_per_node {
125 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
126};
127
128struct mem_cgroup_lru_info {
129 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
130};
131
132/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800133 * The memory controller data structure. The memory controller controls both
134 * page cache and RSS per cgroup. We would eventually like to provide
135 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
136 * to help the administrator determine what knobs to tune.
137 *
138 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800139 * we hit the water mark. May be even add a low water mark, such that
140 * no reclaim occurs from a cgroup at it's low water mark, this is
141 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800142 */
143struct mem_cgroup {
144 struct cgroup_subsys_state css;
145 /*
146 * the counter to account for memory usage
147 */
148 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800149 /*
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800150 * the counter to account for mem+swap usage.
151 */
152 struct res_counter memsw;
153 /*
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800154 * Per cgroup active and inactive list, similar to the
155 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800156 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800157 struct mem_cgroup_lru_info info;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800158
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800159 /*
160 protect against reclaim related member.
161 */
162 spinlock_t reclaim_param_lock;
163
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800164 int prev_priority; /* for recording reclaim priority */
Balbir Singh6d61ef42009-01-07 18:08:06 -0800165
166 /*
167 * While reclaiming in a hiearchy, we cache the last child we
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700168 * reclaimed from.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800169 */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700170 int last_scanned_child;
Balbir Singh18f59ea2009-01-07 18:08:07 -0800171 /*
172 * Should the accounting and control be hierarchical, per subtree?
173 */
174 bool use_hierarchy;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800175 unsigned long last_oom_jiffies;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800176 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800177
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800178 unsigned int swappiness;
179
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800180 /*
Jan Blunckc8dad2b2009-01-07 18:07:53 -0800181 * statistics. This must be placed at the end of memcg.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800182 */
183 struct mem_cgroup_stat stat;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800184};
185
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800186enum charge_type {
187 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
188 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700189 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700190 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800191 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700192 NR_CHARGE_TYPE,
193};
194
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700195/* only for here (for easy reading.) */
196#define PCGF_CACHE (1UL << PCG_CACHE)
197#define PCGF_USED (1UL << PCG_USED)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700198#define PCGF_LOCK (1UL << PCG_LOCK)
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700199static const unsigned long
200pcg_default_flags[NR_CHARGE_TYPE] = {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800201 PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* File Cache */
202 PCGF_USED | PCGF_LOCK, /* Anon */
203 PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* Shmem */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700204 0, /* FORCE */
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800205};
206
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800207/* for encoding cft->private value on file */
208#define _MEM (0)
209#define _MEMSWAP (1)
210#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
211#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
212#define MEMFILE_ATTR(val) ((val) & 0xffff)
213
214static void mem_cgroup_get(struct mem_cgroup *mem);
215static void mem_cgroup_put(struct mem_cgroup *mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -0800216static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800217
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700218static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
219 struct page_cgroup *pc,
220 bool charge)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800221{
222 int val = (charge)? 1 : -1;
223 struct mem_cgroup_stat *stat = &mem->stat;
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700224 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800225 int cpu = get_cpu();
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800226
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800227 cpustat = &stat->cpustat[cpu];
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700228 if (PageCgroupCache(pc))
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700229 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800230 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700231 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700232
233 if (charge)
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700234 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700235 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
236 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700237 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700238 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800239 put_cpu();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800240}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800241
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800242static struct mem_cgroup_per_zone *
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800243mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
244{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800245 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
246}
247
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800248static struct mem_cgroup_per_zone *
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800249page_cgroup_zoneinfo(struct page_cgroup *pc)
250{
251 struct mem_cgroup *mem = pc->mem_cgroup;
252 int nid = page_cgroup_nid(pc);
253 int zid = page_cgroup_zid(pc);
254
KOSAKI Motohiro54992762009-01-07 18:08:18 -0800255 if (!mem)
256 return NULL;
257
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800258 return mem_cgroup_zoneinfo(mem, nid, zid);
259}
260
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700261static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700262 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800263{
264 int nid, zid;
265 struct mem_cgroup_per_zone *mz;
266 u64 total = 0;
267
268 for_each_online_node(nid)
269 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
270 mz = mem_cgroup_zoneinfo(mem, nid, zid);
271 total += MEM_CGROUP_ZSTAT(mz, idx);
272 }
273 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800274}
275
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800276static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800277{
278 return container_of(cgroup_subsys_state(cont,
279 mem_cgroup_subsys_id), struct mem_cgroup,
280 css);
281}
282
Balbir Singhcf475ad2008-04-29 01:00:16 -0700283struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800284{
Balbir Singh31a78f22008-09-28 23:09:31 +0100285 /*
286 * mm_update_next_owner() may clear mm->owner to NULL
287 * if it races with swapoff, page migration, etc.
288 * So this can be called with p == NULL.
289 */
290 if (unlikely(!p))
291 return NULL;
292
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800293 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
294 struct mem_cgroup, css);
295}
296
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800297static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
298{
299 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700300
301 if (!mm)
302 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800303 /*
304 * Because we have no locks, mm->owner's may be being moved to other
305 * cgroup. We use css_tryget() here even if this looks
306 * pessimistic (rather than adding locks here).
307 */
308 rcu_read_lock();
309 do {
310 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
311 if (unlikely(!mem))
312 break;
313 } while (!css_tryget(&mem->css));
314 rcu_read_unlock();
315 return mem;
316}
317
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700318/*
319 * Call callback function against all cgroup under hierarchy tree.
320 */
321static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
322 int (*func)(struct mem_cgroup *, void *))
323{
324 int found, ret, nextid;
325 struct cgroup_subsys_state *css;
326 struct mem_cgroup *mem;
327
328 if (!root->use_hierarchy)
329 return (*func)(root, data);
330
331 nextid = 1;
332 do {
333 ret = 0;
334 mem = NULL;
335
336 rcu_read_lock();
337 css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
338 &found);
339 if (css && css_tryget(css))
340 mem = container_of(css, struct mem_cgroup, css);
341 rcu_read_unlock();
342
343 if (mem) {
344 ret = (*func)(mem, data);
345 css_put(&mem->css);
346 }
347 nextid = found + 1;
348 } while (!ret && css);
349
350 return ret;
351}
352
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800353/*
354 * Following LRU functions are allowed to be used without PCG_LOCK.
355 * Operations are called by routine of global LRU independently from memcg.
356 * What we have to take care of here is validness of pc->mem_cgroup.
357 *
358 * Changes to pc->mem_cgroup happens when
359 * 1. charge
360 * 2. moving account
361 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
362 * It is added to LRU before charge.
363 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
364 * When moving account, the page is not on LRU. It's isolated.
365 */
366
367void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800368{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800369 struct page_cgroup *pc;
370 struct mem_cgroup *mem;
371 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700372
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800373 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800374 return;
375 pc = lookup_page_cgroup(page);
376 /* can happen while we handle swapcache. */
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800377 if (list_empty(&pc->lru) || !pc->mem_cgroup)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800378 return;
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800379 /*
380 * We don't check PCG_USED bit. It's cleared when the "page" is finally
381 * removed from global LRU.
382 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800383 mz = page_cgroup_zoneinfo(pc);
384 mem = pc->mem_cgroup;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700385 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800386 list_del_init(&pc->lru);
387 return;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800388}
389
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800390void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800391{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800392 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800393}
394
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800395void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800396{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800397 struct mem_cgroup_per_zone *mz;
398 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800399
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800400 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700401 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700402
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800403 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800404 /*
405 * Used bit is set without atomic ops but after smp_wmb().
406 * For making pc->mem_cgroup visible, insert smp_rmb() here.
407 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800408 smp_rmb();
409 /* unused page is not rotated. */
410 if (!PageCgroupUsed(pc))
411 return;
412 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700413 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800414}
415
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800416void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
417{
418 struct page_cgroup *pc;
419 struct mem_cgroup_per_zone *mz;
420
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800421 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800422 return;
423 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800424 /*
425 * Used bit is set without atomic ops but after smp_wmb().
426 * For making pc->mem_cgroup visible, insert smp_rmb() here.
427 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800428 smp_rmb();
429 if (!PageCgroupUsed(pc))
430 return;
431
432 mz = page_cgroup_zoneinfo(pc);
433 MEM_CGROUP_ZSTAT(mz, lru) += 1;
434 list_add(&pc->lru, &mz->lists[lru]);
435}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800436
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800437/*
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800438 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
439 * lru because the page may.be reused after it's fully uncharged (because of
440 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
441 * it again. This function is only used to charge SwapCache. It's done under
442 * lock_page and expected that zone->lru_lock is never held.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800443 */
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800444static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800445{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800446 unsigned long flags;
447 struct zone *zone = page_zone(page);
448 struct page_cgroup *pc = lookup_page_cgroup(page);
449
450 spin_lock_irqsave(&zone->lru_lock, flags);
451 /*
452 * Forget old LRU when this page_cgroup is *not* used. This Used bit
453 * is guarded by lock_page() because the page is SwapCache.
454 */
455 if (!PageCgroupUsed(pc))
456 mem_cgroup_del_lru_list(page, page_lru(page));
457 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800458}
459
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800460static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
461{
462 unsigned long flags;
463 struct zone *zone = page_zone(page);
464 struct page_cgroup *pc = lookup_page_cgroup(page);
465
466 spin_lock_irqsave(&zone->lru_lock, flags);
467 /* link when the page is linked to LRU but page_cgroup isn't */
468 if (PageLRU(page) && list_empty(&pc->lru))
469 mem_cgroup_add_lru_list(page, page_lru(page));
470 spin_unlock_irqrestore(&zone->lru_lock, flags);
471}
472
473
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800474void mem_cgroup_move_lists(struct page *page,
475 enum lru_list from, enum lru_list to)
476{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800477 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800478 return;
479 mem_cgroup_del_lru_list(page, from);
480 mem_cgroup_add_lru_list(page, to);
481}
482
David Rientjes4c4a2212008-02-07 00:14:06 -0800483int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
484{
485 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700486 struct mem_cgroup *curr = NULL;
David Rientjes4c4a2212008-02-07 00:14:06 -0800487
488 task_lock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700489 rcu_read_lock();
490 curr = try_get_mem_cgroup_from_mm(task->mm);
491 rcu_read_unlock();
David Rientjes4c4a2212008-02-07 00:14:06 -0800492 task_unlock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700493 if (!curr)
494 return 0;
495 if (curr->use_hierarchy)
496 ret = css_is_ancestor(&curr->css, &mem->css);
497 else
498 ret = (curr == mem);
499 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -0800500 return ret;
501}
502
Balbir Singh66e17072008-02-07 00:13:56 -0800503/*
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800504 * prev_priority control...this will be used in memory reclaim path.
505 */
506int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
507{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800508 int prev_priority;
509
510 spin_lock(&mem->reclaim_param_lock);
511 prev_priority = mem->prev_priority;
512 spin_unlock(&mem->reclaim_param_lock);
513
514 return prev_priority;
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800515}
516
517void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
518{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800519 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800520 if (priority < mem->prev_priority)
521 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800522 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800523}
524
525void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
526{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800527 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800528 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800529 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800530}
531
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800532static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800533{
534 unsigned long active;
535 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800536 unsigned long gb;
537 unsigned long inactive_ratio;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800538
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700539 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
540 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800541
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800542 gb = (inactive + active) >> (30 - PAGE_SHIFT);
543 if (gb)
544 inactive_ratio = int_sqrt(10 * gb);
545 else
546 inactive_ratio = 1;
547
548 if (present_pages) {
549 present_pages[0] = inactive;
550 present_pages[1] = active;
551 }
552
553 return inactive_ratio;
554}
555
556int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
557{
558 unsigned long active;
559 unsigned long inactive;
560 unsigned long present_pages[2];
561 unsigned long inactive_ratio;
562
563 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
564
565 inactive = present_pages[0];
566 active = present_pages[1];
567
568 if (inactive * inactive_ratio < active)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800569 return 1;
570
571 return 0;
572}
573
Rik van Riel56e49d22009-06-16 15:32:28 -0700574int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
575{
576 unsigned long active;
577 unsigned long inactive;
578
579 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
580 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
581
582 return (active > inactive);
583}
584
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800585unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
586 struct zone *zone,
587 enum lru_list lru)
588{
589 int nid = zone->zone_pgdat->node_id;
590 int zid = zone_idx(zone);
591 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
592
593 return MEM_CGROUP_ZSTAT(mz, lru);
594}
595
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800596struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
597 struct zone *zone)
598{
599 int nid = zone->zone_pgdat->node_id;
600 int zid = zone_idx(zone);
601 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
602
603 return &mz->reclaim_stat;
604}
605
606struct zone_reclaim_stat *
607mem_cgroup_get_reclaim_stat_from_page(struct page *page)
608{
609 struct page_cgroup *pc;
610 struct mem_cgroup_per_zone *mz;
611
612 if (mem_cgroup_disabled())
613 return NULL;
614
615 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800616 /*
617 * Used bit is set without atomic ops but after smp_wmb().
618 * For making pc->mem_cgroup visible, insert smp_rmb() here.
619 */
620 smp_rmb();
621 if (!PageCgroupUsed(pc))
622 return NULL;
623
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800624 mz = page_cgroup_zoneinfo(pc);
625 if (!mz)
626 return NULL;
627
628 return &mz->reclaim_stat;
629}
630
Balbir Singh66e17072008-02-07 00:13:56 -0800631unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
632 struct list_head *dst,
633 unsigned long *scanned, int order,
634 int mode, struct zone *z,
635 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700636 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -0800637{
638 unsigned long nr_taken = 0;
639 struct page *page;
640 unsigned long scan;
641 LIST_HEAD(pc_list);
642 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800643 struct page_cgroup *pc, *tmp;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800644 int nid = z->zone_pgdat->node_id;
645 int zid = zone_idx(z);
646 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700647 int lru = LRU_FILE * !!file + !!active;
Balbir Singh66e17072008-02-07 00:13:56 -0800648
Balbir Singhcf475ad2008-04-29 01:00:16 -0700649 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800650 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700651 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -0800652
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800653 scan = 0;
654 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -0800655 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800656 break;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800657
658 page = pc->page;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700659 if (unlikely(!PageCgroupUsed(pc)))
660 continue;
Hugh Dickins436c65412008-02-07 00:14:12 -0800661 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800662 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800663
Hugh Dickins436c65412008-02-07 00:14:12 -0800664 scan++;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700665 if (__isolate_lru_page(page, mode, file) == 0) {
Balbir Singh66e17072008-02-07 00:13:56 -0800666 list_move(&page->lru, dst);
667 nr_taken++;
668 }
669 }
670
Balbir Singh66e17072008-02-07 00:13:56 -0800671 *scanned = scan;
672 return nr_taken;
673}
674
Balbir Singh6d61ef42009-01-07 18:08:06 -0800675#define mem_cgroup_from_res_counter(counter, member) \
676 container_of(counter, struct mem_cgroup, member)
677
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -0800678static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
679{
680 if (do_swap_account) {
681 if (res_counter_check_under_limit(&mem->res) &&
682 res_counter_check_under_limit(&mem->memsw))
683 return true;
684 } else
685 if (res_counter_check_under_limit(&mem->res))
686 return true;
687 return false;
688}
689
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800690static unsigned int get_swappiness(struct mem_cgroup *memcg)
691{
692 struct cgroup *cgrp = memcg->css.cgroup;
693 unsigned int swappiness;
694
695 /* root ? */
696 if (cgrp->parent == NULL)
697 return vm_swappiness;
698
699 spin_lock(&memcg->reclaim_param_lock);
700 swappiness = memcg->swappiness;
701 spin_unlock(&memcg->reclaim_param_lock);
702
703 return swappiness;
704}
705
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700706static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
707{
708 int *val = data;
709 (*val)++;
710 return 0;
711}
Balbir Singhe2224322009-04-02 16:57:39 -0700712
713/**
714 * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode.
715 * @memcg: The memory cgroup that went over limit
716 * @p: Task that is going to be killed
717 *
718 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
719 * enabled
720 */
721void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
722{
723 struct cgroup *task_cgrp;
724 struct cgroup *mem_cgrp;
725 /*
726 * Need a buffer in BSS, can't rely on allocations. The code relies
727 * on the assumption that OOM is serialized for memory controller.
728 * If this assumption is broken, revisit this code.
729 */
730 static char memcg_name[PATH_MAX];
731 int ret;
732
733 if (!memcg)
734 return;
735
736
737 rcu_read_lock();
738
739 mem_cgrp = memcg->css.cgroup;
740 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
741
742 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
743 if (ret < 0) {
744 /*
745 * Unfortunately, we are unable to convert to a useful name
746 * But we'll still print out the usage information
747 */
748 rcu_read_unlock();
749 goto done;
750 }
751 rcu_read_unlock();
752
753 printk(KERN_INFO "Task in %s killed", memcg_name);
754
755 rcu_read_lock();
756 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
757 if (ret < 0) {
758 rcu_read_unlock();
759 goto done;
760 }
761 rcu_read_unlock();
762
763 /*
764 * Continues from above, so we don't need an KERN_ level
765 */
766 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
767done:
768
769 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
770 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
771 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
772 res_counter_read_u64(&memcg->res, RES_FAILCNT));
773 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
774 "failcnt %llu\n",
775 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
776 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
777 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
778}
779
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700780/*
781 * This function returns the number of memcg under hierarchy tree. Returns
782 * 1(self count) if no children.
783 */
784static int mem_cgroup_count_children(struct mem_cgroup *mem)
785{
786 int num = 0;
787 mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
788 return num;
789}
790
Balbir Singh6d61ef42009-01-07 18:08:06 -0800791/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700792 * Visit the first child (need not be the first child as per the ordering
793 * of the cgroup list, since we track last_scanned_child) of @mem and use
794 * that to reclaim free pages from.
795 */
796static struct mem_cgroup *
797mem_cgroup_select_victim(struct mem_cgroup *root_mem)
798{
799 struct mem_cgroup *ret = NULL;
800 struct cgroup_subsys_state *css;
801 int nextid, found;
802
803 if (!root_mem->use_hierarchy) {
804 css_get(&root_mem->css);
805 ret = root_mem;
806 }
807
808 while (!ret) {
809 rcu_read_lock();
810 nextid = root_mem->last_scanned_child + 1;
811 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
812 &found);
813 if (css && css_tryget(css))
814 ret = container_of(css, struct mem_cgroup, css);
815
816 rcu_read_unlock();
817 /* Updates scanning parameter */
818 spin_lock(&root_mem->reclaim_param_lock);
819 if (!css) {
820 /* this means start scan from ID:1 */
821 root_mem->last_scanned_child = 0;
822 } else
823 root_mem->last_scanned_child = found;
824 spin_unlock(&root_mem->reclaim_param_lock);
825 }
826
827 return ret;
828}
829
830/*
831 * Scan the hierarchy if needed to reclaim memory. We remember the last child
832 * we reclaimed from, so that we don't end up penalizing one child extensively
833 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800834 *
835 * root_mem is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700836 *
837 * We give up and return to the caller when we visit root_mem twice.
838 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700839 *
840 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800841 */
842static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700843 gfp_t gfp_mask, bool noswap, bool shrink)
Balbir Singh6d61ef42009-01-07 18:08:06 -0800844{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700845 struct mem_cgroup *victim;
846 int ret, total = 0;
847 int loop = 0;
Balbir Singh6d61ef42009-01-07 18:08:06 -0800848
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700849 while (loop < 2) {
850 victim = mem_cgroup_select_victim(root_mem);
851 if (victim == root_mem)
852 loop++;
853 if (!mem_cgroup_local_usage(&victim->stat)) {
854 /* this cgroup's local usage == 0 */
855 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -0800856 continue;
857 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700858 /* we use swappiness of local cgroup */
859 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask, noswap,
860 get_swappiness(victim));
861 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700862 /*
863 * At shrinking usage, we can't check we should stop here or
864 * reclaim more. It's depends on callers. last_scanned_child
865 * will work enough for keeping fairness under tree.
866 */
867 if (shrink)
868 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700869 total += ret;
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -0800870 if (mem_cgroup_check_under_limit(root_mem))
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700871 return 1 + total;
Balbir Singh6d61ef42009-01-07 18:08:06 -0800872 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700873 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -0800874}
875
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800876bool mem_cgroup_oom_called(struct task_struct *task)
877{
878 bool ret = false;
879 struct mem_cgroup *mem;
880 struct mm_struct *mm;
881
882 rcu_read_lock();
883 mm = task->mm;
884 if (!mm)
885 mm = &init_mm;
886 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
887 if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
888 ret = true;
889 rcu_read_unlock();
890 return ret;
891}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700892
893static int record_last_oom_cb(struct mem_cgroup *mem, void *data)
894{
895 mem->last_oom_jiffies = jiffies;
896 return 0;
897}
898
899static void record_last_oom(struct mem_cgroup *mem)
900{
901 mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb);
902}
903
Balbir Singhd69b0422009-06-17 16:26:34 -0700904/*
905 * Currently used to update mapped file statistics, but the routine can be
906 * generalized to update other statistics as well.
907 */
908void mem_cgroup_update_mapped_file_stat(struct page *page, int val)
909{
910 struct mem_cgroup *mem;
911 struct mem_cgroup_stat *stat;
912 struct mem_cgroup_stat_cpu *cpustat;
913 int cpu;
914 struct page_cgroup *pc;
915
916 if (!page_is_file_cache(page))
917 return;
918
919 pc = lookup_page_cgroup(page);
920 if (unlikely(!pc))
921 return;
922
923 lock_page_cgroup(pc);
924 mem = pc->mem_cgroup;
925 if (!mem)
926 goto done;
927
928 if (!PageCgroupUsed(pc))
929 goto done;
930
931 /*
932 * Preemption is already disabled, we don't need get_cpu()
933 */
934 cpu = smp_processor_id();
935 stat = &mem->stat;
936 cpustat = &stat->cpustat[cpu];
937
938 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE, val);
939done:
940 unlock_page_cgroup(pc);
941}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700942
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800943/*
944 * Unlike exported interface, "oom" parameter is added. if oom==true,
945 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800946 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800947static int __mem_cgroup_try_charge(struct mm_struct *mm,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800948 gfp_t gfp_mask, struct mem_cgroup **memcg,
949 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800950{
Balbir Singh6d61ef42009-01-07 18:08:06 -0800951 struct mem_cgroup *mem, *mem_over_limit;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800952 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Balbir Singh28dbc4b2009-01-07 18:08:05 -0800953 struct res_counter *fail_res;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800954
955 if (unlikely(test_thread_flag(TIF_MEMDIE))) {
956 /* Don't account this! */
957 *memcg = NULL;
958 return 0;
959 }
960
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800961 /*
Hugh Dickins3be91272008-02-07 00:14:19 -0800962 * We always charge the cgroup the mm_struct belongs to.
963 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800964 * thread group leader migrates. It's possible that mm is not
965 * set, if so charge the init_mm (happens for pagecache usage).
966 */
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800967 mem = *memcg;
968 if (likely(!mem)) {
969 mem = try_get_mem_cgroup_from_mm(mm);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800970 *memcg = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700971 } else {
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800972 css_get(&mem->css);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700973 }
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800974 if (unlikely(!mem))
975 return 0;
976
Nikanth Karthikesan46f7e602009-05-28 14:34:41 -0700977 VM_BUG_ON(css_is_removed(&mem->css));
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800978
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800979 while (1) {
980 int ret;
981 bool noswap = false;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800982
Balbir Singh28dbc4b2009-01-07 18:08:05 -0800983 ret = res_counter_charge(&mem->res, PAGE_SIZE, &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800984 if (likely(!ret)) {
985 if (!do_swap_account)
986 break;
Balbir Singh28dbc4b2009-01-07 18:08:05 -0800987 ret = res_counter_charge(&mem->memsw, PAGE_SIZE,
988 &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800989 if (likely(!ret))
990 break;
991 /* mem+swap counter fails */
992 res_counter_uncharge(&mem->res, PAGE_SIZE);
993 noswap = true;
Balbir Singh6d61ef42009-01-07 18:08:06 -0800994 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
995 memsw);
996 } else
997 /* mem counter fails */
998 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
999 res);
1000
Hugh Dickins3be91272008-02-07 00:14:19 -08001001 if (!(gfp_mask & __GFP_WAIT))
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001002 goto nomem;
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001003
Balbir Singh6d61ef42009-01-07 18:08:06 -08001004 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, gfp_mask,
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001005 noswap, false);
Daisuke Nishimura4d1c6272009-01-15 13:51:14 -08001006 if (ret)
1007 continue;
Balbir Singh66e17072008-02-07 00:13:56 -08001008
1009 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001010 * try_to_free_mem_cgroup_pages() might not give us a full
1011 * picture of reclaim. Some pages are reclaimed and might be
1012 * moved to swap cache or just unmapped from the cgroup.
1013 * Check the limit again to see if the reclaim reduced the
1014 * current usage of the cgroup before giving up
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001015 *
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001016 */
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -08001017 if (mem_cgroup_check_under_limit(mem_over_limit))
1018 continue;
Hugh Dickins3be91272008-02-07 00:14:19 -08001019
1020 if (!nr_retries--) {
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001021 if (oom) {
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08001022 mutex_lock(&memcg_tasklist);
KAMEZAWA Hiroyuki88700752009-01-07 18:08:09 -08001023 mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08001024 mutex_unlock(&memcg_tasklist);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001025 record_last_oom(mem_over_limit);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001026 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001027 goto nomem;
Balbir Singh66e17072008-02-07 00:13:56 -08001028 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001029 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001030 return 0;
1031nomem:
1032 css_put(&mem->css);
1033 return -ENOMEM;
1034}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001035
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001036
1037/*
1038 * A helper function to get mem_cgroup from ID. must be called under
1039 * rcu_read_lock(). The caller must check css_is_removed() or some if
1040 * it's concern. (dropping refcnt from swap can be called against removed
1041 * memcg.)
1042 */
1043static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
1044{
1045 struct cgroup_subsys_state *css;
1046
1047 /* ID 0 is unused ID */
1048 if (!id)
1049 return NULL;
1050 css = css_lookup(&mem_cgroup_subsys, id);
1051 if (!css)
1052 return NULL;
1053 return container_of(css, struct mem_cgroup, css);
1054}
1055
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001056static struct mem_cgroup *try_get_mem_cgroup_from_swapcache(struct page *page)
1057{
1058 struct mem_cgroup *mem;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001059 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001060 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001061 swp_entry_t ent;
1062
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001063 VM_BUG_ON(!PageLocked(page));
1064
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001065 if (!PageSwapCache(page))
1066 return NULL;
1067
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001068 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001069 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001070 if (PageCgroupUsed(pc)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001071 mem = pc->mem_cgroup;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001072 if (mem && !css_tryget(&mem->css))
1073 mem = NULL;
1074 } else {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001075 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001076 id = lookup_swap_cgroup(ent);
1077 rcu_read_lock();
1078 mem = mem_cgroup_lookup(id);
1079 if (mem && !css_tryget(&mem->css))
1080 mem = NULL;
1081 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001082 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001083 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001084 return mem;
1085}
1086
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001087/*
Daisuke Nishimuraa5e924f2009-01-07 18:08:28 -08001088 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001089 * USED state. If already USED, uncharge and return.
1090 */
1091
1092static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1093 struct page_cgroup *pc,
1094 enum charge_type ctype)
1095{
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001096 /* try_charge() can return NULL to *memcg, taking care of it. */
1097 if (!mem)
1098 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001099
1100 lock_page_cgroup(pc);
1101 if (unlikely(PageCgroupUsed(pc))) {
1102 unlock_page_cgroup(pc);
1103 res_counter_uncharge(&mem->res, PAGE_SIZE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001104 if (do_swap_account)
1105 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001106 css_put(&mem->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001107 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001108 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001109 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001110 smp_wmb();
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -07001111 pc->flags = pcg_default_flags[ctype];
Hugh Dickins3be91272008-02-07 00:14:19 -08001112
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001113 mem_cgroup_charge_statistics(mem, pc, true);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001114
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001115 unlock_page_cgroup(pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001116}
1117
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001118/**
1119 * mem_cgroup_move_account - move account of the page
1120 * @pc: page_cgroup of the page.
1121 * @from: mem_cgroup which the page is moved from.
1122 * @to: mem_cgroup which the page is moved to. @from != @to.
1123 *
1124 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001125 * - page is not on LRU (isolate_page() is useful.)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001126 *
1127 * returns 0 at success,
1128 * returns -EBUSY when lock is busy or "pc" is unstable.
1129 *
1130 * This function does "uncharge" from old cgroup but doesn't do "charge" to
1131 * new cgroup. It should be done by a caller.
1132 */
1133
1134static int mem_cgroup_move_account(struct page_cgroup *pc,
1135 struct mem_cgroup *from, struct mem_cgroup *to)
1136{
1137 struct mem_cgroup_per_zone *from_mz, *to_mz;
1138 int nid, zid;
1139 int ret = -EBUSY;
Balbir Singhd69b0422009-06-17 16:26:34 -07001140 struct page *page;
1141 int cpu;
1142 struct mem_cgroup_stat *stat;
1143 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001144
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001145 VM_BUG_ON(from == to);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001146 VM_BUG_ON(PageLRU(pc->page));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001147
1148 nid = page_cgroup_nid(pc);
1149 zid = page_cgroup_zid(pc);
1150 from_mz = mem_cgroup_zoneinfo(from, nid, zid);
1151 to_mz = mem_cgroup_zoneinfo(to, nid, zid);
1152
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001153 if (!trylock_page_cgroup(pc))
1154 return ret;
1155
1156 if (!PageCgroupUsed(pc))
1157 goto out;
1158
1159 if (pc->mem_cgroup != from)
1160 goto out;
1161
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001162 res_counter_uncharge(&from->res, PAGE_SIZE);
1163 mem_cgroup_charge_statistics(from, pc, false);
Balbir Singhd69b0422009-06-17 16:26:34 -07001164
1165 page = pc->page;
1166 if (page_is_file_cache(page) && page_mapped(page)) {
1167 cpu = smp_processor_id();
1168 /* Update mapped_file data for mem_cgroup "from" */
1169 stat = &from->stat;
1170 cpustat = &stat->cpustat[cpu];
1171 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1172 -1);
1173
1174 /* Update mapped_file data for mem_cgroup "to" */
1175 stat = &to->stat;
1176 cpustat = &stat->cpustat[cpu];
1177 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1178 1);
1179 }
1180
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001181 if (do_swap_account)
1182 res_counter_uncharge(&from->memsw, PAGE_SIZE);
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001183 css_put(&from->css);
1184
1185 css_get(&to->css);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001186 pc->mem_cgroup = to;
1187 mem_cgroup_charge_statistics(to, pc, true);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001188 ret = 0;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001189out:
1190 unlock_page_cgroup(pc);
1191 return ret;
1192}
1193
1194/*
1195 * move charges to its parent.
1196 */
1197
1198static int mem_cgroup_move_parent(struct page_cgroup *pc,
1199 struct mem_cgroup *child,
1200 gfp_t gfp_mask)
1201{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001202 struct page *page = pc->page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001203 struct cgroup *cg = child->css.cgroup;
1204 struct cgroup *pcg = cg->parent;
1205 struct mem_cgroup *parent;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001206 int ret;
1207
1208 /* Is ROOT ? */
1209 if (!pcg)
1210 return -EINVAL;
1211
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001212
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001213 parent = mem_cgroup_from_cont(pcg);
1214
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001215
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001216 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001217 if (ret || !parent)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001218 return ret;
1219
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001220 if (!get_page_unless_zero(page)) {
1221 ret = -EBUSY;
1222 goto uncharge;
1223 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001224
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001225 ret = isolate_lru_page(page);
1226
1227 if (ret)
1228 goto cancel;
1229
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001230 ret = mem_cgroup_move_account(pc, child, parent);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001231
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001232 putback_lru_page(page);
1233 if (!ret) {
1234 put_page(page);
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001235 /* drop extra refcnt by try_charge() */
1236 css_put(&parent->css);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001237 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001238 }
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001239
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001240cancel:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001241 put_page(page);
1242uncharge:
1243 /* drop extra refcnt by try_charge() */
1244 css_put(&parent->css);
1245 /* uncharge if move fails */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001246 res_counter_uncharge(&parent->res, PAGE_SIZE);
1247 if (do_swap_account)
1248 res_counter_uncharge(&parent->memsw, PAGE_SIZE);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001249 return ret;
1250}
1251
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001252/*
1253 * Charge the memory controller for page usage.
1254 * Return
1255 * 0 if the charge was successful
1256 * < 0 if the cgroup is over its limit
1257 */
1258static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
1259 gfp_t gfp_mask, enum charge_type ctype,
1260 struct mem_cgroup *memcg)
1261{
1262 struct mem_cgroup *mem;
1263 struct page_cgroup *pc;
1264 int ret;
1265
1266 pc = lookup_page_cgroup(page);
1267 /* can happen at boot */
1268 if (unlikely(!pc))
1269 return 0;
1270 prefetchw(pc);
1271
1272 mem = memcg;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001273 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001274 if (ret || !mem)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001275 return ret;
1276
1277 __mem_cgroup_commit_charge(mem, pc, ctype);
1278 return 0;
1279}
1280
1281int mem_cgroup_newpage_charge(struct page *page,
1282 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001283{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001284 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001285 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001286 if (PageCompound(page))
1287 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001288 /*
1289 * If already mapped, we don't have to account.
1290 * If page cache, page->mapping has address_space.
1291 * But page->mapping may have out-of-use anon_vma pointer,
1292 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
1293 * is NULL.
1294 */
1295 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
1296 return 0;
1297 if (unlikely(!mm))
1298 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001299 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001300 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001301}
1302
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001303static void
1304__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1305 enum charge_type ctype);
1306
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001307int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
1308 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08001309{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001310 struct mem_cgroup *mem = NULL;
1311 int ret;
1312
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001313 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001314 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001315 if (PageCompound(page))
1316 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001317 /*
1318 * Corner case handling. This is called from add_to_page_cache()
1319 * in usual. But some FS (shmem) precharges this page before calling it
1320 * and call add_to_page_cache() with GFP_NOWAIT.
1321 *
1322 * For GFP_NOWAIT case, the page may be pre-charged before calling
1323 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
1324 * charge twice. (It works but has to pay a bit larger cost.)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001325 * And when the page is SwapCache, it should take swap information
1326 * into account. This is under lock_page() now.
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001327 */
1328 if (!(gfp_mask & __GFP_WAIT)) {
1329 struct page_cgroup *pc;
1330
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001331
1332 pc = lookup_page_cgroup(page);
1333 if (!pc)
1334 return 0;
1335 lock_page_cgroup(pc);
1336 if (PageCgroupUsed(pc)) {
1337 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001338 return 0;
1339 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001340 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001341 }
1342
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001343 if (unlikely(!mm && !mem))
Balbir Singh8697d332008-02-07 00:13:59 -08001344 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001345
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -07001346 if (page_is_file_cache(page))
1347 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001348 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001349
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001350 /* shmem */
1351 if (PageSwapCache(page)) {
1352 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1353 if (!ret)
1354 __mem_cgroup_commit_charge_swapin(page, mem,
1355 MEM_CGROUP_CHARGE_TYPE_SHMEM);
1356 } else
1357 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
1358 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001359
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001360 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001361}
1362
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001363/*
1364 * While swap-in, try_charge -> commit or cancel, the page is locked.
1365 * And when try_charge() successfully returns, one refcnt to memcg without
1366 * struct page_cgroup is aquired. This refcnt will be cumsumed by
1367 * "commit()" or removed by "cancel()"
1368 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001369int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
1370 struct page *page,
1371 gfp_t mask, struct mem_cgroup **ptr)
1372{
1373 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001374 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001375
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001376 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001377 return 0;
1378
1379 if (!do_swap_account)
1380 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001381 /*
1382 * A racing thread's fault, or swapoff, may have already updated
1383 * the pte, and even removed page from swap cache: return success
1384 * to go on to do_swap_page()'s pte_same() test, which should fail.
1385 */
1386 if (!PageSwapCache(page))
1387 return 0;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001388 mem = try_get_mem_cgroup_from_swapcache(page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001389 if (!mem)
1390 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001391 *ptr = mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001392 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true);
1393 /* drop extra refcnt from tryget */
1394 css_put(&mem->css);
1395 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001396charge_cur_mm:
1397 if (unlikely(!mm))
1398 mm = &init_mm;
1399 return __mem_cgroup_try_charge(mm, mask, ptr, true);
1400}
1401
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001402static void
1403__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1404 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001405{
1406 struct page_cgroup *pc;
1407
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001408 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001409 return;
1410 if (!ptr)
1411 return;
1412 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001413 mem_cgroup_lru_del_before_commit_swapcache(page);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001414 __mem_cgroup_commit_charge(ptr, pc, ctype);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001415 mem_cgroup_lru_add_after_commit_swapcache(page);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001416 /*
1417 * Now swap is on-memory. This means this page may be
1418 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08001419 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
1420 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
1421 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001422 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08001423 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001424 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001425 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001426 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001427
1428 id = swap_cgroup_record(ent, 0);
1429 rcu_read_lock();
1430 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001431 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001432 /*
1433 * This recorded memcg can be obsolete one. So, avoid
1434 * calling css_tryget
1435 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001436 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1437 mem_cgroup_put(memcg);
1438 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001439 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001440 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001441 /* add this page(page_cgroup) to the LRU we want. */
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001442
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001443}
1444
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001445void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
1446{
1447 __mem_cgroup_commit_charge_swapin(page, ptr,
1448 MEM_CGROUP_CHARGE_TYPE_MAPPED);
1449}
1450
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001451void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
1452{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001453 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001454 return;
1455 if (!mem)
1456 return;
1457 res_counter_uncharge(&mem->res, PAGE_SIZE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001458 if (do_swap_account)
1459 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001460 css_put(&mem->css);
1461}
1462
1463
Balbir Singh8697d332008-02-07 00:13:59 -08001464/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001465 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001466 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001467static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001468__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001469{
Hugh Dickins82895462008-03-04 14:29:08 -08001470 struct page_cgroup *pc;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001471 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001472 struct mem_cgroup_per_zone *mz;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001473
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001474 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001475 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07001476
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001477 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001478 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001479
Balbir Singh8697d332008-02-07 00:13:59 -08001480 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08001481 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08001482 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001483 pc = lookup_page_cgroup(page);
1484 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001485 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001486
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001487 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001488
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001489 mem = pc->mem_cgroup;
1490
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001491 if (!PageCgroupUsed(pc))
1492 goto unlock_out;
1493
1494 switch (ctype) {
1495 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1496 if (page_mapped(page))
1497 goto unlock_out;
1498 break;
1499 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
1500 if (!PageAnon(page)) { /* Shared memory */
1501 if (page->mapping && !page_is_file_cache(page))
1502 goto unlock_out;
1503 } else if (page_mapped(page)) /* Anon */
1504 goto unlock_out;
1505 break;
1506 default:
1507 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001508 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001509
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001510 res_counter_uncharge(&mem->res, PAGE_SIZE);
1511 if (do_swap_account && (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT))
1512 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001513 mem_cgroup_charge_statistics(mem, pc, false);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001514
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001515 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001516 /*
1517 * pc->mem_cgroup is not cleared here. It will be accessed when it's
1518 * freed from LRU. This is safe because uncharged page is expected not
1519 * to be reused (freed soon). Exception is SwapCache, it's handled by
1520 * special functions.
1521 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08001522
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001523 mz = page_cgroup_zoneinfo(pc);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001524 unlock_page_cgroup(pc);
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08001525
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08001526 /* at swapout, this memcg will be accessed to record to swap */
1527 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1528 css_put(&mem->css);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001529
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001530 return mem;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001531
1532unlock_out:
1533 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001534 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08001535}
1536
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001537void mem_cgroup_uncharge_page(struct page *page)
1538{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001539 /* early check. */
1540 if (page_mapped(page))
1541 return;
1542 if (page->mapping && !PageAnon(page))
1543 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001544 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
1545}
1546
1547void mem_cgroup_uncharge_cache_page(struct page *page)
1548{
1549 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07001550 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001551 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
1552}
1553
Daisuke Nishimurae767e052009-05-28 14:34:28 -07001554#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001555/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07001556 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001557 * memcg information is recorded to swap_cgroup of "ent"
1558 */
1559void mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001560{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001561 struct mem_cgroup *memcg;
1562
1563 memcg = __mem_cgroup_uncharge_common(page,
1564 MEM_CGROUP_CHARGE_TYPE_SWAPOUT);
1565 /* record memcg information */
1566 if (do_swap_account && memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001567 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001568 mem_cgroup_get(memcg);
1569 }
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08001570 if (memcg)
1571 css_put(&memcg->css);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001572}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07001573#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001574
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001575#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1576/*
1577 * called from swap_entry_free(). remove record in swap_cgroup and
1578 * uncharge "memsw" account.
1579 */
1580void mem_cgroup_uncharge_swap(swp_entry_t ent)
1581{
1582 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001583 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001584
1585 if (!do_swap_account)
1586 return;
1587
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001588 id = swap_cgroup_record(ent, 0);
1589 rcu_read_lock();
1590 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001591 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001592 /*
1593 * We uncharge this because swap is freed.
1594 * This memcg can be obsolete one. We avoid calling css_tryget
1595 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001596 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1597 mem_cgroup_put(memcg);
1598 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001599 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001600}
1601#endif
1602
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001603/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001604 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
1605 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001606 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001607int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001608{
1609 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001610 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001611 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001612
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001613 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07001614 return 0;
1615
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001616 pc = lookup_page_cgroup(page);
1617 lock_page_cgroup(pc);
1618 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001619 mem = pc->mem_cgroup;
1620 css_get(&mem->css);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08001621 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001622 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001623
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001624 if (mem) {
Daisuke Nishimura3bb4edf2009-01-07 18:08:28 -08001625 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001626 css_put(&mem->css);
1627 }
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001628 *ptr = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001629 return ret;
1630}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08001631
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001632/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001633void mem_cgroup_end_migration(struct mem_cgroup *mem,
1634 struct page *oldpage, struct page *newpage)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001635{
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001636 struct page *target, *unused;
1637 struct page_cgroup *pc;
1638 enum charge_type ctype;
1639
1640 if (!mem)
1641 return;
1642
1643 /* at migration success, oldpage->mapping is NULL. */
1644 if (oldpage->mapping) {
1645 target = oldpage;
1646 unused = NULL;
1647 } else {
1648 target = newpage;
1649 unused = oldpage;
1650 }
1651
1652 if (PageAnon(target))
1653 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
1654 else if (page_is_file_cache(target))
1655 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
1656 else
1657 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
1658
1659 /* unused page is not on radix-tree now. */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001660 if (unused)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001661 __mem_cgroup_uncharge_common(unused, ctype);
1662
1663 pc = lookup_page_cgroup(target);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001664 /*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001665 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
1666 * So, double-counting is effectively avoided.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001667 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001668 __mem_cgroup_commit_charge(mem, pc, ctype);
1669
1670 /*
1671 * Both of oldpage and newpage are still under lock_page().
1672 * Then, we don't have to care about race in radix-tree.
1673 * But we have to be careful that this page is unmapped or not.
1674 *
1675 * There is a case for !page_mapped(). At the start of
1676 * migration, oldpage was mapped. But now, it's zapped.
1677 * But we know *target* page is not freed/reused under us.
1678 * mem_cgroup_uncharge_page() does all necessary checks.
1679 */
1680 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
1681 mem_cgroup_uncharge_page(target);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001682}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001683
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001684/*
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07001685 * A call to try to shrink memory usage on charge failure at shmem's swapin.
1686 * Calling hierarchical_reclaim is not enough because we should update
1687 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
1688 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
1689 * not from the memcg which this page would be charged to.
1690 * try_charge_swapin does all of these works properly.
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001691 */
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07001692int mem_cgroup_shmem_charge_fallback(struct page *page,
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001693 struct mm_struct *mm,
1694 gfp_t gfp_mask)
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001695{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001696 struct mem_cgroup *mem = NULL;
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07001697 int ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001698
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001699 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001700 return 0;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001701
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07001702 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1703 if (!ret)
1704 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001705
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07001706 return ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001707}
1708
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001709static DEFINE_MUTEX(set_limit_mutex);
1710
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08001711static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001712 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001713{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001714 int retry_count;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001715 int progress;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001716 u64 memswlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001717 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001718 int children = mem_cgroup_count_children(memcg);
1719 u64 curusage, oldusage;
1720
1721 /*
1722 * For keeping hierarchical_reclaim simple, how long we should retry
1723 * is depends on callers. We set our retry-count to be function
1724 * of # of children which we should visit in this loop.
1725 */
1726 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
1727
1728 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001729
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001730 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001731 if (signal_pending(current)) {
1732 ret = -EINTR;
1733 break;
1734 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001735 /*
1736 * Rather than hide all in some function, I do this in
1737 * open coded manner. You see what this really does.
1738 * We have to guarantee mem->res.limit < mem->memsw.limit.
1739 */
1740 mutex_lock(&set_limit_mutex);
1741 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1742 if (memswlimit < val) {
1743 ret = -EINVAL;
1744 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001745 break;
1746 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001747 ret = res_counter_set_limit(&memcg->res, val);
1748 mutex_unlock(&set_limit_mutex);
1749
1750 if (!ret)
1751 break;
1752
Daisuke Nishimura42e9abb2009-01-07 18:08:30 -08001753 progress = mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL,
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001754 false, true);
1755 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
1756 /* Usage is reduced ? */
1757 if (curusage >= oldusage)
1758 retry_count--;
1759 else
1760 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001761 }
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001762
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001763 return ret;
1764}
1765
Li Zefan338c8432009-06-17 16:27:15 -07001766static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
1767 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001768{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001769 int retry_count;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001770 u64 memlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001771 int children = mem_cgroup_count_children(memcg);
1772 int ret = -EBUSY;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001773
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001774 /* see mem_cgroup_resize_res_limit */
1775 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
1776 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001777 while (retry_count) {
1778 if (signal_pending(current)) {
1779 ret = -EINTR;
1780 break;
1781 }
1782 /*
1783 * Rather than hide all in some function, I do this in
1784 * open coded manner. You see what this really does.
1785 * We have to guarantee mem->res.limit < mem->memsw.limit.
1786 */
1787 mutex_lock(&set_limit_mutex);
1788 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
1789 if (memlimit > val) {
1790 ret = -EINVAL;
1791 mutex_unlock(&set_limit_mutex);
1792 break;
1793 }
1794 ret = res_counter_set_limit(&memcg->memsw, val);
1795 mutex_unlock(&set_limit_mutex);
1796
1797 if (!ret)
1798 break;
1799
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001800 mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL, true, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001801 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001802 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001803 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001804 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001805 else
1806 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001807 }
1808 return ret;
1809}
1810
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001811/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001812 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001813 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
1814 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001815static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001816 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001817{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001818 struct zone *zone;
1819 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001820 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001821 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001822 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001823 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001824
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001825 zone = &NODE_DATA(node)->node_zones[zid];
1826 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001827 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001828
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001829 loop = MEM_CGROUP_ZSTAT(mz, lru);
1830 /* give some margin against EBUSY etc...*/
1831 loop += 256;
1832 busy = NULL;
1833 while (loop--) {
1834 ret = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001835 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001836 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001837 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001838 break;
1839 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001840 pc = list_entry(list->prev, struct page_cgroup, lru);
1841 if (busy == pc) {
1842 list_move(&pc->lru, list);
1843 busy = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001844 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001845 continue;
1846 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001847 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001848
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -08001849 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001850 if (ret == -ENOMEM)
1851 break;
1852
1853 if (ret == -EBUSY || ret == -EINVAL) {
1854 /* found lock contention or "pc" is obsolete. */
1855 busy = pc;
1856 cond_resched();
1857 } else
1858 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001859 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001860
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001861 if (!ret && !list_empty(list))
1862 return -EBUSY;
1863 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001864}
1865
1866/*
1867 * make mem_cgroup's charge to be 0 if there is no task.
1868 * This enables deleting this mem_cgroup.
1869 */
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001870static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001871{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001872 int ret;
1873 int node, zid, shrink;
1874 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001875 struct cgroup *cgrp = mem->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001876
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001877 css_get(&mem->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001878
1879 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001880 /* should free all ? */
1881 if (free_all)
1882 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001883move_account:
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001884 while (mem->res.usage > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001885 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001886 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001887 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001888 ret = -EINTR;
1889 if (signal_pending(current))
1890 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001891 /* This is for making all *used* pages to be on LRU. */
1892 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001893 ret = 0;
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08001894 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001895 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07001896 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001897 for_each_lru(l) {
1898 ret = mem_cgroup_force_empty_list(mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001899 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001900 if (ret)
1901 break;
1902 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001903 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001904 if (ret)
1905 break;
1906 }
1907 /* it seems parent cgroup doesn't have enough mem */
1908 if (ret == -ENOMEM)
1909 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001910 cond_resched();
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001911 }
1912 ret = 0;
1913out:
1914 css_put(&mem->css);
1915 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001916
1917try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001918 /* returns EBUSY if there is a task or if we come here twice. */
1919 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001920 ret = -EBUSY;
1921 goto out;
1922 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001923 /* we call try-to-free pages for make this cgroup empty */
1924 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001925 /* try to free all pages in this cgroup */
1926 shrink = 1;
1927 while (nr_retries && mem->res.usage > 0) {
1928 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001929
1930 if (signal_pending(current)) {
1931 ret = -EINTR;
1932 goto out;
1933 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001934 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
1935 false, get_swappiness(mem));
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001936 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001937 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001938 /* maybe some writeback is necessary */
1939 congestion_wait(WRITE, HZ/10);
1940 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001941
1942 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001943 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001944 /* try move_account...there may be some *locked* pages. */
1945 if (mem->res.usage)
1946 goto move_account;
1947 ret = 0;
1948 goto out;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001949}
1950
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001951int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
1952{
1953 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
1954}
1955
1956
Balbir Singh18f59ea2009-01-07 18:08:07 -08001957static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
1958{
1959 return mem_cgroup_from_cont(cont)->use_hierarchy;
1960}
1961
1962static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
1963 u64 val)
1964{
1965 int retval = 0;
1966 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1967 struct cgroup *parent = cont->parent;
1968 struct mem_cgroup *parent_mem = NULL;
1969
1970 if (parent)
1971 parent_mem = mem_cgroup_from_cont(parent);
1972
1973 cgroup_lock();
1974 /*
1975 * If parent's use_hiearchy is set, we can't make any modifications
1976 * in the child subtrees. If it is unset, then the change can
1977 * occur, provided the current cgroup has no children.
1978 *
1979 * For the root cgroup, parent_mem is NULL, we allow value to be
1980 * set if there are no children.
1981 */
1982 if ((!parent_mem || !parent_mem->use_hierarchy) &&
1983 (val == 1 || val == 0)) {
1984 if (list_empty(&cont->children))
1985 mem->use_hierarchy = val;
1986 else
1987 retval = -EBUSY;
1988 } else
1989 retval = -EINVAL;
1990 cgroup_unlock();
1991
1992 return retval;
1993}
1994
Paul Menage2c3daa72008-04-29 00:59:58 -07001995static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001996{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001997 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1998 u64 val = 0;
1999 int type, name;
2000
2001 type = MEMFILE_TYPE(cft->private);
2002 name = MEMFILE_ATTR(cft->private);
2003 switch (type) {
2004 case _MEM:
2005 val = res_counter_read_u64(&mem->res, name);
2006 break;
2007 case _MEMSWAP:
Li Zefan338c8432009-06-17 16:27:15 -07002008 val = res_counter_read_u64(&mem->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002009 break;
2010 default:
2011 BUG();
2012 break;
2013 }
2014 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002015}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002016/*
2017 * The user of this function is...
2018 * RES_LIMIT.
2019 */
Paul Menage856c13a2008-07-25 01:47:04 -07002020static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
2021 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002022{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002023 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002024 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002025 unsigned long long val;
2026 int ret;
2027
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002028 type = MEMFILE_TYPE(cft->private);
2029 name = MEMFILE_ATTR(cft->private);
2030 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002031 case RES_LIMIT:
2032 /* This function does all necessary parse...reuse it */
2033 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002034 if (ret)
2035 break;
2036 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002037 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002038 else
2039 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002040 break;
2041 default:
2042 ret = -EINVAL; /* should be BUG() ? */
2043 break;
2044 }
2045 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002046}
2047
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08002048static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
2049 unsigned long long *mem_limit, unsigned long long *memsw_limit)
2050{
2051 struct cgroup *cgroup;
2052 unsigned long long min_limit, min_memsw_limit, tmp;
2053
2054 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2055 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2056 cgroup = memcg->css.cgroup;
2057 if (!memcg->use_hierarchy)
2058 goto out;
2059
2060 while (cgroup->parent) {
2061 cgroup = cgroup->parent;
2062 memcg = mem_cgroup_from_cont(cgroup);
2063 if (!memcg->use_hierarchy)
2064 break;
2065 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
2066 min_limit = min(min_limit, tmp);
2067 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2068 min_memsw_limit = min(min_memsw_limit, tmp);
2069 }
2070out:
2071 *mem_limit = min_limit;
2072 *memsw_limit = min_memsw_limit;
2073 return;
2074}
2075
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002076static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002077{
2078 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002079 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002080
2081 mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002082 type = MEMFILE_TYPE(event);
2083 name = MEMFILE_ATTR(event);
2084 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002085 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002086 if (type == _MEM)
2087 res_counter_reset_max(&mem->res);
2088 else
2089 res_counter_reset_max(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002090 break;
2091 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002092 if (type == _MEM)
2093 res_counter_reset_failcnt(&mem->res);
2094 else
2095 res_counter_reset_failcnt(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002096 break;
2097 }
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07002098 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002099}
2100
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002101
2102/* For read statistics */
2103enum {
2104 MCS_CACHE,
2105 MCS_RSS,
Balbir Singhd69b0422009-06-17 16:26:34 -07002106 MCS_MAPPED_FILE,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002107 MCS_PGPGIN,
2108 MCS_PGPGOUT,
2109 MCS_INACTIVE_ANON,
2110 MCS_ACTIVE_ANON,
2111 MCS_INACTIVE_FILE,
2112 MCS_ACTIVE_FILE,
2113 MCS_UNEVICTABLE,
2114 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002115};
2116
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002117struct mcs_total_stat {
2118 s64 stat[NR_MCS_STAT];
2119};
2120
2121struct {
2122 char *local_name;
2123 char *total_name;
2124} memcg_stat_strings[NR_MCS_STAT] = {
2125 {"cache", "total_cache"},
2126 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07002127 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002128 {"pgpgin", "total_pgpgin"},
2129 {"pgpgout", "total_pgpgout"},
2130 {"inactive_anon", "total_inactive_anon"},
2131 {"active_anon", "total_active_anon"},
2132 {"inactive_file", "total_inactive_file"},
2133 {"active_file", "total_active_file"},
2134 {"unevictable", "total_unevictable"}
2135};
2136
2137
2138static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
2139{
2140 struct mcs_total_stat *s = data;
2141 s64 val;
2142
2143 /* per cpu stat */
2144 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE);
2145 s->stat[MCS_CACHE] += val * PAGE_SIZE;
2146 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
2147 s->stat[MCS_RSS] += val * PAGE_SIZE;
Balbir Singhd69b0422009-06-17 16:26:34 -07002148 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_MAPPED_FILE);
2149 s->stat[MCS_MAPPED_FILE] += val * PAGE_SIZE;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002150 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT);
2151 s->stat[MCS_PGPGIN] += val;
2152 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT);
2153 s->stat[MCS_PGPGOUT] += val;
2154
2155 /* per zone stat */
2156 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
2157 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
2158 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
2159 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
2160 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
2161 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
2162 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
2163 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
2164 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
2165 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
2166 return 0;
2167}
2168
2169static void
2170mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
2171{
2172 mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
2173}
2174
Paul Menagec64745c2008-04-29 01:00:02 -07002175static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
2176 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002177{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002178 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002179 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002180 int i;
2181
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002182 memset(&mystat, 0, sizeof(mystat));
2183 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002184
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002185 for (i = 0; i < NR_MCS_STAT; i++)
2186 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002187
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002188 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08002189 {
2190 unsigned long long limit, memsw_limit;
2191 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
2192 cb->fill(cb, "hierarchical_memory_limit", limit);
2193 if (do_swap_account)
2194 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
2195 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002196
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002197 memset(&mystat, 0, sizeof(mystat));
2198 mem_cgroup_get_total_stat(mem_cont, &mystat);
2199 for (i = 0; i < NR_MCS_STAT; i++)
2200 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
2201
2202
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002203#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08002204 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002205
2206 {
2207 int nid, zid;
2208 struct mem_cgroup_per_zone *mz;
2209 unsigned long recent_rotated[2] = {0, 0};
2210 unsigned long recent_scanned[2] = {0, 0};
2211
2212 for_each_online_node(nid)
2213 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
2214 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
2215
2216 recent_rotated[0] +=
2217 mz->reclaim_stat.recent_rotated[0];
2218 recent_rotated[1] +=
2219 mz->reclaim_stat.recent_rotated[1];
2220 recent_scanned[0] +=
2221 mz->reclaim_stat.recent_scanned[0];
2222 recent_scanned[1] +=
2223 mz->reclaim_stat.recent_scanned[1];
2224 }
2225 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
2226 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
2227 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
2228 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
2229 }
2230#endif
2231
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002232 return 0;
2233}
2234
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002235static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
2236{
2237 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2238
2239 return get_swappiness(memcg);
2240}
2241
2242static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
2243 u64 val)
2244{
2245 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2246 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08002247
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002248 if (val > 100)
2249 return -EINVAL;
2250
2251 if (cgrp->parent == NULL)
2252 return -EINVAL;
2253
2254 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08002255
2256 cgroup_lock();
2257
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002258 /* If under hierarchy, only empty-root can set this value */
2259 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08002260 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
2261 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002262 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08002263 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002264
2265 spin_lock(&memcg->reclaim_param_lock);
2266 memcg->swappiness = val;
2267 spin_unlock(&memcg->reclaim_param_lock);
2268
Li Zefan068b38c2009-01-15 13:51:26 -08002269 cgroup_unlock();
2270
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002271 return 0;
2272}
2273
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002274
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002275static struct cftype mem_cgroup_files[] = {
2276 {
Balbir Singh0eea1032008-02-07 00:13:57 -08002277 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002278 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07002279 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002280 },
2281 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002282 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002283 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002284 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002285 .read_u64 = mem_cgroup_read,
2286 },
2287 {
Balbir Singh0eea1032008-02-07 00:13:57 -08002288 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002289 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07002290 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07002291 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002292 },
2293 {
2294 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002295 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002296 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07002297 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002298 },
Balbir Singh8697d332008-02-07 00:13:59 -08002299 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002300 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07002301 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002302 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002303 {
2304 .name = "force_empty",
2305 .trigger = mem_cgroup_force_empty_write,
2306 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08002307 {
2308 .name = "use_hierarchy",
2309 .write_u64 = mem_cgroup_hierarchy_write,
2310 .read_u64 = mem_cgroup_hierarchy_read,
2311 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002312 {
2313 .name = "swappiness",
2314 .read_u64 = mem_cgroup_swappiness_read,
2315 .write_u64 = mem_cgroup_swappiness_write,
2316 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002317};
2318
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002319#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2320static struct cftype memsw_cgroup_files[] = {
2321 {
2322 .name = "memsw.usage_in_bytes",
2323 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
2324 .read_u64 = mem_cgroup_read,
2325 },
2326 {
2327 .name = "memsw.max_usage_in_bytes",
2328 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
2329 .trigger = mem_cgroup_reset,
2330 .read_u64 = mem_cgroup_read,
2331 },
2332 {
2333 .name = "memsw.limit_in_bytes",
2334 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
2335 .write_string = mem_cgroup_write,
2336 .read_u64 = mem_cgroup_read,
2337 },
2338 {
2339 .name = "memsw.failcnt",
2340 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
2341 .trigger = mem_cgroup_reset,
2342 .read_u64 = mem_cgroup_read,
2343 },
2344};
2345
2346static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2347{
2348 if (!do_swap_account)
2349 return 0;
2350 return cgroup_add_files(cont, ss, memsw_cgroup_files,
2351 ARRAY_SIZE(memsw_cgroup_files));
2352};
2353#else
2354static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2355{
2356 return 0;
2357}
2358#endif
2359
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002360static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2361{
2362 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002363 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07002364 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07002365 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002366 /*
2367 * This routine is called against possible nodes.
2368 * But it's BUG to call kmalloc() against offline node.
2369 *
2370 * TODO: this routine can waste much memory for nodes which will
2371 * never be onlined. It's better to use memory hotplug callback
2372 * function.
2373 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07002374 if (!node_state(node, N_NORMAL_MEMORY))
2375 tmp = -1;
2376 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002377 if (!pn)
2378 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002379
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002380 mem->info.nodeinfo[node] = pn;
2381 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002382
2383 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
2384 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07002385 for_each_lru(l)
2386 INIT_LIST_HEAD(&mz->lists[l]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002387 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002388 return 0;
2389}
2390
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002391static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2392{
2393 kfree(mem->info.nodeinfo[node]);
2394}
2395
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002396static int mem_cgroup_size(void)
2397{
2398 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
2399 return sizeof(struct mem_cgroup) + cpustat_size;
2400}
2401
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002402static struct mem_cgroup *mem_cgroup_alloc(void)
2403{
2404 struct mem_cgroup *mem;
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002405 int size = mem_cgroup_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002406
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002407 if (size < PAGE_SIZE)
2408 mem = kmalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002409 else
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002410 mem = vmalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002411
2412 if (mem)
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002413 memset(mem, 0, size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002414 return mem;
2415}
2416
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002417/*
2418 * At destroying mem_cgroup, references from swap_cgroup can remain.
2419 * (scanning all at force_empty is too costly...)
2420 *
2421 * Instead of clearing all references at force_empty, we remember
2422 * the number of reference from swap_cgroup and free mem_cgroup when
2423 * it goes down to 0.
2424 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002425 * Removal of cgroup itself succeeds regardless of refs from swap.
2426 */
2427
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08002428static void __mem_cgroup_free(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002429{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002430 int node;
2431
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002432 free_css_id(&mem_cgroup_subsys, &mem->css);
2433
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002434 for_each_node_state(node, N_POSSIBLE)
2435 free_mem_cgroup_per_zone_info(mem, node);
2436
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002437 if (mem_cgroup_size() < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002438 kfree(mem);
2439 else
2440 vfree(mem);
2441}
2442
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002443static void mem_cgroup_get(struct mem_cgroup *mem)
2444{
2445 atomic_inc(&mem->refcnt);
2446}
2447
2448static void mem_cgroup_put(struct mem_cgroup *mem)
2449{
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08002450 if (atomic_dec_and_test(&mem->refcnt)) {
2451 struct mem_cgroup *parent = parent_mem_cgroup(mem);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08002452 __mem_cgroup_free(mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08002453 if (parent)
2454 mem_cgroup_put(parent);
2455 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002456}
2457
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08002458/*
2459 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
2460 */
2461static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
2462{
2463 if (!mem->res.parent)
2464 return NULL;
2465 return mem_cgroup_from_res_counter(mem->res.parent, res);
2466}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002467
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08002468#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2469static void __init enable_swap_cgroup(void)
2470{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002471 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08002472 do_swap_account = 1;
2473}
2474#else
2475static void __init enable_swap_cgroup(void)
2476{
2477}
2478#endif
2479
Li Zefan0eb253e2009-01-15 13:51:25 -08002480static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002481mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
2482{
Balbir Singh28dbc4b2009-01-07 18:08:05 -08002483 struct mem_cgroup *mem, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002484 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002485 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002486
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002487 mem = mem_cgroup_alloc();
2488 if (!mem)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002489 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08002490
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002491 for_each_node_state(node, N_POSSIBLE)
2492 if (alloc_mem_cgroup_per_zone_info(mem, node))
2493 goto free_out;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08002494 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08002495 if (cont->parent == NULL) {
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08002496 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08002497 parent = NULL;
Balbir Singh18f59ea2009-01-07 18:08:07 -08002498 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08002499 parent = mem_cgroup_from_cont(cont->parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08002500 mem->use_hierarchy = parent->use_hierarchy;
2501 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08002502
Balbir Singh18f59ea2009-01-07 18:08:07 -08002503 if (parent && parent->use_hierarchy) {
2504 res_counter_init(&mem->res, &parent->res);
2505 res_counter_init(&mem->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08002506 /*
2507 * We increment refcnt of the parent to ensure that we can
2508 * safely access it on res_counter_charge/uncharge.
2509 * This refcnt will be decremented when freeing this
2510 * mem_cgroup(see mem_cgroup_put).
2511 */
2512 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08002513 } else {
2514 res_counter_init(&mem->res, NULL);
2515 res_counter_init(&mem->memsw, NULL);
2516 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002517 mem->last_scanned_child = 0;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -08002518 spin_lock_init(&mem->reclaim_param_lock);
Balbir Singh6d61ef42009-01-07 18:08:06 -08002519
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002520 if (parent)
2521 mem->swappiness = get_swappiness(parent);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08002522 atomic_set(&mem->refcnt, 1);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002523 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002524free_out:
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08002525 __mem_cgroup_free(mem);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002526 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002527}
2528
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002529static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08002530 struct cgroup *cont)
2531{
2532 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002533
2534 return mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08002535}
2536
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002537static void mem_cgroup_destroy(struct cgroup_subsys *ss,
2538 struct cgroup *cont)
2539{
Daisuke Nishimurac268e992009-01-15 13:51:13 -08002540 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08002541
Daisuke Nishimurac268e992009-01-15 13:51:13 -08002542 mem_cgroup_put(mem);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002543}
2544
2545static int mem_cgroup_populate(struct cgroup_subsys *ss,
2546 struct cgroup *cont)
2547{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002548 int ret;
2549
2550 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
2551 ARRAY_SIZE(mem_cgroup_files));
2552
2553 if (!ret)
2554 ret = register_memsw_files(cont, ss);
2555 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002556}
2557
Balbir Singh67e465a2008-02-07 00:13:54 -08002558static void mem_cgroup_move_task(struct cgroup_subsys *ss,
2559 struct cgroup *cont,
2560 struct cgroup *old_cont,
2561 struct task_struct *p)
2562{
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08002563 mutex_lock(&memcg_tasklist);
Balbir Singh67e465a2008-02-07 00:13:54 -08002564 /*
Nikanth Karthikesanf9717d22009-01-07 18:08:11 -08002565 * FIXME: It's better to move charges of this process from old
2566 * memcg to new memcg. But it's just on TODO-List now.
Balbir Singh67e465a2008-02-07 00:13:54 -08002567 */
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08002568 mutex_unlock(&memcg_tasklist);
Balbir Singh67e465a2008-02-07 00:13:54 -08002569}
2570
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002571struct cgroup_subsys mem_cgroup_subsys = {
2572 .name = "memory",
2573 .subsys_id = mem_cgroup_subsys_id,
2574 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08002575 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002576 .destroy = mem_cgroup_destroy,
2577 .populate = mem_cgroup_populate,
Balbir Singh67e465a2008-02-07 00:13:54 -08002578 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002579 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002580 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002581};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08002582
2583#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2584
2585static int __init disable_swap_account(char *s)
2586{
2587 really_do_swap_account = 0;
2588 return 1;
2589}
2590__setup("noswapaccount", disable_swap_account);
2591#endif