blob: e2b98a6875c079b36f103b724130462591094735 [file] [log] [blame]
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001/* memcontrol.c - Memory Controller
2 *
3 * Copyright IBM Corporation, 2007
4 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
5 *
Pavel Emelianov78fb7462008-02-07 00:13:51 -08006 * Copyright 2007 OpenVZ SWsoft Inc
7 * Author: Pavel Emelianov <xemul@openvz.org>
8 *
Balbir Singh8cdea7c2008-02-07 00:13:50 -08009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 */
19
20#include <linux/res_counter.h>
21#include <linux/memcontrol.h>
22#include <linux/cgroup.h>
Pavel Emelianov78fb7462008-02-07 00:13:51 -080023#include <linux/mm.h>
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -080024#include <linux/pagemap.h>
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080025#include <linux/smp.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080026#include <linux/page-flags.h>
Balbir Singh66e17072008-02-07 00:13:56 -080027#include <linux/backing-dev.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080028#include <linux/bit_spinlock.h>
29#include <linux/rcupdate.h>
Balbir Singhe2224322009-04-02 16:57:39 -070030#include <linux/limits.h>
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -080031#include <linux/mutex.h>
Balbir Singhf64c3f52009-09-23 15:56:37 -070032#include <linux/rbtree.h>
Balbir Singhb6ac57d2008-04-29 01:00:19 -070033#include <linux/slab.h>
Balbir Singh66e17072008-02-07 00:13:56 -080034#include <linux/swap.h>
35#include <linux/spinlock.h>
36#include <linux/fs.h>
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -080037#include <linux/seq_file.h>
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -070038#include <linux/vmalloc.h>
Christoph Lameterb69408e2008-10-18 20:26:14 -070039#include <linux/mm_inline.h>
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070040#include <linux/page_cgroup.h>
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -080041#include "internal.h"
Balbir Singh8cdea7c2008-02-07 00:13:50 -080042
Balbir Singh8697d332008-02-07 00:13:59 -080043#include <asm/uaccess.h>
44
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070045struct cgroup_subsys mem_cgroup_subsys __read_mostly;
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070046#define MEM_CGROUP_RECLAIM_RETRIES 5
Balbir Singh4b3bde42009-09-23 15:56:32 -070047struct mem_cgroup *root_mem_cgroup __read_mostly;
Balbir Singh8cdea7c2008-02-07 00:13:50 -080048
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080049#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
Li Zefan338c8432009-06-17 16:27:15 -070050/* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080051int do_swap_account __read_mostly;
52static int really_do_swap_account __initdata = 1; /* for remember boot option*/
53#else
54#define do_swap_account (0)
55#endif
56
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -080057static DEFINE_MUTEX(memcg_tasklist); /* can be hold under cgroup_mutex */
Balbir Singhf64c3f52009-09-23 15:56:37 -070058#define SOFTLIMIT_EVENTS_THRESH (1000)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080059
Balbir Singh8cdea7c2008-02-07 00:13:50 -080060/*
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080061 * Statistics for memory cgroup.
62 */
63enum mem_cgroup_stat_index {
64 /*
65 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
66 */
67 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
Balbir Singhd69b0422009-06-17 16:26:34 -070068 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
69 MEM_CGROUP_STAT_MAPPED_FILE, /* # of pages charged as file rss */
Balaji Rao55e462b2008-05-01 04:35:12 -070070 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
71 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
Balbir Singhf64c3f52009-09-23 15:56:37 -070072 MEM_CGROUP_STAT_EVENTS, /* sum of pagein + pageout for internal use */
Balbir Singh0c3e73e2009-09-23 15:56:42 -070073 MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080074
75 MEM_CGROUP_STAT_NSTATS,
76};
77
78struct mem_cgroup_stat_cpu {
79 s64 count[MEM_CGROUP_STAT_NSTATS];
80} ____cacheline_aligned_in_smp;
81
82struct mem_cgroup_stat {
Jan Blunckc8dad2b2009-01-07 18:07:53 -080083 struct mem_cgroup_stat_cpu cpustat[0];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080084};
85
Balbir Singhf64c3f52009-09-23 15:56:37 -070086static inline void
87__mem_cgroup_stat_reset_safe(struct mem_cgroup_stat_cpu *stat,
88 enum mem_cgroup_stat_index idx)
89{
90 stat->count[idx] = 0;
91}
92
93static inline s64
94__mem_cgroup_stat_read_local(struct mem_cgroup_stat_cpu *stat,
95 enum mem_cgroup_stat_index idx)
96{
97 return stat->count[idx];
98}
99
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800100/*
101 * For accounting under irq disable, no need for increment preempt count.
102 */
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700103static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800104 enum mem_cgroup_stat_index idx, int val)
105{
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700106 stat->count[idx] += val;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800107}
108
109static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
110 enum mem_cgroup_stat_index idx)
111{
112 int cpu;
113 s64 ret = 0;
114 for_each_possible_cpu(cpu)
115 ret += stat->cpustat[cpu].count[idx];
116 return ret;
117}
118
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700119static s64 mem_cgroup_local_usage(struct mem_cgroup_stat *stat)
120{
121 s64 ret;
122
123 ret = mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_CACHE);
124 ret += mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_RSS);
125 return ret;
126}
127
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800128/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800129 * per-zone information in memory controller.
130 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800131struct mem_cgroup_per_zone {
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800132 /*
133 * spin_lock to protect the per cgroup LRU
134 */
Christoph Lameterb69408e2008-10-18 20:26:14 -0700135 struct list_head lists[NR_LRU_LISTS];
136 unsigned long count[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800137
138 struct zone_reclaim_stat reclaim_stat;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700139 struct rb_node tree_node; /* RB tree node */
140 unsigned long long usage_in_excess;/* Set to the value by which */
141 /* the soft limit is exceeded*/
142 bool on_tree;
Balbir Singh4e416952009-09-23 15:56:39 -0700143 struct mem_cgroup *mem; /* Back pointer, we cannot */
144 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800145};
146/* Macro for accessing counter */
147#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
148
149struct mem_cgroup_per_node {
150 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
151};
152
153struct mem_cgroup_lru_info {
154 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
155};
156
157/*
Balbir Singhf64c3f52009-09-23 15:56:37 -0700158 * Cgroups above their limits are maintained in a RB-Tree, independent of
159 * their hierarchy representation
160 */
161
162struct mem_cgroup_tree_per_zone {
163 struct rb_root rb_root;
164 spinlock_t lock;
165};
166
167struct mem_cgroup_tree_per_node {
168 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
169};
170
171struct mem_cgroup_tree {
172 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
173};
174
175static struct mem_cgroup_tree soft_limit_tree __read_mostly;
176
177/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800178 * The memory controller data structure. The memory controller controls both
179 * page cache and RSS per cgroup. We would eventually like to provide
180 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
181 * to help the administrator determine what knobs to tune.
182 *
183 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800184 * we hit the water mark. May be even add a low water mark, such that
185 * no reclaim occurs from a cgroup at it's low water mark, this is
186 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800187 */
188struct mem_cgroup {
189 struct cgroup_subsys_state css;
190 /*
191 * the counter to account for memory usage
192 */
193 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800194 /*
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800195 * the counter to account for mem+swap usage.
196 */
197 struct res_counter memsw;
198 /*
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800199 * Per cgroup active and inactive list, similar to the
200 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800201 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800202 struct mem_cgroup_lru_info info;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800203
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800204 /*
205 protect against reclaim related member.
206 */
207 spinlock_t reclaim_param_lock;
208
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800209 int prev_priority; /* for recording reclaim priority */
Balbir Singh6d61ef42009-01-07 18:08:06 -0800210
211 /*
212 * While reclaiming in a hiearchy, 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 Hiroyuki8a9478ca2009-06-17 16:27:17 -0700247 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700248 NR_CHARGE_TYPE,
249};
250
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700251/* only for here (for easy reading.) */
252#define PCGF_CACHE (1UL << PCG_CACHE)
253#define PCGF_USED (1UL << PCG_USED)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700254#define PCGF_LOCK (1UL << PCG_LOCK)
Balbir Singh4b3bde42009-09-23 15:56:32 -0700255/* Not used, but added here for completeness */
256#define PCGF_ACCT (1UL << PCG_ACCT)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800257
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800258/* for encoding cft->private value on file */
259#define _MEM (0)
260#define _MEMSWAP (1)
261#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
262#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
263#define MEMFILE_ATTR(val) ((val) & 0xffff)
264
Balbir Singh75822b42009-09-23 15:56:38 -0700265/*
266 * Reclaim flags for mem_cgroup_hierarchical_reclaim
267 */
268#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
269#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
270#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
271#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
Balbir Singh4e416952009-09-23 15:56:39 -0700272#define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
273#define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
Balbir Singh75822b42009-09-23 15:56:38 -0700274
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800275static void mem_cgroup_get(struct mem_cgroup *mem);
276static void mem_cgroup_put(struct mem_cgroup *mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -0800277static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800278
Balbir Singhf64c3f52009-09-23 15:56:37 -0700279static struct mem_cgroup_per_zone *
280mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
281{
282 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
283}
284
285static struct mem_cgroup_per_zone *
286page_cgroup_zoneinfo(struct page_cgroup *pc)
287{
288 struct mem_cgroup *mem = pc->mem_cgroup;
289 int nid = page_cgroup_nid(pc);
290 int zid = page_cgroup_zid(pc);
291
292 if (!mem)
293 return NULL;
294
295 return mem_cgroup_zoneinfo(mem, nid, zid);
296}
297
298static struct mem_cgroup_tree_per_zone *
299soft_limit_tree_node_zone(int nid, int zid)
300{
301 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
302}
303
304static struct mem_cgroup_tree_per_zone *
305soft_limit_tree_from_page(struct page *page)
306{
307 int nid = page_to_nid(page);
308 int zid = page_zonenum(page);
309
310 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
311}
312
313static void
Balbir Singh4e416952009-09-23 15:56:39 -0700314__mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700315 struct mem_cgroup_per_zone *mz,
316 struct mem_cgroup_tree_per_zone *mctz)
317{
318 struct rb_node **p = &mctz->rb_root.rb_node;
319 struct rb_node *parent = NULL;
320 struct mem_cgroup_per_zone *mz_node;
321
322 if (mz->on_tree)
323 return;
324
325 mz->usage_in_excess = res_counter_soft_limit_excess(&mem->res);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700326 while (*p) {
327 parent = *p;
328 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
329 tree_node);
330 if (mz->usage_in_excess < mz_node->usage_in_excess)
331 p = &(*p)->rb_left;
332 /*
333 * We can't avoid mem cgroups that are over their soft
334 * limit by the same amount
335 */
336 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
337 p = &(*p)->rb_right;
338 }
339 rb_link_node(&mz->tree_node, parent, p);
340 rb_insert_color(&mz->tree_node, &mctz->rb_root);
341 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700342}
343
344static void
345__mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
346 struct mem_cgroup_per_zone *mz,
347 struct mem_cgroup_tree_per_zone *mctz)
348{
349 if (!mz->on_tree)
350 return;
351 rb_erase(&mz->tree_node, &mctz->rb_root);
352 mz->on_tree = false;
353}
354
355static void
356mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
357 struct mem_cgroup_per_zone *mz,
358 struct mem_cgroup_tree_per_zone *mctz)
359{
360 spin_lock(&mctz->lock);
361 __mem_cgroup_insert_exceeded(mem, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700362 spin_unlock(&mctz->lock);
363}
364
365static void
366mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
367 struct mem_cgroup_per_zone *mz,
368 struct mem_cgroup_tree_per_zone *mctz)
369{
370 spin_lock(&mctz->lock);
Balbir Singh4e416952009-09-23 15:56:39 -0700371 __mem_cgroup_remove_exceeded(mem, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700372 spin_unlock(&mctz->lock);
373}
374
375static bool mem_cgroup_soft_limit_check(struct mem_cgroup *mem)
376{
377 bool ret = false;
378 int cpu;
379 s64 val;
380 struct mem_cgroup_stat_cpu *cpustat;
381
382 cpu = get_cpu();
383 cpustat = &mem->stat.cpustat[cpu];
384 val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_EVENTS);
385 if (unlikely(val > SOFTLIMIT_EVENTS_THRESH)) {
386 __mem_cgroup_stat_reset_safe(cpustat, MEM_CGROUP_STAT_EVENTS);
387 ret = true;
388 }
389 put_cpu();
390 return ret;
391}
392
393static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
394{
395 unsigned long long prev_usage_in_excess, new_usage_in_excess;
396 bool updated_tree = false;
397 struct mem_cgroup_per_zone *mz;
398 struct mem_cgroup_tree_per_zone *mctz;
399
400 mz = mem_cgroup_zoneinfo(mem, page_to_nid(page), page_zonenum(page));
401 mctz = soft_limit_tree_from_page(page);
402
403 /*
404 * We do updates in lazy mode, mem's are removed
405 * lazily from the per-zone, per-node rb tree
406 */
407 prev_usage_in_excess = mz->usage_in_excess;
408
409 new_usage_in_excess = res_counter_soft_limit_excess(&mem->res);
410 if (prev_usage_in_excess) {
411 mem_cgroup_remove_exceeded(mem, mz, mctz);
412 updated_tree = true;
413 }
414 if (!new_usage_in_excess)
415 goto done;
416 mem_cgroup_insert_exceeded(mem, mz, mctz);
417
418done:
419 if (updated_tree) {
420 spin_lock(&mctz->lock);
421 mz->usage_in_excess = new_usage_in_excess;
422 spin_unlock(&mctz->lock);
423 }
424}
425
426static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
427{
428 int node, zone;
429 struct mem_cgroup_per_zone *mz;
430 struct mem_cgroup_tree_per_zone *mctz;
431
432 for_each_node_state(node, N_POSSIBLE) {
433 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
434 mz = mem_cgroup_zoneinfo(mem, node, zone);
435 mctz = soft_limit_tree_node_zone(node, zone);
436 mem_cgroup_remove_exceeded(mem, mz, mctz);
437 }
438 }
439}
440
Balbir Singh4e416952009-09-23 15:56:39 -0700441static inline unsigned long mem_cgroup_get_excess(struct mem_cgroup *mem)
442{
443 return res_counter_soft_limit_excess(&mem->res) >> PAGE_SHIFT;
444}
445
446static struct mem_cgroup_per_zone *
447__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
448{
449 struct rb_node *rightmost = NULL;
450 struct mem_cgroup_per_zone *mz = NULL;
451
452retry:
453 rightmost = rb_last(&mctz->rb_root);
454 if (!rightmost)
455 goto done; /* Nothing to reclaim from */
456
457 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
458 /*
459 * Remove the node now but someone else can add it back,
460 * we will to add it back at the end of reclaim to its correct
461 * position in the tree.
462 */
463 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
464 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
465 !css_tryget(&mz->mem->css))
466 goto retry;
467done:
468 return mz;
469}
470
471static struct mem_cgroup_per_zone *
472mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
473{
474 struct mem_cgroup_per_zone *mz;
475
476 spin_lock(&mctz->lock);
477 mz = __mem_cgroup_largest_soft_limit_node(mctz);
478 spin_unlock(&mctz->lock);
479 return mz;
480}
481
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700482static void mem_cgroup_swap_statistics(struct mem_cgroup *mem,
483 bool charge)
484{
485 int val = (charge) ? 1 : -1;
486 struct mem_cgroup_stat *stat = &mem->stat;
487 struct mem_cgroup_stat_cpu *cpustat;
488 int cpu = get_cpu();
489
490 cpustat = &stat->cpustat[cpu];
491 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_SWAPOUT, val);
492 put_cpu();
493}
494
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700495static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
496 struct page_cgroup *pc,
497 bool charge)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800498{
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700499 int val = (charge) ? 1 : -1;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800500 struct mem_cgroup_stat *stat = &mem->stat;
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700501 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800502 int cpu = get_cpu();
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800503
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800504 cpustat = &stat->cpustat[cpu];
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700505 if (PageCgroupCache(pc))
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700506 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800507 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700508 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700509
510 if (charge)
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_PGPGIN_COUNT, 1);
513 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700514 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700515 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700516 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_EVENTS, 1);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800517 put_cpu();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800518}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800519
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700520static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700521 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800522{
523 int nid, zid;
524 struct mem_cgroup_per_zone *mz;
525 u64 total = 0;
526
527 for_each_online_node(nid)
528 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
529 mz = mem_cgroup_zoneinfo(mem, nid, zid);
530 total += MEM_CGROUP_ZSTAT(mz, idx);
531 }
532 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800533}
534
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800535static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800536{
537 return container_of(cgroup_subsys_state(cont,
538 mem_cgroup_subsys_id), struct mem_cgroup,
539 css);
540}
541
Balbir Singhcf475ad2008-04-29 01:00:16 -0700542struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800543{
Balbir Singh31a78f22008-09-28 23:09:31 +0100544 /*
545 * mm_update_next_owner() may clear mm->owner to NULL
546 * if it races with swapoff, page migration, etc.
547 * So this can be called with p == NULL.
548 */
549 if (unlikely(!p))
550 return NULL;
551
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800552 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
553 struct mem_cgroup, css);
554}
555
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800556static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
557{
558 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700559
560 if (!mm)
561 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800562 /*
563 * Because we have no locks, mm->owner's may be being moved to other
564 * cgroup. We use css_tryget() here even if this looks
565 * pessimistic (rather than adding locks here).
566 */
567 rcu_read_lock();
568 do {
569 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
570 if (unlikely(!mem))
571 break;
572 } while (!css_tryget(&mem->css));
573 rcu_read_unlock();
574 return mem;
575}
576
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700577/*
578 * Call callback function against all cgroup under hierarchy tree.
579 */
580static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
581 int (*func)(struct mem_cgroup *, void *))
582{
583 int found, ret, nextid;
584 struct cgroup_subsys_state *css;
585 struct mem_cgroup *mem;
586
587 if (!root->use_hierarchy)
588 return (*func)(root, data);
589
590 nextid = 1;
591 do {
592 ret = 0;
593 mem = NULL;
594
595 rcu_read_lock();
596 css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
597 &found);
598 if (css && css_tryget(css))
599 mem = container_of(css, struct mem_cgroup, css);
600 rcu_read_unlock();
601
602 if (mem) {
603 ret = (*func)(mem, data);
604 css_put(&mem->css);
605 }
606 nextid = found + 1;
607 } while (!ret && css);
608
609 return ret;
610}
611
Balbir Singh4b3bde42009-09-23 15:56:32 -0700612static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
613{
614 return (mem == root_mem_cgroup);
615}
616
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800617/*
618 * Following LRU functions are allowed to be used without PCG_LOCK.
619 * Operations are called by routine of global LRU independently from memcg.
620 * What we have to take care of here is validness of pc->mem_cgroup.
621 *
622 * Changes to pc->mem_cgroup happens when
623 * 1. charge
624 * 2. moving account
625 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
626 * It is added to LRU before charge.
627 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
628 * When moving account, the page is not on LRU. It's isolated.
629 */
630
631void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800632{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800633 struct page_cgroup *pc;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800634 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700635
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800636 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800637 return;
638 pc = lookup_page_cgroup(page);
639 /* can happen while we handle swapcache. */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700640 if (!TestClearPageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800641 return;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700642 VM_BUG_ON(!pc->mem_cgroup);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800643 /*
644 * We don't check PCG_USED bit. It's cleared when the "page" is finally
645 * removed from global LRU.
646 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800647 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700648 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700649 if (mem_cgroup_is_root(pc->mem_cgroup))
650 return;
651 VM_BUG_ON(list_empty(&pc->lru));
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800652 list_del_init(&pc->lru);
653 return;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800654}
655
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800656void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800657{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800658 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800659}
660
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800661void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800662{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800663 struct mem_cgroup_per_zone *mz;
664 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800665
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800666 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700667 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700668
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800669 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800670 /*
671 * Used bit is set without atomic ops but after smp_wmb().
672 * For making pc->mem_cgroup visible, insert smp_rmb() here.
673 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800674 smp_rmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -0700675 /* unused or root page is not rotated. */
676 if (!PageCgroupUsed(pc) || mem_cgroup_is_root(pc->mem_cgroup))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800677 return;
678 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700679 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800680}
681
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800682void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
683{
684 struct page_cgroup *pc;
685 struct mem_cgroup_per_zone *mz;
686
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800687 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800688 return;
689 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700690 VM_BUG_ON(PageCgroupAcctLRU(pc));
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800691 /*
692 * Used bit is set without atomic ops but after smp_wmb().
693 * For making pc->mem_cgroup visible, insert smp_rmb() here.
694 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800695 smp_rmb();
696 if (!PageCgroupUsed(pc))
697 return;
698
699 mz = page_cgroup_zoneinfo(pc);
700 MEM_CGROUP_ZSTAT(mz, lru) += 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700701 SetPageCgroupAcctLRU(pc);
702 if (mem_cgroup_is_root(pc->mem_cgroup))
703 return;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800704 list_add(&pc->lru, &mz->lists[lru]);
705}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800706
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800707/*
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800708 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
709 * lru because the page may.be reused after it's fully uncharged (because of
710 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
711 * it again. This function is only used to charge SwapCache. It's done under
712 * lock_page and expected that zone->lru_lock is never held.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800713 */
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800714static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800715{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800716 unsigned long flags;
717 struct zone *zone = page_zone(page);
718 struct page_cgroup *pc = lookup_page_cgroup(page);
719
720 spin_lock_irqsave(&zone->lru_lock, flags);
721 /*
722 * Forget old LRU when this page_cgroup is *not* used. This Used bit
723 * is guarded by lock_page() because the page is SwapCache.
724 */
725 if (!PageCgroupUsed(pc))
726 mem_cgroup_del_lru_list(page, page_lru(page));
727 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800728}
729
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800730static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
731{
732 unsigned long flags;
733 struct zone *zone = page_zone(page);
734 struct page_cgroup *pc = lookup_page_cgroup(page);
735
736 spin_lock_irqsave(&zone->lru_lock, flags);
737 /* link when the page is linked to LRU but page_cgroup isn't */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700738 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800739 mem_cgroup_add_lru_list(page, page_lru(page));
740 spin_unlock_irqrestore(&zone->lru_lock, flags);
741}
742
743
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800744void mem_cgroup_move_lists(struct page *page,
745 enum lru_list from, enum lru_list to)
746{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800747 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800748 return;
749 mem_cgroup_del_lru_list(page, from);
750 mem_cgroup_add_lru_list(page, to);
751}
752
David Rientjes4c4a2212008-02-07 00:14:06 -0800753int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
754{
755 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700756 struct mem_cgroup *curr = NULL;
David Rientjes4c4a2212008-02-07 00:14:06 -0800757
758 task_lock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700759 rcu_read_lock();
760 curr = try_get_mem_cgroup_from_mm(task->mm);
761 rcu_read_unlock();
David Rientjes4c4a2212008-02-07 00:14:06 -0800762 task_unlock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700763 if (!curr)
764 return 0;
765 if (curr->use_hierarchy)
766 ret = css_is_ancestor(&curr->css, &mem->css);
767 else
768 ret = (curr == mem);
769 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -0800770 return ret;
771}
772
Balbir Singh66e17072008-02-07 00:13:56 -0800773/*
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800774 * prev_priority control...this will be used in memory reclaim path.
775 */
776int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
777{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800778 int prev_priority;
779
780 spin_lock(&mem->reclaim_param_lock);
781 prev_priority = mem->prev_priority;
782 spin_unlock(&mem->reclaim_param_lock);
783
784 return prev_priority;
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800785}
786
787void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
788{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800789 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800790 if (priority < mem->prev_priority)
791 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800792 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800793}
794
795void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
796{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800797 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800798 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800799 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800800}
801
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800802static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800803{
804 unsigned long active;
805 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800806 unsigned long gb;
807 unsigned long inactive_ratio;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800808
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700809 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
810 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800811
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800812 gb = (inactive + active) >> (30 - PAGE_SHIFT);
813 if (gb)
814 inactive_ratio = int_sqrt(10 * gb);
815 else
816 inactive_ratio = 1;
817
818 if (present_pages) {
819 present_pages[0] = inactive;
820 present_pages[1] = active;
821 }
822
823 return inactive_ratio;
824}
825
826int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
827{
828 unsigned long active;
829 unsigned long inactive;
830 unsigned long present_pages[2];
831 unsigned long inactive_ratio;
832
833 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
834
835 inactive = present_pages[0];
836 active = present_pages[1];
837
838 if (inactive * inactive_ratio < active)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800839 return 1;
840
841 return 0;
842}
843
Rik van Riel56e49d22009-06-16 15:32:28 -0700844int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
845{
846 unsigned long active;
847 unsigned long inactive;
848
849 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
850 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
851
852 return (active > inactive);
853}
854
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800855unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
856 struct zone *zone,
857 enum lru_list lru)
858{
859 int nid = zone->zone_pgdat->node_id;
860 int zid = zone_idx(zone);
861 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
862
863 return MEM_CGROUP_ZSTAT(mz, lru);
864}
865
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800866struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
867 struct zone *zone)
868{
869 int nid = zone->zone_pgdat->node_id;
870 int zid = zone_idx(zone);
871 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
872
873 return &mz->reclaim_stat;
874}
875
876struct zone_reclaim_stat *
877mem_cgroup_get_reclaim_stat_from_page(struct page *page)
878{
879 struct page_cgroup *pc;
880 struct mem_cgroup_per_zone *mz;
881
882 if (mem_cgroup_disabled())
883 return NULL;
884
885 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800886 /*
887 * Used bit is set without atomic ops but after smp_wmb().
888 * For making pc->mem_cgroup visible, insert smp_rmb() here.
889 */
890 smp_rmb();
891 if (!PageCgroupUsed(pc))
892 return NULL;
893
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800894 mz = page_cgroup_zoneinfo(pc);
895 if (!mz)
896 return NULL;
897
898 return &mz->reclaim_stat;
899}
900
Balbir Singh66e17072008-02-07 00:13:56 -0800901unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
902 struct list_head *dst,
903 unsigned long *scanned, int order,
904 int mode, struct zone *z,
905 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700906 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -0800907{
908 unsigned long nr_taken = 0;
909 struct page *page;
910 unsigned long scan;
911 LIST_HEAD(pc_list);
912 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800913 struct page_cgroup *pc, *tmp;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800914 int nid = z->zone_pgdat->node_id;
915 int zid = zone_idx(z);
916 struct mem_cgroup_per_zone *mz;
Johannes Weinerb7c46d12009-09-21 17:02:56 -0700917 int lru = LRU_FILE * file + active;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700918 int ret;
Balbir Singh66e17072008-02-07 00:13:56 -0800919
Balbir Singhcf475ad2008-04-29 01:00:16 -0700920 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800921 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700922 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -0800923
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800924 scan = 0;
925 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -0800926 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800927 break;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800928
929 page = pc->page;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700930 if (unlikely(!PageCgroupUsed(pc)))
931 continue;
Hugh Dickins436c65412008-02-07 00:14:12 -0800932 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800933 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800934
Hugh Dickins436c65412008-02-07 00:14:12 -0800935 scan++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700936 ret = __isolate_lru_page(page, mode, file);
937 switch (ret) {
938 case 0:
Balbir Singh66e17072008-02-07 00:13:56 -0800939 list_move(&page->lru, dst);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700940 mem_cgroup_del_lru(page);
Balbir Singh66e17072008-02-07 00:13:56 -0800941 nr_taken++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700942 break;
943 case -EBUSY:
944 /* we don't affect global LRU but rotate in our LRU */
945 mem_cgroup_rotate_lru_list(page, page_lru(page));
946 break;
947 default:
948 break;
Balbir Singh66e17072008-02-07 00:13:56 -0800949 }
950 }
951
Balbir Singh66e17072008-02-07 00:13:56 -0800952 *scanned = scan;
953 return nr_taken;
954}
955
Balbir Singh6d61ef42009-01-07 18:08:06 -0800956#define mem_cgroup_from_res_counter(counter, member) \
957 container_of(counter, struct mem_cgroup, member)
958
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -0800959static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
960{
961 if (do_swap_account) {
962 if (res_counter_check_under_limit(&mem->res) &&
963 res_counter_check_under_limit(&mem->memsw))
964 return true;
965 } else
966 if (res_counter_check_under_limit(&mem->res))
967 return true;
968 return false;
969}
970
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800971static unsigned int get_swappiness(struct mem_cgroup *memcg)
972{
973 struct cgroup *cgrp = memcg->css.cgroup;
974 unsigned int swappiness;
975
976 /* root ? */
977 if (cgrp->parent == NULL)
978 return vm_swappiness;
979
980 spin_lock(&memcg->reclaim_param_lock);
981 swappiness = memcg->swappiness;
982 spin_unlock(&memcg->reclaim_param_lock);
983
984 return swappiness;
985}
986
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -0700987static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
988{
989 int *val = data;
990 (*val)++;
991 return 0;
992}
Balbir Singhe2224322009-04-02 16:57:39 -0700993
994/**
995 * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode.
996 * @memcg: The memory cgroup that went over limit
997 * @p: Task that is going to be killed
998 *
999 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1000 * enabled
1001 */
1002void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1003{
1004 struct cgroup *task_cgrp;
1005 struct cgroup *mem_cgrp;
1006 /*
1007 * Need a buffer in BSS, can't rely on allocations. The code relies
1008 * on the assumption that OOM is serialized for memory controller.
1009 * If this assumption is broken, revisit this code.
1010 */
1011 static char memcg_name[PATH_MAX];
1012 int ret;
1013
1014 if (!memcg)
1015 return;
1016
1017
1018 rcu_read_lock();
1019
1020 mem_cgrp = memcg->css.cgroup;
1021 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1022
1023 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1024 if (ret < 0) {
1025 /*
1026 * Unfortunately, we are unable to convert to a useful name
1027 * But we'll still print out the usage information
1028 */
1029 rcu_read_unlock();
1030 goto done;
1031 }
1032 rcu_read_unlock();
1033
1034 printk(KERN_INFO "Task in %s killed", memcg_name);
1035
1036 rcu_read_lock();
1037 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1038 if (ret < 0) {
1039 rcu_read_unlock();
1040 goto done;
1041 }
1042 rcu_read_unlock();
1043
1044 /*
1045 * Continues from above, so we don't need an KERN_ level
1046 */
1047 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1048done:
1049
1050 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1051 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1052 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1053 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1054 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1055 "failcnt %llu\n",
1056 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1057 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1058 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1059}
1060
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001061/*
1062 * This function returns the number of memcg under hierarchy tree. Returns
1063 * 1(self count) if no children.
1064 */
1065static int mem_cgroup_count_children(struct mem_cgroup *mem)
1066{
1067 int num = 0;
1068 mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
1069 return num;
1070}
1071
Balbir Singh6d61ef42009-01-07 18:08:06 -08001072/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001073 * Visit the first child (need not be the first child as per the ordering
1074 * of the cgroup list, since we track last_scanned_child) of @mem and use
1075 * that to reclaim free pages from.
1076 */
1077static struct mem_cgroup *
1078mem_cgroup_select_victim(struct mem_cgroup *root_mem)
1079{
1080 struct mem_cgroup *ret = NULL;
1081 struct cgroup_subsys_state *css;
1082 int nextid, found;
1083
1084 if (!root_mem->use_hierarchy) {
1085 css_get(&root_mem->css);
1086 ret = root_mem;
1087 }
1088
1089 while (!ret) {
1090 rcu_read_lock();
1091 nextid = root_mem->last_scanned_child + 1;
1092 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
1093 &found);
1094 if (css && css_tryget(css))
1095 ret = container_of(css, struct mem_cgroup, css);
1096
1097 rcu_read_unlock();
1098 /* Updates scanning parameter */
1099 spin_lock(&root_mem->reclaim_param_lock);
1100 if (!css) {
1101 /* this means start scan from ID:1 */
1102 root_mem->last_scanned_child = 0;
1103 } else
1104 root_mem->last_scanned_child = found;
1105 spin_unlock(&root_mem->reclaim_param_lock);
1106 }
1107
1108 return ret;
1109}
1110
1111/*
1112 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1113 * we reclaimed from, so that we don't end up penalizing one child extensively
1114 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001115 *
1116 * root_mem is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001117 *
1118 * We give up and return to the caller when we visit root_mem twice.
1119 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001120 *
1121 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001122 */
1123static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
Balbir Singh4e416952009-09-23 15:56:39 -07001124 struct zone *zone,
Balbir Singh75822b42009-09-23 15:56:38 -07001125 gfp_t gfp_mask,
1126 unsigned long reclaim_options)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001127{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001128 struct mem_cgroup *victim;
1129 int ret, total = 0;
1130 int loop = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001131 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1132 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
Balbir Singh4e416952009-09-23 15:56:39 -07001133 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
1134 unsigned long excess = mem_cgroup_get_excess(root_mem);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001135
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001136 /* If memsw_is_minimum==1, swap-out is of-no-use. */
1137 if (root_mem->memsw_is_minimum)
1138 noswap = true;
1139
Balbir Singh4e416952009-09-23 15:56:39 -07001140 while (1) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001141 victim = mem_cgroup_select_victim(root_mem);
Balbir Singh4e416952009-09-23 15:56:39 -07001142 if (victim == root_mem) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001143 loop++;
Balbir Singh4e416952009-09-23 15:56:39 -07001144 if (loop >= 2) {
1145 /*
1146 * If we have not been able to reclaim
1147 * anything, it might because there are
1148 * no reclaimable pages under this hierarchy
1149 */
1150 if (!check_soft || !total) {
1151 css_put(&victim->css);
1152 break;
1153 }
1154 /*
1155 * We want to do more targetted reclaim.
1156 * excess >> 2 is not to excessive so as to
1157 * reclaim too much, nor too less that we keep
1158 * coming back to reclaim from this cgroup
1159 */
1160 if (total >= (excess >> 2) ||
1161 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1162 css_put(&victim->css);
1163 break;
1164 }
1165 }
1166 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001167 if (!mem_cgroup_local_usage(&victim->stat)) {
1168 /* this cgroup's local usage == 0 */
1169 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001170 continue;
1171 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001172 /* we use swappiness of local cgroup */
Balbir Singh4e416952009-09-23 15:56:39 -07001173 if (check_soft)
1174 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
1175 noswap, get_swappiness(victim), zone,
1176 zone->zone_pgdat->node_id);
1177 else
1178 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
1179 noswap, get_swappiness(victim));
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001180 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001181 /*
1182 * At shrinking usage, we can't check we should stop here or
1183 * reclaim more. It's depends on callers. last_scanned_child
1184 * will work enough for keeping fairness under tree.
1185 */
1186 if (shrink)
1187 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001188 total += ret;
Balbir Singh4e416952009-09-23 15:56:39 -07001189 if (check_soft) {
1190 if (res_counter_check_under_soft_limit(&root_mem->res))
1191 return total;
1192 } else if (mem_cgroup_check_under_limit(root_mem))
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001193 return 1 + total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001194 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001195 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001196}
1197
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001198bool mem_cgroup_oom_called(struct task_struct *task)
1199{
1200 bool ret = false;
1201 struct mem_cgroup *mem;
1202 struct mm_struct *mm;
1203
1204 rcu_read_lock();
1205 mm = task->mm;
1206 if (!mm)
1207 mm = &init_mm;
1208 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
1209 if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
1210 ret = true;
1211 rcu_read_unlock();
1212 return ret;
1213}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001214
1215static int record_last_oom_cb(struct mem_cgroup *mem, void *data)
1216{
1217 mem->last_oom_jiffies = jiffies;
1218 return 0;
1219}
1220
1221static void record_last_oom(struct mem_cgroup *mem)
1222{
1223 mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb);
1224}
1225
Balbir Singhd69b0422009-06-17 16:26:34 -07001226/*
1227 * Currently used to update mapped file statistics, but the routine can be
1228 * generalized to update other statistics as well.
1229 */
1230void mem_cgroup_update_mapped_file_stat(struct page *page, int val)
1231{
1232 struct mem_cgroup *mem;
1233 struct mem_cgroup_stat *stat;
1234 struct mem_cgroup_stat_cpu *cpustat;
1235 int cpu;
1236 struct page_cgroup *pc;
1237
1238 if (!page_is_file_cache(page))
1239 return;
1240
1241 pc = lookup_page_cgroup(page);
1242 if (unlikely(!pc))
1243 return;
1244
1245 lock_page_cgroup(pc);
1246 mem = pc->mem_cgroup;
1247 if (!mem)
1248 goto done;
1249
1250 if (!PageCgroupUsed(pc))
1251 goto done;
1252
1253 /*
1254 * Preemption is already disabled, we don't need get_cpu()
1255 */
1256 cpu = smp_processor_id();
1257 stat = &mem->stat;
1258 cpustat = &stat->cpustat[cpu];
1259
1260 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE, val);
1261done:
1262 unlock_page_cgroup(pc);
1263}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001264
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001265/*
1266 * Unlike exported interface, "oom" parameter is added. if oom==true,
1267 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001268 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001269static int __mem_cgroup_try_charge(struct mm_struct *mm,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001270 gfp_t gfp_mask, struct mem_cgroup **memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -07001271 bool oom, struct page *page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001272{
Balbir Singhf64c3f52009-09-23 15:56:37 -07001273 struct mem_cgroup *mem, *mem_over_limit, *mem_over_soft_limit;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001274 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001275 struct res_counter *fail_res, *soft_fail_res = NULL;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001276
1277 if (unlikely(test_thread_flag(TIF_MEMDIE))) {
1278 /* Don't account this! */
1279 *memcg = NULL;
1280 return 0;
1281 }
1282
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001283 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08001284 * We always charge the cgroup the mm_struct belongs to.
1285 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001286 * thread group leader migrates. It's possible that mm is not
1287 * set, if so charge the init_mm (happens for pagecache usage).
1288 */
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001289 mem = *memcg;
1290 if (likely(!mem)) {
1291 mem = try_get_mem_cgroup_from_mm(mm);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001292 *memcg = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001293 } else {
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001294 css_get(&mem->css);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001295 }
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001296 if (unlikely(!mem))
1297 return 0;
1298
Nikanth Karthikesan46f7e602009-05-28 14:34:41 -07001299 VM_BUG_ON(css_is_removed(&mem->css));
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001300
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001301 while (1) {
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001302 int ret = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001303 unsigned long flags = 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001304
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001305 if (mem_cgroup_is_root(mem))
1306 goto done;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001307 ret = res_counter_charge(&mem->res, PAGE_SIZE, &fail_res,
1308 &soft_fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001309 if (likely(!ret)) {
1310 if (!do_swap_account)
1311 break;
Balbir Singh28dbc4b2009-01-07 18:08:05 -08001312 ret = res_counter_charge(&mem->memsw, PAGE_SIZE,
Balbir Singhf64c3f52009-09-23 15:56:37 -07001313 &fail_res, NULL);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001314 if (likely(!ret))
1315 break;
1316 /* mem+swap counter fails */
Balbir Singhf64c3f52009-09-23 15:56:37 -07001317 res_counter_uncharge(&mem->res, PAGE_SIZE, NULL);
Balbir Singh75822b42009-09-23 15:56:38 -07001318 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001319 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1320 memsw);
1321 } else
1322 /* mem counter fails */
1323 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1324 res);
1325
Hugh Dickins3be91272008-02-07 00:14:19 -08001326 if (!(gfp_mask & __GFP_WAIT))
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001327 goto nomem;
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001328
Balbir Singh4e416952009-09-23 15:56:39 -07001329 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
1330 gfp_mask, flags);
Daisuke Nishimura4d1c6272009-01-15 13:51:14 -08001331 if (ret)
1332 continue;
Balbir Singh66e17072008-02-07 00:13:56 -08001333
1334 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001335 * try_to_free_mem_cgroup_pages() might not give us a full
1336 * picture of reclaim. Some pages are reclaimed and might be
1337 * moved to swap cache or just unmapped from the cgroup.
1338 * Check the limit again to see if the reclaim reduced the
1339 * current usage of the cgroup before giving up
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001340 *
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001341 */
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -08001342 if (mem_cgroup_check_under_limit(mem_over_limit))
1343 continue;
Hugh Dickins3be91272008-02-07 00:14:19 -08001344
1345 if (!nr_retries--) {
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001346 if (oom) {
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08001347 mutex_lock(&memcg_tasklist);
KAMEZAWA Hiroyuki88700752009-01-07 18:08:09 -08001348 mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08001349 mutex_unlock(&memcg_tasklist);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001350 record_last_oom(mem_over_limit);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001351 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001352 goto nomem;
Balbir Singh66e17072008-02-07 00:13:56 -08001353 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001354 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07001355 /*
1356 * Insert just the ancestor, we should trickle down to the correct
1357 * cgroup for reclaim, since the other nodes will be below their
1358 * soft limit
1359 */
1360 if (soft_fail_res) {
1361 mem_over_soft_limit =
1362 mem_cgroup_from_res_counter(soft_fail_res, res);
1363 if (mem_cgroup_soft_limit_check(mem_over_soft_limit))
1364 mem_cgroup_update_tree(mem_over_soft_limit, page);
1365 }
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001366done:
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001367 return 0;
1368nomem:
1369 css_put(&mem->css);
1370 return -ENOMEM;
1371}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001372
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001373/*
1374 * A helper function to get mem_cgroup from ID. must be called under
1375 * rcu_read_lock(). The caller must check css_is_removed() or some if
1376 * it's concern. (dropping refcnt from swap can be called against removed
1377 * memcg.)
1378 */
1379static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
1380{
1381 struct cgroup_subsys_state *css;
1382
1383 /* ID 0 is unused ID */
1384 if (!id)
1385 return NULL;
1386 css = css_lookup(&mem_cgroup_subsys, id);
1387 if (!css)
1388 return NULL;
1389 return container_of(css, struct mem_cgroup, css);
1390}
1391
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001392static struct mem_cgroup *try_get_mem_cgroup_from_swapcache(struct page *page)
1393{
1394 struct mem_cgroup *mem;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001395 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001396 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001397 swp_entry_t ent;
1398
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001399 VM_BUG_ON(!PageLocked(page));
1400
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001401 if (!PageSwapCache(page))
1402 return NULL;
1403
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001404 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001405 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001406 if (PageCgroupUsed(pc)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001407 mem = pc->mem_cgroup;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001408 if (mem && !css_tryget(&mem->css))
1409 mem = NULL;
1410 } else {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001411 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001412 id = lookup_swap_cgroup(ent);
1413 rcu_read_lock();
1414 mem = mem_cgroup_lookup(id);
1415 if (mem && !css_tryget(&mem->css))
1416 mem = NULL;
1417 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001418 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001419 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001420 return mem;
1421}
1422
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001423/*
Daisuke Nishimuraa5e924f2009-01-07 18:08:28 -08001424 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001425 * USED state. If already USED, uncharge and return.
1426 */
1427
1428static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1429 struct page_cgroup *pc,
1430 enum charge_type ctype)
1431{
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001432 /* try_charge() can return NULL to *memcg, taking care of it. */
1433 if (!mem)
1434 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001435
1436 lock_page_cgroup(pc);
1437 if (unlikely(PageCgroupUsed(pc))) {
1438 unlock_page_cgroup(pc);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001439 if (!mem_cgroup_is_root(mem)) {
1440 res_counter_uncharge(&mem->res, PAGE_SIZE, NULL);
1441 if (do_swap_account)
1442 res_counter_uncharge(&mem->memsw, PAGE_SIZE,
1443 NULL);
1444 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001445 css_put(&mem->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001446 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001447 }
Balbir Singh4b3bde42009-09-23 15:56:32 -07001448
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001449 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07001450 /*
1451 * We access a page_cgroup asynchronously without lock_page_cgroup().
1452 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
1453 * is accessed after testing USED bit. To make pc->mem_cgroup visible
1454 * before USED bit, we need memory barrier here.
1455 * See mem_cgroup_add_lru_list(), etc.
1456 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001457 smp_wmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -07001458 switch (ctype) {
1459 case MEM_CGROUP_CHARGE_TYPE_CACHE:
1460 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
1461 SetPageCgroupCache(pc);
1462 SetPageCgroupUsed(pc);
1463 break;
1464 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1465 ClearPageCgroupCache(pc);
1466 SetPageCgroupUsed(pc);
1467 break;
1468 default:
1469 break;
1470 }
Hugh Dickins3be91272008-02-07 00:14:19 -08001471
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001472 mem_cgroup_charge_statistics(mem, pc, true);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001473
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001474 unlock_page_cgroup(pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001475}
1476
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001477/**
1478 * mem_cgroup_move_account - move account of the page
1479 * @pc: page_cgroup of the page.
1480 * @from: mem_cgroup which the page is moved from.
1481 * @to: mem_cgroup which the page is moved to. @from != @to.
1482 *
1483 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001484 * - page is not on LRU (isolate_page() is useful.)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001485 *
1486 * returns 0 at success,
1487 * returns -EBUSY when lock is busy or "pc" is unstable.
1488 *
1489 * This function does "uncharge" from old cgroup but doesn't do "charge" to
1490 * new cgroup. It should be done by a caller.
1491 */
1492
1493static int mem_cgroup_move_account(struct page_cgroup *pc,
1494 struct mem_cgroup *from, struct mem_cgroup *to)
1495{
1496 struct mem_cgroup_per_zone *from_mz, *to_mz;
1497 int nid, zid;
1498 int ret = -EBUSY;
Balbir Singhd69b0422009-06-17 16:26:34 -07001499 struct page *page;
1500 int cpu;
1501 struct mem_cgroup_stat *stat;
1502 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001503
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001504 VM_BUG_ON(from == to);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001505 VM_BUG_ON(PageLRU(pc->page));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001506
1507 nid = page_cgroup_nid(pc);
1508 zid = page_cgroup_zid(pc);
1509 from_mz = mem_cgroup_zoneinfo(from, nid, zid);
1510 to_mz = mem_cgroup_zoneinfo(to, nid, zid);
1511
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001512 if (!trylock_page_cgroup(pc))
1513 return ret;
1514
1515 if (!PageCgroupUsed(pc))
1516 goto out;
1517
1518 if (pc->mem_cgroup != from)
1519 goto out;
1520
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001521 if (!mem_cgroup_is_root(from))
1522 res_counter_uncharge(&from->res, PAGE_SIZE, NULL);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001523 mem_cgroup_charge_statistics(from, pc, false);
Balbir Singhd69b0422009-06-17 16:26:34 -07001524
1525 page = pc->page;
1526 if (page_is_file_cache(page) && page_mapped(page)) {
1527 cpu = smp_processor_id();
1528 /* Update mapped_file data for mem_cgroup "from" */
1529 stat = &from->stat;
1530 cpustat = &stat->cpustat[cpu];
1531 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1532 -1);
1533
1534 /* Update mapped_file data for mem_cgroup "to" */
1535 stat = &to->stat;
1536 cpustat = &stat->cpustat[cpu];
1537 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1538 1);
1539 }
1540
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001541 if (do_swap_account && !mem_cgroup_is_root(from))
Balbir Singhf64c3f52009-09-23 15:56:37 -07001542 res_counter_uncharge(&from->memsw, PAGE_SIZE, NULL);
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001543 css_put(&from->css);
1544
1545 css_get(&to->css);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001546 pc->mem_cgroup = to;
1547 mem_cgroup_charge_statistics(to, pc, true);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001548 ret = 0;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001549out:
1550 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001551 /*
1552 * We charges against "to" which may not have any tasks. Then, "to"
1553 * can be under rmdir(). But in current implementation, caller of
1554 * this function is just force_empty() and it's garanteed that
1555 * "to" is never removed. So, we don't check rmdir status here.
1556 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001557 return ret;
1558}
1559
1560/*
1561 * move charges to its parent.
1562 */
1563
1564static int mem_cgroup_move_parent(struct page_cgroup *pc,
1565 struct mem_cgroup *child,
1566 gfp_t gfp_mask)
1567{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001568 struct page *page = pc->page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001569 struct cgroup *cg = child->css.cgroup;
1570 struct cgroup *pcg = cg->parent;
1571 struct mem_cgroup *parent;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001572 int ret;
1573
1574 /* Is ROOT ? */
1575 if (!pcg)
1576 return -EINVAL;
1577
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001578
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001579 parent = mem_cgroup_from_cont(pcg);
1580
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001581
Balbir Singhf64c3f52009-09-23 15:56:37 -07001582 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false, page);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001583 if (ret || !parent)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001584 return ret;
1585
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001586 if (!get_page_unless_zero(page)) {
1587 ret = -EBUSY;
1588 goto uncharge;
1589 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001590
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001591 ret = isolate_lru_page(page);
1592
1593 if (ret)
1594 goto cancel;
1595
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001596 ret = mem_cgroup_move_account(pc, child, parent);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001597
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001598 putback_lru_page(page);
1599 if (!ret) {
1600 put_page(page);
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001601 /* drop extra refcnt by try_charge() */
1602 css_put(&parent->css);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001603 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001604 }
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001605
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001606cancel:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001607 put_page(page);
1608uncharge:
1609 /* drop extra refcnt by try_charge() */
1610 css_put(&parent->css);
1611 /* uncharge if move fails */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001612 if (!mem_cgroup_is_root(parent)) {
1613 res_counter_uncharge(&parent->res, PAGE_SIZE, NULL);
1614 if (do_swap_account)
1615 res_counter_uncharge(&parent->memsw, PAGE_SIZE, NULL);
1616 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001617 return ret;
1618}
1619
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001620/*
1621 * Charge the memory controller for page usage.
1622 * Return
1623 * 0 if the charge was successful
1624 * < 0 if the cgroup is over its limit
1625 */
1626static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
1627 gfp_t gfp_mask, enum charge_type ctype,
1628 struct mem_cgroup *memcg)
1629{
1630 struct mem_cgroup *mem;
1631 struct page_cgroup *pc;
1632 int ret;
1633
1634 pc = lookup_page_cgroup(page);
1635 /* can happen at boot */
1636 if (unlikely(!pc))
1637 return 0;
1638 prefetchw(pc);
1639
1640 mem = memcg;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001641 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true, page);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001642 if (ret || !mem)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001643 return ret;
1644
1645 __mem_cgroup_commit_charge(mem, pc, ctype);
1646 return 0;
1647}
1648
1649int mem_cgroup_newpage_charge(struct page *page,
1650 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001651{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001652 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001653 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001654 if (PageCompound(page))
1655 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001656 /*
1657 * If already mapped, we don't have to account.
1658 * If page cache, page->mapping has address_space.
1659 * But page->mapping may have out-of-use anon_vma pointer,
1660 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
1661 * is NULL.
1662 */
1663 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
1664 return 0;
1665 if (unlikely(!mm))
1666 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001667 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001668 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001669}
1670
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001671static void
1672__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1673 enum charge_type ctype);
1674
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001675int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
1676 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08001677{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001678 struct mem_cgroup *mem = NULL;
1679 int ret;
1680
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001681 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001682 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001683 if (PageCompound(page))
1684 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001685 /*
1686 * Corner case handling. This is called from add_to_page_cache()
1687 * in usual. But some FS (shmem) precharges this page before calling it
1688 * and call add_to_page_cache() with GFP_NOWAIT.
1689 *
1690 * For GFP_NOWAIT case, the page may be pre-charged before calling
1691 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
1692 * charge twice. (It works but has to pay a bit larger cost.)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001693 * And when the page is SwapCache, it should take swap information
1694 * into account. This is under lock_page() now.
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001695 */
1696 if (!(gfp_mask & __GFP_WAIT)) {
1697 struct page_cgroup *pc;
1698
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001699
1700 pc = lookup_page_cgroup(page);
1701 if (!pc)
1702 return 0;
1703 lock_page_cgroup(pc);
1704 if (PageCgroupUsed(pc)) {
1705 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001706 return 0;
1707 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001708 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001709 }
1710
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001711 if (unlikely(!mm && !mem))
Balbir Singh8697d332008-02-07 00:13:59 -08001712 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001713
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -07001714 if (page_is_file_cache(page))
1715 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001716 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001717
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001718 /* shmem */
1719 if (PageSwapCache(page)) {
1720 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1721 if (!ret)
1722 __mem_cgroup_commit_charge_swapin(page, mem,
1723 MEM_CGROUP_CHARGE_TYPE_SHMEM);
1724 } else
1725 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
1726 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001727
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001728 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001729}
1730
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001731/*
1732 * While swap-in, try_charge -> commit or cancel, the page is locked.
1733 * And when try_charge() successfully returns, one refcnt to memcg without
1734 * struct page_cgroup is aquired. This refcnt will be cumsumed by
1735 * "commit()" or removed by "cancel()"
1736 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001737int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
1738 struct page *page,
1739 gfp_t mask, struct mem_cgroup **ptr)
1740{
1741 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001742 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001743
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001744 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001745 return 0;
1746
1747 if (!do_swap_account)
1748 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001749 /*
1750 * A racing thread's fault, or swapoff, may have already updated
1751 * the pte, and even removed page from swap cache: return success
1752 * to go on to do_swap_page()'s pte_same() test, which should fail.
1753 */
1754 if (!PageSwapCache(page))
1755 return 0;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001756 mem = try_get_mem_cgroup_from_swapcache(page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001757 if (!mem)
1758 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001759 *ptr = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001760 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true, page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001761 /* drop extra refcnt from tryget */
1762 css_put(&mem->css);
1763 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001764charge_cur_mm:
1765 if (unlikely(!mm))
1766 mm = &init_mm;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001767 return __mem_cgroup_try_charge(mm, mask, ptr, true, page);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001768}
1769
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001770static void
1771__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1772 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001773{
1774 struct page_cgroup *pc;
1775
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001776 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001777 return;
1778 if (!ptr)
1779 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001780 cgroup_exclude_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001781 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001782 mem_cgroup_lru_del_before_commit_swapcache(page);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001783 __mem_cgroup_commit_charge(ptr, pc, ctype);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001784 mem_cgroup_lru_add_after_commit_swapcache(page);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001785 /*
1786 * Now swap is on-memory. This means this page may be
1787 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08001788 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
1789 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
1790 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001791 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08001792 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001793 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001794 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001795 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001796
1797 id = swap_cgroup_record(ent, 0);
1798 rcu_read_lock();
1799 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001800 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001801 /*
1802 * This recorded memcg can be obsolete one. So, avoid
1803 * calling css_tryget
1804 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001805 if (!mem_cgroup_is_root(memcg))
1806 res_counter_uncharge(&memcg->memsw, PAGE_SIZE,
1807 NULL);
1808 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001809 mem_cgroup_put(memcg);
1810 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001811 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001812 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001813 /*
1814 * At swapin, we may charge account against cgroup which has no tasks.
1815 * So, rmdir()->pre_destroy() can be called while we do this charge.
1816 * In that case, we need to call pre_destroy() again. check it here.
1817 */
1818 cgroup_release_and_wakeup_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001819}
1820
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001821void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
1822{
1823 __mem_cgroup_commit_charge_swapin(page, ptr,
1824 MEM_CGROUP_CHARGE_TYPE_MAPPED);
1825}
1826
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001827void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
1828{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001829 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001830 return;
1831 if (!mem)
1832 return;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001833 if (!mem_cgroup_is_root(mem)) {
1834 res_counter_uncharge(&mem->res, PAGE_SIZE, NULL);
1835 if (do_swap_account)
1836 res_counter_uncharge(&mem->memsw, PAGE_SIZE, NULL);
1837 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001838 css_put(&mem->css);
1839}
1840
1841
Balbir Singh8697d332008-02-07 00:13:59 -08001842/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001843 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001844 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001845static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001846__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001847{
Hugh Dickins82895462008-03-04 14:29:08 -08001848 struct page_cgroup *pc;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001849 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001850 struct mem_cgroup_per_zone *mz;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001851 bool soft_limit_excess = false;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001852
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001853 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001854 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07001855
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001856 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001857 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001858
Balbir Singh8697d332008-02-07 00:13:59 -08001859 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08001860 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08001861 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001862 pc = lookup_page_cgroup(page);
1863 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001864 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001865
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001866 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001867
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001868 mem = pc->mem_cgroup;
1869
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001870 if (!PageCgroupUsed(pc))
1871 goto unlock_out;
1872
1873 switch (ctype) {
1874 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07001875 case MEM_CGROUP_CHARGE_TYPE_DROP:
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001876 if (page_mapped(page))
1877 goto unlock_out;
1878 break;
1879 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
1880 if (!PageAnon(page)) { /* Shared memory */
1881 if (page->mapping && !page_is_file_cache(page))
1882 goto unlock_out;
1883 } else if (page_mapped(page)) /* Anon */
1884 goto unlock_out;
1885 break;
1886 default:
1887 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001888 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001889
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001890 if (!mem_cgroup_is_root(mem)) {
1891 res_counter_uncharge(&mem->res, PAGE_SIZE, &soft_limit_excess);
1892 if (do_swap_account &&
1893 (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT))
1894 res_counter_uncharge(&mem->memsw, PAGE_SIZE, NULL);
1895 }
1896 if (ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1897 mem_cgroup_swap_statistics(mem, true);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001898 mem_cgroup_charge_statistics(mem, pc, false);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001899
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001900 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001901 /*
1902 * pc->mem_cgroup is not cleared here. It will be accessed when it's
1903 * freed from LRU. This is safe because uncharged page is expected not
1904 * to be reused (freed soon). Exception is SwapCache, it's handled by
1905 * special functions.
1906 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08001907
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001908 mz = page_cgroup_zoneinfo(pc);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001909 unlock_page_cgroup(pc);
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08001910
Balbir Singhf64c3f52009-09-23 15:56:37 -07001911 if (soft_limit_excess && mem_cgroup_soft_limit_check(mem))
1912 mem_cgroup_update_tree(mem, page);
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08001913 /* at swapout, this memcg will be accessed to record to swap */
1914 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1915 css_put(&mem->css);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001916
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001917 return mem;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001918
1919unlock_out:
1920 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001921 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08001922}
1923
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001924void mem_cgroup_uncharge_page(struct page *page)
1925{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001926 /* early check. */
1927 if (page_mapped(page))
1928 return;
1929 if (page->mapping && !PageAnon(page))
1930 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001931 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
1932}
1933
1934void mem_cgroup_uncharge_cache_page(struct page *page)
1935{
1936 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07001937 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001938 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
1939}
1940
Daisuke Nishimurae767e052009-05-28 14:34:28 -07001941#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001942/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07001943 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001944 * memcg information is recorded to swap_cgroup of "ent"
1945 */
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07001946void
1947mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001948{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001949 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07001950 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001951
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07001952 if (!swapout) /* this was a swap cache but the swap is unused ! */
1953 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
1954
1955 memcg = __mem_cgroup_uncharge_common(page, ctype);
1956
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001957 /* record memcg information */
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07001958 if (do_swap_account && swapout && memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001959 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001960 mem_cgroup_get(memcg);
1961 }
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07001962 if (swapout && memcg)
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08001963 css_put(&memcg->css);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001964}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07001965#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001966
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001967#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1968/*
1969 * called from swap_entry_free(). remove record in swap_cgroup and
1970 * uncharge "memsw" account.
1971 */
1972void mem_cgroup_uncharge_swap(swp_entry_t ent)
1973{
1974 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001975 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001976
1977 if (!do_swap_account)
1978 return;
1979
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001980 id = swap_cgroup_record(ent, 0);
1981 rcu_read_lock();
1982 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001983 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001984 /*
1985 * We uncharge this because swap is freed.
1986 * This memcg can be obsolete one. We avoid calling css_tryget
1987 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001988 if (!mem_cgroup_is_root(memcg))
1989 res_counter_uncharge(&memcg->memsw, PAGE_SIZE, NULL);
1990 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001991 mem_cgroup_put(memcg);
1992 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001993 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001994}
1995#endif
1996
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001997/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001998 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
1999 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002000 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002001int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002002{
2003 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002004 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002005 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08002006
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002007 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07002008 return 0;
2009
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002010 pc = lookup_page_cgroup(page);
2011 lock_page_cgroup(pc);
2012 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002013 mem = pc->mem_cgroup;
2014 css_get(&mem->css);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08002015 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002016 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002017
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002018 if (mem) {
Balbir Singhf64c3f52009-09-23 15:56:37 -07002019 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false,
2020 page);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002021 css_put(&mem->css);
2022 }
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002023 *ptr = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002024 return ret;
2025}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08002026
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002027/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002028void mem_cgroup_end_migration(struct mem_cgroup *mem,
2029 struct page *oldpage, struct page *newpage)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002030{
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002031 struct page *target, *unused;
2032 struct page_cgroup *pc;
2033 enum charge_type ctype;
2034
2035 if (!mem)
2036 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002037 cgroup_exclude_rmdir(&mem->css);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002038 /* at migration success, oldpage->mapping is NULL. */
2039 if (oldpage->mapping) {
2040 target = oldpage;
2041 unused = NULL;
2042 } else {
2043 target = newpage;
2044 unused = oldpage;
2045 }
2046
2047 if (PageAnon(target))
2048 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
2049 else if (page_is_file_cache(target))
2050 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
2051 else
2052 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
2053
2054 /* unused page is not on radix-tree now. */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002055 if (unused)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002056 __mem_cgroup_uncharge_common(unused, ctype);
2057
2058 pc = lookup_page_cgroup(target);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002059 /*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002060 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
2061 * So, double-counting is effectively avoided.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002062 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002063 __mem_cgroup_commit_charge(mem, pc, ctype);
2064
2065 /*
2066 * Both of oldpage and newpage are still under lock_page().
2067 * Then, we don't have to care about race in radix-tree.
2068 * But we have to be careful that this page is unmapped or not.
2069 *
2070 * There is a case for !page_mapped(). At the start of
2071 * migration, oldpage was mapped. But now, it's zapped.
2072 * But we know *target* page is not freed/reused under us.
2073 * mem_cgroup_uncharge_page() does all necessary checks.
2074 */
2075 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
2076 mem_cgroup_uncharge_page(target);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002077 /*
2078 * At migration, we may charge account against cgroup which has no tasks
2079 * So, rmdir()->pre_destroy() can be called while we do this charge.
2080 * In that case, we need to call pre_destroy() again. check it here.
2081 */
2082 cgroup_release_and_wakeup_rmdir(&mem->css);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002083}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08002084
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002085/*
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002086 * A call to try to shrink memory usage on charge failure at shmem's swapin.
2087 * Calling hierarchical_reclaim is not enough because we should update
2088 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
2089 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
2090 * not from the memcg which this page would be charged to.
2091 * try_charge_swapin does all of these works properly.
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002092 */
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002093int mem_cgroup_shmem_charge_fallback(struct page *page,
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002094 struct mm_struct *mm,
2095 gfp_t gfp_mask)
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002096{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002097 struct mem_cgroup *mem = NULL;
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002098 int ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002099
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002100 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002101 return 0;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002102
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002103 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2104 if (!ret)
2105 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002106
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002107 return ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002108}
2109
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002110static DEFINE_MUTEX(set_limit_mutex);
2111
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08002112static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002113 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002114{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002115 int retry_count;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002116 int progress;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002117 u64 memswlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002118 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002119 int children = mem_cgroup_count_children(memcg);
2120 u64 curusage, oldusage;
2121
2122 /*
2123 * For keeping hierarchical_reclaim simple, how long we should retry
2124 * is depends on callers. We set our retry-count to be function
2125 * of # of children which we should visit in this loop.
2126 */
2127 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
2128
2129 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002130
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002131 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002132 if (signal_pending(current)) {
2133 ret = -EINTR;
2134 break;
2135 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002136 /*
2137 * Rather than hide all in some function, I do this in
2138 * open coded manner. You see what this really does.
2139 * We have to guarantee mem->res.limit < mem->memsw.limit.
2140 */
2141 mutex_lock(&set_limit_mutex);
2142 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2143 if (memswlimit < val) {
2144 ret = -EINVAL;
2145 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002146 break;
2147 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002148 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07002149 if (!ret) {
2150 if (memswlimit == val)
2151 memcg->memsw_is_minimum = true;
2152 else
2153 memcg->memsw_is_minimum = false;
2154 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002155 mutex_unlock(&set_limit_mutex);
2156
2157 if (!ret)
2158 break;
2159
Balbir Singh4e416952009-09-23 15:56:39 -07002160 progress = mem_cgroup_hierarchical_reclaim(memcg, NULL,
2161 GFP_KERNEL,
2162 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002163 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
2164 /* Usage is reduced ? */
2165 if (curusage >= oldusage)
2166 retry_count--;
2167 else
2168 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002169 }
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002170
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002171 return ret;
2172}
2173
Li Zefan338c8432009-06-17 16:27:15 -07002174static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
2175 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002176{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002177 int retry_count;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002178 u64 memlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002179 int children = mem_cgroup_count_children(memcg);
2180 int ret = -EBUSY;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002181
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002182 /* see mem_cgroup_resize_res_limit */
2183 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
2184 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002185 while (retry_count) {
2186 if (signal_pending(current)) {
2187 ret = -EINTR;
2188 break;
2189 }
2190 /*
2191 * Rather than hide all in some function, I do this in
2192 * open coded manner. You see what this really does.
2193 * We have to guarantee mem->res.limit < mem->memsw.limit.
2194 */
2195 mutex_lock(&set_limit_mutex);
2196 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2197 if (memlimit > val) {
2198 ret = -EINVAL;
2199 mutex_unlock(&set_limit_mutex);
2200 break;
2201 }
2202 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07002203 if (!ret) {
2204 if (memlimit == val)
2205 memcg->memsw_is_minimum = true;
2206 else
2207 memcg->memsw_is_minimum = false;
2208 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002209 mutex_unlock(&set_limit_mutex);
2210
2211 if (!ret)
2212 break;
2213
Balbir Singh4e416952009-09-23 15:56:39 -07002214 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh75822b42009-09-23 15:56:38 -07002215 MEM_CGROUP_RECLAIM_NOSWAP |
2216 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002217 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002218 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002219 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002220 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002221 else
2222 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002223 }
2224 return ret;
2225}
2226
Balbir Singh4e416952009-09-23 15:56:39 -07002227unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
2228 gfp_t gfp_mask, int nid,
2229 int zid)
2230{
2231 unsigned long nr_reclaimed = 0;
2232 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
2233 unsigned long reclaimed;
2234 int loop = 0;
2235 struct mem_cgroup_tree_per_zone *mctz;
2236
2237 if (order > 0)
2238 return 0;
2239
2240 mctz = soft_limit_tree_node_zone(nid, zid);
2241 /*
2242 * This loop can run a while, specially if mem_cgroup's continuously
2243 * keep exceeding their soft limit and putting the system under
2244 * pressure
2245 */
2246 do {
2247 if (next_mz)
2248 mz = next_mz;
2249 else
2250 mz = mem_cgroup_largest_soft_limit_node(mctz);
2251 if (!mz)
2252 break;
2253
2254 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
2255 gfp_mask,
2256 MEM_CGROUP_RECLAIM_SOFT);
2257 nr_reclaimed += reclaimed;
2258 spin_lock(&mctz->lock);
2259
2260 /*
2261 * If we failed to reclaim anything from this memory cgroup
2262 * it is time to move on to the next cgroup
2263 */
2264 next_mz = NULL;
2265 if (!reclaimed) {
2266 do {
2267 /*
2268 * Loop until we find yet another one.
2269 *
2270 * By the time we get the soft_limit lock
2271 * again, someone might have aded the
2272 * group back on the RB tree. Iterate to
2273 * make sure we get a different mem.
2274 * mem_cgroup_largest_soft_limit_node returns
2275 * NULL if no other cgroup is present on
2276 * the tree
2277 */
2278 next_mz =
2279 __mem_cgroup_largest_soft_limit_node(mctz);
2280 if (next_mz == mz) {
2281 css_put(&next_mz->mem->css);
2282 next_mz = NULL;
2283 } else /* next_mz == NULL or other memcg */
2284 break;
2285 } while (1);
2286 }
2287 mz->usage_in_excess =
2288 res_counter_soft_limit_excess(&mz->mem->res);
2289 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
2290 /*
2291 * One school of thought says that we should not add
2292 * back the node to the tree if reclaim returns 0.
2293 * But our reclaim could return 0, simply because due
2294 * to priority we are exposing a smaller subset of
2295 * memory to reclaim from. Consider this as a longer
2296 * term TODO.
2297 */
2298 if (mz->usage_in_excess)
2299 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz);
2300 spin_unlock(&mctz->lock);
2301 css_put(&mz->mem->css);
2302 loop++;
2303 /*
2304 * Could not reclaim anything and there are no more
2305 * mem cgroups to try or we seem to be looping without
2306 * reclaiming anything.
2307 */
2308 if (!nr_reclaimed &&
2309 (next_mz == NULL ||
2310 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
2311 break;
2312 } while (!nr_reclaimed);
2313 if (next_mz)
2314 css_put(&next_mz->mem->css);
2315 return nr_reclaimed;
2316}
2317
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002318/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002319 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002320 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
2321 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002322static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002323 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002324{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002325 struct zone *zone;
2326 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002327 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002328 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002329 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002330 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002331
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002332 zone = &NODE_DATA(node)->node_zones[zid];
2333 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07002334 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002335
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002336 loop = MEM_CGROUP_ZSTAT(mz, lru);
2337 /* give some margin against EBUSY etc...*/
2338 loop += 256;
2339 busy = NULL;
2340 while (loop--) {
2341 ret = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002342 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002343 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002344 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002345 break;
2346 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002347 pc = list_entry(list->prev, struct page_cgroup, lru);
2348 if (busy == pc) {
2349 list_move(&pc->lru, list);
2350 busy = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002351 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002352 continue;
2353 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002354 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002355
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -08002356 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002357 if (ret == -ENOMEM)
2358 break;
2359
2360 if (ret == -EBUSY || ret == -EINVAL) {
2361 /* found lock contention or "pc" is obsolete. */
2362 busy = pc;
2363 cond_resched();
2364 } else
2365 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002366 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002367
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002368 if (!ret && !list_empty(list))
2369 return -EBUSY;
2370 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002371}
2372
2373/*
2374 * make mem_cgroup's charge to be 0 if there is no task.
2375 * This enables deleting this mem_cgroup.
2376 */
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002377static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002378{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002379 int ret;
2380 int node, zid, shrink;
2381 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002382 struct cgroup *cgrp = mem->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08002383
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002384 css_get(&mem->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002385
2386 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002387 /* should free all ? */
2388 if (free_all)
2389 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002390move_account:
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002391 while (mem->res.usage > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002392 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002393 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002394 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002395 ret = -EINTR;
2396 if (signal_pending(current))
2397 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002398 /* This is for making all *used* pages to be on LRU. */
2399 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002400 ret = 0;
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08002401 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002402 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07002403 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002404 for_each_lru(l) {
2405 ret = mem_cgroup_force_empty_list(mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002406 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002407 if (ret)
2408 break;
2409 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002410 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002411 if (ret)
2412 break;
2413 }
2414 /* it seems parent cgroup doesn't have enough mem */
2415 if (ret == -ENOMEM)
2416 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002417 cond_resched();
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002418 }
2419 ret = 0;
2420out:
2421 css_put(&mem->css);
2422 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002423
2424try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002425 /* returns EBUSY if there is a task or if we come here twice. */
2426 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002427 ret = -EBUSY;
2428 goto out;
2429 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002430 /* we call try-to-free pages for make this cgroup empty */
2431 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002432 /* try to free all pages in this cgroup */
2433 shrink = 1;
2434 while (nr_retries && mem->res.usage > 0) {
2435 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002436
2437 if (signal_pending(current)) {
2438 ret = -EINTR;
2439 goto out;
2440 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002441 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
2442 false, get_swappiness(mem));
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002443 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002444 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002445 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02002446 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002447 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002448
2449 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002450 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002451 /* try move_account...there may be some *locked* pages. */
2452 if (mem->res.usage)
2453 goto move_account;
2454 ret = 0;
2455 goto out;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002456}
2457
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002458int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
2459{
2460 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
2461}
2462
2463
Balbir Singh18f59ea2009-01-07 18:08:07 -08002464static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
2465{
2466 return mem_cgroup_from_cont(cont)->use_hierarchy;
2467}
2468
2469static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
2470 u64 val)
2471{
2472 int retval = 0;
2473 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2474 struct cgroup *parent = cont->parent;
2475 struct mem_cgroup *parent_mem = NULL;
2476
2477 if (parent)
2478 parent_mem = mem_cgroup_from_cont(parent);
2479
2480 cgroup_lock();
2481 /*
2482 * If parent's use_hiearchy is set, we can't make any modifications
2483 * in the child subtrees. If it is unset, then the change can
2484 * occur, provided the current cgroup has no children.
2485 *
2486 * For the root cgroup, parent_mem is NULL, we allow value to be
2487 * set if there are no children.
2488 */
2489 if ((!parent_mem || !parent_mem->use_hierarchy) &&
2490 (val == 1 || val == 0)) {
2491 if (list_empty(&cont->children))
2492 mem->use_hierarchy = val;
2493 else
2494 retval = -EBUSY;
2495 } else
2496 retval = -EINVAL;
2497 cgroup_unlock();
2498
2499 return retval;
2500}
2501
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002502struct mem_cgroup_idx_data {
2503 s64 val;
2504 enum mem_cgroup_stat_index idx;
2505};
2506
2507static int
2508mem_cgroup_get_idx_stat(struct mem_cgroup *mem, void *data)
2509{
2510 struct mem_cgroup_idx_data *d = data;
2511 d->val += mem_cgroup_read_stat(&mem->stat, d->idx);
2512 return 0;
2513}
2514
2515static void
2516mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem,
2517 enum mem_cgroup_stat_index idx, s64 *val)
2518{
2519 struct mem_cgroup_idx_data d;
2520 d.idx = idx;
2521 d.val = 0;
2522 mem_cgroup_walk_tree(mem, &d, mem_cgroup_get_idx_stat);
2523 *val = d.val;
2524}
2525
Paul Menage2c3daa72008-04-29 00:59:58 -07002526static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002527{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002528 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002529 u64 idx_val, val;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002530 int type, name;
2531
2532 type = MEMFILE_TYPE(cft->private);
2533 name = MEMFILE_ATTR(cft->private);
2534 switch (type) {
2535 case _MEM:
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002536 if (name == RES_USAGE && mem_cgroup_is_root(mem)) {
2537 mem_cgroup_get_recursive_idx_stat(mem,
2538 MEM_CGROUP_STAT_CACHE, &idx_val);
2539 val = idx_val;
2540 mem_cgroup_get_recursive_idx_stat(mem,
2541 MEM_CGROUP_STAT_RSS, &idx_val);
2542 val += idx_val;
2543 val <<= PAGE_SHIFT;
2544 } else
2545 val = res_counter_read_u64(&mem->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002546 break;
2547 case _MEMSWAP:
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002548 if (name == RES_USAGE && mem_cgroup_is_root(mem)) {
2549 mem_cgroup_get_recursive_idx_stat(mem,
2550 MEM_CGROUP_STAT_CACHE, &idx_val);
2551 val = idx_val;
2552 mem_cgroup_get_recursive_idx_stat(mem,
2553 MEM_CGROUP_STAT_RSS, &idx_val);
2554 val += idx_val;
2555 mem_cgroup_get_recursive_idx_stat(mem,
2556 MEM_CGROUP_STAT_SWAPOUT, &idx_val);
2557 val <<= PAGE_SHIFT;
2558 } else
2559 val = res_counter_read_u64(&mem->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002560 break;
2561 default:
2562 BUG();
2563 break;
2564 }
2565 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002566}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002567/*
2568 * The user of this function is...
2569 * RES_LIMIT.
2570 */
Paul Menage856c13a2008-07-25 01:47:04 -07002571static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
2572 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002573{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002574 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002575 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002576 unsigned long long val;
2577 int ret;
2578
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002579 type = MEMFILE_TYPE(cft->private);
2580 name = MEMFILE_ATTR(cft->private);
2581 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002582 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07002583 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
2584 ret = -EINVAL;
2585 break;
2586 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002587 /* This function does all necessary parse...reuse it */
2588 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002589 if (ret)
2590 break;
2591 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002592 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002593 else
2594 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002595 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07002596 case RES_SOFT_LIMIT:
2597 ret = res_counter_memparse_write_strategy(buffer, &val);
2598 if (ret)
2599 break;
2600 /*
2601 * For memsw, soft limits are hard to implement in terms
2602 * of semantics, for now, we support soft limits for
2603 * control without swap
2604 */
2605 if (type == _MEM)
2606 ret = res_counter_set_soft_limit(&memcg->res, val);
2607 else
2608 ret = -EINVAL;
2609 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002610 default:
2611 ret = -EINVAL; /* should be BUG() ? */
2612 break;
2613 }
2614 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002615}
2616
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08002617static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
2618 unsigned long long *mem_limit, unsigned long long *memsw_limit)
2619{
2620 struct cgroup *cgroup;
2621 unsigned long long min_limit, min_memsw_limit, tmp;
2622
2623 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2624 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2625 cgroup = memcg->css.cgroup;
2626 if (!memcg->use_hierarchy)
2627 goto out;
2628
2629 while (cgroup->parent) {
2630 cgroup = cgroup->parent;
2631 memcg = mem_cgroup_from_cont(cgroup);
2632 if (!memcg->use_hierarchy)
2633 break;
2634 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
2635 min_limit = min(min_limit, tmp);
2636 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2637 min_memsw_limit = min(min_memsw_limit, tmp);
2638 }
2639out:
2640 *mem_limit = min_limit;
2641 *memsw_limit = min_memsw_limit;
2642 return;
2643}
2644
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002645static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002646{
2647 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002648 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002649
2650 mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002651 type = MEMFILE_TYPE(event);
2652 name = MEMFILE_ATTR(event);
2653 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002654 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002655 if (type == _MEM)
2656 res_counter_reset_max(&mem->res);
2657 else
2658 res_counter_reset_max(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002659 break;
2660 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002661 if (type == _MEM)
2662 res_counter_reset_failcnt(&mem->res);
2663 else
2664 res_counter_reset_failcnt(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002665 break;
2666 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07002667
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07002668 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002669}
2670
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002671
2672/* For read statistics */
2673enum {
2674 MCS_CACHE,
2675 MCS_RSS,
Balbir Singhd69b0422009-06-17 16:26:34 -07002676 MCS_MAPPED_FILE,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002677 MCS_PGPGIN,
2678 MCS_PGPGOUT,
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002679 MCS_SWAP,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002680 MCS_INACTIVE_ANON,
2681 MCS_ACTIVE_ANON,
2682 MCS_INACTIVE_FILE,
2683 MCS_ACTIVE_FILE,
2684 MCS_UNEVICTABLE,
2685 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002686};
2687
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002688struct mcs_total_stat {
2689 s64 stat[NR_MCS_STAT];
2690};
2691
2692struct {
2693 char *local_name;
2694 char *total_name;
2695} memcg_stat_strings[NR_MCS_STAT] = {
2696 {"cache", "total_cache"},
2697 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07002698 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002699 {"pgpgin", "total_pgpgin"},
2700 {"pgpgout", "total_pgpgout"},
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002701 {"swap", "total_swap"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002702 {"inactive_anon", "total_inactive_anon"},
2703 {"active_anon", "total_active_anon"},
2704 {"inactive_file", "total_inactive_file"},
2705 {"active_file", "total_active_file"},
2706 {"unevictable", "total_unevictable"}
2707};
2708
2709
2710static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
2711{
2712 struct mcs_total_stat *s = data;
2713 s64 val;
2714
2715 /* per cpu stat */
2716 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE);
2717 s->stat[MCS_CACHE] += val * PAGE_SIZE;
2718 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
2719 s->stat[MCS_RSS] += val * PAGE_SIZE;
Balbir Singhd69b0422009-06-17 16:26:34 -07002720 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_MAPPED_FILE);
2721 s->stat[MCS_MAPPED_FILE] += val * PAGE_SIZE;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002722 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT);
2723 s->stat[MCS_PGPGIN] += val;
2724 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT);
2725 s->stat[MCS_PGPGOUT] += val;
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002726 if (do_swap_account) {
2727 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_SWAPOUT);
2728 s->stat[MCS_SWAP] += val * PAGE_SIZE;
2729 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002730
2731 /* per zone stat */
2732 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
2733 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
2734 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
2735 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
2736 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
2737 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
2738 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
2739 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
2740 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
2741 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
2742 return 0;
2743}
2744
2745static void
2746mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
2747{
2748 mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
2749}
2750
Paul Menagec64745c2008-04-29 01:00:02 -07002751static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
2752 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002753{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002754 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002755 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002756 int i;
2757
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002758 memset(&mystat, 0, sizeof(mystat));
2759 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002760
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002761 for (i = 0; i < NR_MCS_STAT; i++) {
2762 if (i == MCS_SWAP && !do_swap_account)
2763 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002764 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002765 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002766
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002767 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08002768 {
2769 unsigned long long limit, memsw_limit;
2770 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
2771 cb->fill(cb, "hierarchical_memory_limit", limit);
2772 if (do_swap_account)
2773 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
2774 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002775
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002776 memset(&mystat, 0, sizeof(mystat));
2777 mem_cgroup_get_total_stat(mem_cont, &mystat);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002778 for (i = 0; i < NR_MCS_STAT; i++) {
2779 if (i == MCS_SWAP && !do_swap_account)
2780 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002781 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002782 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002783
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002784#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08002785 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08002786
2787 {
2788 int nid, zid;
2789 struct mem_cgroup_per_zone *mz;
2790 unsigned long recent_rotated[2] = {0, 0};
2791 unsigned long recent_scanned[2] = {0, 0};
2792
2793 for_each_online_node(nid)
2794 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
2795 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
2796
2797 recent_rotated[0] +=
2798 mz->reclaim_stat.recent_rotated[0];
2799 recent_rotated[1] +=
2800 mz->reclaim_stat.recent_rotated[1];
2801 recent_scanned[0] +=
2802 mz->reclaim_stat.recent_scanned[0];
2803 recent_scanned[1] +=
2804 mz->reclaim_stat.recent_scanned[1];
2805 }
2806 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
2807 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
2808 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
2809 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
2810 }
2811#endif
2812
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002813 return 0;
2814}
2815
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002816static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
2817{
2818 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2819
2820 return get_swappiness(memcg);
2821}
2822
2823static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
2824 u64 val)
2825{
2826 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2827 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08002828
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002829 if (val > 100)
2830 return -EINVAL;
2831
2832 if (cgrp->parent == NULL)
2833 return -EINVAL;
2834
2835 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08002836
2837 cgroup_lock();
2838
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002839 /* If under hierarchy, only empty-root can set this value */
2840 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08002841 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
2842 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002843 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08002844 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002845
2846 spin_lock(&memcg->reclaim_param_lock);
2847 memcg->swappiness = val;
2848 spin_unlock(&memcg->reclaim_param_lock);
2849
Li Zefan068b38c2009-01-15 13:51:26 -08002850 cgroup_unlock();
2851
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002852 return 0;
2853}
2854
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002855
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002856static struct cftype mem_cgroup_files[] = {
2857 {
Balbir Singh0eea1032008-02-07 00:13:57 -08002858 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002859 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07002860 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002861 },
2862 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002863 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002864 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002865 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002866 .read_u64 = mem_cgroup_read,
2867 },
2868 {
Balbir Singh0eea1032008-02-07 00:13:57 -08002869 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002870 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07002871 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07002872 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002873 },
2874 {
Balbir Singh296c81d2009-09-23 15:56:36 -07002875 .name = "soft_limit_in_bytes",
2876 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
2877 .write_string = mem_cgroup_write,
2878 .read_u64 = mem_cgroup_read,
2879 },
2880 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002881 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002882 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002883 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07002884 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002885 },
Balbir Singh8697d332008-02-07 00:13:59 -08002886 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002887 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07002888 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002889 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002890 {
2891 .name = "force_empty",
2892 .trigger = mem_cgroup_force_empty_write,
2893 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08002894 {
2895 .name = "use_hierarchy",
2896 .write_u64 = mem_cgroup_hierarchy_write,
2897 .read_u64 = mem_cgroup_hierarchy_read,
2898 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002899 {
2900 .name = "swappiness",
2901 .read_u64 = mem_cgroup_swappiness_read,
2902 .write_u64 = mem_cgroup_swappiness_write,
2903 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002904};
2905
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002906#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2907static struct cftype memsw_cgroup_files[] = {
2908 {
2909 .name = "memsw.usage_in_bytes",
2910 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
2911 .read_u64 = mem_cgroup_read,
2912 },
2913 {
2914 .name = "memsw.max_usage_in_bytes",
2915 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
2916 .trigger = mem_cgroup_reset,
2917 .read_u64 = mem_cgroup_read,
2918 },
2919 {
2920 .name = "memsw.limit_in_bytes",
2921 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
2922 .write_string = mem_cgroup_write,
2923 .read_u64 = mem_cgroup_read,
2924 },
2925 {
2926 .name = "memsw.failcnt",
2927 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
2928 .trigger = mem_cgroup_reset,
2929 .read_u64 = mem_cgroup_read,
2930 },
2931};
2932
2933static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2934{
2935 if (!do_swap_account)
2936 return 0;
2937 return cgroup_add_files(cont, ss, memsw_cgroup_files,
2938 ARRAY_SIZE(memsw_cgroup_files));
2939};
2940#else
2941static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2942{
2943 return 0;
2944}
2945#endif
2946
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002947static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2948{
2949 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002950 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07002951 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07002952 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002953 /*
2954 * This routine is called against possible nodes.
2955 * But it's BUG to call kmalloc() against offline node.
2956 *
2957 * TODO: this routine can waste much memory for nodes which will
2958 * never be onlined. It's better to use memory hotplug callback
2959 * function.
2960 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07002961 if (!node_state(node, N_NORMAL_MEMORY))
2962 tmp = -1;
2963 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002964 if (!pn)
2965 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002966
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002967 mem->info.nodeinfo[node] = pn;
2968 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002969
2970 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
2971 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07002972 for_each_lru(l)
2973 INIT_LIST_HEAD(&mz->lists[l]);
Balbir Singhf64c3f52009-09-23 15:56:37 -07002974 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07002975 mz->on_tree = false;
2976 mz->mem = mem;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002977 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002978 return 0;
2979}
2980
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002981static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2982{
2983 kfree(mem->info.nodeinfo[node]);
2984}
2985
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002986static int mem_cgroup_size(void)
2987{
2988 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
2989 return sizeof(struct mem_cgroup) + cpustat_size;
2990}
2991
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002992static struct mem_cgroup *mem_cgroup_alloc(void)
2993{
2994 struct mem_cgroup *mem;
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002995 int size = mem_cgroup_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002996
Jan Blunckc8dad2b2009-01-07 18:07:53 -08002997 if (size < PAGE_SIZE)
2998 mem = kmalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07002999 else
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003000 mem = vmalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003001
3002 if (mem)
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003003 memset(mem, 0, size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003004 return mem;
3005}
3006
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003007/*
3008 * At destroying mem_cgroup, references from swap_cgroup can remain.
3009 * (scanning all at force_empty is too costly...)
3010 *
3011 * Instead of clearing all references at force_empty, we remember
3012 * the number of reference from swap_cgroup and free mem_cgroup when
3013 * it goes down to 0.
3014 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003015 * Removal of cgroup itself succeeds regardless of refs from swap.
3016 */
3017
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003018static void __mem_cgroup_free(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003019{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003020 int node;
3021
Balbir Singhf64c3f52009-09-23 15:56:37 -07003022 mem_cgroup_remove_from_trees(mem);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003023 free_css_id(&mem_cgroup_subsys, &mem->css);
3024
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003025 for_each_node_state(node, N_POSSIBLE)
3026 free_mem_cgroup_per_zone_info(mem, node);
3027
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003028 if (mem_cgroup_size() < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003029 kfree(mem);
3030 else
3031 vfree(mem);
3032}
3033
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003034static void mem_cgroup_get(struct mem_cgroup *mem)
3035{
3036 atomic_inc(&mem->refcnt);
3037}
3038
3039static void mem_cgroup_put(struct mem_cgroup *mem)
3040{
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003041 if (atomic_dec_and_test(&mem->refcnt)) {
3042 struct mem_cgroup *parent = parent_mem_cgroup(mem);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003043 __mem_cgroup_free(mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003044 if (parent)
3045 mem_cgroup_put(parent);
3046 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003047}
3048
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003049/*
3050 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
3051 */
3052static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
3053{
3054 if (!mem->res.parent)
3055 return NULL;
3056 return mem_cgroup_from_res_counter(mem->res.parent, res);
3057}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003058
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003059#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3060static void __init enable_swap_cgroup(void)
3061{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003062 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003063 do_swap_account = 1;
3064}
3065#else
3066static void __init enable_swap_cgroup(void)
3067{
3068}
3069#endif
3070
Balbir Singhf64c3f52009-09-23 15:56:37 -07003071static int mem_cgroup_soft_limit_tree_init(void)
3072{
3073 struct mem_cgroup_tree_per_node *rtpn;
3074 struct mem_cgroup_tree_per_zone *rtpz;
3075 int tmp, node, zone;
3076
3077 for_each_node_state(node, N_POSSIBLE) {
3078 tmp = node;
3079 if (!node_state(node, N_NORMAL_MEMORY))
3080 tmp = -1;
3081 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
3082 if (!rtpn)
3083 return 1;
3084
3085 soft_limit_tree.rb_tree_per_node[node] = rtpn;
3086
3087 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
3088 rtpz = &rtpn->rb_tree_per_zone[zone];
3089 rtpz->rb_root = RB_ROOT;
3090 spin_lock_init(&rtpz->lock);
3091 }
3092 }
3093 return 0;
3094}
3095
Li Zefan0eb253e2009-01-15 13:51:25 -08003096static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003097mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
3098{
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003099 struct mem_cgroup *mem, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003100 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003101 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003102
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003103 mem = mem_cgroup_alloc();
3104 if (!mem)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003105 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08003106
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003107 for_each_node_state(node, N_POSSIBLE)
3108 if (alloc_mem_cgroup_per_zone_info(mem, node))
3109 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07003110
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003111 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003112 if (cont->parent == NULL) {
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003113 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003114 parent = NULL;
Balbir Singh4b3bde42009-09-23 15:56:32 -07003115 root_mem_cgroup = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07003116 if (mem_cgroup_soft_limit_tree_init())
3117 goto free_out;
3118
Balbir Singh18f59ea2009-01-07 18:08:07 -08003119 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003120 parent = mem_cgroup_from_cont(cont->parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003121 mem->use_hierarchy = parent->use_hierarchy;
3122 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003123
Balbir Singh18f59ea2009-01-07 18:08:07 -08003124 if (parent && parent->use_hierarchy) {
3125 res_counter_init(&mem->res, &parent->res);
3126 res_counter_init(&mem->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003127 /*
3128 * We increment refcnt of the parent to ensure that we can
3129 * safely access it on res_counter_charge/uncharge.
3130 * This refcnt will be decremented when freeing this
3131 * mem_cgroup(see mem_cgroup_put).
3132 */
3133 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003134 } else {
3135 res_counter_init(&mem->res, NULL);
3136 res_counter_init(&mem->memsw, NULL);
3137 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003138 mem->last_scanned_child = 0;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -08003139 spin_lock_init(&mem->reclaim_param_lock);
Balbir Singh6d61ef42009-01-07 18:08:06 -08003140
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003141 if (parent)
3142 mem->swappiness = get_swappiness(parent);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003143 atomic_set(&mem->refcnt, 1);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003144 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003145free_out:
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003146 __mem_cgroup_free(mem);
Balbir Singh4b3bde42009-09-23 15:56:32 -07003147 root_mem_cgroup = NULL;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003148 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003149}
3150
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003151static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08003152 struct cgroup *cont)
3153{
3154 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003155
3156 return mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08003157}
3158
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003159static void mem_cgroup_destroy(struct cgroup_subsys *ss,
3160 struct cgroup *cont)
3161{
Daisuke Nishimurac268e992009-01-15 13:51:13 -08003162 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08003163
Daisuke Nishimurac268e992009-01-15 13:51:13 -08003164 mem_cgroup_put(mem);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003165}
3166
3167static int mem_cgroup_populate(struct cgroup_subsys *ss,
3168 struct cgroup *cont)
3169{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003170 int ret;
3171
3172 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
3173 ARRAY_SIZE(mem_cgroup_files));
3174
3175 if (!ret)
3176 ret = register_memsw_files(cont, ss);
3177 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003178}
3179
Balbir Singh67e465a2008-02-07 00:13:54 -08003180static void mem_cgroup_move_task(struct cgroup_subsys *ss,
3181 struct cgroup *cont,
3182 struct cgroup *old_cont,
Ben Blumbe367d02009-09-23 15:56:31 -07003183 struct task_struct *p,
3184 bool threadgroup)
Balbir Singh67e465a2008-02-07 00:13:54 -08003185{
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08003186 mutex_lock(&memcg_tasklist);
Balbir Singh67e465a2008-02-07 00:13:54 -08003187 /*
Nikanth Karthikesanf9717d22009-01-07 18:08:11 -08003188 * FIXME: It's better to move charges of this process from old
3189 * memcg to new memcg. But it's just on TODO-List now.
Balbir Singh67e465a2008-02-07 00:13:54 -08003190 */
Daisuke Nishimura7f4d4542009-01-07 18:08:29 -08003191 mutex_unlock(&memcg_tasklist);
Balbir Singh67e465a2008-02-07 00:13:54 -08003192}
3193
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003194struct cgroup_subsys mem_cgroup_subsys = {
3195 .name = "memory",
3196 .subsys_id = mem_cgroup_subsys_id,
3197 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08003198 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003199 .destroy = mem_cgroup_destroy,
3200 .populate = mem_cgroup_populate,
Balbir Singh67e465a2008-02-07 00:13:54 -08003201 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003202 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003203 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003204};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003205
3206#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3207
3208static int __init disable_swap_account(char *s)
3209{
3210 really_do_swap_account = 0;
3211 return 1;
3212}
3213__setup("noswapaccount", disable_swap_account);
3214#endif