blob: 0ed325943cd1f89dddeff8403148164870bea75c [file] [log] [blame]
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001/* memcontrol.c - Memory Controller
2 *
3 * Copyright IBM Corporation, 2007
4 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
5 *
Pavel Emelianov78fb7462008-02-07 00:13:51 -08006 * Copyright 2007 OpenVZ SWsoft Inc
7 * Author: Pavel Emelianov <xemul@openvz.org>
8 *
Balbir Singh8cdea7c2008-02-07 00:13:50 -08009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 */
19
20#include <linux/res_counter.h>
21#include <linux/memcontrol.h>
22#include <linux/cgroup.h>
Pavel Emelianov78fb7462008-02-07 00:13:51 -080023#include <linux/mm.h>
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -080024#include <linux/pagemap.h>
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080025#include <linux/smp.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080026#include <linux/page-flags.h>
Balbir Singh66e17072008-02-07 00:13:56 -080027#include <linux/backing-dev.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080028#include <linux/bit_spinlock.h>
29#include <linux/rcupdate.h>
Balbir Singhe2224322009-04-02 16:57:39 -070030#include <linux/limits.h>
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -080031#include <linux/mutex.h>
Balbir Singhf64c3f52009-09-23 15:56:37 -070032#include <linux/rbtree.h>
Balbir Singhb6ac57d2008-04-29 01:00:19 -070033#include <linux/slab.h>
Balbir Singh66e17072008-02-07 00:13:56 -080034#include <linux/swap.h>
35#include <linux/spinlock.h>
36#include <linux/fs.h>
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -080037#include <linux/seq_file.h>
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -070038#include <linux/vmalloc.h>
Christoph Lameterb69408e2008-10-18 20:26:14 -070039#include <linux/mm_inline.h>
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070040#include <linux/page_cgroup.h>
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -080041#include "internal.h"
Balbir Singh8cdea7c2008-02-07 00:13:50 -080042
Balbir Singh8697d332008-02-07 00:13:59 -080043#include <asm/uaccess.h>
44
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070045struct cgroup_subsys mem_cgroup_subsys __read_mostly;
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070046#define MEM_CGROUP_RECLAIM_RETRIES 5
Balbir Singh4b3bde42009-09-23 15:56:32 -070047struct mem_cgroup *root_mem_cgroup __read_mostly;
Balbir Singh8cdea7c2008-02-07 00:13:50 -080048
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080049#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
Li Zefan338c8432009-06-17 16:27:15 -070050/* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080051int do_swap_account __read_mostly;
52static int really_do_swap_account __initdata = 1; /* for remember boot option*/
53#else
54#define do_swap_account (0)
55#endif
56
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -080057static DEFINE_MUTEX(memcg_tasklist); /* can be hold under cgroup_mutex */
Balbir Singhf64c3f52009-09-23 15:56:37 -070058#define SOFTLIMIT_EVENTS_THRESH (1000)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080059
Balbir Singh8cdea7c2008-02-07 00:13:50 -080060/*
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080061 * Statistics for memory cgroup.
62 */
63enum mem_cgroup_stat_index {
64 /*
65 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
66 */
67 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
Balbir Singhd69b0422009-06-17 16:26:34 -070068 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
69 MEM_CGROUP_STAT_MAPPED_FILE, /* # of pages charged as file rss */
Balaji Rao55e462b2008-05-01 04:35:12 -070070 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
71 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
Balbir Singhf64c3f52009-09-23 15:56:37 -070072 MEM_CGROUP_STAT_EVENTS, /* sum of pagein + pageout for internal use */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080073
74 MEM_CGROUP_STAT_NSTATS,
75};
76
77struct mem_cgroup_stat_cpu {
78 s64 count[MEM_CGROUP_STAT_NSTATS];
79} ____cacheline_aligned_in_smp;
80
81struct mem_cgroup_stat {
Jan Blunckc8dad2b2009-01-07 18:07:53 -080082 struct mem_cgroup_stat_cpu cpustat[0];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080083};
84
Balbir Singhf64c3f52009-09-23 15:56:37 -070085static inline void
86__mem_cgroup_stat_reset_safe(struct mem_cgroup_stat_cpu *stat,
87 enum mem_cgroup_stat_index idx)
88{
89 stat->count[idx] = 0;
90}
91
92static inline s64
93__mem_cgroup_stat_read_local(struct mem_cgroup_stat_cpu *stat,
94 enum mem_cgroup_stat_index idx)
95{
96 return stat->count[idx];
97}
98
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080099/*
100 * For accounting under irq disable, no need for increment preempt count.
101 */
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700102static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800103 enum mem_cgroup_stat_index idx, int val)
104{
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700105 stat->count[idx] += val;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800106}
107
108static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
109 enum mem_cgroup_stat_index idx)
110{
111 int cpu;
112 s64 ret = 0;
113 for_each_possible_cpu(cpu)
114 ret += stat->cpustat[cpu].count[idx];
115 return ret;
116}
117
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700118static s64 mem_cgroup_local_usage(struct mem_cgroup_stat *stat)
119{
120 s64 ret;
121
122 ret = mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_CACHE);
123 ret += mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_RSS);
124 return ret;
125}
126
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800127/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800128 * per-zone information in memory controller.
129 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800130struct mem_cgroup_per_zone {
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800131 /*
132 * spin_lock to protect the per cgroup LRU
133 */
Christoph Lameterb69408e2008-10-18 20:26:14 -0700134 struct list_head lists[NR_LRU_LISTS];
135 unsigned long count[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800136
137 struct zone_reclaim_stat reclaim_stat;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700138 struct rb_node tree_node; /* RB tree node */
139 unsigned long long usage_in_excess;/* Set to the value by which */
140 /* the soft limit is exceeded*/
141 bool on_tree;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800142};
143/* Macro for accessing counter */
144#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
145
146struct mem_cgroup_per_node {
147 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
148};
149
150struct mem_cgroup_lru_info {
151 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
152};
153
154/*
Balbir Singhf64c3f52009-09-23 15:56:37 -0700155 * Cgroups above their limits are maintained in a RB-Tree, independent of
156 * their hierarchy representation
157 */
158
159struct mem_cgroup_tree_per_zone {
160 struct rb_root rb_root;
161 spinlock_t lock;
162};
163
164struct mem_cgroup_tree_per_node {
165 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
166};
167
168struct mem_cgroup_tree {
169 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
170};
171
172static struct mem_cgroup_tree soft_limit_tree __read_mostly;
173
174/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800175 * The memory controller data structure. The memory controller controls both
176 * page cache and RSS per cgroup. We would eventually like to provide
177 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
178 * to help the administrator determine what knobs to tune.
179 *
180 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800181 * we hit the water mark. May be even add a low water mark, such that
182 * no reclaim occurs from a cgroup at it's low water mark, this is
183 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800184 */
185struct mem_cgroup {
186 struct cgroup_subsys_state css;
187 /*
188 * the counter to account for memory usage
189 */
190 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800191 /*
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800192 * the counter to account for mem+swap usage.
193 */
194 struct res_counter memsw;
195 /*
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800196 * Per cgroup active and inactive list, similar to the
197 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800198 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800199 struct mem_cgroup_lru_info info;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800200
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800201 /*
202 protect against reclaim related member.
203 */
204 spinlock_t reclaim_param_lock;
205
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800206 int prev_priority; /* for recording reclaim priority */
Balbir Singh6d61ef42009-01-07 18:08:06 -0800207
208 /*
209 * While reclaiming in a hiearchy, we cache the last child we
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700210 * reclaimed from.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800211 */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700212 int last_scanned_child;
Balbir Singh18f59ea2009-01-07 18:08:07 -0800213 /*
214 * Should the accounting and control be hierarchical, per subtree?
215 */
216 bool use_hierarchy;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800217 unsigned long last_oom_jiffies;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800218 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800219
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800220 unsigned int swappiness;
221
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700222 /* set when res.limit == memsw.limit */
223 bool memsw_is_minimum;
224
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800225 /*
Jan Blunckc8dad2b2009-01-07 18:07:53 -0800226 * statistics. This must be placed at the end of memcg.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800227 */
228 struct mem_cgroup_stat stat;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800229};
230
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800231enum charge_type {
232 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
233 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700234 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700235 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800236 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700237 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700238 NR_CHARGE_TYPE,
239};
240
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700241/* only for here (for easy reading.) */
242#define PCGF_CACHE (1UL << PCG_CACHE)
243#define PCGF_USED (1UL << PCG_USED)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700244#define PCGF_LOCK (1UL << PCG_LOCK)
Balbir Singh4b3bde42009-09-23 15:56:32 -0700245/* Not used, but added here for completeness */
246#define PCGF_ACCT (1UL << PCG_ACCT)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800247
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800248/* for encoding cft->private value on file */
249#define _MEM (0)
250#define _MEMSWAP (1)
251#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
252#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
253#define MEMFILE_ATTR(val) ((val) & 0xffff)
254
255static void mem_cgroup_get(struct mem_cgroup *mem);
256static void mem_cgroup_put(struct mem_cgroup *mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -0800257static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800258
Balbir Singhf64c3f52009-09-23 15:56:37 -0700259static struct mem_cgroup_per_zone *
260mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
261{
262 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
263}
264
265static struct mem_cgroup_per_zone *
266page_cgroup_zoneinfo(struct page_cgroup *pc)
267{
268 struct mem_cgroup *mem = pc->mem_cgroup;
269 int nid = page_cgroup_nid(pc);
270 int zid = page_cgroup_zid(pc);
271
272 if (!mem)
273 return NULL;
274
275 return mem_cgroup_zoneinfo(mem, nid, zid);
276}
277
278static struct mem_cgroup_tree_per_zone *
279soft_limit_tree_node_zone(int nid, int zid)
280{
281 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
282}
283
284static struct mem_cgroup_tree_per_zone *
285soft_limit_tree_from_page(struct page *page)
286{
287 int nid = page_to_nid(page);
288 int zid = page_zonenum(page);
289
290 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
291}
292
293static void
294mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
295 struct mem_cgroup_per_zone *mz,
296 struct mem_cgroup_tree_per_zone *mctz)
297{
298 struct rb_node **p = &mctz->rb_root.rb_node;
299 struct rb_node *parent = NULL;
300 struct mem_cgroup_per_zone *mz_node;
301
302 if (mz->on_tree)
303 return;
304
305 mz->usage_in_excess = res_counter_soft_limit_excess(&mem->res);
306 spin_lock(&mctz->lock);
307 while (*p) {
308 parent = *p;
309 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
310 tree_node);
311 if (mz->usage_in_excess < mz_node->usage_in_excess)
312 p = &(*p)->rb_left;
313 /*
314 * We can't avoid mem cgroups that are over their soft
315 * limit by the same amount
316 */
317 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
318 p = &(*p)->rb_right;
319 }
320 rb_link_node(&mz->tree_node, parent, p);
321 rb_insert_color(&mz->tree_node, &mctz->rb_root);
322 mz->on_tree = true;
323 spin_unlock(&mctz->lock);
324}
325
326static void
327mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
328 struct mem_cgroup_per_zone *mz,
329 struct mem_cgroup_tree_per_zone *mctz)
330{
331 spin_lock(&mctz->lock);
332 rb_erase(&mz->tree_node, &mctz->rb_root);
333 mz->on_tree = false;
334 spin_unlock(&mctz->lock);
335}
336
337static bool mem_cgroup_soft_limit_check(struct mem_cgroup *mem)
338{
339 bool ret = false;
340 int cpu;
341 s64 val;
342 struct mem_cgroup_stat_cpu *cpustat;
343
344 cpu = get_cpu();
345 cpustat = &mem->stat.cpustat[cpu];
346 val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_EVENTS);
347 if (unlikely(val > SOFTLIMIT_EVENTS_THRESH)) {
348 __mem_cgroup_stat_reset_safe(cpustat, MEM_CGROUP_STAT_EVENTS);
349 ret = true;
350 }
351 put_cpu();
352 return ret;
353}
354
355static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
356{
357 unsigned long long prev_usage_in_excess, new_usage_in_excess;
358 bool updated_tree = false;
359 struct mem_cgroup_per_zone *mz;
360 struct mem_cgroup_tree_per_zone *mctz;
361
362 mz = mem_cgroup_zoneinfo(mem, page_to_nid(page), page_zonenum(page));
363 mctz = soft_limit_tree_from_page(page);
364
365 /*
366 * We do updates in lazy mode, mem's are removed
367 * lazily from the per-zone, per-node rb tree
368 */
369 prev_usage_in_excess = mz->usage_in_excess;
370
371 new_usage_in_excess = res_counter_soft_limit_excess(&mem->res);
372 if (prev_usage_in_excess) {
373 mem_cgroup_remove_exceeded(mem, mz, mctz);
374 updated_tree = true;
375 }
376 if (!new_usage_in_excess)
377 goto done;
378 mem_cgroup_insert_exceeded(mem, mz, mctz);
379
380done:
381 if (updated_tree) {
382 spin_lock(&mctz->lock);
383 mz->usage_in_excess = new_usage_in_excess;
384 spin_unlock(&mctz->lock);
385 }
386}
387
388static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
389{
390 int node, zone;
391 struct mem_cgroup_per_zone *mz;
392 struct mem_cgroup_tree_per_zone *mctz;
393
394 for_each_node_state(node, N_POSSIBLE) {
395 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
396 mz = mem_cgroup_zoneinfo(mem, node, zone);
397 mctz = soft_limit_tree_node_zone(node, zone);
398 mem_cgroup_remove_exceeded(mem, mz, mctz);
399 }
400 }
401}
402
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700403static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
404 struct page_cgroup *pc,
405 bool charge)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800406{
407 int val = (charge)? 1 : -1;
408 struct mem_cgroup_stat *stat = &mem->stat;
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700409 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800410 int cpu = get_cpu();
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800411
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800412 cpustat = &stat->cpustat[cpu];
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700413 if (PageCgroupCache(pc))
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700414 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800415 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700416 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700417
418 if (charge)
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700419 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700420 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
421 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700422 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700423 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700424 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_EVENTS, 1);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800425 put_cpu();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800426}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800427
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700428static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700429 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800430{
431 int nid, zid;
432 struct mem_cgroup_per_zone *mz;
433 u64 total = 0;
434
435 for_each_online_node(nid)
436 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
437 mz = mem_cgroup_zoneinfo(mem, nid, zid);
438 total += MEM_CGROUP_ZSTAT(mz, idx);
439 }
440 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800441}
442
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800443static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800444{
445 return container_of(cgroup_subsys_state(cont,
446 mem_cgroup_subsys_id), struct mem_cgroup,
447 css);
448}
449
Balbir Singhcf475ad2008-04-29 01:00:16 -0700450struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800451{
Balbir Singh31a78f22008-09-28 23:09:31 +0100452 /*
453 * mm_update_next_owner() may clear mm->owner to NULL
454 * if it races with swapoff, page migration, etc.
455 * So this can be called with p == NULL.
456 */
457 if (unlikely(!p))
458 return NULL;
459
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800460 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
461 struct mem_cgroup, css);
462}
463
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800464static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
465{
466 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700467
468 if (!mm)
469 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800470 /*
471 * Because we have no locks, mm->owner's may be being moved to other
472 * cgroup. We use css_tryget() here even if this looks
473 * pessimistic (rather than adding locks here).
474 */
475 rcu_read_lock();
476 do {
477 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
478 if (unlikely(!mem))
479 break;
480 } while (!css_tryget(&mem->css));
481 rcu_read_unlock();
482 return mem;
483}
484
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700485/*
486 * Call callback function against all cgroup under hierarchy tree.
487 */
488static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
489 int (*func)(struct mem_cgroup *, void *))
490{
491 int found, ret, nextid;
492 struct cgroup_subsys_state *css;
493 struct mem_cgroup *mem;
494
495 if (!root->use_hierarchy)
496 return (*func)(root, data);
497
498 nextid = 1;
499 do {
500 ret = 0;
501 mem = NULL;
502
503 rcu_read_lock();
504 css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
505 &found);
506 if (css && css_tryget(css))
507 mem = container_of(css, struct mem_cgroup, css);
508 rcu_read_unlock();
509
510 if (mem) {
511 ret = (*func)(mem, data);
512 css_put(&mem->css);
513 }
514 nextid = found + 1;
515 } while (!ret && css);
516
517 return ret;
518}
519
Balbir Singh4b3bde42009-09-23 15:56:32 -0700520static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
521{
522 return (mem == root_mem_cgroup);
523}
524
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800525/*
526 * Following LRU functions are allowed to be used without PCG_LOCK.
527 * Operations are called by routine of global LRU independently from memcg.
528 * What we have to take care of here is validness of pc->mem_cgroup.
529 *
530 * Changes to pc->mem_cgroup happens when
531 * 1. charge
532 * 2. moving account
533 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
534 * It is added to LRU before charge.
535 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
536 * When moving account, the page is not on LRU. It's isolated.
537 */
538
539void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800540{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800541 struct page_cgroup *pc;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800542 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700543
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800544 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800545 return;
546 pc = lookup_page_cgroup(page);
547 /* can happen while we handle swapcache. */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700548 if (!TestClearPageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800549 return;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700550 VM_BUG_ON(!pc->mem_cgroup);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800551 /*
552 * We don't check PCG_USED bit. It's cleared when the "page" is finally
553 * removed from global LRU.
554 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800555 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700556 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700557 if (mem_cgroup_is_root(pc->mem_cgroup))
558 return;
559 VM_BUG_ON(list_empty(&pc->lru));
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800560 list_del_init(&pc->lru);
561 return;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800562}
563
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800564void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800565{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800566 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800567}
568
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800569void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800570{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800571 struct mem_cgroup_per_zone *mz;
572 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800573
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800574 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700575 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700576
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800577 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800578 /*
579 * Used bit is set without atomic ops but after smp_wmb().
580 * For making pc->mem_cgroup visible, insert smp_rmb() here.
581 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800582 smp_rmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -0700583 /* unused or root page is not rotated. */
584 if (!PageCgroupUsed(pc) || mem_cgroup_is_root(pc->mem_cgroup))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800585 return;
586 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700587 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800588}
589
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800590void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
591{
592 struct page_cgroup *pc;
593 struct mem_cgroup_per_zone *mz;
594
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800595 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800596 return;
597 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700598 VM_BUG_ON(PageCgroupAcctLRU(pc));
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800599 /*
600 * Used bit is set without atomic ops but after smp_wmb().
601 * For making pc->mem_cgroup visible, insert smp_rmb() here.
602 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800603 smp_rmb();
604 if (!PageCgroupUsed(pc))
605 return;
606
607 mz = page_cgroup_zoneinfo(pc);
608 MEM_CGROUP_ZSTAT(mz, lru) += 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700609 SetPageCgroupAcctLRU(pc);
610 if (mem_cgroup_is_root(pc->mem_cgroup))
611 return;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800612 list_add(&pc->lru, &mz->lists[lru]);
613}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800614
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800615/*
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800616 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
617 * lru because the page may.be reused after it's fully uncharged (because of
618 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
619 * it again. This function is only used to charge SwapCache. It's done under
620 * lock_page and expected that zone->lru_lock is never held.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800621 */
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800622static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800623{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800624 unsigned long flags;
625 struct zone *zone = page_zone(page);
626 struct page_cgroup *pc = lookup_page_cgroup(page);
627
628 spin_lock_irqsave(&zone->lru_lock, flags);
629 /*
630 * Forget old LRU when this page_cgroup is *not* used. This Used bit
631 * is guarded by lock_page() because the page is SwapCache.
632 */
633 if (!PageCgroupUsed(pc))
634 mem_cgroup_del_lru_list(page, page_lru(page));
635 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800636}
637
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800638static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
639{
640 unsigned long flags;
641 struct zone *zone = page_zone(page);
642 struct page_cgroup *pc = lookup_page_cgroup(page);
643
644 spin_lock_irqsave(&zone->lru_lock, flags);
645 /* link when the page is linked to LRU but page_cgroup isn't */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700646 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800647 mem_cgroup_add_lru_list(page, page_lru(page));
648 spin_unlock_irqrestore(&zone->lru_lock, flags);
649}
650
651
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800652void mem_cgroup_move_lists(struct page *page,
653 enum lru_list from, enum lru_list to)
654{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800655 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800656 return;
657 mem_cgroup_del_lru_list(page, from);
658 mem_cgroup_add_lru_list(page, to);
659}
660
David Rientjes4c4a2212008-02-07 00:14:06 -0800661int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
662{
663 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700664 struct mem_cgroup *curr = NULL;
David Rientjes4c4a2212008-02-07 00:14:06 -0800665
666 task_lock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700667 rcu_read_lock();
668 curr = try_get_mem_cgroup_from_mm(task->mm);
669 rcu_read_unlock();
David Rientjes4c4a2212008-02-07 00:14:06 -0800670 task_unlock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700671 if (!curr)
672 return 0;
673 if (curr->use_hierarchy)
674 ret = css_is_ancestor(&curr->css, &mem->css);
675 else
676 ret = (curr == mem);
677 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -0800678 return ret;
679}
680
Balbir Singh66e17072008-02-07 00:13:56 -0800681/*
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800682 * prev_priority control...this will be used in memory reclaim path.
683 */
684int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
685{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800686 int prev_priority;
687
688 spin_lock(&mem->reclaim_param_lock);
689 prev_priority = mem->prev_priority;
690 spin_unlock(&mem->reclaim_param_lock);
691
692 return prev_priority;
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800693}
694
695void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
696{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800697 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800698 if (priority < mem->prev_priority)
699 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800700 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800701}
702
703void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
704{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800705 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800706 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800707 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800708}
709
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800710static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800711{
712 unsigned long active;
713 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800714 unsigned long gb;
715 unsigned long inactive_ratio;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800716
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700717 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
718 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800719
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800720 gb = (inactive + active) >> (30 - PAGE_SHIFT);
721 if (gb)
722 inactive_ratio = int_sqrt(10 * gb);
723 else
724 inactive_ratio = 1;
725
726 if (present_pages) {
727 present_pages[0] = inactive;
728 present_pages[1] = active;
729 }
730
731 return inactive_ratio;
732}
733
734int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
735{
736 unsigned long active;
737 unsigned long inactive;
738 unsigned long present_pages[2];
739 unsigned long inactive_ratio;
740
741 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
742
743 inactive = present_pages[0];
744 active = present_pages[1];
745
746 if (inactive * inactive_ratio < active)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800747 return 1;
748
749 return 0;
750}
751
Rik van Riel56e49d22009-06-16 15:32:28 -0700752int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
753{
754 unsigned long active;
755 unsigned long inactive;
756
757 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
758 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
759
760 return (active > inactive);
761}
762
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800763unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
764 struct zone *zone,
765 enum lru_list lru)
766{
767 int nid = zone->zone_pgdat->node_id;
768 int zid = zone_idx(zone);
769 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
770
771 return MEM_CGROUP_ZSTAT(mz, lru);
772}
773
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800774struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
775 struct zone *zone)
776{
777 int nid = zone->zone_pgdat->node_id;
778 int zid = zone_idx(zone);
779 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
780
781 return &mz->reclaim_stat;
782}
783
784struct zone_reclaim_stat *
785mem_cgroup_get_reclaim_stat_from_page(struct page *page)
786{
787 struct page_cgroup *pc;
788 struct mem_cgroup_per_zone *mz;
789
790 if (mem_cgroup_disabled())
791 return NULL;
792
793 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800794 /*
795 * Used bit is set without atomic ops but after smp_wmb().
796 * For making pc->mem_cgroup visible, insert smp_rmb() here.
797 */
798 smp_rmb();
799 if (!PageCgroupUsed(pc))
800 return NULL;
801
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800802 mz = page_cgroup_zoneinfo(pc);
803 if (!mz)
804 return NULL;
805
806 return &mz->reclaim_stat;
807}
808
Balbir Singh66e17072008-02-07 00:13:56 -0800809unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
810 struct list_head *dst,
811 unsigned long *scanned, int order,
812 int mode, struct zone *z,
813 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700814 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -0800815{
816 unsigned long nr_taken = 0;
817 struct page *page;
818 unsigned long scan;
819 LIST_HEAD(pc_list);
820 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800821 struct page_cgroup *pc, *tmp;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800822 int nid = z->zone_pgdat->node_id;
823 int zid = zone_idx(z);
824 struct mem_cgroup_per_zone *mz;
Johannes Weinerb7c46d12009-09-21 17:02:56 -0700825 int lru = LRU_FILE * file + active;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700826 int ret;
Balbir Singh66e17072008-02-07 00:13:56 -0800827
Balbir Singhcf475ad2008-04-29 01:00:16 -0700828 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800829 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700830 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -0800831
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800832 scan = 0;
833 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -0800834 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800835 break;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800836
837 page = pc->page;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700838 if (unlikely(!PageCgroupUsed(pc)))
839 continue;
Hugh Dickins436c65412008-02-07 00:14:12 -0800840 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800841 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800842
Hugh Dickins436c65412008-02-07 00:14:12 -0800843 scan++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700844 ret = __isolate_lru_page(page, mode, file);
845 switch (ret) {
846 case 0:
Balbir Singh66e17072008-02-07 00:13:56 -0800847 list_move(&page->lru, dst);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700848 mem_cgroup_del_lru(page);
Balbir Singh66e17072008-02-07 00:13:56 -0800849 nr_taken++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700850 break;
851 case -EBUSY:
852 /* we don't affect global LRU but rotate in our LRU */
853 mem_cgroup_rotate_lru_list(page, page_lru(page));
854 break;
855 default:
856 break;
Balbir Singh66e17072008-02-07 00:13:56 -0800857 }
858 }
859
Balbir Singh66e17072008-02-07 00:13:56 -0800860 *scanned = scan;
861 return nr_taken;
862}
863
Balbir Singh6d61ef42009-01-07 18:08:06 -0800864#define mem_cgroup_from_res_counter(counter, member) \
865 container_of(counter, struct mem_cgroup, member)
866
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -0800867static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
868{
869 if (do_swap_account) {
870 if (res_counter_check_under_limit(&mem->res) &&
871 res_counter_check_under_limit(&mem->memsw))
872 return true;
873 } else
874 if (res_counter_check_under_limit(&mem->res))
875 return true;
876 return false;
877}
878
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800879static unsigned int get_swappiness(struct mem_cgroup *memcg)
880{
881 struct cgroup *cgrp = memcg->css.cgroup;
882 unsigned int swappiness;
883
884 /* root ? */
885 if (cgrp->parent == NULL)
886 return vm_swappiness;
887
888 spin_lock(&memcg->reclaim_param_lock);
889 swappiness = memcg->swappiness;
890 spin_unlock(&memcg->reclaim_param_lock);
891
892 return swappiness;
893}
894
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700895static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
896{
897 int *val = data;
898 (*val)++;
899 return 0;
900}
Balbir Singhe2224322009-04-02 16:57:39 -0700901
902/**
903 * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode.
904 * @memcg: The memory cgroup that went over limit
905 * @p: Task that is going to be killed
906 *
907 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
908 * enabled
909 */
910void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
911{
912 struct cgroup *task_cgrp;
913 struct cgroup *mem_cgrp;
914 /*
915 * Need a buffer in BSS, can't rely on allocations. The code relies
916 * on the assumption that OOM is serialized for memory controller.
917 * If this assumption is broken, revisit this code.
918 */
919 static char memcg_name[PATH_MAX];
920 int ret;
921
922 if (!memcg)
923 return;
924
925
926 rcu_read_lock();
927
928 mem_cgrp = memcg->css.cgroup;
929 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
930
931 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
932 if (ret < 0) {
933 /*
934 * Unfortunately, we are unable to convert to a useful name
935 * But we'll still print out the usage information
936 */
937 rcu_read_unlock();
938 goto done;
939 }
940 rcu_read_unlock();
941
942 printk(KERN_INFO "Task in %s killed", memcg_name);
943
944 rcu_read_lock();
945 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
946 if (ret < 0) {
947 rcu_read_unlock();
948 goto done;
949 }
950 rcu_read_unlock();
951
952 /*
953 * Continues from above, so we don't need an KERN_ level
954 */
955 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
956done:
957
958 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
959 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
960 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
961 res_counter_read_u64(&memcg->res, RES_FAILCNT));
962 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
963 "failcnt %llu\n",
964 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
965 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
966 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
967}
968
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700969/*
970 * This function returns the number of memcg under hierarchy tree. Returns
971 * 1(self count) if no children.
972 */
973static int mem_cgroup_count_children(struct mem_cgroup *mem)
974{
975 int num = 0;
976 mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
977 return num;
978}
979
Balbir Singh6d61ef42009-01-07 18:08:06 -0800980/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700981 * Visit the first child (need not be the first child as per the ordering
982 * of the cgroup list, since we track last_scanned_child) of @mem and use
983 * that to reclaim free pages from.
984 */
985static struct mem_cgroup *
986mem_cgroup_select_victim(struct mem_cgroup *root_mem)
987{
988 struct mem_cgroup *ret = NULL;
989 struct cgroup_subsys_state *css;
990 int nextid, found;
991
992 if (!root_mem->use_hierarchy) {
993 css_get(&root_mem->css);
994 ret = root_mem;
995 }
996
997 while (!ret) {
998 rcu_read_lock();
999 nextid = root_mem->last_scanned_child + 1;
1000 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
1001 &found);
1002 if (css && css_tryget(css))
1003 ret = container_of(css, struct mem_cgroup, css);
1004
1005 rcu_read_unlock();
1006 /* Updates scanning parameter */
1007 spin_lock(&root_mem->reclaim_param_lock);
1008 if (!css) {
1009 /* this means start scan from ID:1 */
1010 root_mem->last_scanned_child = 0;
1011 } else
1012 root_mem->last_scanned_child = found;
1013 spin_unlock(&root_mem->reclaim_param_lock);
1014 }
1015
1016 return ret;
1017}
1018
1019/*
1020 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1021 * we reclaimed from, so that we don't end up penalizing one child extensively
1022 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001023 *
1024 * root_mem is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001025 *
1026 * We give up and return to the caller when we visit root_mem twice.
1027 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001028 *
1029 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001030 */
1031static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001032 gfp_t gfp_mask, bool noswap, bool shrink)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001033{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001034 struct mem_cgroup *victim;
1035 int ret, total = 0;
1036 int loop = 0;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001037
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001038 /* If memsw_is_minimum==1, swap-out is of-no-use. */
1039 if (root_mem->memsw_is_minimum)
1040 noswap = true;
1041
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001042 while (loop < 2) {
1043 victim = mem_cgroup_select_victim(root_mem);
1044 if (victim == root_mem)
1045 loop++;
1046 if (!mem_cgroup_local_usage(&victim->stat)) {
1047 /* this cgroup's local usage == 0 */
1048 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001049 continue;
1050 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001051 /* we use swappiness of local cgroup */
1052 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask, noswap,
1053 get_swappiness(victim));
1054 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001055 /*
1056 * At shrinking usage, we can't check we should stop here or
1057 * reclaim more. It's depends on callers. last_scanned_child
1058 * will work enough for keeping fairness under tree.
1059 */
1060 if (shrink)
1061 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001062 total += ret;
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -08001063 if (mem_cgroup_check_under_limit(root_mem))
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001064 return 1 + total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001065 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001066 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001067}
1068
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001069bool mem_cgroup_oom_called(struct task_struct *task)
1070{
1071 bool ret = false;
1072 struct mem_cgroup *mem;
1073 struct mm_struct *mm;
1074
1075 rcu_read_lock();
1076 mm = task->mm;
1077 if (!mm)
1078 mm = &init_mm;
1079 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
1080 if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
1081 ret = true;
1082 rcu_read_unlock();
1083 return ret;
1084}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001085
1086static int record_last_oom_cb(struct mem_cgroup *mem, void *data)
1087{
1088 mem->last_oom_jiffies = jiffies;
1089 return 0;
1090}
1091
1092static void record_last_oom(struct mem_cgroup *mem)
1093{
1094 mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb);
1095}
1096
Balbir Singhd69b0422009-06-17 16:26:34 -07001097/*
1098 * Currently used to update mapped file statistics, but the routine can be
1099 * generalized to update other statistics as well.
1100 */
1101void mem_cgroup_update_mapped_file_stat(struct page *page, int val)
1102{
1103 struct mem_cgroup *mem;
1104 struct mem_cgroup_stat *stat;
1105 struct mem_cgroup_stat_cpu *cpustat;
1106 int cpu;
1107 struct page_cgroup *pc;
1108
1109 if (!page_is_file_cache(page))
1110 return;
1111
1112 pc = lookup_page_cgroup(page);
1113 if (unlikely(!pc))
1114 return;
1115
1116 lock_page_cgroup(pc);
1117 mem = pc->mem_cgroup;
1118 if (!mem)
1119 goto done;
1120
1121 if (!PageCgroupUsed(pc))
1122 goto done;
1123
1124 /*
1125 * Preemption is already disabled, we don't need get_cpu()
1126 */
1127 cpu = smp_processor_id();
1128 stat = &mem->stat;
1129 cpustat = &stat->cpustat[cpu];
1130
1131 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE, val);
1132done:
1133 unlock_page_cgroup(pc);
1134}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001135
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001136/*
1137 * Unlike exported interface, "oom" parameter is added. if oom==true,
1138 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001139 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001140static int __mem_cgroup_try_charge(struct mm_struct *mm,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001141 gfp_t gfp_mask, struct mem_cgroup **memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -07001142 bool oom, struct page *page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001143{
Balbir Singhf64c3f52009-09-23 15:56:37 -07001144 struct mem_cgroup *mem, *mem_over_limit, *mem_over_soft_limit;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001145 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001146 struct res_counter *fail_res, *soft_fail_res = NULL;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001147
1148 if (unlikely(test_thread_flag(TIF_MEMDIE))) {
1149 /* Don't account this! */
1150 *memcg = NULL;
1151 return 0;
1152 }
1153
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001154 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08001155 * We always charge the cgroup the mm_struct belongs to.
1156 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001157 * thread group leader migrates. It's possible that mm is not
1158 * set, if so charge the init_mm (happens for pagecache usage).
1159 */
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001160 mem = *memcg;
1161 if (likely(!mem)) {
1162 mem = try_get_mem_cgroup_from_mm(mm);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001163 *memcg = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001164 } else {
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001165 css_get(&mem->css);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001166 }
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001167 if (unlikely(!mem))
1168 return 0;
1169
Nikanth Karthikesan46f7e602009-05-28 14:34:41 -07001170 VM_BUG_ON(css_is_removed(&mem->css));
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001171
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001172 while (1) {
1173 int ret;
1174 bool noswap = false;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001175
Balbir Singhf64c3f52009-09-23 15:56:37 -07001176 ret = res_counter_charge(&mem->res, PAGE_SIZE, &fail_res,
1177 &soft_fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001178 if (likely(!ret)) {
1179 if (!do_swap_account)
1180 break;
Balbir Singh28dbc4b2009-01-07 18:08:05 -08001181 ret = res_counter_charge(&mem->memsw, PAGE_SIZE,
Balbir Singhf64c3f52009-09-23 15:56:37 -07001182 &fail_res, NULL);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001183 if (likely(!ret))
1184 break;
1185 /* mem+swap counter fails */
Balbir Singhf64c3f52009-09-23 15:56:37 -07001186 res_counter_uncharge(&mem->res, PAGE_SIZE, NULL);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001187 noswap = true;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001188 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1189 memsw);
1190 } else
1191 /* mem counter fails */
1192 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1193 res);
1194
Hugh Dickins3be91272008-02-07 00:14:19 -08001195 if (!(gfp_mask & __GFP_WAIT))
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001196 goto nomem;
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001197
Balbir Singh6d61ef42009-01-07 18:08:06 -08001198 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, gfp_mask,
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001199 noswap, false);
Daisuke Nishimura4d1c6272009-01-15 13:51:14 -08001200 if (ret)
1201 continue;
Balbir Singh66e17072008-02-07 00:13:56 -08001202
1203 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001204 * try_to_free_mem_cgroup_pages() might not give us a full
1205 * picture of reclaim. Some pages are reclaimed and might be
1206 * moved to swap cache or just unmapped from the cgroup.
1207 * Check the limit again to see if the reclaim reduced the
1208 * current usage of the cgroup before giving up
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001209 *
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001210 */
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -08001211 if (mem_cgroup_check_under_limit(mem_over_limit))
1212 continue;
Hugh Dickins3be91272008-02-07 00:14:19 -08001213
1214 if (!nr_retries--) {
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001215 if (oom) {
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08001216 mutex_lock(&memcg_tasklist);
KAMEZAWA Hiroyuki88700752009-01-07 18:08:09 -08001217 mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08001218 mutex_unlock(&memcg_tasklist);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001219 record_last_oom(mem_over_limit);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001220 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001221 goto nomem;
Balbir Singh66e17072008-02-07 00:13:56 -08001222 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001223 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07001224 /*
1225 * Insert just the ancestor, we should trickle down to the correct
1226 * cgroup for reclaim, since the other nodes will be below their
1227 * soft limit
1228 */
1229 if (soft_fail_res) {
1230 mem_over_soft_limit =
1231 mem_cgroup_from_res_counter(soft_fail_res, res);
1232 if (mem_cgroup_soft_limit_check(mem_over_soft_limit))
1233 mem_cgroup_update_tree(mem_over_soft_limit, page);
1234 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001235 return 0;
1236nomem:
1237 css_put(&mem->css);
1238 return -ENOMEM;
1239}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001240
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001241/*
1242 * A helper function to get mem_cgroup from ID. must be called under
1243 * rcu_read_lock(). The caller must check css_is_removed() or some if
1244 * it's concern. (dropping refcnt from swap can be called against removed
1245 * memcg.)
1246 */
1247static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
1248{
1249 struct cgroup_subsys_state *css;
1250
1251 /* ID 0 is unused ID */
1252 if (!id)
1253 return NULL;
1254 css = css_lookup(&mem_cgroup_subsys, id);
1255 if (!css)
1256 return NULL;
1257 return container_of(css, struct mem_cgroup, css);
1258}
1259
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001260static struct mem_cgroup *try_get_mem_cgroup_from_swapcache(struct page *page)
1261{
1262 struct mem_cgroup *mem;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001263 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001264 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001265 swp_entry_t ent;
1266
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001267 VM_BUG_ON(!PageLocked(page));
1268
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001269 if (!PageSwapCache(page))
1270 return NULL;
1271
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001272 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001273 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001274 if (PageCgroupUsed(pc)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001275 mem = pc->mem_cgroup;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001276 if (mem && !css_tryget(&mem->css))
1277 mem = NULL;
1278 } else {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001279 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001280 id = lookup_swap_cgroup(ent);
1281 rcu_read_lock();
1282 mem = mem_cgroup_lookup(id);
1283 if (mem && !css_tryget(&mem->css))
1284 mem = NULL;
1285 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001286 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001287 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001288 return mem;
1289}
1290
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001291/*
Daisuke Nishimuraa5e924f2009-01-07 18:08:28 -08001292 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001293 * USED state. If already USED, uncharge and return.
1294 */
1295
1296static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1297 struct page_cgroup *pc,
1298 enum charge_type ctype)
1299{
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001300 /* try_charge() can return NULL to *memcg, taking care of it. */
1301 if (!mem)
1302 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001303
1304 lock_page_cgroup(pc);
1305 if (unlikely(PageCgroupUsed(pc))) {
1306 unlock_page_cgroup(pc);
Balbir Singhf64c3f52009-09-23 15:56:37 -07001307 res_counter_uncharge(&mem->res, PAGE_SIZE, NULL);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001308 if (do_swap_account)
Balbir Singhf64c3f52009-09-23 15:56:37 -07001309 res_counter_uncharge(&mem->memsw, PAGE_SIZE, NULL);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001310 css_put(&mem->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001311 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001312 }
Balbir Singh4b3bde42009-09-23 15:56:32 -07001313
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001314 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07001315 /*
1316 * We access a page_cgroup asynchronously without lock_page_cgroup().
1317 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
1318 * is accessed after testing USED bit. To make pc->mem_cgroup visible
1319 * before USED bit, we need memory barrier here.
1320 * See mem_cgroup_add_lru_list(), etc.
1321 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001322 smp_wmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -07001323 switch (ctype) {
1324 case MEM_CGROUP_CHARGE_TYPE_CACHE:
1325 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
1326 SetPageCgroupCache(pc);
1327 SetPageCgroupUsed(pc);
1328 break;
1329 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1330 ClearPageCgroupCache(pc);
1331 SetPageCgroupUsed(pc);
1332 break;
1333 default:
1334 break;
1335 }
Hugh Dickins3be91272008-02-07 00:14:19 -08001336
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001337 mem_cgroup_charge_statistics(mem, pc, true);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001338
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001339 unlock_page_cgroup(pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001340}
1341
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001342/**
1343 * mem_cgroup_move_account - move account of the page
1344 * @pc: page_cgroup of the page.
1345 * @from: mem_cgroup which the page is moved from.
1346 * @to: mem_cgroup which the page is moved to. @from != @to.
1347 *
1348 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001349 * - page is not on LRU (isolate_page() is useful.)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001350 *
1351 * returns 0 at success,
1352 * returns -EBUSY when lock is busy or "pc" is unstable.
1353 *
1354 * This function does "uncharge" from old cgroup but doesn't do "charge" to
1355 * new cgroup. It should be done by a caller.
1356 */
1357
1358static int mem_cgroup_move_account(struct page_cgroup *pc,
1359 struct mem_cgroup *from, struct mem_cgroup *to)
1360{
1361 struct mem_cgroup_per_zone *from_mz, *to_mz;
1362 int nid, zid;
1363 int ret = -EBUSY;
Balbir Singhd69b0422009-06-17 16:26:34 -07001364 struct page *page;
1365 int cpu;
1366 struct mem_cgroup_stat *stat;
1367 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001368
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001369 VM_BUG_ON(from == to);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001370 VM_BUG_ON(PageLRU(pc->page));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001371
1372 nid = page_cgroup_nid(pc);
1373 zid = page_cgroup_zid(pc);
1374 from_mz = mem_cgroup_zoneinfo(from, nid, zid);
1375 to_mz = mem_cgroup_zoneinfo(to, nid, zid);
1376
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001377 if (!trylock_page_cgroup(pc))
1378 return ret;
1379
1380 if (!PageCgroupUsed(pc))
1381 goto out;
1382
1383 if (pc->mem_cgroup != from)
1384 goto out;
1385
Balbir Singhf64c3f52009-09-23 15:56:37 -07001386 res_counter_uncharge(&from->res, PAGE_SIZE, NULL);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001387 mem_cgroup_charge_statistics(from, pc, false);
Balbir Singhd69b0422009-06-17 16:26:34 -07001388
1389 page = pc->page;
1390 if (page_is_file_cache(page) && page_mapped(page)) {
1391 cpu = smp_processor_id();
1392 /* Update mapped_file data for mem_cgroup "from" */
1393 stat = &from->stat;
1394 cpustat = &stat->cpustat[cpu];
1395 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1396 -1);
1397
1398 /* Update mapped_file data for mem_cgroup "to" */
1399 stat = &to->stat;
1400 cpustat = &stat->cpustat[cpu];
1401 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1402 1);
1403 }
1404
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001405 if (do_swap_account)
Balbir Singhf64c3f52009-09-23 15:56:37 -07001406 res_counter_uncharge(&from->memsw, PAGE_SIZE, NULL);
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001407 css_put(&from->css);
1408
1409 css_get(&to->css);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001410 pc->mem_cgroup = to;
1411 mem_cgroup_charge_statistics(to, pc, true);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001412 ret = 0;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001413out:
1414 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001415 /*
1416 * We charges against "to" which may not have any tasks. Then, "to"
1417 * can be under rmdir(). But in current implementation, caller of
1418 * this function is just force_empty() and it's garanteed that
1419 * "to" is never removed. So, we don't check rmdir status here.
1420 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001421 return ret;
1422}
1423
1424/*
1425 * move charges to its parent.
1426 */
1427
1428static int mem_cgroup_move_parent(struct page_cgroup *pc,
1429 struct mem_cgroup *child,
1430 gfp_t gfp_mask)
1431{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001432 struct page *page = pc->page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001433 struct cgroup *cg = child->css.cgroup;
1434 struct cgroup *pcg = cg->parent;
1435 struct mem_cgroup *parent;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001436 int ret;
1437
1438 /* Is ROOT ? */
1439 if (!pcg)
1440 return -EINVAL;
1441
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001442
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001443 parent = mem_cgroup_from_cont(pcg);
1444
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001445
Balbir Singhf64c3f52009-09-23 15:56:37 -07001446 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false, page);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001447 if (ret || !parent)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001448 return ret;
1449
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001450 if (!get_page_unless_zero(page)) {
1451 ret = -EBUSY;
1452 goto uncharge;
1453 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001454
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001455 ret = isolate_lru_page(page);
1456
1457 if (ret)
1458 goto cancel;
1459
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001460 ret = mem_cgroup_move_account(pc, child, parent);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001461
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001462 putback_lru_page(page);
1463 if (!ret) {
1464 put_page(page);
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001465 /* drop extra refcnt by try_charge() */
1466 css_put(&parent->css);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001467 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001468 }
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001469
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001470cancel:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001471 put_page(page);
1472uncharge:
1473 /* drop extra refcnt by try_charge() */
1474 css_put(&parent->css);
1475 /* uncharge if move fails */
Balbir Singhf64c3f52009-09-23 15:56:37 -07001476 res_counter_uncharge(&parent->res, PAGE_SIZE, NULL);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001477 if (do_swap_account)
Balbir Singhf64c3f52009-09-23 15:56:37 -07001478 res_counter_uncharge(&parent->memsw, PAGE_SIZE, NULL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001479 return ret;
1480}
1481
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001482/*
1483 * Charge the memory controller for page usage.
1484 * Return
1485 * 0 if the charge was successful
1486 * < 0 if the cgroup is over its limit
1487 */
1488static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
1489 gfp_t gfp_mask, enum charge_type ctype,
1490 struct mem_cgroup *memcg)
1491{
1492 struct mem_cgroup *mem;
1493 struct page_cgroup *pc;
1494 int ret;
1495
1496 pc = lookup_page_cgroup(page);
1497 /* can happen at boot */
1498 if (unlikely(!pc))
1499 return 0;
1500 prefetchw(pc);
1501
1502 mem = memcg;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001503 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true, page);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001504 if (ret || !mem)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001505 return ret;
1506
1507 __mem_cgroup_commit_charge(mem, pc, ctype);
1508 return 0;
1509}
1510
1511int mem_cgroup_newpage_charge(struct page *page,
1512 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001513{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001514 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001515 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001516 if (PageCompound(page))
1517 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001518 /*
1519 * If already mapped, we don't have to account.
1520 * If page cache, page->mapping has address_space.
1521 * But page->mapping may have out-of-use anon_vma pointer,
1522 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
1523 * is NULL.
1524 */
1525 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
1526 return 0;
1527 if (unlikely(!mm))
1528 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001529 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001530 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001531}
1532
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001533static void
1534__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1535 enum charge_type ctype);
1536
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001537int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
1538 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08001539{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001540 struct mem_cgroup *mem = NULL;
1541 int ret;
1542
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001543 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001544 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001545 if (PageCompound(page))
1546 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001547 /*
1548 * Corner case handling. This is called from add_to_page_cache()
1549 * in usual. But some FS (shmem) precharges this page before calling it
1550 * and call add_to_page_cache() with GFP_NOWAIT.
1551 *
1552 * For GFP_NOWAIT case, the page may be pre-charged before calling
1553 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
1554 * charge twice. (It works but has to pay a bit larger cost.)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001555 * And when the page is SwapCache, it should take swap information
1556 * into account. This is under lock_page() now.
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001557 */
1558 if (!(gfp_mask & __GFP_WAIT)) {
1559 struct page_cgroup *pc;
1560
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001561
1562 pc = lookup_page_cgroup(page);
1563 if (!pc)
1564 return 0;
1565 lock_page_cgroup(pc);
1566 if (PageCgroupUsed(pc)) {
1567 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001568 return 0;
1569 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001570 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001571 }
1572
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001573 if (unlikely(!mm && !mem))
Balbir Singh8697d332008-02-07 00:13:59 -08001574 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001575
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -07001576 if (page_is_file_cache(page))
1577 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001578 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001579
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001580 /* shmem */
1581 if (PageSwapCache(page)) {
1582 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1583 if (!ret)
1584 __mem_cgroup_commit_charge_swapin(page, mem,
1585 MEM_CGROUP_CHARGE_TYPE_SHMEM);
1586 } else
1587 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
1588 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001589
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001590 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001591}
1592
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001593/*
1594 * While swap-in, try_charge -> commit or cancel, the page is locked.
1595 * And when try_charge() successfully returns, one refcnt to memcg without
1596 * struct page_cgroup is aquired. This refcnt will be cumsumed by
1597 * "commit()" or removed by "cancel()"
1598 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001599int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
1600 struct page *page,
1601 gfp_t mask, struct mem_cgroup **ptr)
1602{
1603 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001604 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001605
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001606 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001607 return 0;
1608
1609 if (!do_swap_account)
1610 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001611 /*
1612 * A racing thread's fault, or swapoff, may have already updated
1613 * the pte, and even removed page from swap cache: return success
1614 * to go on to do_swap_page()'s pte_same() test, which should fail.
1615 */
1616 if (!PageSwapCache(page))
1617 return 0;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001618 mem = try_get_mem_cgroup_from_swapcache(page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001619 if (!mem)
1620 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001621 *ptr = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001622 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true, page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001623 /* drop extra refcnt from tryget */
1624 css_put(&mem->css);
1625 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001626charge_cur_mm:
1627 if (unlikely(!mm))
1628 mm = &init_mm;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001629 return __mem_cgroup_try_charge(mm, mask, ptr, true, page);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001630}
1631
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001632static void
1633__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1634 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001635{
1636 struct page_cgroup *pc;
1637
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001638 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001639 return;
1640 if (!ptr)
1641 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001642 cgroup_exclude_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001643 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001644 mem_cgroup_lru_del_before_commit_swapcache(page);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001645 __mem_cgroup_commit_charge(ptr, pc, ctype);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001646 mem_cgroup_lru_add_after_commit_swapcache(page);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001647 /*
1648 * Now swap is on-memory. This means this page may be
1649 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08001650 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
1651 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
1652 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001653 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08001654 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001655 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001656 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001657 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001658
1659 id = swap_cgroup_record(ent, 0);
1660 rcu_read_lock();
1661 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001662 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001663 /*
1664 * This recorded memcg can be obsolete one. So, avoid
1665 * calling css_tryget
1666 */
Balbir Singhf64c3f52009-09-23 15:56:37 -07001667 res_counter_uncharge(&memcg->memsw, PAGE_SIZE, NULL);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001668 mem_cgroup_put(memcg);
1669 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001670 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001671 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001672 /*
1673 * At swapin, we may charge account against cgroup which has no tasks.
1674 * So, rmdir()->pre_destroy() can be called while we do this charge.
1675 * In that case, we need to call pre_destroy() again. check it here.
1676 */
1677 cgroup_release_and_wakeup_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001678}
1679
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001680void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
1681{
1682 __mem_cgroup_commit_charge_swapin(page, ptr,
1683 MEM_CGROUP_CHARGE_TYPE_MAPPED);
1684}
1685
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001686void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
1687{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001688 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001689 return;
1690 if (!mem)
1691 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001692 res_counter_uncharge(&mem->res, PAGE_SIZE, NULL);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001693 if (do_swap_account)
Balbir Singhf64c3f52009-09-23 15:56:37 -07001694 res_counter_uncharge(&mem->memsw, PAGE_SIZE, NULL);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001695 css_put(&mem->css);
1696}
1697
1698
Balbir Singh8697d332008-02-07 00:13:59 -08001699/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001700 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001701 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001702static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001703__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001704{
Hugh Dickins82895462008-03-04 14:29:08 -08001705 struct page_cgroup *pc;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001706 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001707 struct mem_cgroup_per_zone *mz;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001708 bool soft_limit_excess = false;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001709
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001710 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001711 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07001712
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001713 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001714 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001715
Balbir Singh8697d332008-02-07 00:13:59 -08001716 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08001717 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08001718 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001719 pc = lookup_page_cgroup(page);
1720 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001721 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001722
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001723 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001724
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001725 mem = pc->mem_cgroup;
1726
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001727 if (!PageCgroupUsed(pc))
1728 goto unlock_out;
1729
1730 switch (ctype) {
1731 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07001732 case MEM_CGROUP_CHARGE_TYPE_DROP:
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001733 if (page_mapped(page))
1734 goto unlock_out;
1735 break;
1736 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
1737 if (!PageAnon(page)) { /* Shared memory */
1738 if (page->mapping && !page_is_file_cache(page))
1739 goto unlock_out;
1740 } else if (page_mapped(page)) /* Anon */
1741 goto unlock_out;
1742 break;
1743 default:
1744 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001745 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001746
Balbir Singhf64c3f52009-09-23 15:56:37 -07001747 res_counter_uncharge(&mem->res, PAGE_SIZE, &soft_limit_excess);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001748 if (do_swap_account && (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT))
Balbir Singhf64c3f52009-09-23 15:56:37 -07001749 res_counter_uncharge(&mem->memsw, PAGE_SIZE, NULL);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001750 mem_cgroup_charge_statistics(mem, pc, false);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001751
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001752 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001753 /*
1754 * pc->mem_cgroup is not cleared here. It will be accessed when it's
1755 * freed from LRU. This is safe because uncharged page is expected not
1756 * to be reused (freed soon). Exception is SwapCache, it's handled by
1757 * special functions.
1758 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08001759
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001760 mz = page_cgroup_zoneinfo(pc);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001761 unlock_page_cgroup(pc);
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08001762
Balbir Singhf64c3f52009-09-23 15:56:37 -07001763 if (soft_limit_excess && mem_cgroup_soft_limit_check(mem))
1764 mem_cgroup_update_tree(mem, page);
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08001765 /* at swapout, this memcg will be accessed to record to swap */
1766 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1767 css_put(&mem->css);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001768
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001769 return mem;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001770
1771unlock_out:
1772 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001773 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08001774}
1775
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001776void mem_cgroup_uncharge_page(struct page *page)
1777{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001778 /* early check. */
1779 if (page_mapped(page))
1780 return;
1781 if (page->mapping && !PageAnon(page))
1782 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001783 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
1784}
1785
1786void mem_cgroup_uncharge_cache_page(struct page *page)
1787{
1788 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07001789 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001790 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
1791}
1792
Daisuke Nishimurae767e052009-05-28 14:34:28 -07001793#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001794/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07001795 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001796 * memcg information is recorded to swap_cgroup of "ent"
1797 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07001798void
1799mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001800{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001801 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07001802 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001803
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07001804 if (!swapout) /* this was a swap cache but the swap is unused ! */
1805 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
1806
1807 memcg = __mem_cgroup_uncharge_common(page, ctype);
1808
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001809 /* record memcg information */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07001810 if (do_swap_account && swapout && memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001811 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001812 mem_cgroup_get(memcg);
1813 }
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07001814 if (swapout && memcg)
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08001815 css_put(&memcg->css);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001816}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07001817#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001818
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001819#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1820/*
1821 * called from swap_entry_free(). remove record in swap_cgroup and
1822 * uncharge "memsw" account.
1823 */
1824void mem_cgroup_uncharge_swap(swp_entry_t ent)
1825{
1826 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001827 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001828
1829 if (!do_swap_account)
1830 return;
1831
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001832 id = swap_cgroup_record(ent, 0);
1833 rcu_read_lock();
1834 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001835 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001836 /*
1837 * We uncharge this because swap is freed.
1838 * This memcg can be obsolete one. We avoid calling css_tryget
1839 */
Balbir Singhf64c3f52009-09-23 15:56:37 -07001840 res_counter_uncharge(&memcg->memsw, PAGE_SIZE, NULL);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001841 mem_cgroup_put(memcg);
1842 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001843 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001844}
1845#endif
1846
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001847/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001848 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
1849 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001850 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001851int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001852{
1853 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001854 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001855 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001856
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001857 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07001858 return 0;
1859
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001860 pc = lookup_page_cgroup(page);
1861 lock_page_cgroup(pc);
1862 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001863 mem = pc->mem_cgroup;
1864 css_get(&mem->css);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08001865 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001866 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001867
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001868 if (mem) {
Balbir Singhf64c3f52009-09-23 15:56:37 -07001869 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false,
1870 page);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001871 css_put(&mem->css);
1872 }
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001873 *ptr = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001874 return ret;
1875}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08001876
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001877/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001878void mem_cgroup_end_migration(struct mem_cgroup *mem,
1879 struct page *oldpage, struct page *newpage)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001880{
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001881 struct page *target, *unused;
1882 struct page_cgroup *pc;
1883 enum charge_type ctype;
1884
1885 if (!mem)
1886 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001887 cgroup_exclude_rmdir(&mem->css);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001888 /* at migration success, oldpage->mapping is NULL. */
1889 if (oldpage->mapping) {
1890 target = oldpage;
1891 unused = NULL;
1892 } else {
1893 target = newpage;
1894 unused = oldpage;
1895 }
1896
1897 if (PageAnon(target))
1898 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
1899 else if (page_is_file_cache(target))
1900 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
1901 else
1902 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
1903
1904 /* unused page is not on radix-tree now. */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001905 if (unused)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001906 __mem_cgroup_uncharge_common(unused, ctype);
1907
1908 pc = lookup_page_cgroup(target);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001909 /*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001910 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
1911 * So, double-counting is effectively avoided.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001912 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001913 __mem_cgroup_commit_charge(mem, pc, ctype);
1914
1915 /*
1916 * Both of oldpage and newpage are still under lock_page().
1917 * Then, we don't have to care about race in radix-tree.
1918 * But we have to be careful that this page is unmapped or not.
1919 *
1920 * There is a case for !page_mapped(). At the start of
1921 * migration, oldpage was mapped. But now, it's zapped.
1922 * But we know *target* page is not freed/reused under us.
1923 * mem_cgroup_uncharge_page() does all necessary checks.
1924 */
1925 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
1926 mem_cgroup_uncharge_page(target);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001927 /*
1928 * At migration, we may charge account against cgroup which has no tasks
1929 * So, rmdir()->pre_destroy() can be called while we do this charge.
1930 * In that case, we need to call pre_destroy() again. check it here.
1931 */
1932 cgroup_release_and_wakeup_rmdir(&mem->css);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001933}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001934
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001935/*
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07001936 * A call to try to shrink memory usage on charge failure at shmem's swapin.
1937 * Calling hierarchical_reclaim is not enough because we should update
1938 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
1939 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
1940 * not from the memcg which this page would be charged to.
1941 * try_charge_swapin does all of these works properly.
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001942 */
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07001943int mem_cgroup_shmem_charge_fallback(struct page *page,
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001944 struct mm_struct *mm,
1945 gfp_t gfp_mask)
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001946{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001947 struct mem_cgroup *mem = NULL;
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07001948 int ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001949
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001950 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001951 return 0;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001952
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07001953 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1954 if (!ret)
1955 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001956
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07001957 return ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001958}
1959
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001960static DEFINE_MUTEX(set_limit_mutex);
1961
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08001962static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001963 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001964{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001965 int retry_count;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001966 int progress;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001967 u64 memswlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001968 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001969 int children = mem_cgroup_count_children(memcg);
1970 u64 curusage, oldusage;
1971
1972 /*
1973 * For keeping hierarchical_reclaim simple, how long we should retry
1974 * is depends on callers. We set our retry-count to be function
1975 * of # of children which we should visit in this loop.
1976 */
1977 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
1978
1979 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001980
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001981 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001982 if (signal_pending(current)) {
1983 ret = -EINTR;
1984 break;
1985 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001986 /*
1987 * Rather than hide all in some function, I do this in
1988 * open coded manner. You see what this really does.
1989 * We have to guarantee mem->res.limit < mem->memsw.limit.
1990 */
1991 mutex_lock(&set_limit_mutex);
1992 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1993 if (memswlimit < val) {
1994 ret = -EINVAL;
1995 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001996 break;
1997 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001998 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001999 if (!ret) {
2000 if (memswlimit == val)
2001 memcg->memsw_is_minimum = true;
2002 else
2003 memcg->memsw_is_minimum = false;
2004 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002005 mutex_unlock(&set_limit_mutex);
2006
2007 if (!ret)
2008 break;
2009
Daisuke Nishimura42e9abb2009-01-07 18:08:30 -08002010 progress = mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL,
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002011 false, true);
2012 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
2013 /* Usage is reduced ? */
2014 if (curusage >= oldusage)
2015 retry_count--;
2016 else
2017 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002018 }
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002019
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002020 return ret;
2021}
2022
Li Zefan338c8432009-06-17 16:27:15 -07002023static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
2024 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002025{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002026 int retry_count;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002027 u64 memlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002028 int children = mem_cgroup_count_children(memcg);
2029 int ret = -EBUSY;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002030
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002031 /* see mem_cgroup_resize_res_limit */
2032 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
2033 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002034 while (retry_count) {
2035 if (signal_pending(current)) {
2036 ret = -EINTR;
2037 break;
2038 }
2039 /*
2040 * Rather than hide all in some function, I do this in
2041 * open coded manner. You see what this really does.
2042 * We have to guarantee mem->res.limit < mem->memsw.limit.
2043 */
2044 mutex_lock(&set_limit_mutex);
2045 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2046 if (memlimit > val) {
2047 ret = -EINVAL;
2048 mutex_unlock(&set_limit_mutex);
2049 break;
2050 }
2051 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07002052 if (!ret) {
2053 if (memlimit == val)
2054 memcg->memsw_is_minimum = true;
2055 else
2056 memcg->memsw_is_minimum = false;
2057 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002058 mutex_unlock(&set_limit_mutex);
2059
2060 if (!ret)
2061 break;
2062
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002063 mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL, true, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002064 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002065 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002066 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002067 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002068 else
2069 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002070 }
2071 return ret;
2072}
2073
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002074/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002075 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002076 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
2077 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002078static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002079 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002080{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002081 struct zone *zone;
2082 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002083 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002084 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002085 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002086 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002087
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002088 zone = &NODE_DATA(node)->node_zones[zid];
2089 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07002090 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002091
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002092 loop = MEM_CGROUP_ZSTAT(mz, lru);
2093 /* give some margin against EBUSY etc...*/
2094 loop += 256;
2095 busy = NULL;
2096 while (loop--) {
2097 ret = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002098 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002099 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002100 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002101 break;
2102 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002103 pc = list_entry(list->prev, struct page_cgroup, lru);
2104 if (busy == pc) {
2105 list_move(&pc->lru, list);
2106 busy = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002107 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002108 continue;
2109 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002110 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002111
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -08002112 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002113 if (ret == -ENOMEM)
2114 break;
2115
2116 if (ret == -EBUSY || ret == -EINVAL) {
2117 /* found lock contention or "pc" is obsolete. */
2118 busy = pc;
2119 cond_resched();
2120 } else
2121 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002122 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002123
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002124 if (!ret && !list_empty(list))
2125 return -EBUSY;
2126 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002127}
2128
2129/*
2130 * make mem_cgroup's charge to be 0 if there is no task.
2131 * This enables deleting this mem_cgroup.
2132 */
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002133static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002134{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002135 int ret;
2136 int node, zid, shrink;
2137 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002138 struct cgroup *cgrp = mem->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08002139
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002140 css_get(&mem->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002141
2142 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002143 /* should free all ? */
2144 if (free_all)
2145 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002146move_account:
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002147 while (mem->res.usage > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002148 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002149 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002150 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002151 ret = -EINTR;
2152 if (signal_pending(current))
2153 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002154 /* This is for making all *used* pages to be on LRU. */
2155 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002156 ret = 0;
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08002157 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002158 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07002159 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002160 for_each_lru(l) {
2161 ret = mem_cgroup_force_empty_list(mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002162 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002163 if (ret)
2164 break;
2165 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002166 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002167 if (ret)
2168 break;
2169 }
2170 /* it seems parent cgroup doesn't have enough mem */
2171 if (ret == -ENOMEM)
2172 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002173 cond_resched();
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002174 }
2175 ret = 0;
2176out:
2177 css_put(&mem->css);
2178 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002179
2180try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002181 /* returns EBUSY if there is a task or if we come here twice. */
2182 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002183 ret = -EBUSY;
2184 goto out;
2185 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002186 /* we call try-to-free pages for make this cgroup empty */
2187 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002188 /* try to free all pages in this cgroup */
2189 shrink = 1;
2190 while (nr_retries && mem->res.usage > 0) {
2191 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002192
2193 if (signal_pending(current)) {
2194 ret = -EINTR;
2195 goto out;
2196 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002197 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
2198 false, get_swappiness(mem));
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002199 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002200 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002201 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02002202 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002203 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002204
2205 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002206 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002207 /* try move_account...there may be some *locked* pages. */
2208 if (mem->res.usage)
2209 goto move_account;
2210 ret = 0;
2211 goto out;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002212}
2213
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002214int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
2215{
2216 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
2217}
2218
2219
Balbir Singh18f59ea2009-01-07 18:08:07 -08002220static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
2221{
2222 return mem_cgroup_from_cont(cont)->use_hierarchy;
2223}
2224
2225static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
2226 u64 val)
2227{
2228 int retval = 0;
2229 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2230 struct cgroup *parent = cont->parent;
2231 struct mem_cgroup *parent_mem = NULL;
2232
2233 if (parent)
2234 parent_mem = mem_cgroup_from_cont(parent);
2235
2236 cgroup_lock();
2237 /*
2238 * If parent's use_hiearchy is set, we can't make any modifications
2239 * in the child subtrees. If it is unset, then the change can
2240 * occur, provided the current cgroup has no children.
2241 *
2242 * For the root cgroup, parent_mem is NULL, we allow value to be
2243 * set if there are no children.
2244 */
2245 if ((!parent_mem || !parent_mem->use_hierarchy) &&
2246 (val == 1 || val == 0)) {
2247 if (list_empty(&cont->children))
2248 mem->use_hierarchy = val;
2249 else
2250 retval = -EBUSY;
2251 } else
2252 retval = -EINVAL;
2253 cgroup_unlock();
2254
2255 return retval;
2256}
2257
Paul Menage2c3daa72008-04-29 00:59:58 -07002258static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002259{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002260 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2261 u64 val = 0;
2262 int type, name;
2263
2264 type = MEMFILE_TYPE(cft->private);
2265 name = MEMFILE_ATTR(cft->private);
2266 switch (type) {
2267 case _MEM:
2268 val = res_counter_read_u64(&mem->res, name);
2269 break;
2270 case _MEMSWAP:
Li Zefan338c8432009-06-17 16:27:15 -07002271 val = res_counter_read_u64(&mem->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002272 break;
2273 default:
2274 BUG();
2275 break;
2276 }
2277 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002278}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002279/*
2280 * The user of this function is...
2281 * RES_LIMIT.
2282 */
Paul Menage856c13a2008-07-25 01:47:04 -07002283static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
2284 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002285{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002286 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002287 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002288 unsigned long long val;
2289 int ret;
2290
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002291 type = MEMFILE_TYPE(cft->private);
2292 name = MEMFILE_ATTR(cft->private);
2293 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002294 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07002295 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
2296 ret = -EINVAL;
2297 break;
2298 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002299 /* This function does all necessary parse...reuse it */
2300 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002301 if (ret)
2302 break;
2303 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002304 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002305 else
2306 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002307 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07002308 case RES_SOFT_LIMIT:
2309 ret = res_counter_memparse_write_strategy(buffer, &val);
2310 if (ret)
2311 break;
2312 /*
2313 * For memsw, soft limits are hard to implement in terms
2314 * of semantics, for now, we support soft limits for
2315 * control without swap
2316 */
2317 if (type == _MEM)
2318 ret = res_counter_set_soft_limit(&memcg->res, val);
2319 else
2320 ret = -EINVAL;
2321 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002322 default:
2323 ret = -EINVAL; /* should be BUG() ? */
2324 break;
2325 }
2326 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002327}
2328
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08002329static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
2330 unsigned long long *mem_limit, unsigned long long *memsw_limit)
2331{
2332 struct cgroup *cgroup;
2333 unsigned long long min_limit, min_memsw_limit, tmp;
2334
2335 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2336 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2337 cgroup = memcg->css.cgroup;
2338 if (!memcg->use_hierarchy)
2339 goto out;
2340
2341 while (cgroup->parent) {
2342 cgroup = cgroup->parent;
2343 memcg = mem_cgroup_from_cont(cgroup);
2344 if (!memcg->use_hierarchy)
2345 break;
2346 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
2347 min_limit = min(min_limit, tmp);
2348 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2349 min_memsw_limit = min(min_memsw_limit, tmp);
2350 }
2351out:
2352 *mem_limit = min_limit;
2353 *memsw_limit = min_memsw_limit;
2354 return;
2355}
2356
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002357static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002358{
2359 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002360 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002361
2362 mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002363 type = MEMFILE_TYPE(event);
2364 name = MEMFILE_ATTR(event);
2365 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002366 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002367 if (type == _MEM)
2368 res_counter_reset_max(&mem->res);
2369 else
2370 res_counter_reset_max(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002371 break;
2372 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002373 if (type == _MEM)
2374 res_counter_reset_failcnt(&mem->res);
2375 else
2376 res_counter_reset_failcnt(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002377 break;
2378 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07002379
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07002380 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002381}
2382
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002383
2384/* For read statistics */
2385enum {
2386 MCS_CACHE,
2387 MCS_RSS,
Balbir Singhd69b0422009-06-17 16:26:34 -07002388 MCS_MAPPED_FILE,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002389 MCS_PGPGIN,
2390 MCS_PGPGOUT,
2391 MCS_INACTIVE_ANON,
2392 MCS_ACTIVE_ANON,
2393 MCS_INACTIVE_FILE,
2394 MCS_ACTIVE_FILE,
2395 MCS_UNEVICTABLE,
2396 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002397};
2398
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002399struct mcs_total_stat {
2400 s64 stat[NR_MCS_STAT];
2401};
2402
2403struct {
2404 char *local_name;
2405 char *total_name;
2406} memcg_stat_strings[NR_MCS_STAT] = {
2407 {"cache", "total_cache"},
2408 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07002409 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002410 {"pgpgin", "total_pgpgin"},
2411 {"pgpgout", "total_pgpgout"},
2412 {"inactive_anon", "total_inactive_anon"},
2413 {"active_anon", "total_active_anon"},
2414 {"inactive_file", "total_inactive_file"},
2415 {"active_file", "total_active_file"},
2416 {"unevictable", "total_unevictable"}
2417};
2418
2419
2420static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
2421{
2422 struct mcs_total_stat *s = data;
2423 s64 val;
2424
2425 /* per cpu stat */
2426 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE);
2427 s->stat[MCS_CACHE] += val * PAGE_SIZE;
2428 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
2429 s->stat[MCS_RSS] += val * PAGE_SIZE;
Balbir Singhd69b0422009-06-17 16:26:34 -07002430 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_MAPPED_FILE);
2431 s->stat[MCS_MAPPED_FILE] += val * PAGE_SIZE;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002432 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT);
2433 s->stat[MCS_PGPGIN] += val;
2434 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT);
2435 s->stat[MCS_PGPGOUT] += val;
2436
2437 /* per zone stat */
2438 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
2439 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
2440 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
2441 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
2442 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
2443 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
2444 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
2445 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
2446 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
2447 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
2448 return 0;
2449}
2450
2451static void
2452mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
2453{
2454 mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
2455}
2456
Paul Menagec64745c2008-04-29 01:00:02 -07002457static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
2458 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002459{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002460 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002461 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002462 int i;
2463
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002464 memset(&mystat, 0, sizeof(mystat));
2465 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002466
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002467 for (i = 0; i < NR_MCS_STAT; i++)
2468 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002469
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002470 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08002471 {
2472 unsigned long long limit, memsw_limit;
2473 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
2474 cb->fill(cb, "hierarchical_memory_limit", limit);
2475 if (do_swap_account)
2476 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
2477 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002478
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002479 memset(&mystat, 0, sizeof(mystat));
2480 mem_cgroup_get_total_stat(mem_cont, &mystat);
2481 for (i = 0; i < NR_MCS_STAT; i++)
2482 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
2483
2484
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002485#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08002486 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002487
2488 {
2489 int nid, zid;
2490 struct mem_cgroup_per_zone *mz;
2491 unsigned long recent_rotated[2] = {0, 0};
2492 unsigned long recent_scanned[2] = {0, 0};
2493
2494 for_each_online_node(nid)
2495 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
2496 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
2497
2498 recent_rotated[0] +=
2499 mz->reclaim_stat.recent_rotated[0];
2500 recent_rotated[1] +=
2501 mz->reclaim_stat.recent_rotated[1];
2502 recent_scanned[0] +=
2503 mz->reclaim_stat.recent_scanned[0];
2504 recent_scanned[1] +=
2505 mz->reclaim_stat.recent_scanned[1];
2506 }
2507 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
2508 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
2509 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
2510 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
2511 }
2512#endif
2513
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002514 return 0;
2515}
2516
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002517static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
2518{
2519 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2520
2521 return get_swappiness(memcg);
2522}
2523
2524static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
2525 u64 val)
2526{
2527 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2528 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08002529
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002530 if (val > 100)
2531 return -EINVAL;
2532
2533 if (cgrp->parent == NULL)
2534 return -EINVAL;
2535
2536 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08002537
2538 cgroup_lock();
2539
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002540 /* If under hierarchy, only empty-root can set this value */
2541 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08002542 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
2543 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002544 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08002545 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002546
2547 spin_lock(&memcg->reclaim_param_lock);
2548 memcg->swappiness = val;
2549 spin_unlock(&memcg->reclaim_param_lock);
2550
Li Zefan068b38c2009-01-15 13:51:26 -08002551 cgroup_unlock();
2552
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002553 return 0;
2554}
2555
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002556
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002557static struct cftype mem_cgroup_files[] = {
2558 {
Balbir Singh0eea1032008-02-07 00:13:57 -08002559 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002560 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07002561 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002562 },
2563 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002564 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002565 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002566 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002567 .read_u64 = mem_cgroup_read,
2568 },
2569 {
Balbir Singh0eea1032008-02-07 00:13:57 -08002570 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002571 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07002572 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07002573 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002574 },
2575 {
Balbir Singh296c81d2009-09-23 15:56:36 -07002576 .name = "soft_limit_in_bytes",
2577 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
2578 .write_string = mem_cgroup_write,
2579 .read_u64 = mem_cgroup_read,
2580 },
2581 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002582 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002583 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002584 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07002585 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002586 },
Balbir Singh8697d332008-02-07 00:13:59 -08002587 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002588 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07002589 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002590 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002591 {
2592 .name = "force_empty",
2593 .trigger = mem_cgroup_force_empty_write,
2594 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08002595 {
2596 .name = "use_hierarchy",
2597 .write_u64 = mem_cgroup_hierarchy_write,
2598 .read_u64 = mem_cgroup_hierarchy_read,
2599 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002600 {
2601 .name = "swappiness",
2602 .read_u64 = mem_cgroup_swappiness_read,
2603 .write_u64 = mem_cgroup_swappiness_write,
2604 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002605};
2606
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002607#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2608static struct cftype memsw_cgroup_files[] = {
2609 {
2610 .name = "memsw.usage_in_bytes",
2611 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
2612 .read_u64 = mem_cgroup_read,
2613 },
2614 {
2615 .name = "memsw.max_usage_in_bytes",
2616 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
2617 .trigger = mem_cgroup_reset,
2618 .read_u64 = mem_cgroup_read,
2619 },
2620 {
2621 .name = "memsw.limit_in_bytes",
2622 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
2623 .write_string = mem_cgroup_write,
2624 .read_u64 = mem_cgroup_read,
2625 },
2626 {
2627 .name = "memsw.failcnt",
2628 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
2629 .trigger = mem_cgroup_reset,
2630 .read_u64 = mem_cgroup_read,
2631 },
2632};
2633
2634static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2635{
2636 if (!do_swap_account)
2637 return 0;
2638 return cgroup_add_files(cont, ss, memsw_cgroup_files,
2639 ARRAY_SIZE(memsw_cgroup_files));
2640};
2641#else
2642static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2643{
2644 return 0;
2645}
2646#endif
2647
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002648static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2649{
2650 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002651 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07002652 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07002653 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002654 /*
2655 * This routine is called against possible nodes.
2656 * But it's BUG to call kmalloc() against offline node.
2657 *
2658 * TODO: this routine can waste much memory for nodes which will
2659 * never be onlined. It's better to use memory hotplug callback
2660 * function.
2661 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07002662 if (!node_state(node, N_NORMAL_MEMORY))
2663 tmp = -1;
2664 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002665 if (!pn)
2666 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002667
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002668 mem->info.nodeinfo[node] = pn;
2669 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002670
2671 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
2672 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07002673 for_each_lru(l)
2674 INIT_LIST_HEAD(&mz->lists[l]);
Balbir Singhf64c3f52009-09-23 15:56:37 -07002675 mz->usage_in_excess = 0;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002676 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002677 return 0;
2678}
2679
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002680static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2681{
2682 kfree(mem->info.nodeinfo[node]);
2683}
2684
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002685static int mem_cgroup_size(void)
2686{
2687 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
2688 return sizeof(struct mem_cgroup) + cpustat_size;
2689}
2690
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002691static struct mem_cgroup *mem_cgroup_alloc(void)
2692{
2693 struct mem_cgroup *mem;
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002694 int size = mem_cgroup_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002695
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002696 if (size < PAGE_SIZE)
2697 mem = kmalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002698 else
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002699 mem = vmalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002700
2701 if (mem)
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002702 memset(mem, 0, size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002703 return mem;
2704}
2705
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002706/*
2707 * At destroying mem_cgroup, references from swap_cgroup can remain.
2708 * (scanning all at force_empty is too costly...)
2709 *
2710 * Instead of clearing all references at force_empty, we remember
2711 * the number of reference from swap_cgroup and free mem_cgroup when
2712 * it goes down to 0.
2713 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002714 * Removal of cgroup itself succeeds regardless of refs from swap.
2715 */
2716
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08002717static void __mem_cgroup_free(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002718{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002719 int node;
2720
Balbir Singhf64c3f52009-09-23 15:56:37 -07002721 mem_cgroup_remove_from_trees(mem);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002722 free_css_id(&mem_cgroup_subsys, &mem->css);
2723
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002724 for_each_node_state(node, N_POSSIBLE)
2725 free_mem_cgroup_per_zone_info(mem, node);
2726
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002727 if (mem_cgroup_size() < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002728 kfree(mem);
2729 else
2730 vfree(mem);
2731}
2732
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002733static void mem_cgroup_get(struct mem_cgroup *mem)
2734{
2735 atomic_inc(&mem->refcnt);
2736}
2737
2738static void mem_cgroup_put(struct mem_cgroup *mem)
2739{
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08002740 if (atomic_dec_and_test(&mem->refcnt)) {
2741 struct mem_cgroup *parent = parent_mem_cgroup(mem);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08002742 __mem_cgroup_free(mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08002743 if (parent)
2744 mem_cgroup_put(parent);
2745 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002746}
2747
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08002748/*
2749 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
2750 */
2751static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
2752{
2753 if (!mem->res.parent)
2754 return NULL;
2755 return mem_cgroup_from_res_counter(mem->res.parent, res);
2756}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002757
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08002758#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2759static void __init enable_swap_cgroup(void)
2760{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002761 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08002762 do_swap_account = 1;
2763}
2764#else
2765static void __init enable_swap_cgroup(void)
2766{
2767}
2768#endif
2769
Balbir Singhf64c3f52009-09-23 15:56:37 -07002770static int mem_cgroup_soft_limit_tree_init(void)
2771{
2772 struct mem_cgroup_tree_per_node *rtpn;
2773 struct mem_cgroup_tree_per_zone *rtpz;
2774 int tmp, node, zone;
2775
2776 for_each_node_state(node, N_POSSIBLE) {
2777 tmp = node;
2778 if (!node_state(node, N_NORMAL_MEMORY))
2779 tmp = -1;
2780 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
2781 if (!rtpn)
2782 return 1;
2783
2784 soft_limit_tree.rb_tree_per_node[node] = rtpn;
2785
2786 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
2787 rtpz = &rtpn->rb_tree_per_zone[zone];
2788 rtpz->rb_root = RB_ROOT;
2789 spin_lock_init(&rtpz->lock);
2790 }
2791 }
2792 return 0;
2793}
2794
Li Zefan0eb253e2009-01-15 13:51:25 -08002795static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002796mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
2797{
Balbir Singh28dbc4b2009-01-07 18:08:05 -08002798 struct mem_cgroup *mem, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002799 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002800 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002801
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002802 mem = mem_cgroup_alloc();
2803 if (!mem)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002804 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08002805
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002806 for_each_node_state(node, N_POSSIBLE)
2807 if (alloc_mem_cgroup_per_zone_info(mem, node))
2808 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07002809
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08002810 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08002811 if (cont->parent == NULL) {
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08002812 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08002813 parent = NULL;
Balbir Singh4b3bde42009-09-23 15:56:32 -07002814 root_mem_cgroup = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07002815 if (mem_cgroup_soft_limit_tree_init())
2816 goto free_out;
2817
Balbir Singh18f59ea2009-01-07 18:08:07 -08002818 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08002819 parent = mem_cgroup_from_cont(cont->parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08002820 mem->use_hierarchy = parent->use_hierarchy;
2821 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08002822
Balbir Singh18f59ea2009-01-07 18:08:07 -08002823 if (parent && parent->use_hierarchy) {
2824 res_counter_init(&mem->res, &parent->res);
2825 res_counter_init(&mem->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08002826 /*
2827 * We increment refcnt of the parent to ensure that we can
2828 * safely access it on res_counter_charge/uncharge.
2829 * This refcnt will be decremented when freeing this
2830 * mem_cgroup(see mem_cgroup_put).
2831 */
2832 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08002833 } else {
2834 res_counter_init(&mem->res, NULL);
2835 res_counter_init(&mem->memsw, NULL);
2836 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002837 mem->last_scanned_child = 0;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -08002838 spin_lock_init(&mem->reclaim_param_lock);
Balbir Singh6d61ef42009-01-07 18:08:06 -08002839
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002840 if (parent)
2841 mem->swappiness = get_swappiness(parent);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08002842 atomic_set(&mem->refcnt, 1);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002843 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002844free_out:
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08002845 __mem_cgroup_free(mem);
Balbir Singh4b3bde42009-09-23 15:56:32 -07002846 root_mem_cgroup = NULL;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002847 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002848}
2849
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002850static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08002851 struct cgroup *cont)
2852{
2853 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002854
2855 return mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08002856}
2857
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002858static void mem_cgroup_destroy(struct cgroup_subsys *ss,
2859 struct cgroup *cont)
2860{
Daisuke Nishimurac268e992009-01-15 13:51:13 -08002861 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08002862
Daisuke Nishimurac268e992009-01-15 13:51:13 -08002863 mem_cgroup_put(mem);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002864}
2865
2866static int mem_cgroup_populate(struct cgroup_subsys *ss,
2867 struct cgroup *cont)
2868{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002869 int ret;
2870
2871 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
2872 ARRAY_SIZE(mem_cgroup_files));
2873
2874 if (!ret)
2875 ret = register_memsw_files(cont, ss);
2876 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002877}
2878
Balbir Singh67e465a2008-02-07 00:13:54 -08002879static void mem_cgroup_move_task(struct cgroup_subsys *ss,
2880 struct cgroup *cont,
2881 struct cgroup *old_cont,
Ben Blumbe367d02009-09-23 15:56:31 -07002882 struct task_struct *p,
2883 bool threadgroup)
Balbir Singh67e465a2008-02-07 00:13:54 -08002884{
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08002885 mutex_lock(&memcg_tasklist);
Balbir Singh67e465a2008-02-07 00:13:54 -08002886 /*
Nikanth Karthikesanf9717d22009-01-07 18:08:11 -08002887 * FIXME: It's better to move charges of this process from old
2888 * memcg to new memcg. But it's just on TODO-List now.
Balbir Singh67e465a2008-02-07 00:13:54 -08002889 */
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08002890 mutex_unlock(&memcg_tasklist);
Balbir Singh67e465a2008-02-07 00:13:54 -08002891}
2892
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002893struct cgroup_subsys mem_cgroup_subsys = {
2894 .name = "memory",
2895 .subsys_id = mem_cgroup_subsys_id,
2896 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08002897 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002898 .destroy = mem_cgroup_destroy,
2899 .populate = mem_cgroup_populate,
Balbir Singh67e465a2008-02-07 00:13:54 -08002900 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002901 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002902 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002903};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08002904
2905#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2906
2907static int __init disable_swap_account(char *s)
2908{
2909 really_do_swap_account = 0;
2910 return 1;
2911}
2912__setup("noswapaccount", disable_swap_account);
2913#endif