blob: 6ceb6f2dbac2ba00366fa9f0184186708a71d1d4 [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 Hiroyuki22a668d2009-06-17 16:27:19 -0700180 /* set when res.limit == memsw.limit */
181 bool memsw_is_minimum;
182
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800183 /*
Jan Blunckc8dad2b2009-01-07 18:07:53 -0800184 * statistics. This must be placed at the end of memcg.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800185 */
186 struct mem_cgroup_stat stat;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800187};
188
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800189enum charge_type {
190 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
191 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700192 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700193 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800194 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -0700195 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700196 NR_CHARGE_TYPE,
197};
198
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700199/* only for here (for easy reading.) */
200#define PCGF_CACHE (1UL << PCG_CACHE)
201#define PCGF_USED (1UL << PCG_USED)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700202#define PCGF_LOCK (1UL << PCG_LOCK)
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700203static const unsigned long
204pcg_default_flags[NR_CHARGE_TYPE] = {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800205 PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* File Cache */
206 PCGF_USED | PCGF_LOCK, /* Anon */
207 PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* Shmem */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700208 0, /* FORCE */
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800209};
210
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800211/* for encoding cft->private value on file */
212#define _MEM (0)
213#define _MEMSWAP (1)
214#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
215#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
216#define MEMFILE_ATTR(val) ((val) & 0xffff)
217
218static void mem_cgroup_get(struct mem_cgroup *mem);
219static void mem_cgroup_put(struct mem_cgroup *mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -0800220static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800221
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700222static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
223 struct page_cgroup *pc,
224 bool charge)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800225{
226 int val = (charge)? 1 : -1;
227 struct mem_cgroup_stat *stat = &mem->stat;
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700228 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800229 int cpu = get_cpu();
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800230
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800231 cpustat = &stat->cpustat[cpu];
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700232 if (PageCgroupCache(pc))
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700233 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800234 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700235 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700236
237 if (charge)
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700238 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700239 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
240 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700241 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700242 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800243 put_cpu();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800244}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800245
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800246static struct mem_cgroup_per_zone *
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800247mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
248{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800249 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
250}
251
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800252static struct mem_cgroup_per_zone *
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800253page_cgroup_zoneinfo(struct page_cgroup *pc)
254{
255 struct mem_cgroup *mem = pc->mem_cgroup;
256 int nid = page_cgroup_nid(pc);
257 int zid = page_cgroup_zid(pc);
258
KOSAKI Motohiro54992762009-01-07 18:08:18 -0800259 if (!mem)
260 return NULL;
261
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800262 return mem_cgroup_zoneinfo(mem, nid, zid);
263}
264
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700265static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700266 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800267{
268 int nid, zid;
269 struct mem_cgroup_per_zone *mz;
270 u64 total = 0;
271
272 for_each_online_node(nid)
273 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
274 mz = mem_cgroup_zoneinfo(mem, nid, zid);
275 total += MEM_CGROUP_ZSTAT(mz, idx);
276 }
277 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800278}
279
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800280static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800281{
282 return container_of(cgroup_subsys_state(cont,
283 mem_cgroup_subsys_id), struct mem_cgroup,
284 css);
285}
286
Balbir Singhcf475ad2008-04-29 01:00:16 -0700287struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800288{
Balbir Singh31a78f22008-09-28 23:09:31 +0100289 /*
290 * mm_update_next_owner() may clear mm->owner to NULL
291 * if it races with swapoff, page migration, etc.
292 * So this can be called with p == NULL.
293 */
294 if (unlikely(!p))
295 return NULL;
296
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800297 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
298 struct mem_cgroup, css);
299}
300
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800301static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
302{
303 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700304
305 if (!mm)
306 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800307 /*
308 * Because we have no locks, mm->owner's may be being moved to other
309 * cgroup. We use css_tryget() here even if this looks
310 * pessimistic (rather than adding locks here).
311 */
312 rcu_read_lock();
313 do {
314 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
315 if (unlikely(!mem))
316 break;
317 } while (!css_tryget(&mem->css));
318 rcu_read_unlock();
319 return mem;
320}
321
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700322/*
323 * Call callback function against all cgroup under hierarchy tree.
324 */
325static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
326 int (*func)(struct mem_cgroup *, void *))
327{
328 int found, ret, nextid;
329 struct cgroup_subsys_state *css;
330 struct mem_cgroup *mem;
331
332 if (!root->use_hierarchy)
333 return (*func)(root, data);
334
335 nextid = 1;
336 do {
337 ret = 0;
338 mem = NULL;
339
340 rcu_read_lock();
341 css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
342 &found);
343 if (css && css_tryget(css))
344 mem = container_of(css, struct mem_cgroup, css);
345 rcu_read_unlock();
346
347 if (mem) {
348 ret = (*func)(mem, data);
349 css_put(&mem->css);
350 }
351 nextid = found + 1;
352 } while (!ret && css);
353
354 return ret;
355}
356
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800357/*
358 * Following LRU functions are allowed to be used without PCG_LOCK.
359 * Operations are called by routine of global LRU independently from memcg.
360 * What we have to take care of here is validness of pc->mem_cgroup.
361 *
362 * Changes to pc->mem_cgroup happens when
363 * 1. charge
364 * 2. moving account
365 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
366 * It is added to LRU before charge.
367 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
368 * When moving account, the page is not on LRU. It's isolated.
369 */
370
371void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800372{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800373 struct page_cgroup *pc;
374 struct mem_cgroup *mem;
375 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700376
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800377 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800378 return;
379 pc = lookup_page_cgroup(page);
380 /* can happen while we handle swapcache. */
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800381 if (list_empty(&pc->lru) || !pc->mem_cgroup)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800382 return;
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800383 /*
384 * We don't check PCG_USED bit. It's cleared when the "page" is finally
385 * removed from global LRU.
386 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800387 mz = page_cgroup_zoneinfo(pc);
388 mem = pc->mem_cgroup;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700389 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800390 list_del_init(&pc->lru);
391 return;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800392}
393
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800394void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800395{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800396 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800397}
398
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800399void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800400{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800401 struct mem_cgroup_per_zone *mz;
402 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800403
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800404 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700405 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700406
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800407 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800408 /*
409 * Used bit is set without atomic ops but after smp_wmb().
410 * For making pc->mem_cgroup visible, insert smp_rmb() here.
411 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800412 smp_rmb();
413 /* unused page is not rotated. */
414 if (!PageCgroupUsed(pc))
415 return;
416 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700417 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800418}
419
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800420void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
421{
422 struct page_cgroup *pc;
423 struct mem_cgroup_per_zone *mz;
424
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800425 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800426 return;
427 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800428 /*
429 * Used bit is set without atomic ops but after smp_wmb().
430 * For making pc->mem_cgroup visible, insert smp_rmb() here.
431 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800432 smp_rmb();
433 if (!PageCgroupUsed(pc))
434 return;
435
436 mz = page_cgroup_zoneinfo(pc);
437 MEM_CGROUP_ZSTAT(mz, lru) += 1;
438 list_add(&pc->lru, &mz->lists[lru]);
439}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800440
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800441/*
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800442 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
443 * lru because the page may.be reused after it's fully uncharged (because of
444 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
445 * it again. This function is only used to charge SwapCache. It's done under
446 * lock_page and expected that zone->lru_lock is never held.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800447 */
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800448static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800449{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800450 unsigned long flags;
451 struct zone *zone = page_zone(page);
452 struct page_cgroup *pc = lookup_page_cgroup(page);
453
454 spin_lock_irqsave(&zone->lru_lock, flags);
455 /*
456 * Forget old LRU when this page_cgroup is *not* used. This Used bit
457 * is guarded by lock_page() because the page is SwapCache.
458 */
459 if (!PageCgroupUsed(pc))
460 mem_cgroup_del_lru_list(page, page_lru(page));
461 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800462}
463
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800464static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
465{
466 unsigned long flags;
467 struct zone *zone = page_zone(page);
468 struct page_cgroup *pc = lookup_page_cgroup(page);
469
470 spin_lock_irqsave(&zone->lru_lock, flags);
471 /* link when the page is linked to LRU but page_cgroup isn't */
472 if (PageLRU(page) && list_empty(&pc->lru))
473 mem_cgroup_add_lru_list(page, page_lru(page));
474 spin_unlock_irqrestore(&zone->lru_lock, flags);
475}
476
477
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800478void mem_cgroup_move_lists(struct page *page,
479 enum lru_list from, enum lru_list to)
480{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800481 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800482 return;
483 mem_cgroup_del_lru_list(page, from);
484 mem_cgroup_add_lru_list(page, to);
485}
486
David Rientjes4c4a2212008-02-07 00:14:06 -0800487int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
488{
489 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700490 struct mem_cgroup *curr = NULL;
David Rientjes4c4a2212008-02-07 00:14:06 -0800491
492 task_lock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700493 rcu_read_lock();
494 curr = try_get_mem_cgroup_from_mm(task->mm);
495 rcu_read_unlock();
David Rientjes4c4a2212008-02-07 00:14:06 -0800496 task_unlock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700497 if (!curr)
498 return 0;
499 if (curr->use_hierarchy)
500 ret = css_is_ancestor(&curr->css, &mem->css);
501 else
502 ret = (curr == mem);
503 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -0800504 return ret;
505}
506
Balbir Singh66e17072008-02-07 00:13:56 -0800507/*
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800508 * prev_priority control...this will be used in memory reclaim path.
509 */
510int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
511{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800512 int prev_priority;
513
514 spin_lock(&mem->reclaim_param_lock);
515 prev_priority = mem->prev_priority;
516 spin_unlock(&mem->reclaim_param_lock);
517
518 return prev_priority;
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800519}
520
521void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
522{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800523 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800524 if (priority < mem->prev_priority)
525 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800526 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800527}
528
529void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
530{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800531 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800532 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800533 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800534}
535
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800536static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800537{
538 unsigned long active;
539 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800540 unsigned long gb;
541 unsigned long inactive_ratio;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800542
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700543 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
544 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800545
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800546 gb = (inactive + active) >> (30 - PAGE_SHIFT);
547 if (gb)
548 inactive_ratio = int_sqrt(10 * gb);
549 else
550 inactive_ratio = 1;
551
552 if (present_pages) {
553 present_pages[0] = inactive;
554 present_pages[1] = active;
555 }
556
557 return inactive_ratio;
558}
559
560int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
561{
562 unsigned long active;
563 unsigned long inactive;
564 unsigned long present_pages[2];
565 unsigned long inactive_ratio;
566
567 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
568
569 inactive = present_pages[0];
570 active = present_pages[1];
571
572 if (inactive * inactive_ratio < active)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800573 return 1;
574
575 return 0;
576}
577
Rik van Riel56e49d22009-06-16 15:32:28 -0700578int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
579{
580 unsigned long active;
581 unsigned long inactive;
582
583 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
584 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
585
586 return (active > inactive);
587}
588
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800589unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
590 struct zone *zone,
591 enum lru_list lru)
592{
593 int nid = zone->zone_pgdat->node_id;
594 int zid = zone_idx(zone);
595 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
596
597 return MEM_CGROUP_ZSTAT(mz, lru);
598}
599
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800600struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
601 struct zone *zone)
602{
603 int nid = zone->zone_pgdat->node_id;
604 int zid = zone_idx(zone);
605 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
606
607 return &mz->reclaim_stat;
608}
609
610struct zone_reclaim_stat *
611mem_cgroup_get_reclaim_stat_from_page(struct page *page)
612{
613 struct page_cgroup *pc;
614 struct mem_cgroup_per_zone *mz;
615
616 if (mem_cgroup_disabled())
617 return NULL;
618
619 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800620 /*
621 * Used bit is set without atomic ops but after smp_wmb().
622 * For making pc->mem_cgroup visible, insert smp_rmb() here.
623 */
624 smp_rmb();
625 if (!PageCgroupUsed(pc))
626 return NULL;
627
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800628 mz = page_cgroup_zoneinfo(pc);
629 if (!mz)
630 return NULL;
631
632 return &mz->reclaim_stat;
633}
634
Balbir Singh66e17072008-02-07 00:13:56 -0800635unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
636 struct list_head *dst,
637 unsigned long *scanned, int order,
638 int mode, struct zone *z,
639 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700640 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -0800641{
642 unsigned long nr_taken = 0;
643 struct page *page;
644 unsigned long scan;
645 LIST_HEAD(pc_list);
646 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800647 struct page_cgroup *pc, *tmp;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800648 int nid = z->zone_pgdat->node_id;
649 int zid = zone_idx(z);
650 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700651 int lru = LRU_FILE * !!file + !!active;
Balbir Singh66e17072008-02-07 00:13:56 -0800652
Balbir Singhcf475ad2008-04-29 01:00:16 -0700653 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800654 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700655 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -0800656
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800657 scan = 0;
658 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -0800659 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800660 break;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800661
662 page = pc->page;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700663 if (unlikely(!PageCgroupUsed(pc)))
664 continue;
Hugh Dickins436c65412008-02-07 00:14:12 -0800665 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800666 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800667
Hugh Dickins436c65412008-02-07 00:14:12 -0800668 scan++;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700669 if (__isolate_lru_page(page, mode, file) == 0) {
Balbir Singh66e17072008-02-07 00:13:56 -0800670 list_move(&page->lru, dst);
671 nr_taken++;
672 }
673 }
674
Balbir Singh66e17072008-02-07 00:13:56 -0800675 *scanned = scan;
676 return nr_taken;
677}
678
Balbir Singh6d61ef42009-01-07 18:08:06 -0800679#define mem_cgroup_from_res_counter(counter, member) \
680 container_of(counter, struct mem_cgroup, member)
681
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -0800682static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
683{
684 if (do_swap_account) {
685 if (res_counter_check_under_limit(&mem->res) &&
686 res_counter_check_under_limit(&mem->memsw))
687 return true;
688 } else
689 if (res_counter_check_under_limit(&mem->res))
690 return true;
691 return false;
692}
693
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800694static unsigned int get_swappiness(struct mem_cgroup *memcg)
695{
696 struct cgroup *cgrp = memcg->css.cgroup;
697 unsigned int swappiness;
698
699 /* root ? */
700 if (cgrp->parent == NULL)
701 return vm_swappiness;
702
703 spin_lock(&memcg->reclaim_param_lock);
704 swappiness = memcg->swappiness;
705 spin_unlock(&memcg->reclaim_param_lock);
706
707 return swappiness;
708}
709
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700710static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
711{
712 int *val = data;
713 (*val)++;
714 return 0;
715}
Balbir Singhe2224322009-04-02 16:57:39 -0700716
717/**
718 * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode.
719 * @memcg: The memory cgroup that went over limit
720 * @p: Task that is going to be killed
721 *
722 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
723 * enabled
724 */
725void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
726{
727 struct cgroup *task_cgrp;
728 struct cgroup *mem_cgrp;
729 /*
730 * Need a buffer in BSS, can't rely on allocations. The code relies
731 * on the assumption that OOM is serialized for memory controller.
732 * If this assumption is broken, revisit this code.
733 */
734 static char memcg_name[PATH_MAX];
735 int ret;
736
737 if (!memcg)
738 return;
739
740
741 rcu_read_lock();
742
743 mem_cgrp = memcg->css.cgroup;
744 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
745
746 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
747 if (ret < 0) {
748 /*
749 * Unfortunately, we are unable to convert to a useful name
750 * But we'll still print out the usage information
751 */
752 rcu_read_unlock();
753 goto done;
754 }
755 rcu_read_unlock();
756
757 printk(KERN_INFO "Task in %s killed", memcg_name);
758
759 rcu_read_lock();
760 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
761 if (ret < 0) {
762 rcu_read_unlock();
763 goto done;
764 }
765 rcu_read_unlock();
766
767 /*
768 * Continues from above, so we don't need an KERN_ level
769 */
770 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
771done:
772
773 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
774 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
775 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
776 res_counter_read_u64(&memcg->res, RES_FAILCNT));
777 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
778 "failcnt %llu\n",
779 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
780 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
781 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
782}
783
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700784/*
785 * This function returns the number of memcg under hierarchy tree. Returns
786 * 1(self count) if no children.
787 */
788static int mem_cgroup_count_children(struct mem_cgroup *mem)
789{
790 int num = 0;
791 mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
792 return num;
793}
794
Balbir Singh6d61ef42009-01-07 18:08:06 -0800795/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700796 * Visit the first child (need not be the first child as per the ordering
797 * of the cgroup list, since we track last_scanned_child) of @mem and use
798 * that to reclaim free pages from.
799 */
800static struct mem_cgroup *
801mem_cgroup_select_victim(struct mem_cgroup *root_mem)
802{
803 struct mem_cgroup *ret = NULL;
804 struct cgroup_subsys_state *css;
805 int nextid, found;
806
807 if (!root_mem->use_hierarchy) {
808 css_get(&root_mem->css);
809 ret = root_mem;
810 }
811
812 while (!ret) {
813 rcu_read_lock();
814 nextid = root_mem->last_scanned_child + 1;
815 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
816 &found);
817 if (css && css_tryget(css))
818 ret = container_of(css, struct mem_cgroup, css);
819
820 rcu_read_unlock();
821 /* Updates scanning parameter */
822 spin_lock(&root_mem->reclaim_param_lock);
823 if (!css) {
824 /* this means start scan from ID:1 */
825 root_mem->last_scanned_child = 0;
826 } else
827 root_mem->last_scanned_child = found;
828 spin_unlock(&root_mem->reclaim_param_lock);
829 }
830
831 return ret;
832}
833
834/*
835 * Scan the hierarchy if needed to reclaim memory. We remember the last child
836 * we reclaimed from, so that we don't end up penalizing one child extensively
837 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800838 *
839 * root_mem is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700840 *
841 * We give up and return to the caller when we visit root_mem twice.
842 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700843 *
844 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800845 */
846static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700847 gfp_t gfp_mask, bool noswap, bool shrink)
Balbir Singh6d61ef42009-01-07 18:08:06 -0800848{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700849 struct mem_cgroup *victim;
850 int ret, total = 0;
851 int loop = 0;
Balbir Singh6d61ef42009-01-07 18:08:06 -0800852
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700853 /* If memsw_is_minimum==1, swap-out is of-no-use. */
854 if (root_mem->memsw_is_minimum)
855 noswap = true;
856
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700857 while (loop < 2) {
858 victim = mem_cgroup_select_victim(root_mem);
859 if (victim == root_mem)
860 loop++;
861 if (!mem_cgroup_local_usage(&victim->stat)) {
862 /* this cgroup's local usage == 0 */
863 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -0800864 continue;
865 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700866 /* we use swappiness of local cgroup */
867 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask, noswap,
868 get_swappiness(victim));
869 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700870 /*
871 * At shrinking usage, we can't check we should stop here or
872 * reclaim more. It's depends on callers. last_scanned_child
873 * will work enough for keeping fairness under tree.
874 */
875 if (shrink)
876 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700877 total += ret;
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -0800878 if (mem_cgroup_check_under_limit(root_mem))
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700879 return 1 + total;
Balbir Singh6d61ef42009-01-07 18:08:06 -0800880 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700881 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -0800882}
883
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800884bool mem_cgroup_oom_called(struct task_struct *task)
885{
886 bool ret = false;
887 struct mem_cgroup *mem;
888 struct mm_struct *mm;
889
890 rcu_read_lock();
891 mm = task->mm;
892 if (!mm)
893 mm = &init_mm;
894 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
895 if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
896 ret = true;
897 rcu_read_unlock();
898 return ret;
899}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700900
901static int record_last_oom_cb(struct mem_cgroup *mem, void *data)
902{
903 mem->last_oom_jiffies = jiffies;
904 return 0;
905}
906
907static void record_last_oom(struct mem_cgroup *mem)
908{
909 mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb);
910}
911
Balbir Singhd69b0422009-06-17 16:26:34 -0700912/*
913 * Currently used to update mapped file statistics, but the routine can be
914 * generalized to update other statistics as well.
915 */
916void mem_cgroup_update_mapped_file_stat(struct page *page, int val)
917{
918 struct mem_cgroup *mem;
919 struct mem_cgroup_stat *stat;
920 struct mem_cgroup_stat_cpu *cpustat;
921 int cpu;
922 struct page_cgroup *pc;
923
924 if (!page_is_file_cache(page))
925 return;
926
927 pc = lookup_page_cgroup(page);
928 if (unlikely(!pc))
929 return;
930
931 lock_page_cgroup(pc);
932 mem = pc->mem_cgroup;
933 if (!mem)
934 goto done;
935
936 if (!PageCgroupUsed(pc))
937 goto done;
938
939 /*
940 * Preemption is already disabled, we don't need get_cpu()
941 */
942 cpu = smp_processor_id();
943 stat = &mem->stat;
944 cpustat = &stat->cpustat[cpu];
945
946 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE, val);
947done:
948 unlock_page_cgroup(pc);
949}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700950
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800951/*
952 * Unlike exported interface, "oom" parameter is added. if oom==true,
953 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800954 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800955static int __mem_cgroup_try_charge(struct mm_struct *mm,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800956 gfp_t gfp_mask, struct mem_cgroup **memcg,
957 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800958{
Balbir Singh6d61ef42009-01-07 18:08:06 -0800959 struct mem_cgroup *mem, *mem_over_limit;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800960 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Balbir Singh28dbc4b2009-01-07 18:08:05 -0800961 struct res_counter *fail_res;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800962
963 if (unlikely(test_thread_flag(TIF_MEMDIE))) {
964 /* Don't account this! */
965 *memcg = NULL;
966 return 0;
967 }
968
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800969 /*
Hugh Dickins3be912772008-02-07 00:14:19 -0800970 * We always charge the cgroup the mm_struct belongs to.
971 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800972 * thread group leader migrates. It's possible that mm is not
973 * set, if so charge the init_mm (happens for pagecache usage).
974 */
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800975 mem = *memcg;
976 if (likely(!mem)) {
977 mem = try_get_mem_cgroup_from_mm(mm);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800978 *memcg = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700979 } else {
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800980 css_get(&mem->css);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700981 }
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800982 if (unlikely(!mem))
983 return 0;
984
Nikanth Karthikesan46f7e602009-05-28 14:34:41 -0700985 VM_BUG_ON(css_is_removed(&mem->css));
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800986
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800987 while (1) {
988 int ret;
989 bool noswap = false;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800990
Balbir Singh28dbc4b2009-01-07 18:08:05 -0800991 ret = res_counter_charge(&mem->res, PAGE_SIZE, &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800992 if (likely(!ret)) {
993 if (!do_swap_account)
994 break;
Balbir Singh28dbc4b2009-01-07 18:08:05 -0800995 ret = res_counter_charge(&mem->memsw, PAGE_SIZE,
996 &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800997 if (likely(!ret))
998 break;
999 /* mem+swap counter fails */
1000 res_counter_uncharge(&mem->res, PAGE_SIZE);
1001 noswap = true;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001002 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1003 memsw);
1004 } else
1005 /* mem counter fails */
1006 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1007 res);
1008
Hugh Dickins3be912772008-02-07 00:14:19 -08001009 if (!(gfp_mask & __GFP_WAIT))
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001010 goto nomem;
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001011
Balbir Singh6d61ef42009-01-07 18:08:06 -08001012 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, gfp_mask,
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001013 noswap, false);
Daisuke Nishimura4d1c6272009-01-15 13:51:14 -08001014 if (ret)
1015 continue;
Balbir Singh66e17072008-02-07 00:13:56 -08001016
1017 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001018 * try_to_free_mem_cgroup_pages() might not give us a full
1019 * picture of reclaim. Some pages are reclaimed and might be
1020 * moved to swap cache or just unmapped from the cgroup.
1021 * Check the limit again to see if the reclaim reduced the
1022 * current usage of the cgroup before giving up
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001023 *
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001024 */
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -08001025 if (mem_cgroup_check_under_limit(mem_over_limit))
1026 continue;
Hugh Dickins3be912772008-02-07 00:14:19 -08001027
1028 if (!nr_retries--) {
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001029 if (oom) {
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08001030 mutex_lock(&memcg_tasklist);
KAMEZAWA Hiroyuki88700752009-01-07 18:08:09 -08001031 mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08001032 mutex_unlock(&memcg_tasklist);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001033 record_last_oom(mem_over_limit);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001034 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001035 goto nomem;
Balbir Singh66e17072008-02-07 00:13:56 -08001036 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001037 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001038 return 0;
1039nomem:
1040 css_put(&mem->css);
1041 return -ENOMEM;
1042}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001043
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001044
1045/*
1046 * A helper function to get mem_cgroup from ID. must be called under
1047 * rcu_read_lock(). The caller must check css_is_removed() or some if
1048 * it's concern. (dropping refcnt from swap can be called against removed
1049 * memcg.)
1050 */
1051static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
1052{
1053 struct cgroup_subsys_state *css;
1054
1055 /* ID 0 is unused ID */
1056 if (!id)
1057 return NULL;
1058 css = css_lookup(&mem_cgroup_subsys, id);
1059 if (!css)
1060 return NULL;
1061 return container_of(css, struct mem_cgroup, css);
1062}
1063
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001064static struct mem_cgroup *try_get_mem_cgroup_from_swapcache(struct page *page)
1065{
1066 struct mem_cgroup *mem;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001067 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001068 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001069 swp_entry_t ent;
1070
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001071 VM_BUG_ON(!PageLocked(page));
1072
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001073 if (!PageSwapCache(page))
1074 return NULL;
1075
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001076 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f632009-04-30 15:08:11 -07001077 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001078 if (PageCgroupUsed(pc)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001079 mem = pc->mem_cgroup;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001080 if (mem && !css_tryget(&mem->css))
1081 mem = NULL;
1082 } else {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001083 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001084 id = lookup_swap_cgroup(ent);
1085 rcu_read_lock();
1086 mem = mem_cgroup_lookup(id);
1087 if (mem && !css_tryget(&mem->css))
1088 mem = NULL;
1089 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001090 }
Daisuke Nishimurac0bd3f632009-04-30 15:08:11 -07001091 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001092 return mem;
1093}
1094
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001095/*
Daisuke Nishimuraa5e924f2009-01-07 18:08:28 -08001096 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001097 * USED state. If already USED, uncharge and return.
1098 */
1099
1100static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1101 struct page_cgroup *pc,
1102 enum charge_type ctype)
1103{
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001104 /* try_charge() can return NULL to *memcg, taking care of it. */
1105 if (!mem)
1106 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001107
1108 lock_page_cgroup(pc);
1109 if (unlikely(PageCgroupUsed(pc))) {
1110 unlock_page_cgroup(pc);
1111 res_counter_uncharge(&mem->res, PAGE_SIZE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001112 if (do_swap_account)
1113 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001114 css_put(&mem->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001115 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001116 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001117 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001118 smp_wmb();
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -07001119 pc->flags = pcg_default_flags[ctype];
Hugh Dickins3be912772008-02-07 00:14:19 -08001120
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001121 mem_cgroup_charge_statistics(mem, pc, true);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001122
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001123 unlock_page_cgroup(pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001124}
1125
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001126/**
1127 * mem_cgroup_move_account - move account of the page
1128 * @pc: page_cgroup of the page.
1129 * @from: mem_cgroup which the page is moved from.
1130 * @to: mem_cgroup which the page is moved to. @from != @to.
1131 *
1132 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001133 * - page is not on LRU (isolate_page() is useful.)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001134 *
1135 * returns 0 at success,
1136 * returns -EBUSY when lock is busy or "pc" is unstable.
1137 *
1138 * This function does "uncharge" from old cgroup but doesn't do "charge" to
1139 * new cgroup. It should be done by a caller.
1140 */
1141
1142static int mem_cgroup_move_account(struct page_cgroup *pc,
1143 struct mem_cgroup *from, struct mem_cgroup *to)
1144{
1145 struct mem_cgroup_per_zone *from_mz, *to_mz;
1146 int nid, zid;
1147 int ret = -EBUSY;
Balbir Singhd69b0422009-06-17 16:26:34 -07001148 struct page *page;
1149 int cpu;
1150 struct mem_cgroup_stat *stat;
1151 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001152
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001153 VM_BUG_ON(from == to);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001154 VM_BUG_ON(PageLRU(pc->page));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001155
1156 nid = page_cgroup_nid(pc);
1157 zid = page_cgroup_zid(pc);
1158 from_mz = mem_cgroup_zoneinfo(from, nid, zid);
1159 to_mz = mem_cgroup_zoneinfo(to, nid, zid);
1160
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001161 if (!trylock_page_cgroup(pc))
1162 return ret;
1163
1164 if (!PageCgroupUsed(pc))
1165 goto out;
1166
1167 if (pc->mem_cgroup != from)
1168 goto out;
1169
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001170 res_counter_uncharge(&from->res, PAGE_SIZE);
1171 mem_cgroup_charge_statistics(from, pc, false);
Balbir Singhd69b0422009-06-17 16:26:34 -07001172
1173 page = pc->page;
1174 if (page_is_file_cache(page) && page_mapped(page)) {
1175 cpu = smp_processor_id();
1176 /* Update mapped_file data for mem_cgroup "from" */
1177 stat = &from->stat;
1178 cpustat = &stat->cpustat[cpu];
1179 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1180 -1);
1181
1182 /* Update mapped_file data for mem_cgroup "to" */
1183 stat = &to->stat;
1184 cpustat = &stat->cpustat[cpu];
1185 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1186 1);
1187 }
1188
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001189 if (do_swap_account)
1190 res_counter_uncharge(&from->memsw, PAGE_SIZE);
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001191 css_put(&from->css);
1192
1193 css_get(&to->css);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001194 pc->mem_cgroup = to;
1195 mem_cgroup_charge_statistics(to, pc, true);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001196 ret = 0;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001197out:
1198 unlock_page_cgroup(pc);
1199 return ret;
1200}
1201
1202/*
1203 * move charges to its parent.
1204 */
1205
1206static int mem_cgroup_move_parent(struct page_cgroup *pc,
1207 struct mem_cgroup *child,
1208 gfp_t gfp_mask)
1209{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001210 struct page *page = pc->page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001211 struct cgroup *cg = child->css.cgroup;
1212 struct cgroup *pcg = cg->parent;
1213 struct mem_cgroup *parent;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001214 int ret;
1215
1216 /* Is ROOT ? */
1217 if (!pcg)
1218 return -EINVAL;
1219
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001220
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001221 parent = mem_cgroup_from_cont(pcg);
1222
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001223
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001224 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001225 if (ret || !parent)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001226 return ret;
1227
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001228 if (!get_page_unless_zero(page)) {
1229 ret = -EBUSY;
1230 goto uncharge;
1231 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001232
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001233 ret = isolate_lru_page(page);
1234
1235 if (ret)
1236 goto cancel;
1237
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001238 ret = mem_cgroup_move_account(pc, child, parent);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001239
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001240 putback_lru_page(page);
1241 if (!ret) {
1242 put_page(page);
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001243 /* drop extra refcnt by try_charge() */
1244 css_put(&parent->css);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001245 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001246 }
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001247
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001248cancel:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001249 put_page(page);
1250uncharge:
1251 /* drop extra refcnt by try_charge() */
1252 css_put(&parent->css);
1253 /* uncharge if move fails */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001254 res_counter_uncharge(&parent->res, PAGE_SIZE);
1255 if (do_swap_account)
1256 res_counter_uncharge(&parent->memsw, PAGE_SIZE);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001257 return ret;
1258}
1259
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001260/*
1261 * Charge the memory controller for page usage.
1262 * Return
1263 * 0 if the charge was successful
1264 * < 0 if the cgroup is over its limit
1265 */
1266static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
1267 gfp_t gfp_mask, enum charge_type ctype,
1268 struct mem_cgroup *memcg)
1269{
1270 struct mem_cgroup *mem;
1271 struct page_cgroup *pc;
1272 int ret;
1273
1274 pc = lookup_page_cgroup(page);
1275 /* can happen at boot */
1276 if (unlikely(!pc))
1277 return 0;
1278 prefetchw(pc);
1279
1280 mem = memcg;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001281 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001282 if (ret || !mem)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001283 return ret;
1284
1285 __mem_cgroup_commit_charge(mem, pc, ctype);
1286 return 0;
1287}
1288
1289int mem_cgroup_newpage_charge(struct page *page,
1290 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001291{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001292 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001293 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001294 if (PageCompound(page))
1295 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001296 /*
1297 * If already mapped, we don't have to account.
1298 * If page cache, page->mapping has address_space.
1299 * But page->mapping may have out-of-use anon_vma pointer,
1300 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
1301 * is NULL.
1302 */
1303 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
1304 return 0;
1305 if (unlikely(!mm))
1306 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001307 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001308 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001309}
1310
Daisuke Nishimura83aae4c72009-04-02 16:57:48 -07001311static void
1312__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1313 enum charge_type ctype);
1314
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001315int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
1316 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08001317{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001318 struct mem_cgroup *mem = NULL;
1319 int ret;
1320
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001321 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001322 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001323 if (PageCompound(page))
1324 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001325 /*
1326 * Corner case handling. This is called from add_to_page_cache()
1327 * in usual. But some FS (shmem) precharges this page before calling it
1328 * and call add_to_page_cache() with GFP_NOWAIT.
1329 *
1330 * For GFP_NOWAIT case, the page may be pre-charged before calling
1331 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
1332 * charge twice. (It works but has to pay a bit larger cost.)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001333 * And when the page is SwapCache, it should take swap information
1334 * into account. This is under lock_page() now.
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001335 */
1336 if (!(gfp_mask & __GFP_WAIT)) {
1337 struct page_cgroup *pc;
1338
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001339
1340 pc = lookup_page_cgroup(page);
1341 if (!pc)
1342 return 0;
1343 lock_page_cgroup(pc);
1344 if (PageCgroupUsed(pc)) {
1345 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001346 return 0;
1347 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001348 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001349 }
1350
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001351 if (unlikely(!mm && !mem))
Balbir Singh8697d332008-02-07 00:13:59 -08001352 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001353
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -07001354 if (page_is_file_cache(page))
1355 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001356 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001357
Daisuke Nishimura83aae4c72009-04-02 16:57:48 -07001358 /* shmem */
1359 if (PageSwapCache(page)) {
1360 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1361 if (!ret)
1362 __mem_cgroup_commit_charge_swapin(page, mem,
1363 MEM_CGROUP_CHARGE_TYPE_SHMEM);
1364 } else
1365 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
1366 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001367
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001368 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001369}
1370
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001371/*
1372 * While swap-in, try_charge -> commit or cancel, the page is locked.
1373 * And when try_charge() successfully returns, one refcnt to memcg without
1374 * struct page_cgroup is aquired. This refcnt will be cumsumed by
1375 * "commit()" or removed by "cancel()"
1376 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001377int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
1378 struct page *page,
1379 gfp_t mask, struct mem_cgroup **ptr)
1380{
1381 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001382 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001383
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001384 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001385 return 0;
1386
1387 if (!do_swap_account)
1388 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001389 /*
1390 * A racing thread's fault, or swapoff, may have already updated
1391 * the pte, and even removed page from swap cache: return success
1392 * to go on to do_swap_page()'s pte_same() test, which should fail.
1393 */
1394 if (!PageSwapCache(page))
1395 return 0;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001396 mem = try_get_mem_cgroup_from_swapcache(page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001397 if (!mem)
1398 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001399 *ptr = mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001400 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true);
1401 /* drop extra refcnt from tryget */
1402 css_put(&mem->css);
1403 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001404charge_cur_mm:
1405 if (unlikely(!mm))
1406 mm = &init_mm;
1407 return __mem_cgroup_try_charge(mm, mask, ptr, true);
1408}
1409
Daisuke Nishimura83aae4c72009-04-02 16:57:48 -07001410static void
1411__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1412 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001413{
1414 struct page_cgroup *pc;
1415
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001416 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001417 return;
1418 if (!ptr)
1419 return;
1420 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001421 mem_cgroup_lru_del_before_commit_swapcache(page);
Daisuke Nishimura83aae4c72009-04-02 16:57:48 -07001422 __mem_cgroup_commit_charge(ptr, pc, ctype);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001423 mem_cgroup_lru_add_after_commit_swapcache(page);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001424 /*
1425 * Now swap is on-memory. This means this page may be
1426 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08001427 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
1428 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
1429 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001430 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08001431 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001432 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001433 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001434 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001435
1436 id = swap_cgroup_record(ent, 0);
1437 rcu_read_lock();
1438 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001439 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001440 /*
1441 * This recorded memcg can be obsolete one. So, avoid
1442 * calling css_tryget
1443 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001444 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1445 mem_cgroup_put(memcg);
1446 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001447 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001448 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001449 /* add this page(page_cgroup) to the LRU we want. */
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001450
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001451}
1452
Daisuke Nishimura83aae4c72009-04-02 16:57:48 -07001453void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
1454{
1455 __mem_cgroup_commit_charge_swapin(page, ptr,
1456 MEM_CGROUP_CHARGE_TYPE_MAPPED);
1457}
1458
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001459void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
1460{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001461 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001462 return;
1463 if (!mem)
1464 return;
1465 res_counter_uncharge(&mem->res, PAGE_SIZE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001466 if (do_swap_account)
1467 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001468 css_put(&mem->css);
1469}
1470
1471
Balbir Singh8697d332008-02-07 00:13:59 -08001472/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001473 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001474 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001475static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001476__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001477{
Hugh Dickins82895462008-03-04 14:29:08 -08001478 struct page_cgroup *pc;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001479 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001480 struct mem_cgroup_per_zone *mz;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001481
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001482 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001483 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07001484
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001485 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001486 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001487
Balbir Singh8697d332008-02-07 00:13:59 -08001488 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08001489 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08001490 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001491 pc = lookup_page_cgroup(page);
1492 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001493 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001494
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001495 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001496
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001497 mem = pc->mem_cgroup;
1498
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001499 if (!PageCgroupUsed(pc))
1500 goto unlock_out;
1501
1502 switch (ctype) {
1503 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07001504 case MEM_CGROUP_CHARGE_TYPE_DROP:
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001505 if (page_mapped(page))
1506 goto unlock_out;
1507 break;
1508 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
1509 if (!PageAnon(page)) { /* Shared memory */
1510 if (page->mapping && !page_is_file_cache(page))
1511 goto unlock_out;
1512 } else if (page_mapped(page)) /* Anon */
1513 goto unlock_out;
1514 break;
1515 default:
1516 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001517 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001518
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001519 res_counter_uncharge(&mem->res, PAGE_SIZE);
1520 if (do_swap_account && (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT))
1521 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001522 mem_cgroup_charge_statistics(mem, pc, false);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001523
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001524 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001525 /*
1526 * pc->mem_cgroup is not cleared here. It will be accessed when it's
1527 * freed from LRU. This is safe because uncharged page is expected not
1528 * to be reused (freed soon). Exception is SwapCache, it's handled by
1529 * special functions.
1530 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08001531
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001532 mz = page_cgroup_zoneinfo(pc);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001533 unlock_page_cgroup(pc);
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08001534
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08001535 /* at swapout, this memcg will be accessed to record to swap */
1536 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1537 css_put(&mem->css);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001538
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001539 return mem;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001540
1541unlock_out:
1542 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001543 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08001544}
1545
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001546void mem_cgroup_uncharge_page(struct page *page)
1547{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001548 /* early check. */
1549 if (page_mapped(page))
1550 return;
1551 if (page->mapping && !PageAnon(page))
1552 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001553 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
1554}
1555
1556void mem_cgroup_uncharge_cache_page(struct page *page)
1557{
1558 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07001559 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001560 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
1561}
1562
Daisuke Nishimurae767e052009-05-28 14:34:28 -07001563#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001564/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07001565 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001566 * memcg information is recorded to swap_cgroup of "ent"
1567 */
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07001568void
1569mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001570{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001571 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07001572 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001573
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07001574 if (!swapout) /* this was a swap cache but the swap is unused ! */
1575 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
1576
1577 memcg = __mem_cgroup_uncharge_common(page, ctype);
1578
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001579 /* record memcg information */
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07001580 if (do_swap_account && swapout && memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001581 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001582 mem_cgroup_get(memcg);
1583 }
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07001584 if (swapout && memcg)
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08001585 css_put(&memcg->css);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001586}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07001587#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001588
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001589#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1590/*
1591 * called from swap_entry_free(). remove record in swap_cgroup and
1592 * uncharge "memsw" account.
1593 */
1594void mem_cgroup_uncharge_swap(swp_entry_t ent)
1595{
1596 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001597 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001598
1599 if (!do_swap_account)
1600 return;
1601
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001602 id = swap_cgroup_record(ent, 0);
1603 rcu_read_lock();
1604 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001605 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001606 /*
1607 * We uncharge this because swap is freed.
1608 * This memcg can be obsolete one. We avoid calling css_tryget
1609 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001610 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1611 mem_cgroup_put(memcg);
1612 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001613 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001614}
1615#endif
1616
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001617/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001618 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
1619 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001620 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001621int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001622{
1623 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001624 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001625 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001626
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001627 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07001628 return 0;
1629
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001630 pc = lookup_page_cgroup(page);
1631 lock_page_cgroup(pc);
1632 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001633 mem = pc->mem_cgroup;
1634 css_get(&mem->css);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08001635 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001636 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001637
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001638 if (mem) {
Daisuke Nishimura3bb4edf2009-01-07 18:08:28 -08001639 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001640 css_put(&mem->css);
1641 }
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001642 *ptr = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001643 return ret;
1644}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08001645
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001646/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001647void mem_cgroup_end_migration(struct mem_cgroup *mem,
1648 struct page *oldpage, struct page *newpage)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001649{
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001650 struct page *target, *unused;
1651 struct page_cgroup *pc;
1652 enum charge_type ctype;
1653
1654 if (!mem)
1655 return;
1656
1657 /* at migration success, oldpage->mapping is NULL. */
1658 if (oldpage->mapping) {
1659 target = oldpage;
1660 unused = NULL;
1661 } else {
1662 target = newpage;
1663 unused = oldpage;
1664 }
1665
1666 if (PageAnon(target))
1667 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
1668 else if (page_is_file_cache(target))
1669 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
1670 else
1671 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
1672
1673 /* unused page is not on radix-tree now. */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001674 if (unused)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001675 __mem_cgroup_uncharge_common(unused, ctype);
1676
1677 pc = lookup_page_cgroup(target);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001678 /*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001679 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
1680 * So, double-counting is effectively avoided.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001681 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001682 __mem_cgroup_commit_charge(mem, pc, ctype);
1683
1684 /*
1685 * Both of oldpage and newpage are still under lock_page().
1686 * Then, we don't have to care about race in radix-tree.
1687 * But we have to be careful that this page is unmapped or not.
1688 *
1689 * There is a case for !page_mapped(). At the start of
1690 * migration, oldpage was mapped. But now, it's zapped.
1691 * But we know *target* page is not freed/reused under us.
1692 * mem_cgroup_uncharge_page() does all necessary checks.
1693 */
1694 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
1695 mem_cgroup_uncharge_page(target);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001696}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001697
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001698/*
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07001699 * A call to try to shrink memory usage on charge failure at shmem's swapin.
1700 * Calling hierarchical_reclaim is not enough because we should update
1701 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
1702 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
1703 * not from the memcg which this page would be charged to.
1704 * try_charge_swapin does all of these works properly.
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001705 */
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07001706int mem_cgroup_shmem_charge_fallback(struct page *page,
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001707 struct mm_struct *mm,
1708 gfp_t gfp_mask)
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001709{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001710 struct mem_cgroup *mem = NULL;
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07001711 int ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001712
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001713 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001714 return 0;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001715
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07001716 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1717 if (!ret)
1718 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001719
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07001720 return ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001721}
1722
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001723static DEFINE_MUTEX(set_limit_mutex);
1724
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08001725static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001726 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001727{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001728 int retry_count;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001729 int progress;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001730 u64 memswlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001731 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001732 int children = mem_cgroup_count_children(memcg);
1733 u64 curusage, oldusage;
1734
1735 /*
1736 * For keeping hierarchical_reclaim simple, how long we should retry
1737 * is depends on callers. We set our retry-count to be function
1738 * of # of children which we should visit in this loop.
1739 */
1740 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
1741
1742 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001743
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001744 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001745 if (signal_pending(current)) {
1746 ret = -EINTR;
1747 break;
1748 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001749 /*
1750 * Rather than hide all in some function, I do this in
1751 * open coded manner. You see what this really does.
1752 * We have to guarantee mem->res.limit < mem->memsw.limit.
1753 */
1754 mutex_lock(&set_limit_mutex);
1755 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1756 if (memswlimit < val) {
1757 ret = -EINVAL;
1758 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001759 break;
1760 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001761 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001762 if (!ret) {
1763 if (memswlimit == val)
1764 memcg->memsw_is_minimum = true;
1765 else
1766 memcg->memsw_is_minimum = false;
1767 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001768 mutex_unlock(&set_limit_mutex);
1769
1770 if (!ret)
1771 break;
1772
Daisuke Nishimura42e9abb2009-01-07 18:08:30 -08001773 progress = mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL,
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001774 false, true);
1775 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
1776 /* Usage is reduced ? */
1777 if (curusage >= oldusage)
1778 retry_count--;
1779 else
1780 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001781 }
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001782
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001783 return ret;
1784}
1785
Li Zefan338c8432009-06-17 16:27:15 -07001786static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
1787 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001788{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001789 int retry_count;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001790 u64 memlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001791 int children = mem_cgroup_count_children(memcg);
1792 int ret = -EBUSY;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001793
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001794 /* see mem_cgroup_resize_res_limit */
1795 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
1796 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001797 while (retry_count) {
1798 if (signal_pending(current)) {
1799 ret = -EINTR;
1800 break;
1801 }
1802 /*
1803 * Rather than hide all in some function, I do this in
1804 * open coded manner. You see what this really does.
1805 * We have to guarantee mem->res.limit < mem->memsw.limit.
1806 */
1807 mutex_lock(&set_limit_mutex);
1808 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
1809 if (memlimit > val) {
1810 ret = -EINVAL;
1811 mutex_unlock(&set_limit_mutex);
1812 break;
1813 }
1814 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001815 if (!ret) {
1816 if (memlimit == val)
1817 memcg->memsw_is_minimum = true;
1818 else
1819 memcg->memsw_is_minimum = false;
1820 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001821 mutex_unlock(&set_limit_mutex);
1822
1823 if (!ret)
1824 break;
1825
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001826 mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL, true, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001827 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001828 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001829 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001830 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001831 else
1832 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001833 }
1834 return ret;
1835}
1836
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001837/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001838 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001839 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
1840 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001841static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001842 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001843{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001844 struct zone *zone;
1845 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001846 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001847 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001848 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001849 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001850
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001851 zone = &NODE_DATA(node)->node_zones[zid];
1852 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001853 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001854
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001855 loop = MEM_CGROUP_ZSTAT(mz, lru);
1856 /* give some margin against EBUSY etc...*/
1857 loop += 256;
1858 busy = NULL;
1859 while (loop--) {
1860 ret = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001861 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001862 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001863 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001864 break;
1865 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001866 pc = list_entry(list->prev, struct page_cgroup, lru);
1867 if (busy == pc) {
1868 list_move(&pc->lru, list);
1869 busy = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001870 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001871 continue;
1872 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001873 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001874
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -08001875 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001876 if (ret == -ENOMEM)
1877 break;
1878
1879 if (ret == -EBUSY || ret == -EINVAL) {
1880 /* found lock contention or "pc" is obsolete. */
1881 busy = pc;
1882 cond_resched();
1883 } else
1884 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001885 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001886
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001887 if (!ret && !list_empty(list))
1888 return -EBUSY;
1889 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001890}
1891
1892/*
1893 * make mem_cgroup's charge to be 0 if there is no task.
1894 * This enables deleting this mem_cgroup.
1895 */
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001896static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001897{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001898 int ret;
1899 int node, zid, shrink;
1900 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001901 struct cgroup *cgrp = mem->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001902
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001903 css_get(&mem->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001904
1905 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001906 /* should free all ? */
1907 if (free_all)
1908 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001909move_account:
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001910 while (mem->res.usage > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001911 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001912 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001913 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001914 ret = -EINTR;
1915 if (signal_pending(current))
1916 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001917 /* This is for making all *used* pages to be on LRU. */
1918 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001919 ret = 0;
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08001920 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001921 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07001922 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001923 for_each_lru(l) {
1924 ret = mem_cgroup_force_empty_list(mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001925 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001926 if (ret)
1927 break;
1928 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001929 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001930 if (ret)
1931 break;
1932 }
1933 /* it seems parent cgroup doesn't have enough mem */
1934 if (ret == -ENOMEM)
1935 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001936 cond_resched();
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001937 }
1938 ret = 0;
1939out:
1940 css_put(&mem->css);
1941 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001942
1943try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001944 /* returns EBUSY if there is a task or if we come here twice. */
1945 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001946 ret = -EBUSY;
1947 goto out;
1948 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001949 /* we call try-to-free pages for make this cgroup empty */
1950 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001951 /* try to free all pages in this cgroup */
1952 shrink = 1;
1953 while (nr_retries && mem->res.usage > 0) {
1954 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001955
1956 if (signal_pending(current)) {
1957 ret = -EINTR;
1958 goto out;
1959 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001960 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
1961 false, get_swappiness(mem));
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001962 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001963 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001964 /* maybe some writeback is necessary */
1965 congestion_wait(WRITE, HZ/10);
1966 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001967
1968 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001969 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001970 /* try move_account...there may be some *locked* pages. */
1971 if (mem->res.usage)
1972 goto move_account;
1973 ret = 0;
1974 goto out;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001975}
1976
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001977int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
1978{
1979 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
1980}
1981
1982
Balbir Singh18f59ea2009-01-07 18:08:07 -08001983static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
1984{
1985 return mem_cgroup_from_cont(cont)->use_hierarchy;
1986}
1987
1988static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
1989 u64 val)
1990{
1991 int retval = 0;
1992 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1993 struct cgroup *parent = cont->parent;
1994 struct mem_cgroup *parent_mem = NULL;
1995
1996 if (parent)
1997 parent_mem = mem_cgroup_from_cont(parent);
1998
1999 cgroup_lock();
2000 /*
2001 * If parent's use_hiearchy is set, we can't make any modifications
2002 * in the child subtrees. If it is unset, then the change can
2003 * occur, provided the current cgroup has no children.
2004 *
2005 * For the root cgroup, parent_mem is NULL, we allow value to be
2006 * set if there are no children.
2007 */
2008 if ((!parent_mem || !parent_mem->use_hierarchy) &&
2009 (val == 1 || val == 0)) {
2010 if (list_empty(&cont->children))
2011 mem->use_hierarchy = val;
2012 else
2013 retval = -EBUSY;
2014 } else
2015 retval = -EINVAL;
2016 cgroup_unlock();
2017
2018 return retval;
2019}
2020
Paul Menage2c3daa722008-04-29 00:59:58 -07002021static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002022{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002023 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2024 u64 val = 0;
2025 int type, name;
2026
2027 type = MEMFILE_TYPE(cft->private);
2028 name = MEMFILE_ATTR(cft->private);
2029 switch (type) {
2030 case _MEM:
2031 val = res_counter_read_u64(&mem->res, name);
2032 break;
2033 case _MEMSWAP:
Li Zefan338c8432009-06-17 16:27:15 -07002034 val = res_counter_read_u64(&mem->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002035 break;
2036 default:
2037 BUG();
2038 break;
2039 }
2040 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002041}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002042/*
2043 * The user of this function is...
2044 * RES_LIMIT.
2045 */
Paul Menage856c13a2008-07-25 01:47:04 -07002046static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
2047 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002048{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002049 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002050 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002051 unsigned long long val;
2052 int ret;
2053
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002054 type = MEMFILE_TYPE(cft->private);
2055 name = MEMFILE_ATTR(cft->private);
2056 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002057 case RES_LIMIT:
2058 /* This function does all necessary parse...reuse it */
2059 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002060 if (ret)
2061 break;
2062 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002063 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002064 else
2065 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002066 break;
2067 default:
2068 ret = -EINVAL; /* should be BUG() ? */
2069 break;
2070 }
2071 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002072}
2073
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08002074static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
2075 unsigned long long *mem_limit, unsigned long long *memsw_limit)
2076{
2077 struct cgroup *cgroup;
2078 unsigned long long min_limit, min_memsw_limit, tmp;
2079
2080 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2081 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2082 cgroup = memcg->css.cgroup;
2083 if (!memcg->use_hierarchy)
2084 goto out;
2085
2086 while (cgroup->parent) {
2087 cgroup = cgroup->parent;
2088 memcg = mem_cgroup_from_cont(cgroup);
2089 if (!memcg->use_hierarchy)
2090 break;
2091 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
2092 min_limit = min(min_limit, tmp);
2093 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2094 min_memsw_limit = min(min_memsw_limit, tmp);
2095 }
2096out:
2097 *mem_limit = min_limit;
2098 *memsw_limit = min_memsw_limit;
2099 return;
2100}
2101
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002102static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002103{
2104 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002105 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002106
2107 mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002108 type = MEMFILE_TYPE(event);
2109 name = MEMFILE_ATTR(event);
2110 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002111 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002112 if (type == _MEM)
2113 res_counter_reset_max(&mem->res);
2114 else
2115 res_counter_reset_max(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002116 break;
2117 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002118 if (type == _MEM)
2119 res_counter_reset_failcnt(&mem->res);
2120 else
2121 res_counter_reset_failcnt(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002122 break;
2123 }
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07002124 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002125}
2126
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002127
2128/* For read statistics */
2129enum {
2130 MCS_CACHE,
2131 MCS_RSS,
Balbir Singhd69b0422009-06-17 16:26:34 -07002132 MCS_MAPPED_FILE,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002133 MCS_PGPGIN,
2134 MCS_PGPGOUT,
2135 MCS_INACTIVE_ANON,
2136 MCS_ACTIVE_ANON,
2137 MCS_INACTIVE_FILE,
2138 MCS_ACTIVE_FILE,
2139 MCS_UNEVICTABLE,
2140 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002141};
2142
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002143struct mcs_total_stat {
2144 s64 stat[NR_MCS_STAT];
2145};
2146
2147struct {
2148 char *local_name;
2149 char *total_name;
2150} memcg_stat_strings[NR_MCS_STAT] = {
2151 {"cache", "total_cache"},
2152 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07002153 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002154 {"pgpgin", "total_pgpgin"},
2155 {"pgpgout", "total_pgpgout"},
2156 {"inactive_anon", "total_inactive_anon"},
2157 {"active_anon", "total_active_anon"},
2158 {"inactive_file", "total_inactive_file"},
2159 {"active_file", "total_active_file"},
2160 {"unevictable", "total_unevictable"}
2161};
2162
2163
2164static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
2165{
2166 struct mcs_total_stat *s = data;
2167 s64 val;
2168
2169 /* per cpu stat */
2170 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE);
2171 s->stat[MCS_CACHE] += val * PAGE_SIZE;
2172 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
2173 s->stat[MCS_RSS] += val * PAGE_SIZE;
Balbir Singhd69b0422009-06-17 16:26:34 -07002174 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_MAPPED_FILE);
2175 s->stat[MCS_MAPPED_FILE] += val * PAGE_SIZE;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002176 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT);
2177 s->stat[MCS_PGPGIN] += val;
2178 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT);
2179 s->stat[MCS_PGPGOUT] += val;
2180
2181 /* per zone stat */
2182 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
2183 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
2184 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
2185 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
2186 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
2187 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
2188 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
2189 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
2190 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
2191 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
2192 return 0;
2193}
2194
2195static void
2196mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
2197{
2198 mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
2199}
2200
Paul Menagec64745c2008-04-29 01:00:02 -07002201static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
2202 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002203{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002204 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002205 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002206 int i;
2207
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002208 memset(&mystat, 0, sizeof(mystat));
2209 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002210
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002211 for (i = 0; i < NR_MCS_STAT; i++)
2212 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002213
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002214 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08002215 {
2216 unsigned long long limit, memsw_limit;
2217 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
2218 cb->fill(cb, "hierarchical_memory_limit", limit);
2219 if (do_swap_account)
2220 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
2221 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002222
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002223 memset(&mystat, 0, sizeof(mystat));
2224 mem_cgroup_get_total_stat(mem_cont, &mystat);
2225 for (i = 0; i < NR_MCS_STAT; i++)
2226 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
2227
2228
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002229#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08002230 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002231
2232 {
2233 int nid, zid;
2234 struct mem_cgroup_per_zone *mz;
2235 unsigned long recent_rotated[2] = {0, 0};
2236 unsigned long recent_scanned[2] = {0, 0};
2237
2238 for_each_online_node(nid)
2239 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
2240 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
2241
2242 recent_rotated[0] +=
2243 mz->reclaim_stat.recent_rotated[0];
2244 recent_rotated[1] +=
2245 mz->reclaim_stat.recent_rotated[1];
2246 recent_scanned[0] +=
2247 mz->reclaim_stat.recent_scanned[0];
2248 recent_scanned[1] +=
2249 mz->reclaim_stat.recent_scanned[1];
2250 }
2251 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
2252 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
2253 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
2254 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
2255 }
2256#endif
2257
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002258 return 0;
2259}
2260
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002261static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
2262{
2263 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2264
2265 return get_swappiness(memcg);
2266}
2267
2268static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
2269 u64 val)
2270{
2271 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2272 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08002273
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002274 if (val > 100)
2275 return -EINVAL;
2276
2277 if (cgrp->parent == NULL)
2278 return -EINVAL;
2279
2280 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08002281
2282 cgroup_lock();
2283
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002284 /* If under hierarchy, only empty-root can set this value */
2285 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08002286 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
2287 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002288 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08002289 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002290
2291 spin_lock(&memcg->reclaim_param_lock);
2292 memcg->swappiness = val;
2293 spin_unlock(&memcg->reclaim_param_lock);
2294
Li Zefan068b38c2009-01-15 13:51:26 -08002295 cgroup_unlock();
2296
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002297 return 0;
2298}
2299
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002300
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002301static struct cftype mem_cgroup_files[] = {
2302 {
Balbir Singh0eea1032008-02-07 00:13:57 -08002303 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002304 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa722008-04-29 00:59:58 -07002305 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002306 },
2307 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002308 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002309 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002310 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002311 .read_u64 = mem_cgroup_read,
2312 },
2313 {
Balbir Singh0eea1032008-02-07 00:13:57 -08002314 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002315 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07002316 .write_string = mem_cgroup_write,
Paul Menage2c3daa722008-04-29 00:59:58 -07002317 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002318 },
2319 {
2320 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002321 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002322 .trigger = mem_cgroup_reset,
Paul Menage2c3daa722008-04-29 00:59:58 -07002323 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002324 },
Balbir Singh8697d332008-02-07 00:13:59 -08002325 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002326 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07002327 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002328 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002329 {
2330 .name = "force_empty",
2331 .trigger = mem_cgroup_force_empty_write,
2332 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08002333 {
2334 .name = "use_hierarchy",
2335 .write_u64 = mem_cgroup_hierarchy_write,
2336 .read_u64 = mem_cgroup_hierarchy_read,
2337 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002338 {
2339 .name = "swappiness",
2340 .read_u64 = mem_cgroup_swappiness_read,
2341 .write_u64 = mem_cgroup_swappiness_write,
2342 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002343};
2344
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002345#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2346static struct cftype memsw_cgroup_files[] = {
2347 {
2348 .name = "memsw.usage_in_bytes",
2349 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
2350 .read_u64 = mem_cgroup_read,
2351 },
2352 {
2353 .name = "memsw.max_usage_in_bytes",
2354 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
2355 .trigger = mem_cgroup_reset,
2356 .read_u64 = mem_cgroup_read,
2357 },
2358 {
2359 .name = "memsw.limit_in_bytes",
2360 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
2361 .write_string = mem_cgroup_write,
2362 .read_u64 = mem_cgroup_read,
2363 },
2364 {
2365 .name = "memsw.failcnt",
2366 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
2367 .trigger = mem_cgroup_reset,
2368 .read_u64 = mem_cgroup_read,
2369 },
2370};
2371
2372static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2373{
2374 if (!do_swap_account)
2375 return 0;
2376 return cgroup_add_files(cont, ss, memsw_cgroup_files,
2377 ARRAY_SIZE(memsw_cgroup_files));
2378};
2379#else
2380static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2381{
2382 return 0;
2383}
2384#endif
2385
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002386static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2387{
2388 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002389 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07002390 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07002391 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002392 /*
2393 * This routine is called against possible nodes.
2394 * But it's BUG to call kmalloc() against offline node.
2395 *
2396 * TODO: this routine can waste much memory for nodes which will
2397 * never be onlined. It's better to use memory hotplug callback
2398 * function.
2399 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07002400 if (!node_state(node, N_NORMAL_MEMORY))
2401 tmp = -1;
2402 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002403 if (!pn)
2404 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002405
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002406 mem->info.nodeinfo[node] = pn;
2407 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002408
2409 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
2410 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07002411 for_each_lru(l)
2412 INIT_LIST_HEAD(&mz->lists[l]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002413 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002414 return 0;
2415}
2416
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002417static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2418{
2419 kfree(mem->info.nodeinfo[node]);
2420}
2421
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002422static int mem_cgroup_size(void)
2423{
2424 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
2425 return sizeof(struct mem_cgroup) + cpustat_size;
2426}
2427
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002428static struct mem_cgroup *mem_cgroup_alloc(void)
2429{
2430 struct mem_cgroup *mem;
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002431 int size = mem_cgroup_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002432
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002433 if (size < PAGE_SIZE)
2434 mem = kmalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002435 else
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002436 mem = vmalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002437
2438 if (mem)
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002439 memset(mem, 0, size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002440 return mem;
2441}
2442
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002443/*
2444 * At destroying mem_cgroup, references from swap_cgroup can remain.
2445 * (scanning all at force_empty is too costly...)
2446 *
2447 * Instead of clearing all references at force_empty, we remember
2448 * the number of reference from swap_cgroup and free mem_cgroup when
2449 * it goes down to 0.
2450 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002451 * Removal of cgroup itself succeeds regardless of refs from swap.
2452 */
2453
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08002454static void __mem_cgroup_free(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002455{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002456 int node;
2457
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002458 free_css_id(&mem_cgroup_subsys, &mem->css);
2459
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002460 for_each_node_state(node, N_POSSIBLE)
2461 free_mem_cgroup_per_zone_info(mem, node);
2462
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002463 if (mem_cgroup_size() < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002464 kfree(mem);
2465 else
2466 vfree(mem);
2467}
2468
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002469static void mem_cgroup_get(struct mem_cgroup *mem)
2470{
2471 atomic_inc(&mem->refcnt);
2472}
2473
2474static void mem_cgroup_put(struct mem_cgroup *mem)
2475{
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08002476 if (atomic_dec_and_test(&mem->refcnt)) {
2477 struct mem_cgroup *parent = parent_mem_cgroup(mem);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08002478 __mem_cgroup_free(mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08002479 if (parent)
2480 mem_cgroup_put(parent);
2481 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002482}
2483
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08002484/*
2485 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
2486 */
2487static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
2488{
2489 if (!mem->res.parent)
2490 return NULL;
2491 return mem_cgroup_from_res_counter(mem->res.parent, res);
2492}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002493
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08002494#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2495static void __init enable_swap_cgroup(void)
2496{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002497 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08002498 do_swap_account = 1;
2499}
2500#else
2501static void __init enable_swap_cgroup(void)
2502{
2503}
2504#endif
2505
Li Zefan0eb253e2009-01-15 13:51:25 -08002506static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002507mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
2508{
Balbir Singh28dbc4b2009-01-07 18:08:05 -08002509 struct mem_cgroup *mem, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002510 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002511 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002512
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002513 mem = mem_cgroup_alloc();
2514 if (!mem)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002515 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08002516
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002517 for_each_node_state(node, N_POSSIBLE)
2518 if (alloc_mem_cgroup_per_zone_info(mem, node))
2519 goto free_out;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08002520 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08002521 if (cont->parent == NULL) {
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08002522 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08002523 parent = NULL;
Balbir Singh18f59ea2009-01-07 18:08:07 -08002524 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08002525 parent = mem_cgroup_from_cont(cont->parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08002526 mem->use_hierarchy = parent->use_hierarchy;
2527 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08002528
Balbir Singh18f59ea2009-01-07 18:08:07 -08002529 if (parent && parent->use_hierarchy) {
2530 res_counter_init(&mem->res, &parent->res);
2531 res_counter_init(&mem->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08002532 /*
2533 * We increment refcnt of the parent to ensure that we can
2534 * safely access it on res_counter_charge/uncharge.
2535 * This refcnt will be decremented when freeing this
2536 * mem_cgroup(see mem_cgroup_put).
2537 */
2538 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08002539 } else {
2540 res_counter_init(&mem->res, NULL);
2541 res_counter_init(&mem->memsw, NULL);
2542 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002543 mem->last_scanned_child = 0;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -08002544 spin_lock_init(&mem->reclaim_param_lock);
Balbir Singh6d61ef42009-01-07 18:08:06 -08002545
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002546 if (parent)
2547 mem->swappiness = get_swappiness(parent);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08002548 atomic_set(&mem->refcnt, 1);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002549 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002550free_out:
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08002551 __mem_cgroup_free(mem);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002552 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002553}
2554
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002555static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08002556 struct cgroup *cont)
2557{
2558 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002559
2560 return mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08002561}
2562
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002563static void mem_cgroup_destroy(struct cgroup_subsys *ss,
2564 struct cgroup *cont)
2565{
Daisuke Nishimurac268e992009-01-15 13:51:13 -08002566 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08002567
Daisuke Nishimurac268e992009-01-15 13:51:13 -08002568 mem_cgroup_put(mem);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002569}
2570
2571static int mem_cgroup_populate(struct cgroup_subsys *ss,
2572 struct cgroup *cont)
2573{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002574 int ret;
2575
2576 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
2577 ARRAY_SIZE(mem_cgroup_files));
2578
2579 if (!ret)
2580 ret = register_memsw_files(cont, ss);
2581 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002582}
2583
Balbir Singh67e465a2008-02-07 00:13:54 -08002584static void mem_cgroup_move_task(struct cgroup_subsys *ss,
2585 struct cgroup *cont,
2586 struct cgroup *old_cont,
2587 struct task_struct *p)
2588{
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08002589 mutex_lock(&memcg_tasklist);
Balbir Singh67e465a2008-02-07 00:13:54 -08002590 /*
Nikanth Karthikesanf9717d22009-01-07 18:08:11 -08002591 * FIXME: It's better to move charges of this process from old
2592 * memcg to new memcg. But it's just on TODO-List now.
Balbir Singh67e465a2008-02-07 00:13:54 -08002593 */
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08002594 mutex_unlock(&memcg_tasklist);
Balbir Singh67e465a2008-02-07 00:13:54 -08002595}
2596
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002597struct cgroup_subsys mem_cgroup_subsys = {
2598 .name = "memory",
2599 .subsys_id = mem_cgroup_subsys_id,
2600 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08002601 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002602 .destroy = mem_cgroup_destroy,
2603 .populate = mem_cgroup_populate,
Balbir Singh67e465a2008-02-07 00:13:54 -08002604 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002605 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002606 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002607};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08002608
2609#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2610
2611static int __init disable_swap_account(char *s)
2612{
2613 really_do_swap_account = 0;
2614 return 1;
2615}
2616__setup("noswapaccount", disable_swap_account);
2617#endif