blob: 78eb8552818b6b94d4add7ec06ecdc881add1f67 [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
48/* Turned on only when memory cgroup is enabled && really_do_swap_account = 0 */
49int 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 */
65 MEM_CGROUP_STAT_RSS, /* # of pages charged as rss */
Balaji Rao55e462b2008-05-01 04:35:12 -070066 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
67 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080068
69 MEM_CGROUP_STAT_NSTATS,
70};
71
72struct mem_cgroup_stat_cpu {
73 s64 count[MEM_CGROUP_STAT_NSTATS];
74} ____cacheline_aligned_in_smp;
75
76struct mem_cgroup_stat {
Jan Blunckc8dad2b2009-01-07 18:07:53 -080077 struct mem_cgroup_stat_cpu cpustat[0];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080078};
79
80/*
81 * For accounting under irq disable, no need for increment preempt count.
82 */
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -070083static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080084 enum mem_cgroup_stat_index idx, int val)
85{
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -070086 stat->count[idx] += val;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080087}
88
89static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
90 enum mem_cgroup_stat_index idx)
91{
92 int cpu;
93 s64 ret = 0;
94 for_each_possible_cpu(cpu)
95 ret += stat->cpustat[cpu].count[idx];
96 return ret;
97}
98
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -070099static s64 mem_cgroup_local_usage(struct mem_cgroup_stat *stat)
100{
101 s64 ret;
102
103 ret = mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_CACHE);
104 ret += mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_RSS);
105 return ret;
106}
107
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800108/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800109 * per-zone information in memory controller.
110 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800111struct mem_cgroup_per_zone {
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800112 /*
113 * spin_lock to protect the per cgroup LRU
114 */
Christoph Lameterb69408e2008-10-18 20:26:14 -0700115 struct list_head lists[NR_LRU_LISTS];
116 unsigned long count[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800117
118 struct zone_reclaim_stat reclaim_stat;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800119};
120/* Macro for accessing counter */
121#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
122
123struct mem_cgroup_per_node {
124 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
125};
126
127struct mem_cgroup_lru_info {
128 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
129};
130
131/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800132 * The memory controller data structure. The memory controller controls both
133 * page cache and RSS per cgroup. We would eventually like to provide
134 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
135 * to help the administrator determine what knobs to tune.
136 *
137 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800138 * we hit the water mark. May be even add a low water mark, such that
139 * no reclaim occurs from a cgroup at it's low water mark, this is
140 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800141 */
142struct mem_cgroup {
143 struct cgroup_subsys_state css;
144 /*
145 * the counter to account for memory usage
146 */
147 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800148 /*
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800149 * the counter to account for mem+swap usage.
150 */
151 struct res_counter memsw;
152 /*
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800153 * Per cgroup active and inactive list, similar to the
154 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800155 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800156 struct mem_cgroup_lru_info info;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800157
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800158 /*
159 protect against reclaim related member.
160 */
161 spinlock_t reclaim_param_lock;
162
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800163 int prev_priority; /* for recording reclaim priority */
Balbir Singh6d61ef42009-01-07 18:08:06 -0800164
165 /*
166 * While reclaiming in a hiearchy, we cache the last child we
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700167 * reclaimed from.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800168 */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700169 int last_scanned_child;
Balbir Singh18f59ea2009-01-07 18:08:07 -0800170 /*
171 * Should the accounting and control be hierarchical, per subtree?
172 */
173 bool use_hierarchy;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800174 unsigned long last_oom_jiffies;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800175 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800176
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800177 unsigned int swappiness;
178
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800179 /*
Jan Blunckc8dad2b2009-01-07 18:07:53 -0800180 * statistics. This must be placed at the end of memcg.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800181 */
182 struct mem_cgroup_stat stat;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800183};
184
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800185enum charge_type {
186 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
187 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700188 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700189 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800190 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700191 NR_CHARGE_TYPE,
192};
193
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700194/* only for here (for easy reading.) */
195#define PCGF_CACHE (1UL << PCG_CACHE)
196#define PCGF_USED (1UL << PCG_USED)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700197#define PCGF_LOCK (1UL << PCG_LOCK)
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700198static const unsigned long
199pcg_default_flags[NR_CHARGE_TYPE] = {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800200 PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* File Cache */
201 PCGF_USED | PCGF_LOCK, /* Anon */
202 PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* Shmem */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700203 0, /* FORCE */
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800204};
205
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800206/* for encoding cft->private value on file */
207#define _MEM (0)
208#define _MEMSWAP (1)
209#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
210#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
211#define MEMFILE_ATTR(val) ((val) & 0xffff)
212
213static void mem_cgroup_get(struct mem_cgroup *mem);
214static void mem_cgroup_put(struct mem_cgroup *mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -0800215static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800216
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700217static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
218 struct page_cgroup *pc,
219 bool charge)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800220{
221 int val = (charge)? 1 : -1;
222 struct mem_cgroup_stat *stat = &mem->stat;
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700223 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800224 int cpu = get_cpu();
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800225
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800226 cpustat = &stat->cpustat[cpu];
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700227 if (PageCgroupCache(pc))
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700228 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800229 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700230 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700231
232 if (charge)
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700233 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700234 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
235 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700236 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700237 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800238 put_cpu();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800239}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800240
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800241static struct mem_cgroup_per_zone *
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800242mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
243{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800244 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
245}
246
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800247static struct mem_cgroup_per_zone *
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800248page_cgroup_zoneinfo(struct page_cgroup *pc)
249{
250 struct mem_cgroup *mem = pc->mem_cgroup;
251 int nid = page_cgroup_nid(pc);
252 int zid = page_cgroup_zid(pc);
253
KOSAKI Motohiro54992762009-01-07 18:08:18 -0800254 if (!mem)
255 return NULL;
256
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800257 return mem_cgroup_zoneinfo(mem, nid, zid);
258}
259
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700260static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700261 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800262{
263 int nid, zid;
264 struct mem_cgroup_per_zone *mz;
265 u64 total = 0;
266
267 for_each_online_node(nid)
268 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
269 mz = mem_cgroup_zoneinfo(mem, nid, zid);
270 total += MEM_CGROUP_ZSTAT(mz, idx);
271 }
272 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800273}
274
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800275static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800276{
277 return container_of(cgroup_subsys_state(cont,
278 mem_cgroup_subsys_id), struct mem_cgroup,
279 css);
280}
281
Balbir Singhcf475ad2008-04-29 01:00:16 -0700282struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800283{
Balbir Singh31a78f22008-09-28 23:09:31 +0100284 /*
285 * mm_update_next_owner() may clear mm->owner to NULL
286 * if it races with swapoff, page migration, etc.
287 * So this can be called with p == NULL.
288 */
289 if (unlikely(!p))
290 return NULL;
291
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800292 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
293 struct mem_cgroup, css);
294}
295
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800296static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
297{
298 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700299
300 if (!mm)
301 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800302 /*
303 * Because we have no locks, mm->owner's may be being moved to other
304 * cgroup. We use css_tryget() here even if this looks
305 * pessimistic (rather than adding locks here).
306 */
307 rcu_read_lock();
308 do {
309 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
310 if (unlikely(!mem))
311 break;
312 } while (!css_tryget(&mem->css));
313 rcu_read_unlock();
314 return mem;
315}
316
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700317/*
318 * Call callback function against all cgroup under hierarchy tree.
319 */
320static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
321 int (*func)(struct mem_cgroup *, void *))
322{
323 int found, ret, nextid;
324 struct cgroup_subsys_state *css;
325 struct mem_cgroup *mem;
326
327 if (!root->use_hierarchy)
328 return (*func)(root, data);
329
330 nextid = 1;
331 do {
332 ret = 0;
333 mem = NULL;
334
335 rcu_read_lock();
336 css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
337 &found);
338 if (css && css_tryget(css))
339 mem = container_of(css, struct mem_cgroup, css);
340 rcu_read_unlock();
341
342 if (mem) {
343 ret = (*func)(mem, data);
344 css_put(&mem->css);
345 }
346 nextid = found + 1;
347 } while (!ret && css);
348
349 return ret;
350}
351
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800352/*
353 * Following LRU functions are allowed to be used without PCG_LOCK.
354 * Operations are called by routine of global LRU independently from memcg.
355 * What we have to take care of here is validness of pc->mem_cgroup.
356 *
357 * Changes to pc->mem_cgroup happens when
358 * 1. charge
359 * 2. moving account
360 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
361 * It is added to LRU before charge.
362 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
363 * When moving account, the page is not on LRU. It's isolated.
364 */
365
366void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800367{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800368 struct page_cgroup *pc;
369 struct mem_cgroup *mem;
370 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700371
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800372 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800373 return;
374 pc = lookup_page_cgroup(page);
375 /* can happen while we handle swapcache. */
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800376 if (list_empty(&pc->lru) || !pc->mem_cgroup)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800377 return;
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800378 /*
379 * We don't check PCG_USED bit. It's cleared when the "page" is finally
380 * removed from global LRU.
381 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800382 mz = page_cgroup_zoneinfo(pc);
383 mem = pc->mem_cgroup;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700384 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800385 list_del_init(&pc->lru);
386 return;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800387}
388
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800389void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800390{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800391 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800392}
393
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800394void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800395{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800396 struct mem_cgroup_per_zone *mz;
397 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800398
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800399 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700400 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700401
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800402 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800403 /*
404 * Used bit is set without atomic ops but after smp_wmb().
405 * For making pc->mem_cgroup visible, insert smp_rmb() here.
406 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800407 smp_rmb();
408 /* unused page is not rotated. */
409 if (!PageCgroupUsed(pc))
410 return;
411 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700412 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800413}
414
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800415void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
416{
417 struct page_cgroup *pc;
418 struct mem_cgroup_per_zone *mz;
419
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800420 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800421 return;
422 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800423 /*
424 * Used bit is set without atomic ops but after smp_wmb().
425 * For making pc->mem_cgroup visible, insert smp_rmb() here.
426 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800427 smp_rmb();
428 if (!PageCgroupUsed(pc))
429 return;
430
431 mz = page_cgroup_zoneinfo(pc);
432 MEM_CGROUP_ZSTAT(mz, lru) += 1;
433 list_add(&pc->lru, &mz->lists[lru]);
434}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800435
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800436/*
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800437 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
438 * lru because the page may.be reused after it's fully uncharged (because of
439 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
440 * it again. This function is only used to charge SwapCache. It's done under
441 * lock_page and expected that zone->lru_lock is never held.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800442 */
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800443static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800444{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800445 unsigned long flags;
446 struct zone *zone = page_zone(page);
447 struct page_cgroup *pc = lookup_page_cgroup(page);
448
449 spin_lock_irqsave(&zone->lru_lock, flags);
450 /*
451 * Forget old LRU when this page_cgroup is *not* used. This Used bit
452 * is guarded by lock_page() because the page is SwapCache.
453 */
454 if (!PageCgroupUsed(pc))
455 mem_cgroup_del_lru_list(page, page_lru(page));
456 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800457}
458
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800459static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
460{
461 unsigned long flags;
462 struct zone *zone = page_zone(page);
463 struct page_cgroup *pc = lookup_page_cgroup(page);
464
465 spin_lock_irqsave(&zone->lru_lock, flags);
466 /* link when the page is linked to LRU but page_cgroup isn't */
467 if (PageLRU(page) && list_empty(&pc->lru))
468 mem_cgroup_add_lru_list(page, page_lru(page));
469 spin_unlock_irqrestore(&zone->lru_lock, flags);
470}
471
472
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800473void mem_cgroup_move_lists(struct page *page,
474 enum lru_list from, enum lru_list to)
475{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800476 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800477 return;
478 mem_cgroup_del_lru_list(page, from);
479 mem_cgroup_add_lru_list(page, to);
480}
481
David Rientjes4c4a2212008-02-07 00:14:06 -0800482int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
483{
484 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700485 struct mem_cgroup *curr = NULL;
David Rientjes4c4a2212008-02-07 00:14:06 -0800486
487 task_lock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700488 rcu_read_lock();
489 curr = try_get_mem_cgroup_from_mm(task->mm);
490 rcu_read_unlock();
David Rientjes4c4a2212008-02-07 00:14:06 -0800491 task_unlock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700492 if (!curr)
493 return 0;
494 if (curr->use_hierarchy)
495 ret = css_is_ancestor(&curr->css, &mem->css);
496 else
497 ret = (curr == mem);
498 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -0800499 return ret;
500}
501
Balbir Singh66e17072008-02-07 00:13:56 -0800502/*
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800503 * prev_priority control...this will be used in memory reclaim path.
504 */
505int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
506{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800507 int prev_priority;
508
509 spin_lock(&mem->reclaim_param_lock);
510 prev_priority = mem->prev_priority;
511 spin_unlock(&mem->reclaim_param_lock);
512
513 return prev_priority;
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800514}
515
516void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
517{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800518 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800519 if (priority < mem->prev_priority)
520 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800521 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800522}
523
524void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
525{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800526 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800527 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800528 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800529}
530
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800531static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800532{
533 unsigned long active;
534 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800535 unsigned long gb;
536 unsigned long inactive_ratio;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800537
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700538 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
539 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800540
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800541 gb = (inactive + active) >> (30 - PAGE_SHIFT);
542 if (gb)
543 inactive_ratio = int_sqrt(10 * gb);
544 else
545 inactive_ratio = 1;
546
547 if (present_pages) {
548 present_pages[0] = inactive;
549 present_pages[1] = active;
550 }
551
552 return inactive_ratio;
553}
554
555int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
556{
557 unsigned long active;
558 unsigned long inactive;
559 unsigned long present_pages[2];
560 unsigned long inactive_ratio;
561
562 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
563
564 inactive = present_pages[0];
565 active = present_pages[1];
566
567 if (inactive * inactive_ratio < active)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800568 return 1;
569
570 return 0;
571}
572
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800573unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
574 struct zone *zone,
575 enum lru_list lru)
576{
577 int nid = zone->zone_pgdat->node_id;
578 int zid = zone_idx(zone);
579 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
580
581 return MEM_CGROUP_ZSTAT(mz, lru);
582}
583
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800584struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
585 struct zone *zone)
586{
587 int nid = zone->zone_pgdat->node_id;
588 int zid = zone_idx(zone);
589 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
590
591 return &mz->reclaim_stat;
592}
593
594struct zone_reclaim_stat *
595mem_cgroup_get_reclaim_stat_from_page(struct page *page)
596{
597 struct page_cgroup *pc;
598 struct mem_cgroup_per_zone *mz;
599
600 if (mem_cgroup_disabled())
601 return NULL;
602
603 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800604 /*
605 * Used bit is set without atomic ops but after smp_wmb().
606 * For making pc->mem_cgroup visible, insert smp_rmb() here.
607 */
608 smp_rmb();
609 if (!PageCgroupUsed(pc))
610 return NULL;
611
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800612 mz = page_cgroup_zoneinfo(pc);
613 if (!mz)
614 return NULL;
615
616 return &mz->reclaim_stat;
617}
618
Balbir Singh66e17072008-02-07 00:13:56 -0800619unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
620 struct list_head *dst,
621 unsigned long *scanned, int order,
622 int mode, struct zone *z,
623 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700624 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -0800625{
626 unsigned long nr_taken = 0;
627 struct page *page;
628 unsigned long scan;
629 LIST_HEAD(pc_list);
630 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800631 struct page_cgroup *pc, *tmp;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800632 int nid = z->zone_pgdat->node_id;
633 int zid = zone_idx(z);
634 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700635 int lru = LRU_FILE * !!file + !!active;
Balbir Singh66e17072008-02-07 00:13:56 -0800636
Balbir Singhcf475ad2008-04-29 01:00:16 -0700637 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800638 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700639 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -0800640
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800641 scan = 0;
642 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -0800643 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800644 break;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800645
646 page = pc->page;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700647 if (unlikely(!PageCgroupUsed(pc)))
648 continue;
Hugh Dickins436c65412008-02-07 00:14:12 -0800649 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800650 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800651
Hugh Dickins436c65412008-02-07 00:14:12 -0800652 scan++;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700653 if (__isolate_lru_page(page, mode, file) == 0) {
Balbir Singh66e17072008-02-07 00:13:56 -0800654 list_move(&page->lru, dst);
655 nr_taken++;
656 }
657 }
658
Balbir Singh66e17072008-02-07 00:13:56 -0800659 *scanned = scan;
660 return nr_taken;
661}
662
Balbir Singh6d61ef42009-01-07 18:08:06 -0800663#define mem_cgroup_from_res_counter(counter, member) \
664 container_of(counter, struct mem_cgroup, member)
665
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -0800666static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
667{
668 if (do_swap_account) {
669 if (res_counter_check_under_limit(&mem->res) &&
670 res_counter_check_under_limit(&mem->memsw))
671 return true;
672 } else
673 if (res_counter_check_under_limit(&mem->res))
674 return true;
675 return false;
676}
677
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800678static unsigned int get_swappiness(struct mem_cgroup *memcg)
679{
680 struct cgroup *cgrp = memcg->css.cgroup;
681 unsigned int swappiness;
682
683 /* root ? */
684 if (cgrp->parent == NULL)
685 return vm_swappiness;
686
687 spin_lock(&memcg->reclaim_param_lock);
688 swappiness = memcg->swappiness;
689 spin_unlock(&memcg->reclaim_param_lock);
690
691 return swappiness;
692}
693
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700694static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
695{
696 int *val = data;
697 (*val)++;
698 return 0;
699}
Balbir Singhe2224322009-04-02 16:57:39 -0700700
701/**
702 * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode.
703 * @memcg: The memory cgroup that went over limit
704 * @p: Task that is going to be killed
705 *
706 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
707 * enabled
708 */
709void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
710{
711 struct cgroup *task_cgrp;
712 struct cgroup *mem_cgrp;
713 /*
714 * Need a buffer in BSS, can't rely on allocations. The code relies
715 * on the assumption that OOM is serialized for memory controller.
716 * If this assumption is broken, revisit this code.
717 */
718 static char memcg_name[PATH_MAX];
719 int ret;
720
721 if (!memcg)
722 return;
723
724
725 rcu_read_lock();
726
727 mem_cgrp = memcg->css.cgroup;
728 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
729
730 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
731 if (ret < 0) {
732 /*
733 * Unfortunately, we are unable to convert to a useful name
734 * But we'll still print out the usage information
735 */
736 rcu_read_unlock();
737 goto done;
738 }
739 rcu_read_unlock();
740
741 printk(KERN_INFO "Task in %s killed", memcg_name);
742
743 rcu_read_lock();
744 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
745 if (ret < 0) {
746 rcu_read_unlock();
747 goto done;
748 }
749 rcu_read_unlock();
750
751 /*
752 * Continues from above, so we don't need an KERN_ level
753 */
754 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
755done:
756
757 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
758 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
759 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
760 res_counter_read_u64(&memcg->res, RES_FAILCNT));
761 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
762 "failcnt %llu\n",
763 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
764 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
765 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
766}
767
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700768/*
769 * This function returns the number of memcg under hierarchy tree. Returns
770 * 1(self count) if no children.
771 */
772static int mem_cgroup_count_children(struct mem_cgroup *mem)
773{
774 int num = 0;
775 mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
776 return num;
777}
778
Balbir Singh6d61ef42009-01-07 18:08:06 -0800779/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700780 * Visit the first child (need not be the first child as per the ordering
781 * of the cgroup list, since we track last_scanned_child) of @mem and use
782 * that to reclaim free pages from.
783 */
784static struct mem_cgroup *
785mem_cgroup_select_victim(struct mem_cgroup *root_mem)
786{
787 struct mem_cgroup *ret = NULL;
788 struct cgroup_subsys_state *css;
789 int nextid, found;
790
791 if (!root_mem->use_hierarchy) {
792 css_get(&root_mem->css);
793 ret = root_mem;
794 }
795
796 while (!ret) {
797 rcu_read_lock();
798 nextid = root_mem->last_scanned_child + 1;
799 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
800 &found);
801 if (css && css_tryget(css))
802 ret = container_of(css, struct mem_cgroup, css);
803
804 rcu_read_unlock();
805 /* Updates scanning parameter */
806 spin_lock(&root_mem->reclaim_param_lock);
807 if (!css) {
808 /* this means start scan from ID:1 */
809 root_mem->last_scanned_child = 0;
810 } else
811 root_mem->last_scanned_child = found;
812 spin_unlock(&root_mem->reclaim_param_lock);
813 }
814
815 return ret;
816}
817
818/*
819 * Scan the hierarchy if needed to reclaim memory. We remember the last child
820 * we reclaimed from, so that we don't end up penalizing one child extensively
821 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800822 *
823 * root_mem is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700824 *
825 * We give up and return to the caller when we visit root_mem twice.
826 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700827 *
828 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800829 */
830static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700831 gfp_t gfp_mask, bool noswap, bool shrink)
Balbir Singh6d61ef42009-01-07 18:08:06 -0800832{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700833 struct mem_cgroup *victim;
834 int ret, total = 0;
835 int loop = 0;
Balbir Singh6d61ef42009-01-07 18:08:06 -0800836
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700837 while (loop < 2) {
838 victim = mem_cgroup_select_victim(root_mem);
839 if (victim == root_mem)
840 loop++;
841 if (!mem_cgroup_local_usage(&victim->stat)) {
842 /* this cgroup's local usage == 0 */
843 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -0800844 continue;
845 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700846 /* we use swappiness of local cgroup */
847 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask, noswap,
848 get_swappiness(victim));
849 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700850 /*
851 * At shrinking usage, we can't check we should stop here or
852 * reclaim more. It's depends on callers. last_scanned_child
853 * will work enough for keeping fairness under tree.
854 */
855 if (shrink)
856 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700857 total += ret;
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -0800858 if (mem_cgroup_check_under_limit(root_mem))
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700859 return 1 + total;
Balbir Singh6d61ef42009-01-07 18:08:06 -0800860 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700861 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -0800862}
863
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800864bool mem_cgroup_oom_called(struct task_struct *task)
865{
866 bool ret = false;
867 struct mem_cgroup *mem;
868 struct mm_struct *mm;
869
870 rcu_read_lock();
871 mm = task->mm;
872 if (!mm)
873 mm = &init_mm;
874 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
875 if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
876 ret = true;
877 rcu_read_unlock();
878 return ret;
879}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700880
881static int record_last_oom_cb(struct mem_cgroup *mem, void *data)
882{
883 mem->last_oom_jiffies = jiffies;
884 return 0;
885}
886
887static void record_last_oom(struct mem_cgroup *mem)
888{
889 mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb);
890}
891
892
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800893/*
894 * Unlike exported interface, "oom" parameter is added. if oom==true,
895 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800896 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800897static int __mem_cgroup_try_charge(struct mm_struct *mm,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800898 gfp_t gfp_mask, struct mem_cgroup **memcg,
899 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800900{
Balbir Singh6d61ef42009-01-07 18:08:06 -0800901 struct mem_cgroup *mem, *mem_over_limit;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800902 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Balbir Singh28dbc4b2009-01-07 18:08:05 -0800903 struct res_counter *fail_res;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800904
905 if (unlikely(test_thread_flag(TIF_MEMDIE))) {
906 /* Don't account this! */
907 *memcg = NULL;
908 return 0;
909 }
910
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800911 /*
Hugh Dickins3be91272008-02-07 00:14:19 -0800912 * We always charge the cgroup the mm_struct belongs to.
913 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800914 * thread group leader migrates. It's possible that mm is not
915 * set, if so charge the init_mm (happens for pagecache usage).
916 */
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800917 mem = *memcg;
918 if (likely(!mem)) {
919 mem = try_get_mem_cgroup_from_mm(mm);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800920 *memcg = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700921 } else {
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800922 css_get(&mem->css);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700923 }
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800924 if (unlikely(!mem))
925 return 0;
926
Nikanth Karthikesan46f7e602009-05-28 14:34:41 -0700927 VM_BUG_ON(css_is_removed(&mem->css));
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800928
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800929 while (1) {
930 int ret;
931 bool noswap = false;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800932
Balbir Singh28dbc4b2009-01-07 18:08:05 -0800933 ret = res_counter_charge(&mem->res, PAGE_SIZE, &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800934 if (likely(!ret)) {
935 if (!do_swap_account)
936 break;
Balbir Singh28dbc4b2009-01-07 18:08:05 -0800937 ret = res_counter_charge(&mem->memsw, PAGE_SIZE,
938 &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800939 if (likely(!ret))
940 break;
941 /* mem+swap counter fails */
942 res_counter_uncharge(&mem->res, PAGE_SIZE);
943 noswap = true;
Balbir Singh6d61ef42009-01-07 18:08:06 -0800944 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
945 memsw);
946 } else
947 /* mem counter fails */
948 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
949 res);
950
Hugh Dickins3be91272008-02-07 00:14:19 -0800951 if (!(gfp_mask & __GFP_WAIT))
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800952 goto nomem;
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800953
Balbir Singh6d61ef42009-01-07 18:08:06 -0800954 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, gfp_mask,
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700955 noswap, false);
Daisuke Nishimura4d1c6272009-01-15 13:51:14 -0800956 if (ret)
957 continue;
Balbir Singh66e17072008-02-07 00:13:56 -0800958
959 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800960 * try_to_free_mem_cgroup_pages() might not give us a full
961 * picture of reclaim. Some pages are reclaimed and might be
962 * moved to swap cache or just unmapped from the cgroup.
963 * Check the limit again to see if the reclaim reduced the
964 * current usage of the cgroup before giving up
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800965 *
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800966 */
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -0800967 if (mem_cgroup_check_under_limit(mem_over_limit))
968 continue;
Hugh Dickins3be91272008-02-07 00:14:19 -0800969
970 if (!nr_retries--) {
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800971 if (oom) {
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -0800972 mutex_lock(&memcg_tasklist);
KAMEZAWA Hiroyuki88700752009-01-07 18:08:09 -0800973 mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -0800974 mutex_unlock(&memcg_tasklist);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700975 record_last_oom(mem_over_limit);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800976 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800977 goto nomem;
Balbir Singh66e17072008-02-07 00:13:56 -0800978 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800979 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800980 return 0;
981nomem:
982 css_put(&mem->css);
983 return -ENOMEM;
984}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800985
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -0700986
987/*
988 * A helper function to get mem_cgroup from ID. must be called under
989 * rcu_read_lock(). The caller must check css_is_removed() or some if
990 * it's concern. (dropping refcnt from swap can be called against removed
991 * memcg.)
992 */
993static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
994{
995 struct cgroup_subsys_state *css;
996
997 /* ID 0 is unused ID */
998 if (!id)
999 return NULL;
1000 css = css_lookup(&mem_cgroup_subsys, id);
1001 if (!css)
1002 return NULL;
1003 return container_of(css, struct mem_cgroup, css);
1004}
1005
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001006static struct mem_cgroup *try_get_mem_cgroup_from_swapcache(struct page *page)
1007{
1008 struct mem_cgroup *mem;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001009 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001010 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001011 swp_entry_t ent;
1012
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001013 VM_BUG_ON(!PageLocked(page));
1014
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001015 if (!PageSwapCache(page))
1016 return NULL;
1017
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001018 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001019 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001020 if (PageCgroupUsed(pc)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001021 mem = pc->mem_cgroup;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001022 if (mem && !css_tryget(&mem->css))
1023 mem = NULL;
1024 } else {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001025 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001026 id = lookup_swap_cgroup(ent);
1027 rcu_read_lock();
1028 mem = mem_cgroup_lookup(id);
1029 if (mem && !css_tryget(&mem->css))
1030 mem = NULL;
1031 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001032 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001033 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001034 return mem;
1035}
1036
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001037/*
Daisuke Nishimuraa5e924f2009-01-07 18:08:28 -08001038 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001039 * USED state. If already USED, uncharge and return.
1040 */
1041
1042static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1043 struct page_cgroup *pc,
1044 enum charge_type ctype)
1045{
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001046 /* try_charge() can return NULL to *memcg, taking care of it. */
1047 if (!mem)
1048 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001049
1050 lock_page_cgroup(pc);
1051 if (unlikely(PageCgroupUsed(pc))) {
1052 unlock_page_cgroup(pc);
1053 res_counter_uncharge(&mem->res, PAGE_SIZE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001054 if (do_swap_account)
1055 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001056 css_put(&mem->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001057 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001058 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001059 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001060 smp_wmb();
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -07001061 pc->flags = pcg_default_flags[ctype];
Hugh Dickins3be91272008-02-07 00:14:19 -08001062
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001063 mem_cgroup_charge_statistics(mem, pc, true);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001064
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001065 unlock_page_cgroup(pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001066}
1067
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001068/**
1069 * mem_cgroup_move_account - move account of the page
1070 * @pc: page_cgroup of the page.
1071 * @from: mem_cgroup which the page is moved from.
1072 * @to: mem_cgroup which the page is moved to. @from != @to.
1073 *
1074 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001075 * - page is not on LRU (isolate_page() is useful.)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001076 *
1077 * returns 0 at success,
1078 * returns -EBUSY when lock is busy or "pc" is unstable.
1079 *
1080 * This function does "uncharge" from old cgroup but doesn't do "charge" to
1081 * new cgroup. It should be done by a caller.
1082 */
1083
1084static int mem_cgroup_move_account(struct page_cgroup *pc,
1085 struct mem_cgroup *from, struct mem_cgroup *to)
1086{
1087 struct mem_cgroup_per_zone *from_mz, *to_mz;
1088 int nid, zid;
1089 int ret = -EBUSY;
1090
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001091 VM_BUG_ON(from == to);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001092 VM_BUG_ON(PageLRU(pc->page));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001093
1094 nid = page_cgroup_nid(pc);
1095 zid = page_cgroup_zid(pc);
1096 from_mz = mem_cgroup_zoneinfo(from, nid, zid);
1097 to_mz = mem_cgroup_zoneinfo(to, nid, zid);
1098
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001099 if (!trylock_page_cgroup(pc))
1100 return ret;
1101
1102 if (!PageCgroupUsed(pc))
1103 goto out;
1104
1105 if (pc->mem_cgroup != from)
1106 goto out;
1107
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001108 res_counter_uncharge(&from->res, PAGE_SIZE);
1109 mem_cgroup_charge_statistics(from, pc, false);
1110 if (do_swap_account)
1111 res_counter_uncharge(&from->memsw, PAGE_SIZE);
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001112 css_put(&from->css);
1113
1114 css_get(&to->css);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001115 pc->mem_cgroup = to;
1116 mem_cgroup_charge_statistics(to, pc, true);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001117 ret = 0;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001118out:
1119 unlock_page_cgroup(pc);
1120 return ret;
1121}
1122
1123/*
1124 * move charges to its parent.
1125 */
1126
1127static int mem_cgroup_move_parent(struct page_cgroup *pc,
1128 struct mem_cgroup *child,
1129 gfp_t gfp_mask)
1130{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001131 struct page *page = pc->page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001132 struct cgroup *cg = child->css.cgroup;
1133 struct cgroup *pcg = cg->parent;
1134 struct mem_cgroup *parent;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001135 int ret;
1136
1137 /* Is ROOT ? */
1138 if (!pcg)
1139 return -EINVAL;
1140
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001141
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001142 parent = mem_cgroup_from_cont(pcg);
1143
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001144
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001145 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001146 if (ret || !parent)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001147 return ret;
1148
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001149 if (!get_page_unless_zero(page)) {
1150 ret = -EBUSY;
1151 goto uncharge;
1152 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001153
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001154 ret = isolate_lru_page(page);
1155
1156 if (ret)
1157 goto cancel;
1158
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001159 ret = mem_cgroup_move_account(pc, child, parent);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001160
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001161 putback_lru_page(page);
1162 if (!ret) {
1163 put_page(page);
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001164 /* drop extra refcnt by try_charge() */
1165 css_put(&parent->css);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001166 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001167 }
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001168
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001169cancel:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001170 put_page(page);
1171uncharge:
1172 /* drop extra refcnt by try_charge() */
1173 css_put(&parent->css);
1174 /* uncharge if move fails */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001175 res_counter_uncharge(&parent->res, PAGE_SIZE);
1176 if (do_swap_account)
1177 res_counter_uncharge(&parent->memsw, PAGE_SIZE);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001178 return ret;
1179}
1180
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001181/*
1182 * Charge the memory controller for page usage.
1183 * Return
1184 * 0 if the charge was successful
1185 * < 0 if the cgroup is over its limit
1186 */
1187static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
1188 gfp_t gfp_mask, enum charge_type ctype,
1189 struct mem_cgroup *memcg)
1190{
1191 struct mem_cgroup *mem;
1192 struct page_cgroup *pc;
1193 int ret;
1194
1195 pc = lookup_page_cgroup(page);
1196 /* can happen at boot */
1197 if (unlikely(!pc))
1198 return 0;
1199 prefetchw(pc);
1200
1201 mem = memcg;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001202 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001203 if (ret || !mem)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001204 return ret;
1205
1206 __mem_cgroup_commit_charge(mem, pc, ctype);
1207 return 0;
1208}
1209
1210int mem_cgroup_newpage_charge(struct page *page,
1211 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001212{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001213 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001214 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001215 if (PageCompound(page))
1216 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001217 /*
1218 * If already mapped, we don't have to account.
1219 * If page cache, page->mapping has address_space.
1220 * But page->mapping may have out-of-use anon_vma pointer,
1221 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
1222 * is NULL.
1223 */
1224 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
1225 return 0;
1226 if (unlikely(!mm))
1227 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001228 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001229 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001230}
1231
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001232static void
1233__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1234 enum charge_type ctype);
1235
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001236int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
1237 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08001238{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001239 struct mem_cgroup *mem = NULL;
1240 int ret;
1241
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001242 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001243 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001244 if (PageCompound(page))
1245 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001246 /*
1247 * Corner case handling. This is called from add_to_page_cache()
1248 * in usual. But some FS (shmem) precharges this page before calling it
1249 * and call add_to_page_cache() with GFP_NOWAIT.
1250 *
1251 * For GFP_NOWAIT case, the page may be pre-charged before calling
1252 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
1253 * charge twice. (It works but has to pay a bit larger cost.)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001254 * And when the page is SwapCache, it should take swap information
1255 * into account. This is under lock_page() now.
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001256 */
1257 if (!(gfp_mask & __GFP_WAIT)) {
1258 struct page_cgroup *pc;
1259
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001260
1261 pc = lookup_page_cgroup(page);
1262 if (!pc)
1263 return 0;
1264 lock_page_cgroup(pc);
1265 if (PageCgroupUsed(pc)) {
1266 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001267 return 0;
1268 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001269 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001270 }
1271
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001272 if (unlikely(!mm && !mem))
Balbir Singh8697d332008-02-07 00:13:59 -08001273 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001274
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -07001275 if (page_is_file_cache(page))
1276 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001277 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001278
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001279 /* shmem */
1280 if (PageSwapCache(page)) {
1281 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1282 if (!ret)
1283 __mem_cgroup_commit_charge_swapin(page, mem,
1284 MEM_CGROUP_CHARGE_TYPE_SHMEM);
1285 } else
1286 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
1287 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001288
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001289 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001290}
1291
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001292/*
1293 * While swap-in, try_charge -> commit or cancel, the page is locked.
1294 * And when try_charge() successfully returns, one refcnt to memcg without
1295 * struct page_cgroup is aquired. This refcnt will be cumsumed by
1296 * "commit()" or removed by "cancel()"
1297 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001298int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
1299 struct page *page,
1300 gfp_t mask, struct mem_cgroup **ptr)
1301{
1302 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001303 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001304
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001305 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001306 return 0;
1307
1308 if (!do_swap_account)
1309 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001310 /*
1311 * A racing thread's fault, or swapoff, may have already updated
1312 * the pte, and even removed page from swap cache: return success
1313 * to go on to do_swap_page()'s pte_same() test, which should fail.
1314 */
1315 if (!PageSwapCache(page))
1316 return 0;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001317 mem = try_get_mem_cgroup_from_swapcache(page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001318 if (!mem)
1319 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001320 *ptr = mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001321 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true);
1322 /* drop extra refcnt from tryget */
1323 css_put(&mem->css);
1324 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001325charge_cur_mm:
1326 if (unlikely(!mm))
1327 mm = &init_mm;
1328 return __mem_cgroup_try_charge(mm, mask, ptr, true);
1329}
1330
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001331static void
1332__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1333 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001334{
1335 struct page_cgroup *pc;
1336
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001337 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001338 return;
1339 if (!ptr)
1340 return;
1341 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001342 mem_cgroup_lru_del_before_commit_swapcache(page);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001343 __mem_cgroup_commit_charge(ptr, pc, ctype);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001344 mem_cgroup_lru_add_after_commit_swapcache(page);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001345 /*
1346 * Now swap is on-memory. This means this page may be
1347 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08001348 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
1349 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
1350 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001351 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08001352 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001353 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001354 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001355 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001356
1357 id = swap_cgroup_record(ent, 0);
1358 rcu_read_lock();
1359 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001360 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001361 /*
1362 * This recorded memcg can be obsolete one. So, avoid
1363 * calling css_tryget
1364 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001365 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1366 mem_cgroup_put(memcg);
1367 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001368 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001369 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001370 /* add this page(page_cgroup) to the LRU we want. */
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001371
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001372}
1373
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001374void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
1375{
1376 __mem_cgroup_commit_charge_swapin(page, ptr,
1377 MEM_CGROUP_CHARGE_TYPE_MAPPED);
1378}
1379
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001380void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
1381{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001382 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001383 return;
1384 if (!mem)
1385 return;
1386 res_counter_uncharge(&mem->res, PAGE_SIZE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001387 if (do_swap_account)
1388 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001389 css_put(&mem->css);
1390}
1391
1392
Balbir Singh8697d332008-02-07 00:13:59 -08001393/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001394 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001395 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001396static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001397__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001398{
Hugh Dickins82895462008-03-04 14:29:08 -08001399 struct page_cgroup *pc;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001400 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001401 struct mem_cgroup_per_zone *mz;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001402
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001403 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001404 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07001405
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001406 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001407 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001408
Balbir Singh8697d332008-02-07 00:13:59 -08001409 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08001410 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08001411 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001412 pc = lookup_page_cgroup(page);
1413 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001414 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001415
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001416 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001417
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001418 mem = pc->mem_cgroup;
1419
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001420 if (!PageCgroupUsed(pc))
1421 goto unlock_out;
1422
1423 switch (ctype) {
1424 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1425 if (page_mapped(page))
1426 goto unlock_out;
1427 break;
1428 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
1429 if (!PageAnon(page)) { /* Shared memory */
1430 if (page->mapping && !page_is_file_cache(page))
1431 goto unlock_out;
1432 } else if (page_mapped(page)) /* Anon */
1433 goto unlock_out;
1434 break;
1435 default:
1436 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001437 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001438
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001439 res_counter_uncharge(&mem->res, PAGE_SIZE);
1440 if (do_swap_account && (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT))
1441 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001442 mem_cgroup_charge_statistics(mem, pc, false);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001443
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001444 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001445 /*
1446 * pc->mem_cgroup is not cleared here. It will be accessed when it's
1447 * freed from LRU. This is safe because uncharged page is expected not
1448 * to be reused (freed soon). Exception is SwapCache, it's handled by
1449 * special functions.
1450 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08001451
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001452 mz = page_cgroup_zoneinfo(pc);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001453 unlock_page_cgroup(pc);
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08001454
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08001455 /* at swapout, this memcg will be accessed to record to swap */
1456 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1457 css_put(&mem->css);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001458
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001459 return mem;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001460
1461unlock_out:
1462 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001463 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08001464}
1465
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001466void mem_cgroup_uncharge_page(struct page *page)
1467{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001468 /* early check. */
1469 if (page_mapped(page))
1470 return;
1471 if (page->mapping && !PageAnon(page))
1472 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001473 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
1474}
1475
1476void mem_cgroup_uncharge_cache_page(struct page *page)
1477{
1478 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07001479 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001480 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
1481}
1482
Daisuke Nishimurae767e052009-05-28 14:34:28 -07001483#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001484/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07001485 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001486 * memcg information is recorded to swap_cgroup of "ent"
1487 */
1488void mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001489{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001490 struct mem_cgroup *memcg;
1491
1492 memcg = __mem_cgroup_uncharge_common(page,
1493 MEM_CGROUP_CHARGE_TYPE_SWAPOUT);
1494 /* record memcg information */
1495 if (do_swap_account && memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001496 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001497 mem_cgroup_get(memcg);
1498 }
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08001499 if (memcg)
1500 css_put(&memcg->css);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001501}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07001502#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001503
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001504#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1505/*
1506 * called from swap_entry_free(). remove record in swap_cgroup and
1507 * uncharge "memsw" account.
1508 */
1509void mem_cgroup_uncharge_swap(swp_entry_t ent)
1510{
1511 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001512 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001513
1514 if (!do_swap_account)
1515 return;
1516
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001517 id = swap_cgroup_record(ent, 0);
1518 rcu_read_lock();
1519 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001520 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001521 /*
1522 * We uncharge this because swap is freed.
1523 * This memcg can be obsolete one. We avoid calling css_tryget
1524 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001525 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1526 mem_cgroup_put(memcg);
1527 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001528 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001529}
1530#endif
1531
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001532/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001533 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
1534 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001535 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001536int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001537{
1538 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001539 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001540 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001541
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001542 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07001543 return 0;
1544
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001545 pc = lookup_page_cgroup(page);
1546 lock_page_cgroup(pc);
1547 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001548 mem = pc->mem_cgroup;
1549 css_get(&mem->css);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08001550 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001551 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001552
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001553 if (mem) {
Daisuke Nishimura3bb4edf2009-01-07 18:08:28 -08001554 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001555 css_put(&mem->css);
1556 }
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001557 *ptr = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001558 return ret;
1559}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08001560
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001561/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001562void mem_cgroup_end_migration(struct mem_cgroup *mem,
1563 struct page *oldpage, struct page *newpage)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001564{
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001565 struct page *target, *unused;
1566 struct page_cgroup *pc;
1567 enum charge_type ctype;
1568
1569 if (!mem)
1570 return;
1571
1572 /* at migration success, oldpage->mapping is NULL. */
1573 if (oldpage->mapping) {
1574 target = oldpage;
1575 unused = NULL;
1576 } else {
1577 target = newpage;
1578 unused = oldpage;
1579 }
1580
1581 if (PageAnon(target))
1582 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
1583 else if (page_is_file_cache(target))
1584 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
1585 else
1586 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
1587
1588 /* unused page is not on radix-tree now. */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001589 if (unused)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001590 __mem_cgroup_uncharge_common(unused, ctype);
1591
1592 pc = lookup_page_cgroup(target);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001593 /*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001594 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
1595 * So, double-counting is effectively avoided.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001596 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001597 __mem_cgroup_commit_charge(mem, pc, ctype);
1598
1599 /*
1600 * Both of oldpage and newpage are still under lock_page().
1601 * Then, we don't have to care about race in radix-tree.
1602 * But we have to be careful that this page is unmapped or not.
1603 *
1604 * There is a case for !page_mapped(). At the start of
1605 * migration, oldpage was mapped. But now, it's zapped.
1606 * But we know *target* page is not freed/reused under us.
1607 * mem_cgroup_uncharge_page() does all necessary checks.
1608 */
1609 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
1610 mem_cgroup_uncharge_page(target);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001611}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001612
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001613/*
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07001614 * A call to try to shrink memory usage on charge failure at shmem's swapin.
1615 * Calling hierarchical_reclaim is not enough because we should update
1616 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
1617 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
1618 * not from the memcg which this page would be charged to.
1619 * try_charge_swapin does all of these works properly.
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001620 */
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07001621int mem_cgroup_shmem_charge_fallback(struct page *page,
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001622 struct mm_struct *mm,
1623 gfp_t gfp_mask)
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001624{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001625 struct mem_cgroup *mem = NULL;
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07001626 int ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001627
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001628 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001629 return 0;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001630
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07001631 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1632 if (!ret)
1633 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001634
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07001635 return ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001636}
1637
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001638static DEFINE_MUTEX(set_limit_mutex);
1639
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08001640static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001641 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001642{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001643 int retry_count;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001644 int progress;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001645 u64 memswlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001646 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001647 int children = mem_cgroup_count_children(memcg);
1648 u64 curusage, oldusage;
1649
1650 /*
1651 * For keeping hierarchical_reclaim simple, how long we should retry
1652 * is depends on callers. We set our retry-count to be function
1653 * of # of children which we should visit in this loop.
1654 */
1655 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
1656
1657 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001658
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001659 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001660 if (signal_pending(current)) {
1661 ret = -EINTR;
1662 break;
1663 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001664 /*
1665 * Rather than hide all in some function, I do this in
1666 * open coded manner. You see what this really does.
1667 * We have to guarantee mem->res.limit < mem->memsw.limit.
1668 */
1669 mutex_lock(&set_limit_mutex);
1670 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1671 if (memswlimit < val) {
1672 ret = -EINVAL;
1673 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001674 break;
1675 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001676 ret = res_counter_set_limit(&memcg->res, val);
1677 mutex_unlock(&set_limit_mutex);
1678
1679 if (!ret)
1680 break;
1681
Daisuke Nishimura42e9abb2009-01-07 18:08:30 -08001682 progress = mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL,
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001683 false, true);
1684 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
1685 /* Usage is reduced ? */
1686 if (curusage >= oldusage)
1687 retry_count--;
1688 else
1689 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001690 }
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001691
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001692 return ret;
1693}
1694
1695int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
1696 unsigned long long val)
1697{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001698 int retry_count;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001699 u64 memlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001700 int children = mem_cgroup_count_children(memcg);
1701 int ret = -EBUSY;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001702
1703 if (!do_swap_account)
1704 return -EINVAL;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001705 /* see mem_cgroup_resize_res_limit */
1706 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
1707 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001708 while (retry_count) {
1709 if (signal_pending(current)) {
1710 ret = -EINTR;
1711 break;
1712 }
1713 /*
1714 * Rather than hide all in some function, I do this in
1715 * open coded manner. You see what this really does.
1716 * We have to guarantee mem->res.limit < mem->memsw.limit.
1717 */
1718 mutex_lock(&set_limit_mutex);
1719 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
1720 if (memlimit > val) {
1721 ret = -EINVAL;
1722 mutex_unlock(&set_limit_mutex);
1723 break;
1724 }
1725 ret = res_counter_set_limit(&memcg->memsw, val);
1726 mutex_unlock(&set_limit_mutex);
1727
1728 if (!ret)
1729 break;
1730
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001731 mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL, true, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001732 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001733 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001734 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001735 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001736 else
1737 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001738 }
1739 return ret;
1740}
1741
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001742/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001743 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001744 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
1745 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001746static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001747 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001748{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001749 struct zone *zone;
1750 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001751 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001752 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001753 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001754 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001755
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001756 zone = &NODE_DATA(node)->node_zones[zid];
1757 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001758 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001759
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001760 loop = MEM_CGROUP_ZSTAT(mz, lru);
1761 /* give some margin against EBUSY etc...*/
1762 loop += 256;
1763 busy = NULL;
1764 while (loop--) {
1765 ret = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001766 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001767 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001768 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001769 break;
1770 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001771 pc = list_entry(list->prev, struct page_cgroup, lru);
1772 if (busy == pc) {
1773 list_move(&pc->lru, list);
1774 busy = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001775 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001776 continue;
1777 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001778 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001779
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -08001780 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001781 if (ret == -ENOMEM)
1782 break;
1783
1784 if (ret == -EBUSY || ret == -EINVAL) {
1785 /* found lock contention or "pc" is obsolete. */
1786 busy = pc;
1787 cond_resched();
1788 } else
1789 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001790 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001791
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001792 if (!ret && !list_empty(list))
1793 return -EBUSY;
1794 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001795}
1796
1797/*
1798 * make mem_cgroup's charge to be 0 if there is no task.
1799 * This enables deleting this mem_cgroup.
1800 */
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001801static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001802{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001803 int ret;
1804 int node, zid, shrink;
1805 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001806 struct cgroup *cgrp = mem->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001807
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001808 css_get(&mem->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001809
1810 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001811 /* should free all ? */
1812 if (free_all)
1813 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001814move_account:
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001815 while (mem->res.usage > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001816 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001817 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001818 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001819 ret = -EINTR;
1820 if (signal_pending(current))
1821 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001822 /* This is for making all *used* pages to be on LRU. */
1823 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001824 ret = 0;
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08001825 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001826 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07001827 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001828 for_each_lru(l) {
1829 ret = mem_cgroup_force_empty_list(mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001830 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001831 if (ret)
1832 break;
1833 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001834 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001835 if (ret)
1836 break;
1837 }
1838 /* it seems parent cgroup doesn't have enough mem */
1839 if (ret == -ENOMEM)
1840 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001841 cond_resched();
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001842 }
1843 ret = 0;
1844out:
1845 css_put(&mem->css);
1846 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001847
1848try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001849 /* returns EBUSY if there is a task or if we come here twice. */
1850 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001851 ret = -EBUSY;
1852 goto out;
1853 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001854 /* we call try-to-free pages for make this cgroup empty */
1855 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001856 /* try to free all pages in this cgroup */
1857 shrink = 1;
1858 while (nr_retries && mem->res.usage > 0) {
1859 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001860
1861 if (signal_pending(current)) {
1862 ret = -EINTR;
1863 goto out;
1864 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001865 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
1866 false, get_swappiness(mem));
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001867 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001868 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001869 /* maybe some writeback is necessary */
1870 congestion_wait(WRITE, HZ/10);
1871 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001872
1873 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001874 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001875 /* try move_account...there may be some *locked* pages. */
1876 if (mem->res.usage)
1877 goto move_account;
1878 ret = 0;
1879 goto out;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001880}
1881
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001882int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
1883{
1884 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
1885}
1886
1887
Balbir Singh18f59ea2009-01-07 18:08:07 -08001888static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
1889{
1890 return mem_cgroup_from_cont(cont)->use_hierarchy;
1891}
1892
1893static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
1894 u64 val)
1895{
1896 int retval = 0;
1897 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1898 struct cgroup *parent = cont->parent;
1899 struct mem_cgroup *parent_mem = NULL;
1900
1901 if (parent)
1902 parent_mem = mem_cgroup_from_cont(parent);
1903
1904 cgroup_lock();
1905 /*
1906 * If parent's use_hiearchy is set, we can't make any modifications
1907 * in the child subtrees. If it is unset, then the change can
1908 * occur, provided the current cgroup has no children.
1909 *
1910 * For the root cgroup, parent_mem is NULL, we allow value to be
1911 * set if there are no children.
1912 */
1913 if ((!parent_mem || !parent_mem->use_hierarchy) &&
1914 (val == 1 || val == 0)) {
1915 if (list_empty(&cont->children))
1916 mem->use_hierarchy = val;
1917 else
1918 retval = -EBUSY;
1919 } else
1920 retval = -EINVAL;
1921 cgroup_unlock();
1922
1923 return retval;
1924}
1925
Paul Menage2c3daa72008-04-29 00:59:58 -07001926static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001927{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001928 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1929 u64 val = 0;
1930 int type, name;
1931
1932 type = MEMFILE_TYPE(cft->private);
1933 name = MEMFILE_ATTR(cft->private);
1934 switch (type) {
1935 case _MEM:
1936 val = res_counter_read_u64(&mem->res, name);
1937 break;
1938 case _MEMSWAP:
1939 if (do_swap_account)
1940 val = res_counter_read_u64(&mem->memsw, name);
1941 break;
1942 default:
1943 BUG();
1944 break;
1945 }
1946 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001947}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001948/*
1949 * The user of this function is...
1950 * RES_LIMIT.
1951 */
Paul Menage856c13a2008-07-25 01:47:04 -07001952static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
1953 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001954{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001955 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001956 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001957 unsigned long long val;
1958 int ret;
1959
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001960 type = MEMFILE_TYPE(cft->private);
1961 name = MEMFILE_ATTR(cft->private);
1962 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001963 case RES_LIMIT:
1964 /* This function does all necessary parse...reuse it */
1965 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001966 if (ret)
1967 break;
1968 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001969 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001970 else
1971 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001972 break;
1973 default:
1974 ret = -EINVAL; /* should be BUG() ? */
1975 break;
1976 }
1977 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001978}
1979
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08001980static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
1981 unsigned long long *mem_limit, unsigned long long *memsw_limit)
1982{
1983 struct cgroup *cgroup;
1984 unsigned long long min_limit, min_memsw_limit, tmp;
1985
1986 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
1987 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1988 cgroup = memcg->css.cgroup;
1989 if (!memcg->use_hierarchy)
1990 goto out;
1991
1992 while (cgroup->parent) {
1993 cgroup = cgroup->parent;
1994 memcg = mem_cgroup_from_cont(cgroup);
1995 if (!memcg->use_hierarchy)
1996 break;
1997 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
1998 min_limit = min(min_limit, tmp);
1999 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2000 min_memsw_limit = min(min_memsw_limit, tmp);
2001 }
2002out:
2003 *mem_limit = min_limit;
2004 *memsw_limit = min_memsw_limit;
2005 return;
2006}
2007
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002008static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002009{
2010 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002011 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002012
2013 mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002014 type = MEMFILE_TYPE(event);
2015 name = MEMFILE_ATTR(event);
2016 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002017 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002018 if (type == _MEM)
2019 res_counter_reset_max(&mem->res);
2020 else
2021 res_counter_reset_max(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002022 break;
2023 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002024 if (type == _MEM)
2025 res_counter_reset_failcnt(&mem->res);
2026 else
2027 res_counter_reset_failcnt(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002028 break;
2029 }
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07002030 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002031}
2032
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002033
2034/* For read statistics */
2035enum {
2036 MCS_CACHE,
2037 MCS_RSS,
2038 MCS_PGPGIN,
2039 MCS_PGPGOUT,
2040 MCS_INACTIVE_ANON,
2041 MCS_ACTIVE_ANON,
2042 MCS_INACTIVE_FILE,
2043 MCS_ACTIVE_FILE,
2044 MCS_UNEVICTABLE,
2045 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002046};
2047
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002048struct mcs_total_stat {
2049 s64 stat[NR_MCS_STAT];
2050};
2051
2052struct {
2053 char *local_name;
2054 char *total_name;
2055} memcg_stat_strings[NR_MCS_STAT] = {
2056 {"cache", "total_cache"},
2057 {"rss", "total_rss"},
2058 {"pgpgin", "total_pgpgin"},
2059 {"pgpgout", "total_pgpgout"},
2060 {"inactive_anon", "total_inactive_anon"},
2061 {"active_anon", "total_active_anon"},
2062 {"inactive_file", "total_inactive_file"},
2063 {"active_file", "total_active_file"},
2064 {"unevictable", "total_unevictable"}
2065};
2066
2067
2068static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
2069{
2070 struct mcs_total_stat *s = data;
2071 s64 val;
2072
2073 /* per cpu stat */
2074 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE);
2075 s->stat[MCS_CACHE] += val * PAGE_SIZE;
2076 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
2077 s->stat[MCS_RSS] += val * PAGE_SIZE;
2078 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT);
2079 s->stat[MCS_PGPGIN] += val;
2080 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT);
2081 s->stat[MCS_PGPGOUT] += val;
2082
2083 /* per zone stat */
2084 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
2085 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
2086 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
2087 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
2088 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
2089 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
2090 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
2091 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
2092 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
2093 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
2094 return 0;
2095}
2096
2097static void
2098mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
2099{
2100 mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
2101}
2102
Paul Menagec64745c2008-04-29 01:00:02 -07002103static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
2104 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002105{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002106 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002107 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002108 int i;
2109
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002110 memset(&mystat, 0, sizeof(mystat));
2111 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002112
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002113 for (i = 0; i < NR_MCS_STAT; i++)
2114 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002115
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002116 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08002117 {
2118 unsigned long long limit, memsw_limit;
2119 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
2120 cb->fill(cb, "hierarchical_memory_limit", limit);
2121 if (do_swap_account)
2122 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
2123 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002124
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002125 memset(&mystat, 0, sizeof(mystat));
2126 mem_cgroup_get_total_stat(mem_cont, &mystat);
2127 for (i = 0; i < NR_MCS_STAT; i++)
2128 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
2129
2130
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002131#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08002132 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002133
2134 {
2135 int nid, zid;
2136 struct mem_cgroup_per_zone *mz;
2137 unsigned long recent_rotated[2] = {0, 0};
2138 unsigned long recent_scanned[2] = {0, 0};
2139
2140 for_each_online_node(nid)
2141 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
2142 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
2143
2144 recent_rotated[0] +=
2145 mz->reclaim_stat.recent_rotated[0];
2146 recent_rotated[1] +=
2147 mz->reclaim_stat.recent_rotated[1];
2148 recent_scanned[0] +=
2149 mz->reclaim_stat.recent_scanned[0];
2150 recent_scanned[1] +=
2151 mz->reclaim_stat.recent_scanned[1];
2152 }
2153 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
2154 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
2155 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
2156 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
2157 }
2158#endif
2159
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002160 return 0;
2161}
2162
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002163static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
2164{
2165 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2166
2167 return get_swappiness(memcg);
2168}
2169
2170static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
2171 u64 val)
2172{
2173 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2174 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08002175
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002176 if (val > 100)
2177 return -EINVAL;
2178
2179 if (cgrp->parent == NULL)
2180 return -EINVAL;
2181
2182 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08002183
2184 cgroup_lock();
2185
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002186 /* If under hierarchy, only empty-root can set this value */
2187 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08002188 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
2189 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002190 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08002191 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002192
2193 spin_lock(&memcg->reclaim_param_lock);
2194 memcg->swappiness = val;
2195 spin_unlock(&memcg->reclaim_param_lock);
2196
Li Zefan068b38c2009-01-15 13:51:26 -08002197 cgroup_unlock();
2198
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002199 return 0;
2200}
2201
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002202
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002203static struct cftype mem_cgroup_files[] = {
2204 {
Balbir Singh0eea1032008-02-07 00:13:57 -08002205 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002206 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07002207 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002208 },
2209 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002210 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002211 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002212 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002213 .read_u64 = mem_cgroup_read,
2214 },
2215 {
Balbir Singh0eea1032008-02-07 00:13:57 -08002216 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002217 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07002218 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07002219 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002220 },
2221 {
2222 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002223 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002224 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07002225 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002226 },
Balbir Singh8697d332008-02-07 00:13:59 -08002227 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002228 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07002229 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002230 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002231 {
2232 .name = "force_empty",
2233 .trigger = mem_cgroup_force_empty_write,
2234 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08002235 {
2236 .name = "use_hierarchy",
2237 .write_u64 = mem_cgroup_hierarchy_write,
2238 .read_u64 = mem_cgroup_hierarchy_read,
2239 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002240 {
2241 .name = "swappiness",
2242 .read_u64 = mem_cgroup_swappiness_read,
2243 .write_u64 = mem_cgroup_swappiness_write,
2244 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002245};
2246
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002247#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2248static struct cftype memsw_cgroup_files[] = {
2249 {
2250 .name = "memsw.usage_in_bytes",
2251 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
2252 .read_u64 = mem_cgroup_read,
2253 },
2254 {
2255 .name = "memsw.max_usage_in_bytes",
2256 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
2257 .trigger = mem_cgroup_reset,
2258 .read_u64 = mem_cgroup_read,
2259 },
2260 {
2261 .name = "memsw.limit_in_bytes",
2262 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
2263 .write_string = mem_cgroup_write,
2264 .read_u64 = mem_cgroup_read,
2265 },
2266 {
2267 .name = "memsw.failcnt",
2268 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
2269 .trigger = mem_cgroup_reset,
2270 .read_u64 = mem_cgroup_read,
2271 },
2272};
2273
2274static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2275{
2276 if (!do_swap_account)
2277 return 0;
2278 return cgroup_add_files(cont, ss, memsw_cgroup_files,
2279 ARRAY_SIZE(memsw_cgroup_files));
2280};
2281#else
2282static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2283{
2284 return 0;
2285}
2286#endif
2287
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002288static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2289{
2290 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002291 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07002292 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07002293 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002294 /*
2295 * This routine is called against possible nodes.
2296 * But it's BUG to call kmalloc() against offline node.
2297 *
2298 * TODO: this routine can waste much memory for nodes which will
2299 * never be onlined. It's better to use memory hotplug callback
2300 * function.
2301 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07002302 if (!node_state(node, N_NORMAL_MEMORY))
2303 tmp = -1;
2304 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002305 if (!pn)
2306 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002307
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002308 mem->info.nodeinfo[node] = pn;
2309 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002310
2311 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
2312 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07002313 for_each_lru(l)
2314 INIT_LIST_HEAD(&mz->lists[l]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002315 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002316 return 0;
2317}
2318
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002319static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2320{
2321 kfree(mem->info.nodeinfo[node]);
2322}
2323
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002324static int mem_cgroup_size(void)
2325{
2326 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
2327 return sizeof(struct mem_cgroup) + cpustat_size;
2328}
2329
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002330static struct mem_cgroup *mem_cgroup_alloc(void)
2331{
2332 struct mem_cgroup *mem;
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002333 int size = mem_cgroup_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002334
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002335 if (size < PAGE_SIZE)
2336 mem = kmalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002337 else
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002338 mem = vmalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002339
2340 if (mem)
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002341 memset(mem, 0, size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002342 return mem;
2343}
2344
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002345/*
2346 * At destroying mem_cgroup, references from swap_cgroup can remain.
2347 * (scanning all at force_empty is too costly...)
2348 *
2349 * Instead of clearing all references at force_empty, we remember
2350 * the number of reference from swap_cgroup and free mem_cgroup when
2351 * it goes down to 0.
2352 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002353 * Removal of cgroup itself succeeds regardless of refs from swap.
2354 */
2355
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08002356static void __mem_cgroup_free(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002357{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002358 int node;
2359
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002360 free_css_id(&mem_cgroup_subsys, &mem->css);
2361
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002362 for_each_node_state(node, N_POSSIBLE)
2363 free_mem_cgroup_per_zone_info(mem, node);
2364
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002365 if (mem_cgroup_size() < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002366 kfree(mem);
2367 else
2368 vfree(mem);
2369}
2370
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002371static void mem_cgroup_get(struct mem_cgroup *mem)
2372{
2373 atomic_inc(&mem->refcnt);
2374}
2375
2376static void mem_cgroup_put(struct mem_cgroup *mem)
2377{
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08002378 if (atomic_dec_and_test(&mem->refcnt)) {
2379 struct mem_cgroup *parent = parent_mem_cgroup(mem);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08002380 __mem_cgroup_free(mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08002381 if (parent)
2382 mem_cgroup_put(parent);
2383 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002384}
2385
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08002386/*
2387 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
2388 */
2389static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
2390{
2391 if (!mem->res.parent)
2392 return NULL;
2393 return mem_cgroup_from_res_counter(mem->res.parent, res);
2394}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002395
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08002396#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2397static void __init enable_swap_cgroup(void)
2398{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002399 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08002400 do_swap_account = 1;
2401}
2402#else
2403static void __init enable_swap_cgroup(void)
2404{
2405}
2406#endif
2407
Li Zefan0eb253e2009-01-15 13:51:25 -08002408static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002409mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
2410{
Balbir Singh28dbc4b2009-01-07 18:08:05 -08002411 struct mem_cgroup *mem, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002412 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002413 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002414
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002415 mem = mem_cgroup_alloc();
2416 if (!mem)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002417 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08002418
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002419 for_each_node_state(node, N_POSSIBLE)
2420 if (alloc_mem_cgroup_per_zone_info(mem, node))
2421 goto free_out;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08002422 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08002423 if (cont->parent == NULL) {
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08002424 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08002425 parent = NULL;
Balbir Singh18f59ea2009-01-07 18:08:07 -08002426 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08002427 parent = mem_cgroup_from_cont(cont->parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08002428 mem->use_hierarchy = parent->use_hierarchy;
2429 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08002430
Balbir Singh18f59ea2009-01-07 18:08:07 -08002431 if (parent && parent->use_hierarchy) {
2432 res_counter_init(&mem->res, &parent->res);
2433 res_counter_init(&mem->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08002434 /*
2435 * We increment refcnt of the parent to ensure that we can
2436 * safely access it on res_counter_charge/uncharge.
2437 * This refcnt will be decremented when freeing this
2438 * mem_cgroup(see mem_cgroup_put).
2439 */
2440 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08002441 } else {
2442 res_counter_init(&mem->res, NULL);
2443 res_counter_init(&mem->memsw, NULL);
2444 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002445 mem->last_scanned_child = 0;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -08002446 spin_lock_init(&mem->reclaim_param_lock);
Balbir Singh6d61ef42009-01-07 18:08:06 -08002447
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002448 if (parent)
2449 mem->swappiness = get_swappiness(parent);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08002450 atomic_set(&mem->refcnt, 1);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002451 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002452free_out:
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08002453 __mem_cgroup_free(mem);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002454 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002455}
2456
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002457static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08002458 struct cgroup *cont)
2459{
2460 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002461
2462 return mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08002463}
2464
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002465static void mem_cgroup_destroy(struct cgroup_subsys *ss,
2466 struct cgroup *cont)
2467{
Daisuke Nishimurac268e992009-01-15 13:51:13 -08002468 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08002469
Daisuke Nishimurac268e992009-01-15 13:51:13 -08002470 mem_cgroup_put(mem);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002471}
2472
2473static int mem_cgroup_populate(struct cgroup_subsys *ss,
2474 struct cgroup *cont)
2475{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002476 int ret;
2477
2478 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
2479 ARRAY_SIZE(mem_cgroup_files));
2480
2481 if (!ret)
2482 ret = register_memsw_files(cont, ss);
2483 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002484}
2485
Balbir Singh67e465a2008-02-07 00:13:54 -08002486static void mem_cgroup_move_task(struct cgroup_subsys *ss,
2487 struct cgroup *cont,
2488 struct cgroup *old_cont,
2489 struct task_struct *p)
2490{
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08002491 mutex_lock(&memcg_tasklist);
Balbir Singh67e465a2008-02-07 00:13:54 -08002492 /*
Nikanth Karthikesanf9717d22009-01-07 18:08:11 -08002493 * FIXME: It's better to move charges of this process from old
2494 * memcg to new memcg. But it's just on TODO-List now.
Balbir Singh67e465a2008-02-07 00:13:54 -08002495 */
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08002496 mutex_unlock(&memcg_tasklist);
Balbir Singh67e465a2008-02-07 00:13:54 -08002497}
2498
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002499struct cgroup_subsys mem_cgroup_subsys = {
2500 .name = "memory",
2501 .subsys_id = mem_cgroup_subsys_id,
2502 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08002503 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002504 .destroy = mem_cgroup_destroy,
2505 .populate = mem_cgroup_populate,
Balbir Singh67e465a2008-02-07 00:13:54 -08002506 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002507 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002508 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002509};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08002510
2511#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2512
2513static int __init disable_swap_account(char *s)
2514{
2515 really_do_swap_account = 0;
2516 return 1;
2517}
2518__setup("noswapaccount", disable_swap_account);
2519#endif