blob: 9eee80d6d490b93e15f6a87ed568250f81b51b5b [file] [log] [blame]
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001/* memcontrol.c - Memory Controller
2 *
3 * Copyright IBM Corporation, 2007
4 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
5 *
Pavel Emelianov78fb7462008-02-07 00:13:51 -08006 * Copyright 2007 OpenVZ SWsoft Inc
7 * Author: Pavel Emelianov <xemul@openvz.org>
8 *
Balbir Singh8cdea7c2008-02-07 00:13:50 -08009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 */
19
20#include <linux/res_counter.h>
21#include <linux/memcontrol.h>
22#include <linux/cgroup.h>
Pavel Emelianov78fb7462008-02-07 00:13:51 -080023#include <linux/mm.h>
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -080024#include <linux/pagemap.h>
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080025#include <linux/smp.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080026#include <linux/page-flags.h>
Balbir Singh66e17072008-02-07 00:13:56 -080027#include <linux/backing-dev.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080028#include <linux/bit_spinlock.h>
29#include <linux/rcupdate.h>
Balbir Singhe2224322009-04-02 16:57:39 -070030#include <linux/limits.h>
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -080031#include <linux/mutex.h>
Balbir 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 Hiroyuki072c56c12008-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 Hiroyuki8c7c6e342009-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 Hiroyuki072c56c12008-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 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200212 * While reclaiming in a hierarchy, 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 Hiroyuki8c7c6e342009-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 Hiroyuki8c7c6e342009-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 Hiroyuki8c7c6e342009-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 Hiroyuki8c7c6e342009-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
Wu Fengguangd3242362009-12-16 12:19:59 +0100285struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *mem)
286{
287 return &mem->css;
288}
289
Balbir Singhf64c3f52009-09-23 15:56:37 -0700290static struct mem_cgroup_per_zone *
291page_cgroup_zoneinfo(struct page_cgroup *pc)
292{
293 struct mem_cgroup *mem = pc->mem_cgroup;
294 int nid = page_cgroup_nid(pc);
295 int zid = page_cgroup_zid(pc);
296
297 if (!mem)
298 return NULL;
299
300 return mem_cgroup_zoneinfo(mem, nid, zid);
301}
302
303static struct mem_cgroup_tree_per_zone *
304soft_limit_tree_node_zone(int nid, int zid)
305{
306 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
307}
308
309static struct mem_cgroup_tree_per_zone *
310soft_limit_tree_from_page(struct page *page)
311{
312 int nid = page_to_nid(page);
313 int zid = page_zonenum(page);
314
315 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
316}
317
318static void
Balbir Singh4e416952009-09-23 15:56:39 -0700319__mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700320 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700321 struct mem_cgroup_tree_per_zone *mctz,
322 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700323{
324 struct rb_node **p = &mctz->rb_root.rb_node;
325 struct rb_node *parent = NULL;
326 struct mem_cgroup_per_zone *mz_node;
327
328 if (mz->on_tree)
329 return;
330
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700331 mz->usage_in_excess = new_usage_in_excess;
332 if (!mz->usage_in_excess)
333 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700334 while (*p) {
335 parent = *p;
336 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
337 tree_node);
338 if (mz->usage_in_excess < mz_node->usage_in_excess)
339 p = &(*p)->rb_left;
340 /*
341 * We can't avoid mem cgroups that are over their soft
342 * limit by the same amount
343 */
344 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
345 p = &(*p)->rb_right;
346 }
347 rb_link_node(&mz->tree_node, parent, p);
348 rb_insert_color(&mz->tree_node, &mctz->rb_root);
349 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700350}
351
352static void
353__mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
354 struct mem_cgroup_per_zone *mz,
355 struct mem_cgroup_tree_per_zone *mctz)
356{
357 if (!mz->on_tree)
358 return;
359 rb_erase(&mz->tree_node, &mctz->rb_root);
360 mz->on_tree = false;
361}
362
363static void
Balbir Singhf64c3f52009-09-23 15:56:37 -0700364mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
365 struct mem_cgroup_per_zone *mz,
366 struct mem_cgroup_tree_per_zone *mctz)
367{
368 spin_lock(&mctz->lock);
Balbir Singh4e416952009-09-23 15:56:39 -0700369 __mem_cgroup_remove_exceeded(mem, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700370 spin_unlock(&mctz->lock);
371}
372
373static bool mem_cgroup_soft_limit_check(struct mem_cgroup *mem)
374{
375 bool ret = false;
376 int cpu;
377 s64 val;
378 struct mem_cgroup_stat_cpu *cpustat;
379
380 cpu = get_cpu();
381 cpustat = &mem->stat.cpustat[cpu];
382 val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_EVENTS);
383 if (unlikely(val > SOFTLIMIT_EVENTS_THRESH)) {
384 __mem_cgroup_stat_reset_safe(cpustat, MEM_CGROUP_STAT_EVENTS);
385 ret = true;
386 }
387 put_cpu();
388 return ret;
389}
390
391static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
392{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700393 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700394 struct mem_cgroup_per_zone *mz;
395 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700396 int nid = page_to_nid(page);
397 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700398 mctz = soft_limit_tree_from_page(page);
399
400 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700401 * Necessary to update all ancestors when hierarchy is used.
402 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700403 */
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700404 for (; mem; mem = parent_mem_cgroup(mem)) {
405 mz = mem_cgroup_zoneinfo(mem, nid, zid);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700406 excess = res_counter_soft_limit_excess(&mem->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700407 /*
408 * We have to update the tree if mz is on RB-tree or
409 * mem is over its softlimit.
410 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700411 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700412 spin_lock(&mctz->lock);
413 /* if on-tree, remove it */
414 if (mz->on_tree)
415 __mem_cgroup_remove_exceeded(mem, mz, mctz);
416 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700417 * Insert again. mz->usage_in_excess will be updated.
418 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700419 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700420 __mem_cgroup_insert_exceeded(mem, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700421 spin_unlock(&mctz->lock);
422 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700423 }
424}
425
426static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
427{
428 int node, zone;
429 struct mem_cgroup_per_zone *mz;
430 struct mem_cgroup_tree_per_zone *mctz;
431
432 for_each_node_state(node, N_POSSIBLE) {
433 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
434 mz = mem_cgroup_zoneinfo(mem, node, zone);
435 mctz = soft_limit_tree_node_zone(node, zone);
436 mem_cgroup_remove_exceeded(mem, mz, mctz);
437 }
438 }
439}
440
Balbir Singh4e416952009-09-23 15:56:39 -0700441static inline unsigned long mem_cgroup_get_excess(struct mem_cgroup *mem)
442{
443 return res_counter_soft_limit_excess(&mem->res) >> PAGE_SHIFT;
444}
445
446static struct mem_cgroup_per_zone *
447__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
448{
449 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700450 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700451
452retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700453 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700454 rightmost = rb_last(&mctz->rb_root);
455 if (!rightmost)
456 goto done; /* Nothing to reclaim from */
457
458 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
459 /*
460 * Remove the node now but someone else can add it back,
461 * we will to add it back at the end of reclaim to its correct
462 * position in the tree.
463 */
464 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
465 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
466 !css_tryget(&mz->mem->css))
467 goto retry;
468done:
469 return mz;
470}
471
472static struct mem_cgroup_per_zone *
473mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
474{
475 struct mem_cgroup_per_zone *mz;
476
477 spin_lock(&mctz->lock);
478 mz = __mem_cgroup_largest_soft_limit_node(mctz);
479 spin_unlock(&mctz->lock);
480 return mz;
481}
482
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700483static void mem_cgroup_swap_statistics(struct mem_cgroup *mem,
484 bool charge)
485{
486 int val = (charge) ? 1 : -1;
487 struct mem_cgroup_stat *stat = &mem->stat;
488 struct mem_cgroup_stat_cpu *cpustat;
489 int cpu = get_cpu();
490
491 cpustat = &stat->cpustat[cpu];
492 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_SWAPOUT, val);
493 put_cpu();
494}
495
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700496static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
497 struct page_cgroup *pc,
498 bool charge)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800499{
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700500 int val = (charge) ? 1 : -1;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800501 struct mem_cgroup_stat *stat = &mem->stat;
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700502 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800503 int cpu = get_cpu();
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800504
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800505 cpustat = &stat->cpustat[cpu];
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700506 if (PageCgroupCache(pc))
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700507 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800508 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700509 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700510
511 if (charge)
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700512 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700513 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
514 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700515 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700516 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700517 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_EVENTS, 1);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800518 put_cpu();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800519}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800520
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700521static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700522 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800523{
524 int nid, zid;
525 struct mem_cgroup_per_zone *mz;
526 u64 total = 0;
527
528 for_each_online_node(nid)
529 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
530 mz = mem_cgroup_zoneinfo(mem, nid, zid);
531 total += MEM_CGROUP_ZSTAT(mz, idx);
532 }
533 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800534}
535
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800536static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800537{
538 return container_of(cgroup_subsys_state(cont,
539 mem_cgroup_subsys_id), struct mem_cgroup,
540 css);
541}
542
Balbir Singhcf475ad2008-04-29 01:00:16 -0700543struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800544{
Balbir Singh31a78f22008-09-28 23:09:31 +0100545 /*
546 * mm_update_next_owner() may clear mm->owner to NULL
547 * if it races with swapoff, page migration, etc.
548 * So this can be called with p == NULL.
549 */
550 if (unlikely(!p))
551 return NULL;
552
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800553 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
554 struct mem_cgroup, css);
555}
556
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800557static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
558{
559 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700560
561 if (!mm)
562 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800563 /*
564 * Because we have no locks, mm->owner's may be being moved to other
565 * cgroup. We use css_tryget() here even if this looks
566 * pessimistic (rather than adding locks here).
567 */
568 rcu_read_lock();
569 do {
570 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
571 if (unlikely(!mem))
572 break;
573 } while (!css_tryget(&mem->css));
574 rcu_read_unlock();
575 return mem;
576}
577
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700578/*
579 * Call callback function against all cgroup under hierarchy tree.
580 */
581static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
582 int (*func)(struct mem_cgroup *, void *))
583{
584 int found, ret, nextid;
585 struct cgroup_subsys_state *css;
586 struct mem_cgroup *mem;
587
588 if (!root->use_hierarchy)
589 return (*func)(root, data);
590
591 nextid = 1;
592 do {
593 ret = 0;
594 mem = NULL;
595
596 rcu_read_lock();
597 css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
598 &found);
599 if (css && css_tryget(css))
600 mem = container_of(css, struct mem_cgroup, css);
601 rcu_read_unlock();
602
603 if (mem) {
604 ret = (*func)(mem, data);
605 css_put(&mem->css);
606 }
607 nextid = found + 1;
608 } while (!ret && css);
609
610 return ret;
611}
612
Balbir Singh4b3bde42009-09-23 15:56:32 -0700613static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
614{
615 return (mem == root_mem_cgroup);
616}
617
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800618/*
619 * Following LRU functions are allowed to be used without PCG_LOCK.
620 * Operations are called by routine of global LRU independently from memcg.
621 * What we have to take care of here is validness of pc->mem_cgroup.
622 *
623 * Changes to pc->mem_cgroup happens when
624 * 1. charge
625 * 2. moving account
626 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
627 * It is added to LRU before charge.
628 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
629 * When moving account, the page is not on LRU. It's isolated.
630 */
631
632void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800633{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800634 struct page_cgroup *pc;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800635 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700636
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800637 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800638 return;
639 pc = lookup_page_cgroup(page);
640 /* can happen while we handle swapcache. */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700641 if (!TestClearPageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800642 return;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700643 VM_BUG_ON(!pc->mem_cgroup);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800644 /*
645 * We don't check PCG_USED bit. It's cleared when the "page" is finally
646 * removed from global LRU.
647 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800648 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700649 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700650 if (mem_cgroup_is_root(pc->mem_cgroup))
651 return;
652 VM_BUG_ON(list_empty(&pc->lru));
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800653 list_del_init(&pc->lru);
654 return;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800655}
656
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800657void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800658{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800659 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800660}
661
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800662void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800663{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800664 struct mem_cgroup_per_zone *mz;
665 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800666
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800667 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700668 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700669
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800670 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800671 /*
672 * Used bit is set without atomic ops but after smp_wmb().
673 * For making pc->mem_cgroup visible, insert smp_rmb() here.
674 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800675 smp_rmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -0700676 /* unused or root page is not rotated. */
677 if (!PageCgroupUsed(pc) || mem_cgroup_is_root(pc->mem_cgroup))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800678 return;
679 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700680 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800681}
682
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800683void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
684{
685 struct page_cgroup *pc;
686 struct mem_cgroup_per_zone *mz;
687
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800688 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800689 return;
690 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700691 VM_BUG_ON(PageCgroupAcctLRU(pc));
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800692 /*
693 * Used bit is set without atomic ops but after smp_wmb().
694 * For making pc->mem_cgroup visible, insert smp_rmb() here.
695 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800696 smp_rmb();
697 if (!PageCgroupUsed(pc))
698 return;
699
700 mz = page_cgroup_zoneinfo(pc);
701 MEM_CGROUP_ZSTAT(mz, lru) += 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700702 SetPageCgroupAcctLRU(pc);
703 if (mem_cgroup_is_root(pc->mem_cgroup))
704 return;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800705 list_add(&pc->lru, &mz->lists[lru]);
706}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800707
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800708/*
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800709 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
710 * lru because the page may.be reused after it's fully uncharged (because of
711 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
712 * it again. This function is only used to charge SwapCache. It's done under
713 * lock_page and expected that zone->lru_lock is never held.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800714 */
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800715static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800716{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800717 unsigned long flags;
718 struct zone *zone = page_zone(page);
719 struct page_cgroup *pc = lookup_page_cgroup(page);
720
721 spin_lock_irqsave(&zone->lru_lock, flags);
722 /*
723 * Forget old LRU when this page_cgroup is *not* used. This Used bit
724 * is guarded by lock_page() because the page is SwapCache.
725 */
726 if (!PageCgroupUsed(pc))
727 mem_cgroup_del_lru_list(page, page_lru(page));
728 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800729}
730
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800731static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
732{
733 unsigned long flags;
734 struct zone *zone = page_zone(page);
735 struct page_cgroup *pc = lookup_page_cgroup(page);
736
737 spin_lock_irqsave(&zone->lru_lock, flags);
738 /* link when the page is linked to LRU but page_cgroup isn't */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700739 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800740 mem_cgroup_add_lru_list(page, page_lru(page));
741 spin_unlock_irqrestore(&zone->lru_lock, flags);
742}
743
744
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800745void mem_cgroup_move_lists(struct page *page,
746 enum lru_list from, enum lru_list to)
747{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800748 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800749 return;
750 mem_cgroup_del_lru_list(page, from);
751 mem_cgroup_add_lru_list(page, to);
752}
753
David Rientjes4c4a2212008-02-07 00:14:06 -0800754int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
755{
756 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700757 struct mem_cgroup *curr = NULL;
David Rientjes4c4a2212008-02-07 00:14:06 -0800758
759 task_lock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700760 rcu_read_lock();
761 curr = try_get_mem_cgroup_from_mm(task->mm);
762 rcu_read_unlock();
David Rientjes4c4a2212008-02-07 00:14:06 -0800763 task_unlock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700764 if (!curr)
765 return 0;
766 if (curr->use_hierarchy)
767 ret = css_is_ancestor(&curr->css, &mem->css);
768 else
769 ret = (curr == mem);
770 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -0800771 return ret;
772}
773
Balbir Singh66e17072008-02-07 00:13:56 -0800774/*
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800775 * prev_priority control...this will be used in memory reclaim path.
776 */
777int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
778{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800779 int prev_priority;
780
781 spin_lock(&mem->reclaim_param_lock);
782 prev_priority = mem->prev_priority;
783 spin_unlock(&mem->reclaim_param_lock);
784
785 return prev_priority;
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800786}
787
788void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
789{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800790 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800791 if (priority < mem->prev_priority)
792 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800793 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800794}
795
796void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
797{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800798 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800799 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800800 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800801}
802
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800803static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800804{
805 unsigned long active;
806 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800807 unsigned long gb;
808 unsigned long inactive_ratio;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800809
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700810 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
811 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800812
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800813 gb = (inactive + active) >> (30 - PAGE_SHIFT);
814 if (gb)
815 inactive_ratio = int_sqrt(10 * gb);
816 else
817 inactive_ratio = 1;
818
819 if (present_pages) {
820 present_pages[0] = inactive;
821 present_pages[1] = active;
822 }
823
824 return inactive_ratio;
825}
826
827int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
828{
829 unsigned long active;
830 unsigned long inactive;
831 unsigned long present_pages[2];
832 unsigned long inactive_ratio;
833
834 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
835
836 inactive = present_pages[0];
837 active = present_pages[1];
838
839 if (inactive * inactive_ratio < active)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800840 return 1;
841
842 return 0;
843}
844
Rik van Riel56e49d22009-06-16 15:32:28 -0700845int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
846{
847 unsigned long active;
848 unsigned long inactive;
849
850 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
851 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
852
853 return (active > inactive);
854}
855
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800856unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
857 struct zone *zone,
858 enum lru_list lru)
859{
860 int nid = zone->zone_pgdat->node_id;
861 int zid = zone_idx(zone);
862 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
863
864 return MEM_CGROUP_ZSTAT(mz, lru);
865}
866
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800867struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
868 struct zone *zone)
869{
870 int nid = zone->zone_pgdat->node_id;
871 int zid = zone_idx(zone);
872 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
873
874 return &mz->reclaim_stat;
875}
876
877struct zone_reclaim_stat *
878mem_cgroup_get_reclaim_stat_from_page(struct page *page)
879{
880 struct page_cgroup *pc;
881 struct mem_cgroup_per_zone *mz;
882
883 if (mem_cgroup_disabled())
884 return NULL;
885
886 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800887 /*
888 * Used bit is set without atomic ops but after smp_wmb().
889 * For making pc->mem_cgroup visible, insert smp_rmb() here.
890 */
891 smp_rmb();
892 if (!PageCgroupUsed(pc))
893 return NULL;
894
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800895 mz = page_cgroup_zoneinfo(pc);
896 if (!mz)
897 return NULL;
898
899 return &mz->reclaim_stat;
900}
901
Balbir Singh66e17072008-02-07 00:13:56 -0800902unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
903 struct list_head *dst,
904 unsigned long *scanned, int order,
905 int mode, struct zone *z,
906 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700907 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -0800908{
909 unsigned long nr_taken = 0;
910 struct page *page;
911 unsigned long scan;
912 LIST_HEAD(pc_list);
913 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800914 struct page_cgroup *pc, *tmp;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800915 int nid = z->zone_pgdat->node_id;
916 int zid = zone_idx(z);
917 struct mem_cgroup_per_zone *mz;
Johannes Weinerb7c46d12009-09-21 17:02:56 -0700918 int lru = LRU_FILE * file + active;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700919 int ret;
Balbir Singh66e17072008-02-07 00:13:56 -0800920
Balbir Singhcf475ad2008-04-29 01:00:16 -0700921 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800922 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700923 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -0800924
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800925 scan = 0;
926 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -0800927 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800928 break;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800929
930 page = pc->page;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700931 if (unlikely(!PageCgroupUsed(pc)))
932 continue;
Hugh Dickins436c65412008-02-07 00:14:12 -0800933 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800934 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800935
Hugh Dickins436c65412008-02-07 00:14:12 -0800936 scan++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700937 ret = __isolate_lru_page(page, mode, file);
938 switch (ret) {
939 case 0:
Balbir Singh66e17072008-02-07 00:13:56 -0800940 list_move(&page->lru, dst);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700941 mem_cgroup_del_lru(page);
Balbir Singh66e17072008-02-07 00:13:56 -0800942 nr_taken++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700943 break;
944 case -EBUSY:
945 /* we don't affect global LRU but rotate in our LRU */
946 mem_cgroup_rotate_lru_list(page, page_lru(page));
947 break;
948 default:
949 break;
Balbir Singh66e17072008-02-07 00:13:56 -0800950 }
951 }
952
Balbir Singh66e17072008-02-07 00:13:56 -0800953 *scanned = scan;
954 return nr_taken;
955}
956
Balbir Singh6d61ef42009-01-07 18:08:06 -0800957#define mem_cgroup_from_res_counter(counter, member) \
958 container_of(counter, struct mem_cgroup, member)
959
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -0800960static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
961{
962 if (do_swap_account) {
963 if (res_counter_check_under_limit(&mem->res) &&
964 res_counter_check_under_limit(&mem->memsw))
965 return true;
966 } else
967 if (res_counter_check_under_limit(&mem->res))
968 return true;
969 return false;
970}
971
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800972static unsigned int get_swappiness(struct mem_cgroup *memcg)
973{
974 struct cgroup *cgrp = memcg->css.cgroup;
975 unsigned int swappiness;
976
977 /* root ? */
978 if (cgrp->parent == NULL)
979 return vm_swappiness;
980
981 spin_lock(&memcg->reclaim_param_lock);
982 swappiness = memcg->swappiness;
983 spin_unlock(&memcg->reclaim_param_lock);
984
985 return swappiness;
986}
987
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700988static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
989{
990 int *val = data;
991 (*val)++;
992 return 0;
993}
Balbir Singhe2224322009-04-02 16:57:39 -0700994
995/**
996 * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode.
997 * @memcg: The memory cgroup that went over limit
998 * @p: Task that is going to be killed
999 *
1000 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1001 * enabled
1002 */
1003void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1004{
1005 struct cgroup *task_cgrp;
1006 struct cgroup *mem_cgrp;
1007 /*
1008 * Need a buffer in BSS, can't rely on allocations. The code relies
1009 * on the assumption that OOM is serialized for memory controller.
1010 * If this assumption is broken, revisit this code.
1011 */
1012 static char memcg_name[PATH_MAX];
1013 int ret;
1014
1015 if (!memcg)
1016 return;
1017
1018
1019 rcu_read_lock();
1020
1021 mem_cgrp = memcg->css.cgroup;
1022 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1023
1024 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1025 if (ret < 0) {
1026 /*
1027 * Unfortunately, we are unable to convert to a useful name
1028 * But we'll still print out the usage information
1029 */
1030 rcu_read_unlock();
1031 goto done;
1032 }
1033 rcu_read_unlock();
1034
1035 printk(KERN_INFO "Task in %s killed", memcg_name);
1036
1037 rcu_read_lock();
1038 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1039 if (ret < 0) {
1040 rcu_read_unlock();
1041 goto done;
1042 }
1043 rcu_read_unlock();
1044
1045 /*
1046 * Continues from above, so we don't need an KERN_ level
1047 */
1048 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1049done:
1050
1051 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1052 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1053 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1054 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1055 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1056 "failcnt %llu\n",
1057 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1058 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1059 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1060}
1061
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001062/*
1063 * This function returns the number of memcg under hierarchy tree. Returns
1064 * 1(self count) if no children.
1065 */
1066static int mem_cgroup_count_children(struct mem_cgroup *mem)
1067{
1068 int num = 0;
1069 mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
1070 return num;
1071}
1072
Balbir Singh6d61ef42009-01-07 18:08:06 -08001073/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001074 * Visit the first child (need not be the first child as per the ordering
1075 * of the cgroup list, since we track last_scanned_child) of @mem and use
1076 * that to reclaim free pages from.
1077 */
1078static struct mem_cgroup *
1079mem_cgroup_select_victim(struct mem_cgroup *root_mem)
1080{
1081 struct mem_cgroup *ret = NULL;
1082 struct cgroup_subsys_state *css;
1083 int nextid, found;
1084
1085 if (!root_mem->use_hierarchy) {
1086 css_get(&root_mem->css);
1087 ret = root_mem;
1088 }
1089
1090 while (!ret) {
1091 rcu_read_lock();
1092 nextid = root_mem->last_scanned_child + 1;
1093 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
1094 &found);
1095 if (css && css_tryget(css))
1096 ret = container_of(css, struct mem_cgroup, css);
1097
1098 rcu_read_unlock();
1099 /* Updates scanning parameter */
1100 spin_lock(&root_mem->reclaim_param_lock);
1101 if (!css) {
1102 /* this means start scan from ID:1 */
1103 root_mem->last_scanned_child = 0;
1104 } else
1105 root_mem->last_scanned_child = found;
1106 spin_unlock(&root_mem->reclaim_param_lock);
1107 }
1108
1109 return ret;
1110}
1111
1112/*
1113 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1114 * we reclaimed from, so that we don't end up penalizing one child extensively
1115 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001116 *
1117 * root_mem is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001118 *
1119 * We give up and return to the caller when we visit root_mem twice.
1120 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001121 *
1122 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001123 */
1124static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
Balbir Singh4e416952009-09-23 15:56:39 -07001125 struct zone *zone,
Balbir Singh75822b42009-09-23 15:56:38 -07001126 gfp_t gfp_mask,
1127 unsigned long reclaim_options)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001128{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001129 struct mem_cgroup *victim;
1130 int ret, total = 0;
1131 int loop = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001132 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1133 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
Balbir Singh4e416952009-09-23 15:56:39 -07001134 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
1135 unsigned long excess = mem_cgroup_get_excess(root_mem);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001136
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001137 /* If memsw_is_minimum==1, swap-out is of-no-use. */
1138 if (root_mem->memsw_is_minimum)
1139 noswap = true;
1140
Balbir Singh4e416952009-09-23 15:56:39 -07001141 while (1) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001142 victim = mem_cgroup_select_victim(root_mem);
Balbir Singh4e416952009-09-23 15:56:39 -07001143 if (victim == root_mem) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001144 loop++;
Balbir Singh4e416952009-09-23 15:56:39 -07001145 if (loop >= 2) {
1146 /*
1147 * If we have not been able to reclaim
1148 * anything, it might because there are
1149 * no reclaimable pages under this hierarchy
1150 */
1151 if (!check_soft || !total) {
1152 css_put(&victim->css);
1153 break;
1154 }
1155 /*
1156 * We want to do more targetted reclaim.
1157 * excess >> 2 is not to excessive so as to
1158 * reclaim too much, nor too less that we keep
1159 * coming back to reclaim from this cgroup
1160 */
1161 if (total >= (excess >> 2) ||
1162 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1163 css_put(&victim->css);
1164 break;
1165 }
1166 }
1167 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001168 if (!mem_cgroup_local_usage(&victim->stat)) {
1169 /* this cgroup's local usage == 0 */
1170 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001171 continue;
1172 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001173 /* we use swappiness of local cgroup */
Balbir Singh4e416952009-09-23 15:56:39 -07001174 if (check_soft)
1175 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
1176 noswap, get_swappiness(victim), zone,
1177 zone->zone_pgdat->node_id);
1178 else
1179 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
1180 noswap, get_swappiness(victim));
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001181 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001182 /*
1183 * At shrinking usage, we can't check we should stop here or
1184 * reclaim more. It's depends on callers. last_scanned_child
1185 * will work enough for keeping fairness under tree.
1186 */
1187 if (shrink)
1188 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001189 total += ret;
Balbir Singh4e416952009-09-23 15:56:39 -07001190 if (check_soft) {
1191 if (res_counter_check_under_soft_limit(&root_mem->res))
1192 return total;
1193 } else if (mem_cgroup_check_under_limit(root_mem))
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001194 return 1 + total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001195 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001196 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001197}
1198
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001199bool mem_cgroup_oom_called(struct task_struct *task)
1200{
1201 bool ret = false;
1202 struct mem_cgroup *mem;
1203 struct mm_struct *mm;
1204
1205 rcu_read_lock();
1206 mm = task->mm;
1207 if (!mm)
1208 mm = &init_mm;
1209 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
1210 if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
1211 ret = true;
1212 rcu_read_unlock();
1213 return ret;
1214}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001215
1216static int record_last_oom_cb(struct mem_cgroup *mem, void *data)
1217{
1218 mem->last_oom_jiffies = jiffies;
1219 return 0;
1220}
1221
1222static void record_last_oom(struct mem_cgroup *mem)
1223{
1224 mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb);
1225}
1226
Balbir Singhd69b0422009-06-17 16:26:34 -07001227/*
1228 * Currently used to update mapped file statistics, but the routine can be
1229 * generalized to update other statistics as well.
1230 */
1231void mem_cgroup_update_mapped_file_stat(struct page *page, int val)
1232{
1233 struct mem_cgroup *mem;
1234 struct mem_cgroup_stat *stat;
1235 struct mem_cgroup_stat_cpu *cpustat;
1236 int cpu;
1237 struct page_cgroup *pc;
1238
1239 if (!page_is_file_cache(page))
1240 return;
1241
1242 pc = lookup_page_cgroup(page);
1243 if (unlikely(!pc))
1244 return;
1245
1246 lock_page_cgroup(pc);
1247 mem = pc->mem_cgroup;
1248 if (!mem)
1249 goto done;
1250
1251 if (!PageCgroupUsed(pc))
1252 goto done;
1253
1254 /*
1255 * Preemption is already disabled, we don't need get_cpu()
1256 */
1257 cpu = smp_processor_id();
1258 stat = &mem->stat;
1259 cpustat = &stat->cpustat[cpu];
1260
1261 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE, val);
1262done:
1263 unlock_page_cgroup(pc);
1264}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001265
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001266/*
1267 * Unlike exported interface, "oom" parameter is added. if oom==true,
1268 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001269 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001270static int __mem_cgroup_try_charge(struct mm_struct *mm,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001271 gfp_t gfp_mask, struct mem_cgroup **memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -07001272 bool oom, struct page *page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001273{
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001274 struct mem_cgroup *mem, *mem_over_limit;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001275 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001276 struct res_counter *fail_res;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001277
1278 if (unlikely(test_thread_flag(TIF_MEMDIE))) {
1279 /* Don't account this! */
1280 *memcg = NULL;
1281 return 0;
1282 }
1283
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001284 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08001285 * We always charge the cgroup the mm_struct belongs to.
1286 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001287 * thread group leader migrates. It's possible that mm is not
1288 * set, if so charge the init_mm (happens for pagecache usage).
1289 */
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001290 mem = *memcg;
1291 if (likely(!mem)) {
1292 mem = try_get_mem_cgroup_from_mm(mm);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001293 *memcg = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001294 } else {
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001295 css_get(&mem->css);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001296 }
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001297 if (unlikely(!mem))
1298 return 0;
1299
Nikanth Karthikesan46f7e602009-05-28 14:34:41 -07001300 VM_BUG_ON(css_is_removed(&mem->css));
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001301
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001302 while (1) {
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001303 int ret = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001304 unsigned long flags = 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001305
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001306 if (mem_cgroup_is_root(mem))
1307 goto done;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001308 ret = res_counter_charge(&mem->res, PAGE_SIZE, &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001309 if (likely(!ret)) {
1310 if (!do_swap_account)
1311 break;
Balbir Singh28dbc4b2009-01-07 18:08:05 -08001312 ret = res_counter_charge(&mem->memsw, PAGE_SIZE,
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001313 &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001314 if (likely(!ret))
1315 break;
1316 /* mem+swap counter fails */
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001317 res_counter_uncharge(&mem->res, PAGE_SIZE);
Balbir Singh75822b42009-09-23 15:56:38 -07001318 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001319 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1320 memsw);
1321 } else
1322 /* mem counter fails */
1323 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1324 res);
1325
Hugh Dickins3be91272008-02-07 00:14:19 -08001326 if (!(gfp_mask & __GFP_WAIT))
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001327 goto nomem;
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001328
Balbir Singh4e416952009-09-23 15:56:39 -07001329 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
1330 gfp_mask, flags);
Daisuke Nishimura4d1c6272009-01-15 13:51:14 -08001331 if (ret)
1332 continue;
Balbir Singh66e17072008-02-07 00:13:56 -08001333
1334 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001335 * try_to_free_mem_cgroup_pages() might not give us a full
1336 * picture of reclaim. Some pages are reclaimed and might be
1337 * moved to swap cache or just unmapped from the cgroup.
1338 * Check the limit again to see if the reclaim reduced the
1339 * current usage of the cgroup before giving up
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001340 *
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001341 */
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -08001342 if (mem_cgroup_check_under_limit(mem_over_limit))
1343 continue;
Hugh Dickins3be91272008-02-07 00:14:19 -08001344
1345 if (!nr_retries--) {
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001346 if (oom) {
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08001347 mutex_lock(&memcg_tasklist);
KAMEZAWA Hiroyuki88700752009-01-07 18:08:09 -08001348 mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08001349 mutex_unlock(&memcg_tasklist);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001350 record_last_oom(mem_over_limit);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001351 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001352 goto nomem;
Balbir Singh66e17072008-02-07 00:13:56 -08001353 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001354 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07001355 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001356 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
1357 * if they exceeds softlimit.
Balbir Singhf64c3f52009-09-23 15:56:37 -07001358 */
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001359 if (mem_cgroup_soft_limit_check(mem))
1360 mem_cgroup_update_tree(mem, page);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001361done:
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001362 return 0;
1363nomem:
1364 css_put(&mem->css);
1365 return -ENOMEM;
1366}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001367
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001368/*
1369 * A helper function to get mem_cgroup from ID. must be called under
1370 * rcu_read_lock(). The caller must check css_is_removed() or some if
1371 * it's concern. (dropping refcnt from swap can be called against removed
1372 * memcg.)
1373 */
1374static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
1375{
1376 struct cgroup_subsys_state *css;
1377
1378 /* ID 0 is unused ID */
1379 if (!id)
1380 return NULL;
1381 css = css_lookup(&mem_cgroup_subsys, id);
1382 if (!css)
1383 return NULL;
1384 return container_of(css, struct mem_cgroup, css);
1385}
1386
Wu Fengguange42d9d52009-12-16 12:19:59 +01001387struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001388{
Wu Fengguange42d9d52009-12-16 12:19:59 +01001389 struct mem_cgroup *mem = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001390 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001391 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001392 swp_entry_t ent;
1393
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001394 VM_BUG_ON(!PageLocked(page));
1395
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001396 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001397 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001398 if (PageCgroupUsed(pc)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001399 mem = pc->mem_cgroup;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001400 if (mem && !css_tryget(&mem->css))
1401 mem = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01001402 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001403 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001404 id = lookup_swap_cgroup(ent);
1405 rcu_read_lock();
1406 mem = mem_cgroup_lookup(id);
1407 if (mem && !css_tryget(&mem->css))
1408 mem = NULL;
1409 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001410 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001411 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001412 return mem;
1413}
1414
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001415/*
Daisuke Nishimuraa5e924f2009-01-07 18:08:28 -08001416 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001417 * USED state. If already USED, uncharge and return.
1418 */
1419
1420static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1421 struct page_cgroup *pc,
1422 enum charge_type ctype)
1423{
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001424 /* try_charge() can return NULL to *memcg, taking care of it. */
1425 if (!mem)
1426 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001427
1428 lock_page_cgroup(pc);
1429 if (unlikely(PageCgroupUsed(pc))) {
1430 unlock_page_cgroup(pc);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001431 if (!mem_cgroup_is_root(mem)) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001432 res_counter_uncharge(&mem->res, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001433 if (do_swap_account)
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001434 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001435 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001436 css_put(&mem->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001437 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001438 }
Balbir Singh4b3bde42009-09-23 15:56:32 -07001439
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001440 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07001441 /*
1442 * We access a page_cgroup asynchronously without lock_page_cgroup().
1443 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
1444 * is accessed after testing USED bit. To make pc->mem_cgroup visible
1445 * before USED bit, we need memory barrier here.
1446 * See mem_cgroup_add_lru_list(), etc.
1447 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001448 smp_wmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -07001449 switch (ctype) {
1450 case MEM_CGROUP_CHARGE_TYPE_CACHE:
1451 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
1452 SetPageCgroupCache(pc);
1453 SetPageCgroupUsed(pc);
1454 break;
1455 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1456 ClearPageCgroupCache(pc);
1457 SetPageCgroupUsed(pc);
1458 break;
1459 default:
1460 break;
1461 }
Hugh Dickins3be91272008-02-07 00:14:19 -08001462
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001463 mem_cgroup_charge_statistics(mem, pc, true);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001464
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001465 unlock_page_cgroup(pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001466}
1467
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001468/**
1469 * mem_cgroup_move_account - move account of the page
1470 * @pc: page_cgroup of the page.
1471 * @from: mem_cgroup which the page is moved from.
1472 * @to: mem_cgroup which the page is moved to. @from != @to.
1473 *
1474 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001475 * - page is not on LRU (isolate_page() is useful.)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001476 *
1477 * returns 0 at success,
1478 * returns -EBUSY when lock is busy or "pc" is unstable.
1479 *
1480 * This function does "uncharge" from old cgroup but doesn't do "charge" to
1481 * new cgroup. It should be done by a caller.
1482 */
1483
1484static int mem_cgroup_move_account(struct page_cgroup *pc,
1485 struct mem_cgroup *from, struct mem_cgroup *to)
1486{
1487 struct mem_cgroup_per_zone *from_mz, *to_mz;
1488 int nid, zid;
1489 int ret = -EBUSY;
Balbir Singhd69b0422009-06-17 16:26:34 -07001490 struct page *page;
1491 int cpu;
1492 struct mem_cgroup_stat *stat;
1493 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001494
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001495 VM_BUG_ON(from == to);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001496 VM_BUG_ON(PageLRU(pc->page));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001497
1498 nid = page_cgroup_nid(pc);
1499 zid = page_cgroup_zid(pc);
1500 from_mz = mem_cgroup_zoneinfo(from, nid, zid);
1501 to_mz = mem_cgroup_zoneinfo(to, nid, zid);
1502
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001503 if (!trylock_page_cgroup(pc))
1504 return ret;
1505
1506 if (!PageCgroupUsed(pc))
1507 goto out;
1508
1509 if (pc->mem_cgroup != from)
1510 goto out;
1511
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001512 if (!mem_cgroup_is_root(from))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001513 res_counter_uncharge(&from->res, PAGE_SIZE);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001514 mem_cgroup_charge_statistics(from, pc, false);
Balbir Singhd69b0422009-06-17 16:26:34 -07001515
1516 page = pc->page;
1517 if (page_is_file_cache(page) && page_mapped(page)) {
1518 cpu = smp_processor_id();
1519 /* Update mapped_file data for mem_cgroup "from" */
1520 stat = &from->stat;
1521 cpustat = &stat->cpustat[cpu];
1522 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1523 -1);
1524
1525 /* Update mapped_file data for mem_cgroup "to" */
1526 stat = &to->stat;
1527 cpustat = &stat->cpustat[cpu];
1528 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1529 1);
1530 }
1531
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001532 if (do_swap_account && !mem_cgroup_is_root(from))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001533 res_counter_uncharge(&from->memsw, PAGE_SIZE);
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001534 css_put(&from->css);
1535
1536 css_get(&to->css);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001537 pc->mem_cgroup = to;
1538 mem_cgroup_charge_statistics(to, pc, true);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001539 ret = 0;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001540out:
1541 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001542 /*
1543 * We charges against "to" which may not have any tasks. Then, "to"
1544 * can be under rmdir(). But in current implementation, caller of
1545 * this function is just force_empty() and it's garanteed that
1546 * "to" is never removed. So, we don't check rmdir status here.
1547 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001548 return ret;
1549}
1550
1551/*
1552 * move charges to its parent.
1553 */
1554
1555static int mem_cgroup_move_parent(struct page_cgroup *pc,
1556 struct mem_cgroup *child,
1557 gfp_t gfp_mask)
1558{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001559 struct page *page = pc->page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001560 struct cgroup *cg = child->css.cgroup;
1561 struct cgroup *pcg = cg->parent;
1562 struct mem_cgroup *parent;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001563 int ret;
1564
1565 /* Is ROOT ? */
1566 if (!pcg)
1567 return -EINVAL;
1568
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001569
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001570 parent = mem_cgroup_from_cont(pcg);
1571
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001572
Balbir Singhf64c3f52009-09-23 15:56:37 -07001573 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false, page);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001574 if (ret || !parent)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001575 return ret;
1576
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001577 if (!get_page_unless_zero(page)) {
1578 ret = -EBUSY;
1579 goto uncharge;
1580 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001581
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001582 ret = isolate_lru_page(page);
1583
1584 if (ret)
1585 goto cancel;
1586
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001587 ret = mem_cgroup_move_account(pc, child, parent);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001588
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001589 putback_lru_page(page);
1590 if (!ret) {
1591 put_page(page);
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001592 /* drop extra refcnt by try_charge() */
1593 css_put(&parent->css);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001594 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001595 }
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001596
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001597cancel:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001598 put_page(page);
1599uncharge:
1600 /* drop extra refcnt by try_charge() */
1601 css_put(&parent->css);
1602 /* uncharge if move fails */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001603 if (!mem_cgroup_is_root(parent)) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001604 res_counter_uncharge(&parent->res, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001605 if (do_swap_account)
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001606 res_counter_uncharge(&parent->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001607 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001608 return ret;
1609}
1610
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001611/*
1612 * Charge the memory controller for page usage.
1613 * Return
1614 * 0 if the charge was successful
1615 * < 0 if the cgroup is over its limit
1616 */
1617static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
1618 gfp_t gfp_mask, enum charge_type ctype,
1619 struct mem_cgroup *memcg)
1620{
1621 struct mem_cgroup *mem;
1622 struct page_cgroup *pc;
1623 int ret;
1624
1625 pc = lookup_page_cgroup(page);
1626 /* can happen at boot */
1627 if (unlikely(!pc))
1628 return 0;
1629 prefetchw(pc);
1630
1631 mem = memcg;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001632 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true, page);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001633 if (ret || !mem)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001634 return ret;
1635
1636 __mem_cgroup_commit_charge(mem, pc, ctype);
1637 return 0;
1638}
1639
1640int mem_cgroup_newpage_charge(struct page *page,
1641 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001642{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001643 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001644 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001645 if (PageCompound(page))
1646 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001647 /*
1648 * If already mapped, we don't have to account.
1649 * If page cache, page->mapping has address_space.
1650 * But page->mapping may have out-of-use anon_vma pointer,
1651 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
1652 * is NULL.
1653 */
1654 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
1655 return 0;
1656 if (unlikely(!mm))
1657 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001658 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001659 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001660}
1661
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001662static void
1663__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1664 enum charge_type ctype);
1665
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001666int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
1667 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08001668{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001669 struct mem_cgroup *mem = NULL;
1670 int ret;
1671
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001672 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001673 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001674 if (PageCompound(page))
1675 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001676 /*
1677 * Corner case handling. This is called from add_to_page_cache()
1678 * in usual. But some FS (shmem) precharges this page before calling it
1679 * and call add_to_page_cache() with GFP_NOWAIT.
1680 *
1681 * For GFP_NOWAIT case, the page may be pre-charged before calling
1682 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
1683 * charge twice. (It works but has to pay a bit larger cost.)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001684 * And when the page is SwapCache, it should take swap information
1685 * into account. This is under lock_page() now.
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001686 */
1687 if (!(gfp_mask & __GFP_WAIT)) {
1688 struct page_cgroup *pc;
1689
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001690
1691 pc = lookup_page_cgroup(page);
1692 if (!pc)
1693 return 0;
1694 lock_page_cgroup(pc);
1695 if (PageCgroupUsed(pc)) {
1696 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001697 return 0;
1698 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001699 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001700 }
1701
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001702 if (unlikely(!mm && !mem))
Balbir Singh8697d332008-02-07 00:13:59 -08001703 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001704
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -07001705 if (page_is_file_cache(page))
1706 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001707 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001708
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001709 /* shmem */
1710 if (PageSwapCache(page)) {
1711 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1712 if (!ret)
1713 __mem_cgroup_commit_charge_swapin(page, mem,
1714 MEM_CGROUP_CHARGE_TYPE_SHMEM);
1715 } else
1716 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
1717 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001718
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001719 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001720}
1721
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001722/*
1723 * While swap-in, try_charge -> commit or cancel, the page is locked.
1724 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02001725 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001726 * "commit()" or removed by "cancel()"
1727 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001728int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
1729 struct page *page,
1730 gfp_t mask, struct mem_cgroup **ptr)
1731{
1732 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001733 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001734
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001735 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001736 return 0;
1737
1738 if (!do_swap_account)
1739 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001740 /*
1741 * A racing thread's fault, or swapoff, may have already updated
Hugh Dickins407f9c82009-12-14 17:59:30 -08001742 * the pte, and even removed page from swap cache: in those cases
1743 * do_swap_page()'s pte_same() test will fail; but there's also a
1744 * KSM case which does need to charge the page.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001745 */
1746 if (!PageSwapCache(page))
Hugh Dickins407f9c82009-12-14 17:59:30 -08001747 goto charge_cur_mm;
Wu Fengguange42d9d52009-12-16 12:19:59 +01001748 mem = try_get_mem_cgroup_from_page(page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001749 if (!mem)
1750 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001751 *ptr = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001752 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true, page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001753 /* drop extra refcnt from tryget */
1754 css_put(&mem->css);
1755 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001756charge_cur_mm:
1757 if (unlikely(!mm))
1758 mm = &init_mm;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001759 return __mem_cgroup_try_charge(mm, mask, ptr, true, page);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001760}
1761
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001762static void
1763__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1764 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001765{
1766 struct page_cgroup *pc;
1767
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001768 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001769 return;
1770 if (!ptr)
1771 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001772 cgroup_exclude_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001773 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001774 mem_cgroup_lru_del_before_commit_swapcache(page);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001775 __mem_cgroup_commit_charge(ptr, pc, ctype);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001776 mem_cgroup_lru_add_after_commit_swapcache(page);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001777 /*
1778 * Now swap is on-memory. This means this page may be
1779 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08001780 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
1781 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
1782 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001783 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08001784 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001785 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001786 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001787 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001788
1789 id = swap_cgroup_record(ent, 0);
1790 rcu_read_lock();
1791 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001792 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001793 /*
1794 * This recorded memcg can be obsolete one. So, avoid
1795 * calling css_tryget
1796 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001797 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001798 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001799 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001800 mem_cgroup_put(memcg);
1801 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001802 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001803 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001804 /*
1805 * At swapin, we may charge account against cgroup which has no tasks.
1806 * So, rmdir()->pre_destroy() can be called while we do this charge.
1807 * In that case, we need to call pre_destroy() again. check it here.
1808 */
1809 cgroup_release_and_wakeup_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001810}
1811
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001812void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
1813{
1814 __mem_cgroup_commit_charge_swapin(page, ptr,
1815 MEM_CGROUP_CHARGE_TYPE_MAPPED);
1816}
1817
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001818void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
1819{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001820 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001821 return;
1822 if (!mem)
1823 return;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001824 if (!mem_cgroup_is_root(mem)) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001825 res_counter_uncharge(&mem->res, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001826 if (do_swap_account)
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001827 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001828 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001829 css_put(&mem->css);
1830}
1831
1832
Balbir Singh8697d332008-02-07 00:13:59 -08001833/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001834 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001835 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001836static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001837__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001838{
Hugh Dickins82895462008-03-04 14:29:08 -08001839 struct page_cgroup *pc;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001840 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001841 struct mem_cgroup_per_zone *mz;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001842
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001843 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001844 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07001845
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001846 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001847 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001848
Balbir Singh8697d332008-02-07 00:13:59 -08001849 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08001850 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08001851 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001852 pc = lookup_page_cgroup(page);
1853 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001854 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001855
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001856 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001857
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001858 mem = pc->mem_cgroup;
1859
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001860 if (!PageCgroupUsed(pc))
1861 goto unlock_out;
1862
1863 switch (ctype) {
1864 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07001865 case MEM_CGROUP_CHARGE_TYPE_DROP:
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001866 if (page_mapped(page))
1867 goto unlock_out;
1868 break;
1869 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
1870 if (!PageAnon(page)) { /* Shared memory */
1871 if (page->mapping && !page_is_file_cache(page))
1872 goto unlock_out;
1873 } else if (page_mapped(page)) /* Anon */
1874 goto unlock_out;
1875 break;
1876 default:
1877 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001878 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001879
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001880 if (!mem_cgroup_is_root(mem)) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001881 res_counter_uncharge(&mem->res, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001882 if (do_swap_account &&
1883 (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001884 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001885 }
1886 if (ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1887 mem_cgroup_swap_statistics(mem, true);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001888 mem_cgroup_charge_statistics(mem, pc, false);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001889
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001890 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001891 /*
1892 * pc->mem_cgroup is not cleared here. It will be accessed when it's
1893 * freed from LRU. This is safe because uncharged page is expected not
1894 * to be reused (freed soon). Exception is SwapCache, it's handled by
1895 * special functions.
1896 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08001897
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001898 mz = page_cgroup_zoneinfo(pc);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001899 unlock_page_cgroup(pc);
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08001900
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001901 if (mem_cgroup_soft_limit_check(mem))
Balbir Singhf64c3f52009-09-23 15:56:37 -07001902 mem_cgroup_update_tree(mem, page);
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08001903 /* at swapout, this memcg will be accessed to record to swap */
1904 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1905 css_put(&mem->css);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001906
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001907 return mem;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001908
1909unlock_out:
1910 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001911 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08001912}
1913
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001914void mem_cgroup_uncharge_page(struct page *page)
1915{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001916 /* early check. */
1917 if (page_mapped(page))
1918 return;
1919 if (page->mapping && !PageAnon(page))
1920 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001921 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
1922}
1923
1924void mem_cgroup_uncharge_cache_page(struct page *page)
1925{
1926 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07001927 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001928 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
1929}
1930
Daisuke Nishimurae767e052009-05-28 14:34:28 -07001931#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001932/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07001933 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001934 * memcg information is recorded to swap_cgroup of "ent"
1935 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07001936void
1937mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001938{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001939 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07001940 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001941
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07001942 if (!swapout) /* this was a swap cache but the swap is unused ! */
1943 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
1944
1945 memcg = __mem_cgroup_uncharge_common(page, ctype);
1946
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001947 /* record memcg information */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07001948 if (do_swap_account && swapout && memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001949 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001950 mem_cgroup_get(memcg);
1951 }
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07001952 if (swapout && memcg)
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08001953 css_put(&memcg->css);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001954}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07001955#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001956
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001957#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1958/*
1959 * called from swap_entry_free(). remove record in swap_cgroup and
1960 * uncharge "memsw" account.
1961 */
1962void mem_cgroup_uncharge_swap(swp_entry_t ent)
1963{
1964 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001965 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001966
1967 if (!do_swap_account)
1968 return;
1969
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001970 id = swap_cgroup_record(ent, 0);
1971 rcu_read_lock();
1972 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001973 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001974 /*
1975 * We uncharge this because swap is freed.
1976 * This memcg can be obsolete one. We avoid calling css_tryget
1977 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001978 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001979 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001980 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001981 mem_cgroup_put(memcg);
1982 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001983 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001984}
1985#endif
1986
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001987/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001988 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
1989 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001990 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001991int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001992{
1993 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001994 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001995 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001996
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001997 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07001998 return 0;
1999
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002000 pc = lookup_page_cgroup(page);
2001 lock_page_cgroup(pc);
2002 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002003 mem = pc->mem_cgroup;
2004 css_get(&mem->css);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08002005 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002006 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002007
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002008 if (mem) {
Balbir Singhf64c3f52009-09-23 15:56:37 -07002009 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false,
2010 page);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002011 css_put(&mem->css);
2012 }
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002013 *ptr = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002014 return ret;
2015}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08002016
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002017/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002018void mem_cgroup_end_migration(struct mem_cgroup *mem,
2019 struct page *oldpage, struct page *newpage)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002020{
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002021 struct page *target, *unused;
2022 struct page_cgroup *pc;
2023 enum charge_type ctype;
2024
2025 if (!mem)
2026 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002027 cgroup_exclude_rmdir(&mem->css);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002028 /* at migration success, oldpage->mapping is NULL. */
2029 if (oldpage->mapping) {
2030 target = oldpage;
2031 unused = NULL;
2032 } else {
2033 target = newpage;
2034 unused = oldpage;
2035 }
2036
2037 if (PageAnon(target))
2038 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
2039 else if (page_is_file_cache(target))
2040 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
2041 else
2042 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
2043
2044 /* unused page is not on radix-tree now. */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002045 if (unused)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002046 __mem_cgroup_uncharge_common(unused, ctype);
2047
2048 pc = lookup_page_cgroup(target);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002049 /*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002050 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
2051 * So, double-counting is effectively avoided.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002052 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002053 __mem_cgroup_commit_charge(mem, pc, ctype);
2054
2055 /*
2056 * Both of oldpage and newpage are still under lock_page().
2057 * Then, we don't have to care about race in radix-tree.
2058 * But we have to be careful that this page is unmapped or not.
2059 *
2060 * There is a case for !page_mapped(). At the start of
2061 * migration, oldpage was mapped. But now, it's zapped.
2062 * But we know *target* page is not freed/reused under us.
2063 * mem_cgroup_uncharge_page() does all necessary checks.
2064 */
2065 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
2066 mem_cgroup_uncharge_page(target);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002067 /*
2068 * At migration, we may charge account against cgroup which has no tasks
2069 * So, rmdir()->pre_destroy() can be called while we do this charge.
2070 * In that case, we need to call pre_destroy() again. check it here.
2071 */
2072 cgroup_release_and_wakeup_rmdir(&mem->css);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002073}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08002074
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002075/*
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002076 * A call to try to shrink memory usage on charge failure at shmem's swapin.
2077 * Calling hierarchical_reclaim is not enough because we should update
2078 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
2079 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
2080 * not from the memcg which this page would be charged to.
2081 * try_charge_swapin does all of these works properly.
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002082 */
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002083int mem_cgroup_shmem_charge_fallback(struct page *page,
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002084 struct mm_struct *mm,
2085 gfp_t gfp_mask)
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002086{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002087 struct mem_cgroup *mem = NULL;
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002088 int ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002089
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002090 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002091 return 0;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002092
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002093 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2094 if (!ret)
2095 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002096
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002097 return ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002098}
2099
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002100static DEFINE_MUTEX(set_limit_mutex);
2101
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08002102static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002103 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002104{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002105 int retry_count;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002106 int progress;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002107 u64 memswlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002108 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002109 int children = mem_cgroup_count_children(memcg);
2110 u64 curusage, oldusage;
2111
2112 /*
2113 * For keeping hierarchical_reclaim simple, how long we should retry
2114 * is depends on callers. We set our retry-count to be function
2115 * of # of children which we should visit in this loop.
2116 */
2117 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
2118
2119 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002120
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002121 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002122 if (signal_pending(current)) {
2123 ret = -EINTR;
2124 break;
2125 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002126 /*
2127 * Rather than hide all in some function, I do this in
2128 * open coded manner. You see what this really does.
2129 * We have to guarantee mem->res.limit < mem->memsw.limit.
2130 */
2131 mutex_lock(&set_limit_mutex);
2132 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2133 if (memswlimit < val) {
2134 ret = -EINVAL;
2135 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002136 break;
2137 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002138 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07002139 if (!ret) {
2140 if (memswlimit == val)
2141 memcg->memsw_is_minimum = true;
2142 else
2143 memcg->memsw_is_minimum = false;
2144 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002145 mutex_unlock(&set_limit_mutex);
2146
2147 if (!ret)
2148 break;
2149
Balbir Singh4e416952009-09-23 15:56:39 -07002150 progress = mem_cgroup_hierarchical_reclaim(memcg, NULL,
2151 GFP_KERNEL,
2152 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002153 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
2154 /* Usage is reduced ? */
2155 if (curusage >= oldusage)
2156 retry_count--;
2157 else
2158 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002159 }
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002160
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002161 return ret;
2162}
2163
Li Zefan338c8432009-06-17 16:27:15 -07002164static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
2165 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002166{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002167 int retry_count;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002168 u64 memlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002169 int children = mem_cgroup_count_children(memcg);
2170 int ret = -EBUSY;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002171
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002172 /* see mem_cgroup_resize_res_limit */
2173 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
2174 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002175 while (retry_count) {
2176 if (signal_pending(current)) {
2177 ret = -EINTR;
2178 break;
2179 }
2180 /*
2181 * Rather than hide all in some function, I do this in
2182 * open coded manner. You see what this really does.
2183 * We have to guarantee mem->res.limit < mem->memsw.limit.
2184 */
2185 mutex_lock(&set_limit_mutex);
2186 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2187 if (memlimit > val) {
2188 ret = -EINVAL;
2189 mutex_unlock(&set_limit_mutex);
2190 break;
2191 }
2192 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07002193 if (!ret) {
2194 if (memlimit == val)
2195 memcg->memsw_is_minimum = true;
2196 else
2197 memcg->memsw_is_minimum = false;
2198 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002199 mutex_unlock(&set_limit_mutex);
2200
2201 if (!ret)
2202 break;
2203
Balbir Singh4e416952009-09-23 15:56:39 -07002204 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh75822b42009-09-23 15:56:38 -07002205 MEM_CGROUP_RECLAIM_NOSWAP |
2206 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002207 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002208 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002209 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002210 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002211 else
2212 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002213 }
2214 return ret;
2215}
2216
Balbir Singh4e416952009-09-23 15:56:39 -07002217unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
2218 gfp_t gfp_mask, int nid,
2219 int zid)
2220{
2221 unsigned long nr_reclaimed = 0;
2222 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
2223 unsigned long reclaimed;
2224 int loop = 0;
2225 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002226 unsigned long long excess;
Balbir Singh4e416952009-09-23 15:56:39 -07002227
2228 if (order > 0)
2229 return 0;
2230
2231 mctz = soft_limit_tree_node_zone(nid, zid);
2232 /*
2233 * This loop can run a while, specially if mem_cgroup's continuously
2234 * keep exceeding their soft limit and putting the system under
2235 * pressure
2236 */
2237 do {
2238 if (next_mz)
2239 mz = next_mz;
2240 else
2241 mz = mem_cgroup_largest_soft_limit_node(mctz);
2242 if (!mz)
2243 break;
2244
2245 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
2246 gfp_mask,
2247 MEM_CGROUP_RECLAIM_SOFT);
2248 nr_reclaimed += reclaimed;
2249 spin_lock(&mctz->lock);
2250
2251 /*
2252 * If we failed to reclaim anything from this memory cgroup
2253 * it is time to move on to the next cgroup
2254 */
2255 next_mz = NULL;
2256 if (!reclaimed) {
2257 do {
2258 /*
2259 * Loop until we find yet another one.
2260 *
2261 * By the time we get the soft_limit lock
2262 * again, someone might have aded the
2263 * group back on the RB tree. Iterate to
2264 * make sure we get a different mem.
2265 * mem_cgroup_largest_soft_limit_node returns
2266 * NULL if no other cgroup is present on
2267 * the tree
2268 */
2269 next_mz =
2270 __mem_cgroup_largest_soft_limit_node(mctz);
2271 if (next_mz == mz) {
2272 css_put(&next_mz->mem->css);
2273 next_mz = NULL;
2274 } else /* next_mz == NULL or other memcg */
2275 break;
2276 } while (1);
2277 }
Balbir Singh4e416952009-09-23 15:56:39 -07002278 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002279 excess = res_counter_soft_limit_excess(&mz->mem->res);
Balbir Singh4e416952009-09-23 15:56:39 -07002280 /*
2281 * One school of thought says that we should not add
2282 * back the node to the tree if reclaim returns 0.
2283 * But our reclaim could return 0, simply because due
2284 * to priority we are exposing a smaller subset of
2285 * memory to reclaim from. Consider this as a longer
2286 * term TODO.
2287 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002288 /* If excess == 0, no tree ops */
2289 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07002290 spin_unlock(&mctz->lock);
2291 css_put(&mz->mem->css);
2292 loop++;
2293 /*
2294 * Could not reclaim anything and there are no more
2295 * mem cgroups to try or we seem to be looping without
2296 * reclaiming anything.
2297 */
2298 if (!nr_reclaimed &&
2299 (next_mz == NULL ||
2300 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
2301 break;
2302 } while (!nr_reclaimed);
2303 if (next_mz)
2304 css_put(&next_mz->mem->css);
2305 return nr_reclaimed;
2306}
2307
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002308/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002309 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002310 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
2311 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002312static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002313 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002314{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002315 struct zone *zone;
2316 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002317 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002318 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002319 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002320 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002321
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002322 zone = &NODE_DATA(node)->node_zones[zid];
2323 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07002324 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002325
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002326 loop = MEM_CGROUP_ZSTAT(mz, lru);
2327 /* give some margin against EBUSY etc...*/
2328 loop += 256;
2329 busy = NULL;
2330 while (loop--) {
2331 ret = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002332 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002333 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002334 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002335 break;
2336 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002337 pc = list_entry(list->prev, struct page_cgroup, lru);
2338 if (busy == pc) {
2339 list_move(&pc->lru, list);
2340 busy = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002341 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002342 continue;
2343 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002344 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002345
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -08002346 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002347 if (ret == -ENOMEM)
2348 break;
2349
2350 if (ret == -EBUSY || ret == -EINVAL) {
2351 /* found lock contention or "pc" is obsolete. */
2352 busy = pc;
2353 cond_resched();
2354 } else
2355 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002356 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002357
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002358 if (!ret && !list_empty(list))
2359 return -EBUSY;
2360 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002361}
2362
2363/*
2364 * make mem_cgroup's charge to be 0 if there is no task.
2365 * This enables deleting this mem_cgroup.
2366 */
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002367static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002368{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002369 int ret;
2370 int node, zid, shrink;
2371 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002372 struct cgroup *cgrp = mem->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08002373
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002374 css_get(&mem->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002375
2376 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002377 /* should free all ? */
2378 if (free_all)
2379 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002380move_account:
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002381 while (mem->res.usage > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002382 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002383 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002384 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002385 ret = -EINTR;
2386 if (signal_pending(current))
2387 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002388 /* This is for making all *used* pages to be on LRU. */
2389 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002390 ret = 0;
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08002391 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002392 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07002393 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002394 for_each_lru(l) {
2395 ret = mem_cgroup_force_empty_list(mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002396 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002397 if (ret)
2398 break;
2399 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002400 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002401 if (ret)
2402 break;
2403 }
2404 /* it seems parent cgroup doesn't have enough mem */
2405 if (ret == -ENOMEM)
2406 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002407 cond_resched();
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002408 }
2409 ret = 0;
2410out:
2411 css_put(&mem->css);
2412 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002413
2414try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002415 /* returns EBUSY if there is a task or if we come here twice. */
2416 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002417 ret = -EBUSY;
2418 goto out;
2419 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002420 /* we call try-to-free pages for make this cgroup empty */
2421 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002422 /* try to free all pages in this cgroup */
2423 shrink = 1;
2424 while (nr_retries && mem->res.usage > 0) {
2425 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002426
2427 if (signal_pending(current)) {
2428 ret = -EINTR;
2429 goto out;
2430 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002431 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
2432 false, get_swappiness(mem));
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002433 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002434 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002435 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02002436 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002437 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002438
2439 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002440 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002441 /* try move_account...there may be some *locked* pages. */
2442 if (mem->res.usage)
2443 goto move_account;
2444 ret = 0;
2445 goto out;
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 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002472 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08002473 * 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 Hiroyuki8c7c6e342009-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 Hiroyuki8c7c6e342009-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 Hiroyuki8c7c6e342009-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);
2547 val <<= PAGE_SHIFT;
2548 } else
2549 val = res_counter_read_u64(&mem->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002550 break;
2551 default:
2552 BUG();
2553 break;
2554 }
2555 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002556}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002557/*
2558 * The user of this function is...
2559 * RES_LIMIT.
2560 */
Paul Menage856c13a2008-07-25 01:47:04 -07002561static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
2562 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002563{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002564 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002565 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002566 unsigned long long val;
2567 int ret;
2568
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002569 type = MEMFILE_TYPE(cft->private);
2570 name = MEMFILE_ATTR(cft->private);
2571 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002572 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07002573 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
2574 ret = -EINVAL;
2575 break;
2576 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002577 /* This function does all necessary parse...reuse it */
2578 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002579 if (ret)
2580 break;
2581 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002582 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002583 else
2584 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002585 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07002586 case RES_SOFT_LIMIT:
2587 ret = res_counter_memparse_write_strategy(buffer, &val);
2588 if (ret)
2589 break;
2590 /*
2591 * For memsw, soft limits are hard to implement in terms
2592 * of semantics, for now, we support soft limits for
2593 * control without swap
2594 */
2595 if (type == _MEM)
2596 ret = res_counter_set_soft_limit(&memcg->res, val);
2597 else
2598 ret = -EINVAL;
2599 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002600 default:
2601 ret = -EINVAL; /* should be BUG() ? */
2602 break;
2603 }
2604 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002605}
2606
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08002607static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
2608 unsigned long long *mem_limit, unsigned long long *memsw_limit)
2609{
2610 struct cgroup *cgroup;
2611 unsigned long long min_limit, min_memsw_limit, tmp;
2612
2613 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2614 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2615 cgroup = memcg->css.cgroup;
2616 if (!memcg->use_hierarchy)
2617 goto out;
2618
2619 while (cgroup->parent) {
2620 cgroup = cgroup->parent;
2621 memcg = mem_cgroup_from_cont(cgroup);
2622 if (!memcg->use_hierarchy)
2623 break;
2624 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
2625 min_limit = min(min_limit, tmp);
2626 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2627 min_memsw_limit = min(min_memsw_limit, tmp);
2628 }
2629out:
2630 *mem_limit = min_limit;
2631 *memsw_limit = min_memsw_limit;
2632 return;
2633}
2634
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002635static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002636{
2637 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002638 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002639
2640 mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002641 type = MEMFILE_TYPE(event);
2642 name = MEMFILE_ATTR(event);
2643 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002644 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002645 if (type == _MEM)
2646 res_counter_reset_max(&mem->res);
2647 else
2648 res_counter_reset_max(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002649 break;
2650 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002651 if (type == _MEM)
2652 res_counter_reset_failcnt(&mem->res);
2653 else
2654 res_counter_reset_failcnt(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002655 break;
2656 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07002657
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07002658 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002659}
2660
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002661
2662/* For read statistics */
2663enum {
2664 MCS_CACHE,
2665 MCS_RSS,
Balbir Singhd69b0422009-06-17 16:26:34 -07002666 MCS_MAPPED_FILE,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002667 MCS_PGPGIN,
2668 MCS_PGPGOUT,
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002669 MCS_SWAP,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002670 MCS_INACTIVE_ANON,
2671 MCS_ACTIVE_ANON,
2672 MCS_INACTIVE_FILE,
2673 MCS_ACTIVE_FILE,
2674 MCS_UNEVICTABLE,
2675 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002676};
2677
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002678struct mcs_total_stat {
2679 s64 stat[NR_MCS_STAT];
2680};
2681
2682struct {
2683 char *local_name;
2684 char *total_name;
2685} memcg_stat_strings[NR_MCS_STAT] = {
2686 {"cache", "total_cache"},
2687 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07002688 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002689 {"pgpgin", "total_pgpgin"},
2690 {"pgpgout", "total_pgpgout"},
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002691 {"swap", "total_swap"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002692 {"inactive_anon", "total_inactive_anon"},
2693 {"active_anon", "total_active_anon"},
2694 {"inactive_file", "total_inactive_file"},
2695 {"active_file", "total_active_file"},
2696 {"unevictable", "total_unevictable"}
2697};
2698
2699
2700static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
2701{
2702 struct mcs_total_stat *s = data;
2703 s64 val;
2704
2705 /* per cpu stat */
2706 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE);
2707 s->stat[MCS_CACHE] += val * PAGE_SIZE;
2708 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
2709 s->stat[MCS_RSS] += val * PAGE_SIZE;
Balbir Singhd69b0422009-06-17 16:26:34 -07002710 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_MAPPED_FILE);
2711 s->stat[MCS_MAPPED_FILE] += val * PAGE_SIZE;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002712 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT);
2713 s->stat[MCS_PGPGIN] += val;
2714 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT);
2715 s->stat[MCS_PGPGOUT] += val;
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002716 if (do_swap_account) {
2717 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_SWAPOUT);
2718 s->stat[MCS_SWAP] += val * PAGE_SIZE;
2719 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002720
2721 /* per zone stat */
2722 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
2723 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
2724 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
2725 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
2726 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
2727 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
2728 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
2729 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
2730 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
2731 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
2732 return 0;
2733}
2734
2735static void
2736mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
2737{
2738 mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
2739}
2740
Paul Menagec64745c2008-04-29 01:00:02 -07002741static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
2742 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002743{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002744 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002745 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002746 int i;
2747
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002748 memset(&mystat, 0, sizeof(mystat));
2749 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002750
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002751 for (i = 0; i < NR_MCS_STAT; i++) {
2752 if (i == MCS_SWAP && !do_swap_account)
2753 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002754 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002755 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002756
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002757 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08002758 {
2759 unsigned long long limit, memsw_limit;
2760 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
2761 cb->fill(cb, "hierarchical_memory_limit", limit);
2762 if (do_swap_account)
2763 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
2764 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002765
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002766 memset(&mystat, 0, sizeof(mystat));
2767 mem_cgroup_get_total_stat(mem_cont, &mystat);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002768 for (i = 0; i < NR_MCS_STAT; i++) {
2769 if (i == MCS_SWAP && !do_swap_account)
2770 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002771 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002772 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002773
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002774#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08002775 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002776
2777 {
2778 int nid, zid;
2779 struct mem_cgroup_per_zone *mz;
2780 unsigned long recent_rotated[2] = {0, 0};
2781 unsigned long recent_scanned[2] = {0, 0};
2782
2783 for_each_online_node(nid)
2784 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
2785 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
2786
2787 recent_rotated[0] +=
2788 mz->reclaim_stat.recent_rotated[0];
2789 recent_rotated[1] +=
2790 mz->reclaim_stat.recent_rotated[1];
2791 recent_scanned[0] +=
2792 mz->reclaim_stat.recent_scanned[0];
2793 recent_scanned[1] +=
2794 mz->reclaim_stat.recent_scanned[1];
2795 }
2796 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
2797 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
2798 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
2799 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
2800 }
2801#endif
2802
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002803 return 0;
2804}
2805
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002806static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
2807{
2808 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2809
2810 return get_swappiness(memcg);
2811}
2812
2813static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
2814 u64 val)
2815{
2816 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2817 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08002818
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002819 if (val > 100)
2820 return -EINVAL;
2821
2822 if (cgrp->parent == NULL)
2823 return -EINVAL;
2824
2825 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08002826
2827 cgroup_lock();
2828
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002829 /* If under hierarchy, only empty-root can set this value */
2830 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08002831 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
2832 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002833 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08002834 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002835
2836 spin_lock(&memcg->reclaim_param_lock);
2837 memcg->swappiness = val;
2838 spin_unlock(&memcg->reclaim_param_lock);
2839
Li Zefan068b38c2009-01-15 13:51:26 -08002840 cgroup_unlock();
2841
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002842 return 0;
2843}
2844
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002845
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002846static struct cftype mem_cgroup_files[] = {
2847 {
Balbir Singh0eea1032008-02-07 00:13:57 -08002848 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002849 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07002850 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002851 },
2852 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002853 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002854 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002855 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002856 .read_u64 = mem_cgroup_read,
2857 },
2858 {
Balbir Singh0eea1032008-02-07 00:13:57 -08002859 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002860 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07002861 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07002862 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002863 },
2864 {
Balbir Singh296c81d2009-09-23 15:56:36 -07002865 .name = "soft_limit_in_bytes",
2866 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
2867 .write_string = mem_cgroup_write,
2868 .read_u64 = mem_cgroup_read,
2869 },
2870 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002871 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002872 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002873 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07002874 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002875 },
Balbir Singh8697d332008-02-07 00:13:59 -08002876 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002877 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07002878 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002879 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002880 {
2881 .name = "force_empty",
2882 .trigger = mem_cgroup_force_empty_write,
2883 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08002884 {
2885 .name = "use_hierarchy",
2886 .write_u64 = mem_cgroup_hierarchy_write,
2887 .read_u64 = mem_cgroup_hierarchy_read,
2888 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002889 {
2890 .name = "swappiness",
2891 .read_u64 = mem_cgroup_swappiness_read,
2892 .write_u64 = mem_cgroup_swappiness_write,
2893 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002894};
2895
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002896#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2897static struct cftype memsw_cgroup_files[] = {
2898 {
2899 .name = "memsw.usage_in_bytes",
2900 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
2901 .read_u64 = mem_cgroup_read,
2902 },
2903 {
2904 .name = "memsw.max_usage_in_bytes",
2905 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
2906 .trigger = mem_cgroup_reset,
2907 .read_u64 = mem_cgroup_read,
2908 },
2909 {
2910 .name = "memsw.limit_in_bytes",
2911 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
2912 .write_string = mem_cgroup_write,
2913 .read_u64 = mem_cgroup_read,
2914 },
2915 {
2916 .name = "memsw.failcnt",
2917 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
2918 .trigger = mem_cgroup_reset,
2919 .read_u64 = mem_cgroup_read,
2920 },
2921};
2922
2923static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2924{
2925 if (!do_swap_account)
2926 return 0;
2927 return cgroup_add_files(cont, ss, memsw_cgroup_files,
2928 ARRAY_SIZE(memsw_cgroup_files));
2929};
2930#else
2931static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2932{
2933 return 0;
2934}
2935#endif
2936
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002937static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2938{
2939 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002940 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07002941 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07002942 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002943 /*
2944 * This routine is called against possible nodes.
2945 * But it's BUG to call kmalloc() against offline node.
2946 *
2947 * TODO: this routine can waste much memory for nodes which will
2948 * never be onlined. It's better to use memory hotplug callback
2949 * function.
2950 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07002951 if (!node_state(node, N_NORMAL_MEMORY))
2952 tmp = -1;
2953 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002954 if (!pn)
2955 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002956
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002957 mem->info.nodeinfo[node] = pn;
2958 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002959
2960 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
2961 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07002962 for_each_lru(l)
2963 INIT_LIST_HEAD(&mz->lists[l]);
Balbir Singhf64c3f52009-09-23 15:56:37 -07002964 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07002965 mz->on_tree = false;
2966 mz->mem = mem;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002967 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002968 return 0;
2969}
2970
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002971static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2972{
2973 kfree(mem->info.nodeinfo[node]);
2974}
2975
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002976static int mem_cgroup_size(void)
2977{
2978 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
2979 return sizeof(struct mem_cgroup) + cpustat_size;
2980}
2981
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002982static struct mem_cgroup *mem_cgroup_alloc(void)
2983{
2984 struct mem_cgroup *mem;
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002985 int size = mem_cgroup_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002986
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002987 if (size < PAGE_SIZE)
2988 mem = kmalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002989 else
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002990 mem = vmalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002991
2992 if (mem)
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002993 memset(mem, 0, size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002994 return mem;
2995}
2996
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002997/*
2998 * At destroying mem_cgroup, references from swap_cgroup can remain.
2999 * (scanning all at force_empty is too costly...)
3000 *
3001 * Instead of clearing all references at force_empty, we remember
3002 * the number of reference from swap_cgroup and free mem_cgroup when
3003 * it goes down to 0.
3004 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003005 * Removal of cgroup itself succeeds regardless of refs from swap.
3006 */
3007
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003008static void __mem_cgroup_free(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003009{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003010 int node;
3011
Balbir Singhf64c3f52009-09-23 15:56:37 -07003012 mem_cgroup_remove_from_trees(mem);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003013 free_css_id(&mem_cgroup_subsys, &mem->css);
3014
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003015 for_each_node_state(node, N_POSSIBLE)
3016 free_mem_cgroup_per_zone_info(mem, node);
3017
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003018 if (mem_cgroup_size() < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003019 kfree(mem);
3020 else
3021 vfree(mem);
3022}
3023
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003024static void mem_cgroup_get(struct mem_cgroup *mem)
3025{
3026 atomic_inc(&mem->refcnt);
3027}
3028
3029static void mem_cgroup_put(struct mem_cgroup *mem)
3030{
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003031 if (atomic_dec_and_test(&mem->refcnt)) {
3032 struct mem_cgroup *parent = parent_mem_cgroup(mem);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003033 __mem_cgroup_free(mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003034 if (parent)
3035 mem_cgroup_put(parent);
3036 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003037}
3038
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003039/*
3040 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
3041 */
3042static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
3043{
3044 if (!mem->res.parent)
3045 return NULL;
3046 return mem_cgroup_from_res_counter(mem->res.parent, res);
3047}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003048
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003049#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3050static void __init enable_swap_cgroup(void)
3051{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003052 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003053 do_swap_account = 1;
3054}
3055#else
3056static void __init enable_swap_cgroup(void)
3057{
3058}
3059#endif
3060
Balbir Singhf64c3f52009-09-23 15:56:37 -07003061static int mem_cgroup_soft_limit_tree_init(void)
3062{
3063 struct mem_cgroup_tree_per_node *rtpn;
3064 struct mem_cgroup_tree_per_zone *rtpz;
3065 int tmp, node, zone;
3066
3067 for_each_node_state(node, N_POSSIBLE) {
3068 tmp = node;
3069 if (!node_state(node, N_NORMAL_MEMORY))
3070 tmp = -1;
3071 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
3072 if (!rtpn)
3073 return 1;
3074
3075 soft_limit_tree.rb_tree_per_node[node] = rtpn;
3076
3077 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
3078 rtpz = &rtpn->rb_tree_per_zone[zone];
3079 rtpz->rb_root = RB_ROOT;
3080 spin_lock_init(&rtpz->lock);
3081 }
3082 }
3083 return 0;
3084}
3085
Li Zefan0eb253e2009-01-15 13:51:25 -08003086static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003087mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
3088{
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003089 struct mem_cgroup *mem, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003090 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003091 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003092
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003093 mem = mem_cgroup_alloc();
3094 if (!mem)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003095 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08003096
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003097 for_each_node_state(node, N_POSSIBLE)
3098 if (alloc_mem_cgroup_per_zone_info(mem, node))
3099 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07003100
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003101 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003102 if (cont->parent == NULL) {
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003103 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003104 parent = NULL;
Balbir Singh4b3bde42009-09-23 15:56:32 -07003105 root_mem_cgroup = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07003106 if (mem_cgroup_soft_limit_tree_init())
3107 goto free_out;
3108
Balbir Singh18f59ea2009-01-07 18:08:07 -08003109 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003110 parent = mem_cgroup_from_cont(cont->parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003111 mem->use_hierarchy = parent->use_hierarchy;
3112 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003113
Balbir Singh18f59ea2009-01-07 18:08:07 -08003114 if (parent && parent->use_hierarchy) {
3115 res_counter_init(&mem->res, &parent->res);
3116 res_counter_init(&mem->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003117 /*
3118 * We increment refcnt of the parent to ensure that we can
3119 * safely access it on res_counter_charge/uncharge.
3120 * This refcnt will be decremented when freeing this
3121 * mem_cgroup(see mem_cgroup_put).
3122 */
3123 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003124 } else {
3125 res_counter_init(&mem->res, NULL);
3126 res_counter_init(&mem->memsw, NULL);
3127 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003128 mem->last_scanned_child = 0;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -08003129 spin_lock_init(&mem->reclaim_param_lock);
Balbir Singh6d61ef42009-01-07 18:08:06 -08003130
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003131 if (parent)
3132 mem->swappiness = get_swappiness(parent);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003133 atomic_set(&mem->refcnt, 1);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003134 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003135free_out:
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003136 __mem_cgroup_free(mem);
Balbir Singh4b3bde42009-09-23 15:56:32 -07003137 root_mem_cgroup = NULL;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003138 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003139}
3140
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003141static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08003142 struct cgroup *cont)
3143{
3144 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003145
3146 return mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08003147}
3148
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003149static void mem_cgroup_destroy(struct cgroup_subsys *ss,
3150 struct cgroup *cont)
3151{
Daisuke Nishimurac268e992009-01-15 13:51:13 -08003152 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08003153
Daisuke Nishimurac268e992009-01-15 13:51:13 -08003154 mem_cgroup_put(mem);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003155}
3156
3157static int mem_cgroup_populate(struct cgroup_subsys *ss,
3158 struct cgroup *cont)
3159{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003160 int ret;
3161
3162 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
3163 ARRAY_SIZE(mem_cgroup_files));
3164
3165 if (!ret)
3166 ret = register_memsw_files(cont, ss);
3167 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003168}
3169
Balbir Singh67e465a2008-02-07 00:13:54 -08003170static void mem_cgroup_move_task(struct cgroup_subsys *ss,
3171 struct cgroup *cont,
3172 struct cgroup *old_cont,
Ben Blumbe367d02009-09-23 15:56:31 -07003173 struct task_struct *p,
3174 bool threadgroup)
Balbir Singh67e465a2008-02-07 00:13:54 -08003175{
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08003176 mutex_lock(&memcg_tasklist);
Balbir Singh67e465a2008-02-07 00:13:54 -08003177 /*
Nikanth Karthikesanf9717d22009-01-07 18:08:11 -08003178 * FIXME: It's better to move charges of this process from old
3179 * memcg to new memcg. But it's just on TODO-List now.
Balbir Singh67e465a2008-02-07 00:13:54 -08003180 */
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08003181 mutex_unlock(&memcg_tasklist);
Balbir Singh67e465a2008-02-07 00:13:54 -08003182}
3183
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003184struct cgroup_subsys mem_cgroup_subsys = {
3185 .name = "memory",
3186 .subsys_id = mem_cgroup_subsys_id,
3187 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08003188 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003189 .destroy = mem_cgroup_destroy,
3190 .populate = mem_cgroup_populate,
Balbir Singh67e465a2008-02-07 00:13:54 -08003191 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003192 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003193 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003194};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003195
3196#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3197
3198static int __init disable_swap_account(char *s)
3199{
3200 really_do_swap_account = 0;
3201 return 1;
3202}
3203__setup("noswapaccount", disable_swap_account);
3204#endif