blob: 0b3efb843a872d8051c960933daac17bdbbf170e [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 Hiroyukicdec2e42009-12-15 16:47:08 -080041#include <linux/cpu.h>
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -080042#include "internal.h"
Balbir Singh8cdea7c2008-02-07 00:13:50 -080043
Balbir Singh8697d332008-02-07 00:13:59 -080044#include <asm/uaccess.h>
45
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070046struct cgroup_subsys mem_cgroup_subsys __read_mostly;
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070047#define MEM_CGROUP_RECLAIM_RETRIES 5
Balbir Singh4b3bde42009-09-23 15:56:32 -070048struct mem_cgroup *root_mem_cgroup __read_mostly;
Balbir Singh8cdea7c2008-02-07 00:13:50 -080049
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080050#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
Li Zefan338c8432009-06-17 16:27:15 -070051/* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080052int do_swap_account __read_mostly;
53static int really_do_swap_account __initdata = 1; /* for remember boot option*/
54#else
55#define do_swap_account (0)
56#endif
57
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -080058static DEFINE_MUTEX(memcg_tasklist); /* can be hold under cgroup_mutex */
Balbir Singhf64c3f52009-09-23 15:56:37 -070059#define SOFTLIMIT_EVENTS_THRESH (1000)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080060
Balbir Singh8cdea7c2008-02-07 00:13:50 -080061/*
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080062 * Statistics for memory cgroup.
63 */
64enum mem_cgroup_stat_index {
65 /*
66 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
67 */
68 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
Balbir Singhd69b0422009-06-17 16:26:34 -070069 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -080070 MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */
Balaji Rao55e462b2008-05-01 04:35:12 -070071 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
72 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
Balbir Singhf64c3f52009-09-23 15:56:37 -070073 MEM_CGROUP_STAT_EVENTS, /* sum of pagein + pageout for internal use */
Balbir Singh0c3e73e2009-09-23 15:56:42 -070074 MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080075
76 MEM_CGROUP_STAT_NSTATS,
77};
78
79struct mem_cgroup_stat_cpu {
80 s64 count[MEM_CGROUP_STAT_NSTATS];
81} ____cacheline_aligned_in_smp;
82
83struct mem_cgroup_stat {
Jan Blunckc8dad2b2009-01-07 18:07:53 -080084 struct mem_cgroup_stat_cpu cpustat[0];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080085};
86
Balbir Singhf64c3f52009-09-23 15:56:37 -070087static inline void
88__mem_cgroup_stat_reset_safe(struct mem_cgroup_stat_cpu *stat,
89 enum mem_cgroup_stat_index idx)
90{
91 stat->count[idx] = 0;
92}
93
94static inline s64
95__mem_cgroup_stat_read_local(struct mem_cgroup_stat_cpu *stat,
96 enum mem_cgroup_stat_index idx)
97{
98 return stat->count[idx];
99}
100
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800101/*
102 * For accounting under irq disable, no need for increment preempt count.
103 */
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700104static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800105 enum mem_cgroup_stat_index idx, int val)
106{
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700107 stat->count[idx] += val;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800108}
109
110static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
111 enum mem_cgroup_stat_index idx)
112{
113 int cpu;
114 s64 ret = 0;
115 for_each_possible_cpu(cpu)
116 ret += stat->cpustat[cpu].count[idx];
117 return ret;
118}
119
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700120static s64 mem_cgroup_local_usage(struct mem_cgroup_stat *stat)
121{
122 s64 ret;
123
124 ret = mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_CACHE);
125 ret += mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_RSS);
126 return ret;
127}
128
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800129/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800130 * per-zone information in memory controller.
131 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800132struct mem_cgroup_per_zone {
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800133 /*
134 * spin_lock to protect the per cgroup LRU
135 */
Christoph Lameterb69408e2008-10-18 20:26:14 -0700136 struct list_head lists[NR_LRU_LISTS];
137 unsigned long count[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800138
139 struct zone_reclaim_stat reclaim_stat;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700140 struct rb_node tree_node; /* RB tree node */
141 unsigned long long usage_in_excess;/* Set to the value by which */
142 /* the soft limit is exceeded*/
143 bool on_tree;
Balbir Singh4e416952009-09-23 15:56:39 -0700144 struct mem_cgroup *mem; /* Back pointer, we cannot */
145 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800146};
147/* Macro for accessing counter */
148#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
149
150struct mem_cgroup_per_node {
151 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
152};
153
154struct mem_cgroup_lru_info {
155 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
156};
157
158/*
Balbir Singhf64c3f52009-09-23 15:56:37 -0700159 * Cgroups above their limits are maintained in a RB-Tree, independent of
160 * their hierarchy representation
161 */
162
163struct mem_cgroup_tree_per_zone {
164 struct rb_root rb_root;
165 spinlock_t lock;
166};
167
168struct mem_cgroup_tree_per_node {
169 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
170};
171
172struct mem_cgroup_tree {
173 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
174};
175
176static struct mem_cgroup_tree soft_limit_tree __read_mostly;
177
178/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800179 * The memory controller data structure. The memory controller controls both
180 * page cache and RSS per cgroup. We would eventually like to provide
181 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
182 * to help the administrator determine what knobs to tune.
183 *
184 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800185 * we hit the water mark. May be even add a low water mark, such that
186 * no reclaim occurs from a cgroup at it's low water mark, this is
187 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800188 */
189struct mem_cgroup {
190 struct cgroup_subsys_state css;
191 /*
192 * the counter to account for memory usage
193 */
194 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800195 /*
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800196 * the counter to account for mem+swap usage.
197 */
198 struct res_counter memsw;
199 /*
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800200 * Per cgroup active and inactive list, similar to the
201 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800202 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800203 struct mem_cgroup_lru_info info;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800204
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800205 /*
206 protect against reclaim related member.
207 */
208 spinlock_t reclaim_param_lock;
209
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800210 int prev_priority; /* for recording reclaim priority */
Balbir Singh6d61ef42009-01-07 18:08:06 -0800211
212 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200213 * While reclaiming in a hierarchy, we cache the last child we
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700214 * reclaimed from.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800215 */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700216 int last_scanned_child;
Balbir Singh18f59ea2009-01-07 18:08:07 -0800217 /*
218 * Should the accounting and control be hierarchical, per subtree?
219 */
220 bool use_hierarchy;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800221 unsigned long last_oom_jiffies;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800222 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800223
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800224 unsigned int swappiness;
225
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700226 /* set when res.limit == memsw.limit */
227 bool memsw_is_minimum;
228
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800229 /*
Jan Blunckc8dad2b2009-01-07 18:07:53 -0800230 * statistics. This must be placed at the end of memcg.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800231 */
232 struct mem_cgroup_stat stat;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800233};
234
Balbir Singh4e416952009-09-23 15:56:39 -0700235/*
236 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
237 * limit reclaim to prevent infinite loops, if they ever occur.
238 */
239#define MEM_CGROUP_MAX_RECLAIM_LOOPS (100)
240#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2)
241
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800242enum charge_type {
243 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
244 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700245 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700246 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800247 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -0700248 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700249 NR_CHARGE_TYPE,
250};
251
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700252/* only for here (for easy reading.) */
253#define PCGF_CACHE (1UL << PCG_CACHE)
254#define PCGF_USED (1UL << PCG_USED)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700255#define PCGF_LOCK (1UL << PCG_LOCK)
Balbir Singh4b3bde42009-09-23 15:56:32 -0700256/* Not used, but added here for completeness */
257#define PCGF_ACCT (1UL << PCG_ACCT)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800258
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800259/* for encoding cft->private value on file */
260#define _MEM (0)
261#define _MEMSWAP (1)
262#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
263#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
264#define MEMFILE_ATTR(val) ((val) & 0xffff)
265
Balbir Singh75822b42009-09-23 15:56:38 -0700266/*
267 * Reclaim flags for mem_cgroup_hierarchical_reclaim
268 */
269#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
270#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
271#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
272#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
Balbir Singh4e416952009-09-23 15:56:39 -0700273#define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
274#define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
Balbir Singh75822b42009-09-23 15:56:38 -0700275
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800276static void mem_cgroup_get(struct mem_cgroup *mem);
277static void mem_cgroup_put(struct mem_cgroup *mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -0800278static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -0800279static void drain_all_stock_async(void);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800280
Balbir Singhf64c3f52009-09-23 15:56:37 -0700281static struct mem_cgroup_per_zone *
282mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
283{
284 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
285}
286
287static struct mem_cgroup_per_zone *
288page_cgroup_zoneinfo(struct page_cgroup *pc)
289{
290 struct mem_cgroup *mem = pc->mem_cgroup;
291 int nid = page_cgroup_nid(pc);
292 int zid = page_cgroup_zid(pc);
293
294 if (!mem)
295 return NULL;
296
297 return mem_cgroup_zoneinfo(mem, nid, zid);
298}
299
300static struct mem_cgroup_tree_per_zone *
301soft_limit_tree_node_zone(int nid, int zid)
302{
303 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
304}
305
306static struct mem_cgroup_tree_per_zone *
307soft_limit_tree_from_page(struct page *page)
308{
309 int nid = page_to_nid(page);
310 int zid = page_zonenum(page);
311
312 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
313}
314
315static void
Balbir Singh4e416952009-09-23 15:56:39 -0700316__mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700317 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700318 struct mem_cgroup_tree_per_zone *mctz,
319 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700320{
321 struct rb_node **p = &mctz->rb_root.rb_node;
322 struct rb_node *parent = NULL;
323 struct mem_cgroup_per_zone *mz_node;
324
325 if (mz->on_tree)
326 return;
327
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700328 mz->usage_in_excess = new_usage_in_excess;
329 if (!mz->usage_in_excess)
330 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700331 while (*p) {
332 parent = *p;
333 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
334 tree_node);
335 if (mz->usage_in_excess < mz_node->usage_in_excess)
336 p = &(*p)->rb_left;
337 /*
338 * We can't avoid mem cgroups that are over their soft
339 * limit by the same amount
340 */
341 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
342 p = &(*p)->rb_right;
343 }
344 rb_link_node(&mz->tree_node, parent, p);
345 rb_insert_color(&mz->tree_node, &mctz->rb_root);
346 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700347}
348
349static void
350__mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
351 struct mem_cgroup_per_zone *mz,
352 struct mem_cgroup_tree_per_zone *mctz)
353{
354 if (!mz->on_tree)
355 return;
356 rb_erase(&mz->tree_node, &mctz->rb_root);
357 mz->on_tree = false;
358}
359
360static void
Balbir Singhf64c3f52009-09-23 15:56:37 -0700361mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
362 struct mem_cgroup_per_zone *mz,
363 struct mem_cgroup_tree_per_zone *mctz)
364{
365 spin_lock(&mctz->lock);
Balbir Singh4e416952009-09-23 15:56:39 -0700366 __mem_cgroup_remove_exceeded(mem, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700367 spin_unlock(&mctz->lock);
368}
369
370static bool mem_cgroup_soft_limit_check(struct mem_cgroup *mem)
371{
372 bool ret = false;
373 int cpu;
374 s64 val;
375 struct mem_cgroup_stat_cpu *cpustat;
376
377 cpu = get_cpu();
378 cpustat = &mem->stat.cpustat[cpu];
379 val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_EVENTS);
380 if (unlikely(val > SOFTLIMIT_EVENTS_THRESH)) {
381 __mem_cgroup_stat_reset_safe(cpustat, MEM_CGROUP_STAT_EVENTS);
382 ret = true;
383 }
384 put_cpu();
385 return ret;
386}
387
388static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
389{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700390 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700391 struct mem_cgroup_per_zone *mz;
392 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700393 int nid = page_to_nid(page);
394 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700395 mctz = soft_limit_tree_from_page(page);
396
397 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700398 * Necessary to update all ancestors when hierarchy is used.
399 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700400 */
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700401 for (; mem; mem = parent_mem_cgroup(mem)) {
402 mz = mem_cgroup_zoneinfo(mem, nid, zid);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700403 excess = res_counter_soft_limit_excess(&mem->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700404 /*
405 * We have to update the tree if mz is on RB-tree or
406 * mem is over its softlimit.
407 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700408 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700409 spin_lock(&mctz->lock);
410 /* if on-tree, remove it */
411 if (mz->on_tree)
412 __mem_cgroup_remove_exceeded(mem, mz, mctz);
413 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700414 * Insert again. mz->usage_in_excess will be updated.
415 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700416 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700417 __mem_cgroup_insert_exceeded(mem, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700418 spin_unlock(&mctz->lock);
419 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700420 }
421}
422
423static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
424{
425 int node, zone;
426 struct mem_cgroup_per_zone *mz;
427 struct mem_cgroup_tree_per_zone *mctz;
428
429 for_each_node_state(node, N_POSSIBLE) {
430 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
431 mz = mem_cgroup_zoneinfo(mem, node, zone);
432 mctz = soft_limit_tree_node_zone(node, zone);
433 mem_cgroup_remove_exceeded(mem, mz, mctz);
434 }
435 }
436}
437
Balbir Singh4e416952009-09-23 15:56:39 -0700438static inline unsigned long mem_cgroup_get_excess(struct mem_cgroup *mem)
439{
440 return res_counter_soft_limit_excess(&mem->res) >> PAGE_SHIFT;
441}
442
443static struct mem_cgroup_per_zone *
444__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
445{
446 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700447 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700448
449retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700450 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700451 rightmost = rb_last(&mctz->rb_root);
452 if (!rightmost)
453 goto done; /* Nothing to reclaim from */
454
455 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
456 /*
457 * Remove the node now but someone else can add it back,
458 * we will to add it back at the end of reclaim to its correct
459 * position in the tree.
460 */
461 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
462 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
463 !css_tryget(&mz->mem->css))
464 goto retry;
465done:
466 return mz;
467}
468
469static struct mem_cgroup_per_zone *
470mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
471{
472 struct mem_cgroup_per_zone *mz;
473
474 spin_lock(&mctz->lock);
475 mz = __mem_cgroup_largest_soft_limit_node(mctz);
476 spin_unlock(&mctz->lock);
477 return mz;
478}
479
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700480static void mem_cgroup_swap_statistics(struct mem_cgroup *mem,
481 bool charge)
482{
483 int val = (charge) ? 1 : -1;
484 struct mem_cgroup_stat *stat = &mem->stat;
485 struct mem_cgroup_stat_cpu *cpustat;
486 int cpu = get_cpu();
487
488 cpustat = &stat->cpustat[cpu];
489 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_SWAPOUT, val);
490 put_cpu();
491}
492
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700493static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
494 struct page_cgroup *pc,
495 bool charge)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800496{
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700497 int val = (charge) ? 1 : -1;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800498 struct mem_cgroup_stat *stat = &mem->stat;
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700499 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800500 int cpu = get_cpu();
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800501
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800502 cpustat = &stat->cpustat[cpu];
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700503 if (PageCgroupCache(pc))
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700504 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800505 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700506 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700507
508 if (charge)
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700509 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700510 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
511 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700512 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700513 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700514 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_EVENTS, 1);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800515 put_cpu();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800516}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800517
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700518static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700519 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800520{
521 int nid, zid;
522 struct mem_cgroup_per_zone *mz;
523 u64 total = 0;
524
525 for_each_online_node(nid)
526 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
527 mz = mem_cgroup_zoneinfo(mem, nid, zid);
528 total += MEM_CGROUP_ZSTAT(mz, idx);
529 }
530 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800531}
532
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800533static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800534{
535 return container_of(cgroup_subsys_state(cont,
536 mem_cgroup_subsys_id), struct mem_cgroup,
537 css);
538}
539
Balbir Singhcf475ad2008-04-29 01:00:16 -0700540struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800541{
Balbir Singh31a78f22008-09-28 23:09:31 +0100542 /*
543 * mm_update_next_owner() may clear mm->owner to NULL
544 * if it races with swapoff, page migration, etc.
545 * So this can be called with p == NULL.
546 */
547 if (unlikely(!p))
548 return NULL;
549
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800550 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
551 struct mem_cgroup, css);
552}
553
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800554static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
555{
556 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700557
558 if (!mm)
559 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800560 /*
561 * Because we have no locks, mm->owner's may be being moved to other
562 * cgroup. We use css_tryget() here even if this looks
563 * pessimistic (rather than adding locks here).
564 */
565 rcu_read_lock();
566 do {
567 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
568 if (unlikely(!mem))
569 break;
570 } while (!css_tryget(&mem->css));
571 rcu_read_unlock();
572 return mem;
573}
574
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700575/*
576 * Call callback function against all cgroup under hierarchy tree.
577 */
578static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
579 int (*func)(struct mem_cgroup *, void *))
580{
581 int found, ret, nextid;
582 struct cgroup_subsys_state *css;
583 struct mem_cgroup *mem;
584
585 if (!root->use_hierarchy)
586 return (*func)(root, data);
587
588 nextid = 1;
589 do {
590 ret = 0;
591 mem = NULL;
592
593 rcu_read_lock();
594 css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
595 &found);
596 if (css && css_tryget(css))
597 mem = container_of(css, struct mem_cgroup, css);
598 rcu_read_unlock();
599
600 if (mem) {
601 ret = (*func)(mem, data);
602 css_put(&mem->css);
603 }
604 nextid = found + 1;
605 } while (!ret && css);
606
607 return ret;
608}
609
Balbir Singh4b3bde42009-09-23 15:56:32 -0700610static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
611{
612 return (mem == root_mem_cgroup);
613}
614
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800615/*
616 * Following LRU functions are allowed to be used without PCG_LOCK.
617 * Operations are called by routine of global LRU independently from memcg.
618 * What we have to take care of here is validness of pc->mem_cgroup.
619 *
620 * Changes to pc->mem_cgroup happens when
621 * 1. charge
622 * 2. moving account
623 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
624 * It is added to LRU before charge.
625 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
626 * When moving account, the page is not on LRU. It's isolated.
627 */
628
629void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800630{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800631 struct page_cgroup *pc;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800632 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700633
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800634 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800635 return;
636 pc = lookup_page_cgroup(page);
637 /* can happen while we handle swapcache. */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700638 if (!TestClearPageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800639 return;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700640 VM_BUG_ON(!pc->mem_cgroup);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800641 /*
642 * We don't check PCG_USED bit. It's cleared when the "page" is finally
643 * removed from global LRU.
644 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800645 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700646 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700647 if (mem_cgroup_is_root(pc->mem_cgroup))
648 return;
649 VM_BUG_ON(list_empty(&pc->lru));
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800650 list_del_init(&pc->lru);
651 return;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800652}
653
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800654void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800655{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800656 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800657}
658
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800659void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800660{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800661 struct mem_cgroup_per_zone *mz;
662 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800663
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800664 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700665 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700666
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800667 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800668 /*
669 * Used bit is set without atomic ops but after smp_wmb().
670 * For making pc->mem_cgroup visible, insert smp_rmb() here.
671 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800672 smp_rmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -0700673 /* unused or root page is not rotated. */
674 if (!PageCgroupUsed(pc) || mem_cgroup_is_root(pc->mem_cgroup))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800675 return;
676 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700677 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800678}
679
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800680void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
681{
682 struct page_cgroup *pc;
683 struct mem_cgroup_per_zone *mz;
684
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800685 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800686 return;
687 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700688 VM_BUG_ON(PageCgroupAcctLRU(pc));
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800689 /*
690 * Used bit is set without atomic ops but after smp_wmb().
691 * For making pc->mem_cgroup visible, insert smp_rmb() here.
692 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800693 smp_rmb();
694 if (!PageCgroupUsed(pc))
695 return;
696
697 mz = page_cgroup_zoneinfo(pc);
698 MEM_CGROUP_ZSTAT(mz, lru) += 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700699 SetPageCgroupAcctLRU(pc);
700 if (mem_cgroup_is_root(pc->mem_cgroup))
701 return;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800702 list_add(&pc->lru, &mz->lists[lru]);
703}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800704
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800705/*
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800706 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
707 * lru because the page may.be reused after it's fully uncharged (because of
708 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
709 * it again. This function is only used to charge SwapCache. It's done under
710 * lock_page and expected that zone->lru_lock is never held.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800711 */
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800712static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800713{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800714 unsigned long flags;
715 struct zone *zone = page_zone(page);
716 struct page_cgroup *pc = lookup_page_cgroup(page);
717
718 spin_lock_irqsave(&zone->lru_lock, flags);
719 /*
720 * Forget old LRU when this page_cgroup is *not* used. This Used bit
721 * is guarded by lock_page() because the page is SwapCache.
722 */
723 if (!PageCgroupUsed(pc))
724 mem_cgroup_del_lru_list(page, page_lru(page));
725 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800726}
727
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800728static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
729{
730 unsigned long flags;
731 struct zone *zone = page_zone(page);
732 struct page_cgroup *pc = lookup_page_cgroup(page);
733
734 spin_lock_irqsave(&zone->lru_lock, flags);
735 /* link when the page is linked to LRU but page_cgroup isn't */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700736 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800737 mem_cgroup_add_lru_list(page, page_lru(page));
738 spin_unlock_irqrestore(&zone->lru_lock, flags);
739}
740
741
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800742void mem_cgroup_move_lists(struct page *page,
743 enum lru_list from, enum lru_list to)
744{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800745 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800746 return;
747 mem_cgroup_del_lru_list(page, from);
748 mem_cgroup_add_lru_list(page, to);
749}
750
David Rientjes4c4a2212008-02-07 00:14:06 -0800751int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
752{
753 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700754 struct mem_cgroup *curr = NULL;
David Rientjes4c4a2212008-02-07 00:14:06 -0800755
756 task_lock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700757 rcu_read_lock();
758 curr = try_get_mem_cgroup_from_mm(task->mm);
759 rcu_read_unlock();
David Rientjes4c4a2212008-02-07 00:14:06 -0800760 task_unlock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700761 if (!curr)
762 return 0;
763 if (curr->use_hierarchy)
764 ret = css_is_ancestor(&curr->css, &mem->css);
765 else
766 ret = (curr == mem);
767 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -0800768 return ret;
769}
770
Balbir Singh66e17072008-02-07 00:13:56 -0800771/*
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800772 * prev_priority control...this will be used in memory reclaim path.
773 */
774int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
775{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800776 int prev_priority;
777
778 spin_lock(&mem->reclaim_param_lock);
779 prev_priority = mem->prev_priority;
780 spin_unlock(&mem->reclaim_param_lock);
781
782 return prev_priority;
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800783}
784
785void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
786{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800787 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800788 if (priority < mem->prev_priority)
789 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800790 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800791}
792
793void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
794{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800795 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800796 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800797 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800798}
799
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800800static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800801{
802 unsigned long active;
803 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800804 unsigned long gb;
805 unsigned long inactive_ratio;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800806
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700807 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
808 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800809
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800810 gb = (inactive + active) >> (30 - PAGE_SHIFT);
811 if (gb)
812 inactive_ratio = int_sqrt(10 * gb);
813 else
814 inactive_ratio = 1;
815
816 if (present_pages) {
817 present_pages[0] = inactive;
818 present_pages[1] = active;
819 }
820
821 return inactive_ratio;
822}
823
824int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
825{
826 unsigned long active;
827 unsigned long inactive;
828 unsigned long present_pages[2];
829 unsigned long inactive_ratio;
830
831 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
832
833 inactive = present_pages[0];
834 active = present_pages[1];
835
836 if (inactive * inactive_ratio < active)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800837 return 1;
838
839 return 0;
840}
841
Rik van Riel56e49d22009-06-16 15:32:28 -0700842int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
843{
844 unsigned long active;
845 unsigned long inactive;
846
847 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
848 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
849
850 return (active > inactive);
851}
852
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800853unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
854 struct zone *zone,
855 enum lru_list lru)
856{
857 int nid = zone->zone_pgdat->node_id;
858 int zid = zone_idx(zone);
859 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
860
861 return MEM_CGROUP_ZSTAT(mz, lru);
862}
863
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800864struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
865 struct zone *zone)
866{
867 int nid = zone->zone_pgdat->node_id;
868 int zid = zone_idx(zone);
869 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
870
871 return &mz->reclaim_stat;
872}
873
874struct zone_reclaim_stat *
875mem_cgroup_get_reclaim_stat_from_page(struct page *page)
876{
877 struct page_cgroup *pc;
878 struct mem_cgroup_per_zone *mz;
879
880 if (mem_cgroup_disabled())
881 return NULL;
882
883 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800884 /*
885 * Used bit is set without atomic ops but after smp_wmb().
886 * For making pc->mem_cgroup visible, insert smp_rmb() here.
887 */
888 smp_rmb();
889 if (!PageCgroupUsed(pc))
890 return NULL;
891
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800892 mz = page_cgroup_zoneinfo(pc);
893 if (!mz)
894 return NULL;
895
896 return &mz->reclaim_stat;
897}
898
Balbir Singh66e17072008-02-07 00:13:56 -0800899unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
900 struct list_head *dst,
901 unsigned long *scanned, int order,
902 int mode, struct zone *z,
903 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700904 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -0800905{
906 unsigned long nr_taken = 0;
907 struct page *page;
908 unsigned long scan;
909 LIST_HEAD(pc_list);
910 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800911 struct page_cgroup *pc, *tmp;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800912 int nid = z->zone_pgdat->node_id;
913 int zid = zone_idx(z);
914 struct mem_cgroup_per_zone *mz;
Johannes Weinerb7c46d12009-09-21 17:02:56 -0700915 int lru = LRU_FILE * file + active;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700916 int ret;
Balbir Singh66e17072008-02-07 00:13:56 -0800917
Balbir Singhcf475ad2008-04-29 01:00:16 -0700918 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800919 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700920 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -0800921
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800922 scan = 0;
923 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -0800924 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800925 break;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800926
927 page = pc->page;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700928 if (unlikely(!PageCgroupUsed(pc)))
929 continue;
Hugh Dickins436c65412008-02-07 00:14:12 -0800930 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800931 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800932
Hugh Dickins436c65412008-02-07 00:14:12 -0800933 scan++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700934 ret = __isolate_lru_page(page, mode, file);
935 switch (ret) {
936 case 0:
Balbir Singh66e17072008-02-07 00:13:56 -0800937 list_move(&page->lru, dst);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700938 mem_cgroup_del_lru(page);
Balbir Singh66e17072008-02-07 00:13:56 -0800939 nr_taken++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700940 break;
941 case -EBUSY:
942 /* we don't affect global LRU but rotate in our LRU */
943 mem_cgroup_rotate_lru_list(page, page_lru(page));
944 break;
945 default:
946 break;
Balbir Singh66e17072008-02-07 00:13:56 -0800947 }
948 }
949
Balbir Singh66e17072008-02-07 00:13:56 -0800950 *scanned = scan;
951 return nr_taken;
952}
953
Balbir Singh6d61ef42009-01-07 18:08:06 -0800954#define mem_cgroup_from_res_counter(counter, member) \
955 container_of(counter, struct mem_cgroup, member)
956
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -0800957static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
958{
959 if (do_swap_account) {
960 if (res_counter_check_under_limit(&mem->res) &&
961 res_counter_check_under_limit(&mem->memsw))
962 return true;
963 } else
964 if (res_counter_check_under_limit(&mem->res))
965 return true;
966 return false;
967}
968
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800969static unsigned int get_swappiness(struct mem_cgroup *memcg)
970{
971 struct cgroup *cgrp = memcg->css.cgroup;
972 unsigned int swappiness;
973
974 /* root ? */
975 if (cgrp->parent == NULL)
976 return vm_swappiness;
977
978 spin_lock(&memcg->reclaim_param_lock);
979 swappiness = memcg->swappiness;
980 spin_unlock(&memcg->reclaim_param_lock);
981
982 return swappiness;
983}
984
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700985static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
986{
987 int *val = data;
988 (*val)++;
989 return 0;
990}
Balbir Singhe2224322009-04-02 16:57:39 -0700991
992/**
993 * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode.
994 * @memcg: The memory cgroup that went over limit
995 * @p: Task that is going to be killed
996 *
997 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
998 * enabled
999 */
1000void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1001{
1002 struct cgroup *task_cgrp;
1003 struct cgroup *mem_cgrp;
1004 /*
1005 * Need a buffer in BSS, can't rely on allocations. The code relies
1006 * on the assumption that OOM is serialized for memory controller.
1007 * If this assumption is broken, revisit this code.
1008 */
1009 static char memcg_name[PATH_MAX];
1010 int ret;
1011
1012 if (!memcg)
1013 return;
1014
1015
1016 rcu_read_lock();
1017
1018 mem_cgrp = memcg->css.cgroup;
1019 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1020
1021 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1022 if (ret < 0) {
1023 /*
1024 * Unfortunately, we are unable to convert to a useful name
1025 * But we'll still print out the usage information
1026 */
1027 rcu_read_unlock();
1028 goto done;
1029 }
1030 rcu_read_unlock();
1031
1032 printk(KERN_INFO "Task in %s killed", memcg_name);
1033
1034 rcu_read_lock();
1035 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1036 if (ret < 0) {
1037 rcu_read_unlock();
1038 goto done;
1039 }
1040 rcu_read_unlock();
1041
1042 /*
1043 * Continues from above, so we don't need an KERN_ level
1044 */
1045 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1046done:
1047
1048 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1049 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1050 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1051 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1052 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1053 "failcnt %llu\n",
1054 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1055 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1056 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1057}
1058
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001059/*
1060 * This function returns the number of memcg under hierarchy tree. Returns
1061 * 1(self count) if no children.
1062 */
1063static int mem_cgroup_count_children(struct mem_cgroup *mem)
1064{
1065 int num = 0;
1066 mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
1067 return num;
1068}
1069
Balbir Singh6d61ef42009-01-07 18:08:06 -08001070/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001071 * Visit the first child (need not be the first child as per the ordering
1072 * of the cgroup list, since we track last_scanned_child) of @mem and use
1073 * that to reclaim free pages from.
1074 */
1075static struct mem_cgroup *
1076mem_cgroup_select_victim(struct mem_cgroup *root_mem)
1077{
1078 struct mem_cgroup *ret = NULL;
1079 struct cgroup_subsys_state *css;
1080 int nextid, found;
1081
1082 if (!root_mem->use_hierarchy) {
1083 css_get(&root_mem->css);
1084 ret = root_mem;
1085 }
1086
1087 while (!ret) {
1088 rcu_read_lock();
1089 nextid = root_mem->last_scanned_child + 1;
1090 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
1091 &found);
1092 if (css && css_tryget(css))
1093 ret = container_of(css, struct mem_cgroup, css);
1094
1095 rcu_read_unlock();
1096 /* Updates scanning parameter */
1097 spin_lock(&root_mem->reclaim_param_lock);
1098 if (!css) {
1099 /* this means start scan from ID:1 */
1100 root_mem->last_scanned_child = 0;
1101 } else
1102 root_mem->last_scanned_child = found;
1103 spin_unlock(&root_mem->reclaim_param_lock);
1104 }
1105
1106 return ret;
1107}
1108
1109/*
1110 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1111 * we reclaimed from, so that we don't end up penalizing one child extensively
1112 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001113 *
1114 * root_mem is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001115 *
1116 * We give up and return to the caller when we visit root_mem twice.
1117 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001118 *
1119 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001120 */
1121static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
Balbir Singh4e416952009-09-23 15:56:39 -07001122 struct zone *zone,
Balbir Singh75822b42009-09-23 15:56:38 -07001123 gfp_t gfp_mask,
1124 unsigned long reclaim_options)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001125{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001126 struct mem_cgroup *victim;
1127 int ret, total = 0;
1128 int loop = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001129 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1130 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
Balbir Singh4e416952009-09-23 15:56:39 -07001131 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
1132 unsigned long excess = mem_cgroup_get_excess(root_mem);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001133
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001134 /* If memsw_is_minimum==1, swap-out is of-no-use. */
1135 if (root_mem->memsw_is_minimum)
1136 noswap = true;
1137
Balbir Singh4e416952009-09-23 15:56:39 -07001138 while (1) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001139 victim = mem_cgroup_select_victim(root_mem);
Balbir Singh4e416952009-09-23 15:56:39 -07001140 if (victim == root_mem) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001141 loop++;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001142 if (loop >= 1)
1143 drain_all_stock_async();
Balbir Singh4e416952009-09-23 15:56:39 -07001144 if (loop >= 2) {
1145 /*
1146 * If we have not been able to reclaim
1147 * anything, it might because there are
1148 * no reclaimable pages under this hierarchy
1149 */
1150 if (!check_soft || !total) {
1151 css_put(&victim->css);
1152 break;
1153 }
1154 /*
1155 * We want to do more targetted reclaim.
1156 * excess >> 2 is not to excessive so as to
1157 * reclaim too much, nor too less that we keep
1158 * coming back to reclaim from this cgroup
1159 */
1160 if (total >= (excess >> 2) ||
1161 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1162 css_put(&victim->css);
1163 break;
1164 }
1165 }
1166 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001167 if (!mem_cgroup_local_usage(&victim->stat)) {
1168 /* this cgroup's local usage == 0 */
1169 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001170 continue;
1171 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001172 /* we use swappiness of local cgroup */
Balbir Singh4e416952009-09-23 15:56:39 -07001173 if (check_soft)
1174 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
1175 noswap, get_swappiness(victim), zone,
1176 zone->zone_pgdat->node_id);
1177 else
1178 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
1179 noswap, get_swappiness(victim));
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001180 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001181 /*
1182 * At shrinking usage, we can't check we should stop here or
1183 * reclaim more. It's depends on callers. last_scanned_child
1184 * will work enough for keeping fairness under tree.
1185 */
1186 if (shrink)
1187 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001188 total += ret;
Balbir Singh4e416952009-09-23 15:56:39 -07001189 if (check_soft) {
1190 if (res_counter_check_under_soft_limit(&root_mem->res))
1191 return total;
1192 } else if (mem_cgroup_check_under_limit(root_mem))
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001193 return 1 + total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001194 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001195 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001196}
1197
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001198bool mem_cgroup_oom_called(struct task_struct *task)
1199{
1200 bool ret = false;
1201 struct mem_cgroup *mem;
1202 struct mm_struct *mm;
1203
1204 rcu_read_lock();
1205 mm = task->mm;
1206 if (!mm)
1207 mm = &init_mm;
1208 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
1209 if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
1210 ret = true;
1211 rcu_read_unlock();
1212 return ret;
1213}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001214
1215static int record_last_oom_cb(struct mem_cgroup *mem, void *data)
1216{
1217 mem->last_oom_jiffies = jiffies;
1218 return 0;
1219}
1220
1221static void record_last_oom(struct mem_cgroup *mem)
1222{
1223 mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb);
1224}
1225
Balbir Singhd69b0422009-06-17 16:26:34 -07001226/*
1227 * Currently used to update mapped file statistics, but the routine can be
1228 * generalized to update other statistics as well.
1229 */
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001230void mem_cgroup_update_file_mapped(struct page *page, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07001231{
1232 struct mem_cgroup *mem;
1233 struct mem_cgroup_stat *stat;
1234 struct mem_cgroup_stat_cpu *cpustat;
1235 int cpu;
1236 struct page_cgroup *pc;
1237
Balbir Singhd69b0422009-06-17 16:26:34 -07001238 pc = lookup_page_cgroup(page);
1239 if (unlikely(!pc))
1240 return;
1241
1242 lock_page_cgroup(pc);
1243 mem = pc->mem_cgroup;
1244 if (!mem)
1245 goto done;
1246
1247 if (!PageCgroupUsed(pc))
1248 goto done;
1249
1250 /*
1251 * Preemption is already disabled, we don't need get_cpu()
1252 */
1253 cpu = smp_processor_id();
1254 stat = &mem->stat;
1255 cpustat = &stat->cpustat[cpu];
1256
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001257 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED, val);
Balbir Singhd69b0422009-06-17 16:26:34 -07001258done:
1259 unlock_page_cgroup(pc);
1260}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001261
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001262/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001263 * size of first charge trial. "32" comes from vmscan.c's magic value.
1264 * TODO: maybe necessary to use big numbers in big irons.
1265 */
1266#define CHARGE_SIZE (32 * PAGE_SIZE)
1267struct memcg_stock_pcp {
1268 struct mem_cgroup *cached; /* this never be root cgroup */
1269 int charge;
1270 struct work_struct work;
1271};
1272static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
1273static atomic_t memcg_drain_count;
1274
1275/*
1276 * Try to consume stocked charge on this cpu. If success, PAGE_SIZE is consumed
1277 * from local stock and true is returned. If the stock is 0 or charges from a
1278 * cgroup which is not current target, returns false. This stock will be
1279 * refilled.
1280 */
1281static bool consume_stock(struct mem_cgroup *mem)
1282{
1283 struct memcg_stock_pcp *stock;
1284 bool ret = true;
1285
1286 stock = &get_cpu_var(memcg_stock);
1287 if (mem == stock->cached && stock->charge)
1288 stock->charge -= PAGE_SIZE;
1289 else /* need to call res_counter_charge */
1290 ret = false;
1291 put_cpu_var(memcg_stock);
1292 return ret;
1293}
1294
1295/*
1296 * Returns stocks cached in percpu to res_counter and reset cached information.
1297 */
1298static void drain_stock(struct memcg_stock_pcp *stock)
1299{
1300 struct mem_cgroup *old = stock->cached;
1301
1302 if (stock->charge) {
1303 res_counter_uncharge(&old->res, stock->charge);
1304 if (do_swap_account)
1305 res_counter_uncharge(&old->memsw, stock->charge);
1306 }
1307 stock->cached = NULL;
1308 stock->charge = 0;
1309}
1310
1311/*
1312 * This must be called under preempt disabled or must be called by
1313 * a thread which is pinned to local cpu.
1314 */
1315static void drain_local_stock(struct work_struct *dummy)
1316{
1317 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
1318 drain_stock(stock);
1319}
1320
1321/*
1322 * Cache charges(val) which is from res_counter, to local per_cpu area.
1323 * This will be consumed by consumt_stock() function, later.
1324 */
1325static void refill_stock(struct mem_cgroup *mem, int val)
1326{
1327 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
1328
1329 if (stock->cached != mem) { /* reset if necessary */
1330 drain_stock(stock);
1331 stock->cached = mem;
1332 }
1333 stock->charge += val;
1334 put_cpu_var(memcg_stock);
1335}
1336
1337/*
1338 * Tries to drain stocked charges in other cpus. This function is asynchronous
1339 * and just put a work per cpu for draining localy on each cpu. Caller can
1340 * expects some charges will be back to res_counter later but cannot wait for
1341 * it.
1342 */
1343static void drain_all_stock_async(void)
1344{
1345 int cpu;
1346 /* This function is for scheduling "drain" in asynchronous way.
1347 * The result of "drain" is not directly handled by callers. Then,
1348 * if someone is calling drain, we don't have to call drain more.
1349 * Anyway, WORK_STRUCT_PENDING check in queue_work_on() will catch if
1350 * there is a race. We just do loose check here.
1351 */
1352 if (atomic_read(&memcg_drain_count))
1353 return;
1354 /* Notify other cpus that system-wide "drain" is running */
1355 atomic_inc(&memcg_drain_count);
1356 get_online_cpus();
1357 for_each_online_cpu(cpu) {
1358 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
1359 schedule_work_on(cpu, &stock->work);
1360 }
1361 put_online_cpus();
1362 atomic_dec(&memcg_drain_count);
1363 /* We don't wait for flush_work */
1364}
1365
1366/* This is a synchronous drain interface. */
1367static void drain_all_stock_sync(void)
1368{
1369 /* called when force_empty is called */
1370 atomic_inc(&memcg_drain_count);
1371 schedule_on_each_cpu(drain_local_stock);
1372 atomic_dec(&memcg_drain_count);
1373}
1374
1375static int __cpuinit memcg_stock_cpu_callback(struct notifier_block *nb,
1376 unsigned long action,
1377 void *hcpu)
1378{
1379 int cpu = (unsigned long)hcpu;
1380 struct memcg_stock_pcp *stock;
1381
1382 if (action != CPU_DEAD)
1383 return NOTIFY_OK;
1384 stock = &per_cpu(memcg_stock, cpu);
1385 drain_stock(stock);
1386 return NOTIFY_OK;
1387}
1388
1389/*
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001390 * Unlike exported interface, "oom" parameter is added. if oom==true,
1391 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001392 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001393static int __mem_cgroup_try_charge(struct mm_struct *mm,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001394 gfp_t gfp_mask, struct mem_cgroup **memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -07001395 bool oom, struct page *page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001396{
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001397 struct mem_cgroup *mem, *mem_over_limit;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001398 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001399 struct res_counter *fail_res;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001400 int csize = CHARGE_SIZE;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001401
1402 if (unlikely(test_thread_flag(TIF_MEMDIE))) {
1403 /* Don't account this! */
1404 *memcg = NULL;
1405 return 0;
1406 }
1407
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001408 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08001409 * We always charge the cgroup the mm_struct belongs to.
1410 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001411 * thread group leader migrates. It's possible that mm is not
1412 * set, if so charge the init_mm (happens for pagecache usage).
1413 */
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001414 mem = *memcg;
1415 if (likely(!mem)) {
1416 mem = try_get_mem_cgroup_from_mm(mm);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001417 *memcg = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001418 } else {
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001419 css_get(&mem->css);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001420 }
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001421 if (unlikely(!mem))
1422 return 0;
1423
Nikanth Karthikesan46f7e602009-05-28 14:34:41 -07001424 VM_BUG_ON(css_is_removed(&mem->css));
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001425 if (mem_cgroup_is_root(mem))
1426 goto done;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001427
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001428 while (1) {
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001429 int ret = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001430 unsigned long flags = 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001431
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001432 if (consume_stock(mem))
1433 goto charged;
1434
1435 ret = res_counter_charge(&mem->res, csize, &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001436 if (likely(!ret)) {
1437 if (!do_swap_account)
1438 break;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001439 ret = res_counter_charge(&mem->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001440 if (likely(!ret))
1441 break;
1442 /* mem+swap counter fails */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001443 res_counter_uncharge(&mem->res, csize);
Balbir Singh75822b42009-09-23 15:56:38 -07001444 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001445 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1446 memsw);
1447 } else
1448 /* mem counter fails */
1449 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1450 res);
1451
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001452 /* reduce request size and retry */
1453 if (csize > PAGE_SIZE) {
1454 csize = PAGE_SIZE;
1455 continue;
1456 }
Hugh Dickins3be91272008-02-07 00:14:19 -08001457 if (!(gfp_mask & __GFP_WAIT))
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001458 goto nomem;
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001459
Balbir Singh4e416952009-09-23 15:56:39 -07001460 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
1461 gfp_mask, flags);
Daisuke Nishimura4d1c6272009-01-15 13:51:14 -08001462 if (ret)
1463 continue;
Balbir Singh66e17072008-02-07 00:13:56 -08001464
1465 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001466 * try_to_free_mem_cgroup_pages() might not give us a full
1467 * picture of reclaim. Some pages are reclaimed and might be
1468 * moved to swap cache or just unmapped from the cgroup.
1469 * Check the limit again to see if the reclaim reduced the
1470 * current usage of the cgroup before giving up
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001471 *
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001472 */
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -08001473 if (mem_cgroup_check_under_limit(mem_over_limit))
1474 continue;
Hugh Dickins3be91272008-02-07 00:14:19 -08001475
1476 if (!nr_retries--) {
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001477 if (oom) {
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08001478 mutex_lock(&memcg_tasklist);
KAMEZAWA Hiroyuki88700752009-01-07 18:08:09 -08001479 mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08001480 mutex_unlock(&memcg_tasklist);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001481 record_last_oom(mem_over_limit);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001482 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001483 goto nomem;
Balbir Singh66e17072008-02-07 00:13:56 -08001484 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001485 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001486 if (csize > PAGE_SIZE)
1487 refill_stock(mem, csize - PAGE_SIZE);
1488charged:
Balbir Singhf64c3f52009-09-23 15:56:37 -07001489 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001490 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
1491 * if they exceeds softlimit.
Balbir Singhf64c3f52009-09-23 15:56:37 -07001492 */
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001493 if (mem_cgroup_soft_limit_check(mem))
1494 mem_cgroup_update_tree(mem, page);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001495done:
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001496 return 0;
1497nomem:
1498 css_put(&mem->css);
1499 return -ENOMEM;
1500}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001501
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001502/*
1503 * A helper function to get mem_cgroup from ID. must be called under
1504 * rcu_read_lock(). The caller must check css_is_removed() or some if
1505 * it's concern. (dropping refcnt from swap can be called against removed
1506 * memcg.)
1507 */
1508static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
1509{
1510 struct cgroup_subsys_state *css;
1511
1512 /* ID 0 is unused ID */
1513 if (!id)
1514 return NULL;
1515 css = css_lookup(&mem_cgroup_subsys, id);
1516 if (!css)
1517 return NULL;
1518 return container_of(css, struct mem_cgroup, css);
1519}
1520
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001521static struct mem_cgroup *try_get_mem_cgroup_from_swapcache(struct page *page)
1522{
1523 struct mem_cgroup *mem;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001524 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001525 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001526 swp_entry_t ent;
1527
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001528 VM_BUG_ON(!PageLocked(page));
1529
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001530 if (!PageSwapCache(page))
1531 return NULL;
1532
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001533 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001534 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001535 if (PageCgroupUsed(pc)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001536 mem = pc->mem_cgroup;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001537 if (mem && !css_tryget(&mem->css))
1538 mem = NULL;
1539 } else {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001540 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001541 id = lookup_swap_cgroup(ent);
1542 rcu_read_lock();
1543 mem = mem_cgroup_lookup(id);
1544 if (mem && !css_tryget(&mem->css))
1545 mem = NULL;
1546 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001547 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001548 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001549 return mem;
1550}
1551
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001552/*
Daisuke Nishimuraa5e924f2009-01-07 18:08:28 -08001553 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001554 * USED state. If already USED, uncharge and return.
1555 */
1556
1557static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1558 struct page_cgroup *pc,
1559 enum charge_type ctype)
1560{
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001561 /* try_charge() can return NULL to *memcg, taking care of it. */
1562 if (!mem)
1563 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001564
1565 lock_page_cgroup(pc);
1566 if (unlikely(PageCgroupUsed(pc))) {
1567 unlock_page_cgroup(pc);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001568 if (!mem_cgroup_is_root(mem)) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001569 res_counter_uncharge(&mem->res, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001570 if (do_swap_account)
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001571 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001572 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001573 css_put(&mem->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001574 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001575 }
Balbir Singh4b3bde42009-09-23 15:56:32 -07001576
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001577 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07001578 /*
1579 * We access a page_cgroup asynchronously without lock_page_cgroup().
1580 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
1581 * is accessed after testing USED bit. To make pc->mem_cgroup visible
1582 * before USED bit, we need memory barrier here.
1583 * See mem_cgroup_add_lru_list(), etc.
1584 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001585 smp_wmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -07001586 switch (ctype) {
1587 case MEM_CGROUP_CHARGE_TYPE_CACHE:
1588 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
1589 SetPageCgroupCache(pc);
1590 SetPageCgroupUsed(pc);
1591 break;
1592 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1593 ClearPageCgroupCache(pc);
1594 SetPageCgroupUsed(pc);
1595 break;
1596 default:
1597 break;
1598 }
Hugh Dickins3be91272008-02-07 00:14:19 -08001599
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001600 mem_cgroup_charge_statistics(mem, pc, true);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001601
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001602 unlock_page_cgroup(pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001603}
1604
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001605/**
1606 * mem_cgroup_move_account - move account of the page
1607 * @pc: page_cgroup of the page.
1608 * @from: mem_cgroup which the page is moved from.
1609 * @to: mem_cgroup which the page is moved to. @from != @to.
1610 *
1611 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001612 * - page is not on LRU (isolate_page() is useful.)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001613 *
1614 * returns 0 at success,
1615 * returns -EBUSY when lock is busy or "pc" is unstable.
1616 *
1617 * This function does "uncharge" from old cgroup but doesn't do "charge" to
1618 * new cgroup. It should be done by a caller.
1619 */
1620
1621static int mem_cgroup_move_account(struct page_cgroup *pc,
1622 struct mem_cgroup *from, struct mem_cgroup *to)
1623{
1624 struct mem_cgroup_per_zone *from_mz, *to_mz;
1625 int nid, zid;
1626 int ret = -EBUSY;
Balbir Singhd69b0422009-06-17 16:26:34 -07001627 struct page *page;
1628 int cpu;
1629 struct mem_cgroup_stat *stat;
1630 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001631
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001632 VM_BUG_ON(from == to);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001633 VM_BUG_ON(PageLRU(pc->page));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001634
1635 nid = page_cgroup_nid(pc);
1636 zid = page_cgroup_zid(pc);
1637 from_mz = mem_cgroup_zoneinfo(from, nid, zid);
1638 to_mz = mem_cgroup_zoneinfo(to, nid, zid);
1639
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001640 if (!trylock_page_cgroup(pc))
1641 return ret;
1642
1643 if (!PageCgroupUsed(pc))
1644 goto out;
1645
1646 if (pc->mem_cgroup != from)
1647 goto out;
1648
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001649 if (!mem_cgroup_is_root(from))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001650 res_counter_uncharge(&from->res, PAGE_SIZE);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001651 mem_cgroup_charge_statistics(from, pc, false);
Balbir Singhd69b0422009-06-17 16:26:34 -07001652
1653 page = pc->page;
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001654 if (page_mapped(page) && !PageAnon(page)) {
Balbir Singhd69b0422009-06-17 16:26:34 -07001655 cpu = smp_processor_id();
1656 /* Update mapped_file data for mem_cgroup "from" */
1657 stat = &from->stat;
1658 cpustat = &stat->cpustat[cpu];
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001659 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED,
Balbir Singhd69b0422009-06-17 16:26:34 -07001660 -1);
1661
1662 /* Update mapped_file data for mem_cgroup "to" */
1663 stat = &to->stat;
1664 cpustat = &stat->cpustat[cpu];
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001665 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED,
Balbir Singhd69b0422009-06-17 16:26:34 -07001666 1);
1667 }
1668
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001669 if (do_swap_account && !mem_cgroup_is_root(from))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001670 res_counter_uncharge(&from->memsw, PAGE_SIZE);
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001671 css_put(&from->css);
1672
1673 css_get(&to->css);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001674 pc->mem_cgroup = to;
1675 mem_cgroup_charge_statistics(to, pc, true);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001676 ret = 0;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001677out:
1678 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001679 /*
1680 * We charges against "to" which may not have any tasks. Then, "to"
1681 * can be under rmdir(). But in current implementation, caller of
1682 * this function is just force_empty() and it's garanteed that
1683 * "to" is never removed. So, we don't check rmdir status here.
1684 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001685 return ret;
1686}
1687
1688/*
1689 * move charges to its parent.
1690 */
1691
1692static int mem_cgroup_move_parent(struct page_cgroup *pc,
1693 struct mem_cgroup *child,
1694 gfp_t gfp_mask)
1695{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001696 struct page *page = pc->page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001697 struct cgroup *cg = child->css.cgroup;
1698 struct cgroup *pcg = cg->parent;
1699 struct mem_cgroup *parent;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001700 int ret;
1701
1702 /* Is ROOT ? */
1703 if (!pcg)
1704 return -EINVAL;
1705
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001706
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001707 parent = mem_cgroup_from_cont(pcg);
1708
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001709
Balbir Singhf64c3f52009-09-23 15:56:37 -07001710 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false, page);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001711 if (ret || !parent)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001712 return ret;
1713
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001714 if (!get_page_unless_zero(page)) {
1715 ret = -EBUSY;
1716 goto uncharge;
1717 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001718
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001719 ret = isolate_lru_page(page);
1720
1721 if (ret)
1722 goto cancel;
1723
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001724 ret = mem_cgroup_move_account(pc, child, parent);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001725
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001726 putback_lru_page(page);
1727 if (!ret) {
1728 put_page(page);
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001729 /* drop extra refcnt by try_charge() */
1730 css_put(&parent->css);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001731 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001732 }
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001733
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001734cancel:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001735 put_page(page);
1736uncharge:
1737 /* drop extra refcnt by try_charge() */
1738 css_put(&parent->css);
1739 /* uncharge if move fails */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001740 if (!mem_cgroup_is_root(parent)) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001741 res_counter_uncharge(&parent->res, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001742 if (do_swap_account)
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001743 res_counter_uncharge(&parent->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001744 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001745 return ret;
1746}
1747
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001748/*
1749 * Charge the memory controller for page usage.
1750 * Return
1751 * 0 if the charge was successful
1752 * < 0 if the cgroup is over its limit
1753 */
1754static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
1755 gfp_t gfp_mask, enum charge_type ctype,
1756 struct mem_cgroup *memcg)
1757{
1758 struct mem_cgroup *mem;
1759 struct page_cgroup *pc;
1760 int ret;
1761
1762 pc = lookup_page_cgroup(page);
1763 /* can happen at boot */
1764 if (unlikely(!pc))
1765 return 0;
1766 prefetchw(pc);
1767
1768 mem = memcg;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001769 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true, page);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001770 if (ret || !mem)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001771 return ret;
1772
1773 __mem_cgroup_commit_charge(mem, pc, ctype);
1774 return 0;
1775}
1776
1777int mem_cgroup_newpage_charge(struct page *page,
1778 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001779{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001780 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001781 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001782 if (PageCompound(page))
1783 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001784 /*
1785 * If already mapped, we don't have to account.
1786 * If page cache, page->mapping has address_space.
1787 * But page->mapping may have out-of-use anon_vma pointer,
1788 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
1789 * is NULL.
1790 */
1791 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
1792 return 0;
1793 if (unlikely(!mm))
1794 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001795 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001796 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001797}
1798
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001799static void
1800__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1801 enum charge_type ctype);
1802
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001803int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
1804 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08001805{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001806 struct mem_cgroup *mem = NULL;
1807 int ret;
1808
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001809 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001810 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001811 if (PageCompound(page))
1812 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001813 /*
1814 * Corner case handling. This is called from add_to_page_cache()
1815 * in usual. But some FS (shmem) precharges this page before calling it
1816 * and call add_to_page_cache() with GFP_NOWAIT.
1817 *
1818 * For GFP_NOWAIT case, the page may be pre-charged before calling
1819 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
1820 * charge twice. (It works but has to pay a bit larger cost.)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001821 * And when the page is SwapCache, it should take swap information
1822 * into account. This is under lock_page() now.
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001823 */
1824 if (!(gfp_mask & __GFP_WAIT)) {
1825 struct page_cgroup *pc;
1826
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001827
1828 pc = lookup_page_cgroup(page);
1829 if (!pc)
1830 return 0;
1831 lock_page_cgroup(pc);
1832 if (PageCgroupUsed(pc)) {
1833 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001834 return 0;
1835 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001836 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001837 }
1838
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001839 if (unlikely(!mm && !mem))
Balbir Singh8697d332008-02-07 00:13:59 -08001840 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001841
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -07001842 if (page_is_file_cache(page))
1843 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001844 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001845
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001846 /* shmem */
1847 if (PageSwapCache(page)) {
1848 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1849 if (!ret)
1850 __mem_cgroup_commit_charge_swapin(page, mem,
1851 MEM_CGROUP_CHARGE_TYPE_SHMEM);
1852 } else
1853 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
1854 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001855
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001856 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001857}
1858
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001859/*
1860 * While swap-in, try_charge -> commit or cancel, the page is locked.
1861 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02001862 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001863 * "commit()" or removed by "cancel()"
1864 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001865int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
1866 struct page *page,
1867 gfp_t mask, struct mem_cgroup **ptr)
1868{
1869 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001870 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001871
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001872 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001873 return 0;
1874
1875 if (!do_swap_account)
1876 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001877 /*
1878 * A racing thread's fault, or swapoff, may have already updated
Hugh Dickins407f9c82009-12-14 17:59:30 -08001879 * the pte, and even removed page from swap cache: in those cases
1880 * do_swap_page()'s pte_same() test will fail; but there's also a
1881 * KSM case which does need to charge the page.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001882 */
1883 if (!PageSwapCache(page))
Hugh Dickins407f9c82009-12-14 17:59:30 -08001884 goto charge_cur_mm;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001885 mem = try_get_mem_cgroup_from_swapcache(page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001886 if (!mem)
1887 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001888 *ptr = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001889 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true, page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001890 /* drop extra refcnt from tryget */
1891 css_put(&mem->css);
1892 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001893charge_cur_mm:
1894 if (unlikely(!mm))
1895 mm = &init_mm;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001896 return __mem_cgroup_try_charge(mm, mask, ptr, true, page);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001897}
1898
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001899static void
1900__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1901 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001902{
1903 struct page_cgroup *pc;
1904
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001905 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001906 return;
1907 if (!ptr)
1908 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001909 cgroup_exclude_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001910 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001911 mem_cgroup_lru_del_before_commit_swapcache(page);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001912 __mem_cgroup_commit_charge(ptr, pc, ctype);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001913 mem_cgroup_lru_add_after_commit_swapcache(page);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001914 /*
1915 * Now swap is on-memory. This means this page may be
1916 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08001917 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
1918 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
1919 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001920 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08001921 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001922 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001923 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001924 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001925
1926 id = swap_cgroup_record(ent, 0);
1927 rcu_read_lock();
1928 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001929 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001930 /*
1931 * This recorded memcg can be obsolete one. So, avoid
1932 * calling css_tryget
1933 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001934 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001935 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001936 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001937 mem_cgroup_put(memcg);
1938 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001939 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001940 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001941 /*
1942 * At swapin, we may charge account against cgroup which has no tasks.
1943 * So, rmdir()->pre_destroy() can be called while we do this charge.
1944 * In that case, we need to call pre_destroy() again. check it here.
1945 */
1946 cgroup_release_and_wakeup_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001947}
1948
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001949void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
1950{
1951 __mem_cgroup_commit_charge_swapin(page, ptr,
1952 MEM_CGROUP_CHARGE_TYPE_MAPPED);
1953}
1954
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001955void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
1956{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001957 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001958 return;
1959 if (!mem)
1960 return;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001961 if (!mem_cgroup_is_root(mem)) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001962 res_counter_uncharge(&mem->res, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001963 if (do_swap_account)
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001964 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001965 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001966 css_put(&mem->css);
1967}
1968
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08001969static void
1970__do_uncharge(struct mem_cgroup *mem, const enum charge_type ctype)
1971{
1972 struct memcg_batch_info *batch = NULL;
1973 bool uncharge_memsw = true;
1974 /* If swapout, usage of swap doesn't decrease */
1975 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1976 uncharge_memsw = false;
1977 /*
1978 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
1979 * In those cases, all pages freed continously can be expected to be in
1980 * the same cgroup and we have chance to coalesce uncharges.
1981 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
1982 * because we want to do uncharge as soon as possible.
1983 */
1984 if (!current->memcg_batch.do_batch || test_thread_flag(TIF_MEMDIE))
1985 goto direct_uncharge;
1986
1987 batch = &current->memcg_batch;
1988 /*
1989 * In usual, we do css_get() when we remember memcg pointer.
1990 * But in this case, we keep res->usage until end of a series of
1991 * uncharges. Then, it's ok to ignore memcg's refcnt.
1992 */
1993 if (!batch->memcg)
1994 batch->memcg = mem;
1995 /*
1996 * In typical case, batch->memcg == mem. This means we can
1997 * merge a series of uncharges to an uncharge of res_counter.
1998 * If not, we uncharge res_counter ony by one.
1999 */
2000 if (batch->memcg != mem)
2001 goto direct_uncharge;
2002 /* remember freed charge and uncharge it later */
2003 batch->bytes += PAGE_SIZE;
2004 if (uncharge_memsw)
2005 batch->memsw_bytes += PAGE_SIZE;
2006 return;
2007direct_uncharge:
2008 res_counter_uncharge(&mem->res, PAGE_SIZE);
2009 if (uncharge_memsw)
2010 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
2011 return;
2012}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002013
Balbir Singh8697d332008-02-07 00:13:59 -08002014/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002015 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002016 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002017static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002018__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002019{
Hugh Dickins82895462008-03-04 14:29:08 -08002020 struct page_cgroup *pc;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002021 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002022 struct mem_cgroup_per_zone *mz;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002023
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002024 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002025 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07002026
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002027 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002028 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002029
Balbir Singh8697d332008-02-07 00:13:59 -08002030 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08002031 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08002032 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002033 pc = lookup_page_cgroup(page);
2034 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002035 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002036
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002037 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002038
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002039 mem = pc->mem_cgroup;
2040
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002041 if (!PageCgroupUsed(pc))
2042 goto unlock_out;
2043
2044 switch (ctype) {
2045 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07002046 case MEM_CGROUP_CHARGE_TYPE_DROP:
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002047 if (page_mapped(page))
2048 goto unlock_out;
2049 break;
2050 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
2051 if (!PageAnon(page)) { /* Shared memory */
2052 if (page->mapping && !page_is_file_cache(page))
2053 goto unlock_out;
2054 } else if (page_mapped(page)) /* Anon */
2055 goto unlock_out;
2056 break;
2057 default:
2058 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002059 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002060
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002061 if (!mem_cgroup_is_root(mem))
2062 __do_uncharge(mem, ctype);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002063 if (ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2064 mem_cgroup_swap_statistics(mem, true);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002065 mem_cgroup_charge_statistics(mem, pc, false);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002066
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002067 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08002068 /*
2069 * pc->mem_cgroup is not cleared here. It will be accessed when it's
2070 * freed from LRU. This is safe because uncharged page is expected not
2071 * to be reused (freed soon). Exception is SwapCache, it's handled by
2072 * special functions.
2073 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08002074
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002075 mz = page_cgroup_zoneinfo(pc);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002076 unlock_page_cgroup(pc);
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08002077
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002078 if (mem_cgroup_soft_limit_check(mem))
Balbir Singhf64c3f52009-09-23 15:56:37 -07002079 mem_cgroup_update_tree(mem, page);
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08002080 /* at swapout, this memcg will be accessed to record to swap */
2081 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2082 css_put(&mem->css);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002083
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002084 return mem;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002085
2086unlock_out:
2087 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002088 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08002089}
2090
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002091void mem_cgroup_uncharge_page(struct page *page)
2092{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002093 /* early check. */
2094 if (page_mapped(page))
2095 return;
2096 if (page->mapping && !PageAnon(page))
2097 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002098 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
2099}
2100
2101void mem_cgroup_uncharge_cache_page(struct page *page)
2102{
2103 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07002104 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002105 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
2106}
2107
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002108/*
2109 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
2110 * In that cases, pages are freed continuously and we can expect pages
2111 * are in the same memcg. All these calls itself limits the number of
2112 * pages freed at once, then uncharge_start/end() is called properly.
2113 * This may be called prural(2) times in a context,
2114 */
2115
2116void mem_cgroup_uncharge_start(void)
2117{
2118 current->memcg_batch.do_batch++;
2119 /* We can do nest. */
2120 if (current->memcg_batch.do_batch == 1) {
2121 current->memcg_batch.memcg = NULL;
2122 current->memcg_batch.bytes = 0;
2123 current->memcg_batch.memsw_bytes = 0;
2124 }
2125}
2126
2127void mem_cgroup_uncharge_end(void)
2128{
2129 struct memcg_batch_info *batch = &current->memcg_batch;
2130
2131 if (!batch->do_batch)
2132 return;
2133
2134 batch->do_batch--;
2135 if (batch->do_batch) /* If stacked, do nothing. */
2136 return;
2137
2138 if (!batch->memcg)
2139 return;
2140 /*
2141 * This "batch->memcg" is valid without any css_get/put etc...
2142 * bacause we hide charges behind us.
2143 */
2144 if (batch->bytes)
2145 res_counter_uncharge(&batch->memcg->res, batch->bytes);
2146 if (batch->memsw_bytes)
2147 res_counter_uncharge(&batch->memcg->memsw, batch->memsw_bytes);
2148 /* forget this pointer (for sanity check) */
2149 batch->memcg = NULL;
2150}
2151
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002152#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002153/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002154 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002155 * memcg information is recorded to swap_cgroup of "ent"
2156 */
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07002157void
2158mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002159{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002160 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07002161 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002162
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07002163 if (!swapout) /* this was a swap cache but the swap is unused ! */
2164 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
2165
2166 memcg = __mem_cgroup_uncharge_common(page, ctype);
2167
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002168 /* record memcg information */
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07002169 if (do_swap_account && swapout && memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002170 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002171 mem_cgroup_get(memcg);
2172 }
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07002173 if (swapout && memcg)
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08002174 css_put(&memcg->css);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002175}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002176#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002177
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002178#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2179/*
2180 * called from swap_entry_free(). remove record in swap_cgroup and
2181 * uncharge "memsw" account.
2182 */
2183void mem_cgroup_uncharge_swap(swp_entry_t ent)
2184{
2185 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002186 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002187
2188 if (!do_swap_account)
2189 return;
2190
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002191 id = swap_cgroup_record(ent, 0);
2192 rcu_read_lock();
2193 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002194 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002195 /*
2196 * We uncharge this because swap is freed.
2197 * This memcg can be obsolete one. We avoid calling css_tryget
2198 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002199 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002200 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002201 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002202 mem_cgroup_put(memcg);
2203 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002204 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002205}
2206#endif
2207
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002208/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002209 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
2210 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002211 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002212int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002213{
2214 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002215 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002216 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08002217
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002218 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07002219 return 0;
2220
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002221 pc = lookup_page_cgroup(page);
2222 lock_page_cgroup(pc);
2223 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002224 mem = pc->mem_cgroup;
2225 css_get(&mem->css);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08002226 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002227 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002228
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002229 if (mem) {
Balbir Singhf64c3f52009-09-23 15:56:37 -07002230 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false,
2231 page);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002232 css_put(&mem->css);
2233 }
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002234 *ptr = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002235 return ret;
2236}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08002237
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002238/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002239void mem_cgroup_end_migration(struct mem_cgroup *mem,
2240 struct page *oldpage, struct page *newpage)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002241{
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002242 struct page *target, *unused;
2243 struct page_cgroup *pc;
2244 enum charge_type ctype;
2245
2246 if (!mem)
2247 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002248 cgroup_exclude_rmdir(&mem->css);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002249 /* at migration success, oldpage->mapping is NULL. */
2250 if (oldpage->mapping) {
2251 target = oldpage;
2252 unused = NULL;
2253 } else {
2254 target = newpage;
2255 unused = oldpage;
2256 }
2257
2258 if (PageAnon(target))
2259 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
2260 else if (page_is_file_cache(target))
2261 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
2262 else
2263 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
2264
2265 /* unused page is not on radix-tree now. */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002266 if (unused)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002267 __mem_cgroup_uncharge_common(unused, ctype);
2268
2269 pc = lookup_page_cgroup(target);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002270 /*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002271 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
2272 * So, double-counting is effectively avoided.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002273 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002274 __mem_cgroup_commit_charge(mem, pc, ctype);
2275
2276 /*
2277 * Both of oldpage and newpage are still under lock_page().
2278 * Then, we don't have to care about race in radix-tree.
2279 * But we have to be careful that this page is unmapped or not.
2280 *
2281 * There is a case for !page_mapped(). At the start of
2282 * migration, oldpage was mapped. But now, it's zapped.
2283 * But we know *target* page is not freed/reused under us.
2284 * mem_cgroup_uncharge_page() does all necessary checks.
2285 */
2286 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
2287 mem_cgroup_uncharge_page(target);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002288 /*
2289 * At migration, we may charge account against cgroup which has no tasks
2290 * So, rmdir()->pre_destroy() can be called while we do this charge.
2291 * In that case, we need to call pre_destroy() again. check it here.
2292 */
2293 cgroup_release_and_wakeup_rmdir(&mem->css);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002294}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08002295
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002296/*
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002297 * A call to try to shrink memory usage on charge failure at shmem's swapin.
2298 * Calling hierarchical_reclaim is not enough because we should update
2299 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
2300 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
2301 * not from the memcg which this page would be charged to.
2302 * try_charge_swapin does all of these works properly.
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002303 */
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002304int mem_cgroup_shmem_charge_fallback(struct page *page,
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002305 struct mm_struct *mm,
2306 gfp_t gfp_mask)
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002307{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002308 struct mem_cgroup *mem = NULL;
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002309 int ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002310
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002311 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002312 return 0;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002313
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002314 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2315 if (!ret)
2316 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002317
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002318 return ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002319}
2320
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002321static DEFINE_MUTEX(set_limit_mutex);
2322
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08002323static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002324 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002325{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002326 int retry_count;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002327 int progress;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002328 u64 memswlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002329 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002330 int children = mem_cgroup_count_children(memcg);
2331 u64 curusage, oldusage;
2332
2333 /*
2334 * For keeping hierarchical_reclaim simple, how long we should retry
2335 * is depends on callers. We set our retry-count to be function
2336 * of # of children which we should visit in this loop.
2337 */
2338 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
2339
2340 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002341
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002342 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002343 if (signal_pending(current)) {
2344 ret = -EINTR;
2345 break;
2346 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002347 /*
2348 * Rather than hide all in some function, I do this in
2349 * open coded manner. You see what this really does.
2350 * We have to guarantee mem->res.limit < mem->memsw.limit.
2351 */
2352 mutex_lock(&set_limit_mutex);
2353 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2354 if (memswlimit < val) {
2355 ret = -EINVAL;
2356 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002357 break;
2358 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002359 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07002360 if (!ret) {
2361 if (memswlimit == val)
2362 memcg->memsw_is_minimum = true;
2363 else
2364 memcg->memsw_is_minimum = false;
2365 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002366 mutex_unlock(&set_limit_mutex);
2367
2368 if (!ret)
2369 break;
2370
Balbir Singh4e416952009-09-23 15:56:39 -07002371 progress = mem_cgroup_hierarchical_reclaim(memcg, NULL,
2372 GFP_KERNEL,
2373 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002374 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
2375 /* Usage is reduced ? */
2376 if (curusage >= oldusage)
2377 retry_count--;
2378 else
2379 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002380 }
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002381
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002382 return ret;
2383}
2384
Li Zefan338c8432009-06-17 16:27:15 -07002385static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
2386 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002387{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002388 int retry_count;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002389 u64 memlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002390 int children = mem_cgroup_count_children(memcg);
2391 int ret = -EBUSY;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002392
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002393 /* see mem_cgroup_resize_res_limit */
2394 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
2395 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002396 while (retry_count) {
2397 if (signal_pending(current)) {
2398 ret = -EINTR;
2399 break;
2400 }
2401 /*
2402 * Rather than hide all in some function, I do this in
2403 * open coded manner. You see what this really does.
2404 * We have to guarantee mem->res.limit < mem->memsw.limit.
2405 */
2406 mutex_lock(&set_limit_mutex);
2407 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2408 if (memlimit > val) {
2409 ret = -EINVAL;
2410 mutex_unlock(&set_limit_mutex);
2411 break;
2412 }
2413 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07002414 if (!ret) {
2415 if (memlimit == val)
2416 memcg->memsw_is_minimum = true;
2417 else
2418 memcg->memsw_is_minimum = false;
2419 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002420 mutex_unlock(&set_limit_mutex);
2421
2422 if (!ret)
2423 break;
2424
Balbir Singh4e416952009-09-23 15:56:39 -07002425 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh75822b42009-09-23 15:56:38 -07002426 MEM_CGROUP_RECLAIM_NOSWAP |
2427 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002428 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002429 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002430 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002431 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002432 else
2433 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002434 }
2435 return ret;
2436}
2437
Balbir Singh4e416952009-09-23 15:56:39 -07002438unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
2439 gfp_t gfp_mask, int nid,
2440 int zid)
2441{
2442 unsigned long nr_reclaimed = 0;
2443 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
2444 unsigned long reclaimed;
2445 int loop = 0;
2446 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002447 unsigned long long excess;
Balbir Singh4e416952009-09-23 15:56:39 -07002448
2449 if (order > 0)
2450 return 0;
2451
2452 mctz = soft_limit_tree_node_zone(nid, zid);
2453 /*
2454 * This loop can run a while, specially if mem_cgroup's continuously
2455 * keep exceeding their soft limit and putting the system under
2456 * pressure
2457 */
2458 do {
2459 if (next_mz)
2460 mz = next_mz;
2461 else
2462 mz = mem_cgroup_largest_soft_limit_node(mctz);
2463 if (!mz)
2464 break;
2465
2466 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
2467 gfp_mask,
2468 MEM_CGROUP_RECLAIM_SOFT);
2469 nr_reclaimed += reclaimed;
2470 spin_lock(&mctz->lock);
2471
2472 /*
2473 * If we failed to reclaim anything from this memory cgroup
2474 * it is time to move on to the next cgroup
2475 */
2476 next_mz = NULL;
2477 if (!reclaimed) {
2478 do {
2479 /*
2480 * Loop until we find yet another one.
2481 *
2482 * By the time we get the soft_limit lock
2483 * again, someone might have aded the
2484 * group back on the RB tree. Iterate to
2485 * make sure we get a different mem.
2486 * mem_cgroup_largest_soft_limit_node returns
2487 * NULL if no other cgroup is present on
2488 * the tree
2489 */
2490 next_mz =
2491 __mem_cgroup_largest_soft_limit_node(mctz);
2492 if (next_mz == mz) {
2493 css_put(&next_mz->mem->css);
2494 next_mz = NULL;
2495 } else /* next_mz == NULL or other memcg */
2496 break;
2497 } while (1);
2498 }
Balbir Singh4e416952009-09-23 15:56:39 -07002499 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002500 excess = res_counter_soft_limit_excess(&mz->mem->res);
Balbir Singh4e416952009-09-23 15:56:39 -07002501 /*
2502 * One school of thought says that we should not add
2503 * back the node to the tree if reclaim returns 0.
2504 * But our reclaim could return 0, simply because due
2505 * to priority we are exposing a smaller subset of
2506 * memory to reclaim from. Consider this as a longer
2507 * term TODO.
2508 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002509 /* If excess == 0, no tree ops */
2510 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07002511 spin_unlock(&mctz->lock);
2512 css_put(&mz->mem->css);
2513 loop++;
2514 /*
2515 * Could not reclaim anything and there are no more
2516 * mem cgroups to try or we seem to be looping without
2517 * reclaiming anything.
2518 */
2519 if (!nr_reclaimed &&
2520 (next_mz == NULL ||
2521 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
2522 break;
2523 } while (!nr_reclaimed);
2524 if (next_mz)
2525 css_put(&next_mz->mem->css);
2526 return nr_reclaimed;
2527}
2528
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002529/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002530 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002531 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
2532 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002533static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002534 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002535{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002536 struct zone *zone;
2537 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002538 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002539 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002540 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002541 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002542
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002543 zone = &NODE_DATA(node)->node_zones[zid];
2544 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07002545 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002546
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002547 loop = MEM_CGROUP_ZSTAT(mz, lru);
2548 /* give some margin against EBUSY etc...*/
2549 loop += 256;
2550 busy = NULL;
2551 while (loop--) {
2552 ret = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002553 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002554 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002555 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002556 break;
2557 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002558 pc = list_entry(list->prev, struct page_cgroup, lru);
2559 if (busy == pc) {
2560 list_move(&pc->lru, list);
2561 busy = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002562 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002563 continue;
2564 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002565 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002566
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -08002567 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002568 if (ret == -ENOMEM)
2569 break;
2570
2571 if (ret == -EBUSY || ret == -EINVAL) {
2572 /* found lock contention or "pc" is obsolete. */
2573 busy = pc;
2574 cond_resched();
2575 } else
2576 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002577 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002578
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002579 if (!ret && !list_empty(list))
2580 return -EBUSY;
2581 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002582}
2583
2584/*
2585 * make mem_cgroup's charge to be 0 if there is no task.
2586 * This enables deleting this mem_cgroup.
2587 */
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002588static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002589{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002590 int ret;
2591 int node, zid, shrink;
2592 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002593 struct cgroup *cgrp = mem->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08002594
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002595 css_get(&mem->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002596
2597 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002598 /* should free all ? */
2599 if (free_all)
2600 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002601move_account:
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002602 while (mem->res.usage > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002603 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002604 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002605 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002606 ret = -EINTR;
2607 if (signal_pending(current))
2608 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002609 /* This is for making all *used* pages to be on LRU. */
2610 lru_add_drain_all();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002611 drain_all_stock_sync();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002612 ret = 0;
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08002613 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002614 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07002615 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002616 for_each_lru(l) {
2617 ret = mem_cgroup_force_empty_list(mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002618 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002619 if (ret)
2620 break;
2621 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002622 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002623 if (ret)
2624 break;
2625 }
2626 /* it seems parent cgroup doesn't have enough mem */
2627 if (ret == -ENOMEM)
2628 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002629 cond_resched();
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002630 }
2631 ret = 0;
2632out:
2633 css_put(&mem->css);
2634 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002635
2636try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002637 /* returns EBUSY if there is a task or if we come here twice. */
2638 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002639 ret = -EBUSY;
2640 goto out;
2641 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002642 /* we call try-to-free pages for make this cgroup empty */
2643 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002644 /* try to free all pages in this cgroup */
2645 shrink = 1;
2646 while (nr_retries && mem->res.usage > 0) {
2647 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002648
2649 if (signal_pending(current)) {
2650 ret = -EINTR;
2651 goto out;
2652 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002653 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
2654 false, get_swappiness(mem));
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002655 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002656 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002657 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02002658 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002659 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002660
2661 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002662 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002663 /* try move_account...there may be some *locked* pages. */
2664 if (mem->res.usage)
2665 goto move_account;
2666 ret = 0;
2667 goto out;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002668}
2669
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002670int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
2671{
2672 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
2673}
2674
2675
Balbir Singh18f59ea2009-01-07 18:08:07 -08002676static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
2677{
2678 return mem_cgroup_from_cont(cont)->use_hierarchy;
2679}
2680
2681static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
2682 u64 val)
2683{
2684 int retval = 0;
2685 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2686 struct cgroup *parent = cont->parent;
2687 struct mem_cgroup *parent_mem = NULL;
2688
2689 if (parent)
2690 parent_mem = mem_cgroup_from_cont(parent);
2691
2692 cgroup_lock();
2693 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002694 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08002695 * in the child subtrees. If it is unset, then the change can
2696 * occur, provided the current cgroup has no children.
2697 *
2698 * For the root cgroup, parent_mem is NULL, we allow value to be
2699 * set if there are no children.
2700 */
2701 if ((!parent_mem || !parent_mem->use_hierarchy) &&
2702 (val == 1 || val == 0)) {
2703 if (list_empty(&cont->children))
2704 mem->use_hierarchy = val;
2705 else
2706 retval = -EBUSY;
2707 } else
2708 retval = -EINVAL;
2709 cgroup_unlock();
2710
2711 return retval;
2712}
2713
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002714struct mem_cgroup_idx_data {
2715 s64 val;
2716 enum mem_cgroup_stat_index idx;
2717};
2718
2719static int
2720mem_cgroup_get_idx_stat(struct mem_cgroup *mem, void *data)
2721{
2722 struct mem_cgroup_idx_data *d = data;
2723 d->val += mem_cgroup_read_stat(&mem->stat, d->idx);
2724 return 0;
2725}
2726
2727static void
2728mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem,
2729 enum mem_cgroup_stat_index idx, s64 *val)
2730{
2731 struct mem_cgroup_idx_data d;
2732 d.idx = idx;
2733 d.val = 0;
2734 mem_cgroup_walk_tree(mem, &d, mem_cgroup_get_idx_stat);
2735 *val = d.val;
2736}
2737
Paul Menage2c3daa72008-04-29 00:59:58 -07002738static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002739{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002740 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002741 u64 idx_val, val;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002742 int type, name;
2743
2744 type = MEMFILE_TYPE(cft->private);
2745 name = MEMFILE_ATTR(cft->private);
2746 switch (type) {
2747 case _MEM:
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002748 if (name == RES_USAGE && mem_cgroup_is_root(mem)) {
2749 mem_cgroup_get_recursive_idx_stat(mem,
2750 MEM_CGROUP_STAT_CACHE, &idx_val);
2751 val = idx_val;
2752 mem_cgroup_get_recursive_idx_stat(mem,
2753 MEM_CGROUP_STAT_RSS, &idx_val);
2754 val += idx_val;
2755 val <<= PAGE_SHIFT;
2756 } else
2757 val = res_counter_read_u64(&mem->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002758 break;
2759 case _MEMSWAP:
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002760 if (name == RES_USAGE && mem_cgroup_is_root(mem)) {
2761 mem_cgroup_get_recursive_idx_stat(mem,
2762 MEM_CGROUP_STAT_CACHE, &idx_val);
2763 val = idx_val;
2764 mem_cgroup_get_recursive_idx_stat(mem,
2765 MEM_CGROUP_STAT_RSS, &idx_val);
2766 val += idx_val;
2767 mem_cgroup_get_recursive_idx_stat(mem,
2768 MEM_CGROUP_STAT_SWAPOUT, &idx_val);
Kirill A. Shutemovcd9b45b2009-12-15 16:47:01 -08002769 val += idx_val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002770 val <<= PAGE_SHIFT;
2771 } else
2772 val = res_counter_read_u64(&mem->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002773 break;
2774 default:
2775 BUG();
2776 break;
2777 }
2778 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002779}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002780/*
2781 * The user of this function is...
2782 * RES_LIMIT.
2783 */
Paul Menage856c13a2008-07-25 01:47:04 -07002784static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
2785 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002786{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002787 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002788 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002789 unsigned long long val;
2790 int ret;
2791
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002792 type = MEMFILE_TYPE(cft->private);
2793 name = MEMFILE_ATTR(cft->private);
2794 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002795 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07002796 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
2797 ret = -EINVAL;
2798 break;
2799 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002800 /* This function does all necessary parse...reuse it */
2801 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002802 if (ret)
2803 break;
2804 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002805 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002806 else
2807 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002808 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07002809 case RES_SOFT_LIMIT:
2810 ret = res_counter_memparse_write_strategy(buffer, &val);
2811 if (ret)
2812 break;
2813 /*
2814 * For memsw, soft limits are hard to implement in terms
2815 * of semantics, for now, we support soft limits for
2816 * control without swap
2817 */
2818 if (type == _MEM)
2819 ret = res_counter_set_soft_limit(&memcg->res, val);
2820 else
2821 ret = -EINVAL;
2822 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002823 default:
2824 ret = -EINVAL; /* should be BUG() ? */
2825 break;
2826 }
2827 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002828}
2829
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08002830static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
2831 unsigned long long *mem_limit, unsigned long long *memsw_limit)
2832{
2833 struct cgroup *cgroup;
2834 unsigned long long min_limit, min_memsw_limit, tmp;
2835
2836 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2837 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2838 cgroup = memcg->css.cgroup;
2839 if (!memcg->use_hierarchy)
2840 goto out;
2841
2842 while (cgroup->parent) {
2843 cgroup = cgroup->parent;
2844 memcg = mem_cgroup_from_cont(cgroup);
2845 if (!memcg->use_hierarchy)
2846 break;
2847 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
2848 min_limit = min(min_limit, tmp);
2849 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2850 min_memsw_limit = min(min_memsw_limit, tmp);
2851 }
2852out:
2853 *mem_limit = min_limit;
2854 *memsw_limit = min_memsw_limit;
2855 return;
2856}
2857
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002858static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002859{
2860 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002861 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002862
2863 mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002864 type = MEMFILE_TYPE(event);
2865 name = MEMFILE_ATTR(event);
2866 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002867 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002868 if (type == _MEM)
2869 res_counter_reset_max(&mem->res);
2870 else
2871 res_counter_reset_max(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002872 break;
2873 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002874 if (type == _MEM)
2875 res_counter_reset_failcnt(&mem->res);
2876 else
2877 res_counter_reset_failcnt(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002878 break;
2879 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07002880
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07002881 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002882}
2883
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002884
2885/* For read statistics */
2886enum {
2887 MCS_CACHE,
2888 MCS_RSS,
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08002889 MCS_FILE_MAPPED,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002890 MCS_PGPGIN,
2891 MCS_PGPGOUT,
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002892 MCS_SWAP,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002893 MCS_INACTIVE_ANON,
2894 MCS_ACTIVE_ANON,
2895 MCS_INACTIVE_FILE,
2896 MCS_ACTIVE_FILE,
2897 MCS_UNEVICTABLE,
2898 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002899};
2900
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002901struct mcs_total_stat {
2902 s64 stat[NR_MCS_STAT];
2903};
2904
2905struct {
2906 char *local_name;
2907 char *total_name;
2908} memcg_stat_strings[NR_MCS_STAT] = {
2909 {"cache", "total_cache"},
2910 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07002911 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002912 {"pgpgin", "total_pgpgin"},
2913 {"pgpgout", "total_pgpgout"},
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002914 {"swap", "total_swap"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002915 {"inactive_anon", "total_inactive_anon"},
2916 {"active_anon", "total_active_anon"},
2917 {"inactive_file", "total_inactive_file"},
2918 {"active_file", "total_active_file"},
2919 {"unevictable", "total_unevictable"}
2920};
2921
2922
2923static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
2924{
2925 struct mcs_total_stat *s = data;
2926 s64 val;
2927
2928 /* per cpu stat */
2929 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE);
2930 s->stat[MCS_CACHE] += val * PAGE_SIZE;
2931 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
2932 s->stat[MCS_RSS] += val * PAGE_SIZE;
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08002933 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_FILE_MAPPED);
2934 s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002935 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT);
2936 s->stat[MCS_PGPGIN] += val;
2937 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT);
2938 s->stat[MCS_PGPGOUT] += val;
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002939 if (do_swap_account) {
2940 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_SWAPOUT);
2941 s->stat[MCS_SWAP] += val * PAGE_SIZE;
2942 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002943
2944 /* per zone stat */
2945 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
2946 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
2947 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
2948 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
2949 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
2950 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
2951 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
2952 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
2953 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
2954 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
2955 return 0;
2956}
2957
2958static void
2959mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
2960{
2961 mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
2962}
2963
Paul Menagec64745c2008-04-29 01:00:02 -07002964static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
2965 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002966{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002967 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002968 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002969 int i;
2970
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002971 memset(&mystat, 0, sizeof(mystat));
2972 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002973
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002974 for (i = 0; i < NR_MCS_STAT; i++) {
2975 if (i == MCS_SWAP && !do_swap_account)
2976 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002977 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002978 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002979
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002980 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08002981 {
2982 unsigned long long limit, memsw_limit;
2983 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
2984 cb->fill(cb, "hierarchical_memory_limit", limit);
2985 if (do_swap_account)
2986 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
2987 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002988
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002989 memset(&mystat, 0, sizeof(mystat));
2990 mem_cgroup_get_total_stat(mem_cont, &mystat);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002991 for (i = 0; i < NR_MCS_STAT; i++) {
2992 if (i == MCS_SWAP && !do_swap_account)
2993 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002994 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002995 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002996
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002997#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08002998 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002999
3000 {
3001 int nid, zid;
3002 struct mem_cgroup_per_zone *mz;
3003 unsigned long recent_rotated[2] = {0, 0};
3004 unsigned long recent_scanned[2] = {0, 0};
3005
3006 for_each_online_node(nid)
3007 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
3008 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
3009
3010 recent_rotated[0] +=
3011 mz->reclaim_stat.recent_rotated[0];
3012 recent_rotated[1] +=
3013 mz->reclaim_stat.recent_rotated[1];
3014 recent_scanned[0] +=
3015 mz->reclaim_stat.recent_scanned[0];
3016 recent_scanned[1] +=
3017 mz->reclaim_stat.recent_scanned[1];
3018 }
3019 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
3020 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
3021 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
3022 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
3023 }
3024#endif
3025
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003026 return 0;
3027}
3028
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003029static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
3030{
3031 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3032
3033 return get_swappiness(memcg);
3034}
3035
3036static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
3037 u64 val)
3038{
3039 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3040 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08003041
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003042 if (val > 100)
3043 return -EINVAL;
3044
3045 if (cgrp->parent == NULL)
3046 return -EINVAL;
3047
3048 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08003049
3050 cgroup_lock();
3051
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003052 /* If under hierarchy, only empty-root can set this value */
3053 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08003054 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
3055 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003056 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08003057 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003058
3059 spin_lock(&memcg->reclaim_param_lock);
3060 memcg->swappiness = val;
3061 spin_unlock(&memcg->reclaim_param_lock);
3062
Li Zefan068b38c2009-01-15 13:51:26 -08003063 cgroup_unlock();
3064
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003065 return 0;
3066}
3067
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003068
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003069static struct cftype mem_cgroup_files[] = {
3070 {
Balbir Singh0eea1032008-02-07 00:13:57 -08003071 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003072 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07003073 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003074 },
3075 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003076 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003077 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003078 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003079 .read_u64 = mem_cgroup_read,
3080 },
3081 {
Balbir Singh0eea1032008-02-07 00:13:57 -08003082 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003083 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07003084 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07003085 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003086 },
3087 {
Balbir Singh296c81d2009-09-23 15:56:36 -07003088 .name = "soft_limit_in_bytes",
3089 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
3090 .write_string = mem_cgroup_write,
3091 .read_u64 = mem_cgroup_read,
3092 },
3093 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003094 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003095 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003096 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07003097 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003098 },
Balbir Singh8697d332008-02-07 00:13:59 -08003099 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003100 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07003101 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003102 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003103 {
3104 .name = "force_empty",
3105 .trigger = mem_cgroup_force_empty_write,
3106 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08003107 {
3108 .name = "use_hierarchy",
3109 .write_u64 = mem_cgroup_hierarchy_write,
3110 .read_u64 = mem_cgroup_hierarchy_read,
3111 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003112 {
3113 .name = "swappiness",
3114 .read_u64 = mem_cgroup_swappiness_read,
3115 .write_u64 = mem_cgroup_swappiness_write,
3116 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003117};
3118
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003119#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3120static struct cftype memsw_cgroup_files[] = {
3121 {
3122 .name = "memsw.usage_in_bytes",
3123 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
3124 .read_u64 = mem_cgroup_read,
3125 },
3126 {
3127 .name = "memsw.max_usage_in_bytes",
3128 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
3129 .trigger = mem_cgroup_reset,
3130 .read_u64 = mem_cgroup_read,
3131 },
3132 {
3133 .name = "memsw.limit_in_bytes",
3134 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
3135 .write_string = mem_cgroup_write,
3136 .read_u64 = mem_cgroup_read,
3137 },
3138 {
3139 .name = "memsw.failcnt",
3140 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
3141 .trigger = mem_cgroup_reset,
3142 .read_u64 = mem_cgroup_read,
3143 },
3144};
3145
3146static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
3147{
3148 if (!do_swap_account)
3149 return 0;
3150 return cgroup_add_files(cont, ss, memsw_cgroup_files,
3151 ARRAY_SIZE(memsw_cgroup_files));
3152};
3153#else
3154static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
3155{
3156 return 0;
3157}
3158#endif
3159
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003160static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
3161{
3162 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003163 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07003164 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07003165 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003166 /*
3167 * This routine is called against possible nodes.
3168 * But it's BUG to call kmalloc() against offline node.
3169 *
3170 * TODO: this routine can waste much memory for nodes which will
3171 * never be onlined. It's better to use memory hotplug callback
3172 * function.
3173 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07003174 if (!node_state(node, N_NORMAL_MEMORY))
3175 tmp = -1;
3176 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003177 if (!pn)
3178 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003179
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003180 mem->info.nodeinfo[node] = pn;
3181 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003182
3183 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
3184 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07003185 for_each_lru(l)
3186 INIT_LIST_HEAD(&mz->lists[l]);
Balbir Singhf64c3f52009-09-23 15:56:37 -07003187 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07003188 mz->on_tree = false;
3189 mz->mem = mem;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003190 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003191 return 0;
3192}
3193
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003194static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
3195{
3196 kfree(mem->info.nodeinfo[node]);
3197}
3198
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003199static int mem_cgroup_size(void)
3200{
3201 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
3202 return sizeof(struct mem_cgroup) + cpustat_size;
3203}
3204
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003205static struct mem_cgroup *mem_cgroup_alloc(void)
3206{
3207 struct mem_cgroup *mem;
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003208 int size = mem_cgroup_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003209
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003210 if (size < PAGE_SIZE)
3211 mem = kmalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003212 else
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003213 mem = vmalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003214
3215 if (mem)
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003216 memset(mem, 0, size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003217 return mem;
3218}
3219
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003220/*
3221 * At destroying mem_cgroup, references from swap_cgroup can remain.
3222 * (scanning all at force_empty is too costly...)
3223 *
3224 * Instead of clearing all references at force_empty, we remember
3225 * the number of reference from swap_cgroup and free mem_cgroup when
3226 * it goes down to 0.
3227 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003228 * Removal of cgroup itself succeeds regardless of refs from swap.
3229 */
3230
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003231static void __mem_cgroup_free(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003232{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003233 int node;
3234
Balbir Singhf64c3f52009-09-23 15:56:37 -07003235 mem_cgroup_remove_from_trees(mem);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003236 free_css_id(&mem_cgroup_subsys, &mem->css);
3237
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003238 for_each_node_state(node, N_POSSIBLE)
3239 free_mem_cgroup_per_zone_info(mem, node);
3240
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003241 if (mem_cgroup_size() < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003242 kfree(mem);
3243 else
3244 vfree(mem);
3245}
3246
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003247static void mem_cgroup_get(struct mem_cgroup *mem)
3248{
3249 atomic_inc(&mem->refcnt);
3250}
3251
3252static void mem_cgroup_put(struct mem_cgroup *mem)
3253{
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003254 if (atomic_dec_and_test(&mem->refcnt)) {
3255 struct mem_cgroup *parent = parent_mem_cgroup(mem);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003256 __mem_cgroup_free(mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003257 if (parent)
3258 mem_cgroup_put(parent);
3259 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003260}
3261
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003262/*
3263 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
3264 */
3265static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
3266{
3267 if (!mem->res.parent)
3268 return NULL;
3269 return mem_cgroup_from_res_counter(mem->res.parent, res);
3270}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003271
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003272#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3273static void __init enable_swap_cgroup(void)
3274{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003275 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003276 do_swap_account = 1;
3277}
3278#else
3279static void __init enable_swap_cgroup(void)
3280{
3281}
3282#endif
3283
Balbir Singhf64c3f52009-09-23 15:56:37 -07003284static int mem_cgroup_soft_limit_tree_init(void)
3285{
3286 struct mem_cgroup_tree_per_node *rtpn;
3287 struct mem_cgroup_tree_per_zone *rtpz;
3288 int tmp, node, zone;
3289
3290 for_each_node_state(node, N_POSSIBLE) {
3291 tmp = node;
3292 if (!node_state(node, N_NORMAL_MEMORY))
3293 tmp = -1;
3294 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
3295 if (!rtpn)
3296 return 1;
3297
3298 soft_limit_tree.rb_tree_per_node[node] = rtpn;
3299
3300 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
3301 rtpz = &rtpn->rb_tree_per_zone[zone];
3302 rtpz->rb_root = RB_ROOT;
3303 spin_lock_init(&rtpz->lock);
3304 }
3305 }
3306 return 0;
3307}
3308
Li Zefan0eb253e2009-01-15 13:51:25 -08003309static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003310mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
3311{
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003312 struct mem_cgroup *mem, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003313 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003314 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003315
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003316 mem = mem_cgroup_alloc();
3317 if (!mem)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003318 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08003319
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003320 for_each_node_state(node, N_POSSIBLE)
3321 if (alloc_mem_cgroup_per_zone_info(mem, node))
3322 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07003323
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003324 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003325 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08003326 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003327 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003328 parent = NULL;
Balbir Singh4b3bde42009-09-23 15:56:32 -07003329 root_mem_cgroup = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07003330 if (mem_cgroup_soft_limit_tree_init())
3331 goto free_out;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08003332 for_each_possible_cpu(cpu) {
3333 struct memcg_stock_pcp *stock =
3334 &per_cpu(memcg_stock, cpu);
3335 INIT_WORK(&stock->work, drain_local_stock);
3336 }
3337 hotcpu_notifier(memcg_stock_cpu_callback, 0);
Balbir Singhf64c3f52009-09-23 15:56:37 -07003338
Balbir Singh18f59ea2009-01-07 18:08:07 -08003339 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003340 parent = mem_cgroup_from_cont(cont->parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003341 mem->use_hierarchy = parent->use_hierarchy;
3342 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003343
Balbir Singh18f59ea2009-01-07 18:08:07 -08003344 if (parent && parent->use_hierarchy) {
3345 res_counter_init(&mem->res, &parent->res);
3346 res_counter_init(&mem->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003347 /*
3348 * We increment refcnt of the parent to ensure that we can
3349 * safely access it on res_counter_charge/uncharge.
3350 * This refcnt will be decremented when freeing this
3351 * mem_cgroup(see mem_cgroup_put).
3352 */
3353 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003354 } else {
3355 res_counter_init(&mem->res, NULL);
3356 res_counter_init(&mem->memsw, NULL);
3357 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003358 mem->last_scanned_child = 0;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -08003359 spin_lock_init(&mem->reclaim_param_lock);
Balbir Singh6d61ef42009-01-07 18:08:06 -08003360
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003361 if (parent)
3362 mem->swappiness = get_swappiness(parent);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003363 atomic_set(&mem->refcnt, 1);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003364 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003365free_out:
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003366 __mem_cgroup_free(mem);
Balbir Singh4b3bde42009-09-23 15:56:32 -07003367 root_mem_cgroup = NULL;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003368 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003369}
3370
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003371static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08003372 struct cgroup *cont)
3373{
3374 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003375
3376 return mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08003377}
3378
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003379static void mem_cgroup_destroy(struct cgroup_subsys *ss,
3380 struct cgroup *cont)
3381{
Daisuke Nishimurac268e992009-01-15 13:51:13 -08003382 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08003383
Daisuke Nishimurac268e992009-01-15 13:51:13 -08003384 mem_cgroup_put(mem);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003385}
3386
3387static int mem_cgroup_populate(struct cgroup_subsys *ss,
3388 struct cgroup *cont)
3389{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003390 int ret;
3391
3392 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
3393 ARRAY_SIZE(mem_cgroup_files));
3394
3395 if (!ret)
3396 ret = register_memsw_files(cont, ss);
3397 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003398}
3399
Balbir Singh67e465a2008-02-07 00:13:54 -08003400static void mem_cgroup_move_task(struct cgroup_subsys *ss,
3401 struct cgroup *cont,
3402 struct cgroup *old_cont,
Ben Blumbe367d02009-09-23 15:56:31 -07003403 struct task_struct *p,
3404 bool threadgroup)
Balbir Singh67e465a2008-02-07 00:13:54 -08003405{
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08003406 mutex_lock(&memcg_tasklist);
Balbir Singh67e465a2008-02-07 00:13:54 -08003407 /*
Nikanth Karthikesanf9717d22009-01-07 18:08:11 -08003408 * FIXME: It's better to move charges of this process from old
3409 * memcg to new memcg. But it's just on TODO-List now.
Balbir Singh67e465a2008-02-07 00:13:54 -08003410 */
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08003411 mutex_unlock(&memcg_tasklist);
Balbir Singh67e465a2008-02-07 00:13:54 -08003412}
3413
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003414struct cgroup_subsys mem_cgroup_subsys = {
3415 .name = "memory",
3416 .subsys_id = mem_cgroup_subsys_id,
3417 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08003418 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003419 .destroy = mem_cgroup_destroy,
3420 .populate = mem_cgroup_populate,
Balbir Singh67e465a2008-02-07 00:13:54 -08003421 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003422 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003423 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003424};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003425
3426#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3427
3428static int __init disable_swap_account(char *s)
3429{
3430 really_do_swap_account = 0;
3431 return 1;
3432}
3433__setup("noswapaccount", disable_swap_account);
3434#endif