blob: 4ad3e6be045d65bf25ad4c1f0551753d48ecd2f7 [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 Singh4b3bde42009-09-23 15:56:32 -070046struct mem_cgroup *root_mem_cgroup __read_mostly;
Balbir Singh8cdea7c2008-02-07 00:13:50 -080047
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080048#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
Li Zefan338c8432009-06-17 16:27:15 -070049/* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080050int do_swap_account __read_mostly;
51static int really_do_swap_account __initdata = 1; /* for remember boot option*/
52#else
53#define do_swap_account (0)
54#endif
55
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -080056static DEFINE_MUTEX(memcg_tasklist); /* can be hold under cgroup_mutex */
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080057
Balbir Singh8cdea7c2008-02-07 00:13:50 -080058/*
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080059 * Statistics for memory cgroup.
60 */
61enum mem_cgroup_stat_index {
62 /*
63 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
64 */
65 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
Balbir Singhd69b0422009-06-17 16:26:34 -070066 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
67 MEM_CGROUP_STAT_MAPPED_FILE, /* # of pages charged as file rss */
Balaji Rao55e462b2008-05-01 04:35:12 -070068 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
69 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080070
71 MEM_CGROUP_STAT_NSTATS,
72};
73
74struct mem_cgroup_stat_cpu {
75 s64 count[MEM_CGROUP_STAT_NSTATS];
76} ____cacheline_aligned_in_smp;
77
78struct mem_cgroup_stat {
Jan Blunckc8dad2b2009-01-07 18:07:53 -080079 struct mem_cgroup_stat_cpu cpustat[0];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080080};
81
82/*
83 * For accounting under irq disable, no need for increment preempt count.
84 */
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -070085static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080086 enum mem_cgroup_stat_index idx, int val)
87{
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -070088 stat->count[idx] += val;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080089}
90
91static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
92 enum mem_cgroup_stat_index idx)
93{
94 int cpu;
95 s64 ret = 0;
96 for_each_possible_cpu(cpu)
97 ret += stat->cpustat[cpu].count[idx];
98 return ret;
99}
100
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700101static s64 mem_cgroup_local_usage(struct mem_cgroup_stat *stat)
102{
103 s64 ret;
104
105 ret = mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_CACHE);
106 ret += mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_RSS);
107 return ret;
108}
109
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800110/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800111 * per-zone information in memory controller.
112 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800113struct mem_cgroup_per_zone {
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800114 /*
115 * spin_lock to protect the per cgroup LRU
116 */
Christoph Lameterb69408e2008-10-18 20:26:14 -0700117 struct list_head lists[NR_LRU_LISTS];
118 unsigned long count[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800119
120 struct zone_reclaim_stat reclaim_stat;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800121};
122/* Macro for accessing counter */
123#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
124
125struct mem_cgroup_per_node {
126 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
127};
128
129struct mem_cgroup_lru_info {
130 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
131};
132
133/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800134 * The memory controller data structure. The memory controller controls both
135 * page cache and RSS per cgroup. We would eventually like to provide
136 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
137 * to help the administrator determine what knobs to tune.
138 *
139 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800140 * we hit the water mark. May be even add a low water mark, such that
141 * no reclaim occurs from a cgroup at it's low water mark, this is
142 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800143 */
144struct mem_cgroup {
145 struct cgroup_subsys_state css;
146 /*
147 * the counter to account for memory usage
148 */
149 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800150 /*
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800151 * the counter to account for mem+swap usage.
152 */
153 struct res_counter memsw;
154 /*
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800155 * Per cgroup active and inactive list, similar to the
156 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800157 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800158 struct mem_cgroup_lru_info info;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800159
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800160 /*
161 protect against reclaim related member.
162 */
163 spinlock_t reclaim_param_lock;
164
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800165 int prev_priority; /* for recording reclaim priority */
Balbir Singh6d61ef42009-01-07 18:08:06 -0800166
167 /*
168 * While reclaiming in a hiearchy, we cache the last child we
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700169 * reclaimed from.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800170 */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700171 int last_scanned_child;
Balbir Singh18f59ea2009-01-07 18:08:07 -0800172 /*
173 * Should the accounting and control be hierarchical, per subtree?
174 */
175 bool use_hierarchy;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800176 unsigned long last_oom_jiffies;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800177 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800178
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800179 unsigned int swappiness;
180
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700181 /* set when res.limit == memsw.limit */
182 bool memsw_is_minimum;
183
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800184 /*
Jan Blunckc8dad2b2009-01-07 18:07:53 -0800185 * statistics. This must be placed at the end of memcg.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800186 */
187 struct mem_cgroup_stat stat;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800188};
189
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800190enum charge_type {
191 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
192 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700193 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700194 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800195 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700196 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700197 NR_CHARGE_TYPE,
198};
199
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700200/* only for here (for easy reading.) */
201#define PCGF_CACHE (1UL << PCG_CACHE)
202#define PCGF_USED (1UL << PCG_USED)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700203#define PCGF_LOCK (1UL << PCG_LOCK)
Balbir Singh4b3bde42009-09-23 15:56:32 -0700204/* Not used, but added here for completeness */
205#define PCGF_ACCT (1UL << PCG_ACCT)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800206
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
Balbir Singh4b3bde42009-09-23 15:56:32 -0700353static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
354{
355 return (mem == root_mem_cgroup);
356}
357
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800358/*
359 * Following LRU functions are allowed to be used without PCG_LOCK.
360 * Operations are called by routine of global LRU independently from memcg.
361 * What we have to take care of here is validness of pc->mem_cgroup.
362 *
363 * Changes to pc->mem_cgroup happens when
364 * 1. charge
365 * 2. moving account
366 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
367 * It is added to LRU before charge.
368 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
369 * When moving account, the page is not on LRU. It's isolated.
370 */
371
372void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800373{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800374 struct page_cgroup *pc;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800375 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700376
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800377 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800378 return;
379 pc = lookup_page_cgroup(page);
380 /* can happen while we handle swapcache. */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700381 if (!TestClearPageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800382 return;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700383 VM_BUG_ON(!pc->mem_cgroup);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800384 /*
385 * We don't check PCG_USED bit. It's cleared when the "page" is finally
386 * removed from global LRU.
387 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800388 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700389 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700390 if (mem_cgroup_is_root(pc->mem_cgroup))
391 return;
392 VM_BUG_ON(list_empty(&pc->lru));
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800393 list_del_init(&pc->lru);
394 return;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800395}
396
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800397void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800398{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800399 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800400}
401
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800402void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800403{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800404 struct mem_cgroup_per_zone *mz;
405 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800406
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800407 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700408 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700409
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800410 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800411 /*
412 * Used bit is set without atomic ops but after smp_wmb().
413 * For making pc->mem_cgroup visible, insert smp_rmb() here.
414 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800415 smp_rmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -0700416 /* unused or root page is not rotated. */
417 if (!PageCgroupUsed(pc) || mem_cgroup_is_root(pc->mem_cgroup))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800418 return;
419 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700420 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800421}
422
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800423void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
424{
425 struct page_cgroup *pc;
426 struct mem_cgroup_per_zone *mz;
427
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800428 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800429 return;
430 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700431 VM_BUG_ON(PageCgroupAcctLRU(pc));
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800432 /*
433 * Used bit is set without atomic ops but after smp_wmb().
434 * For making pc->mem_cgroup visible, insert smp_rmb() here.
435 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800436 smp_rmb();
437 if (!PageCgroupUsed(pc))
438 return;
439
440 mz = page_cgroup_zoneinfo(pc);
441 MEM_CGROUP_ZSTAT(mz, lru) += 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700442 SetPageCgroupAcctLRU(pc);
443 if (mem_cgroup_is_root(pc->mem_cgroup))
444 return;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800445 list_add(&pc->lru, &mz->lists[lru]);
446}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800447
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800448/*
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800449 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
450 * lru because the page may.be reused after it's fully uncharged (because of
451 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
452 * it again. This function is only used to charge SwapCache. It's done under
453 * lock_page and expected that zone->lru_lock is never held.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800454 */
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800455static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800456{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800457 unsigned long flags;
458 struct zone *zone = page_zone(page);
459 struct page_cgroup *pc = lookup_page_cgroup(page);
460
461 spin_lock_irqsave(&zone->lru_lock, flags);
462 /*
463 * Forget old LRU when this page_cgroup is *not* used. This Used bit
464 * is guarded by lock_page() because the page is SwapCache.
465 */
466 if (!PageCgroupUsed(pc))
467 mem_cgroup_del_lru_list(page, page_lru(page));
468 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800469}
470
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800471static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
472{
473 unsigned long flags;
474 struct zone *zone = page_zone(page);
475 struct page_cgroup *pc = lookup_page_cgroup(page);
476
477 spin_lock_irqsave(&zone->lru_lock, flags);
478 /* link when the page is linked to LRU but page_cgroup isn't */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700479 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800480 mem_cgroup_add_lru_list(page, page_lru(page));
481 spin_unlock_irqrestore(&zone->lru_lock, flags);
482}
483
484
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800485void mem_cgroup_move_lists(struct page *page,
486 enum lru_list from, enum lru_list to)
487{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800488 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800489 return;
490 mem_cgroup_del_lru_list(page, from);
491 mem_cgroup_add_lru_list(page, to);
492}
493
David Rientjes4c4a2212008-02-07 00:14:06 -0800494int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
495{
496 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700497 struct mem_cgroup *curr = NULL;
David Rientjes4c4a2212008-02-07 00:14:06 -0800498
499 task_lock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700500 rcu_read_lock();
501 curr = try_get_mem_cgroup_from_mm(task->mm);
502 rcu_read_unlock();
David Rientjes4c4a2212008-02-07 00:14:06 -0800503 task_unlock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700504 if (!curr)
505 return 0;
506 if (curr->use_hierarchy)
507 ret = css_is_ancestor(&curr->css, &mem->css);
508 else
509 ret = (curr == mem);
510 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -0800511 return ret;
512}
513
Balbir Singh66e17072008-02-07 00:13:56 -0800514/*
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800515 * prev_priority control...this will be used in memory reclaim path.
516 */
517int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
518{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800519 int prev_priority;
520
521 spin_lock(&mem->reclaim_param_lock);
522 prev_priority = mem->prev_priority;
523 spin_unlock(&mem->reclaim_param_lock);
524
525 return prev_priority;
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800526}
527
528void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
529{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800530 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800531 if (priority < mem->prev_priority)
532 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800533 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800534}
535
536void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
537{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800538 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800539 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800540 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800541}
542
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800543static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800544{
545 unsigned long active;
546 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800547 unsigned long gb;
548 unsigned long inactive_ratio;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800549
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700550 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
551 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800552
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800553 gb = (inactive + active) >> (30 - PAGE_SHIFT);
554 if (gb)
555 inactive_ratio = int_sqrt(10 * gb);
556 else
557 inactive_ratio = 1;
558
559 if (present_pages) {
560 present_pages[0] = inactive;
561 present_pages[1] = active;
562 }
563
564 return inactive_ratio;
565}
566
567int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
568{
569 unsigned long active;
570 unsigned long inactive;
571 unsigned long present_pages[2];
572 unsigned long inactive_ratio;
573
574 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
575
576 inactive = present_pages[0];
577 active = present_pages[1];
578
579 if (inactive * inactive_ratio < active)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800580 return 1;
581
582 return 0;
583}
584
Rik van Riel56e49d22009-06-16 15:32:28 -0700585int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
586{
587 unsigned long active;
588 unsigned long inactive;
589
590 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
591 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
592
593 return (active > inactive);
594}
595
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800596unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
597 struct zone *zone,
598 enum lru_list lru)
599{
600 int nid = zone->zone_pgdat->node_id;
601 int zid = zone_idx(zone);
602 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
603
604 return MEM_CGROUP_ZSTAT(mz, lru);
605}
606
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800607struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
608 struct zone *zone)
609{
610 int nid = zone->zone_pgdat->node_id;
611 int zid = zone_idx(zone);
612 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
613
614 return &mz->reclaim_stat;
615}
616
617struct zone_reclaim_stat *
618mem_cgroup_get_reclaim_stat_from_page(struct page *page)
619{
620 struct page_cgroup *pc;
621 struct mem_cgroup_per_zone *mz;
622
623 if (mem_cgroup_disabled())
624 return NULL;
625
626 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800627 /*
628 * Used bit is set without atomic ops but after smp_wmb().
629 * For making pc->mem_cgroup visible, insert smp_rmb() here.
630 */
631 smp_rmb();
632 if (!PageCgroupUsed(pc))
633 return NULL;
634
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800635 mz = page_cgroup_zoneinfo(pc);
636 if (!mz)
637 return NULL;
638
639 return &mz->reclaim_stat;
640}
641
Balbir Singh66e17072008-02-07 00:13:56 -0800642unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
643 struct list_head *dst,
644 unsigned long *scanned, int order,
645 int mode, struct zone *z,
646 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700647 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -0800648{
649 unsigned long nr_taken = 0;
650 struct page *page;
651 unsigned long scan;
652 LIST_HEAD(pc_list);
653 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800654 struct page_cgroup *pc, *tmp;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800655 int nid = z->zone_pgdat->node_id;
656 int zid = zone_idx(z);
657 struct mem_cgroup_per_zone *mz;
Johannes Weinerb7c46d12009-09-21 17:02:56 -0700658 int lru = LRU_FILE * file + active;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700659 int ret;
Balbir Singh66e17072008-02-07 00:13:56 -0800660
Balbir Singhcf475ad2008-04-29 01:00:16 -0700661 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800662 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700663 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -0800664
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800665 scan = 0;
666 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -0800667 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800668 break;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800669
670 page = pc->page;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700671 if (unlikely(!PageCgroupUsed(pc)))
672 continue;
Hugh Dickins436c65412008-02-07 00:14:12 -0800673 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800674 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800675
Hugh Dickins436c65412008-02-07 00:14:12 -0800676 scan++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700677 ret = __isolate_lru_page(page, mode, file);
678 switch (ret) {
679 case 0:
Balbir Singh66e17072008-02-07 00:13:56 -0800680 list_move(&page->lru, dst);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700681 mem_cgroup_del_lru(page);
Balbir Singh66e17072008-02-07 00:13:56 -0800682 nr_taken++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700683 break;
684 case -EBUSY:
685 /* we don't affect global LRU but rotate in our LRU */
686 mem_cgroup_rotate_lru_list(page, page_lru(page));
687 break;
688 default:
689 break;
Balbir Singh66e17072008-02-07 00:13:56 -0800690 }
691 }
692
Balbir Singh66e17072008-02-07 00:13:56 -0800693 *scanned = scan;
694 return nr_taken;
695}
696
Balbir Singh6d61ef42009-01-07 18:08:06 -0800697#define mem_cgroup_from_res_counter(counter, member) \
698 container_of(counter, struct mem_cgroup, member)
699
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -0800700static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
701{
702 if (do_swap_account) {
703 if (res_counter_check_under_limit(&mem->res) &&
704 res_counter_check_under_limit(&mem->memsw))
705 return true;
706 } else
707 if (res_counter_check_under_limit(&mem->res))
708 return true;
709 return false;
710}
711
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800712static unsigned int get_swappiness(struct mem_cgroup *memcg)
713{
714 struct cgroup *cgrp = memcg->css.cgroup;
715 unsigned int swappiness;
716
717 /* root ? */
718 if (cgrp->parent == NULL)
719 return vm_swappiness;
720
721 spin_lock(&memcg->reclaim_param_lock);
722 swappiness = memcg->swappiness;
723 spin_unlock(&memcg->reclaim_param_lock);
724
725 return swappiness;
726}
727
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700728static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
729{
730 int *val = data;
731 (*val)++;
732 return 0;
733}
Balbir Singhe2224322009-04-02 16:57:39 -0700734
735/**
736 * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode.
737 * @memcg: The memory cgroup that went over limit
738 * @p: Task that is going to be killed
739 *
740 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
741 * enabled
742 */
743void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
744{
745 struct cgroup *task_cgrp;
746 struct cgroup *mem_cgrp;
747 /*
748 * Need a buffer in BSS, can't rely on allocations. The code relies
749 * on the assumption that OOM is serialized for memory controller.
750 * If this assumption is broken, revisit this code.
751 */
752 static char memcg_name[PATH_MAX];
753 int ret;
754
755 if (!memcg)
756 return;
757
758
759 rcu_read_lock();
760
761 mem_cgrp = memcg->css.cgroup;
762 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
763
764 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
765 if (ret < 0) {
766 /*
767 * Unfortunately, we are unable to convert to a useful name
768 * But we'll still print out the usage information
769 */
770 rcu_read_unlock();
771 goto done;
772 }
773 rcu_read_unlock();
774
775 printk(KERN_INFO "Task in %s killed", memcg_name);
776
777 rcu_read_lock();
778 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
779 if (ret < 0) {
780 rcu_read_unlock();
781 goto done;
782 }
783 rcu_read_unlock();
784
785 /*
786 * Continues from above, so we don't need an KERN_ level
787 */
788 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
789done:
790
791 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
792 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
793 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
794 res_counter_read_u64(&memcg->res, RES_FAILCNT));
795 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
796 "failcnt %llu\n",
797 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
798 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
799 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
800}
801
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700802/*
803 * This function returns the number of memcg under hierarchy tree. Returns
804 * 1(self count) if no children.
805 */
806static int mem_cgroup_count_children(struct mem_cgroup *mem)
807{
808 int num = 0;
809 mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
810 return num;
811}
812
Balbir Singh6d61ef42009-01-07 18:08:06 -0800813/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700814 * Visit the first child (need not be the first child as per the ordering
815 * of the cgroup list, since we track last_scanned_child) of @mem and use
816 * that to reclaim free pages from.
817 */
818static struct mem_cgroup *
819mem_cgroup_select_victim(struct mem_cgroup *root_mem)
820{
821 struct mem_cgroup *ret = NULL;
822 struct cgroup_subsys_state *css;
823 int nextid, found;
824
825 if (!root_mem->use_hierarchy) {
826 css_get(&root_mem->css);
827 ret = root_mem;
828 }
829
830 while (!ret) {
831 rcu_read_lock();
832 nextid = root_mem->last_scanned_child + 1;
833 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
834 &found);
835 if (css && css_tryget(css))
836 ret = container_of(css, struct mem_cgroup, css);
837
838 rcu_read_unlock();
839 /* Updates scanning parameter */
840 spin_lock(&root_mem->reclaim_param_lock);
841 if (!css) {
842 /* this means start scan from ID:1 */
843 root_mem->last_scanned_child = 0;
844 } else
845 root_mem->last_scanned_child = found;
846 spin_unlock(&root_mem->reclaim_param_lock);
847 }
848
849 return ret;
850}
851
852/*
853 * Scan the hierarchy if needed to reclaim memory. We remember the last child
854 * we reclaimed from, so that we don't end up penalizing one child extensively
855 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800856 *
857 * root_mem is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700858 *
859 * We give up and return to the caller when we visit root_mem twice.
860 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700861 *
862 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800863 */
864static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700865 gfp_t gfp_mask, bool noswap, bool shrink)
Balbir Singh6d61ef42009-01-07 18:08:06 -0800866{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700867 struct mem_cgroup *victim;
868 int ret, total = 0;
869 int loop = 0;
Balbir Singh6d61ef42009-01-07 18:08:06 -0800870
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700871 /* If memsw_is_minimum==1, swap-out is of-no-use. */
872 if (root_mem->memsw_is_minimum)
873 noswap = true;
874
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700875 while (loop < 2) {
876 victim = mem_cgroup_select_victim(root_mem);
877 if (victim == root_mem)
878 loop++;
879 if (!mem_cgroup_local_usage(&victim->stat)) {
880 /* this cgroup's local usage == 0 */
881 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -0800882 continue;
883 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700884 /* we use swappiness of local cgroup */
885 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask, noswap,
886 get_swappiness(victim));
887 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700888 /*
889 * At shrinking usage, we can't check we should stop here or
890 * reclaim more. It's depends on callers. last_scanned_child
891 * will work enough for keeping fairness under tree.
892 */
893 if (shrink)
894 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700895 total += ret;
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -0800896 if (mem_cgroup_check_under_limit(root_mem))
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700897 return 1 + total;
Balbir Singh6d61ef42009-01-07 18:08:06 -0800898 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700899 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -0800900}
901
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800902bool mem_cgroup_oom_called(struct task_struct *task)
903{
904 bool ret = false;
905 struct mem_cgroup *mem;
906 struct mm_struct *mm;
907
908 rcu_read_lock();
909 mm = task->mm;
910 if (!mm)
911 mm = &init_mm;
912 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
913 if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
914 ret = true;
915 rcu_read_unlock();
916 return ret;
917}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700918
919static int record_last_oom_cb(struct mem_cgroup *mem, void *data)
920{
921 mem->last_oom_jiffies = jiffies;
922 return 0;
923}
924
925static void record_last_oom(struct mem_cgroup *mem)
926{
927 mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb);
928}
929
Balbir Singhd69b0422009-06-17 16:26:34 -0700930/*
931 * Currently used to update mapped file statistics, but the routine can be
932 * generalized to update other statistics as well.
933 */
934void mem_cgroup_update_mapped_file_stat(struct page *page, int val)
935{
936 struct mem_cgroup *mem;
937 struct mem_cgroup_stat *stat;
938 struct mem_cgroup_stat_cpu *cpustat;
939 int cpu;
940 struct page_cgroup *pc;
941
942 if (!page_is_file_cache(page))
943 return;
944
945 pc = lookup_page_cgroup(page);
946 if (unlikely(!pc))
947 return;
948
949 lock_page_cgroup(pc);
950 mem = pc->mem_cgroup;
951 if (!mem)
952 goto done;
953
954 if (!PageCgroupUsed(pc))
955 goto done;
956
957 /*
958 * Preemption is already disabled, we don't need get_cpu()
959 */
960 cpu = smp_processor_id();
961 stat = &mem->stat;
962 cpustat = &stat->cpustat[cpu];
963
964 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE, val);
965done:
966 unlock_page_cgroup(pc);
967}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700968
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800969/*
970 * Unlike exported interface, "oom" parameter is added. if oom==true,
971 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800972 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800973static int __mem_cgroup_try_charge(struct mm_struct *mm,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800974 gfp_t gfp_mask, struct mem_cgroup **memcg,
975 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800976{
Balbir Singh6d61ef42009-01-07 18:08:06 -0800977 struct mem_cgroup *mem, *mem_over_limit;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800978 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Balbir Singh28dbc4b2009-01-07 18:08:05 -0800979 struct res_counter *fail_res;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800980
981 if (unlikely(test_thread_flag(TIF_MEMDIE))) {
982 /* Don't account this! */
983 *memcg = NULL;
984 return 0;
985 }
986
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800987 /*
Hugh Dickins3be91272008-02-07 00:14:19 -0800988 * We always charge the cgroup the mm_struct belongs to.
989 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800990 * thread group leader migrates. It's possible that mm is not
991 * set, if so charge the init_mm (happens for pagecache usage).
992 */
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800993 mem = *memcg;
994 if (likely(!mem)) {
995 mem = try_get_mem_cgroup_from_mm(mm);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800996 *memcg = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700997 } else {
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800998 css_get(&mem->css);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700999 }
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001000 if (unlikely(!mem))
1001 return 0;
1002
Nikanth Karthikesan46f7e602009-05-28 14:34:41 -07001003 VM_BUG_ON(css_is_removed(&mem->css));
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001004
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001005 while (1) {
1006 int ret;
1007 bool noswap = false;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001008
Balbir Singh28dbc4b2009-01-07 18:08:05 -08001009 ret = res_counter_charge(&mem->res, PAGE_SIZE, &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001010 if (likely(!ret)) {
1011 if (!do_swap_account)
1012 break;
Balbir Singh28dbc4b2009-01-07 18:08:05 -08001013 ret = res_counter_charge(&mem->memsw, PAGE_SIZE,
1014 &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001015 if (likely(!ret))
1016 break;
1017 /* mem+swap counter fails */
1018 res_counter_uncharge(&mem->res, PAGE_SIZE);
1019 noswap = true;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001020 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1021 memsw);
1022 } else
1023 /* mem counter fails */
1024 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1025 res);
1026
Hugh Dickins3be91272008-02-07 00:14:19 -08001027 if (!(gfp_mask & __GFP_WAIT))
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001028 goto nomem;
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001029
Balbir Singh6d61ef42009-01-07 18:08:06 -08001030 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, gfp_mask,
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001031 noswap, false);
Daisuke Nishimura4d1c6272009-01-15 13:51:14 -08001032 if (ret)
1033 continue;
Balbir Singh66e17072008-02-07 00:13:56 -08001034
1035 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001036 * try_to_free_mem_cgroup_pages() might not give us a full
1037 * picture of reclaim. Some pages are reclaimed and might be
1038 * moved to swap cache or just unmapped from the cgroup.
1039 * Check the limit again to see if the reclaim reduced the
1040 * current usage of the cgroup before giving up
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001041 *
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001042 */
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -08001043 if (mem_cgroup_check_under_limit(mem_over_limit))
1044 continue;
Hugh Dickins3be91272008-02-07 00:14:19 -08001045
1046 if (!nr_retries--) {
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001047 if (oom) {
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08001048 mutex_lock(&memcg_tasklist);
KAMEZAWA Hiroyuki88700752009-01-07 18:08:09 -08001049 mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08001050 mutex_unlock(&memcg_tasklist);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001051 record_last_oom(mem_over_limit);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001052 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001053 goto nomem;
Balbir Singh66e17072008-02-07 00:13:56 -08001054 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001055 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001056 return 0;
1057nomem:
1058 css_put(&mem->css);
1059 return -ENOMEM;
1060}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001061
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001062
1063/*
1064 * A helper function to get mem_cgroup from ID. must be called under
1065 * rcu_read_lock(). The caller must check css_is_removed() or some if
1066 * it's concern. (dropping refcnt from swap can be called against removed
1067 * memcg.)
1068 */
1069static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
1070{
1071 struct cgroup_subsys_state *css;
1072
1073 /* ID 0 is unused ID */
1074 if (!id)
1075 return NULL;
1076 css = css_lookup(&mem_cgroup_subsys, id);
1077 if (!css)
1078 return NULL;
1079 return container_of(css, struct mem_cgroup, css);
1080}
1081
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001082static struct mem_cgroup *try_get_mem_cgroup_from_swapcache(struct page *page)
1083{
1084 struct mem_cgroup *mem;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001085 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001086 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001087 swp_entry_t ent;
1088
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001089 VM_BUG_ON(!PageLocked(page));
1090
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001091 if (!PageSwapCache(page))
1092 return NULL;
1093
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001094 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001095 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001096 if (PageCgroupUsed(pc)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001097 mem = pc->mem_cgroup;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001098 if (mem && !css_tryget(&mem->css))
1099 mem = NULL;
1100 } else {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001101 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001102 id = lookup_swap_cgroup(ent);
1103 rcu_read_lock();
1104 mem = mem_cgroup_lookup(id);
1105 if (mem && !css_tryget(&mem->css))
1106 mem = NULL;
1107 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001108 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001109 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001110 return mem;
1111}
1112
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001113/*
Daisuke Nishimuraa5e924f2009-01-07 18:08:28 -08001114 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001115 * USED state. If already USED, uncharge and return.
1116 */
1117
1118static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1119 struct page_cgroup *pc,
1120 enum charge_type ctype)
1121{
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001122 /* try_charge() can return NULL to *memcg, taking care of it. */
1123 if (!mem)
1124 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001125
1126 lock_page_cgroup(pc);
1127 if (unlikely(PageCgroupUsed(pc))) {
1128 unlock_page_cgroup(pc);
1129 res_counter_uncharge(&mem->res, PAGE_SIZE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001130 if (do_swap_account)
1131 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001132 css_put(&mem->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001133 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001134 }
Balbir Singh4b3bde42009-09-23 15:56:32 -07001135
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001136 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07001137 /*
1138 * We access a page_cgroup asynchronously without lock_page_cgroup().
1139 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
1140 * is accessed after testing USED bit. To make pc->mem_cgroup visible
1141 * before USED bit, we need memory barrier here.
1142 * See mem_cgroup_add_lru_list(), etc.
1143 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001144 smp_wmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -07001145 switch (ctype) {
1146 case MEM_CGROUP_CHARGE_TYPE_CACHE:
1147 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
1148 SetPageCgroupCache(pc);
1149 SetPageCgroupUsed(pc);
1150 break;
1151 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1152 ClearPageCgroupCache(pc);
1153 SetPageCgroupUsed(pc);
1154 break;
1155 default:
1156 break;
1157 }
Hugh Dickins3be91272008-02-07 00:14:19 -08001158
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001159 mem_cgroup_charge_statistics(mem, pc, true);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001160
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001161 unlock_page_cgroup(pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001162}
1163
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001164/**
1165 * mem_cgroup_move_account - move account of the page
1166 * @pc: page_cgroup of the page.
1167 * @from: mem_cgroup which the page is moved from.
1168 * @to: mem_cgroup which the page is moved to. @from != @to.
1169 *
1170 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001171 * - page is not on LRU (isolate_page() is useful.)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001172 *
1173 * returns 0 at success,
1174 * returns -EBUSY when lock is busy or "pc" is unstable.
1175 *
1176 * This function does "uncharge" from old cgroup but doesn't do "charge" to
1177 * new cgroup. It should be done by a caller.
1178 */
1179
1180static int mem_cgroup_move_account(struct page_cgroup *pc,
1181 struct mem_cgroup *from, struct mem_cgroup *to)
1182{
1183 struct mem_cgroup_per_zone *from_mz, *to_mz;
1184 int nid, zid;
1185 int ret = -EBUSY;
Balbir Singhd69b0422009-06-17 16:26:34 -07001186 struct page *page;
1187 int cpu;
1188 struct mem_cgroup_stat *stat;
1189 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001190
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001191 VM_BUG_ON(from == to);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001192 VM_BUG_ON(PageLRU(pc->page));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001193
1194 nid = page_cgroup_nid(pc);
1195 zid = page_cgroup_zid(pc);
1196 from_mz = mem_cgroup_zoneinfo(from, nid, zid);
1197 to_mz = mem_cgroup_zoneinfo(to, nid, zid);
1198
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001199 if (!trylock_page_cgroup(pc))
1200 return ret;
1201
1202 if (!PageCgroupUsed(pc))
1203 goto out;
1204
1205 if (pc->mem_cgroup != from)
1206 goto out;
1207
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001208 res_counter_uncharge(&from->res, PAGE_SIZE);
1209 mem_cgroup_charge_statistics(from, pc, false);
Balbir Singhd69b0422009-06-17 16:26:34 -07001210
1211 page = pc->page;
1212 if (page_is_file_cache(page) && page_mapped(page)) {
1213 cpu = smp_processor_id();
1214 /* Update mapped_file data for mem_cgroup "from" */
1215 stat = &from->stat;
1216 cpustat = &stat->cpustat[cpu];
1217 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1218 -1);
1219
1220 /* Update mapped_file data for mem_cgroup "to" */
1221 stat = &to->stat;
1222 cpustat = &stat->cpustat[cpu];
1223 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1224 1);
1225 }
1226
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001227 if (do_swap_account)
1228 res_counter_uncharge(&from->memsw, PAGE_SIZE);
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001229 css_put(&from->css);
1230
1231 css_get(&to->css);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001232 pc->mem_cgroup = to;
1233 mem_cgroup_charge_statistics(to, pc, true);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001234 ret = 0;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001235out:
1236 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001237 /*
1238 * We charges against "to" which may not have any tasks. Then, "to"
1239 * can be under rmdir(). But in current implementation, caller of
1240 * this function is just force_empty() and it's garanteed that
1241 * "to" is never removed. So, we don't check rmdir status here.
1242 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001243 return ret;
1244}
1245
1246/*
1247 * move charges to its parent.
1248 */
1249
1250static int mem_cgroup_move_parent(struct page_cgroup *pc,
1251 struct mem_cgroup *child,
1252 gfp_t gfp_mask)
1253{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001254 struct page *page = pc->page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001255 struct cgroup *cg = child->css.cgroup;
1256 struct cgroup *pcg = cg->parent;
1257 struct mem_cgroup *parent;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001258 int ret;
1259
1260 /* Is ROOT ? */
1261 if (!pcg)
1262 return -EINVAL;
1263
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001264
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001265 parent = mem_cgroup_from_cont(pcg);
1266
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001267
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001268 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001269 if (ret || !parent)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001270 return ret;
1271
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001272 if (!get_page_unless_zero(page)) {
1273 ret = -EBUSY;
1274 goto uncharge;
1275 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001276
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001277 ret = isolate_lru_page(page);
1278
1279 if (ret)
1280 goto cancel;
1281
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001282 ret = mem_cgroup_move_account(pc, child, parent);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001283
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001284 putback_lru_page(page);
1285 if (!ret) {
1286 put_page(page);
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001287 /* drop extra refcnt by try_charge() */
1288 css_put(&parent->css);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001289 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001290 }
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001291
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001292cancel:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001293 put_page(page);
1294uncharge:
1295 /* drop extra refcnt by try_charge() */
1296 css_put(&parent->css);
1297 /* uncharge if move fails */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001298 res_counter_uncharge(&parent->res, PAGE_SIZE);
1299 if (do_swap_account)
1300 res_counter_uncharge(&parent->memsw, PAGE_SIZE);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001301 return ret;
1302}
1303
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001304/*
1305 * Charge the memory controller for page usage.
1306 * Return
1307 * 0 if the charge was successful
1308 * < 0 if the cgroup is over its limit
1309 */
1310static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
1311 gfp_t gfp_mask, enum charge_type ctype,
1312 struct mem_cgroup *memcg)
1313{
1314 struct mem_cgroup *mem;
1315 struct page_cgroup *pc;
1316 int ret;
1317
1318 pc = lookup_page_cgroup(page);
1319 /* can happen at boot */
1320 if (unlikely(!pc))
1321 return 0;
1322 prefetchw(pc);
1323
1324 mem = memcg;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001325 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001326 if (ret || !mem)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001327 return ret;
1328
1329 __mem_cgroup_commit_charge(mem, pc, ctype);
1330 return 0;
1331}
1332
1333int mem_cgroup_newpage_charge(struct page *page,
1334 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001335{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001336 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001337 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001338 if (PageCompound(page))
1339 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001340 /*
1341 * If already mapped, we don't have to account.
1342 * If page cache, page->mapping has address_space.
1343 * But page->mapping may have out-of-use anon_vma pointer,
1344 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
1345 * is NULL.
1346 */
1347 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
1348 return 0;
1349 if (unlikely(!mm))
1350 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001351 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001352 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001353}
1354
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001355static void
1356__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1357 enum charge_type ctype);
1358
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001359int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
1360 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08001361{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001362 struct mem_cgroup *mem = NULL;
1363 int ret;
1364
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001365 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001366 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001367 if (PageCompound(page))
1368 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001369 /*
1370 * Corner case handling. This is called from add_to_page_cache()
1371 * in usual. But some FS (shmem) precharges this page before calling it
1372 * and call add_to_page_cache() with GFP_NOWAIT.
1373 *
1374 * For GFP_NOWAIT case, the page may be pre-charged before calling
1375 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
1376 * charge twice. (It works but has to pay a bit larger cost.)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001377 * And when the page is SwapCache, it should take swap information
1378 * into account. This is under lock_page() now.
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001379 */
1380 if (!(gfp_mask & __GFP_WAIT)) {
1381 struct page_cgroup *pc;
1382
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001383
1384 pc = lookup_page_cgroup(page);
1385 if (!pc)
1386 return 0;
1387 lock_page_cgroup(pc);
1388 if (PageCgroupUsed(pc)) {
1389 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001390 return 0;
1391 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001392 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001393 }
1394
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001395 if (unlikely(!mm && !mem))
Balbir Singh8697d332008-02-07 00:13:59 -08001396 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001397
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -07001398 if (page_is_file_cache(page))
1399 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001400 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001401
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001402 /* shmem */
1403 if (PageSwapCache(page)) {
1404 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1405 if (!ret)
1406 __mem_cgroup_commit_charge_swapin(page, mem,
1407 MEM_CGROUP_CHARGE_TYPE_SHMEM);
1408 } else
1409 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
1410 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001411
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001412 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001413}
1414
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001415/*
1416 * While swap-in, try_charge -> commit or cancel, the page is locked.
1417 * And when try_charge() successfully returns, one refcnt to memcg without
1418 * struct page_cgroup is aquired. This refcnt will be cumsumed by
1419 * "commit()" or removed by "cancel()"
1420 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001421int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
1422 struct page *page,
1423 gfp_t mask, struct mem_cgroup **ptr)
1424{
1425 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001426 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001427
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001428 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001429 return 0;
1430
1431 if (!do_swap_account)
1432 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001433 /*
1434 * A racing thread's fault, or swapoff, may have already updated
1435 * the pte, and even removed page from swap cache: return success
1436 * to go on to do_swap_page()'s pte_same() test, which should fail.
1437 */
1438 if (!PageSwapCache(page))
1439 return 0;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001440 mem = try_get_mem_cgroup_from_swapcache(page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001441 if (!mem)
1442 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001443 *ptr = mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001444 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true);
1445 /* drop extra refcnt from tryget */
1446 css_put(&mem->css);
1447 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001448charge_cur_mm:
1449 if (unlikely(!mm))
1450 mm = &init_mm;
1451 return __mem_cgroup_try_charge(mm, mask, ptr, true);
1452}
1453
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001454static void
1455__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1456 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001457{
1458 struct page_cgroup *pc;
1459
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001460 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001461 return;
1462 if (!ptr)
1463 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001464 cgroup_exclude_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001465 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001466 mem_cgroup_lru_del_before_commit_swapcache(page);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001467 __mem_cgroup_commit_charge(ptr, pc, ctype);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001468 mem_cgroup_lru_add_after_commit_swapcache(page);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001469 /*
1470 * Now swap is on-memory. This means this page may be
1471 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08001472 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
1473 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
1474 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001475 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08001476 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001477 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001478 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001479 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001480
1481 id = swap_cgroup_record(ent, 0);
1482 rcu_read_lock();
1483 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001484 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001485 /*
1486 * This recorded memcg can be obsolete one. So, avoid
1487 * calling css_tryget
1488 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001489 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1490 mem_cgroup_put(memcg);
1491 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001492 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001493 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001494 /*
1495 * At swapin, we may charge account against cgroup which has no tasks.
1496 * So, rmdir()->pre_destroy() can be called while we do this charge.
1497 * In that case, we need to call pre_destroy() again. check it here.
1498 */
1499 cgroup_release_and_wakeup_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001500}
1501
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001502void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
1503{
1504 __mem_cgroup_commit_charge_swapin(page, ptr,
1505 MEM_CGROUP_CHARGE_TYPE_MAPPED);
1506}
1507
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001508void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
1509{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001510 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001511 return;
1512 if (!mem)
1513 return;
1514 res_counter_uncharge(&mem->res, PAGE_SIZE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001515 if (do_swap_account)
1516 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001517 css_put(&mem->css);
1518}
1519
1520
Balbir Singh8697d332008-02-07 00:13:59 -08001521/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001522 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001523 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001524static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001525__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001526{
Hugh Dickins82895462008-03-04 14:29:08 -08001527 struct page_cgroup *pc;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001528 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001529 struct mem_cgroup_per_zone *mz;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001530
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001531 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001532 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07001533
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001534 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001535 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001536
Balbir Singh8697d332008-02-07 00:13:59 -08001537 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08001538 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08001539 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001540 pc = lookup_page_cgroup(page);
1541 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001542 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001543
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001544 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001545
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001546 mem = pc->mem_cgroup;
1547
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001548 if (!PageCgroupUsed(pc))
1549 goto unlock_out;
1550
1551 switch (ctype) {
1552 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07001553 case MEM_CGROUP_CHARGE_TYPE_DROP:
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001554 if (page_mapped(page))
1555 goto unlock_out;
1556 break;
1557 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
1558 if (!PageAnon(page)) { /* Shared memory */
1559 if (page->mapping && !page_is_file_cache(page))
1560 goto unlock_out;
1561 } else if (page_mapped(page)) /* Anon */
1562 goto unlock_out;
1563 break;
1564 default:
1565 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001566 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001567
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001568 res_counter_uncharge(&mem->res, PAGE_SIZE);
1569 if (do_swap_account && (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT))
1570 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001571 mem_cgroup_charge_statistics(mem, pc, false);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001572
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001573 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001574 /*
1575 * pc->mem_cgroup is not cleared here. It will be accessed when it's
1576 * freed from LRU. This is safe because uncharged page is expected not
1577 * to be reused (freed soon). Exception is SwapCache, it's handled by
1578 * special functions.
1579 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08001580
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001581 mz = page_cgroup_zoneinfo(pc);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001582 unlock_page_cgroup(pc);
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08001583
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08001584 /* at swapout, this memcg will be accessed to record to swap */
1585 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1586 css_put(&mem->css);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001587
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001588 return mem;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001589
1590unlock_out:
1591 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001592 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08001593}
1594
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001595void mem_cgroup_uncharge_page(struct page *page)
1596{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001597 /* early check. */
1598 if (page_mapped(page))
1599 return;
1600 if (page->mapping && !PageAnon(page))
1601 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001602 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
1603}
1604
1605void mem_cgroup_uncharge_cache_page(struct page *page)
1606{
1607 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07001608 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001609 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
1610}
1611
Daisuke Nishimurae767e052009-05-28 14:34:28 -07001612#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001613/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07001614 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001615 * memcg information is recorded to swap_cgroup of "ent"
1616 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07001617void
1618mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001619{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001620 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07001621 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001622
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07001623 if (!swapout) /* this was a swap cache but the swap is unused ! */
1624 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
1625
1626 memcg = __mem_cgroup_uncharge_common(page, ctype);
1627
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001628 /* record memcg information */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07001629 if (do_swap_account && swapout && memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001630 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001631 mem_cgroup_get(memcg);
1632 }
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07001633 if (swapout && memcg)
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08001634 css_put(&memcg->css);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001635}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07001636#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001637
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001638#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1639/*
1640 * called from swap_entry_free(). remove record in swap_cgroup and
1641 * uncharge "memsw" account.
1642 */
1643void mem_cgroup_uncharge_swap(swp_entry_t ent)
1644{
1645 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001646 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001647
1648 if (!do_swap_account)
1649 return;
1650
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001651 id = swap_cgroup_record(ent, 0);
1652 rcu_read_lock();
1653 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001654 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001655 /*
1656 * We uncharge this because swap is freed.
1657 * This memcg can be obsolete one. We avoid calling css_tryget
1658 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001659 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1660 mem_cgroup_put(memcg);
1661 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001662 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001663}
1664#endif
1665
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001666/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001667 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
1668 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001669 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001670int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001671{
1672 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001673 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001674 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001675
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001676 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07001677 return 0;
1678
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001679 pc = lookup_page_cgroup(page);
1680 lock_page_cgroup(pc);
1681 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001682 mem = pc->mem_cgroup;
1683 css_get(&mem->css);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08001684 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001685 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001686
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001687 if (mem) {
Daisuke Nishimura3bb4edf2009-01-07 18:08:28 -08001688 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001689 css_put(&mem->css);
1690 }
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001691 *ptr = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001692 return ret;
1693}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08001694
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001695/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001696void mem_cgroup_end_migration(struct mem_cgroup *mem,
1697 struct page *oldpage, struct page *newpage)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001698{
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001699 struct page *target, *unused;
1700 struct page_cgroup *pc;
1701 enum charge_type ctype;
1702
1703 if (!mem)
1704 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001705 cgroup_exclude_rmdir(&mem->css);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001706 /* at migration success, oldpage->mapping is NULL. */
1707 if (oldpage->mapping) {
1708 target = oldpage;
1709 unused = NULL;
1710 } else {
1711 target = newpage;
1712 unused = oldpage;
1713 }
1714
1715 if (PageAnon(target))
1716 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
1717 else if (page_is_file_cache(target))
1718 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
1719 else
1720 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
1721
1722 /* unused page is not on radix-tree now. */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001723 if (unused)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001724 __mem_cgroup_uncharge_common(unused, ctype);
1725
1726 pc = lookup_page_cgroup(target);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001727 /*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001728 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
1729 * So, double-counting is effectively avoided.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001730 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001731 __mem_cgroup_commit_charge(mem, pc, ctype);
1732
1733 /*
1734 * Both of oldpage and newpage are still under lock_page().
1735 * Then, we don't have to care about race in radix-tree.
1736 * But we have to be careful that this page is unmapped or not.
1737 *
1738 * There is a case for !page_mapped(). At the start of
1739 * migration, oldpage was mapped. But now, it's zapped.
1740 * But we know *target* page is not freed/reused under us.
1741 * mem_cgroup_uncharge_page() does all necessary checks.
1742 */
1743 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
1744 mem_cgroup_uncharge_page(target);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001745 /*
1746 * At migration, we may charge account against cgroup which has no tasks
1747 * So, rmdir()->pre_destroy() can be called while we do this charge.
1748 * In that case, we need to call pre_destroy() again. check it here.
1749 */
1750 cgroup_release_and_wakeup_rmdir(&mem->css);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001751}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001752
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001753/*
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07001754 * A call to try to shrink memory usage on charge failure at shmem's swapin.
1755 * Calling hierarchical_reclaim is not enough because we should update
1756 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
1757 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
1758 * not from the memcg which this page would be charged to.
1759 * try_charge_swapin does all of these works properly.
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001760 */
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07001761int mem_cgroup_shmem_charge_fallback(struct page *page,
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001762 struct mm_struct *mm,
1763 gfp_t gfp_mask)
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001764{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001765 struct mem_cgroup *mem = NULL;
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07001766 int ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001767
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001768 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001769 return 0;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001770
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07001771 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1772 if (!ret)
1773 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001774
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07001775 return ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001776}
1777
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001778static DEFINE_MUTEX(set_limit_mutex);
1779
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08001780static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001781 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001782{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001783 int retry_count;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001784 int progress;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001785 u64 memswlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001786 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001787 int children = mem_cgroup_count_children(memcg);
1788 u64 curusage, oldusage;
1789
1790 /*
1791 * For keeping hierarchical_reclaim simple, how long we should retry
1792 * is depends on callers. We set our retry-count to be function
1793 * of # of children which we should visit in this loop.
1794 */
1795 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
1796
1797 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001798
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001799 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001800 if (signal_pending(current)) {
1801 ret = -EINTR;
1802 break;
1803 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001804 /*
1805 * Rather than hide all in some function, I do this in
1806 * open coded manner. You see what this really does.
1807 * We have to guarantee mem->res.limit < mem->memsw.limit.
1808 */
1809 mutex_lock(&set_limit_mutex);
1810 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1811 if (memswlimit < val) {
1812 ret = -EINVAL;
1813 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001814 break;
1815 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001816 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001817 if (!ret) {
1818 if (memswlimit == val)
1819 memcg->memsw_is_minimum = true;
1820 else
1821 memcg->memsw_is_minimum = false;
1822 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001823 mutex_unlock(&set_limit_mutex);
1824
1825 if (!ret)
1826 break;
1827
Daisuke Nishimura42e9abb2009-01-07 18:08:30 -08001828 progress = mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL,
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001829 false, true);
1830 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
1831 /* Usage is reduced ? */
1832 if (curusage >= oldusage)
1833 retry_count--;
1834 else
1835 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001836 }
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001837
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001838 return ret;
1839}
1840
Li Zefan338c8432009-06-17 16:27:15 -07001841static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
1842 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001843{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001844 int retry_count;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001845 u64 memlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001846 int children = mem_cgroup_count_children(memcg);
1847 int ret = -EBUSY;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001848
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001849 /* see mem_cgroup_resize_res_limit */
1850 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
1851 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001852 while (retry_count) {
1853 if (signal_pending(current)) {
1854 ret = -EINTR;
1855 break;
1856 }
1857 /*
1858 * Rather than hide all in some function, I do this in
1859 * open coded manner. You see what this really does.
1860 * We have to guarantee mem->res.limit < mem->memsw.limit.
1861 */
1862 mutex_lock(&set_limit_mutex);
1863 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
1864 if (memlimit > val) {
1865 ret = -EINVAL;
1866 mutex_unlock(&set_limit_mutex);
1867 break;
1868 }
1869 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001870 if (!ret) {
1871 if (memlimit == val)
1872 memcg->memsw_is_minimum = true;
1873 else
1874 memcg->memsw_is_minimum = false;
1875 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001876 mutex_unlock(&set_limit_mutex);
1877
1878 if (!ret)
1879 break;
1880
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001881 mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL, true, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001882 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001883 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001884 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001885 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001886 else
1887 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001888 }
1889 return ret;
1890}
1891
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001892/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001893 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001894 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
1895 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001896static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001897 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001898{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001899 struct zone *zone;
1900 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001901 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001902 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001903 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001904 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001905
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001906 zone = &NODE_DATA(node)->node_zones[zid];
1907 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001908 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001909
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001910 loop = MEM_CGROUP_ZSTAT(mz, lru);
1911 /* give some margin against EBUSY etc...*/
1912 loop += 256;
1913 busy = NULL;
1914 while (loop--) {
1915 ret = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001916 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001917 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001918 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001919 break;
1920 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001921 pc = list_entry(list->prev, struct page_cgroup, lru);
1922 if (busy == pc) {
1923 list_move(&pc->lru, list);
1924 busy = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001925 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001926 continue;
1927 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001928 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001929
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -08001930 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001931 if (ret == -ENOMEM)
1932 break;
1933
1934 if (ret == -EBUSY || ret == -EINVAL) {
1935 /* found lock contention or "pc" is obsolete. */
1936 busy = pc;
1937 cond_resched();
1938 } else
1939 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001940 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001941
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001942 if (!ret && !list_empty(list))
1943 return -EBUSY;
1944 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001945}
1946
1947/*
1948 * make mem_cgroup's charge to be 0 if there is no task.
1949 * This enables deleting this mem_cgroup.
1950 */
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001951static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001952{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001953 int ret;
1954 int node, zid, shrink;
1955 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001956 struct cgroup *cgrp = mem->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001957
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001958 css_get(&mem->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001959
1960 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001961 /* should free all ? */
1962 if (free_all)
1963 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001964move_account:
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001965 while (mem->res.usage > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001966 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001967 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001968 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001969 ret = -EINTR;
1970 if (signal_pending(current))
1971 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001972 /* This is for making all *used* pages to be on LRU. */
1973 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001974 ret = 0;
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08001975 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001976 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07001977 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001978 for_each_lru(l) {
1979 ret = mem_cgroup_force_empty_list(mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001980 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001981 if (ret)
1982 break;
1983 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001984 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001985 if (ret)
1986 break;
1987 }
1988 /* it seems parent cgroup doesn't have enough mem */
1989 if (ret == -ENOMEM)
1990 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001991 cond_resched();
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001992 }
1993 ret = 0;
1994out:
1995 css_put(&mem->css);
1996 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001997
1998try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001999 /* returns EBUSY if there is a task or if we come here twice. */
2000 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002001 ret = -EBUSY;
2002 goto out;
2003 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002004 /* we call try-to-free pages for make this cgroup empty */
2005 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002006 /* try to free all pages in this cgroup */
2007 shrink = 1;
2008 while (nr_retries && mem->res.usage > 0) {
2009 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002010
2011 if (signal_pending(current)) {
2012 ret = -EINTR;
2013 goto out;
2014 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002015 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
2016 false, get_swappiness(mem));
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002017 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002018 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002019 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02002020 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002021 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002022
2023 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002024 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002025 /* try move_account...there may be some *locked* pages. */
2026 if (mem->res.usage)
2027 goto move_account;
2028 ret = 0;
2029 goto out;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002030}
2031
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002032int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
2033{
2034 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
2035}
2036
2037
Balbir Singh18f59ea2009-01-07 18:08:07 -08002038static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
2039{
2040 return mem_cgroup_from_cont(cont)->use_hierarchy;
2041}
2042
2043static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
2044 u64 val)
2045{
2046 int retval = 0;
2047 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2048 struct cgroup *parent = cont->parent;
2049 struct mem_cgroup *parent_mem = NULL;
2050
2051 if (parent)
2052 parent_mem = mem_cgroup_from_cont(parent);
2053
2054 cgroup_lock();
2055 /*
2056 * If parent's use_hiearchy is set, we can't make any modifications
2057 * in the child subtrees. If it is unset, then the change can
2058 * occur, provided the current cgroup has no children.
2059 *
2060 * For the root cgroup, parent_mem is NULL, we allow value to be
2061 * set if there are no children.
2062 */
2063 if ((!parent_mem || !parent_mem->use_hierarchy) &&
2064 (val == 1 || val == 0)) {
2065 if (list_empty(&cont->children))
2066 mem->use_hierarchy = val;
2067 else
2068 retval = -EBUSY;
2069 } else
2070 retval = -EINVAL;
2071 cgroup_unlock();
2072
2073 return retval;
2074}
2075
Paul Menage2c3daa72008-04-29 00:59:58 -07002076static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002077{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002078 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2079 u64 val = 0;
2080 int type, name;
2081
2082 type = MEMFILE_TYPE(cft->private);
2083 name = MEMFILE_ATTR(cft->private);
2084 switch (type) {
2085 case _MEM:
2086 val = res_counter_read_u64(&mem->res, name);
2087 break;
2088 case _MEMSWAP:
Li Zefan338c8432009-06-17 16:27:15 -07002089 val = res_counter_read_u64(&mem->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002090 break;
2091 default:
2092 BUG();
2093 break;
2094 }
2095 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002096}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002097/*
2098 * The user of this function is...
2099 * RES_LIMIT.
2100 */
Paul Menage856c13a2008-07-25 01:47:04 -07002101static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
2102 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002103{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002104 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002105 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002106 unsigned long long val;
2107 int ret;
2108
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002109 type = MEMFILE_TYPE(cft->private);
2110 name = MEMFILE_ATTR(cft->private);
2111 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002112 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07002113 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
2114 ret = -EINVAL;
2115 break;
2116 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002117 /* This function does all necessary parse...reuse it */
2118 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002119 if (ret)
2120 break;
2121 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002122 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002123 else
2124 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002125 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07002126 case RES_SOFT_LIMIT:
2127 ret = res_counter_memparse_write_strategy(buffer, &val);
2128 if (ret)
2129 break;
2130 /*
2131 * For memsw, soft limits are hard to implement in terms
2132 * of semantics, for now, we support soft limits for
2133 * control without swap
2134 */
2135 if (type == _MEM)
2136 ret = res_counter_set_soft_limit(&memcg->res, val);
2137 else
2138 ret = -EINVAL;
2139 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002140 default:
2141 ret = -EINVAL; /* should be BUG() ? */
2142 break;
2143 }
2144 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002145}
2146
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08002147static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
2148 unsigned long long *mem_limit, unsigned long long *memsw_limit)
2149{
2150 struct cgroup *cgroup;
2151 unsigned long long min_limit, min_memsw_limit, tmp;
2152
2153 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2154 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2155 cgroup = memcg->css.cgroup;
2156 if (!memcg->use_hierarchy)
2157 goto out;
2158
2159 while (cgroup->parent) {
2160 cgroup = cgroup->parent;
2161 memcg = mem_cgroup_from_cont(cgroup);
2162 if (!memcg->use_hierarchy)
2163 break;
2164 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
2165 min_limit = min(min_limit, tmp);
2166 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2167 min_memsw_limit = min(min_memsw_limit, tmp);
2168 }
2169out:
2170 *mem_limit = min_limit;
2171 *memsw_limit = min_memsw_limit;
2172 return;
2173}
2174
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002175static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002176{
2177 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002178 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002179
2180 mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002181 type = MEMFILE_TYPE(event);
2182 name = MEMFILE_ATTR(event);
2183 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002184 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002185 if (type == _MEM)
2186 res_counter_reset_max(&mem->res);
2187 else
2188 res_counter_reset_max(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002189 break;
2190 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002191 if (type == _MEM)
2192 res_counter_reset_failcnt(&mem->res);
2193 else
2194 res_counter_reset_failcnt(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002195 break;
2196 }
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07002197 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002198}
2199
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002200
2201/* For read statistics */
2202enum {
2203 MCS_CACHE,
2204 MCS_RSS,
Balbir Singhd69b0422009-06-17 16:26:34 -07002205 MCS_MAPPED_FILE,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002206 MCS_PGPGIN,
2207 MCS_PGPGOUT,
2208 MCS_INACTIVE_ANON,
2209 MCS_ACTIVE_ANON,
2210 MCS_INACTIVE_FILE,
2211 MCS_ACTIVE_FILE,
2212 MCS_UNEVICTABLE,
2213 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002214};
2215
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002216struct mcs_total_stat {
2217 s64 stat[NR_MCS_STAT];
2218};
2219
2220struct {
2221 char *local_name;
2222 char *total_name;
2223} memcg_stat_strings[NR_MCS_STAT] = {
2224 {"cache", "total_cache"},
2225 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07002226 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002227 {"pgpgin", "total_pgpgin"},
2228 {"pgpgout", "total_pgpgout"},
2229 {"inactive_anon", "total_inactive_anon"},
2230 {"active_anon", "total_active_anon"},
2231 {"inactive_file", "total_inactive_file"},
2232 {"active_file", "total_active_file"},
2233 {"unevictable", "total_unevictable"}
2234};
2235
2236
2237static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
2238{
2239 struct mcs_total_stat *s = data;
2240 s64 val;
2241
2242 /* per cpu stat */
2243 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE);
2244 s->stat[MCS_CACHE] += val * PAGE_SIZE;
2245 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
2246 s->stat[MCS_RSS] += val * PAGE_SIZE;
Balbir Singhd69b0422009-06-17 16:26:34 -07002247 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_MAPPED_FILE);
2248 s->stat[MCS_MAPPED_FILE] += val * PAGE_SIZE;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002249 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT);
2250 s->stat[MCS_PGPGIN] += val;
2251 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT);
2252 s->stat[MCS_PGPGOUT] += val;
2253
2254 /* per zone stat */
2255 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
2256 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
2257 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
2258 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
2259 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
2260 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
2261 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
2262 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
2263 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
2264 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
2265 return 0;
2266}
2267
2268static void
2269mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
2270{
2271 mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
2272}
2273
Paul Menagec64745c2008-04-29 01:00:02 -07002274static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
2275 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002276{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002277 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002278 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002279 int i;
2280
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002281 memset(&mystat, 0, sizeof(mystat));
2282 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002283
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002284 for (i = 0; i < NR_MCS_STAT; i++)
2285 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002286
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002287 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08002288 {
2289 unsigned long long limit, memsw_limit;
2290 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
2291 cb->fill(cb, "hierarchical_memory_limit", limit);
2292 if (do_swap_account)
2293 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
2294 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002295
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002296 memset(&mystat, 0, sizeof(mystat));
2297 mem_cgroup_get_total_stat(mem_cont, &mystat);
2298 for (i = 0; i < NR_MCS_STAT; i++)
2299 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
2300
2301
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002302#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08002303 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002304
2305 {
2306 int nid, zid;
2307 struct mem_cgroup_per_zone *mz;
2308 unsigned long recent_rotated[2] = {0, 0};
2309 unsigned long recent_scanned[2] = {0, 0};
2310
2311 for_each_online_node(nid)
2312 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
2313 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
2314
2315 recent_rotated[0] +=
2316 mz->reclaim_stat.recent_rotated[0];
2317 recent_rotated[1] +=
2318 mz->reclaim_stat.recent_rotated[1];
2319 recent_scanned[0] +=
2320 mz->reclaim_stat.recent_scanned[0];
2321 recent_scanned[1] +=
2322 mz->reclaim_stat.recent_scanned[1];
2323 }
2324 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
2325 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
2326 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
2327 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
2328 }
2329#endif
2330
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002331 return 0;
2332}
2333
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002334static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
2335{
2336 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2337
2338 return get_swappiness(memcg);
2339}
2340
2341static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
2342 u64 val)
2343{
2344 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2345 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08002346
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002347 if (val > 100)
2348 return -EINVAL;
2349
2350 if (cgrp->parent == NULL)
2351 return -EINVAL;
2352
2353 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08002354
2355 cgroup_lock();
2356
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002357 /* If under hierarchy, only empty-root can set this value */
2358 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08002359 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
2360 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002361 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08002362 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002363
2364 spin_lock(&memcg->reclaim_param_lock);
2365 memcg->swappiness = val;
2366 spin_unlock(&memcg->reclaim_param_lock);
2367
Li Zefan068b38c2009-01-15 13:51:26 -08002368 cgroup_unlock();
2369
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002370 return 0;
2371}
2372
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002373
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002374static struct cftype mem_cgroup_files[] = {
2375 {
Balbir Singh0eea1032008-02-07 00:13:57 -08002376 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002377 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07002378 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002379 },
2380 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002381 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002382 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002383 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002384 .read_u64 = mem_cgroup_read,
2385 },
2386 {
Balbir Singh0eea1032008-02-07 00:13:57 -08002387 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002388 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07002389 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07002390 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002391 },
2392 {
Balbir Singh296c81d2009-09-23 15:56:36 -07002393 .name = "soft_limit_in_bytes",
2394 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
2395 .write_string = mem_cgroup_write,
2396 .read_u64 = mem_cgroup_read,
2397 },
2398 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002399 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002400 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002401 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07002402 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002403 },
Balbir Singh8697d332008-02-07 00:13:59 -08002404 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002405 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07002406 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002407 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002408 {
2409 .name = "force_empty",
2410 .trigger = mem_cgroup_force_empty_write,
2411 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08002412 {
2413 .name = "use_hierarchy",
2414 .write_u64 = mem_cgroup_hierarchy_write,
2415 .read_u64 = mem_cgroup_hierarchy_read,
2416 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002417 {
2418 .name = "swappiness",
2419 .read_u64 = mem_cgroup_swappiness_read,
2420 .write_u64 = mem_cgroup_swappiness_write,
2421 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002422};
2423
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002424#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2425static struct cftype memsw_cgroup_files[] = {
2426 {
2427 .name = "memsw.usage_in_bytes",
2428 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
2429 .read_u64 = mem_cgroup_read,
2430 },
2431 {
2432 .name = "memsw.max_usage_in_bytes",
2433 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
2434 .trigger = mem_cgroup_reset,
2435 .read_u64 = mem_cgroup_read,
2436 },
2437 {
2438 .name = "memsw.limit_in_bytes",
2439 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
2440 .write_string = mem_cgroup_write,
2441 .read_u64 = mem_cgroup_read,
2442 },
2443 {
2444 .name = "memsw.failcnt",
2445 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
2446 .trigger = mem_cgroup_reset,
2447 .read_u64 = mem_cgroup_read,
2448 },
2449};
2450
2451static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2452{
2453 if (!do_swap_account)
2454 return 0;
2455 return cgroup_add_files(cont, ss, memsw_cgroup_files,
2456 ARRAY_SIZE(memsw_cgroup_files));
2457};
2458#else
2459static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2460{
2461 return 0;
2462}
2463#endif
2464
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002465static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2466{
2467 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002468 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07002469 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07002470 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002471 /*
2472 * This routine is called against possible nodes.
2473 * But it's BUG to call kmalloc() against offline node.
2474 *
2475 * TODO: this routine can waste much memory for nodes which will
2476 * never be onlined. It's better to use memory hotplug callback
2477 * function.
2478 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07002479 if (!node_state(node, N_NORMAL_MEMORY))
2480 tmp = -1;
2481 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002482 if (!pn)
2483 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002484
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002485 mem->info.nodeinfo[node] = pn;
2486 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002487
2488 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
2489 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07002490 for_each_lru(l)
2491 INIT_LIST_HEAD(&mz->lists[l]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002492 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002493 return 0;
2494}
2495
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002496static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2497{
2498 kfree(mem->info.nodeinfo[node]);
2499}
2500
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002501static int mem_cgroup_size(void)
2502{
2503 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
2504 return sizeof(struct mem_cgroup) + cpustat_size;
2505}
2506
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002507static struct mem_cgroup *mem_cgroup_alloc(void)
2508{
2509 struct mem_cgroup *mem;
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002510 int size = mem_cgroup_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002511
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002512 if (size < PAGE_SIZE)
2513 mem = kmalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002514 else
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002515 mem = vmalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002516
2517 if (mem)
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002518 memset(mem, 0, size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002519 return mem;
2520}
2521
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002522/*
2523 * At destroying mem_cgroup, references from swap_cgroup can remain.
2524 * (scanning all at force_empty is too costly...)
2525 *
2526 * Instead of clearing all references at force_empty, we remember
2527 * the number of reference from swap_cgroup and free mem_cgroup when
2528 * it goes down to 0.
2529 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002530 * Removal of cgroup itself succeeds regardless of refs from swap.
2531 */
2532
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08002533static void __mem_cgroup_free(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002534{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002535 int node;
2536
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002537 free_css_id(&mem_cgroup_subsys, &mem->css);
2538
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002539 for_each_node_state(node, N_POSSIBLE)
2540 free_mem_cgroup_per_zone_info(mem, node);
2541
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002542 if (mem_cgroup_size() < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002543 kfree(mem);
2544 else
2545 vfree(mem);
2546}
2547
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002548static void mem_cgroup_get(struct mem_cgroup *mem)
2549{
2550 atomic_inc(&mem->refcnt);
2551}
2552
2553static void mem_cgroup_put(struct mem_cgroup *mem)
2554{
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08002555 if (atomic_dec_and_test(&mem->refcnt)) {
2556 struct mem_cgroup *parent = parent_mem_cgroup(mem);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08002557 __mem_cgroup_free(mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08002558 if (parent)
2559 mem_cgroup_put(parent);
2560 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002561}
2562
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08002563/*
2564 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
2565 */
2566static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
2567{
2568 if (!mem->res.parent)
2569 return NULL;
2570 return mem_cgroup_from_res_counter(mem->res.parent, res);
2571}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002572
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08002573#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2574static void __init enable_swap_cgroup(void)
2575{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002576 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08002577 do_swap_account = 1;
2578}
2579#else
2580static void __init enable_swap_cgroup(void)
2581{
2582}
2583#endif
2584
Li Zefan0eb253e2009-01-15 13:51:25 -08002585static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002586mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
2587{
Balbir Singh28dbc4b2009-01-07 18:08:05 -08002588 struct mem_cgroup *mem, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002589 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002590 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002591
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002592 mem = mem_cgroup_alloc();
2593 if (!mem)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002594 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08002595
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002596 for_each_node_state(node, N_POSSIBLE)
2597 if (alloc_mem_cgroup_per_zone_info(mem, node))
2598 goto free_out;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08002599 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08002600 if (cont->parent == NULL) {
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08002601 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08002602 parent = NULL;
Balbir Singh4b3bde42009-09-23 15:56:32 -07002603 root_mem_cgroup = mem;
Balbir Singh18f59ea2009-01-07 18:08:07 -08002604 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08002605 parent = mem_cgroup_from_cont(cont->parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08002606 mem->use_hierarchy = parent->use_hierarchy;
2607 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08002608
Balbir Singh18f59ea2009-01-07 18:08:07 -08002609 if (parent && parent->use_hierarchy) {
2610 res_counter_init(&mem->res, &parent->res);
2611 res_counter_init(&mem->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08002612 /*
2613 * We increment refcnt of the parent to ensure that we can
2614 * safely access it on res_counter_charge/uncharge.
2615 * This refcnt will be decremented when freeing this
2616 * mem_cgroup(see mem_cgroup_put).
2617 */
2618 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08002619 } else {
2620 res_counter_init(&mem->res, NULL);
2621 res_counter_init(&mem->memsw, NULL);
2622 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002623 mem->last_scanned_child = 0;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -08002624 spin_lock_init(&mem->reclaim_param_lock);
Balbir Singh6d61ef42009-01-07 18:08:06 -08002625
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002626 if (parent)
2627 mem->swappiness = get_swappiness(parent);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08002628 atomic_set(&mem->refcnt, 1);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002629 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002630free_out:
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08002631 __mem_cgroup_free(mem);
Balbir Singh4b3bde42009-09-23 15:56:32 -07002632 root_mem_cgroup = NULL;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002633 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002634}
2635
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002636static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08002637 struct cgroup *cont)
2638{
2639 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002640
2641 return mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08002642}
2643
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002644static void mem_cgroup_destroy(struct cgroup_subsys *ss,
2645 struct cgroup *cont)
2646{
Daisuke Nishimurac268e992009-01-15 13:51:13 -08002647 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08002648
Daisuke Nishimurac268e992009-01-15 13:51:13 -08002649 mem_cgroup_put(mem);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002650}
2651
2652static int mem_cgroup_populate(struct cgroup_subsys *ss,
2653 struct cgroup *cont)
2654{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002655 int ret;
2656
2657 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
2658 ARRAY_SIZE(mem_cgroup_files));
2659
2660 if (!ret)
2661 ret = register_memsw_files(cont, ss);
2662 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002663}
2664
Balbir Singh67e465a2008-02-07 00:13:54 -08002665static void mem_cgroup_move_task(struct cgroup_subsys *ss,
2666 struct cgroup *cont,
2667 struct cgroup *old_cont,
Ben Blumbe367d02009-09-23 15:56:31 -07002668 struct task_struct *p,
2669 bool threadgroup)
Balbir Singh67e465a2008-02-07 00:13:54 -08002670{
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08002671 mutex_lock(&memcg_tasklist);
Balbir Singh67e465a2008-02-07 00:13:54 -08002672 /*
Nikanth Karthikesanf9717d22009-01-07 18:08:11 -08002673 * FIXME: It's better to move charges of this process from old
2674 * memcg to new memcg. But it's just on TODO-List now.
Balbir Singh67e465a2008-02-07 00:13:54 -08002675 */
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08002676 mutex_unlock(&memcg_tasklist);
Balbir Singh67e465a2008-02-07 00:13:54 -08002677}
2678
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002679struct cgroup_subsys mem_cgroup_subsys = {
2680 .name = "memory",
2681 .subsys_id = mem_cgroup_subsys_id,
2682 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08002683 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002684 .destroy = mem_cgroup_destroy,
2685 .populate = mem_cgroup_populate,
Balbir Singh67e465a2008-02-07 00:13:54 -08002686 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002687 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002688 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002689};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08002690
2691#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2692
2693static int __init disable_swap_account(char *s)
2694{
2695 really_do_swap_account = 0;
2696 return 1;
2697}
2698__setup("noswapaccount", disable_swap_account);
2699#endif