blob: 1aff6c3fcbd8df4d585542ce7cd81a822391ec96 [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
286static struct mem_cgroup_per_zone *
287page_cgroup_zoneinfo(struct page_cgroup *pc)
288{
289 struct mem_cgroup *mem = pc->mem_cgroup;
290 int nid = page_cgroup_nid(pc);
291 int zid = page_cgroup_zid(pc);
292
293 if (!mem)
294 return NULL;
295
296 return mem_cgroup_zoneinfo(mem, nid, zid);
297}
298
299static struct mem_cgroup_tree_per_zone *
300soft_limit_tree_node_zone(int nid, int zid)
301{
302 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
303}
304
305static struct mem_cgroup_tree_per_zone *
306soft_limit_tree_from_page(struct page *page)
307{
308 int nid = page_to_nid(page);
309 int zid = page_zonenum(page);
310
311 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
312}
313
314static void
Balbir Singh4e416952009-09-23 15:56:39 -0700315__mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700316 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700317 struct mem_cgroup_tree_per_zone *mctz,
318 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700319{
320 struct rb_node **p = &mctz->rb_root.rb_node;
321 struct rb_node *parent = NULL;
322 struct mem_cgroup_per_zone *mz_node;
323
324 if (mz->on_tree)
325 return;
326
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700327 mz->usage_in_excess = new_usage_in_excess;
328 if (!mz->usage_in_excess)
329 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700330 while (*p) {
331 parent = *p;
332 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
333 tree_node);
334 if (mz->usage_in_excess < mz_node->usage_in_excess)
335 p = &(*p)->rb_left;
336 /*
337 * We can't avoid mem cgroups that are over their soft
338 * limit by the same amount
339 */
340 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
341 p = &(*p)->rb_right;
342 }
343 rb_link_node(&mz->tree_node, parent, p);
344 rb_insert_color(&mz->tree_node, &mctz->rb_root);
345 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700346}
347
348static void
349__mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
350 struct mem_cgroup_per_zone *mz,
351 struct mem_cgroup_tree_per_zone *mctz)
352{
353 if (!mz->on_tree)
354 return;
355 rb_erase(&mz->tree_node, &mctz->rb_root);
356 mz->on_tree = false;
357}
358
359static void
Balbir Singhf64c3f52009-09-23 15:56:37 -0700360mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
361 struct mem_cgroup_per_zone *mz,
362 struct mem_cgroup_tree_per_zone *mctz)
363{
364 spin_lock(&mctz->lock);
Balbir Singh4e416952009-09-23 15:56:39 -0700365 __mem_cgroup_remove_exceeded(mem, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700366 spin_unlock(&mctz->lock);
367}
368
369static bool mem_cgroup_soft_limit_check(struct mem_cgroup *mem)
370{
371 bool ret = false;
372 int cpu;
373 s64 val;
374 struct mem_cgroup_stat_cpu *cpustat;
375
376 cpu = get_cpu();
377 cpustat = &mem->stat.cpustat[cpu];
378 val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_EVENTS);
379 if (unlikely(val > SOFTLIMIT_EVENTS_THRESH)) {
380 __mem_cgroup_stat_reset_safe(cpustat, MEM_CGROUP_STAT_EVENTS);
381 ret = true;
382 }
383 put_cpu();
384 return ret;
385}
386
387static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
388{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700389 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700390 struct mem_cgroup_per_zone *mz;
391 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700392 int nid = page_to_nid(page);
393 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700394 mctz = soft_limit_tree_from_page(page);
395
396 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700397 * Necessary to update all ancestors when hierarchy is used.
398 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700399 */
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700400 for (; mem; mem = parent_mem_cgroup(mem)) {
401 mz = mem_cgroup_zoneinfo(mem, nid, zid);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700402 excess = res_counter_soft_limit_excess(&mem->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700403 /*
404 * We have to update the tree if mz is on RB-tree or
405 * mem is over its softlimit.
406 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700407 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700408 spin_lock(&mctz->lock);
409 /* if on-tree, remove it */
410 if (mz->on_tree)
411 __mem_cgroup_remove_exceeded(mem, mz, mctz);
412 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700413 * Insert again. mz->usage_in_excess will be updated.
414 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700415 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700416 __mem_cgroup_insert_exceeded(mem, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700417 spin_unlock(&mctz->lock);
418 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700419 }
420}
421
422static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
423{
424 int node, zone;
425 struct mem_cgroup_per_zone *mz;
426 struct mem_cgroup_tree_per_zone *mctz;
427
428 for_each_node_state(node, N_POSSIBLE) {
429 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
430 mz = mem_cgroup_zoneinfo(mem, node, zone);
431 mctz = soft_limit_tree_node_zone(node, zone);
432 mem_cgroup_remove_exceeded(mem, mz, mctz);
433 }
434 }
435}
436
Balbir Singh4e416952009-09-23 15:56:39 -0700437static inline unsigned long mem_cgroup_get_excess(struct mem_cgroup *mem)
438{
439 return res_counter_soft_limit_excess(&mem->res) >> PAGE_SHIFT;
440}
441
442static struct mem_cgroup_per_zone *
443__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
444{
445 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700446 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700447
448retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700449 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700450 rightmost = rb_last(&mctz->rb_root);
451 if (!rightmost)
452 goto done; /* Nothing to reclaim from */
453
454 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
455 /*
456 * Remove the node now but someone else can add it back,
457 * we will to add it back at the end of reclaim to its correct
458 * position in the tree.
459 */
460 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
461 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
462 !css_tryget(&mz->mem->css))
463 goto retry;
464done:
465 return mz;
466}
467
468static struct mem_cgroup_per_zone *
469mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
470{
471 struct mem_cgroup_per_zone *mz;
472
473 spin_lock(&mctz->lock);
474 mz = __mem_cgroup_largest_soft_limit_node(mctz);
475 spin_unlock(&mctz->lock);
476 return mz;
477}
478
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700479static void mem_cgroup_swap_statistics(struct mem_cgroup *mem,
480 bool charge)
481{
482 int val = (charge) ? 1 : -1;
483 struct mem_cgroup_stat *stat = &mem->stat;
484 struct mem_cgroup_stat_cpu *cpustat;
485 int cpu = get_cpu();
486
487 cpustat = &stat->cpustat[cpu];
488 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_SWAPOUT, val);
489 put_cpu();
490}
491
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700492static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
493 struct page_cgroup *pc,
494 bool charge)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800495{
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700496 int val = (charge) ? 1 : -1;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800497 struct mem_cgroup_stat *stat = &mem->stat;
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700498 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800499 int cpu = get_cpu();
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800500
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800501 cpustat = &stat->cpustat[cpu];
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700502 if (PageCgroupCache(pc))
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700503 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800504 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700505 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700506
507 if (charge)
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700508 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700509 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
510 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700511 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700512 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700513 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_EVENTS, 1);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800514 put_cpu();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800515}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800516
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700517static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700518 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800519{
520 int nid, zid;
521 struct mem_cgroup_per_zone *mz;
522 u64 total = 0;
523
524 for_each_online_node(nid)
525 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
526 mz = mem_cgroup_zoneinfo(mem, nid, zid);
527 total += MEM_CGROUP_ZSTAT(mz, idx);
528 }
529 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800530}
531
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800532static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800533{
534 return container_of(cgroup_subsys_state(cont,
535 mem_cgroup_subsys_id), struct mem_cgroup,
536 css);
537}
538
Balbir Singhcf475ad2008-04-29 01:00:16 -0700539struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800540{
Balbir Singh31a78f22008-09-28 23:09:31 +0100541 /*
542 * mm_update_next_owner() may clear mm->owner to NULL
543 * if it races with swapoff, page migration, etc.
544 * So this can be called with p == NULL.
545 */
546 if (unlikely(!p))
547 return NULL;
548
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800549 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
550 struct mem_cgroup, css);
551}
552
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800553static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
554{
555 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700556
557 if (!mm)
558 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800559 /*
560 * Because we have no locks, mm->owner's may be being moved to other
561 * cgroup. We use css_tryget() here even if this looks
562 * pessimistic (rather than adding locks here).
563 */
564 rcu_read_lock();
565 do {
566 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
567 if (unlikely(!mem))
568 break;
569 } while (!css_tryget(&mem->css));
570 rcu_read_unlock();
571 return mem;
572}
573
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700574/*
575 * Call callback function against all cgroup under hierarchy tree.
576 */
577static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
578 int (*func)(struct mem_cgroup *, void *))
579{
580 int found, ret, nextid;
581 struct cgroup_subsys_state *css;
582 struct mem_cgroup *mem;
583
584 if (!root->use_hierarchy)
585 return (*func)(root, data);
586
587 nextid = 1;
588 do {
589 ret = 0;
590 mem = NULL;
591
592 rcu_read_lock();
593 css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
594 &found);
595 if (css && css_tryget(css))
596 mem = container_of(css, struct mem_cgroup, css);
597 rcu_read_unlock();
598
599 if (mem) {
600 ret = (*func)(mem, data);
601 css_put(&mem->css);
602 }
603 nextid = found + 1;
604 } while (!ret && css);
605
606 return ret;
607}
608
Balbir Singh4b3bde42009-09-23 15:56:32 -0700609static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
610{
611 return (mem == root_mem_cgroup);
612}
613
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800614/*
615 * Following LRU functions are allowed to be used without PCG_LOCK.
616 * Operations are called by routine of global LRU independently from memcg.
617 * What we have to take care of here is validness of pc->mem_cgroup.
618 *
619 * Changes to pc->mem_cgroup happens when
620 * 1. charge
621 * 2. moving account
622 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
623 * It is added to LRU before charge.
624 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
625 * When moving account, the page is not on LRU. It's isolated.
626 */
627
628void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800629{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800630 struct page_cgroup *pc;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800631 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700632
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800633 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800634 return;
635 pc = lookup_page_cgroup(page);
636 /* can happen while we handle swapcache. */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700637 if (!TestClearPageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800638 return;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700639 VM_BUG_ON(!pc->mem_cgroup);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800640 /*
641 * We don't check PCG_USED bit. It's cleared when the "page" is finally
642 * removed from global LRU.
643 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800644 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700645 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700646 if (mem_cgroup_is_root(pc->mem_cgroup))
647 return;
648 VM_BUG_ON(list_empty(&pc->lru));
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800649 list_del_init(&pc->lru);
650 return;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800651}
652
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800653void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800654{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800655 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800656}
657
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800658void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800659{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800660 struct mem_cgroup_per_zone *mz;
661 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800662
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800663 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700664 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700665
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800666 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800667 /*
668 * Used bit is set without atomic ops but after smp_wmb().
669 * For making pc->mem_cgroup visible, insert smp_rmb() here.
670 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800671 smp_rmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -0700672 /* unused or root page is not rotated. */
673 if (!PageCgroupUsed(pc) || mem_cgroup_is_root(pc->mem_cgroup))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800674 return;
675 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700676 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800677}
678
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800679void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
680{
681 struct page_cgroup *pc;
682 struct mem_cgroup_per_zone *mz;
683
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800684 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800685 return;
686 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700687 VM_BUG_ON(PageCgroupAcctLRU(pc));
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800688 /*
689 * Used bit is set without atomic ops but after smp_wmb().
690 * For making pc->mem_cgroup visible, insert smp_rmb() here.
691 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800692 smp_rmb();
693 if (!PageCgroupUsed(pc))
694 return;
695
696 mz = page_cgroup_zoneinfo(pc);
697 MEM_CGROUP_ZSTAT(mz, lru) += 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700698 SetPageCgroupAcctLRU(pc);
699 if (mem_cgroup_is_root(pc->mem_cgroup))
700 return;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800701 list_add(&pc->lru, &mz->lists[lru]);
702}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800703
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800704/*
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800705 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
706 * lru because the page may.be reused after it's fully uncharged (because of
707 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
708 * it again. This function is only used to charge SwapCache. It's done under
709 * lock_page and expected that zone->lru_lock is never held.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800710 */
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800711static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800712{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800713 unsigned long flags;
714 struct zone *zone = page_zone(page);
715 struct page_cgroup *pc = lookup_page_cgroup(page);
716
717 spin_lock_irqsave(&zone->lru_lock, flags);
718 /*
719 * Forget old LRU when this page_cgroup is *not* used. This Used bit
720 * is guarded by lock_page() because the page is SwapCache.
721 */
722 if (!PageCgroupUsed(pc))
723 mem_cgroup_del_lru_list(page, page_lru(page));
724 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800725}
726
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800727static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
728{
729 unsigned long flags;
730 struct zone *zone = page_zone(page);
731 struct page_cgroup *pc = lookup_page_cgroup(page);
732
733 spin_lock_irqsave(&zone->lru_lock, flags);
734 /* link when the page is linked to LRU but page_cgroup isn't */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700735 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800736 mem_cgroup_add_lru_list(page, page_lru(page));
737 spin_unlock_irqrestore(&zone->lru_lock, flags);
738}
739
740
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800741void mem_cgroup_move_lists(struct page *page,
742 enum lru_list from, enum lru_list to)
743{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800744 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800745 return;
746 mem_cgroup_del_lru_list(page, from);
747 mem_cgroup_add_lru_list(page, to);
748}
749
David Rientjes4c4a2212008-02-07 00:14:06 -0800750int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
751{
752 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700753 struct mem_cgroup *curr = NULL;
David Rientjes4c4a2212008-02-07 00:14:06 -0800754
755 task_lock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700756 rcu_read_lock();
757 curr = try_get_mem_cgroup_from_mm(task->mm);
758 rcu_read_unlock();
David Rientjes4c4a2212008-02-07 00:14:06 -0800759 task_unlock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700760 if (!curr)
761 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -0800762 /*
763 * We should check use_hierarchy of "mem" not "curr". Because checking
764 * use_hierarchy of "curr" here make this function true if hierarchy is
765 * enabled in "curr" and "curr" is a child of "mem" in *cgroup*
766 * hierarchy(even if use_hierarchy is disabled in "mem").
767 */
768 if (mem->use_hierarchy)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700769 ret = css_is_ancestor(&curr->css, &mem->css);
770 else
771 ret = (curr == mem);
772 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -0800773 return ret;
774}
775
Balbir Singh66e17072008-02-07 00:13:56 -0800776/*
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800777 * prev_priority control...this will be used in memory reclaim path.
778 */
779int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
780{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800781 int prev_priority;
782
783 spin_lock(&mem->reclaim_param_lock);
784 prev_priority = mem->prev_priority;
785 spin_unlock(&mem->reclaim_param_lock);
786
787 return prev_priority;
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800788}
789
790void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
791{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800792 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800793 if (priority < mem->prev_priority)
794 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800795 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800796}
797
798void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
799{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800800 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800801 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800802 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800803}
804
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800805static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800806{
807 unsigned long active;
808 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800809 unsigned long gb;
810 unsigned long inactive_ratio;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800811
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700812 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
813 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800814
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800815 gb = (inactive + active) >> (30 - PAGE_SHIFT);
816 if (gb)
817 inactive_ratio = int_sqrt(10 * gb);
818 else
819 inactive_ratio = 1;
820
821 if (present_pages) {
822 present_pages[0] = inactive;
823 present_pages[1] = active;
824 }
825
826 return inactive_ratio;
827}
828
829int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
830{
831 unsigned long active;
832 unsigned long inactive;
833 unsigned long present_pages[2];
834 unsigned long inactive_ratio;
835
836 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
837
838 inactive = present_pages[0];
839 active = present_pages[1];
840
841 if (inactive * inactive_ratio < active)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800842 return 1;
843
844 return 0;
845}
846
Rik van Riel56e49d22009-06-16 15:32:28 -0700847int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
848{
849 unsigned long active;
850 unsigned long inactive;
851
852 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
853 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
854
855 return (active > inactive);
856}
857
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800858unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
859 struct zone *zone,
860 enum lru_list lru)
861{
862 int nid = zone->zone_pgdat->node_id;
863 int zid = zone_idx(zone);
864 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
865
866 return MEM_CGROUP_ZSTAT(mz, lru);
867}
868
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800869struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
870 struct zone *zone)
871{
872 int nid = zone->zone_pgdat->node_id;
873 int zid = zone_idx(zone);
874 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
875
876 return &mz->reclaim_stat;
877}
878
879struct zone_reclaim_stat *
880mem_cgroup_get_reclaim_stat_from_page(struct page *page)
881{
882 struct page_cgroup *pc;
883 struct mem_cgroup_per_zone *mz;
884
885 if (mem_cgroup_disabled())
886 return NULL;
887
888 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800889 /*
890 * Used bit is set without atomic ops but after smp_wmb().
891 * For making pc->mem_cgroup visible, insert smp_rmb() here.
892 */
893 smp_rmb();
894 if (!PageCgroupUsed(pc))
895 return NULL;
896
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800897 mz = page_cgroup_zoneinfo(pc);
898 if (!mz)
899 return NULL;
900
901 return &mz->reclaim_stat;
902}
903
Balbir Singh66e17072008-02-07 00:13:56 -0800904unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
905 struct list_head *dst,
906 unsigned long *scanned, int order,
907 int mode, struct zone *z,
908 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700909 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -0800910{
911 unsigned long nr_taken = 0;
912 struct page *page;
913 unsigned long scan;
914 LIST_HEAD(pc_list);
915 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800916 struct page_cgroup *pc, *tmp;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800917 int nid = z->zone_pgdat->node_id;
918 int zid = zone_idx(z);
919 struct mem_cgroup_per_zone *mz;
Johannes Weinerb7c46d12009-09-21 17:02:56 -0700920 int lru = LRU_FILE * file + active;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700921 int ret;
Balbir Singh66e17072008-02-07 00:13:56 -0800922
Balbir Singhcf475ad2008-04-29 01:00:16 -0700923 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800924 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700925 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -0800926
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800927 scan = 0;
928 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -0800929 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800930 break;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800931
932 page = pc->page;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700933 if (unlikely(!PageCgroupUsed(pc)))
934 continue;
Hugh Dickins436c65412008-02-07 00:14:12 -0800935 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800936 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800937
Hugh Dickins436c65412008-02-07 00:14:12 -0800938 scan++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700939 ret = __isolate_lru_page(page, mode, file);
940 switch (ret) {
941 case 0:
Balbir Singh66e17072008-02-07 00:13:56 -0800942 list_move(&page->lru, dst);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700943 mem_cgroup_del_lru(page);
Balbir Singh66e17072008-02-07 00:13:56 -0800944 nr_taken++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700945 break;
946 case -EBUSY:
947 /* we don't affect global LRU but rotate in our LRU */
948 mem_cgroup_rotate_lru_list(page, page_lru(page));
949 break;
950 default:
951 break;
Balbir Singh66e17072008-02-07 00:13:56 -0800952 }
953 }
954
Balbir Singh66e17072008-02-07 00:13:56 -0800955 *scanned = scan;
956 return nr_taken;
957}
958
Balbir Singh6d61ef42009-01-07 18:08:06 -0800959#define mem_cgroup_from_res_counter(counter, member) \
960 container_of(counter, struct mem_cgroup, member)
961
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -0800962static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
963{
964 if (do_swap_account) {
965 if (res_counter_check_under_limit(&mem->res) &&
966 res_counter_check_under_limit(&mem->memsw))
967 return true;
968 } else
969 if (res_counter_check_under_limit(&mem->res))
970 return true;
971 return false;
972}
973
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800974static unsigned int get_swappiness(struct mem_cgroup *memcg)
975{
976 struct cgroup *cgrp = memcg->css.cgroup;
977 unsigned int swappiness;
978
979 /* root ? */
980 if (cgrp->parent == NULL)
981 return vm_swappiness;
982
983 spin_lock(&memcg->reclaim_param_lock);
984 swappiness = memcg->swappiness;
985 spin_unlock(&memcg->reclaim_param_lock);
986
987 return swappiness;
988}
989
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700990static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
991{
992 int *val = data;
993 (*val)++;
994 return 0;
995}
Balbir Singhe2224322009-04-02 16:57:39 -0700996
997/**
998 * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode.
999 * @memcg: The memory cgroup that went over limit
1000 * @p: Task that is going to be killed
1001 *
1002 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1003 * enabled
1004 */
1005void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1006{
1007 struct cgroup *task_cgrp;
1008 struct cgroup *mem_cgrp;
1009 /*
1010 * Need a buffer in BSS, can't rely on allocations. The code relies
1011 * on the assumption that OOM is serialized for memory controller.
1012 * If this assumption is broken, revisit this code.
1013 */
1014 static char memcg_name[PATH_MAX];
1015 int ret;
1016
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001017 if (!memcg || !p)
Balbir Singhe2224322009-04-02 16:57:39 -07001018 return;
1019
1020
1021 rcu_read_lock();
1022
1023 mem_cgrp = memcg->css.cgroup;
1024 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1025
1026 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1027 if (ret < 0) {
1028 /*
1029 * Unfortunately, we are unable to convert to a useful name
1030 * But we'll still print out the usage information
1031 */
1032 rcu_read_unlock();
1033 goto done;
1034 }
1035 rcu_read_unlock();
1036
1037 printk(KERN_INFO "Task in %s killed", memcg_name);
1038
1039 rcu_read_lock();
1040 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1041 if (ret < 0) {
1042 rcu_read_unlock();
1043 goto done;
1044 }
1045 rcu_read_unlock();
1046
1047 /*
1048 * Continues from above, so we don't need an KERN_ level
1049 */
1050 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1051done:
1052
1053 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1054 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1055 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1056 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1057 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1058 "failcnt %llu\n",
1059 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1060 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1061 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1062}
1063
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001064/*
1065 * This function returns the number of memcg under hierarchy tree. Returns
1066 * 1(self count) if no children.
1067 */
1068static int mem_cgroup_count_children(struct mem_cgroup *mem)
1069{
1070 int num = 0;
1071 mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
1072 return num;
1073}
1074
Balbir Singh6d61ef42009-01-07 18:08:06 -08001075/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001076 * Visit the first child (need not be the first child as per the ordering
1077 * of the cgroup list, since we track last_scanned_child) of @mem and use
1078 * that to reclaim free pages from.
1079 */
1080static struct mem_cgroup *
1081mem_cgroup_select_victim(struct mem_cgroup *root_mem)
1082{
1083 struct mem_cgroup *ret = NULL;
1084 struct cgroup_subsys_state *css;
1085 int nextid, found;
1086
1087 if (!root_mem->use_hierarchy) {
1088 css_get(&root_mem->css);
1089 ret = root_mem;
1090 }
1091
1092 while (!ret) {
1093 rcu_read_lock();
1094 nextid = root_mem->last_scanned_child + 1;
1095 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
1096 &found);
1097 if (css && css_tryget(css))
1098 ret = container_of(css, struct mem_cgroup, css);
1099
1100 rcu_read_unlock();
1101 /* Updates scanning parameter */
1102 spin_lock(&root_mem->reclaim_param_lock);
1103 if (!css) {
1104 /* this means start scan from ID:1 */
1105 root_mem->last_scanned_child = 0;
1106 } else
1107 root_mem->last_scanned_child = found;
1108 spin_unlock(&root_mem->reclaim_param_lock);
1109 }
1110
1111 return ret;
1112}
1113
1114/*
1115 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1116 * we reclaimed from, so that we don't end up penalizing one child extensively
1117 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001118 *
1119 * root_mem is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001120 *
1121 * We give up and return to the caller when we visit root_mem twice.
1122 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001123 *
1124 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001125 */
1126static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
Balbir Singh4e416952009-09-23 15:56:39 -07001127 struct zone *zone,
Balbir Singh75822b42009-09-23 15:56:38 -07001128 gfp_t gfp_mask,
1129 unsigned long reclaim_options)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001130{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001131 struct mem_cgroup *victim;
1132 int ret, total = 0;
1133 int loop = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001134 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1135 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
Balbir Singh4e416952009-09-23 15:56:39 -07001136 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
1137 unsigned long excess = mem_cgroup_get_excess(root_mem);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001138
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001139 /* If memsw_is_minimum==1, swap-out is of-no-use. */
1140 if (root_mem->memsw_is_minimum)
1141 noswap = true;
1142
Balbir Singh4e416952009-09-23 15:56:39 -07001143 while (1) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001144 victim = mem_cgroup_select_victim(root_mem);
Balbir Singh4e416952009-09-23 15:56:39 -07001145 if (victim == root_mem) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001146 loop++;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001147 if (loop >= 1)
1148 drain_all_stock_async();
Balbir Singh4e416952009-09-23 15:56:39 -07001149 if (loop >= 2) {
1150 /*
1151 * If we have not been able to reclaim
1152 * anything, it might because there are
1153 * no reclaimable pages under this hierarchy
1154 */
1155 if (!check_soft || !total) {
1156 css_put(&victim->css);
1157 break;
1158 }
1159 /*
1160 * We want to do more targetted reclaim.
1161 * excess >> 2 is not to excessive so as to
1162 * reclaim too much, nor too less that we keep
1163 * coming back to reclaim from this cgroup
1164 */
1165 if (total >= (excess >> 2) ||
1166 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1167 css_put(&victim->css);
1168 break;
1169 }
1170 }
1171 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001172 if (!mem_cgroup_local_usage(&victim->stat)) {
1173 /* this cgroup's local usage == 0 */
1174 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001175 continue;
1176 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001177 /* we use swappiness of local cgroup */
Balbir Singh4e416952009-09-23 15:56:39 -07001178 if (check_soft)
1179 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
1180 noswap, get_swappiness(victim), zone,
1181 zone->zone_pgdat->node_id);
1182 else
1183 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
1184 noswap, get_swappiness(victim));
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001185 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001186 /*
1187 * At shrinking usage, we can't check we should stop here or
1188 * reclaim more. It's depends on callers. last_scanned_child
1189 * will work enough for keeping fairness under tree.
1190 */
1191 if (shrink)
1192 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001193 total += ret;
Balbir Singh4e416952009-09-23 15:56:39 -07001194 if (check_soft) {
1195 if (res_counter_check_under_soft_limit(&root_mem->res))
1196 return total;
1197 } else if (mem_cgroup_check_under_limit(root_mem))
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001198 return 1 + total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001199 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001200 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001201}
1202
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001203bool mem_cgroup_oom_called(struct task_struct *task)
1204{
1205 bool ret = false;
1206 struct mem_cgroup *mem;
1207 struct mm_struct *mm;
1208
1209 rcu_read_lock();
1210 mm = task->mm;
1211 if (!mm)
1212 mm = &init_mm;
1213 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
1214 if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
1215 ret = true;
1216 rcu_read_unlock();
1217 return ret;
1218}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001219
1220static int record_last_oom_cb(struct mem_cgroup *mem, void *data)
1221{
1222 mem->last_oom_jiffies = jiffies;
1223 return 0;
1224}
1225
1226static void record_last_oom(struct mem_cgroup *mem)
1227{
1228 mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb);
1229}
1230
Balbir Singhd69b0422009-06-17 16:26:34 -07001231/*
1232 * Currently used to update mapped file statistics, but the routine can be
1233 * generalized to update other statistics as well.
1234 */
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001235void mem_cgroup_update_file_mapped(struct page *page, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07001236{
1237 struct mem_cgroup *mem;
1238 struct mem_cgroup_stat *stat;
1239 struct mem_cgroup_stat_cpu *cpustat;
1240 int cpu;
1241 struct page_cgroup *pc;
1242
Balbir Singhd69b0422009-06-17 16:26:34 -07001243 pc = lookup_page_cgroup(page);
1244 if (unlikely(!pc))
1245 return;
1246
1247 lock_page_cgroup(pc);
1248 mem = pc->mem_cgroup;
1249 if (!mem)
1250 goto done;
1251
1252 if (!PageCgroupUsed(pc))
1253 goto done;
1254
1255 /*
1256 * Preemption is already disabled, we don't need get_cpu()
1257 */
1258 cpu = smp_processor_id();
1259 stat = &mem->stat;
1260 cpustat = &stat->cpustat[cpu];
1261
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001262 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED, val);
Balbir Singhd69b0422009-06-17 16:26:34 -07001263done:
1264 unlock_page_cgroup(pc);
1265}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001266
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001267/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001268 * size of first charge trial. "32" comes from vmscan.c's magic value.
1269 * TODO: maybe necessary to use big numbers in big irons.
1270 */
1271#define CHARGE_SIZE (32 * PAGE_SIZE)
1272struct memcg_stock_pcp {
1273 struct mem_cgroup *cached; /* this never be root cgroup */
1274 int charge;
1275 struct work_struct work;
1276};
1277static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
1278static atomic_t memcg_drain_count;
1279
1280/*
1281 * Try to consume stocked charge on this cpu. If success, PAGE_SIZE is consumed
1282 * from local stock and true is returned. If the stock is 0 or charges from a
1283 * cgroup which is not current target, returns false. This stock will be
1284 * refilled.
1285 */
1286static bool consume_stock(struct mem_cgroup *mem)
1287{
1288 struct memcg_stock_pcp *stock;
1289 bool ret = true;
1290
1291 stock = &get_cpu_var(memcg_stock);
1292 if (mem == stock->cached && stock->charge)
1293 stock->charge -= PAGE_SIZE;
1294 else /* need to call res_counter_charge */
1295 ret = false;
1296 put_cpu_var(memcg_stock);
1297 return ret;
1298}
1299
1300/*
1301 * Returns stocks cached in percpu to res_counter and reset cached information.
1302 */
1303static void drain_stock(struct memcg_stock_pcp *stock)
1304{
1305 struct mem_cgroup *old = stock->cached;
1306
1307 if (stock->charge) {
1308 res_counter_uncharge(&old->res, stock->charge);
1309 if (do_swap_account)
1310 res_counter_uncharge(&old->memsw, stock->charge);
1311 }
1312 stock->cached = NULL;
1313 stock->charge = 0;
1314}
1315
1316/*
1317 * This must be called under preempt disabled or must be called by
1318 * a thread which is pinned to local cpu.
1319 */
1320static void drain_local_stock(struct work_struct *dummy)
1321{
1322 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
1323 drain_stock(stock);
1324}
1325
1326/*
1327 * Cache charges(val) which is from res_counter, to local per_cpu area.
1328 * This will be consumed by consumt_stock() function, later.
1329 */
1330static void refill_stock(struct mem_cgroup *mem, int val)
1331{
1332 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
1333
1334 if (stock->cached != mem) { /* reset if necessary */
1335 drain_stock(stock);
1336 stock->cached = mem;
1337 }
1338 stock->charge += val;
1339 put_cpu_var(memcg_stock);
1340}
1341
1342/*
1343 * Tries to drain stocked charges in other cpus. This function is asynchronous
1344 * and just put a work per cpu for draining localy on each cpu. Caller can
1345 * expects some charges will be back to res_counter later but cannot wait for
1346 * it.
1347 */
1348static void drain_all_stock_async(void)
1349{
1350 int cpu;
1351 /* This function is for scheduling "drain" in asynchronous way.
1352 * The result of "drain" is not directly handled by callers. Then,
1353 * if someone is calling drain, we don't have to call drain more.
1354 * Anyway, WORK_STRUCT_PENDING check in queue_work_on() will catch if
1355 * there is a race. We just do loose check here.
1356 */
1357 if (atomic_read(&memcg_drain_count))
1358 return;
1359 /* Notify other cpus that system-wide "drain" is running */
1360 atomic_inc(&memcg_drain_count);
1361 get_online_cpus();
1362 for_each_online_cpu(cpu) {
1363 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
1364 schedule_work_on(cpu, &stock->work);
1365 }
1366 put_online_cpus();
1367 atomic_dec(&memcg_drain_count);
1368 /* We don't wait for flush_work */
1369}
1370
1371/* This is a synchronous drain interface. */
1372static void drain_all_stock_sync(void)
1373{
1374 /* called when force_empty is called */
1375 atomic_inc(&memcg_drain_count);
1376 schedule_on_each_cpu(drain_local_stock);
1377 atomic_dec(&memcg_drain_count);
1378}
1379
1380static int __cpuinit memcg_stock_cpu_callback(struct notifier_block *nb,
1381 unsigned long action,
1382 void *hcpu)
1383{
1384 int cpu = (unsigned long)hcpu;
1385 struct memcg_stock_pcp *stock;
1386
1387 if (action != CPU_DEAD)
1388 return NOTIFY_OK;
1389 stock = &per_cpu(memcg_stock, cpu);
1390 drain_stock(stock);
1391 return NOTIFY_OK;
1392}
1393
1394/*
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001395 * Unlike exported interface, "oom" parameter is added. if oom==true,
1396 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001397 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001398static int __mem_cgroup_try_charge(struct mm_struct *mm,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001399 gfp_t gfp_mask, struct mem_cgroup **memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -07001400 bool oom, struct page *page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001401{
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001402 struct mem_cgroup *mem, *mem_over_limit;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001403 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001404 struct res_counter *fail_res;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001405 int csize = CHARGE_SIZE;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001406
1407 if (unlikely(test_thread_flag(TIF_MEMDIE))) {
1408 /* Don't account this! */
1409 *memcg = NULL;
1410 return 0;
1411 }
1412
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001413 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08001414 * We always charge the cgroup the mm_struct belongs to.
1415 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001416 * thread group leader migrates. It's possible that mm is not
1417 * set, if so charge the init_mm (happens for pagecache usage).
1418 */
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001419 mem = *memcg;
1420 if (likely(!mem)) {
1421 mem = try_get_mem_cgroup_from_mm(mm);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001422 *memcg = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001423 } else {
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001424 css_get(&mem->css);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001425 }
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001426 if (unlikely(!mem))
1427 return 0;
1428
Nikanth Karthikesan46f7e602009-05-28 14:34:41 -07001429 VM_BUG_ON(css_is_removed(&mem->css));
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001430 if (mem_cgroup_is_root(mem))
1431 goto done;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001432
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001433 while (1) {
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001434 int ret = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001435 unsigned long flags = 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001436
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001437 if (consume_stock(mem))
1438 goto charged;
1439
1440 ret = res_counter_charge(&mem->res, csize, &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001441 if (likely(!ret)) {
1442 if (!do_swap_account)
1443 break;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001444 ret = res_counter_charge(&mem->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001445 if (likely(!ret))
1446 break;
1447 /* mem+swap counter fails */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001448 res_counter_uncharge(&mem->res, csize);
Balbir Singh75822b42009-09-23 15:56:38 -07001449 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001450 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1451 memsw);
1452 } else
1453 /* mem counter fails */
1454 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1455 res);
1456
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001457 /* reduce request size and retry */
1458 if (csize > PAGE_SIZE) {
1459 csize = PAGE_SIZE;
1460 continue;
1461 }
Hugh Dickins3be91272008-02-07 00:14:19 -08001462 if (!(gfp_mask & __GFP_WAIT))
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001463 goto nomem;
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001464
Balbir Singh4e416952009-09-23 15:56:39 -07001465 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
1466 gfp_mask, flags);
Daisuke Nishimura4d1c6272009-01-15 13:51:14 -08001467 if (ret)
1468 continue;
Balbir Singh66e17072008-02-07 00:13:56 -08001469
1470 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001471 * try_to_free_mem_cgroup_pages() might not give us a full
1472 * picture of reclaim. Some pages are reclaimed and might be
1473 * moved to swap cache or just unmapped from the cgroup.
1474 * Check the limit again to see if the reclaim reduced the
1475 * current usage of the cgroup before giving up
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001476 *
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001477 */
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -08001478 if (mem_cgroup_check_under_limit(mem_over_limit))
1479 continue;
Hugh Dickins3be91272008-02-07 00:14:19 -08001480
1481 if (!nr_retries--) {
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001482 if (oom) {
KAMEZAWA Hiroyuki88700752009-01-07 18:08:09 -08001483 mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001484 record_last_oom(mem_over_limit);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001485 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001486 goto nomem;
Balbir Singh66e17072008-02-07 00:13:56 -08001487 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001488 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001489 if (csize > PAGE_SIZE)
1490 refill_stock(mem, csize - PAGE_SIZE);
1491charged:
Balbir Singhf64c3f52009-09-23 15:56:37 -07001492 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001493 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
1494 * if they exceeds softlimit.
Balbir Singhf64c3f52009-09-23 15:56:37 -07001495 */
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001496 if (mem_cgroup_soft_limit_check(mem))
1497 mem_cgroup_update_tree(mem, page);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001498done:
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001499 return 0;
1500nomem:
1501 css_put(&mem->css);
1502 return -ENOMEM;
1503}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001504
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001505/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001506 * Somemtimes we have to undo a charge we got by try_charge().
1507 * This function is for that and do uncharge, put css's refcnt.
1508 * gotten by try_charge().
1509 */
1510static void mem_cgroup_cancel_charge(struct mem_cgroup *mem)
1511{
1512 if (!mem_cgroup_is_root(mem)) {
1513 res_counter_uncharge(&mem->res, PAGE_SIZE);
1514 if (do_swap_account)
1515 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1516 }
1517 css_put(&mem->css);
1518}
1519
1520/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001521 * A helper function to get mem_cgroup from ID. must be called under
1522 * rcu_read_lock(). The caller must check css_is_removed() or some if
1523 * it's concern. (dropping refcnt from swap can be called against removed
1524 * memcg.)
1525 */
1526static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
1527{
1528 struct cgroup_subsys_state *css;
1529
1530 /* ID 0 is unused ID */
1531 if (!id)
1532 return NULL;
1533 css = css_lookup(&mem_cgroup_subsys, id);
1534 if (!css)
1535 return NULL;
1536 return container_of(css, struct mem_cgroup, css);
1537}
1538
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001539static struct mem_cgroup *try_get_mem_cgroup_from_swapcache(struct page *page)
1540{
1541 struct mem_cgroup *mem;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001542 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001543 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001544 swp_entry_t ent;
1545
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001546 VM_BUG_ON(!PageLocked(page));
1547
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001548 if (!PageSwapCache(page))
1549 return NULL;
1550
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001551 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001552 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001553 if (PageCgroupUsed(pc)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001554 mem = pc->mem_cgroup;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001555 if (mem && !css_tryget(&mem->css))
1556 mem = NULL;
1557 } else {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001558 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001559 id = lookup_swap_cgroup(ent);
1560 rcu_read_lock();
1561 mem = mem_cgroup_lookup(id);
1562 if (mem && !css_tryget(&mem->css))
1563 mem = NULL;
1564 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001565 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001566 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001567 return mem;
1568}
1569
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001570/*
Daisuke Nishimuraa5e924f2009-01-07 18:08:28 -08001571 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001572 * USED state. If already USED, uncharge and return.
1573 */
1574
1575static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1576 struct page_cgroup *pc,
1577 enum charge_type ctype)
1578{
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001579 /* try_charge() can return NULL to *memcg, taking care of it. */
1580 if (!mem)
1581 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001582
1583 lock_page_cgroup(pc);
1584 if (unlikely(PageCgroupUsed(pc))) {
1585 unlock_page_cgroup(pc);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001586 mem_cgroup_cancel_charge(mem);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001587 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001588 }
Balbir Singh4b3bde42009-09-23 15:56:32 -07001589
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001590 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07001591 /*
1592 * We access a page_cgroup asynchronously without lock_page_cgroup().
1593 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
1594 * is accessed after testing USED bit. To make pc->mem_cgroup visible
1595 * before USED bit, we need memory barrier here.
1596 * See mem_cgroup_add_lru_list(), etc.
1597 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001598 smp_wmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -07001599 switch (ctype) {
1600 case MEM_CGROUP_CHARGE_TYPE_CACHE:
1601 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
1602 SetPageCgroupCache(pc);
1603 SetPageCgroupUsed(pc);
1604 break;
1605 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1606 ClearPageCgroupCache(pc);
1607 SetPageCgroupUsed(pc);
1608 break;
1609 default:
1610 break;
1611 }
Hugh Dickins3be91272008-02-07 00:14:19 -08001612
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001613 mem_cgroup_charge_statistics(mem, pc, true);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001614
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001615 unlock_page_cgroup(pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001616}
1617
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001618/**
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001619 * __mem_cgroup_move_account - move account of the page
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001620 * @pc: page_cgroup of the page.
1621 * @from: mem_cgroup which the page is moved from.
1622 * @to: mem_cgroup which the page is moved to. @from != @to.
1623 *
1624 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001625 * - page is not on LRU (isolate_page() is useful.)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001626 * - the pc is locked, used, and ->mem_cgroup points to @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001627 *
1628 * This function does "uncharge" from old cgroup but doesn't do "charge" to
1629 * new cgroup. It should be done by a caller.
1630 */
1631
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001632static void __mem_cgroup_move_account(struct page_cgroup *pc,
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001633 struct mem_cgroup *from, struct mem_cgroup *to)
1634{
Balbir Singhd69b0422009-06-17 16:26:34 -07001635 struct page *page;
1636 int cpu;
1637 struct mem_cgroup_stat *stat;
1638 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001639
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001640 VM_BUG_ON(from == to);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001641 VM_BUG_ON(PageLRU(pc->page));
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001642 VM_BUG_ON(!PageCgroupLocked(pc));
1643 VM_BUG_ON(!PageCgroupUsed(pc));
1644 VM_BUG_ON(pc->mem_cgroup != from);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001645
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001646 if (!mem_cgroup_is_root(from))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001647 res_counter_uncharge(&from->res, PAGE_SIZE);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001648 mem_cgroup_charge_statistics(from, pc, false);
Balbir Singhd69b0422009-06-17 16:26:34 -07001649
1650 page = pc->page;
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001651 if (page_mapped(page) && !PageAnon(page)) {
Balbir Singhd69b0422009-06-17 16:26:34 -07001652 cpu = smp_processor_id();
1653 /* Update mapped_file data for mem_cgroup "from" */
1654 stat = &from->stat;
1655 cpustat = &stat->cpustat[cpu];
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001656 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED,
Balbir Singhd69b0422009-06-17 16:26:34 -07001657 -1);
1658
1659 /* Update mapped_file data for mem_cgroup "to" */
1660 stat = &to->stat;
1661 cpustat = &stat->cpustat[cpu];
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001662 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED,
Balbir Singhd69b0422009-06-17 16:26:34 -07001663 1);
1664 }
1665
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001666 if (do_swap_account && !mem_cgroup_is_root(from))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001667 res_counter_uncharge(&from->memsw, PAGE_SIZE);
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001668 css_put(&from->css);
1669
1670 css_get(&to->css);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001671 pc->mem_cgroup = to;
1672 mem_cgroup_charge_statistics(to, pc, true);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001673 /*
1674 * We charges against "to" which may not have any tasks. Then, "to"
1675 * can be under rmdir(). But in current implementation, caller of
1676 * this function is just force_empty() and it's garanteed that
1677 * "to" is never removed. So, we don't check rmdir status here.
1678 */
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001679}
1680
1681/*
1682 * check whether the @pc is valid for moving account and call
1683 * __mem_cgroup_move_account()
1684 */
1685static int mem_cgroup_move_account(struct page_cgroup *pc,
1686 struct mem_cgroup *from, struct mem_cgroup *to)
1687{
1688 int ret = -EINVAL;
1689 lock_page_cgroup(pc);
1690 if (PageCgroupUsed(pc) && pc->mem_cgroup == from) {
1691 __mem_cgroup_move_account(pc, from, to);
1692 ret = 0;
1693 }
1694 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001695 return ret;
1696}
1697
1698/*
1699 * move charges to its parent.
1700 */
1701
1702static int mem_cgroup_move_parent(struct page_cgroup *pc,
1703 struct mem_cgroup *child,
1704 gfp_t gfp_mask)
1705{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001706 struct page *page = pc->page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001707 struct cgroup *cg = child->css.cgroup;
1708 struct cgroup *pcg = cg->parent;
1709 struct mem_cgroup *parent;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001710 int ret;
1711
1712 /* Is ROOT ? */
1713 if (!pcg)
1714 return -EINVAL;
1715
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001716 ret = -EBUSY;
1717 if (!get_page_unless_zero(page))
1718 goto out;
1719 if (isolate_lru_page(page))
1720 goto put;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001721
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001722 parent = mem_cgroup_from_cont(pcg);
Balbir Singhf64c3f52009-09-23 15:56:37 -07001723 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false, page);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001724 if (ret || !parent)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001725 goto put_back;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001726
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001727 ret = mem_cgroup_move_account(pc, child, parent);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001728 if (!ret)
1729 css_put(&parent->css); /* drop extra refcnt by try_charge() */
1730 else
1731 mem_cgroup_cancel_charge(parent); /* does css_put */
1732put_back:
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001733 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001734put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001735 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001736out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001737 return ret;
1738}
1739
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001740/*
1741 * Charge the memory controller for page usage.
1742 * Return
1743 * 0 if the charge was successful
1744 * < 0 if the cgroup is over its limit
1745 */
1746static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
1747 gfp_t gfp_mask, enum charge_type ctype,
1748 struct mem_cgroup *memcg)
1749{
1750 struct mem_cgroup *mem;
1751 struct page_cgroup *pc;
1752 int ret;
1753
1754 pc = lookup_page_cgroup(page);
1755 /* can happen at boot */
1756 if (unlikely(!pc))
1757 return 0;
1758 prefetchw(pc);
1759
1760 mem = memcg;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001761 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true, page);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001762 if (ret || !mem)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001763 return ret;
1764
1765 __mem_cgroup_commit_charge(mem, pc, ctype);
1766 return 0;
1767}
1768
1769int mem_cgroup_newpage_charge(struct page *page,
1770 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001771{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001772 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001773 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001774 if (PageCompound(page))
1775 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001776 /*
1777 * If already mapped, we don't have to account.
1778 * If page cache, page->mapping has address_space.
1779 * But page->mapping may have out-of-use anon_vma pointer,
1780 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
1781 * is NULL.
1782 */
1783 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
1784 return 0;
1785 if (unlikely(!mm))
1786 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001787 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001788 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001789}
1790
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001791static void
1792__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1793 enum charge_type ctype);
1794
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001795int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
1796 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08001797{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001798 struct mem_cgroup *mem = NULL;
1799 int ret;
1800
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001801 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001802 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001803 if (PageCompound(page))
1804 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001805 /*
1806 * Corner case handling. This is called from add_to_page_cache()
1807 * in usual. But some FS (shmem) precharges this page before calling it
1808 * and call add_to_page_cache() with GFP_NOWAIT.
1809 *
1810 * For GFP_NOWAIT case, the page may be pre-charged before calling
1811 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
1812 * charge twice. (It works but has to pay a bit larger cost.)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001813 * And when the page is SwapCache, it should take swap information
1814 * into account. This is under lock_page() now.
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001815 */
1816 if (!(gfp_mask & __GFP_WAIT)) {
1817 struct page_cgroup *pc;
1818
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001819
1820 pc = lookup_page_cgroup(page);
1821 if (!pc)
1822 return 0;
1823 lock_page_cgroup(pc);
1824 if (PageCgroupUsed(pc)) {
1825 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001826 return 0;
1827 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001828 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001829 }
1830
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001831 if (unlikely(!mm && !mem))
Balbir Singh8697d332008-02-07 00:13:59 -08001832 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001833
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -07001834 if (page_is_file_cache(page))
1835 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001836 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001837
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001838 /* shmem */
1839 if (PageSwapCache(page)) {
1840 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1841 if (!ret)
1842 __mem_cgroup_commit_charge_swapin(page, mem,
1843 MEM_CGROUP_CHARGE_TYPE_SHMEM);
1844 } else
1845 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
1846 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001847
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001848 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001849}
1850
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001851/*
1852 * While swap-in, try_charge -> commit or cancel, the page is locked.
1853 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02001854 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001855 * "commit()" or removed by "cancel()"
1856 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001857int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
1858 struct page *page,
1859 gfp_t mask, struct mem_cgroup **ptr)
1860{
1861 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001862 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001863
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001864 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001865 return 0;
1866
1867 if (!do_swap_account)
1868 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001869 /*
1870 * A racing thread's fault, or swapoff, may have already updated
Hugh Dickins407f9c82009-12-14 17:59:30 -08001871 * the pte, and even removed page from swap cache: in those cases
1872 * do_swap_page()'s pte_same() test will fail; but there's also a
1873 * KSM case which does need to charge the page.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001874 */
1875 if (!PageSwapCache(page))
Hugh Dickins407f9c82009-12-14 17:59:30 -08001876 goto charge_cur_mm;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001877 mem = try_get_mem_cgroup_from_swapcache(page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001878 if (!mem)
1879 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001880 *ptr = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001881 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true, page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001882 /* drop extra refcnt from tryget */
1883 css_put(&mem->css);
1884 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001885charge_cur_mm:
1886 if (unlikely(!mm))
1887 mm = &init_mm;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001888 return __mem_cgroup_try_charge(mm, mask, ptr, true, page);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001889}
1890
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001891static void
1892__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1893 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001894{
1895 struct page_cgroup *pc;
1896
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001897 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001898 return;
1899 if (!ptr)
1900 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001901 cgroup_exclude_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001902 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001903 mem_cgroup_lru_del_before_commit_swapcache(page);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001904 __mem_cgroup_commit_charge(ptr, pc, ctype);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001905 mem_cgroup_lru_add_after_commit_swapcache(page);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001906 /*
1907 * Now swap is on-memory. This means this page may be
1908 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08001909 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
1910 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
1911 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001912 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08001913 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001914 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001915 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001916 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001917
1918 id = swap_cgroup_record(ent, 0);
1919 rcu_read_lock();
1920 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001921 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001922 /*
1923 * This recorded memcg can be obsolete one. So, avoid
1924 * calling css_tryget
1925 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001926 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001927 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001928 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001929 mem_cgroup_put(memcg);
1930 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001931 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001932 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001933 /*
1934 * At swapin, we may charge account against cgroup which has no tasks.
1935 * So, rmdir()->pre_destroy() can be called while we do this charge.
1936 * In that case, we need to call pre_destroy() again. check it here.
1937 */
1938 cgroup_release_and_wakeup_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001939}
1940
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001941void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
1942{
1943 __mem_cgroup_commit_charge_swapin(page, ptr,
1944 MEM_CGROUP_CHARGE_TYPE_MAPPED);
1945}
1946
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001947void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
1948{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001949 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001950 return;
1951 if (!mem)
1952 return;
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001953 mem_cgroup_cancel_charge(mem);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001954}
1955
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08001956static void
1957__do_uncharge(struct mem_cgroup *mem, const enum charge_type ctype)
1958{
1959 struct memcg_batch_info *batch = NULL;
1960 bool uncharge_memsw = true;
1961 /* If swapout, usage of swap doesn't decrease */
1962 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1963 uncharge_memsw = false;
1964 /*
1965 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
1966 * In those cases, all pages freed continously can be expected to be in
1967 * the same cgroup and we have chance to coalesce uncharges.
1968 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
1969 * because we want to do uncharge as soon as possible.
1970 */
1971 if (!current->memcg_batch.do_batch || test_thread_flag(TIF_MEMDIE))
1972 goto direct_uncharge;
1973
1974 batch = &current->memcg_batch;
1975 /*
1976 * In usual, we do css_get() when we remember memcg pointer.
1977 * But in this case, we keep res->usage until end of a series of
1978 * uncharges. Then, it's ok to ignore memcg's refcnt.
1979 */
1980 if (!batch->memcg)
1981 batch->memcg = mem;
1982 /*
1983 * In typical case, batch->memcg == mem. This means we can
1984 * merge a series of uncharges to an uncharge of res_counter.
1985 * If not, we uncharge res_counter ony by one.
1986 */
1987 if (batch->memcg != mem)
1988 goto direct_uncharge;
1989 /* remember freed charge and uncharge it later */
1990 batch->bytes += PAGE_SIZE;
1991 if (uncharge_memsw)
1992 batch->memsw_bytes += PAGE_SIZE;
1993 return;
1994direct_uncharge:
1995 res_counter_uncharge(&mem->res, PAGE_SIZE);
1996 if (uncharge_memsw)
1997 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1998 return;
1999}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002000
Balbir Singh8697d332008-02-07 00:13:59 -08002001/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002002 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002003 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002004static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002005__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002006{
Hugh Dickins82895462008-03-04 14:29:08 -08002007 struct page_cgroup *pc;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002008 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002009 struct mem_cgroup_per_zone *mz;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002010
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002011 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002012 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07002013
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002014 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002015 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002016
Balbir Singh8697d332008-02-07 00:13:59 -08002017 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08002018 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08002019 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002020 pc = lookup_page_cgroup(page);
2021 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002022 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002023
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002024 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002025
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002026 mem = pc->mem_cgroup;
2027
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002028 if (!PageCgroupUsed(pc))
2029 goto unlock_out;
2030
2031 switch (ctype) {
2032 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002033 case MEM_CGROUP_CHARGE_TYPE_DROP:
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002034 if (page_mapped(page))
2035 goto unlock_out;
2036 break;
2037 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
2038 if (!PageAnon(page)) { /* Shared memory */
2039 if (page->mapping && !page_is_file_cache(page))
2040 goto unlock_out;
2041 } else if (page_mapped(page)) /* Anon */
2042 goto unlock_out;
2043 break;
2044 default:
2045 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002046 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002047
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002048 if (!mem_cgroup_is_root(mem))
2049 __do_uncharge(mem, ctype);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002050 if (ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2051 mem_cgroup_swap_statistics(mem, true);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002052 mem_cgroup_charge_statistics(mem, pc, false);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002053
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002054 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08002055 /*
2056 * pc->mem_cgroup is not cleared here. It will be accessed when it's
2057 * freed from LRU. This is safe because uncharged page is expected not
2058 * to be reused (freed soon). Exception is SwapCache, it's handled by
2059 * special functions.
2060 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08002061
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002062 mz = page_cgroup_zoneinfo(pc);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002063 unlock_page_cgroup(pc);
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08002064
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002065 if (mem_cgroup_soft_limit_check(mem))
Balbir Singhf64c3f52009-09-23 15:56:37 -07002066 mem_cgroup_update_tree(mem, page);
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08002067 /* at swapout, this memcg will be accessed to record to swap */
2068 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2069 css_put(&mem->css);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002070
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002071 return mem;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002072
2073unlock_out:
2074 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002075 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08002076}
2077
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002078void mem_cgroup_uncharge_page(struct page *page)
2079{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002080 /* early check. */
2081 if (page_mapped(page))
2082 return;
2083 if (page->mapping && !PageAnon(page))
2084 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002085 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
2086}
2087
2088void mem_cgroup_uncharge_cache_page(struct page *page)
2089{
2090 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07002091 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002092 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
2093}
2094
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002095/*
2096 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
2097 * In that cases, pages are freed continuously and we can expect pages
2098 * are in the same memcg. All these calls itself limits the number of
2099 * pages freed at once, then uncharge_start/end() is called properly.
2100 * This may be called prural(2) times in a context,
2101 */
2102
2103void mem_cgroup_uncharge_start(void)
2104{
2105 current->memcg_batch.do_batch++;
2106 /* We can do nest. */
2107 if (current->memcg_batch.do_batch == 1) {
2108 current->memcg_batch.memcg = NULL;
2109 current->memcg_batch.bytes = 0;
2110 current->memcg_batch.memsw_bytes = 0;
2111 }
2112}
2113
2114void mem_cgroup_uncharge_end(void)
2115{
2116 struct memcg_batch_info *batch = &current->memcg_batch;
2117
2118 if (!batch->do_batch)
2119 return;
2120
2121 batch->do_batch--;
2122 if (batch->do_batch) /* If stacked, do nothing. */
2123 return;
2124
2125 if (!batch->memcg)
2126 return;
2127 /*
2128 * This "batch->memcg" is valid without any css_get/put etc...
2129 * bacause we hide charges behind us.
2130 */
2131 if (batch->bytes)
2132 res_counter_uncharge(&batch->memcg->res, batch->bytes);
2133 if (batch->memsw_bytes)
2134 res_counter_uncharge(&batch->memcg->memsw, batch->memsw_bytes);
2135 /* forget this pointer (for sanity check) */
2136 batch->memcg = NULL;
2137}
2138
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002139#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002140/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002141 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002142 * memcg information is recorded to swap_cgroup of "ent"
2143 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002144void
2145mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002146{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002147 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002148 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002149
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002150 if (!swapout) /* this was a swap cache but the swap is unused ! */
2151 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
2152
2153 memcg = __mem_cgroup_uncharge_common(page, ctype);
2154
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002155 /* record memcg information */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002156 if (do_swap_account && swapout && memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002157 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002158 mem_cgroup_get(memcg);
2159 }
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002160 if (swapout && memcg)
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08002161 css_put(&memcg->css);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002162}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002163#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002164
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002165#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2166/*
2167 * called from swap_entry_free(). remove record in swap_cgroup and
2168 * uncharge "memsw" account.
2169 */
2170void mem_cgroup_uncharge_swap(swp_entry_t ent)
2171{
2172 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002173 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002174
2175 if (!do_swap_account)
2176 return;
2177
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002178 id = swap_cgroup_record(ent, 0);
2179 rcu_read_lock();
2180 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002181 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002182 /*
2183 * We uncharge this because swap is freed.
2184 * This memcg can be obsolete one. We avoid calling css_tryget
2185 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002186 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002187 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002188 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002189 mem_cgroup_put(memcg);
2190 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002191 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002192}
2193#endif
2194
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002195/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002196 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
2197 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002198 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002199int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002200{
2201 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002202 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002203 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08002204
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002205 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07002206 return 0;
2207
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002208 pc = lookup_page_cgroup(page);
2209 lock_page_cgroup(pc);
2210 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002211 mem = pc->mem_cgroup;
2212 css_get(&mem->css);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08002213 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002214 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002215
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002216 if (mem) {
Balbir Singhf64c3f52009-09-23 15:56:37 -07002217 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false,
2218 page);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002219 css_put(&mem->css);
2220 }
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002221 *ptr = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002222 return ret;
2223}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08002224
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002225/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002226void mem_cgroup_end_migration(struct mem_cgroup *mem,
2227 struct page *oldpage, struct page *newpage)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002228{
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002229 struct page *target, *unused;
2230 struct page_cgroup *pc;
2231 enum charge_type ctype;
2232
2233 if (!mem)
2234 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002235 cgroup_exclude_rmdir(&mem->css);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002236 /* at migration success, oldpage->mapping is NULL. */
2237 if (oldpage->mapping) {
2238 target = oldpage;
2239 unused = NULL;
2240 } else {
2241 target = newpage;
2242 unused = oldpage;
2243 }
2244
2245 if (PageAnon(target))
2246 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
2247 else if (page_is_file_cache(target))
2248 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
2249 else
2250 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
2251
2252 /* unused page is not on radix-tree now. */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002253 if (unused)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002254 __mem_cgroup_uncharge_common(unused, ctype);
2255
2256 pc = lookup_page_cgroup(target);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002257 /*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002258 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
2259 * So, double-counting is effectively avoided.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002260 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002261 __mem_cgroup_commit_charge(mem, pc, ctype);
2262
2263 /*
2264 * Both of oldpage and newpage are still under lock_page().
2265 * Then, we don't have to care about race in radix-tree.
2266 * But we have to be careful that this page is unmapped or not.
2267 *
2268 * There is a case for !page_mapped(). At the start of
2269 * migration, oldpage was mapped. But now, it's zapped.
2270 * But we know *target* page is not freed/reused under us.
2271 * mem_cgroup_uncharge_page() does all necessary checks.
2272 */
2273 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
2274 mem_cgroup_uncharge_page(target);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002275 /*
2276 * At migration, we may charge account against cgroup which has no tasks
2277 * So, rmdir()->pre_destroy() can be called while we do this charge.
2278 * In that case, we need to call pre_destroy() again. check it here.
2279 */
2280 cgroup_release_and_wakeup_rmdir(&mem->css);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002281}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08002282
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002283/*
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002284 * A call to try to shrink memory usage on charge failure at shmem's swapin.
2285 * Calling hierarchical_reclaim is not enough because we should update
2286 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
2287 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
2288 * not from the memcg which this page would be charged to.
2289 * try_charge_swapin does all of these works properly.
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002290 */
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002291int mem_cgroup_shmem_charge_fallback(struct page *page,
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002292 struct mm_struct *mm,
2293 gfp_t gfp_mask)
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002294{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002295 struct mem_cgroup *mem = NULL;
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002296 int ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002297
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002298 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002299 return 0;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002300
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002301 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2302 if (!ret)
2303 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002304
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002305 return ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002306}
2307
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002308static DEFINE_MUTEX(set_limit_mutex);
2309
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08002310static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002311 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002312{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002313 int retry_count;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002314 int progress;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002315 u64 memswlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002316 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002317 int children = mem_cgroup_count_children(memcg);
2318 u64 curusage, oldusage;
2319
2320 /*
2321 * For keeping hierarchical_reclaim simple, how long we should retry
2322 * is depends on callers. We set our retry-count to be function
2323 * of # of children which we should visit in this loop.
2324 */
2325 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
2326
2327 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002328
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002329 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002330 if (signal_pending(current)) {
2331 ret = -EINTR;
2332 break;
2333 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002334 /*
2335 * Rather than hide all in some function, I do this in
2336 * open coded manner. You see what this really does.
2337 * We have to guarantee mem->res.limit < mem->memsw.limit.
2338 */
2339 mutex_lock(&set_limit_mutex);
2340 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2341 if (memswlimit < val) {
2342 ret = -EINVAL;
2343 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002344 break;
2345 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002346 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07002347 if (!ret) {
2348 if (memswlimit == val)
2349 memcg->memsw_is_minimum = true;
2350 else
2351 memcg->memsw_is_minimum = false;
2352 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002353 mutex_unlock(&set_limit_mutex);
2354
2355 if (!ret)
2356 break;
2357
Balbir Singh4e416952009-09-23 15:56:39 -07002358 progress = mem_cgroup_hierarchical_reclaim(memcg, NULL,
2359 GFP_KERNEL,
2360 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:
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002589 while (mem->res.usage > 0) {
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();
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002617 }
2618 ret = 0;
2619out:
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. */
2651 if (mem->res.usage)
2652 goto move_account;
2653 ret = 0;
2654 goto out;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002655}
2656
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002657int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
2658{
2659 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
2660}
2661
2662
Balbir Singh18f59ea2009-01-07 18:08:07 -08002663static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
2664{
2665 return mem_cgroup_from_cont(cont)->use_hierarchy;
2666}
2667
2668static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
2669 u64 val)
2670{
2671 int retval = 0;
2672 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2673 struct cgroup *parent = cont->parent;
2674 struct mem_cgroup *parent_mem = NULL;
2675
2676 if (parent)
2677 parent_mem = mem_cgroup_from_cont(parent);
2678
2679 cgroup_lock();
2680 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002681 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08002682 * in the child subtrees. If it is unset, then the change can
2683 * occur, provided the current cgroup has no children.
2684 *
2685 * For the root cgroup, parent_mem is NULL, we allow value to be
2686 * set if there are no children.
2687 */
2688 if ((!parent_mem || !parent_mem->use_hierarchy) &&
2689 (val == 1 || val == 0)) {
2690 if (list_empty(&cont->children))
2691 mem->use_hierarchy = val;
2692 else
2693 retval = -EBUSY;
2694 } else
2695 retval = -EINVAL;
2696 cgroup_unlock();
2697
2698 return retval;
2699}
2700
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002701struct mem_cgroup_idx_data {
2702 s64 val;
2703 enum mem_cgroup_stat_index idx;
2704};
2705
2706static int
2707mem_cgroup_get_idx_stat(struct mem_cgroup *mem, void *data)
2708{
2709 struct mem_cgroup_idx_data *d = data;
2710 d->val += mem_cgroup_read_stat(&mem->stat, d->idx);
2711 return 0;
2712}
2713
2714static void
2715mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem,
2716 enum mem_cgroup_stat_index idx, s64 *val)
2717{
2718 struct mem_cgroup_idx_data d;
2719 d.idx = idx;
2720 d.val = 0;
2721 mem_cgroup_walk_tree(mem, &d, mem_cgroup_get_idx_stat);
2722 *val = d.val;
2723}
2724
Paul Menage2c3daa72008-04-29 00:59:58 -07002725static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002726{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002727 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002728 u64 idx_val, val;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002729 int type, name;
2730
2731 type = MEMFILE_TYPE(cft->private);
2732 name = MEMFILE_ATTR(cft->private);
2733 switch (type) {
2734 case _MEM:
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002735 if (name == RES_USAGE && mem_cgroup_is_root(mem)) {
2736 mem_cgroup_get_recursive_idx_stat(mem,
2737 MEM_CGROUP_STAT_CACHE, &idx_val);
2738 val = idx_val;
2739 mem_cgroup_get_recursive_idx_stat(mem,
2740 MEM_CGROUP_STAT_RSS, &idx_val);
2741 val += idx_val;
2742 val <<= PAGE_SHIFT;
2743 } else
2744 val = res_counter_read_u64(&mem->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002745 break;
2746 case _MEMSWAP:
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002747 if (name == RES_USAGE && mem_cgroup_is_root(mem)) {
2748 mem_cgroup_get_recursive_idx_stat(mem,
2749 MEM_CGROUP_STAT_CACHE, &idx_val);
2750 val = idx_val;
2751 mem_cgroup_get_recursive_idx_stat(mem,
2752 MEM_CGROUP_STAT_RSS, &idx_val);
2753 val += idx_val;
2754 mem_cgroup_get_recursive_idx_stat(mem,
2755 MEM_CGROUP_STAT_SWAPOUT, &idx_val);
Kirill A. Shutemovcd9b45b2009-12-15 16:47:01 -08002756 val += idx_val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002757 val <<= PAGE_SHIFT;
2758 } else
2759 val = res_counter_read_u64(&mem->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002760 break;
2761 default:
2762 BUG();
2763 break;
2764 }
2765 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002766}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002767/*
2768 * The user of this function is...
2769 * RES_LIMIT.
2770 */
Paul Menage856c13a2008-07-25 01:47:04 -07002771static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
2772 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002773{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002774 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002775 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002776 unsigned long long val;
2777 int ret;
2778
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002779 type = MEMFILE_TYPE(cft->private);
2780 name = MEMFILE_ATTR(cft->private);
2781 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002782 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07002783 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
2784 ret = -EINVAL;
2785 break;
2786 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002787 /* This function does all necessary parse...reuse it */
2788 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002789 if (ret)
2790 break;
2791 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002792 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002793 else
2794 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002795 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07002796 case RES_SOFT_LIMIT:
2797 ret = res_counter_memparse_write_strategy(buffer, &val);
2798 if (ret)
2799 break;
2800 /*
2801 * For memsw, soft limits are hard to implement in terms
2802 * of semantics, for now, we support soft limits for
2803 * control without swap
2804 */
2805 if (type == _MEM)
2806 ret = res_counter_set_soft_limit(&memcg->res, val);
2807 else
2808 ret = -EINVAL;
2809 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002810 default:
2811 ret = -EINVAL; /* should be BUG() ? */
2812 break;
2813 }
2814 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002815}
2816
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08002817static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
2818 unsigned long long *mem_limit, unsigned long long *memsw_limit)
2819{
2820 struct cgroup *cgroup;
2821 unsigned long long min_limit, min_memsw_limit, tmp;
2822
2823 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2824 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2825 cgroup = memcg->css.cgroup;
2826 if (!memcg->use_hierarchy)
2827 goto out;
2828
2829 while (cgroup->parent) {
2830 cgroup = cgroup->parent;
2831 memcg = mem_cgroup_from_cont(cgroup);
2832 if (!memcg->use_hierarchy)
2833 break;
2834 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
2835 min_limit = min(min_limit, tmp);
2836 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2837 min_memsw_limit = min(min_memsw_limit, tmp);
2838 }
2839out:
2840 *mem_limit = min_limit;
2841 *memsw_limit = min_memsw_limit;
2842 return;
2843}
2844
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002845static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002846{
2847 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002848 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002849
2850 mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002851 type = MEMFILE_TYPE(event);
2852 name = MEMFILE_ATTR(event);
2853 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002854 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002855 if (type == _MEM)
2856 res_counter_reset_max(&mem->res);
2857 else
2858 res_counter_reset_max(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002859 break;
2860 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002861 if (type == _MEM)
2862 res_counter_reset_failcnt(&mem->res);
2863 else
2864 res_counter_reset_failcnt(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002865 break;
2866 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07002867
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07002868 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002869}
2870
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002871
2872/* For read statistics */
2873enum {
2874 MCS_CACHE,
2875 MCS_RSS,
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08002876 MCS_FILE_MAPPED,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002877 MCS_PGPGIN,
2878 MCS_PGPGOUT,
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002879 MCS_SWAP,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002880 MCS_INACTIVE_ANON,
2881 MCS_ACTIVE_ANON,
2882 MCS_INACTIVE_FILE,
2883 MCS_ACTIVE_FILE,
2884 MCS_UNEVICTABLE,
2885 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002886};
2887
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002888struct mcs_total_stat {
2889 s64 stat[NR_MCS_STAT];
2890};
2891
2892struct {
2893 char *local_name;
2894 char *total_name;
2895} memcg_stat_strings[NR_MCS_STAT] = {
2896 {"cache", "total_cache"},
2897 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07002898 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002899 {"pgpgin", "total_pgpgin"},
2900 {"pgpgout", "total_pgpgout"},
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002901 {"swap", "total_swap"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002902 {"inactive_anon", "total_inactive_anon"},
2903 {"active_anon", "total_active_anon"},
2904 {"inactive_file", "total_inactive_file"},
2905 {"active_file", "total_active_file"},
2906 {"unevictable", "total_unevictable"}
2907};
2908
2909
2910static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
2911{
2912 struct mcs_total_stat *s = data;
2913 s64 val;
2914
2915 /* per cpu stat */
2916 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE);
2917 s->stat[MCS_CACHE] += val * PAGE_SIZE;
2918 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
2919 s->stat[MCS_RSS] += val * PAGE_SIZE;
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08002920 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_FILE_MAPPED);
2921 s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002922 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT);
2923 s->stat[MCS_PGPGIN] += val;
2924 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT);
2925 s->stat[MCS_PGPGOUT] += val;
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002926 if (do_swap_account) {
2927 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_SWAPOUT);
2928 s->stat[MCS_SWAP] += val * PAGE_SIZE;
2929 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002930
2931 /* per zone stat */
2932 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
2933 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
2934 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
2935 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
2936 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
2937 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
2938 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
2939 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
2940 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
2941 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
2942 return 0;
2943}
2944
2945static void
2946mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
2947{
2948 mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
2949}
2950
Paul Menagec64745c2008-04-29 01:00:02 -07002951static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
2952 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002953{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002954 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002955 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002956 int i;
2957
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002958 memset(&mystat, 0, sizeof(mystat));
2959 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002960
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002961 for (i = 0; i < NR_MCS_STAT; i++) {
2962 if (i == MCS_SWAP && !do_swap_account)
2963 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002964 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002965 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002966
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002967 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08002968 {
2969 unsigned long long limit, memsw_limit;
2970 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
2971 cb->fill(cb, "hierarchical_memory_limit", limit);
2972 if (do_swap_account)
2973 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
2974 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002975
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002976 memset(&mystat, 0, sizeof(mystat));
2977 mem_cgroup_get_total_stat(mem_cont, &mystat);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002978 for (i = 0; i < NR_MCS_STAT; i++) {
2979 if (i == MCS_SWAP && !do_swap_account)
2980 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002981 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002982 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002983
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002984#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08002985 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002986
2987 {
2988 int nid, zid;
2989 struct mem_cgroup_per_zone *mz;
2990 unsigned long recent_rotated[2] = {0, 0};
2991 unsigned long recent_scanned[2] = {0, 0};
2992
2993 for_each_online_node(nid)
2994 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
2995 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
2996
2997 recent_rotated[0] +=
2998 mz->reclaim_stat.recent_rotated[0];
2999 recent_rotated[1] +=
3000 mz->reclaim_stat.recent_rotated[1];
3001 recent_scanned[0] +=
3002 mz->reclaim_stat.recent_scanned[0];
3003 recent_scanned[1] +=
3004 mz->reclaim_stat.recent_scanned[1];
3005 }
3006 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
3007 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
3008 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
3009 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
3010 }
3011#endif
3012
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003013 return 0;
3014}
3015
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003016static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
3017{
3018 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3019
3020 return get_swappiness(memcg);
3021}
3022
3023static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
3024 u64 val)
3025{
3026 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3027 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08003028
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003029 if (val > 100)
3030 return -EINVAL;
3031
3032 if (cgrp->parent == NULL)
3033 return -EINVAL;
3034
3035 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08003036
3037 cgroup_lock();
3038
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003039 /* If under hierarchy, only empty-root can set this value */
3040 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08003041 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
3042 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003043 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08003044 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003045
3046 spin_lock(&memcg->reclaim_param_lock);
3047 memcg->swappiness = val;
3048 spin_unlock(&memcg->reclaim_param_lock);
3049
Li Zefan068b38c2009-01-15 13:51:26 -08003050 cgroup_unlock();
3051
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003052 return 0;
3053}
3054
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003055
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003056static struct cftype mem_cgroup_files[] = {
3057 {
Balbir Singh0eea1032008-02-07 00:13:57 -08003058 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003059 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07003060 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003061 },
3062 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003063 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003064 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003065 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003066 .read_u64 = mem_cgroup_read,
3067 },
3068 {
Balbir Singh0eea1032008-02-07 00:13:57 -08003069 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003070 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07003071 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07003072 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003073 },
3074 {
Balbir Singh296c81d2009-09-23 15:56:36 -07003075 .name = "soft_limit_in_bytes",
3076 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
3077 .write_string = mem_cgroup_write,
3078 .read_u64 = mem_cgroup_read,
3079 },
3080 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003081 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003082 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003083 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07003084 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003085 },
Balbir Singh8697d332008-02-07 00:13:59 -08003086 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003087 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07003088 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003089 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003090 {
3091 .name = "force_empty",
3092 .trigger = mem_cgroup_force_empty_write,
3093 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08003094 {
3095 .name = "use_hierarchy",
3096 .write_u64 = mem_cgroup_hierarchy_write,
3097 .read_u64 = mem_cgroup_hierarchy_read,
3098 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003099 {
3100 .name = "swappiness",
3101 .read_u64 = mem_cgroup_swappiness_read,
3102 .write_u64 = mem_cgroup_swappiness_write,
3103 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003104};
3105
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003106#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3107static struct cftype memsw_cgroup_files[] = {
3108 {
3109 .name = "memsw.usage_in_bytes",
3110 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
3111 .read_u64 = mem_cgroup_read,
3112 },
3113 {
3114 .name = "memsw.max_usage_in_bytes",
3115 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
3116 .trigger = mem_cgroup_reset,
3117 .read_u64 = mem_cgroup_read,
3118 },
3119 {
3120 .name = "memsw.limit_in_bytes",
3121 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
3122 .write_string = mem_cgroup_write,
3123 .read_u64 = mem_cgroup_read,
3124 },
3125 {
3126 .name = "memsw.failcnt",
3127 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
3128 .trigger = mem_cgroup_reset,
3129 .read_u64 = mem_cgroup_read,
3130 },
3131};
3132
3133static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
3134{
3135 if (!do_swap_account)
3136 return 0;
3137 return cgroup_add_files(cont, ss, memsw_cgroup_files,
3138 ARRAY_SIZE(memsw_cgroup_files));
3139};
3140#else
3141static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
3142{
3143 return 0;
3144}
3145#endif
3146
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003147static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
3148{
3149 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003150 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07003151 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07003152 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003153 /*
3154 * This routine is called against possible nodes.
3155 * But it's BUG to call kmalloc() against offline node.
3156 *
3157 * TODO: this routine can waste much memory for nodes which will
3158 * never be onlined. It's better to use memory hotplug callback
3159 * function.
3160 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07003161 if (!node_state(node, N_NORMAL_MEMORY))
3162 tmp = -1;
3163 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003164 if (!pn)
3165 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003166
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003167 mem->info.nodeinfo[node] = pn;
3168 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003169
3170 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
3171 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07003172 for_each_lru(l)
3173 INIT_LIST_HEAD(&mz->lists[l]);
Balbir Singhf64c3f52009-09-23 15:56:37 -07003174 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07003175 mz->on_tree = false;
3176 mz->mem = mem;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003177 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003178 return 0;
3179}
3180
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003181static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
3182{
3183 kfree(mem->info.nodeinfo[node]);
3184}
3185
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003186static int mem_cgroup_size(void)
3187{
3188 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
3189 return sizeof(struct mem_cgroup) + cpustat_size;
3190}
3191
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003192static struct mem_cgroup *mem_cgroup_alloc(void)
3193{
3194 struct mem_cgroup *mem;
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003195 int size = mem_cgroup_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003196
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003197 if (size < PAGE_SIZE)
3198 mem = kmalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003199 else
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003200 mem = vmalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003201
3202 if (mem)
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003203 memset(mem, 0, size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003204 return mem;
3205}
3206
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003207/*
3208 * At destroying mem_cgroup, references from swap_cgroup can remain.
3209 * (scanning all at force_empty is too costly...)
3210 *
3211 * Instead of clearing all references at force_empty, we remember
3212 * the number of reference from swap_cgroup and free mem_cgroup when
3213 * it goes down to 0.
3214 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003215 * Removal of cgroup itself succeeds regardless of refs from swap.
3216 */
3217
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003218static void __mem_cgroup_free(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003219{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003220 int node;
3221
Balbir Singhf64c3f52009-09-23 15:56:37 -07003222 mem_cgroup_remove_from_trees(mem);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003223 free_css_id(&mem_cgroup_subsys, &mem->css);
3224
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003225 for_each_node_state(node, N_POSSIBLE)
3226 free_mem_cgroup_per_zone_info(mem, node);
3227
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003228 if (mem_cgroup_size() < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003229 kfree(mem);
3230 else
3231 vfree(mem);
3232}
3233
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003234static void mem_cgroup_get(struct mem_cgroup *mem)
3235{
3236 atomic_inc(&mem->refcnt);
3237}
3238
3239static void mem_cgroup_put(struct mem_cgroup *mem)
3240{
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003241 if (atomic_dec_and_test(&mem->refcnt)) {
3242 struct mem_cgroup *parent = parent_mem_cgroup(mem);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003243 __mem_cgroup_free(mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003244 if (parent)
3245 mem_cgroup_put(parent);
3246 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003247}
3248
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003249/*
3250 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
3251 */
3252static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
3253{
3254 if (!mem->res.parent)
3255 return NULL;
3256 return mem_cgroup_from_res_counter(mem->res.parent, res);
3257}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003258
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003259#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3260static void __init enable_swap_cgroup(void)
3261{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003262 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003263 do_swap_account = 1;
3264}
3265#else
3266static void __init enable_swap_cgroup(void)
3267{
3268}
3269#endif
3270
Balbir Singhf64c3f52009-09-23 15:56:37 -07003271static int mem_cgroup_soft_limit_tree_init(void)
3272{
3273 struct mem_cgroup_tree_per_node *rtpn;
3274 struct mem_cgroup_tree_per_zone *rtpz;
3275 int tmp, node, zone;
3276
3277 for_each_node_state(node, N_POSSIBLE) {
3278 tmp = node;
3279 if (!node_state(node, N_NORMAL_MEMORY))
3280 tmp = -1;
3281 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
3282 if (!rtpn)
3283 return 1;
3284
3285 soft_limit_tree.rb_tree_per_node[node] = rtpn;
3286
3287 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
3288 rtpz = &rtpn->rb_tree_per_zone[zone];
3289 rtpz->rb_root = RB_ROOT;
3290 spin_lock_init(&rtpz->lock);
3291 }
3292 }
3293 return 0;
3294}
3295
Li Zefan0eb253e2009-01-15 13:51:25 -08003296static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003297mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
3298{
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003299 struct mem_cgroup *mem, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003300 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003301 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003302
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003303 mem = mem_cgroup_alloc();
3304 if (!mem)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003305 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08003306
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003307 for_each_node_state(node, N_POSSIBLE)
3308 if (alloc_mem_cgroup_per_zone_info(mem, node))
3309 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07003310
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003311 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003312 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08003313 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003314 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003315 parent = NULL;
Balbir Singh4b3bde42009-09-23 15:56:32 -07003316 root_mem_cgroup = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07003317 if (mem_cgroup_soft_limit_tree_init())
3318 goto free_out;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08003319 for_each_possible_cpu(cpu) {
3320 struct memcg_stock_pcp *stock =
3321 &per_cpu(memcg_stock, cpu);
3322 INIT_WORK(&stock->work, drain_local_stock);
3323 }
3324 hotcpu_notifier(memcg_stock_cpu_callback, 0);
Balbir Singhf64c3f52009-09-23 15:56:37 -07003325
Balbir Singh18f59ea2009-01-07 18:08:07 -08003326 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003327 parent = mem_cgroup_from_cont(cont->parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003328 mem->use_hierarchy = parent->use_hierarchy;
3329 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003330
Balbir Singh18f59ea2009-01-07 18:08:07 -08003331 if (parent && parent->use_hierarchy) {
3332 res_counter_init(&mem->res, &parent->res);
3333 res_counter_init(&mem->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003334 /*
3335 * We increment refcnt of the parent to ensure that we can
3336 * safely access it on res_counter_charge/uncharge.
3337 * This refcnt will be decremented when freeing this
3338 * mem_cgroup(see mem_cgroup_put).
3339 */
3340 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003341 } else {
3342 res_counter_init(&mem->res, NULL);
3343 res_counter_init(&mem->memsw, NULL);
3344 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003345 mem->last_scanned_child = 0;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -08003346 spin_lock_init(&mem->reclaim_param_lock);
Balbir Singh6d61ef42009-01-07 18:08:06 -08003347
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003348 if (parent)
3349 mem->swappiness = get_swappiness(parent);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003350 atomic_set(&mem->refcnt, 1);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003351 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003352free_out:
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003353 __mem_cgroup_free(mem);
Balbir Singh4b3bde42009-09-23 15:56:32 -07003354 root_mem_cgroup = NULL;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003355 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003356}
3357
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003358static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08003359 struct cgroup *cont)
3360{
3361 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003362
3363 return mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08003364}
3365
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003366static void mem_cgroup_destroy(struct cgroup_subsys *ss,
3367 struct cgroup *cont)
3368{
Daisuke Nishimurac268e992009-01-15 13:51:13 -08003369 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08003370
Daisuke Nishimurac268e992009-01-15 13:51:13 -08003371 mem_cgroup_put(mem);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003372}
3373
3374static int mem_cgroup_populate(struct cgroup_subsys *ss,
3375 struct cgroup *cont)
3376{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003377 int ret;
3378
3379 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
3380 ARRAY_SIZE(mem_cgroup_files));
3381
3382 if (!ret)
3383 ret = register_memsw_files(cont, ss);
3384 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003385}
3386
Balbir Singh67e465a2008-02-07 00:13:54 -08003387static void mem_cgroup_move_task(struct cgroup_subsys *ss,
3388 struct cgroup *cont,
3389 struct cgroup *old_cont,
Ben Blumbe367d02009-09-23 15:56:31 -07003390 struct task_struct *p,
3391 bool threadgroup)
Balbir Singh67e465a2008-02-07 00:13:54 -08003392{
Balbir Singh67e465a2008-02-07 00:13:54 -08003393 /*
Nikanth Karthikesanf9717d22009-01-07 18:08:11 -08003394 * FIXME: It's better to move charges of this process from old
3395 * memcg to new memcg. But it's just on TODO-List now.
Balbir Singh67e465a2008-02-07 00:13:54 -08003396 */
Balbir Singh67e465a2008-02-07 00:13:54 -08003397}
3398
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003399struct cgroup_subsys mem_cgroup_subsys = {
3400 .name = "memory",
3401 .subsys_id = mem_cgroup_subsys_id,
3402 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08003403 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003404 .destroy = mem_cgroup_destroy,
3405 .populate = mem_cgroup_populate,
Balbir Singh67e465a2008-02-07 00:13:54 -08003406 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003407 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003408 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003409};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003410
3411#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3412
3413static int __init disable_swap_account(char *s)
3414{
3415 really_do_swap_account = 0;
3416 return 1;
3417}
3418__setup("noswapaccount", disable_swap_account);
3419#endif