blob: 26218df8d19d052ede8a438b6cb5ac5fa085d5d8 [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 *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08009 * Memory thresholds
10 * Copyright (C) 2009 Nokia Corporation
11 * Author: Kirill A. Shutemov
12 *
Balbir Singh8cdea7c2008-02-07 00:13:50 -080013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 */
23
24#include <linux/res_counter.h>
25#include <linux/memcontrol.h>
26#include <linux/cgroup.h>
Pavel Emelianov78fb7462008-02-07 00:13:51 -080027#include <linux/mm.h>
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -080028#include <linux/hugetlb.h>
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -080029#include <linux/pagemap.h>
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080030#include <linux/smp.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080031#include <linux/page-flags.h>
Balbir Singh66e17072008-02-07 00:13:56 -080032#include <linux/backing-dev.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080033#include <linux/bit_spinlock.h>
34#include <linux/rcupdate.h>
Balbir Singhe2224322009-04-02 16:57:39 -070035#include <linux/limits.h>
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -080036#include <linux/mutex.h>
Balbir Singhf64c3f52009-09-23 15:56:37 -070037#include <linux/rbtree.h>
Balbir Singhb6ac57d2008-04-29 01:00:19 -070038#include <linux/slab.h>
Balbir Singh66e17072008-02-07 00:13:56 -080039#include <linux/swap.h>
Daisuke Nishimura02491442010-03-10 15:22:17 -080040#include <linux/swapops.h>
Balbir Singh66e17072008-02-07 00:13:56 -080041#include <linux/spinlock.h>
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -080042#include <linux/eventfd.h>
43#include <linux/sort.h>
Balbir Singh66e17072008-02-07 00:13:56 -080044#include <linux/fs.h>
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -080045#include <linux/seq_file.h>
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -070046#include <linux/vmalloc.h>
Christoph Lameterb69408e2008-10-18 20:26:14 -070047#include <linux/mm_inline.h>
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070048#include <linux/page_cgroup.h>
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -080049#include <linux/cpu.h>
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -070050#include <linux/oom.h>
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -080051#include "internal.h"
Balbir Singh8cdea7c2008-02-07 00:13:50 -080052
Balbir Singh8697d332008-02-07 00:13:59 -080053#include <asm/uaccess.h>
54
KOSAKI Motohirocc8e9702010-08-09 17:19:57 -070055#include <trace/events/vmscan.h>
56
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070057struct cgroup_subsys mem_cgroup_subsys __read_mostly;
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070058#define MEM_CGROUP_RECLAIM_RETRIES 5
Balbir Singh4b3bde42009-09-23 15:56:32 -070059struct mem_cgroup *root_mem_cgroup __read_mostly;
Balbir Singh8cdea7c2008-02-07 00:13:50 -080060
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080061#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
Li Zefan338c8432009-06-17 16:27:15 -070062/* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080063int do_swap_account __read_mostly;
64static int really_do_swap_account __initdata = 1; /* for remember boot option*/
65#else
66#define do_swap_account (0)
67#endif
68
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -080069/*
70 * Per memcg event counter is incremented at every pagein/pageout. This counter
71 * is used for trigger some periodic events. This is straightforward and better
72 * than using jiffies etc. to handle periodic memcg event.
73 *
74 * These values will be used as !((event) & ((1 <<(thresh)) - 1))
75 */
76#define THRESHOLDS_EVENTS_THRESH (7) /* once in 128 */
77#define SOFTLIMIT_EVENTS_THRESH (10) /* once in 1024 */
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080078
Balbir Singh8cdea7c2008-02-07 00:13:50 -080079/*
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080080 * Statistics for memory cgroup.
81 */
82enum mem_cgroup_stat_index {
83 /*
84 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
85 */
86 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
Balbir Singhd69b0422009-06-17 16:26:34 -070087 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -080088 MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */
Balaji Rao55e462b2008-05-01 04:35:12 -070089 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
90 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
Balbir Singh0c3e73e2009-09-23 15:56:42 -070091 MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -070092 MEM_CGROUP_STAT_DATA, /* end of data requires synchronization */
93 /* incremented at every pagein/pageout */
94 MEM_CGROUP_EVENTS = MEM_CGROUP_STAT_DATA,
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -070095 MEM_CGROUP_ON_MOVE, /* someone is moving account between groups */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080096
97 MEM_CGROUP_STAT_NSTATS,
98};
99
100struct mem_cgroup_stat_cpu {
101 s64 count[MEM_CGROUP_STAT_NSTATS];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800102};
103
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800104/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800105 * per-zone information in memory controller.
106 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800107struct mem_cgroup_per_zone {
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800108 /*
109 * spin_lock to protect the per cgroup LRU
110 */
Christoph Lameterb69408e2008-10-18 20:26:14 -0700111 struct list_head lists[NR_LRU_LISTS];
112 unsigned long count[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800113
114 struct zone_reclaim_stat reclaim_stat;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700115 struct rb_node tree_node; /* RB tree node */
116 unsigned long long usage_in_excess;/* Set to the value by which */
117 /* the soft limit is exceeded*/
118 bool on_tree;
Balbir Singh4e416952009-09-23 15:56:39 -0700119 struct mem_cgroup *mem; /* Back pointer, we cannot */
120 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800121};
122/* Macro for accessing counter */
123#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
124
125struct mem_cgroup_per_node {
126 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
127};
128
129struct mem_cgroup_lru_info {
130 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
131};
132
133/*
Balbir Singhf64c3f52009-09-23 15:56:37 -0700134 * Cgroups above their limits are maintained in a RB-Tree, independent of
135 * their hierarchy representation
136 */
137
138struct mem_cgroup_tree_per_zone {
139 struct rb_root rb_root;
140 spinlock_t lock;
141};
142
143struct mem_cgroup_tree_per_node {
144 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
145};
146
147struct mem_cgroup_tree {
148 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
149};
150
151static struct mem_cgroup_tree soft_limit_tree __read_mostly;
152
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800153struct mem_cgroup_threshold {
154 struct eventfd_ctx *eventfd;
155 u64 threshold;
156};
157
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700158/* For threshold */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800159struct mem_cgroup_threshold_ary {
160 /* An array index points to threshold just below usage. */
Phil Carmody5407a562010-05-26 14:42:42 -0700161 int current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800162 /* Size of entries[] */
163 unsigned int size;
164 /* Array of thresholds */
165 struct mem_cgroup_threshold entries[0];
166};
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700167
168struct mem_cgroup_thresholds {
169 /* Primary thresholds array */
170 struct mem_cgroup_threshold_ary *primary;
171 /*
172 * Spare threshold array.
173 * This is needed to make mem_cgroup_unregister_event() "never fail".
174 * It must be able to store at least primary->size - 1 entries.
175 */
176 struct mem_cgroup_threshold_ary *spare;
177};
178
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700179/* for OOM */
180struct mem_cgroup_eventfd_list {
181 struct list_head list;
182 struct eventfd_ctx *eventfd;
183};
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800184
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800185static void mem_cgroup_threshold(struct mem_cgroup *mem);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700186static void mem_cgroup_oom_notify(struct mem_cgroup *mem);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800187
Balbir Singhf64c3f52009-09-23 15:56:37 -0700188/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800189 * The memory controller data structure. The memory controller controls both
190 * page cache and RSS per cgroup. We would eventually like to provide
191 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
192 * to help the administrator determine what knobs to tune.
193 *
194 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800195 * we hit the water mark. May be even add a low water mark, such that
196 * no reclaim occurs from a cgroup at it's low water mark, this is
197 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800198 */
199struct mem_cgroup {
200 struct cgroup_subsys_state css;
201 /*
202 * the counter to account for memory usage
203 */
204 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800205 /*
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800206 * the counter to account for mem+swap usage.
207 */
208 struct res_counter memsw;
209 /*
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800210 * Per cgroup active and inactive list, similar to the
211 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800212 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800213 struct mem_cgroup_lru_info info;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800214
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800215 /*
216 protect against reclaim related member.
217 */
218 spinlock_t reclaim_param_lock;
219
Balbir Singh6d61ef42009-01-07 18:08:06 -0800220 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200221 * While reclaiming in a hierarchy, we cache the last child we
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700222 * reclaimed from.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800223 */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700224 int last_scanned_child;
Balbir Singh18f59ea2009-01-07 18:08:07 -0800225 /*
226 * Should the accounting and control be hierarchical, per subtree?
227 */
228 bool use_hierarchy;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -0800229 atomic_t oom_lock;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800230 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800231
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800232 unsigned int swappiness;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -0700233 /* OOM-Killer disable */
234 int oom_kill_disable;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800235
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700236 /* set when res.limit == memsw.limit */
237 bool memsw_is_minimum;
238
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800239 /* protect arrays of thresholds */
240 struct mutex thresholds_lock;
241
242 /* thresholds for memory usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700243 struct mem_cgroup_thresholds thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700244
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800245 /* thresholds for mem+swap usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700246 struct mem_cgroup_thresholds memsw_thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700247
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700248 /* For oom notifier event fd */
249 struct list_head oom_notify;
250
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800251 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800252 * Should we move charges of a task when a task is moved into this
253 * mem_cgroup ? And what type of charges should we move ?
254 */
255 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800256 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800257 * percpu counter.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800258 */
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800259 struct mem_cgroup_stat_cpu *stat;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700260 /*
261 * used when a cpu is offlined or other synchronizations
262 * See mem_cgroup_read_stat().
263 */
264 struct mem_cgroup_stat_cpu nocpu_base;
265 spinlock_t pcp_counter_lock;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800266};
267
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800268/* Stuffs for move charges at task migration. */
269/*
270 * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a
271 * left-shifted bitmap of these types.
272 */
273enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800274 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700275 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800276 NR_MOVE_TYPE,
277};
278
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800279/* "mc" and its members are protected by cgroup_mutex */
280static struct move_charge_struct {
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -0800281 spinlock_t lock; /* for from, to */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800282 struct mem_cgroup *from;
283 struct mem_cgroup *to;
284 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800285 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800286 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800287 struct task_struct *moving_task; /* a task moving charges */
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -0800288 struct mm_struct *mm;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800289 wait_queue_head_t waitq; /* a waitq for other context */
290} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700291 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800292 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
293};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800294
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700295static bool move_anon(void)
296{
297 return test_bit(MOVE_CHARGE_TYPE_ANON,
298 &mc.to->move_charge_at_immigrate);
299}
300
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700301static bool move_file(void)
302{
303 return test_bit(MOVE_CHARGE_TYPE_FILE,
304 &mc.to->move_charge_at_immigrate);
305}
306
Balbir Singh4e416952009-09-23 15:56:39 -0700307/*
308 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
309 * limit reclaim to prevent infinite loops, if they ever occur.
310 */
311#define MEM_CGROUP_MAX_RECLAIM_LOOPS (100)
312#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2)
313
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800314enum charge_type {
315 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
316 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700317 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700318 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800319 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -0700320 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700321 NR_CHARGE_TYPE,
322};
323
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700324/* only for here (for easy reading.) */
325#define PCGF_CACHE (1UL << PCG_CACHE)
326#define PCGF_USED (1UL << PCG_USED)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700327#define PCGF_LOCK (1UL << PCG_LOCK)
Balbir Singh4b3bde42009-09-23 15:56:32 -0700328/* Not used, but added here for completeness */
329#define PCGF_ACCT (1UL << PCG_ACCT)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800330
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800331/* for encoding cft->private value on file */
332#define _MEM (0)
333#define _MEMSWAP (1)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700334#define _OOM_TYPE (2)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800335#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
336#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
337#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700338/* Used for OOM nofiier */
339#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800340
Balbir Singh75822b42009-09-23 15:56:38 -0700341/*
342 * Reclaim flags for mem_cgroup_hierarchical_reclaim
343 */
344#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
345#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
346#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
347#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
Balbir Singh4e416952009-09-23 15:56:39 -0700348#define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
349#define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
Balbir Singh75822b42009-09-23 15:56:38 -0700350
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800351static void mem_cgroup_get(struct mem_cgroup *mem);
352static void mem_cgroup_put(struct mem_cgroup *mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -0800353static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -0800354static void drain_all_stock_async(void);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800355
Balbir Singhf64c3f52009-09-23 15:56:37 -0700356static struct mem_cgroup_per_zone *
357mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
358{
359 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
360}
361
Wu Fengguangd3242362009-12-16 12:19:59 +0100362struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *mem)
363{
364 return &mem->css;
365}
366
Balbir Singhf64c3f52009-09-23 15:56:37 -0700367static struct mem_cgroup_per_zone *
368page_cgroup_zoneinfo(struct page_cgroup *pc)
369{
370 struct mem_cgroup *mem = pc->mem_cgroup;
371 int nid = page_cgroup_nid(pc);
372 int zid = page_cgroup_zid(pc);
373
374 if (!mem)
375 return NULL;
376
377 return mem_cgroup_zoneinfo(mem, nid, zid);
378}
379
380static struct mem_cgroup_tree_per_zone *
381soft_limit_tree_node_zone(int nid, int zid)
382{
383 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
384}
385
386static struct mem_cgroup_tree_per_zone *
387soft_limit_tree_from_page(struct page *page)
388{
389 int nid = page_to_nid(page);
390 int zid = page_zonenum(page);
391
392 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
393}
394
395static void
Balbir Singh4e416952009-09-23 15:56:39 -0700396__mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700397 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700398 struct mem_cgroup_tree_per_zone *mctz,
399 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700400{
401 struct rb_node **p = &mctz->rb_root.rb_node;
402 struct rb_node *parent = NULL;
403 struct mem_cgroup_per_zone *mz_node;
404
405 if (mz->on_tree)
406 return;
407
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700408 mz->usage_in_excess = new_usage_in_excess;
409 if (!mz->usage_in_excess)
410 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700411 while (*p) {
412 parent = *p;
413 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
414 tree_node);
415 if (mz->usage_in_excess < mz_node->usage_in_excess)
416 p = &(*p)->rb_left;
417 /*
418 * We can't avoid mem cgroups that are over their soft
419 * limit by the same amount
420 */
421 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
422 p = &(*p)->rb_right;
423 }
424 rb_link_node(&mz->tree_node, parent, p);
425 rb_insert_color(&mz->tree_node, &mctz->rb_root);
426 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700427}
428
429static void
430__mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
431 struct mem_cgroup_per_zone *mz,
432 struct mem_cgroup_tree_per_zone *mctz)
433{
434 if (!mz->on_tree)
435 return;
436 rb_erase(&mz->tree_node, &mctz->rb_root);
437 mz->on_tree = false;
438}
439
440static void
Balbir Singhf64c3f52009-09-23 15:56:37 -0700441mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
442 struct mem_cgroup_per_zone *mz,
443 struct mem_cgroup_tree_per_zone *mctz)
444{
445 spin_lock(&mctz->lock);
Balbir Singh4e416952009-09-23 15:56:39 -0700446 __mem_cgroup_remove_exceeded(mem, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700447 spin_unlock(&mctz->lock);
448}
449
Balbir Singhf64c3f52009-09-23 15:56:37 -0700450
451static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
452{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700453 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700454 struct mem_cgroup_per_zone *mz;
455 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700456 int nid = page_to_nid(page);
457 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700458 mctz = soft_limit_tree_from_page(page);
459
460 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700461 * Necessary to update all ancestors when hierarchy is used.
462 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700463 */
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700464 for (; mem; mem = parent_mem_cgroup(mem)) {
465 mz = mem_cgroup_zoneinfo(mem, nid, zid);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700466 excess = res_counter_soft_limit_excess(&mem->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700467 /*
468 * We have to update the tree if mz is on RB-tree or
469 * mem is over its softlimit.
470 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700471 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700472 spin_lock(&mctz->lock);
473 /* if on-tree, remove it */
474 if (mz->on_tree)
475 __mem_cgroup_remove_exceeded(mem, mz, mctz);
476 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700477 * Insert again. mz->usage_in_excess will be updated.
478 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700479 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700480 __mem_cgroup_insert_exceeded(mem, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700481 spin_unlock(&mctz->lock);
482 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700483 }
484}
485
486static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
487{
488 int node, zone;
489 struct mem_cgroup_per_zone *mz;
490 struct mem_cgroup_tree_per_zone *mctz;
491
492 for_each_node_state(node, N_POSSIBLE) {
493 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
494 mz = mem_cgroup_zoneinfo(mem, node, zone);
495 mctz = soft_limit_tree_node_zone(node, zone);
496 mem_cgroup_remove_exceeded(mem, mz, mctz);
497 }
498 }
499}
500
Balbir Singh4e416952009-09-23 15:56:39 -0700501static inline unsigned long mem_cgroup_get_excess(struct mem_cgroup *mem)
502{
503 return res_counter_soft_limit_excess(&mem->res) >> PAGE_SHIFT;
504}
505
506static struct mem_cgroup_per_zone *
507__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
508{
509 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700510 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700511
512retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700513 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700514 rightmost = rb_last(&mctz->rb_root);
515 if (!rightmost)
516 goto done; /* Nothing to reclaim from */
517
518 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
519 /*
520 * Remove the node now but someone else can add it back,
521 * we will to add it back at the end of reclaim to its correct
522 * position in the tree.
523 */
524 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
525 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
526 !css_tryget(&mz->mem->css))
527 goto retry;
528done:
529 return mz;
530}
531
532static struct mem_cgroup_per_zone *
533mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
534{
535 struct mem_cgroup_per_zone *mz;
536
537 spin_lock(&mctz->lock);
538 mz = __mem_cgroup_largest_soft_limit_node(mctz);
539 spin_unlock(&mctz->lock);
540 return mz;
541}
542
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700543/*
544 * Implementation Note: reading percpu statistics for memcg.
545 *
546 * Both of vmstat[] and percpu_counter has threshold and do periodic
547 * synchronization to implement "quick" read. There are trade-off between
548 * reading cost and precision of value. Then, we may have a chance to implement
549 * a periodic synchronizion of counter in memcg's counter.
550 *
551 * But this _read() function is used for user interface now. The user accounts
552 * memory usage by memory cgroup and he _always_ requires exact value because
553 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
554 * have to visit all online cpus and make sum. So, for now, unnecessary
555 * synchronization is not implemented. (just implemented for cpu hotplug)
556 *
557 * If there are kernel internal actions which can make use of some not-exact
558 * value, and reading all cpu value can be performance bottleneck in some
559 * common workload, threashold and synchonization as vmstat[] should be
560 * implemented.
561 */
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800562static s64 mem_cgroup_read_stat(struct mem_cgroup *mem,
563 enum mem_cgroup_stat_index idx)
564{
565 int cpu;
566 s64 val = 0;
567
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700568 get_online_cpus();
569 for_each_online_cpu(cpu)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800570 val += per_cpu(mem->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700571#ifdef CONFIG_HOTPLUG_CPU
572 spin_lock(&mem->pcp_counter_lock);
573 val += mem->nocpu_base.count[idx];
574 spin_unlock(&mem->pcp_counter_lock);
575#endif
576 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800577 return val;
578}
579
580static s64 mem_cgroup_local_usage(struct mem_cgroup *mem)
581{
582 s64 ret;
583
584 ret = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS);
585 ret += mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE);
586 return ret;
587}
588
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700589static void mem_cgroup_swap_statistics(struct mem_cgroup *mem,
590 bool charge)
591{
592 int val = (charge) ? 1 : -1;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800593 this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_SWAPOUT], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700594}
595
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700596static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
597 struct page_cgroup *pc,
598 bool charge)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800599{
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700600 int val = (charge) ? 1 : -1;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800601
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800602 preempt_disable();
603
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700604 if (PageCgroupCache(pc))
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800605 __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_CACHE], val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800606 else
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800607 __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_RSS], val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700608
609 if (charge)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800610 __this_cpu_inc(mem->stat->count[MEM_CGROUP_STAT_PGPGIN_COUNT]);
Balaji Rao55e462b2008-05-01 04:35:12 -0700611 else
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800612 __this_cpu_inc(mem->stat->count[MEM_CGROUP_STAT_PGPGOUT_COUNT]);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800613 __this_cpu_inc(mem->stat->count[MEM_CGROUP_EVENTS]);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800614
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800615 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800616}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800617
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700618static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700619 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800620{
621 int nid, zid;
622 struct mem_cgroup_per_zone *mz;
623 u64 total = 0;
624
625 for_each_online_node(nid)
626 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
627 mz = mem_cgroup_zoneinfo(mem, nid, zid);
628 total += MEM_CGROUP_ZSTAT(mz, idx);
629 }
630 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800631}
632
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800633static bool __memcg_event_check(struct mem_cgroup *mem, int event_mask_shift)
634{
635 s64 val;
636
637 val = this_cpu_read(mem->stat->count[MEM_CGROUP_EVENTS]);
638
639 return !(val & ((1 << event_mask_shift) - 1));
640}
641
642/*
643 * Check events in order.
644 *
645 */
646static void memcg_check_events(struct mem_cgroup *mem, struct page *page)
647{
648 /* threshold event is triggered in finer grain than soft limit */
649 if (unlikely(__memcg_event_check(mem, THRESHOLDS_EVENTS_THRESH))) {
650 mem_cgroup_threshold(mem);
651 if (unlikely(__memcg_event_check(mem, SOFTLIMIT_EVENTS_THRESH)))
652 mem_cgroup_update_tree(mem, page);
653 }
654}
655
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800656static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800657{
658 return container_of(cgroup_subsys_state(cont,
659 mem_cgroup_subsys_id), struct mem_cgroup,
660 css);
661}
662
Balbir Singhcf475ad2008-04-29 01:00:16 -0700663struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800664{
Balbir Singh31a78f22008-09-28 23:09:31 +0100665 /*
666 * mm_update_next_owner() may clear mm->owner to NULL
667 * if it races with swapoff, page migration, etc.
668 * So this can be called with p == NULL.
669 */
670 if (unlikely(!p))
671 return NULL;
672
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800673 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
674 struct mem_cgroup, css);
675}
676
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800677static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
678{
679 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700680
681 if (!mm)
682 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800683 /*
684 * Because we have no locks, mm->owner's may be being moved to other
685 * cgroup. We use css_tryget() here even if this looks
686 * pessimistic (rather than adding locks here).
687 */
688 rcu_read_lock();
689 do {
690 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
691 if (unlikely(!mem))
692 break;
693 } while (!css_tryget(&mem->css));
694 rcu_read_unlock();
695 return mem;
696}
697
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700698/* The caller has to guarantee "mem" exists before calling this */
699static struct mem_cgroup *mem_cgroup_start_loop(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700700{
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700701 struct cgroup_subsys_state *css;
702 int found;
703
704 if (!mem) /* ROOT cgroup has the smallest ID */
705 return root_mem_cgroup; /*css_put/get against root is ignored*/
706 if (!mem->use_hierarchy) {
707 if (css_tryget(&mem->css))
708 return mem;
709 return NULL;
710 }
711 rcu_read_lock();
712 /*
713 * searching a memory cgroup which has the smallest ID under given
714 * ROOT cgroup. (ID >= 1)
715 */
716 css = css_get_next(&mem_cgroup_subsys, 1, &mem->css, &found);
717 if (css && css_tryget(css))
718 mem = container_of(css, struct mem_cgroup, css);
719 else
720 mem = NULL;
721 rcu_read_unlock();
722 return mem;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700723}
724
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700725static struct mem_cgroup *mem_cgroup_get_next(struct mem_cgroup *iter,
726 struct mem_cgroup *root,
727 bool cond)
728{
729 int nextid = css_id(&iter->css) + 1;
730 int found;
731 int hierarchy_used;
732 struct cgroup_subsys_state *css;
733
734 hierarchy_used = iter->use_hierarchy;
735
736 css_put(&iter->css);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700737 /* If no ROOT, walk all, ignore hierarchy */
738 if (!cond || (root && !hierarchy_used))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700739 return NULL;
740
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700741 if (!root)
742 root = root_mem_cgroup;
743
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700744 do {
745 iter = NULL;
746 rcu_read_lock();
747
748 css = css_get_next(&mem_cgroup_subsys, nextid,
749 &root->css, &found);
750 if (css && css_tryget(css))
751 iter = container_of(css, struct mem_cgroup, css);
752 rcu_read_unlock();
753 /* If css is NULL, no more cgroups will be found */
754 nextid = found + 1;
755 } while (css && !iter);
756
757 return iter;
758}
759/*
760 * for_eacn_mem_cgroup_tree() for visiting all cgroup under tree. Please
761 * be careful that "break" loop is not allowed. We have reference count.
762 * Instead of that modify "cond" to be false and "continue" to exit the loop.
763 */
764#define for_each_mem_cgroup_tree_cond(iter, root, cond) \
765 for (iter = mem_cgroup_start_loop(root);\
766 iter != NULL;\
767 iter = mem_cgroup_get_next(iter, root, cond))
768
769#define for_each_mem_cgroup_tree(iter, root) \
770 for_each_mem_cgroup_tree_cond(iter, root, true)
771
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700772#define for_each_mem_cgroup_all(iter) \
773 for_each_mem_cgroup_tree_cond(iter, NULL, true)
774
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700775
Balbir Singh4b3bde42009-09-23 15:56:32 -0700776static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
777{
778 return (mem == root_mem_cgroup);
779}
780
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800781/*
782 * Following LRU functions are allowed to be used without PCG_LOCK.
783 * Operations are called by routine of global LRU independently from memcg.
784 * What we have to take care of here is validness of pc->mem_cgroup.
785 *
786 * Changes to pc->mem_cgroup happens when
787 * 1. charge
788 * 2. moving account
789 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
790 * It is added to LRU before charge.
791 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
792 * When moving account, the page is not on LRU. It's isolated.
793 */
794
795void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800796{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800797 struct page_cgroup *pc;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800798 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700799
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800800 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800801 return;
802 pc = lookup_page_cgroup(page);
803 /* can happen while we handle swapcache. */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700804 if (!TestClearPageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800805 return;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700806 VM_BUG_ON(!pc->mem_cgroup);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800807 /*
808 * We don't check PCG_USED bit. It's cleared when the "page" is finally
809 * removed from global LRU.
810 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800811 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700812 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700813 if (mem_cgroup_is_root(pc->mem_cgroup))
814 return;
815 VM_BUG_ON(list_empty(&pc->lru));
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800816 list_del_init(&pc->lru);
817 return;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800818}
819
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800820void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800821{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800822 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800823}
824
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800825void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800826{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800827 struct mem_cgroup_per_zone *mz;
828 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800829
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800830 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700831 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700832
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800833 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800834 /*
835 * Used bit is set without atomic ops but after smp_wmb().
836 * For making pc->mem_cgroup visible, insert smp_rmb() here.
837 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800838 smp_rmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -0700839 /* unused or root page is not rotated. */
840 if (!PageCgroupUsed(pc) || mem_cgroup_is_root(pc->mem_cgroup))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800841 return;
842 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700843 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800844}
845
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800846void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
847{
848 struct page_cgroup *pc;
849 struct mem_cgroup_per_zone *mz;
850
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800851 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800852 return;
853 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700854 VM_BUG_ON(PageCgroupAcctLRU(pc));
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800855 /*
856 * Used bit is set without atomic ops but after smp_wmb().
857 * For making pc->mem_cgroup visible, insert smp_rmb() here.
858 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800859 smp_rmb();
860 if (!PageCgroupUsed(pc))
861 return;
862
863 mz = page_cgroup_zoneinfo(pc);
864 MEM_CGROUP_ZSTAT(mz, lru) += 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700865 SetPageCgroupAcctLRU(pc);
866 if (mem_cgroup_is_root(pc->mem_cgroup))
867 return;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800868 list_add(&pc->lru, &mz->lists[lru]);
869}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800870
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800871/*
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800872 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
873 * lru because the page may.be reused after it's fully uncharged (because of
874 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
875 * it again. This function is only used to charge SwapCache. It's done under
876 * lock_page and expected that zone->lru_lock is never held.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800877 */
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800878static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800879{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800880 unsigned long flags;
881 struct zone *zone = page_zone(page);
882 struct page_cgroup *pc = lookup_page_cgroup(page);
883
884 spin_lock_irqsave(&zone->lru_lock, flags);
885 /*
886 * Forget old LRU when this page_cgroup is *not* used. This Used bit
887 * is guarded by lock_page() because the page is SwapCache.
888 */
889 if (!PageCgroupUsed(pc))
890 mem_cgroup_del_lru_list(page, page_lru(page));
891 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800892}
893
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800894static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
895{
896 unsigned long flags;
897 struct zone *zone = page_zone(page);
898 struct page_cgroup *pc = lookup_page_cgroup(page);
899
900 spin_lock_irqsave(&zone->lru_lock, flags);
901 /* link when the page is linked to LRU but page_cgroup isn't */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700902 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800903 mem_cgroup_add_lru_list(page, page_lru(page));
904 spin_unlock_irqrestore(&zone->lru_lock, flags);
905}
906
907
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800908void mem_cgroup_move_lists(struct page *page,
909 enum lru_list from, enum lru_list to)
910{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800911 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800912 return;
913 mem_cgroup_del_lru_list(page, from);
914 mem_cgroup_add_lru_list(page, to);
915}
916
David Rientjes4c4a2212008-02-07 00:14:06 -0800917int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
918{
919 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700920 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -0700921 struct task_struct *p;
David Rientjes4c4a2212008-02-07 00:14:06 -0800922
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -0700923 p = find_lock_task_mm(task);
924 if (!p)
925 return 0;
926 curr = try_get_mem_cgroup_from_mm(p->mm);
927 task_unlock(p);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700928 if (!curr)
929 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -0800930 /*
931 * We should check use_hierarchy of "mem" not "curr". Because checking
932 * use_hierarchy of "curr" here make this function true if hierarchy is
933 * enabled in "curr" and "curr" is a child of "mem" in *cgroup*
934 * hierarchy(even if use_hierarchy is disabled in "mem").
935 */
936 if (mem->use_hierarchy)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700937 ret = css_is_ancestor(&curr->css, &mem->css);
938 else
939 ret = (curr == mem);
940 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -0800941 return ret;
942}
943
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800944static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800945{
946 unsigned long active;
947 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800948 unsigned long gb;
949 unsigned long inactive_ratio;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800950
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700951 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
952 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800953
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800954 gb = (inactive + active) >> (30 - PAGE_SHIFT);
955 if (gb)
956 inactive_ratio = int_sqrt(10 * gb);
957 else
958 inactive_ratio = 1;
959
960 if (present_pages) {
961 present_pages[0] = inactive;
962 present_pages[1] = active;
963 }
964
965 return inactive_ratio;
966}
967
968int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
969{
970 unsigned long active;
971 unsigned long inactive;
972 unsigned long present_pages[2];
973 unsigned long inactive_ratio;
974
975 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
976
977 inactive = present_pages[0];
978 active = present_pages[1];
979
980 if (inactive * inactive_ratio < active)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800981 return 1;
982
983 return 0;
984}
985
Rik van Riel56e49d22009-06-16 15:32:28 -0700986int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
987{
988 unsigned long active;
989 unsigned long inactive;
990
991 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
992 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
993
994 return (active > inactive);
995}
996
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800997unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
998 struct zone *zone,
999 enum lru_list lru)
1000{
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -07001001 int nid = zone_to_nid(zone);
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -08001002 int zid = zone_idx(zone);
1003 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
1004
1005 return MEM_CGROUP_ZSTAT(mz, lru);
1006}
1007
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001008struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
1009 struct zone *zone)
1010{
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -07001011 int nid = zone_to_nid(zone);
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001012 int zid = zone_idx(zone);
1013 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
1014
1015 return &mz->reclaim_stat;
1016}
1017
1018struct zone_reclaim_stat *
1019mem_cgroup_get_reclaim_stat_from_page(struct page *page)
1020{
1021 struct page_cgroup *pc;
1022 struct mem_cgroup_per_zone *mz;
1023
1024 if (mem_cgroup_disabled())
1025 return NULL;
1026
1027 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -08001028 /*
1029 * Used bit is set without atomic ops but after smp_wmb().
1030 * For making pc->mem_cgroup visible, insert smp_rmb() here.
1031 */
1032 smp_rmb();
1033 if (!PageCgroupUsed(pc))
1034 return NULL;
1035
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001036 mz = page_cgroup_zoneinfo(pc);
1037 if (!mz)
1038 return NULL;
1039
1040 return &mz->reclaim_stat;
1041}
1042
Balbir Singh66e17072008-02-07 00:13:56 -08001043unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
1044 struct list_head *dst,
1045 unsigned long *scanned, int order,
1046 int mode, struct zone *z,
1047 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001048 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -08001049{
1050 unsigned long nr_taken = 0;
1051 struct page *page;
1052 unsigned long scan;
1053 LIST_HEAD(pc_list);
1054 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001055 struct page_cgroup *pc, *tmp;
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -07001056 int nid = zone_to_nid(z);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001057 int zid = zone_idx(z);
1058 struct mem_cgroup_per_zone *mz;
Johannes Weinerb7c46d12009-09-21 17:02:56 -07001059 int lru = LRU_FILE * file + active;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001060 int ret;
Balbir Singh66e17072008-02-07 00:13:56 -08001061
Balbir Singhcf475ad2008-04-29 01:00:16 -07001062 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001063 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001064 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -08001065
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001066 scan = 0;
1067 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -08001068 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001069 break;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001070
1071 page = pc->page;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001072 if (unlikely(!PageCgroupUsed(pc)))
1073 continue;
Hugh Dickins436c65412008-02-07 00:14:12 -08001074 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001075 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001076
Hugh Dickins436c65412008-02-07 00:14:12 -08001077 scan++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001078 ret = __isolate_lru_page(page, mode, file);
1079 switch (ret) {
1080 case 0:
Balbir Singh66e17072008-02-07 00:13:56 -08001081 list_move(&page->lru, dst);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001082 mem_cgroup_del_lru(page);
Balbir Singh66e17072008-02-07 00:13:56 -08001083 nr_taken++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001084 break;
1085 case -EBUSY:
1086 /* we don't affect global LRU but rotate in our LRU */
1087 mem_cgroup_rotate_lru_list(page, page_lru(page));
1088 break;
1089 default:
1090 break;
Balbir Singh66e17072008-02-07 00:13:56 -08001091 }
1092 }
1093
Balbir Singh66e17072008-02-07 00:13:56 -08001094 *scanned = scan;
KOSAKI Motohirocc8e9702010-08-09 17:19:57 -07001095
1096 trace_mm_vmscan_memcg_isolate(0, nr_to_scan, scan, nr_taken,
1097 0, 0, 0, mode);
1098
Balbir Singh66e17072008-02-07 00:13:56 -08001099 return nr_taken;
1100}
1101
Balbir Singh6d61ef42009-01-07 18:08:06 -08001102#define mem_cgroup_from_res_counter(counter, member) \
1103 container_of(counter, struct mem_cgroup, member)
1104
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -08001105static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
1106{
1107 if (do_swap_account) {
1108 if (res_counter_check_under_limit(&mem->res) &&
1109 res_counter_check_under_limit(&mem->memsw))
1110 return true;
1111 } else
1112 if (res_counter_check_under_limit(&mem->res))
1113 return true;
1114 return false;
1115}
1116
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001117static unsigned int get_swappiness(struct mem_cgroup *memcg)
1118{
1119 struct cgroup *cgrp = memcg->css.cgroup;
1120 unsigned int swappiness;
1121
1122 /* root ? */
1123 if (cgrp->parent == NULL)
1124 return vm_swappiness;
1125
1126 spin_lock(&memcg->reclaim_param_lock);
1127 swappiness = memcg->swappiness;
1128 spin_unlock(&memcg->reclaim_param_lock);
1129
1130 return swappiness;
1131}
1132
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001133static void mem_cgroup_start_move(struct mem_cgroup *mem)
1134{
1135 int cpu;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001136
1137 get_online_cpus();
1138 spin_lock(&mem->pcp_counter_lock);
1139 for_each_online_cpu(cpu)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001140 per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) += 1;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001141 mem->nocpu_base.count[MEM_CGROUP_ON_MOVE] += 1;
1142 spin_unlock(&mem->pcp_counter_lock);
1143 put_online_cpus();
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001144
1145 synchronize_rcu();
1146}
1147
1148static void mem_cgroup_end_move(struct mem_cgroup *mem)
1149{
1150 int cpu;
1151
1152 if (!mem)
1153 return;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001154 get_online_cpus();
1155 spin_lock(&mem->pcp_counter_lock);
1156 for_each_online_cpu(cpu)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001157 per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) -= 1;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001158 mem->nocpu_base.count[MEM_CGROUP_ON_MOVE] -= 1;
1159 spin_unlock(&mem->pcp_counter_lock);
1160 put_online_cpus();
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001161}
1162/*
1163 * 2 routines for checking "mem" is under move_account() or not.
1164 *
1165 * mem_cgroup_stealed() - checking a cgroup is mc.from or not. This is used
1166 * for avoiding race in accounting. If true,
1167 * pc->mem_cgroup may be overwritten.
1168 *
1169 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1170 * under hierarchy of moving cgroups. This is for
1171 * waiting at hith-memory prressure caused by "move".
1172 */
1173
1174static bool mem_cgroup_stealed(struct mem_cgroup *mem)
1175{
1176 VM_BUG_ON(!rcu_read_lock_held());
1177 return this_cpu_read(mem->stat->count[MEM_CGROUP_ON_MOVE]) > 0;
1178}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001179
1180static bool mem_cgroup_under_move(struct mem_cgroup *mem)
1181{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001182 struct mem_cgroup *from;
1183 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001184 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001185 /*
1186 * Unlike task_move routines, we access mc.to, mc.from not under
1187 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1188 */
1189 spin_lock(&mc.lock);
1190 from = mc.from;
1191 to = mc.to;
1192 if (!from)
1193 goto unlock;
1194 if (from == mem || to == mem
1195 || (mem->use_hierarchy && css_is_ancestor(&from->css, &mem->css))
1196 || (mem->use_hierarchy && css_is_ancestor(&to->css, &mem->css)))
1197 ret = true;
1198unlock:
1199 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001200 return ret;
1201}
1202
1203static bool mem_cgroup_wait_acct_move(struct mem_cgroup *mem)
1204{
1205 if (mc.moving_task && current != mc.moving_task) {
1206 if (mem_cgroup_under_move(mem)) {
1207 DEFINE_WAIT(wait);
1208 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1209 /* moving charge context might have finished. */
1210 if (mc.moving_task)
1211 schedule();
1212 finish_wait(&mc.waitq, &wait);
1213 return true;
1214 }
1215 }
1216 return false;
1217}
1218
Balbir Singhe2224322009-04-02 16:57:39 -07001219/**
Kirill A. Shutemov6a6135b2010-03-10 15:22:25 -08001220 * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode.
Balbir Singhe2224322009-04-02 16:57:39 -07001221 * @memcg: The memory cgroup that went over limit
1222 * @p: Task that is going to be killed
1223 *
1224 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1225 * enabled
1226 */
1227void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1228{
1229 struct cgroup *task_cgrp;
1230 struct cgroup *mem_cgrp;
1231 /*
1232 * Need a buffer in BSS, can't rely on allocations. The code relies
1233 * on the assumption that OOM is serialized for memory controller.
1234 * If this assumption is broken, revisit this code.
1235 */
1236 static char memcg_name[PATH_MAX];
1237 int ret;
1238
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001239 if (!memcg || !p)
Balbir Singhe2224322009-04-02 16:57:39 -07001240 return;
1241
1242
1243 rcu_read_lock();
1244
1245 mem_cgrp = memcg->css.cgroup;
1246 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1247
1248 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1249 if (ret < 0) {
1250 /*
1251 * Unfortunately, we are unable to convert to a useful name
1252 * But we'll still print out the usage information
1253 */
1254 rcu_read_unlock();
1255 goto done;
1256 }
1257 rcu_read_unlock();
1258
1259 printk(KERN_INFO "Task in %s killed", memcg_name);
1260
1261 rcu_read_lock();
1262 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1263 if (ret < 0) {
1264 rcu_read_unlock();
1265 goto done;
1266 }
1267 rcu_read_unlock();
1268
1269 /*
1270 * Continues from above, so we don't need an KERN_ level
1271 */
1272 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1273done:
1274
1275 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1276 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1277 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1278 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1279 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1280 "failcnt %llu\n",
1281 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1282 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1283 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1284}
1285
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001286/*
1287 * This function returns the number of memcg under hierarchy tree. Returns
1288 * 1(self count) if no children.
1289 */
1290static int mem_cgroup_count_children(struct mem_cgroup *mem)
1291{
1292 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001293 struct mem_cgroup *iter;
1294
1295 for_each_mem_cgroup_tree(iter, mem)
1296 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001297 return num;
1298}
1299
Balbir Singh6d61ef42009-01-07 18:08:06 -08001300/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001301 * Return the memory (and swap, if configured) limit for a memcg.
1302 */
1303u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
1304{
1305 u64 limit;
1306 u64 memsw;
1307
1308 limit = res_counter_read_u64(&memcg->res, RES_LIMIT) +
1309 total_swap_pages;
1310 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1311 /*
1312 * If memsw is finite and limits the amount of swap space available
1313 * to this memcg, return that limit.
1314 */
1315 return min(limit, memsw);
1316}
1317
1318/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001319 * Visit the first child (need not be the first child as per the ordering
1320 * of the cgroup list, since we track last_scanned_child) of @mem and use
1321 * that to reclaim free pages from.
1322 */
1323static struct mem_cgroup *
1324mem_cgroup_select_victim(struct mem_cgroup *root_mem)
1325{
1326 struct mem_cgroup *ret = NULL;
1327 struct cgroup_subsys_state *css;
1328 int nextid, found;
1329
1330 if (!root_mem->use_hierarchy) {
1331 css_get(&root_mem->css);
1332 ret = root_mem;
1333 }
1334
1335 while (!ret) {
1336 rcu_read_lock();
1337 nextid = root_mem->last_scanned_child + 1;
1338 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
1339 &found);
1340 if (css && css_tryget(css))
1341 ret = container_of(css, struct mem_cgroup, css);
1342
1343 rcu_read_unlock();
1344 /* Updates scanning parameter */
1345 spin_lock(&root_mem->reclaim_param_lock);
1346 if (!css) {
1347 /* this means start scan from ID:1 */
1348 root_mem->last_scanned_child = 0;
1349 } else
1350 root_mem->last_scanned_child = found;
1351 spin_unlock(&root_mem->reclaim_param_lock);
1352 }
1353
1354 return ret;
1355}
1356
1357/*
1358 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1359 * we reclaimed from, so that we don't end up penalizing one child extensively
1360 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001361 *
1362 * root_mem is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001363 *
1364 * We give up and return to the caller when we visit root_mem twice.
1365 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001366 *
1367 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001368 */
1369static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
Balbir Singh4e416952009-09-23 15:56:39 -07001370 struct zone *zone,
Balbir Singh75822b42009-09-23 15:56:38 -07001371 gfp_t gfp_mask,
1372 unsigned long reclaim_options)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001373{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001374 struct mem_cgroup *victim;
1375 int ret, total = 0;
1376 int loop = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001377 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1378 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
Balbir Singh4e416952009-09-23 15:56:39 -07001379 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
1380 unsigned long excess = mem_cgroup_get_excess(root_mem);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001381
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001382 /* If memsw_is_minimum==1, swap-out is of-no-use. */
1383 if (root_mem->memsw_is_minimum)
1384 noswap = true;
1385
Balbir Singh4e416952009-09-23 15:56:39 -07001386 while (1) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001387 victim = mem_cgroup_select_victim(root_mem);
Balbir Singh4e416952009-09-23 15:56:39 -07001388 if (victim == root_mem) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001389 loop++;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001390 if (loop >= 1)
1391 drain_all_stock_async();
Balbir Singh4e416952009-09-23 15:56:39 -07001392 if (loop >= 2) {
1393 /*
1394 * If we have not been able to reclaim
1395 * anything, it might because there are
1396 * no reclaimable pages under this hierarchy
1397 */
1398 if (!check_soft || !total) {
1399 css_put(&victim->css);
1400 break;
1401 }
1402 /*
1403 * We want to do more targetted reclaim.
1404 * excess >> 2 is not to excessive so as to
1405 * reclaim too much, nor too less that we keep
1406 * coming back to reclaim from this cgroup
1407 */
1408 if (total >= (excess >> 2) ||
1409 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1410 css_put(&victim->css);
1411 break;
1412 }
1413 }
1414 }
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08001415 if (!mem_cgroup_local_usage(victim)) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001416 /* this cgroup's local usage == 0 */
1417 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001418 continue;
1419 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001420 /* we use swappiness of local cgroup */
Balbir Singh4e416952009-09-23 15:56:39 -07001421 if (check_soft)
1422 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
KOSAKI Motohiro14fec792010-08-10 18:03:05 -07001423 noswap, get_swappiness(victim), zone);
Balbir Singh4e416952009-09-23 15:56:39 -07001424 else
1425 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
1426 noswap, get_swappiness(victim));
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001427 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001428 /*
1429 * At shrinking usage, we can't check we should stop here or
1430 * reclaim more. It's depends on callers. last_scanned_child
1431 * will work enough for keeping fairness under tree.
1432 */
1433 if (shrink)
1434 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001435 total += ret;
Balbir Singh4e416952009-09-23 15:56:39 -07001436 if (check_soft) {
1437 if (res_counter_check_under_soft_limit(&root_mem->res))
1438 return total;
1439 } else if (mem_cgroup_check_under_limit(root_mem))
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001440 return 1 + total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001441 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001442 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001443}
1444
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001445/*
1446 * Check OOM-Killer is already running under our hierarchy.
1447 * If someone is running, return false.
1448 */
1449static bool mem_cgroup_oom_lock(struct mem_cgroup *mem)
1450{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001451 int x, lock_count = 0;
1452 struct mem_cgroup *iter;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001453
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001454 for_each_mem_cgroup_tree(iter, mem) {
1455 x = atomic_inc_return(&iter->oom_lock);
1456 lock_count = max(x, lock_count);
1457 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001458
1459 if (lock_count == 1)
1460 return true;
1461 return false;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001462}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001463
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001464static int mem_cgroup_oom_unlock(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001465{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001466 struct mem_cgroup *iter;
1467
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001468 /*
1469 * When a new child is created while the hierarchy is under oom,
1470 * mem_cgroup_oom_lock() may not be called. We have to use
1471 * atomic_add_unless() here.
1472 */
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001473 for_each_mem_cgroup_tree(iter, mem)
1474 atomic_add_unless(&iter->oom_lock, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001475 return 0;
1476}
1477
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001478
1479static DEFINE_MUTEX(memcg_oom_mutex);
1480static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1481
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001482struct oom_wait_info {
1483 struct mem_cgroup *mem;
1484 wait_queue_t wait;
1485};
1486
1487static int memcg_oom_wake_function(wait_queue_t *wait,
1488 unsigned mode, int sync, void *arg)
1489{
1490 struct mem_cgroup *wake_mem = (struct mem_cgroup *)arg;
1491 struct oom_wait_info *oom_wait_info;
1492
1493 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
1494
1495 if (oom_wait_info->mem == wake_mem)
1496 goto wakeup;
1497 /* if no hierarchy, no match */
1498 if (!oom_wait_info->mem->use_hierarchy || !wake_mem->use_hierarchy)
1499 return 0;
1500 /*
1501 * Both of oom_wait_info->mem and wake_mem are stable under us.
1502 * Then we can use css_is_ancestor without taking care of RCU.
1503 */
1504 if (!css_is_ancestor(&oom_wait_info->mem->css, &wake_mem->css) &&
1505 !css_is_ancestor(&wake_mem->css, &oom_wait_info->mem->css))
1506 return 0;
1507
1508wakeup:
1509 return autoremove_wake_function(wait, mode, sync, arg);
1510}
1511
1512static void memcg_wakeup_oom(struct mem_cgroup *mem)
1513{
1514 /* for filtering, pass "mem" as argument. */
1515 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, mem);
1516}
1517
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001518static void memcg_oom_recover(struct mem_cgroup *mem)
1519{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001520 if (mem && atomic_read(&mem->oom_lock))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001521 memcg_wakeup_oom(mem);
1522}
1523
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001524/*
1525 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
1526 */
1527bool mem_cgroup_handle_oom(struct mem_cgroup *mem, gfp_t mask)
1528{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001529 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001530 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001531
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001532 owait.mem = mem;
1533 owait.wait.flags = 0;
1534 owait.wait.func = memcg_oom_wake_function;
1535 owait.wait.private = current;
1536 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001537 need_to_kill = true;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001538 /* At first, try to OOM lock hierarchy under mem.*/
1539 mutex_lock(&memcg_oom_mutex);
1540 locked = mem_cgroup_oom_lock(mem);
1541 /*
1542 * Even if signal_pending(), we can't quit charge() loop without
1543 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
1544 * under OOM is always welcomed, use TASK_KILLABLE here.
1545 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001546 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
1547 if (!locked || mem->oom_kill_disable)
1548 need_to_kill = false;
1549 if (locked)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07001550 mem_cgroup_oom_notify(mem);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001551 mutex_unlock(&memcg_oom_mutex);
1552
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001553 if (need_to_kill) {
1554 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001555 mem_cgroup_out_of_memory(mem, mask);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001556 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001557 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001558 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001559 }
1560 mutex_lock(&memcg_oom_mutex);
1561 mem_cgroup_oom_unlock(mem);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001562 memcg_wakeup_oom(mem);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001563 mutex_unlock(&memcg_oom_mutex);
1564
1565 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
1566 return false;
1567 /* Give chance to dying process */
1568 schedule_timeout(1);
1569 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001570}
1571
Balbir Singhd69b0422009-06-17 16:26:34 -07001572/*
1573 * Currently used to update mapped file statistics, but the routine can be
1574 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001575 *
1576 * Notes: Race condition
1577 *
1578 * We usually use page_cgroup_lock() for accessing page_cgroup member but
1579 * it tends to be costly. But considering some conditions, we doesn't need
1580 * to do so _always_.
1581 *
1582 * Considering "charge", lock_page_cgroup() is not required because all
1583 * file-stat operations happen after a page is attached to radix-tree. There
1584 * are no race with "charge".
1585 *
1586 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
1587 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
1588 * if there are race with "uncharge". Statistics itself is properly handled
1589 * by flags.
1590 *
1591 * Considering "move", this is an only case we see a race. To make the race
1592 * small, we check MEM_CGROUP_ON_MOVE percpu value and detect there are
1593 * possibility of race condition. If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07001594 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001595
1596static void mem_cgroup_update_file_stat(struct page *page, int idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07001597{
1598 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001599 struct page_cgroup *pc = lookup_page_cgroup(page);
1600 bool need_unlock = false;
Balbir Singhd69b0422009-06-17 16:26:34 -07001601
Balbir Singhd69b0422009-06-17 16:26:34 -07001602 if (unlikely(!pc))
1603 return;
1604
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001605 rcu_read_lock();
Balbir Singhd69b0422009-06-17 16:26:34 -07001606 mem = pc->mem_cgroup;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001607 if (unlikely(!mem || !PageCgroupUsed(pc)))
1608 goto out;
1609 /* pc->mem_cgroup is unstable ? */
1610 if (unlikely(mem_cgroup_stealed(mem))) {
1611 /* take a lock against to access pc->mem_cgroup */
1612 lock_page_cgroup(pc);
1613 need_unlock = true;
1614 mem = pc->mem_cgroup;
1615 if (!mem || !PageCgroupUsed(pc))
1616 goto out;
1617 }
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001618
1619 this_cpu_add(mem->stat->count[idx], val);
1620
1621 switch (idx) {
1622 case MEM_CGROUP_STAT_FILE_MAPPED:
1623 if (val > 0)
1624 SetPageCgroupFileMapped(pc);
1625 else if (!page_mapped(page))
KAMEZAWA Hiroyuki0c270f82010-10-27 15:33:39 -07001626 ClearPageCgroupFileMapped(pc);
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001627 break;
1628 default:
1629 BUG();
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07001630 }
Balbir Singhd69b0422009-06-17 16:26:34 -07001631
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001632out:
1633 if (unlikely(need_unlock))
1634 unlock_page_cgroup(pc);
1635 rcu_read_unlock();
1636 return;
Balbir Singhd69b0422009-06-17 16:26:34 -07001637}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001638
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001639void mem_cgroup_update_file_mapped(struct page *page, int val)
1640{
1641 mem_cgroup_update_file_stat(page, MEM_CGROUP_STAT_FILE_MAPPED, val);
1642}
1643
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001644/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001645 * size of first charge trial. "32" comes from vmscan.c's magic value.
1646 * TODO: maybe necessary to use big numbers in big irons.
1647 */
1648#define CHARGE_SIZE (32 * PAGE_SIZE)
1649struct memcg_stock_pcp {
1650 struct mem_cgroup *cached; /* this never be root cgroup */
1651 int charge;
1652 struct work_struct work;
1653};
1654static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
1655static atomic_t memcg_drain_count;
1656
1657/*
1658 * Try to consume stocked charge on this cpu. If success, PAGE_SIZE is consumed
1659 * from local stock and true is returned. If the stock is 0 or charges from a
1660 * cgroup which is not current target, returns false. This stock will be
1661 * refilled.
1662 */
1663static bool consume_stock(struct mem_cgroup *mem)
1664{
1665 struct memcg_stock_pcp *stock;
1666 bool ret = true;
1667
1668 stock = &get_cpu_var(memcg_stock);
1669 if (mem == stock->cached && stock->charge)
1670 stock->charge -= PAGE_SIZE;
1671 else /* need to call res_counter_charge */
1672 ret = false;
1673 put_cpu_var(memcg_stock);
1674 return ret;
1675}
1676
1677/*
1678 * Returns stocks cached in percpu to res_counter and reset cached information.
1679 */
1680static void drain_stock(struct memcg_stock_pcp *stock)
1681{
1682 struct mem_cgroup *old = stock->cached;
1683
1684 if (stock->charge) {
1685 res_counter_uncharge(&old->res, stock->charge);
1686 if (do_swap_account)
1687 res_counter_uncharge(&old->memsw, stock->charge);
1688 }
1689 stock->cached = NULL;
1690 stock->charge = 0;
1691}
1692
1693/*
1694 * This must be called under preempt disabled or must be called by
1695 * a thread which is pinned to local cpu.
1696 */
1697static void drain_local_stock(struct work_struct *dummy)
1698{
1699 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
1700 drain_stock(stock);
1701}
1702
1703/*
1704 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01001705 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001706 */
1707static void refill_stock(struct mem_cgroup *mem, int val)
1708{
1709 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
1710
1711 if (stock->cached != mem) { /* reset if necessary */
1712 drain_stock(stock);
1713 stock->cached = mem;
1714 }
1715 stock->charge += val;
1716 put_cpu_var(memcg_stock);
1717}
1718
1719/*
1720 * Tries to drain stocked charges in other cpus. This function is asynchronous
1721 * and just put a work per cpu for draining localy on each cpu. Caller can
1722 * expects some charges will be back to res_counter later but cannot wait for
1723 * it.
1724 */
1725static void drain_all_stock_async(void)
1726{
1727 int cpu;
1728 /* This function is for scheduling "drain" in asynchronous way.
1729 * The result of "drain" is not directly handled by callers. Then,
1730 * if someone is calling drain, we don't have to call drain more.
1731 * Anyway, WORK_STRUCT_PENDING check in queue_work_on() will catch if
1732 * there is a race. We just do loose check here.
1733 */
1734 if (atomic_read(&memcg_drain_count))
1735 return;
1736 /* Notify other cpus that system-wide "drain" is running */
1737 atomic_inc(&memcg_drain_count);
1738 get_online_cpus();
1739 for_each_online_cpu(cpu) {
1740 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
1741 schedule_work_on(cpu, &stock->work);
1742 }
1743 put_online_cpus();
1744 atomic_dec(&memcg_drain_count);
1745 /* We don't wait for flush_work */
1746}
1747
1748/* This is a synchronous drain interface. */
1749static void drain_all_stock_sync(void)
1750{
1751 /* called when force_empty is called */
1752 atomic_inc(&memcg_drain_count);
1753 schedule_on_each_cpu(drain_local_stock);
1754 atomic_dec(&memcg_drain_count);
1755}
1756
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001757/*
1758 * This function drains percpu counter value from DEAD cpu and
1759 * move it to local cpu. Note that this function can be preempted.
1760 */
1761static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *mem, int cpu)
1762{
1763 int i;
1764
1765 spin_lock(&mem->pcp_counter_lock);
1766 for (i = 0; i < MEM_CGROUP_STAT_DATA; i++) {
1767 s64 x = per_cpu(mem->stat->count[i], cpu);
1768
1769 per_cpu(mem->stat->count[i], cpu) = 0;
1770 mem->nocpu_base.count[i] += x;
1771 }
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001772 /* need to clear ON_MOVE value, works as a kind of lock. */
1773 per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) = 0;
1774 spin_unlock(&mem->pcp_counter_lock);
1775}
1776
1777static void synchronize_mem_cgroup_on_move(struct mem_cgroup *mem, int cpu)
1778{
1779 int idx = MEM_CGROUP_ON_MOVE;
1780
1781 spin_lock(&mem->pcp_counter_lock);
1782 per_cpu(mem->stat->count[idx], cpu) = mem->nocpu_base.count[idx];
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001783 spin_unlock(&mem->pcp_counter_lock);
1784}
1785
1786static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001787 unsigned long action,
1788 void *hcpu)
1789{
1790 int cpu = (unsigned long)hcpu;
1791 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001792 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001793
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001794 if ((action == CPU_ONLINE)) {
1795 for_each_mem_cgroup_all(iter)
1796 synchronize_mem_cgroup_on_move(iter, cpu);
1797 return NOTIFY_OK;
1798 }
1799
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001800 if ((action != CPU_DEAD) || action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001801 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001802
1803 for_each_mem_cgroup_all(iter)
1804 mem_cgroup_drain_pcp_counter(iter, cpu);
1805
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001806 stock = &per_cpu(memcg_stock, cpu);
1807 drain_stock(stock);
1808 return NOTIFY_OK;
1809}
1810
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001811
1812/* See __mem_cgroup_try_charge() for details */
1813enum {
1814 CHARGE_OK, /* success */
1815 CHARGE_RETRY, /* need to retry but retry is not bad */
1816 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
1817 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
1818 CHARGE_OOM_DIE, /* the current is killed because of OOM */
1819};
1820
1821static int __mem_cgroup_do_charge(struct mem_cgroup *mem, gfp_t gfp_mask,
1822 int csize, bool oom_check)
1823{
1824 struct mem_cgroup *mem_over_limit;
1825 struct res_counter *fail_res;
1826 unsigned long flags = 0;
1827 int ret;
1828
1829 ret = res_counter_charge(&mem->res, csize, &fail_res);
1830
1831 if (likely(!ret)) {
1832 if (!do_swap_account)
1833 return CHARGE_OK;
1834 ret = res_counter_charge(&mem->memsw, csize, &fail_res);
1835 if (likely(!ret))
1836 return CHARGE_OK;
1837
1838 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
1839 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
1840 } else
1841 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
1842
1843 if (csize > PAGE_SIZE) /* change csize and retry */
1844 return CHARGE_RETRY;
1845
1846 if (!(gfp_mask & __GFP_WAIT))
1847 return CHARGE_WOULDBLOCK;
1848
1849 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
1850 gfp_mask, flags);
1851 /*
1852 * try_to_free_mem_cgroup_pages() might not give us a full
1853 * picture of reclaim. Some pages are reclaimed and might be
1854 * moved to swap cache or just unmapped from the cgroup.
1855 * Check the limit again to see if the reclaim reduced the
1856 * current usage of the cgroup before giving up
1857 */
1858 if (ret || mem_cgroup_check_under_limit(mem_over_limit))
1859 return CHARGE_RETRY;
1860
1861 /*
1862 * At task move, charge accounts can be doubly counted. So, it's
1863 * better to wait until the end of task_move if something is going on.
1864 */
1865 if (mem_cgroup_wait_acct_move(mem_over_limit))
1866 return CHARGE_RETRY;
1867
1868 /* If we don't need to call oom-killer at el, return immediately */
1869 if (!oom_check)
1870 return CHARGE_NOMEM;
1871 /* check OOM */
1872 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask))
1873 return CHARGE_OOM_DIE;
1874
1875 return CHARGE_RETRY;
1876}
1877
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001878/*
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001879 * Unlike exported interface, "oom" parameter is added. if oom==true,
1880 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001881 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001882static int __mem_cgroup_try_charge(struct mm_struct *mm,
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001883 gfp_t gfp_mask, struct mem_cgroup **memcg, bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001884{
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001885 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
1886 struct mem_cgroup *mem = NULL;
1887 int ret;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001888 int csize = CHARGE_SIZE;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001889
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001890 /*
1891 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
1892 * in system level. So, allow to go ahead dying process in addition to
1893 * MEMDIE process.
1894 */
1895 if (unlikely(test_thread_flag(TIF_MEMDIE)
1896 || fatal_signal_pending(current)))
1897 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001898
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001899 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08001900 * We always charge the cgroup the mm_struct belongs to.
1901 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001902 * thread group leader migrates. It's possible that mm is not
1903 * set, if so charge the init_mm (happens for pagecache usage).
1904 */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001905 if (!*memcg && !mm)
1906 goto bypass;
1907again:
1908 if (*memcg) { /* css should be a valid one */
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001909 mem = *memcg;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001910 VM_BUG_ON(css_is_removed(&mem->css));
1911 if (mem_cgroup_is_root(mem))
1912 goto done;
1913 if (consume_stock(mem))
1914 goto done;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001915 css_get(&mem->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001916 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001917 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001918
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001919 rcu_read_lock();
1920 p = rcu_dereference(mm->owner);
1921 VM_BUG_ON(!p);
1922 /*
1923 * because we don't have task_lock(), "p" can exit while
1924 * we're here. In that case, "mem" can point to root
1925 * cgroup but never be NULL. (and task_struct itself is freed
1926 * by RCU, cgroup itself is RCU safe.) Then, we have small
1927 * risk here to get wrong cgroup. But such kind of mis-account
1928 * by race always happens because we don't have cgroup_mutex().
1929 * It's overkill and we allow that small race, here.
1930 */
1931 mem = mem_cgroup_from_task(p);
1932 VM_BUG_ON(!mem);
1933 if (mem_cgroup_is_root(mem)) {
1934 rcu_read_unlock();
1935 goto done;
1936 }
1937 if (consume_stock(mem)) {
1938 /*
1939 * It seems dagerous to access memcg without css_get().
1940 * But considering how consume_stok works, it's not
1941 * necessary. If consume_stock success, some charges
1942 * from this memcg are cached on this cpu. So, we
1943 * don't need to call css_get()/css_tryget() before
1944 * calling consume_stock().
1945 */
1946 rcu_read_unlock();
1947 goto done;
1948 }
1949 /* after here, we may be blocked. we need to get refcnt */
1950 if (!css_tryget(&mem->css)) {
1951 rcu_read_unlock();
1952 goto again;
1953 }
1954 rcu_read_unlock();
1955 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001956
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001957 do {
1958 bool oom_check;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001959
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001960 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001961 if (fatal_signal_pending(current)) {
1962 css_put(&mem->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001963 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001964 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001965
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001966 oom_check = false;
1967 if (oom && !nr_oom_retries) {
1968 oom_check = true;
1969 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
1970 }
Balbir Singh6d61ef42009-01-07 18:08:06 -08001971
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001972 ret = __mem_cgroup_do_charge(mem, gfp_mask, csize, oom_check);
1973
1974 switch (ret) {
1975 case CHARGE_OK:
1976 break;
1977 case CHARGE_RETRY: /* not in OOM situation but retry */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001978 csize = PAGE_SIZE;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001979 css_put(&mem->css);
1980 mem = NULL;
1981 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001982 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001983 css_put(&mem->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001984 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001985 case CHARGE_NOMEM: /* OOM routine works */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001986 if (!oom) {
1987 css_put(&mem->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001988 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001989 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001990 /* If oom, we never return -ENOMEM */
1991 nr_oom_retries--;
1992 break;
1993 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001994 css_put(&mem->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001995 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08001996 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001997 } while (ret != CHARGE_OK);
1998
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001999 if (csize > PAGE_SIZE)
2000 refill_stock(mem, csize - PAGE_SIZE);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002001 css_put(&mem->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002002done:
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002003 *memcg = mem;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002004 return 0;
2005nomem:
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002006 *memcg = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002007 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002008bypass:
2009 *memcg = NULL;
2010 return 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002011}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002012
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002013/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002014 * Somemtimes we have to undo a charge we got by try_charge().
2015 * This function is for that and do uncharge, put css's refcnt.
2016 * gotten by try_charge().
2017 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002018static void __mem_cgroup_cancel_charge(struct mem_cgroup *mem,
2019 unsigned long count)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002020{
2021 if (!mem_cgroup_is_root(mem)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002022 res_counter_uncharge(&mem->res, PAGE_SIZE * count);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002023 if (do_swap_account)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002024 res_counter_uncharge(&mem->memsw, PAGE_SIZE * count);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002025 }
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002026}
2027
2028static void mem_cgroup_cancel_charge(struct mem_cgroup *mem)
2029{
2030 __mem_cgroup_cancel_charge(mem, 1);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002031}
2032
2033/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002034 * A helper function to get mem_cgroup from ID. must be called under
2035 * rcu_read_lock(). The caller must check css_is_removed() or some if
2036 * it's concern. (dropping refcnt from swap can be called against removed
2037 * memcg.)
2038 */
2039static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2040{
2041 struct cgroup_subsys_state *css;
2042
2043 /* ID 0 is unused ID */
2044 if (!id)
2045 return NULL;
2046 css = css_lookup(&mem_cgroup_subsys, id);
2047 if (!css)
2048 return NULL;
2049 return container_of(css, struct mem_cgroup, css);
2050}
2051
Wu Fengguange42d9d52009-12-16 12:19:59 +01002052struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002053{
Wu Fengguange42d9d52009-12-16 12:19:59 +01002054 struct mem_cgroup *mem = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002055 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002056 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002057 swp_entry_t ent;
2058
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002059 VM_BUG_ON(!PageLocked(page));
2060
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002061 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002062 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002063 if (PageCgroupUsed(pc)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002064 mem = pc->mem_cgroup;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002065 if (mem && !css_tryget(&mem->css))
2066 mem = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002067 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002068 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002069 id = lookup_swap_cgroup(ent);
2070 rcu_read_lock();
2071 mem = mem_cgroup_lookup(id);
2072 if (mem && !css_tryget(&mem->css))
2073 mem = NULL;
2074 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002075 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002076 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002077 return mem;
2078}
2079
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002080/*
Daisuke Nishimuraa5e924f2009-01-07 18:08:28 -08002081 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002082 * USED state. If already USED, uncharge and return.
2083 */
2084
2085static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
2086 struct page_cgroup *pc,
2087 enum charge_type ctype)
2088{
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002089 /* try_charge() can return NULL to *memcg, taking care of it. */
2090 if (!mem)
2091 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002092
2093 lock_page_cgroup(pc);
2094 if (unlikely(PageCgroupUsed(pc))) {
2095 unlock_page_cgroup(pc);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002096 mem_cgroup_cancel_charge(mem);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002097 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002098 }
Balbir Singh4b3bde42009-09-23 15:56:32 -07002099
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002100 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002101 /*
2102 * We access a page_cgroup asynchronously without lock_page_cgroup().
2103 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2104 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2105 * before USED bit, we need memory barrier here.
2106 * See mem_cgroup_add_lru_list(), etc.
2107 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002108 smp_wmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -07002109 switch (ctype) {
2110 case MEM_CGROUP_CHARGE_TYPE_CACHE:
2111 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
2112 SetPageCgroupCache(pc);
2113 SetPageCgroupUsed(pc);
2114 break;
2115 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
2116 ClearPageCgroupCache(pc);
2117 SetPageCgroupUsed(pc);
2118 break;
2119 default:
2120 break;
2121 }
Hugh Dickins3be91272008-02-07 00:14:19 -08002122
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002123 mem_cgroup_charge_statistics(mem, pc, true);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002124
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002125 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002126 /*
2127 * "charge_statistics" updated event counter. Then, check it.
2128 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2129 * if they exceeds softlimit.
2130 */
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002131 memcg_check_events(mem, pc->page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002132}
2133
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002134/**
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002135 * __mem_cgroup_move_account - move account of the page
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002136 * @pc: page_cgroup of the page.
2137 * @from: mem_cgroup which the page is moved from.
2138 * @to: mem_cgroup which the page is moved to. @from != @to.
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002139 * @uncharge: whether we should call uncharge and css_put against @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002140 *
2141 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002142 * - page is not on LRU (isolate_page() is useful.)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002143 * - the pc is locked, used, and ->mem_cgroup points to @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002144 *
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002145 * This function doesn't do "charge" nor css_get to new cgroup. It should be
2146 * done by a caller(__mem_cgroup_try_charge would be usefull). If @uncharge is
2147 * true, this function does "uncharge" from old cgroup, but it doesn't if
2148 * @uncharge is false, so a caller should do "uncharge".
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002149 */
2150
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002151static void __mem_cgroup_move_account(struct page_cgroup *pc,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002152 struct mem_cgroup *from, struct mem_cgroup *to, bool uncharge)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002153{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002154 VM_BUG_ON(from == to);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002155 VM_BUG_ON(PageLRU(pc->page));
Kirill A. Shutemov112bc2e2010-11-24 12:56:58 -08002156 VM_BUG_ON(!page_is_cgroup_locked(pc));
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002157 VM_BUG_ON(!PageCgroupUsed(pc));
2158 VM_BUG_ON(pc->mem_cgroup != from);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002159
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002160 if (PageCgroupFileMapped(pc)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08002161 /* Update mapped_file data for mem_cgroup */
2162 preempt_disable();
2163 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2164 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2165 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07002166 }
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002167 mem_cgroup_charge_statistics(from, pc, false);
2168 if (uncharge)
2169 /* This is not "cancel", but cancel_charge does all we need. */
2170 mem_cgroup_cancel_charge(from);
Balbir Singhd69b0422009-06-17 16:26:34 -07002171
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002172 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002173 pc->mem_cgroup = to;
2174 mem_cgroup_charge_statistics(to, pc, true);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002175 /*
2176 * We charges against "to" which may not have any tasks. Then, "to"
2177 * can be under rmdir(). But in current implementation, caller of
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08002178 * this function is just force_empty() and move charge, so it's
2179 * garanteed that "to" is never removed. So, we don't check rmdir
2180 * status here.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002181 */
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002182}
2183
2184/*
2185 * check whether the @pc is valid for moving account and call
2186 * __mem_cgroup_move_account()
2187 */
2188static int mem_cgroup_move_account(struct page_cgroup *pc,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002189 struct mem_cgroup *from, struct mem_cgroup *to, bool uncharge)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002190{
2191 int ret = -EINVAL;
2192 lock_page_cgroup(pc);
2193 if (PageCgroupUsed(pc) && pc->mem_cgroup == from) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002194 __mem_cgroup_move_account(pc, from, to, uncharge);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002195 ret = 0;
2196 }
2197 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002198 /*
2199 * check events
2200 */
2201 memcg_check_events(to, pc->page);
2202 memcg_check_events(from, pc->page);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002203 return ret;
2204}
2205
2206/*
2207 * move charges to its parent.
2208 */
2209
2210static int mem_cgroup_move_parent(struct page_cgroup *pc,
2211 struct mem_cgroup *child,
2212 gfp_t gfp_mask)
2213{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002214 struct page *page = pc->page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002215 struct cgroup *cg = child->css.cgroup;
2216 struct cgroup *pcg = cg->parent;
2217 struct mem_cgroup *parent;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002218 int ret;
2219
2220 /* Is ROOT ? */
2221 if (!pcg)
2222 return -EINVAL;
2223
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002224 ret = -EBUSY;
2225 if (!get_page_unless_zero(page))
2226 goto out;
2227 if (isolate_lru_page(page))
2228 goto put;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002229
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002230 parent = mem_cgroup_from_cont(pcg);
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002231 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002232 if (ret || !parent)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002233 goto put_back;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002234
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002235 ret = mem_cgroup_move_account(pc, child, parent, true);
2236 if (ret)
2237 mem_cgroup_cancel_charge(parent);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002238put_back:
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002239 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002240put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08002241 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002242out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002243 return ret;
2244}
2245
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002246/*
2247 * Charge the memory controller for page usage.
2248 * Return
2249 * 0 if the charge was successful
2250 * < 0 if the cgroup is over its limit
2251 */
2252static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002253 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002254{
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002255 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002256 struct page_cgroup *pc;
2257 int ret;
2258
2259 pc = lookup_page_cgroup(page);
2260 /* can happen at boot */
2261 if (unlikely(!pc))
2262 return 0;
2263 prefetchw(pc);
2264
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002265 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002266 if (ret || !mem)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002267 return ret;
2268
2269 __mem_cgroup_commit_charge(mem, pc, ctype);
2270 return 0;
2271}
2272
2273int mem_cgroup_newpage_charge(struct page *page,
2274 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002275{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002276 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002277 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002278 if (PageCompound(page))
2279 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002280 /*
2281 * If already mapped, we don't have to account.
2282 * If page cache, page->mapping has address_space.
2283 * But page->mapping may have out-of-use anon_vma pointer,
2284 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
2285 * is NULL.
2286 */
2287 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
2288 return 0;
2289 if (unlikely(!mm))
2290 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002291 return mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002292 MEM_CGROUP_CHARGE_TYPE_MAPPED);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002293}
2294
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002295static void
2296__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2297 enum charge_type ctype);
2298
Balbir Singhe1a1cd52008-02-07 00:14:02 -08002299int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
2300 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08002301{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002302 int ret;
2303
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002304 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002305 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002306 if (PageCompound(page))
2307 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002308 /*
2309 * Corner case handling. This is called from add_to_page_cache()
2310 * in usual. But some FS (shmem) precharges this page before calling it
2311 * and call add_to_page_cache() with GFP_NOWAIT.
2312 *
2313 * For GFP_NOWAIT case, the page may be pre-charged before calling
2314 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
2315 * charge twice. (It works but has to pay a bit larger cost.)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002316 * And when the page is SwapCache, it should take swap information
2317 * into account. This is under lock_page() now.
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002318 */
2319 if (!(gfp_mask & __GFP_WAIT)) {
2320 struct page_cgroup *pc;
2321
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002322 pc = lookup_page_cgroup(page);
2323 if (!pc)
2324 return 0;
2325 lock_page_cgroup(pc);
2326 if (PageCgroupUsed(pc)) {
2327 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002328 return 0;
2329 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002330 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002331 }
2332
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002333 if (unlikely(!mm))
Balbir Singh8697d332008-02-07 00:13:59 -08002334 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002335
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -07002336 if (page_is_file_cache(page))
2337 return mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002338 MEM_CGROUP_CHARGE_TYPE_CACHE);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002339
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002340 /* shmem */
2341 if (PageSwapCache(page)) {
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002342 struct mem_cgroup *mem = NULL;
2343
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002344 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2345 if (!ret)
2346 __mem_cgroup_commit_charge_swapin(page, mem,
2347 MEM_CGROUP_CHARGE_TYPE_SHMEM);
2348 } else
2349 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002350 MEM_CGROUP_CHARGE_TYPE_SHMEM);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002351
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002352 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002353}
2354
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002355/*
2356 * While swap-in, try_charge -> commit or cancel, the page is locked.
2357 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02002358 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002359 * "commit()" or removed by "cancel()"
2360 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002361int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
2362 struct page *page,
2363 gfp_t mask, struct mem_cgroup **ptr)
2364{
2365 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002366 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002367
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002368 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002369 return 0;
2370
2371 if (!do_swap_account)
2372 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002373 /*
2374 * A racing thread's fault, or swapoff, may have already updated
Hugh Dickins407f9c82009-12-14 17:59:30 -08002375 * the pte, and even removed page from swap cache: in those cases
2376 * do_swap_page()'s pte_same() test will fail; but there's also a
2377 * KSM case which does need to charge the page.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002378 */
2379 if (!PageSwapCache(page))
Hugh Dickins407f9c82009-12-14 17:59:30 -08002380 goto charge_cur_mm;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002381 mem = try_get_mem_cgroup_from_page(page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002382 if (!mem)
2383 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002384 *ptr = mem;
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002385 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002386 css_put(&mem->css);
2387 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002388charge_cur_mm:
2389 if (unlikely(!mm))
2390 mm = &init_mm;
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002391 return __mem_cgroup_try_charge(mm, mask, ptr, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002392}
2393
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002394static void
2395__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2396 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002397{
2398 struct page_cgroup *pc;
2399
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002400 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002401 return;
2402 if (!ptr)
2403 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002404 cgroup_exclude_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002405 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08002406 mem_cgroup_lru_del_before_commit_swapcache(page);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002407 __mem_cgroup_commit_charge(ptr, pc, ctype);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08002408 mem_cgroup_lru_add_after_commit_swapcache(page);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002409 /*
2410 * Now swap is on-memory. This means this page may be
2411 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002412 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
2413 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
2414 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002415 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002416 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002417 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002418 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002419 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002420
2421 id = swap_cgroup_record(ent, 0);
2422 rcu_read_lock();
2423 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002424 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002425 /*
2426 * This recorded memcg can be obsolete one. So, avoid
2427 * calling css_tryget
2428 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002429 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002430 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002431 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002432 mem_cgroup_put(memcg);
2433 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002434 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002435 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002436 /*
2437 * At swapin, we may charge account against cgroup which has no tasks.
2438 * So, rmdir()->pre_destroy() can be called while we do this charge.
2439 * In that case, we need to call pre_destroy() again. check it here.
2440 */
2441 cgroup_release_and_wakeup_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002442}
2443
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002444void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
2445{
2446 __mem_cgroup_commit_charge_swapin(page, ptr,
2447 MEM_CGROUP_CHARGE_TYPE_MAPPED);
2448}
2449
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002450void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
2451{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002452 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002453 return;
2454 if (!mem)
2455 return;
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002456 mem_cgroup_cancel_charge(mem);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002457}
2458
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002459static void
2460__do_uncharge(struct mem_cgroup *mem, const enum charge_type ctype)
2461{
2462 struct memcg_batch_info *batch = NULL;
2463 bool uncharge_memsw = true;
2464 /* If swapout, usage of swap doesn't decrease */
2465 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2466 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002467
2468 batch = &current->memcg_batch;
2469 /*
2470 * In usual, we do css_get() when we remember memcg pointer.
2471 * But in this case, we keep res->usage until end of a series of
2472 * uncharges. Then, it's ok to ignore memcg's refcnt.
2473 */
2474 if (!batch->memcg)
2475 batch->memcg = mem;
2476 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002477 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
2478 * In those cases, all pages freed continously can be expected to be in
2479 * the same cgroup and we have chance to coalesce uncharges.
2480 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
2481 * because we want to do uncharge as soon as possible.
2482 */
2483
2484 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
2485 goto direct_uncharge;
2486
2487 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002488 * In typical case, batch->memcg == mem. This means we can
2489 * merge a series of uncharges to an uncharge of res_counter.
2490 * If not, we uncharge res_counter ony by one.
2491 */
2492 if (batch->memcg != mem)
2493 goto direct_uncharge;
2494 /* remember freed charge and uncharge it later */
2495 batch->bytes += PAGE_SIZE;
2496 if (uncharge_memsw)
2497 batch->memsw_bytes += PAGE_SIZE;
2498 return;
2499direct_uncharge:
2500 res_counter_uncharge(&mem->res, PAGE_SIZE);
2501 if (uncharge_memsw)
2502 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002503 if (unlikely(batch->memcg != mem))
2504 memcg_oom_recover(mem);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002505 return;
2506}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002507
Balbir Singh8697d332008-02-07 00:13:59 -08002508/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002509 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002510 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002511static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002512__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002513{
Hugh Dickins82895462008-03-04 14:29:08 -08002514 struct page_cgroup *pc;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002515 struct mem_cgroup *mem = NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002516
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002517 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002518 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07002519
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002520 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002521 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002522
Balbir Singh8697d332008-02-07 00:13:59 -08002523 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08002524 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08002525 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002526 pc = lookup_page_cgroup(page);
2527 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002528 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002529
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002530 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002531
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002532 mem = pc->mem_cgroup;
2533
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002534 if (!PageCgroupUsed(pc))
2535 goto unlock_out;
2536
2537 switch (ctype) {
2538 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07002539 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002540 /* See mem_cgroup_prepare_migration() */
2541 if (page_mapped(page) || PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002542 goto unlock_out;
2543 break;
2544 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
2545 if (!PageAnon(page)) { /* Shared memory */
2546 if (page->mapping && !page_is_file_cache(page))
2547 goto unlock_out;
2548 } else if (page_mapped(page)) /* Anon */
2549 goto unlock_out;
2550 break;
2551 default:
2552 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002553 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002554
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002555 mem_cgroup_charge_statistics(mem, pc, false);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002556
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002557 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08002558 /*
2559 * pc->mem_cgroup is not cleared here. It will be accessed when it's
2560 * freed from LRU. This is safe because uncharged page is expected not
2561 * to be reused (freed soon). Exception is SwapCache, it's handled by
2562 * special functions.
2563 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08002564
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002565 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002566 /*
2567 * even after unlock, we have mem->res.usage here and this memcg
2568 * will never be freed.
2569 */
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002570 memcg_check_events(mem, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002571 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
2572 mem_cgroup_swap_statistics(mem, true);
2573 mem_cgroup_get(mem);
2574 }
2575 if (!mem_cgroup_is_root(mem))
2576 __do_uncharge(mem, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002577
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002578 return mem;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002579
2580unlock_out:
2581 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002582 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08002583}
2584
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002585void mem_cgroup_uncharge_page(struct page *page)
2586{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002587 /* early check. */
2588 if (page_mapped(page))
2589 return;
2590 if (page->mapping && !PageAnon(page))
2591 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002592 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
2593}
2594
2595void mem_cgroup_uncharge_cache_page(struct page *page)
2596{
2597 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07002598 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002599 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
2600}
2601
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002602/*
2603 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
2604 * In that cases, pages are freed continuously and we can expect pages
2605 * are in the same memcg. All these calls itself limits the number of
2606 * pages freed at once, then uncharge_start/end() is called properly.
2607 * This may be called prural(2) times in a context,
2608 */
2609
2610void mem_cgroup_uncharge_start(void)
2611{
2612 current->memcg_batch.do_batch++;
2613 /* We can do nest. */
2614 if (current->memcg_batch.do_batch == 1) {
2615 current->memcg_batch.memcg = NULL;
2616 current->memcg_batch.bytes = 0;
2617 current->memcg_batch.memsw_bytes = 0;
2618 }
2619}
2620
2621void mem_cgroup_uncharge_end(void)
2622{
2623 struct memcg_batch_info *batch = &current->memcg_batch;
2624
2625 if (!batch->do_batch)
2626 return;
2627
2628 batch->do_batch--;
2629 if (batch->do_batch) /* If stacked, do nothing. */
2630 return;
2631
2632 if (!batch->memcg)
2633 return;
2634 /*
2635 * This "batch->memcg" is valid without any css_get/put etc...
2636 * bacause we hide charges behind us.
2637 */
2638 if (batch->bytes)
2639 res_counter_uncharge(&batch->memcg->res, batch->bytes);
2640 if (batch->memsw_bytes)
2641 res_counter_uncharge(&batch->memcg->memsw, batch->memsw_bytes);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002642 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002643 /* forget this pointer (for sanity check) */
2644 batch->memcg = NULL;
2645}
2646
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002647#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002648/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002649 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002650 * memcg information is recorded to swap_cgroup of "ent"
2651 */
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07002652void
2653mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002654{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002655 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07002656 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002657
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07002658 if (!swapout) /* this was a swap cache but the swap is unused ! */
2659 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
2660
2661 memcg = __mem_cgroup_uncharge_common(page, ctype);
2662
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002663 /*
2664 * record memcg information, if swapout && memcg != NULL,
2665 * mem_cgroup_get() was called in uncharge().
2666 */
2667 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002668 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002669}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002670#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002671
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002672#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2673/*
2674 * called from swap_entry_free(). remove record in swap_cgroup and
2675 * uncharge "memsw" account.
2676 */
2677void mem_cgroup_uncharge_swap(swp_entry_t ent)
2678{
2679 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002680 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002681
2682 if (!do_swap_account)
2683 return;
2684
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002685 id = swap_cgroup_record(ent, 0);
2686 rcu_read_lock();
2687 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002688 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002689 /*
2690 * We uncharge this because swap is freed.
2691 * This memcg can be obsolete one. We avoid calling css_tryget
2692 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002693 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002694 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002695 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002696 mem_cgroup_put(memcg);
2697 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002698 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002699}
Daisuke Nishimura02491442010-03-10 15:22:17 -08002700
2701/**
2702 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
2703 * @entry: swap entry to be moved
2704 * @from: mem_cgroup which the entry is moved from
2705 * @to: mem_cgroup which the entry is moved to
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002706 * @need_fixup: whether we should fixup res_counters and refcounts.
Daisuke Nishimura02491442010-03-10 15:22:17 -08002707 *
2708 * It succeeds only when the swap_cgroup's record for this entry is the same
2709 * as the mem_cgroup's id of @from.
2710 *
2711 * Returns 0 on success, -EINVAL on failure.
2712 *
2713 * The caller must have charged to @to, IOW, called res_counter_charge() about
2714 * both res and memsw, and called css_get().
2715 */
2716static int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002717 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08002718{
2719 unsigned short old_id, new_id;
2720
2721 old_id = css_id(&from->css);
2722 new_id = css_id(&to->css);
2723
2724 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08002725 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08002726 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002727 /*
2728 * This function is only called from task migration context now.
2729 * It postpones res_counter and refcount handling till the end
2730 * of task migration(mem_cgroup_clear_mc()) for performance
2731 * improvement. But we cannot postpone mem_cgroup_get(to)
2732 * because if the process that has been moved to @to does
2733 * swap-in, the refcount of @to might be decreased to 0.
2734 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08002735 mem_cgroup_get(to);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002736 if (need_fixup) {
2737 if (!mem_cgroup_is_root(from))
2738 res_counter_uncharge(&from->memsw, PAGE_SIZE);
2739 mem_cgroup_put(from);
2740 /*
2741 * we charged both to->res and to->memsw, so we should
2742 * uncharge to->res.
2743 */
2744 if (!mem_cgroup_is_root(to))
2745 res_counter_uncharge(&to->res, PAGE_SIZE);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002746 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08002747 return 0;
2748 }
2749 return -EINVAL;
2750}
2751#else
2752static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002753 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08002754{
2755 return -EINVAL;
2756}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002757#endif
2758
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002759/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002760 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
2761 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002762 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002763int mem_cgroup_prepare_migration(struct page *page,
2764 struct page *newpage, struct mem_cgroup **ptr)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002765{
2766 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002767 struct mem_cgroup *mem = NULL;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002768 enum charge_type ctype;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002769 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08002770
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002771 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07002772 return 0;
2773
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002774 pc = lookup_page_cgroup(page);
2775 lock_page_cgroup(pc);
2776 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002777 mem = pc->mem_cgroup;
2778 css_get(&mem->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002779 /*
2780 * At migrating an anonymous page, its mapcount goes down
2781 * to 0 and uncharge() will be called. But, even if it's fully
2782 * unmapped, migration may fail and this page has to be
2783 * charged again. We set MIGRATION flag here and delay uncharge
2784 * until end_migration() is called
2785 *
2786 * Corner Case Thinking
2787 * A)
2788 * When the old page was mapped as Anon and it's unmap-and-freed
2789 * while migration was ongoing.
2790 * If unmap finds the old page, uncharge() of it will be delayed
2791 * until end_migration(). If unmap finds a new page, it's
2792 * uncharged when it make mapcount to be 1->0. If unmap code
2793 * finds swap_migration_entry, the new page will not be mapped
2794 * and end_migration() will find it(mapcount==0).
2795 *
2796 * B)
2797 * When the old page was mapped but migraion fails, the kernel
2798 * remaps it. A charge for it is kept by MIGRATION flag even
2799 * if mapcount goes down to 0. We can do remap successfully
2800 * without charging it again.
2801 *
2802 * C)
2803 * The "old" page is under lock_page() until the end of
2804 * migration, so, the old page itself will not be swapped-out.
2805 * If the new page is swapped out before end_migraton, our
2806 * hook to usual swap-out path will catch the event.
2807 */
2808 if (PageAnon(page))
2809 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08002810 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002811 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002812 /*
2813 * If the page is not charged at this point,
2814 * we return here.
2815 */
2816 if (!mem)
2817 return 0;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002818
Andrea Arcangeli93d5c9b2010-04-23 13:17:39 -04002819 *ptr = mem;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002820 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, ptr, false);
2821 css_put(&mem->css);/* drop extra refcnt */
2822 if (ret || *ptr == NULL) {
2823 if (PageAnon(page)) {
2824 lock_page_cgroup(pc);
2825 ClearPageCgroupMigration(pc);
2826 unlock_page_cgroup(pc);
2827 /*
2828 * The old page may be fully unmapped while we kept it.
2829 */
2830 mem_cgroup_uncharge_page(page);
2831 }
2832 return -ENOMEM;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002833 }
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002834 /*
2835 * We charge new page before it's used/mapped. So, even if unlock_page()
2836 * is called before end_migration, we can catch all events on this new
2837 * page. In the case new page is migrated but not remapped, new page's
2838 * mapcount will be finally 0 and we call uncharge in end_migration().
2839 */
2840 pc = lookup_page_cgroup(newpage);
2841 if (PageAnon(page))
2842 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
2843 else if (page_is_file_cache(page))
2844 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
2845 else
2846 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
2847 __mem_cgroup_commit_charge(mem, pc, ctype);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002848 return ret;
2849}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08002850
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002851/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002852void mem_cgroup_end_migration(struct mem_cgroup *mem,
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002853 struct page *oldpage, struct page *newpage)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002854{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002855 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002856 struct page_cgroup *pc;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002857
2858 if (!mem)
2859 return;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002860 /* blocks rmdir() */
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002861 cgroup_exclude_rmdir(&mem->css);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002862 /* at migration success, oldpage->mapping is NULL. */
2863 if (oldpage->mapping) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002864 used = oldpage;
2865 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002866 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002867 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002868 unused = oldpage;
2869 }
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002870 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002871 * We disallowed uncharge of pages under migration because mapcount
2872 * of the page goes down to zero, temporarly.
2873 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002874 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002875 pc = lookup_page_cgroup(oldpage);
2876 lock_page_cgroup(pc);
2877 ClearPageCgroupMigration(pc);
2878 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002879
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002880 __mem_cgroup_uncharge_common(unused, MEM_CGROUP_CHARGE_TYPE_FORCE);
2881
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002882 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002883 * If a page is a file cache, radix-tree replacement is very atomic
2884 * and we can skip this check. When it was an Anon page, its mapcount
2885 * goes down to 0. But because we added MIGRATION flage, it's not
2886 * uncharged yet. There are several case but page->mapcount check
2887 * and USED bit check in mem_cgroup_uncharge_page() will do enough
2888 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002889 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002890 if (PageAnon(used))
2891 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002892 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002893 * At migration, we may charge account against cgroup which has no
2894 * tasks.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002895 * So, rmdir()->pre_destroy() can be called while we do this charge.
2896 * In that case, we need to call pre_destroy() again. check it here.
2897 */
2898 cgroup_release_and_wakeup_rmdir(&mem->css);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002899}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08002900
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002901/*
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002902 * A call to try to shrink memory usage on charge failure at shmem's swapin.
2903 * Calling hierarchical_reclaim is not enough because we should update
2904 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
2905 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
2906 * not from the memcg which this page would be charged to.
2907 * try_charge_swapin does all of these works properly.
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002908 */
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002909int mem_cgroup_shmem_charge_fallback(struct page *page,
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002910 struct mm_struct *mm,
2911 gfp_t gfp_mask)
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002912{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002913 struct mem_cgroup *mem = NULL;
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002914 int ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002915
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002916 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002917 return 0;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002918
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002919 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2920 if (!ret)
2921 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002922
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002923 return ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002924}
2925
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002926static DEFINE_MUTEX(set_limit_mutex);
2927
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08002928static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002929 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002930{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002931 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002932 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002933 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002934 int children = mem_cgroup_count_children(memcg);
2935 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002936 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002937
2938 /*
2939 * For keeping hierarchical_reclaim simple, how long we should retry
2940 * is depends on callers. We set our retry-count to be function
2941 * of # of children which we should visit in this loop.
2942 */
2943 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
2944
2945 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002946
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002947 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002948 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002949 if (signal_pending(current)) {
2950 ret = -EINTR;
2951 break;
2952 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002953 /*
2954 * Rather than hide all in some function, I do this in
2955 * open coded manner. You see what this really does.
2956 * We have to guarantee mem->res.limit < mem->memsw.limit.
2957 */
2958 mutex_lock(&set_limit_mutex);
2959 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2960 if (memswlimit < val) {
2961 ret = -EINVAL;
2962 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002963 break;
2964 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002965
2966 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2967 if (memlimit < val)
2968 enlarge = 1;
2969
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002970 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07002971 if (!ret) {
2972 if (memswlimit == val)
2973 memcg->memsw_is_minimum = true;
2974 else
2975 memcg->memsw_is_minimum = false;
2976 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002977 mutex_unlock(&set_limit_mutex);
2978
2979 if (!ret)
2980 break;
2981
Bob Liuaa20d482009-12-15 16:47:14 -08002982 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh4e416952009-09-23 15:56:39 -07002983 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002984 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
2985 /* Usage is reduced ? */
2986 if (curusage >= oldusage)
2987 retry_count--;
2988 else
2989 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002990 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002991 if (!ret && enlarge)
2992 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002993
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002994 return ret;
2995}
2996
Li Zefan338c8432009-06-17 16:27:15 -07002997static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
2998 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002999{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003000 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003001 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003002 int children = mem_cgroup_count_children(memcg);
3003 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003004 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003005
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003006 /* see mem_cgroup_resize_res_limit */
3007 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
3008 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003009 while (retry_count) {
3010 if (signal_pending(current)) {
3011 ret = -EINTR;
3012 break;
3013 }
3014 /*
3015 * Rather than hide all in some function, I do this in
3016 * open coded manner. You see what this really does.
3017 * We have to guarantee mem->res.limit < mem->memsw.limit.
3018 */
3019 mutex_lock(&set_limit_mutex);
3020 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3021 if (memlimit > val) {
3022 ret = -EINVAL;
3023 mutex_unlock(&set_limit_mutex);
3024 break;
3025 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003026 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3027 if (memswlimit < val)
3028 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003029 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003030 if (!ret) {
3031 if (memlimit == val)
3032 memcg->memsw_is_minimum = true;
3033 else
3034 memcg->memsw_is_minimum = false;
3035 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003036 mutex_unlock(&set_limit_mutex);
3037
3038 if (!ret)
3039 break;
3040
Balbir Singh4e416952009-09-23 15:56:39 -07003041 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh75822b42009-09-23 15:56:38 -07003042 MEM_CGROUP_RECLAIM_NOSWAP |
3043 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003044 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003045 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003046 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003047 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003048 else
3049 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003050 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003051 if (!ret && enlarge)
3052 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003053 return ret;
3054}
3055
Balbir Singh4e416952009-09-23 15:56:39 -07003056unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07003057 gfp_t gfp_mask)
Balbir Singh4e416952009-09-23 15:56:39 -07003058{
3059 unsigned long nr_reclaimed = 0;
3060 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
3061 unsigned long reclaimed;
3062 int loop = 0;
3063 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003064 unsigned long long excess;
Balbir Singh4e416952009-09-23 15:56:39 -07003065
3066 if (order > 0)
3067 return 0;
3068
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07003069 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
Balbir Singh4e416952009-09-23 15:56:39 -07003070 /*
3071 * This loop can run a while, specially if mem_cgroup's continuously
3072 * keep exceeding their soft limit and putting the system under
3073 * pressure
3074 */
3075 do {
3076 if (next_mz)
3077 mz = next_mz;
3078 else
3079 mz = mem_cgroup_largest_soft_limit_node(mctz);
3080 if (!mz)
3081 break;
3082
3083 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
3084 gfp_mask,
3085 MEM_CGROUP_RECLAIM_SOFT);
3086 nr_reclaimed += reclaimed;
3087 spin_lock(&mctz->lock);
3088
3089 /*
3090 * If we failed to reclaim anything from this memory cgroup
3091 * it is time to move on to the next cgroup
3092 */
3093 next_mz = NULL;
3094 if (!reclaimed) {
3095 do {
3096 /*
3097 * Loop until we find yet another one.
3098 *
3099 * By the time we get the soft_limit lock
3100 * again, someone might have aded the
3101 * group back on the RB tree. Iterate to
3102 * make sure we get a different mem.
3103 * mem_cgroup_largest_soft_limit_node returns
3104 * NULL if no other cgroup is present on
3105 * the tree
3106 */
3107 next_mz =
3108 __mem_cgroup_largest_soft_limit_node(mctz);
3109 if (next_mz == mz) {
3110 css_put(&next_mz->mem->css);
3111 next_mz = NULL;
3112 } else /* next_mz == NULL or other memcg */
3113 break;
3114 } while (1);
3115 }
Balbir Singh4e416952009-09-23 15:56:39 -07003116 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003117 excess = res_counter_soft_limit_excess(&mz->mem->res);
Balbir Singh4e416952009-09-23 15:56:39 -07003118 /*
3119 * One school of thought says that we should not add
3120 * back the node to the tree if reclaim returns 0.
3121 * But our reclaim could return 0, simply because due
3122 * to priority we are exposing a smaller subset of
3123 * memory to reclaim from. Consider this as a longer
3124 * term TODO.
3125 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003126 /* If excess == 0, no tree ops */
3127 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07003128 spin_unlock(&mctz->lock);
3129 css_put(&mz->mem->css);
3130 loop++;
3131 /*
3132 * Could not reclaim anything and there are no more
3133 * mem cgroups to try or we seem to be looping without
3134 * reclaiming anything.
3135 */
3136 if (!nr_reclaimed &&
3137 (next_mz == NULL ||
3138 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3139 break;
3140 } while (!nr_reclaimed);
3141 if (next_mz)
3142 css_put(&next_mz->mem->css);
3143 return nr_reclaimed;
3144}
3145
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003146/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003147 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003148 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
3149 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003150static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003151 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003152{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003153 struct zone *zone;
3154 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003155 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003156 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003157 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003158 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003159
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003160 zone = &NODE_DATA(node)->node_zones[zid];
3161 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07003162 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003163
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003164 loop = MEM_CGROUP_ZSTAT(mz, lru);
3165 /* give some margin against EBUSY etc...*/
3166 loop += 256;
3167 busy = NULL;
3168 while (loop--) {
3169 ret = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003170 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003171 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003172 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003173 break;
3174 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003175 pc = list_entry(list->prev, struct page_cgroup, lru);
3176 if (busy == pc) {
3177 list_move(&pc->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08003178 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003179 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003180 continue;
3181 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003182 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003183
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -08003184 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003185 if (ret == -ENOMEM)
3186 break;
3187
3188 if (ret == -EBUSY || ret == -EINVAL) {
3189 /* found lock contention or "pc" is obsolete. */
3190 busy = pc;
3191 cond_resched();
3192 } else
3193 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003194 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003195
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003196 if (!ret && !list_empty(list))
3197 return -EBUSY;
3198 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003199}
3200
3201/*
3202 * make mem_cgroup's charge to be 0 if there is no task.
3203 * This enables deleting this mem_cgroup.
3204 */
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003205static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003206{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003207 int ret;
3208 int node, zid, shrink;
3209 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003210 struct cgroup *cgrp = mem->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003211
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003212 css_get(&mem->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003213
3214 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003215 /* should free all ? */
3216 if (free_all)
3217 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003218move_account:
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003219 do {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003220 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003221 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003222 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003223 ret = -EINTR;
3224 if (signal_pending(current))
3225 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003226 /* This is for making all *used* pages to be on LRU. */
3227 lru_add_drain_all();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08003228 drain_all_stock_sync();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003229 ret = 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07003230 mem_cgroup_start_move(mem);
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08003231 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003232 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07003233 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003234 for_each_lru(l) {
3235 ret = mem_cgroup_force_empty_list(mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003236 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003237 if (ret)
3238 break;
3239 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003240 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003241 if (ret)
3242 break;
3243 }
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07003244 mem_cgroup_end_move(mem);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003245 memcg_oom_recover(mem);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003246 /* it seems parent cgroup doesn't have enough mem */
3247 if (ret == -ENOMEM)
3248 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003249 cond_resched();
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003250 /* "ret" should also be checked to ensure all lists are empty. */
3251 } while (mem->res.usage > 0 || ret);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003252out:
3253 css_put(&mem->css);
3254 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003255
3256try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003257 /* returns EBUSY if there is a task or if we come here twice. */
3258 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003259 ret = -EBUSY;
3260 goto out;
3261 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003262 /* we call try-to-free pages for make this cgroup empty */
3263 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003264 /* try to free all pages in this cgroup */
3265 shrink = 1;
3266 while (nr_retries && mem->res.usage > 0) {
3267 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003268
3269 if (signal_pending(current)) {
3270 ret = -EINTR;
3271 goto out;
3272 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003273 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
3274 false, get_swappiness(mem));
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003275 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003276 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003277 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02003278 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003279 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003280
3281 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003282 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003283 /* try move_account...there may be some *locked* pages. */
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003284 goto move_account;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003285}
3286
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003287int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
3288{
3289 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
3290}
3291
3292
Balbir Singh18f59ea2009-01-07 18:08:07 -08003293static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
3294{
3295 return mem_cgroup_from_cont(cont)->use_hierarchy;
3296}
3297
3298static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
3299 u64 val)
3300{
3301 int retval = 0;
3302 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
3303 struct cgroup *parent = cont->parent;
3304 struct mem_cgroup *parent_mem = NULL;
3305
3306 if (parent)
3307 parent_mem = mem_cgroup_from_cont(parent);
3308
3309 cgroup_lock();
3310 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003311 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08003312 * in the child subtrees. If it is unset, then the change can
3313 * occur, provided the current cgroup has no children.
3314 *
3315 * For the root cgroup, parent_mem is NULL, we allow value to be
3316 * set if there are no children.
3317 */
3318 if ((!parent_mem || !parent_mem->use_hierarchy) &&
3319 (val == 1 || val == 0)) {
3320 if (list_empty(&cont->children))
3321 mem->use_hierarchy = val;
3322 else
3323 retval = -EBUSY;
3324 } else
3325 retval = -EINVAL;
3326 cgroup_unlock();
3327
3328 return retval;
3329}
3330
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003331
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003332static u64 mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem,
3333 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003334{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003335 struct mem_cgroup *iter;
3336 s64 val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003337
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003338 /* each per cpu's value can be minus.Then, use s64 */
3339 for_each_mem_cgroup_tree(iter, mem)
3340 val += mem_cgroup_read_stat(iter, idx);
3341
3342 if (val < 0) /* race ? */
3343 val = 0;
3344 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003345}
3346
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003347static inline u64 mem_cgroup_usage(struct mem_cgroup *mem, bool swap)
3348{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003349 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003350
3351 if (!mem_cgroup_is_root(mem)) {
3352 if (!swap)
3353 return res_counter_read_u64(&mem->res, RES_USAGE);
3354 else
3355 return res_counter_read_u64(&mem->memsw, RES_USAGE);
3356 }
3357
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003358 val = mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_CACHE);
3359 val += mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003360
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003361 if (swap)
3362 val += mem_cgroup_get_recursive_idx_stat(mem,
3363 MEM_CGROUP_STAT_SWAPOUT);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003364
3365 return val << PAGE_SHIFT;
3366}
3367
Paul Menage2c3daa72008-04-29 00:59:58 -07003368static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003369{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003370 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003371 u64 val;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003372 int type, name;
3373
3374 type = MEMFILE_TYPE(cft->private);
3375 name = MEMFILE_ATTR(cft->private);
3376 switch (type) {
3377 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003378 if (name == RES_USAGE)
3379 val = mem_cgroup_usage(mem, false);
3380 else
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003381 val = res_counter_read_u64(&mem->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003382 break;
3383 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003384 if (name == RES_USAGE)
3385 val = mem_cgroup_usage(mem, true);
3386 else
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003387 val = res_counter_read_u64(&mem->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003388 break;
3389 default:
3390 BUG();
3391 break;
3392 }
3393 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003394}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003395/*
3396 * The user of this function is...
3397 * RES_LIMIT.
3398 */
Paul Menage856c13a2008-07-25 01:47:04 -07003399static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
3400 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003401{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003402 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003403 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003404 unsigned long long val;
3405 int ret;
3406
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003407 type = MEMFILE_TYPE(cft->private);
3408 name = MEMFILE_ATTR(cft->private);
3409 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003410 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07003411 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
3412 ret = -EINVAL;
3413 break;
3414 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003415 /* This function does all necessary parse...reuse it */
3416 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003417 if (ret)
3418 break;
3419 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003420 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003421 else
3422 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003423 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07003424 case RES_SOFT_LIMIT:
3425 ret = res_counter_memparse_write_strategy(buffer, &val);
3426 if (ret)
3427 break;
3428 /*
3429 * For memsw, soft limits are hard to implement in terms
3430 * of semantics, for now, we support soft limits for
3431 * control without swap
3432 */
3433 if (type == _MEM)
3434 ret = res_counter_set_soft_limit(&memcg->res, val);
3435 else
3436 ret = -EINVAL;
3437 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003438 default:
3439 ret = -EINVAL; /* should be BUG() ? */
3440 break;
3441 }
3442 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003443}
3444
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08003445static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
3446 unsigned long long *mem_limit, unsigned long long *memsw_limit)
3447{
3448 struct cgroup *cgroup;
3449 unsigned long long min_limit, min_memsw_limit, tmp;
3450
3451 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3452 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3453 cgroup = memcg->css.cgroup;
3454 if (!memcg->use_hierarchy)
3455 goto out;
3456
3457 while (cgroup->parent) {
3458 cgroup = cgroup->parent;
3459 memcg = mem_cgroup_from_cont(cgroup);
3460 if (!memcg->use_hierarchy)
3461 break;
3462 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
3463 min_limit = min(min_limit, tmp);
3464 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3465 min_memsw_limit = min(min_memsw_limit, tmp);
3466 }
3467out:
3468 *mem_limit = min_limit;
3469 *memsw_limit = min_memsw_limit;
3470 return;
3471}
3472
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003473static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003474{
3475 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003476 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003477
3478 mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003479 type = MEMFILE_TYPE(event);
3480 name = MEMFILE_ATTR(event);
3481 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003482 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003483 if (type == _MEM)
3484 res_counter_reset_max(&mem->res);
3485 else
3486 res_counter_reset_max(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003487 break;
3488 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003489 if (type == _MEM)
3490 res_counter_reset_failcnt(&mem->res);
3491 else
3492 res_counter_reset_failcnt(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003493 break;
3494 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07003495
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07003496 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003497}
3498
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003499static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
3500 struct cftype *cft)
3501{
3502 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
3503}
3504
Daisuke Nishimura02491442010-03-10 15:22:17 -08003505#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003506static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
3507 struct cftype *cft, u64 val)
3508{
3509 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3510
3511 if (val >= (1 << NR_MOVE_TYPE))
3512 return -EINVAL;
3513 /*
3514 * We check this value several times in both in can_attach() and
3515 * attach(), so we need cgroup lock to prevent this value from being
3516 * inconsistent.
3517 */
3518 cgroup_lock();
3519 mem->move_charge_at_immigrate = val;
3520 cgroup_unlock();
3521
3522 return 0;
3523}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003524#else
3525static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
3526 struct cftype *cft, u64 val)
3527{
3528 return -ENOSYS;
3529}
3530#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003531
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003532
3533/* For read statistics */
3534enum {
3535 MCS_CACHE,
3536 MCS_RSS,
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08003537 MCS_FILE_MAPPED,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003538 MCS_PGPGIN,
3539 MCS_PGPGOUT,
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003540 MCS_SWAP,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003541 MCS_INACTIVE_ANON,
3542 MCS_ACTIVE_ANON,
3543 MCS_INACTIVE_FILE,
3544 MCS_ACTIVE_FILE,
3545 MCS_UNEVICTABLE,
3546 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003547};
3548
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003549struct mcs_total_stat {
3550 s64 stat[NR_MCS_STAT];
3551};
3552
3553struct {
3554 char *local_name;
3555 char *total_name;
3556} memcg_stat_strings[NR_MCS_STAT] = {
3557 {"cache", "total_cache"},
3558 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07003559 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003560 {"pgpgin", "total_pgpgin"},
3561 {"pgpgout", "total_pgpgout"},
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003562 {"swap", "total_swap"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003563 {"inactive_anon", "total_inactive_anon"},
3564 {"active_anon", "total_active_anon"},
3565 {"inactive_file", "total_inactive_file"},
3566 {"active_file", "total_active_file"},
3567 {"unevictable", "total_unevictable"}
3568};
3569
3570
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003571static void
3572mem_cgroup_get_local_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003573{
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003574 s64 val;
3575
3576 /* per cpu stat */
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003577 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003578 s->stat[MCS_CACHE] += val * PAGE_SIZE;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003579 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003580 s->stat[MCS_RSS] += val * PAGE_SIZE;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003581 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_FILE_MAPPED);
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08003582 s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003583 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_PGPGIN_COUNT);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003584 s->stat[MCS_PGPGIN] += val;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003585 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_PGPGOUT_COUNT);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003586 s->stat[MCS_PGPGOUT] += val;
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003587 if (do_swap_account) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003588 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_SWAPOUT);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003589 s->stat[MCS_SWAP] += val * PAGE_SIZE;
3590 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003591
3592 /* per zone stat */
3593 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
3594 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
3595 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
3596 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
3597 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
3598 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
3599 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
3600 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
3601 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
3602 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003603}
3604
3605static void
3606mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
3607{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003608 struct mem_cgroup *iter;
3609
3610 for_each_mem_cgroup_tree(iter, mem)
3611 mem_cgroup_get_local_stat(iter, s);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003612}
3613
Paul Menagec64745c2008-04-29 01:00:02 -07003614static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
3615 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003616{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003617 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003618 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003619 int i;
3620
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003621 memset(&mystat, 0, sizeof(mystat));
3622 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003623
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003624 for (i = 0; i < NR_MCS_STAT; i++) {
3625 if (i == MCS_SWAP && !do_swap_account)
3626 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003627 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003628 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003629
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003630 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08003631 {
3632 unsigned long long limit, memsw_limit;
3633 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
3634 cb->fill(cb, "hierarchical_memory_limit", limit);
3635 if (do_swap_account)
3636 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
3637 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003638
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003639 memset(&mystat, 0, sizeof(mystat));
3640 mem_cgroup_get_total_stat(mem_cont, &mystat);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003641 for (i = 0; i < NR_MCS_STAT; i++) {
3642 if (i == MCS_SWAP && !do_swap_account)
3643 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003644 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003645 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003646
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003647#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08003648 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003649
3650 {
3651 int nid, zid;
3652 struct mem_cgroup_per_zone *mz;
3653 unsigned long recent_rotated[2] = {0, 0};
3654 unsigned long recent_scanned[2] = {0, 0};
3655
3656 for_each_online_node(nid)
3657 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
3658 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
3659
3660 recent_rotated[0] +=
3661 mz->reclaim_stat.recent_rotated[0];
3662 recent_rotated[1] +=
3663 mz->reclaim_stat.recent_rotated[1];
3664 recent_scanned[0] +=
3665 mz->reclaim_stat.recent_scanned[0];
3666 recent_scanned[1] +=
3667 mz->reclaim_stat.recent_scanned[1];
3668 }
3669 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
3670 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
3671 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
3672 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
3673 }
3674#endif
3675
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003676 return 0;
3677}
3678
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003679static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
3680{
3681 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3682
3683 return get_swappiness(memcg);
3684}
3685
3686static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
3687 u64 val)
3688{
3689 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3690 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08003691
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003692 if (val > 100)
3693 return -EINVAL;
3694
3695 if (cgrp->parent == NULL)
3696 return -EINVAL;
3697
3698 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08003699
3700 cgroup_lock();
3701
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003702 /* If under hierarchy, only empty-root can set this value */
3703 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08003704 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
3705 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003706 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08003707 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003708
3709 spin_lock(&memcg->reclaim_param_lock);
3710 memcg->swappiness = val;
3711 spin_unlock(&memcg->reclaim_param_lock);
3712
Li Zefan068b38c2009-01-15 13:51:26 -08003713 cgroup_unlock();
3714
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003715 return 0;
3716}
3717
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003718static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
3719{
3720 struct mem_cgroup_threshold_ary *t;
3721 u64 usage;
3722 int i;
3723
3724 rcu_read_lock();
3725 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003726 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003727 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003728 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003729
3730 if (!t)
3731 goto unlock;
3732
3733 usage = mem_cgroup_usage(memcg, swap);
3734
3735 /*
3736 * current_threshold points to threshold just below usage.
3737 * If it's not true, a threshold was crossed after last
3738 * call of __mem_cgroup_threshold().
3739 */
Phil Carmody5407a562010-05-26 14:42:42 -07003740 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003741
3742 /*
3743 * Iterate backward over array of thresholds starting from
3744 * current_threshold and check if a threshold is crossed.
3745 * If none of thresholds below usage is crossed, we read
3746 * only one element of the array here.
3747 */
3748 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
3749 eventfd_signal(t->entries[i].eventfd, 1);
3750
3751 /* i = current_threshold + 1 */
3752 i++;
3753
3754 /*
3755 * Iterate forward over array of thresholds starting from
3756 * current_threshold+1 and check if a threshold is crossed.
3757 * If none of thresholds above usage is crossed, we read
3758 * only one element of the array here.
3759 */
3760 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
3761 eventfd_signal(t->entries[i].eventfd, 1);
3762
3763 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07003764 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003765unlock:
3766 rcu_read_unlock();
3767}
3768
3769static void mem_cgroup_threshold(struct mem_cgroup *memcg)
3770{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07003771 while (memcg) {
3772 __mem_cgroup_threshold(memcg, false);
3773 if (do_swap_account)
3774 __mem_cgroup_threshold(memcg, true);
3775
3776 memcg = parent_mem_cgroup(memcg);
3777 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003778}
3779
3780static int compare_thresholds(const void *a, const void *b)
3781{
3782 const struct mem_cgroup_threshold *_a = a;
3783 const struct mem_cgroup_threshold *_b = b;
3784
3785 return _a->threshold - _b->threshold;
3786}
3787
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003788static int mem_cgroup_oom_notify_cb(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003789{
3790 struct mem_cgroup_eventfd_list *ev;
3791
3792 list_for_each_entry(ev, &mem->oom_notify, list)
3793 eventfd_signal(ev->eventfd, 1);
3794 return 0;
3795}
3796
3797static void mem_cgroup_oom_notify(struct mem_cgroup *mem)
3798{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003799 struct mem_cgroup *iter;
3800
3801 for_each_mem_cgroup_tree(iter, mem)
3802 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003803}
3804
3805static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
3806 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003807{
3808 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003809 struct mem_cgroup_thresholds *thresholds;
3810 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003811 int type = MEMFILE_TYPE(cft->private);
3812 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003813 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003814
3815 ret = res_counter_memparse_write_strategy(args, &threshold);
3816 if (ret)
3817 return ret;
3818
3819 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003820
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003821 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003822 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003823 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003824 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003825 else
3826 BUG();
3827
3828 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
3829
3830 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003831 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003832 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
3833
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003834 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003835
3836 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003837 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003838 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003839 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003840 ret = -ENOMEM;
3841 goto unlock;
3842 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003843 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003844
3845 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003846 if (thresholds->primary) {
3847 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003848 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003849 }
3850
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003851 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003852 new->entries[size - 1].eventfd = eventfd;
3853 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003854
3855 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003856 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003857 compare_thresholds, NULL);
3858
3859 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003860 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003861 for (i = 0; i < size; i++) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003862 if (new->entries[i].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003863 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003864 * new->current_threshold will not be used until
3865 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003866 * it here.
3867 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003868 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003869 }
3870 }
3871
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003872 /* Free old spare buffer and save old primary buffer as spare */
3873 kfree(thresholds->spare);
3874 thresholds->spare = thresholds->primary;
3875
3876 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003877
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003878 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003879 synchronize_rcu();
3880
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003881unlock:
3882 mutex_unlock(&memcg->thresholds_lock);
3883
3884 return ret;
3885}
3886
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003887static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003888 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003889{
3890 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003891 struct mem_cgroup_thresholds *thresholds;
3892 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003893 int type = MEMFILE_TYPE(cft->private);
3894 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003895 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003896
3897 mutex_lock(&memcg->thresholds_lock);
3898 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003899 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003900 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003901 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003902 else
3903 BUG();
3904
3905 /*
3906 * Something went wrong if we trying to unregister a threshold
3907 * if we don't have thresholds
3908 */
3909 BUG_ON(!thresholds);
3910
3911 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
3912
3913 /* Check if a threshold crossed before removing */
3914 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
3915
3916 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003917 size = 0;
3918 for (i = 0; i < thresholds->primary->size; i++) {
3919 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003920 size++;
3921 }
3922
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003923 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003924
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003925 /* Set thresholds array to NULL if we don't have thresholds */
3926 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003927 kfree(new);
3928 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003929 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003930 }
3931
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003932 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003933
3934 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003935 new->current_threshold = -1;
3936 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
3937 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003938 continue;
3939
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003940 new->entries[j] = thresholds->primary->entries[i];
3941 if (new->entries[j].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003942 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003943 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003944 * until rcu_assign_pointer(), so it's safe to increment
3945 * it here.
3946 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003947 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003948 }
3949 j++;
3950 }
3951
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003952swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003953 /* Swap primary and spare array */
3954 thresholds->spare = thresholds->primary;
3955 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003956
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003957 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003958 synchronize_rcu();
3959
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003960 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003961}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003962
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003963static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
3964 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
3965{
3966 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3967 struct mem_cgroup_eventfd_list *event;
3968 int type = MEMFILE_TYPE(cft->private);
3969
3970 BUG_ON(type != _OOM_TYPE);
3971 event = kmalloc(sizeof(*event), GFP_KERNEL);
3972 if (!event)
3973 return -ENOMEM;
3974
3975 mutex_lock(&memcg_oom_mutex);
3976
3977 event->eventfd = eventfd;
3978 list_add(&event->list, &memcg->oom_notify);
3979
3980 /* already in OOM ? */
3981 if (atomic_read(&memcg->oom_lock))
3982 eventfd_signal(eventfd, 1);
3983 mutex_unlock(&memcg_oom_mutex);
3984
3985 return 0;
3986}
3987
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003988static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003989 struct cftype *cft, struct eventfd_ctx *eventfd)
3990{
3991 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3992 struct mem_cgroup_eventfd_list *ev, *tmp;
3993 int type = MEMFILE_TYPE(cft->private);
3994
3995 BUG_ON(type != _OOM_TYPE);
3996
3997 mutex_lock(&memcg_oom_mutex);
3998
3999 list_for_each_entry_safe(ev, tmp, &mem->oom_notify, list) {
4000 if (ev->eventfd == eventfd) {
4001 list_del(&ev->list);
4002 kfree(ev);
4003 }
4004 }
4005
4006 mutex_unlock(&memcg_oom_mutex);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004007}
4008
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004009static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
4010 struct cftype *cft, struct cgroup_map_cb *cb)
4011{
4012 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
4013
4014 cb->fill(cb, "oom_kill_disable", mem->oom_kill_disable);
4015
4016 if (atomic_read(&mem->oom_lock))
4017 cb->fill(cb, "under_oom", 1);
4018 else
4019 cb->fill(cb, "under_oom", 0);
4020 return 0;
4021}
4022
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004023static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
4024 struct cftype *cft, u64 val)
4025{
4026 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
4027 struct mem_cgroup *parent;
4028
4029 /* cannot set to root cgroup and only 0 and 1 are allowed */
4030 if (!cgrp->parent || !((val == 0) || (val == 1)))
4031 return -EINVAL;
4032
4033 parent = mem_cgroup_from_cont(cgrp->parent);
4034
4035 cgroup_lock();
4036 /* oom-kill-disable is a flag for subhierarchy. */
4037 if ((parent->use_hierarchy) ||
4038 (mem->use_hierarchy && !list_empty(&cgrp->children))) {
4039 cgroup_unlock();
4040 return -EINVAL;
4041 }
4042 mem->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07004043 if (!val)
4044 memcg_oom_recover(mem);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004045 cgroup_unlock();
4046 return 0;
4047}
4048
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004049static struct cftype mem_cgroup_files[] = {
4050 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004051 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004052 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07004053 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004054 .register_event = mem_cgroup_usage_register_event,
4055 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004056 },
4057 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004058 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004059 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004060 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004061 .read_u64 = mem_cgroup_read,
4062 },
4063 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004064 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004065 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07004066 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07004067 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004068 },
4069 {
Balbir Singh296c81d2009-09-23 15:56:36 -07004070 .name = "soft_limit_in_bytes",
4071 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
4072 .write_string = mem_cgroup_write,
4073 .read_u64 = mem_cgroup_read,
4074 },
4075 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004076 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004077 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004078 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07004079 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004080 },
Balbir Singh8697d332008-02-07 00:13:59 -08004081 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004082 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07004083 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004084 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004085 {
4086 .name = "force_empty",
4087 .trigger = mem_cgroup_force_empty_write,
4088 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08004089 {
4090 .name = "use_hierarchy",
4091 .write_u64 = mem_cgroup_hierarchy_write,
4092 .read_u64 = mem_cgroup_hierarchy_read,
4093 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004094 {
4095 .name = "swappiness",
4096 .read_u64 = mem_cgroup_swappiness_read,
4097 .write_u64 = mem_cgroup_swappiness_write,
4098 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004099 {
4100 .name = "move_charge_at_immigrate",
4101 .read_u64 = mem_cgroup_move_charge_read,
4102 .write_u64 = mem_cgroup_move_charge_write,
4103 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004104 {
4105 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004106 .read_map = mem_cgroup_oom_control_read,
4107 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004108 .register_event = mem_cgroup_oom_register_event,
4109 .unregister_event = mem_cgroup_oom_unregister_event,
4110 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
4111 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004112};
4113
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004114#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4115static struct cftype memsw_cgroup_files[] = {
4116 {
4117 .name = "memsw.usage_in_bytes",
4118 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
4119 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004120 .register_event = mem_cgroup_usage_register_event,
4121 .unregister_event = mem_cgroup_usage_unregister_event,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004122 },
4123 {
4124 .name = "memsw.max_usage_in_bytes",
4125 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
4126 .trigger = mem_cgroup_reset,
4127 .read_u64 = mem_cgroup_read,
4128 },
4129 {
4130 .name = "memsw.limit_in_bytes",
4131 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
4132 .write_string = mem_cgroup_write,
4133 .read_u64 = mem_cgroup_read,
4134 },
4135 {
4136 .name = "memsw.failcnt",
4137 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
4138 .trigger = mem_cgroup_reset,
4139 .read_u64 = mem_cgroup_read,
4140 },
4141};
4142
4143static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
4144{
4145 if (!do_swap_account)
4146 return 0;
4147 return cgroup_add_files(cont, ss, memsw_cgroup_files,
4148 ARRAY_SIZE(memsw_cgroup_files));
4149};
4150#else
4151static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
4152{
4153 return 0;
4154}
4155#endif
4156
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004157static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
4158{
4159 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004160 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07004161 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004162 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004163 /*
4164 * This routine is called against possible nodes.
4165 * But it's BUG to call kmalloc() against offline node.
4166 *
4167 * TODO: this routine can waste much memory for nodes which will
4168 * never be onlined. It's better to use memory hotplug callback
4169 * function.
4170 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004171 if (!node_state(node, N_NORMAL_MEMORY))
4172 tmp = -1;
4173 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004174 if (!pn)
4175 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004176
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004177 mem->info.nodeinfo[node] = pn;
4178 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004179
4180 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4181 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07004182 for_each_lru(l)
4183 INIT_LIST_HEAD(&mz->lists[l]);
Balbir Singhf64c3f52009-09-23 15:56:37 -07004184 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07004185 mz->on_tree = false;
4186 mz->mem = mem;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004187 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004188 return 0;
4189}
4190
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004191static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
4192{
4193 kfree(mem->info.nodeinfo[node]);
4194}
4195
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004196static struct mem_cgroup *mem_cgroup_alloc(void)
4197{
4198 struct mem_cgroup *mem;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004199 int size = sizeof(struct mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004200
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004201 /* Can be very big if MAX_NUMNODES is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08004202 if (size < PAGE_SIZE)
4203 mem = kmalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004204 else
Jan Blunckc8dad2b2009-01-07 18:07:53 -08004205 mem = vmalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004206
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07004207 if (!mem)
4208 return NULL;
4209
4210 memset(mem, 0, size);
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004211 mem->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004212 if (!mem->stat)
4213 goto out_free;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07004214 spin_lock_init(&mem->pcp_counter_lock);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004215 return mem;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004216
4217out_free:
4218 if (size < PAGE_SIZE)
4219 kfree(mem);
4220 else
4221 vfree(mem);
4222 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004223}
4224
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004225/*
4226 * At destroying mem_cgroup, references from swap_cgroup can remain.
4227 * (scanning all at force_empty is too costly...)
4228 *
4229 * Instead of clearing all references at force_empty, we remember
4230 * the number of reference from swap_cgroup and free mem_cgroup when
4231 * it goes down to 0.
4232 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004233 * Removal of cgroup itself succeeds regardless of refs from swap.
4234 */
4235
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004236static void __mem_cgroup_free(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004237{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004238 int node;
4239
Balbir Singhf64c3f52009-09-23 15:56:37 -07004240 mem_cgroup_remove_from_trees(mem);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004241 free_css_id(&mem_cgroup_subsys, &mem->css);
4242
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004243 for_each_node_state(node, N_POSSIBLE)
4244 free_mem_cgroup_per_zone_info(mem, node);
4245
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004246 free_percpu(mem->stat);
4247 if (sizeof(struct mem_cgroup) < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004248 kfree(mem);
4249 else
4250 vfree(mem);
4251}
4252
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004253static void mem_cgroup_get(struct mem_cgroup *mem)
4254{
4255 atomic_inc(&mem->refcnt);
4256}
4257
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004258static void __mem_cgroup_put(struct mem_cgroup *mem, int count)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004259{
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004260 if (atomic_sub_and_test(count, &mem->refcnt)) {
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004261 struct mem_cgroup *parent = parent_mem_cgroup(mem);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004262 __mem_cgroup_free(mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004263 if (parent)
4264 mem_cgroup_put(parent);
4265 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004266}
4267
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004268static void mem_cgroup_put(struct mem_cgroup *mem)
4269{
4270 __mem_cgroup_put(mem, 1);
4271}
4272
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004273/*
4274 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
4275 */
4276static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
4277{
4278 if (!mem->res.parent)
4279 return NULL;
4280 return mem_cgroup_from_res_counter(mem->res.parent, res);
4281}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004282
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004283#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4284static void __init enable_swap_cgroup(void)
4285{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004286 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004287 do_swap_account = 1;
4288}
4289#else
4290static void __init enable_swap_cgroup(void)
4291{
4292}
4293#endif
4294
Balbir Singhf64c3f52009-09-23 15:56:37 -07004295static int mem_cgroup_soft_limit_tree_init(void)
4296{
4297 struct mem_cgroup_tree_per_node *rtpn;
4298 struct mem_cgroup_tree_per_zone *rtpz;
4299 int tmp, node, zone;
4300
4301 for_each_node_state(node, N_POSSIBLE) {
4302 tmp = node;
4303 if (!node_state(node, N_NORMAL_MEMORY))
4304 tmp = -1;
4305 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
4306 if (!rtpn)
4307 return 1;
4308
4309 soft_limit_tree.rb_tree_per_node[node] = rtpn;
4310
4311 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4312 rtpz = &rtpn->rb_tree_per_zone[zone];
4313 rtpz->rb_root = RB_ROOT;
4314 spin_lock_init(&rtpz->lock);
4315 }
4316 }
4317 return 0;
4318}
4319
Li Zefan0eb253e2009-01-15 13:51:25 -08004320static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004321mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
4322{
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004323 struct mem_cgroup *mem, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004324 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004325 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004326
Jan Blunckc8dad2b2009-01-07 18:07:53 -08004327 mem = mem_cgroup_alloc();
4328 if (!mem)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004329 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004330
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004331 for_each_node_state(node, N_POSSIBLE)
4332 if (alloc_mem_cgroup_per_zone_info(mem, node))
4333 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004334
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004335 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004336 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004337 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004338 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004339 parent = NULL;
Balbir Singh4b3bde42009-09-23 15:56:32 -07004340 root_mem_cgroup = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004341 if (mem_cgroup_soft_limit_tree_init())
4342 goto free_out;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004343 for_each_possible_cpu(cpu) {
4344 struct memcg_stock_pcp *stock =
4345 &per_cpu(memcg_stock, cpu);
4346 INIT_WORK(&stock->work, drain_local_stock);
4347 }
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07004348 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004349 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004350 parent = mem_cgroup_from_cont(cont->parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004351 mem->use_hierarchy = parent->use_hierarchy;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004352 mem->oom_kill_disable = parent->oom_kill_disable;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004353 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004354
Balbir Singh18f59ea2009-01-07 18:08:07 -08004355 if (parent && parent->use_hierarchy) {
4356 res_counter_init(&mem->res, &parent->res);
4357 res_counter_init(&mem->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004358 /*
4359 * We increment refcnt of the parent to ensure that we can
4360 * safely access it on res_counter_charge/uncharge.
4361 * This refcnt will be decremented when freeing this
4362 * mem_cgroup(see mem_cgroup_put).
4363 */
4364 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004365 } else {
4366 res_counter_init(&mem->res, NULL);
4367 res_counter_init(&mem->memsw, NULL);
4368 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004369 mem->last_scanned_child = 0;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -08004370 spin_lock_init(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004371 INIT_LIST_HEAD(&mem->oom_notify);
Balbir Singh6d61ef42009-01-07 18:08:06 -08004372
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004373 if (parent)
4374 mem->swappiness = get_swappiness(parent);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004375 atomic_set(&mem->refcnt, 1);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004376 mem->move_charge_at_immigrate = 0;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004377 mutex_init(&mem->thresholds_lock);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004378 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004379free_out:
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004380 __mem_cgroup_free(mem);
Balbir Singh4b3bde42009-09-23 15:56:32 -07004381 root_mem_cgroup = NULL;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004382 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004383}
4384
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004385static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004386 struct cgroup *cont)
4387{
4388 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004389
4390 return mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004391}
4392
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004393static void mem_cgroup_destroy(struct cgroup_subsys *ss,
4394 struct cgroup *cont)
4395{
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004396 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004397
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004398 mem_cgroup_put(mem);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004399}
4400
4401static int mem_cgroup_populate(struct cgroup_subsys *ss,
4402 struct cgroup *cont)
4403{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004404 int ret;
4405
4406 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
4407 ARRAY_SIZE(mem_cgroup_files));
4408
4409 if (!ret)
4410 ret = register_memsw_files(cont, ss);
4411 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004412}
4413
Daisuke Nishimura02491442010-03-10 15:22:17 -08004414#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004415/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004416#define PRECHARGE_COUNT_AT_ONCE 256
4417static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004418{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004419 int ret = 0;
4420 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004421 struct mem_cgroup *mem = mc.to;
4422
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004423 if (mem_cgroup_is_root(mem)) {
4424 mc.precharge += count;
4425 /* we don't need css_get for root */
4426 return ret;
4427 }
4428 /* try to charge at once */
4429 if (count > 1) {
4430 struct res_counter *dummy;
4431 /*
4432 * "mem" cannot be under rmdir() because we've already checked
4433 * by cgroup_lock_live_cgroup() that it is not removed and we
4434 * are still under the same cgroup_mutex. So we can postpone
4435 * css_get().
4436 */
4437 if (res_counter_charge(&mem->res, PAGE_SIZE * count, &dummy))
4438 goto one_by_one;
4439 if (do_swap_account && res_counter_charge(&mem->memsw,
4440 PAGE_SIZE * count, &dummy)) {
4441 res_counter_uncharge(&mem->res, PAGE_SIZE * count);
4442 goto one_by_one;
4443 }
4444 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004445 return ret;
4446 }
4447one_by_one:
4448 /* fall back to one by one charge */
4449 while (count--) {
4450 if (signal_pending(current)) {
4451 ret = -EINTR;
4452 break;
4453 }
4454 if (!batch_count--) {
4455 batch_count = PRECHARGE_COUNT_AT_ONCE;
4456 cond_resched();
4457 }
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08004458 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004459 if (ret || !mem)
4460 /* mem_cgroup_clear_mc() will do uncharge later */
4461 return -ENOMEM;
4462 mc.precharge++;
4463 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004464 return ret;
4465}
4466
4467/**
4468 * is_target_pte_for_mc - check a pte whether it is valid for move charge
4469 * @vma: the vma the pte to be checked belongs
4470 * @addr: the address corresponding to the pte to be checked
4471 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08004472 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004473 *
4474 * Returns
4475 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
4476 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
4477 * move charge. if @target is not NULL, the page is stored in target->page
4478 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08004479 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
4480 * target for charge migration. if @target is not NULL, the entry is stored
4481 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004482 *
4483 * Called with pte lock held.
4484 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004485union mc_target {
4486 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004487 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004488};
4489
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004490enum mc_target_type {
4491 MC_TARGET_NONE, /* not used */
4492 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08004493 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004494};
4495
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004496static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
4497 unsigned long addr, pte_t ptent)
4498{
4499 struct page *page = vm_normal_page(vma, addr, ptent);
4500
4501 if (!page || !page_mapped(page))
4502 return NULL;
4503 if (PageAnon(page)) {
4504 /* we don't move shared anon */
4505 if (!move_anon() || page_mapcount(page) > 2)
4506 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004507 } else if (!move_file())
4508 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004509 return NULL;
4510 if (!get_page_unless_zero(page))
4511 return NULL;
4512
4513 return page;
4514}
4515
4516static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
4517 unsigned long addr, pte_t ptent, swp_entry_t *entry)
4518{
4519 int usage_count;
4520 struct page *page = NULL;
4521 swp_entry_t ent = pte_to_swp_entry(ptent);
4522
4523 if (!move_anon() || non_swap_entry(ent))
4524 return NULL;
4525 usage_count = mem_cgroup_count_swap_user(ent, &page);
4526 if (usage_count > 1) { /* we don't move shared anon */
4527 if (page)
4528 put_page(page);
4529 return NULL;
4530 }
4531 if (do_swap_account)
4532 entry->val = ent.val;
4533
4534 return page;
4535}
4536
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004537static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
4538 unsigned long addr, pte_t ptent, swp_entry_t *entry)
4539{
4540 struct page *page = NULL;
4541 struct inode *inode;
4542 struct address_space *mapping;
4543 pgoff_t pgoff;
4544
4545 if (!vma->vm_file) /* anonymous vma */
4546 return NULL;
4547 if (!move_file())
4548 return NULL;
4549
4550 inode = vma->vm_file->f_path.dentry->d_inode;
4551 mapping = vma->vm_file->f_mapping;
4552 if (pte_none(ptent))
4553 pgoff = linear_page_index(vma, addr);
4554 else /* pte_file(ptent) is true */
4555 pgoff = pte_to_pgoff(ptent);
4556
4557 /* page is moved even if it's not RSS of this task(page-faulted). */
4558 if (!mapping_cap_swap_backed(mapping)) { /* normal file */
4559 page = find_get_page(mapping, pgoff);
4560 } else { /* shmem/tmpfs file. we should take account of swap too. */
4561 swp_entry_t ent;
4562 mem_cgroup_get_shmem_target(inode, pgoff, &page, &ent);
4563 if (do_swap_account)
4564 entry->val = ent.val;
4565 }
4566
4567 return page;
4568}
4569
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004570static int is_target_pte_for_mc(struct vm_area_struct *vma,
4571 unsigned long addr, pte_t ptent, union mc_target *target)
4572{
Daisuke Nishimura02491442010-03-10 15:22:17 -08004573 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004574 struct page_cgroup *pc;
4575 int ret = 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004576 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004577
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004578 if (pte_present(ptent))
4579 page = mc_handle_present_pte(vma, addr, ptent);
4580 else if (is_swap_pte(ptent))
4581 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004582 else if (pte_none(ptent) || pte_file(ptent))
4583 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004584
4585 if (!page && !ent.val)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004586 return 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004587 if (page) {
4588 pc = lookup_page_cgroup(page);
4589 /*
4590 * Do only loose check w/o page_cgroup lock.
4591 * mem_cgroup_move_account() checks the pc is valid or not under
4592 * the lock.
4593 */
4594 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
4595 ret = MC_TARGET_PAGE;
4596 if (target)
4597 target->page = page;
4598 }
4599 if (!ret || !target)
4600 put_page(page);
4601 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004602 /* There is a swap entry and a page doesn't exist or isn't charged */
4603 if (ent.val && !ret &&
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07004604 css_id(&mc.from->css) == lookup_swap_cgroup(ent)) {
4605 ret = MC_TARGET_SWAP;
4606 if (target)
4607 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004608 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004609 return ret;
4610}
4611
4612static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
4613 unsigned long addr, unsigned long end,
4614 struct mm_walk *walk)
4615{
4616 struct vm_area_struct *vma = walk->private;
4617 pte_t *pte;
4618 spinlock_t *ptl;
4619
4620 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
4621 for (; addr != end; pte++, addr += PAGE_SIZE)
4622 if (is_target_pte_for_mc(vma, addr, *pte, NULL))
4623 mc.precharge++; /* increment precharge temporarily */
4624 pte_unmap_unlock(pte - 1, ptl);
4625 cond_resched();
4626
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004627 return 0;
4628}
4629
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004630static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
4631{
4632 unsigned long precharge;
4633 struct vm_area_struct *vma;
4634
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -08004635 /* We've already held the mmap_sem */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004636 for (vma = mm->mmap; vma; vma = vma->vm_next) {
4637 struct mm_walk mem_cgroup_count_precharge_walk = {
4638 .pmd_entry = mem_cgroup_count_precharge_pte_range,
4639 .mm = mm,
4640 .private = vma,
4641 };
4642 if (is_vm_hugetlb_page(vma))
4643 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004644 walk_page_range(vma->vm_start, vma->vm_end,
4645 &mem_cgroup_count_precharge_walk);
4646 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004647
4648 precharge = mc.precharge;
4649 mc.precharge = 0;
4650
4651 return precharge;
4652}
4653
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004654static int mem_cgroup_precharge_mc(struct mm_struct *mm)
4655{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004656 return mem_cgroup_do_precharge(mem_cgroup_count_precharge(mm));
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004657}
4658
4659static void mem_cgroup_clear_mc(void)
4660{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07004661 struct mem_cgroup *from = mc.from;
4662 struct mem_cgroup *to = mc.to;
4663
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004664 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004665 if (mc.precharge) {
4666 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
4667 mc.precharge = 0;
4668 }
4669 /*
4670 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
4671 * we must uncharge here.
4672 */
4673 if (mc.moved_charge) {
4674 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
4675 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004676 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004677 /* we must fixup refcnts and charges */
4678 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004679 /* uncharge swap account from the old cgroup */
4680 if (!mem_cgroup_is_root(mc.from))
4681 res_counter_uncharge(&mc.from->memsw,
4682 PAGE_SIZE * mc.moved_swap);
4683 __mem_cgroup_put(mc.from, mc.moved_swap);
4684
4685 if (!mem_cgroup_is_root(mc.to)) {
4686 /*
4687 * we charged both to->res and to->memsw, so we should
4688 * uncharge to->res.
4689 */
4690 res_counter_uncharge(&mc.to->res,
4691 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004692 }
4693 /* we've already done mem_cgroup_get(mc.to) */
4694
4695 mc.moved_swap = 0;
4696 }
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -08004697 if (mc.mm) {
4698 up_read(&mc.mm->mmap_sem);
4699 mmput(mc.mm);
4700 }
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07004701 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004702 mc.from = NULL;
4703 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07004704 spin_unlock(&mc.lock);
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -08004705 mc.moving_task = NULL;
4706 mc.mm = NULL;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07004707 mem_cgroup_end_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07004708 memcg_oom_recover(from);
4709 memcg_oom_recover(to);
Daisuke Nishimura8033b972010-03-10 15:22:16 -08004710 wake_up_all(&mc.waitq);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004711}
4712
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004713static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
4714 struct cgroup *cgroup,
4715 struct task_struct *p,
4716 bool threadgroup)
4717{
4718 int ret = 0;
4719 struct mem_cgroup *mem = mem_cgroup_from_cont(cgroup);
4720
4721 if (mem->move_charge_at_immigrate) {
4722 struct mm_struct *mm;
4723 struct mem_cgroup *from = mem_cgroup_from_task(p);
4724
4725 VM_BUG_ON(from == mem);
4726
4727 mm = get_task_mm(p);
4728 if (!mm)
4729 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004730 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004731 if (mm->owner == p) {
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -08004732 /*
4733 * We do all the move charge works under one mmap_sem to
4734 * avoid deadlock with down_write(&mmap_sem)
4735 * -> try_charge() -> if (mc.moving_task) -> sleep.
4736 */
4737 down_read(&mm->mmap_sem);
4738
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004739 VM_BUG_ON(mc.from);
4740 VM_BUG_ON(mc.to);
4741 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004742 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004743 VM_BUG_ON(mc.moved_swap);
Daisuke Nishimura8033b972010-03-10 15:22:16 -08004744 VM_BUG_ON(mc.moving_task);
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -08004745 VM_BUG_ON(mc.mm);
4746
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07004747 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07004748 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004749 mc.from = from;
4750 mc.to = mem;
4751 mc.precharge = 0;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004752 mc.moved_charge = 0;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004753 mc.moved_swap = 0;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07004754 spin_unlock(&mc.lock);
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -08004755 mc.moving_task = current;
4756 mc.mm = mm;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004757
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004758 ret = mem_cgroup_precharge_mc(mm);
4759 if (ret)
4760 mem_cgroup_clear_mc();
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -08004761 /* We call up_read() and mmput() in clear_mc(). */
4762 } else
4763 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004764 }
4765 return ret;
4766}
4767
4768static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
4769 struct cgroup *cgroup,
4770 struct task_struct *p,
4771 bool threadgroup)
4772{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004773 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004774}
4775
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004776static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
4777 unsigned long addr, unsigned long end,
4778 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004779{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004780 int ret = 0;
4781 struct vm_area_struct *vma = walk->private;
4782 pte_t *pte;
4783 spinlock_t *ptl;
4784
4785retry:
4786 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
4787 for (; addr != end; addr += PAGE_SIZE) {
4788 pte_t ptent = *(pte++);
4789 union mc_target target;
4790 int type;
4791 struct page *page;
4792 struct page_cgroup *pc;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004793 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004794
4795 if (!mc.precharge)
4796 break;
4797
4798 type = is_target_pte_for_mc(vma, addr, ptent, &target);
4799 switch (type) {
4800 case MC_TARGET_PAGE:
4801 page = target.page;
4802 if (isolate_lru_page(page))
4803 goto put;
4804 pc = lookup_page_cgroup(page);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004805 if (!mem_cgroup_move_account(pc,
4806 mc.from, mc.to, false)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004807 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004808 /* we uncharge from mc.from later. */
4809 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004810 }
4811 putback_lru_page(page);
4812put: /* is_target_pte_for_mc() gets the page */
4813 put_page(page);
4814 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004815 case MC_TARGET_SWAP:
4816 ent = target.ent;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004817 if (!mem_cgroup_move_swap_account(ent,
4818 mc.from, mc.to, false)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08004819 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004820 /* we fixup refcnts and charges later. */
4821 mc.moved_swap++;
4822 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08004823 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004824 default:
4825 break;
4826 }
4827 }
4828 pte_unmap_unlock(pte - 1, ptl);
4829 cond_resched();
4830
4831 if (addr != end) {
4832 /*
4833 * We have consumed all precharges we got in can_attach().
4834 * We try charge one by one, but don't do any additional
4835 * charges to mc.to if we have failed in charge once in attach()
4836 * phase.
4837 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004838 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004839 if (!ret)
4840 goto retry;
4841 }
4842
4843 return ret;
4844}
4845
4846static void mem_cgroup_move_charge(struct mm_struct *mm)
4847{
4848 struct vm_area_struct *vma;
4849
4850 lru_add_drain_all();
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -08004851 /* We've already held the mmap_sem */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004852 for (vma = mm->mmap; vma; vma = vma->vm_next) {
4853 int ret;
4854 struct mm_walk mem_cgroup_move_charge_walk = {
4855 .pmd_entry = mem_cgroup_move_charge_pte_range,
4856 .mm = mm,
4857 .private = vma,
4858 };
4859 if (is_vm_hugetlb_page(vma))
4860 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004861 ret = walk_page_range(vma->vm_start, vma->vm_end,
4862 &mem_cgroup_move_charge_walk);
4863 if (ret)
4864 /*
4865 * means we have consumed all precharges and failed in
4866 * doing additional charge. Just abandon here.
4867 */
4868 break;
4869 }
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004870}
4871
Balbir Singh67e465a2008-02-07 00:13:54 -08004872static void mem_cgroup_move_task(struct cgroup_subsys *ss,
4873 struct cgroup *cont,
4874 struct cgroup *old_cont,
Ben Blumbe367d02009-09-23 15:56:31 -07004875 struct task_struct *p,
4876 bool threadgroup)
Balbir Singh67e465a2008-02-07 00:13:54 -08004877{
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -08004878 if (!mc.mm)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004879 /* no need to move charge */
4880 return;
4881
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -08004882 mem_cgroup_move_charge(mc.mm);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004883 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08004884}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07004885#else /* !CONFIG_MMU */
4886static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
4887 struct cgroup *cgroup,
4888 struct task_struct *p,
4889 bool threadgroup)
4890{
4891 return 0;
4892}
4893static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
4894 struct cgroup *cgroup,
4895 struct task_struct *p,
4896 bool threadgroup)
4897{
4898}
4899static void mem_cgroup_move_task(struct cgroup_subsys *ss,
4900 struct cgroup *cont,
4901 struct cgroup *old_cont,
4902 struct task_struct *p,
4903 bool threadgroup)
4904{
4905}
4906#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08004907
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004908struct cgroup_subsys mem_cgroup_subsys = {
4909 .name = "memory",
4910 .subsys_id = mem_cgroup_subsys_id,
4911 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004912 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004913 .destroy = mem_cgroup_destroy,
4914 .populate = mem_cgroup_populate,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004915 .can_attach = mem_cgroup_can_attach,
4916 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08004917 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004918 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004919 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004920};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004921
4922#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4923
4924static int __init disable_swap_account(char *s)
4925{
4926 really_do_swap_account = 0;
4927 return 1;
4928}
4929__setup("noswapaccount", disable_swap_account);
4930#endif