blob: 52840adae62a2799239d1013d5ae691a4b704f4d [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 {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700281 spinlock_t lock; /* for from, to, moving_task */
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 */
288 wait_queue_head_t waitq; /* a waitq for other context */
289} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700290 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800291 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
292};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800293
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700294static bool move_anon(void)
295{
296 return test_bit(MOVE_CHARGE_TYPE_ANON,
297 &mc.to->move_charge_at_immigrate);
298}
299
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700300static bool move_file(void)
301{
302 return test_bit(MOVE_CHARGE_TYPE_FILE,
303 &mc.to->move_charge_at_immigrate);
304}
305
Balbir Singh4e416952009-09-23 15:56:39 -0700306/*
307 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
308 * limit reclaim to prevent infinite loops, if they ever occur.
309 */
310#define MEM_CGROUP_MAX_RECLAIM_LOOPS (100)
311#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2)
312
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800313enum charge_type {
314 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
315 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700316 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700317 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800318 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700319 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700320 NR_CHARGE_TYPE,
321};
322
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700323/* only for here (for easy reading.) */
324#define PCGF_CACHE (1UL << PCG_CACHE)
325#define PCGF_USED (1UL << PCG_USED)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700326#define PCGF_LOCK (1UL << PCG_LOCK)
Balbir Singh4b3bde42009-09-23 15:56:32 -0700327/* Not used, but added here for completeness */
328#define PCGF_ACCT (1UL << PCG_ACCT)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800329
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800330/* for encoding cft->private value on file */
331#define _MEM (0)
332#define _MEMSWAP (1)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700333#define _OOM_TYPE (2)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800334#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
335#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
336#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700337/* Used for OOM nofiier */
338#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800339
Balbir Singh75822b42009-09-23 15:56:38 -0700340/*
341 * Reclaim flags for mem_cgroup_hierarchical_reclaim
342 */
343#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
344#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
345#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
346#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
Balbir Singh4e416952009-09-23 15:56:39 -0700347#define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
348#define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
Balbir Singh75822b42009-09-23 15:56:38 -0700349
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800350static void mem_cgroup_get(struct mem_cgroup *mem);
351static void mem_cgroup_put(struct mem_cgroup *mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -0800352static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -0800353static void drain_all_stock_async(void);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800354
Balbir Singhf64c3f52009-09-23 15:56:37 -0700355static struct mem_cgroup_per_zone *
356mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
357{
358 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
359}
360
Wu Fengguangd3242362009-12-16 12:19:59 +0100361struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *mem)
362{
363 return &mem->css;
364}
365
Balbir Singhf64c3f52009-09-23 15:56:37 -0700366static struct mem_cgroup_per_zone *
367page_cgroup_zoneinfo(struct page_cgroup *pc)
368{
369 struct mem_cgroup *mem = pc->mem_cgroup;
370 int nid = page_cgroup_nid(pc);
371 int zid = page_cgroup_zid(pc);
372
373 if (!mem)
374 return NULL;
375
376 return mem_cgroup_zoneinfo(mem, nid, zid);
377}
378
379static struct mem_cgroup_tree_per_zone *
380soft_limit_tree_node_zone(int nid, int zid)
381{
382 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
383}
384
385static struct mem_cgroup_tree_per_zone *
386soft_limit_tree_from_page(struct page *page)
387{
388 int nid = page_to_nid(page);
389 int zid = page_zonenum(page);
390
391 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
392}
393
394static void
Balbir Singh4e416952009-09-23 15:56:39 -0700395__mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700396 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700397 struct mem_cgroup_tree_per_zone *mctz,
398 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700399{
400 struct rb_node **p = &mctz->rb_root.rb_node;
401 struct rb_node *parent = NULL;
402 struct mem_cgroup_per_zone *mz_node;
403
404 if (mz->on_tree)
405 return;
406
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700407 mz->usage_in_excess = new_usage_in_excess;
408 if (!mz->usage_in_excess)
409 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700410 while (*p) {
411 parent = *p;
412 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
413 tree_node);
414 if (mz->usage_in_excess < mz_node->usage_in_excess)
415 p = &(*p)->rb_left;
416 /*
417 * We can't avoid mem cgroups that are over their soft
418 * limit by the same amount
419 */
420 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
421 p = &(*p)->rb_right;
422 }
423 rb_link_node(&mz->tree_node, parent, p);
424 rb_insert_color(&mz->tree_node, &mctz->rb_root);
425 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700426}
427
428static void
429__mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
430 struct mem_cgroup_per_zone *mz,
431 struct mem_cgroup_tree_per_zone *mctz)
432{
433 if (!mz->on_tree)
434 return;
435 rb_erase(&mz->tree_node, &mctz->rb_root);
436 mz->on_tree = false;
437}
438
439static void
Balbir Singhf64c3f52009-09-23 15:56:37 -0700440mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
441 struct mem_cgroup_per_zone *mz,
442 struct mem_cgroup_tree_per_zone *mctz)
443{
444 spin_lock(&mctz->lock);
Balbir Singh4e416952009-09-23 15:56:39 -0700445 __mem_cgroup_remove_exceeded(mem, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700446 spin_unlock(&mctz->lock);
447}
448
Balbir Singhf64c3f52009-09-23 15:56:37 -0700449
450static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
451{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700452 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700453 struct mem_cgroup_per_zone *mz;
454 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700455 int nid = page_to_nid(page);
456 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700457 mctz = soft_limit_tree_from_page(page);
458
459 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700460 * Necessary to update all ancestors when hierarchy is used.
461 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700462 */
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700463 for (; mem; mem = parent_mem_cgroup(mem)) {
464 mz = mem_cgroup_zoneinfo(mem, nid, zid);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700465 excess = res_counter_soft_limit_excess(&mem->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700466 /*
467 * We have to update the tree if mz is on RB-tree or
468 * mem is over its softlimit.
469 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700470 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700471 spin_lock(&mctz->lock);
472 /* if on-tree, remove it */
473 if (mz->on_tree)
474 __mem_cgroup_remove_exceeded(mem, mz, mctz);
475 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700476 * Insert again. mz->usage_in_excess will be updated.
477 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700478 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700479 __mem_cgroup_insert_exceeded(mem, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700480 spin_unlock(&mctz->lock);
481 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700482 }
483}
484
485static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
486{
487 int node, zone;
488 struct mem_cgroup_per_zone *mz;
489 struct mem_cgroup_tree_per_zone *mctz;
490
491 for_each_node_state(node, N_POSSIBLE) {
492 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
493 mz = mem_cgroup_zoneinfo(mem, node, zone);
494 mctz = soft_limit_tree_node_zone(node, zone);
495 mem_cgroup_remove_exceeded(mem, mz, mctz);
496 }
497 }
498}
499
Balbir Singh4e416952009-09-23 15:56:39 -0700500static inline unsigned long mem_cgroup_get_excess(struct mem_cgroup *mem)
501{
502 return res_counter_soft_limit_excess(&mem->res) >> PAGE_SHIFT;
503}
504
505static struct mem_cgroup_per_zone *
506__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
507{
508 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700509 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700510
511retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700512 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700513 rightmost = rb_last(&mctz->rb_root);
514 if (!rightmost)
515 goto done; /* Nothing to reclaim from */
516
517 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
518 /*
519 * Remove the node now but someone else can add it back,
520 * we will to add it back at the end of reclaim to its correct
521 * position in the tree.
522 */
523 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
524 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
525 !css_tryget(&mz->mem->css))
526 goto retry;
527done:
528 return mz;
529}
530
531static struct mem_cgroup_per_zone *
532mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
533{
534 struct mem_cgroup_per_zone *mz;
535
536 spin_lock(&mctz->lock);
537 mz = __mem_cgroup_largest_soft_limit_node(mctz);
538 spin_unlock(&mctz->lock);
539 return mz;
540}
541
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700542/*
543 * Implementation Note: reading percpu statistics for memcg.
544 *
545 * Both of vmstat[] and percpu_counter has threshold and do periodic
546 * synchronization to implement "quick" read. There are trade-off between
547 * reading cost and precision of value. Then, we may have a chance to implement
548 * a periodic synchronizion of counter in memcg's counter.
549 *
550 * But this _read() function is used for user interface now. The user accounts
551 * memory usage by memory cgroup and he _always_ requires exact value because
552 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
553 * have to visit all online cpus and make sum. So, for now, unnecessary
554 * synchronization is not implemented. (just implemented for cpu hotplug)
555 *
556 * If there are kernel internal actions which can make use of some not-exact
557 * value, and reading all cpu value can be performance bottleneck in some
558 * common workload, threashold and synchonization as vmstat[] should be
559 * implemented.
560 */
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800561static s64 mem_cgroup_read_stat(struct mem_cgroup *mem,
562 enum mem_cgroup_stat_index idx)
563{
564 int cpu;
565 s64 val = 0;
566
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700567 get_online_cpus();
568 for_each_online_cpu(cpu)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800569 val += per_cpu(mem->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700570#ifdef CONFIG_HOTPLUG_CPU
571 spin_lock(&mem->pcp_counter_lock);
572 val += mem->nocpu_base.count[idx];
573 spin_unlock(&mem->pcp_counter_lock);
574#endif
575 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800576 return val;
577}
578
579static s64 mem_cgroup_local_usage(struct mem_cgroup *mem)
580{
581 s64 ret;
582
583 ret = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS);
584 ret += mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE);
585 return ret;
586}
587
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700588static void mem_cgroup_swap_statistics(struct mem_cgroup *mem,
589 bool charge)
590{
591 int val = (charge) ? 1 : -1;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800592 this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_SWAPOUT], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700593}
594
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700595static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
596 struct page_cgroup *pc,
597 bool charge)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800598{
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700599 int val = (charge) ? 1 : -1;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800600
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800601 preempt_disable();
602
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700603 if (PageCgroupCache(pc))
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800604 __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_CACHE], val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800605 else
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800606 __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_RSS], val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700607
608 if (charge)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800609 __this_cpu_inc(mem->stat->count[MEM_CGROUP_STAT_PGPGIN_COUNT]);
Balaji Rao55e462b2008-05-01 04:35:12 -0700610 else
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800611 __this_cpu_inc(mem->stat->count[MEM_CGROUP_STAT_PGPGOUT_COUNT]);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800612 __this_cpu_inc(mem->stat->count[MEM_CGROUP_EVENTS]);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800613
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800614 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800615}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800616
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700617static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700618 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800619{
620 int nid, zid;
621 struct mem_cgroup_per_zone *mz;
622 u64 total = 0;
623
624 for_each_online_node(nid)
625 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
626 mz = mem_cgroup_zoneinfo(mem, nid, zid);
627 total += MEM_CGROUP_ZSTAT(mz, idx);
628 }
629 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800630}
631
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800632static bool __memcg_event_check(struct mem_cgroup *mem, int event_mask_shift)
633{
634 s64 val;
635
636 val = this_cpu_read(mem->stat->count[MEM_CGROUP_EVENTS]);
637
638 return !(val & ((1 << event_mask_shift) - 1));
639}
640
641/*
642 * Check events in order.
643 *
644 */
645static void memcg_check_events(struct mem_cgroup *mem, struct page *page)
646{
647 /* threshold event is triggered in finer grain than soft limit */
648 if (unlikely(__memcg_event_check(mem, THRESHOLDS_EVENTS_THRESH))) {
649 mem_cgroup_threshold(mem);
650 if (unlikely(__memcg_event_check(mem, SOFTLIMIT_EVENTS_THRESH)))
651 mem_cgroup_update_tree(mem, page);
652 }
653}
654
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800655static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800656{
657 return container_of(cgroup_subsys_state(cont,
658 mem_cgroup_subsys_id), struct mem_cgroup,
659 css);
660}
661
Balbir Singhcf475ad2008-04-29 01:00:16 -0700662struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800663{
Balbir Singh31a78f22008-09-28 23:09:31 +0100664 /*
665 * mm_update_next_owner() may clear mm->owner to NULL
666 * if it races with swapoff, page migration, etc.
667 * So this can be called with p == NULL.
668 */
669 if (unlikely(!p))
670 return NULL;
671
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800672 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
673 struct mem_cgroup, css);
674}
675
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800676static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
677{
678 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700679
680 if (!mm)
681 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800682 /*
683 * Because we have no locks, mm->owner's may be being moved to other
684 * cgroup. We use css_tryget() here even if this looks
685 * pessimistic (rather than adding locks here).
686 */
687 rcu_read_lock();
688 do {
689 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
690 if (unlikely(!mem))
691 break;
692 } while (!css_tryget(&mem->css));
693 rcu_read_unlock();
694 return mem;
695}
696
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700697/* The caller has to guarantee "mem" exists before calling this */
698static struct mem_cgroup *mem_cgroup_start_loop(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700699{
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700700 struct cgroup_subsys_state *css;
701 int found;
702
703 if (!mem) /* ROOT cgroup has the smallest ID */
704 return root_mem_cgroup; /*css_put/get against root is ignored*/
705 if (!mem->use_hierarchy) {
706 if (css_tryget(&mem->css))
707 return mem;
708 return NULL;
709 }
710 rcu_read_lock();
711 /*
712 * searching a memory cgroup which has the smallest ID under given
713 * ROOT cgroup. (ID >= 1)
714 */
715 css = css_get_next(&mem_cgroup_subsys, 1, &mem->css, &found);
716 if (css && css_tryget(css))
717 mem = container_of(css, struct mem_cgroup, css);
718 else
719 mem = NULL;
720 rcu_read_unlock();
721 return mem;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700722}
723
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700724static struct mem_cgroup *mem_cgroup_get_next(struct mem_cgroup *iter,
725 struct mem_cgroup *root,
726 bool cond)
727{
728 int nextid = css_id(&iter->css) + 1;
729 int found;
730 int hierarchy_used;
731 struct cgroup_subsys_state *css;
732
733 hierarchy_used = iter->use_hierarchy;
734
735 css_put(&iter->css);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700736 /* If no ROOT, walk all, ignore hierarchy */
737 if (!cond || (root && !hierarchy_used))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700738 return NULL;
739
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700740 if (!root)
741 root = root_mem_cgroup;
742
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700743 do {
744 iter = NULL;
745 rcu_read_lock();
746
747 css = css_get_next(&mem_cgroup_subsys, nextid,
748 &root->css, &found);
749 if (css && css_tryget(css))
750 iter = container_of(css, struct mem_cgroup, css);
751 rcu_read_unlock();
752 /* If css is NULL, no more cgroups will be found */
753 nextid = found + 1;
754 } while (css && !iter);
755
756 return iter;
757}
758/*
759 * for_eacn_mem_cgroup_tree() for visiting all cgroup under tree. Please
760 * be careful that "break" loop is not allowed. We have reference count.
761 * Instead of that modify "cond" to be false and "continue" to exit the loop.
762 */
763#define for_each_mem_cgroup_tree_cond(iter, root, cond) \
764 for (iter = mem_cgroup_start_loop(root);\
765 iter != NULL;\
766 iter = mem_cgroup_get_next(iter, root, cond))
767
768#define for_each_mem_cgroup_tree(iter, root) \
769 for_each_mem_cgroup_tree_cond(iter, root, true)
770
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700771#define for_each_mem_cgroup_all(iter) \
772 for_each_mem_cgroup_tree_cond(iter, NULL, true)
773
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700774
Balbir Singh4b3bde42009-09-23 15:56:32 -0700775static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
776{
777 return (mem == root_mem_cgroup);
778}
779
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800780/*
781 * Following LRU functions are allowed to be used without PCG_LOCK.
782 * Operations are called by routine of global LRU independently from memcg.
783 * What we have to take care of here is validness of pc->mem_cgroup.
784 *
785 * Changes to pc->mem_cgroup happens when
786 * 1. charge
787 * 2. moving account
788 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
789 * It is added to LRU before charge.
790 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
791 * When moving account, the page is not on LRU. It's isolated.
792 */
793
794void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800795{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800796 struct page_cgroup *pc;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800797 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700798
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800799 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800800 return;
801 pc = lookup_page_cgroup(page);
802 /* can happen while we handle swapcache. */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700803 if (!TestClearPageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800804 return;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700805 VM_BUG_ON(!pc->mem_cgroup);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800806 /*
807 * We don't check PCG_USED bit. It's cleared when the "page" is finally
808 * removed from global LRU.
809 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800810 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700811 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700812 if (mem_cgroup_is_root(pc->mem_cgroup))
813 return;
814 VM_BUG_ON(list_empty(&pc->lru));
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800815 list_del_init(&pc->lru);
816 return;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800817}
818
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800819void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800820{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800821 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800822}
823
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800824void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800825{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800826 struct mem_cgroup_per_zone *mz;
827 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800828
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800829 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700830 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700831
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800832 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800833 /*
834 * Used bit is set without atomic ops but after smp_wmb().
835 * For making pc->mem_cgroup visible, insert smp_rmb() here.
836 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800837 smp_rmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -0700838 /* unused or root page is not rotated. */
839 if (!PageCgroupUsed(pc) || mem_cgroup_is_root(pc->mem_cgroup))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800840 return;
841 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700842 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800843}
844
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800845void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
846{
847 struct page_cgroup *pc;
848 struct mem_cgroup_per_zone *mz;
849
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800850 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800851 return;
852 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700853 VM_BUG_ON(PageCgroupAcctLRU(pc));
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800854 /*
855 * Used bit is set without atomic ops but after smp_wmb().
856 * For making pc->mem_cgroup visible, insert smp_rmb() here.
857 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800858 smp_rmb();
859 if (!PageCgroupUsed(pc))
860 return;
861
862 mz = page_cgroup_zoneinfo(pc);
863 MEM_CGROUP_ZSTAT(mz, lru) += 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700864 SetPageCgroupAcctLRU(pc);
865 if (mem_cgroup_is_root(pc->mem_cgroup))
866 return;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800867 list_add(&pc->lru, &mz->lists[lru]);
868}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800869
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800870/*
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800871 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
872 * lru because the page may.be reused after it's fully uncharged (because of
873 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
874 * it again. This function is only used to charge SwapCache. It's done under
875 * lock_page and expected that zone->lru_lock is never held.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800876 */
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800877static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800878{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800879 unsigned long flags;
880 struct zone *zone = page_zone(page);
881 struct page_cgroup *pc = lookup_page_cgroup(page);
882
883 spin_lock_irqsave(&zone->lru_lock, flags);
884 /*
885 * Forget old LRU when this page_cgroup is *not* used. This Used bit
886 * is guarded by lock_page() because the page is SwapCache.
887 */
888 if (!PageCgroupUsed(pc))
889 mem_cgroup_del_lru_list(page, page_lru(page));
890 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800891}
892
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800893static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
894{
895 unsigned long flags;
896 struct zone *zone = page_zone(page);
897 struct page_cgroup *pc = lookup_page_cgroup(page);
898
899 spin_lock_irqsave(&zone->lru_lock, flags);
900 /* link when the page is linked to LRU but page_cgroup isn't */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700901 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800902 mem_cgroup_add_lru_list(page, page_lru(page));
903 spin_unlock_irqrestore(&zone->lru_lock, flags);
904}
905
906
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800907void mem_cgroup_move_lists(struct page *page,
908 enum lru_list from, enum lru_list to)
909{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800910 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800911 return;
912 mem_cgroup_del_lru_list(page, from);
913 mem_cgroup_add_lru_list(page, to);
914}
915
David Rientjes4c4a2212008-02-07 00:14:06 -0800916int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
917{
918 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700919 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -0700920 struct task_struct *p;
David Rientjes4c4a2212008-02-07 00:14:06 -0800921
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -0700922 p = find_lock_task_mm(task);
923 if (!p)
924 return 0;
925 curr = try_get_mem_cgroup_from_mm(p->mm);
926 task_unlock(p);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700927 if (!curr)
928 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -0800929 /*
930 * We should check use_hierarchy of "mem" not "curr". Because checking
931 * use_hierarchy of "curr" here make this function true if hierarchy is
932 * enabled in "curr" and "curr" is a child of "mem" in *cgroup*
933 * hierarchy(even if use_hierarchy is disabled in "mem").
934 */
935 if (mem->use_hierarchy)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700936 ret = css_is_ancestor(&curr->css, &mem->css);
937 else
938 ret = (curr == mem);
939 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -0800940 return ret;
941}
942
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800943static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800944{
945 unsigned long active;
946 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800947 unsigned long gb;
948 unsigned long inactive_ratio;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800949
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700950 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
951 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800952
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800953 gb = (inactive + active) >> (30 - PAGE_SHIFT);
954 if (gb)
955 inactive_ratio = int_sqrt(10 * gb);
956 else
957 inactive_ratio = 1;
958
959 if (present_pages) {
960 present_pages[0] = inactive;
961 present_pages[1] = active;
962 }
963
964 return inactive_ratio;
965}
966
967int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
968{
969 unsigned long active;
970 unsigned long inactive;
971 unsigned long present_pages[2];
972 unsigned long inactive_ratio;
973
974 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
975
976 inactive = present_pages[0];
977 active = present_pages[1];
978
979 if (inactive * inactive_ratio < active)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800980 return 1;
981
982 return 0;
983}
984
Rik van Riel56e49d22009-06-16 15:32:28 -0700985int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
986{
987 unsigned long active;
988 unsigned long inactive;
989
990 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
991 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
992
993 return (active > inactive);
994}
995
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800996unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
997 struct zone *zone,
998 enum lru_list lru)
999{
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -07001000 int nid = zone_to_nid(zone);
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -08001001 int zid = zone_idx(zone);
1002 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
1003
1004 return MEM_CGROUP_ZSTAT(mz, lru);
1005}
1006
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001007struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
1008 struct zone *zone)
1009{
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -07001010 int nid = zone_to_nid(zone);
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001011 int zid = zone_idx(zone);
1012 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
1013
1014 return &mz->reclaim_stat;
1015}
1016
1017struct zone_reclaim_stat *
1018mem_cgroup_get_reclaim_stat_from_page(struct page *page)
1019{
1020 struct page_cgroup *pc;
1021 struct mem_cgroup_per_zone *mz;
1022
1023 if (mem_cgroup_disabled())
1024 return NULL;
1025
1026 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -08001027 /*
1028 * Used bit is set without atomic ops but after smp_wmb().
1029 * For making pc->mem_cgroup visible, insert smp_rmb() here.
1030 */
1031 smp_rmb();
1032 if (!PageCgroupUsed(pc))
1033 return NULL;
1034
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001035 mz = page_cgroup_zoneinfo(pc);
1036 if (!mz)
1037 return NULL;
1038
1039 return &mz->reclaim_stat;
1040}
1041
Balbir Singh66e17072008-02-07 00:13:56 -08001042unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
1043 struct list_head *dst,
1044 unsigned long *scanned, int order,
1045 int mode, struct zone *z,
1046 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001047 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -08001048{
1049 unsigned long nr_taken = 0;
1050 struct page *page;
1051 unsigned long scan;
1052 LIST_HEAD(pc_list);
1053 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001054 struct page_cgroup *pc, *tmp;
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -07001055 int nid = zone_to_nid(z);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001056 int zid = zone_idx(z);
1057 struct mem_cgroup_per_zone *mz;
Johannes Weinerb7c46d12009-09-21 17:02:56 -07001058 int lru = LRU_FILE * file + active;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001059 int ret;
Balbir Singh66e17072008-02-07 00:13:56 -08001060
Balbir Singhcf475ad2008-04-29 01:00:16 -07001061 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001062 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001063 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -08001064
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001065 scan = 0;
1066 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -08001067 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001068 break;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001069
1070 page = pc->page;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001071 if (unlikely(!PageCgroupUsed(pc)))
1072 continue;
Hugh Dickins436c65412008-02-07 00:14:12 -08001073 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001074 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001075
Hugh Dickins436c65412008-02-07 00:14:12 -08001076 scan++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001077 ret = __isolate_lru_page(page, mode, file);
1078 switch (ret) {
1079 case 0:
Balbir Singh66e17072008-02-07 00:13:56 -08001080 list_move(&page->lru, dst);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001081 mem_cgroup_del_lru(page);
Balbir Singh66e17072008-02-07 00:13:56 -08001082 nr_taken++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001083 break;
1084 case -EBUSY:
1085 /* we don't affect global LRU but rotate in our LRU */
1086 mem_cgroup_rotate_lru_list(page, page_lru(page));
1087 break;
1088 default:
1089 break;
Balbir Singh66e17072008-02-07 00:13:56 -08001090 }
1091 }
1092
Balbir Singh66e17072008-02-07 00:13:56 -08001093 *scanned = scan;
KOSAKI Motohirocc8e9702010-08-09 17:19:57 -07001094
1095 trace_mm_vmscan_memcg_isolate(0, nr_to_scan, scan, nr_taken,
1096 0, 0, 0, mode);
1097
Balbir Singh66e17072008-02-07 00:13:56 -08001098 return nr_taken;
1099}
1100
Balbir Singh6d61ef42009-01-07 18:08:06 -08001101#define mem_cgroup_from_res_counter(counter, member) \
1102 container_of(counter, struct mem_cgroup, member)
1103
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -08001104static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
1105{
1106 if (do_swap_account) {
1107 if (res_counter_check_under_limit(&mem->res) &&
1108 res_counter_check_under_limit(&mem->memsw))
1109 return true;
1110 } else
1111 if (res_counter_check_under_limit(&mem->res))
1112 return true;
1113 return false;
1114}
1115
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001116static unsigned int get_swappiness(struct mem_cgroup *memcg)
1117{
1118 struct cgroup *cgrp = memcg->css.cgroup;
1119 unsigned int swappiness;
1120
1121 /* root ? */
1122 if (cgrp->parent == NULL)
1123 return vm_swappiness;
1124
1125 spin_lock(&memcg->reclaim_param_lock);
1126 swappiness = memcg->swappiness;
1127 spin_unlock(&memcg->reclaim_param_lock);
1128
1129 return swappiness;
1130}
1131
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001132static void mem_cgroup_start_move(struct mem_cgroup *mem)
1133{
1134 int cpu;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001135
1136 get_online_cpus();
1137 spin_lock(&mem->pcp_counter_lock);
1138 for_each_online_cpu(cpu)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001139 per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) += 1;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001140 mem->nocpu_base.count[MEM_CGROUP_ON_MOVE] += 1;
1141 spin_unlock(&mem->pcp_counter_lock);
1142 put_online_cpus();
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001143
1144 synchronize_rcu();
1145}
1146
1147static void mem_cgroup_end_move(struct mem_cgroup *mem)
1148{
1149 int cpu;
1150
1151 if (!mem)
1152 return;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001153 get_online_cpus();
1154 spin_lock(&mem->pcp_counter_lock);
1155 for_each_online_cpu(cpu)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001156 per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) -= 1;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001157 mem->nocpu_base.count[MEM_CGROUP_ON_MOVE] -= 1;
1158 spin_unlock(&mem->pcp_counter_lock);
1159 put_online_cpus();
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001160}
1161/*
1162 * 2 routines for checking "mem" is under move_account() or not.
1163 *
1164 * mem_cgroup_stealed() - checking a cgroup is mc.from or not. This is used
1165 * for avoiding race in accounting. If true,
1166 * pc->mem_cgroup may be overwritten.
1167 *
1168 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1169 * under hierarchy of moving cgroups. This is for
1170 * waiting at hith-memory prressure caused by "move".
1171 */
1172
1173static bool mem_cgroup_stealed(struct mem_cgroup *mem)
1174{
1175 VM_BUG_ON(!rcu_read_lock_held());
1176 return this_cpu_read(mem->stat->count[MEM_CGROUP_ON_MOVE]) > 0;
1177}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001178
1179static bool mem_cgroup_under_move(struct mem_cgroup *mem)
1180{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001181 struct mem_cgroup *from;
1182 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001183 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001184 /*
1185 * Unlike task_move routines, we access mc.to, mc.from not under
1186 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1187 */
1188 spin_lock(&mc.lock);
1189 from = mc.from;
1190 to = mc.to;
1191 if (!from)
1192 goto unlock;
1193 if (from == mem || to == mem
1194 || (mem->use_hierarchy && css_is_ancestor(&from->css, &mem->css))
1195 || (mem->use_hierarchy && css_is_ancestor(&to->css, &mem->css)))
1196 ret = true;
1197unlock:
1198 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001199 return ret;
1200}
1201
1202static bool mem_cgroup_wait_acct_move(struct mem_cgroup *mem)
1203{
1204 if (mc.moving_task && current != mc.moving_task) {
1205 if (mem_cgroup_under_move(mem)) {
1206 DEFINE_WAIT(wait);
1207 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1208 /* moving charge context might have finished. */
1209 if (mc.moving_task)
1210 schedule();
1211 finish_wait(&mc.waitq, &wait);
1212 return true;
1213 }
1214 }
1215 return false;
1216}
1217
Balbir Singhe2224322009-04-02 16:57:39 -07001218/**
Kirill A. Shutemov6a6135b2010-03-10 15:22:25 -08001219 * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode.
Balbir Singhe2224322009-04-02 16:57:39 -07001220 * @memcg: The memory cgroup that went over limit
1221 * @p: Task that is going to be killed
1222 *
1223 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1224 * enabled
1225 */
1226void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1227{
1228 struct cgroup *task_cgrp;
1229 struct cgroup *mem_cgrp;
1230 /*
1231 * Need a buffer in BSS, can't rely on allocations. The code relies
1232 * on the assumption that OOM is serialized for memory controller.
1233 * If this assumption is broken, revisit this code.
1234 */
1235 static char memcg_name[PATH_MAX];
1236 int ret;
1237
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001238 if (!memcg || !p)
Balbir Singhe2224322009-04-02 16:57:39 -07001239 return;
1240
1241
1242 rcu_read_lock();
1243
1244 mem_cgrp = memcg->css.cgroup;
1245 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1246
1247 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1248 if (ret < 0) {
1249 /*
1250 * Unfortunately, we are unable to convert to a useful name
1251 * But we'll still print out the usage information
1252 */
1253 rcu_read_unlock();
1254 goto done;
1255 }
1256 rcu_read_unlock();
1257
1258 printk(KERN_INFO "Task in %s killed", memcg_name);
1259
1260 rcu_read_lock();
1261 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1262 if (ret < 0) {
1263 rcu_read_unlock();
1264 goto done;
1265 }
1266 rcu_read_unlock();
1267
1268 /*
1269 * Continues from above, so we don't need an KERN_ level
1270 */
1271 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1272done:
1273
1274 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1275 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1276 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1277 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1278 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1279 "failcnt %llu\n",
1280 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1281 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1282 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1283}
1284
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001285/*
1286 * This function returns the number of memcg under hierarchy tree. Returns
1287 * 1(self count) if no children.
1288 */
1289static int mem_cgroup_count_children(struct mem_cgroup *mem)
1290{
1291 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001292 struct mem_cgroup *iter;
1293
1294 for_each_mem_cgroup_tree(iter, mem)
1295 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001296 return num;
1297}
1298
Balbir Singh6d61ef42009-01-07 18:08:06 -08001299/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001300 * Return the memory (and swap, if configured) limit for a memcg.
1301 */
1302u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
1303{
1304 u64 limit;
1305 u64 memsw;
1306
1307 limit = res_counter_read_u64(&memcg->res, RES_LIMIT) +
1308 total_swap_pages;
1309 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1310 /*
1311 * If memsw is finite and limits the amount of swap space available
1312 * to this memcg, return that limit.
1313 */
1314 return min(limit, memsw);
1315}
1316
1317/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001318 * Visit the first child (need not be the first child as per the ordering
1319 * of the cgroup list, since we track last_scanned_child) of @mem and use
1320 * that to reclaim free pages from.
1321 */
1322static struct mem_cgroup *
1323mem_cgroup_select_victim(struct mem_cgroup *root_mem)
1324{
1325 struct mem_cgroup *ret = NULL;
1326 struct cgroup_subsys_state *css;
1327 int nextid, found;
1328
1329 if (!root_mem->use_hierarchy) {
1330 css_get(&root_mem->css);
1331 ret = root_mem;
1332 }
1333
1334 while (!ret) {
1335 rcu_read_lock();
1336 nextid = root_mem->last_scanned_child + 1;
1337 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
1338 &found);
1339 if (css && css_tryget(css))
1340 ret = container_of(css, struct mem_cgroup, css);
1341
1342 rcu_read_unlock();
1343 /* Updates scanning parameter */
1344 spin_lock(&root_mem->reclaim_param_lock);
1345 if (!css) {
1346 /* this means start scan from ID:1 */
1347 root_mem->last_scanned_child = 0;
1348 } else
1349 root_mem->last_scanned_child = found;
1350 spin_unlock(&root_mem->reclaim_param_lock);
1351 }
1352
1353 return ret;
1354}
1355
1356/*
1357 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1358 * we reclaimed from, so that we don't end up penalizing one child extensively
1359 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001360 *
1361 * root_mem is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001362 *
1363 * We give up and return to the caller when we visit root_mem twice.
1364 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001365 *
1366 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001367 */
1368static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
Balbir Singh4e416952009-09-23 15:56:39 -07001369 struct zone *zone,
Balbir Singh75822b42009-09-23 15:56:38 -07001370 gfp_t gfp_mask,
1371 unsigned long reclaim_options)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001372{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001373 struct mem_cgroup *victim;
1374 int ret, total = 0;
1375 int loop = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001376 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1377 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
Balbir Singh4e416952009-09-23 15:56:39 -07001378 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
1379 unsigned long excess = mem_cgroup_get_excess(root_mem);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001380
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001381 /* If memsw_is_minimum==1, swap-out is of-no-use. */
1382 if (root_mem->memsw_is_minimum)
1383 noswap = true;
1384
Balbir Singh4e416952009-09-23 15:56:39 -07001385 while (1) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001386 victim = mem_cgroup_select_victim(root_mem);
Balbir Singh4e416952009-09-23 15:56:39 -07001387 if (victim == root_mem) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001388 loop++;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001389 if (loop >= 1)
1390 drain_all_stock_async();
Balbir Singh4e416952009-09-23 15:56:39 -07001391 if (loop >= 2) {
1392 /*
1393 * If we have not been able to reclaim
1394 * anything, it might because there are
1395 * no reclaimable pages under this hierarchy
1396 */
1397 if (!check_soft || !total) {
1398 css_put(&victim->css);
1399 break;
1400 }
1401 /*
1402 * We want to do more targetted reclaim.
1403 * excess >> 2 is not to excessive so as to
1404 * reclaim too much, nor too less that we keep
1405 * coming back to reclaim from this cgroup
1406 */
1407 if (total >= (excess >> 2) ||
1408 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1409 css_put(&victim->css);
1410 break;
1411 }
1412 }
1413 }
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08001414 if (!mem_cgroup_local_usage(victim)) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001415 /* this cgroup's local usage == 0 */
1416 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001417 continue;
1418 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001419 /* we use swappiness of local cgroup */
Balbir Singh4e416952009-09-23 15:56:39 -07001420 if (check_soft)
1421 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
KOSAKI Motohiro14fec792010-08-10 18:03:05 -07001422 noswap, get_swappiness(victim), zone);
Balbir Singh4e416952009-09-23 15:56:39 -07001423 else
1424 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
1425 noswap, get_swappiness(victim));
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001426 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001427 /*
1428 * At shrinking usage, we can't check we should stop here or
1429 * reclaim more. It's depends on callers. last_scanned_child
1430 * will work enough for keeping fairness under tree.
1431 */
1432 if (shrink)
1433 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001434 total += ret;
Balbir Singh4e416952009-09-23 15:56:39 -07001435 if (check_soft) {
1436 if (res_counter_check_under_soft_limit(&root_mem->res))
1437 return total;
1438 } else if (mem_cgroup_check_under_limit(root_mem))
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001439 return 1 + total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001440 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001441 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001442}
1443
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001444/*
1445 * Check OOM-Killer is already running under our hierarchy.
1446 * If someone is running, return false.
1447 */
1448static bool mem_cgroup_oom_lock(struct mem_cgroup *mem)
1449{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001450 int x, lock_count = 0;
1451 struct mem_cgroup *iter;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001452
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001453 for_each_mem_cgroup_tree(iter, mem) {
1454 x = atomic_inc_return(&iter->oom_lock);
1455 lock_count = max(x, lock_count);
1456 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001457
1458 if (lock_count == 1)
1459 return true;
1460 return false;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001461}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001462
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001463static int mem_cgroup_oom_unlock(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001464{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001465 struct mem_cgroup *iter;
1466
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001467 /*
1468 * When a new child is created while the hierarchy is under oom,
1469 * mem_cgroup_oom_lock() may not be called. We have to use
1470 * atomic_add_unless() here.
1471 */
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001472 for_each_mem_cgroup_tree(iter, mem)
1473 atomic_add_unless(&iter->oom_lock, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001474 return 0;
1475}
1476
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001477
1478static DEFINE_MUTEX(memcg_oom_mutex);
1479static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1480
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001481struct oom_wait_info {
1482 struct mem_cgroup *mem;
1483 wait_queue_t wait;
1484};
1485
1486static int memcg_oom_wake_function(wait_queue_t *wait,
1487 unsigned mode, int sync, void *arg)
1488{
1489 struct mem_cgroup *wake_mem = (struct mem_cgroup *)arg;
1490 struct oom_wait_info *oom_wait_info;
1491
1492 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
1493
1494 if (oom_wait_info->mem == wake_mem)
1495 goto wakeup;
1496 /* if no hierarchy, no match */
1497 if (!oom_wait_info->mem->use_hierarchy || !wake_mem->use_hierarchy)
1498 return 0;
1499 /*
1500 * Both of oom_wait_info->mem and wake_mem are stable under us.
1501 * Then we can use css_is_ancestor without taking care of RCU.
1502 */
1503 if (!css_is_ancestor(&oom_wait_info->mem->css, &wake_mem->css) &&
1504 !css_is_ancestor(&wake_mem->css, &oom_wait_info->mem->css))
1505 return 0;
1506
1507wakeup:
1508 return autoremove_wake_function(wait, mode, sync, arg);
1509}
1510
1511static void memcg_wakeup_oom(struct mem_cgroup *mem)
1512{
1513 /* for filtering, pass "mem" as argument. */
1514 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, mem);
1515}
1516
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001517static void memcg_oom_recover(struct mem_cgroup *mem)
1518{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001519 if (mem && atomic_read(&mem->oom_lock))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001520 memcg_wakeup_oom(mem);
1521}
1522
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001523/*
1524 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
1525 */
1526bool mem_cgroup_handle_oom(struct mem_cgroup *mem, gfp_t mask)
1527{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001528 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001529 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001530
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001531 owait.mem = mem;
1532 owait.wait.flags = 0;
1533 owait.wait.func = memcg_oom_wake_function;
1534 owait.wait.private = current;
1535 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001536 need_to_kill = true;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001537 /* At first, try to OOM lock hierarchy under mem.*/
1538 mutex_lock(&memcg_oom_mutex);
1539 locked = mem_cgroup_oom_lock(mem);
1540 /*
1541 * Even if signal_pending(), we can't quit charge() loop without
1542 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
1543 * under OOM is always welcomed, use TASK_KILLABLE here.
1544 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001545 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
1546 if (!locked || mem->oom_kill_disable)
1547 need_to_kill = false;
1548 if (locked)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07001549 mem_cgroup_oom_notify(mem);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001550 mutex_unlock(&memcg_oom_mutex);
1551
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001552 if (need_to_kill) {
1553 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001554 mem_cgroup_out_of_memory(mem, mask);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001555 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001556 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001557 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001558 }
1559 mutex_lock(&memcg_oom_mutex);
1560 mem_cgroup_oom_unlock(mem);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001561 memcg_wakeup_oom(mem);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001562 mutex_unlock(&memcg_oom_mutex);
1563
1564 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
1565 return false;
1566 /* Give chance to dying process */
1567 schedule_timeout(1);
1568 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001569}
1570
Balbir Singhd69b0422009-06-17 16:26:34 -07001571/*
1572 * Currently used to update mapped file statistics, but the routine can be
1573 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001574 *
1575 * Notes: Race condition
1576 *
1577 * We usually use page_cgroup_lock() for accessing page_cgroup member but
1578 * it tends to be costly. But considering some conditions, we doesn't need
1579 * to do so _always_.
1580 *
1581 * Considering "charge", lock_page_cgroup() is not required because all
1582 * file-stat operations happen after a page is attached to radix-tree. There
1583 * are no race with "charge".
1584 *
1585 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
1586 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
1587 * if there are race with "uncharge". Statistics itself is properly handled
1588 * by flags.
1589 *
1590 * Considering "move", this is an only case we see a race. To make the race
1591 * small, we check MEM_CGROUP_ON_MOVE percpu value and detect there are
1592 * possibility of race condition. If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07001593 */
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001594void mem_cgroup_update_file_mapped(struct page *page, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07001595{
1596 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001597 struct page_cgroup *pc = lookup_page_cgroup(page);
1598 bool need_unlock = false;
Balbir Singhd69b0422009-06-17 16:26:34 -07001599
Balbir Singhd69b0422009-06-17 16:26:34 -07001600 if (unlikely(!pc))
1601 return;
1602
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001603 rcu_read_lock();
Balbir Singhd69b0422009-06-17 16:26:34 -07001604 mem = pc->mem_cgroup;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001605 if (unlikely(!mem || !PageCgroupUsed(pc)))
1606 goto out;
1607 /* pc->mem_cgroup is unstable ? */
1608 if (unlikely(mem_cgroup_stealed(mem))) {
1609 /* take a lock against to access pc->mem_cgroup */
1610 lock_page_cgroup(pc);
1611 need_unlock = true;
1612 mem = pc->mem_cgroup;
1613 if (!mem || !PageCgroupUsed(pc))
1614 goto out;
1615 }
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07001616 if (val > 0) {
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001617 this_cpu_inc(mem->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07001618 SetPageCgroupFileMapped(pc);
1619 } else {
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001620 this_cpu_dec(mem->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
KAMEZAWA Hiroyuki0c270f82010-10-27 15:33:39 -07001621 if (!page_mapped(page)) /* for race between dec->inc counter */
1622 ClearPageCgroupFileMapped(pc);
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07001623 }
Balbir Singhd69b0422009-06-17 16:26:34 -07001624
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001625out:
1626 if (unlikely(need_unlock))
1627 unlock_page_cgroup(pc);
1628 rcu_read_unlock();
1629 return;
Balbir Singhd69b0422009-06-17 16:26:34 -07001630}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001631
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001632/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001633 * size of first charge trial. "32" comes from vmscan.c's magic value.
1634 * TODO: maybe necessary to use big numbers in big irons.
1635 */
1636#define CHARGE_SIZE (32 * PAGE_SIZE)
1637struct memcg_stock_pcp {
1638 struct mem_cgroup *cached; /* this never be root cgroup */
1639 int charge;
1640 struct work_struct work;
1641};
1642static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
1643static atomic_t memcg_drain_count;
1644
1645/*
1646 * Try to consume stocked charge on this cpu. If success, PAGE_SIZE is consumed
1647 * from local stock and true is returned. If the stock is 0 or charges from a
1648 * cgroup which is not current target, returns false. This stock will be
1649 * refilled.
1650 */
1651static bool consume_stock(struct mem_cgroup *mem)
1652{
1653 struct memcg_stock_pcp *stock;
1654 bool ret = true;
1655
1656 stock = &get_cpu_var(memcg_stock);
1657 if (mem == stock->cached && stock->charge)
1658 stock->charge -= PAGE_SIZE;
1659 else /* need to call res_counter_charge */
1660 ret = false;
1661 put_cpu_var(memcg_stock);
1662 return ret;
1663}
1664
1665/*
1666 * Returns stocks cached in percpu to res_counter and reset cached information.
1667 */
1668static void drain_stock(struct memcg_stock_pcp *stock)
1669{
1670 struct mem_cgroup *old = stock->cached;
1671
1672 if (stock->charge) {
1673 res_counter_uncharge(&old->res, stock->charge);
1674 if (do_swap_account)
1675 res_counter_uncharge(&old->memsw, stock->charge);
1676 }
1677 stock->cached = NULL;
1678 stock->charge = 0;
1679}
1680
1681/*
1682 * This must be called under preempt disabled or must be called by
1683 * a thread which is pinned to local cpu.
1684 */
1685static void drain_local_stock(struct work_struct *dummy)
1686{
1687 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
1688 drain_stock(stock);
1689}
1690
1691/*
1692 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01001693 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001694 */
1695static void refill_stock(struct mem_cgroup *mem, int val)
1696{
1697 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
1698
1699 if (stock->cached != mem) { /* reset if necessary */
1700 drain_stock(stock);
1701 stock->cached = mem;
1702 }
1703 stock->charge += val;
1704 put_cpu_var(memcg_stock);
1705}
1706
1707/*
1708 * Tries to drain stocked charges in other cpus. This function is asynchronous
1709 * and just put a work per cpu for draining localy on each cpu. Caller can
1710 * expects some charges will be back to res_counter later but cannot wait for
1711 * it.
1712 */
1713static void drain_all_stock_async(void)
1714{
1715 int cpu;
1716 /* This function is for scheduling "drain" in asynchronous way.
1717 * The result of "drain" is not directly handled by callers. Then,
1718 * if someone is calling drain, we don't have to call drain more.
1719 * Anyway, WORK_STRUCT_PENDING check in queue_work_on() will catch if
1720 * there is a race. We just do loose check here.
1721 */
1722 if (atomic_read(&memcg_drain_count))
1723 return;
1724 /* Notify other cpus that system-wide "drain" is running */
1725 atomic_inc(&memcg_drain_count);
1726 get_online_cpus();
1727 for_each_online_cpu(cpu) {
1728 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
1729 schedule_work_on(cpu, &stock->work);
1730 }
1731 put_online_cpus();
1732 atomic_dec(&memcg_drain_count);
1733 /* We don't wait for flush_work */
1734}
1735
1736/* This is a synchronous drain interface. */
1737static void drain_all_stock_sync(void)
1738{
1739 /* called when force_empty is called */
1740 atomic_inc(&memcg_drain_count);
1741 schedule_on_each_cpu(drain_local_stock);
1742 atomic_dec(&memcg_drain_count);
1743}
1744
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001745/*
1746 * This function drains percpu counter value from DEAD cpu and
1747 * move it to local cpu. Note that this function can be preempted.
1748 */
1749static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *mem, int cpu)
1750{
1751 int i;
1752
1753 spin_lock(&mem->pcp_counter_lock);
1754 for (i = 0; i < MEM_CGROUP_STAT_DATA; i++) {
1755 s64 x = per_cpu(mem->stat->count[i], cpu);
1756
1757 per_cpu(mem->stat->count[i], cpu) = 0;
1758 mem->nocpu_base.count[i] += x;
1759 }
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001760 /* need to clear ON_MOVE value, works as a kind of lock. */
1761 per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) = 0;
1762 spin_unlock(&mem->pcp_counter_lock);
1763}
1764
1765static void synchronize_mem_cgroup_on_move(struct mem_cgroup *mem, int cpu)
1766{
1767 int idx = MEM_CGROUP_ON_MOVE;
1768
1769 spin_lock(&mem->pcp_counter_lock);
1770 per_cpu(mem->stat->count[idx], cpu) = mem->nocpu_base.count[idx];
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001771 spin_unlock(&mem->pcp_counter_lock);
1772}
1773
1774static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001775 unsigned long action,
1776 void *hcpu)
1777{
1778 int cpu = (unsigned long)hcpu;
1779 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001780 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001781
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001782 if ((action == CPU_ONLINE)) {
1783 for_each_mem_cgroup_all(iter)
1784 synchronize_mem_cgroup_on_move(iter, cpu);
1785 return NOTIFY_OK;
1786 }
1787
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001788 if ((action != CPU_DEAD) || action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001789 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001790
1791 for_each_mem_cgroup_all(iter)
1792 mem_cgroup_drain_pcp_counter(iter, cpu);
1793
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001794 stock = &per_cpu(memcg_stock, cpu);
1795 drain_stock(stock);
1796 return NOTIFY_OK;
1797}
1798
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001799
1800/* See __mem_cgroup_try_charge() for details */
1801enum {
1802 CHARGE_OK, /* success */
1803 CHARGE_RETRY, /* need to retry but retry is not bad */
1804 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
1805 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
1806 CHARGE_OOM_DIE, /* the current is killed because of OOM */
1807};
1808
1809static int __mem_cgroup_do_charge(struct mem_cgroup *mem, gfp_t gfp_mask,
1810 int csize, bool oom_check)
1811{
1812 struct mem_cgroup *mem_over_limit;
1813 struct res_counter *fail_res;
1814 unsigned long flags = 0;
1815 int ret;
1816
1817 ret = res_counter_charge(&mem->res, csize, &fail_res);
1818
1819 if (likely(!ret)) {
1820 if (!do_swap_account)
1821 return CHARGE_OK;
1822 ret = res_counter_charge(&mem->memsw, csize, &fail_res);
1823 if (likely(!ret))
1824 return CHARGE_OK;
1825
1826 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
1827 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
1828 } else
1829 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
1830
1831 if (csize > PAGE_SIZE) /* change csize and retry */
1832 return CHARGE_RETRY;
1833
1834 if (!(gfp_mask & __GFP_WAIT))
1835 return CHARGE_WOULDBLOCK;
1836
1837 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
1838 gfp_mask, flags);
1839 /*
1840 * try_to_free_mem_cgroup_pages() might not give us a full
1841 * picture of reclaim. Some pages are reclaimed and might be
1842 * moved to swap cache or just unmapped from the cgroup.
1843 * Check the limit again to see if the reclaim reduced the
1844 * current usage of the cgroup before giving up
1845 */
1846 if (ret || mem_cgroup_check_under_limit(mem_over_limit))
1847 return CHARGE_RETRY;
1848
1849 /*
1850 * At task move, charge accounts can be doubly counted. So, it's
1851 * better to wait until the end of task_move if something is going on.
1852 */
1853 if (mem_cgroup_wait_acct_move(mem_over_limit))
1854 return CHARGE_RETRY;
1855
1856 /* If we don't need to call oom-killer at el, return immediately */
1857 if (!oom_check)
1858 return CHARGE_NOMEM;
1859 /* check OOM */
1860 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask))
1861 return CHARGE_OOM_DIE;
1862
1863 return CHARGE_RETRY;
1864}
1865
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001866/*
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001867 * Unlike exported interface, "oom" parameter is added. if oom==true,
1868 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001869 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001870static int __mem_cgroup_try_charge(struct mm_struct *mm,
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001871 gfp_t gfp_mask, struct mem_cgroup **memcg, bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001872{
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001873 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
1874 struct mem_cgroup *mem = NULL;
1875 int ret;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001876 int csize = CHARGE_SIZE;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001877
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001878 /*
1879 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
1880 * in system level. So, allow to go ahead dying process in addition to
1881 * MEMDIE process.
1882 */
1883 if (unlikely(test_thread_flag(TIF_MEMDIE)
1884 || fatal_signal_pending(current)))
1885 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001886
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001887 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08001888 * We always charge the cgroup the mm_struct belongs to.
1889 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001890 * thread group leader migrates. It's possible that mm is not
1891 * set, if so charge the init_mm (happens for pagecache usage).
1892 */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001893 if (!*memcg && !mm)
1894 goto bypass;
1895again:
1896 if (*memcg) { /* css should be a valid one */
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001897 mem = *memcg;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001898 VM_BUG_ON(css_is_removed(&mem->css));
1899 if (mem_cgroup_is_root(mem))
1900 goto done;
1901 if (consume_stock(mem))
1902 goto done;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001903 css_get(&mem->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001904 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001905 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001906
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001907 rcu_read_lock();
1908 p = rcu_dereference(mm->owner);
1909 VM_BUG_ON(!p);
1910 /*
1911 * because we don't have task_lock(), "p" can exit while
1912 * we're here. In that case, "mem" can point to root
1913 * cgroup but never be NULL. (and task_struct itself is freed
1914 * by RCU, cgroup itself is RCU safe.) Then, we have small
1915 * risk here to get wrong cgroup. But such kind of mis-account
1916 * by race always happens because we don't have cgroup_mutex().
1917 * It's overkill and we allow that small race, here.
1918 */
1919 mem = mem_cgroup_from_task(p);
1920 VM_BUG_ON(!mem);
1921 if (mem_cgroup_is_root(mem)) {
1922 rcu_read_unlock();
1923 goto done;
1924 }
1925 if (consume_stock(mem)) {
1926 /*
1927 * It seems dagerous to access memcg without css_get().
1928 * But considering how consume_stok works, it's not
1929 * necessary. If consume_stock success, some charges
1930 * from this memcg are cached on this cpu. So, we
1931 * don't need to call css_get()/css_tryget() before
1932 * calling consume_stock().
1933 */
1934 rcu_read_unlock();
1935 goto done;
1936 }
1937 /* after here, we may be blocked. we need to get refcnt */
1938 if (!css_tryget(&mem->css)) {
1939 rcu_read_unlock();
1940 goto again;
1941 }
1942 rcu_read_unlock();
1943 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001944
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001945 do {
1946 bool oom_check;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001947
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001948 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001949 if (fatal_signal_pending(current)) {
1950 css_put(&mem->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001951 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001952 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001953
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001954 oom_check = false;
1955 if (oom && !nr_oom_retries) {
1956 oom_check = true;
1957 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
1958 }
Balbir Singh6d61ef42009-01-07 18:08:06 -08001959
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001960 ret = __mem_cgroup_do_charge(mem, gfp_mask, csize, oom_check);
1961
1962 switch (ret) {
1963 case CHARGE_OK:
1964 break;
1965 case CHARGE_RETRY: /* not in OOM situation but retry */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001966 csize = PAGE_SIZE;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001967 css_put(&mem->css);
1968 mem = NULL;
1969 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001970 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001971 css_put(&mem->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001972 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001973 case CHARGE_NOMEM: /* OOM routine works */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001974 if (!oom) {
1975 css_put(&mem->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001976 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001977 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001978 /* If oom, we never return -ENOMEM */
1979 nr_oom_retries--;
1980 break;
1981 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001982 css_put(&mem->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001983 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08001984 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001985 } while (ret != CHARGE_OK);
1986
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001987 if (csize > PAGE_SIZE)
1988 refill_stock(mem, csize - PAGE_SIZE);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001989 css_put(&mem->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001990done:
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001991 *memcg = mem;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001992 return 0;
1993nomem:
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001994 *memcg = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001995 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001996bypass:
1997 *memcg = NULL;
1998 return 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001999}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002000
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002001/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002002 * Somemtimes we have to undo a charge we got by try_charge().
2003 * This function is for that and do uncharge, put css's refcnt.
2004 * gotten by try_charge().
2005 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002006static void __mem_cgroup_cancel_charge(struct mem_cgroup *mem,
2007 unsigned long count)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002008{
2009 if (!mem_cgroup_is_root(mem)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002010 res_counter_uncharge(&mem->res, PAGE_SIZE * count);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002011 if (do_swap_account)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002012 res_counter_uncharge(&mem->memsw, PAGE_SIZE * count);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002013 }
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002014}
2015
2016static void mem_cgroup_cancel_charge(struct mem_cgroup *mem)
2017{
2018 __mem_cgroup_cancel_charge(mem, 1);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002019}
2020
2021/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002022 * A helper function to get mem_cgroup from ID. must be called under
2023 * rcu_read_lock(). The caller must check css_is_removed() or some if
2024 * it's concern. (dropping refcnt from swap can be called against removed
2025 * memcg.)
2026 */
2027static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2028{
2029 struct cgroup_subsys_state *css;
2030
2031 /* ID 0 is unused ID */
2032 if (!id)
2033 return NULL;
2034 css = css_lookup(&mem_cgroup_subsys, id);
2035 if (!css)
2036 return NULL;
2037 return container_of(css, struct mem_cgroup, css);
2038}
2039
Wu Fengguange42d9d52009-12-16 12:19:59 +01002040struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002041{
Wu Fengguange42d9d52009-12-16 12:19:59 +01002042 struct mem_cgroup *mem = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002043 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002044 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002045 swp_entry_t ent;
2046
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002047 VM_BUG_ON(!PageLocked(page));
2048
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002049 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002050 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002051 if (PageCgroupUsed(pc)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002052 mem = pc->mem_cgroup;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002053 if (mem && !css_tryget(&mem->css))
2054 mem = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002055 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002056 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002057 id = lookup_swap_cgroup(ent);
2058 rcu_read_lock();
2059 mem = mem_cgroup_lookup(id);
2060 if (mem && !css_tryget(&mem->css))
2061 mem = NULL;
2062 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002063 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002064 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002065 return mem;
2066}
2067
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002068/*
Daisuke Nishimuraa5e924f2009-01-07 18:08:28 -08002069 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002070 * USED state. If already USED, uncharge and return.
2071 */
2072
2073static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
2074 struct page_cgroup *pc,
2075 enum charge_type ctype)
2076{
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002077 /* try_charge() can return NULL to *memcg, taking care of it. */
2078 if (!mem)
2079 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002080
2081 lock_page_cgroup(pc);
2082 if (unlikely(PageCgroupUsed(pc))) {
2083 unlock_page_cgroup(pc);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002084 mem_cgroup_cancel_charge(mem);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002085 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002086 }
Balbir Singh4b3bde42009-09-23 15:56:32 -07002087
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002088 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002089 /*
2090 * We access a page_cgroup asynchronously without lock_page_cgroup().
2091 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2092 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2093 * before USED bit, we need memory barrier here.
2094 * See mem_cgroup_add_lru_list(), etc.
2095 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002096 smp_wmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -07002097 switch (ctype) {
2098 case MEM_CGROUP_CHARGE_TYPE_CACHE:
2099 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
2100 SetPageCgroupCache(pc);
2101 SetPageCgroupUsed(pc);
2102 break;
2103 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
2104 ClearPageCgroupCache(pc);
2105 SetPageCgroupUsed(pc);
2106 break;
2107 default:
2108 break;
2109 }
Hugh Dickins3be91272008-02-07 00:14:19 -08002110
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002111 mem_cgroup_charge_statistics(mem, pc, true);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002112
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002113 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002114 /*
2115 * "charge_statistics" updated event counter. Then, check it.
2116 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2117 * if they exceeds softlimit.
2118 */
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002119 memcg_check_events(mem, pc->page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002120}
2121
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002122/**
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002123 * __mem_cgroup_move_account - move account of the page
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002124 * @pc: page_cgroup of the page.
2125 * @from: mem_cgroup which the page is moved from.
2126 * @to: mem_cgroup which the page is moved to. @from != @to.
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002127 * @uncharge: whether we should call uncharge and css_put against @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002128 *
2129 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002130 * - page is not on LRU (isolate_page() is useful.)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002131 * - the pc is locked, used, and ->mem_cgroup points to @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002132 *
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002133 * This function doesn't do "charge" nor css_get to new cgroup. It should be
2134 * done by a caller(__mem_cgroup_try_charge would be usefull). If @uncharge is
2135 * true, this function does "uncharge" from old cgroup, but it doesn't if
2136 * @uncharge is false, so a caller should do "uncharge".
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002137 */
2138
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002139static void __mem_cgroup_move_account(struct page_cgroup *pc,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002140 struct mem_cgroup *from, struct mem_cgroup *to, bool uncharge)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002141{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002142 VM_BUG_ON(from == to);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002143 VM_BUG_ON(PageLRU(pc->page));
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002144 VM_BUG_ON(!PageCgroupLocked(pc));
2145 VM_BUG_ON(!PageCgroupUsed(pc));
2146 VM_BUG_ON(pc->mem_cgroup != from);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002147
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002148 if (PageCgroupFileMapped(pc)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08002149 /* Update mapped_file data for mem_cgroup */
2150 preempt_disable();
2151 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2152 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2153 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07002154 }
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002155 mem_cgroup_charge_statistics(from, pc, false);
2156 if (uncharge)
2157 /* This is not "cancel", but cancel_charge does all we need. */
2158 mem_cgroup_cancel_charge(from);
Balbir Singhd69b0422009-06-17 16:26:34 -07002159
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002160 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002161 pc->mem_cgroup = to;
2162 mem_cgroup_charge_statistics(to, pc, true);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002163 /*
2164 * We charges against "to" which may not have any tasks. Then, "to"
2165 * can be under rmdir(). But in current implementation, caller of
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08002166 * this function is just force_empty() and move charge, so it's
2167 * garanteed that "to" is never removed. So, we don't check rmdir
2168 * status here.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002169 */
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002170}
2171
2172/*
2173 * check whether the @pc is valid for moving account and call
2174 * __mem_cgroup_move_account()
2175 */
2176static int mem_cgroup_move_account(struct page_cgroup *pc,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002177 struct mem_cgroup *from, struct mem_cgroup *to, bool uncharge)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002178{
2179 int ret = -EINVAL;
2180 lock_page_cgroup(pc);
2181 if (PageCgroupUsed(pc) && pc->mem_cgroup == from) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002182 __mem_cgroup_move_account(pc, from, to, uncharge);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002183 ret = 0;
2184 }
2185 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002186 /*
2187 * check events
2188 */
2189 memcg_check_events(to, pc->page);
2190 memcg_check_events(from, pc->page);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002191 return ret;
2192}
2193
2194/*
2195 * move charges to its parent.
2196 */
2197
2198static int mem_cgroup_move_parent(struct page_cgroup *pc,
2199 struct mem_cgroup *child,
2200 gfp_t gfp_mask)
2201{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002202 struct page *page = pc->page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002203 struct cgroup *cg = child->css.cgroup;
2204 struct cgroup *pcg = cg->parent;
2205 struct mem_cgroup *parent;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002206 int ret;
2207
2208 /* Is ROOT ? */
2209 if (!pcg)
2210 return -EINVAL;
2211
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002212 ret = -EBUSY;
2213 if (!get_page_unless_zero(page))
2214 goto out;
2215 if (isolate_lru_page(page))
2216 goto put;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002217
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002218 parent = mem_cgroup_from_cont(pcg);
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002219 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002220 if (ret || !parent)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002221 goto put_back;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002222
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002223 ret = mem_cgroup_move_account(pc, child, parent, true);
2224 if (ret)
2225 mem_cgroup_cancel_charge(parent);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002226put_back:
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002227 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002228put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08002229 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002230out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002231 return ret;
2232}
2233
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002234/*
2235 * Charge the memory controller for page usage.
2236 * Return
2237 * 0 if the charge was successful
2238 * < 0 if the cgroup is over its limit
2239 */
2240static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002241 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002242{
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002243 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002244 struct page_cgroup *pc;
2245 int ret;
2246
2247 pc = lookup_page_cgroup(page);
2248 /* can happen at boot */
2249 if (unlikely(!pc))
2250 return 0;
2251 prefetchw(pc);
2252
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002253 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002254 if (ret || !mem)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002255 return ret;
2256
2257 __mem_cgroup_commit_charge(mem, pc, ctype);
2258 return 0;
2259}
2260
2261int mem_cgroup_newpage_charge(struct page *page,
2262 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002263{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002264 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002265 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002266 if (PageCompound(page))
2267 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002268 /*
2269 * If already mapped, we don't have to account.
2270 * If page cache, page->mapping has address_space.
2271 * But page->mapping may have out-of-use anon_vma pointer,
2272 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
2273 * is NULL.
2274 */
2275 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
2276 return 0;
2277 if (unlikely(!mm))
2278 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002279 return mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002280 MEM_CGROUP_CHARGE_TYPE_MAPPED);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002281}
2282
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002283static void
2284__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2285 enum charge_type ctype);
2286
Balbir Singhe1a1cd52008-02-07 00:14:02 -08002287int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
2288 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08002289{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002290 int ret;
2291
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002292 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002293 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002294 if (PageCompound(page))
2295 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002296 /*
2297 * Corner case handling. This is called from add_to_page_cache()
2298 * in usual. But some FS (shmem) precharges this page before calling it
2299 * and call add_to_page_cache() with GFP_NOWAIT.
2300 *
2301 * For GFP_NOWAIT case, the page may be pre-charged before calling
2302 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
2303 * charge twice. (It works but has to pay a bit larger cost.)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002304 * And when the page is SwapCache, it should take swap information
2305 * into account. This is under lock_page() now.
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002306 */
2307 if (!(gfp_mask & __GFP_WAIT)) {
2308 struct page_cgroup *pc;
2309
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002310 pc = lookup_page_cgroup(page);
2311 if (!pc)
2312 return 0;
2313 lock_page_cgroup(pc);
2314 if (PageCgroupUsed(pc)) {
2315 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002316 return 0;
2317 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002318 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002319 }
2320
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002321 if (unlikely(!mm))
Balbir Singh8697d332008-02-07 00:13:59 -08002322 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002323
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -07002324 if (page_is_file_cache(page))
2325 return mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002326 MEM_CGROUP_CHARGE_TYPE_CACHE);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002327
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002328 /* shmem */
2329 if (PageSwapCache(page)) {
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002330 struct mem_cgroup *mem = NULL;
2331
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002332 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2333 if (!ret)
2334 __mem_cgroup_commit_charge_swapin(page, mem,
2335 MEM_CGROUP_CHARGE_TYPE_SHMEM);
2336 } else
2337 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002338 MEM_CGROUP_CHARGE_TYPE_SHMEM);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002339
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002340 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002341}
2342
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002343/*
2344 * While swap-in, try_charge -> commit or cancel, the page is locked.
2345 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02002346 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002347 * "commit()" or removed by "cancel()"
2348 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002349int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
2350 struct page *page,
2351 gfp_t mask, struct mem_cgroup **ptr)
2352{
2353 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002354 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002355
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002356 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002357 return 0;
2358
2359 if (!do_swap_account)
2360 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002361 /*
2362 * A racing thread's fault, or swapoff, may have already updated
Hugh Dickins407f9c82009-12-14 17:59:30 -08002363 * the pte, and even removed page from swap cache: in those cases
2364 * do_swap_page()'s pte_same() test will fail; but there's also a
2365 * KSM case which does need to charge the page.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002366 */
2367 if (!PageSwapCache(page))
Hugh Dickins407f9c82009-12-14 17:59:30 -08002368 goto charge_cur_mm;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002369 mem = try_get_mem_cgroup_from_page(page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002370 if (!mem)
2371 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002372 *ptr = mem;
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002373 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002374 css_put(&mem->css);
2375 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002376charge_cur_mm:
2377 if (unlikely(!mm))
2378 mm = &init_mm;
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002379 return __mem_cgroup_try_charge(mm, mask, ptr, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002380}
2381
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002382static void
2383__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2384 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002385{
2386 struct page_cgroup *pc;
2387
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002388 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002389 return;
2390 if (!ptr)
2391 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002392 cgroup_exclude_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002393 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08002394 mem_cgroup_lru_del_before_commit_swapcache(page);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002395 __mem_cgroup_commit_charge(ptr, pc, ctype);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08002396 mem_cgroup_lru_add_after_commit_swapcache(page);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002397 /*
2398 * Now swap is on-memory. This means this page may be
2399 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002400 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
2401 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
2402 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002403 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002404 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002405 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002406 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002407 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002408
2409 id = swap_cgroup_record(ent, 0);
2410 rcu_read_lock();
2411 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002412 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002413 /*
2414 * This recorded memcg can be obsolete one. So, avoid
2415 * calling css_tryget
2416 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002417 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002418 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002419 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002420 mem_cgroup_put(memcg);
2421 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002422 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002423 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002424 /*
2425 * At swapin, we may charge account against cgroup which has no tasks.
2426 * So, rmdir()->pre_destroy() can be called while we do this charge.
2427 * In that case, we need to call pre_destroy() again. check it here.
2428 */
2429 cgroup_release_and_wakeup_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002430}
2431
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002432void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
2433{
2434 __mem_cgroup_commit_charge_swapin(page, ptr,
2435 MEM_CGROUP_CHARGE_TYPE_MAPPED);
2436}
2437
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002438void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
2439{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002440 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002441 return;
2442 if (!mem)
2443 return;
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002444 mem_cgroup_cancel_charge(mem);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002445}
2446
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002447static void
2448__do_uncharge(struct mem_cgroup *mem, const enum charge_type ctype)
2449{
2450 struct memcg_batch_info *batch = NULL;
2451 bool uncharge_memsw = true;
2452 /* If swapout, usage of swap doesn't decrease */
2453 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2454 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002455
2456 batch = &current->memcg_batch;
2457 /*
2458 * In usual, we do css_get() when we remember memcg pointer.
2459 * But in this case, we keep res->usage until end of a series of
2460 * uncharges. Then, it's ok to ignore memcg's refcnt.
2461 */
2462 if (!batch->memcg)
2463 batch->memcg = mem;
2464 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002465 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
2466 * In those cases, all pages freed continously can be expected to be in
2467 * the same cgroup and we have chance to coalesce uncharges.
2468 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
2469 * because we want to do uncharge as soon as possible.
2470 */
2471
2472 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
2473 goto direct_uncharge;
2474
2475 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002476 * In typical case, batch->memcg == mem. This means we can
2477 * merge a series of uncharges to an uncharge of res_counter.
2478 * If not, we uncharge res_counter ony by one.
2479 */
2480 if (batch->memcg != mem)
2481 goto direct_uncharge;
2482 /* remember freed charge and uncharge it later */
2483 batch->bytes += PAGE_SIZE;
2484 if (uncharge_memsw)
2485 batch->memsw_bytes += PAGE_SIZE;
2486 return;
2487direct_uncharge:
2488 res_counter_uncharge(&mem->res, PAGE_SIZE);
2489 if (uncharge_memsw)
2490 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002491 if (unlikely(batch->memcg != mem))
2492 memcg_oom_recover(mem);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002493 return;
2494}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002495
Balbir Singh8697d332008-02-07 00:13:59 -08002496/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002497 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002498 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002499static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002500__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002501{
Hugh Dickins82895462008-03-04 14:29:08 -08002502 struct page_cgroup *pc;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002503 struct mem_cgroup *mem = NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002504
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002505 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002506 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07002507
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002508 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002509 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002510
Balbir Singh8697d332008-02-07 00:13:59 -08002511 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08002512 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08002513 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002514 pc = lookup_page_cgroup(page);
2515 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002516 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002517
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002518 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002519
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002520 mem = pc->mem_cgroup;
2521
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002522 if (!PageCgroupUsed(pc))
2523 goto unlock_out;
2524
2525 switch (ctype) {
2526 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002527 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002528 /* See mem_cgroup_prepare_migration() */
2529 if (page_mapped(page) || PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002530 goto unlock_out;
2531 break;
2532 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
2533 if (!PageAnon(page)) { /* Shared memory */
2534 if (page->mapping && !page_is_file_cache(page))
2535 goto unlock_out;
2536 } else if (page_mapped(page)) /* Anon */
2537 goto unlock_out;
2538 break;
2539 default:
2540 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002541 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002542
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002543 mem_cgroup_charge_statistics(mem, pc, false);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002544
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002545 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08002546 /*
2547 * pc->mem_cgroup is not cleared here. It will be accessed when it's
2548 * freed from LRU. This is safe because uncharged page is expected not
2549 * to be reused (freed soon). Exception is SwapCache, it's handled by
2550 * special functions.
2551 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08002552
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002553 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002554 /*
2555 * even after unlock, we have mem->res.usage here and this memcg
2556 * will never be freed.
2557 */
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002558 memcg_check_events(mem, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002559 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
2560 mem_cgroup_swap_statistics(mem, true);
2561 mem_cgroup_get(mem);
2562 }
2563 if (!mem_cgroup_is_root(mem))
2564 __do_uncharge(mem, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002565
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002566 return mem;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002567
2568unlock_out:
2569 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002570 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08002571}
2572
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002573void mem_cgroup_uncharge_page(struct page *page)
2574{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002575 /* early check. */
2576 if (page_mapped(page))
2577 return;
2578 if (page->mapping && !PageAnon(page))
2579 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002580 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
2581}
2582
2583void mem_cgroup_uncharge_cache_page(struct page *page)
2584{
2585 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07002586 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002587 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
2588}
2589
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002590/*
2591 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
2592 * In that cases, pages are freed continuously and we can expect pages
2593 * are in the same memcg. All these calls itself limits the number of
2594 * pages freed at once, then uncharge_start/end() is called properly.
2595 * This may be called prural(2) times in a context,
2596 */
2597
2598void mem_cgroup_uncharge_start(void)
2599{
2600 current->memcg_batch.do_batch++;
2601 /* We can do nest. */
2602 if (current->memcg_batch.do_batch == 1) {
2603 current->memcg_batch.memcg = NULL;
2604 current->memcg_batch.bytes = 0;
2605 current->memcg_batch.memsw_bytes = 0;
2606 }
2607}
2608
2609void mem_cgroup_uncharge_end(void)
2610{
2611 struct memcg_batch_info *batch = &current->memcg_batch;
2612
2613 if (!batch->do_batch)
2614 return;
2615
2616 batch->do_batch--;
2617 if (batch->do_batch) /* If stacked, do nothing. */
2618 return;
2619
2620 if (!batch->memcg)
2621 return;
2622 /*
2623 * This "batch->memcg" is valid without any css_get/put etc...
2624 * bacause we hide charges behind us.
2625 */
2626 if (batch->bytes)
2627 res_counter_uncharge(&batch->memcg->res, batch->bytes);
2628 if (batch->memsw_bytes)
2629 res_counter_uncharge(&batch->memcg->memsw, batch->memsw_bytes);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002630 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002631 /* forget this pointer (for sanity check) */
2632 batch->memcg = NULL;
2633}
2634
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002635#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002636/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002637 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002638 * memcg information is recorded to swap_cgroup of "ent"
2639 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002640void
2641mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002642{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002643 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002644 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002645
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002646 if (!swapout) /* this was a swap cache but the swap is unused ! */
2647 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
2648
2649 memcg = __mem_cgroup_uncharge_common(page, ctype);
2650
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002651 /*
2652 * record memcg information, if swapout && memcg != NULL,
2653 * mem_cgroup_get() was called in uncharge().
2654 */
2655 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002656 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002657}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002658#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002659
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002660#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2661/*
2662 * called from swap_entry_free(). remove record in swap_cgroup and
2663 * uncharge "memsw" account.
2664 */
2665void mem_cgroup_uncharge_swap(swp_entry_t ent)
2666{
2667 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002668 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002669
2670 if (!do_swap_account)
2671 return;
2672
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002673 id = swap_cgroup_record(ent, 0);
2674 rcu_read_lock();
2675 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002676 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002677 /*
2678 * We uncharge this because swap is freed.
2679 * This memcg can be obsolete one. We avoid calling css_tryget
2680 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002681 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002682 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002683 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002684 mem_cgroup_put(memcg);
2685 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002686 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002687}
Daisuke Nishimura02491442010-03-10 15:22:17 -08002688
2689/**
2690 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
2691 * @entry: swap entry to be moved
2692 * @from: mem_cgroup which the entry is moved from
2693 * @to: mem_cgroup which the entry is moved to
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002694 * @need_fixup: whether we should fixup res_counters and refcounts.
Daisuke Nishimura02491442010-03-10 15:22:17 -08002695 *
2696 * It succeeds only when the swap_cgroup's record for this entry is the same
2697 * as the mem_cgroup's id of @from.
2698 *
2699 * Returns 0 on success, -EINVAL on failure.
2700 *
2701 * The caller must have charged to @to, IOW, called res_counter_charge() about
2702 * both res and memsw, and called css_get().
2703 */
2704static int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002705 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08002706{
2707 unsigned short old_id, new_id;
2708
2709 old_id = css_id(&from->css);
2710 new_id = css_id(&to->css);
2711
2712 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08002713 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08002714 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002715 /*
2716 * This function is only called from task migration context now.
2717 * It postpones res_counter and refcount handling till the end
2718 * of task migration(mem_cgroup_clear_mc()) for performance
2719 * improvement. But we cannot postpone mem_cgroup_get(to)
2720 * because if the process that has been moved to @to does
2721 * swap-in, the refcount of @to might be decreased to 0.
2722 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08002723 mem_cgroup_get(to);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002724 if (need_fixup) {
2725 if (!mem_cgroup_is_root(from))
2726 res_counter_uncharge(&from->memsw, PAGE_SIZE);
2727 mem_cgroup_put(from);
2728 /*
2729 * we charged both to->res and to->memsw, so we should
2730 * uncharge to->res.
2731 */
2732 if (!mem_cgroup_is_root(to))
2733 res_counter_uncharge(&to->res, PAGE_SIZE);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002734 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08002735 return 0;
2736 }
2737 return -EINVAL;
2738}
2739#else
2740static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002741 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08002742{
2743 return -EINVAL;
2744}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002745#endif
2746
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002747/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002748 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
2749 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002750 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002751int mem_cgroup_prepare_migration(struct page *page,
2752 struct page *newpage, struct mem_cgroup **ptr)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002753{
2754 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002755 struct mem_cgroup *mem = NULL;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002756 enum charge_type ctype;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002757 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08002758
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002759 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07002760 return 0;
2761
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002762 pc = lookup_page_cgroup(page);
2763 lock_page_cgroup(pc);
2764 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002765 mem = pc->mem_cgroup;
2766 css_get(&mem->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002767 /*
2768 * At migrating an anonymous page, its mapcount goes down
2769 * to 0 and uncharge() will be called. But, even if it's fully
2770 * unmapped, migration may fail and this page has to be
2771 * charged again. We set MIGRATION flag here and delay uncharge
2772 * until end_migration() is called
2773 *
2774 * Corner Case Thinking
2775 * A)
2776 * When the old page was mapped as Anon and it's unmap-and-freed
2777 * while migration was ongoing.
2778 * If unmap finds the old page, uncharge() of it will be delayed
2779 * until end_migration(). If unmap finds a new page, it's
2780 * uncharged when it make mapcount to be 1->0. If unmap code
2781 * finds swap_migration_entry, the new page will not be mapped
2782 * and end_migration() will find it(mapcount==0).
2783 *
2784 * B)
2785 * When the old page was mapped but migraion fails, the kernel
2786 * remaps it. A charge for it is kept by MIGRATION flag even
2787 * if mapcount goes down to 0. We can do remap successfully
2788 * without charging it again.
2789 *
2790 * C)
2791 * The "old" page is under lock_page() until the end of
2792 * migration, so, the old page itself will not be swapped-out.
2793 * If the new page is swapped out before end_migraton, our
2794 * hook to usual swap-out path will catch the event.
2795 */
2796 if (PageAnon(page))
2797 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08002798 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002799 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002800 /*
2801 * If the page is not charged at this point,
2802 * we return here.
2803 */
2804 if (!mem)
2805 return 0;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002806
Andrea Arcangeli93d5c9b2010-04-23 13:17:39 -04002807 *ptr = mem;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002808 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, ptr, false);
2809 css_put(&mem->css);/* drop extra refcnt */
2810 if (ret || *ptr == NULL) {
2811 if (PageAnon(page)) {
2812 lock_page_cgroup(pc);
2813 ClearPageCgroupMigration(pc);
2814 unlock_page_cgroup(pc);
2815 /*
2816 * The old page may be fully unmapped while we kept it.
2817 */
2818 mem_cgroup_uncharge_page(page);
2819 }
2820 return -ENOMEM;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002821 }
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002822 /*
2823 * We charge new page before it's used/mapped. So, even if unlock_page()
2824 * is called before end_migration, we can catch all events on this new
2825 * page. In the case new page is migrated but not remapped, new page's
2826 * mapcount will be finally 0 and we call uncharge in end_migration().
2827 */
2828 pc = lookup_page_cgroup(newpage);
2829 if (PageAnon(page))
2830 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
2831 else if (page_is_file_cache(page))
2832 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
2833 else
2834 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
2835 __mem_cgroup_commit_charge(mem, pc, ctype);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002836 return ret;
2837}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08002838
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002839/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002840void mem_cgroup_end_migration(struct mem_cgroup *mem,
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002841 struct page *oldpage, struct page *newpage)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002842{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002843 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002844 struct page_cgroup *pc;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002845
2846 if (!mem)
2847 return;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002848 /* blocks rmdir() */
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002849 cgroup_exclude_rmdir(&mem->css);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002850 /* at migration success, oldpage->mapping is NULL. */
2851 if (oldpage->mapping) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002852 used = oldpage;
2853 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002854 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002855 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002856 unused = oldpage;
2857 }
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002858 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002859 * We disallowed uncharge of pages under migration because mapcount
2860 * of the page goes down to zero, temporarly.
2861 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002862 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002863 pc = lookup_page_cgroup(oldpage);
2864 lock_page_cgroup(pc);
2865 ClearPageCgroupMigration(pc);
2866 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002867
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002868 __mem_cgroup_uncharge_common(unused, MEM_CGROUP_CHARGE_TYPE_FORCE);
2869
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002870 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002871 * If a page is a file cache, radix-tree replacement is very atomic
2872 * and we can skip this check. When it was an Anon page, its mapcount
2873 * goes down to 0. But because we added MIGRATION flage, it's not
2874 * uncharged yet. There are several case but page->mapcount check
2875 * and USED bit check in mem_cgroup_uncharge_page() will do enough
2876 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002877 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002878 if (PageAnon(used))
2879 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002880 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002881 * At migration, we may charge account against cgroup which has no
2882 * tasks.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002883 * So, rmdir()->pre_destroy() can be called while we do this charge.
2884 * In that case, we need to call pre_destroy() again. check it here.
2885 */
2886 cgroup_release_and_wakeup_rmdir(&mem->css);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002887}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08002888
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002889/*
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002890 * A call to try to shrink memory usage on charge failure at shmem's swapin.
2891 * Calling hierarchical_reclaim is not enough because we should update
2892 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
2893 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
2894 * not from the memcg which this page would be charged to.
2895 * try_charge_swapin does all of these works properly.
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002896 */
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002897int mem_cgroup_shmem_charge_fallback(struct page *page,
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002898 struct mm_struct *mm,
2899 gfp_t gfp_mask)
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002900{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002901 struct mem_cgroup *mem = NULL;
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002902 int ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002903
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002904 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002905 return 0;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002906
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002907 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2908 if (!ret)
2909 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002910
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002911 return ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002912}
2913
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002914static DEFINE_MUTEX(set_limit_mutex);
2915
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08002916static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002917 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002918{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002919 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002920 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002921 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002922 int children = mem_cgroup_count_children(memcg);
2923 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002924 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002925
2926 /*
2927 * For keeping hierarchical_reclaim simple, how long we should retry
2928 * is depends on callers. We set our retry-count to be function
2929 * of # of children which we should visit in this loop.
2930 */
2931 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
2932
2933 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002934
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002935 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002936 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002937 if (signal_pending(current)) {
2938 ret = -EINTR;
2939 break;
2940 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002941 /*
2942 * Rather than hide all in some function, I do this in
2943 * open coded manner. You see what this really does.
2944 * We have to guarantee mem->res.limit < mem->memsw.limit.
2945 */
2946 mutex_lock(&set_limit_mutex);
2947 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2948 if (memswlimit < val) {
2949 ret = -EINVAL;
2950 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002951 break;
2952 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002953
2954 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2955 if (memlimit < val)
2956 enlarge = 1;
2957
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002958 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07002959 if (!ret) {
2960 if (memswlimit == val)
2961 memcg->memsw_is_minimum = true;
2962 else
2963 memcg->memsw_is_minimum = false;
2964 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002965 mutex_unlock(&set_limit_mutex);
2966
2967 if (!ret)
2968 break;
2969
Bob Liuaa20d482009-12-15 16:47:14 -08002970 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh4e416952009-09-23 15:56:39 -07002971 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002972 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
2973 /* Usage is reduced ? */
2974 if (curusage >= oldusage)
2975 retry_count--;
2976 else
2977 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002978 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002979 if (!ret && enlarge)
2980 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002981
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002982 return ret;
2983}
2984
Li Zefan338c8432009-06-17 16:27:15 -07002985static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
2986 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002987{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002988 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002989 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002990 int children = mem_cgroup_count_children(memcg);
2991 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002992 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002993
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002994 /* see mem_cgroup_resize_res_limit */
2995 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
2996 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002997 while (retry_count) {
2998 if (signal_pending(current)) {
2999 ret = -EINTR;
3000 break;
3001 }
3002 /*
3003 * Rather than hide all in some function, I do this in
3004 * open coded manner. You see what this really does.
3005 * We have to guarantee mem->res.limit < mem->memsw.limit.
3006 */
3007 mutex_lock(&set_limit_mutex);
3008 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3009 if (memlimit > val) {
3010 ret = -EINVAL;
3011 mutex_unlock(&set_limit_mutex);
3012 break;
3013 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003014 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3015 if (memswlimit < val)
3016 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003017 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003018 if (!ret) {
3019 if (memlimit == val)
3020 memcg->memsw_is_minimum = true;
3021 else
3022 memcg->memsw_is_minimum = false;
3023 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003024 mutex_unlock(&set_limit_mutex);
3025
3026 if (!ret)
3027 break;
3028
Balbir Singh4e416952009-09-23 15:56:39 -07003029 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh75822b42009-09-23 15:56:38 -07003030 MEM_CGROUP_RECLAIM_NOSWAP |
3031 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003032 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003033 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003034 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003035 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003036 else
3037 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003038 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003039 if (!ret && enlarge)
3040 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003041 return ret;
3042}
3043
Balbir Singh4e416952009-09-23 15:56:39 -07003044unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07003045 gfp_t gfp_mask)
Balbir Singh4e416952009-09-23 15:56:39 -07003046{
3047 unsigned long nr_reclaimed = 0;
3048 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
3049 unsigned long reclaimed;
3050 int loop = 0;
3051 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003052 unsigned long long excess;
Balbir Singh4e416952009-09-23 15:56:39 -07003053
3054 if (order > 0)
3055 return 0;
3056
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07003057 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
Balbir Singh4e416952009-09-23 15:56:39 -07003058 /*
3059 * This loop can run a while, specially if mem_cgroup's continuously
3060 * keep exceeding their soft limit and putting the system under
3061 * pressure
3062 */
3063 do {
3064 if (next_mz)
3065 mz = next_mz;
3066 else
3067 mz = mem_cgroup_largest_soft_limit_node(mctz);
3068 if (!mz)
3069 break;
3070
3071 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
3072 gfp_mask,
3073 MEM_CGROUP_RECLAIM_SOFT);
3074 nr_reclaimed += reclaimed;
3075 spin_lock(&mctz->lock);
3076
3077 /*
3078 * If we failed to reclaim anything from this memory cgroup
3079 * it is time to move on to the next cgroup
3080 */
3081 next_mz = NULL;
3082 if (!reclaimed) {
3083 do {
3084 /*
3085 * Loop until we find yet another one.
3086 *
3087 * By the time we get the soft_limit lock
3088 * again, someone might have aded the
3089 * group back on the RB tree. Iterate to
3090 * make sure we get a different mem.
3091 * mem_cgroup_largest_soft_limit_node returns
3092 * NULL if no other cgroup is present on
3093 * the tree
3094 */
3095 next_mz =
3096 __mem_cgroup_largest_soft_limit_node(mctz);
3097 if (next_mz == mz) {
3098 css_put(&next_mz->mem->css);
3099 next_mz = NULL;
3100 } else /* next_mz == NULL or other memcg */
3101 break;
3102 } while (1);
3103 }
Balbir Singh4e416952009-09-23 15:56:39 -07003104 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003105 excess = res_counter_soft_limit_excess(&mz->mem->res);
Balbir Singh4e416952009-09-23 15:56:39 -07003106 /*
3107 * One school of thought says that we should not add
3108 * back the node to the tree if reclaim returns 0.
3109 * But our reclaim could return 0, simply because due
3110 * to priority we are exposing a smaller subset of
3111 * memory to reclaim from. Consider this as a longer
3112 * term TODO.
3113 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003114 /* If excess == 0, no tree ops */
3115 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07003116 spin_unlock(&mctz->lock);
3117 css_put(&mz->mem->css);
3118 loop++;
3119 /*
3120 * Could not reclaim anything and there are no more
3121 * mem cgroups to try or we seem to be looping without
3122 * reclaiming anything.
3123 */
3124 if (!nr_reclaimed &&
3125 (next_mz == NULL ||
3126 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3127 break;
3128 } while (!nr_reclaimed);
3129 if (next_mz)
3130 css_put(&next_mz->mem->css);
3131 return nr_reclaimed;
3132}
3133
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003134/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003135 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003136 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
3137 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003138static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003139 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003140{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003141 struct zone *zone;
3142 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003143 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003144 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003145 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003146 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003147
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003148 zone = &NODE_DATA(node)->node_zones[zid];
3149 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07003150 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003151
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003152 loop = MEM_CGROUP_ZSTAT(mz, lru);
3153 /* give some margin against EBUSY etc...*/
3154 loop += 256;
3155 busy = NULL;
3156 while (loop--) {
3157 ret = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003158 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003159 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003160 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003161 break;
3162 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003163 pc = list_entry(list->prev, struct page_cgroup, lru);
3164 if (busy == pc) {
3165 list_move(&pc->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08003166 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003167 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003168 continue;
3169 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003170 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003171
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -08003172 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003173 if (ret == -ENOMEM)
3174 break;
3175
3176 if (ret == -EBUSY || ret == -EINVAL) {
3177 /* found lock contention or "pc" is obsolete. */
3178 busy = pc;
3179 cond_resched();
3180 } else
3181 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003182 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003183
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003184 if (!ret && !list_empty(list))
3185 return -EBUSY;
3186 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003187}
3188
3189/*
3190 * make mem_cgroup's charge to be 0 if there is no task.
3191 * This enables deleting this mem_cgroup.
3192 */
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003193static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003194{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003195 int ret;
3196 int node, zid, shrink;
3197 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003198 struct cgroup *cgrp = mem->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003199
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003200 css_get(&mem->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003201
3202 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003203 /* should free all ? */
3204 if (free_all)
3205 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003206move_account:
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003207 do {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003208 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003209 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003210 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003211 ret = -EINTR;
3212 if (signal_pending(current))
3213 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003214 /* This is for making all *used* pages to be on LRU. */
3215 lru_add_drain_all();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08003216 drain_all_stock_sync();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003217 ret = 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07003218 mem_cgroup_start_move(mem);
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08003219 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003220 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07003221 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003222 for_each_lru(l) {
3223 ret = mem_cgroup_force_empty_list(mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003224 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003225 if (ret)
3226 break;
3227 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003228 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003229 if (ret)
3230 break;
3231 }
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07003232 mem_cgroup_end_move(mem);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003233 memcg_oom_recover(mem);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003234 /* it seems parent cgroup doesn't have enough mem */
3235 if (ret == -ENOMEM)
3236 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003237 cond_resched();
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003238 /* "ret" should also be checked to ensure all lists are empty. */
3239 } while (mem->res.usage > 0 || ret);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003240out:
3241 css_put(&mem->css);
3242 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003243
3244try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003245 /* returns EBUSY if there is a task or if we come here twice. */
3246 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003247 ret = -EBUSY;
3248 goto out;
3249 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003250 /* we call try-to-free pages for make this cgroup empty */
3251 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003252 /* try to free all pages in this cgroup */
3253 shrink = 1;
3254 while (nr_retries && mem->res.usage > 0) {
3255 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003256
3257 if (signal_pending(current)) {
3258 ret = -EINTR;
3259 goto out;
3260 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003261 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
3262 false, get_swappiness(mem));
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003263 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003264 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003265 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02003266 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003267 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003268
3269 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003270 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003271 /* try move_account...there may be some *locked* pages. */
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003272 goto move_account;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003273}
3274
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003275int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
3276{
3277 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
3278}
3279
3280
Balbir Singh18f59ea2009-01-07 18:08:07 -08003281static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
3282{
3283 return mem_cgroup_from_cont(cont)->use_hierarchy;
3284}
3285
3286static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
3287 u64 val)
3288{
3289 int retval = 0;
3290 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
3291 struct cgroup *parent = cont->parent;
3292 struct mem_cgroup *parent_mem = NULL;
3293
3294 if (parent)
3295 parent_mem = mem_cgroup_from_cont(parent);
3296
3297 cgroup_lock();
3298 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003299 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08003300 * in the child subtrees. If it is unset, then the change can
3301 * occur, provided the current cgroup has no children.
3302 *
3303 * For the root cgroup, parent_mem is NULL, we allow value to be
3304 * set if there are no children.
3305 */
3306 if ((!parent_mem || !parent_mem->use_hierarchy) &&
3307 (val == 1 || val == 0)) {
3308 if (list_empty(&cont->children))
3309 mem->use_hierarchy = val;
3310 else
3311 retval = -EBUSY;
3312 } else
3313 retval = -EINVAL;
3314 cgroup_unlock();
3315
3316 return retval;
3317}
3318
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003319
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003320static u64 mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem,
3321 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003322{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003323 struct mem_cgroup *iter;
3324 s64 val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003325
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003326 /* each per cpu's value can be minus.Then, use s64 */
3327 for_each_mem_cgroup_tree(iter, mem)
3328 val += mem_cgroup_read_stat(iter, idx);
3329
3330 if (val < 0) /* race ? */
3331 val = 0;
3332 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003333}
3334
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003335static inline u64 mem_cgroup_usage(struct mem_cgroup *mem, bool swap)
3336{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003337 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003338
3339 if (!mem_cgroup_is_root(mem)) {
3340 if (!swap)
3341 return res_counter_read_u64(&mem->res, RES_USAGE);
3342 else
3343 return res_counter_read_u64(&mem->memsw, RES_USAGE);
3344 }
3345
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003346 val = mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_CACHE);
3347 val += mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003348
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003349 if (swap)
3350 val += mem_cgroup_get_recursive_idx_stat(mem,
3351 MEM_CGROUP_STAT_SWAPOUT);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003352
3353 return val << PAGE_SHIFT;
3354}
3355
Paul Menage2c3daa72008-04-29 00:59:58 -07003356static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003357{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003358 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003359 u64 val;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003360 int type, name;
3361
3362 type = MEMFILE_TYPE(cft->private);
3363 name = MEMFILE_ATTR(cft->private);
3364 switch (type) {
3365 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003366 if (name == RES_USAGE)
3367 val = mem_cgroup_usage(mem, false);
3368 else
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003369 val = res_counter_read_u64(&mem->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003370 break;
3371 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003372 if (name == RES_USAGE)
3373 val = mem_cgroup_usage(mem, true);
3374 else
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003375 val = res_counter_read_u64(&mem->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003376 break;
3377 default:
3378 BUG();
3379 break;
3380 }
3381 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003382}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003383/*
3384 * The user of this function is...
3385 * RES_LIMIT.
3386 */
Paul Menage856c13a2008-07-25 01:47:04 -07003387static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
3388 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003389{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003390 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003391 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003392 unsigned long long val;
3393 int ret;
3394
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003395 type = MEMFILE_TYPE(cft->private);
3396 name = MEMFILE_ATTR(cft->private);
3397 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003398 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07003399 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
3400 ret = -EINVAL;
3401 break;
3402 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003403 /* This function does all necessary parse...reuse it */
3404 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003405 if (ret)
3406 break;
3407 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003408 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003409 else
3410 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003411 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07003412 case RES_SOFT_LIMIT:
3413 ret = res_counter_memparse_write_strategy(buffer, &val);
3414 if (ret)
3415 break;
3416 /*
3417 * For memsw, soft limits are hard to implement in terms
3418 * of semantics, for now, we support soft limits for
3419 * control without swap
3420 */
3421 if (type == _MEM)
3422 ret = res_counter_set_soft_limit(&memcg->res, val);
3423 else
3424 ret = -EINVAL;
3425 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003426 default:
3427 ret = -EINVAL; /* should be BUG() ? */
3428 break;
3429 }
3430 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003431}
3432
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08003433static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
3434 unsigned long long *mem_limit, unsigned long long *memsw_limit)
3435{
3436 struct cgroup *cgroup;
3437 unsigned long long min_limit, min_memsw_limit, tmp;
3438
3439 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3440 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3441 cgroup = memcg->css.cgroup;
3442 if (!memcg->use_hierarchy)
3443 goto out;
3444
3445 while (cgroup->parent) {
3446 cgroup = cgroup->parent;
3447 memcg = mem_cgroup_from_cont(cgroup);
3448 if (!memcg->use_hierarchy)
3449 break;
3450 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
3451 min_limit = min(min_limit, tmp);
3452 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3453 min_memsw_limit = min(min_memsw_limit, tmp);
3454 }
3455out:
3456 *mem_limit = min_limit;
3457 *memsw_limit = min_memsw_limit;
3458 return;
3459}
3460
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003461static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003462{
3463 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003464 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003465
3466 mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003467 type = MEMFILE_TYPE(event);
3468 name = MEMFILE_ATTR(event);
3469 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003470 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003471 if (type == _MEM)
3472 res_counter_reset_max(&mem->res);
3473 else
3474 res_counter_reset_max(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003475 break;
3476 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003477 if (type == _MEM)
3478 res_counter_reset_failcnt(&mem->res);
3479 else
3480 res_counter_reset_failcnt(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003481 break;
3482 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07003483
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07003484 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003485}
3486
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003487static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
3488 struct cftype *cft)
3489{
3490 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
3491}
3492
Daisuke Nishimura02491442010-03-10 15:22:17 -08003493#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003494static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
3495 struct cftype *cft, u64 val)
3496{
3497 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3498
3499 if (val >= (1 << NR_MOVE_TYPE))
3500 return -EINVAL;
3501 /*
3502 * We check this value several times in both in can_attach() and
3503 * attach(), so we need cgroup lock to prevent this value from being
3504 * inconsistent.
3505 */
3506 cgroup_lock();
3507 mem->move_charge_at_immigrate = val;
3508 cgroup_unlock();
3509
3510 return 0;
3511}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003512#else
3513static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
3514 struct cftype *cft, u64 val)
3515{
3516 return -ENOSYS;
3517}
3518#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003519
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003520
3521/* For read statistics */
3522enum {
3523 MCS_CACHE,
3524 MCS_RSS,
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08003525 MCS_FILE_MAPPED,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003526 MCS_PGPGIN,
3527 MCS_PGPGOUT,
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003528 MCS_SWAP,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003529 MCS_INACTIVE_ANON,
3530 MCS_ACTIVE_ANON,
3531 MCS_INACTIVE_FILE,
3532 MCS_ACTIVE_FILE,
3533 MCS_UNEVICTABLE,
3534 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003535};
3536
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003537struct mcs_total_stat {
3538 s64 stat[NR_MCS_STAT];
3539};
3540
3541struct {
3542 char *local_name;
3543 char *total_name;
3544} memcg_stat_strings[NR_MCS_STAT] = {
3545 {"cache", "total_cache"},
3546 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07003547 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003548 {"pgpgin", "total_pgpgin"},
3549 {"pgpgout", "total_pgpgout"},
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003550 {"swap", "total_swap"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003551 {"inactive_anon", "total_inactive_anon"},
3552 {"active_anon", "total_active_anon"},
3553 {"inactive_file", "total_inactive_file"},
3554 {"active_file", "total_active_file"},
3555 {"unevictable", "total_unevictable"}
3556};
3557
3558
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003559static void
3560mem_cgroup_get_local_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003561{
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003562 s64 val;
3563
3564 /* per cpu stat */
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003565 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003566 s->stat[MCS_CACHE] += val * PAGE_SIZE;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003567 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003568 s->stat[MCS_RSS] += val * PAGE_SIZE;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003569 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_FILE_MAPPED);
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08003570 s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003571 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_PGPGIN_COUNT);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003572 s->stat[MCS_PGPGIN] += val;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003573 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_PGPGOUT_COUNT);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003574 s->stat[MCS_PGPGOUT] += val;
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003575 if (do_swap_account) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003576 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_SWAPOUT);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003577 s->stat[MCS_SWAP] += val * PAGE_SIZE;
3578 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003579
3580 /* per zone stat */
3581 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
3582 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
3583 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
3584 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
3585 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
3586 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
3587 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
3588 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
3589 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
3590 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003591}
3592
3593static void
3594mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
3595{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003596 struct mem_cgroup *iter;
3597
3598 for_each_mem_cgroup_tree(iter, mem)
3599 mem_cgroup_get_local_stat(iter, s);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003600}
3601
Paul Menagec64745c2008-04-29 01:00:02 -07003602static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
3603 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003604{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003605 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003606 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003607 int i;
3608
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003609 memset(&mystat, 0, sizeof(mystat));
3610 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003611
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003612 for (i = 0; i < NR_MCS_STAT; i++) {
3613 if (i == MCS_SWAP && !do_swap_account)
3614 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003615 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003616 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003617
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003618 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08003619 {
3620 unsigned long long limit, memsw_limit;
3621 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
3622 cb->fill(cb, "hierarchical_memory_limit", limit);
3623 if (do_swap_account)
3624 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
3625 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003626
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003627 memset(&mystat, 0, sizeof(mystat));
3628 mem_cgroup_get_total_stat(mem_cont, &mystat);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003629 for (i = 0; i < NR_MCS_STAT; i++) {
3630 if (i == MCS_SWAP && !do_swap_account)
3631 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003632 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003633 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003634
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003635#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08003636 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003637
3638 {
3639 int nid, zid;
3640 struct mem_cgroup_per_zone *mz;
3641 unsigned long recent_rotated[2] = {0, 0};
3642 unsigned long recent_scanned[2] = {0, 0};
3643
3644 for_each_online_node(nid)
3645 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
3646 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
3647
3648 recent_rotated[0] +=
3649 mz->reclaim_stat.recent_rotated[0];
3650 recent_rotated[1] +=
3651 mz->reclaim_stat.recent_rotated[1];
3652 recent_scanned[0] +=
3653 mz->reclaim_stat.recent_scanned[0];
3654 recent_scanned[1] +=
3655 mz->reclaim_stat.recent_scanned[1];
3656 }
3657 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
3658 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
3659 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
3660 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
3661 }
3662#endif
3663
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003664 return 0;
3665}
3666
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003667static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
3668{
3669 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3670
3671 return get_swappiness(memcg);
3672}
3673
3674static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
3675 u64 val)
3676{
3677 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3678 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08003679
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003680 if (val > 100)
3681 return -EINVAL;
3682
3683 if (cgrp->parent == NULL)
3684 return -EINVAL;
3685
3686 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08003687
3688 cgroup_lock();
3689
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003690 /* If under hierarchy, only empty-root can set this value */
3691 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08003692 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
3693 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003694 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08003695 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003696
3697 spin_lock(&memcg->reclaim_param_lock);
3698 memcg->swappiness = val;
3699 spin_unlock(&memcg->reclaim_param_lock);
3700
Li Zefan068b38c2009-01-15 13:51:26 -08003701 cgroup_unlock();
3702
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003703 return 0;
3704}
3705
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003706static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
3707{
3708 struct mem_cgroup_threshold_ary *t;
3709 u64 usage;
3710 int i;
3711
3712 rcu_read_lock();
3713 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003714 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003715 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003716 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003717
3718 if (!t)
3719 goto unlock;
3720
3721 usage = mem_cgroup_usage(memcg, swap);
3722
3723 /*
3724 * current_threshold points to threshold just below usage.
3725 * If it's not true, a threshold was crossed after last
3726 * call of __mem_cgroup_threshold().
3727 */
Phil Carmody5407a562010-05-26 14:42:42 -07003728 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003729
3730 /*
3731 * Iterate backward over array of thresholds starting from
3732 * current_threshold and check if a threshold is crossed.
3733 * If none of thresholds below usage is crossed, we read
3734 * only one element of the array here.
3735 */
3736 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
3737 eventfd_signal(t->entries[i].eventfd, 1);
3738
3739 /* i = current_threshold + 1 */
3740 i++;
3741
3742 /*
3743 * Iterate forward over array of thresholds starting from
3744 * current_threshold+1 and check if a threshold is crossed.
3745 * If none of thresholds above usage is crossed, we read
3746 * only one element of the array here.
3747 */
3748 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
3749 eventfd_signal(t->entries[i].eventfd, 1);
3750
3751 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07003752 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003753unlock:
3754 rcu_read_unlock();
3755}
3756
3757static void mem_cgroup_threshold(struct mem_cgroup *memcg)
3758{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07003759 while (memcg) {
3760 __mem_cgroup_threshold(memcg, false);
3761 if (do_swap_account)
3762 __mem_cgroup_threshold(memcg, true);
3763
3764 memcg = parent_mem_cgroup(memcg);
3765 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003766}
3767
3768static int compare_thresholds(const void *a, const void *b)
3769{
3770 const struct mem_cgroup_threshold *_a = a;
3771 const struct mem_cgroup_threshold *_b = b;
3772
3773 return _a->threshold - _b->threshold;
3774}
3775
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003776static int mem_cgroup_oom_notify_cb(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003777{
3778 struct mem_cgroup_eventfd_list *ev;
3779
3780 list_for_each_entry(ev, &mem->oom_notify, list)
3781 eventfd_signal(ev->eventfd, 1);
3782 return 0;
3783}
3784
3785static void mem_cgroup_oom_notify(struct mem_cgroup *mem)
3786{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003787 struct mem_cgroup *iter;
3788
3789 for_each_mem_cgroup_tree(iter, mem)
3790 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003791}
3792
3793static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
3794 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003795{
3796 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003797 struct mem_cgroup_thresholds *thresholds;
3798 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003799 int type = MEMFILE_TYPE(cft->private);
3800 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003801 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003802
3803 ret = res_counter_memparse_write_strategy(args, &threshold);
3804 if (ret)
3805 return ret;
3806
3807 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003808
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003809 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003810 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003811 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003812 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003813 else
3814 BUG();
3815
3816 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
3817
3818 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003819 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003820 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
3821
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003822 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003823
3824 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003825 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003826 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003827 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003828 ret = -ENOMEM;
3829 goto unlock;
3830 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003831 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003832
3833 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003834 if (thresholds->primary) {
3835 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003836 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003837 }
3838
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003839 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003840 new->entries[size - 1].eventfd = eventfd;
3841 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003842
3843 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003844 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003845 compare_thresholds, NULL);
3846
3847 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003848 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003849 for (i = 0; i < size; i++) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003850 if (new->entries[i].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003851 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003852 * new->current_threshold will not be used until
3853 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003854 * it here.
3855 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003856 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003857 }
3858 }
3859
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003860 /* Free old spare buffer and save old primary buffer as spare */
3861 kfree(thresholds->spare);
3862 thresholds->spare = thresholds->primary;
3863
3864 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003865
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003866 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003867 synchronize_rcu();
3868
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003869unlock:
3870 mutex_unlock(&memcg->thresholds_lock);
3871
3872 return ret;
3873}
3874
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003875static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003876 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003877{
3878 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003879 struct mem_cgroup_thresholds *thresholds;
3880 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003881 int type = MEMFILE_TYPE(cft->private);
3882 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003883 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003884
3885 mutex_lock(&memcg->thresholds_lock);
3886 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003887 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003888 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003889 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003890 else
3891 BUG();
3892
3893 /*
3894 * Something went wrong if we trying to unregister a threshold
3895 * if we don't have thresholds
3896 */
3897 BUG_ON(!thresholds);
3898
3899 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
3900
3901 /* Check if a threshold crossed before removing */
3902 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
3903
3904 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003905 size = 0;
3906 for (i = 0; i < thresholds->primary->size; i++) {
3907 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003908 size++;
3909 }
3910
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003911 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003912
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003913 /* Set thresholds array to NULL if we don't have thresholds */
3914 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003915 kfree(new);
3916 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003917 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003918 }
3919
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003920 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003921
3922 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003923 new->current_threshold = -1;
3924 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
3925 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003926 continue;
3927
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003928 new->entries[j] = thresholds->primary->entries[i];
3929 if (new->entries[j].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003930 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003931 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003932 * until rcu_assign_pointer(), so it's safe to increment
3933 * it here.
3934 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003935 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003936 }
3937 j++;
3938 }
3939
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003940swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003941 /* Swap primary and spare array */
3942 thresholds->spare = thresholds->primary;
3943 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003944
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003945 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003946 synchronize_rcu();
3947
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003948 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003949}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003950
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003951static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
3952 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
3953{
3954 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3955 struct mem_cgroup_eventfd_list *event;
3956 int type = MEMFILE_TYPE(cft->private);
3957
3958 BUG_ON(type != _OOM_TYPE);
3959 event = kmalloc(sizeof(*event), GFP_KERNEL);
3960 if (!event)
3961 return -ENOMEM;
3962
3963 mutex_lock(&memcg_oom_mutex);
3964
3965 event->eventfd = eventfd;
3966 list_add(&event->list, &memcg->oom_notify);
3967
3968 /* already in OOM ? */
3969 if (atomic_read(&memcg->oom_lock))
3970 eventfd_signal(eventfd, 1);
3971 mutex_unlock(&memcg_oom_mutex);
3972
3973 return 0;
3974}
3975
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003976static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003977 struct cftype *cft, struct eventfd_ctx *eventfd)
3978{
3979 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3980 struct mem_cgroup_eventfd_list *ev, *tmp;
3981 int type = MEMFILE_TYPE(cft->private);
3982
3983 BUG_ON(type != _OOM_TYPE);
3984
3985 mutex_lock(&memcg_oom_mutex);
3986
3987 list_for_each_entry_safe(ev, tmp, &mem->oom_notify, list) {
3988 if (ev->eventfd == eventfd) {
3989 list_del(&ev->list);
3990 kfree(ev);
3991 }
3992 }
3993
3994 mutex_unlock(&memcg_oom_mutex);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003995}
3996
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003997static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
3998 struct cftype *cft, struct cgroup_map_cb *cb)
3999{
4000 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
4001
4002 cb->fill(cb, "oom_kill_disable", mem->oom_kill_disable);
4003
4004 if (atomic_read(&mem->oom_lock))
4005 cb->fill(cb, "under_oom", 1);
4006 else
4007 cb->fill(cb, "under_oom", 0);
4008 return 0;
4009}
4010
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004011static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
4012 struct cftype *cft, u64 val)
4013{
4014 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
4015 struct mem_cgroup *parent;
4016
4017 /* cannot set to root cgroup and only 0 and 1 are allowed */
4018 if (!cgrp->parent || !((val == 0) || (val == 1)))
4019 return -EINVAL;
4020
4021 parent = mem_cgroup_from_cont(cgrp->parent);
4022
4023 cgroup_lock();
4024 /* oom-kill-disable is a flag for subhierarchy. */
4025 if ((parent->use_hierarchy) ||
4026 (mem->use_hierarchy && !list_empty(&cgrp->children))) {
4027 cgroup_unlock();
4028 return -EINVAL;
4029 }
4030 mem->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07004031 if (!val)
4032 memcg_oom_recover(mem);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004033 cgroup_unlock();
4034 return 0;
4035}
4036
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004037static struct cftype mem_cgroup_files[] = {
4038 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004039 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004040 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07004041 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004042 .register_event = mem_cgroup_usage_register_event,
4043 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004044 },
4045 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004046 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004047 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004048 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004049 .read_u64 = mem_cgroup_read,
4050 },
4051 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004052 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004053 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07004054 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07004055 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004056 },
4057 {
Balbir Singh296c81d2009-09-23 15:56:36 -07004058 .name = "soft_limit_in_bytes",
4059 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
4060 .write_string = mem_cgroup_write,
4061 .read_u64 = mem_cgroup_read,
4062 },
4063 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004064 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004065 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004066 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07004067 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004068 },
Balbir Singh8697d332008-02-07 00:13:59 -08004069 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004070 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07004071 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004072 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004073 {
4074 .name = "force_empty",
4075 .trigger = mem_cgroup_force_empty_write,
4076 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08004077 {
4078 .name = "use_hierarchy",
4079 .write_u64 = mem_cgroup_hierarchy_write,
4080 .read_u64 = mem_cgroup_hierarchy_read,
4081 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004082 {
4083 .name = "swappiness",
4084 .read_u64 = mem_cgroup_swappiness_read,
4085 .write_u64 = mem_cgroup_swappiness_write,
4086 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004087 {
4088 .name = "move_charge_at_immigrate",
4089 .read_u64 = mem_cgroup_move_charge_read,
4090 .write_u64 = mem_cgroup_move_charge_write,
4091 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004092 {
4093 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004094 .read_map = mem_cgroup_oom_control_read,
4095 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004096 .register_event = mem_cgroup_oom_register_event,
4097 .unregister_event = mem_cgroup_oom_unregister_event,
4098 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
4099 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004100};
4101
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004102#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4103static struct cftype memsw_cgroup_files[] = {
4104 {
4105 .name = "memsw.usage_in_bytes",
4106 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
4107 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004108 .register_event = mem_cgroup_usage_register_event,
4109 .unregister_event = mem_cgroup_usage_unregister_event,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004110 },
4111 {
4112 .name = "memsw.max_usage_in_bytes",
4113 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
4114 .trigger = mem_cgroup_reset,
4115 .read_u64 = mem_cgroup_read,
4116 },
4117 {
4118 .name = "memsw.limit_in_bytes",
4119 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
4120 .write_string = mem_cgroup_write,
4121 .read_u64 = mem_cgroup_read,
4122 },
4123 {
4124 .name = "memsw.failcnt",
4125 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
4126 .trigger = mem_cgroup_reset,
4127 .read_u64 = mem_cgroup_read,
4128 },
4129};
4130
4131static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
4132{
4133 if (!do_swap_account)
4134 return 0;
4135 return cgroup_add_files(cont, ss, memsw_cgroup_files,
4136 ARRAY_SIZE(memsw_cgroup_files));
4137};
4138#else
4139static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
4140{
4141 return 0;
4142}
4143#endif
4144
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004145static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
4146{
4147 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004148 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07004149 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004150 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004151 /*
4152 * This routine is called against possible nodes.
4153 * But it's BUG to call kmalloc() against offline node.
4154 *
4155 * TODO: this routine can waste much memory for nodes which will
4156 * never be onlined. It's better to use memory hotplug callback
4157 * function.
4158 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004159 if (!node_state(node, N_NORMAL_MEMORY))
4160 tmp = -1;
4161 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004162 if (!pn)
4163 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004164
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004165 mem->info.nodeinfo[node] = pn;
4166 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004167
4168 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4169 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07004170 for_each_lru(l)
4171 INIT_LIST_HEAD(&mz->lists[l]);
Balbir Singhf64c3f52009-09-23 15:56:37 -07004172 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07004173 mz->on_tree = false;
4174 mz->mem = mem;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004175 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004176 return 0;
4177}
4178
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004179static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
4180{
4181 kfree(mem->info.nodeinfo[node]);
4182}
4183
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004184static struct mem_cgroup *mem_cgroup_alloc(void)
4185{
4186 struct mem_cgroup *mem;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004187 int size = sizeof(struct mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004188
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004189 /* Can be very big if MAX_NUMNODES is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08004190 if (size < PAGE_SIZE)
4191 mem = kmalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004192 else
Jan Blunckc8dad2b2009-01-07 18:07:53 -08004193 mem = vmalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004194
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07004195 if (!mem)
4196 return NULL;
4197
4198 memset(mem, 0, size);
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004199 mem->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
4200 if (!mem->stat) {
4201 if (size < PAGE_SIZE)
4202 kfree(mem);
4203 else
4204 vfree(mem);
4205 mem = NULL;
4206 }
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07004207 spin_lock_init(&mem->pcp_counter_lock);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004208 return mem;
4209}
4210
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004211/*
4212 * At destroying mem_cgroup, references from swap_cgroup can remain.
4213 * (scanning all at force_empty is too costly...)
4214 *
4215 * Instead of clearing all references at force_empty, we remember
4216 * the number of reference from swap_cgroup and free mem_cgroup when
4217 * it goes down to 0.
4218 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004219 * Removal of cgroup itself succeeds regardless of refs from swap.
4220 */
4221
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004222static void __mem_cgroup_free(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004223{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004224 int node;
4225
Balbir Singhf64c3f52009-09-23 15:56:37 -07004226 mem_cgroup_remove_from_trees(mem);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004227 free_css_id(&mem_cgroup_subsys, &mem->css);
4228
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004229 for_each_node_state(node, N_POSSIBLE)
4230 free_mem_cgroup_per_zone_info(mem, node);
4231
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004232 free_percpu(mem->stat);
4233 if (sizeof(struct mem_cgroup) < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004234 kfree(mem);
4235 else
4236 vfree(mem);
4237}
4238
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004239static void mem_cgroup_get(struct mem_cgroup *mem)
4240{
4241 atomic_inc(&mem->refcnt);
4242}
4243
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004244static void __mem_cgroup_put(struct mem_cgroup *mem, int count)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004245{
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004246 if (atomic_sub_and_test(count, &mem->refcnt)) {
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004247 struct mem_cgroup *parent = parent_mem_cgroup(mem);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004248 __mem_cgroup_free(mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004249 if (parent)
4250 mem_cgroup_put(parent);
4251 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004252}
4253
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004254static void mem_cgroup_put(struct mem_cgroup *mem)
4255{
4256 __mem_cgroup_put(mem, 1);
4257}
4258
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004259/*
4260 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
4261 */
4262static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
4263{
4264 if (!mem->res.parent)
4265 return NULL;
4266 return mem_cgroup_from_res_counter(mem->res.parent, res);
4267}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004268
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004269#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4270static void __init enable_swap_cgroup(void)
4271{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004272 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004273 do_swap_account = 1;
4274}
4275#else
4276static void __init enable_swap_cgroup(void)
4277{
4278}
4279#endif
4280
Balbir Singhf64c3f52009-09-23 15:56:37 -07004281static int mem_cgroup_soft_limit_tree_init(void)
4282{
4283 struct mem_cgroup_tree_per_node *rtpn;
4284 struct mem_cgroup_tree_per_zone *rtpz;
4285 int tmp, node, zone;
4286
4287 for_each_node_state(node, N_POSSIBLE) {
4288 tmp = node;
4289 if (!node_state(node, N_NORMAL_MEMORY))
4290 tmp = -1;
4291 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
4292 if (!rtpn)
4293 return 1;
4294
4295 soft_limit_tree.rb_tree_per_node[node] = rtpn;
4296
4297 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4298 rtpz = &rtpn->rb_tree_per_zone[zone];
4299 rtpz->rb_root = RB_ROOT;
4300 spin_lock_init(&rtpz->lock);
4301 }
4302 }
4303 return 0;
4304}
4305
Li Zefan0eb253e2009-01-15 13:51:25 -08004306static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004307mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
4308{
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004309 struct mem_cgroup *mem, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004310 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004311 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004312
Jan Blunckc8dad2b2009-01-07 18:07:53 -08004313 mem = mem_cgroup_alloc();
4314 if (!mem)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004315 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004316
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004317 for_each_node_state(node, N_POSSIBLE)
4318 if (alloc_mem_cgroup_per_zone_info(mem, node))
4319 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004320
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004321 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004322 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004323 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004324 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004325 parent = NULL;
Balbir Singh4b3bde42009-09-23 15:56:32 -07004326 root_mem_cgroup = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004327 if (mem_cgroup_soft_limit_tree_init())
4328 goto free_out;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004329 for_each_possible_cpu(cpu) {
4330 struct memcg_stock_pcp *stock =
4331 &per_cpu(memcg_stock, cpu);
4332 INIT_WORK(&stock->work, drain_local_stock);
4333 }
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07004334 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004335 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004336 parent = mem_cgroup_from_cont(cont->parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004337 mem->use_hierarchy = parent->use_hierarchy;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004338 mem->oom_kill_disable = parent->oom_kill_disable;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004339 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004340
Balbir Singh18f59ea2009-01-07 18:08:07 -08004341 if (parent && parent->use_hierarchy) {
4342 res_counter_init(&mem->res, &parent->res);
4343 res_counter_init(&mem->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004344 /*
4345 * We increment refcnt of the parent to ensure that we can
4346 * safely access it on res_counter_charge/uncharge.
4347 * This refcnt will be decremented when freeing this
4348 * mem_cgroup(see mem_cgroup_put).
4349 */
4350 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004351 } else {
4352 res_counter_init(&mem->res, NULL);
4353 res_counter_init(&mem->memsw, NULL);
4354 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004355 mem->last_scanned_child = 0;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -08004356 spin_lock_init(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004357 INIT_LIST_HEAD(&mem->oom_notify);
Balbir Singh6d61ef42009-01-07 18:08:06 -08004358
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004359 if (parent)
4360 mem->swappiness = get_swappiness(parent);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004361 atomic_set(&mem->refcnt, 1);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004362 mem->move_charge_at_immigrate = 0;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004363 mutex_init(&mem->thresholds_lock);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004364 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004365free_out:
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004366 __mem_cgroup_free(mem);
Balbir Singh4b3bde42009-09-23 15:56:32 -07004367 root_mem_cgroup = NULL;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004368 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004369}
4370
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004371static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004372 struct cgroup *cont)
4373{
4374 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004375
4376 return mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004377}
4378
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004379static void mem_cgroup_destroy(struct cgroup_subsys *ss,
4380 struct cgroup *cont)
4381{
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004382 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004383
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004384 mem_cgroup_put(mem);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004385}
4386
4387static int mem_cgroup_populate(struct cgroup_subsys *ss,
4388 struct cgroup *cont)
4389{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004390 int ret;
4391
4392 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
4393 ARRAY_SIZE(mem_cgroup_files));
4394
4395 if (!ret)
4396 ret = register_memsw_files(cont, ss);
4397 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004398}
4399
Daisuke Nishimura02491442010-03-10 15:22:17 -08004400#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004401/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004402#define PRECHARGE_COUNT_AT_ONCE 256
4403static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004404{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004405 int ret = 0;
4406 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004407 struct mem_cgroup *mem = mc.to;
4408
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004409 if (mem_cgroup_is_root(mem)) {
4410 mc.precharge += count;
4411 /* we don't need css_get for root */
4412 return ret;
4413 }
4414 /* try to charge at once */
4415 if (count > 1) {
4416 struct res_counter *dummy;
4417 /*
4418 * "mem" cannot be under rmdir() because we've already checked
4419 * by cgroup_lock_live_cgroup() that it is not removed and we
4420 * are still under the same cgroup_mutex. So we can postpone
4421 * css_get().
4422 */
4423 if (res_counter_charge(&mem->res, PAGE_SIZE * count, &dummy))
4424 goto one_by_one;
4425 if (do_swap_account && res_counter_charge(&mem->memsw,
4426 PAGE_SIZE * count, &dummy)) {
4427 res_counter_uncharge(&mem->res, PAGE_SIZE * count);
4428 goto one_by_one;
4429 }
4430 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004431 return ret;
4432 }
4433one_by_one:
4434 /* fall back to one by one charge */
4435 while (count--) {
4436 if (signal_pending(current)) {
4437 ret = -EINTR;
4438 break;
4439 }
4440 if (!batch_count--) {
4441 batch_count = PRECHARGE_COUNT_AT_ONCE;
4442 cond_resched();
4443 }
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08004444 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004445 if (ret || !mem)
4446 /* mem_cgroup_clear_mc() will do uncharge later */
4447 return -ENOMEM;
4448 mc.precharge++;
4449 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004450 return ret;
4451}
4452
4453/**
4454 * is_target_pte_for_mc - check a pte whether it is valid for move charge
4455 * @vma: the vma the pte to be checked belongs
4456 * @addr: the address corresponding to the pte to be checked
4457 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08004458 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004459 *
4460 * Returns
4461 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
4462 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
4463 * move charge. if @target is not NULL, the page is stored in target->page
4464 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08004465 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
4466 * target for charge migration. if @target is not NULL, the entry is stored
4467 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004468 *
4469 * Called with pte lock held.
4470 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004471union mc_target {
4472 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004473 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004474};
4475
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004476enum mc_target_type {
4477 MC_TARGET_NONE, /* not used */
4478 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08004479 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004480};
4481
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004482static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
4483 unsigned long addr, pte_t ptent)
4484{
4485 struct page *page = vm_normal_page(vma, addr, ptent);
4486
4487 if (!page || !page_mapped(page))
4488 return NULL;
4489 if (PageAnon(page)) {
4490 /* we don't move shared anon */
4491 if (!move_anon() || page_mapcount(page) > 2)
4492 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004493 } else if (!move_file())
4494 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004495 return NULL;
4496 if (!get_page_unless_zero(page))
4497 return NULL;
4498
4499 return page;
4500}
4501
4502static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
4503 unsigned long addr, pte_t ptent, swp_entry_t *entry)
4504{
4505 int usage_count;
4506 struct page *page = NULL;
4507 swp_entry_t ent = pte_to_swp_entry(ptent);
4508
4509 if (!move_anon() || non_swap_entry(ent))
4510 return NULL;
4511 usage_count = mem_cgroup_count_swap_user(ent, &page);
4512 if (usage_count > 1) { /* we don't move shared anon */
4513 if (page)
4514 put_page(page);
4515 return NULL;
4516 }
4517 if (do_swap_account)
4518 entry->val = ent.val;
4519
4520 return page;
4521}
4522
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004523static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
4524 unsigned long addr, pte_t ptent, swp_entry_t *entry)
4525{
4526 struct page *page = NULL;
4527 struct inode *inode;
4528 struct address_space *mapping;
4529 pgoff_t pgoff;
4530
4531 if (!vma->vm_file) /* anonymous vma */
4532 return NULL;
4533 if (!move_file())
4534 return NULL;
4535
4536 inode = vma->vm_file->f_path.dentry->d_inode;
4537 mapping = vma->vm_file->f_mapping;
4538 if (pte_none(ptent))
4539 pgoff = linear_page_index(vma, addr);
4540 else /* pte_file(ptent) is true */
4541 pgoff = pte_to_pgoff(ptent);
4542
4543 /* page is moved even if it's not RSS of this task(page-faulted). */
4544 if (!mapping_cap_swap_backed(mapping)) { /* normal file */
4545 page = find_get_page(mapping, pgoff);
4546 } else { /* shmem/tmpfs file. we should take account of swap too. */
4547 swp_entry_t ent;
4548 mem_cgroup_get_shmem_target(inode, pgoff, &page, &ent);
4549 if (do_swap_account)
4550 entry->val = ent.val;
4551 }
4552
4553 return page;
4554}
4555
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004556static int is_target_pte_for_mc(struct vm_area_struct *vma,
4557 unsigned long addr, pte_t ptent, union mc_target *target)
4558{
Daisuke Nishimura02491442010-03-10 15:22:17 -08004559 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004560 struct page_cgroup *pc;
4561 int ret = 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004562 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004563
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004564 if (pte_present(ptent))
4565 page = mc_handle_present_pte(vma, addr, ptent);
4566 else if (is_swap_pte(ptent))
4567 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004568 else if (pte_none(ptent) || pte_file(ptent))
4569 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004570
4571 if (!page && !ent.val)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004572 return 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004573 if (page) {
4574 pc = lookup_page_cgroup(page);
4575 /*
4576 * Do only loose check w/o page_cgroup lock.
4577 * mem_cgroup_move_account() checks the pc is valid or not under
4578 * the lock.
4579 */
4580 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
4581 ret = MC_TARGET_PAGE;
4582 if (target)
4583 target->page = page;
4584 }
4585 if (!ret || !target)
4586 put_page(page);
4587 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004588 /* There is a swap entry and a page doesn't exist or isn't charged */
4589 if (ent.val && !ret &&
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07004590 css_id(&mc.from->css) == lookup_swap_cgroup(ent)) {
4591 ret = MC_TARGET_SWAP;
4592 if (target)
4593 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004594 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004595 return ret;
4596}
4597
4598static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
4599 unsigned long addr, unsigned long end,
4600 struct mm_walk *walk)
4601{
4602 struct vm_area_struct *vma = walk->private;
4603 pte_t *pte;
4604 spinlock_t *ptl;
4605
4606 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
4607 for (; addr != end; pte++, addr += PAGE_SIZE)
4608 if (is_target_pte_for_mc(vma, addr, *pte, NULL))
4609 mc.precharge++; /* increment precharge temporarily */
4610 pte_unmap_unlock(pte - 1, ptl);
4611 cond_resched();
4612
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004613 return 0;
4614}
4615
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004616static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
4617{
4618 unsigned long precharge;
4619 struct vm_area_struct *vma;
4620
4621 down_read(&mm->mmap_sem);
4622 for (vma = mm->mmap; vma; vma = vma->vm_next) {
4623 struct mm_walk mem_cgroup_count_precharge_walk = {
4624 .pmd_entry = mem_cgroup_count_precharge_pte_range,
4625 .mm = mm,
4626 .private = vma,
4627 };
4628 if (is_vm_hugetlb_page(vma))
4629 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004630 walk_page_range(vma->vm_start, vma->vm_end,
4631 &mem_cgroup_count_precharge_walk);
4632 }
4633 up_read(&mm->mmap_sem);
4634
4635 precharge = mc.precharge;
4636 mc.precharge = 0;
4637
4638 return precharge;
4639}
4640
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004641static int mem_cgroup_precharge_mc(struct mm_struct *mm)
4642{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004643 return mem_cgroup_do_precharge(mem_cgroup_count_precharge(mm));
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004644}
4645
4646static void mem_cgroup_clear_mc(void)
4647{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07004648 struct mem_cgroup *from = mc.from;
4649 struct mem_cgroup *to = mc.to;
4650
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004651 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004652 if (mc.precharge) {
4653 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
4654 mc.precharge = 0;
4655 }
4656 /*
4657 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
4658 * we must uncharge here.
4659 */
4660 if (mc.moved_charge) {
4661 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
4662 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004663 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004664 /* we must fixup refcnts and charges */
4665 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004666 /* uncharge swap account from the old cgroup */
4667 if (!mem_cgroup_is_root(mc.from))
4668 res_counter_uncharge(&mc.from->memsw,
4669 PAGE_SIZE * mc.moved_swap);
4670 __mem_cgroup_put(mc.from, mc.moved_swap);
4671
4672 if (!mem_cgroup_is_root(mc.to)) {
4673 /*
4674 * we charged both to->res and to->memsw, so we should
4675 * uncharge to->res.
4676 */
4677 res_counter_uncharge(&mc.to->res,
4678 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004679 }
4680 /* we've already done mem_cgroup_get(mc.to) */
4681
4682 mc.moved_swap = 0;
4683 }
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07004684 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004685 mc.from = NULL;
4686 mc.to = NULL;
Daisuke Nishimura8033b972010-03-10 15:22:16 -08004687 mc.moving_task = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07004688 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07004689 mem_cgroup_end_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07004690 memcg_oom_recover(from);
4691 memcg_oom_recover(to);
Daisuke Nishimura8033b972010-03-10 15:22:16 -08004692 wake_up_all(&mc.waitq);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004693}
4694
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004695static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
4696 struct cgroup *cgroup,
4697 struct task_struct *p,
4698 bool threadgroup)
4699{
4700 int ret = 0;
4701 struct mem_cgroup *mem = mem_cgroup_from_cont(cgroup);
4702
4703 if (mem->move_charge_at_immigrate) {
4704 struct mm_struct *mm;
4705 struct mem_cgroup *from = mem_cgroup_from_task(p);
4706
4707 VM_BUG_ON(from == mem);
4708
4709 mm = get_task_mm(p);
4710 if (!mm)
4711 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004712 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004713 if (mm->owner == p) {
4714 VM_BUG_ON(mc.from);
4715 VM_BUG_ON(mc.to);
4716 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004717 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004718 VM_BUG_ON(mc.moved_swap);
Daisuke Nishimura8033b972010-03-10 15:22:16 -08004719 VM_BUG_ON(mc.moving_task);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07004720 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07004721 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004722 mc.from = from;
4723 mc.to = mem;
4724 mc.precharge = 0;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004725 mc.moved_charge = 0;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004726 mc.moved_swap = 0;
Daisuke Nishimura8033b972010-03-10 15:22:16 -08004727 mc.moving_task = current;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07004728 spin_unlock(&mc.lock);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004729
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004730 ret = mem_cgroup_precharge_mc(mm);
4731 if (ret)
4732 mem_cgroup_clear_mc();
4733 }
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004734 mmput(mm);
4735 }
4736 return ret;
4737}
4738
4739static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
4740 struct cgroup *cgroup,
4741 struct task_struct *p,
4742 bool threadgroup)
4743{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004744 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004745}
4746
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004747static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
4748 unsigned long addr, unsigned long end,
4749 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004750{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004751 int ret = 0;
4752 struct vm_area_struct *vma = walk->private;
4753 pte_t *pte;
4754 spinlock_t *ptl;
4755
4756retry:
4757 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
4758 for (; addr != end; addr += PAGE_SIZE) {
4759 pte_t ptent = *(pte++);
4760 union mc_target target;
4761 int type;
4762 struct page *page;
4763 struct page_cgroup *pc;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004764 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004765
4766 if (!mc.precharge)
4767 break;
4768
4769 type = is_target_pte_for_mc(vma, addr, ptent, &target);
4770 switch (type) {
4771 case MC_TARGET_PAGE:
4772 page = target.page;
4773 if (isolate_lru_page(page))
4774 goto put;
4775 pc = lookup_page_cgroup(page);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004776 if (!mem_cgroup_move_account(pc,
4777 mc.from, mc.to, false)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004778 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004779 /* we uncharge from mc.from later. */
4780 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004781 }
4782 putback_lru_page(page);
4783put: /* is_target_pte_for_mc() gets the page */
4784 put_page(page);
4785 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004786 case MC_TARGET_SWAP:
4787 ent = target.ent;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004788 if (!mem_cgroup_move_swap_account(ent,
4789 mc.from, mc.to, false)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08004790 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004791 /* we fixup refcnts and charges later. */
4792 mc.moved_swap++;
4793 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08004794 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004795 default:
4796 break;
4797 }
4798 }
4799 pte_unmap_unlock(pte - 1, ptl);
4800 cond_resched();
4801
4802 if (addr != end) {
4803 /*
4804 * We have consumed all precharges we got in can_attach().
4805 * We try charge one by one, but don't do any additional
4806 * charges to mc.to if we have failed in charge once in attach()
4807 * phase.
4808 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004809 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004810 if (!ret)
4811 goto retry;
4812 }
4813
4814 return ret;
4815}
4816
4817static void mem_cgroup_move_charge(struct mm_struct *mm)
4818{
4819 struct vm_area_struct *vma;
4820
4821 lru_add_drain_all();
4822 down_read(&mm->mmap_sem);
4823 for (vma = mm->mmap; vma; vma = vma->vm_next) {
4824 int ret;
4825 struct mm_walk mem_cgroup_move_charge_walk = {
4826 .pmd_entry = mem_cgroup_move_charge_pte_range,
4827 .mm = mm,
4828 .private = vma,
4829 };
4830 if (is_vm_hugetlb_page(vma))
4831 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004832 ret = walk_page_range(vma->vm_start, vma->vm_end,
4833 &mem_cgroup_move_charge_walk);
4834 if (ret)
4835 /*
4836 * means we have consumed all precharges and failed in
4837 * doing additional charge. Just abandon here.
4838 */
4839 break;
4840 }
4841 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004842}
4843
Balbir Singh67e465a2008-02-07 00:13:54 -08004844static void mem_cgroup_move_task(struct cgroup_subsys *ss,
4845 struct cgroup *cont,
4846 struct cgroup *old_cont,
Ben Blumbe367d02009-09-23 15:56:31 -07004847 struct task_struct *p,
4848 bool threadgroup)
Balbir Singh67e465a2008-02-07 00:13:54 -08004849{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004850 struct mm_struct *mm;
4851
4852 if (!mc.to)
4853 /* no need to move charge */
4854 return;
4855
4856 mm = get_task_mm(p);
4857 if (mm) {
4858 mem_cgroup_move_charge(mm);
4859 mmput(mm);
4860 }
4861 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08004862}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07004863#else /* !CONFIG_MMU */
4864static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
4865 struct cgroup *cgroup,
4866 struct task_struct *p,
4867 bool threadgroup)
4868{
4869 return 0;
4870}
4871static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
4872 struct cgroup *cgroup,
4873 struct task_struct *p,
4874 bool threadgroup)
4875{
4876}
4877static void mem_cgroup_move_task(struct cgroup_subsys *ss,
4878 struct cgroup *cont,
4879 struct cgroup *old_cont,
4880 struct task_struct *p,
4881 bool threadgroup)
4882{
4883}
4884#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08004885
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004886struct cgroup_subsys mem_cgroup_subsys = {
4887 .name = "memory",
4888 .subsys_id = mem_cgroup_subsys_id,
4889 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004890 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004891 .destroy = mem_cgroup_destroy,
4892 .populate = mem_cgroup_populate,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004893 .can_attach = mem_cgroup_can_attach,
4894 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08004895 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004896 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004897 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004898};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004899
4900#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4901
4902static int __init disable_swap_account(char *s)
4903{
4904 really_do_swap_account = 0;
4905 return 1;
4906}
4907__setup("noswapaccount", disable_swap_account);
4908#endif