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