blob: 66035bf926120cff9c33b0fdcf8815dde0da2961 [file] [log] [blame]
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001/* memcontrol.c - Memory Controller
2 *
3 * Copyright IBM Corporation, 2007
4 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
5 *
Pavel Emelianov78fb7462008-02-07 00:13:51 -08006 * Copyright 2007 OpenVZ SWsoft Inc
7 * Author: Pavel Emelianov <xemul@openvz.org>
8 *
Balbir Singh8cdea7c2008-02-07 00:13:50 -08009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 */
19
20#include <linux/res_counter.h>
21#include <linux/memcontrol.h>
22#include <linux/cgroup.h>
Pavel Emelianov78fb7462008-02-07 00:13:51 -080023#include <linux/mm.h>
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -080024#include <linux/pagemap.h>
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080025#include <linux/smp.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080026#include <linux/page-flags.h>
Balbir Singh66e17072008-02-07 00:13:56 -080027#include <linux/backing-dev.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080028#include <linux/bit_spinlock.h>
29#include <linux/rcupdate.h>
Balbir Singhe2224322009-04-02 16:57:39 -070030#include <linux/limits.h>
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -080031#include <linux/mutex.h>
Balbir Singhf64c3f52009-09-23 15:56:37 -070032#include <linux/rbtree.h>
Balbir Singhb6ac57d2008-04-29 01:00:19 -070033#include <linux/slab.h>
Balbir Singh66e17072008-02-07 00:13:56 -080034#include <linux/swap.h>
35#include <linux/spinlock.h>
36#include <linux/fs.h>
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -080037#include <linux/seq_file.h>
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -070038#include <linux/vmalloc.h>
Christoph Lameterb69408e2008-10-18 20:26:14 -070039#include <linux/mm_inline.h>
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070040#include <linux/page_cgroup.h>
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -080041#include "internal.h"
Balbir Singh8cdea7c2008-02-07 00:13:50 -080042
Balbir Singh8697d332008-02-07 00:13:59 -080043#include <asm/uaccess.h>
44
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070045struct cgroup_subsys mem_cgroup_subsys __read_mostly;
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070046#define MEM_CGROUP_RECLAIM_RETRIES 5
Balbir Singh4b3bde42009-09-23 15:56:32 -070047struct mem_cgroup *root_mem_cgroup __read_mostly;
Balbir Singh8cdea7c2008-02-07 00:13:50 -080048
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080049#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
Li Zefan338c8432009-06-17 16:27:15 -070050/* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080051int do_swap_account __read_mostly;
52static int really_do_swap_account __initdata = 1; /* for remember boot option*/
53#else
54#define do_swap_account (0)
55#endif
56
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -080057static DEFINE_MUTEX(memcg_tasklist); /* can be hold under cgroup_mutex */
Balbir Singhf64c3f52009-09-23 15:56:37 -070058#define SOFTLIMIT_EVENTS_THRESH (1000)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080059
Balbir Singh8cdea7c2008-02-07 00:13:50 -080060/*
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080061 * Statistics for memory cgroup.
62 */
63enum mem_cgroup_stat_index {
64 /*
65 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
66 */
67 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
Balbir Singhd69b0422009-06-17 16:26:34 -070068 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
69 MEM_CGROUP_STAT_MAPPED_FILE, /* # of pages charged as file rss */
Balaji Rao55e462b2008-05-01 04:35:12 -070070 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
71 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
Balbir Singhf64c3f52009-09-23 15:56:37 -070072 MEM_CGROUP_STAT_EVENTS, /* sum of pagein + pageout for internal use */
Balbir Singh0c3e73e2009-09-23 15:56:42 -070073 MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080074
75 MEM_CGROUP_STAT_NSTATS,
76};
77
78struct mem_cgroup_stat_cpu {
79 s64 count[MEM_CGROUP_STAT_NSTATS];
80} ____cacheline_aligned_in_smp;
81
82struct mem_cgroup_stat {
Jan Blunckc8dad2b2009-01-07 18:07:53 -080083 struct mem_cgroup_stat_cpu cpustat[0];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080084};
85
Balbir Singhf64c3f52009-09-23 15:56:37 -070086static inline void
87__mem_cgroup_stat_reset_safe(struct mem_cgroup_stat_cpu *stat,
88 enum mem_cgroup_stat_index idx)
89{
90 stat->count[idx] = 0;
91}
92
93static inline s64
94__mem_cgroup_stat_read_local(struct mem_cgroup_stat_cpu *stat,
95 enum mem_cgroup_stat_index idx)
96{
97 return stat->count[idx];
98}
99
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800100/*
101 * For accounting under irq disable, no need for increment preempt count.
102 */
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700103static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800104 enum mem_cgroup_stat_index idx, int val)
105{
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700106 stat->count[idx] += val;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800107}
108
109static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
110 enum mem_cgroup_stat_index idx)
111{
112 int cpu;
113 s64 ret = 0;
114 for_each_possible_cpu(cpu)
115 ret += stat->cpustat[cpu].count[idx];
116 return ret;
117}
118
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700119static s64 mem_cgroup_local_usage(struct mem_cgroup_stat *stat)
120{
121 s64 ret;
122
123 ret = mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_CACHE);
124 ret += mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_RSS);
125 return ret;
126}
127
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800128/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800129 * per-zone information in memory controller.
130 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800131struct mem_cgroup_per_zone {
KAMEZAWA Hiroyuki072c56c2008-02-07 00:14:39 -0800132 /*
133 * spin_lock to protect the per cgroup LRU
134 */
Christoph Lameterb69408e2008-10-18 20:26:14 -0700135 struct list_head lists[NR_LRU_LISTS];
136 unsigned long count[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800137
138 struct zone_reclaim_stat reclaim_stat;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700139 struct rb_node tree_node; /* RB tree node */
140 unsigned long long usage_in_excess;/* Set to the value by which */
141 /* the soft limit is exceeded*/
142 bool on_tree;
Balbir Singh4e416952009-09-23 15:56:39 -0700143 struct mem_cgroup *mem; /* Back pointer, we cannot */
144 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800145};
146/* Macro for accessing counter */
147#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
148
149struct mem_cgroup_per_node {
150 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
151};
152
153struct mem_cgroup_lru_info {
154 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
155};
156
157/*
Balbir Singhf64c3f52009-09-23 15:56:37 -0700158 * Cgroups above their limits are maintained in a RB-Tree, independent of
159 * their hierarchy representation
160 */
161
162struct mem_cgroup_tree_per_zone {
163 struct rb_root rb_root;
164 spinlock_t lock;
165};
166
167struct mem_cgroup_tree_per_node {
168 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
169};
170
171struct mem_cgroup_tree {
172 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
173};
174
175static struct mem_cgroup_tree soft_limit_tree __read_mostly;
176
177/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800178 * The memory controller data structure. The memory controller controls both
179 * page cache and RSS per cgroup. We would eventually like to provide
180 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
181 * to help the administrator determine what knobs to tune.
182 *
183 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800184 * we hit the water mark. May be even add a low water mark, such that
185 * no reclaim occurs from a cgroup at it's low water mark, this is
186 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800187 */
188struct mem_cgroup {
189 struct cgroup_subsys_state css;
190 /*
191 * the counter to account for memory usage
192 */
193 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800194 /*
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800195 * the counter to account for mem+swap usage.
196 */
197 struct res_counter memsw;
198 /*
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800199 * Per cgroup active and inactive list, similar to the
200 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800201 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800202 struct mem_cgroup_lru_info info;
KAMEZAWA Hiroyuki072c56c2008-02-07 00:14:39 -0800203
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800204 /*
205 protect against reclaim related member.
206 */
207 spinlock_t reclaim_param_lock;
208
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800209 int prev_priority; /* for recording reclaim priority */
Balbir Singh6d61ef42009-01-07 18:08:06 -0800210
211 /*
212 * While reclaiming in a hiearchy, we cache the last child we
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700213 * reclaimed from.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800214 */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700215 int last_scanned_child;
Balbir Singh18f59ea2009-01-07 18:08:07 -0800216 /*
217 * Should the accounting and control be hierarchical, per subtree?
218 */
219 bool use_hierarchy;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800220 unsigned long last_oom_jiffies;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800221 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800222
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800223 unsigned int swappiness;
224
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700225 /* set when res.limit == memsw.limit */
226 bool memsw_is_minimum;
227
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800228 /*
Jan Blunckc8dad2b2009-01-07 18:07:53 -0800229 * statistics. This must be placed at the end of memcg.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800230 */
231 struct mem_cgroup_stat stat;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800232};
233
Balbir Singh4e416952009-09-23 15:56:39 -0700234/*
235 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
236 * limit reclaim to prevent infinite loops, if they ever occur.
237 */
238#define MEM_CGROUP_MAX_RECLAIM_LOOPS (100)
239#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2)
240
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800241enum charge_type {
242 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
243 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700244 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700245 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800246 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700247 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700248 NR_CHARGE_TYPE,
249};
250
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700251/* only for here (for easy reading.) */
252#define PCGF_CACHE (1UL << PCG_CACHE)
253#define PCGF_USED (1UL << PCG_USED)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700254#define PCGF_LOCK (1UL << PCG_LOCK)
Balbir Singh4b3bde42009-09-23 15:56:32 -0700255/* Not used, but added here for completeness */
256#define PCGF_ACCT (1UL << PCG_ACCT)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800257
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800258/* for encoding cft->private value on file */
259#define _MEM (0)
260#define _MEMSWAP (1)
261#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
262#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
263#define MEMFILE_ATTR(val) ((val) & 0xffff)
264
Balbir Singh75822b42009-09-23 15:56:38 -0700265/*
266 * Reclaim flags for mem_cgroup_hierarchical_reclaim
267 */
268#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
269#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
270#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
271#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
Balbir Singh4e416952009-09-23 15:56:39 -0700272#define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
273#define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
Balbir Singh75822b42009-09-23 15:56:38 -0700274
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800275static void mem_cgroup_get(struct mem_cgroup *mem);
276static void mem_cgroup_put(struct mem_cgroup *mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -0800277static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800278
Balbir Singhf64c3f52009-09-23 15:56:37 -0700279static struct mem_cgroup_per_zone *
280mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
281{
282 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
283}
284
285static struct mem_cgroup_per_zone *
286page_cgroup_zoneinfo(struct page_cgroup *pc)
287{
288 struct mem_cgroup *mem = pc->mem_cgroup;
289 int nid = page_cgroup_nid(pc);
290 int zid = page_cgroup_zid(pc);
291
292 if (!mem)
293 return NULL;
294
295 return mem_cgroup_zoneinfo(mem, nid, zid);
296}
297
298static struct mem_cgroup_tree_per_zone *
299soft_limit_tree_node_zone(int nid, int zid)
300{
301 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
302}
303
304static struct mem_cgroup_tree_per_zone *
305soft_limit_tree_from_page(struct page *page)
306{
307 int nid = page_to_nid(page);
308 int zid = page_zonenum(page);
309
310 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
311}
312
313static void
Balbir Singh4e416952009-09-23 15:56:39 -0700314__mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700315 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700316 struct mem_cgroup_tree_per_zone *mctz,
317 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700318{
319 struct rb_node **p = &mctz->rb_root.rb_node;
320 struct rb_node *parent = NULL;
321 struct mem_cgroup_per_zone *mz_node;
322
323 if (mz->on_tree)
324 return;
325
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700326 mz->usage_in_excess = new_usage_in_excess;
327 if (!mz->usage_in_excess)
328 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700329 while (*p) {
330 parent = *p;
331 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
332 tree_node);
333 if (mz->usage_in_excess < mz_node->usage_in_excess)
334 p = &(*p)->rb_left;
335 /*
336 * We can't avoid mem cgroups that are over their soft
337 * limit by the same amount
338 */
339 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
340 p = &(*p)->rb_right;
341 }
342 rb_link_node(&mz->tree_node, parent, p);
343 rb_insert_color(&mz->tree_node, &mctz->rb_root);
344 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700345}
346
347static void
348__mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
349 struct mem_cgroup_per_zone *mz,
350 struct mem_cgroup_tree_per_zone *mctz)
351{
352 if (!mz->on_tree)
353 return;
354 rb_erase(&mz->tree_node, &mctz->rb_root);
355 mz->on_tree = false;
356}
357
358static void
Balbir Singhf64c3f52009-09-23 15:56:37 -0700359mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
360 struct mem_cgroup_per_zone *mz,
361 struct mem_cgroup_tree_per_zone *mctz)
362{
363 spin_lock(&mctz->lock);
Balbir Singh4e416952009-09-23 15:56:39 -0700364 __mem_cgroup_remove_exceeded(mem, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700365 spin_unlock(&mctz->lock);
366}
367
368static bool mem_cgroup_soft_limit_check(struct mem_cgroup *mem)
369{
370 bool ret = false;
371 int cpu;
372 s64 val;
373 struct mem_cgroup_stat_cpu *cpustat;
374
375 cpu = get_cpu();
376 cpustat = &mem->stat.cpustat[cpu];
377 val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_EVENTS);
378 if (unlikely(val > SOFTLIMIT_EVENTS_THRESH)) {
379 __mem_cgroup_stat_reset_safe(cpustat, MEM_CGROUP_STAT_EVENTS);
380 ret = true;
381 }
382 put_cpu();
383 return ret;
384}
385
386static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
387{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700388 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700389 struct mem_cgroup_per_zone *mz;
390 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700391 int nid = page_to_nid(page);
392 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700393 mctz = soft_limit_tree_from_page(page);
394
395 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700396 * Necessary to update all ancestors when hierarchy is used.
397 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700398 */
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700399 for (; mem; mem = parent_mem_cgroup(mem)) {
400 mz = mem_cgroup_zoneinfo(mem, nid, zid);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700401 excess = res_counter_soft_limit_excess(&mem->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700402 /*
403 * We have to update the tree if mz is on RB-tree or
404 * mem is over its softlimit.
405 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700406 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700407 spin_lock(&mctz->lock);
408 /* if on-tree, remove it */
409 if (mz->on_tree)
410 __mem_cgroup_remove_exceeded(mem, mz, mctz);
411 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700412 * Insert again. mz->usage_in_excess will be updated.
413 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700414 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700415 __mem_cgroup_insert_exceeded(mem, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700416 spin_unlock(&mctz->lock);
417 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700418 }
419}
420
421static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
422{
423 int node, zone;
424 struct mem_cgroup_per_zone *mz;
425 struct mem_cgroup_tree_per_zone *mctz;
426
427 for_each_node_state(node, N_POSSIBLE) {
428 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
429 mz = mem_cgroup_zoneinfo(mem, node, zone);
430 mctz = soft_limit_tree_node_zone(node, zone);
431 mem_cgroup_remove_exceeded(mem, mz, mctz);
432 }
433 }
434}
435
Balbir Singh4e416952009-09-23 15:56:39 -0700436static inline unsigned long mem_cgroup_get_excess(struct mem_cgroup *mem)
437{
438 return res_counter_soft_limit_excess(&mem->res) >> PAGE_SHIFT;
439}
440
441static struct mem_cgroup_per_zone *
442__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
443{
444 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700445 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700446
447retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700448 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700449 rightmost = rb_last(&mctz->rb_root);
450 if (!rightmost)
451 goto done; /* Nothing to reclaim from */
452
453 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
454 /*
455 * Remove the node now but someone else can add it back,
456 * we will to add it back at the end of reclaim to its correct
457 * position in the tree.
458 */
459 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
460 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
461 !css_tryget(&mz->mem->css))
462 goto retry;
463done:
464 return mz;
465}
466
467static struct mem_cgroup_per_zone *
468mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
469{
470 struct mem_cgroup_per_zone *mz;
471
472 spin_lock(&mctz->lock);
473 mz = __mem_cgroup_largest_soft_limit_node(mctz);
474 spin_unlock(&mctz->lock);
475 return mz;
476}
477
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700478static void mem_cgroup_swap_statistics(struct mem_cgroup *mem,
479 bool charge)
480{
481 int val = (charge) ? 1 : -1;
482 struct mem_cgroup_stat *stat = &mem->stat;
483 struct mem_cgroup_stat_cpu *cpustat;
484 int cpu = get_cpu();
485
486 cpustat = &stat->cpustat[cpu];
487 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_SWAPOUT, val);
488 put_cpu();
489}
490
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700491static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
492 struct page_cgroup *pc,
493 bool charge)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800494{
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700495 int val = (charge) ? 1 : -1;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800496 struct mem_cgroup_stat *stat = &mem->stat;
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700497 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800498 int cpu = get_cpu();
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800499
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800500 cpustat = &stat->cpustat[cpu];
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700501 if (PageCgroupCache(pc))
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700502 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800503 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700504 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700505
506 if (charge)
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700507 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700508 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
509 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700510 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700511 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700512 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_EVENTS, 1);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800513 put_cpu();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800514}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800515
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700516static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700517 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800518{
519 int nid, zid;
520 struct mem_cgroup_per_zone *mz;
521 u64 total = 0;
522
523 for_each_online_node(nid)
524 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
525 mz = mem_cgroup_zoneinfo(mem, nid, zid);
526 total += MEM_CGROUP_ZSTAT(mz, idx);
527 }
528 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800529}
530
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800531static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800532{
533 return container_of(cgroup_subsys_state(cont,
534 mem_cgroup_subsys_id), struct mem_cgroup,
535 css);
536}
537
Balbir Singhcf475ad2008-04-29 01:00:16 -0700538struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800539{
Balbir Singh31a78f22008-09-28 23:09:31 +0100540 /*
541 * mm_update_next_owner() may clear mm->owner to NULL
542 * if it races with swapoff, page migration, etc.
543 * So this can be called with p == NULL.
544 */
545 if (unlikely(!p))
546 return NULL;
547
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800548 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
549 struct mem_cgroup, css);
550}
551
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800552static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
553{
554 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700555
556 if (!mm)
557 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800558 /*
559 * Because we have no locks, mm->owner's may be being moved to other
560 * cgroup. We use css_tryget() here even if this looks
561 * pessimistic (rather than adding locks here).
562 */
563 rcu_read_lock();
564 do {
565 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
566 if (unlikely(!mem))
567 break;
568 } while (!css_tryget(&mem->css));
569 rcu_read_unlock();
570 return mem;
571}
572
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700573/*
574 * Call callback function against all cgroup under hierarchy tree.
575 */
576static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
577 int (*func)(struct mem_cgroup *, void *))
578{
579 int found, ret, nextid;
580 struct cgroup_subsys_state *css;
581 struct mem_cgroup *mem;
582
583 if (!root->use_hierarchy)
584 return (*func)(root, data);
585
586 nextid = 1;
587 do {
588 ret = 0;
589 mem = NULL;
590
591 rcu_read_lock();
592 css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
593 &found);
594 if (css && css_tryget(css))
595 mem = container_of(css, struct mem_cgroup, css);
596 rcu_read_unlock();
597
598 if (mem) {
599 ret = (*func)(mem, data);
600 css_put(&mem->css);
601 }
602 nextid = found + 1;
603 } while (!ret && css);
604
605 return ret;
606}
607
Balbir Singh4b3bde42009-09-23 15:56:32 -0700608static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
609{
610 return (mem == root_mem_cgroup);
611}
612
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800613/*
614 * Following LRU functions are allowed to be used without PCG_LOCK.
615 * Operations are called by routine of global LRU independently from memcg.
616 * What we have to take care of here is validness of pc->mem_cgroup.
617 *
618 * Changes to pc->mem_cgroup happens when
619 * 1. charge
620 * 2. moving account
621 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
622 * It is added to LRU before charge.
623 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
624 * When moving account, the page is not on LRU. It's isolated.
625 */
626
627void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800628{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800629 struct page_cgroup *pc;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800630 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700631
Hirokazu Takahashif8d66542009-01-07 18:08:02 -0800632 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800633 return;
634 pc = lookup_page_cgroup(page);
635 /* can happen while we handle swapcache. */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700636 if (!TestClearPageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800637 return;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700638 VM_BUG_ON(!pc->mem_cgroup);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800639 /*
640 * We don't check PCG_USED bit. It's cleared when the "page" is finally
641 * removed from global LRU.
642 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800643 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700644 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700645 if (mem_cgroup_is_root(pc->mem_cgroup))
646 return;
647 VM_BUG_ON(list_empty(&pc->lru));
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800648 list_del_init(&pc->lru);
649 return;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800650}
651
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800652void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800653{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800654 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800655}
656
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800657void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800658{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800659 struct mem_cgroup_per_zone *mz;
660 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800661
Hirokazu Takahashif8d66542009-01-07 18:08:02 -0800662 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700663 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700664
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800665 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800666 /*
667 * Used bit is set without atomic ops but after smp_wmb().
668 * For making pc->mem_cgroup visible, insert smp_rmb() here.
669 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800670 smp_rmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -0700671 /* unused or root page is not rotated. */
672 if (!PageCgroupUsed(pc) || mem_cgroup_is_root(pc->mem_cgroup))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800673 return;
674 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700675 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800676}
677
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800678void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
679{
680 struct page_cgroup *pc;
681 struct mem_cgroup_per_zone *mz;
682
Hirokazu Takahashif8d66542009-01-07 18:08:02 -0800683 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800684 return;
685 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700686 VM_BUG_ON(PageCgroupAcctLRU(pc));
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800687 /*
688 * Used bit is set without atomic ops but after smp_wmb().
689 * For making pc->mem_cgroup visible, insert smp_rmb() here.
690 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800691 smp_rmb();
692 if (!PageCgroupUsed(pc))
693 return;
694
695 mz = page_cgroup_zoneinfo(pc);
696 MEM_CGROUP_ZSTAT(mz, lru) += 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700697 SetPageCgroupAcctLRU(pc);
698 if (mem_cgroup_is_root(pc->mem_cgroup))
699 return;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800700 list_add(&pc->lru, &mz->lists[lru]);
701}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800702
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800703/*
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800704 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
705 * lru because the page may.be reused after it's fully uncharged (because of
706 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
707 * it again. This function is only used to charge SwapCache. It's done under
708 * lock_page and expected that zone->lru_lock is never held.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800709 */
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800710static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800711{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800712 unsigned long flags;
713 struct zone *zone = page_zone(page);
714 struct page_cgroup *pc = lookup_page_cgroup(page);
715
716 spin_lock_irqsave(&zone->lru_lock, flags);
717 /*
718 * Forget old LRU when this page_cgroup is *not* used. This Used bit
719 * is guarded by lock_page() because the page is SwapCache.
720 */
721 if (!PageCgroupUsed(pc))
722 mem_cgroup_del_lru_list(page, page_lru(page));
723 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800724}
725
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800726static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
727{
728 unsigned long flags;
729 struct zone *zone = page_zone(page);
730 struct page_cgroup *pc = lookup_page_cgroup(page);
731
732 spin_lock_irqsave(&zone->lru_lock, flags);
733 /* link when the page is linked to LRU but page_cgroup isn't */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700734 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800735 mem_cgroup_add_lru_list(page, page_lru(page));
736 spin_unlock_irqrestore(&zone->lru_lock, flags);
737}
738
739
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800740void mem_cgroup_move_lists(struct page *page,
741 enum lru_list from, enum lru_list to)
742{
Hirokazu Takahashif8d66542009-01-07 18:08:02 -0800743 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800744 return;
745 mem_cgroup_del_lru_list(page, from);
746 mem_cgroup_add_lru_list(page, to);
747}
748
David Rientjes4c4a2212008-02-07 00:14:06 -0800749int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
750{
751 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700752 struct mem_cgroup *curr = NULL;
David Rientjes4c4a2212008-02-07 00:14:06 -0800753
754 task_lock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700755 rcu_read_lock();
756 curr = try_get_mem_cgroup_from_mm(task->mm);
757 rcu_read_unlock();
David Rientjes4c4a2212008-02-07 00:14:06 -0800758 task_unlock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700759 if (!curr)
760 return 0;
Daisuke Nishimuraad46fed2009-12-15 16:47:12 -0800761 /*
762 * We should check use_hierarchy of "mem" not "curr". Because checking
763 * use_hierarchy of "curr" here make this function true if hierarchy is
764 * enabled in "curr" and "curr" is a child of "mem" in *cgroup*
765 * hierarchy(even if use_hierarchy is disabled in "mem").
766 */
767 if (mem->use_hierarchy)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700768 ret = css_is_ancestor(&curr->css, &mem->css);
769 else
770 ret = (curr == mem);
771 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -0800772 return ret;
773}
774
Balbir Singh66e17072008-02-07 00:13:56 -0800775/*
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800776 * prev_priority control...this will be used in memory reclaim path.
777 */
778int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
779{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800780 int prev_priority;
781
782 spin_lock(&mem->reclaim_param_lock);
783 prev_priority = mem->prev_priority;
784 spin_unlock(&mem->reclaim_param_lock);
785
786 return prev_priority;
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800787}
788
789void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
790{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800791 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800792 if (priority < mem->prev_priority)
793 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800794 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800795}
796
797void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
798{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800799 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800800 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800801 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800802}
803
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800804static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800805{
806 unsigned long active;
807 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800808 unsigned long gb;
809 unsigned long inactive_ratio;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800810
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700811 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
812 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800813
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800814 gb = (inactive + active) >> (30 - PAGE_SHIFT);
815 if (gb)
816 inactive_ratio = int_sqrt(10 * gb);
817 else
818 inactive_ratio = 1;
819
820 if (present_pages) {
821 present_pages[0] = inactive;
822 present_pages[1] = active;
823 }
824
825 return inactive_ratio;
826}
827
828int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
829{
830 unsigned long active;
831 unsigned long inactive;
832 unsigned long present_pages[2];
833 unsigned long inactive_ratio;
834
835 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
836
837 inactive = present_pages[0];
838 active = present_pages[1];
839
840 if (inactive * inactive_ratio < active)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800841 return 1;
842
843 return 0;
844}
845
Rik van Riel56e49d22009-06-16 15:32:28 -0700846int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
847{
848 unsigned long active;
849 unsigned long inactive;
850
851 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
852 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
853
854 return (active > inactive);
855}
856
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800857unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
858 struct zone *zone,
859 enum lru_list lru)
860{
861 int nid = zone->zone_pgdat->node_id;
862 int zid = zone_idx(zone);
863 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
864
865 return MEM_CGROUP_ZSTAT(mz, lru);
866}
867
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800868struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
869 struct zone *zone)
870{
871 int nid = zone->zone_pgdat->node_id;
872 int zid = zone_idx(zone);
873 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
874
875 return &mz->reclaim_stat;
876}
877
878struct zone_reclaim_stat *
879mem_cgroup_get_reclaim_stat_from_page(struct page *page)
880{
881 struct page_cgroup *pc;
882 struct mem_cgroup_per_zone *mz;
883
884 if (mem_cgroup_disabled())
885 return NULL;
886
887 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800888 /*
889 * Used bit is set without atomic ops but after smp_wmb().
890 * For making pc->mem_cgroup visible, insert smp_rmb() here.
891 */
892 smp_rmb();
893 if (!PageCgroupUsed(pc))
894 return NULL;
895
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800896 mz = page_cgroup_zoneinfo(pc);
897 if (!mz)
898 return NULL;
899
900 return &mz->reclaim_stat;
901}
902
Balbir Singh66e17072008-02-07 00:13:56 -0800903unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
904 struct list_head *dst,
905 unsigned long *scanned, int order,
906 int mode, struct zone *z,
907 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700908 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -0800909{
910 unsigned long nr_taken = 0;
911 struct page *page;
912 unsigned long scan;
913 LIST_HEAD(pc_list);
914 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800915 struct page_cgroup *pc, *tmp;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800916 int nid = z->zone_pgdat->node_id;
917 int zid = zone_idx(z);
918 struct mem_cgroup_per_zone *mz;
Johannes Weinerb7c46d12009-09-21 17:02:56 -0700919 int lru = LRU_FILE * file + active;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700920 int ret;
Balbir Singh66e17072008-02-07 00:13:56 -0800921
Balbir Singhcf475ad2008-04-29 01:00:16 -0700922 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800923 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700924 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -0800925
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800926 scan = 0;
927 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -0800928 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800929 break;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800930
931 page = pc->page;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700932 if (unlikely(!PageCgroupUsed(pc)))
933 continue;
Hugh Dickins436c65412008-02-07 00:14:12 -0800934 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800935 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800936
Hugh Dickins436c65412008-02-07 00:14:12 -0800937 scan++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700938 ret = __isolate_lru_page(page, mode, file);
939 switch (ret) {
940 case 0:
Balbir Singh66e17072008-02-07 00:13:56 -0800941 list_move(&page->lru, dst);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700942 mem_cgroup_del_lru(page);
Balbir Singh66e17072008-02-07 00:13:56 -0800943 nr_taken++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700944 break;
945 case -EBUSY:
946 /* we don't affect global LRU but rotate in our LRU */
947 mem_cgroup_rotate_lru_list(page, page_lru(page));
948 break;
949 default:
950 break;
Balbir Singh66e17072008-02-07 00:13:56 -0800951 }
952 }
953
Balbir Singh66e17072008-02-07 00:13:56 -0800954 *scanned = scan;
955 return nr_taken;
956}
957
Balbir Singh6d61ef42009-01-07 18:08:06 -0800958#define mem_cgroup_from_res_counter(counter, member) \
959 container_of(counter, struct mem_cgroup, member)
960
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -0800961static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
962{
963 if (do_swap_account) {
964 if (res_counter_check_under_limit(&mem->res) &&
965 res_counter_check_under_limit(&mem->memsw))
966 return true;
967 } else
968 if (res_counter_check_under_limit(&mem->res))
969 return true;
970 return false;
971}
972
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800973static unsigned int get_swappiness(struct mem_cgroup *memcg)
974{
975 struct cgroup *cgrp = memcg->css.cgroup;
976 unsigned int swappiness;
977
978 /* root ? */
979 if (cgrp->parent == NULL)
980 return vm_swappiness;
981
982 spin_lock(&memcg->reclaim_param_lock);
983 swappiness = memcg->swappiness;
984 spin_unlock(&memcg->reclaim_param_lock);
985
986 return swappiness;
987}
988
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700989static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
990{
991 int *val = data;
992 (*val)++;
993 return 0;
994}
Balbir Singhe2224322009-04-02 16:57:39 -0700995
996/**
997 * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode.
998 * @memcg: The memory cgroup that went over limit
999 * @p: Task that is going to be killed
1000 *
1001 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1002 * enabled
1003 */
1004void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1005{
1006 struct cgroup *task_cgrp;
1007 struct cgroup *mem_cgrp;
1008 /*
1009 * Need a buffer in BSS, can't rely on allocations. The code relies
1010 * on the assumption that OOM is serialized for memory controller.
1011 * If this assumption is broken, revisit this code.
1012 */
1013 static char memcg_name[PATH_MAX];
1014 int ret;
1015
1016 if (!memcg)
1017 return;
1018
1019
1020 rcu_read_lock();
1021
1022 mem_cgrp = memcg->css.cgroup;
1023 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1024
1025 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1026 if (ret < 0) {
1027 /*
1028 * Unfortunately, we are unable to convert to a useful name
1029 * But we'll still print out the usage information
1030 */
1031 rcu_read_unlock();
1032 goto done;
1033 }
1034 rcu_read_unlock();
1035
1036 printk(KERN_INFO "Task in %s killed", memcg_name);
1037
1038 rcu_read_lock();
1039 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1040 if (ret < 0) {
1041 rcu_read_unlock();
1042 goto done;
1043 }
1044 rcu_read_unlock();
1045
1046 /*
1047 * Continues from above, so we don't need an KERN_ level
1048 */
1049 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1050done:
1051
1052 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1053 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1054 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1055 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1056 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1057 "failcnt %llu\n",
1058 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1059 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1060 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1061}
1062
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001063/*
1064 * This function returns the number of memcg under hierarchy tree. Returns
1065 * 1(self count) if no children.
1066 */
1067static int mem_cgroup_count_children(struct mem_cgroup *mem)
1068{
1069 int num = 0;
1070 mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
1071 return num;
1072}
1073
Balbir Singh6d61ef42009-01-07 18:08:06 -08001074/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001075 * Visit the first child (need not be the first child as per the ordering
1076 * of the cgroup list, since we track last_scanned_child) of @mem and use
1077 * that to reclaim free pages from.
1078 */
1079static struct mem_cgroup *
1080mem_cgroup_select_victim(struct mem_cgroup *root_mem)
1081{
1082 struct mem_cgroup *ret = NULL;
1083 struct cgroup_subsys_state *css;
1084 int nextid, found;
1085
1086 if (!root_mem->use_hierarchy) {
1087 css_get(&root_mem->css);
1088 ret = root_mem;
1089 }
1090
1091 while (!ret) {
1092 rcu_read_lock();
1093 nextid = root_mem->last_scanned_child + 1;
1094 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
1095 &found);
1096 if (css && css_tryget(css))
1097 ret = container_of(css, struct mem_cgroup, css);
1098
1099 rcu_read_unlock();
1100 /* Updates scanning parameter */
1101 spin_lock(&root_mem->reclaim_param_lock);
1102 if (!css) {
1103 /* this means start scan from ID:1 */
1104 root_mem->last_scanned_child = 0;
1105 } else
1106 root_mem->last_scanned_child = found;
1107 spin_unlock(&root_mem->reclaim_param_lock);
1108 }
1109
1110 return ret;
1111}
1112
1113/*
1114 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1115 * we reclaimed from, so that we don't end up penalizing one child extensively
1116 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001117 *
1118 * root_mem is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001119 *
1120 * We give up and return to the caller when we visit root_mem twice.
1121 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001122 *
1123 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001124 */
1125static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
Balbir Singh4e416952009-09-23 15:56:39 -07001126 struct zone *zone,
Balbir Singh75822b42009-09-23 15:56:38 -07001127 gfp_t gfp_mask,
1128 unsigned long reclaim_options)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001129{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001130 struct mem_cgroup *victim;
1131 int ret, total = 0;
1132 int loop = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001133 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1134 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
Balbir Singh4e416952009-09-23 15:56:39 -07001135 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
1136 unsigned long excess = mem_cgroup_get_excess(root_mem);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001137
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001138 /* If memsw_is_minimum==1, swap-out is of-no-use. */
1139 if (root_mem->memsw_is_minimum)
1140 noswap = true;
1141
Balbir Singh4e416952009-09-23 15:56:39 -07001142 while (1) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001143 victim = mem_cgroup_select_victim(root_mem);
Balbir Singh4e416952009-09-23 15:56:39 -07001144 if (victim == root_mem) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001145 loop++;
Balbir Singh4e416952009-09-23 15:56:39 -07001146 if (loop >= 2) {
1147 /*
1148 * If we have not been able to reclaim
1149 * anything, it might because there are
1150 * no reclaimable pages under this hierarchy
1151 */
1152 if (!check_soft || !total) {
1153 css_put(&victim->css);
1154 break;
1155 }
1156 /*
1157 * We want to do more targetted reclaim.
1158 * excess >> 2 is not to excessive so as to
1159 * reclaim too much, nor too less that we keep
1160 * coming back to reclaim from this cgroup
1161 */
1162 if (total >= (excess >> 2) ||
1163 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1164 css_put(&victim->css);
1165 break;
1166 }
1167 }
1168 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001169 if (!mem_cgroup_local_usage(&victim->stat)) {
1170 /* this cgroup's local usage == 0 */
1171 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001172 continue;
1173 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001174 /* we use swappiness of local cgroup */
Balbir Singh4e416952009-09-23 15:56:39 -07001175 if (check_soft)
1176 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
1177 noswap, get_swappiness(victim), zone,
1178 zone->zone_pgdat->node_id);
1179 else
1180 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
1181 noswap, get_swappiness(victim));
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001182 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001183 /*
1184 * At shrinking usage, we can't check we should stop here or
1185 * reclaim more. It's depends on callers. last_scanned_child
1186 * will work enough for keeping fairness under tree.
1187 */
1188 if (shrink)
1189 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001190 total += ret;
Balbir Singh4e416952009-09-23 15:56:39 -07001191 if (check_soft) {
1192 if (res_counter_check_under_soft_limit(&root_mem->res))
1193 return total;
1194 } else if (mem_cgroup_check_under_limit(root_mem))
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001195 return 1 + total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001196 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001197 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001198}
1199
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001200bool mem_cgroup_oom_called(struct task_struct *task)
1201{
1202 bool ret = false;
1203 struct mem_cgroup *mem;
1204 struct mm_struct *mm;
1205
1206 rcu_read_lock();
1207 mm = task->mm;
1208 if (!mm)
1209 mm = &init_mm;
1210 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
1211 if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
1212 ret = true;
1213 rcu_read_unlock();
1214 return ret;
1215}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001216
1217static int record_last_oom_cb(struct mem_cgroup *mem, void *data)
1218{
1219 mem->last_oom_jiffies = jiffies;
1220 return 0;
1221}
1222
1223static void record_last_oom(struct mem_cgroup *mem)
1224{
1225 mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb);
1226}
1227
Balbir Singhd69b0422009-06-17 16:26:34 -07001228/*
1229 * Currently used to update mapped file statistics, but the routine can be
1230 * generalized to update other statistics as well.
1231 */
1232void mem_cgroup_update_mapped_file_stat(struct page *page, int val)
1233{
1234 struct mem_cgroup *mem;
1235 struct mem_cgroup_stat *stat;
1236 struct mem_cgroup_stat_cpu *cpustat;
1237 int cpu;
1238 struct page_cgroup *pc;
1239
1240 if (!page_is_file_cache(page))
1241 return;
1242
1243 pc = lookup_page_cgroup(page);
1244 if (unlikely(!pc))
1245 return;
1246
1247 lock_page_cgroup(pc);
1248 mem = pc->mem_cgroup;
1249 if (!mem)
1250 goto done;
1251
1252 if (!PageCgroupUsed(pc))
1253 goto done;
1254
1255 /*
1256 * Preemption is already disabled, we don't need get_cpu()
1257 */
1258 cpu = smp_processor_id();
1259 stat = &mem->stat;
1260 cpustat = &stat->cpustat[cpu];
1261
1262 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE, val);
1263done:
1264 unlock_page_cgroup(pc);
1265}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001266
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001267/*
1268 * Unlike exported interface, "oom" parameter is added. if oom==true,
1269 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001270 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001271static int __mem_cgroup_try_charge(struct mm_struct *mm,
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001272 gfp_t gfp_mask, struct mem_cgroup **memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -07001273 bool oom, struct page *page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001274{
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001275 struct mem_cgroup *mem, *mem_over_limit;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001276 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001277 struct res_counter *fail_res;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001278
1279 if (unlikely(test_thread_flag(TIF_MEMDIE))) {
1280 /* Don't account this! */
1281 *memcg = NULL;
1282 return 0;
1283 }
1284
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001285 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08001286 * We always charge the cgroup the mm_struct belongs to.
1287 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001288 * thread group leader migrates. It's possible that mm is not
1289 * set, if so charge the init_mm (happens for pagecache usage).
1290 */
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001291 mem = *memcg;
1292 if (likely(!mem)) {
1293 mem = try_get_mem_cgroup_from_mm(mm);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001294 *memcg = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001295 } else {
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001296 css_get(&mem->css);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001297 }
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001298 if (unlikely(!mem))
1299 return 0;
1300
Nikanth Karthikesan46f7e602009-05-28 14:34:41 -07001301 VM_BUG_ON(css_is_removed(&mem->css));
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001302
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001303 while (1) {
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001304 int ret = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001305 unsigned long flags = 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001306
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001307 if (mem_cgroup_is_root(mem))
1308 goto done;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001309 ret = res_counter_charge(&mem->res, PAGE_SIZE, &fail_res);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001310 if (likely(!ret)) {
1311 if (!do_swap_account)
1312 break;
Balbir Singh28dbc4b2009-01-07 18:08:05 -08001313 ret = res_counter_charge(&mem->memsw, PAGE_SIZE,
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001314 &fail_res);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001315 if (likely(!ret))
1316 break;
1317 /* mem+swap counter fails */
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001318 res_counter_uncharge(&mem->res, PAGE_SIZE);
Balbir Singh75822b42009-09-23 15:56:38 -07001319 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001320 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1321 memsw);
1322 } else
1323 /* mem counter fails */
1324 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1325 res);
1326
Hugh Dickins3be91272008-02-07 00:14:19 -08001327 if (!(gfp_mask & __GFP_WAIT))
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001328 goto nomem;
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001329
Balbir Singh4e416952009-09-23 15:56:39 -07001330 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
1331 gfp_mask, flags);
Daisuke Nishimura4d1c6272009-01-15 13:51:14 -08001332 if (ret)
1333 continue;
Balbir Singh66e17072008-02-07 00:13:56 -08001334
1335 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001336 * try_to_free_mem_cgroup_pages() might not give us a full
1337 * picture of reclaim. Some pages are reclaimed and might be
1338 * moved to swap cache or just unmapped from the cgroup.
1339 * Check the limit again to see if the reclaim reduced the
1340 * current usage of the cgroup before giving up
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001341 *
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001342 */
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -08001343 if (mem_cgroup_check_under_limit(mem_over_limit))
1344 continue;
Hugh Dickins3be91272008-02-07 00:14:19 -08001345
1346 if (!nr_retries--) {
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001347 if (oom) {
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08001348 mutex_lock(&memcg_tasklist);
KAMEZAWA Hiroyuki88700752009-01-07 18:08:09 -08001349 mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08001350 mutex_unlock(&memcg_tasklist);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001351 record_last_oom(mem_over_limit);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001352 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001353 goto nomem;
Balbir Singh66e17072008-02-07 00:13:56 -08001354 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001355 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07001356 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001357 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
1358 * if they exceeds softlimit.
Balbir Singhf64c3f52009-09-23 15:56:37 -07001359 */
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001360 if (mem_cgroup_soft_limit_check(mem))
1361 mem_cgroup_update_tree(mem, page);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001362done:
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001363 return 0;
1364nomem:
1365 css_put(&mem->css);
1366 return -ENOMEM;
1367}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001368
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001369/*
1370 * A helper function to get mem_cgroup from ID. must be called under
1371 * rcu_read_lock(). The caller must check css_is_removed() or some if
1372 * it's concern. (dropping refcnt from swap can be called against removed
1373 * memcg.)
1374 */
1375static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
1376{
1377 struct cgroup_subsys_state *css;
1378
1379 /* ID 0 is unused ID */
1380 if (!id)
1381 return NULL;
1382 css = css_lookup(&mem_cgroup_subsys, id);
1383 if (!css)
1384 return NULL;
1385 return container_of(css, struct mem_cgroup, css);
1386}
1387
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001388static struct mem_cgroup *try_get_mem_cgroup_from_swapcache(struct page *page)
1389{
1390 struct mem_cgroup *mem;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001391 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001392 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001393 swp_entry_t ent;
1394
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001395 VM_BUG_ON(!PageLocked(page));
1396
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001397 if (!PageSwapCache(page))
1398 return NULL;
1399
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001400 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001401 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001402 if (PageCgroupUsed(pc)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001403 mem = pc->mem_cgroup;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001404 if (mem && !css_tryget(&mem->css))
1405 mem = NULL;
1406 } else {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001407 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001408 id = lookup_swap_cgroup(ent);
1409 rcu_read_lock();
1410 mem = mem_cgroup_lookup(id);
1411 if (mem && !css_tryget(&mem->css))
1412 mem = NULL;
1413 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001414 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001415 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001416 return mem;
1417}
1418
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001419/*
Daisuke Nishimuraa5e924f2009-01-07 18:08:28 -08001420 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001421 * USED state. If already USED, uncharge and return.
1422 */
1423
1424static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1425 struct page_cgroup *pc,
1426 enum charge_type ctype)
1427{
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001428 /* try_charge() can return NULL to *memcg, taking care of it. */
1429 if (!mem)
1430 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001431
1432 lock_page_cgroup(pc);
1433 if (unlikely(PageCgroupUsed(pc))) {
1434 unlock_page_cgroup(pc);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001435 if (!mem_cgroup_is_root(mem)) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001436 res_counter_uncharge(&mem->res, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001437 if (do_swap_account)
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001438 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001439 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001440 css_put(&mem->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001441 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001442 }
Balbir Singh4b3bde42009-09-23 15:56:32 -07001443
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001444 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07001445 /*
1446 * We access a page_cgroup asynchronously without lock_page_cgroup().
1447 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
1448 * is accessed after testing USED bit. To make pc->mem_cgroup visible
1449 * before USED bit, we need memory barrier here.
1450 * See mem_cgroup_add_lru_list(), etc.
1451 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001452 smp_wmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -07001453 switch (ctype) {
1454 case MEM_CGROUP_CHARGE_TYPE_CACHE:
1455 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
1456 SetPageCgroupCache(pc);
1457 SetPageCgroupUsed(pc);
1458 break;
1459 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1460 ClearPageCgroupCache(pc);
1461 SetPageCgroupUsed(pc);
1462 break;
1463 default:
1464 break;
1465 }
Hugh Dickins3be91272008-02-07 00:14:19 -08001466
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001467 mem_cgroup_charge_statistics(mem, pc, true);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001468
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001469 unlock_page_cgroup(pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001470}
1471
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001472/**
1473 * mem_cgroup_move_account - move account of the page
1474 * @pc: page_cgroup of the page.
1475 * @from: mem_cgroup which the page is moved from.
1476 * @to: mem_cgroup which the page is moved to. @from != @to.
1477 *
1478 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001479 * - page is not on LRU (isolate_page() is useful.)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001480 *
1481 * returns 0 at success,
1482 * returns -EBUSY when lock is busy or "pc" is unstable.
1483 *
1484 * This function does "uncharge" from old cgroup but doesn't do "charge" to
1485 * new cgroup. It should be done by a caller.
1486 */
1487
1488static int mem_cgroup_move_account(struct page_cgroup *pc,
1489 struct mem_cgroup *from, struct mem_cgroup *to)
1490{
1491 struct mem_cgroup_per_zone *from_mz, *to_mz;
1492 int nid, zid;
1493 int ret = -EBUSY;
Balbir Singhd69b0422009-06-17 16:26:34 -07001494 struct page *page;
1495 int cpu;
1496 struct mem_cgroup_stat *stat;
1497 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001498
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001499 VM_BUG_ON(from == to);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001500 VM_BUG_ON(PageLRU(pc->page));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001501
1502 nid = page_cgroup_nid(pc);
1503 zid = page_cgroup_zid(pc);
1504 from_mz = mem_cgroup_zoneinfo(from, nid, zid);
1505 to_mz = mem_cgroup_zoneinfo(to, nid, zid);
1506
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001507 if (!trylock_page_cgroup(pc))
1508 return ret;
1509
1510 if (!PageCgroupUsed(pc))
1511 goto out;
1512
1513 if (pc->mem_cgroup != from)
1514 goto out;
1515
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001516 if (!mem_cgroup_is_root(from))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001517 res_counter_uncharge(&from->res, PAGE_SIZE);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001518 mem_cgroup_charge_statistics(from, pc, false);
Balbir Singhd69b0422009-06-17 16:26:34 -07001519
1520 page = pc->page;
1521 if (page_is_file_cache(page) && page_mapped(page)) {
1522 cpu = smp_processor_id();
1523 /* Update mapped_file data for mem_cgroup "from" */
1524 stat = &from->stat;
1525 cpustat = &stat->cpustat[cpu];
1526 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1527 -1);
1528
1529 /* Update mapped_file data for mem_cgroup "to" */
1530 stat = &to->stat;
1531 cpustat = &stat->cpustat[cpu];
1532 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1533 1);
1534 }
1535
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001536 if (do_swap_account && !mem_cgroup_is_root(from))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001537 res_counter_uncharge(&from->memsw, PAGE_SIZE);
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001538 css_put(&from->css);
1539
1540 css_get(&to->css);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001541 pc->mem_cgroup = to;
1542 mem_cgroup_charge_statistics(to, pc, true);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001543 ret = 0;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001544out:
1545 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001546 /*
1547 * We charges against "to" which may not have any tasks. Then, "to"
1548 * can be under rmdir(). But in current implementation, caller of
1549 * this function is just force_empty() and it's garanteed that
1550 * "to" is never removed. So, we don't check rmdir status here.
1551 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001552 return ret;
1553}
1554
1555/*
1556 * move charges to its parent.
1557 */
1558
1559static int mem_cgroup_move_parent(struct page_cgroup *pc,
1560 struct mem_cgroup *child,
1561 gfp_t gfp_mask)
1562{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001563 struct page *page = pc->page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001564 struct cgroup *cg = child->css.cgroup;
1565 struct cgroup *pcg = cg->parent;
1566 struct mem_cgroup *parent;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001567 int ret;
1568
1569 /* Is ROOT ? */
1570 if (!pcg)
1571 return -EINVAL;
1572
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001573
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001574 parent = mem_cgroup_from_cont(pcg);
1575
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001576
Balbir Singhf64c3f52009-09-23 15:56:37 -07001577 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false, page);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001578 if (ret || !parent)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001579 return ret;
1580
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001581 if (!get_page_unless_zero(page)) {
1582 ret = -EBUSY;
1583 goto uncharge;
1584 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001585
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001586 ret = isolate_lru_page(page);
1587
1588 if (ret)
1589 goto cancel;
1590
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001591 ret = mem_cgroup_move_account(pc, child, parent);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001592
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001593 putback_lru_page(page);
1594 if (!ret) {
1595 put_page(page);
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001596 /* drop extra refcnt by try_charge() */
1597 css_put(&parent->css);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001598 return 0;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001599 }
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001600
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001601cancel:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001602 put_page(page);
1603uncharge:
1604 /* drop extra refcnt by try_charge() */
1605 css_put(&parent->css);
1606 /* uncharge if move fails */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001607 if (!mem_cgroup_is_root(parent)) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001608 res_counter_uncharge(&parent->res, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001609 if (do_swap_account)
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001610 res_counter_uncharge(&parent->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001611 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001612 return ret;
1613}
1614
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001615/*
1616 * Charge the memory controller for page usage.
1617 * Return
1618 * 0 if the charge was successful
1619 * < 0 if the cgroup is over its limit
1620 */
1621static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
1622 gfp_t gfp_mask, enum charge_type ctype,
1623 struct mem_cgroup *memcg)
1624{
1625 struct mem_cgroup *mem;
1626 struct page_cgroup *pc;
1627 int ret;
1628
1629 pc = lookup_page_cgroup(page);
1630 /* can happen at boot */
1631 if (unlikely(!pc))
1632 return 0;
1633 prefetchw(pc);
1634
1635 mem = memcg;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001636 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true, page);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001637 if (ret || !mem)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001638 return ret;
1639
1640 __mem_cgroup_commit_charge(mem, pc, ctype);
1641 return 0;
1642}
1643
1644int mem_cgroup_newpage_charge(struct page *page,
1645 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001646{
Hirokazu Takahashif8d66542009-01-07 18:08:02 -08001647 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001648 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001649 if (PageCompound(page))
1650 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001651 /*
1652 * If already mapped, we don't have to account.
1653 * If page cache, page->mapping has address_space.
1654 * But page->mapping may have out-of-use anon_vma pointer,
1655 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
1656 * is NULL.
1657 */
1658 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
1659 return 0;
1660 if (unlikely(!mm))
1661 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001662 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001663 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001664}
1665
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001666static void
1667__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1668 enum charge_type ctype);
1669
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001670int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
1671 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08001672{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001673 struct mem_cgroup *mem = NULL;
1674 int ret;
1675
Hirokazu Takahashif8d66542009-01-07 18:08:02 -08001676 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001677 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001678 if (PageCompound(page))
1679 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001680 /*
1681 * Corner case handling. This is called from add_to_page_cache()
1682 * in usual. But some FS (shmem) precharges this page before calling it
1683 * and call add_to_page_cache() with GFP_NOWAIT.
1684 *
1685 * For GFP_NOWAIT case, the page may be pre-charged before calling
1686 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
1687 * charge twice. (It works but has to pay a bit larger cost.)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001688 * And when the page is SwapCache, it should take swap information
1689 * into account. This is under lock_page() now.
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001690 */
1691 if (!(gfp_mask & __GFP_WAIT)) {
1692 struct page_cgroup *pc;
1693
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001694
1695 pc = lookup_page_cgroup(page);
1696 if (!pc)
1697 return 0;
1698 lock_page_cgroup(pc);
1699 if (PageCgroupUsed(pc)) {
1700 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001701 return 0;
1702 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001703 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001704 }
1705
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001706 if (unlikely(!mm && !mem))
Balbir Singh8697d332008-02-07 00:13:59 -08001707 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001708
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -07001709 if (page_is_file_cache(page))
1710 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001711 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001712
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001713 /* shmem */
1714 if (PageSwapCache(page)) {
1715 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1716 if (!ret)
1717 __mem_cgroup_commit_charge_swapin(page, mem,
1718 MEM_CGROUP_CHARGE_TYPE_SHMEM);
1719 } else
1720 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
1721 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001722
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001723 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001724}
1725
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001726/*
1727 * While swap-in, try_charge -> commit or cancel, the page is locked.
1728 * And when try_charge() successfully returns, one refcnt to memcg without
1729 * struct page_cgroup is aquired. This refcnt will be cumsumed by
1730 * "commit()" or removed by "cancel()"
1731 */
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001732int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
1733 struct page *page,
1734 gfp_t mask, struct mem_cgroup **ptr)
1735{
1736 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001737 int ret;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001738
Hirokazu Takahashif8d66542009-01-07 18:08:02 -08001739 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001740 return 0;
1741
1742 if (!do_swap_account)
1743 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001744 /*
1745 * A racing thread's fault, or swapoff, may have already updated
1746 * the pte, and even removed page from swap cache: return success
1747 * to go on to do_swap_page()'s pte_same() test, which should fail.
1748 */
1749 if (!PageSwapCache(page))
1750 return 0;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001751 mem = try_get_mem_cgroup_from_swapcache(page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001752 if (!mem)
1753 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001754 *ptr = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001755 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true, page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001756 /* drop extra refcnt from tryget */
1757 css_put(&mem->css);
1758 return ret;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001759charge_cur_mm:
1760 if (unlikely(!mm))
1761 mm = &init_mm;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001762 return __mem_cgroup_try_charge(mm, mask, ptr, true, page);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001763}
1764
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001765static void
1766__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1767 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001768{
1769 struct page_cgroup *pc;
1770
Hirokazu Takahashif8d66542009-01-07 18:08:02 -08001771 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001772 return;
1773 if (!ptr)
1774 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001775 cgroup_exclude_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001776 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001777 mem_cgroup_lru_del_before_commit_swapcache(page);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001778 __mem_cgroup_commit_charge(ptr, pc, ctype);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001779 mem_cgroup_lru_add_after_commit_swapcache(page);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001780 /*
1781 * Now swap is on-memory. This means this page may be
1782 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08001783 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
1784 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
1785 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001786 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08001787 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001788 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001789 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001790 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001791
1792 id = swap_cgroup_record(ent, 0);
1793 rcu_read_lock();
1794 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001795 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001796 /*
1797 * This recorded memcg can be obsolete one. So, avoid
1798 * calling css_tryget
1799 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001800 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001801 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001802 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001803 mem_cgroup_put(memcg);
1804 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001805 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001806 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001807 /*
1808 * At swapin, we may charge account against cgroup which has no tasks.
1809 * So, rmdir()->pre_destroy() can be called while we do this charge.
1810 * In that case, we need to call pre_destroy() again. check it here.
1811 */
1812 cgroup_release_and_wakeup_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001813}
1814
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001815void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
1816{
1817 __mem_cgroup_commit_charge_swapin(page, ptr,
1818 MEM_CGROUP_CHARGE_TYPE_MAPPED);
1819}
1820
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001821void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
1822{
Hirokazu Takahashif8d66542009-01-07 18:08:02 -08001823 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001824 return;
1825 if (!mem)
1826 return;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001827 if (!mem_cgroup_is_root(mem)) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001828 res_counter_uncharge(&mem->res, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001829 if (do_swap_account)
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001830 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001831 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001832 css_put(&mem->css);
1833}
1834
1835
Balbir Singh8697d332008-02-07 00:13:59 -08001836/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001837 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001838 */
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001839static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001840__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001841{
Hugh Dickins82895462008-03-04 14:29:08 -08001842 struct page_cgroup *pc;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001843 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki072c56c2008-02-07 00:14:39 -08001844 struct mem_cgroup_per_zone *mz;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001845
Hirokazu Takahashif8d66542009-01-07 18:08:02 -08001846 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001847 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07001848
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001849 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001850 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001851
Balbir Singh8697d332008-02-07 00:13:59 -08001852 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08001853 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08001854 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001855 pc = lookup_page_cgroup(page);
1856 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001857 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001858
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001859 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001860
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001861 mem = pc->mem_cgroup;
1862
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001863 if (!PageCgroupUsed(pc))
1864 goto unlock_out;
1865
1866 switch (ctype) {
1867 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07001868 case MEM_CGROUP_CHARGE_TYPE_DROP:
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001869 if (page_mapped(page))
1870 goto unlock_out;
1871 break;
1872 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
1873 if (!PageAnon(page)) { /* Shared memory */
1874 if (page->mapping && !page_is_file_cache(page))
1875 goto unlock_out;
1876 } else if (page_mapped(page)) /* Anon */
1877 goto unlock_out;
1878 break;
1879 default:
1880 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001881 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001882
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001883 if (!mem_cgroup_is_root(mem)) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001884 res_counter_uncharge(&mem->res, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001885 if (do_swap_account &&
1886 (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001887 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001888 }
1889 if (ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1890 mem_cgroup_swap_statistics(mem, true);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001891 mem_cgroup_charge_statistics(mem, pc, false);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001892
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001893 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001894 /*
1895 * pc->mem_cgroup is not cleared here. It will be accessed when it's
1896 * freed from LRU. This is safe because uncharged page is expected not
1897 * to be reused (freed soon). Exception is SwapCache, it's handled by
1898 * special functions.
1899 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08001900
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001901 mz = page_cgroup_zoneinfo(pc);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001902 unlock_page_cgroup(pc);
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08001903
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001904 if (mem_cgroup_soft_limit_check(mem))
Balbir Singhf64c3f52009-09-23 15:56:37 -07001905 mem_cgroup_update_tree(mem, page);
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08001906 /* at swapout, this memcg will be accessed to record to swap */
1907 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1908 css_put(&mem->css);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001909
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001910 return mem;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001911
1912unlock_out:
1913 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001914 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08001915}
1916
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001917void mem_cgroup_uncharge_page(struct page *page)
1918{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001919 /* early check. */
1920 if (page_mapped(page))
1921 return;
1922 if (page->mapping && !PageAnon(page))
1923 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001924 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
1925}
1926
1927void mem_cgroup_uncharge_cache_page(struct page *page)
1928{
1929 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07001930 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001931 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
1932}
1933
Daisuke Nishimurae767e052009-05-28 14:34:28 -07001934#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001935/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07001936 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001937 * memcg information is recorded to swap_cgroup of "ent"
1938 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07001939void
1940mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001941{
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001942 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07001943 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001944
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07001945 if (!swapout) /* this was a swap cache but the swap is unused ! */
1946 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
1947
1948 memcg = __mem_cgroup_uncharge_common(page, ctype);
1949
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001950 /* record memcg information */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07001951 if (do_swap_account && swapout && memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001952 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001953 mem_cgroup_get(memcg);
1954 }
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07001955 if (swapout && memcg)
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08001956 css_put(&memcg->css);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001957}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07001958#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001959
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001960#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1961/*
1962 * called from swap_entry_free(). remove record in swap_cgroup and
1963 * uncharge "memsw" account.
1964 */
1965void mem_cgroup_uncharge_swap(swp_entry_t ent)
1966{
1967 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001968 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001969
1970 if (!do_swap_account)
1971 return;
1972
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001973 id = swap_cgroup_record(ent, 0);
1974 rcu_read_lock();
1975 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001976 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001977 /*
1978 * We uncharge this because swap is freed.
1979 * This memcg can be obsolete one. We avoid calling css_tryget
1980 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001981 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001982 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001983 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001984 mem_cgroup_put(memcg);
1985 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001986 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001987}
1988#endif
1989
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001990/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001991 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
1992 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001993 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001994int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001995{
1996 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001997 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001998 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001999
Hirokazu Takahashif8d66542009-01-07 18:08:02 -08002000 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07002001 return 0;
2002
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002003 pc = lookup_page_cgroup(page);
2004 lock_page_cgroup(pc);
2005 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002006 mem = pc->mem_cgroup;
2007 css_get(&mem->css);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08002008 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002009 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002010
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002011 if (mem) {
Balbir Singhf64c3f52009-09-23 15:56:37 -07002012 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false,
2013 page);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002014 css_put(&mem->css);
2015 }
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002016 *ptr = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002017 return ret;
2018}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08002019
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002020/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002021void mem_cgroup_end_migration(struct mem_cgroup *mem,
2022 struct page *oldpage, struct page *newpage)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002023{
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002024 struct page *target, *unused;
2025 struct page_cgroup *pc;
2026 enum charge_type ctype;
2027
2028 if (!mem)
2029 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002030 cgroup_exclude_rmdir(&mem->css);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002031 /* at migration success, oldpage->mapping is NULL. */
2032 if (oldpage->mapping) {
2033 target = oldpage;
2034 unused = NULL;
2035 } else {
2036 target = newpage;
2037 unused = oldpage;
2038 }
2039
2040 if (PageAnon(target))
2041 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
2042 else if (page_is_file_cache(target))
2043 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
2044 else
2045 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
2046
2047 /* unused page is not on radix-tree now. */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002048 if (unused)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002049 __mem_cgroup_uncharge_common(unused, ctype);
2050
2051 pc = lookup_page_cgroup(target);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002052 /*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002053 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
2054 * So, double-counting is effectively avoided.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002055 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002056 __mem_cgroup_commit_charge(mem, pc, ctype);
2057
2058 /*
2059 * Both of oldpage and newpage are still under lock_page().
2060 * Then, we don't have to care about race in radix-tree.
2061 * But we have to be careful that this page is unmapped or not.
2062 *
2063 * There is a case for !page_mapped(). At the start of
2064 * migration, oldpage was mapped. But now, it's zapped.
2065 * But we know *target* page is not freed/reused under us.
2066 * mem_cgroup_uncharge_page() does all necessary checks.
2067 */
2068 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
2069 mem_cgroup_uncharge_page(target);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002070 /*
2071 * At migration, we may charge account against cgroup which has no tasks
2072 * So, rmdir()->pre_destroy() can be called while we do this charge.
2073 * In that case, we need to call pre_destroy() again. check it here.
2074 */
2075 cgroup_release_and_wakeup_rmdir(&mem->css);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002076}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08002077
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002078/*
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002079 * A call to try to shrink memory usage on charge failure at shmem's swapin.
2080 * Calling hierarchical_reclaim is not enough because we should update
2081 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
2082 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
2083 * not from the memcg which this page would be charged to.
2084 * try_charge_swapin does all of these works properly.
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002085 */
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002086int mem_cgroup_shmem_charge_fallback(struct page *page,
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002087 struct mm_struct *mm,
2088 gfp_t gfp_mask)
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002089{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002090 struct mem_cgroup *mem = NULL;
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002091 int ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002092
Hirokazu Takahashif8d66542009-01-07 18:08:02 -08002093 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002094 return 0;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002095
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002096 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2097 if (!ret)
2098 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002099
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002100 return ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002101}
2102
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002103static DEFINE_MUTEX(set_limit_mutex);
2104
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08002105static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002106 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002107{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002108 int retry_count;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002109 int progress;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002110 u64 memswlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002111 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002112 int children = mem_cgroup_count_children(memcg);
2113 u64 curusage, oldusage;
2114
2115 /*
2116 * For keeping hierarchical_reclaim simple, how long we should retry
2117 * is depends on callers. We set our retry-count to be function
2118 * of # of children which we should visit in this loop.
2119 */
2120 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
2121
2122 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002123
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002124 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002125 if (signal_pending(current)) {
2126 ret = -EINTR;
2127 break;
2128 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002129 /*
2130 * Rather than hide all in some function, I do this in
2131 * open coded manner. You see what this really does.
2132 * We have to guarantee mem->res.limit < mem->memsw.limit.
2133 */
2134 mutex_lock(&set_limit_mutex);
2135 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2136 if (memswlimit < val) {
2137 ret = -EINVAL;
2138 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002139 break;
2140 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002141 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07002142 if (!ret) {
2143 if (memswlimit == val)
2144 memcg->memsw_is_minimum = true;
2145 else
2146 memcg->memsw_is_minimum = false;
2147 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002148 mutex_unlock(&set_limit_mutex);
2149
2150 if (!ret)
2151 break;
2152
Balbir Singh4e416952009-09-23 15:56:39 -07002153 progress = mem_cgroup_hierarchical_reclaim(memcg, NULL,
2154 GFP_KERNEL,
2155 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002156 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
2157 /* Usage is reduced ? */
2158 if (curusage >= oldusage)
2159 retry_count--;
2160 else
2161 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002162 }
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002163
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002164 return ret;
2165}
2166
Li Zefan338c8432009-06-17 16:27:15 -07002167static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
2168 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002169{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002170 int retry_count;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002171 u64 memlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002172 int children = mem_cgroup_count_children(memcg);
2173 int ret = -EBUSY;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002174
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002175 /* see mem_cgroup_resize_res_limit */
2176 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
2177 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002178 while (retry_count) {
2179 if (signal_pending(current)) {
2180 ret = -EINTR;
2181 break;
2182 }
2183 /*
2184 * Rather than hide all in some function, I do this in
2185 * open coded manner. You see what this really does.
2186 * We have to guarantee mem->res.limit < mem->memsw.limit.
2187 */
2188 mutex_lock(&set_limit_mutex);
2189 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2190 if (memlimit > val) {
2191 ret = -EINVAL;
2192 mutex_unlock(&set_limit_mutex);
2193 break;
2194 }
2195 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07002196 if (!ret) {
2197 if (memlimit == val)
2198 memcg->memsw_is_minimum = true;
2199 else
2200 memcg->memsw_is_minimum = false;
2201 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002202 mutex_unlock(&set_limit_mutex);
2203
2204 if (!ret)
2205 break;
2206
Balbir Singh4e416952009-09-23 15:56:39 -07002207 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh75822b42009-09-23 15:56:38 -07002208 MEM_CGROUP_RECLAIM_NOSWAP |
2209 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002210 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002211 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002212 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002213 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002214 else
2215 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002216 }
2217 return ret;
2218}
2219
Balbir Singh4e416952009-09-23 15:56:39 -07002220unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
2221 gfp_t gfp_mask, int nid,
2222 int zid)
2223{
2224 unsigned long nr_reclaimed = 0;
2225 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
2226 unsigned long reclaimed;
2227 int loop = 0;
2228 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002229 unsigned long long excess;
Balbir Singh4e416952009-09-23 15:56:39 -07002230
2231 if (order > 0)
2232 return 0;
2233
2234 mctz = soft_limit_tree_node_zone(nid, zid);
2235 /*
2236 * This loop can run a while, specially if mem_cgroup's continuously
2237 * keep exceeding their soft limit and putting the system under
2238 * pressure
2239 */
2240 do {
2241 if (next_mz)
2242 mz = next_mz;
2243 else
2244 mz = mem_cgroup_largest_soft_limit_node(mctz);
2245 if (!mz)
2246 break;
2247
2248 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
2249 gfp_mask,
2250 MEM_CGROUP_RECLAIM_SOFT);
2251 nr_reclaimed += reclaimed;
2252 spin_lock(&mctz->lock);
2253
2254 /*
2255 * If we failed to reclaim anything from this memory cgroup
2256 * it is time to move on to the next cgroup
2257 */
2258 next_mz = NULL;
2259 if (!reclaimed) {
2260 do {
2261 /*
2262 * Loop until we find yet another one.
2263 *
2264 * By the time we get the soft_limit lock
2265 * again, someone might have aded the
2266 * group back on the RB tree. Iterate to
2267 * make sure we get a different mem.
2268 * mem_cgroup_largest_soft_limit_node returns
2269 * NULL if no other cgroup is present on
2270 * the tree
2271 */
2272 next_mz =
2273 __mem_cgroup_largest_soft_limit_node(mctz);
2274 if (next_mz == mz) {
2275 css_put(&next_mz->mem->css);
2276 next_mz = NULL;
2277 } else /* next_mz == NULL or other memcg */
2278 break;
2279 } while (1);
2280 }
Balbir Singh4e416952009-09-23 15:56:39 -07002281 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002282 excess = res_counter_soft_limit_excess(&mz->mem->res);
Balbir Singh4e416952009-09-23 15:56:39 -07002283 /*
2284 * One school of thought says that we should not add
2285 * back the node to the tree if reclaim returns 0.
2286 * But our reclaim could return 0, simply because due
2287 * to priority we are exposing a smaller subset of
2288 * memory to reclaim from. Consider this as a longer
2289 * term TODO.
2290 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002291 /* If excess == 0, no tree ops */
2292 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07002293 spin_unlock(&mctz->lock);
2294 css_put(&mz->mem->css);
2295 loop++;
2296 /*
2297 * Could not reclaim anything and there are no more
2298 * mem cgroups to try or we seem to be looping without
2299 * reclaiming anything.
2300 */
2301 if (!nr_reclaimed &&
2302 (next_mz == NULL ||
2303 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
2304 break;
2305 } while (!nr_reclaimed);
2306 if (next_mz)
2307 css_put(&next_mz->mem->css);
2308 return nr_reclaimed;
2309}
2310
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002311/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002312 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002313 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
2314 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002315static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002316 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002317{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002318 struct zone *zone;
2319 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002320 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002321 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c2008-02-07 00:14:39 -08002322 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002323 int ret = 0;
KAMEZAWA Hiroyuki072c56c2008-02-07 00:14:39 -08002324
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002325 zone = &NODE_DATA(node)->node_zones[zid];
2326 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07002327 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002328
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002329 loop = MEM_CGROUP_ZSTAT(mz, lru);
2330 /* give some margin against EBUSY etc...*/
2331 loop += 256;
2332 busy = NULL;
2333 while (loop--) {
2334 ret = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002335 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002336 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002337 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002338 break;
2339 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002340 pc = list_entry(list->prev, struct page_cgroup, lru);
2341 if (busy == pc) {
2342 list_move(&pc->lru, list);
2343 busy = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002344 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002345 continue;
2346 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002347 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002348
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -08002349 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002350 if (ret == -ENOMEM)
2351 break;
2352
2353 if (ret == -EBUSY || ret == -EINVAL) {
2354 /* found lock contention or "pc" is obsolete. */
2355 busy = pc;
2356 cond_resched();
2357 } else
2358 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002359 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002360
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002361 if (!ret && !list_empty(list))
2362 return -EBUSY;
2363 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002364}
2365
2366/*
2367 * make mem_cgroup's charge to be 0 if there is no task.
2368 * This enables deleting this mem_cgroup.
2369 */
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002370static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002371{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002372 int ret;
2373 int node, zid, shrink;
2374 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002375 struct cgroup *cgrp = mem->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08002376
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002377 css_get(&mem->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002378
2379 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002380 /* should free all ? */
2381 if (free_all)
2382 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002383move_account:
Daisuke Nishimura4334ab72010-01-15 17:01:30 -08002384 do {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002385 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002386 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002387 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002388 ret = -EINTR;
2389 if (signal_pending(current))
2390 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002391 /* This is for making all *used* pages to be on LRU. */
2392 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002393 ret = 0;
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08002394 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002395 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07002396 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002397 for_each_lru(l) {
2398 ret = mem_cgroup_force_empty_list(mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002399 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002400 if (ret)
2401 break;
2402 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002403 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002404 if (ret)
2405 break;
2406 }
2407 /* it seems parent cgroup doesn't have enough mem */
2408 if (ret == -ENOMEM)
2409 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002410 cond_resched();
Daisuke Nishimura4334ab72010-01-15 17:01:30 -08002411 /* "ret" should also be checked to ensure all lists are empty. */
2412 } while (mem->res.usage > 0 || ret);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002413out:
2414 css_put(&mem->css);
2415 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002416
2417try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002418 /* returns EBUSY if there is a task or if we come here twice. */
2419 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002420 ret = -EBUSY;
2421 goto out;
2422 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002423 /* we call try-to-free pages for make this cgroup empty */
2424 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002425 /* try to free all pages in this cgroup */
2426 shrink = 1;
2427 while (nr_retries && mem->res.usage > 0) {
2428 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002429
2430 if (signal_pending(current)) {
2431 ret = -EINTR;
2432 goto out;
2433 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002434 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
2435 false, get_swappiness(mem));
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002436 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002437 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002438 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02002439 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002440 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002441
2442 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002443 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002444 /* try move_account...there may be some *locked* pages. */
Daisuke Nishimura4334ab72010-01-15 17:01:30 -08002445 goto move_account;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002446}
2447
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002448int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
2449{
2450 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
2451}
2452
2453
Balbir Singh18f59ea2009-01-07 18:08:07 -08002454static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
2455{
2456 return mem_cgroup_from_cont(cont)->use_hierarchy;
2457}
2458
2459static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
2460 u64 val)
2461{
2462 int retval = 0;
2463 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2464 struct cgroup *parent = cont->parent;
2465 struct mem_cgroup *parent_mem = NULL;
2466
2467 if (parent)
2468 parent_mem = mem_cgroup_from_cont(parent);
2469
2470 cgroup_lock();
2471 /*
2472 * If parent's use_hiearchy is set, we can't make any modifications
2473 * in the child subtrees. If it is unset, then the change can
2474 * occur, provided the current cgroup has no children.
2475 *
2476 * For the root cgroup, parent_mem is NULL, we allow value to be
2477 * set if there are no children.
2478 */
2479 if ((!parent_mem || !parent_mem->use_hierarchy) &&
2480 (val == 1 || val == 0)) {
2481 if (list_empty(&cont->children))
2482 mem->use_hierarchy = val;
2483 else
2484 retval = -EBUSY;
2485 } else
2486 retval = -EINVAL;
2487 cgroup_unlock();
2488
2489 return retval;
2490}
2491
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002492struct mem_cgroup_idx_data {
2493 s64 val;
2494 enum mem_cgroup_stat_index idx;
2495};
2496
2497static int
2498mem_cgroup_get_idx_stat(struct mem_cgroup *mem, void *data)
2499{
2500 struct mem_cgroup_idx_data *d = data;
2501 d->val += mem_cgroup_read_stat(&mem->stat, d->idx);
2502 return 0;
2503}
2504
2505static void
2506mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem,
2507 enum mem_cgroup_stat_index idx, s64 *val)
2508{
2509 struct mem_cgroup_idx_data d;
2510 d.idx = idx;
2511 d.val = 0;
2512 mem_cgroup_walk_tree(mem, &d, mem_cgroup_get_idx_stat);
2513 *val = d.val;
2514}
2515
Paul Menage2c3daa72008-04-29 00:59:58 -07002516static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002517{
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002518 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002519 u64 idx_val, val;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002520 int type, name;
2521
2522 type = MEMFILE_TYPE(cft->private);
2523 name = MEMFILE_ATTR(cft->private);
2524 switch (type) {
2525 case _MEM:
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002526 if (name == RES_USAGE && mem_cgroup_is_root(mem)) {
2527 mem_cgroup_get_recursive_idx_stat(mem,
2528 MEM_CGROUP_STAT_CACHE, &idx_val);
2529 val = idx_val;
2530 mem_cgroup_get_recursive_idx_stat(mem,
2531 MEM_CGROUP_STAT_RSS, &idx_val);
2532 val += idx_val;
2533 val <<= PAGE_SHIFT;
2534 } else
2535 val = res_counter_read_u64(&mem->res, name);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002536 break;
2537 case _MEMSWAP:
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002538 if (name == RES_USAGE && mem_cgroup_is_root(mem)) {
2539 mem_cgroup_get_recursive_idx_stat(mem,
2540 MEM_CGROUP_STAT_CACHE, &idx_val);
2541 val = idx_val;
2542 mem_cgroup_get_recursive_idx_stat(mem,
2543 MEM_CGROUP_STAT_RSS, &idx_val);
2544 val += idx_val;
2545 mem_cgroup_get_recursive_idx_stat(mem,
2546 MEM_CGROUP_STAT_SWAPOUT, &idx_val);
Kirill A. Shutemov3dac78c2009-12-15 16:47:01 -08002547 val += idx_val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002548 val <<= PAGE_SHIFT;
2549 } else
2550 val = res_counter_read_u64(&mem->memsw, name);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002551 break;
2552 default:
2553 BUG();
2554 break;
2555 }
2556 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002557}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002558/*
2559 * The user of this function is...
2560 * RES_LIMIT.
2561 */
Paul Menage856c13a2008-07-25 01:47:04 -07002562static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
2563 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002564{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002565 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002566 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002567 unsigned long long val;
2568 int ret;
2569
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002570 type = MEMFILE_TYPE(cft->private);
2571 name = MEMFILE_ATTR(cft->private);
2572 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002573 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07002574 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
2575 ret = -EINVAL;
2576 break;
2577 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002578 /* This function does all necessary parse...reuse it */
2579 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002580 if (ret)
2581 break;
2582 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002583 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002584 else
2585 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002586 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07002587 case RES_SOFT_LIMIT:
2588 ret = res_counter_memparse_write_strategy(buffer, &val);
2589 if (ret)
2590 break;
2591 /*
2592 * For memsw, soft limits are hard to implement in terms
2593 * of semantics, for now, we support soft limits for
2594 * control without swap
2595 */
2596 if (type == _MEM)
2597 ret = res_counter_set_soft_limit(&memcg->res, val);
2598 else
2599 ret = -EINVAL;
2600 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002601 default:
2602 ret = -EINVAL; /* should be BUG() ? */
2603 break;
2604 }
2605 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002606}
2607
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08002608static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
2609 unsigned long long *mem_limit, unsigned long long *memsw_limit)
2610{
2611 struct cgroup *cgroup;
2612 unsigned long long min_limit, min_memsw_limit, tmp;
2613
2614 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2615 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2616 cgroup = memcg->css.cgroup;
2617 if (!memcg->use_hierarchy)
2618 goto out;
2619
2620 while (cgroup->parent) {
2621 cgroup = cgroup->parent;
2622 memcg = mem_cgroup_from_cont(cgroup);
2623 if (!memcg->use_hierarchy)
2624 break;
2625 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
2626 min_limit = min(min_limit, tmp);
2627 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2628 min_memsw_limit = min(min_memsw_limit, tmp);
2629 }
2630out:
2631 *mem_limit = min_limit;
2632 *memsw_limit = min_memsw_limit;
2633 return;
2634}
2635
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002636static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002637{
2638 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002639 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002640
2641 mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002642 type = MEMFILE_TYPE(event);
2643 name = MEMFILE_ATTR(event);
2644 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002645 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002646 if (type == _MEM)
2647 res_counter_reset_max(&mem->res);
2648 else
2649 res_counter_reset_max(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002650 break;
2651 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002652 if (type == _MEM)
2653 res_counter_reset_failcnt(&mem->res);
2654 else
2655 res_counter_reset_failcnt(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002656 break;
2657 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07002658
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07002659 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002660}
2661
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002662
2663/* For read statistics */
2664enum {
2665 MCS_CACHE,
2666 MCS_RSS,
Balbir Singhd69b0422009-06-17 16:26:34 -07002667 MCS_MAPPED_FILE,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002668 MCS_PGPGIN,
2669 MCS_PGPGOUT,
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002670 MCS_SWAP,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002671 MCS_INACTIVE_ANON,
2672 MCS_ACTIVE_ANON,
2673 MCS_INACTIVE_FILE,
2674 MCS_ACTIVE_FILE,
2675 MCS_UNEVICTABLE,
2676 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002677};
2678
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002679struct mcs_total_stat {
2680 s64 stat[NR_MCS_STAT];
2681};
2682
2683struct {
2684 char *local_name;
2685 char *total_name;
2686} memcg_stat_strings[NR_MCS_STAT] = {
2687 {"cache", "total_cache"},
2688 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07002689 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002690 {"pgpgin", "total_pgpgin"},
2691 {"pgpgout", "total_pgpgout"},
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002692 {"swap", "total_swap"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002693 {"inactive_anon", "total_inactive_anon"},
2694 {"active_anon", "total_active_anon"},
2695 {"inactive_file", "total_inactive_file"},
2696 {"active_file", "total_active_file"},
2697 {"unevictable", "total_unevictable"}
2698};
2699
2700
2701static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
2702{
2703 struct mcs_total_stat *s = data;
2704 s64 val;
2705
2706 /* per cpu stat */
2707 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE);
2708 s->stat[MCS_CACHE] += val * PAGE_SIZE;
2709 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
2710 s->stat[MCS_RSS] += val * PAGE_SIZE;
Balbir Singhd69b0422009-06-17 16:26:34 -07002711 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_MAPPED_FILE);
2712 s->stat[MCS_MAPPED_FILE] += val * PAGE_SIZE;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002713 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT);
2714 s->stat[MCS_PGPGIN] += val;
2715 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT);
2716 s->stat[MCS_PGPGOUT] += val;
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002717 if (do_swap_account) {
2718 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_SWAPOUT);
2719 s->stat[MCS_SWAP] += val * PAGE_SIZE;
2720 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002721
2722 /* per zone stat */
2723 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
2724 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
2725 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
2726 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
2727 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
2728 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
2729 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
2730 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
2731 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
2732 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
2733 return 0;
2734}
2735
2736static void
2737mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
2738{
2739 mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
2740}
2741
Paul Menagec64745c2008-04-29 01:00:02 -07002742static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
2743 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002744{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002745 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002746 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002747 int i;
2748
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002749 memset(&mystat, 0, sizeof(mystat));
2750 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002751
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002752 for (i = 0; i < NR_MCS_STAT; i++) {
2753 if (i == MCS_SWAP && !do_swap_account)
2754 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002755 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002756 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002757
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002758 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08002759 {
2760 unsigned long long limit, memsw_limit;
2761 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
2762 cb->fill(cb, "hierarchical_memory_limit", limit);
2763 if (do_swap_account)
2764 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
2765 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002766
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002767 memset(&mystat, 0, sizeof(mystat));
2768 mem_cgroup_get_total_stat(mem_cont, &mystat);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002769 for (i = 0; i < NR_MCS_STAT; i++) {
2770 if (i == MCS_SWAP && !do_swap_account)
2771 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002772 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002773 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002774
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002775#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08002776 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002777
2778 {
2779 int nid, zid;
2780 struct mem_cgroup_per_zone *mz;
2781 unsigned long recent_rotated[2] = {0, 0};
2782 unsigned long recent_scanned[2] = {0, 0};
2783
2784 for_each_online_node(nid)
2785 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
2786 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
2787
2788 recent_rotated[0] +=
2789 mz->reclaim_stat.recent_rotated[0];
2790 recent_rotated[1] +=
2791 mz->reclaim_stat.recent_rotated[1];
2792 recent_scanned[0] +=
2793 mz->reclaim_stat.recent_scanned[0];
2794 recent_scanned[1] +=
2795 mz->reclaim_stat.recent_scanned[1];
2796 }
2797 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
2798 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
2799 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
2800 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
2801 }
2802#endif
2803
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002804 return 0;
2805}
2806
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002807static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
2808{
2809 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2810
2811 return get_swappiness(memcg);
2812}
2813
2814static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
2815 u64 val)
2816{
2817 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2818 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08002819
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002820 if (val > 100)
2821 return -EINVAL;
2822
2823 if (cgrp->parent == NULL)
2824 return -EINVAL;
2825
2826 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08002827
2828 cgroup_lock();
2829
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002830 /* If under hierarchy, only empty-root can set this value */
2831 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08002832 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
2833 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002834 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08002835 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002836
2837 spin_lock(&memcg->reclaim_param_lock);
2838 memcg->swappiness = val;
2839 spin_unlock(&memcg->reclaim_param_lock);
2840
Li Zefan068b38c2009-01-15 13:51:26 -08002841 cgroup_unlock();
2842
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002843 return 0;
2844}
2845
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002846
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002847static struct cftype mem_cgroup_files[] = {
2848 {
Balbir Singh0eea1032008-02-07 00:13:57 -08002849 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002850 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07002851 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002852 },
2853 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002854 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002855 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002856 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002857 .read_u64 = mem_cgroup_read,
2858 },
2859 {
Balbir Singh0eea1032008-02-07 00:13:57 -08002860 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002861 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07002862 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07002863 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002864 },
2865 {
Balbir Singh296c81d2009-09-23 15:56:36 -07002866 .name = "soft_limit_in_bytes",
2867 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
2868 .write_string = mem_cgroup_write,
2869 .read_u64 = mem_cgroup_read,
2870 },
2871 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002872 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002873 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002874 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07002875 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002876 },
Balbir Singh8697d332008-02-07 00:13:59 -08002877 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002878 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07002879 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002880 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002881 {
2882 .name = "force_empty",
2883 .trigger = mem_cgroup_force_empty_write,
2884 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08002885 {
2886 .name = "use_hierarchy",
2887 .write_u64 = mem_cgroup_hierarchy_write,
2888 .read_u64 = mem_cgroup_hierarchy_read,
2889 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002890 {
2891 .name = "swappiness",
2892 .read_u64 = mem_cgroup_swappiness_read,
2893 .write_u64 = mem_cgroup_swappiness_write,
2894 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002895};
2896
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002897#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2898static struct cftype memsw_cgroup_files[] = {
2899 {
2900 .name = "memsw.usage_in_bytes",
2901 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
2902 .read_u64 = mem_cgroup_read,
2903 },
2904 {
2905 .name = "memsw.max_usage_in_bytes",
2906 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
2907 .trigger = mem_cgroup_reset,
2908 .read_u64 = mem_cgroup_read,
2909 },
2910 {
2911 .name = "memsw.limit_in_bytes",
2912 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
2913 .write_string = mem_cgroup_write,
2914 .read_u64 = mem_cgroup_read,
2915 },
2916 {
2917 .name = "memsw.failcnt",
2918 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
2919 .trigger = mem_cgroup_reset,
2920 .read_u64 = mem_cgroup_read,
2921 },
2922};
2923
2924static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2925{
2926 if (!do_swap_account)
2927 return 0;
2928 return cgroup_add_files(cont, ss, memsw_cgroup_files,
2929 ARRAY_SIZE(memsw_cgroup_files));
2930};
2931#else
2932static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2933{
2934 return 0;
2935}
2936#endif
2937
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002938static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2939{
2940 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002941 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07002942 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07002943 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002944 /*
2945 * This routine is called against possible nodes.
2946 * But it's BUG to call kmalloc() against offline node.
2947 *
2948 * TODO: this routine can waste much memory for nodes which will
2949 * never be onlined. It's better to use memory hotplug callback
2950 * function.
2951 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07002952 if (!node_state(node, N_NORMAL_MEMORY))
2953 tmp = -1;
2954 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002955 if (!pn)
2956 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002957
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002958 mem->info.nodeinfo[node] = pn;
2959 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002960
2961 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
2962 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07002963 for_each_lru(l)
2964 INIT_LIST_HEAD(&mz->lists[l]);
Balbir Singhf64c3f52009-09-23 15:56:37 -07002965 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07002966 mz->on_tree = false;
2967 mz->mem = mem;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002968 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002969 return 0;
2970}
2971
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002972static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2973{
2974 kfree(mem->info.nodeinfo[node]);
2975}
2976
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002977static int mem_cgroup_size(void)
2978{
2979 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
2980 return sizeof(struct mem_cgroup) + cpustat_size;
2981}
2982
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002983static struct mem_cgroup *mem_cgroup_alloc(void)
2984{
2985 struct mem_cgroup *mem;
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002986 int size = mem_cgroup_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002987
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002988 if (size < PAGE_SIZE)
2989 mem = kmalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002990 else
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002991 mem = vmalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002992
2993 if (mem)
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002994 memset(mem, 0, size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002995 return mem;
2996}
2997
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002998/*
2999 * At destroying mem_cgroup, references from swap_cgroup can remain.
3000 * (scanning all at force_empty is too costly...)
3001 *
3002 * Instead of clearing all references at force_empty, we remember
3003 * the number of reference from swap_cgroup and free mem_cgroup when
3004 * it goes down to 0.
3005 *
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003006 * Removal of cgroup itself succeeds regardless of refs from swap.
3007 */
3008
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003009static void __mem_cgroup_free(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003010{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003011 int node;
3012
Balbir Singhf64c3f52009-09-23 15:56:37 -07003013 mem_cgroup_remove_from_trees(mem);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003014 free_css_id(&mem_cgroup_subsys, &mem->css);
3015
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003016 for_each_node_state(node, N_POSSIBLE)
3017 free_mem_cgroup_per_zone_info(mem, node);
3018
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003019 if (mem_cgroup_size() < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003020 kfree(mem);
3021 else
3022 vfree(mem);
3023}
3024
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003025static void mem_cgroup_get(struct mem_cgroup *mem)
3026{
3027 atomic_inc(&mem->refcnt);
3028}
3029
3030static void mem_cgroup_put(struct mem_cgroup *mem)
3031{
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003032 if (atomic_dec_and_test(&mem->refcnt)) {
3033 struct mem_cgroup *parent = parent_mem_cgroup(mem);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003034 __mem_cgroup_free(mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003035 if (parent)
3036 mem_cgroup_put(parent);
3037 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003038}
3039
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003040/*
3041 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
3042 */
3043static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
3044{
3045 if (!mem->res.parent)
3046 return NULL;
3047 return mem_cgroup_from_res_counter(mem->res.parent, res);
3048}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003049
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003050#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3051static void __init enable_swap_cgroup(void)
3052{
Hirokazu Takahashif8d66542009-01-07 18:08:02 -08003053 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003054 do_swap_account = 1;
3055}
3056#else
3057static void __init enable_swap_cgroup(void)
3058{
3059}
3060#endif
3061
Balbir Singhf64c3f52009-09-23 15:56:37 -07003062static int mem_cgroup_soft_limit_tree_init(void)
3063{
3064 struct mem_cgroup_tree_per_node *rtpn;
3065 struct mem_cgroup_tree_per_zone *rtpz;
3066 int tmp, node, zone;
3067
3068 for_each_node_state(node, N_POSSIBLE) {
3069 tmp = node;
3070 if (!node_state(node, N_NORMAL_MEMORY))
3071 tmp = -1;
3072 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
3073 if (!rtpn)
3074 return 1;
3075
3076 soft_limit_tree.rb_tree_per_node[node] = rtpn;
3077
3078 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
3079 rtpz = &rtpn->rb_tree_per_zone[zone];
3080 rtpz->rb_root = RB_ROOT;
3081 spin_lock_init(&rtpz->lock);
3082 }
3083 }
3084 return 0;
3085}
3086
Li Zefan0eb253e2009-01-15 13:51:25 -08003087static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003088mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
3089{
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003090 struct mem_cgroup *mem, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003091 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003092 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003093
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003094 mem = mem_cgroup_alloc();
3095 if (!mem)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003096 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08003097
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003098 for_each_node_state(node, N_POSSIBLE)
3099 if (alloc_mem_cgroup_per_zone_info(mem, node))
3100 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07003101
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003102 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003103 if (cont->parent == NULL) {
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003104 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003105 parent = NULL;
Balbir Singh4b3bde42009-09-23 15:56:32 -07003106 root_mem_cgroup = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07003107 if (mem_cgroup_soft_limit_tree_init())
3108 goto free_out;
3109
Balbir Singh18f59ea2009-01-07 18:08:07 -08003110 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003111 parent = mem_cgroup_from_cont(cont->parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003112 mem->use_hierarchy = parent->use_hierarchy;
3113 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003114
Balbir Singh18f59ea2009-01-07 18:08:07 -08003115 if (parent && parent->use_hierarchy) {
3116 res_counter_init(&mem->res, &parent->res);
3117 res_counter_init(&mem->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003118 /*
3119 * We increment refcnt of the parent to ensure that we can
3120 * safely access it on res_counter_charge/uncharge.
3121 * This refcnt will be decremented when freeing this
3122 * mem_cgroup(see mem_cgroup_put).
3123 */
3124 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003125 } else {
3126 res_counter_init(&mem->res, NULL);
3127 res_counter_init(&mem->memsw, NULL);
3128 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003129 mem->last_scanned_child = 0;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -08003130 spin_lock_init(&mem->reclaim_param_lock);
Balbir Singh6d61ef42009-01-07 18:08:06 -08003131
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003132 if (parent)
3133 mem->swappiness = get_swappiness(parent);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003134 atomic_set(&mem->refcnt, 1);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003135 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003136free_out:
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003137 __mem_cgroup_free(mem);
Balbir Singh4b3bde42009-09-23 15:56:32 -07003138 root_mem_cgroup = NULL;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003139 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003140}
3141
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003142static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08003143 struct cgroup *cont)
3144{
3145 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003146
3147 return mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08003148}
3149
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003150static void mem_cgroup_destroy(struct cgroup_subsys *ss,
3151 struct cgroup *cont)
3152{
Daisuke Nishimurac268e992009-01-15 13:51:13 -08003153 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08003154
Daisuke Nishimurac268e992009-01-15 13:51:13 -08003155 mem_cgroup_put(mem);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003156}
3157
3158static int mem_cgroup_populate(struct cgroup_subsys *ss,
3159 struct cgroup *cont)
3160{
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003161 int ret;
3162
3163 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
3164 ARRAY_SIZE(mem_cgroup_files));
3165
3166 if (!ret)
3167 ret = register_memsw_files(cont, ss);
3168 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003169}
3170
Balbir Singh67e465a2008-02-07 00:13:54 -08003171static void mem_cgroup_move_task(struct cgroup_subsys *ss,
3172 struct cgroup *cont,
3173 struct cgroup *old_cont,
Ben Blumbe367d02009-09-23 15:56:31 -07003174 struct task_struct *p,
3175 bool threadgroup)
Balbir Singh67e465a2008-02-07 00:13:54 -08003176{
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08003177 mutex_lock(&memcg_tasklist);
Balbir Singh67e465a2008-02-07 00:13:54 -08003178 /*
Nikanth Karthikesanf9717d22009-01-07 18:08:11 -08003179 * FIXME: It's better to move charges of this process from old
3180 * memcg to new memcg. But it's just on TODO-List now.
Balbir Singh67e465a2008-02-07 00:13:54 -08003181 */
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08003182 mutex_unlock(&memcg_tasklist);
Balbir Singh67e465a2008-02-07 00:13:54 -08003183}
3184
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003185struct cgroup_subsys mem_cgroup_subsys = {
3186 .name = "memory",
3187 .subsys_id = mem_cgroup_subsys_id,
3188 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08003189 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003190 .destroy = mem_cgroup_destroy,
3191 .populate = mem_cgroup_populate,
Balbir Singh67e465a2008-02-07 00:13:54 -08003192 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003193 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003194 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003195};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003196
3197#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3198
3199static int __init disable_swap_account(char *s)
3200{
3201 really_do_swap_account = 0;
3202 return 1;
3203}
3204__setup("noswapaccount", disable_swap_account);
3205#endif