blob: 954032b80bedd0f761d66ba8c984dc88207c9584 [file] [log] [blame]
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001/* memcontrol.c - Memory Controller
2 *
3 * Copyright IBM Corporation, 2007
4 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
5 *
Pavel Emelianov78fb7462008-02-07 00:13:51 -08006 * Copyright 2007 OpenVZ SWsoft Inc
7 * Author: Pavel Emelianov <xemul@openvz.org>
8 *
Balbir Singh8cdea7c2008-02-07 00:13:50 -08009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 */
19
20#include <linux/res_counter.h>
21#include <linux/memcontrol.h>
22#include <linux/cgroup.h>
Pavel Emelianov78fb7462008-02-07 00:13:51 -080023#include <linux/mm.h>
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -080024#include <linux/pagemap.h>
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080025#include <linux/smp.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080026#include <linux/page-flags.h>
Balbir Singh66e17072008-02-07 00:13:56 -080027#include <linux/backing-dev.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080028#include <linux/bit_spinlock.h>
29#include <linux/rcupdate.h>
Balbir Singhe2224322009-04-02 16:57:39 -070030#include <linux/limits.h>
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -080031#include <linux/mutex.h>
Balbir Singhf64c3f52009-09-23 15:56:37 -070032#include <linux/rbtree.h>
Balbir Singhb6ac57d2008-04-29 01:00:19 -070033#include <linux/slab.h>
Balbir Singh66e17072008-02-07 00:13:56 -080034#include <linux/swap.h>
35#include <linux/spinlock.h>
36#include <linux/fs.h>
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -080037#include <linux/seq_file.h>
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -070038#include <linux/vmalloc.h>
Christoph Lameterb69408e2008-10-18 20:26:14 -070039#include <linux/mm_inline.h>
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070040#include <linux/page_cgroup.h>
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -080041#include <linux/cpu.h>
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -080042#include "internal.h"
Balbir Singh8cdea7c2008-02-07 00:13:50 -080043
Balbir Singh8697d332008-02-07 00:13:59 -080044#include <asm/uaccess.h>
45
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070046struct cgroup_subsys mem_cgroup_subsys __read_mostly;
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070047#define MEM_CGROUP_RECLAIM_RETRIES 5
Balbir Singh4b3bde42009-09-23 15:56:32 -070048struct mem_cgroup *root_mem_cgroup __read_mostly;
Balbir Singh8cdea7c2008-02-07 00:13:50 -080049
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080050#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
Li Zefan338c8432009-06-17 16:27:15 -070051/* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080052int do_swap_account __read_mostly;
53static int really_do_swap_account __initdata = 1; /* for remember boot option*/
54#else
55#define do_swap_account (0)
56#endif
57
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 */
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -080069 MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */
Balaji Rao55e462b2008-05-01 04:35:12 -070070 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
71 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
Balbir Singhf64c3f52009-09-23 15:56:37 -070072 MEM_CGROUP_STAT_EVENTS, /* sum of pagein + pageout for internal use */
Balbir Singh0c3e73e2009-09-23 15:56:42 -070073 MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080074
75 MEM_CGROUP_STAT_NSTATS,
76};
77
78struct mem_cgroup_stat_cpu {
79 s64 count[MEM_CGROUP_STAT_NSTATS];
80} ____cacheline_aligned_in_smp;
81
82struct mem_cgroup_stat {
Jan Blunckc8dad2b2009-01-07 18:07:53 -080083 struct mem_cgroup_stat_cpu cpustat[0];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080084};
85
Balbir Singhf64c3f52009-09-23 15:56:37 -070086static inline void
87__mem_cgroup_stat_reset_safe(struct mem_cgroup_stat_cpu *stat,
88 enum mem_cgroup_stat_index idx)
89{
90 stat->count[idx] = 0;
91}
92
93static inline s64
94__mem_cgroup_stat_read_local(struct mem_cgroup_stat_cpu *stat,
95 enum mem_cgroup_stat_index idx)
96{
97 return stat->count[idx];
98}
99
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800100/*
101 * For accounting under irq disable, no need for increment preempt count.
102 */
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700103static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800104 enum mem_cgroup_stat_index idx, int val)
105{
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700106 stat->count[idx] += val;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800107}
108
109static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
110 enum mem_cgroup_stat_index idx)
111{
112 int cpu;
113 s64 ret = 0;
114 for_each_possible_cpu(cpu)
115 ret += stat->cpustat[cpu].count[idx];
116 return ret;
117}
118
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700119static s64 mem_cgroup_local_usage(struct mem_cgroup_stat *stat)
120{
121 s64 ret;
122
123 ret = mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_CACHE);
124 ret += mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_RSS);
125 return ret;
126}
127
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800128/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800129 * per-zone information in memory controller.
130 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800131struct mem_cgroup_per_zone {
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800132 /*
133 * spin_lock to protect the per cgroup LRU
134 */
Christoph Lameterb69408e2008-10-18 20:26:14 -0700135 struct list_head lists[NR_LRU_LISTS];
136 unsigned long count[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800137
138 struct zone_reclaim_stat reclaim_stat;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700139 struct rb_node tree_node; /* RB tree node */
140 unsigned long long usage_in_excess;/* Set to the value by which */
141 /* the soft limit is exceeded*/
142 bool on_tree;
Balbir Singh4e416952009-09-23 15:56:39 -0700143 struct mem_cgroup *mem; /* Back pointer, we cannot */
144 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800145};
146/* Macro for accessing counter */
147#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
148
149struct mem_cgroup_per_node {
150 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
151};
152
153struct mem_cgroup_lru_info {
154 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
155};
156
157/*
Balbir Singhf64c3f52009-09-23 15:56:37 -0700158 * Cgroups above their limits are maintained in a RB-Tree, independent of
159 * their hierarchy representation
160 */
161
162struct mem_cgroup_tree_per_zone {
163 struct rb_root rb_root;
164 spinlock_t lock;
165};
166
167struct mem_cgroup_tree_per_node {
168 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
169};
170
171struct mem_cgroup_tree {
172 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
173};
174
175static struct mem_cgroup_tree soft_limit_tree __read_mostly;
176
177/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800178 * The memory controller data structure. The memory controller controls both
179 * page cache and RSS per cgroup. We would eventually like to provide
180 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
181 * to help the administrator determine what knobs to tune.
182 *
183 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800184 * we hit the water mark. May be even add a low water mark, such that
185 * no reclaim occurs from a cgroup at it's low water mark, this is
186 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800187 */
188struct mem_cgroup {
189 struct cgroup_subsys_state css;
190 /*
191 * the counter to account for memory usage
192 */
193 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800194 /*
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800195 * the counter to account for mem+swap usage.
196 */
197 struct res_counter memsw;
198 /*
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800199 * Per cgroup active and inactive list, similar to the
200 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800201 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800202 struct mem_cgroup_lru_info info;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800203
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800204 /*
205 protect against reclaim related member.
206 */
207 spinlock_t reclaim_param_lock;
208
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800209 int prev_priority; /* for recording reclaim priority */
Balbir Singh6d61ef42009-01-07 18:08:06 -0800210
211 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200212 * While reclaiming in a hierarchy, we cache the last child we
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700213 * reclaimed from.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800214 */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700215 int last_scanned_child;
Balbir Singh18f59ea2009-01-07 18:08:07 -0800216 /*
217 * Should the accounting and control be hierarchical, per subtree?
218 */
219 bool use_hierarchy;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800220 unsigned long last_oom_jiffies;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800221 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800222
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800223 unsigned int swappiness;
224
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700225 /* set when res.limit == memsw.limit */
226 bool memsw_is_minimum;
227
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800228 /*
Jan Blunckc8dad2b2009-01-07 18:07:53 -0800229 * statistics. This must be placed at the end of memcg.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800230 */
231 struct mem_cgroup_stat stat;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800232};
233
Balbir Singh4e416952009-09-23 15:56:39 -0700234/*
235 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
236 * limit reclaim to prevent infinite loops, if they ever occur.
237 */
238#define MEM_CGROUP_MAX_RECLAIM_LOOPS (100)
239#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2)
240
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800241enum charge_type {
242 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
243 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700244 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700245 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800246 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700247 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700248 NR_CHARGE_TYPE,
249};
250
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700251/* only for here (for easy reading.) */
252#define PCGF_CACHE (1UL << PCG_CACHE)
253#define PCGF_USED (1UL << PCG_USED)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700254#define PCGF_LOCK (1UL << PCG_LOCK)
Balbir Singh4b3bde42009-09-23 15:56:32 -0700255/* Not used, but added here for completeness */
256#define PCGF_ACCT (1UL << PCG_ACCT)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800257
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800258/* for encoding cft->private value on file */
259#define _MEM (0)
260#define _MEMSWAP (1)
261#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
262#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
263#define MEMFILE_ATTR(val) ((val) & 0xffff)
264
Balbir Singh75822b42009-09-23 15:56:38 -0700265/*
266 * Reclaim flags for mem_cgroup_hierarchical_reclaim
267 */
268#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
269#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
270#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
271#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
Balbir Singh4e416952009-09-23 15:56:39 -0700272#define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
273#define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
Balbir Singh75822b42009-09-23 15:56:38 -0700274
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800275static void mem_cgroup_get(struct mem_cgroup *mem);
276static void mem_cgroup_put(struct mem_cgroup *mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -0800277static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -0800278static void drain_all_stock_async(void);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800279
Balbir Singhf64c3f52009-09-23 15:56:37 -0700280static struct mem_cgroup_per_zone *
281mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
282{
283 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
284}
285
Wu Fengguangd3242362009-12-16 12:19:59 +0100286struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *mem)
287{
288 return &mem->css;
289}
290
Balbir Singhf64c3f52009-09-23 15:56:37 -0700291static struct mem_cgroup_per_zone *
292page_cgroup_zoneinfo(struct page_cgroup *pc)
293{
294 struct mem_cgroup *mem = pc->mem_cgroup;
295 int nid = page_cgroup_nid(pc);
296 int zid = page_cgroup_zid(pc);
297
298 if (!mem)
299 return NULL;
300
301 return mem_cgroup_zoneinfo(mem, nid, zid);
302}
303
304static struct mem_cgroup_tree_per_zone *
305soft_limit_tree_node_zone(int nid, int zid)
306{
307 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
308}
309
310static struct mem_cgroup_tree_per_zone *
311soft_limit_tree_from_page(struct page *page)
312{
313 int nid = page_to_nid(page);
314 int zid = page_zonenum(page);
315
316 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
317}
318
319static void
Balbir Singh4e416952009-09-23 15:56:39 -0700320__mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700321 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700322 struct mem_cgroup_tree_per_zone *mctz,
323 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700324{
325 struct rb_node **p = &mctz->rb_root.rb_node;
326 struct rb_node *parent = NULL;
327 struct mem_cgroup_per_zone *mz_node;
328
329 if (mz->on_tree)
330 return;
331
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700332 mz->usage_in_excess = new_usage_in_excess;
333 if (!mz->usage_in_excess)
334 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700335 while (*p) {
336 parent = *p;
337 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
338 tree_node);
339 if (mz->usage_in_excess < mz_node->usage_in_excess)
340 p = &(*p)->rb_left;
341 /*
342 * We can't avoid mem cgroups that are over their soft
343 * limit by the same amount
344 */
345 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
346 p = &(*p)->rb_right;
347 }
348 rb_link_node(&mz->tree_node, parent, p);
349 rb_insert_color(&mz->tree_node, &mctz->rb_root);
350 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700351}
352
353static void
354__mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
355 struct mem_cgroup_per_zone *mz,
356 struct mem_cgroup_tree_per_zone *mctz)
357{
358 if (!mz->on_tree)
359 return;
360 rb_erase(&mz->tree_node, &mctz->rb_root);
361 mz->on_tree = false;
362}
363
364static void
Balbir Singhf64c3f52009-09-23 15:56:37 -0700365mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
366 struct mem_cgroup_per_zone *mz,
367 struct mem_cgroup_tree_per_zone *mctz)
368{
369 spin_lock(&mctz->lock);
Balbir Singh4e416952009-09-23 15:56:39 -0700370 __mem_cgroup_remove_exceeded(mem, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700371 spin_unlock(&mctz->lock);
372}
373
374static bool mem_cgroup_soft_limit_check(struct mem_cgroup *mem)
375{
376 bool ret = false;
377 int cpu;
378 s64 val;
379 struct mem_cgroup_stat_cpu *cpustat;
380
381 cpu = get_cpu();
382 cpustat = &mem->stat.cpustat[cpu];
383 val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_EVENTS);
384 if (unlikely(val > SOFTLIMIT_EVENTS_THRESH)) {
385 __mem_cgroup_stat_reset_safe(cpustat, MEM_CGROUP_STAT_EVENTS);
386 ret = true;
387 }
388 put_cpu();
389 return ret;
390}
391
392static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
393{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700394 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700395 struct mem_cgroup_per_zone *mz;
396 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700397 int nid = page_to_nid(page);
398 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700399 mctz = soft_limit_tree_from_page(page);
400
401 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700402 * Necessary to update all ancestors when hierarchy is used.
403 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700404 */
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700405 for (; mem; mem = parent_mem_cgroup(mem)) {
406 mz = mem_cgroup_zoneinfo(mem, nid, zid);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700407 excess = res_counter_soft_limit_excess(&mem->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700408 /*
409 * We have to update the tree if mz is on RB-tree or
410 * mem is over its softlimit.
411 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700412 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700413 spin_lock(&mctz->lock);
414 /* if on-tree, remove it */
415 if (mz->on_tree)
416 __mem_cgroup_remove_exceeded(mem, mz, mctz);
417 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700418 * Insert again. mz->usage_in_excess will be updated.
419 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700420 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700421 __mem_cgroup_insert_exceeded(mem, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700422 spin_unlock(&mctz->lock);
423 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700424 }
425}
426
427static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
428{
429 int node, zone;
430 struct mem_cgroup_per_zone *mz;
431 struct mem_cgroup_tree_per_zone *mctz;
432
433 for_each_node_state(node, N_POSSIBLE) {
434 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
435 mz = mem_cgroup_zoneinfo(mem, node, zone);
436 mctz = soft_limit_tree_node_zone(node, zone);
437 mem_cgroup_remove_exceeded(mem, mz, mctz);
438 }
439 }
440}
441
Balbir Singh4e416952009-09-23 15:56:39 -0700442static inline unsigned long mem_cgroup_get_excess(struct mem_cgroup *mem)
443{
444 return res_counter_soft_limit_excess(&mem->res) >> PAGE_SHIFT;
445}
446
447static struct mem_cgroup_per_zone *
448__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
449{
450 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700451 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700452
453retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700454 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700455 rightmost = rb_last(&mctz->rb_root);
456 if (!rightmost)
457 goto done; /* Nothing to reclaim from */
458
459 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
460 /*
461 * Remove the node now but someone else can add it back,
462 * we will to add it back at the end of reclaim to its correct
463 * position in the tree.
464 */
465 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
466 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
467 !css_tryget(&mz->mem->css))
468 goto retry;
469done:
470 return mz;
471}
472
473static struct mem_cgroup_per_zone *
474mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
475{
476 struct mem_cgroup_per_zone *mz;
477
478 spin_lock(&mctz->lock);
479 mz = __mem_cgroup_largest_soft_limit_node(mctz);
480 spin_unlock(&mctz->lock);
481 return mz;
482}
483
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700484static void mem_cgroup_swap_statistics(struct mem_cgroup *mem,
485 bool charge)
486{
487 int val = (charge) ? 1 : -1;
488 struct mem_cgroup_stat *stat = &mem->stat;
489 struct mem_cgroup_stat_cpu *cpustat;
490 int cpu = get_cpu();
491
492 cpustat = &stat->cpustat[cpu];
493 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_SWAPOUT, val);
494 put_cpu();
495}
496
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700497static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
498 struct page_cgroup *pc,
499 bool charge)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800500{
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700501 int val = (charge) ? 1 : -1;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800502 struct mem_cgroup_stat *stat = &mem->stat;
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700503 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800504 int cpu = get_cpu();
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800505
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800506 cpustat = &stat->cpustat[cpu];
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700507 if (PageCgroupCache(pc))
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700508 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800509 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700510 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700511
512 if (charge)
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700513 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700514 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
515 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700516 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700517 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700518 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_EVENTS, 1);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800519 put_cpu();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800520}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800521
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700522static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700523 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800524{
525 int nid, zid;
526 struct mem_cgroup_per_zone *mz;
527 u64 total = 0;
528
529 for_each_online_node(nid)
530 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
531 mz = mem_cgroup_zoneinfo(mem, nid, zid);
532 total += MEM_CGROUP_ZSTAT(mz, idx);
533 }
534 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800535}
536
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800537static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800538{
539 return container_of(cgroup_subsys_state(cont,
540 mem_cgroup_subsys_id), struct mem_cgroup,
541 css);
542}
543
Balbir Singhcf475ad2008-04-29 01:00:16 -0700544struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800545{
Balbir Singh31a78f22008-09-28 23:09:31 +0100546 /*
547 * mm_update_next_owner() may clear mm->owner to NULL
548 * if it races with swapoff, page migration, etc.
549 * So this can be called with p == NULL.
550 */
551 if (unlikely(!p))
552 return NULL;
553
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800554 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
555 struct mem_cgroup, css);
556}
557
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800558static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
559{
560 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700561
562 if (!mm)
563 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800564 /*
565 * Because we have no locks, mm->owner's may be being moved to other
566 * cgroup. We use css_tryget() here even if this looks
567 * pessimistic (rather than adding locks here).
568 */
569 rcu_read_lock();
570 do {
571 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
572 if (unlikely(!mem))
573 break;
574 } while (!css_tryget(&mem->css));
575 rcu_read_unlock();
576 return mem;
577}
578
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700579/*
580 * Call callback function against all cgroup under hierarchy tree.
581 */
582static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
583 int (*func)(struct mem_cgroup *, void *))
584{
585 int found, ret, nextid;
586 struct cgroup_subsys_state *css;
587 struct mem_cgroup *mem;
588
589 if (!root->use_hierarchy)
590 return (*func)(root, data);
591
592 nextid = 1;
593 do {
594 ret = 0;
595 mem = NULL;
596
597 rcu_read_lock();
598 css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
599 &found);
600 if (css && css_tryget(css))
601 mem = container_of(css, struct mem_cgroup, css);
602 rcu_read_unlock();
603
604 if (mem) {
605 ret = (*func)(mem, data);
606 css_put(&mem->css);
607 }
608 nextid = found + 1;
609 } while (!ret && css);
610
611 return ret;
612}
613
Balbir Singh4b3bde42009-09-23 15:56:32 -0700614static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
615{
616 return (mem == root_mem_cgroup);
617}
618
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800619/*
620 * Following LRU functions are allowed to be used without PCG_LOCK.
621 * Operations are called by routine of global LRU independently from memcg.
622 * What we have to take care of here is validness of pc->mem_cgroup.
623 *
624 * Changes to pc->mem_cgroup happens when
625 * 1. charge
626 * 2. moving account
627 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
628 * It is added to LRU before charge.
629 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
630 * When moving account, the page is not on LRU. It's isolated.
631 */
632
633void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800634{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800635 struct page_cgroup *pc;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800636 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700637
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800638 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800639 return;
640 pc = lookup_page_cgroup(page);
641 /* can happen while we handle swapcache. */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700642 if (!TestClearPageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800643 return;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700644 VM_BUG_ON(!pc->mem_cgroup);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800645 /*
646 * We don't check PCG_USED bit. It's cleared when the "page" is finally
647 * removed from global LRU.
648 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800649 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700650 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700651 if (mem_cgroup_is_root(pc->mem_cgroup))
652 return;
653 VM_BUG_ON(list_empty(&pc->lru));
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800654 list_del_init(&pc->lru);
655 return;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800656}
657
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800658void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800659{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800660 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800661}
662
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800663void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800664{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800665 struct mem_cgroup_per_zone *mz;
666 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800667
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800668 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700669 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700670
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800671 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800672 /*
673 * Used bit is set without atomic ops but after smp_wmb().
674 * For making pc->mem_cgroup visible, insert smp_rmb() here.
675 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800676 smp_rmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -0700677 /* unused or root page is not rotated. */
678 if (!PageCgroupUsed(pc) || mem_cgroup_is_root(pc->mem_cgroup))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800679 return;
680 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700681 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800682}
683
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800684void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
685{
686 struct page_cgroup *pc;
687 struct mem_cgroup_per_zone *mz;
688
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800689 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800690 return;
691 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700692 VM_BUG_ON(PageCgroupAcctLRU(pc));
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800693 /*
694 * Used bit is set without atomic ops but after smp_wmb().
695 * For making pc->mem_cgroup visible, insert smp_rmb() here.
696 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800697 smp_rmb();
698 if (!PageCgroupUsed(pc))
699 return;
700
701 mz = page_cgroup_zoneinfo(pc);
702 MEM_CGROUP_ZSTAT(mz, lru) += 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700703 SetPageCgroupAcctLRU(pc);
704 if (mem_cgroup_is_root(pc->mem_cgroup))
705 return;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800706 list_add(&pc->lru, &mz->lists[lru]);
707}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800708
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800709/*
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800710 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
711 * lru because the page may.be reused after it's fully uncharged (because of
712 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
713 * it again. This function is only used to charge SwapCache. It's done under
714 * lock_page and expected that zone->lru_lock is never held.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800715 */
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800716static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800717{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800718 unsigned long flags;
719 struct zone *zone = page_zone(page);
720 struct page_cgroup *pc = lookup_page_cgroup(page);
721
722 spin_lock_irqsave(&zone->lru_lock, flags);
723 /*
724 * Forget old LRU when this page_cgroup is *not* used. This Used bit
725 * is guarded by lock_page() because the page is SwapCache.
726 */
727 if (!PageCgroupUsed(pc))
728 mem_cgroup_del_lru_list(page, page_lru(page));
729 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800730}
731
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800732static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
733{
734 unsigned long flags;
735 struct zone *zone = page_zone(page);
736 struct page_cgroup *pc = lookup_page_cgroup(page);
737
738 spin_lock_irqsave(&zone->lru_lock, flags);
739 /* link when the page is linked to LRU but page_cgroup isn't */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700740 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800741 mem_cgroup_add_lru_list(page, page_lru(page));
742 spin_unlock_irqrestore(&zone->lru_lock, flags);
743}
744
745
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800746void mem_cgroup_move_lists(struct page *page,
747 enum lru_list from, enum lru_list to)
748{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800749 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800750 return;
751 mem_cgroup_del_lru_list(page, from);
752 mem_cgroup_add_lru_list(page, to);
753}
754
David Rientjes4c4a2212008-02-07 00:14:06 -0800755int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
756{
757 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700758 struct mem_cgroup *curr = NULL;
David Rientjes4c4a2212008-02-07 00:14:06 -0800759
760 task_lock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700761 rcu_read_lock();
762 curr = try_get_mem_cgroup_from_mm(task->mm);
763 rcu_read_unlock();
David Rientjes4c4a2212008-02-07 00:14:06 -0800764 task_unlock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700765 if (!curr)
766 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -0800767 /*
768 * We should check use_hierarchy of "mem" not "curr". Because checking
769 * use_hierarchy of "curr" here make this function true if hierarchy is
770 * enabled in "curr" and "curr" is a child of "mem" in *cgroup*
771 * hierarchy(even if use_hierarchy is disabled in "mem").
772 */
773 if (mem->use_hierarchy)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700774 ret = css_is_ancestor(&curr->css, &mem->css);
775 else
776 ret = (curr == mem);
777 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -0800778 return ret;
779}
780
Balbir Singh66e17072008-02-07 00:13:56 -0800781/*
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800782 * prev_priority control...this will be used in memory reclaim path.
783 */
784int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
785{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800786 int prev_priority;
787
788 spin_lock(&mem->reclaim_param_lock);
789 prev_priority = mem->prev_priority;
790 spin_unlock(&mem->reclaim_param_lock);
791
792 return prev_priority;
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800793}
794
795void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
796{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800797 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800798 if (priority < mem->prev_priority)
799 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800800 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800801}
802
803void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
804{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800805 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800806 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800807 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800808}
809
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800810static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800811{
812 unsigned long active;
813 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800814 unsigned long gb;
815 unsigned long inactive_ratio;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800816
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700817 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
818 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800819
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800820 gb = (inactive + active) >> (30 - PAGE_SHIFT);
821 if (gb)
822 inactive_ratio = int_sqrt(10 * gb);
823 else
824 inactive_ratio = 1;
825
826 if (present_pages) {
827 present_pages[0] = inactive;
828 present_pages[1] = active;
829 }
830
831 return inactive_ratio;
832}
833
834int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
835{
836 unsigned long active;
837 unsigned long inactive;
838 unsigned long present_pages[2];
839 unsigned long inactive_ratio;
840
841 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
842
843 inactive = present_pages[0];
844 active = present_pages[1];
845
846 if (inactive * inactive_ratio < active)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800847 return 1;
848
849 return 0;
850}
851
Rik van Riel56e49d22009-06-16 15:32:28 -0700852int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
853{
854 unsigned long active;
855 unsigned long inactive;
856
857 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
858 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
859
860 return (active > inactive);
861}
862
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800863unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
864 struct zone *zone,
865 enum lru_list lru)
866{
867 int nid = zone->zone_pgdat->node_id;
868 int zid = zone_idx(zone);
869 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
870
871 return MEM_CGROUP_ZSTAT(mz, lru);
872}
873
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800874struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
875 struct zone *zone)
876{
877 int nid = zone->zone_pgdat->node_id;
878 int zid = zone_idx(zone);
879 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
880
881 return &mz->reclaim_stat;
882}
883
884struct zone_reclaim_stat *
885mem_cgroup_get_reclaim_stat_from_page(struct page *page)
886{
887 struct page_cgroup *pc;
888 struct mem_cgroup_per_zone *mz;
889
890 if (mem_cgroup_disabled())
891 return NULL;
892
893 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800894 /*
895 * Used bit is set without atomic ops but after smp_wmb().
896 * For making pc->mem_cgroup visible, insert smp_rmb() here.
897 */
898 smp_rmb();
899 if (!PageCgroupUsed(pc))
900 return NULL;
901
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800902 mz = page_cgroup_zoneinfo(pc);
903 if (!mz)
904 return NULL;
905
906 return &mz->reclaim_stat;
907}
908
Balbir Singh66e17072008-02-07 00:13:56 -0800909unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
910 struct list_head *dst,
911 unsigned long *scanned, int order,
912 int mode, struct zone *z,
913 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700914 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -0800915{
916 unsigned long nr_taken = 0;
917 struct page *page;
918 unsigned long scan;
919 LIST_HEAD(pc_list);
920 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800921 struct page_cgroup *pc, *tmp;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800922 int nid = z->zone_pgdat->node_id;
923 int zid = zone_idx(z);
924 struct mem_cgroup_per_zone *mz;
Johannes Weinerb7c46d12009-09-21 17:02:56 -0700925 int lru = LRU_FILE * file + active;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700926 int ret;
Balbir Singh66e17072008-02-07 00:13:56 -0800927
Balbir Singhcf475ad2008-04-29 01:00:16 -0700928 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800929 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700930 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -0800931
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800932 scan = 0;
933 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -0800934 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800935 break;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800936
937 page = pc->page;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700938 if (unlikely(!PageCgroupUsed(pc)))
939 continue;
Hugh Dickins436c65412008-02-07 00:14:12 -0800940 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800941 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800942
Hugh Dickins436c65412008-02-07 00:14:12 -0800943 scan++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700944 ret = __isolate_lru_page(page, mode, file);
945 switch (ret) {
946 case 0:
Balbir Singh66e17072008-02-07 00:13:56 -0800947 list_move(&page->lru, dst);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700948 mem_cgroup_del_lru(page);
Balbir Singh66e17072008-02-07 00:13:56 -0800949 nr_taken++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700950 break;
951 case -EBUSY:
952 /* we don't affect global LRU but rotate in our LRU */
953 mem_cgroup_rotate_lru_list(page, page_lru(page));
954 break;
955 default:
956 break;
Balbir Singh66e17072008-02-07 00:13:56 -0800957 }
958 }
959
Balbir Singh66e17072008-02-07 00:13:56 -0800960 *scanned = scan;
961 return nr_taken;
962}
963
Balbir Singh6d61ef42009-01-07 18:08:06 -0800964#define mem_cgroup_from_res_counter(counter, member) \
965 container_of(counter, struct mem_cgroup, member)
966
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -0800967static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
968{
969 if (do_swap_account) {
970 if (res_counter_check_under_limit(&mem->res) &&
971 res_counter_check_under_limit(&mem->memsw))
972 return true;
973 } else
974 if (res_counter_check_under_limit(&mem->res))
975 return true;
976 return false;
977}
978
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800979static unsigned int get_swappiness(struct mem_cgroup *memcg)
980{
981 struct cgroup *cgrp = memcg->css.cgroup;
982 unsigned int swappiness;
983
984 /* root ? */
985 if (cgrp->parent == NULL)
986 return vm_swappiness;
987
988 spin_lock(&memcg->reclaim_param_lock);
989 swappiness = memcg->swappiness;
990 spin_unlock(&memcg->reclaim_param_lock);
991
992 return swappiness;
993}
994
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700995static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
996{
997 int *val = data;
998 (*val)++;
999 return 0;
1000}
Balbir Singhe2224322009-04-02 16:57:39 -07001001
1002/**
1003 * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode.
1004 * @memcg: The memory cgroup that went over limit
1005 * @p: Task that is going to be killed
1006 *
1007 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1008 * enabled
1009 */
1010void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1011{
1012 struct cgroup *task_cgrp;
1013 struct cgroup *mem_cgrp;
1014 /*
1015 * Need a buffer in BSS, can't rely on allocations. The code relies
1016 * on the assumption that OOM is serialized for memory controller.
1017 * If this assumption is broken, revisit this code.
1018 */
1019 static char memcg_name[PATH_MAX];
1020 int ret;
1021
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001022 if (!memcg || !p)
Balbir Singhe2224322009-04-02 16:57:39 -07001023 return;
1024
1025
1026 rcu_read_lock();
1027
1028 mem_cgrp = memcg->css.cgroup;
1029 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1030
1031 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1032 if (ret < 0) {
1033 /*
1034 * Unfortunately, we are unable to convert to a useful name
1035 * But we'll still print out the usage information
1036 */
1037 rcu_read_unlock();
1038 goto done;
1039 }
1040 rcu_read_unlock();
1041
1042 printk(KERN_INFO "Task in %s killed", memcg_name);
1043
1044 rcu_read_lock();
1045 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1046 if (ret < 0) {
1047 rcu_read_unlock();
1048 goto done;
1049 }
1050 rcu_read_unlock();
1051
1052 /*
1053 * Continues from above, so we don't need an KERN_ level
1054 */
1055 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1056done:
1057
1058 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1059 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1060 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1061 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1062 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1063 "failcnt %llu\n",
1064 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1065 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1066 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1067}
1068
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001069/*
1070 * This function returns the number of memcg under hierarchy tree. Returns
1071 * 1(self count) if no children.
1072 */
1073static int mem_cgroup_count_children(struct mem_cgroup *mem)
1074{
1075 int num = 0;
1076 mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
1077 return num;
1078}
1079
Balbir Singh6d61ef42009-01-07 18:08:06 -08001080/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001081 * Visit the first child (need not be the first child as per the ordering
1082 * of the cgroup list, since we track last_scanned_child) of @mem and use
1083 * that to reclaim free pages from.
1084 */
1085static struct mem_cgroup *
1086mem_cgroup_select_victim(struct mem_cgroup *root_mem)
1087{
1088 struct mem_cgroup *ret = NULL;
1089 struct cgroup_subsys_state *css;
1090 int nextid, found;
1091
1092 if (!root_mem->use_hierarchy) {
1093 css_get(&root_mem->css);
1094 ret = root_mem;
1095 }
1096
1097 while (!ret) {
1098 rcu_read_lock();
1099 nextid = root_mem->last_scanned_child + 1;
1100 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
1101 &found);
1102 if (css && css_tryget(css))
1103 ret = container_of(css, struct mem_cgroup, css);
1104
1105 rcu_read_unlock();
1106 /* Updates scanning parameter */
1107 spin_lock(&root_mem->reclaim_param_lock);
1108 if (!css) {
1109 /* this means start scan from ID:1 */
1110 root_mem->last_scanned_child = 0;
1111 } else
1112 root_mem->last_scanned_child = found;
1113 spin_unlock(&root_mem->reclaim_param_lock);
1114 }
1115
1116 return ret;
1117}
1118
1119/*
1120 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1121 * we reclaimed from, so that we don't end up penalizing one child extensively
1122 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001123 *
1124 * root_mem is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001125 *
1126 * We give up and return to the caller when we visit root_mem twice.
1127 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001128 *
1129 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001130 */
1131static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
Balbir Singh4e416952009-09-23 15:56:39 -07001132 struct zone *zone,
Balbir Singh75822b42009-09-23 15:56:38 -07001133 gfp_t gfp_mask,
1134 unsigned long reclaim_options)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001135{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001136 struct mem_cgroup *victim;
1137 int ret, total = 0;
1138 int loop = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001139 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1140 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
Balbir Singh4e416952009-09-23 15:56:39 -07001141 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
1142 unsigned long excess = mem_cgroup_get_excess(root_mem);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001143
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001144 /* If memsw_is_minimum==1, swap-out is of-no-use. */
1145 if (root_mem->memsw_is_minimum)
1146 noswap = true;
1147
Balbir Singh4e416952009-09-23 15:56:39 -07001148 while (1) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001149 victim = mem_cgroup_select_victim(root_mem);
Balbir Singh4e416952009-09-23 15:56:39 -07001150 if (victim == root_mem) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001151 loop++;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001152 if (loop >= 1)
1153 drain_all_stock_async();
Balbir Singh4e416952009-09-23 15:56:39 -07001154 if (loop >= 2) {
1155 /*
1156 * If we have not been able to reclaim
1157 * anything, it might because there are
1158 * no reclaimable pages under this hierarchy
1159 */
1160 if (!check_soft || !total) {
1161 css_put(&victim->css);
1162 break;
1163 }
1164 /*
1165 * We want to do more targetted reclaim.
1166 * excess >> 2 is not to excessive so as to
1167 * reclaim too much, nor too less that we keep
1168 * coming back to reclaim from this cgroup
1169 */
1170 if (total >= (excess >> 2) ||
1171 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1172 css_put(&victim->css);
1173 break;
1174 }
1175 }
1176 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001177 if (!mem_cgroup_local_usage(&victim->stat)) {
1178 /* this cgroup's local usage == 0 */
1179 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001180 continue;
1181 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001182 /* we use swappiness of local cgroup */
Balbir Singh4e416952009-09-23 15:56:39 -07001183 if (check_soft)
1184 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
1185 noswap, get_swappiness(victim), zone,
1186 zone->zone_pgdat->node_id);
1187 else
1188 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
1189 noswap, get_swappiness(victim));
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001190 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001191 /*
1192 * At shrinking usage, we can't check we should stop here or
1193 * reclaim more. It's depends on callers. last_scanned_child
1194 * will work enough for keeping fairness under tree.
1195 */
1196 if (shrink)
1197 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001198 total += ret;
Balbir Singh4e416952009-09-23 15:56:39 -07001199 if (check_soft) {
1200 if (res_counter_check_under_soft_limit(&root_mem->res))
1201 return total;
1202 } else if (mem_cgroup_check_under_limit(root_mem))
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001203 return 1 + total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001204 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001205 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001206}
1207
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001208bool mem_cgroup_oom_called(struct task_struct *task)
1209{
1210 bool ret = false;
1211 struct mem_cgroup *mem;
1212 struct mm_struct *mm;
1213
1214 rcu_read_lock();
1215 mm = task->mm;
1216 if (!mm)
1217 mm = &init_mm;
1218 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
1219 if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
1220 ret = true;
1221 rcu_read_unlock();
1222 return ret;
1223}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001224
1225static int record_last_oom_cb(struct mem_cgroup *mem, void *data)
1226{
1227 mem->last_oom_jiffies = jiffies;
1228 return 0;
1229}
1230
1231static void record_last_oom(struct mem_cgroup *mem)
1232{
1233 mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb);
1234}
1235
Balbir Singhd69b0422009-06-17 16:26:34 -07001236/*
1237 * Currently used to update mapped file statistics, but the routine can be
1238 * generalized to update other statistics as well.
1239 */
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001240void mem_cgroup_update_file_mapped(struct page *page, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07001241{
1242 struct mem_cgroup *mem;
1243 struct mem_cgroup_stat *stat;
1244 struct mem_cgroup_stat_cpu *cpustat;
1245 int cpu;
1246 struct page_cgroup *pc;
1247
Balbir Singhd69b0422009-06-17 16:26:34 -07001248 pc = lookup_page_cgroup(page);
1249 if (unlikely(!pc))
1250 return;
1251
1252 lock_page_cgroup(pc);
1253 mem = pc->mem_cgroup;
1254 if (!mem)
1255 goto done;
1256
1257 if (!PageCgroupUsed(pc))
1258 goto done;
1259
1260 /*
1261 * Preemption is already disabled, we don't need get_cpu()
1262 */
1263 cpu = smp_processor_id();
1264 stat = &mem->stat;
1265 cpustat = &stat->cpustat[cpu];
1266
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001267 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED, val);
Balbir Singhd69b0422009-06-17 16:26:34 -07001268done:
1269 unlock_page_cgroup(pc);
1270}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001271
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001272/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001273 * size of first charge trial. "32" comes from vmscan.c's magic value.
1274 * TODO: maybe necessary to use big numbers in big irons.
1275 */
1276#define CHARGE_SIZE (32 * PAGE_SIZE)
1277struct memcg_stock_pcp {
1278 struct mem_cgroup *cached; /* this never be root cgroup */
1279 int charge;
1280 struct work_struct work;
1281};
1282static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
1283static atomic_t memcg_drain_count;
1284
1285/*
1286 * Try to consume stocked charge on this cpu. If success, PAGE_SIZE is consumed
1287 * from local stock and true is returned. If the stock is 0 or charges from a
1288 * cgroup which is not current target, returns false. This stock will be
1289 * refilled.
1290 */
1291static bool consume_stock(struct mem_cgroup *mem)
1292{
1293 struct memcg_stock_pcp *stock;
1294 bool ret = true;
1295
1296 stock = &get_cpu_var(memcg_stock);
1297 if (mem == stock->cached && stock->charge)
1298 stock->charge -= PAGE_SIZE;
1299 else /* need to call res_counter_charge */
1300 ret = false;
1301 put_cpu_var(memcg_stock);
1302 return ret;
1303}
1304
1305/*
1306 * Returns stocks cached in percpu to res_counter and reset cached information.
1307 */
1308static void drain_stock(struct memcg_stock_pcp *stock)
1309{
1310 struct mem_cgroup *old = stock->cached;
1311
1312 if (stock->charge) {
1313 res_counter_uncharge(&old->res, stock->charge);
1314 if (do_swap_account)
1315 res_counter_uncharge(&old->memsw, stock->charge);
1316 }
1317 stock->cached = NULL;
1318 stock->charge = 0;
1319}
1320
1321/*
1322 * This must be called under preempt disabled or must be called by
1323 * a thread which is pinned to local cpu.
1324 */
1325static void drain_local_stock(struct work_struct *dummy)
1326{
1327 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
1328 drain_stock(stock);
1329}
1330
1331/*
1332 * Cache charges(val) which is from res_counter, to local per_cpu area.
1333 * This will be consumed by consumt_stock() function, later.
1334 */
1335static void refill_stock(struct mem_cgroup *mem, int val)
1336{
1337 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
1338
1339 if (stock->cached != mem) { /* reset if necessary */
1340 drain_stock(stock);
1341 stock->cached = mem;
1342 }
1343 stock->charge += val;
1344 put_cpu_var(memcg_stock);
1345}
1346
1347/*
1348 * Tries to drain stocked charges in other cpus. This function is asynchronous
1349 * and just put a work per cpu for draining localy on each cpu. Caller can
1350 * expects some charges will be back to res_counter later but cannot wait for
1351 * it.
1352 */
1353static void drain_all_stock_async(void)
1354{
1355 int cpu;
1356 /* This function is for scheduling "drain" in asynchronous way.
1357 * The result of "drain" is not directly handled by callers. Then,
1358 * if someone is calling drain, we don't have to call drain more.
1359 * Anyway, WORK_STRUCT_PENDING check in queue_work_on() will catch if
1360 * there is a race. We just do loose check here.
1361 */
1362 if (atomic_read(&memcg_drain_count))
1363 return;
1364 /* Notify other cpus that system-wide "drain" is running */
1365 atomic_inc(&memcg_drain_count);
1366 get_online_cpus();
1367 for_each_online_cpu(cpu) {
1368 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
1369 schedule_work_on(cpu, &stock->work);
1370 }
1371 put_online_cpus();
1372 atomic_dec(&memcg_drain_count);
1373 /* We don't wait for flush_work */
1374}
1375
1376/* This is a synchronous drain interface. */
1377static void drain_all_stock_sync(void)
1378{
1379 /* called when force_empty is called */
1380 atomic_inc(&memcg_drain_count);
1381 schedule_on_each_cpu(drain_local_stock);
1382 atomic_dec(&memcg_drain_count);
1383}
1384
1385static int __cpuinit memcg_stock_cpu_callback(struct notifier_block *nb,
1386 unsigned long action,
1387 void *hcpu)
1388{
1389 int cpu = (unsigned long)hcpu;
1390 struct memcg_stock_pcp *stock;
1391
1392 if (action != CPU_DEAD)
1393 return NOTIFY_OK;
1394 stock = &per_cpu(memcg_stock, cpu);
1395 drain_stock(stock);
1396 return NOTIFY_OK;
1397}
1398
1399/*
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001400 * Unlike exported interface, "oom" parameter is added. if oom==true,
1401 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001402 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001403static int __mem_cgroup_try_charge(struct mm_struct *mm,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001404 gfp_t gfp_mask, struct mem_cgroup **memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -07001405 bool oom, struct page *page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001406{
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001407 struct mem_cgroup *mem, *mem_over_limit;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001408 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001409 struct res_counter *fail_res;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001410 int csize = CHARGE_SIZE;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001411
1412 if (unlikely(test_thread_flag(TIF_MEMDIE))) {
1413 /* Don't account this! */
1414 *memcg = NULL;
1415 return 0;
1416 }
1417
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001418 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08001419 * We always charge the cgroup the mm_struct belongs to.
1420 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001421 * thread group leader migrates. It's possible that mm is not
1422 * set, if so charge the init_mm (happens for pagecache usage).
1423 */
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001424 mem = *memcg;
1425 if (likely(!mem)) {
1426 mem = try_get_mem_cgroup_from_mm(mm);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001427 *memcg = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001428 } else {
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001429 css_get(&mem->css);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001430 }
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001431 if (unlikely(!mem))
1432 return 0;
1433
Nikanth Karthikesan46f7e602009-05-28 14:34:41 -07001434 VM_BUG_ON(css_is_removed(&mem->css));
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001435 if (mem_cgroup_is_root(mem))
1436 goto done;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001437
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001438 while (1) {
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001439 int ret = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001440 unsigned long flags = 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001441
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001442 if (consume_stock(mem))
1443 goto charged;
1444
1445 ret = res_counter_charge(&mem->res, csize, &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001446 if (likely(!ret)) {
1447 if (!do_swap_account)
1448 break;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001449 ret = res_counter_charge(&mem->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001450 if (likely(!ret))
1451 break;
1452 /* mem+swap counter fails */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001453 res_counter_uncharge(&mem->res, csize);
Balbir Singh75822b42009-09-23 15:56:38 -07001454 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001455 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1456 memsw);
1457 } else
1458 /* mem counter fails */
1459 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1460 res);
1461
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001462 /* reduce request size and retry */
1463 if (csize > PAGE_SIZE) {
1464 csize = PAGE_SIZE;
1465 continue;
1466 }
Hugh Dickins3be91272008-02-07 00:14:19 -08001467 if (!(gfp_mask & __GFP_WAIT))
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001468 goto nomem;
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001469
Balbir Singh4e416952009-09-23 15:56:39 -07001470 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
1471 gfp_mask, flags);
Daisuke Nishimura4d1c6272009-01-15 13:51:14 -08001472 if (ret)
1473 continue;
Balbir Singh66e17072008-02-07 00:13:56 -08001474
1475 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001476 * try_to_free_mem_cgroup_pages() might not give us a full
1477 * picture of reclaim. Some pages are reclaimed and might be
1478 * moved to swap cache or just unmapped from the cgroup.
1479 * Check the limit again to see if the reclaim reduced the
1480 * current usage of the cgroup before giving up
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001481 *
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001482 */
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -08001483 if (mem_cgroup_check_under_limit(mem_over_limit))
1484 continue;
Hugh Dickins3be91272008-02-07 00:14:19 -08001485
1486 if (!nr_retries--) {
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001487 if (oom) {
KAMEZAWA Hiroyuki88700752009-01-07 18:08:09 -08001488 mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001489 record_last_oom(mem_over_limit);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001490 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001491 goto nomem;
Balbir Singh66e17072008-02-07 00:13:56 -08001492 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001493 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001494 if (csize > PAGE_SIZE)
1495 refill_stock(mem, csize - PAGE_SIZE);
1496charged:
Balbir Singhf64c3f52009-09-23 15:56:37 -07001497 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001498 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
1499 * if they exceeds softlimit.
Balbir Singhf64c3f52009-09-23 15:56:37 -07001500 */
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001501 if (mem_cgroup_soft_limit_check(mem))
1502 mem_cgroup_update_tree(mem, page);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001503done:
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001504 return 0;
1505nomem:
1506 css_put(&mem->css);
1507 return -ENOMEM;
1508}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001509
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001510/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001511 * Somemtimes we have to undo a charge we got by try_charge().
1512 * This function is for that and do uncharge, put css's refcnt.
1513 * gotten by try_charge().
1514 */
1515static void mem_cgroup_cancel_charge(struct mem_cgroup *mem)
1516{
1517 if (!mem_cgroup_is_root(mem)) {
1518 res_counter_uncharge(&mem->res, PAGE_SIZE);
1519 if (do_swap_account)
1520 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1521 }
1522 css_put(&mem->css);
1523}
1524
1525/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001526 * A helper function to get mem_cgroup from ID. must be called under
1527 * rcu_read_lock(). The caller must check css_is_removed() or some if
1528 * it's concern. (dropping refcnt from swap can be called against removed
1529 * memcg.)
1530 */
1531static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
1532{
1533 struct cgroup_subsys_state *css;
1534
1535 /* ID 0 is unused ID */
1536 if (!id)
1537 return NULL;
1538 css = css_lookup(&mem_cgroup_subsys, id);
1539 if (!css)
1540 return NULL;
1541 return container_of(css, struct mem_cgroup, css);
1542}
1543
Wu Fengguange42d9d52009-12-16 12:19:59 +01001544struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001545{
Wu Fengguange42d9d52009-12-16 12:19:59 +01001546 struct mem_cgroup *mem = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001547 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001548 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001549 swp_entry_t ent;
1550
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001551 VM_BUG_ON(!PageLocked(page));
1552
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001553 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001554 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001555 if (PageCgroupUsed(pc)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001556 mem = pc->mem_cgroup;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001557 if (mem && !css_tryget(&mem->css))
1558 mem = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01001559 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001560 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001561 id = lookup_swap_cgroup(ent);
1562 rcu_read_lock();
1563 mem = mem_cgroup_lookup(id);
1564 if (mem && !css_tryget(&mem->css))
1565 mem = NULL;
1566 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001567 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001568 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001569 return mem;
1570}
1571
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001572/*
Daisuke Nishimuraa5e924f2009-01-07 18:08:28 -08001573 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001574 * USED state. If already USED, uncharge and return.
1575 */
1576
1577static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1578 struct page_cgroup *pc,
1579 enum charge_type ctype)
1580{
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001581 /* try_charge() can return NULL to *memcg, taking care of it. */
1582 if (!mem)
1583 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001584
1585 lock_page_cgroup(pc);
1586 if (unlikely(PageCgroupUsed(pc))) {
1587 unlock_page_cgroup(pc);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001588 mem_cgroup_cancel_charge(mem);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001589 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001590 }
Balbir Singh4b3bde42009-09-23 15:56:32 -07001591
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001592 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07001593 /*
1594 * We access a page_cgroup asynchronously without lock_page_cgroup().
1595 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
1596 * is accessed after testing USED bit. To make pc->mem_cgroup visible
1597 * before USED bit, we need memory barrier here.
1598 * See mem_cgroup_add_lru_list(), etc.
1599 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001600 smp_wmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -07001601 switch (ctype) {
1602 case MEM_CGROUP_CHARGE_TYPE_CACHE:
1603 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
1604 SetPageCgroupCache(pc);
1605 SetPageCgroupUsed(pc);
1606 break;
1607 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1608 ClearPageCgroupCache(pc);
1609 SetPageCgroupUsed(pc);
1610 break;
1611 default:
1612 break;
1613 }
Hugh Dickins3be91272008-02-07 00:14:19 -08001614
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001615 mem_cgroup_charge_statistics(mem, pc, true);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001616
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001617 unlock_page_cgroup(pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001618}
1619
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001620/**
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001621 * __mem_cgroup_move_account - move account of the page
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001622 * @pc: page_cgroup of the page.
1623 * @from: mem_cgroup which the page is moved from.
1624 * @to: mem_cgroup which the page is moved to. @from != @to.
1625 *
1626 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001627 * - page is not on LRU (isolate_page() is useful.)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001628 * - the pc is locked, used, and ->mem_cgroup points to @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001629 *
1630 * This function does "uncharge" from old cgroup but doesn't do "charge" to
1631 * new cgroup. It should be done by a caller.
1632 */
1633
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001634static void __mem_cgroup_move_account(struct page_cgroup *pc,
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001635 struct mem_cgroup *from, struct mem_cgroup *to)
1636{
Balbir Singhd69b0422009-06-17 16:26:34 -07001637 struct page *page;
1638 int cpu;
1639 struct mem_cgroup_stat *stat;
1640 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001641
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001642 VM_BUG_ON(from == to);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001643 VM_BUG_ON(PageLRU(pc->page));
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001644 VM_BUG_ON(!PageCgroupLocked(pc));
1645 VM_BUG_ON(!PageCgroupUsed(pc));
1646 VM_BUG_ON(pc->mem_cgroup != from);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001647
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001648 if (!mem_cgroup_is_root(from))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001649 res_counter_uncharge(&from->res, PAGE_SIZE);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001650 mem_cgroup_charge_statistics(from, pc, false);
Balbir Singhd69b0422009-06-17 16:26:34 -07001651
1652 page = pc->page;
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001653 if (page_mapped(page) && !PageAnon(page)) {
Balbir Singhd69b0422009-06-17 16:26:34 -07001654 cpu = smp_processor_id();
1655 /* Update mapped_file data for mem_cgroup "from" */
1656 stat = &from->stat;
1657 cpustat = &stat->cpustat[cpu];
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001658 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED,
Balbir Singhd69b0422009-06-17 16:26:34 -07001659 -1);
1660
1661 /* Update mapped_file data for mem_cgroup "to" */
1662 stat = &to->stat;
1663 cpustat = &stat->cpustat[cpu];
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001664 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED,
Balbir Singhd69b0422009-06-17 16:26:34 -07001665 1);
1666 }
1667
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001668 if (do_swap_account && !mem_cgroup_is_root(from))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001669 res_counter_uncharge(&from->memsw, PAGE_SIZE);
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001670 css_put(&from->css);
1671
1672 css_get(&to->css);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001673 pc->mem_cgroup = to;
1674 mem_cgroup_charge_statistics(to, pc, true);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001675 /*
1676 * We charges against "to" which may not have any tasks. Then, "to"
1677 * can be under rmdir(). But in current implementation, caller of
1678 * this function is just force_empty() and it's garanteed that
1679 * "to" is never removed. So, we don't check rmdir status here.
1680 */
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001681}
1682
1683/*
1684 * check whether the @pc is valid for moving account and call
1685 * __mem_cgroup_move_account()
1686 */
1687static int mem_cgroup_move_account(struct page_cgroup *pc,
1688 struct mem_cgroup *from, struct mem_cgroup *to)
1689{
1690 int ret = -EINVAL;
1691 lock_page_cgroup(pc);
1692 if (PageCgroupUsed(pc) && pc->mem_cgroup == from) {
1693 __mem_cgroup_move_account(pc, from, to);
1694 ret = 0;
1695 }
1696 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001697 return ret;
1698}
1699
1700/*
1701 * move charges to its parent.
1702 */
1703
1704static int mem_cgroup_move_parent(struct page_cgroup *pc,
1705 struct mem_cgroup *child,
1706 gfp_t gfp_mask)
1707{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001708 struct page *page = pc->page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001709 struct cgroup *cg = child->css.cgroup;
1710 struct cgroup *pcg = cg->parent;
1711 struct mem_cgroup *parent;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001712 int ret;
1713
1714 /* Is ROOT ? */
1715 if (!pcg)
1716 return -EINVAL;
1717
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001718 ret = -EBUSY;
1719 if (!get_page_unless_zero(page))
1720 goto out;
1721 if (isolate_lru_page(page))
1722 goto put;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001723
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001724 parent = mem_cgroup_from_cont(pcg);
Balbir Singhf64c3f52009-09-23 15:56:37 -07001725 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false, page);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001726 if (ret || !parent)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001727 goto put_back;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001728
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001729 ret = mem_cgroup_move_account(pc, child, parent);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001730 if (!ret)
1731 css_put(&parent->css); /* drop extra refcnt by try_charge() */
1732 else
1733 mem_cgroup_cancel_charge(parent); /* does css_put */
1734put_back:
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001735 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001736put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001737 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001738out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001739 return ret;
1740}
1741
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001742/*
1743 * Charge the memory controller for page usage.
1744 * Return
1745 * 0 if the charge was successful
1746 * < 0 if the cgroup is over its limit
1747 */
1748static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
1749 gfp_t gfp_mask, enum charge_type ctype,
1750 struct mem_cgroup *memcg)
1751{
1752 struct mem_cgroup *mem;
1753 struct page_cgroup *pc;
1754 int ret;
1755
1756 pc = lookup_page_cgroup(page);
1757 /* can happen at boot */
1758 if (unlikely(!pc))
1759 return 0;
1760 prefetchw(pc);
1761
1762 mem = memcg;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001763 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true, page);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001764 if (ret || !mem)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001765 return ret;
1766
1767 __mem_cgroup_commit_charge(mem, pc, ctype);
1768 return 0;
1769}
1770
1771int mem_cgroup_newpage_charge(struct page *page,
1772 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001773{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001774 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001775 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001776 if (PageCompound(page))
1777 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001778 /*
1779 * If already mapped, we don't have to account.
1780 * If page cache, page->mapping has address_space.
1781 * But page->mapping may have out-of-use anon_vma pointer,
1782 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
1783 * is NULL.
1784 */
1785 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
1786 return 0;
1787 if (unlikely(!mm))
1788 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001789 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001790 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001791}
1792
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001793static void
1794__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1795 enum charge_type ctype);
1796
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001797int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
1798 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08001799{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001800 struct mem_cgroup *mem = NULL;
1801 int ret;
1802
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001803 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001804 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001805 if (PageCompound(page))
1806 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001807 /*
1808 * Corner case handling. This is called from add_to_page_cache()
1809 * in usual. But some FS (shmem) precharges this page before calling it
1810 * and call add_to_page_cache() with GFP_NOWAIT.
1811 *
1812 * For GFP_NOWAIT case, the page may be pre-charged before calling
1813 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
1814 * charge twice. (It works but has to pay a bit larger cost.)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001815 * And when the page is SwapCache, it should take swap information
1816 * into account. This is under lock_page() now.
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001817 */
1818 if (!(gfp_mask & __GFP_WAIT)) {
1819 struct page_cgroup *pc;
1820
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001821
1822 pc = lookup_page_cgroup(page);
1823 if (!pc)
1824 return 0;
1825 lock_page_cgroup(pc);
1826 if (PageCgroupUsed(pc)) {
1827 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001828 return 0;
1829 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001830 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001831 }
1832
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001833 if (unlikely(!mm && !mem))
Balbir Singh8697d332008-02-07 00:13:59 -08001834 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001835
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -07001836 if (page_is_file_cache(page))
1837 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001838 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001839
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001840 /* shmem */
1841 if (PageSwapCache(page)) {
1842 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1843 if (!ret)
1844 __mem_cgroup_commit_charge_swapin(page, mem,
1845 MEM_CGROUP_CHARGE_TYPE_SHMEM);
1846 } else
1847 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
1848 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001849
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001850 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001851}
1852
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001853/*
1854 * While swap-in, try_charge -> commit or cancel, the page is locked.
1855 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02001856 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001857 * "commit()" or removed by "cancel()"
1858 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001859int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
1860 struct page *page,
1861 gfp_t mask, struct mem_cgroup **ptr)
1862{
1863 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001864 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001865
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001866 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001867 return 0;
1868
1869 if (!do_swap_account)
1870 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001871 /*
1872 * A racing thread's fault, or swapoff, may have already updated
Hugh Dickins407f9c82009-12-14 17:59:30 -08001873 * the pte, and even removed page from swap cache: in those cases
1874 * do_swap_page()'s pte_same() test will fail; but there's also a
1875 * KSM case which does need to charge the page.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001876 */
1877 if (!PageSwapCache(page))
Hugh Dickins407f9c82009-12-14 17:59:30 -08001878 goto charge_cur_mm;
Wu Fengguange42d9d52009-12-16 12:19:59 +01001879 mem = try_get_mem_cgroup_from_page(page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001880 if (!mem)
1881 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001882 *ptr = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001883 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true, page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001884 /* drop extra refcnt from tryget */
1885 css_put(&mem->css);
1886 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001887charge_cur_mm:
1888 if (unlikely(!mm))
1889 mm = &init_mm;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001890 return __mem_cgroup_try_charge(mm, mask, ptr, true, page);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001891}
1892
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001893static void
1894__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1895 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001896{
1897 struct page_cgroup *pc;
1898
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001899 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001900 return;
1901 if (!ptr)
1902 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001903 cgroup_exclude_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001904 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001905 mem_cgroup_lru_del_before_commit_swapcache(page);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001906 __mem_cgroup_commit_charge(ptr, pc, ctype);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001907 mem_cgroup_lru_add_after_commit_swapcache(page);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001908 /*
1909 * Now swap is on-memory. This means this page may be
1910 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08001911 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
1912 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
1913 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001914 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08001915 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001916 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001917 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001918 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001919
1920 id = swap_cgroup_record(ent, 0);
1921 rcu_read_lock();
1922 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001923 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001924 /*
1925 * This recorded memcg can be obsolete one. So, avoid
1926 * calling css_tryget
1927 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001928 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001929 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001930 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001931 mem_cgroup_put(memcg);
1932 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001933 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001934 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001935 /*
1936 * At swapin, we may charge account against cgroup which has no tasks.
1937 * So, rmdir()->pre_destroy() can be called while we do this charge.
1938 * In that case, we need to call pre_destroy() again. check it here.
1939 */
1940 cgroup_release_and_wakeup_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001941}
1942
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001943void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
1944{
1945 __mem_cgroup_commit_charge_swapin(page, ptr,
1946 MEM_CGROUP_CHARGE_TYPE_MAPPED);
1947}
1948
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001949void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
1950{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001951 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001952 return;
1953 if (!mem)
1954 return;
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001955 mem_cgroup_cancel_charge(mem);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001956}
1957
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08001958static void
1959__do_uncharge(struct mem_cgroup *mem, const enum charge_type ctype)
1960{
1961 struct memcg_batch_info *batch = NULL;
1962 bool uncharge_memsw = true;
1963 /* If swapout, usage of swap doesn't decrease */
1964 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1965 uncharge_memsw = false;
1966 /*
1967 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
1968 * In those cases, all pages freed continously can be expected to be in
1969 * the same cgroup and we have chance to coalesce uncharges.
1970 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
1971 * because we want to do uncharge as soon as possible.
1972 */
1973 if (!current->memcg_batch.do_batch || test_thread_flag(TIF_MEMDIE))
1974 goto direct_uncharge;
1975
1976 batch = &current->memcg_batch;
1977 /*
1978 * In usual, we do css_get() when we remember memcg pointer.
1979 * But in this case, we keep res->usage until end of a series of
1980 * uncharges. Then, it's ok to ignore memcg's refcnt.
1981 */
1982 if (!batch->memcg)
1983 batch->memcg = mem;
1984 /*
1985 * In typical case, batch->memcg == mem. This means we can
1986 * merge a series of uncharges to an uncharge of res_counter.
1987 * If not, we uncharge res_counter ony by one.
1988 */
1989 if (batch->memcg != mem)
1990 goto direct_uncharge;
1991 /* remember freed charge and uncharge it later */
1992 batch->bytes += PAGE_SIZE;
1993 if (uncharge_memsw)
1994 batch->memsw_bytes += PAGE_SIZE;
1995 return;
1996direct_uncharge:
1997 res_counter_uncharge(&mem->res, PAGE_SIZE);
1998 if (uncharge_memsw)
1999 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
2000 return;
2001}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002002
Balbir Singh8697d332008-02-07 00:13:59 -08002003/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002004 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002005 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002006static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002007__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002008{
Hugh Dickins82895462008-03-04 14:29:08 -08002009 struct page_cgroup *pc;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002010 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002011 struct mem_cgroup_per_zone *mz;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002012
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002013 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002014 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07002015
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002016 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002017 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002018
Balbir Singh8697d332008-02-07 00:13:59 -08002019 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08002020 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08002021 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002022 pc = lookup_page_cgroup(page);
2023 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002024 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002025
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002026 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002027
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002028 mem = pc->mem_cgroup;
2029
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002030 if (!PageCgroupUsed(pc))
2031 goto unlock_out;
2032
2033 switch (ctype) {
2034 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002035 case MEM_CGROUP_CHARGE_TYPE_DROP:
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002036 if (page_mapped(page))
2037 goto unlock_out;
2038 break;
2039 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
2040 if (!PageAnon(page)) { /* Shared memory */
2041 if (page->mapping && !page_is_file_cache(page))
2042 goto unlock_out;
2043 } else if (page_mapped(page)) /* Anon */
2044 goto unlock_out;
2045 break;
2046 default:
2047 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002048 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002049
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002050 if (!mem_cgroup_is_root(mem))
2051 __do_uncharge(mem, ctype);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002052 if (ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2053 mem_cgroup_swap_statistics(mem, true);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002054 mem_cgroup_charge_statistics(mem, pc, false);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002055
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002056 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08002057 /*
2058 * pc->mem_cgroup is not cleared here. It will be accessed when it's
2059 * freed from LRU. This is safe because uncharged page is expected not
2060 * to be reused (freed soon). Exception is SwapCache, it's handled by
2061 * special functions.
2062 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08002063
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002064 mz = page_cgroup_zoneinfo(pc);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002065 unlock_page_cgroup(pc);
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08002066
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002067 if (mem_cgroup_soft_limit_check(mem))
Balbir Singhf64c3f52009-09-23 15:56:37 -07002068 mem_cgroup_update_tree(mem, page);
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08002069 /* at swapout, this memcg will be accessed to record to swap */
2070 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2071 css_put(&mem->css);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002072
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002073 return mem;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002074
2075unlock_out:
2076 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002077 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08002078}
2079
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002080void mem_cgroup_uncharge_page(struct page *page)
2081{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002082 /* early check. */
2083 if (page_mapped(page))
2084 return;
2085 if (page->mapping && !PageAnon(page))
2086 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002087 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
2088}
2089
2090void mem_cgroup_uncharge_cache_page(struct page *page)
2091{
2092 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07002093 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002094 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
2095}
2096
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002097/*
2098 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
2099 * In that cases, pages are freed continuously and we can expect pages
2100 * are in the same memcg. All these calls itself limits the number of
2101 * pages freed at once, then uncharge_start/end() is called properly.
2102 * This may be called prural(2) times in a context,
2103 */
2104
2105void mem_cgroup_uncharge_start(void)
2106{
2107 current->memcg_batch.do_batch++;
2108 /* We can do nest. */
2109 if (current->memcg_batch.do_batch == 1) {
2110 current->memcg_batch.memcg = NULL;
2111 current->memcg_batch.bytes = 0;
2112 current->memcg_batch.memsw_bytes = 0;
2113 }
2114}
2115
2116void mem_cgroup_uncharge_end(void)
2117{
2118 struct memcg_batch_info *batch = &current->memcg_batch;
2119
2120 if (!batch->do_batch)
2121 return;
2122
2123 batch->do_batch--;
2124 if (batch->do_batch) /* If stacked, do nothing. */
2125 return;
2126
2127 if (!batch->memcg)
2128 return;
2129 /*
2130 * This "batch->memcg" is valid without any css_get/put etc...
2131 * bacause we hide charges behind us.
2132 */
2133 if (batch->bytes)
2134 res_counter_uncharge(&batch->memcg->res, batch->bytes);
2135 if (batch->memsw_bytes)
2136 res_counter_uncharge(&batch->memcg->memsw, batch->memsw_bytes);
2137 /* forget this pointer (for sanity check) */
2138 batch->memcg = NULL;
2139}
2140
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002141#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002142/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002143 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002144 * memcg information is recorded to swap_cgroup of "ent"
2145 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002146void
2147mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002148{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002149 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002150 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002151
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002152 if (!swapout) /* this was a swap cache but the swap is unused ! */
2153 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
2154
2155 memcg = __mem_cgroup_uncharge_common(page, ctype);
2156
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002157 /* record memcg information */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002158 if (do_swap_account && swapout && memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002159 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002160 mem_cgroup_get(memcg);
2161 }
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002162 if (swapout && memcg)
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08002163 css_put(&memcg->css);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002164}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002165#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002166
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002167#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2168/*
2169 * called from swap_entry_free(). remove record in swap_cgroup and
2170 * uncharge "memsw" account.
2171 */
2172void mem_cgroup_uncharge_swap(swp_entry_t ent)
2173{
2174 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002175 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002176
2177 if (!do_swap_account)
2178 return;
2179
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002180 id = swap_cgroup_record(ent, 0);
2181 rcu_read_lock();
2182 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002183 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002184 /*
2185 * We uncharge this because swap is freed.
2186 * This memcg can be obsolete one. We avoid calling css_tryget
2187 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002188 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002189 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002190 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002191 mem_cgroup_put(memcg);
2192 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002193 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002194}
2195#endif
2196
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002197/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002198 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
2199 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002200 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002201int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002202{
2203 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002204 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002205 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08002206
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002207 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07002208 return 0;
2209
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002210 pc = lookup_page_cgroup(page);
2211 lock_page_cgroup(pc);
2212 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002213 mem = pc->mem_cgroup;
2214 css_get(&mem->css);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08002215 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002216 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002217
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002218 if (mem) {
Balbir Singhf64c3f52009-09-23 15:56:37 -07002219 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false,
2220 page);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002221 css_put(&mem->css);
2222 }
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002223 *ptr = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002224 return ret;
2225}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08002226
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002227/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002228void mem_cgroup_end_migration(struct mem_cgroup *mem,
2229 struct page *oldpage, struct page *newpage)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002230{
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002231 struct page *target, *unused;
2232 struct page_cgroup *pc;
2233 enum charge_type ctype;
2234
2235 if (!mem)
2236 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002237 cgroup_exclude_rmdir(&mem->css);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002238 /* at migration success, oldpage->mapping is NULL. */
2239 if (oldpage->mapping) {
2240 target = oldpage;
2241 unused = NULL;
2242 } else {
2243 target = newpage;
2244 unused = oldpage;
2245 }
2246
2247 if (PageAnon(target))
2248 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
2249 else if (page_is_file_cache(target))
2250 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
2251 else
2252 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
2253
2254 /* unused page is not on radix-tree now. */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002255 if (unused)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002256 __mem_cgroup_uncharge_common(unused, ctype);
2257
2258 pc = lookup_page_cgroup(target);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002259 /*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002260 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
2261 * So, double-counting is effectively avoided.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002262 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002263 __mem_cgroup_commit_charge(mem, pc, ctype);
2264
2265 /*
2266 * Both of oldpage and newpage are still under lock_page().
2267 * Then, we don't have to care about race in radix-tree.
2268 * But we have to be careful that this page is unmapped or not.
2269 *
2270 * There is a case for !page_mapped(). At the start of
2271 * migration, oldpage was mapped. But now, it's zapped.
2272 * But we know *target* page is not freed/reused under us.
2273 * mem_cgroup_uncharge_page() does all necessary checks.
2274 */
2275 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
2276 mem_cgroup_uncharge_page(target);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002277 /*
2278 * At migration, we may charge account against cgroup which has no tasks
2279 * So, rmdir()->pre_destroy() can be called while we do this charge.
2280 * In that case, we need to call pre_destroy() again. check it here.
2281 */
2282 cgroup_release_and_wakeup_rmdir(&mem->css);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002283}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08002284
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002285/*
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002286 * A call to try to shrink memory usage on charge failure at shmem's swapin.
2287 * Calling hierarchical_reclaim is not enough because we should update
2288 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
2289 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
2290 * not from the memcg which this page would be charged to.
2291 * try_charge_swapin does all of these works properly.
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002292 */
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002293int mem_cgroup_shmem_charge_fallback(struct page *page,
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002294 struct mm_struct *mm,
2295 gfp_t gfp_mask)
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002296{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002297 struct mem_cgroup *mem = NULL;
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002298 int ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002299
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002300 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002301 return 0;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002302
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002303 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2304 if (!ret)
2305 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002306
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002307 return ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002308}
2309
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002310static DEFINE_MUTEX(set_limit_mutex);
2311
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08002312static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002313 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002314{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002315 int retry_count;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002316 u64 memswlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002317 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002318 int children = mem_cgroup_count_children(memcg);
2319 u64 curusage, oldusage;
2320
2321 /*
2322 * For keeping hierarchical_reclaim simple, how long we should retry
2323 * is depends on callers. We set our retry-count to be function
2324 * of # of children which we should visit in this loop.
2325 */
2326 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
2327
2328 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002329
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002330 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002331 if (signal_pending(current)) {
2332 ret = -EINTR;
2333 break;
2334 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002335 /*
2336 * Rather than hide all in some function, I do this in
2337 * open coded manner. You see what this really does.
2338 * We have to guarantee mem->res.limit < mem->memsw.limit.
2339 */
2340 mutex_lock(&set_limit_mutex);
2341 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2342 if (memswlimit < val) {
2343 ret = -EINVAL;
2344 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002345 break;
2346 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002347 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07002348 if (!ret) {
2349 if (memswlimit == val)
2350 memcg->memsw_is_minimum = true;
2351 else
2352 memcg->memsw_is_minimum = false;
2353 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002354 mutex_unlock(&set_limit_mutex);
2355
2356 if (!ret)
2357 break;
2358
Bob Liuaa20d482009-12-15 16:47:14 -08002359 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh4e416952009-09-23 15:56:39 -07002360 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002361 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
2362 /* Usage is reduced ? */
2363 if (curusage >= oldusage)
2364 retry_count--;
2365 else
2366 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002367 }
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002368
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002369 return ret;
2370}
2371
Li Zefan338c8432009-06-17 16:27:15 -07002372static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
2373 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002374{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002375 int retry_count;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002376 u64 memlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002377 int children = mem_cgroup_count_children(memcg);
2378 int ret = -EBUSY;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002379
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002380 /* see mem_cgroup_resize_res_limit */
2381 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
2382 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002383 while (retry_count) {
2384 if (signal_pending(current)) {
2385 ret = -EINTR;
2386 break;
2387 }
2388 /*
2389 * Rather than hide all in some function, I do this in
2390 * open coded manner. You see what this really does.
2391 * We have to guarantee mem->res.limit < mem->memsw.limit.
2392 */
2393 mutex_lock(&set_limit_mutex);
2394 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2395 if (memlimit > val) {
2396 ret = -EINVAL;
2397 mutex_unlock(&set_limit_mutex);
2398 break;
2399 }
2400 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07002401 if (!ret) {
2402 if (memlimit == val)
2403 memcg->memsw_is_minimum = true;
2404 else
2405 memcg->memsw_is_minimum = false;
2406 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002407 mutex_unlock(&set_limit_mutex);
2408
2409 if (!ret)
2410 break;
2411
Balbir Singh4e416952009-09-23 15:56:39 -07002412 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh75822b42009-09-23 15:56:38 -07002413 MEM_CGROUP_RECLAIM_NOSWAP |
2414 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002415 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002416 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002417 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002418 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002419 else
2420 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002421 }
2422 return ret;
2423}
2424
Balbir Singh4e416952009-09-23 15:56:39 -07002425unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
2426 gfp_t gfp_mask, int nid,
2427 int zid)
2428{
2429 unsigned long nr_reclaimed = 0;
2430 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
2431 unsigned long reclaimed;
2432 int loop = 0;
2433 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002434 unsigned long long excess;
Balbir Singh4e416952009-09-23 15:56:39 -07002435
2436 if (order > 0)
2437 return 0;
2438
2439 mctz = soft_limit_tree_node_zone(nid, zid);
2440 /*
2441 * This loop can run a while, specially if mem_cgroup's continuously
2442 * keep exceeding their soft limit and putting the system under
2443 * pressure
2444 */
2445 do {
2446 if (next_mz)
2447 mz = next_mz;
2448 else
2449 mz = mem_cgroup_largest_soft_limit_node(mctz);
2450 if (!mz)
2451 break;
2452
2453 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
2454 gfp_mask,
2455 MEM_CGROUP_RECLAIM_SOFT);
2456 nr_reclaimed += reclaimed;
2457 spin_lock(&mctz->lock);
2458
2459 /*
2460 * If we failed to reclaim anything from this memory cgroup
2461 * it is time to move on to the next cgroup
2462 */
2463 next_mz = NULL;
2464 if (!reclaimed) {
2465 do {
2466 /*
2467 * Loop until we find yet another one.
2468 *
2469 * By the time we get the soft_limit lock
2470 * again, someone might have aded the
2471 * group back on the RB tree. Iterate to
2472 * make sure we get a different mem.
2473 * mem_cgroup_largest_soft_limit_node returns
2474 * NULL if no other cgroup is present on
2475 * the tree
2476 */
2477 next_mz =
2478 __mem_cgroup_largest_soft_limit_node(mctz);
2479 if (next_mz == mz) {
2480 css_put(&next_mz->mem->css);
2481 next_mz = NULL;
2482 } else /* next_mz == NULL or other memcg */
2483 break;
2484 } while (1);
2485 }
Balbir Singh4e416952009-09-23 15:56:39 -07002486 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002487 excess = res_counter_soft_limit_excess(&mz->mem->res);
Balbir Singh4e416952009-09-23 15:56:39 -07002488 /*
2489 * One school of thought says that we should not add
2490 * back the node to the tree if reclaim returns 0.
2491 * But our reclaim could return 0, simply because due
2492 * to priority we are exposing a smaller subset of
2493 * memory to reclaim from. Consider this as a longer
2494 * term TODO.
2495 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002496 /* If excess == 0, no tree ops */
2497 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07002498 spin_unlock(&mctz->lock);
2499 css_put(&mz->mem->css);
2500 loop++;
2501 /*
2502 * Could not reclaim anything and there are no more
2503 * mem cgroups to try or we seem to be looping without
2504 * reclaiming anything.
2505 */
2506 if (!nr_reclaimed &&
2507 (next_mz == NULL ||
2508 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
2509 break;
2510 } while (!nr_reclaimed);
2511 if (next_mz)
2512 css_put(&next_mz->mem->css);
2513 return nr_reclaimed;
2514}
2515
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002516/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002517 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002518 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
2519 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002520static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002521 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002522{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002523 struct zone *zone;
2524 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002525 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002526 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002527 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002528 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002529
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002530 zone = &NODE_DATA(node)->node_zones[zid];
2531 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07002532 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002533
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002534 loop = MEM_CGROUP_ZSTAT(mz, lru);
2535 /* give some margin against EBUSY etc...*/
2536 loop += 256;
2537 busy = NULL;
2538 while (loop--) {
2539 ret = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002540 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002541 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002542 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002543 break;
2544 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002545 pc = list_entry(list->prev, struct page_cgroup, lru);
2546 if (busy == pc) {
2547 list_move(&pc->lru, list);
2548 busy = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002549 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002550 continue;
2551 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002552 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002553
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -08002554 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002555 if (ret == -ENOMEM)
2556 break;
2557
2558 if (ret == -EBUSY || ret == -EINVAL) {
2559 /* found lock contention or "pc" is obsolete. */
2560 busy = pc;
2561 cond_resched();
2562 } else
2563 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002564 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002565
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002566 if (!ret && !list_empty(list))
2567 return -EBUSY;
2568 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002569}
2570
2571/*
2572 * make mem_cgroup's charge to be 0 if there is no task.
2573 * This enables deleting this mem_cgroup.
2574 */
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002575static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002576{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002577 int ret;
2578 int node, zid, shrink;
2579 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002580 struct cgroup *cgrp = mem->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08002581
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002582 css_get(&mem->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002583
2584 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002585 /* should free all ? */
2586 if (free_all)
2587 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002588move_account:
Daisuke Nishimurafce66472010-01-15 17:01:30 -08002589 do {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002590 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002591 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002592 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002593 ret = -EINTR;
2594 if (signal_pending(current))
2595 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002596 /* This is for making all *used* pages to be on LRU. */
2597 lru_add_drain_all();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002598 drain_all_stock_sync();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002599 ret = 0;
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08002600 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002601 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07002602 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002603 for_each_lru(l) {
2604 ret = mem_cgroup_force_empty_list(mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002605 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002606 if (ret)
2607 break;
2608 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002609 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002610 if (ret)
2611 break;
2612 }
2613 /* it seems parent cgroup doesn't have enough mem */
2614 if (ret == -ENOMEM)
2615 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002616 cond_resched();
Daisuke Nishimurafce66472010-01-15 17:01:30 -08002617 /* "ret" should also be checked to ensure all lists are empty. */
2618 } while (mem->res.usage > 0 || ret);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002619out:
2620 css_put(&mem->css);
2621 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002622
2623try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002624 /* returns EBUSY if there is a task or if we come here twice. */
2625 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002626 ret = -EBUSY;
2627 goto out;
2628 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002629 /* we call try-to-free pages for make this cgroup empty */
2630 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002631 /* try to free all pages in this cgroup */
2632 shrink = 1;
2633 while (nr_retries && mem->res.usage > 0) {
2634 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002635
2636 if (signal_pending(current)) {
2637 ret = -EINTR;
2638 goto out;
2639 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002640 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
2641 false, get_swappiness(mem));
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002642 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002643 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002644 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02002645 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002646 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002647
2648 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002649 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002650 /* try move_account...there may be some *locked* pages. */
Daisuke Nishimurafce66472010-01-15 17:01:30 -08002651 goto move_account;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002652}
2653
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002654int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
2655{
2656 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
2657}
2658
2659
Balbir Singh18f59ea2009-01-07 18:08:07 -08002660static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
2661{
2662 return mem_cgroup_from_cont(cont)->use_hierarchy;
2663}
2664
2665static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
2666 u64 val)
2667{
2668 int retval = 0;
2669 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2670 struct cgroup *parent = cont->parent;
2671 struct mem_cgroup *parent_mem = NULL;
2672
2673 if (parent)
2674 parent_mem = mem_cgroup_from_cont(parent);
2675
2676 cgroup_lock();
2677 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002678 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08002679 * in the child subtrees. If it is unset, then the change can
2680 * occur, provided the current cgroup has no children.
2681 *
2682 * For the root cgroup, parent_mem is NULL, we allow value to be
2683 * set if there are no children.
2684 */
2685 if ((!parent_mem || !parent_mem->use_hierarchy) &&
2686 (val == 1 || val == 0)) {
2687 if (list_empty(&cont->children))
2688 mem->use_hierarchy = val;
2689 else
2690 retval = -EBUSY;
2691 } else
2692 retval = -EINVAL;
2693 cgroup_unlock();
2694
2695 return retval;
2696}
2697
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002698struct mem_cgroup_idx_data {
2699 s64 val;
2700 enum mem_cgroup_stat_index idx;
2701};
2702
2703static int
2704mem_cgroup_get_idx_stat(struct mem_cgroup *mem, void *data)
2705{
2706 struct mem_cgroup_idx_data *d = data;
2707 d->val += mem_cgroup_read_stat(&mem->stat, d->idx);
2708 return 0;
2709}
2710
2711static void
2712mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem,
2713 enum mem_cgroup_stat_index idx, s64 *val)
2714{
2715 struct mem_cgroup_idx_data d;
2716 d.idx = idx;
2717 d.val = 0;
2718 mem_cgroup_walk_tree(mem, &d, mem_cgroup_get_idx_stat);
2719 *val = d.val;
2720}
2721
Paul Menage2c3daa72008-04-29 00:59:58 -07002722static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002723{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002724 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002725 u64 idx_val, val;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002726 int type, name;
2727
2728 type = MEMFILE_TYPE(cft->private);
2729 name = MEMFILE_ATTR(cft->private);
2730 switch (type) {
2731 case _MEM:
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002732 if (name == RES_USAGE && mem_cgroup_is_root(mem)) {
2733 mem_cgroup_get_recursive_idx_stat(mem,
2734 MEM_CGROUP_STAT_CACHE, &idx_val);
2735 val = idx_val;
2736 mem_cgroup_get_recursive_idx_stat(mem,
2737 MEM_CGROUP_STAT_RSS, &idx_val);
2738 val += idx_val;
2739 val <<= PAGE_SHIFT;
2740 } else
2741 val = res_counter_read_u64(&mem->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002742 break;
2743 case _MEMSWAP:
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002744 if (name == RES_USAGE && mem_cgroup_is_root(mem)) {
2745 mem_cgroup_get_recursive_idx_stat(mem,
2746 MEM_CGROUP_STAT_CACHE, &idx_val);
2747 val = idx_val;
2748 mem_cgroup_get_recursive_idx_stat(mem,
2749 MEM_CGROUP_STAT_RSS, &idx_val);
2750 val += idx_val;
2751 mem_cgroup_get_recursive_idx_stat(mem,
2752 MEM_CGROUP_STAT_SWAPOUT, &idx_val);
Kirill A. Shutemovcd9b45b2009-12-15 16:47:01 -08002753 val += idx_val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002754 val <<= PAGE_SHIFT;
2755 } else
2756 val = res_counter_read_u64(&mem->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002757 break;
2758 default:
2759 BUG();
2760 break;
2761 }
2762 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002763}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002764/*
2765 * The user of this function is...
2766 * RES_LIMIT.
2767 */
Paul Menage856c13a2008-07-25 01:47:04 -07002768static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
2769 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002770{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002771 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002772 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002773 unsigned long long val;
2774 int ret;
2775
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002776 type = MEMFILE_TYPE(cft->private);
2777 name = MEMFILE_ATTR(cft->private);
2778 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002779 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07002780 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
2781 ret = -EINVAL;
2782 break;
2783 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002784 /* This function does all necessary parse...reuse it */
2785 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002786 if (ret)
2787 break;
2788 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002789 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002790 else
2791 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002792 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07002793 case RES_SOFT_LIMIT:
2794 ret = res_counter_memparse_write_strategy(buffer, &val);
2795 if (ret)
2796 break;
2797 /*
2798 * For memsw, soft limits are hard to implement in terms
2799 * of semantics, for now, we support soft limits for
2800 * control without swap
2801 */
2802 if (type == _MEM)
2803 ret = res_counter_set_soft_limit(&memcg->res, val);
2804 else
2805 ret = -EINVAL;
2806 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002807 default:
2808 ret = -EINVAL; /* should be BUG() ? */
2809 break;
2810 }
2811 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002812}
2813
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08002814static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
2815 unsigned long long *mem_limit, unsigned long long *memsw_limit)
2816{
2817 struct cgroup *cgroup;
2818 unsigned long long min_limit, min_memsw_limit, tmp;
2819
2820 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2821 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2822 cgroup = memcg->css.cgroup;
2823 if (!memcg->use_hierarchy)
2824 goto out;
2825
2826 while (cgroup->parent) {
2827 cgroup = cgroup->parent;
2828 memcg = mem_cgroup_from_cont(cgroup);
2829 if (!memcg->use_hierarchy)
2830 break;
2831 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
2832 min_limit = min(min_limit, tmp);
2833 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2834 min_memsw_limit = min(min_memsw_limit, tmp);
2835 }
2836out:
2837 *mem_limit = min_limit;
2838 *memsw_limit = min_memsw_limit;
2839 return;
2840}
2841
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002842static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002843{
2844 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002845 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002846
2847 mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002848 type = MEMFILE_TYPE(event);
2849 name = MEMFILE_ATTR(event);
2850 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002851 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002852 if (type == _MEM)
2853 res_counter_reset_max(&mem->res);
2854 else
2855 res_counter_reset_max(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002856 break;
2857 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002858 if (type == _MEM)
2859 res_counter_reset_failcnt(&mem->res);
2860 else
2861 res_counter_reset_failcnt(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002862 break;
2863 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07002864
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07002865 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002866}
2867
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002868
2869/* For read statistics */
2870enum {
2871 MCS_CACHE,
2872 MCS_RSS,
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08002873 MCS_FILE_MAPPED,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002874 MCS_PGPGIN,
2875 MCS_PGPGOUT,
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002876 MCS_SWAP,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002877 MCS_INACTIVE_ANON,
2878 MCS_ACTIVE_ANON,
2879 MCS_INACTIVE_FILE,
2880 MCS_ACTIVE_FILE,
2881 MCS_UNEVICTABLE,
2882 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002883};
2884
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002885struct mcs_total_stat {
2886 s64 stat[NR_MCS_STAT];
2887};
2888
2889struct {
2890 char *local_name;
2891 char *total_name;
2892} memcg_stat_strings[NR_MCS_STAT] = {
2893 {"cache", "total_cache"},
2894 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07002895 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002896 {"pgpgin", "total_pgpgin"},
2897 {"pgpgout", "total_pgpgout"},
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002898 {"swap", "total_swap"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002899 {"inactive_anon", "total_inactive_anon"},
2900 {"active_anon", "total_active_anon"},
2901 {"inactive_file", "total_inactive_file"},
2902 {"active_file", "total_active_file"},
2903 {"unevictable", "total_unevictable"}
2904};
2905
2906
2907static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
2908{
2909 struct mcs_total_stat *s = data;
2910 s64 val;
2911
2912 /* per cpu stat */
2913 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE);
2914 s->stat[MCS_CACHE] += val * PAGE_SIZE;
2915 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
2916 s->stat[MCS_RSS] += val * PAGE_SIZE;
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08002917 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_FILE_MAPPED);
2918 s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002919 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT);
2920 s->stat[MCS_PGPGIN] += val;
2921 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT);
2922 s->stat[MCS_PGPGOUT] += val;
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002923 if (do_swap_account) {
2924 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_SWAPOUT);
2925 s->stat[MCS_SWAP] += val * PAGE_SIZE;
2926 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002927
2928 /* per zone stat */
2929 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
2930 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
2931 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
2932 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
2933 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
2934 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
2935 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
2936 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
2937 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
2938 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
2939 return 0;
2940}
2941
2942static void
2943mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
2944{
2945 mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
2946}
2947
Paul Menagec64745c2008-04-29 01:00:02 -07002948static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
2949 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002950{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002951 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002952 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002953 int i;
2954
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002955 memset(&mystat, 0, sizeof(mystat));
2956 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002957
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002958 for (i = 0; i < NR_MCS_STAT; i++) {
2959 if (i == MCS_SWAP && !do_swap_account)
2960 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002961 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002962 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002963
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002964 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08002965 {
2966 unsigned long long limit, memsw_limit;
2967 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
2968 cb->fill(cb, "hierarchical_memory_limit", limit);
2969 if (do_swap_account)
2970 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
2971 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002972
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002973 memset(&mystat, 0, sizeof(mystat));
2974 mem_cgroup_get_total_stat(mem_cont, &mystat);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002975 for (i = 0; i < NR_MCS_STAT; i++) {
2976 if (i == MCS_SWAP && !do_swap_account)
2977 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002978 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002979 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002980
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002981#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08002982 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002983
2984 {
2985 int nid, zid;
2986 struct mem_cgroup_per_zone *mz;
2987 unsigned long recent_rotated[2] = {0, 0};
2988 unsigned long recent_scanned[2] = {0, 0};
2989
2990 for_each_online_node(nid)
2991 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
2992 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
2993
2994 recent_rotated[0] +=
2995 mz->reclaim_stat.recent_rotated[0];
2996 recent_rotated[1] +=
2997 mz->reclaim_stat.recent_rotated[1];
2998 recent_scanned[0] +=
2999 mz->reclaim_stat.recent_scanned[0];
3000 recent_scanned[1] +=
3001 mz->reclaim_stat.recent_scanned[1];
3002 }
3003 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
3004 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
3005 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
3006 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
3007 }
3008#endif
3009
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003010 return 0;
3011}
3012
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003013static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
3014{
3015 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3016
3017 return get_swappiness(memcg);
3018}
3019
3020static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
3021 u64 val)
3022{
3023 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3024 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08003025
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003026 if (val > 100)
3027 return -EINVAL;
3028
3029 if (cgrp->parent == NULL)
3030 return -EINVAL;
3031
3032 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08003033
3034 cgroup_lock();
3035
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003036 /* If under hierarchy, only empty-root can set this value */
3037 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08003038 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
3039 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003040 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08003041 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003042
3043 spin_lock(&memcg->reclaim_param_lock);
3044 memcg->swappiness = val;
3045 spin_unlock(&memcg->reclaim_param_lock);
3046
Li Zefan068b38c2009-01-15 13:51:26 -08003047 cgroup_unlock();
3048
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003049 return 0;
3050}
3051
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003052
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003053static struct cftype mem_cgroup_files[] = {
3054 {
Balbir Singh0eea1032008-02-07 00:13:57 -08003055 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003056 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07003057 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003058 },
3059 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003060 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003061 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003062 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003063 .read_u64 = mem_cgroup_read,
3064 },
3065 {
Balbir Singh0eea1032008-02-07 00:13:57 -08003066 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003067 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07003068 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07003069 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003070 },
3071 {
Balbir Singh296c81d2009-09-23 15:56:36 -07003072 .name = "soft_limit_in_bytes",
3073 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
3074 .write_string = mem_cgroup_write,
3075 .read_u64 = mem_cgroup_read,
3076 },
3077 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003078 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003079 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003080 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07003081 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003082 },
Balbir Singh8697d332008-02-07 00:13:59 -08003083 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003084 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07003085 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003086 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003087 {
3088 .name = "force_empty",
3089 .trigger = mem_cgroup_force_empty_write,
3090 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08003091 {
3092 .name = "use_hierarchy",
3093 .write_u64 = mem_cgroup_hierarchy_write,
3094 .read_u64 = mem_cgroup_hierarchy_read,
3095 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003096 {
3097 .name = "swappiness",
3098 .read_u64 = mem_cgroup_swappiness_read,
3099 .write_u64 = mem_cgroup_swappiness_write,
3100 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003101};
3102
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003103#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3104static struct cftype memsw_cgroup_files[] = {
3105 {
3106 .name = "memsw.usage_in_bytes",
3107 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
3108 .read_u64 = mem_cgroup_read,
3109 },
3110 {
3111 .name = "memsw.max_usage_in_bytes",
3112 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
3113 .trigger = mem_cgroup_reset,
3114 .read_u64 = mem_cgroup_read,
3115 },
3116 {
3117 .name = "memsw.limit_in_bytes",
3118 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
3119 .write_string = mem_cgroup_write,
3120 .read_u64 = mem_cgroup_read,
3121 },
3122 {
3123 .name = "memsw.failcnt",
3124 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
3125 .trigger = mem_cgroup_reset,
3126 .read_u64 = mem_cgroup_read,
3127 },
3128};
3129
3130static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
3131{
3132 if (!do_swap_account)
3133 return 0;
3134 return cgroup_add_files(cont, ss, memsw_cgroup_files,
3135 ARRAY_SIZE(memsw_cgroup_files));
3136};
3137#else
3138static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
3139{
3140 return 0;
3141}
3142#endif
3143
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003144static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
3145{
3146 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003147 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07003148 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07003149 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003150 /*
3151 * This routine is called against possible nodes.
3152 * But it's BUG to call kmalloc() against offline node.
3153 *
3154 * TODO: this routine can waste much memory for nodes which will
3155 * never be onlined. It's better to use memory hotplug callback
3156 * function.
3157 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07003158 if (!node_state(node, N_NORMAL_MEMORY))
3159 tmp = -1;
3160 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003161 if (!pn)
3162 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003163
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003164 mem->info.nodeinfo[node] = pn;
3165 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003166
3167 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
3168 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07003169 for_each_lru(l)
3170 INIT_LIST_HEAD(&mz->lists[l]);
Balbir Singhf64c3f52009-09-23 15:56:37 -07003171 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07003172 mz->on_tree = false;
3173 mz->mem = mem;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003174 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003175 return 0;
3176}
3177
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003178static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
3179{
3180 kfree(mem->info.nodeinfo[node]);
3181}
3182
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003183static int mem_cgroup_size(void)
3184{
3185 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
3186 return sizeof(struct mem_cgroup) + cpustat_size;
3187}
3188
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003189static struct mem_cgroup *mem_cgroup_alloc(void)
3190{
3191 struct mem_cgroup *mem;
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003192 int size = mem_cgroup_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003193
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003194 if (size < PAGE_SIZE)
3195 mem = kmalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003196 else
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003197 mem = vmalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003198
3199 if (mem)
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003200 memset(mem, 0, size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003201 return mem;
3202}
3203
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003204/*
3205 * At destroying mem_cgroup, references from swap_cgroup can remain.
3206 * (scanning all at force_empty is too costly...)
3207 *
3208 * Instead of clearing all references at force_empty, we remember
3209 * the number of reference from swap_cgroup and free mem_cgroup when
3210 * it goes down to 0.
3211 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003212 * Removal of cgroup itself succeeds regardless of refs from swap.
3213 */
3214
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003215static void __mem_cgroup_free(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003216{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003217 int node;
3218
Balbir Singhf64c3f52009-09-23 15:56:37 -07003219 mem_cgroup_remove_from_trees(mem);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003220 free_css_id(&mem_cgroup_subsys, &mem->css);
3221
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003222 for_each_node_state(node, N_POSSIBLE)
3223 free_mem_cgroup_per_zone_info(mem, node);
3224
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003225 if (mem_cgroup_size() < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003226 kfree(mem);
3227 else
3228 vfree(mem);
3229}
3230
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003231static void mem_cgroup_get(struct mem_cgroup *mem)
3232{
3233 atomic_inc(&mem->refcnt);
3234}
3235
3236static void mem_cgroup_put(struct mem_cgroup *mem)
3237{
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003238 if (atomic_dec_and_test(&mem->refcnt)) {
3239 struct mem_cgroup *parent = parent_mem_cgroup(mem);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003240 __mem_cgroup_free(mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003241 if (parent)
3242 mem_cgroup_put(parent);
3243 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003244}
3245
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003246/*
3247 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
3248 */
3249static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
3250{
3251 if (!mem->res.parent)
3252 return NULL;
3253 return mem_cgroup_from_res_counter(mem->res.parent, res);
3254}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003255
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003256#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3257static void __init enable_swap_cgroup(void)
3258{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003259 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003260 do_swap_account = 1;
3261}
3262#else
3263static void __init enable_swap_cgroup(void)
3264{
3265}
3266#endif
3267
Balbir Singhf64c3f52009-09-23 15:56:37 -07003268static int mem_cgroup_soft_limit_tree_init(void)
3269{
3270 struct mem_cgroup_tree_per_node *rtpn;
3271 struct mem_cgroup_tree_per_zone *rtpz;
3272 int tmp, node, zone;
3273
3274 for_each_node_state(node, N_POSSIBLE) {
3275 tmp = node;
3276 if (!node_state(node, N_NORMAL_MEMORY))
3277 tmp = -1;
3278 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
3279 if (!rtpn)
3280 return 1;
3281
3282 soft_limit_tree.rb_tree_per_node[node] = rtpn;
3283
3284 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
3285 rtpz = &rtpn->rb_tree_per_zone[zone];
3286 rtpz->rb_root = RB_ROOT;
3287 spin_lock_init(&rtpz->lock);
3288 }
3289 }
3290 return 0;
3291}
3292
Li Zefan0eb253e2009-01-15 13:51:25 -08003293static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003294mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
3295{
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003296 struct mem_cgroup *mem, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003297 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003298 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003299
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003300 mem = mem_cgroup_alloc();
3301 if (!mem)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003302 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08003303
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003304 for_each_node_state(node, N_POSSIBLE)
3305 if (alloc_mem_cgroup_per_zone_info(mem, node))
3306 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07003307
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003308 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003309 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08003310 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003311 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003312 parent = NULL;
Balbir Singh4b3bde42009-09-23 15:56:32 -07003313 root_mem_cgroup = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07003314 if (mem_cgroup_soft_limit_tree_init())
3315 goto free_out;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08003316 for_each_possible_cpu(cpu) {
3317 struct memcg_stock_pcp *stock =
3318 &per_cpu(memcg_stock, cpu);
3319 INIT_WORK(&stock->work, drain_local_stock);
3320 }
3321 hotcpu_notifier(memcg_stock_cpu_callback, 0);
Balbir Singhf64c3f52009-09-23 15:56:37 -07003322
Balbir Singh18f59ea2009-01-07 18:08:07 -08003323 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003324 parent = mem_cgroup_from_cont(cont->parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003325 mem->use_hierarchy = parent->use_hierarchy;
3326 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003327
Balbir Singh18f59ea2009-01-07 18:08:07 -08003328 if (parent && parent->use_hierarchy) {
3329 res_counter_init(&mem->res, &parent->res);
3330 res_counter_init(&mem->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003331 /*
3332 * We increment refcnt of the parent to ensure that we can
3333 * safely access it on res_counter_charge/uncharge.
3334 * This refcnt will be decremented when freeing this
3335 * mem_cgroup(see mem_cgroup_put).
3336 */
3337 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003338 } else {
3339 res_counter_init(&mem->res, NULL);
3340 res_counter_init(&mem->memsw, NULL);
3341 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003342 mem->last_scanned_child = 0;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -08003343 spin_lock_init(&mem->reclaim_param_lock);
Balbir Singh6d61ef42009-01-07 18:08:06 -08003344
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003345 if (parent)
3346 mem->swappiness = get_swappiness(parent);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003347 atomic_set(&mem->refcnt, 1);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003348 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003349free_out:
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003350 __mem_cgroup_free(mem);
Balbir Singh4b3bde42009-09-23 15:56:32 -07003351 root_mem_cgroup = NULL;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003352 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003353}
3354
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003355static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08003356 struct cgroup *cont)
3357{
3358 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003359
3360 return mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08003361}
3362
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003363static void mem_cgroup_destroy(struct cgroup_subsys *ss,
3364 struct cgroup *cont)
3365{
Daisuke Nishimurac268e992009-01-15 13:51:13 -08003366 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08003367
Daisuke Nishimurac268e992009-01-15 13:51:13 -08003368 mem_cgroup_put(mem);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003369}
3370
3371static int mem_cgroup_populate(struct cgroup_subsys *ss,
3372 struct cgroup *cont)
3373{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003374 int ret;
3375
3376 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
3377 ARRAY_SIZE(mem_cgroup_files));
3378
3379 if (!ret)
3380 ret = register_memsw_files(cont, ss);
3381 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003382}
3383
Balbir Singh67e465a2008-02-07 00:13:54 -08003384static void mem_cgroup_move_task(struct cgroup_subsys *ss,
3385 struct cgroup *cont,
3386 struct cgroup *old_cont,
Ben Blumbe367d02009-09-23 15:56:31 -07003387 struct task_struct *p,
3388 bool threadgroup)
Balbir Singh67e465a2008-02-07 00:13:54 -08003389{
Balbir Singh67e465a2008-02-07 00:13:54 -08003390 /*
Nikanth Karthikesanf9717d22009-01-07 18:08:11 -08003391 * FIXME: It's better to move charges of this process from old
3392 * memcg to new memcg. But it's just on TODO-List now.
Balbir Singh67e465a2008-02-07 00:13:54 -08003393 */
Balbir Singh67e465a2008-02-07 00:13:54 -08003394}
3395
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003396struct cgroup_subsys mem_cgroup_subsys = {
3397 .name = "memory",
3398 .subsys_id = mem_cgroup_subsys_id,
3399 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08003400 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003401 .destroy = mem_cgroup_destroy,
3402 .populate = mem_cgroup_populate,
Balbir Singh67e465a2008-02-07 00:13:54 -08003403 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003404 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003405 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003406};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003407
3408#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3409
3410static int __init disable_swap_account(char *s)
3411{
3412 really_do_swap_account = 0;
3413 return 1;
3414}
3415__setup("noswapaccount", disable_swap_account);
3416#endif