blob: 025f8abfae2d945f420f1c0f25e88bddafb6b838 [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>
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -080030#include <linux/mutex.h>
Balbir Singhb6ac57d2008-04-29 01:00:19 -070031#include <linux/slab.h>
Balbir Singh66e17072008-02-07 00:13:56 -080032#include <linux/swap.h>
33#include <linux/spinlock.h>
34#include <linux/fs.h>
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -080035#include <linux/seq_file.h>
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -070036#include <linux/vmalloc.h>
Christoph Lameterb69408e2008-10-18 20:26:14 -070037#include <linux/mm_inline.h>
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070038#include <linux/page_cgroup.h>
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -080039#include "internal.h"
Balbir Singh8cdea7c2008-02-07 00:13:50 -080040
Balbir Singh8697d332008-02-07 00:13:59 -080041#include <asm/uaccess.h>
42
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070043struct cgroup_subsys mem_cgroup_subsys __read_mostly;
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070044#define MEM_CGROUP_RECLAIM_RETRIES 5
Balbir Singh8cdea7c2008-02-07 00:13:50 -080045
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080046#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
47/* Turned on only when memory cgroup is enabled && really_do_swap_account = 0 */
48int do_swap_account __read_mostly;
49static int really_do_swap_account __initdata = 1; /* for remember boot option*/
50#else
51#define do_swap_account (0)
52#endif
53
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -080054static DEFINE_MUTEX(memcg_tasklist); /* can be hold under cgroup_mutex */
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080055
Balbir Singh8cdea7c2008-02-07 00:13:50 -080056/*
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080057 * Statistics for memory cgroup.
58 */
59enum mem_cgroup_stat_index {
60 /*
61 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
62 */
63 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
64 MEM_CGROUP_STAT_RSS, /* # of pages charged as rss */
Balaji Rao55e462b2008-05-01 04:35:12 -070065 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
66 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080067
68 MEM_CGROUP_STAT_NSTATS,
69};
70
71struct mem_cgroup_stat_cpu {
72 s64 count[MEM_CGROUP_STAT_NSTATS];
73} ____cacheline_aligned_in_smp;
74
75struct mem_cgroup_stat {
Jan Blunckc8dad2b2009-01-07 18:07:53 -080076 struct mem_cgroup_stat_cpu cpustat[0];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080077};
78
79/*
80 * For accounting under irq disable, no need for increment preempt count.
81 */
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -070082static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080083 enum mem_cgroup_stat_index idx, int val)
84{
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -070085 stat->count[idx] += val;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080086}
87
88static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
89 enum mem_cgroup_stat_index idx)
90{
91 int cpu;
92 s64 ret = 0;
93 for_each_possible_cpu(cpu)
94 ret += stat->cpustat[cpu].count[idx];
95 return ret;
96}
97
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -070098static s64 mem_cgroup_local_usage(struct mem_cgroup_stat *stat)
99{
100 s64 ret;
101
102 ret = mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_CACHE);
103 ret += mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_RSS);
104 return ret;
105}
106
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800107/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800108 * per-zone information in memory controller.
109 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800110struct mem_cgroup_per_zone {
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800111 /*
112 * spin_lock to protect the per cgroup LRU
113 */
Christoph Lameterb69408e2008-10-18 20:26:14 -0700114 struct list_head lists[NR_LRU_LISTS];
115 unsigned long count[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800116
117 struct zone_reclaim_stat reclaim_stat;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800118};
119/* Macro for accessing counter */
120#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
121
122struct mem_cgroup_per_node {
123 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
124};
125
126struct mem_cgroup_lru_info {
127 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
128};
129
130/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800131 * The memory controller data structure. The memory controller controls both
132 * page cache and RSS per cgroup. We would eventually like to provide
133 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
134 * to help the administrator determine what knobs to tune.
135 *
136 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800137 * we hit the water mark. May be even add a low water mark, such that
138 * no reclaim occurs from a cgroup at it's low water mark, this is
139 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800140 */
141struct mem_cgroup {
142 struct cgroup_subsys_state css;
143 /*
144 * the counter to account for memory usage
145 */
146 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800147 /*
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800148 * the counter to account for mem+swap usage.
149 */
150 struct res_counter memsw;
151 /*
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800152 * Per cgroup active and inactive list, similar to the
153 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800154 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800155 struct mem_cgroup_lru_info info;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800156
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800157 /*
158 protect against reclaim related member.
159 */
160 spinlock_t reclaim_param_lock;
161
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800162 int prev_priority; /* for recording reclaim priority */
Balbir Singh6d61ef42009-01-07 18:08:06 -0800163
164 /*
165 * While reclaiming in a hiearchy, we cache the last child we
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700166 * reclaimed from.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800167 */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700168 int last_scanned_child;
Balbir Singh18f59ea2009-01-07 18:08:07 -0800169 /*
170 * Should the accounting and control be hierarchical, per subtree?
171 */
172 bool use_hierarchy;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800173 unsigned long last_oom_jiffies;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800174 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800175
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800176 unsigned int swappiness;
177
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800178 /*
Jan Blunckc8dad2b2009-01-07 18:07:53 -0800179 * statistics. This must be placed at the end of memcg.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800180 */
181 struct mem_cgroup_stat stat;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800182};
183
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800184enum charge_type {
185 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
186 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700187 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700188 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800189 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700190 NR_CHARGE_TYPE,
191};
192
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700193/* only for here (for easy reading.) */
194#define PCGF_CACHE (1UL << PCG_CACHE)
195#define PCGF_USED (1UL << PCG_USED)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700196#define PCGF_LOCK (1UL << PCG_LOCK)
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700197static const unsigned long
198pcg_default_flags[NR_CHARGE_TYPE] = {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800199 PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* File Cache */
200 PCGF_USED | PCGF_LOCK, /* Anon */
201 PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* Shmem */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700202 0, /* FORCE */
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800203};
204
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800205/* for encoding cft->private value on file */
206#define _MEM (0)
207#define _MEMSWAP (1)
208#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
209#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
210#define MEMFILE_ATTR(val) ((val) & 0xffff)
211
212static void mem_cgroup_get(struct mem_cgroup *mem);
213static void mem_cgroup_put(struct mem_cgroup *mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -0800214static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800215
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700216static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
217 struct page_cgroup *pc,
218 bool charge)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800219{
220 int val = (charge)? 1 : -1;
221 struct mem_cgroup_stat *stat = &mem->stat;
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700222 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800223 int cpu = get_cpu();
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800224
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800225 cpustat = &stat->cpustat[cpu];
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700226 if (PageCgroupCache(pc))
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700227 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800228 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700229 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700230
231 if (charge)
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700232 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700233 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
234 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700235 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700236 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800237 put_cpu();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800238}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800239
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800240static struct mem_cgroup_per_zone *
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800241mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
242{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800243 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
244}
245
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800246static struct mem_cgroup_per_zone *
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800247page_cgroup_zoneinfo(struct page_cgroup *pc)
248{
249 struct mem_cgroup *mem = pc->mem_cgroup;
250 int nid = page_cgroup_nid(pc);
251 int zid = page_cgroup_zid(pc);
252
KOSAKI Motohiro54992762009-01-07 18:08:18 -0800253 if (!mem)
254 return NULL;
255
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800256 return mem_cgroup_zoneinfo(mem, nid, zid);
257}
258
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700259static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700260 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800261{
262 int nid, zid;
263 struct mem_cgroup_per_zone *mz;
264 u64 total = 0;
265
266 for_each_online_node(nid)
267 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
268 mz = mem_cgroup_zoneinfo(mem, nid, zid);
269 total += MEM_CGROUP_ZSTAT(mz, idx);
270 }
271 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800272}
273
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800274static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800275{
276 return container_of(cgroup_subsys_state(cont,
277 mem_cgroup_subsys_id), struct mem_cgroup,
278 css);
279}
280
Balbir Singhcf475ad2008-04-29 01:00:16 -0700281struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800282{
Balbir Singh31a78f22008-09-28 23:09:31 +0100283 /*
284 * mm_update_next_owner() may clear mm->owner to NULL
285 * if it races with swapoff, page migration, etc.
286 * So this can be called with p == NULL.
287 */
288 if (unlikely(!p))
289 return NULL;
290
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800291 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
292 struct mem_cgroup, css);
293}
294
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800295static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
296{
297 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700298
299 if (!mm)
300 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800301 /*
302 * Because we have no locks, mm->owner's may be being moved to other
303 * cgroup. We use css_tryget() here even if this looks
304 * pessimistic (rather than adding locks here).
305 */
306 rcu_read_lock();
307 do {
308 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
309 if (unlikely(!mem))
310 break;
311 } while (!css_tryget(&mem->css));
312 rcu_read_unlock();
313 return mem;
314}
315
316static bool mem_cgroup_is_obsolete(struct mem_cgroup *mem)
317{
318 if (!mem)
319 return true;
320 return css_is_removed(&mem->css);
321}
322
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700323
324/*
325 * Call callback function against all cgroup under hierarchy tree.
326 */
327static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
328 int (*func)(struct mem_cgroup *, void *))
329{
330 int found, ret, nextid;
331 struct cgroup_subsys_state *css;
332 struct mem_cgroup *mem;
333
334 if (!root->use_hierarchy)
335 return (*func)(root, data);
336
337 nextid = 1;
338 do {
339 ret = 0;
340 mem = NULL;
341
342 rcu_read_lock();
343 css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
344 &found);
345 if (css && css_tryget(css))
346 mem = container_of(css, struct mem_cgroup, css);
347 rcu_read_unlock();
348
349 if (mem) {
350 ret = (*func)(mem, data);
351 css_put(&mem->css);
352 }
353 nextid = found + 1;
354 } while (!ret && css);
355
356 return ret;
357}
358
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800359/*
360 * Following LRU functions are allowed to be used without PCG_LOCK.
361 * Operations are called by routine of global LRU independently from memcg.
362 * What we have to take care of here is validness of pc->mem_cgroup.
363 *
364 * Changes to pc->mem_cgroup happens when
365 * 1. charge
366 * 2. moving account
367 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
368 * It is added to LRU before charge.
369 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
370 * When moving account, the page is not on LRU. It's isolated.
371 */
372
373void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800374{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800375 struct page_cgroup *pc;
376 struct mem_cgroup *mem;
377 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700378
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800379 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800380 return;
381 pc = lookup_page_cgroup(page);
382 /* can happen while we handle swapcache. */
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800383 if (list_empty(&pc->lru) || !pc->mem_cgroup)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800384 return;
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800385 /*
386 * We don't check PCG_USED bit. It's cleared when the "page" is finally
387 * removed from global LRU.
388 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800389 mz = page_cgroup_zoneinfo(pc);
390 mem = pc->mem_cgroup;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700391 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800392 list_del_init(&pc->lru);
393 return;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800394}
395
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800396void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800397{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800398 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800399}
400
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800401void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800402{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800403 struct mem_cgroup_per_zone *mz;
404 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800405
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800406 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700407 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700408
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800409 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800410 /*
411 * Used bit is set without atomic ops but after smp_wmb().
412 * For making pc->mem_cgroup visible, insert smp_rmb() here.
413 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800414 smp_rmb();
415 /* unused page is not rotated. */
416 if (!PageCgroupUsed(pc))
417 return;
418 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700419 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800420}
421
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800422void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
423{
424 struct page_cgroup *pc;
425 struct mem_cgroup_per_zone *mz;
426
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800427 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800428 return;
429 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800430 /*
431 * Used bit is set without atomic ops but after smp_wmb().
432 * For making pc->mem_cgroup visible, insert smp_rmb() here.
433 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800434 smp_rmb();
435 if (!PageCgroupUsed(pc))
436 return;
437
438 mz = page_cgroup_zoneinfo(pc);
439 MEM_CGROUP_ZSTAT(mz, lru) += 1;
440 list_add(&pc->lru, &mz->lists[lru]);
441}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800442
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800443/*
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800444 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
445 * lru because the page may.be reused after it's fully uncharged (because of
446 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
447 * it again. This function is only used to charge SwapCache. It's done under
448 * lock_page and expected that zone->lru_lock is never held.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800449 */
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800450static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800451{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800452 unsigned long flags;
453 struct zone *zone = page_zone(page);
454 struct page_cgroup *pc = lookup_page_cgroup(page);
455
456 spin_lock_irqsave(&zone->lru_lock, flags);
457 /*
458 * Forget old LRU when this page_cgroup is *not* used. This Used bit
459 * is guarded by lock_page() because the page is SwapCache.
460 */
461 if (!PageCgroupUsed(pc))
462 mem_cgroup_del_lru_list(page, page_lru(page));
463 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800464}
465
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800466static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
467{
468 unsigned long flags;
469 struct zone *zone = page_zone(page);
470 struct page_cgroup *pc = lookup_page_cgroup(page);
471
472 spin_lock_irqsave(&zone->lru_lock, flags);
473 /* link when the page is linked to LRU but page_cgroup isn't */
474 if (PageLRU(page) && list_empty(&pc->lru))
475 mem_cgroup_add_lru_list(page, page_lru(page));
476 spin_unlock_irqrestore(&zone->lru_lock, flags);
477}
478
479
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800480void mem_cgroup_move_lists(struct page *page,
481 enum lru_list from, enum lru_list to)
482{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800483 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800484 return;
485 mem_cgroup_del_lru_list(page, from);
486 mem_cgroup_add_lru_list(page, to);
487}
488
David Rientjes4c4a2212008-02-07 00:14:06 -0800489int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
490{
491 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700492 struct mem_cgroup *curr = NULL;
David Rientjes4c4a2212008-02-07 00:14:06 -0800493
494 task_lock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700495 rcu_read_lock();
496 curr = try_get_mem_cgroup_from_mm(task->mm);
497 rcu_read_unlock();
David Rientjes4c4a2212008-02-07 00:14:06 -0800498 task_unlock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700499 if (!curr)
500 return 0;
501 if (curr->use_hierarchy)
502 ret = css_is_ancestor(&curr->css, &mem->css);
503 else
504 ret = (curr == mem);
505 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -0800506 return ret;
507}
508
Balbir Singh66e17072008-02-07 00:13:56 -0800509/*
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800510 * Calculate mapped_ratio under memory controller. This will be used in
511 * vmscan.c for deteremining we have to reclaim mapped pages.
512 */
513int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
514{
515 long total, rss;
516
517 /*
518 * usage is recorded in bytes. But, here, we assume the number of
519 * physical pages can be represented by "long" on any arch.
520 */
521 total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
522 rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
523 return (int)((rss * 100L) / total);
524}
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800525
KAMEZAWA Hiroyuki5932f362008-02-07 00:14:33 -0800526/*
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800527 * prev_priority control...this will be used in memory reclaim path.
528 */
529int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
530{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800531 int prev_priority;
532
533 spin_lock(&mem->reclaim_param_lock);
534 prev_priority = mem->prev_priority;
535 spin_unlock(&mem->reclaim_param_lock);
536
537 return prev_priority;
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800538}
539
540void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
541{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800542 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800543 if (priority < mem->prev_priority)
544 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800545 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800546}
547
548void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
549{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800550 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800551 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800552 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800553}
554
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800555static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800556{
557 unsigned long active;
558 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800559 unsigned long gb;
560 unsigned long inactive_ratio;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800561
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700562 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
563 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800564
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800565 gb = (inactive + active) >> (30 - PAGE_SHIFT);
566 if (gb)
567 inactive_ratio = int_sqrt(10 * gb);
568 else
569 inactive_ratio = 1;
570
571 if (present_pages) {
572 present_pages[0] = inactive;
573 present_pages[1] = active;
574 }
575
576 return inactive_ratio;
577}
578
579int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
580{
581 unsigned long active;
582 unsigned long inactive;
583 unsigned long present_pages[2];
584 unsigned long inactive_ratio;
585
586 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
587
588 inactive = present_pages[0];
589 active = present_pages[1];
590
591 if (inactive * inactive_ratio < active)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800592 return 1;
593
594 return 0;
595}
596
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800597unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
598 struct zone *zone,
599 enum lru_list lru)
600{
601 int nid = zone->zone_pgdat->node_id;
602 int zid = zone_idx(zone);
603 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
604
605 return MEM_CGROUP_ZSTAT(mz, lru);
606}
607
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800608struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
609 struct zone *zone)
610{
611 int nid = zone->zone_pgdat->node_id;
612 int zid = zone_idx(zone);
613 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
614
615 return &mz->reclaim_stat;
616}
617
618struct zone_reclaim_stat *
619mem_cgroup_get_reclaim_stat_from_page(struct page *page)
620{
621 struct page_cgroup *pc;
622 struct mem_cgroup_per_zone *mz;
623
624 if (mem_cgroup_disabled())
625 return NULL;
626
627 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800628 /*
629 * Used bit is set without atomic ops but after smp_wmb().
630 * For making pc->mem_cgroup visible, insert smp_rmb() here.
631 */
632 smp_rmb();
633 if (!PageCgroupUsed(pc))
634 return NULL;
635
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800636 mz = page_cgroup_zoneinfo(pc);
637 if (!mz)
638 return NULL;
639
640 return &mz->reclaim_stat;
641}
642
Balbir Singh66e17072008-02-07 00:13:56 -0800643unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
644 struct list_head *dst,
645 unsigned long *scanned, int order,
646 int mode, struct zone *z,
647 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700648 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -0800649{
650 unsigned long nr_taken = 0;
651 struct page *page;
652 unsigned long scan;
653 LIST_HEAD(pc_list);
654 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800655 struct page_cgroup *pc, *tmp;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800656 int nid = z->zone_pgdat->node_id;
657 int zid = zone_idx(z);
658 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700659 int lru = LRU_FILE * !!file + !!active;
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++;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700677 if (__isolate_lru_page(page, mode, file) == 0) {
Balbir Singh66e17072008-02-07 00:13:56 -0800678 list_move(&page->lru, dst);
679 nr_taken++;
680 }
681 }
682
Balbir Singh66e17072008-02-07 00:13:56 -0800683 *scanned = scan;
684 return nr_taken;
685}
686
Balbir Singh6d61ef42009-01-07 18:08:06 -0800687#define mem_cgroup_from_res_counter(counter, member) \
688 container_of(counter, struct mem_cgroup, member)
689
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -0800690static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
691{
692 if (do_swap_account) {
693 if (res_counter_check_under_limit(&mem->res) &&
694 res_counter_check_under_limit(&mem->memsw))
695 return true;
696 } else
697 if (res_counter_check_under_limit(&mem->res))
698 return true;
699 return false;
700}
701
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800702static unsigned int get_swappiness(struct mem_cgroup *memcg)
703{
704 struct cgroup *cgrp = memcg->css.cgroup;
705 unsigned int swappiness;
706
707 /* root ? */
708 if (cgrp->parent == NULL)
709 return vm_swappiness;
710
711 spin_lock(&memcg->reclaim_param_lock);
712 swappiness = memcg->swappiness;
713 spin_unlock(&memcg->reclaim_param_lock);
714
715 return swappiness;
716}
717
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700718static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
719{
720 int *val = data;
721 (*val)++;
722 return 0;
723}
724/*
725 * This function returns the number of memcg under hierarchy tree. Returns
726 * 1(self count) if no children.
727 */
728static int mem_cgroup_count_children(struct mem_cgroup *mem)
729{
730 int num = 0;
731 mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
732 return num;
733}
734
Balbir Singh6d61ef42009-01-07 18:08:06 -0800735/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700736 * Visit the first child (need not be the first child as per the ordering
737 * of the cgroup list, since we track last_scanned_child) of @mem and use
738 * that to reclaim free pages from.
739 */
740static struct mem_cgroup *
741mem_cgroup_select_victim(struct mem_cgroup *root_mem)
742{
743 struct mem_cgroup *ret = NULL;
744 struct cgroup_subsys_state *css;
745 int nextid, found;
746
747 if (!root_mem->use_hierarchy) {
748 css_get(&root_mem->css);
749 ret = root_mem;
750 }
751
752 while (!ret) {
753 rcu_read_lock();
754 nextid = root_mem->last_scanned_child + 1;
755 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
756 &found);
757 if (css && css_tryget(css))
758 ret = container_of(css, struct mem_cgroup, css);
759
760 rcu_read_unlock();
761 /* Updates scanning parameter */
762 spin_lock(&root_mem->reclaim_param_lock);
763 if (!css) {
764 /* this means start scan from ID:1 */
765 root_mem->last_scanned_child = 0;
766 } else
767 root_mem->last_scanned_child = found;
768 spin_unlock(&root_mem->reclaim_param_lock);
769 }
770
771 return ret;
772}
773
774/*
775 * Scan the hierarchy if needed to reclaim memory. We remember the last child
776 * we reclaimed from, so that we don't end up penalizing one child extensively
777 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800778 *
779 * root_mem is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700780 *
781 * We give up and return to the caller when we visit root_mem twice.
782 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700783 *
784 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800785 */
786static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700787 gfp_t gfp_mask, bool noswap, bool shrink)
Balbir Singh6d61ef42009-01-07 18:08:06 -0800788{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700789 struct mem_cgroup *victim;
790 int ret, total = 0;
791 int loop = 0;
Balbir Singh6d61ef42009-01-07 18:08:06 -0800792
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700793 while (loop < 2) {
794 victim = mem_cgroup_select_victim(root_mem);
795 if (victim == root_mem)
796 loop++;
797 if (!mem_cgroup_local_usage(&victim->stat)) {
798 /* this cgroup's local usage == 0 */
799 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -0800800 continue;
801 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700802 /* we use swappiness of local cgroup */
803 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask, noswap,
804 get_swappiness(victim));
805 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700806 /*
807 * At shrinking usage, we can't check we should stop here or
808 * reclaim more. It's depends on callers. last_scanned_child
809 * will work enough for keeping fairness under tree.
810 */
811 if (shrink)
812 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700813 total += ret;
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -0800814 if (mem_cgroup_check_under_limit(root_mem))
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700815 return 1 + total;
Balbir Singh6d61ef42009-01-07 18:08:06 -0800816 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700817 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -0800818}
819
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800820bool mem_cgroup_oom_called(struct task_struct *task)
821{
822 bool ret = false;
823 struct mem_cgroup *mem;
824 struct mm_struct *mm;
825
826 rcu_read_lock();
827 mm = task->mm;
828 if (!mm)
829 mm = &init_mm;
830 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
831 if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
832 ret = true;
833 rcu_read_unlock();
834 return ret;
835}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700836
837static int record_last_oom_cb(struct mem_cgroup *mem, void *data)
838{
839 mem->last_oom_jiffies = jiffies;
840 return 0;
841}
842
843static void record_last_oom(struct mem_cgroup *mem)
844{
845 mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb);
846}
847
848
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800849/*
850 * Unlike exported interface, "oom" parameter is added. if oom==true,
851 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800852 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800853static int __mem_cgroup_try_charge(struct mm_struct *mm,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800854 gfp_t gfp_mask, struct mem_cgroup **memcg,
855 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800856{
Balbir Singh6d61ef42009-01-07 18:08:06 -0800857 struct mem_cgroup *mem, *mem_over_limit;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800858 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Balbir Singh28dbc4b2009-01-07 18:08:05 -0800859 struct res_counter *fail_res;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800860
861 if (unlikely(test_thread_flag(TIF_MEMDIE))) {
862 /* Don't account this! */
863 *memcg = NULL;
864 return 0;
865 }
866
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800867 /*
Hugh Dickins3be91272008-02-07 00:14:19 -0800868 * We always charge the cgroup the mm_struct belongs to.
869 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800870 * thread group leader migrates. It's possible that mm is not
871 * set, if so charge the init_mm (happens for pagecache usage).
872 */
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800873 mem = *memcg;
874 if (likely(!mem)) {
875 mem = try_get_mem_cgroup_from_mm(mm);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800876 *memcg = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700877 } else {
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800878 css_get(&mem->css);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700879 }
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800880 if (unlikely(!mem))
881 return 0;
882
883 VM_BUG_ON(mem_cgroup_is_obsolete(mem));
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800884
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800885 while (1) {
886 int ret;
887 bool noswap = false;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800888
Balbir Singh28dbc4b2009-01-07 18:08:05 -0800889 ret = res_counter_charge(&mem->res, PAGE_SIZE, &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800890 if (likely(!ret)) {
891 if (!do_swap_account)
892 break;
Balbir Singh28dbc4b2009-01-07 18:08:05 -0800893 ret = res_counter_charge(&mem->memsw, PAGE_SIZE,
894 &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800895 if (likely(!ret))
896 break;
897 /* mem+swap counter fails */
898 res_counter_uncharge(&mem->res, PAGE_SIZE);
899 noswap = true;
Balbir Singh6d61ef42009-01-07 18:08:06 -0800900 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
901 memsw);
902 } else
903 /* mem counter fails */
904 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
905 res);
906
Hugh Dickins3be91272008-02-07 00:14:19 -0800907 if (!(gfp_mask & __GFP_WAIT))
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800908 goto nomem;
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800909
Balbir Singh6d61ef42009-01-07 18:08:06 -0800910 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, gfp_mask,
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700911 noswap, false);
Daisuke Nishimura4d1c6272009-01-15 13:51:14 -0800912 if (ret)
913 continue;
Balbir Singh66e17072008-02-07 00:13:56 -0800914
915 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800916 * try_to_free_mem_cgroup_pages() might not give us a full
917 * picture of reclaim. Some pages are reclaimed and might be
918 * moved to swap cache or just unmapped from the cgroup.
919 * Check the limit again to see if the reclaim reduced the
920 * current usage of the cgroup before giving up
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800921 *
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800922 */
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -0800923 if (mem_cgroup_check_under_limit(mem_over_limit))
924 continue;
Hugh Dickins3be91272008-02-07 00:14:19 -0800925
926 if (!nr_retries--) {
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800927 if (oom) {
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -0800928 mutex_lock(&memcg_tasklist);
KAMEZAWA Hiroyuki88700752009-01-07 18:08:09 -0800929 mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -0800930 mutex_unlock(&memcg_tasklist);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700931 record_last_oom(mem_over_limit);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800932 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800933 goto nomem;
Balbir Singh66e17072008-02-07 00:13:56 -0800934 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800935 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800936 return 0;
937nomem:
938 css_put(&mem->css);
939 return -ENOMEM;
940}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800941
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -0800942static struct mem_cgroup *try_get_mem_cgroup_from_swapcache(struct page *page)
943{
944 struct mem_cgroup *mem;
945 swp_entry_t ent;
946
947 if (!PageSwapCache(page))
948 return NULL;
949
950 ent.val = page_private(page);
951 mem = lookup_swap_cgroup(ent);
952 if (!mem)
953 return NULL;
954 if (!css_tryget(&mem->css))
955 return NULL;
956 return mem;
957}
958
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800959/*
Daisuke Nishimuraa5e924f2009-01-07 18:08:28 -0800960 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800961 * USED state. If already USED, uncharge and return.
962 */
963
964static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
965 struct page_cgroup *pc,
966 enum charge_type ctype)
967{
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800968 /* try_charge() can return NULL to *memcg, taking care of it. */
969 if (!mem)
970 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700971
972 lock_page_cgroup(pc);
973 if (unlikely(PageCgroupUsed(pc))) {
974 unlock_page_cgroup(pc);
975 res_counter_uncharge(&mem->res, PAGE_SIZE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800976 if (do_swap_account)
977 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700978 css_put(&mem->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800979 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700980 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800981 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800982 smp_wmb();
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700983 pc->flags = pcg_default_flags[ctype];
Hugh Dickins3be91272008-02-07 00:14:19 -0800984
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800985 mem_cgroup_charge_statistics(mem, pc, true);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700986
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700987 unlock_page_cgroup(pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800988}
989
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800990/**
991 * mem_cgroup_move_account - move account of the page
992 * @pc: page_cgroup of the page.
993 * @from: mem_cgroup which the page is moved from.
994 * @to: mem_cgroup which the page is moved to. @from != @to.
995 *
996 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800997 * - page is not on LRU (isolate_page() is useful.)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800998 *
999 * returns 0 at success,
1000 * returns -EBUSY when lock is busy or "pc" is unstable.
1001 *
1002 * This function does "uncharge" from old cgroup but doesn't do "charge" to
1003 * new cgroup. It should be done by a caller.
1004 */
1005
1006static int mem_cgroup_move_account(struct page_cgroup *pc,
1007 struct mem_cgroup *from, struct mem_cgroup *to)
1008{
1009 struct mem_cgroup_per_zone *from_mz, *to_mz;
1010 int nid, zid;
1011 int ret = -EBUSY;
1012
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001013 VM_BUG_ON(from == to);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001014 VM_BUG_ON(PageLRU(pc->page));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001015
1016 nid = page_cgroup_nid(pc);
1017 zid = page_cgroup_zid(pc);
1018 from_mz = mem_cgroup_zoneinfo(from, nid, zid);
1019 to_mz = mem_cgroup_zoneinfo(to, nid, zid);
1020
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001021 if (!trylock_page_cgroup(pc))
1022 return ret;
1023
1024 if (!PageCgroupUsed(pc))
1025 goto out;
1026
1027 if (pc->mem_cgroup != from)
1028 goto out;
1029
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001030 res_counter_uncharge(&from->res, PAGE_SIZE);
1031 mem_cgroup_charge_statistics(from, pc, false);
1032 if (do_swap_account)
1033 res_counter_uncharge(&from->memsw, PAGE_SIZE);
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001034 css_put(&from->css);
1035
1036 css_get(&to->css);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001037 pc->mem_cgroup = to;
1038 mem_cgroup_charge_statistics(to, pc, true);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001039 ret = 0;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001040out:
1041 unlock_page_cgroup(pc);
1042 return ret;
1043}
1044
1045/*
1046 * move charges to its parent.
1047 */
1048
1049static int mem_cgroup_move_parent(struct page_cgroup *pc,
1050 struct mem_cgroup *child,
1051 gfp_t gfp_mask)
1052{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001053 struct page *page = pc->page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001054 struct cgroup *cg = child->css.cgroup;
1055 struct cgroup *pcg = cg->parent;
1056 struct mem_cgroup *parent;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001057 int ret;
1058
1059 /* Is ROOT ? */
1060 if (!pcg)
1061 return -EINVAL;
1062
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001063
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001064 parent = mem_cgroup_from_cont(pcg);
1065
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001066
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001067 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001068 if (ret || !parent)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001069 return ret;
1070
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001071 if (!get_page_unless_zero(page)) {
1072 ret = -EBUSY;
1073 goto uncharge;
1074 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001075
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001076 ret = isolate_lru_page(page);
1077
1078 if (ret)
1079 goto cancel;
1080
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001081 ret = mem_cgroup_move_account(pc, child, parent);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001082
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001083 putback_lru_page(page);
1084 if (!ret) {
1085 put_page(page);
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001086 /* drop extra refcnt by try_charge() */
1087 css_put(&parent->css);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001088 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001089 }
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001090
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001091cancel:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001092 put_page(page);
1093uncharge:
1094 /* drop extra refcnt by try_charge() */
1095 css_put(&parent->css);
1096 /* uncharge if move fails */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001097 res_counter_uncharge(&parent->res, PAGE_SIZE);
1098 if (do_swap_account)
1099 res_counter_uncharge(&parent->memsw, PAGE_SIZE);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001100 return ret;
1101}
1102
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001103/*
1104 * Charge the memory controller for page usage.
1105 * Return
1106 * 0 if the charge was successful
1107 * < 0 if the cgroup is over its limit
1108 */
1109static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
1110 gfp_t gfp_mask, enum charge_type ctype,
1111 struct mem_cgroup *memcg)
1112{
1113 struct mem_cgroup *mem;
1114 struct page_cgroup *pc;
1115 int ret;
1116
1117 pc = lookup_page_cgroup(page);
1118 /* can happen at boot */
1119 if (unlikely(!pc))
1120 return 0;
1121 prefetchw(pc);
1122
1123 mem = memcg;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001124 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001125 if (ret || !mem)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001126 return ret;
1127
1128 __mem_cgroup_commit_charge(mem, pc, ctype);
1129 return 0;
1130}
1131
1132int mem_cgroup_newpage_charge(struct page *page,
1133 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001134{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001135 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001136 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001137 if (PageCompound(page))
1138 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001139 /*
1140 * If already mapped, we don't have to account.
1141 * If page cache, page->mapping has address_space.
1142 * But page->mapping may have out-of-use anon_vma pointer,
1143 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
1144 * is NULL.
1145 */
1146 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
1147 return 0;
1148 if (unlikely(!mm))
1149 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001150 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001151 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001152}
1153
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001154int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
1155 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08001156{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001157 struct mem_cgroup *mem = NULL;
1158 int ret;
1159
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001160 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001161 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001162 if (PageCompound(page))
1163 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001164 /*
1165 * Corner case handling. This is called from add_to_page_cache()
1166 * in usual. But some FS (shmem) precharges this page before calling it
1167 * and call add_to_page_cache() with GFP_NOWAIT.
1168 *
1169 * For GFP_NOWAIT case, the page may be pre-charged before calling
1170 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
1171 * charge twice. (It works but has to pay a bit larger cost.)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001172 * And when the page is SwapCache, it should take swap information
1173 * into account. This is under lock_page() now.
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001174 */
1175 if (!(gfp_mask & __GFP_WAIT)) {
1176 struct page_cgroup *pc;
1177
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001178
1179 pc = lookup_page_cgroup(page);
1180 if (!pc)
1181 return 0;
1182 lock_page_cgroup(pc);
1183 if (PageCgroupUsed(pc)) {
1184 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001185 return 0;
1186 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001187 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001188 }
1189
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001190 if (do_swap_account && PageSwapCache(page)) {
1191 mem = try_get_mem_cgroup_from_swapcache(page);
1192 if (mem)
1193 mm = NULL;
1194 else
1195 mem = NULL;
1196 /* SwapCache may be still linked to LRU now. */
1197 mem_cgroup_lru_del_before_commit_swapcache(page);
1198 }
1199
1200 if (unlikely(!mm && !mem))
Balbir Singh8697d332008-02-07 00:13:59 -08001201 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001202
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -07001203 if (page_is_file_cache(page))
1204 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001205 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001206
1207 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
1208 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
1209 if (mem)
1210 css_put(&mem->css);
1211 if (PageSwapCache(page))
1212 mem_cgroup_lru_add_after_commit_swapcache(page);
1213
1214 if (do_swap_account && !ret && PageSwapCache(page)) {
1215 swp_entry_t ent = {.val = page_private(page)};
1216 /* avoid double counting */
1217 mem = swap_cgroup_record(ent, NULL);
1218 if (mem) {
1219 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1220 mem_cgroup_put(mem);
1221 }
1222 }
1223 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001224}
1225
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001226/*
1227 * While swap-in, try_charge -> commit or cancel, the page is locked.
1228 * And when try_charge() successfully returns, one refcnt to memcg without
1229 * struct page_cgroup is aquired. This refcnt will be cumsumed by
1230 * "commit()" or removed by "cancel()"
1231 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001232int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
1233 struct page *page,
1234 gfp_t mask, struct mem_cgroup **ptr)
1235{
1236 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001237 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001238
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001239 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001240 return 0;
1241
1242 if (!do_swap_account)
1243 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001244 /*
1245 * A racing thread's fault, or swapoff, may have already updated
1246 * the pte, and even removed page from swap cache: return success
1247 * to go on to do_swap_page()'s pte_same() test, which should fail.
1248 */
1249 if (!PageSwapCache(page))
1250 return 0;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001251 mem = try_get_mem_cgroup_from_swapcache(page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001252 if (!mem)
1253 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001254 *ptr = mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001255 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true);
1256 /* drop extra refcnt from tryget */
1257 css_put(&mem->css);
1258 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001259charge_cur_mm:
1260 if (unlikely(!mm))
1261 mm = &init_mm;
1262 return __mem_cgroup_try_charge(mm, mask, ptr, true);
1263}
1264
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001265void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
1266{
1267 struct page_cgroup *pc;
1268
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001269 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001270 return;
1271 if (!ptr)
1272 return;
1273 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001274 mem_cgroup_lru_del_before_commit_swapcache(page);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001275 __mem_cgroup_commit_charge(ptr, pc, MEM_CGROUP_CHARGE_TYPE_MAPPED);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001276 mem_cgroup_lru_add_after_commit_swapcache(page);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001277 /*
1278 * Now swap is on-memory. This means this page may be
1279 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08001280 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
1281 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
1282 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001283 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08001284 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001285 swp_entry_t ent = {.val = page_private(page)};
1286 struct mem_cgroup *memcg;
1287 memcg = swap_cgroup_record(ent, NULL);
1288 if (memcg) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001289 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1290 mem_cgroup_put(memcg);
1291 }
1292
1293 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001294 /* add this page(page_cgroup) to the LRU we want. */
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001295
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001296}
1297
1298void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
1299{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001300 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001301 return;
1302 if (!mem)
1303 return;
1304 res_counter_uncharge(&mem->res, PAGE_SIZE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001305 if (do_swap_account)
1306 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001307 css_put(&mem->css);
1308}
1309
1310
Balbir Singh8697d332008-02-07 00:13:59 -08001311/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001312 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001313 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001314static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001315__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001316{
Hugh Dickins82895462008-03-04 14:29:08 -08001317 struct page_cgroup *pc;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001318 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001319 struct mem_cgroup_per_zone *mz;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001320
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001321 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001322 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07001323
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001324 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001325 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001326
Balbir Singh8697d332008-02-07 00:13:59 -08001327 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08001328 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08001329 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001330 pc = lookup_page_cgroup(page);
1331 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001332 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001333
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001334 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001335
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001336 mem = pc->mem_cgroup;
1337
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001338 if (!PageCgroupUsed(pc))
1339 goto unlock_out;
1340
1341 switch (ctype) {
1342 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1343 if (page_mapped(page))
1344 goto unlock_out;
1345 break;
1346 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
1347 if (!PageAnon(page)) { /* Shared memory */
1348 if (page->mapping && !page_is_file_cache(page))
1349 goto unlock_out;
1350 } else if (page_mapped(page)) /* Anon */
1351 goto unlock_out;
1352 break;
1353 default:
1354 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001355 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001356
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001357 res_counter_uncharge(&mem->res, PAGE_SIZE);
1358 if (do_swap_account && (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT))
1359 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001360 mem_cgroup_charge_statistics(mem, pc, false);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001361
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001362 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001363 /*
1364 * pc->mem_cgroup is not cleared here. It will be accessed when it's
1365 * freed from LRU. This is safe because uncharged page is expected not
1366 * to be reused (freed soon). Exception is SwapCache, it's handled by
1367 * special functions.
1368 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08001369
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001370 mz = page_cgroup_zoneinfo(pc);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001371 unlock_page_cgroup(pc);
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08001372
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08001373 /* at swapout, this memcg will be accessed to record to swap */
1374 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1375 css_put(&mem->css);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001376
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001377 return mem;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001378
1379unlock_out:
1380 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001381 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08001382}
1383
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001384void mem_cgroup_uncharge_page(struct page *page)
1385{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001386 /* early check. */
1387 if (page_mapped(page))
1388 return;
1389 if (page->mapping && !PageAnon(page))
1390 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001391 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
1392}
1393
1394void mem_cgroup_uncharge_cache_page(struct page *page)
1395{
1396 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07001397 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001398 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
1399}
1400
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001401/*
1402 * called from __delete_from_swap_cache() and drop "page" account.
1403 * memcg information is recorded to swap_cgroup of "ent"
1404 */
1405void mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001406{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001407 struct mem_cgroup *memcg;
1408
1409 memcg = __mem_cgroup_uncharge_common(page,
1410 MEM_CGROUP_CHARGE_TYPE_SWAPOUT);
1411 /* record memcg information */
1412 if (do_swap_account && memcg) {
1413 swap_cgroup_record(ent, memcg);
1414 mem_cgroup_get(memcg);
1415 }
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08001416 if (memcg)
1417 css_put(&memcg->css);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001418}
1419
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001420#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1421/*
1422 * called from swap_entry_free(). remove record in swap_cgroup and
1423 * uncharge "memsw" account.
1424 */
1425void mem_cgroup_uncharge_swap(swp_entry_t ent)
1426{
1427 struct mem_cgroup *memcg;
1428
1429 if (!do_swap_account)
1430 return;
1431
1432 memcg = swap_cgroup_record(ent, NULL);
1433 if (memcg) {
1434 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1435 mem_cgroup_put(memcg);
1436 }
1437}
1438#endif
1439
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001440/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001441 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
1442 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001443 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001444int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001445{
1446 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001447 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001448 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001449
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001450 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07001451 return 0;
1452
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001453 pc = lookup_page_cgroup(page);
1454 lock_page_cgroup(pc);
1455 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001456 mem = pc->mem_cgroup;
1457 css_get(&mem->css);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08001458 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001459 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001460
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001461 if (mem) {
Daisuke Nishimura3bb4edf2009-01-07 18:08:28 -08001462 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001463 css_put(&mem->css);
1464 }
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001465 *ptr = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001466 return ret;
1467}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08001468
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001469/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001470void mem_cgroup_end_migration(struct mem_cgroup *mem,
1471 struct page *oldpage, struct page *newpage)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001472{
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001473 struct page *target, *unused;
1474 struct page_cgroup *pc;
1475 enum charge_type ctype;
1476
1477 if (!mem)
1478 return;
1479
1480 /* at migration success, oldpage->mapping is NULL. */
1481 if (oldpage->mapping) {
1482 target = oldpage;
1483 unused = NULL;
1484 } else {
1485 target = newpage;
1486 unused = oldpage;
1487 }
1488
1489 if (PageAnon(target))
1490 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
1491 else if (page_is_file_cache(target))
1492 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
1493 else
1494 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
1495
1496 /* unused page is not on radix-tree now. */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001497 if (unused)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001498 __mem_cgroup_uncharge_common(unused, ctype);
1499
1500 pc = lookup_page_cgroup(target);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001501 /*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001502 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
1503 * So, double-counting is effectively avoided.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001504 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001505 __mem_cgroup_commit_charge(mem, pc, ctype);
1506
1507 /*
1508 * Both of oldpage and newpage are still under lock_page().
1509 * Then, we don't have to care about race in radix-tree.
1510 * But we have to be careful that this page is unmapped or not.
1511 *
1512 * There is a case for !page_mapped(). At the start of
1513 * migration, oldpage was mapped. But now, it's zapped.
1514 * But we know *target* page is not freed/reused under us.
1515 * mem_cgroup_uncharge_page() does all necessary checks.
1516 */
1517 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
1518 mem_cgroup_uncharge_page(target);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001519}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001520
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001521/*
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001522 * A call to try to shrink memory usage under specified resource controller.
1523 * This is typically used for page reclaiming for shmem for reducing side
1524 * effect of page allocation from shmem, which is used by some mem_cgroup.
1525 */
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001526int mem_cgroup_shrink_usage(struct page *page,
1527 struct mm_struct *mm,
1528 gfp_t gfp_mask)
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001529{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001530 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001531 int progress = 0;
1532 int retry = MEM_CGROUP_RECLAIM_RETRIES;
1533
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001534 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001535 return 0;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001536 if (page)
1537 mem = try_get_mem_cgroup_from_swapcache(page);
1538 if (!mem && mm)
1539 mem = try_get_mem_cgroup_from_mm(mm);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001540 if (unlikely(!mem))
Balbir Singh31a78f22008-09-28 23:09:31 +01001541 return 0;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001542
1543 do {
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001544 progress = mem_cgroup_hierarchical_reclaim(mem,
1545 gfp_mask, true, false);
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -08001546 progress += mem_cgroup_check_under_limit(mem);
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001547 } while (!progress && --retry);
1548
1549 css_put(&mem->css);
1550 if (!retry)
1551 return -ENOMEM;
1552 return 0;
1553}
1554
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001555static DEFINE_MUTEX(set_limit_mutex);
1556
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08001557static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001558 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001559{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001560 int retry_count;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001561 int progress;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001562 u64 memswlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001563 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001564 int children = mem_cgroup_count_children(memcg);
1565 u64 curusage, oldusage;
1566
1567 /*
1568 * For keeping hierarchical_reclaim simple, how long we should retry
1569 * is depends on callers. We set our retry-count to be function
1570 * of # of children which we should visit in this loop.
1571 */
1572 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
1573
1574 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001575
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001576 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001577 if (signal_pending(current)) {
1578 ret = -EINTR;
1579 break;
1580 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001581 /*
1582 * Rather than hide all in some function, I do this in
1583 * open coded manner. You see what this really does.
1584 * We have to guarantee mem->res.limit < mem->memsw.limit.
1585 */
1586 mutex_lock(&set_limit_mutex);
1587 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1588 if (memswlimit < val) {
1589 ret = -EINVAL;
1590 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001591 break;
1592 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001593 ret = res_counter_set_limit(&memcg->res, val);
1594 mutex_unlock(&set_limit_mutex);
1595
1596 if (!ret)
1597 break;
1598
Daisuke Nishimura42e9abb2009-01-07 18:08:30 -08001599 progress = mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL,
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001600 false, true);
1601 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
1602 /* Usage is reduced ? */
1603 if (curusage >= oldusage)
1604 retry_count--;
1605 else
1606 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001607 }
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001608
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001609 return ret;
1610}
1611
1612int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
1613 unsigned long long val)
1614{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001615 int retry_count;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001616 u64 memlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001617 int children = mem_cgroup_count_children(memcg);
1618 int ret = -EBUSY;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001619
1620 if (!do_swap_account)
1621 return -EINVAL;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001622 /* see mem_cgroup_resize_res_limit */
1623 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
1624 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001625 while (retry_count) {
1626 if (signal_pending(current)) {
1627 ret = -EINTR;
1628 break;
1629 }
1630 /*
1631 * Rather than hide all in some function, I do this in
1632 * open coded manner. You see what this really does.
1633 * We have to guarantee mem->res.limit < mem->memsw.limit.
1634 */
1635 mutex_lock(&set_limit_mutex);
1636 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
1637 if (memlimit > val) {
1638 ret = -EINVAL;
1639 mutex_unlock(&set_limit_mutex);
1640 break;
1641 }
1642 ret = res_counter_set_limit(&memcg->memsw, val);
1643 mutex_unlock(&set_limit_mutex);
1644
1645 if (!ret)
1646 break;
1647
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001648 mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL, true, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001649 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001650 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001651 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001652 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001653 else
1654 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001655 }
1656 return ret;
1657}
1658
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001659/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001660 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001661 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
1662 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001663static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001664 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001665{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001666 struct zone *zone;
1667 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001668 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001669 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001670 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001671 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001672
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001673 zone = &NODE_DATA(node)->node_zones[zid];
1674 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001675 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001676
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001677 loop = MEM_CGROUP_ZSTAT(mz, lru);
1678 /* give some margin against EBUSY etc...*/
1679 loop += 256;
1680 busy = NULL;
1681 while (loop--) {
1682 ret = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001683 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001684 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001685 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001686 break;
1687 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001688 pc = list_entry(list->prev, struct page_cgroup, lru);
1689 if (busy == pc) {
1690 list_move(&pc->lru, list);
1691 busy = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001692 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001693 continue;
1694 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001695 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001696
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -08001697 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001698 if (ret == -ENOMEM)
1699 break;
1700
1701 if (ret == -EBUSY || ret == -EINVAL) {
1702 /* found lock contention or "pc" is obsolete. */
1703 busy = pc;
1704 cond_resched();
1705 } else
1706 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001707 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001708
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001709 if (!ret && !list_empty(list))
1710 return -EBUSY;
1711 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001712}
1713
1714/*
1715 * make mem_cgroup's charge to be 0 if there is no task.
1716 * This enables deleting this mem_cgroup.
1717 */
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001718static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001719{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001720 int ret;
1721 int node, zid, shrink;
1722 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001723 struct cgroup *cgrp = mem->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001724
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001725 css_get(&mem->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001726
1727 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001728 /* should free all ? */
1729 if (free_all)
1730 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001731move_account:
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001732 while (mem->res.usage > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001733 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001734 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001735 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001736 ret = -EINTR;
1737 if (signal_pending(current))
1738 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001739 /* This is for making all *used* pages to be on LRU. */
1740 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001741 ret = 0;
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08001742 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001743 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07001744 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001745 for_each_lru(l) {
1746 ret = mem_cgroup_force_empty_list(mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001747 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001748 if (ret)
1749 break;
1750 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001751 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001752 if (ret)
1753 break;
1754 }
1755 /* it seems parent cgroup doesn't have enough mem */
1756 if (ret == -ENOMEM)
1757 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001758 cond_resched();
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001759 }
1760 ret = 0;
1761out:
1762 css_put(&mem->css);
1763 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001764
1765try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001766 /* returns EBUSY if there is a task or if we come here twice. */
1767 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001768 ret = -EBUSY;
1769 goto out;
1770 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001771 /* we call try-to-free pages for make this cgroup empty */
1772 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001773 /* try to free all pages in this cgroup */
1774 shrink = 1;
1775 while (nr_retries && mem->res.usage > 0) {
1776 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001777
1778 if (signal_pending(current)) {
1779 ret = -EINTR;
1780 goto out;
1781 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001782 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
1783 false, get_swappiness(mem));
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001784 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001785 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001786 /* maybe some writeback is necessary */
1787 congestion_wait(WRITE, HZ/10);
1788 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001789
1790 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001791 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001792 /* try move_account...there may be some *locked* pages. */
1793 if (mem->res.usage)
1794 goto move_account;
1795 ret = 0;
1796 goto out;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001797}
1798
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001799int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
1800{
1801 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
1802}
1803
1804
Balbir Singh18f59ea2009-01-07 18:08:07 -08001805static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
1806{
1807 return mem_cgroup_from_cont(cont)->use_hierarchy;
1808}
1809
1810static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
1811 u64 val)
1812{
1813 int retval = 0;
1814 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1815 struct cgroup *parent = cont->parent;
1816 struct mem_cgroup *parent_mem = NULL;
1817
1818 if (parent)
1819 parent_mem = mem_cgroup_from_cont(parent);
1820
1821 cgroup_lock();
1822 /*
1823 * If parent's use_hiearchy is set, we can't make any modifications
1824 * in the child subtrees. If it is unset, then the change can
1825 * occur, provided the current cgroup has no children.
1826 *
1827 * For the root cgroup, parent_mem is NULL, we allow value to be
1828 * set if there are no children.
1829 */
1830 if ((!parent_mem || !parent_mem->use_hierarchy) &&
1831 (val == 1 || val == 0)) {
1832 if (list_empty(&cont->children))
1833 mem->use_hierarchy = val;
1834 else
1835 retval = -EBUSY;
1836 } else
1837 retval = -EINVAL;
1838 cgroup_unlock();
1839
1840 return retval;
1841}
1842
Paul Menage2c3daa72008-04-29 00:59:58 -07001843static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001844{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001845 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1846 u64 val = 0;
1847 int type, name;
1848
1849 type = MEMFILE_TYPE(cft->private);
1850 name = MEMFILE_ATTR(cft->private);
1851 switch (type) {
1852 case _MEM:
1853 val = res_counter_read_u64(&mem->res, name);
1854 break;
1855 case _MEMSWAP:
1856 if (do_swap_account)
1857 val = res_counter_read_u64(&mem->memsw, name);
1858 break;
1859 default:
1860 BUG();
1861 break;
1862 }
1863 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001864}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001865/*
1866 * The user of this function is...
1867 * RES_LIMIT.
1868 */
Paul Menage856c13a2008-07-25 01:47:04 -07001869static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
1870 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001871{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001872 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001873 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001874 unsigned long long val;
1875 int ret;
1876
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001877 type = MEMFILE_TYPE(cft->private);
1878 name = MEMFILE_ATTR(cft->private);
1879 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001880 case RES_LIMIT:
1881 /* This function does all necessary parse...reuse it */
1882 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001883 if (ret)
1884 break;
1885 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001886 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001887 else
1888 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001889 break;
1890 default:
1891 ret = -EINVAL; /* should be BUG() ? */
1892 break;
1893 }
1894 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001895}
1896
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08001897static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
1898 unsigned long long *mem_limit, unsigned long long *memsw_limit)
1899{
1900 struct cgroup *cgroup;
1901 unsigned long long min_limit, min_memsw_limit, tmp;
1902
1903 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
1904 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1905 cgroup = memcg->css.cgroup;
1906 if (!memcg->use_hierarchy)
1907 goto out;
1908
1909 while (cgroup->parent) {
1910 cgroup = cgroup->parent;
1911 memcg = mem_cgroup_from_cont(cgroup);
1912 if (!memcg->use_hierarchy)
1913 break;
1914 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
1915 min_limit = min(min_limit, tmp);
1916 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1917 min_memsw_limit = min(min_memsw_limit, tmp);
1918 }
1919out:
1920 *mem_limit = min_limit;
1921 *memsw_limit = min_memsw_limit;
1922 return;
1923}
1924
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001925static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001926{
1927 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001928 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001929
1930 mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001931 type = MEMFILE_TYPE(event);
1932 name = MEMFILE_ATTR(event);
1933 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001934 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001935 if (type == _MEM)
1936 res_counter_reset_max(&mem->res);
1937 else
1938 res_counter_reset_max(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001939 break;
1940 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001941 if (type == _MEM)
1942 res_counter_reset_failcnt(&mem->res);
1943 else
1944 res_counter_reset_failcnt(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001945 break;
1946 }
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07001947 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001948}
1949
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07001950
1951/* For read statistics */
1952enum {
1953 MCS_CACHE,
1954 MCS_RSS,
1955 MCS_PGPGIN,
1956 MCS_PGPGOUT,
1957 MCS_INACTIVE_ANON,
1958 MCS_ACTIVE_ANON,
1959 MCS_INACTIVE_FILE,
1960 MCS_ACTIVE_FILE,
1961 MCS_UNEVICTABLE,
1962 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001963};
1964
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07001965struct mcs_total_stat {
1966 s64 stat[NR_MCS_STAT];
1967};
1968
1969struct {
1970 char *local_name;
1971 char *total_name;
1972} memcg_stat_strings[NR_MCS_STAT] = {
1973 {"cache", "total_cache"},
1974 {"rss", "total_rss"},
1975 {"pgpgin", "total_pgpgin"},
1976 {"pgpgout", "total_pgpgout"},
1977 {"inactive_anon", "total_inactive_anon"},
1978 {"active_anon", "total_active_anon"},
1979 {"inactive_file", "total_inactive_file"},
1980 {"active_file", "total_active_file"},
1981 {"unevictable", "total_unevictable"}
1982};
1983
1984
1985static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
1986{
1987 struct mcs_total_stat *s = data;
1988 s64 val;
1989
1990 /* per cpu stat */
1991 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE);
1992 s->stat[MCS_CACHE] += val * PAGE_SIZE;
1993 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
1994 s->stat[MCS_RSS] += val * PAGE_SIZE;
1995 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT);
1996 s->stat[MCS_PGPGIN] += val;
1997 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT);
1998 s->stat[MCS_PGPGOUT] += val;
1999
2000 /* per zone stat */
2001 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
2002 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
2003 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
2004 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
2005 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
2006 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
2007 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
2008 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
2009 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
2010 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
2011 return 0;
2012}
2013
2014static void
2015mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
2016{
2017 mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
2018}
2019
Paul Menagec64745c2008-04-29 01:00:02 -07002020static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
2021 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002022{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002023 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002024 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002025 int i;
2026
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002027 memset(&mystat, 0, sizeof(mystat));
2028 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002029
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002030 for (i = 0; i < NR_MCS_STAT; i++)
2031 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002032
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002033 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08002034 {
2035 unsigned long long limit, memsw_limit;
2036 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
2037 cb->fill(cb, "hierarchical_memory_limit", limit);
2038 if (do_swap_account)
2039 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
2040 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002041
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002042 memset(&mystat, 0, sizeof(mystat));
2043 mem_cgroup_get_total_stat(mem_cont, &mystat);
2044 for (i = 0; i < NR_MCS_STAT; i++)
2045 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
2046
2047
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002048#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08002049 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002050
2051 {
2052 int nid, zid;
2053 struct mem_cgroup_per_zone *mz;
2054 unsigned long recent_rotated[2] = {0, 0};
2055 unsigned long recent_scanned[2] = {0, 0};
2056
2057 for_each_online_node(nid)
2058 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
2059 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
2060
2061 recent_rotated[0] +=
2062 mz->reclaim_stat.recent_rotated[0];
2063 recent_rotated[1] +=
2064 mz->reclaim_stat.recent_rotated[1];
2065 recent_scanned[0] +=
2066 mz->reclaim_stat.recent_scanned[0];
2067 recent_scanned[1] +=
2068 mz->reclaim_stat.recent_scanned[1];
2069 }
2070 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
2071 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
2072 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
2073 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
2074 }
2075#endif
2076
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002077 return 0;
2078}
2079
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002080static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
2081{
2082 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2083
2084 return get_swappiness(memcg);
2085}
2086
2087static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
2088 u64 val)
2089{
2090 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2091 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08002092
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002093 if (val > 100)
2094 return -EINVAL;
2095
2096 if (cgrp->parent == NULL)
2097 return -EINVAL;
2098
2099 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08002100
2101 cgroup_lock();
2102
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002103 /* If under hierarchy, only empty-root can set this value */
2104 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08002105 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
2106 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002107 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08002108 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002109
2110 spin_lock(&memcg->reclaim_param_lock);
2111 memcg->swappiness = val;
2112 spin_unlock(&memcg->reclaim_param_lock);
2113
Li Zefan068b38c2009-01-15 13:51:26 -08002114 cgroup_unlock();
2115
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002116 return 0;
2117}
2118
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002119
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002120static struct cftype mem_cgroup_files[] = {
2121 {
Balbir Singh0eea1032008-02-07 00:13:57 -08002122 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002123 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07002124 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002125 },
2126 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002127 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002128 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002129 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002130 .read_u64 = mem_cgroup_read,
2131 },
2132 {
Balbir Singh0eea1032008-02-07 00:13:57 -08002133 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002134 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07002135 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07002136 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002137 },
2138 {
2139 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002140 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002141 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07002142 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002143 },
Balbir Singh8697d332008-02-07 00:13:59 -08002144 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002145 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07002146 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002147 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002148 {
2149 .name = "force_empty",
2150 .trigger = mem_cgroup_force_empty_write,
2151 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08002152 {
2153 .name = "use_hierarchy",
2154 .write_u64 = mem_cgroup_hierarchy_write,
2155 .read_u64 = mem_cgroup_hierarchy_read,
2156 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002157 {
2158 .name = "swappiness",
2159 .read_u64 = mem_cgroup_swappiness_read,
2160 .write_u64 = mem_cgroup_swappiness_write,
2161 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002162};
2163
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002164#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2165static struct cftype memsw_cgroup_files[] = {
2166 {
2167 .name = "memsw.usage_in_bytes",
2168 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
2169 .read_u64 = mem_cgroup_read,
2170 },
2171 {
2172 .name = "memsw.max_usage_in_bytes",
2173 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
2174 .trigger = mem_cgroup_reset,
2175 .read_u64 = mem_cgroup_read,
2176 },
2177 {
2178 .name = "memsw.limit_in_bytes",
2179 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
2180 .write_string = mem_cgroup_write,
2181 .read_u64 = mem_cgroup_read,
2182 },
2183 {
2184 .name = "memsw.failcnt",
2185 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
2186 .trigger = mem_cgroup_reset,
2187 .read_u64 = mem_cgroup_read,
2188 },
2189};
2190
2191static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2192{
2193 if (!do_swap_account)
2194 return 0;
2195 return cgroup_add_files(cont, ss, memsw_cgroup_files,
2196 ARRAY_SIZE(memsw_cgroup_files));
2197};
2198#else
2199static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2200{
2201 return 0;
2202}
2203#endif
2204
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002205static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2206{
2207 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002208 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07002209 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07002210 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002211 /*
2212 * This routine is called against possible nodes.
2213 * But it's BUG to call kmalloc() against offline node.
2214 *
2215 * TODO: this routine can waste much memory for nodes which will
2216 * never be onlined. It's better to use memory hotplug callback
2217 * function.
2218 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07002219 if (!node_state(node, N_NORMAL_MEMORY))
2220 tmp = -1;
2221 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002222 if (!pn)
2223 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002224
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002225 mem->info.nodeinfo[node] = pn;
2226 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002227
2228 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
2229 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07002230 for_each_lru(l)
2231 INIT_LIST_HEAD(&mz->lists[l]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002232 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002233 return 0;
2234}
2235
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002236static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2237{
2238 kfree(mem->info.nodeinfo[node]);
2239}
2240
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002241static int mem_cgroup_size(void)
2242{
2243 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
2244 return sizeof(struct mem_cgroup) + cpustat_size;
2245}
2246
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002247static struct mem_cgroup *mem_cgroup_alloc(void)
2248{
2249 struct mem_cgroup *mem;
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002250 int size = mem_cgroup_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002251
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002252 if (size < PAGE_SIZE)
2253 mem = kmalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002254 else
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002255 mem = vmalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002256
2257 if (mem)
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002258 memset(mem, 0, size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002259 return mem;
2260}
2261
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002262/*
2263 * At destroying mem_cgroup, references from swap_cgroup can remain.
2264 * (scanning all at force_empty is too costly...)
2265 *
2266 * Instead of clearing all references at force_empty, we remember
2267 * the number of reference from swap_cgroup and free mem_cgroup when
2268 * it goes down to 0.
2269 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002270 * Removal of cgroup itself succeeds regardless of refs from swap.
2271 */
2272
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08002273static void __mem_cgroup_free(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002274{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002275 int node;
2276
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002277 free_css_id(&mem_cgroup_subsys, &mem->css);
2278
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002279 for_each_node_state(node, N_POSSIBLE)
2280 free_mem_cgroup_per_zone_info(mem, node);
2281
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002282 if (mem_cgroup_size() < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002283 kfree(mem);
2284 else
2285 vfree(mem);
2286}
2287
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002288static void mem_cgroup_get(struct mem_cgroup *mem)
2289{
2290 atomic_inc(&mem->refcnt);
2291}
2292
2293static void mem_cgroup_put(struct mem_cgroup *mem)
2294{
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08002295 if (atomic_dec_and_test(&mem->refcnt)) {
2296 struct mem_cgroup *parent = parent_mem_cgroup(mem);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08002297 __mem_cgroup_free(mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08002298 if (parent)
2299 mem_cgroup_put(parent);
2300 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002301}
2302
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08002303/*
2304 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
2305 */
2306static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
2307{
2308 if (!mem->res.parent)
2309 return NULL;
2310 return mem_cgroup_from_res_counter(mem->res.parent, res);
2311}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002312
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08002313#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2314static void __init enable_swap_cgroup(void)
2315{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002316 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08002317 do_swap_account = 1;
2318}
2319#else
2320static void __init enable_swap_cgroup(void)
2321{
2322}
2323#endif
2324
Li Zefan0eb253e2009-01-15 13:51:25 -08002325static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002326mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
2327{
Balbir Singh28dbc4b2009-01-07 18:08:05 -08002328 struct mem_cgroup *mem, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002329 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002330 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002331
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002332 mem = mem_cgroup_alloc();
2333 if (!mem)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002334 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08002335
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002336 for_each_node_state(node, N_POSSIBLE)
2337 if (alloc_mem_cgroup_per_zone_info(mem, node))
2338 goto free_out;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08002339 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08002340 if (cont->parent == NULL) {
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08002341 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08002342 parent = NULL;
Balbir Singh18f59ea2009-01-07 18:08:07 -08002343 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08002344 parent = mem_cgroup_from_cont(cont->parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08002345 mem->use_hierarchy = parent->use_hierarchy;
2346 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08002347
Balbir Singh18f59ea2009-01-07 18:08:07 -08002348 if (parent && parent->use_hierarchy) {
2349 res_counter_init(&mem->res, &parent->res);
2350 res_counter_init(&mem->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08002351 /*
2352 * We increment refcnt of the parent to ensure that we can
2353 * safely access it on res_counter_charge/uncharge.
2354 * This refcnt will be decremented when freeing this
2355 * mem_cgroup(see mem_cgroup_put).
2356 */
2357 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08002358 } else {
2359 res_counter_init(&mem->res, NULL);
2360 res_counter_init(&mem->memsw, NULL);
2361 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002362 mem->last_scanned_child = 0;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -08002363 spin_lock_init(&mem->reclaim_param_lock);
Balbir Singh6d61ef42009-01-07 18:08:06 -08002364
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002365 if (parent)
2366 mem->swappiness = get_swappiness(parent);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08002367 atomic_set(&mem->refcnt, 1);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002368 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002369free_out:
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08002370 __mem_cgroup_free(mem);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002371 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002372}
2373
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002374static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08002375 struct cgroup *cont)
2376{
2377 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002378
2379 return mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08002380}
2381
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002382static void mem_cgroup_destroy(struct cgroup_subsys *ss,
2383 struct cgroup *cont)
2384{
Daisuke Nishimurac268e992009-01-15 13:51:13 -08002385 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08002386
Daisuke Nishimurac268e992009-01-15 13:51:13 -08002387 mem_cgroup_put(mem);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002388}
2389
2390static int mem_cgroup_populate(struct cgroup_subsys *ss,
2391 struct cgroup *cont)
2392{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002393 int ret;
2394
2395 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
2396 ARRAY_SIZE(mem_cgroup_files));
2397
2398 if (!ret)
2399 ret = register_memsw_files(cont, ss);
2400 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002401}
2402
Balbir Singh67e465a2008-02-07 00:13:54 -08002403static void mem_cgroup_move_task(struct cgroup_subsys *ss,
2404 struct cgroup *cont,
2405 struct cgroup *old_cont,
2406 struct task_struct *p)
2407{
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08002408 mutex_lock(&memcg_tasklist);
Balbir Singh67e465a2008-02-07 00:13:54 -08002409 /*
Nikanth Karthikesanf9717d22009-01-07 18:08:11 -08002410 * FIXME: It's better to move charges of this process from old
2411 * memcg to new memcg. But it's just on TODO-List now.
Balbir Singh67e465a2008-02-07 00:13:54 -08002412 */
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08002413 mutex_unlock(&memcg_tasklist);
Balbir Singh67e465a2008-02-07 00:13:54 -08002414}
2415
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002416struct cgroup_subsys mem_cgroup_subsys = {
2417 .name = "memory",
2418 .subsys_id = mem_cgroup_subsys_id,
2419 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08002420 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002421 .destroy = mem_cgroup_destroy,
2422 .populate = mem_cgroup_populate,
Balbir Singh67e465a2008-02-07 00:13:54 -08002423 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002424 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002425 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002426};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08002427
2428#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2429
2430static int __init disable_swap_account(char *s)
2431{
2432 really_do_swap_account = 0;
2433 return 1;
2434}
2435__setup("noswapaccount", disable_swap_account);
2436#endif