blob: 5f8f93d83edfaa4494a53e58b8a9bd91f514dfa3 [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>
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -080024#include <linux/hugetlb.h>
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -080025#include <linux/pagemap.h>
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080026#include <linux/smp.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080027#include <linux/page-flags.h>
Balbir Singh66e17072008-02-07 00:13:56 -080028#include <linux/backing-dev.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080029#include <linux/bit_spinlock.h>
30#include <linux/rcupdate.h>
Balbir Singhe2224322009-04-02 16:57:39 -070031#include <linux/limits.h>
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -080032#include <linux/mutex.h>
Balbir Singhf64c3f52009-09-23 15:56:37 -070033#include <linux/rbtree.h>
Balbir Singhb6ac57d2008-04-29 01:00:19 -070034#include <linux/slab.h>
Balbir Singh66e17072008-02-07 00:13:56 -080035#include <linux/swap.h>
Daisuke Nishimura02491442010-03-10 15:22:17 -080036#include <linux/swapops.h>
Balbir Singh66e17072008-02-07 00:13:56 -080037#include <linux/spinlock.h>
38#include <linux/fs.h>
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -080039#include <linux/seq_file.h>
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -070040#include <linux/vmalloc.h>
Christoph Lameterb69408e2008-10-18 20:26:14 -070041#include <linux/mm_inline.h>
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070042#include <linux/page_cgroup.h>
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -080043#include <linux/cpu.h>
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -080044#include "internal.h"
Balbir Singh8cdea7c2008-02-07 00:13:50 -080045
Balbir Singh8697d332008-02-07 00:13:59 -080046#include <asm/uaccess.h>
47
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070048struct cgroup_subsys mem_cgroup_subsys __read_mostly;
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070049#define MEM_CGROUP_RECLAIM_RETRIES 5
Balbir Singh4b3bde42009-09-23 15:56:32 -070050struct mem_cgroup *root_mem_cgroup __read_mostly;
Balbir Singh8cdea7c2008-02-07 00:13:50 -080051
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080052#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
Li Zefan338c8432009-06-17 16:27:15 -070053/* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080054int do_swap_account __read_mostly;
55static int really_do_swap_account __initdata = 1; /* for remember boot option*/
56#else
57#define do_swap_account (0)
58#endif
59
Balbir Singhf64c3f52009-09-23 15:56:37 -070060#define SOFTLIMIT_EVENTS_THRESH (1000)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080061
Balbir Singh8cdea7c2008-02-07 00:13:50 -080062/*
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080063 * Statistics for memory cgroup.
64 */
65enum mem_cgroup_stat_index {
66 /*
67 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
68 */
69 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
Balbir Singhd69b0422009-06-17 16:26:34 -070070 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -080071 MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */
Balaji Rao55e462b2008-05-01 04:35:12 -070072 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
73 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
Balbir Singhf64c3f52009-09-23 15:56:37 -070074 MEM_CGROUP_STAT_EVENTS, /* sum of pagein + pageout for internal use */
Balbir Singh0c3e73e2009-09-23 15:56:42 -070075 MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080076
77 MEM_CGROUP_STAT_NSTATS,
78};
79
80struct mem_cgroup_stat_cpu {
81 s64 count[MEM_CGROUP_STAT_NSTATS];
82} ____cacheline_aligned_in_smp;
83
84struct mem_cgroup_stat {
Jan Blunckc8dad2b2009-01-07 18:07:53 -080085 struct mem_cgroup_stat_cpu cpustat[0];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080086};
87
Balbir Singhf64c3f52009-09-23 15:56:37 -070088static inline void
89__mem_cgroup_stat_reset_safe(struct mem_cgroup_stat_cpu *stat,
90 enum mem_cgroup_stat_index idx)
91{
92 stat->count[idx] = 0;
93}
94
95static inline s64
96__mem_cgroup_stat_read_local(struct mem_cgroup_stat_cpu *stat,
97 enum mem_cgroup_stat_index idx)
98{
99 return stat->count[idx];
100}
101
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800102/*
103 * For accounting under irq disable, no need for increment preempt count.
104 */
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700105static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800106 enum mem_cgroup_stat_index idx, int val)
107{
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700108 stat->count[idx] += val;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800109}
110
111static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
112 enum mem_cgroup_stat_index idx)
113{
114 int cpu;
115 s64 ret = 0;
116 for_each_possible_cpu(cpu)
117 ret += stat->cpustat[cpu].count[idx];
118 return ret;
119}
120
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700121static s64 mem_cgroup_local_usage(struct mem_cgroup_stat *stat)
122{
123 s64 ret;
124
125 ret = mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_CACHE);
126 ret += mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_RSS);
127 return ret;
128}
129
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800130/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800131 * per-zone information in memory controller.
132 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800133struct mem_cgroup_per_zone {
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800134 /*
135 * spin_lock to protect the per cgroup LRU
136 */
Christoph Lameterb69408e2008-10-18 20:26:14 -0700137 struct list_head lists[NR_LRU_LISTS];
138 unsigned long count[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800139
140 struct zone_reclaim_stat reclaim_stat;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700141 struct rb_node tree_node; /* RB tree node */
142 unsigned long long usage_in_excess;/* Set to the value by which */
143 /* the soft limit is exceeded*/
144 bool on_tree;
Balbir Singh4e416952009-09-23 15:56:39 -0700145 struct mem_cgroup *mem; /* Back pointer, we cannot */
146 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800147};
148/* Macro for accessing counter */
149#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
150
151struct mem_cgroup_per_node {
152 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
153};
154
155struct mem_cgroup_lru_info {
156 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
157};
158
159/*
Balbir Singhf64c3f52009-09-23 15:56:37 -0700160 * Cgroups above their limits are maintained in a RB-Tree, independent of
161 * their hierarchy representation
162 */
163
164struct mem_cgroup_tree_per_zone {
165 struct rb_root rb_root;
166 spinlock_t lock;
167};
168
169struct mem_cgroup_tree_per_node {
170 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
171};
172
173struct mem_cgroup_tree {
174 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
175};
176
177static struct mem_cgroup_tree soft_limit_tree __read_mostly;
178
179/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800180 * The memory controller data structure. The memory controller controls both
181 * page cache and RSS per cgroup. We would eventually like to provide
182 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
183 * to help the administrator determine what knobs to tune.
184 *
185 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800186 * we hit the water mark. May be even add a low water mark, such that
187 * no reclaim occurs from a cgroup at it's low water mark, this is
188 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800189 */
190struct mem_cgroup {
191 struct cgroup_subsys_state css;
192 /*
193 * the counter to account for memory usage
194 */
195 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800196 /*
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800197 * the counter to account for mem+swap usage.
198 */
199 struct res_counter memsw;
200 /*
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800201 * Per cgroup active and inactive list, similar to the
202 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800203 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800204 struct mem_cgroup_lru_info info;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800205
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800206 /*
207 protect against reclaim related member.
208 */
209 spinlock_t reclaim_param_lock;
210
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800211 int prev_priority; /* for recording reclaim priority */
Balbir Singh6d61ef42009-01-07 18:08:06 -0800212
213 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200214 * While reclaiming in a hierarchy, we cache the last child we
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700215 * reclaimed from.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800216 */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700217 int last_scanned_child;
Balbir Singh18f59ea2009-01-07 18:08:07 -0800218 /*
219 * Should the accounting and control be hierarchical, per subtree?
220 */
221 bool use_hierarchy;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800222 unsigned long last_oom_jiffies;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800223 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800224
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800225 unsigned int swappiness;
226
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700227 /* set when res.limit == memsw.limit */
228 bool memsw_is_minimum;
229
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800230 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800231 * Should we move charges of a task when a task is moved into this
232 * mem_cgroup ? And what type of charges should we move ?
233 */
234 unsigned long move_charge_at_immigrate;
235
236 /*
Jan Blunckc8dad2b2009-01-07 18:07:53 -0800237 * statistics. This must be placed at the end of memcg.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800238 */
239 struct mem_cgroup_stat stat;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800240};
241
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800242/* Stuffs for move charges at task migration. */
243/*
244 * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a
245 * left-shifted bitmap of these types.
246 */
247enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800248 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800249 NR_MOVE_TYPE,
250};
251
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800252/* "mc" and its members are protected by cgroup_mutex */
253static struct move_charge_struct {
254 struct mem_cgroup *from;
255 struct mem_cgroup *to;
256 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800257 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800258 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800259 struct task_struct *moving_task; /* a task moving charges */
260 wait_queue_head_t waitq; /* a waitq for other context */
261} mc = {
262 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
263};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800264
Balbir Singh4e416952009-09-23 15:56:39 -0700265/*
266 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
267 * limit reclaim to prevent infinite loops, if they ever occur.
268 */
269#define MEM_CGROUP_MAX_RECLAIM_LOOPS (100)
270#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2)
271
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800272enum charge_type {
273 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
274 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700275 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700276 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800277 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700278 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700279 NR_CHARGE_TYPE,
280};
281
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700282/* only for here (for easy reading.) */
283#define PCGF_CACHE (1UL << PCG_CACHE)
284#define PCGF_USED (1UL << PCG_USED)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700285#define PCGF_LOCK (1UL << PCG_LOCK)
Balbir Singh4b3bde42009-09-23 15:56:32 -0700286/* Not used, but added here for completeness */
287#define PCGF_ACCT (1UL << PCG_ACCT)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800288
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800289/* for encoding cft->private value on file */
290#define _MEM (0)
291#define _MEMSWAP (1)
292#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
293#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
294#define MEMFILE_ATTR(val) ((val) & 0xffff)
295
Balbir Singh75822b42009-09-23 15:56:38 -0700296/*
297 * Reclaim flags for mem_cgroup_hierarchical_reclaim
298 */
299#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
300#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
301#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
302#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
Balbir Singh4e416952009-09-23 15:56:39 -0700303#define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
304#define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
Balbir Singh75822b42009-09-23 15:56:38 -0700305
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800306static void mem_cgroup_get(struct mem_cgroup *mem);
307static void mem_cgroup_put(struct mem_cgroup *mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -0800308static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -0800309static void drain_all_stock_async(void);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800310
Balbir Singhf64c3f52009-09-23 15:56:37 -0700311static struct mem_cgroup_per_zone *
312mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
313{
314 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
315}
316
Wu Fengguangd3242362009-12-16 12:19:59 +0100317struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *mem)
318{
319 return &mem->css;
320}
321
Balbir Singhf64c3f52009-09-23 15:56:37 -0700322static struct mem_cgroup_per_zone *
323page_cgroup_zoneinfo(struct page_cgroup *pc)
324{
325 struct mem_cgroup *mem = pc->mem_cgroup;
326 int nid = page_cgroup_nid(pc);
327 int zid = page_cgroup_zid(pc);
328
329 if (!mem)
330 return NULL;
331
332 return mem_cgroup_zoneinfo(mem, nid, zid);
333}
334
335static struct mem_cgroup_tree_per_zone *
336soft_limit_tree_node_zone(int nid, int zid)
337{
338 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
339}
340
341static struct mem_cgroup_tree_per_zone *
342soft_limit_tree_from_page(struct page *page)
343{
344 int nid = page_to_nid(page);
345 int zid = page_zonenum(page);
346
347 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
348}
349
350static void
Balbir Singh4e416952009-09-23 15:56:39 -0700351__mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700352 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700353 struct mem_cgroup_tree_per_zone *mctz,
354 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700355{
356 struct rb_node **p = &mctz->rb_root.rb_node;
357 struct rb_node *parent = NULL;
358 struct mem_cgroup_per_zone *mz_node;
359
360 if (mz->on_tree)
361 return;
362
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700363 mz->usage_in_excess = new_usage_in_excess;
364 if (!mz->usage_in_excess)
365 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700366 while (*p) {
367 parent = *p;
368 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
369 tree_node);
370 if (mz->usage_in_excess < mz_node->usage_in_excess)
371 p = &(*p)->rb_left;
372 /*
373 * We can't avoid mem cgroups that are over their soft
374 * limit by the same amount
375 */
376 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
377 p = &(*p)->rb_right;
378 }
379 rb_link_node(&mz->tree_node, parent, p);
380 rb_insert_color(&mz->tree_node, &mctz->rb_root);
381 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700382}
383
384static void
385__mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
386 struct mem_cgroup_per_zone *mz,
387 struct mem_cgroup_tree_per_zone *mctz)
388{
389 if (!mz->on_tree)
390 return;
391 rb_erase(&mz->tree_node, &mctz->rb_root);
392 mz->on_tree = false;
393}
394
395static void
Balbir Singhf64c3f52009-09-23 15:56:37 -0700396mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
397 struct mem_cgroup_per_zone *mz,
398 struct mem_cgroup_tree_per_zone *mctz)
399{
400 spin_lock(&mctz->lock);
Balbir Singh4e416952009-09-23 15:56:39 -0700401 __mem_cgroup_remove_exceeded(mem, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700402 spin_unlock(&mctz->lock);
403}
404
405static bool mem_cgroup_soft_limit_check(struct mem_cgroup *mem)
406{
407 bool ret = false;
408 int cpu;
409 s64 val;
410 struct mem_cgroup_stat_cpu *cpustat;
411
412 cpu = get_cpu();
413 cpustat = &mem->stat.cpustat[cpu];
414 val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_EVENTS);
415 if (unlikely(val > SOFTLIMIT_EVENTS_THRESH)) {
416 __mem_cgroup_stat_reset_safe(cpustat, MEM_CGROUP_STAT_EVENTS);
417 ret = true;
418 }
419 put_cpu();
420 return ret;
421}
422
423static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
424{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700425 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700426 struct mem_cgroup_per_zone *mz;
427 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700428 int nid = page_to_nid(page);
429 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700430 mctz = soft_limit_tree_from_page(page);
431
432 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700433 * Necessary to update all ancestors when hierarchy is used.
434 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700435 */
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700436 for (; mem; mem = parent_mem_cgroup(mem)) {
437 mz = mem_cgroup_zoneinfo(mem, nid, zid);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700438 excess = res_counter_soft_limit_excess(&mem->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700439 /*
440 * We have to update the tree if mz is on RB-tree or
441 * mem is over its softlimit.
442 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700443 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700444 spin_lock(&mctz->lock);
445 /* if on-tree, remove it */
446 if (mz->on_tree)
447 __mem_cgroup_remove_exceeded(mem, mz, mctz);
448 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700449 * Insert again. mz->usage_in_excess will be updated.
450 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700451 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700452 __mem_cgroup_insert_exceeded(mem, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700453 spin_unlock(&mctz->lock);
454 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700455 }
456}
457
458static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
459{
460 int node, zone;
461 struct mem_cgroup_per_zone *mz;
462 struct mem_cgroup_tree_per_zone *mctz;
463
464 for_each_node_state(node, N_POSSIBLE) {
465 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
466 mz = mem_cgroup_zoneinfo(mem, node, zone);
467 mctz = soft_limit_tree_node_zone(node, zone);
468 mem_cgroup_remove_exceeded(mem, mz, mctz);
469 }
470 }
471}
472
Balbir Singh4e416952009-09-23 15:56:39 -0700473static inline unsigned long mem_cgroup_get_excess(struct mem_cgroup *mem)
474{
475 return res_counter_soft_limit_excess(&mem->res) >> PAGE_SHIFT;
476}
477
478static struct mem_cgroup_per_zone *
479__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
480{
481 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700482 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700483
484retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700485 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700486 rightmost = rb_last(&mctz->rb_root);
487 if (!rightmost)
488 goto done; /* Nothing to reclaim from */
489
490 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
491 /*
492 * Remove the node now but someone else can add it back,
493 * we will to add it back at the end of reclaim to its correct
494 * position in the tree.
495 */
496 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
497 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
498 !css_tryget(&mz->mem->css))
499 goto retry;
500done:
501 return mz;
502}
503
504static struct mem_cgroup_per_zone *
505mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
506{
507 struct mem_cgroup_per_zone *mz;
508
509 spin_lock(&mctz->lock);
510 mz = __mem_cgroup_largest_soft_limit_node(mctz);
511 spin_unlock(&mctz->lock);
512 return mz;
513}
514
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700515static void mem_cgroup_swap_statistics(struct mem_cgroup *mem,
516 bool charge)
517{
518 int val = (charge) ? 1 : -1;
519 struct mem_cgroup_stat *stat = &mem->stat;
520 struct mem_cgroup_stat_cpu *cpustat;
521 int cpu = get_cpu();
522
523 cpustat = &stat->cpustat[cpu];
524 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_SWAPOUT, val);
525 put_cpu();
526}
527
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700528static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
529 struct page_cgroup *pc,
530 bool charge)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800531{
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700532 int val = (charge) ? 1 : -1;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800533 struct mem_cgroup_stat *stat = &mem->stat;
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700534 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800535 int cpu = get_cpu();
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800536
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800537 cpustat = &stat->cpustat[cpu];
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700538 if (PageCgroupCache(pc))
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700539 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800540 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700541 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700542
543 if (charge)
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700544 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700545 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
546 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700547 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700548 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700549 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_EVENTS, 1);
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800550 put_cpu();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800551}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800552
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700553static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700554 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800555{
556 int nid, zid;
557 struct mem_cgroup_per_zone *mz;
558 u64 total = 0;
559
560 for_each_online_node(nid)
561 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
562 mz = mem_cgroup_zoneinfo(mem, nid, zid);
563 total += MEM_CGROUP_ZSTAT(mz, idx);
564 }
565 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800566}
567
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800568static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800569{
570 return container_of(cgroup_subsys_state(cont,
571 mem_cgroup_subsys_id), struct mem_cgroup,
572 css);
573}
574
Balbir Singhcf475ad2008-04-29 01:00:16 -0700575struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800576{
Balbir Singh31a78f22008-09-28 23:09:31 +0100577 /*
578 * mm_update_next_owner() may clear mm->owner to NULL
579 * if it races with swapoff, page migration, etc.
580 * So this can be called with p == NULL.
581 */
582 if (unlikely(!p))
583 return NULL;
584
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800585 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
586 struct mem_cgroup, css);
587}
588
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800589static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
590{
591 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700592
593 if (!mm)
594 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800595 /*
596 * Because we have no locks, mm->owner's may be being moved to other
597 * cgroup. We use css_tryget() here even if this looks
598 * pessimistic (rather than adding locks here).
599 */
600 rcu_read_lock();
601 do {
602 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
603 if (unlikely(!mem))
604 break;
605 } while (!css_tryget(&mem->css));
606 rcu_read_unlock();
607 return mem;
608}
609
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700610/*
611 * Call callback function against all cgroup under hierarchy tree.
612 */
613static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
614 int (*func)(struct mem_cgroup *, void *))
615{
616 int found, ret, nextid;
617 struct cgroup_subsys_state *css;
618 struct mem_cgroup *mem;
619
620 if (!root->use_hierarchy)
621 return (*func)(root, data);
622
623 nextid = 1;
624 do {
625 ret = 0;
626 mem = NULL;
627
628 rcu_read_lock();
629 css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
630 &found);
631 if (css && css_tryget(css))
632 mem = container_of(css, struct mem_cgroup, css);
633 rcu_read_unlock();
634
635 if (mem) {
636 ret = (*func)(mem, data);
637 css_put(&mem->css);
638 }
639 nextid = found + 1;
640 } while (!ret && css);
641
642 return ret;
643}
644
Balbir Singh4b3bde42009-09-23 15:56:32 -0700645static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
646{
647 return (mem == root_mem_cgroup);
648}
649
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800650/*
651 * Following LRU functions are allowed to be used without PCG_LOCK.
652 * Operations are called by routine of global LRU independently from memcg.
653 * What we have to take care of here is validness of pc->mem_cgroup.
654 *
655 * Changes to pc->mem_cgroup happens when
656 * 1. charge
657 * 2. moving account
658 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
659 * It is added to LRU before charge.
660 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
661 * When moving account, the page is not on LRU. It's isolated.
662 */
663
664void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800665{
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800666 struct page_cgroup *pc;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800667 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700668
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800669 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800670 return;
671 pc = lookup_page_cgroup(page);
672 /* can happen while we handle swapcache. */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700673 if (!TestClearPageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800674 return;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700675 VM_BUG_ON(!pc->mem_cgroup);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800676 /*
677 * We don't check PCG_USED bit. It's cleared when the "page" is finally
678 * removed from global LRU.
679 */
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800680 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700681 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700682 if (mem_cgroup_is_root(pc->mem_cgroup))
683 return;
684 VM_BUG_ON(list_empty(&pc->lru));
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800685 list_del_init(&pc->lru);
686 return;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800687}
688
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800689void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800690{
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800691 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800692}
693
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800694void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800695{
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800696 struct mem_cgroup_per_zone *mz;
697 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800698
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800699 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700700 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700701
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800702 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800703 /*
704 * Used bit is set without atomic ops but after smp_wmb().
705 * For making pc->mem_cgroup visible, insert smp_rmb() here.
706 */
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800707 smp_rmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -0700708 /* unused or root page is not rotated. */
709 if (!PageCgroupUsed(pc) || mem_cgroup_is_root(pc->mem_cgroup))
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800710 return;
711 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700712 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800713}
714
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800715void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
716{
717 struct page_cgroup *pc;
718 struct mem_cgroup_per_zone *mz;
719
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800720 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800721 return;
722 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700723 VM_BUG_ON(PageCgroupAcctLRU(pc));
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800724 /*
725 * Used bit is set without atomic ops but after smp_wmb().
726 * For making pc->mem_cgroup visible, insert smp_rmb() here.
727 */
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800728 smp_rmb();
729 if (!PageCgroupUsed(pc))
730 return;
731
732 mz = page_cgroup_zoneinfo(pc);
733 MEM_CGROUP_ZSTAT(mz, lru) += 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700734 SetPageCgroupAcctLRU(pc);
735 if (mem_cgroup_is_root(pc->mem_cgroup))
736 return;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800737 list_add(&pc->lru, &mz->lists[lru]);
738}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800739
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800740/*
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800741 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
742 * lru because the page may.be reused after it's fully uncharged (because of
743 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
744 * it again. This function is only used to charge SwapCache. It's done under
745 * lock_page and expected that zone->lru_lock is never held.
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800746 */
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800747static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800748{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800749 unsigned long flags;
750 struct zone *zone = page_zone(page);
751 struct page_cgroup *pc = lookup_page_cgroup(page);
752
753 spin_lock_irqsave(&zone->lru_lock, flags);
754 /*
755 * Forget old LRU when this page_cgroup is *not* used. This Used bit
756 * is guarded by lock_page() because the page is SwapCache.
757 */
758 if (!PageCgroupUsed(pc))
759 mem_cgroup_del_lru_list(page, page_lru(page));
760 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800761}
762
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800763static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
764{
765 unsigned long flags;
766 struct zone *zone = page_zone(page);
767 struct page_cgroup *pc = lookup_page_cgroup(page);
768
769 spin_lock_irqsave(&zone->lru_lock, flags);
770 /* link when the page is linked to LRU but page_cgroup isn't */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700771 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800772 mem_cgroup_add_lru_list(page, page_lru(page));
773 spin_unlock_irqrestore(&zone->lru_lock, flags);
774}
775
776
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800777void mem_cgroup_move_lists(struct page *page,
778 enum lru_list from, enum lru_list to)
779{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800780 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800781 return;
782 mem_cgroup_del_lru_list(page, from);
783 mem_cgroup_add_lru_list(page, to);
784}
785
David Rientjes4c4a2212008-02-07 00:14:06 -0800786int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
787{
788 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700789 struct mem_cgroup *curr = NULL;
David Rientjes4c4a2212008-02-07 00:14:06 -0800790
791 task_lock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700792 rcu_read_lock();
793 curr = try_get_mem_cgroup_from_mm(task->mm);
794 rcu_read_unlock();
David Rientjes4c4a2212008-02-07 00:14:06 -0800795 task_unlock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700796 if (!curr)
797 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -0800798 /*
799 * We should check use_hierarchy of "mem" not "curr". Because checking
800 * use_hierarchy of "curr" here make this function true if hierarchy is
801 * enabled in "curr" and "curr" is a child of "mem" in *cgroup*
802 * hierarchy(even if use_hierarchy is disabled in "mem").
803 */
804 if (mem->use_hierarchy)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700805 ret = css_is_ancestor(&curr->css, &mem->css);
806 else
807 ret = (curr == mem);
808 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -0800809 return ret;
810}
811
Balbir Singh66e17072008-02-07 00:13:56 -0800812/*
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800813 * prev_priority control...this will be used in memory reclaim path.
814 */
815int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
816{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800817 int prev_priority;
818
819 spin_lock(&mem->reclaim_param_lock);
820 prev_priority = mem->prev_priority;
821 spin_unlock(&mem->reclaim_param_lock);
822
823 return prev_priority;
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800824}
825
826void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
827{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800828 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800829 if (priority < mem->prev_priority)
830 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800831 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800832}
833
834void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
835{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800836 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800837 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800838 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800839}
840
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800841static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800842{
843 unsigned long active;
844 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800845 unsigned long gb;
846 unsigned long inactive_ratio;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800847
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700848 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
849 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800850
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800851 gb = (inactive + active) >> (30 - PAGE_SHIFT);
852 if (gb)
853 inactive_ratio = int_sqrt(10 * gb);
854 else
855 inactive_ratio = 1;
856
857 if (present_pages) {
858 present_pages[0] = inactive;
859 present_pages[1] = active;
860 }
861
862 return inactive_ratio;
863}
864
865int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
866{
867 unsigned long active;
868 unsigned long inactive;
869 unsigned long present_pages[2];
870 unsigned long inactive_ratio;
871
872 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
873
874 inactive = present_pages[0];
875 active = present_pages[1];
876
877 if (inactive * inactive_ratio < active)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800878 return 1;
879
880 return 0;
881}
882
Rik van Riel56e49d22009-06-16 15:32:28 -0700883int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
884{
885 unsigned long active;
886 unsigned long inactive;
887
888 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
889 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
890
891 return (active > inactive);
892}
893
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800894unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
895 struct zone *zone,
896 enum lru_list lru)
897{
898 int nid = zone->zone_pgdat->node_id;
899 int zid = zone_idx(zone);
900 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
901
902 return MEM_CGROUP_ZSTAT(mz, lru);
903}
904
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800905struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
906 struct zone *zone)
907{
908 int nid = zone->zone_pgdat->node_id;
909 int zid = zone_idx(zone);
910 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
911
912 return &mz->reclaim_stat;
913}
914
915struct zone_reclaim_stat *
916mem_cgroup_get_reclaim_stat_from_page(struct page *page)
917{
918 struct page_cgroup *pc;
919 struct mem_cgroup_per_zone *mz;
920
921 if (mem_cgroup_disabled())
922 return NULL;
923
924 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800925 /*
926 * Used bit is set without atomic ops but after smp_wmb().
927 * For making pc->mem_cgroup visible, insert smp_rmb() here.
928 */
929 smp_rmb();
930 if (!PageCgroupUsed(pc))
931 return NULL;
932
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800933 mz = page_cgroup_zoneinfo(pc);
934 if (!mz)
935 return NULL;
936
937 return &mz->reclaim_stat;
938}
939
Balbir Singh66e17072008-02-07 00:13:56 -0800940unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
941 struct list_head *dst,
942 unsigned long *scanned, int order,
943 int mode, struct zone *z,
944 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700945 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -0800946{
947 unsigned long nr_taken = 0;
948 struct page *page;
949 unsigned long scan;
950 LIST_HEAD(pc_list);
951 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800952 struct page_cgroup *pc, *tmp;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800953 int nid = z->zone_pgdat->node_id;
954 int zid = zone_idx(z);
955 struct mem_cgroup_per_zone *mz;
Johannes Weinerb7c46d12009-09-21 17:02:56 -0700956 int lru = LRU_FILE * file + active;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700957 int ret;
Balbir Singh66e17072008-02-07 00:13:56 -0800958
Balbir Singhcf475ad2008-04-29 01:00:16 -0700959 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800960 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700961 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -0800962
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800963 scan = 0;
964 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -0800965 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800966 break;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800967
968 page = pc->page;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700969 if (unlikely(!PageCgroupUsed(pc)))
970 continue;
Hugh Dickins436c65412008-02-07 00:14:12 -0800971 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800972 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800973
Hugh Dickins436c65412008-02-07 00:14:12 -0800974 scan++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700975 ret = __isolate_lru_page(page, mode, file);
976 switch (ret) {
977 case 0:
Balbir Singh66e17072008-02-07 00:13:56 -0800978 list_move(&page->lru, dst);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700979 mem_cgroup_del_lru(page);
Balbir Singh66e17072008-02-07 00:13:56 -0800980 nr_taken++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700981 break;
982 case -EBUSY:
983 /* we don't affect global LRU but rotate in our LRU */
984 mem_cgroup_rotate_lru_list(page, page_lru(page));
985 break;
986 default:
987 break;
Balbir Singh66e17072008-02-07 00:13:56 -0800988 }
989 }
990
Balbir Singh66e17072008-02-07 00:13:56 -0800991 *scanned = scan;
992 return nr_taken;
993}
994
Balbir Singh6d61ef42009-01-07 18:08:06 -0800995#define mem_cgroup_from_res_counter(counter, member) \
996 container_of(counter, struct mem_cgroup, member)
997
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -0800998static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
999{
1000 if (do_swap_account) {
1001 if (res_counter_check_under_limit(&mem->res) &&
1002 res_counter_check_under_limit(&mem->memsw))
1003 return true;
1004 } else
1005 if (res_counter_check_under_limit(&mem->res))
1006 return true;
1007 return false;
1008}
1009
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001010static unsigned int get_swappiness(struct mem_cgroup *memcg)
1011{
1012 struct cgroup *cgrp = memcg->css.cgroup;
1013 unsigned int swappiness;
1014
1015 /* root ? */
1016 if (cgrp->parent == NULL)
1017 return vm_swappiness;
1018
1019 spin_lock(&memcg->reclaim_param_lock);
1020 swappiness = memcg->swappiness;
1021 spin_unlock(&memcg->reclaim_param_lock);
1022
1023 return swappiness;
1024}
1025
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001026static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
1027{
1028 int *val = data;
1029 (*val)++;
1030 return 0;
1031}
Balbir Singhe2224322009-04-02 16:57:39 -07001032
1033/**
1034 * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode.
1035 * @memcg: The memory cgroup that went over limit
1036 * @p: Task that is going to be killed
1037 *
1038 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1039 * enabled
1040 */
1041void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1042{
1043 struct cgroup *task_cgrp;
1044 struct cgroup *mem_cgrp;
1045 /*
1046 * Need a buffer in BSS, can't rely on allocations. The code relies
1047 * on the assumption that OOM is serialized for memory controller.
1048 * If this assumption is broken, revisit this code.
1049 */
1050 static char memcg_name[PATH_MAX];
1051 int ret;
1052
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001053 if (!memcg || !p)
Balbir Singhe2224322009-04-02 16:57:39 -07001054 return;
1055
1056
1057 rcu_read_lock();
1058
1059 mem_cgrp = memcg->css.cgroup;
1060 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1061
1062 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1063 if (ret < 0) {
1064 /*
1065 * Unfortunately, we are unable to convert to a useful name
1066 * But we'll still print out the usage information
1067 */
1068 rcu_read_unlock();
1069 goto done;
1070 }
1071 rcu_read_unlock();
1072
1073 printk(KERN_INFO "Task in %s killed", memcg_name);
1074
1075 rcu_read_lock();
1076 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1077 if (ret < 0) {
1078 rcu_read_unlock();
1079 goto done;
1080 }
1081 rcu_read_unlock();
1082
1083 /*
1084 * Continues from above, so we don't need an KERN_ level
1085 */
1086 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1087done:
1088
1089 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1090 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1091 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1092 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1093 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1094 "failcnt %llu\n",
1095 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1096 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1097 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1098}
1099
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001100/*
1101 * This function returns the number of memcg under hierarchy tree. Returns
1102 * 1(self count) if no children.
1103 */
1104static int mem_cgroup_count_children(struct mem_cgroup *mem)
1105{
1106 int num = 0;
1107 mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
1108 return num;
1109}
1110
Balbir Singh6d61ef42009-01-07 18:08:06 -08001111/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001112 * Visit the first child (need not be the first child as per the ordering
1113 * of the cgroup list, since we track last_scanned_child) of @mem and use
1114 * that to reclaim free pages from.
1115 */
1116static struct mem_cgroup *
1117mem_cgroup_select_victim(struct mem_cgroup *root_mem)
1118{
1119 struct mem_cgroup *ret = NULL;
1120 struct cgroup_subsys_state *css;
1121 int nextid, found;
1122
1123 if (!root_mem->use_hierarchy) {
1124 css_get(&root_mem->css);
1125 ret = root_mem;
1126 }
1127
1128 while (!ret) {
1129 rcu_read_lock();
1130 nextid = root_mem->last_scanned_child + 1;
1131 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
1132 &found);
1133 if (css && css_tryget(css))
1134 ret = container_of(css, struct mem_cgroup, css);
1135
1136 rcu_read_unlock();
1137 /* Updates scanning parameter */
1138 spin_lock(&root_mem->reclaim_param_lock);
1139 if (!css) {
1140 /* this means start scan from ID:1 */
1141 root_mem->last_scanned_child = 0;
1142 } else
1143 root_mem->last_scanned_child = found;
1144 spin_unlock(&root_mem->reclaim_param_lock);
1145 }
1146
1147 return ret;
1148}
1149
1150/*
1151 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1152 * we reclaimed from, so that we don't end up penalizing one child extensively
1153 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001154 *
1155 * root_mem is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001156 *
1157 * We give up and return to the caller when we visit root_mem twice.
1158 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001159 *
1160 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001161 */
1162static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
Balbir Singh4e416952009-09-23 15:56:39 -07001163 struct zone *zone,
Balbir Singh75822b42009-09-23 15:56:38 -07001164 gfp_t gfp_mask,
1165 unsigned long reclaim_options)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001166{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001167 struct mem_cgroup *victim;
1168 int ret, total = 0;
1169 int loop = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001170 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1171 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
Balbir Singh4e416952009-09-23 15:56:39 -07001172 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
1173 unsigned long excess = mem_cgroup_get_excess(root_mem);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001174
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001175 /* If memsw_is_minimum==1, swap-out is of-no-use. */
1176 if (root_mem->memsw_is_minimum)
1177 noswap = true;
1178
Balbir Singh4e416952009-09-23 15:56:39 -07001179 while (1) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001180 victim = mem_cgroup_select_victim(root_mem);
Balbir Singh4e416952009-09-23 15:56:39 -07001181 if (victim == root_mem) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001182 loop++;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001183 if (loop >= 1)
1184 drain_all_stock_async();
Balbir Singh4e416952009-09-23 15:56:39 -07001185 if (loop >= 2) {
1186 /*
1187 * If we have not been able to reclaim
1188 * anything, it might because there are
1189 * no reclaimable pages under this hierarchy
1190 */
1191 if (!check_soft || !total) {
1192 css_put(&victim->css);
1193 break;
1194 }
1195 /*
1196 * We want to do more targetted reclaim.
1197 * excess >> 2 is not to excessive so as to
1198 * reclaim too much, nor too less that we keep
1199 * coming back to reclaim from this cgroup
1200 */
1201 if (total >= (excess >> 2) ||
1202 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1203 css_put(&victim->css);
1204 break;
1205 }
1206 }
1207 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001208 if (!mem_cgroup_local_usage(&victim->stat)) {
1209 /* this cgroup's local usage == 0 */
1210 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001211 continue;
1212 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001213 /* we use swappiness of local cgroup */
Balbir Singh4e416952009-09-23 15:56:39 -07001214 if (check_soft)
1215 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
1216 noswap, get_swappiness(victim), zone,
1217 zone->zone_pgdat->node_id);
1218 else
1219 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
1220 noswap, get_swappiness(victim));
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001221 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001222 /*
1223 * At shrinking usage, we can't check we should stop here or
1224 * reclaim more. It's depends on callers. last_scanned_child
1225 * will work enough for keeping fairness under tree.
1226 */
1227 if (shrink)
1228 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001229 total += ret;
Balbir Singh4e416952009-09-23 15:56:39 -07001230 if (check_soft) {
1231 if (res_counter_check_under_soft_limit(&root_mem->res))
1232 return total;
1233 } else if (mem_cgroup_check_under_limit(root_mem))
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001234 return 1 + total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001235 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001236 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001237}
1238
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001239bool mem_cgroup_oom_called(struct task_struct *task)
1240{
1241 bool ret = false;
1242 struct mem_cgroup *mem;
1243 struct mm_struct *mm;
1244
1245 rcu_read_lock();
1246 mm = task->mm;
1247 if (!mm)
1248 mm = &init_mm;
1249 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
1250 if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
1251 ret = true;
1252 rcu_read_unlock();
1253 return ret;
1254}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001255
1256static int record_last_oom_cb(struct mem_cgroup *mem, void *data)
1257{
1258 mem->last_oom_jiffies = jiffies;
1259 return 0;
1260}
1261
1262static void record_last_oom(struct mem_cgroup *mem)
1263{
1264 mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb);
1265}
1266
Balbir Singhd69b0422009-06-17 16:26:34 -07001267/*
1268 * Currently used to update mapped file statistics, but the routine can be
1269 * generalized to update other statistics as well.
1270 */
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001271void mem_cgroup_update_file_mapped(struct page *page, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07001272{
1273 struct mem_cgroup *mem;
1274 struct mem_cgroup_stat *stat;
1275 struct mem_cgroup_stat_cpu *cpustat;
1276 int cpu;
1277 struct page_cgroup *pc;
1278
Balbir Singhd69b0422009-06-17 16:26:34 -07001279 pc = lookup_page_cgroup(page);
1280 if (unlikely(!pc))
1281 return;
1282
1283 lock_page_cgroup(pc);
1284 mem = pc->mem_cgroup;
1285 if (!mem)
1286 goto done;
1287
1288 if (!PageCgroupUsed(pc))
1289 goto done;
1290
1291 /*
1292 * Preemption is already disabled, we don't need get_cpu()
1293 */
1294 cpu = smp_processor_id();
1295 stat = &mem->stat;
1296 cpustat = &stat->cpustat[cpu];
1297
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001298 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED, val);
Balbir Singhd69b0422009-06-17 16:26:34 -07001299done:
1300 unlock_page_cgroup(pc);
1301}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001302
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001303/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001304 * size of first charge trial. "32" comes from vmscan.c's magic value.
1305 * TODO: maybe necessary to use big numbers in big irons.
1306 */
1307#define CHARGE_SIZE (32 * PAGE_SIZE)
1308struct memcg_stock_pcp {
1309 struct mem_cgroup *cached; /* this never be root cgroup */
1310 int charge;
1311 struct work_struct work;
1312};
1313static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
1314static atomic_t memcg_drain_count;
1315
1316/*
1317 * Try to consume stocked charge on this cpu. If success, PAGE_SIZE is consumed
1318 * from local stock and true is returned. If the stock is 0 or charges from a
1319 * cgroup which is not current target, returns false. This stock will be
1320 * refilled.
1321 */
1322static bool consume_stock(struct mem_cgroup *mem)
1323{
1324 struct memcg_stock_pcp *stock;
1325 bool ret = true;
1326
1327 stock = &get_cpu_var(memcg_stock);
1328 if (mem == stock->cached && stock->charge)
1329 stock->charge -= PAGE_SIZE;
1330 else /* need to call res_counter_charge */
1331 ret = false;
1332 put_cpu_var(memcg_stock);
1333 return ret;
1334}
1335
1336/*
1337 * Returns stocks cached in percpu to res_counter and reset cached information.
1338 */
1339static void drain_stock(struct memcg_stock_pcp *stock)
1340{
1341 struct mem_cgroup *old = stock->cached;
1342
1343 if (stock->charge) {
1344 res_counter_uncharge(&old->res, stock->charge);
1345 if (do_swap_account)
1346 res_counter_uncharge(&old->memsw, stock->charge);
1347 }
1348 stock->cached = NULL;
1349 stock->charge = 0;
1350}
1351
1352/*
1353 * This must be called under preempt disabled or must be called by
1354 * a thread which is pinned to local cpu.
1355 */
1356static void drain_local_stock(struct work_struct *dummy)
1357{
1358 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
1359 drain_stock(stock);
1360}
1361
1362/*
1363 * Cache charges(val) which is from res_counter, to local per_cpu area.
1364 * This will be consumed by consumt_stock() function, later.
1365 */
1366static void refill_stock(struct mem_cgroup *mem, int val)
1367{
1368 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
1369
1370 if (stock->cached != mem) { /* reset if necessary */
1371 drain_stock(stock);
1372 stock->cached = mem;
1373 }
1374 stock->charge += val;
1375 put_cpu_var(memcg_stock);
1376}
1377
1378/*
1379 * Tries to drain stocked charges in other cpus. This function is asynchronous
1380 * and just put a work per cpu for draining localy on each cpu. Caller can
1381 * expects some charges will be back to res_counter later but cannot wait for
1382 * it.
1383 */
1384static void drain_all_stock_async(void)
1385{
1386 int cpu;
1387 /* This function is for scheduling "drain" in asynchronous way.
1388 * The result of "drain" is not directly handled by callers. Then,
1389 * if someone is calling drain, we don't have to call drain more.
1390 * Anyway, WORK_STRUCT_PENDING check in queue_work_on() will catch if
1391 * there is a race. We just do loose check here.
1392 */
1393 if (atomic_read(&memcg_drain_count))
1394 return;
1395 /* Notify other cpus that system-wide "drain" is running */
1396 atomic_inc(&memcg_drain_count);
1397 get_online_cpus();
1398 for_each_online_cpu(cpu) {
1399 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
1400 schedule_work_on(cpu, &stock->work);
1401 }
1402 put_online_cpus();
1403 atomic_dec(&memcg_drain_count);
1404 /* We don't wait for flush_work */
1405}
1406
1407/* This is a synchronous drain interface. */
1408static void drain_all_stock_sync(void)
1409{
1410 /* called when force_empty is called */
1411 atomic_inc(&memcg_drain_count);
1412 schedule_on_each_cpu(drain_local_stock);
1413 atomic_dec(&memcg_drain_count);
1414}
1415
1416static int __cpuinit memcg_stock_cpu_callback(struct notifier_block *nb,
1417 unsigned long action,
1418 void *hcpu)
1419{
1420 int cpu = (unsigned long)hcpu;
1421 struct memcg_stock_pcp *stock;
1422
1423 if (action != CPU_DEAD)
1424 return NOTIFY_OK;
1425 stock = &per_cpu(memcg_stock, cpu);
1426 drain_stock(stock);
1427 return NOTIFY_OK;
1428}
1429
1430/*
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001431 * Unlike exported interface, "oom" parameter is added. if oom==true,
1432 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001433 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001434static int __mem_cgroup_try_charge(struct mm_struct *mm,
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001435 gfp_t gfp_mask, struct mem_cgroup **memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -07001436 bool oom, struct page *page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001437{
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001438 struct mem_cgroup *mem, *mem_over_limit;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001439 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001440 struct res_counter *fail_res;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001441 int csize = CHARGE_SIZE;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001442
1443 if (unlikely(test_thread_flag(TIF_MEMDIE))) {
1444 /* Don't account this! */
1445 *memcg = NULL;
1446 return 0;
1447 }
1448
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001449 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08001450 * We always charge the cgroup the mm_struct belongs to.
1451 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001452 * thread group leader migrates. It's possible that mm is not
1453 * set, if so charge the init_mm (happens for pagecache usage).
1454 */
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001455 mem = *memcg;
1456 if (likely(!mem)) {
1457 mem = try_get_mem_cgroup_from_mm(mm);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001458 *memcg = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001459 } else {
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001460 css_get(&mem->css);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001461 }
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001462 if (unlikely(!mem))
1463 return 0;
1464
Nikanth Karthikesan46f7e602009-05-28 14:34:41 -07001465 VM_BUG_ON(css_is_removed(&mem->css));
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001466 if (mem_cgroup_is_root(mem))
1467 goto done;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001468
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001469 while (1) {
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001470 int ret = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001471 unsigned long flags = 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001472
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001473 if (consume_stock(mem))
1474 goto charged;
1475
1476 ret = res_counter_charge(&mem->res, csize, &fail_res);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001477 if (likely(!ret)) {
1478 if (!do_swap_account)
1479 break;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001480 ret = res_counter_charge(&mem->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001481 if (likely(!ret))
1482 break;
1483 /* mem+swap counter fails */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001484 res_counter_uncharge(&mem->res, csize);
Balbir Singh75822b42009-09-23 15:56:38 -07001485 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001486 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1487 memsw);
1488 } else
1489 /* mem counter fails */
1490 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1491 res);
1492
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001493 /* reduce request size and retry */
1494 if (csize > PAGE_SIZE) {
1495 csize = PAGE_SIZE;
1496 continue;
1497 }
Hugh Dickins3be91272008-02-07 00:14:19 -08001498 if (!(gfp_mask & __GFP_WAIT))
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001499 goto nomem;
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001500
Balbir Singh4e416952009-09-23 15:56:39 -07001501 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
1502 gfp_mask, flags);
Daisuke Nishimura4d1c6272009-01-15 13:51:14 -08001503 if (ret)
1504 continue;
Balbir Singh66e17072008-02-07 00:13:56 -08001505
1506 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001507 * try_to_free_mem_cgroup_pages() might not give us a full
1508 * picture of reclaim. Some pages are reclaimed and might be
1509 * moved to swap cache or just unmapped from the cgroup.
1510 * Check the limit again to see if the reclaim reduced the
1511 * current usage of the cgroup before giving up
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001512 *
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001513 */
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -08001514 if (mem_cgroup_check_under_limit(mem_over_limit))
1515 continue;
Hugh Dickins3be91272008-02-07 00:14:19 -08001516
Daisuke Nishimura8033b972010-03-10 15:22:16 -08001517 /* try to avoid oom while someone is moving charge */
1518 if (mc.moving_task && current != mc.moving_task) {
1519 struct mem_cgroup *from, *to;
1520 bool do_continue = false;
1521 /*
1522 * There is a small race that "from" or "to" can be
1523 * freed by rmdir, so we use css_tryget().
1524 */
1525 rcu_read_lock();
1526 from = mc.from;
1527 to = mc.to;
1528 if (from && css_tryget(&from->css)) {
1529 if (mem_over_limit->use_hierarchy)
1530 do_continue = css_is_ancestor(
1531 &from->css,
1532 &mem_over_limit->css);
1533 else
1534 do_continue = (from == mem_over_limit);
1535 css_put(&from->css);
1536 }
1537 if (!do_continue && to && css_tryget(&to->css)) {
1538 if (mem_over_limit->use_hierarchy)
1539 do_continue = css_is_ancestor(
1540 &to->css,
1541 &mem_over_limit->css);
1542 else
1543 do_continue = (to == mem_over_limit);
1544 css_put(&to->css);
1545 }
1546 rcu_read_unlock();
1547 if (do_continue) {
1548 DEFINE_WAIT(wait);
1549 prepare_to_wait(&mc.waitq, &wait,
1550 TASK_INTERRUPTIBLE);
1551 /* moving charge context might have finished. */
1552 if (mc.moving_task)
1553 schedule();
1554 finish_wait(&mc.waitq, &wait);
1555 continue;
1556 }
1557 }
1558
Hugh Dickins3be91272008-02-07 00:14:19 -08001559 if (!nr_retries--) {
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001560 if (oom) {
KAMEZAWA Hiroyuki88700752009-01-07 18:08:09 -08001561 mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001562 record_last_oom(mem_over_limit);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001563 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001564 goto nomem;
Balbir Singh66e17072008-02-07 00:13:56 -08001565 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001566 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001567 if (csize > PAGE_SIZE)
1568 refill_stock(mem, csize - PAGE_SIZE);
1569charged:
Balbir Singhf64c3f52009-09-23 15:56:37 -07001570 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001571 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
1572 * if they exceeds softlimit.
Balbir Singhf64c3f52009-09-23 15:56:37 -07001573 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08001574 if (page && mem_cgroup_soft_limit_check(mem))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001575 mem_cgroup_update_tree(mem, page);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001576done:
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001577 return 0;
1578nomem:
1579 css_put(&mem->css);
1580 return -ENOMEM;
1581}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001582
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001583/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001584 * Somemtimes we have to undo a charge we got by try_charge().
1585 * This function is for that and do uncharge, put css's refcnt.
1586 * gotten by try_charge().
1587 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001588static void __mem_cgroup_cancel_charge(struct mem_cgroup *mem,
1589 unsigned long count)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001590{
1591 if (!mem_cgroup_is_root(mem)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001592 res_counter_uncharge(&mem->res, PAGE_SIZE * count);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001593 if (do_swap_account)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001594 res_counter_uncharge(&mem->memsw, PAGE_SIZE * count);
1595 VM_BUG_ON(test_bit(CSS_ROOT, &mem->css.flags));
1596 WARN_ON_ONCE(count > INT_MAX);
1597 __css_put(&mem->css, (int)count);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001598 }
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001599 /* we don't need css_put for root */
1600}
1601
1602static void mem_cgroup_cancel_charge(struct mem_cgroup *mem)
1603{
1604 __mem_cgroup_cancel_charge(mem, 1);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001605}
1606
1607/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001608 * A helper function to get mem_cgroup from ID. must be called under
1609 * rcu_read_lock(). The caller must check css_is_removed() or some if
1610 * it's concern. (dropping refcnt from swap can be called against removed
1611 * memcg.)
1612 */
1613static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
1614{
1615 struct cgroup_subsys_state *css;
1616
1617 /* ID 0 is unused ID */
1618 if (!id)
1619 return NULL;
1620 css = css_lookup(&mem_cgroup_subsys, id);
1621 if (!css)
1622 return NULL;
1623 return container_of(css, struct mem_cgroup, css);
1624}
1625
Wu Fengguange42d9d52009-12-16 12:19:59 +01001626struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001627{
Wu Fengguange42d9d52009-12-16 12:19:59 +01001628 struct mem_cgroup *mem = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001629 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001630 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001631 swp_entry_t ent;
1632
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001633 VM_BUG_ON(!PageLocked(page));
1634
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001635 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001636 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001637 if (PageCgroupUsed(pc)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001638 mem = pc->mem_cgroup;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001639 if (mem && !css_tryget(&mem->css))
1640 mem = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01001641 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001642 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001643 id = lookup_swap_cgroup(ent);
1644 rcu_read_lock();
1645 mem = mem_cgroup_lookup(id);
1646 if (mem && !css_tryget(&mem->css))
1647 mem = NULL;
1648 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001649 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001650 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001651 return mem;
1652}
1653
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001654/*
Daisuke Nishimuraa5e924f2009-01-07 18:08:28 -08001655 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001656 * USED state. If already USED, uncharge and return.
1657 */
1658
1659static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1660 struct page_cgroup *pc,
1661 enum charge_type ctype)
1662{
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001663 /* try_charge() can return NULL to *memcg, taking care of it. */
1664 if (!mem)
1665 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001666
1667 lock_page_cgroup(pc);
1668 if (unlikely(PageCgroupUsed(pc))) {
1669 unlock_page_cgroup(pc);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001670 mem_cgroup_cancel_charge(mem);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001671 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001672 }
Balbir Singh4b3bde42009-09-23 15:56:32 -07001673
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001674 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07001675 /*
1676 * We access a page_cgroup asynchronously without lock_page_cgroup().
1677 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
1678 * is accessed after testing USED bit. To make pc->mem_cgroup visible
1679 * before USED bit, we need memory barrier here.
1680 * See mem_cgroup_add_lru_list(), etc.
1681 */
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001682 smp_wmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -07001683 switch (ctype) {
1684 case MEM_CGROUP_CHARGE_TYPE_CACHE:
1685 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
1686 SetPageCgroupCache(pc);
1687 SetPageCgroupUsed(pc);
1688 break;
1689 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1690 ClearPageCgroupCache(pc);
1691 SetPageCgroupUsed(pc);
1692 break;
1693 default:
1694 break;
1695 }
Hugh Dickins3be91272008-02-07 00:14:19 -08001696
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001697 mem_cgroup_charge_statistics(mem, pc, true);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001698
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001699 unlock_page_cgroup(pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001700}
1701
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001702/**
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001703 * __mem_cgroup_move_account - move account of the page
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001704 * @pc: page_cgroup of the page.
1705 * @from: mem_cgroup which the page is moved from.
1706 * @to: mem_cgroup which the page is moved to. @from != @to.
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001707 * @uncharge: whether we should call uncharge and css_put against @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001708 *
1709 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001710 * - page is not on LRU (isolate_page() is useful.)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001711 * - the pc is locked, used, and ->mem_cgroup points to @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001712 *
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001713 * This function doesn't do "charge" nor css_get to new cgroup. It should be
1714 * done by a caller(__mem_cgroup_try_charge would be usefull). If @uncharge is
1715 * true, this function does "uncharge" from old cgroup, but it doesn't if
1716 * @uncharge is false, so a caller should do "uncharge".
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001717 */
1718
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001719static void __mem_cgroup_move_account(struct page_cgroup *pc,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001720 struct mem_cgroup *from, struct mem_cgroup *to, bool uncharge)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001721{
Balbir Singhd69b0422009-06-17 16:26:34 -07001722 struct page *page;
1723 int cpu;
1724 struct mem_cgroup_stat *stat;
1725 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001726
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001727 VM_BUG_ON(from == to);
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001728 VM_BUG_ON(PageLRU(pc->page));
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001729 VM_BUG_ON(!PageCgroupLocked(pc));
1730 VM_BUG_ON(!PageCgroupUsed(pc));
1731 VM_BUG_ON(pc->mem_cgroup != from);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001732
Balbir Singhd69b0422009-06-17 16:26:34 -07001733 page = pc->page;
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001734 if (page_mapped(page) && !PageAnon(page)) {
Balbir Singhd69b0422009-06-17 16:26:34 -07001735 cpu = smp_processor_id();
1736 /* Update mapped_file data for mem_cgroup "from" */
1737 stat = &from->stat;
1738 cpustat = &stat->cpustat[cpu];
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001739 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED,
Balbir Singhd69b0422009-06-17 16:26:34 -07001740 -1);
1741
1742 /* Update mapped_file data for mem_cgroup "to" */
1743 stat = &to->stat;
1744 cpustat = &stat->cpustat[cpu];
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001745 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED,
Balbir Singhd69b0422009-06-17 16:26:34 -07001746 1);
1747 }
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001748 mem_cgroup_charge_statistics(from, pc, false);
1749 if (uncharge)
1750 /* This is not "cancel", but cancel_charge does all we need. */
1751 mem_cgroup_cancel_charge(from);
Balbir Singhd69b0422009-06-17 16:26:34 -07001752
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001753 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001754 pc->mem_cgroup = to;
1755 mem_cgroup_charge_statistics(to, pc, true);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001756 /*
1757 * We charges against "to" which may not have any tasks. Then, "to"
1758 * can be under rmdir(). But in current implementation, caller of
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08001759 * this function is just force_empty() and move charge, so it's
1760 * garanteed that "to" is never removed. So, we don't check rmdir
1761 * status here.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001762 */
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001763}
1764
1765/*
1766 * check whether the @pc is valid for moving account and call
1767 * __mem_cgroup_move_account()
1768 */
1769static int mem_cgroup_move_account(struct page_cgroup *pc,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001770 struct mem_cgroup *from, struct mem_cgroup *to, bool uncharge)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001771{
1772 int ret = -EINVAL;
1773 lock_page_cgroup(pc);
1774 if (PageCgroupUsed(pc) && pc->mem_cgroup == from) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001775 __mem_cgroup_move_account(pc, from, to, uncharge);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001776 ret = 0;
1777 }
1778 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001779 return ret;
1780}
1781
1782/*
1783 * move charges to its parent.
1784 */
1785
1786static int mem_cgroup_move_parent(struct page_cgroup *pc,
1787 struct mem_cgroup *child,
1788 gfp_t gfp_mask)
1789{
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001790 struct page *page = pc->page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001791 struct cgroup *cg = child->css.cgroup;
1792 struct cgroup *pcg = cg->parent;
1793 struct mem_cgroup *parent;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001794 int ret;
1795
1796 /* Is ROOT ? */
1797 if (!pcg)
1798 return -EINVAL;
1799
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001800 ret = -EBUSY;
1801 if (!get_page_unless_zero(page))
1802 goto out;
1803 if (isolate_lru_page(page))
1804 goto put;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001805
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001806 parent = mem_cgroup_from_cont(pcg);
Balbir Singhf64c3f52009-09-23 15:56:37 -07001807 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false, page);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001808 if (ret || !parent)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001809 goto put_back;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001810
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001811 ret = mem_cgroup_move_account(pc, child, parent, true);
1812 if (ret)
1813 mem_cgroup_cancel_charge(parent);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001814put_back:
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001815 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001816put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001817 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001818out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001819 return ret;
1820}
1821
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001822/*
1823 * Charge the memory controller for page usage.
1824 * Return
1825 * 0 if the charge was successful
1826 * < 0 if the cgroup is over its limit
1827 */
1828static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
1829 gfp_t gfp_mask, enum charge_type ctype,
1830 struct mem_cgroup *memcg)
1831{
1832 struct mem_cgroup *mem;
1833 struct page_cgroup *pc;
1834 int ret;
1835
1836 pc = lookup_page_cgroup(page);
1837 /* can happen at boot */
1838 if (unlikely(!pc))
1839 return 0;
1840 prefetchw(pc);
1841
1842 mem = memcg;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001843 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true, page);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001844 if (ret || !mem)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001845 return ret;
1846
1847 __mem_cgroup_commit_charge(mem, pc, ctype);
1848 return 0;
1849}
1850
1851int mem_cgroup_newpage_charge(struct page *page,
1852 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001853{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001854 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001855 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001856 if (PageCompound(page))
1857 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001858 /*
1859 * If already mapped, we don't have to account.
1860 * If page cache, page->mapping has address_space.
1861 * But page->mapping may have out-of-use anon_vma pointer,
1862 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
1863 * is NULL.
1864 */
1865 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
1866 return 0;
1867 if (unlikely(!mm))
1868 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001869 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001870 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001871}
1872
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001873static void
1874__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1875 enum charge_type ctype);
1876
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001877int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
1878 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08001879{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001880 struct mem_cgroup *mem = NULL;
1881 int ret;
1882
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001883 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001884 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001885 if (PageCompound(page))
1886 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001887 /*
1888 * Corner case handling. This is called from add_to_page_cache()
1889 * in usual. But some FS (shmem) precharges this page before calling it
1890 * and call add_to_page_cache() with GFP_NOWAIT.
1891 *
1892 * For GFP_NOWAIT case, the page may be pre-charged before calling
1893 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
1894 * charge twice. (It works but has to pay a bit larger cost.)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001895 * And when the page is SwapCache, it should take swap information
1896 * into account. This is under lock_page() now.
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001897 */
1898 if (!(gfp_mask & __GFP_WAIT)) {
1899 struct page_cgroup *pc;
1900
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001901
1902 pc = lookup_page_cgroup(page);
1903 if (!pc)
1904 return 0;
1905 lock_page_cgroup(pc);
1906 if (PageCgroupUsed(pc)) {
1907 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001908 return 0;
1909 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001910 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001911 }
1912
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001913 if (unlikely(!mm && !mem))
Balbir Singh8697d332008-02-07 00:13:59 -08001914 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001915
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -07001916 if (page_is_file_cache(page))
1917 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001918 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001919
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001920 /* shmem */
1921 if (PageSwapCache(page)) {
1922 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1923 if (!ret)
1924 __mem_cgroup_commit_charge_swapin(page, mem,
1925 MEM_CGROUP_CHARGE_TYPE_SHMEM);
1926 } else
1927 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
1928 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001929
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001930 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001931}
1932
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001933/*
1934 * While swap-in, try_charge -> commit or cancel, the page is locked.
1935 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02001936 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001937 * "commit()" or removed by "cancel()"
1938 */
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001939int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
1940 struct page *page,
1941 gfp_t mask, struct mem_cgroup **ptr)
1942{
1943 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001944 int ret;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001945
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001946 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001947 return 0;
1948
1949 if (!do_swap_account)
1950 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001951 /*
1952 * A racing thread's fault, or swapoff, may have already updated
Hugh Dickins407f9c82009-12-14 17:59:30 -08001953 * the pte, and even removed page from swap cache: in those cases
1954 * do_swap_page()'s pte_same() test will fail; but there's also a
1955 * KSM case which does need to charge the page.
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001956 */
1957 if (!PageSwapCache(page))
Hugh Dickins407f9c82009-12-14 17:59:30 -08001958 goto charge_cur_mm;
Wu Fengguange42d9d52009-12-16 12:19:59 +01001959 mem = try_get_mem_cgroup_from_page(page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001960 if (!mem)
1961 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001962 *ptr = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001963 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true, page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001964 /* drop extra refcnt from tryget */
1965 css_put(&mem->css);
1966 return ret;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001967charge_cur_mm:
1968 if (unlikely(!mm))
1969 mm = &init_mm;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001970 return __mem_cgroup_try_charge(mm, mask, ptr, true, page);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001971}
1972
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001973static void
1974__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1975 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001976{
1977 struct page_cgroup *pc;
1978
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001979 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001980 return;
1981 if (!ptr)
1982 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001983 cgroup_exclude_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001984 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001985 mem_cgroup_lru_del_before_commit_swapcache(page);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001986 __mem_cgroup_commit_charge(ptr, pc, ctype);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001987 mem_cgroup_lru_add_after_commit_swapcache(page);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001988 /*
1989 * Now swap is on-memory. This means this page may be
1990 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08001991 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
1992 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
1993 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001994 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08001995 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001996 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001997 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08001998 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001999
2000 id = swap_cgroup_record(ent, 0);
2001 rcu_read_lock();
2002 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002003 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002004 /*
2005 * This recorded memcg can be obsolete one. So, avoid
2006 * calling css_tryget
2007 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002008 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002009 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002010 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002011 mem_cgroup_put(memcg);
2012 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002013 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002014 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002015 /*
2016 * At swapin, we may charge account against cgroup which has no tasks.
2017 * So, rmdir()->pre_destroy() can be called while we do this charge.
2018 * In that case, we need to call pre_destroy() again. check it here.
2019 */
2020 cgroup_release_and_wakeup_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002021}
2022
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002023void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
2024{
2025 __mem_cgroup_commit_charge_swapin(page, ptr,
2026 MEM_CGROUP_CHARGE_TYPE_MAPPED);
2027}
2028
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002029void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
2030{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002031 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002032 return;
2033 if (!mem)
2034 return;
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002035 mem_cgroup_cancel_charge(mem);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002036}
2037
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002038static void
2039__do_uncharge(struct mem_cgroup *mem, const enum charge_type ctype)
2040{
2041 struct memcg_batch_info *batch = NULL;
2042 bool uncharge_memsw = true;
2043 /* If swapout, usage of swap doesn't decrease */
2044 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2045 uncharge_memsw = false;
2046 /*
2047 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
2048 * In those cases, all pages freed continously can be expected to be in
2049 * the same cgroup and we have chance to coalesce uncharges.
2050 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
2051 * because we want to do uncharge as soon as possible.
2052 */
2053 if (!current->memcg_batch.do_batch || test_thread_flag(TIF_MEMDIE))
2054 goto direct_uncharge;
2055
2056 batch = &current->memcg_batch;
2057 /*
2058 * In usual, we do css_get() when we remember memcg pointer.
2059 * But in this case, we keep res->usage until end of a series of
2060 * uncharges. Then, it's ok to ignore memcg's refcnt.
2061 */
2062 if (!batch->memcg)
2063 batch->memcg = mem;
2064 /*
2065 * In typical case, batch->memcg == mem. This means we can
2066 * merge a series of uncharges to an uncharge of res_counter.
2067 * If not, we uncharge res_counter ony by one.
2068 */
2069 if (batch->memcg != mem)
2070 goto direct_uncharge;
2071 /* remember freed charge and uncharge it later */
2072 batch->bytes += PAGE_SIZE;
2073 if (uncharge_memsw)
2074 batch->memsw_bytes += PAGE_SIZE;
2075 return;
2076direct_uncharge:
2077 res_counter_uncharge(&mem->res, PAGE_SIZE);
2078 if (uncharge_memsw)
2079 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
2080 return;
2081}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002082
Balbir Singh8697d332008-02-07 00:13:59 -08002083/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002084 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002085 */
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002086static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002087__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002088{
Hugh Dickins82895462008-03-04 14:29:08 -08002089 struct page_cgroup *pc;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002090 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002091 struct mem_cgroup_per_zone *mz;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002092
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002093 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002094 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07002095
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002096 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002097 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002098
Balbir Singh8697d332008-02-07 00:13:59 -08002099 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08002100 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08002101 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002102 pc = lookup_page_cgroup(page);
2103 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002104 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002105
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002106 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002107
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002108 mem = pc->mem_cgroup;
2109
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002110 if (!PageCgroupUsed(pc))
2111 goto unlock_out;
2112
2113 switch (ctype) {
2114 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002115 case MEM_CGROUP_CHARGE_TYPE_DROP:
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002116 if (page_mapped(page))
2117 goto unlock_out;
2118 break;
2119 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
2120 if (!PageAnon(page)) { /* Shared memory */
2121 if (page->mapping && !page_is_file_cache(page))
2122 goto unlock_out;
2123 } else if (page_mapped(page)) /* Anon */
2124 goto unlock_out;
2125 break;
2126 default:
2127 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002128 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002129
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002130 if (!mem_cgroup_is_root(mem))
2131 __do_uncharge(mem, ctype);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002132 if (ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2133 mem_cgroup_swap_statistics(mem, true);
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002134 mem_cgroup_charge_statistics(mem, pc, false);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002135
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002136 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08002137 /*
2138 * pc->mem_cgroup is not cleared here. It will be accessed when it's
2139 * freed from LRU. This is safe because uncharged page is expected not
2140 * to be reused (freed soon). Exception is SwapCache, it's handled by
2141 * special functions.
2142 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08002143
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002144 mz = page_cgroup_zoneinfo(pc);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002145 unlock_page_cgroup(pc);
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08002146
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002147 if (mem_cgroup_soft_limit_check(mem))
Balbir Singhf64c3f52009-09-23 15:56:37 -07002148 mem_cgroup_update_tree(mem, page);
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08002149 /* at swapout, this memcg will be accessed to record to swap */
2150 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2151 css_put(&mem->css);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002152
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002153 return mem;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002154
2155unlock_out:
2156 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002157 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08002158}
2159
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002160void mem_cgroup_uncharge_page(struct page *page)
2161{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002162 /* early check. */
2163 if (page_mapped(page))
2164 return;
2165 if (page->mapping && !PageAnon(page))
2166 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002167 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
2168}
2169
2170void mem_cgroup_uncharge_cache_page(struct page *page)
2171{
2172 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07002173 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002174 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
2175}
2176
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002177/*
2178 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
2179 * In that cases, pages are freed continuously and we can expect pages
2180 * are in the same memcg. All these calls itself limits the number of
2181 * pages freed at once, then uncharge_start/end() is called properly.
2182 * This may be called prural(2) times in a context,
2183 */
2184
2185void mem_cgroup_uncharge_start(void)
2186{
2187 current->memcg_batch.do_batch++;
2188 /* We can do nest. */
2189 if (current->memcg_batch.do_batch == 1) {
2190 current->memcg_batch.memcg = NULL;
2191 current->memcg_batch.bytes = 0;
2192 current->memcg_batch.memsw_bytes = 0;
2193 }
2194}
2195
2196void mem_cgroup_uncharge_end(void)
2197{
2198 struct memcg_batch_info *batch = &current->memcg_batch;
2199
2200 if (!batch->do_batch)
2201 return;
2202
2203 batch->do_batch--;
2204 if (batch->do_batch) /* If stacked, do nothing. */
2205 return;
2206
2207 if (!batch->memcg)
2208 return;
2209 /*
2210 * This "batch->memcg" is valid without any css_get/put etc...
2211 * bacause we hide charges behind us.
2212 */
2213 if (batch->bytes)
2214 res_counter_uncharge(&batch->memcg->res, batch->bytes);
2215 if (batch->memsw_bytes)
2216 res_counter_uncharge(&batch->memcg->memsw, batch->memsw_bytes);
2217 /* forget this pointer (for sanity check) */
2218 batch->memcg = NULL;
2219}
2220
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002221#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002222/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002223 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002224 * memcg information is recorded to swap_cgroup of "ent"
2225 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002226void
2227mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002228{
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002229 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002230 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002231
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002232 if (!swapout) /* this was a swap cache but the swap is unused ! */
2233 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
2234
2235 memcg = __mem_cgroup_uncharge_common(page, ctype);
2236
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002237 /* record memcg information */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002238 if (do_swap_account && swapout && memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002239 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002240 mem_cgroup_get(memcg);
2241 }
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002242 if (swapout && memcg)
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08002243 css_put(&memcg->css);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002244}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002245#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002246
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002247#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2248/*
2249 * called from swap_entry_free(). remove record in swap_cgroup and
2250 * uncharge "memsw" account.
2251 */
2252void mem_cgroup_uncharge_swap(swp_entry_t ent)
2253{
2254 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002255 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002256
2257 if (!do_swap_account)
2258 return;
2259
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002260 id = swap_cgroup_record(ent, 0);
2261 rcu_read_lock();
2262 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002263 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002264 /*
2265 * We uncharge this because swap is freed.
2266 * This memcg can be obsolete one. We avoid calling css_tryget
2267 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002268 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002269 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002270 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002271 mem_cgroup_put(memcg);
2272 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002273 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002274}
Daisuke Nishimura02491442010-03-10 15:22:17 -08002275
2276/**
2277 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
2278 * @entry: swap entry to be moved
2279 * @from: mem_cgroup which the entry is moved from
2280 * @to: mem_cgroup which the entry is moved to
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002281 * @need_fixup: whether we should fixup res_counters and refcounts.
Daisuke Nishimura02491442010-03-10 15:22:17 -08002282 *
2283 * It succeeds only when the swap_cgroup's record for this entry is the same
2284 * as the mem_cgroup's id of @from.
2285 *
2286 * Returns 0 on success, -EINVAL on failure.
2287 *
2288 * The caller must have charged to @to, IOW, called res_counter_charge() about
2289 * both res and memsw, and called css_get().
2290 */
2291static int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002292 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08002293{
2294 unsigned short old_id, new_id;
2295
2296 old_id = css_id(&from->css);
2297 new_id = css_id(&to->css);
2298
2299 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08002300 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08002301 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002302 /*
2303 * This function is only called from task migration context now.
2304 * It postpones res_counter and refcount handling till the end
2305 * of task migration(mem_cgroup_clear_mc()) for performance
2306 * improvement. But we cannot postpone mem_cgroup_get(to)
2307 * because if the process that has been moved to @to does
2308 * swap-in, the refcount of @to might be decreased to 0.
2309 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08002310 mem_cgroup_get(to);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002311 if (need_fixup) {
2312 if (!mem_cgroup_is_root(from))
2313 res_counter_uncharge(&from->memsw, PAGE_SIZE);
2314 mem_cgroup_put(from);
2315 /*
2316 * we charged both to->res and to->memsw, so we should
2317 * uncharge to->res.
2318 */
2319 if (!mem_cgroup_is_root(to))
2320 res_counter_uncharge(&to->res, PAGE_SIZE);
2321 css_put(&to->css);
2322 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08002323 return 0;
2324 }
2325 return -EINVAL;
2326}
2327#else
2328static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002329 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08002330{
2331 return -EINVAL;
2332}
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002333#endif
2334
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002335/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002336 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
2337 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002338 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002339int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002340{
2341 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002342 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002343 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08002344
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002345 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07002346 return 0;
2347
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002348 pc = lookup_page_cgroup(page);
2349 lock_page_cgroup(pc);
2350 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002351 mem = pc->mem_cgroup;
2352 css_get(&mem->css);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08002353 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002354 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002355
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002356 if (mem) {
Balbir Singhf64c3f52009-09-23 15:56:37 -07002357 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false,
2358 page);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002359 css_put(&mem->css);
2360 }
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002361 *ptr = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002362 return ret;
2363}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08002364
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002365/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002366void mem_cgroup_end_migration(struct mem_cgroup *mem,
2367 struct page *oldpage, struct page *newpage)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002368{
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002369 struct page *target, *unused;
2370 struct page_cgroup *pc;
2371 enum charge_type ctype;
2372
2373 if (!mem)
2374 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002375 cgroup_exclude_rmdir(&mem->css);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002376 /* at migration success, oldpage->mapping is NULL. */
2377 if (oldpage->mapping) {
2378 target = oldpage;
2379 unused = NULL;
2380 } else {
2381 target = newpage;
2382 unused = oldpage;
2383 }
2384
2385 if (PageAnon(target))
2386 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
2387 else if (page_is_file_cache(target))
2388 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
2389 else
2390 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
2391
2392 /* unused page is not on radix-tree now. */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002393 if (unused)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002394 __mem_cgroup_uncharge_common(unused, ctype);
2395
2396 pc = lookup_page_cgroup(target);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002397 /*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002398 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
2399 * So, double-counting is effectively avoided.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002400 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002401 __mem_cgroup_commit_charge(mem, pc, ctype);
2402
2403 /*
2404 * Both of oldpage and newpage are still under lock_page().
2405 * Then, we don't have to care about race in radix-tree.
2406 * But we have to be careful that this page is unmapped or not.
2407 *
2408 * There is a case for !page_mapped(). At the start of
2409 * migration, oldpage was mapped. But now, it's zapped.
2410 * But we know *target* page is not freed/reused under us.
2411 * mem_cgroup_uncharge_page() does all necessary checks.
2412 */
2413 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
2414 mem_cgroup_uncharge_page(target);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002415 /*
2416 * At migration, we may charge account against cgroup which has no tasks
2417 * So, rmdir()->pre_destroy() can be called while we do this charge.
2418 * In that case, we need to call pre_destroy() again. check it here.
2419 */
2420 cgroup_release_and_wakeup_rmdir(&mem->css);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002421}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08002422
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002423/*
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002424 * A call to try to shrink memory usage on charge failure at shmem's swapin.
2425 * Calling hierarchical_reclaim is not enough because we should update
2426 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
2427 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
2428 * not from the memcg which this page would be charged to.
2429 * try_charge_swapin does all of these works properly.
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002430 */
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002431int mem_cgroup_shmem_charge_fallback(struct page *page,
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002432 struct mm_struct *mm,
2433 gfp_t gfp_mask)
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002434{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002435 struct mem_cgroup *mem = NULL;
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002436 int ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002437
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002438 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002439 return 0;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002440
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002441 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2442 if (!ret)
2443 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002444
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002445 return ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002446}
2447
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002448static DEFINE_MUTEX(set_limit_mutex);
2449
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08002450static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002451 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002452{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002453 int retry_count;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002454 u64 memswlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002455 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002456 int children = mem_cgroup_count_children(memcg);
2457 u64 curusage, oldusage;
2458
2459 /*
2460 * For keeping hierarchical_reclaim simple, how long we should retry
2461 * is depends on callers. We set our retry-count to be function
2462 * of # of children which we should visit in this loop.
2463 */
2464 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
2465
2466 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002467
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002468 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002469 if (signal_pending(current)) {
2470 ret = -EINTR;
2471 break;
2472 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002473 /*
2474 * Rather than hide all in some function, I do this in
2475 * open coded manner. You see what this really does.
2476 * We have to guarantee mem->res.limit < mem->memsw.limit.
2477 */
2478 mutex_lock(&set_limit_mutex);
2479 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2480 if (memswlimit < val) {
2481 ret = -EINVAL;
2482 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002483 break;
2484 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002485 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07002486 if (!ret) {
2487 if (memswlimit == val)
2488 memcg->memsw_is_minimum = true;
2489 else
2490 memcg->memsw_is_minimum = false;
2491 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002492 mutex_unlock(&set_limit_mutex);
2493
2494 if (!ret)
2495 break;
2496
Bob Liuaa20d482009-12-15 16:47:14 -08002497 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh4e416952009-09-23 15:56:39 -07002498 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002499 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
2500 /* Usage is reduced ? */
2501 if (curusage >= oldusage)
2502 retry_count--;
2503 else
2504 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002505 }
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002506
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002507 return ret;
2508}
2509
Li Zefan338c8432009-06-17 16:27:15 -07002510static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
2511 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002512{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002513 int retry_count;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002514 u64 memlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002515 int children = mem_cgroup_count_children(memcg);
2516 int ret = -EBUSY;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002517
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002518 /* see mem_cgroup_resize_res_limit */
2519 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
2520 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002521 while (retry_count) {
2522 if (signal_pending(current)) {
2523 ret = -EINTR;
2524 break;
2525 }
2526 /*
2527 * Rather than hide all in some function, I do this in
2528 * open coded manner. You see what this really does.
2529 * We have to guarantee mem->res.limit < mem->memsw.limit.
2530 */
2531 mutex_lock(&set_limit_mutex);
2532 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2533 if (memlimit > val) {
2534 ret = -EINVAL;
2535 mutex_unlock(&set_limit_mutex);
2536 break;
2537 }
2538 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07002539 if (!ret) {
2540 if (memlimit == val)
2541 memcg->memsw_is_minimum = true;
2542 else
2543 memcg->memsw_is_minimum = false;
2544 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002545 mutex_unlock(&set_limit_mutex);
2546
2547 if (!ret)
2548 break;
2549
Balbir Singh4e416952009-09-23 15:56:39 -07002550 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh75822b42009-09-23 15:56:38 -07002551 MEM_CGROUP_RECLAIM_NOSWAP |
2552 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002553 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002554 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002555 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002556 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002557 else
2558 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002559 }
2560 return ret;
2561}
2562
Balbir Singh4e416952009-09-23 15:56:39 -07002563unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
2564 gfp_t gfp_mask, int nid,
2565 int zid)
2566{
2567 unsigned long nr_reclaimed = 0;
2568 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
2569 unsigned long reclaimed;
2570 int loop = 0;
2571 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002572 unsigned long long excess;
Balbir Singh4e416952009-09-23 15:56:39 -07002573
2574 if (order > 0)
2575 return 0;
2576
2577 mctz = soft_limit_tree_node_zone(nid, zid);
2578 /*
2579 * This loop can run a while, specially if mem_cgroup's continuously
2580 * keep exceeding their soft limit and putting the system under
2581 * pressure
2582 */
2583 do {
2584 if (next_mz)
2585 mz = next_mz;
2586 else
2587 mz = mem_cgroup_largest_soft_limit_node(mctz);
2588 if (!mz)
2589 break;
2590
2591 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
2592 gfp_mask,
2593 MEM_CGROUP_RECLAIM_SOFT);
2594 nr_reclaimed += reclaimed;
2595 spin_lock(&mctz->lock);
2596
2597 /*
2598 * If we failed to reclaim anything from this memory cgroup
2599 * it is time to move on to the next cgroup
2600 */
2601 next_mz = NULL;
2602 if (!reclaimed) {
2603 do {
2604 /*
2605 * Loop until we find yet another one.
2606 *
2607 * By the time we get the soft_limit lock
2608 * again, someone might have aded the
2609 * group back on the RB tree. Iterate to
2610 * make sure we get a different mem.
2611 * mem_cgroup_largest_soft_limit_node returns
2612 * NULL if no other cgroup is present on
2613 * the tree
2614 */
2615 next_mz =
2616 __mem_cgroup_largest_soft_limit_node(mctz);
2617 if (next_mz == mz) {
2618 css_put(&next_mz->mem->css);
2619 next_mz = NULL;
2620 } else /* next_mz == NULL or other memcg */
2621 break;
2622 } while (1);
2623 }
Balbir Singh4e416952009-09-23 15:56:39 -07002624 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002625 excess = res_counter_soft_limit_excess(&mz->mem->res);
Balbir Singh4e416952009-09-23 15:56:39 -07002626 /*
2627 * One school of thought says that we should not add
2628 * back the node to the tree if reclaim returns 0.
2629 * But our reclaim could return 0, simply because due
2630 * to priority we are exposing a smaller subset of
2631 * memory to reclaim from. Consider this as a longer
2632 * term TODO.
2633 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002634 /* If excess == 0, no tree ops */
2635 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07002636 spin_unlock(&mctz->lock);
2637 css_put(&mz->mem->css);
2638 loop++;
2639 /*
2640 * Could not reclaim anything and there are no more
2641 * mem cgroups to try or we seem to be looping without
2642 * reclaiming anything.
2643 */
2644 if (!nr_reclaimed &&
2645 (next_mz == NULL ||
2646 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
2647 break;
2648 } while (!nr_reclaimed);
2649 if (next_mz)
2650 css_put(&next_mz->mem->css);
2651 return nr_reclaimed;
2652}
2653
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002654/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002655 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002656 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
2657 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002658static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002659 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002660{
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002661 struct zone *zone;
2662 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002663 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002664 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002665 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002666 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002667
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002668 zone = &NODE_DATA(node)->node_zones[zid];
2669 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07002670 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002671
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002672 loop = MEM_CGROUP_ZSTAT(mz, lru);
2673 /* give some margin against EBUSY etc...*/
2674 loop += 256;
2675 busy = NULL;
2676 while (loop--) {
2677 ret = 0;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002678 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002679 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002680 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002681 break;
2682 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002683 pc = list_entry(list->prev, struct page_cgroup, lru);
2684 if (busy == pc) {
2685 list_move(&pc->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08002686 busy = NULL;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002687 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002688 continue;
2689 }
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002690 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002691
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -08002692 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002693 if (ret == -ENOMEM)
2694 break;
2695
2696 if (ret == -EBUSY || ret == -EINVAL) {
2697 /* found lock contention or "pc" is obsolete. */
2698 busy = pc;
2699 cond_resched();
2700 } else
2701 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002702 }
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002703
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002704 if (!ret && !list_empty(list))
2705 return -EBUSY;
2706 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002707}
2708
2709/*
2710 * make mem_cgroup's charge to be 0 if there is no task.
2711 * This enables deleting this mem_cgroup.
2712 */
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002713static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002714{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002715 int ret;
2716 int node, zid, shrink;
2717 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002718 struct cgroup *cgrp = mem->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08002719
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002720 css_get(&mem->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002721
2722 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002723 /* should free all ? */
2724 if (free_all)
2725 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002726move_account:
Daisuke Nishimurafce66472010-01-15 17:01:30 -08002727 do {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002728 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002729 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002730 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002731 ret = -EINTR;
2732 if (signal_pending(current))
2733 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002734 /* This is for making all *used* pages to be on LRU. */
2735 lru_add_drain_all();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002736 drain_all_stock_sync();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002737 ret = 0;
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08002738 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002739 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07002740 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002741 for_each_lru(l) {
2742 ret = mem_cgroup_force_empty_list(mem,
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002743 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002744 if (ret)
2745 break;
2746 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002747 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002748 if (ret)
2749 break;
2750 }
2751 /* it seems parent cgroup doesn't have enough mem */
2752 if (ret == -ENOMEM)
2753 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002754 cond_resched();
Daisuke Nishimurafce66472010-01-15 17:01:30 -08002755 /* "ret" should also be checked to ensure all lists are empty. */
2756 } while (mem->res.usage > 0 || ret);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002757out:
2758 css_put(&mem->css);
2759 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002760
2761try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002762 /* returns EBUSY if there is a task or if we come here twice. */
2763 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002764 ret = -EBUSY;
2765 goto out;
2766 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002767 /* we call try-to-free pages for make this cgroup empty */
2768 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002769 /* try to free all pages in this cgroup */
2770 shrink = 1;
2771 while (nr_retries && mem->res.usage > 0) {
2772 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002773
2774 if (signal_pending(current)) {
2775 ret = -EINTR;
2776 goto out;
2777 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002778 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
2779 false, get_swappiness(mem));
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002780 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002781 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002782 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02002783 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002784 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002785
2786 }
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002787 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002788 /* try move_account...there may be some *locked* pages. */
Daisuke Nishimurafce66472010-01-15 17:01:30 -08002789 goto move_account;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002790}
2791
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002792int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
2793{
2794 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
2795}
2796
2797
Balbir Singh18f59ea2009-01-07 18:08:07 -08002798static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
2799{
2800 return mem_cgroup_from_cont(cont)->use_hierarchy;
2801}
2802
2803static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
2804 u64 val)
2805{
2806 int retval = 0;
2807 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2808 struct cgroup *parent = cont->parent;
2809 struct mem_cgroup *parent_mem = NULL;
2810
2811 if (parent)
2812 parent_mem = mem_cgroup_from_cont(parent);
2813
2814 cgroup_lock();
2815 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002816 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08002817 * in the child subtrees. If it is unset, then the change can
2818 * occur, provided the current cgroup has no children.
2819 *
2820 * For the root cgroup, parent_mem is NULL, we allow value to be
2821 * set if there are no children.
2822 */
2823 if ((!parent_mem || !parent_mem->use_hierarchy) &&
2824 (val == 1 || val == 0)) {
2825 if (list_empty(&cont->children))
2826 mem->use_hierarchy = val;
2827 else
2828 retval = -EBUSY;
2829 } else
2830 retval = -EINVAL;
2831 cgroup_unlock();
2832
2833 return retval;
2834}
2835
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002836struct mem_cgroup_idx_data {
2837 s64 val;
2838 enum mem_cgroup_stat_index idx;
2839};
2840
2841static int
2842mem_cgroup_get_idx_stat(struct mem_cgroup *mem, void *data)
2843{
2844 struct mem_cgroup_idx_data *d = data;
2845 d->val += mem_cgroup_read_stat(&mem->stat, d->idx);
2846 return 0;
2847}
2848
2849static void
2850mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem,
2851 enum mem_cgroup_stat_index idx, s64 *val)
2852{
2853 struct mem_cgroup_idx_data d;
2854 d.idx = idx;
2855 d.val = 0;
2856 mem_cgroup_walk_tree(mem, &d, mem_cgroup_get_idx_stat);
2857 *val = d.val;
2858}
2859
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08002860static inline u64 mem_cgroup_usage(struct mem_cgroup *mem, bool swap)
2861{
2862 u64 idx_val, val;
2863
2864 if (!mem_cgroup_is_root(mem)) {
2865 if (!swap)
2866 return res_counter_read_u64(&mem->res, RES_USAGE);
2867 else
2868 return res_counter_read_u64(&mem->memsw, RES_USAGE);
2869 }
2870
2871 mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_CACHE, &idx_val);
2872 val = idx_val;
2873 mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_RSS, &idx_val);
2874 val += idx_val;
2875
2876 if (swap) {
2877 mem_cgroup_get_recursive_idx_stat(mem,
2878 MEM_CGROUP_STAT_SWAPOUT, &idx_val);
2879 val += idx_val;
2880 }
2881
2882 return val << PAGE_SHIFT;
2883}
2884
Paul Menage2c3daa72008-04-29 00:59:58 -07002885static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002886{
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002887 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08002888 u64 val;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002889 int type, name;
2890
2891 type = MEMFILE_TYPE(cft->private);
2892 name = MEMFILE_ATTR(cft->private);
2893 switch (type) {
2894 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08002895 if (name == RES_USAGE)
2896 val = mem_cgroup_usage(mem, false);
2897 else
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002898 val = res_counter_read_u64(&mem->res, name);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002899 break;
2900 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08002901 if (name == RES_USAGE)
2902 val = mem_cgroup_usage(mem, true);
2903 else
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002904 val = res_counter_read_u64(&mem->memsw, name);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002905 break;
2906 default:
2907 BUG();
2908 break;
2909 }
2910 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002911}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002912/*
2913 * The user of this function is...
2914 * RES_LIMIT.
2915 */
Paul Menage856c13a2008-07-25 01:47:04 -07002916static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
2917 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002918{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002919 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002920 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002921 unsigned long long val;
2922 int ret;
2923
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002924 type = MEMFILE_TYPE(cft->private);
2925 name = MEMFILE_ATTR(cft->private);
2926 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002927 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07002928 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
2929 ret = -EINVAL;
2930 break;
2931 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002932 /* This function does all necessary parse...reuse it */
2933 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002934 if (ret)
2935 break;
2936 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002937 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002938 else
2939 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002940 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07002941 case RES_SOFT_LIMIT:
2942 ret = res_counter_memparse_write_strategy(buffer, &val);
2943 if (ret)
2944 break;
2945 /*
2946 * For memsw, soft limits are hard to implement in terms
2947 * of semantics, for now, we support soft limits for
2948 * control without swap
2949 */
2950 if (type == _MEM)
2951 ret = res_counter_set_soft_limit(&memcg->res, val);
2952 else
2953 ret = -EINVAL;
2954 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002955 default:
2956 ret = -EINVAL; /* should be BUG() ? */
2957 break;
2958 }
2959 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002960}
2961
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08002962static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
2963 unsigned long long *mem_limit, unsigned long long *memsw_limit)
2964{
2965 struct cgroup *cgroup;
2966 unsigned long long min_limit, min_memsw_limit, tmp;
2967
2968 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2969 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2970 cgroup = memcg->css.cgroup;
2971 if (!memcg->use_hierarchy)
2972 goto out;
2973
2974 while (cgroup->parent) {
2975 cgroup = cgroup->parent;
2976 memcg = mem_cgroup_from_cont(cgroup);
2977 if (!memcg->use_hierarchy)
2978 break;
2979 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
2980 min_limit = min(min_limit, tmp);
2981 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2982 min_memsw_limit = min(min_memsw_limit, tmp);
2983 }
2984out:
2985 *mem_limit = min_limit;
2986 *memsw_limit = min_memsw_limit;
2987 return;
2988}
2989
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002990static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002991{
2992 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002993 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002994
2995 mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002996 type = MEMFILE_TYPE(event);
2997 name = MEMFILE_ATTR(event);
2998 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002999 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003000 if (type == _MEM)
3001 res_counter_reset_max(&mem->res);
3002 else
3003 res_counter_reset_max(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003004 break;
3005 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003006 if (type == _MEM)
3007 res_counter_reset_failcnt(&mem->res);
3008 else
3009 res_counter_reset_failcnt(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003010 break;
3011 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07003012
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07003013 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003014}
3015
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003016static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
3017 struct cftype *cft)
3018{
3019 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
3020}
3021
Daisuke Nishimura02491442010-03-10 15:22:17 -08003022#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003023static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
3024 struct cftype *cft, u64 val)
3025{
3026 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3027
3028 if (val >= (1 << NR_MOVE_TYPE))
3029 return -EINVAL;
3030 /*
3031 * We check this value several times in both in can_attach() and
3032 * attach(), so we need cgroup lock to prevent this value from being
3033 * inconsistent.
3034 */
3035 cgroup_lock();
3036 mem->move_charge_at_immigrate = val;
3037 cgroup_unlock();
3038
3039 return 0;
3040}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003041#else
3042static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
3043 struct cftype *cft, u64 val)
3044{
3045 return -ENOSYS;
3046}
3047#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003048
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003049
3050/* For read statistics */
3051enum {
3052 MCS_CACHE,
3053 MCS_RSS,
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08003054 MCS_FILE_MAPPED,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003055 MCS_PGPGIN,
3056 MCS_PGPGOUT,
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003057 MCS_SWAP,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003058 MCS_INACTIVE_ANON,
3059 MCS_ACTIVE_ANON,
3060 MCS_INACTIVE_FILE,
3061 MCS_ACTIVE_FILE,
3062 MCS_UNEVICTABLE,
3063 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003064};
3065
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003066struct mcs_total_stat {
3067 s64 stat[NR_MCS_STAT];
3068};
3069
3070struct {
3071 char *local_name;
3072 char *total_name;
3073} memcg_stat_strings[NR_MCS_STAT] = {
3074 {"cache", "total_cache"},
3075 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07003076 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003077 {"pgpgin", "total_pgpgin"},
3078 {"pgpgout", "total_pgpgout"},
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003079 {"swap", "total_swap"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003080 {"inactive_anon", "total_inactive_anon"},
3081 {"active_anon", "total_active_anon"},
3082 {"inactive_file", "total_inactive_file"},
3083 {"active_file", "total_active_file"},
3084 {"unevictable", "total_unevictable"}
3085};
3086
3087
3088static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
3089{
3090 struct mcs_total_stat *s = data;
3091 s64 val;
3092
3093 /* per cpu stat */
3094 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE);
3095 s->stat[MCS_CACHE] += val * PAGE_SIZE;
3096 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
3097 s->stat[MCS_RSS] += val * PAGE_SIZE;
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08003098 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_FILE_MAPPED);
3099 s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003100 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT);
3101 s->stat[MCS_PGPGIN] += val;
3102 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT);
3103 s->stat[MCS_PGPGOUT] += val;
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003104 if (do_swap_account) {
3105 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_SWAPOUT);
3106 s->stat[MCS_SWAP] += val * PAGE_SIZE;
3107 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003108
3109 /* per zone stat */
3110 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
3111 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
3112 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
3113 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
3114 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
3115 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
3116 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
3117 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
3118 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
3119 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
3120 return 0;
3121}
3122
3123static void
3124mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
3125{
3126 mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
3127}
3128
Paul Menagec64745c2008-04-29 01:00:02 -07003129static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
3130 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003131{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003132 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003133 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003134 int i;
3135
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003136 memset(&mystat, 0, sizeof(mystat));
3137 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003138
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003139 for (i = 0; i < NR_MCS_STAT; i++) {
3140 if (i == MCS_SWAP && !do_swap_account)
3141 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003142 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003143 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003144
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003145 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08003146 {
3147 unsigned long long limit, memsw_limit;
3148 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
3149 cb->fill(cb, "hierarchical_memory_limit", limit);
3150 if (do_swap_account)
3151 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
3152 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003153
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003154 memset(&mystat, 0, sizeof(mystat));
3155 mem_cgroup_get_total_stat(mem_cont, &mystat);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003156 for (i = 0; i < NR_MCS_STAT; i++) {
3157 if (i == MCS_SWAP && !do_swap_account)
3158 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003159 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003160 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003161
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003162#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08003163 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003164
3165 {
3166 int nid, zid;
3167 struct mem_cgroup_per_zone *mz;
3168 unsigned long recent_rotated[2] = {0, 0};
3169 unsigned long recent_scanned[2] = {0, 0};
3170
3171 for_each_online_node(nid)
3172 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
3173 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
3174
3175 recent_rotated[0] +=
3176 mz->reclaim_stat.recent_rotated[0];
3177 recent_rotated[1] +=
3178 mz->reclaim_stat.recent_rotated[1];
3179 recent_scanned[0] +=
3180 mz->reclaim_stat.recent_scanned[0];
3181 recent_scanned[1] +=
3182 mz->reclaim_stat.recent_scanned[1];
3183 }
3184 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
3185 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
3186 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
3187 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
3188 }
3189#endif
3190
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003191 return 0;
3192}
3193
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003194static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
3195{
3196 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3197
3198 return get_swappiness(memcg);
3199}
3200
3201static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
3202 u64 val)
3203{
3204 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3205 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08003206
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003207 if (val > 100)
3208 return -EINVAL;
3209
3210 if (cgrp->parent == NULL)
3211 return -EINVAL;
3212
3213 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08003214
3215 cgroup_lock();
3216
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003217 /* If under hierarchy, only empty-root can set this value */
3218 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08003219 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
3220 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003221 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08003222 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003223
3224 spin_lock(&memcg->reclaim_param_lock);
3225 memcg->swappiness = val;
3226 spin_unlock(&memcg->reclaim_param_lock);
3227
Li Zefan068b38c2009-01-15 13:51:26 -08003228 cgroup_unlock();
3229
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003230 return 0;
3231}
3232
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003233
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003234static struct cftype mem_cgroup_files[] = {
3235 {
Balbir Singh0eea1032008-02-07 00:13:57 -08003236 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003237 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07003238 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003239 },
3240 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003241 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003242 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003243 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003244 .read_u64 = mem_cgroup_read,
3245 },
3246 {
Balbir Singh0eea1032008-02-07 00:13:57 -08003247 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003248 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07003249 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07003250 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003251 },
3252 {
Balbir Singh296c81d2009-09-23 15:56:36 -07003253 .name = "soft_limit_in_bytes",
3254 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
3255 .write_string = mem_cgroup_write,
3256 .read_u64 = mem_cgroup_read,
3257 },
3258 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003259 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003260 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003261 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07003262 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003263 },
Balbir Singh8697d332008-02-07 00:13:59 -08003264 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003265 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07003266 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003267 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003268 {
3269 .name = "force_empty",
3270 .trigger = mem_cgroup_force_empty_write,
3271 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08003272 {
3273 .name = "use_hierarchy",
3274 .write_u64 = mem_cgroup_hierarchy_write,
3275 .read_u64 = mem_cgroup_hierarchy_read,
3276 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003277 {
3278 .name = "swappiness",
3279 .read_u64 = mem_cgroup_swappiness_read,
3280 .write_u64 = mem_cgroup_swappiness_write,
3281 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003282 {
3283 .name = "move_charge_at_immigrate",
3284 .read_u64 = mem_cgroup_move_charge_read,
3285 .write_u64 = mem_cgroup_move_charge_write,
3286 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003287};
3288
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003289#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3290static struct cftype memsw_cgroup_files[] = {
3291 {
3292 .name = "memsw.usage_in_bytes",
3293 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
3294 .read_u64 = mem_cgroup_read,
3295 },
3296 {
3297 .name = "memsw.max_usage_in_bytes",
3298 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
3299 .trigger = mem_cgroup_reset,
3300 .read_u64 = mem_cgroup_read,
3301 },
3302 {
3303 .name = "memsw.limit_in_bytes",
3304 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
3305 .write_string = mem_cgroup_write,
3306 .read_u64 = mem_cgroup_read,
3307 },
3308 {
3309 .name = "memsw.failcnt",
3310 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
3311 .trigger = mem_cgroup_reset,
3312 .read_u64 = mem_cgroup_read,
3313 },
3314};
3315
3316static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
3317{
3318 if (!do_swap_account)
3319 return 0;
3320 return cgroup_add_files(cont, ss, memsw_cgroup_files,
3321 ARRAY_SIZE(memsw_cgroup_files));
3322};
3323#else
3324static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
3325{
3326 return 0;
3327}
3328#endif
3329
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003330static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
3331{
3332 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003333 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07003334 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07003335 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003336 /*
3337 * This routine is called against possible nodes.
3338 * But it's BUG to call kmalloc() against offline node.
3339 *
3340 * TODO: this routine can waste much memory for nodes which will
3341 * never be onlined. It's better to use memory hotplug callback
3342 * function.
3343 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07003344 if (!node_state(node, N_NORMAL_MEMORY))
3345 tmp = -1;
3346 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003347 if (!pn)
3348 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003349
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003350 mem->info.nodeinfo[node] = pn;
3351 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003352
3353 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
3354 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07003355 for_each_lru(l)
3356 INIT_LIST_HEAD(&mz->lists[l]);
Balbir Singhf64c3f52009-09-23 15:56:37 -07003357 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07003358 mz->on_tree = false;
3359 mz->mem = mem;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003360 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003361 return 0;
3362}
3363
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003364static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
3365{
3366 kfree(mem->info.nodeinfo[node]);
3367}
3368
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003369static int mem_cgroup_size(void)
3370{
3371 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
3372 return sizeof(struct mem_cgroup) + cpustat_size;
3373}
3374
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003375static struct mem_cgroup *mem_cgroup_alloc(void)
3376{
3377 struct mem_cgroup *mem;
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003378 int size = mem_cgroup_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003379
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003380 if (size < PAGE_SIZE)
3381 mem = kmalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003382 else
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003383 mem = vmalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003384
3385 if (mem)
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003386 memset(mem, 0, size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003387 return mem;
3388}
3389
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003390/*
3391 * At destroying mem_cgroup, references from swap_cgroup can remain.
3392 * (scanning all at force_empty is too costly...)
3393 *
3394 * Instead of clearing all references at force_empty, we remember
3395 * the number of reference from swap_cgroup and free mem_cgroup when
3396 * it goes down to 0.
3397 *
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003398 * Removal of cgroup itself succeeds regardless of refs from swap.
3399 */
3400
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003401static void __mem_cgroup_free(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003402{
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003403 int node;
3404
Balbir Singhf64c3f52009-09-23 15:56:37 -07003405 mem_cgroup_remove_from_trees(mem);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003406 free_css_id(&mem_cgroup_subsys, &mem->css);
3407
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003408 for_each_node_state(node, N_POSSIBLE)
3409 free_mem_cgroup_per_zone_info(mem, node);
3410
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003411 if (mem_cgroup_size() < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003412 kfree(mem);
3413 else
3414 vfree(mem);
3415}
3416
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003417static void mem_cgroup_get(struct mem_cgroup *mem)
3418{
3419 atomic_inc(&mem->refcnt);
3420}
3421
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003422static void __mem_cgroup_put(struct mem_cgroup *mem, int count)
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003423{
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003424 if (atomic_sub_and_test(count, &mem->refcnt)) {
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003425 struct mem_cgroup *parent = parent_mem_cgroup(mem);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003426 __mem_cgroup_free(mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003427 if (parent)
3428 mem_cgroup_put(parent);
3429 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003430}
3431
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003432static void mem_cgroup_put(struct mem_cgroup *mem)
3433{
3434 __mem_cgroup_put(mem, 1);
3435}
3436
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003437/*
3438 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
3439 */
3440static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
3441{
3442 if (!mem->res.parent)
3443 return NULL;
3444 return mem_cgroup_from_res_counter(mem->res.parent, res);
3445}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003446
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003447#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3448static void __init enable_swap_cgroup(void)
3449{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003450 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003451 do_swap_account = 1;
3452}
3453#else
3454static void __init enable_swap_cgroup(void)
3455{
3456}
3457#endif
3458
Balbir Singhf64c3f52009-09-23 15:56:37 -07003459static int mem_cgroup_soft_limit_tree_init(void)
3460{
3461 struct mem_cgroup_tree_per_node *rtpn;
3462 struct mem_cgroup_tree_per_zone *rtpz;
3463 int tmp, node, zone;
3464
3465 for_each_node_state(node, N_POSSIBLE) {
3466 tmp = node;
3467 if (!node_state(node, N_NORMAL_MEMORY))
3468 tmp = -1;
3469 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
3470 if (!rtpn)
3471 return 1;
3472
3473 soft_limit_tree.rb_tree_per_node[node] = rtpn;
3474
3475 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
3476 rtpz = &rtpn->rb_tree_per_zone[zone];
3477 rtpz->rb_root = RB_ROOT;
3478 spin_lock_init(&rtpz->lock);
3479 }
3480 }
3481 return 0;
3482}
3483
Li Zefan0eb253e2009-01-15 13:51:25 -08003484static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003485mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
3486{
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003487 struct mem_cgroup *mem, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003488 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003489 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003490
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003491 mem = mem_cgroup_alloc();
3492 if (!mem)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003493 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08003494
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003495 for_each_node_state(node, N_POSSIBLE)
3496 if (alloc_mem_cgroup_per_zone_info(mem, node))
3497 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07003498
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003499 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003500 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08003501 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003502 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003503 parent = NULL;
Balbir Singh4b3bde42009-09-23 15:56:32 -07003504 root_mem_cgroup = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07003505 if (mem_cgroup_soft_limit_tree_init())
3506 goto free_out;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08003507 for_each_possible_cpu(cpu) {
3508 struct memcg_stock_pcp *stock =
3509 &per_cpu(memcg_stock, cpu);
3510 INIT_WORK(&stock->work, drain_local_stock);
3511 }
3512 hotcpu_notifier(memcg_stock_cpu_callback, 0);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003513 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003514 parent = mem_cgroup_from_cont(cont->parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003515 mem->use_hierarchy = parent->use_hierarchy;
3516 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003517
Balbir Singh18f59ea2009-01-07 18:08:07 -08003518 if (parent && parent->use_hierarchy) {
3519 res_counter_init(&mem->res, &parent->res);
3520 res_counter_init(&mem->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003521 /*
3522 * We increment refcnt of the parent to ensure that we can
3523 * safely access it on res_counter_charge/uncharge.
3524 * This refcnt will be decremented when freeing this
3525 * mem_cgroup(see mem_cgroup_put).
3526 */
3527 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003528 } else {
3529 res_counter_init(&mem->res, NULL);
3530 res_counter_init(&mem->memsw, NULL);
3531 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003532 mem->last_scanned_child = 0;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -08003533 spin_lock_init(&mem->reclaim_param_lock);
Balbir Singh6d61ef42009-01-07 18:08:06 -08003534
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003535 if (parent)
3536 mem->swappiness = get_swappiness(parent);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003537 atomic_set(&mem->refcnt, 1);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003538 mem->move_charge_at_immigrate = 0;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003539 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003540free_out:
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003541 __mem_cgroup_free(mem);
Balbir Singh4b3bde42009-09-23 15:56:32 -07003542 root_mem_cgroup = NULL;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003543 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003544}
3545
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003546static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08003547 struct cgroup *cont)
3548{
3549 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003550
3551 return mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08003552}
3553
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003554static void mem_cgroup_destroy(struct cgroup_subsys *ss,
3555 struct cgroup *cont)
3556{
Daisuke Nishimurac268e992009-01-15 13:51:13 -08003557 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08003558
Daisuke Nishimurac268e992009-01-15 13:51:13 -08003559 mem_cgroup_put(mem);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003560}
3561
3562static int mem_cgroup_populate(struct cgroup_subsys *ss,
3563 struct cgroup *cont)
3564{
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003565 int ret;
3566
3567 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
3568 ARRAY_SIZE(mem_cgroup_files));
3569
3570 if (!ret)
3571 ret = register_memsw_files(cont, ss);
3572 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003573}
3574
Daisuke Nishimura02491442010-03-10 15:22:17 -08003575#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003576/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08003577#define PRECHARGE_COUNT_AT_ONCE 256
3578static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003579{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08003580 int ret = 0;
3581 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003582 struct mem_cgroup *mem = mc.to;
3583
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08003584 if (mem_cgroup_is_root(mem)) {
3585 mc.precharge += count;
3586 /* we don't need css_get for root */
3587 return ret;
3588 }
3589 /* try to charge at once */
3590 if (count > 1) {
3591 struct res_counter *dummy;
3592 /*
3593 * "mem" cannot be under rmdir() because we've already checked
3594 * by cgroup_lock_live_cgroup() that it is not removed and we
3595 * are still under the same cgroup_mutex. So we can postpone
3596 * css_get().
3597 */
3598 if (res_counter_charge(&mem->res, PAGE_SIZE * count, &dummy))
3599 goto one_by_one;
3600 if (do_swap_account && res_counter_charge(&mem->memsw,
3601 PAGE_SIZE * count, &dummy)) {
3602 res_counter_uncharge(&mem->res, PAGE_SIZE * count);
3603 goto one_by_one;
3604 }
3605 mc.precharge += count;
3606 VM_BUG_ON(test_bit(CSS_ROOT, &mem->css.flags));
3607 WARN_ON_ONCE(count > INT_MAX);
3608 __css_get(&mem->css, (int)count);
3609 return ret;
3610 }
3611one_by_one:
3612 /* fall back to one by one charge */
3613 while (count--) {
3614 if (signal_pending(current)) {
3615 ret = -EINTR;
3616 break;
3617 }
3618 if (!batch_count--) {
3619 batch_count = PRECHARGE_COUNT_AT_ONCE;
3620 cond_resched();
3621 }
3622 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem,
3623 false, NULL);
3624 if (ret || !mem)
3625 /* mem_cgroup_clear_mc() will do uncharge later */
3626 return -ENOMEM;
3627 mc.precharge++;
3628 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003629 return ret;
3630}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003631#else /* !CONFIG_MMU */
3632static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
3633 struct cgroup *cgroup,
3634 struct task_struct *p,
3635 bool threadgroup)
3636{
3637 return 0;
3638}
3639static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
3640 struct cgroup *cgroup,
3641 struct task_struct *p,
3642 bool threadgroup)
3643{
3644}
3645static void mem_cgroup_move_task(struct cgroup_subsys *ss,
3646 struct cgroup *cont,
3647 struct cgroup *old_cont,
3648 struct task_struct *p,
3649 bool threadgroup)
3650{
3651}
3652#endif
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003653
3654/**
3655 * is_target_pte_for_mc - check a pte whether it is valid for move charge
3656 * @vma: the vma the pte to be checked belongs
3657 * @addr: the address corresponding to the pte to be checked
3658 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08003659 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003660 *
3661 * Returns
3662 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
3663 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
3664 * move charge. if @target is not NULL, the page is stored in target->page
3665 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08003666 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
3667 * target for charge migration. if @target is not NULL, the entry is stored
3668 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003669 *
3670 * Called with pte lock held.
3671 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003672union mc_target {
3673 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08003674 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003675};
3676
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003677enum mc_target_type {
3678 MC_TARGET_NONE, /* not used */
3679 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08003680 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003681};
3682
3683static int is_target_pte_for_mc(struct vm_area_struct *vma,
3684 unsigned long addr, pte_t ptent, union mc_target *target)
3685{
Daisuke Nishimura02491442010-03-10 15:22:17 -08003686 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003687 struct page_cgroup *pc;
3688 int ret = 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08003689 swp_entry_t ent = { .val = 0 };
3690 int usage_count = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003691 bool move_anon = test_bit(MOVE_CHARGE_TYPE_ANON,
3692 &mc.to->move_charge_at_immigrate);
3693
Daisuke Nishimura02491442010-03-10 15:22:17 -08003694 if (!pte_present(ptent)) {
3695 /* TODO: handle swap of shmes/tmpfs */
3696 if (pte_none(ptent) || pte_file(ptent))
3697 return 0;
3698 else if (is_swap_pte(ptent)) {
3699 ent = pte_to_swp_entry(ptent);
3700 if (!move_anon || non_swap_entry(ent))
3701 return 0;
3702 usage_count = mem_cgroup_count_swap_user(ent, &page);
3703 }
3704 } else {
3705 page = vm_normal_page(vma, addr, ptent);
3706 if (!page || !page_mapped(page))
3707 return 0;
3708 /*
3709 * TODO: We don't move charges of file(including shmem/tmpfs)
3710 * pages for now.
3711 */
3712 if (!move_anon || !PageAnon(page))
3713 return 0;
3714 if (!get_page_unless_zero(page))
3715 return 0;
3716 usage_count = page_mapcount(page);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003717 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08003718 if (usage_count > 1) {
3719 /*
3720 * TODO: We don't move charges of shared(used by multiple
3721 * processes) pages for now.
3722 */
3723 if (page)
3724 put_page(page);
3725 return 0;
3726 }
3727 if (page) {
3728 pc = lookup_page_cgroup(page);
3729 /*
3730 * Do only loose check w/o page_cgroup lock.
3731 * mem_cgroup_move_account() checks the pc is valid or not under
3732 * the lock.
3733 */
3734 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
3735 ret = MC_TARGET_PAGE;
3736 if (target)
3737 target->page = page;
3738 }
3739 if (!ret || !target)
3740 put_page(page);
3741 }
3742 /* throught */
3743 if (ent.val && do_swap_account && !ret &&
3744 css_id(&mc.from->css) == lookup_swap_cgroup(ent)) {
3745 ret = MC_TARGET_SWAP;
3746 if (target)
3747 target->ent = ent;
3748 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003749 return ret;
3750}
3751
3752static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
3753 unsigned long addr, unsigned long end,
3754 struct mm_walk *walk)
3755{
3756 struct vm_area_struct *vma = walk->private;
3757 pte_t *pte;
3758 spinlock_t *ptl;
3759
3760 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
3761 for (; addr != end; pte++, addr += PAGE_SIZE)
3762 if (is_target_pte_for_mc(vma, addr, *pte, NULL))
3763 mc.precharge++; /* increment precharge temporarily */
3764 pte_unmap_unlock(pte - 1, ptl);
3765 cond_resched();
3766
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003767 return 0;
3768}
3769
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003770static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
3771{
3772 unsigned long precharge;
3773 struct vm_area_struct *vma;
3774
3775 down_read(&mm->mmap_sem);
3776 for (vma = mm->mmap; vma; vma = vma->vm_next) {
3777 struct mm_walk mem_cgroup_count_precharge_walk = {
3778 .pmd_entry = mem_cgroup_count_precharge_pte_range,
3779 .mm = mm,
3780 .private = vma,
3781 };
3782 if (is_vm_hugetlb_page(vma))
3783 continue;
3784 /* TODO: We don't move charges of shmem/tmpfs pages for now. */
3785 if (vma->vm_flags & VM_SHARED)
3786 continue;
3787 walk_page_range(vma->vm_start, vma->vm_end,
3788 &mem_cgroup_count_precharge_walk);
3789 }
3790 up_read(&mm->mmap_sem);
3791
3792 precharge = mc.precharge;
3793 mc.precharge = 0;
3794
3795 return precharge;
3796}
3797
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003798static int mem_cgroup_precharge_mc(struct mm_struct *mm)
3799{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08003800 return mem_cgroup_do_precharge(mem_cgroup_count_precharge(mm));
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003801}
3802
3803static void mem_cgroup_clear_mc(void)
3804{
3805 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08003806 if (mc.precharge) {
3807 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
3808 mc.precharge = 0;
3809 }
3810 /*
3811 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
3812 * we must uncharge here.
3813 */
3814 if (mc.moved_charge) {
3815 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
3816 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003817 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003818 /* we must fixup refcnts and charges */
3819 if (mc.moved_swap) {
3820 WARN_ON_ONCE(mc.moved_swap > INT_MAX);
3821 /* uncharge swap account from the old cgroup */
3822 if (!mem_cgroup_is_root(mc.from))
3823 res_counter_uncharge(&mc.from->memsw,
3824 PAGE_SIZE * mc.moved_swap);
3825 __mem_cgroup_put(mc.from, mc.moved_swap);
3826
3827 if (!mem_cgroup_is_root(mc.to)) {
3828 /*
3829 * we charged both to->res and to->memsw, so we should
3830 * uncharge to->res.
3831 */
3832 res_counter_uncharge(&mc.to->res,
3833 PAGE_SIZE * mc.moved_swap);
3834 VM_BUG_ON(test_bit(CSS_ROOT, &mc.to->css.flags));
3835 __css_put(&mc.to->css, mc.moved_swap);
3836 }
3837 /* we've already done mem_cgroup_get(mc.to) */
3838
3839 mc.moved_swap = 0;
3840 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003841 mc.from = NULL;
3842 mc.to = NULL;
Daisuke Nishimura8033b972010-03-10 15:22:16 -08003843 mc.moving_task = NULL;
3844 wake_up_all(&mc.waitq);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003845}
3846
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003847static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
3848 struct cgroup *cgroup,
3849 struct task_struct *p,
3850 bool threadgroup)
3851{
3852 int ret = 0;
3853 struct mem_cgroup *mem = mem_cgroup_from_cont(cgroup);
3854
3855 if (mem->move_charge_at_immigrate) {
3856 struct mm_struct *mm;
3857 struct mem_cgroup *from = mem_cgroup_from_task(p);
3858
3859 VM_BUG_ON(from == mem);
3860
3861 mm = get_task_mm(p);
3862 if (!mm)
3863 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003864 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003865 if (mm->owner == p) {
3866 VM_BUG_ON(mc.from);
3867 VM_BUG_ON(mc.to);
3868 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08003869 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003870 VM_BUG_ON(mc.moved_swap);
Daisuke Nishimura8033b972010-03-10 15:22:16 -08003871 VM_BUG_ON(mc.moving_task);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003872 mc.from = from;
3873 mc.to = mem;
3874 mc.precharge = 0;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08003875 mc.moved_charge = 0;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003876 mc.moved_swap = 0;
Daisuke Nishimura8033b972010-03-10 15:22:16 -08003877 mc.moving_task = current;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003878
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003879 ret = mem_cgroup_precharge_mc(mm);
3880 if (ret)
3881 mem_cgroup_clear_mc();
3882 }
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003883 mmput(mm);
3884 }
3885 return ret;
3886}
3887
3888static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
3889 struct cgroup *cgroup,
3890 struct task_struct *p,
3891 bool threadgroup)
3892{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003893 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003894}
3895
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003896static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
3897 unsigned long addr, unsigned long end,
3898 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003899{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003900 int ret = 0;
3901 struct vm_area_struct *vma = walk->private;
3902 pte_t *pte;
3903 spinlock_t *ptl;
3904
3905retry:
3906 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
3907 for (; addr != end; addr += PAGE_SIZE) {
3908 pte_t ptent = *(pte++);
3909 union mc_target target;
3910 int type;
3911 struct page *page;
3912 struct page_cgroup *pc;
Daisuke Nishimura02491442010-03-10 15:22:17 -08003913 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003914
3915 if (!mc.precharge)
3916 break;
3917
3918 type = is_target_pte_for_mc(vma, addr, ptent, &target);
3919 switch (type) {
3920 case MC_TARGET_PAGE:
3921 page = target.page;
3922 if (isolate_lru_page(page))
3923 goto put;
3924 pc = lookup_page_cgroup(page);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08003925 if (!mem_cgroup_move_account(pc,
3926 mc.from, mc.to, false)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003927 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08003928 /* we uncharge from mc.from later. */
3929 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003930 }
3931 putback_lru_page(page);
3932put: /* is_target_pte_for_mc() gets the page */
3933 put_page(page);
3934 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08003935 case MC_TARGET_SWAP:
3936 ent = target.ent;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003937 if (!mem_cgroup_move_swap_account(ent,
3938 mc.from, mc.to, false)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08003939 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003940 /* we fixup refcnts and charges later. */
3941 mc.moved_swap++;
3942 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08003943 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003944 default:
3945 break;
3946 }
3947 }
3948 pte_unmap_unlock(pte - 1, ptl);
3949 cond_resched();
3950
3951 if (addr != end) {
3952 /*
3953 * We have consumed all precharges we got in can_attach().
3954 * We try charge one by one, but don't do any additional
3955 * charges to mc.to if we have failed in charge once in attach()
3956 * phase.
3957 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08003958 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003959 if (!ret)
3960 goto retry;
3961 }
3962
3963 return ret;
3964}
3965
3966static void mem_cgroup_move_charge(struct mm_struct *mm)
3967{
3968 struct vm_area_struct *vma;
3969
3970 lru_add_drain_all();
3971 down_read(&mm->mmap_sem);
3972 for (vma = mm->mmap; vma; vma = vma->vm_next) {
3973 int ret;
3974 struct mm_walk mem_cgroup_move_charge_walk = {
3975 .pmd_entry = mem_cgroup_move_charge_pte_range,
3976 .mm = mm,
3977 .private = vma,
3978 };
3979 if (is_vm_hugetlb_page(vma))
3980 continue;
3981 /* TODO: We don't move charges of shmem/tmpfs pages for now. */
3982 if (vma->vm_flags & VM_SHARED)
3983 continue;
3984 ret = walk_page_range(vma->vm_start, vma->vm_end,
3985 &mem_cgroup_move_charge_walk);
3986 if (ret)
3987 /*
3988 * means we have consumed all precharges and failed in
3989 * doing additional charge. Just abandon here.
3990 */
3991 break;
3992 }
3993 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003994}
3995
Balbir Singh67e465a2008-02-07 00:13:54 -08003996static void mem_cgroup_move_task(struct cgroup_subsys *ss,
3997 struct cgroup *cont,
3998 struct cgroup *old_cont,
Ben Blumbe367d02009-09-23 15:56:31 -07003999 struct task_struct *p,
4000 bool threadgroup)
Balbir Singh67e465a2008-02-07 00:13:54 -08004001{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004002 struct mm_struct *mm;
4003
4004 if (!mc.to)
4005 /* no need to move charge */
4006 return;
4007
4008 mm = get_task_mm(p);
4009 if (mm) {
4010 mem_cgroup_move_charge(mm);
4011 mmput(mm);
4012 }
4013 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08004014}
4015
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004016struct cgroup_subsys mem_cgroup_subsys = {
4017 .name = "memory",
4018 .subsys_id = mem_cgroup_subsys_id,
4019 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004020 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004021 .destroy = mem_cgroup_destroy,
4022 .populate = mem_cgroup_populate,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004023 .can_attach = mem_cgroup_can_attach,
4024 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08004025 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004026 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004027 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004028};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004029
4030#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4031
4032static int __init disable_swap_account(char *s)
4033{
4034 really_do_swap_account = 0;
4035 return 1;
4036}
4037__setup("noswapaccount", disable_swap_account);
4038#endif