blob: b5ac61ce73461531bd29a455d768ca26885a3d42 [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
285static struct mem_cgroup_per_zone *
286page_cgroup_zoneinfo(struct page_cgroup *pc)
287{
288 struct mem_cgroup *mem = pc->mem_cgroup;
289 int nid = page_cgroup_nid(pc);
290 int zid = page_cgroup_zid(pc);
291
292 if (!mem)
293 return NULL;
294
295 return mem_cgroup_zoneinfo(mem, nid, zid);
296}
297
298static struct mem_cgroup_tree_per_zone *
299soft_limit_tree_node_zone(int nid, int zid)
300{
301 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
302}
303
304static struct mem_cgroup_tree_per_zone *
305soft_limit_tree_from_page(struct page *page)
306{
307 int nid = page_to_nid(page);
308 int zid = page_zonenum(page);
309
310 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
311}
312
313static void
Balbir Singh4e416952009-09-23 15:56:39 -0700314__mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700315 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700316 struct mem_cgroup_tree_per_zone *mctz,
317 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700318{
319 struct rb_node **p = &mctz->rb_root.rb_node;
320 struct rb_node *parent = NULL;
321 struct mem_cgroup_per_zone *mz_node;
322
323 if (mz->on_tree)
324 return;
325
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700326 mz->usage_in_excess = new_usage_in_excess;
327 if (!mz->usage_in_excess)
328 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700329 while (*p) {
330 parent = *p;
331 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
332 tree_node);
333 if (mz->usage_in_excess < mz_node->usage_in_excess)
334 p = &(*p)->rb_left;
335 /*
336 * We can't avoid mem cgroups that are over their soft
337 * limit by the same amount
338 */
339 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
340 p = &(*p)->rb_right;
341 }
342 rb_link_node(&mz->tree_node, parent, p);
343 rb_insert_color(&mz->tree_node, &mctz->rb_root);
344 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700345}
346
347static void
348__mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
349 struct mem_cgroup_per_zone *mz,
350 struct mem_cgroup_tree_per_zone *mctz)
351{
352 if (!mz->on_tree)
353 return;
354 rb_erase(&mz->tree_node, &mctz->rb_root);
355 mz->on_tree = false;
356}
357
358static void
Balbir Singhf64c3f52009-09-23 15:56:37 -0700359mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
360 struct mem_cgroup_per_zone *mz,
361 struct mem_cgroup_tree_per_zone *mctz)
362{
363 spin_lock(&mctz->lock);
Balbir Singh4e416952009-09-23 15:56:39 -0700364 __mem_cgroup_remove_exceeded(mem, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700365 spin_unlock(&mctz->lock);
366}
367
368static bool mem_cgroup_soft_limit_check(struct mem_cgroup *mem)
369{
370 bool ret = false;
371 int cpu;
372 s64 val;
373 struct mem_cgroup_stat_cpu *cpustat;
374
375 cpu = get_cpu();
376 cpustat = &mem->stat.cpustat[cpu];
377 val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_EVENTS);
378 if (unlikely(val > SOFTLIMIT_EVENTS_THRESH)) {
379 __mem_cgroup_stat_reset_safe(cpustat, MEM_CGROUP_STAT_EVENTS);
380 ret = true;
381 }
382 put_cpu();
383 return ret;
384}
385
386static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
387{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700388 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700389 struct mem_cgroup_per_zone *mz;
390 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700391 int nid = page_to_nid(page);
392 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700393 mctz = soft_limit_tree_from_page(page);
394
395 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700396 * Necessary to update all ancestors when hierarchy is used.
397 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700398 */
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700399 for (; mem; mem = parent_mem_cgroup(mem)) {
400 mz = mem_cgroup_zoneinfo(mem, nid, zid);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700401 excess = res_counter_soft_limit_excess(&mem->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700402 /*
403 * We have to update the tree if mz is on RB-tree or
404 * mem is over its softlimit.
405 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700406 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700407 spin_lock(&mctz->lock);
408 /* if on-tree, remove it */
409 if (mz->on_tree)
410 __mem_cgroup_remove_exceeded(mem, mz, mctz);
411 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700412 * Insert again. mz->usage_in_excess will be updated.
413 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700414 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700415 __mem_cgroup_insert_exceeded(mem, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700416 spin_unlock(&mctz->lock);
417 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700418 }
419}
420
421static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
422{
423 int node, zone;
424 struct mem_cgroup_per_zone *mz;
425 struct mem_cgroup_tree_per_zone *mctz;
426
427 for_each_node_state(node, N_POSSIBLE) {
428 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
429 mz = mem_cgroup_zoneinfo(mem, node, zone);
430 mctz = soft_limit_tree_node_zone(node, zone);
431 mem_cgroup_remove_exceeded(mem, mz, mctz);
432 }
433 }
434}
435
Balbir Singh4e416952009-09-23 15:56:39 -0700436static inline unsigned long mem_cgroup_get_excess(struct mem_cgroup *mem)
437{
438 return res_counter_soft_limit_excess(&mem->res) >> PAGE_SHIFT;
439}
440
441static struct mem_cgroup_per_zone *
442__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
443{
444 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700445 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700446
447retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700448 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700449 rightmost = rb_last(&mctz->rb_root);
450 if (!rightmost)
451 goto done; /* Nothing to reclaim from */
452
453 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
454 /*
455 * Remove the node now but someone else can add it back,
456 * we will to add it back at the end of reclaim to its correct
457 * position in the tree.
458 */
459 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
460 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
461 !css_tryget(&mz->mem->css))
462 goto retry;
463done:
464 return mz;
465}
466
467static struct mem_cgroup_per_zone *
468mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
469{
470 struct mem_cgroup_per_zone *mz;
471
472 spin_lock(&mctz->lock);
473 mz = __mem_cgroup_largest_soft_limit_node(mctz);
474 spin_unlock(&mctz->lock);
475 return mz;
476}
477
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700478static void mem_cgroup_swap_statistics(struct mem_cgroup *mem,
479 bool charge)
480{
481 int val = (charge) ? 1 : -1;
482 struct mem_cgroup_stat *stat = &mem->stat;
483 struct mem_cgroup_stat_cpu *cpustat;
484 int cpu = get_cpu();
485
486 cpustat = &stat->cpustat[cpu];
487 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_SWAPOUT, val);
488 put_cpu();
489}
490
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700491static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
492 struct page_cgroup *pc,
493 bool charge)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800494{
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700495 int val = (charge) ? 1 : -1;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800496 struct mem_cgroup_stat *stat = &mem->stat;
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700497 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800498 int cpu = get_cpu();
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800499
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800500 cpustat = &stat->cpustat[cpu];
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700501 if (PageCgroupCache(pc))
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700502 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800503 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700504 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700505
506 if (charge)
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700507 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700508 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
509 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700510 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700511 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700512 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_EVENTS, 1);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800513 put_cpu();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800514}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800515
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700516static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700517 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800518{
519 int nid, zid;
520 struct mem_cgroup_per_zone *mz;
521 u64 total = 0;
522
523 for_each_online_node(nid)
524 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
525 mz = mem_cgroup_zoneinfo(mem, nid, zid);
526 total += MEM_CGROUP_ZSTAT(mz, idx);
527 }
528 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800529}
530
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800531static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800532{
533 return container_of(cgroup_subsys_state(cont,
534 mem_cgroup_subsys_id), struct mem_cgroup,
535 css);
536}
537
Balbir Singhcf475ad2008-04-29 01:00:16 -0700538struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800539{
Balbir Singh31a78f22008-09-28 23:09:31 +0100540 /*
541 * mm_update_next_owner() may clear mm->owner to NULL
542 * if it races with swapoff, page migration, etc.
543 * So this can be called with p == NULL.
544 */
545 if (unlikely(!p))
546 return NULL;
547
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800548 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
549 struct mem_cgroup, css);
550}
551
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800552static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
553{
554 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700555
556 if (!mm)
557 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800558 /*
559 * Because we have no locks, mm->owner's may be being moved to other
560 * cgroup. We use css_tryget() here even if this looks
561 * pessimistic (rather than adding locks here).
562 */
563 rcu_read_lock();
564 do {
565 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
566 if (unlikely(!mem))
567 break;
568 } while (!css_tryget(&mem->css));
569 rcu_read_unlock();
570 return mem;
571}
572
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700573/*
574 * Call callback function against all cgroup under hierarchy tree.
575 */
576static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
577 int (*func)(struct mem_cgroup *, void *))
578{
579 int found, ret, nextid;
580 struct cgroup_subsys_state *css;
581 struct mem_cgroup *mem;
582
583 if (!root->use_hierarchy)
584 return (*func)(root, data);
585
586 nextid = 1;
587 do {
588 ret = 0;
589 mem = NULL;
590
591 rcu_read_lock();
592 css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
593 &found);
594 if (css && css_tryget(css))
595 mem = container_of(css, struct mem_cgroup, css);
596 rcu_read_unlock();
597
598 if (mem) {
599 ret = (*func)(mem, data);
600 css_put(&mem->css);
601 }
602 nextid = found + 1;
603 } while (!ret && css);
604
605 return ret;
606}
607
Balbir Singh4b3bde42009-09-23 15:56:32 -0700608static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
609{
610 return (mem == root_mem_cgroup);
611}
612
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800613/*
614 * Following LRU functions are allowed to be used without PCG_LOCK.
615 * Operations are called by routine of global LRU independently from memcg.
616 * What we have to take care of here is validness of pc->mem_cgroup.
617 *
618 * Changes to pc->mem_cgroup happens when
619 * 1. charge
620 * 2. moving account
621 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
622 * It is added to LRU before charge.
623 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
624 * When moving account, the page is not on LRU. It's isolated.
625 */
626
627void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800628{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800629 struct page_cgroup *pc;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800630 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700631
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800632 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800633 return;
634 pc = lookup_page_cgroup(page);
635 /* can happen while we handle swapcache. */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700636 if (!TestClearPageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800637 return;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700638 VM_BUG_ON(!pc->mem_cgroup);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800639 /*
640 * We don't check PCG_USED bit. It's cleared when the "page" is finally
641 * removed from global LRU.
642 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800643 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700644 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700645 if (mem_cgroup_is_root(pc->mem_cgroup))
646 return;
647 VM_BUG_ON(list_empty(&pc->lru));
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800648 list_del_init(&pc->lru);
649 return;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800650}
651
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800652void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800653{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800654 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800655}
656
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800657void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800658{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800659 struct mem_cgroup_per_zone *mz;
660 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800661
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800662 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700663 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700664
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800665 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800666 /*
667 * Used bit is set without atomic ops but after smp_wmb().
668 * For making pc->mem_cgroup visible, insert smp_rmb() here.
669 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800670 smp_rmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -0700671 /* unused or root page is not rotated. */
672 if (!PageCgroupUsed(pc) || mem_cgroup_is_root(pc->mem_cgroup))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800673 return;
674 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700675 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800676}
677
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800678void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
679{
680 struct page_cgroup *pc;
681 struct mem_cgroup_per_zone *mz;
682
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800683 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800684 return;
685 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700686 VM_BUG_ON(PageCgroupAcctLRU(pc));
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800687 /*
688 * Used bit is set without atomic ops but after smp_wmb().
689 * For making pc->mem_cgroup visible, insert smp_rmb() here.
690 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800691 smp_rmb();
692 if (!PageCgroupUsed(pc))
693 return;
694
695 mz = page_cgroup_zoneinfo(pc);
696 MEM_CGROUP_ZSTAT(mz, lru) += 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700697 SetPageCgroupAcctLRU(pc);
698 if (mem_cgroup_is_root(pc->mem_cgroup))
699 return;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800700 list_add(&pc->lru, &mz->lists[lru]);
701}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800702
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800703/*
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800704 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
705 * lru because the page may.be reused after it's fully uncharged (because of
706 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
707 * it again. This function is only used to charge SwapCache. It's done under
708 * lock_page and expected that zone->lru_lock is never held.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800709 */
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800710static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800711{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800712 unsigned long flags;
713 struct zone *zone = page_zone(page);
714 struct page_cgroup *pc = lookup_page_cgroup(page);
715
716 spin_lock_irqsave(&zone->lru_lock, flags);
717 /*
718 * Forget old LRU when this page_cgroup is *not* used. This Used bit
719 * is guarded by lock_page() because the page is SwapCache.
720 */
721 if (!PageCgroupUsed(pc))
722 mem_cgroup_del_lru_list(page, page_lru(page));
723 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800724}
725
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800726static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
727{
728 unsigned long flags;
729 struct zone *zone = page_zone(page);
730 struct page_cgroup *pc = lookup_page_cgroup(page);
731
732 spin_lock_irqsave(&zone->lru_lock, flags);
733 /* link when the page is linked to LRU but page_cgroup isn't */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700734 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800735 mem_cgroup_add_lru_list(page, page_lru(page));
736 spin_unlock_irqrestore(&zone->lru_lock, flags);
737}
738
739
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800740void mem_cgroup_move_lists(struct page *page,
741 enum lru_list from, enum lru_list to)
742{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800743 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800744 return;
745 mem_cgroup_del_lru_list(page, from);
746 mem_cgroup_add_lru_list(page, to);
747}
748
David Rientjes4c4a2212008-02-07 00:14:06 -0800749int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
750{
751 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700752 struct mem_cgroup *curr = NULL;
David Rientjes4c4a2212008-02-07 00:14:06 -0800753
754 task_lock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700755 rcu_read_lock();
756 curr = try_get_mem_cgroup_from_mm(task->mm);
757 rcu_read_unlock();
David Rientjes4c4a2212008-02-07 00:14:06 -0800758 task_unlock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700759 if (!curr)
760 return 0;
761 if (curr->use_hierarchy)
762 ret = css_is_ancestor(&curr->css, &mem->css);
763 else
764 ret = (curr == mem);
765 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -0800766 return ret;
767}
768
Balbir Singh66e17072008-02-07 00:13:56 -0800769/*
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800770 * prev_priority control...this will be used in memory reclaim path.
771 */
772int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
773{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800774 int prev_priority;
775
776 spin_lock(&mem->reclaim_param_lock);
777 prev_priority = mem->prev_priority;
778 spin_unlock(&mem->reclaim_param_lock);
779
780 return prev_priority;
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800781}
782
783void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
784{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800785 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800786 if (priority < mem->prev_priority)
787 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800788 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800789}
790
791void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
792{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800793 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800794 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800795 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800796}
797
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800798static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800799{
800 unsigned long active;
801 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800802 unsigned long gb;
803 unsigned long inactive_ratio;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800804
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700805 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
806 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800807
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800808 gb = (inactive + active) >> (30 - PAGE_SHIFT);
809 if (gb)
810 inactive_ratio = int_sqrt(10 * gb);
811 else
812 inactive_ratio = 1;
813
814 if (present_pages) {
815 present_pages[0] = inactive;
816 present_pages[1] = active;
817 }
818
819 return inactive_ratio;
820}
821
822int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
823{
824 unsigned long active;
825 unsigned long inactive;
826 unsigned long present_pages[2];
827 unsigned long inactive_ratio;
828
829 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
830
831 inactive = present_pages[0];
832 active = present_pages[1];
833
834 if (inactive * inactive_ratio < active)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800835 return 1;
836
837 return 0;
838}
839
Rik van Riel56e49d22009-06-16 15:32:28 -0700840int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
841{
842 unsigned long active;
843 unsigned long inactive;
844
845 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
846 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
847
848 return (active > inactive);
849}
850
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800851unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
852 struct zone *zone,
853 enum lru_list lru)
854{
855 int nid = zone->zone_pgdat->node_id;
856 int zid = zone_idx(zone);
857 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
858
859 return MEM_CGROUP_ZSTAT(mz, lru);
860}
861
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800862struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
863 struct zone *zone)
864{
865 int nid = zone->zone_pgdat->node_id;
866 int zid = zone_idx(zone);
867 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
868
869 return &mz->reclaim_stat;
870}
871
872struct zone_reclaim_stat *
873mem_cgroup_get_reclaim_stat_from_page(struct page *page)
874{
875 struct page_cgroup *pc;
876 struct mem_cgroup_per_zone *mz;
877
878 if (mem_cgroup_disabled())
879 return NULL;
880
881 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800882 /*
883 * Used bit is set without atomic ops but after smp_wmb().
884 * For making pc->mem_cgroup visible, insert smp_rmb() here.
885 */
886 smp_rmb();
887 if (!PageCgroupUsed(pc))
888 return NULL;
889
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800890 mz = page_cgroup_zoneinfo(pc);
891 if (!mz)
892 return NULL;
893
894 return &mz->reclaim_stat;
895}
896
Balbir Singh66e17072008-02-07 00:13:56 -0800897unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
898 struct list_head *dst,
899 unsigned long *scanned, int order,
900 int mode, struct zone *z,
901 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700902 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -0800903{
904 unsigned long nr_taken = 0;
905 struct page *page;
906 unsigned long scan;
907 LIST_HEAD(pc_list);
908 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800909 struct page_cgroup *pc, *tmp;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800910 int nid = z->zone_pgdat->node_id;
911 int zid = zone_idx(z);
912 struct mem_cgroup_per_zone *mz;
Johannes Weinerb7c46d12009-09-21 17:02:56 -0700913 int lru = LRU_FILE * file + active;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700914 int ret;
Balbir Singh66e17072008-02-07 00:13:56 -0800915
Balbir Singhcf475ad2008-04-29 01:00:16 -0700916 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800917 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700918 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -0800919
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800920 scan = 0;
921 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -0800922 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800923 break;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800924
925 page = pc->page;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700926 if (unlikely(!PageCgroupUsed(pc)))
927 continue;
Hugh Dickins436c65412008-02-07 00:14:12 -0800928 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800929 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800930
Hugh Dickins436c65412008-02-07 00:14:12 -0800931 scan++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700932 ret = __isolate_lru_page(page, mode, file);
933 switch (ret) {
934 case 0:
Balbir Singh66e17072008-02-07 00:13:56 -0800935 list_move(&page->lru, dst);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700936 mem_cgroup_del_lru(page);
Balbir Singh66e17072008-02-07 00:13:56 -0800937 nr_taken++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700938 break;
939 case -EBUSY:
940 /* we don't affect global LRU but rotate in our LRU */
941 mem_cgroup_rotate_lru_list(page, page_lru(page));
942 break;
943 default:
944 break;
Balbir Singh66e17072008-02-07 00:13:56 -0800945 }
946 }
947
Balbir Singh66e17072008-02-07 00:13:56 -0800948 *scanned = scan;
949 return nr_taken;
950}
951
Balbir Singh6d61ef42009-01-07 18:08:06 -0800952#define mem_cgroup_from_res_counter(counter, member) \
953 container_of(counter, struct mem_cgroup, member)
954
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -0800955static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
956{
957 if (do_swap_account) {
958 if (res_counter_check_under_limit(&mem->res) &&
959 res_counter_check_under_limit(&mem->memsw))
960 return true;
961 } else
962 if (res_counter_check_under_limit(&mem->res))
963 return true;
964 return false;
965}
966
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800967static unsigned int get_swappiness(struct mem_cgroup *memcg)
968{
969 struct cgroup *cgrp = memcg->css.cgroup;
970 unsigned int swappiness;
971
972 /* root ? */
973 if (cgrp->parent == NULL)
974 return vm_swappiness;
975
976 spin_lock(&memcg->reclaim_param_lock);
977 swappiness = memcg->swappiness;
978 spin_unlock(&memcg->reclaim_param_lock);
979
980 return swappiness;
981}
982
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700983static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
984{
985 int *val = data;
986 (*val)++;
987 return 0;
988}
Balbir Singhe2224322009-04-02 16:57:39 -0700989
990/**
991 * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode.
992 * @memcg: The memory cgroup that went over limit
993 * @p: Task that is going to be killed
994 *
995 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
996 * enabled
997 */
998void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
999{
1000 struct cgroup *task_cgrp;
1001 struct cgroup *mem_cgrp;
1002 /*
1003 * Need a buffer in BSS, can't rely on allocations. The code relies
1004 * on the assumption that OOM is serialized for memory controller.
1005 * If this assumption is broken, revisit this code.
1006 */
1007 static char memcg_name[PATH_MAX];
1008 int ret;
1009
1010 if (!memcg)
1011 return;
1012
1013
1014 rcu_read_lock();
1015
1016 mem_cgrp = memcg->css.cgroup;
1017 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1018
1019 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1020 if (ret < 0) {
1021 /*
1022 * Unfortunately, we are unable to convert to a useful name
1023 * But we'll still print out the usage information
1024 */
1025 rcu_read_unlock();
1026 goto done;
1027 }
1028 rcu_read_unlock();
1029
1030 printk(KERN_INFO "Task in %s killed", memcg_name);
1031
1032 rcu_read_lock();
1033 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1034 if (ret < 0) {
1035 rcu_read_unlock();
1036 goto done;
1037 }
1038 rcu_read_unlock();
1039
1040 /*
1041 * Continues from above, so we don't need an KERN_ level
1042 */
1043 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1044done:
1045
1046 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1047 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1048 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1049 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1050 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1051 "failcnt %llu\n",
1052 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1053 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1054 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1055}
1056
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001057/*
1058 * This function returns the number of memcg under hierarchy tree. Returns
1059 * 1(self count) if no children.
1060 */
1061static int mem_cgroup_count_children(struct mem_cgroup *mem)
1062{
1063 int num = 0;
1064 mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
1065 return num;
1066}
1067
Balbir Singh6d61ef42009-01-07 18:08:06 -08001068/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001069 * Visit the first child (need not be the first child as per the ordering
1070 * of the cgroup list, since we track last_scanned_child) of @mem and use
1071 * that to reclaim free pages from.
1072 */
1073static struct mem_cgroup *
1074mem_cgroup_select_victim(struct mem_cgroup *root_mem)
1075{
1076 struct mem_cgroup *ret = NULL;
1077 struct cgroup_subsys_state *css;
1078 int nextid, found;
1079
1080 if (!root_mem->use_hierarchy) {
1081 css_get(&root_mem->css);
1082 ret = root_mem;
1083 }
1084
1085 while (!ret) {
1086 rcu_read_lock();
1087 nextid = root_mem->last_scanned_child + 1;
1088 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
1089 &found);
1090 if (css && css_tryget(css))
1091 ret = container_of(css, struct mem_cgroup, css);
1092
1093 rcu_read_unlock();
1094 /* Updates scanning parameter */
1095 spin_lock(&root_mem->reclaim_param_lock);
1096 if (!css) {
1097 /* this means start scan from ID:1 */
1098 root_mem->last_scanned_child = 0;
1099 } else
1100 root_mem->last_scanned_child = found;
1101 spin_unlock(&root_mem->reclaim_param_lock);
1102 }
1103
1104 return ret;
1105}
1106
1107/*
1108 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1109 * we reclaimed from, so that we don't end up penalizing one child extensively
1110 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001111 *
1112 * root_mem is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001113 *
1114 * We give up and return to the caller when we visit root_mem twice.
1115 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001116 *
1117 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001118 */
1119static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
Balbir Singh4e416952009-09-23 15:56:39 -07001120 struct zone *zone,
Balbir Singh75822b42009-09-23 15:56:38 -07001121 gfp_t gfp_mask,
1122 unsigned long reclaim_options)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001123{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001124 struct mem_cgroup *victim;
1125 int ret, total = 0;
1126 int loop = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001127 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1128 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
Balbir Singh4e416952009-09-23 15:56:39 -07001129 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
1130 unsigned long excess = mem_cgroup_get_excess(root_mem);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001131
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001132 /* If memsw_is_minimum==1, swap-out is of-no-use. */
1133 if (root_mem->memsw_is_minimum)
1134 noswap = true;
1135
Balbir Singh4e416952009-09-23 15:56:39 -07001136 while (1) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001137 victim = mem_cgroup_select_victim(root_mem);
Balbir Singh4e416952009-09-23 15:56:39 -07001138 if (victim == root_mem) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001139 loop++;
Balbir Singh4e416952009-09-23 15:56:39 -07001140 if (loop >= 2) {
1141 /*
1142 * If we have not been able to reclaim
1143 * anything, it might because there are
1144 * no reclaimable pages under this hierarchy
1145 */
1146 if (!check_soft || !total) {
1147 css_put(&victim->css);
1148 break;
1149 }
1150 /*
1151 * We want to do more targetted reclaim.
1152 * excess >> 2 is not to excessive so as to
1153 * reclaim too much, nor too less that we keep
1154 * coming back to reclaim from this cgroup
1155 */
1156 if (total >= (excess >> 2) ||
1157 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1158 css_put(&victim->css);
1159 break;
1160 }
1161 }
1162 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001163 if (!mem_cgroup_local_usage(&victim->stat)) {
1164 /* this cgroup's local usage == 0 */
1165 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001166 continue;
1167 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001168 /* we use swappiness of local cgroup */
Balbir Singh4e416952009-09-23 15:56:39 -07001169 if (check_soft)
1170 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
1171 noswap, get_swappiness(victim), zone,
1172 zone->zone_pgdat->node_id);
1173 else
1174 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
1175 noswap, get_swappiness(victim));
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001176 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001177 /*
1178 * At shrinking usage, we can't check we should stop here or
1179 * reclaim more. It's depends on callers. last_scanned_child
1180 * will work enough for keeping fairness under tree.
1181 */
1182 if (shrink)
1183 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001184 total += ret;
Balbir Singh4e416952009-09-23 15:56:39 -07001185 if (check_soft) {
1186 if (res_counter_check_under_soft_limit(&root_mem->res))
1187 return total;
1188 } else if (mem_cgroup_check_under_limit(root_mem))
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001189 return 1 + total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001190 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001191 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001192}
1193
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001194bool mem_cgroup_oom_called(struct task_struct *task)
1195{
1196 bool ret = false;
1197 struct mem_cgroup *mem;
1198 struct mm_struct *mm;
1199
1200 rcu_read_lock();
1201 mm = task->mm;
1202 if (!mm)
1203 mm = &init_mm;
1204 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
1205 if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
1206 ret = true;
1207 rcu_read_unlock();
1208 return ret;
1209}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001210
1211static int record_last_oom_cb(struct mem_cgroup *mem, void *data)
1212{
1213 mem->last_oom_jiffies = jiffies;
1214 return 0;
1215}
1216
1217static void record_last_oom(struct mem_cgroup *mem)
1218{
1219 mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb);
1220}
1221
Balbir Singhd69b0422009-06-17 16:26:34 -07001222/*
1223 * Currently used to update mapped file statistics, but the routine can be
1224 * generalized to update other statistics as well.
1225 */
1226void mem_cgroup_update_mapped_file_stat(struct page *page, int val)
1227{
1228 struct mem_cgroup *mem;
1229 struct mem_cgroup_stat *stat;
1230 struct mem_cgroup_stat_cpu *cpustat;
1231 int cpu;
1232 struct page_cgroup *pc;
1233
1234 if (!page_is_file_cache(page))
1235 return;
1236
1237 pc = lookup_page_cgroup(page);
1238 if (unlikely(!pc))
1239 return;
1240
1241 lock_page_cgroup(pc);
1242 mem = pc->mem_cgroup;
1243 if (!mem)
1244 goto done;
1245
1246 if (!PageCgroupUsed(pc))
1247 goto done;
1248
1249 /*
1250 * Preemption is already disabled, we don't need get_cpu()
1251 */
1252 cpu = smp_processor_id();
1253 stat = &mem->stat;
1254 cpustat = &stat->cpustat[cpu];
1255
1256 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE, val);
1257done:
1258 unlock_page_cgroup(pc);
1259}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001260
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001261/*
1262 * Unlike exported interface, "oom" parameter is added. if oom==true,
1263 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001264 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001265static int __mem_cgroup_try_charge(struct mm_struct *mm,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001266 gfp_t gfp_mask, struct mem_cgroup **memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -07001267 bool oom, struct page *page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001268{
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001269 struct mem_cgroup *mem, *mem_over_limit;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001270 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001271 struct res_counter *fail_res;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001272
1273 if (unlikely(test_thread_flag(TIF_MEMDIE))) {
1274 /* Don't account this! */
1275 *memcg = NULL;
1276 return 0;
1277 }
1278
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001279 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08001280 * We always charge the cgroup the mm_struct belongs to.
1281 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001282 * thread group leader migrates. It's possible that mm is not
1283 * set, if so charge the init_mm (happens for pagecache usage).
1284 */
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001285 mem = *memcg;
1286 if (likely(!mem)) {
1287 mem = try_get_mem_cgroup_from_mm(mm);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001288 *memcg = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001289 } else {
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001290 css_get(&mem->css);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001291 }
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001292 if (unlikely(!mem))
1293 return 0;
1294
Nikanth Karthikesan46f7e602009-05-28 14:34:41 -07001295 VM_BUG_ON(css_is_removed(&mem->css));
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001296
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001297 while (1) {
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001298 int ret = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001299 unsigned long flags = 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001300
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001301 if (mem_cgroup_is_root(mem))
1302 goto done;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001303 ret = res_counter_charge(&mem->res, PAGE_SIZE, &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001304 if (likely(!ret)) {
1305 if (!do_swap_account)
1306 break;
Balbir Singh28dbc4b2009-01-07 18:08:05 -08001307 ret = res_counter_charge(&mem->memsw, PAGE_SIZE,
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001308 &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001309 if (likely(!ret))
1310 break;
1311 /* mem+swap counter fails */
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001312 res_counter_uncharge(&mem->res, PAGE_SIZE);
Balbir Singh75822b42009-09-23 15:56:38 -07001313 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001314 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1315 memsw);
1316 } else
1317 /* mem counter fails */
1318 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1319 res);
1320
Hugh Dickins3be91272008-02-07 00:14:19 -08001321 if (!(gfp_mask & __GFP_WAIT))
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001322 goto nomem;
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001323
Balbir Singh4e416952009-09-23 15:56:39 -07001324 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
1325 gfp_mask, flags);
Daisuke Nishimura4d1c6272009-01-15 13:51:14 -08001326 if (ret)
1327 continue;
Balbir Singh66e17072008-02-07 00:13:56 -08001328
1329 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001330 * try_to_free_mem_cgroup_pages() might not give us a full
1331 * picture of reclaim. Some pages are reclaimed and might be
1332 * moved to swap cache or just unmapped from the cgroup.
1333 * Check the limit again to see if the reclaim reduced the
1334 * current usage of the cgroup before giving up
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001335 *
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001336 */
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -08001337 if (mem_cgroup_check_under_limit(mem_over_limit))
1338 continue;
Hugh Dickins3be91272008-02-07 00:14:19 -08001339
1340 if (!nr_retries--) {
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001341 if (oom) {
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08001342 mutex_lock(&memcg_tasklist);
KAMEZAWA Hiroyuki88700752009-01-07 18:08:09 -08001343 mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08001344 mutex_unlock(&memcg_tasklist);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001345 record_last_oom(mem_over_limit);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001346 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001347 goto nomem;
Balbir Singh66e17072008-02-07 00:13:56 -08001348 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001349 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07001350 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001351 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
1352 * if they exceeds softlimit.
Balbir Singhf64c3f52009-09-23 15:56:37 -07001353 */
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001354 if (mem_cgroup_soft_limit_check(mem))
1355 mem_cgroup_update_tree(mem, page);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001356done:
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001357 return 0;
1358nomem:
1359 css_put(&mem->css);
1360 return -ENOMEM;
1361}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001362
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001363/*
1364 * A helper function to get mem_cgroup from ID. must be called under
1365 * rcu_read_lock(). The caller must check css_is_removed() or some if
1366 * it's concern. (dropping refcnt from swap can be called against removed
1367 * memcg.)
1368 */
1369static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
1370{
1371 struct cgroup_subsys_state *css;
1372
1373 /* ID 0 is unused ID */
1374 if (!id)
1375 return NULL;
1376 css = css_lookup(&mem_cgroup_subsys, id);
1377 if (!css)
1378 return NULL;
1379 return container_of(css, struct mem_cgroup, css);
1380}
1381
Wu Fengguange42d9d52009-12-16 12:19:59 +01001382struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001383{
Wu Fengguange42d9d52009-12-16 12:19:59 +01001384 struct mem_cgroup *mem = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001385 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001386 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001387 swp_entry_t ent;
1388
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001389 VM_BUG_ON(!PageLocked(page));
1390
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001391 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001392 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001393 if (PageCgroupUsed(pc)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001394 mem = pc->mem_cgroup;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001395 if (mem && !css_tryget(&mem->css))
1396 mem = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01001397 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001398 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001399 id = lookup_swap_cgroup(ent);
1400 rcu_read_lock();
1401 mem = mem_cgroup_lookup(id);
1402 if (mem && !css_tryget(&mem->css))
1403 mem = NULL;
1404 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001405 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001406 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001407 return mem;
1408}
1409
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001410/*
Daisuke Nishimuraa5e924f2009-01-07 18:08:28 -08001411 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001412 * USED state. If already USED, uncharge and return.
1413 */
1414
1415static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1416 struct page_cgroup *pc,
1417 enum charge_type ctype)
1418{
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001419 /* try_charge() can return NULL to *memcg, taking care of it. */
1420 if (!mem)
1421 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001422
1423 lock_page_cgroup(pc);
1424 if (unlikely(PageCgroupUsed(pc))) {
1425 unlock_page_cgroup(pc);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001426 if (!mem_cgroup_is_root(mem)) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001427 res_counter_uncharge(&mem->res, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001428 if (do_swap_account)
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001429 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001430 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001431 css_put(&mem->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001432 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001433 }
Balbir Singh4b3bde42009-09-23 15:56:32 -07001434
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001435 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07001436 /*
1437 * We access a page_cgroup asynchronously without lock_page_cgroup().
1438 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
1439 * is accessed after testing USED bit. To make pc->mem_cgroup visible
1440 * before USED bit, we need memory barrier here.
1441 * See mem_cgroup_add_lru_list(), etc.
1442 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001443 smp_wmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -07001444 switch (ctype) {
1445 case MEM_CGROUP_CHARGE_TYPE_CACHE:
1446 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
1447 SetPageCgroupCache(pc);
1448 SetPageCgroupUsed(pc);
1449 break;
1450 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1451 ClearPageCgroupCache(pc);
1452 SetPageCgroupUsed(pc);
1453 break;
1454 default:
1455 break;
1456 }
Hugh Dickins3be91272008-02-07 00:14:19 -08001457
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001458 mem_cgroup_charge_statistics(mem, pc, true);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001459
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001460 unlock_page_cgroup(pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001461}
1462
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001463/**
1464 * mem_cgroup_move_account - move account of the page
1465 * @pc: page_cgroup of the page.
1466 * @from: mem_cgroup which the page is moved from.
1467 * @to: mem_cgroup which the page is moved to. @from != @to.
1468 *
1469 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001470 * - page is not on LRU (isolate_page() is useful.)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001471 *
1472 * returns 0 at success,
1473 * returns -EBUSY when lock is busy or "pc" is unstable.
1474 *
1475 * This function does "uncharge" from old cgroup but doesn't do "charge" to
1476 * new cgroup. It should be done by a caller.
1477 */
1478
1479static int mem_cgroup_move_account(struct page_cgroup *pc,
1480 struct mem_cgroup *from, struct mem_cgroup *to)
1481{
1482 struct mem_cgroup_per_zone *from_mz, *to_mz;
1483 int nid, zid;
1484 int ret = -EBUSY;
Balbir Singhd69b0422009-06-17 16:26:34 -07001485 struct page *page;
1486 int cpu;
1487 struct mem_cgroup_stat *stat;
1488 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001489
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001490 VM_BUG_ON(from == to);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001491 VM_BUG_ON(PageLRU(pc->page));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001492
1493 nid = page_cgroup_nid(pc);
1494 zid = page_cgroup_zid(pc);
1495 from_mz = mem_cgroup_zoneinfo(from, nid, zid);
1496 to_mz = mem_cgroup_zoneinfo(to, nid, zid);
1497
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001498 if (!trylock_page_cgroup(pc))
1499 return ret;
1500
1501 if (!PageCgroupUsed(pc))
1502 goto out;
1503
1504 if (pc->mem_cgroup != from)
1505 goto out;
1506
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001507 if (!mem_cgroup_is_root(from))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001508 res_counter_uncharge(&from->res, PAGE_SIZE);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001509 mem_cgroup_charge_statistics(from, pc, false);
Balbir Singhd69b0422009-06-17 16:26:34 -07001510
1511 page = pc->page;
1512 if (page_is_file_cache(page) && page_mapped(page)) {
1513 cpu = smp_processor_id();
1514 /* Update mapped_file data for mem_cgroup "from" */
1515 stat = &from->stat;
1516 cpustat = &stat->cpustat[cpu];
1517 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1518 -1);
1519
1520 /* Update mapped_file data for mem_cgroup "to" */
1521 stat = &to->stat;
1522 cpustat = &stat->cpustat[cpu];
1523 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1524 1);
1525 }
1526
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001527 if (do_swap_account && !mem_cgroup_is_root(from))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001528 res_counter_uncharge(&from->memsw, PAGE_SIZE);
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001529 css_put(&from->css);
1530
1531 css_get(&to->css);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001532 pc->mem_cgroup = to;
1533 mem_cgroup_charge_statistics(to, pc, true);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001534 ret = 0;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001535out:
1536 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001537 /*
1538 * We charges against "to" which may not have any tasks. Then, "to"
1539 * can be under rmdir(). But in current implementation, caller of
1540 * this function is just force_empty() and it's garanteed that
1541 * "to" is never removed. So, we don't check rmdir status here.
1542 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001543 return ret;
1544}
1545
1546/*
1547 * move charges to its parent.
1548 */
1549
1550static int mem_cgroup_move_parent(struct page_cgroup *pc,
1551 struct mem_cgroup *child,
1552 gfp_t gfp_mask)
1553{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001554 struct page *page = pc->page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001555 struct cgroup *cg = child->css.cgroup;
1556 struct cgroup *pcg = cg->parent;
1557 struct mem_cgroup *parent;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001558 int ret;
1559
1560 /* Is ROOT ? */
1561 if (!pcg)
1562 return -EINVAL;
1563
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001564
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001565 parent = mem_cgroup_from_cont(pcg);
1566
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001567
Balbir Singhf64c3f52009-09-23 15:56:37 -07001568 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false, page);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001569 if (ret || !parent)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001570 return ret;
1571
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001572 if (!get_page_unless_zero(page)) {
1573 ret = -EBUSY;
1574 goto uncharge;
1575 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001576
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001577 ret = isolate_lru_page(page);
1578
1579 if (ret)
1580 goto cancel;
1581
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001582 ret = mem_cgroup_move_account(pc, child, parent);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001583
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001584 putback_lru_page(page);
1585 if (!ret) {
1586 put_page(page);
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001587 /* drop extra refcnt by try_charge() */
1588 css_put(&parent->css);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001589 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001590 }
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001591
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001592cancel:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001593 put_page(page);
1594uncharge:
1595 /* drop extra refcnt by try_charge() */
1596 css_put(&parent->css);
1597 /* uncharge if move fails */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001598 if (!mem_cgroup_is_root(parent)) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001599 res_counter_uncharge(&parent->res, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001600 if (do_swap_account)
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001601 res_counter_uncharge(&parent->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001602 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001603 return ret;
1604}
1605
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001606/*
1607 * Charge the memory controller for page usage.
1608 * Return
1609 * 0 if the charge was successful
1610 * < 0 if the cgroup is over its limit
1611 */
1612static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
1613 gfp_t gfp_mask, enum charge_type ctype,
1614 struct mem_cgroup *memcg)
1615{
1616 struct mem_cgroup *mem;
1617 struct page_cgroup *pc;
1618 int ret;
1619
1620 pc = lookup_page_cgroup(page);
1621 /* can happen at boot */
1622 if (unlikely(!pc))
1623 return 0;
1624 prefetchw(pc);
1625
1626 mem = memcg;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001627 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true, page);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001628 if (ret || !mem)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001629 return ret;
1630
1631 __mem_cgroup_commit_charge(mem, pc, ctype);
1632 return 0;
1633}
1634
1635int mem_cgroup_newpage_charge(struct page *page,
1636 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001637{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001638 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001639 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001640 if (PageCompound(page))
1641 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001642 /*
1643 * If already mapped, we don't have to account.
1644 * If page cache, page->mapping has address_space.
1645 * But page->mapping may have out-of-use anon_vma pointer,
1646 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
1647 * is NULL.
1648 */
1649 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
1650 return 0;
1651 if (unlikely(!mm))
1652 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001653 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001654 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001655}
1656
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001657static void
1658__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1659 enum charge_type ctype);
1660
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001661int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
1662 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08001663{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001664 struct mem_cgroup *mem = NULL;
1665 int ret;
1666
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001667 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001668 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001669 if (PageCompound(page))
1670 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001671 /*
1672 * Corner case handling. This is called from add_to_page_cache()
1673 * in usual. But some FS (shmem) precharges this page before calling it
1674 * and call add_to_page_cache() with GFP_NOWAIT.
1675 *
1676 * For GFP_NOWAIT case, the page may be pre-charged before calling
1677 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
1678 * charge twice. (It works but has to pay a bit larger cost.)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001679 * And when the page is SwapCache, it should take swap information
1680 * into account. This is under lock_page() now.
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001681 */
1682 if (!(gfp_mask & __GFP_WAIT)) {
1683 struct page_cgroup *pc;
1684
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001685
1686 pc = lookup_page_cgroup(page);
1687 if (!pc)
1688 return 0;
1689 lock_page_cgroup(pc);
1690 if (PageCgroupUsed(pc)) {
1691 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001692 return 0;
1693 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001694 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001695 }
1696
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001697 if (unlikely(!mm && !mem))
Balbir Singh8697d332008-02-07 00:13:59 -08001698 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001699
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -07001700 if (page_is_file_cache(page))
1701 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001702 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001703
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001704 /* shmem */
1705 if (PageSwapCache(page)) {
1706 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1707 if (!ret)
1708 __mem_cgroup_commit_charge_swapin(page, mem,
1709 MEM_CGROUP_CHARGE_TYPE_SHMEM);
1710 } else
1711 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
1712 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001713
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001714 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001715}
1716
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001717/*
1718 * While swap-in, try_charge -> commit or cancel, the page is locked.
1719 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02001720 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001721 * "commit()" or removed by "cancel()"
1722 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001723int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
1724 struct page *page,
1725 gfp_t mask, struct mem_cgroup **ptr)
1726{
1727 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001728 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001729
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001730 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001731 return 0;
1732
1733 if (!do_swap_account)
1734 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001735 /*
1736 * A racing thread's fault, or swapoff, may have already updated
Hugh Dickins407f9c82009-12-14 17:59:30 -08001737 * the pte, and even removed page from swap cache: in those cases
1738 * do_swap_page()'s pte_same() test will fail; but there's also a
1739 * KSM case which does need to charge the page.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001740 */
1741 if (!PageSwapCache(page))
Hugh Dickins407f9c82009-12-14 17:59:30 -08001742 goto charge_cur_mm;
Wu Fengguange42d9d52009-12-16 12:19:59 +01001743 mem = try_get_mem_cgroup_from_page(page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001744 if (!mem)
1745 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001746 *ptr = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001747 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true, page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001748 /* drop extra refcnt from tryget */
1749 css_put(&mem->css);
1750 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001751charge_cur_mm:
1752 if (unlikely(!mm))
1753 mm = &init_mm;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001754 return __mem_cgroup_try_charge(mm, mask, ptr, true, page);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001755}
1756
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001757static void
1758__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1759 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001760{
1761 struct page_cgroup *pc;
1762
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001763 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001764 return;
1765 if (!ptr)
1766 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001767 cgroup_exclude_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001768 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001769 mem_cgroup_lru_del_before_commit_swapcache(page);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001770 __mem_cgroup_commit_charge(ptr, pc, ctype);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001771 mem_cgroup_lru_add_after_commit_swapcache(page);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001772 /*
1773 * Now swap is on-memory. This means this page may be
1774 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08001775 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
1776 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
1777 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001778 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08001779 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001780 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001781 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001782 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001783
1784 id = swap_cgroup_record(ent, 0);
1785 rcu_read_lock();
1786 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001787 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001788 /*
1789 * This recorded memcg can be obsolete one. So, avoid
1790 * calling css_tryget
1791 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001792 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001793 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001794 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001795 mem_cgroup_put(memcg);
1796 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001797 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001798 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001799 /*
1800 * At swapin, we may charge account against cgroup which has no tasks.
1801 * So, rmdir()->pre_destroy() can be called while we do this charge.
1802 * In that case, we need to call pre_destroy() again. check it here.
1803 */
1804 cgroup_release_and_wakeup_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001805}
1806
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001807void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
1808{
1809 __mem_cgroup_commit_charge_swapin(page, ptr,
1810 MEM_CGROUP_CHARGE_TYPE_MAPPED);
1811}
1812
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001813void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
1814{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001815 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001816 return;
1817 if (!mem)
1818 return;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001819 if (!mem_cgroup_is_root(mem)) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001820 res_counter_uncharge(&mem->res, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001821 if (do_swap_account)
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001822 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001823 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001824 css_put(&mem->css);
1825}
1826
1827
Balbir Singh8697d332008-02-07 00:13:59 -08001828/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001829 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001830 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001831static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001832__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001833{
Hugh Dickins82895462008-03-04 14:29:08 -08001834 struct page_cgroup *pc;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001835 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001836 struct mem_cgroup_per_zone *mz;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001837
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001838 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001839 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07001840
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001841 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001842 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001843
Balbir Singh8697d332008-02-07 00:13:59 -08001844 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08001845 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08001846 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001847 pc = lookup_page_cgroup(page);
1848 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001849 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001850
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001851 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001852
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001853 mem = pc->mem_cgroup;
1854
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001855 if (!PageCgroupUsed(pc))
1856 goto unlock_out;
1857
1858 switch (ctype) {
1859 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07001860 case MEM_CGROUP_CHARGE_TYPE_DROP:
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001861 if (page_mapped(page))
1862 goto unlock_out;
1863 break;
1864 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
1865 if (!PageAnon(page)) { /* Shared memory */
1866 if (page->mapping && !page_is_file_cache(page))
1867 goto unlock_out;
1868 } else if (page_mapped(page)) /* Anon */
1869 goto unlock_out;
1870 break;
1871 default:
1872 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001873 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001874
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001875 if (!mem_cgroup_is_root(mem)) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001876 res_counter_uncharge(&mem->res, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001877 if (do_swap_account &&
1878 (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001879 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001880 }
1881 if (ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1882 mem_cgroup_swap_statistics(mem, true);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001883 mem_cgroup_charge_statistics(mem, pc, false);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001884
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001885 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001886 /*
1887 * pc->mem_cgroup is not cleared here. It will be accessed when it's
1888 * freed from LRU. This is safe because uncharged page is expected not
1889 * to be reused (freed soon). Exception is SwapCache, it's handled by
1890 * special functions.
1891 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08001892
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001893 mz = page_cgroup_zoneinfo(pc);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001894 unlock_page_cgroup(pc);
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08001895
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001896 if (mem_cgroup_soft_limit_check(mem))
Balbir Singhf64c3f52009-09-23 15:56:37 -07001897 mem_cgroup_update_tree(mem, page);
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08001898 /* at swapout, this memcg will be accessed to record to swap */
1899 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1900 css_put(&mem->css);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001901
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001902 return mem;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001903
1904unlock_out:
1905 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001906 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08001907}
1908
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001909void mem_cgroup_uncharge_page(struct page *page)
1910{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001911 /* early check. */
1912 if (page_mapped(page))
1913 return;
1914 if (page->mapping && !PageAnon(page))
1915 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001916 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
1917}
1918
1919void mem_cgroup_uncharge_cache_page(struct page *page)
1920{
1921 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07001922 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001923 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
1924}
1925
Daisuke Nishimurae767e052009-05-28 14:34:28 -07001926#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001927/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07001928 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001929 * memcg information is recorded to swap_cgroup of "ent"
1930 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07001931void
1932mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001933{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001934 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07001935 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001936
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07001937 if (!swapout) /* this was a swap cache but the swap is unused ! */
1938 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
1939
1940 memcg = __mem_cgroup_uncharge_common(page, ctype);
1941
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001942 /* record memcg information */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07001943 if (do_swap_account && swapout && memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001944 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001945 mem_cgroup_get(memcg);
1946 }
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07001947 if (swapout && memcg)
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08001948 css_put(&memcg->css);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001949}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07001950#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001951
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001952#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1953/*
1954 * called from swap_entry_free(). remove record in swap_cgroup and
1955 * uncharge "memsw" account.
1956 */
1957void mem_cgroup_uncharge_swap(swp_entry_t ent)
1958{
1959 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001960 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001961
1962 if (!do_swap_account)
1963 return;
1964
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001965 id = swap_cgroup_record(ent, 0);
1966 rcu_read_lock();
1967 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001968 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001969 /*
1970 * We uncharge this because swap is freed.
1971 * This memcg can be obsolete one. We avoid calling css_tryget
1972 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001973 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001974 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001975 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001976 mem_cgroup_put(memcg);
1977 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001978 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001979}
1980#endif
1981
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001982/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001983 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
1984 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001985 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001986int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001987{
1988 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001989 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001990 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001991
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001992 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07001993 return 0;
1994
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001995 pc = lookup_page_cgroup(page);
1996 lock_page_cgroup(pc);
1997 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001998 mem = pc->mem_cgroup;
1999 css_get(&mem->css);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08002000 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002001 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002002
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002003 if (mem) {
Balbir Singhf64c3f52009-09-23 15:56:37 -07002004 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false,
2005 page);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002006 css_put(&mem->css);
2007 }
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002008 *ptr = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002009 return ret;
2010}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08002011
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002012/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002013void mem_cgroup_end_migration(struct mem_cgroup *mem,
2014 struct page *oldpage, struct page *newpage)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002015{
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002016 struct page *target, *unused;
2017 struct page_cgroup *pc;
2018 enum charge_type ctype;
2019
2020 if (!mem)
2021 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002022 cgroup_exclude_rmdir(&mem->css);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002023 /* at migration success, oldpage->mapping is NULL. */
2024 if (oldpage->mapping) {
2025 target = oldpage;
2026 unused = NULL;
2027 } else {
2028 target = newpage;
2029 unused = oldpage;
2030 }
2031
2032 if (PageAnon(target))
2033 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
2034 else if (page_is_file_cache(target))
2035 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
2036 else
2037 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
2038
2039 /* unused page is not on radix-tree now. */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002040 if (unused)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002041 __mem_cgroup_uncharge_common(unused, ctype);
2042
2043 pc = lookup_page_cgroup(target);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002044 /*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002045 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
2046 * So, double-counting is effectively avoided.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002047 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002048 __mem_cgroup_commit_charge(mem, pc, ctype);
2049
2050 /*
2051 * Both of oldpage and newpage are still under lock_page().
2052 * Then, we don't have to care about race in radix-tree.
2053 * But we have to be careful that this page is unmapped or not.
2054 *
2055 * There is a case for !page_mapped(). At the start of
2056 * migration, oldpage was mapped. But now, it's zapped.
2057 * But we know *target* page is not freed/reused under us.
2058 * mem_cgroup_uncharge_page() does all necessary checks.
2059 */
2060 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
2061 mem_cgroup_uncharge_page(target);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002062 /*
2063 * At migration, we may charge account against cgroup which has no tasks
2064 * So, rmdir()->pre_destroy() can be called while we do this charge.
2065 * In that case, we need to call pre_destroy() again. check it here.
2066 */
2067 cgroup_release_and_wakeup_rmdir(&mem->css);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002068}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08002069
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002070/*
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002071 * A call to try to shrink memory usage on charge failure at shmem's swapin.
2072 * Calling hierarchical_reclaim is not enough because we should update
2073 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
2074 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
2075 * not from the memcg which this page would be charged to.
2076 * try_charge_swapin does all of these works properly.
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002077 */
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002078int mem_cgroup_shmem_charge_fallback(struct page *page,
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002079 struct mm_struct *mm,
2080 gfp_t gfp_mask)
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002081{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002082 struct mem_cgroup *mem = NULL;
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002083 int ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002084
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002085 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002086 return 0;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002087
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002088 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2089 if (!ret)
2090 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002091
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002092 return ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002093}
2094
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002095static DEFINE_MUTEX(set_limit_mutex);
2096
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08002097static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002098 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002099{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002100 int retry_count;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002101 int progress;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002102 u64 memswlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002103 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002104 int children = mem_cgroup_count_children(memcg);
2105 u64 curusage, oldusage;
2106
2107 /*
2108 * For keeping hierarchical_reclaim simple, how long we should retry
2109 * is depends on callers. We set our retry-count to be function
2110 * of # of children which we should visit in this loop.
2111 */
2112 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
2113
2114 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002115
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002116 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002117 if (signal_pending(current)) {
2118 ret = -EINTR;
2119 break;
2120 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002121 /*
2122 * Rather than hide all in some function, I do this in
2123 * open coded manner. You see what this really does.
2124 * We have to guarantee mem->res.limit < mem->memsw.limit.
2125 */
2126 mutex_lock(&set_limit_mutex);
2127 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2128 if (memswlimit < val) {
2129 ret = -EINVAL;
2130 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002131 break;
2132 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002133 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07002134 if (!ret) {
2135 if (memswlimit == val)
2136 memcg->memsw_is_minimum = true;
2137 else
2138 memcg->memsw_is_minimum = false;
2139 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002140 mutex_unlock(&set_limit_mutex);
2141
2142 if (!ret)
2143 break;
2144
Balbir Singh4e416952009-09-23 15:56:39 -07002145 progress = mem_cgroup_hierarchical_reclaim(memcg, NULL,
2146 GFP_KERNEL,
2147 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002148 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
2149 /* Usage is reduced ? */
2150 if (curusage >= oldusage)
2151 retry_count--;
2152 else
2153 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002154 }
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002155
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002156 return ret;
2157}
2158
Li Zefan338c8432009-06-17 16:27:15 -07002159static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
2160 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002161{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002162 int retry_count;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002163 u64 memlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002164 int children = mem_cgroup_count_children(memcg);
2165 int ret = -EBUSY;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002166
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002167 /* see mem_cgroup_resize_res_limit */
2168 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
2169 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002170 while (retry_count) {
2171 if (signal_pending(current)) {
2172 ret = -EINTR;
2173 break;
2174 }
2175 /*
2176 * Rather than hide all in some function, I do this in
2177 * open coded manner. You see what this really does.
2178 * We have to guarantee mem->res.limit < mem->memsw.limit.
2179 */
2180 mutex_lock(&set_limit_mutex);
2181 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2182 if (memlimit > val) {
2183 ret = -EINVAL;
2184 mutex_unlock(&set_limit_mutex);
2185 break;
2186 }
2187 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07002188 if (!ret) {
2189 if (memlimit == val)
2190 memcg->memsw_is_minimum = true;
2191 else
2192 memcg->memsw_is_minimum = false;
2193 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002194 mutex_unlock(&set_limit_mutex);
2195
2196 if (!ret)
2197 break;
2198
Balbir Singh4e416952009-09-23 15:56:39 -07002199 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh75822b42009-09-23 15:56:38 -07002200 MEM_CGROUP_RECLAIM_NOSWAP |
2201 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002202 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002203 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002204 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002205 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002206 else
2207 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002208 }
2209 return ret;
2210}
2211
Balbir Singh4e416952009-09-23 15:56:39 -07002212unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
2213 gfp_t gfp_mask, int nid,
2214 int zid)
2215{
2216 unsigned long nr_reclaimed = 0;
2217 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
2218 unsigned long reclaimed;
2219 int loop = 0;
2220 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002221 unsigned long long excess;
Balbir Singh4e416952009-09-23 15:56:39 -07002222
2223 if (order > 0)
2224 return 0;
2225
2226 mctz = soft_limit_tree_node_zone(nid, zid);
2227 /*
2228 * This loop can run a while, specially if mem_cgroup's continuously
2229 * keep exceeding their soft limit and putting the system under
2230 * pressure
2231 */
2232 do {
2233 if (next_mz)
2234 mz = next_mz;
2235 else
2236 mz = mem_cgroup_largest_soft_limit_node(mctz);
2237 if (!mz)
2238 break;
2239
2240 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
2241 gfp_mask,
2242 MEM_CGROUP_RECLAIM_SOFT);
2243 nr_reclaimed += reclaimed;
2244 spin_lock(&mctz->lock);
2245
2246 /*
2247 * If we failed to reclaim anything from this memory cgroup
2248 * it is time to move on to the next cgroup
2249 */
2250 next_mz = NULL;
2251 if (!reclaimed) {
2252 do {
2253 /*
2254 * Loop until we find yet another one.
2255 *
2256 * By the time we get the soft_limit lock
2257 * again, someone might have aded the
2258 * group back on the RB tree. Iterate to
2259 * make sure we get a different mem.
2260 * mem_cgroup_largest_soft_limit_node returns
2261 * NULL if no other cgroup is present on
2262 * the tree
2263 */
2264 next_mz =
2265 __mem_cgroup_largest_soft_limit_node(mctz);
2266 if (next_mz == mz) {
2267 css_put(&next_mz->mem->css);
2268 next_mz = NULL;
2269 } else /* next_mz == NULL or other memcg */
2270 break;
2271 } while (1);
2272 }
Balbir Singh4e416952009-09-23 15:56:39 -07002273 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002274 excess = res_counter_soft_limit_excess(&mz->mem->res);
Balbir Singh4e416952009-09-23 15:56:39 -07002275 /*
2276 * One school of thought says that we should not add
2277 * back the node to the tree if reclaim returns 0.
2278 * But our reclaim could return 0, simply because due
2279 * to priority we are exposing a smaller subset of
2280 * memory to reclaim from. Consider this as a longer
2281 * term TODO.
2282 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002283 /* If excess == 0, no tree ops */
2284 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07002285 spin_unlock(&mctz->lock);
2286 css_put(&mz->mem->css);
2287 loop++;
2288 /*
2289 * Could not reclaim anything and there are no more
2290 * mem cgroups to try or we seem to be looping without
2291 * reclaiming anything.
2292 */
2293 if (!nr_reclaimed &&
2294 (next_mz == NULL ||
2295 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
2296 break;
2297 } while (!nr_reclaimed);
2298 if (next_mz)
2299 css_put(&next_mz->mem->css);
2300 return nr_reclaimed;
2301}
2302
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002303/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002304 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002305 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
2306 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002307static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002308 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002309{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002310 struct zone *zone;
2311 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002312 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002313 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002314 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002315 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002316
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002317 zone = &NODE_DATA(node)->node_zones[zid];
2318 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07002319 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002320
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002321 loop = MEM_CGROUP_ZSTAT(mz, lru);
2322 /* give some margin against EBUSY etc...*/
2323 loop += 256;
2324 busy = NULL;
2325 while (loop--) {
2326 ret = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002327 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002328 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002329 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002330 break;
2331 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002332 pc = list_entry(list->prev, struct page_cgroup, lru);
2333 if (busy == pc) {
2334 list_move(&pc->lru, list);
2335 busy = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002336 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002337 continue;
2338 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002339 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002340
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -08002341 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002342 if (ret == -ENOMEM)
2343 break;
2344
2345 if (ret == -EBUSY || ret == -EINVAL) {
2346 /* found lock contention or "pc" is obsolete. */
2347 busy = pc;
2348 cond_resched();
2349 } else
2350 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002351 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002352
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002353 if (!ret && !list_empty(list))
2354 return -EBUSY;
2355 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002356}
2357
2358/*
2359 * make mem_cgroup's charge to be 0 if there is no task.
2360 * This enables deleting this mem_cgroup.
2361 */
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002362static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002363{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002364 int ret;
2365 int node, zid, shrink;
2366 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002367 struct cgroup *cgrp = mem->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08002368
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002369 css_get(&mem->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002370
2371 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002372 /* should free all ? */
2373 if (free_all)
2374 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002375move_account:
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002376 while (mem->res.usage > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002377 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002378 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002379 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002380 ret = -EINTR;
2381 if (signal_pending(current))
2382 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002383 /* This is for making all *used* pages to be on LRU. */
2384 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002385 ret = 0;
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08002386 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002387 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07002388 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002389 for_each_lru(l) {
2390 ret = mem_cgroup_force_empty_list(mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002391 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002392 if (ret)
2393 break;
2394 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002395 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002396 if (ret)
2397 break;
2398 }
2399 /* it seems parent cgroup doesn't have enough mem */
2400 if (ret == -ENOMEM)
2401 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002402 cond_resched();
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002403 }
2404 ret = 0;
2405out:
2406 css_put(&mem->css);
2407 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002408
2409try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002410 /* returns EBUSY if there is a task or if we come here twice. */
2411 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002412 ret = -EBUSY;
2413 goto out;
2414 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002415 /* we call try-to-free pages for make this cgroup empty */
2416 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002417 /* try to free all pages in this cgroup */
2418 shrink = 1;
2419 while (nr_retries && mem->res.usage > 0) {
2420 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002421
2422 if (signal_pending(current)) {
2423 ret = -EINTR;
2424 goto out;
2425 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002426 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
2427 false, get_swappiness(mem));
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002428 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002429 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002430 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02002431 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002432 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002433
2434 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002435 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002436 /* try move_account...there may be some *locked* pages. */
2437 if (mem->res.usage)
2438 goto move_account;
2439 ret = 0;
2440 goto out;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002441}
2442
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002443int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
2444{
2445 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
2446}
2447
2448
Balbir Singh18f59ea2009-01-07 18:08:07 -08002449static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
2450{
2451 return mem_cgroup_from_cont(cont)->use_hierarchy;
2452}
2453
2454static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
2455 u64 val)
2456{
2457 int retval = 0;
2458 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2459 struct cgroup *parent = cont->parent;
2460 struct mem_cgroup *parent_mem = NULL;
2461
2462 if (parent)
2463 parent_mem = mem_cgroup_from_cont(parent);
2464
2465 cgroup_lock();
2466 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002467 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08002468 * in the child subtrees. If it is unset, then the change can
2469 * occur, provided the current cgroup has no children.
2470 *
2471 * For the root cgroup, parent_mem is NULL, we allow value to be
2472 * set if there are no children.
2473 */
2474 if ((!parent_mem || !parent_mem->use_hierarchy) &&
2475 (val == 1 || val == 0)) {
2476 if (list_empty(&cont->children))
2477 mem->use_hierarchy = val;
2478 else
2479 retval = -EBUSY;
2480 } else
2481 retval = -EINVAL;
2482 cgroup_unlock();
2483
2484 return retval;
2485}
2486
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002487struct mem_cgroup_idx_data {
2488 s64 val;
2489 enum mem_cgroup_stat_index idx;
2490};
2491
2492static int
2493mem_cgroup_get_idx_stat(struct mem_cgroup *mem, void *data)
2494{
2495 struct mem_cgroup_idx_data *d = data;
2496 d->val += mem_cgroup_read_stat(&mem->stat, d->idx);
2497 return 0;
2498}
2499
2500static void
2501mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem,
2502 enum mem_cgroup_stat_index idx, s64 *val)
2503{
2504 struct mem_cgroup_idx_data d;
2505 d.idx = idx;
2506 d.val = 0;
2507 mem_cgroup_walk_tree(mem, &d, mem_cgroup_get_idx_stat);
2508 *val = d.val;
2509}
2510
Paul Menage2c3daa72008-04-29 00:59:58 -07002511static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002512{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002513 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002514 u64 idx_val, val;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002515 int type, name;
2516
2517 type = MEMFILE_TYPE(cft->private);
2518 name = MEMFILE_ATTR(cft->private);
2519 switch (type) {
2520 case _MEM:
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002521 if (name == RES_USAGE && mem_cgroup_is_root(mem)) {
2522 mem_cgroup_get_recursive_idx_stat(mem,
2523 MEM_CGROUP_STAT_CACHE, &idx_val);
2524 val = idx_val;
2525 mem_cgroup_get_recursive_idx_stat(mem,
2526 MEM_CGROUP_STAT_RSS, &idx_val);
2527 val += idx_val;
2528 val <<= PAGE_SHIFT;
2529 } else
2530 val = res_counter_read_u64(&mem->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002531 break;
2532 case _MEMSWAP:
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002533 if (name == RES_USAGE && mem_cgroup_is_root(mem)) {
2534 mem_cgroup_get_recursive_idx_stat(mem,
2535 MEM_CGROUP_STAT_CACHE, &idx_val);
2536 val = idx_val;
2537 mem_cgroup_get_recursive_idx_stat(mem,
2538 MEM_CGROUP_STAT_RSS, &idx_val);
2539 val += idx_val;
2540 mem_cgroup_get_recursive_idx_stat(mem,
2541 MEM_CGROUP_STAT_SWAPOUT, &idx_val);
2542 val <<= PAGE_SHIFT;
2543 } else
2544 val = res_counter_read_u64(&mem->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002545 break;
2546 default:
2547 BUG();
2548 break;
2549 }
2550 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002551}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002552/*
2553 * The user of this function is...
2554 * RES_LIMIT.
2555 */
Paul Menage856c13a2008-07-25 01:47:04 -07002556static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
2557 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002558{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002559 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002560 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002561 unsigned long long val;
2562 int ret;
2563
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002564 type = MEMFILE_TYPE(cft->private);
2565 name = MEMFILE_ATTR(cft->private);
2566 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002567 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07002568 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
2569 ret = -EINVAL;
2570 break;
2571 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002572 /* This function does all necessary parse...reuse it */
2573 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002574 if (ret)
2575 break;
2576 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002577 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002578 else
2579 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002580 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07002581 case RES_SOFT_LIMIT:
2582 ret = res_counter_memparse_write_strategy(buffer, &val);
2583 if (ret)
2584 break;
2585 /*
2586 * For memsw, soft limits are hard to implement in terms
2587 * of semantics, for now, we support soft limits for
2588 * control without swap
2589 */
2590 if (type == _MEM)
2591 ret = res_counter_set_soft_limit(&memcg->res, val);
2592 else
2593 ret = -EINVAL;
2594 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002595 default:
2596 ret = -EINVAL; /* should be BUG() ? */
2597 break;
2598 }
2599 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002600}
2601
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08002602static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
2603 unsigned long long *mem_limit, unsigned long long *memsw_limit)
2604{
2605 struct cgroup *cgroup;
2606 unsigned long long min_limit, min_memsw_limit, tmp;
2607
2608 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2609 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2610 cgroup = memcg->css.cgroup;
2611 if (!memcg->use_hierarchy)
2612 goto out;
2613
2614 while (cgroup->parent) {
2615 cgroup = cgroup->parent;
2616 memcg = mem_cgroup_from_cont(cgroup);
2617 if (!memcg->use_hierarchy)
2618 break;
2619 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
2620 min_limit = min(min_limit, tmp);
2621 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2622 min_memsw_limit = min(min_memsw_limit, tmp);
2623 }
2624out:
2625 *mem_limit = min_limit;
2626 *memsw_limit = min_memsw_limit;
2627 return;
2628}
2629
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002630static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002631{
2632 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002633 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002634
2635 mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002636 type = MEMFILE_TYPE(event);
2637 name = MEMFILE_ATTR(event);
2638 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002639 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002640 if (type == _MEM)
2641 res_counter_reset_max(&mem->res);
2642 else
2643 res_counter_reset_max(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002644 break;
2645 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002646 if (type == _MEM)
2647 res_counter_reset_failcnt(&mem->res);
2648 else
2649 res_counter_reset_failcnt(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002650 break;
2651 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07002652
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07002653 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002654}
2655
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002656
2657/* For read statistics */
2658enum {
2659 MCS_CACHE,
2660 MCS_RSS,
Balbir Singhd69b0422009-06-17 16:26:34 -07002661 MCS_MAPPED_FILE,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002662 MCS_PGPGIN,
2663 MCS_PGPGOUT,
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002664 MCS_SWAP,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002665 MCS_INACTIVE_ANON,
2666 MCS_ACTIVE_ANON,
2667 MCS_INACTIVE_FILE,
2668 MCS_ACTIVE_FILE,
2669 MCS_UNEVICTABLE,
2670 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002671};
2672
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002673struct mcs_total_stat {
2674 s64 stat[NR_MCS_STAT];
2675};
2676
2677struct {
2678 char *local_name;
2679 char *total_name;
2680} memcg_stat_strings[NR_MCS_STAT] = {
2681 {"cache", "total_cache"},
2682 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07002683 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002684 {"pgpgin", "total_pgpgin"},
2685 {"pgpgout", "total_pgpgout"},
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002686 {"swap", "total_swap"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002687 {"inactive_anon", "total_inactive_anon"},
2688 {"active_anon", "total_active_anon"},
2689 {"inactive_file", "total_inactive_file"},
2690 {"active_file", "total_active_file"},
2691 {"unevictable", "total_unevictable"}
2692};
2693
2694
2695static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
2696{
2697 struct mcs_total_stat *s = data;
2698 s64 val;
2699
2700 /* per cpu stat */
2701 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE);
2702 s->stat[MCS_CACHE] += val * PAGE_SIZE;
2703 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
2704 s->stat[MCS_RSS] += val * PAGE_SIZE;
Balbir Singhd69b0422009-06-17 16:26:34 -07002705 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_MAPPED_FILE);
2706 s->stat[MCS_MAPPED_FILE] += val * PAGE_SIZE;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002707 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT);
2708 s->stat[MCS_PGPGIN] += val;
2709 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT);
2710 s->stat[MCS_PGPGOUT] += val;
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002711 if (do_swap_account) {
2712 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_SWAPOUT);
2713 s->stat[MCS_SWAP] += val * PAGE_SIZE;
2714 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002715
2716 /* per zone stat */
2717 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
2718 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
2719 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
2720 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
2721 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
2722 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
2723 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
2724 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
2725 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
2726 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
2727 return 0;
2728}
2729
2730static void
2731mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
2732{
2733 mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
2734}
2735
Paul Menagec64745c2008-04-29 01:00:02 -07002736static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
2737 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002738{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002739 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002740 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002741 int i;
2742
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002743 memset(&mystat, 0, sizeof(mystat));
2744 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002745
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002746 for (i = 0; i < NR_MCS_STAT; i++) {
2747 if (i == MCS_SWAP && !do_swap_account)
2748 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002749 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002750 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002751
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002752 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08002753 {
2754 unsigned long long limit, memsw_limit;
2755 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
2756 cb->fill(cb, "hierarchical_memory_limit", limit);
2757 if (do_swap_account)
2758 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
2759 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002760
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002761 memset(&mystat, 0, sizeof(mystat));
2762 mem_cgroup_get_total_stat(mem_cont, &mystat);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002763 for (i = 0; i < NR_MCS_STAT; i++) {
2764 if (i == MCS_SWAP && !do_swap_account)
2765 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002766 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002767 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002768
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002769#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08002770 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002771
2772 {
2773 int nid, zid;
2774 struct mem_cgroup_per_zone *mz;
2775 unsigned long recent_rotated[2] = {0, 0};
2776 unsigned long recent_scanned[2] = {0, 0};
2777
2778 for_each_online_node(nid)
2779 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
2780 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
2781
2782 recent_rotated[0] +=
2783 mz->reclaim_stat.recent_rotated[0];
2784 recent_rotated[1] +=
2785 mz->reclaim_stat.recent_rotated[1];
2786 recent_scanned[0] +=
2787 mz->reclaim_stat.recent_scanned[0];
2788 recent_scanned[1] +=
2789 mz->reclaim_stat.recent_scanned[1];
2790 }
2791 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
2792 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
2793 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
2794 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
2795 }
2796#endif
2797
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002798 return 0;
2799}
2800
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002801static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
2802{
2803 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2804
2805 return get_swappiness(memcg);
2806}
2807
2808static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
2809 u64 val)
2810{
2811 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2812 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08002813
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002814 if (val > 100)
2815 return -EINVAL;
2816
2817 if (cgrp->parent == NULL)
2818 return -EINVAL;
2819
2820 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08002821
2822 cgroup_lock();
2823
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002824 /* If under hierarchy, only empty-root can set this value */
2825 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08002826 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
2827 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002828 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08002829 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002830
2831 spin_lock(&memcg->reclaim_param_lock);
2832 memcg->swappiness = val;
2833 spin_unlock(&memcg->reclaim_param_lock);
2834
Li Zefan068b38c2009-01-15 13:51:26 -08002835 cgroup_unlock();
2836
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002837 return 0;
2838}
2839
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002840
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002841static struct cftype mem_cgroup_files[] = {
2842 {
Balbir Singh0eea1032008-02-07 00:13:57 -08002843 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002844 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07002845 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002846 },
2847 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002848 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002849 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002850 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002851 .read_u64 = mem_cgroup_read,
2852 },
2853 {
Balbir Singh0eea1032008-02-07 00:13:57 -08002854 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002855 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07002856 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07002857 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002858 },
2859 {
Balbir Singh296c81d2009-09-23 15:56:36 -07002860 .name = "soft_limit_in_bytes",
2861 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
2862 .write_string = mem_cgroup_write,
2863 .read_u64 = mem_cgroup_read,
2864 },
2865 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002866 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002867 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002868 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07002869 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002870 },
Balbir Singh8697d332008-02-07 00:13:59 -08002871 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002872 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07002873 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002874 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002875 {
2876 .name = "force_empty",
2877 .trigger = mem_cgroup_force_empty_write,
2878 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08002879 {
2880 .name = "use_hierarchy",
2881 .write_u64 = mem_cgroup_hierarchy_write,
2882 .read_u64 = mem_cgroup_hierarchy_read,
2883 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002884 {
2885 .name = "swappiness",
2886 .read_u64 = mem_cgroup_swappiness_read,
2887 .write_u64 = mem_cgroup_swappiness_write,
2888 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002889};
2890
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002891#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2892static struct cftype memsw_cgroup_files[] = {
2893 {
2894 .name = "memsw.usage_in_bytes",
2895 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
2896 .read_u64 = mem_cgroup_read,
2897 },
2898 {
2899 .name = "memsw.max_usage_in_bytes",
2900 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
2901 .trigger = mem_cgroup_reset,
2902 .read_u64 = mem_cgroup_read,
2903 },
2904 {
2905 .name = "memsw.limit_in_bytes",
2906 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
2907 .write_string = mem_cgroup_write,
2908 .read_u64 = mem_cgroup_read,
2909 },
2910 {
2911 .name = "memsw.failcnt",
2912 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
2913 .trigger = mem_cgroup_reset,
2914 .read_u64 = mem_cgroup_read,
2915 },
2916};
2917
2918static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2919{
2920 if (!do_swap_account)
2921 return 0;
2922 return cgroup_add_files(cont, ss, memsw_cgroup_files,
2923 ARRAY_SIZE(memsw_cgroup_files));
2924};
2925#else
2926static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2927{
2928 return 0;
2929}
2930#endif
2931
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002932static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2933{
2934 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002935 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07002936 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07002937 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002938 /*
2939 * This routine is called against possible nodes.
2940 * But it's BUG to call kmalloc() against offline node.
2941 *
2942 * TODO: this routine can waste much memory for nodes which will
2943 * never be onlined. It's better to use memory hotplug callback
2944 * function.
2945 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07002946 if (!node_state(node, N_NORMAL_MEMORY))
2947 tmp = -1;
2948 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002949 if (!pn)
2950 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002951
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002952 mem->info.nodeinfo[node] = pn;
2953 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002954
2955 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
2956 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07002957 for_each_lru(l)
2958 INIT_LIST_HEAD(&mz->lists[l]);
Balbir Singhf64c3f52009-09-23 15:56:37 -07002959 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07002960 mz->on_tree = false;
2961 mz->mem = mem;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002962 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002963 return 0;
2964}
2965
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002966static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2967{
2968 kfree(mem->info.nodeinfo[node]);
2969}
2970
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002971static int mem_cgroup_size(void)
2972{
2973 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
2974 return sizeof(struct mem_cgroup) + cpustat_size;
2975}
2976
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002977static struct mem_cgroup *mem_cgroup_alloc(void)
2978{
2979 struct mem_cgroup *mem;
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002980 int size = mem_cgroup_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002981
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002982 if (size < PAGE_SIZE)
2983 mem = kmalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002984 else
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002985 mem = vmalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002986
2987 if (mem)
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002988 memset(mem, 0, size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002989 return mem;
2990}
2991
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002992/*
2993 * At destroying mem_cgroup, references from swap_cgroup can remain.
2994 * (scanning all at force_empty is too costly...)
2995 *
2996 * Instead of clearing all references at force_empty, we remember
2997 * the number of reference from swap_cgroup and free mem_cgroup when
2998 * it goes down to 0.
2999 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003000 * Removal of cgroup itself succeeds regardless of refs from swap.
3001 */
3002
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003003static void __mem_cgroup_free(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003004{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003005 int node;
3006
Balbir Singhf64c3f52009-09-23 15:56:37 -07003007 mem_cgroup_remove_from_trees(mem);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003008 free_css_id(&mem_cgroup_subsys, &mem->css);
3009
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003010 for_each_node_state(node, N_POSSIBLE)
3011 free_mem_cgroup_per_zone_info(mem, node);
3012
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003013 if (mem_cgroup_size() < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003014 kfree(mem);
3015 else
3016 vfree(mem);
3017}
3018
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003019static void mem_cgroup_get(struct mem_cgroup *mem)
3020{
3021 atomic_inc(&mem->refcnt);
3022}
3023
3024static void mem_cgroup_put(struct mem_cgroup *mem)
3025{
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003026 if (atomic_dec_and_test(&mem->refcnt)) {
3027 struct mem_cgroup *parent = parent_mem_cgroup(mem);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003028 __mem_cgroup_free(mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003029 if (parent)
3030 mem_cgroup_put(parent);
3031 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003032}
3033
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003034/*
3035 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
3036 */
3037static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
3038{
3039 if (!mem->res.parent)
3040 return NULL;
3041 return mem_cgroup_from_res_counter(mem->res.parent, res);
3042}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003043
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003044#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3045static void __init enable_swap_cgroup(void)
3046{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003047 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003048 do_swap_account = 1;
3049}
3050#else
3051static void __init enable_swap_cgroup(void)
3052{
3053}
3054#endif
3055
Balbir Singhf64c3f52009-09-23 15:56:37 -07003056static int mem_cgroup_soft_limit_tree_init(void)
3057{
3058 struct mem_cgroup_tree_per_node *rtpn;
3059 struct mem_cgroup_tree_per_zone *rtpz;
3060 int tmp, node, zone;
3061
3062 for_each_node_state(node, N_POSSIBLE) {
3063 tmp = node;
3064 if (!node_state(node, N_NORMAL_MEMORY))
3065 tmp = -1;
3066 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
3067 if (!rtpn)
3068 return 1;
3069
3070 soft_limit_tree.rb_tree_per_node[node] = rtpn;
3071
3072 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
3073 rtpz = &rtpn->rb_tree_per_zone[zone];
3074 rtpz->rb_root = RB_ROOT;
3075 spin_lock_init(&rtpz->lock);
3076 }
3077 }
3078 return 0;
3079}
3080
Li Zefan0eb253e2009-01-15 13:51:25 -08003081static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003082mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
3083{
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003084 struct mem_cgroup *mem, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003085 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003086 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003087
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003088 mem = mem_cgroup_alloc();
3089 if (!mem)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003090 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08003091
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003092 for_each_node_state(node, N_POSSIBLE)
3093 if (alloc_mem_cgroup_per_zone_info(mem, node))
3094 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07003095
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003096 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003097 if (cont->parent == NULL) {
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003098 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003099 parent = NULL;
Balbir Singh4b3bde42009-09-23 15:56:32 -07003100 root_mem_cgroup = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07003101 if (mem_cgroup_soft_limit_tree_init())
3102 goto free_out;
3103
Balbir Singh18f59ea2009-01-07 18:08:07 -08003104 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003105 parent = mem_cgroup_from_cont(cont->parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003106 mem->use_hierarchy = parent->use_hierarchy;
3107 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003108
Balbir Singh18f59ea2009-01-07 18:08:07 -08003109 if (parent && parent->use_hierarchy) {
3110 res_counter_init(&mem->res, &parent->res);
3111 res_counter_init(&mem->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003112 /*
3113 * We increment refcnt of the parent to ensure that we can
3114 * safely access it on res_counter_charge/uncharge.
3115 * This refcnt will be decremented when freeing this
3116 * mem_cgroup(see mem_cgroup_put).
3117 */
3118 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003119 } else {
3120 res_counter_init(&mem->res, NULL);
3121 res_counter_init(&mem->memsw, NULL);
3122 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003123 mem->last_scanned_child = 0;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -08003124 spin_lock_init(&mem->reclaim_param_lock);
Balbir Singh6d61ef42009-01-07 18:08:06 -08003125
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003126 if (parent)
3127 mem->swappiness = get_swappiness(parent);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003128 atomic_set(&mem->refcnt, 1);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003129 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003130free_out:
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003131 __mem_cgroup_free(mem);
Balbir Singh4b3bde42009-09-23 15:56:32 -07003132 root_mem_cgroup = NULL;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003133 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003134}
3135
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003136static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08003137 struct cgroup *cont)
3138{
3139 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003140
3141 return mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08003142}
3143
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003144static void mem_cgroup_destroy(struct cgroup_subsys *ss,
3145 struct cgroup *cont)
3146{
Daisuke Nishimurac268e992009-01-15 13:51:13 -08003147 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08003148
Daisuke Nishimurac268e992009-01-15 13:51:13 -08003149 mem_cgroup_put(mem);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003150}
3151
3152static int mem_cgroup_populate(struct cgroup_subsys *ss,
3153 struct cgroup *cont)
3154{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003155 int ret;
3156
3157 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
3158 ARRAY_SIZE(mem_cgroup_files));
3159
3160 if (!ret)
3161 ret = register_memsw_files(cont, ss);
3162 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003163}
3164
Balbir Singh67e465a2008-02-07 00:13:54 -08003165static void mem_cgroup_move_task(struct cgroup_subsys *ss,
3166 struct cgroup *cont,
3167 struct cgroup *old_cont,
Ben Blumbe367d02009-09-23 15:56:31 -07003168 struct task_struct *p,
3169 bool threadgroup)
Balbir Singh67e465a2008-02-07 00:13:54 -08003170{
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08003171 mutex_lock(&memcg_tasklist);
Balbir Singh67e465a2008-02-07 00:13:54 -08003172 /*
Nikanth Karthikesanf9717d22009-01-07 18:08:11 -08003173 * FIXME: It's better to move charges of this process from old
3174 * memcg to new memcg. But it's just on TODO-List now.
Balbir Singh67e465a2008-02-07 00:13:54 -08003175 */
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08003176 mutex_unlock(&memcg_tasklist);
Balbir Singh67e465a2008-02-07 00:13:54 -08003177}
3178
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003179struct cgroup_subsys mem_cgroup_subsys = {
3180 .name = "memory",
3181 .subsys_id = mem_cgroup_subsys_id,
3182 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08003183 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003184 .destroy = mem_cgroup_destroy,
3185 .populate = mem_cgroup_populate,
Balbir Singh67e465a2008-02-07 00:13:54 -08003186 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003187 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003188 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003189};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003190
3191#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3192
3193static int __init disable_swap_account(char *s)
3194{
3195 really_do_swap_account = 0;
3196 return 1;
3197}
3198__setup("noswapaccount", disable_swap_account);
3199#endif