blob: a1c3c317a4dd92fa34314b72c2bf07e634530e5e [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 Hiroyuki8c7c6e32009-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 Hiroyuki08e552c62009-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 Hiroyukid2265e62010-03-10 15:22:31 -080092 MEM_CGROUP_EVENTS, /* incremented at every pagein/pageout */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080093
94 MEM_CGROUP_STAT_NSTATS,
95};
96
97struct mem_cgroup_stat_cpu {
98 s64 count[MEM_CGROUP_STAT_NSTATS];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080099};
100
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800101/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800102 * per-zone information in memory controller.
103 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800104struct mem_cgroup_per_zone {
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800105 /*
106 * spin_lock to protect the per cgroup LRU
107 */
Christoph Lameterb69408e2008-10-18 20:26:14 -0700108 struct list_head lists[NR_LRU_LISTS];
109 unsigned long count[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800110
111 struct zone_reclaim_stat reclaim_stat;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700112 struct rb_node tree_node; /* RB tree node */
113 unsigned long long usage_in_excess;/* Set to the value by which */
114 /* the soft limit is exceeded*/
115 bool on_tree;
Balbir Singh4e416952009-09-23 15:56:39 -0700116 struct mem_cgroup *mem; /* Back pointer, we cannot */
117 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800118};
119/* Macro for accessing counter */
120#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
121
122struct mem_cgroup_per_node {
123 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
124};
125
126struct mem_cgroup_lru_info {
127 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
128};
129
130/*
Balbir Singhf64c3f52009-09-23 15:56:37 -0700131 * Cgroups above their limits are maintained in a RB-Tree, independent of
132 * their hierarchy representation
133 */
134
135struct mem_cgroup_tree_per_zone {
136 struct rb_root rb_root;
137 spinlock_t lock;
138};
139
140struct mem_cgroup_tree_per_node {
141 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
142};
143
144struct mem_cgroup_tree {
145 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
146};
147
148static struct mem_cgroup_tree soft_limit_tree __read_mostly;
149
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800150struct mem_cgroup_threshold {
151 struct eventfd_ctx *eventfd;
152 u64 threshold;
153};
154
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700155/* For threshold */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800156struct mem_cgroup_threshold_ary {
157 /* An array index points to threshold just below usage. */
Phil Carmody5407a562010-05-26 14:42:42 -0700158 int current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800159 /* Size of entries[] */
160 unsigned int size;
161 /* Array of thresholds */
162 struct mem_cgroup_threshold entries[0];
163};
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700164
165struct mem_cgroup_thresholds {
166 /* Primary thresholds array */
167 struct mem_cgroup_threshold_ary *primary;
168 /*
169 * Spare threshold array.
170 * This is needed to make mem_cgroup_unregister_event() "never fail".
171 * It must be able to store at least primary->size - 1 entries.
172 */
173 struct mem_cgroup_threshold_ary *spare;
174};
175
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700176/* for OOM */
177struct mem_cgroup_eventfd_list {
178 struct list_head list;
179 struct eventfd_ctx *eventfd;
180};
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800181
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800182static void mem_cgroup_threshold(struct mem_cgroup *mem);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700183static void mem_cgroup_oom_notify(struct mem_cgroup *mem);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800184
Balbir Singhf64c3f52009-09-23 15:56:37 -0700185/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800186 * The memory controller data structure. The memory controller controls both
187 * page cache and RSS per cgroup. We would eventually like to provide
188 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
189 * to help the administrator determine what knobs to tune.
190 *
191 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800192 * we hit the water mark. May be even add a low water mark, such that
193 * no reclaim occurs from a cgroup at it's low water mark, this is
194 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800195 */
196struct mem_cgroup {
197 struct cgroup_subsys_state css;
198 /*
199 * the counter to account for memory usage
200 */
201 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800202 /*
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800203 * the counter to account for mem+swap usage.
204 */
205 struct res_counter memsw;
206 /*
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800207 * Per cgroup active and inactive list, similar to the
208 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800209 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800210 struct mem_cgroup_lru_info info;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800211
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800212 /*
213 protect against reclaim related member.
214 */
215 spinlock_t reclaim_param_lock;
216
Balbir Singh6d61ef42009-01-07 18:08:06 -0800217 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200218 * While reclaiming in a hierarchy, we cache the last child we
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700219 * reclaimed from.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800220 */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700221 int last_scanned_child;
Balbir Singh18f59ea2009-01-07 18:08:07 -0800222 /*
223 * Should the accounting and control be hierarchical, per subtree?
224 */
225 bool use_hierarchy;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -0800226 atomic_t oom_lock;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800227 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800228
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800229 unsigned int swappiness;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -0700230 /* OOM-Killer disable */
231 int oom_kill_disable;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800232
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700233 /* set when res.limit == memsw.limit */
234 bool memsw_is_minimum;
235
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800236 /* protect arrays of thresholds */
237 struct mutex thresholds_lock;
238
239 /* thresholds for memory usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700240 struct mem_cgroup_thresholds thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700241
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800242 /* thresholds for mem+swap usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700243 struct mem_cgroup_thresholds memsw_thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700244
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700245 /* For oom notifier event fd */
246 struct list_head oom_notify;
247
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800248 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800249 * Should we move charges of a task when a task is moved into this
250 * mem_cgroup ? And what type of charges should we move ?
251 */
252 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800253 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800254 * percpu counter.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800255 */
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800256 struct mem_cgroup_stat_cpu *stat;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800257};
258
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800259/* Stuffs for move charges at task migration. */
260/*
261 * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a
262 * left-shifted bitmap of these types.
263 */
264enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800265 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700266 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800267 NR_MOVE_TYPE,
268};
269
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800270/* "mc" and its members are protected by cgroup_mutex */
271static struct move_charge_struct {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700272 spinlock_t lock; /* for from, to, moving_task */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800273 struct mem_cgroup *from;
274 struct mem_cgroup *to;
275 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800276 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800277 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800278 struct task_struct *moving_task; /* a task moving charges */
279 wait_queue_head_t waitq; /* a waitq for other context */
280} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700281 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800282 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
283};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800284
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700285static bool move_anon(void)
286{
287 return test_bit(MOVE_CHARGE_TYPE_ANON,
288 &mc.to->move_charge_at_immigrate);
289}
290
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700291static bool move_file(void)
292{
293 return test_bit(MOVE_CHARGE_TYPE_FILE,
294 &mc.to->move_charge_at_immigrate);
295}
296
Balbir Singh4e416952009-09-23 15:56:39 -0700297/*
298 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
299 * limit reclaim to prevent infinite loops, if they ever occur.
300 */
301#define MEM_CGROUP_MAX_RECLAIM_LOOPS (100)
302#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2)
303
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800304enum charge_type {
305 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
306 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700307 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700308 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800309 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700310 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700311 NR_CHARGE_TYPE,
312};
313
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700314/* only for here (for easy reading.) */
315#define PCGF_CACHE (1UL << PCG_CACHE)
316#define PCGF_USED (1UL << PCG_USED)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700317#define PCGF_LOCK (1UL << PCG_LOCK)
Balbir Singh4b3bde42009-09-23 15:56:32 -0700318/* Not used, but added here for completeness */
319#define PCGF_ACCT (1UL << PCG_ACCT)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800320
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800321/* for encoding cft->private value on file */
322#define _MEM (0)
323#define _MEMSWAP (1)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700324#define _OOM_TYPE (2)
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800325#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
326#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
327#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700328/* Used for OOM nofiier */
329#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800330
Balbir Singh75822b42009-09-23 15:56:38 -0700331/*
332 * Reclaim flags for mem_cgroup_hierarchical_reclaim
333 */
334#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
335#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
336#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
337#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
Balbir Singh4e416952009-09-23 15:56:39 -0700338#define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
339#define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
Balbir Singh75822b42009-09-23 15:56:38 -0700340
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800341static void mem_cgroup_get(struct mem_cgroup *mem);
342static void mem_cgroup_put(struct mem_cgroup *mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -0800343static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -0800344static void drain_all_stock_async(void);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800345
Balbir Singhf64c3f52009-09-23 15:56:37 -0700346static struct mem_cgroup_per_zone *
347mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
348{
349 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
350}
351
Wu Fengguangd3242362009-12-16 12:19:59 +0100352struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *mem)
353{
354 return &mem->css;
355}
356
Balbir Singhf64c3f52009-09-23 15:56:37 -0700357static struct mem_cgroup_per_zone *
358page_cgroup_zoneinfo(struct page_cgroup *pc)
359{
360 struct mem_cgroup *mem = pc->mem_cgroup;
361 int nid = page_cgroup_nid(pc);
362 int zid = page_cgroup_zid(pc);
363
364 if (!mem)
365 return NULL;
366
367 return mem_cgroup_zoneinfo(mem, nid, zid);
368}
369
370static struct mem_cgroup_tree_per_zone *
371soft_limit_tree_node_zone(int nid, int zid)
372{
373 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
374}
375
376static struct mem_cgroup_tree_per_zone *
377soft_limit_tree_from_page(struct page *page)
378{
379 int nid = page_to_nid(page);
380 int zid = page_zonenum(page);
381
382 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
383}
384
385static void
Balbir Singh4e416952009-09-23 15:56:39 -0700386__mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700387 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700388 struct mem_cgroup_tree_per_zone *mctz,
389 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700390{
391 struct rb_node **p = &mctz->rb_root.rb_node;
392 struct rb_node *parent = NULL;
393 struct mem_cgroup_per_zone *mz_node;
394
395 if (mz->on_tree)
396 return;
397
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700398 mz->usage_in_excess = new_usage_in_excess;
399 if (!mz->usage_in_excess)
400 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700401 while (*p) {
402 parent = *p;
403 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
404 tree_node);
405 if (mz->usage_in_excess < mz_node->usage_in_excess)
406 p = &(*p)->rb_left;
407 /*
408 * We can't avoid mem cgroups that are over their soft
409 * limit by the same amount
410 */
411 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
412 p = &(*p)->rb_right;
413 }
414 rb_link_node(&mz->tree_node, parent, p);
415 rb_insert_color(&mz->tree_node, &mctz->rb_root);
416 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700417}
418
419static void
420__mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
421 struct mem_cgroup_per_zone *mz,
422 struct mem_cgroup_tree_per_zone *mctz)
423{
424 if (!mz->on_tree)
425 return;
426 rb_erase(&mz->tree_node, &mctz->rb_root);
427 mz->on_tree = false;
428}
429
430static void
Balbir Singhf64c3f52009-09-23 15:56:37 -0700431mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
432 struct mem_cgroup_per_zone *mz,
433 struct mem_cgroup_tree_per_zone *mctz)
434{
435 spin_lock(&mctz->lock);
Balbir Singh4e416952009-09-23 15:56:39 -0700436 __mem_cgroup_remove_exceeded(mem, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700437 spin_unlock(&mctz->lock);
438}
439
Balbir Singhf64c3f52009-09-23 15:56:37 -0700440
441static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
442{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700443 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700444 struct mem_cgroup_per_zone *mz;
445 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700446 int nid = page_to_nid(page);
447 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700448 mctz = soft_limit_tree_from_page(page);
449
450 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700451 * Necessary to update all ancestors when hierarchy is used.
452 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700453 */
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700454 for (; mem; mem = parent_mem_cgroup(mem)) {
455 mz = mem_cgroup_zoneinfo(mem, nid, zid);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700456 excess = res_counter_soft_limit_excess(&mem->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700457 /*
458 * We have to update the tree if mz is on RB-tree or
459 * mem is over its softlimit.
460 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700461 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700462 spin_lock(&mctz->lock);
463 /* if on-tree, remove it */
464 if (mz->on_tree)
465 __mem_cgroup_remove_exceeded(mem, mz, mctz);
466 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700467 * Insert again. mz->usage_in_excess will be updated.
468 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700469 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700470 __mem_cgroup_insert_exceeded(mem, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700471 spin_unlock(&mctz->lock);
472 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700473 }
474}
475
476static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
477{
478 int node, zone;
479 struct mem_cgroup_per_zone *mz;
480 struct mem_cgroup_tree_per_zone *mctz;
481
482 for_each_node_state(node, N_POSSIBLE) {
483 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
484 mz = mem_cgroup_zoneinfo(mem, node, zone);
485 mctz = soft_limit_tree_node_zone(node, zone);
486 mem_cgroup_remove_exceeded(mem, mz, mctz);
487 }
488 }
489}
490
Balbir Singh4e416952009-09-23 15:56:39 -0700491static inline unsigned long mem_cgroup_get_excess(struct mem_cgroup *mem)
492{
493 return res_counter_soft_limit_excess(&mem->res) >> PAGE_SHIFT;
494}
495
496static struct mem_cgroup_per_zone *
497__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
498{
499 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700500 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700501
502retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700503 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700504 rightmost = rb_last(&mctz->rb_root);
505 if (!rightmost)
506 goto done; /* Nothing to reclaim from */
507
508 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
509 /*
510 * Remove the node now but someone else can add it back,
511 * we will to add it back at the end of reclaim to its correct
512 * position in the tree.
513 */
514 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
515 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
516 !css_tryget(&mz->mem->css))
517 goto retry;
518done:
519 return mz;
520}
521
522static struct mem_cgroup_per_zone *
523mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
524{
525 struct mem_cgroup_per_zone *mz;
526
527 spin_lock(&mctz->lock);
528 mz = __mem_cgroup_largest_soft_limit_node(mctz);
529 spin_unlock(&mctz->lock);
530 return mz;
531}
532
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800533static s64 mem_cgroup_read_stat(struct mem_cgroup *mem,
534 enum mem_cgroup_stat_index idx)
535{
536 int cpu;
537 s64 val = 0;
538
539 for_each_possible_cpu(cpu)
540 val += per_cpu(mem->stat->count[idx], cpu);
541 return val;
542}
543
544static s64 mem_cgroup_local_usage(struct mem_cgroup *mem)
545{
546 s64 ret;
547
548 ret = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS);
549 ret += mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE);
550 return ret;
551}
552
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700553static void mem_cgroup_swap_statistics(struct mem_cgroup *mem,
554 bool charge)
555{
556 int val = (charge) ? 1 : -1;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800557 this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_SWAPOUT], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700558}
559
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700560static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
561 struct page_cgroup *pc,
562 bool charge)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800563{
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700564 int val = (charge) ? 1 : -1;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800565
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800566 preempt_disable();
567
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700568 if (PageCgroupCache(pc))
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800569 __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_CACHE], val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800570 else
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800571 __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_RSS], val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700572
573 if (charge)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800574 __this_cpu_inc(mem->stat->count[MEM_CGROUP_STAT_PGPGIN_COUNT]);
Balaji Rao55e462b2008-05-01 04:35:12 -0700575 else
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800576 __this_cpu_inc(mem->stat->count[MEM_CGROUP_STAT_PGPGOUT_COUNT]);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800577 __this_cpu_inc(mem->stat->count[MEM_CGROUP_EVENTS]);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800578
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800579 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800580}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800581
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700582static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700583 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800584{
585 int nid, zid;
586 struct mem_cgroup_per_zone *mz;
587 u64 total = 0;
588
589 for_each_online_node(nid)
590 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
591 mz = mem_cgroup_zoneinfo(mem, nid, zid);
592 total += MEM_CGROUP_ZSTAT(mz, idx);
593 }
594 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800595}
596
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800597static bool __memcg_event_check(struct mem_cgroup *mem, int event_mask_shift)
598{
599 s64 val;
600
601 val = this_cpu_read(mem->stat->count[MEM_CGROUP_EVENTS]);
602
603 return !(val & ((1 << event_mask_shift) - 1));
604}
605
606/*
607 * Check events in order.
608 *
609 */
610static void memcg_check_events(struct mem_cgroup *mem, struct page *page)
611{
612 /* threshold event is triggered in finer grain than soft limit */
613 if (unlikely(__memcg_event_check(mem, THRESHOLDS_EVENTS_THRESH))) {
614 mem_cgroup_threshold(mem);
615 if (unlikely(__memcg_event_check(mem, SOFTLIMIT_EVENTS_THRESH)))
616 mem_cgroup_update_tree(mem, page);
617 }
618}
619
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800620static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800621{
622 return container_of(cgroup_subsys_state(cont,
623 mem_cgroup_subsys_id), struct mem_cgroup,
624 css);
625}
626
Balbir Singhcf475ad2008-04-29 01:00:16 -0700627struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800628{
Balbir Singh31a78f22008-09-28 23:09:31 +0100629 /*
630 * mm_update_next_owner() may clear mm->owner to NULL
631 * if it races with swapoff, page migration, etc.
632 * So this can be called with p == NULL.
633 */
634 if (unlikely(!p))
635 return NULL;
636
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800637 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
638 struct mem_cgroup, css);
639}
640
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800641static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
642{
643 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700644
645 if (!mm)
646 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800647 /*
648 * Because we have no locks, mm->owner's may be being moved to other
649 * cgroup. We use css_tryget() here even if this looks
650 * pessimistic (rather than adding locks here).
651 */
652 rcu_read_lock();
653 do {
654 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
655 if (unlikely(!mem))
656 break;
657 } while (!css_tryget(&mem->css));
658 rcu_read_unlock();
659 return mem;
660}
661
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700662/*
663 * Call callback function against all cgroup under hierarchy tree.
664 */
665static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
666 int (*func)(struct mem_cgroup *, void *))
667{
668 int found, ret, nextid;
669 struct cgroup_subsys_state *css;
670 struct mem_cgroup *mem;
671
672 if (!root->use_hierarchy)
673 return (*func)(root, data);
674
675 nextid = 1;
676 do {
677 ret = 0;
678 mem = NULL;
679
680 rcu_read_lock();
681 css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
682 &found);
683 if (css && css_tryget(css))
684 mem = container_of(css, struct mem_cgroup, css);
685 rcu_read_unlock();
686
687 if (mem) {
688 ret = (*func)(mem, data);
689 css_put(&mem->css);
690 }
691 nextid = found + 1;
692 } while (!ret && css);
693
694 return ret;
695}
696
Balbir Singh4b3bde42009-09-23 15:56:32 -0700697static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
698{
699 return (mem == root_mem_cgroup);
700}
701
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800702/*
703 * Following LRU functions are allowed to be used without PCG_LOCK.
704 * Operations are called by routine of global LRU independently from memcg.
705 * What we have to take care of here is validness of pc->mem_cgroup.
706 *
707 * Changes to pc->mem_cgroup happens when
708 * 1. charge
709 * 2. moving account
710 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
711 * It is added to LRU before charge.
712 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
713 * When moving account, the page is not on LRU. It's isolated.
714 */
715
716void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800717{
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800718 struct page_cgroup *pc;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800719 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700720
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800721 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800722 return;
723 pc = lookup_page_cgroup(page);
724 /* can happen while we handle swapcache. */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700725 if (!TestClearPageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800726 return;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700727 VM_BUG_ON(!pc->mem_cgroup);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800728 /*
729 * We don't check PCG_USED bit. It's cleared when the "page" is finally
730 * removed from global LRU.
731 */
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800732 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700733 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700734 if (mem_cgroup_is_root(pc->mem_cgroup))
735 return;
736 VM_BUG_ON(list_empty(&pc->lru));
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800737 list_del_init(&pc->lru);
738 return;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800739}
740
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800741void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800742{
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800743 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800744}
745
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800746void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800747{
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800748 struct mem_cgroup_per_zone *mz;
749 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800750
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800751 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700752 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700753
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800754 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800755 /*
756 * Used bit is set without atomic ops but after smp_wmb().
757 * For making pc->mem_cgroup visible, insert smp_rmb() here.
758 */
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800759 smp_rmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -0700760 /* unused or root page is not rotated. */
761 if (!PageCgroupUsed(pc) || mem_cgroup_is_root(pc->mem_cgroup))
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800762 return;
763 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700764 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800765}
766
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800767void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
768{
769 struct page_cgroup *pc;
770 struct mem_cgroup_per_zone *mz;
771
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800772 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800773 return;
774 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700775 VM_BUG_ON(PageCgroupAcctLRU(pc));
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800776 /*
777 * Used bit is set without atomic ops but after smp_wmb().
778 * For making pc->mem_cgroup visible, insert smp_rmb() here.
779 */
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800780 smp_rmb();
781 if (!PageCgroupUsed(pc))
782 return;
783
784 mz = page_cgroup_zoneinfo(pc);
785 MEM_CGROUP_ZSTAT(mz, lru) += 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700786 SetPageCgroupAcctLRU(pc);
787 if (mem_cgroup_is_root(pc->mem_cgroup))
788 return;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800789 list_add(&pc->lru, &mz->lists[lru]);
790}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800791
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800792/*
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800793 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
794 * lru because the page may.be reused after it's fully uncharged (because of
795 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
796 * it again. This function is only used to charge SwapCache. It's done under
797 * lock_page and expected that zone->lru_lock is never held.
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800798 */
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800799static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800800{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800801 unsigned long flags;
802 struct zone *zone = page_zone(page);
803 struct page_cgroup *pc = lookup_page_cgroup(page);
804
805 spin_lock_irqsave(&zone->lru_lock, flags);
806 /*
807 * Forget old LRU when this page_cgroup is *not* used. This Used bit
808 * is guarded by lock_page() because the page is SwapCache.
809 */
810 if (!PageCgroupUsed(pc))
811 mem_cgroup_del_lru_list(page, page_lru(page));
812 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800813}
814
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800815static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
816{
817 unsigned long flags;
818 struct zone *zone = page_zone(page);
819 struct page_cgroup *pc = lookup_page_cgroup(page);
820
821 spin_lock_irqsave(&zone->lru_lock, flags);
822 /* link when the page is linked to LRU but page_cgroup isn't */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700823 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800824 mem_cgroup_add_lru_list(page, page_lru(page));
825 spin_unlock_irqrestore(&zone->lru_lock, flags);
826}
827
828
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800829void mem_cgroup_move_lists(struct page *page,
830 enum lru_list from, enum lru_list to)
831{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800832 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800833 return;
834 mem_cgroup_del_lru_list(page, from);
835 mem_cgroup_add_lru_list(page, to);
836}
837
David Rientjes4c4a2212008-02-07 00:14:06 -0800838int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
839{
840 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700841 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -0700842 struct task_struct *p;
David Rientjes4c4a2212008-02-07 00:14:06 -0800843
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -0700844 p = find_lock_task_mm(task);
845 if (!p)
846 return 0;
847 curr = try_get_mem_cgroup_from_mm(p->mm);
848 task_unlock(p);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700849 if (!curr)
850 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -0800851 /*
852 * We should check use_hierarchy of "mem" not "curr". Because checking
853 * use_hierarchy of "curr" here make this function true if hierarchy is
854 * enabled in "curr" and "curr" is a child of "mem" in *cgroup*
855 * hierarchy(even if use_hierarchy is disabled in "mem").
856 */
857 if (mem->use_hierarchy)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700858 ret = css_is_ancestor(&curr->css, &mem->css);
859 else
860 ret = (curr == mem);
861 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -0800862 return ret;
863}
864
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800865static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800866{
867 unsigned long active;
868 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800869 unsigned long gb;
870 unsigned long inactive_ratio;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800871
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700872 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
873 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800874
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800875 gb = (inactive + active) >> (30 - PAGE_SHIFT);
876 if (gb)
877 inactive_ratio = int_sqrt(10 * gb);
878 else
879 inactive_ratio = 1;
880
881 if (present_pages) {
882 present_pages[0] = inactive;
883 present_pages[1] = active;
884 }
885
886 return inactive_ratio;
887}
888
889int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
890{
891 unsigned long active;
892 unsigned long inactive;
893 unsigned long present_pages[2];
894 unsigned long inactive_ratio;
895
896 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
897
898 inactive = present_pages[0];
899 active = present_pages[1];
900
901 if (inactive * inactive_ratio < active)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800902 return 1;
903
904 return 0;
905}
906
Rik van Riel56e49d22009-06-16 15:32:28 -0700907int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
908{
909 unsigned long active;
910 unsigned long inactive;
911
912 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
913 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
914
915 return (active > inactive);
916}
917
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800918unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
919 struct zone *zone,
920 enum lru_list lru)
921{
922 int nid = zone->zone_pgdat->node_id;
923 int zid = zone_idx(zone);
924 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
925
926 return MEM_CGROUP_ZSTAT(mz, lru);
927}
928
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800929struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
930 struct zone *zone)
931{
932 int nid = zone->zone_pgdat->node_id;
933 int zid = zone_idx(zone);
934 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
935
936 return &mz->reclaim_stat;
937}
938
939struct zone_reclaim_stat *
940mem_cgroup_get_reclaim_stat_from_page(struct page *page)
941{
942 struct page_cgroup *pc;
943 struct mem_cgroup_per_zone *mz;
944
945 if (mem_cgroup_disabled())
946 return NULL;
947
948 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800949 /*
950 * Used bit is set without atomic ops but after smp_wmb().
951 * For making pc->mem_cgroup visible, insert smp_rmb() here.
952 */
953 smp_rmb();
954 if (!PageCgroupUsed(pc))
955 return NULL;
956
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800957 mz = page_cgroup_zoneinfo(pc);
958 if (!mz)
959 return NULL;
960
961 return &mz->reclaim_stat;
962}
963
Balbir Singh66e17072008-02-07 00:13:56 -0800964unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
965 struct list_head *dst,
966 unsigned long *scanned, int order,
967 int mode, struct zone *z,
968 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700969 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -0800970{
971 unsigned long nr_taken = 0;
972 struct page *page;
973 unsigned long scan;
974 LIST_HEAD(pc_list);
975 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800976 struct page_cgroup *pc, *tmp;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800977 int nid = z->zone_pgdat->node_id;
978 int zid = zone_idx(z);
979 struct mem_cgroup_per_zone *mz;
Johannes Weinerb7c46d12009-09-21 17:02:56 -0700980 int lru = LRU_FILE * file + active;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700981 int ret;
Balbir Singh66e17072008-02-07 00:13:56 -0800982
Balbir Singhcf475ad2008-04-29 01:00:16 -0700983 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800984 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700985 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -0800986
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800987 scan = 0;
988 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -0800989 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800990 break;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800991
992 page = pc->page;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700993 if (unlikely(!PageCgroupUsed(pc)))
994 continue;
Hugh Dickins436c65412008-02-07 00:14:12 -0800995 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800996 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800997
Hugh Dickins436c65412008-02-07 00:14:12 -0800998 scan++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700999 ret = __isolate_lru_page(page, mode, file);
1000 switch (ret) {
1001 case 0:
Balbir Singh66e17072008-02-07 00:13:56 -08001002 list_move(&page->lru, dst);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001003 mem_cgroup_del_lru(page);
Balbir Singh66e17072008-02-07 00:13:56 -08001004 nr_taken++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001005 break;
1006 case -EBUSY:
1007 /* we don't affect global LRU but rotate in our LRU */
1008 mem_cgroup_rotate_lru_list(page, page_lru(page));
1009 break;
1010 default:
1011 break;
Balbir Singh66e17072008-02-07 00:13:56 -08001012 }
1013 }
1014
Balbir Singh66e17072008-02-07 00:13:56 -08001015 *scanned = scan;
KOSAKI Motohirocc8e9702010-08-09 17:19:57 -07001016
1017 trace_mm_vmscan_memcg_isolate(0, nr_to_scan, scan, nr_taken,
1018 0, 0, 0, mode);
1019
Balbir Singh66e17072008-02-07 00:13:56 -08001020 return nr_taken;
1021}
1022
Balbir Singh6d61ef42009-01-07 18:08:06 -08001023#define mem_cgroup_from_res_counter(counter, member) \
1024 container_of(counter, struct mem_cgroup, member)
1025
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -08001026static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
1027{
1028 if (do_swap_account) {
1029 if (res_counter_check_under_limit(&mem->res) &&
1030 res_counter_check_under_limit(&mem->memsw))
1031 return true;
1032 } else
1033 if (res_counter_check_under_limit(&mem->res))
1034 return true;
1035 return false;
1036}
1037
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001038static unsigned int get_swappiness(struct mem_cgroup *memcg)
1039{
1040 struct cgroup *cgrp = memcg->css.cgroup;
1041 unsigned int swappiness;
1042
1043 /* root ? */
1044 if (cgrp->parent == NULL)
1045 return vm_swappiness;
1046
1047 spin_lock(&memcg->reclaim_param_lock);
1048 swappiness = memcg->swappiness;
1049 spin_unlock(&memcg->reclaim_param_lock);
1050
1051 return swappiness;
1052}
1053
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001054/* A routine for testing mem is not under move_account */
1055
1056static bool mem_cgroup_under_move(struct mem_cgroup *mem)
1057{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001058 struct mem_cgroup *from;
1059 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001060 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001061 /*
1062 * Unlike task_move routines, we access mc.to, mc.from not under
1063 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1064 */
1065 spin_lock(&mc.lock);
1066 from = mc.from;
1067 to = mc.to;
1068 if (!from)
1069 goto unlock;
1070 if (from == mem || to == mem
1071 || (mem->use_hierarchy && css_is_ancestor(&from->css, &mem->css))
1072 || (mem->use_hierarchy && css_is_ancestor(&to->css, &mem->css)))
1073 ret = true;
1074unlock:
1075 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001076 return ret;
1077}
1078
1079static bool mem_cgroup_wait_acct_move(struct mem_cgroup *mem)
1080{
1081 if (mc.moving_task && current != mc.moving_task) {
1082 if (mem_cgroup_under_move(mem)) {
1083 DEFINE_WAIT(wait);
1084 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1085 /* moving charge context might have finished. */
1086 if (mc.moving_task)
1087 schedule();
1088 finish_wait(&mc.waitq, &wait);
1089 return true;
1090 }
1091 }
1092 return false;
1093}
1094
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001095static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
1096{
1097 int *val = data;
1098 (*val)++;
1099 return 0;
1100}
Balbir Singhe2224322009-04-02 16:57:39 -07001101
1102/**
Kirill A. Shutemov6a6135b2010-03-10 15:22:25 -08001103 * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode.
Balbir Singhe2224322009-04-02 16:57:39 -07001104 * @memcg: The memory cgroup that went over limit
1105 * @p: Task that is going to be killed
1106 *
1107 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1108 * enabled
1109 */
1110void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1111{
1112 struct cgroup *task_cgrp;
1113 struct cgroup *mem_cgrp;
1114 /*
1115 * Need a buffer in BSS, can't rely on allocations. The code relies
1116 * on the assumption that OOM is serialized for memory controller.
1117 * If this assumption is broken, revisit this code.
1118 */
1119 static char memcg_name[PATH_MAX];
1120 int ret;
1121
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001122 if (!memcg || !p)
Balbir Singhe2224322009-04-02 16:57:39 -07001123 return;
1124
1125
1126 rcu_read_lock();
1127
1128 mem_cgrp = memcg->css.cgroup;
1129 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1130
1131 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1132 if (ret < 0) {
1133 /*
1134 * Unfortunately, we are unable to convert to a useful name
1135 * But we'll still print out the usage information
1136 */
1137 rcu_read_unlock();
1138 goto done;
1139 }
1140 rcu_read_unlock();
1141
1142 printk(KERN_INFO "Task in %s killed", memcg_name);
1143
1144 rcu_read_lock();
1145 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1146 if (ret < 0) {
1147 rcu_read_unlock();
1148 goto done;
1149 }
1150 rcu_read_unlock();
1151
1152 /*
1153 * Continues from above, so we don't need an KERN_ level
1154 */
1155 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1156done:
1157
1158 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1159 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1160 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1161 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1162 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1163 "failcnt %llu\n",
1164 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1165 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1166 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1167}
1168
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001169/*
1170 * This function returns the number of memcg under hierarchy tree. Returns
1171 * 1(self count) if no children.
1172 */
1173static int mem_cgroup_count_children(struct mem_cgroup *mem)
1174{
1175 int num = 0;
1176 mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
1177 return num;
1178}
1179
Balbir Singh6d61ef42009-01-07 18:08:06 -08001180/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001181 * Return the memory (and swap, if configured) limit for a memcg.
1182 */
1183u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
1184{
1185 u64 limit;
1186 u64 memsw;
1187
1188 limit = res_counter_read_u64(&memcg->res, RES_LIMIT) +
1189 total_swap_pages;
1190 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1191 /*
1192 * If memsw is finite and limits the amount of swap space available
1193 * to this memcg, return that limit.
1194 */
1195 return min(limit, memsw);
1196}
1197
1198/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001199 * Visit the first child (need not be the first child as per the ordering
1200 * of the cgroup list, since we track last_scanned_child) of @mem and use
1201 * that to reclaim free pages from.
1202 */
1203static struct mem_cgroup *
1204mem_cgroup_select_victim(struct mem_cgroup *root_mem)
1205{
1206 struct mem_cgroup *ret = NULL;
1207 struct cgroup_subsys_state *css;
1208 int nextid, found;
1209
1210 if (!root_mem->use_hierarchy) {
1211 css_get(&root_mem->css);
1212 ret = root_mem;
1213 }
1214
1215 while (!ret) {
1216 rcu_read_lock();
1217 nextid = root_mem->last_scanned_child + 1;
1218 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
1219 &found);
1220 if (css && css_tryget(css))
1221 ret = container_of(css, struct mem_cgroup, css);
1222
1223 rcu_read_unlock();
1224 /* Updates scanning parameter */
1225 spin_lock(&root_mem->reclaim_param_lock);
1226 if (!css) {
1227 /* this means start scan from ID:1 */
1228 root_mem->last_scanned_child = 0;
1229 } else
1230 root_mem->last_scanned_child = found;
1231 spin_unlock(&root_mem->reclaim_param_lock);
1232 }
1233
1234 return ret;
1235}
1236
1237/*
1238 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1239 * we reclaimed from, so that we don't end up penalizing one child extensively
1240 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001241 *
1242 * root_mem is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001243 *
1244 * We give up and return to the caller when we visit root_mem twice.
1245 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001246 *
1247 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001248 */
1249static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
Balbir Singh4e416952009-09-23 15:56:39 -07001250 struct zone *zone,
Balbir Singh75822b42009-09-23 15:56:38 -07001251 gfp_t gfp_mask,
1252 unsigned long reclaim_options)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001253{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001254 struct mem_cgroup *victim;
1255 int ret, total = 0;
1256 int loop = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001257 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1258 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
Balbir Singh4e416952009-09-23 15:56:39 -07001259 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
1260 unsigned long excess = mem_cgroup_get_excess(root_mem);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001261
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001262 /* If memsw_is_minimum==1, swap-out is of-no-use. */
1263 if (root_mem->memsw_is_minimum)
1264 noswap = true;
1265
Balbir Singh4e416952009-09-23 15:56:39 -07001266 while (1) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001267 victim = mem_cgroup_select_victim(root_mem);
Balbir Singh4e416952009-09-23 15:56:39 -07001268 if (victim == root_mem) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001269 loop++;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001270 if (loop >= 1)
1271 drain_all_stock_async();
Balbir Singh4e416952009-09-23 15:56:39 -07001272 if (loop >= 2) {
1273 /*
1274 * If we have not been able to reclaim
1275 * anything, it might because there are
1276 * no reclaimable pages under this hierarchy
1277 */
1278 if (!check_soft || !total) {
1279 css_put(&victim->css);
1280 break;
1281 }
1282 /*
1283 * We want to do more targetted reclaim.
1284 * excess >> 2 is not to excessive so as to
1285 * reclaim too much, nor too less that we keep
1286 * coming back to reclaim from this cgroup
1287 */
1288 if (total >= (excess >> 2) ||
1289 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1290 css_put(&victim->css);
1291 break;
1292 }
1293 }
1294 }
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08001295 if (!mem_cgroup_local_usage(victim)) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001296 /* this cgroup's local usage == 0 */
1297 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001298 continue;
1299 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001300 /* we use swappiness of local cgroup */
Balbir Singh4e416952009-09-23 15:56:39 -07001301 if (check_soft)
1302 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
1303 noswap, get_swappiness(victim), zone,
1304 zone->zone_pgdat->node_id);
1305 else
1306 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
1307 noswap, get_swappiness(victim));
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001308 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001309 /*
1310 * At shrinking usage, we can't check we should stop here or
1311 * reclaim more. It's depends on callers. last_scanned_child
1312 * will work enough for keeping fairness under tree.
1313 */
1314 if (shrink)
1315 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001316 total += ret;
Balbir Singh4e416952009-09-23 15:56:39 -07001317 if (check_soft) {
1318 if (res_counter_check_under_soft_limit(&root_mem->res))
1319 return total;
1320 } else if (mem_cgroup_check_under_limit(root_mem))
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001321 return 1 + total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001322 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001323 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001324}
1325
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001326static int mem_cgroup_oom_lock_cb(struct mem_cgroup *mem, void *data)
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001327{
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001328 int *val = (int *)data;
1329 int x;
1330 /*
1331 * Logically, we can stop scanning immediately when we find
1332 * a memcg is already locked. But condidering unlock ops and
1333 * creation/removal of memcg, scan-all is simple operation.
1334 */
1335 x = atomic_inc_return(&mem->oom_lock);
1336 *val = max(x, *val);
1337 return 0;
1338}
1339/*
1340 * Check OOM-Killer is already running under our hierarchy.
1341 * If someone is running, return false.
1342 */
1343static bool mem_cgroup_oom_lock(struct mem_cgroup *mem)
1344{
1345 int lock_count = 0;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001346
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001347 mem_cgroup_walk_tree(mem, &lock_count, mem_cgroup_oom_lock_cb);
1348
1349 if (lock_count == 1)
1350 return true;
1351 return false;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001352}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001353
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001354static int mem_cgroup_oom_unlock_cb(struct mem_cgroup *mem, void *data)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001355{
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001356 /*
1357 * When a new child is created while the hierarchy is under oom,
1358 * mem_cgroup_oom_lock() may not be called. We have to use
1359 * atomic_add_unless() here.
1360 */
1361 atomic_add_unless(&mem->oom_lock, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001362 return 0;
1363}
1364
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001365static void mem_cgroup_oom_unlock(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001366{
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001367 mem_cgroup_walk_tree(mem, NULL, mem_cgroup_oom_unlock_cb);
1368}
1369
1370static DEFINE_MUTEX(memcg_oom_mutex);
1371static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1372
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001373struct oom_wait_info {
1374 struct mem_cgroup *mem;
1375 wait_queue_t wait;
1376};
1377
1378static int memcg_oom_wake_function(wait_queue_t *wait,
1379 unsigned mode, int sync, void *arg)
1380{
1381 struct mem_cgroup *wake_mem = (struct mem_cgroup *)arg;
1382 struct oom_wait_info *oom_wait_info;
1383
1384 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
1385
1386 if (oom_wait_info->mem == wake_mem)
1387 goto wakeup;
1388 /* if no hierarchy, no match */
1389 if (!oom_wait_info->mem->use_hierarchy || !wake_mem->use_hierarchy)
1390 return 0;
1391 /*
1392 * Both of oom_wait_info->mem and wake_mem are stable under us.
1393 * Then we can use css_is_ancestor without taking care of RCU.
1394 */
1395 if (!css_is_ancestor(&oom_wait_info->mem->css, &wake_mem->css) &&
1396 !css_is_ancestor(&wake_mem->css, &oom_wait_info->mem->css))
1397 return 0;
1398
1399wakeup:
1400 return autoremove_wake_function(wait, mode, sync, arg);
1401}
1402
1403static void memcg_wakeup_oom(struct mem_cgroup *mem)
1404{
1405 /* for filtering, pass "mem" as argument. */
1406 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, mem);
1407}
1408
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001409static void memcg_oom_recover(struct mem_cgroup *mem)
1410{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001411 if (mem && atomic_read(&mem->oom_lock))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001412 memcg_wakeup_oom(mem);
1413}
1414
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001415/*
1416 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
1417 */
1418bool mem_cgroup_handle_oom(struct mem_cgroup *mem, gfp_t mask)
1419{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001420 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001421 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001422
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001423 owait.mem = mem;
1424 owait.wait.flags = 0;
1425 owait.wait.func = memcg_oom_wake_function;
1426 owait.wait.private = current;
1427 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001428 need_to_kill = true;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001429 /* At first, try to OOM lock hierarchy under mem.*/
1430 mutex_lock(&memcg_oom_mutex);
1431 locked = mem_cgroup_oom_lock(mem);
1432 /*
1433 * Even if signal_pending(), we can't quit charge() loop without
1434 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
1435 * under OOM is always welcomed, use TASK_KILLABLE here.
1436 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001437 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
1438 if (!locked || mem->oom_kill_disable)
1439 need_to_kill = false;
1440 if (locked)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07001441 mem_cgroup_oom_notify(mem);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001442 mutex_unlock(&memcg_oom_mutex);
1443
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001444 if (need_to_kill) {
1445 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001446 mem_cgroup_out_of_memory(mem, mask);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001447 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001448 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001449 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001450 }
1451 mutex_lock(&memcg_oom_mutex);
1452 mem_cgroup_oom_unlock(mem);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001453 memcg_wakeup_oom(mem);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001454 mutex_unlock(&memcg_oom_mutex);
1455
1456 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
1457 return false;
1458 /* Give chance to dying process */
1459 schedule_timeout(1);
1460 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001461}
1462
Balbir Singhd69b0422009-06-17 16:26:34 -07001463/*
1464 * Currently used to update mapped file statistics, but the routine can be
1465 * generalized to update other statistics as well.
1466 */
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001467void mem_cgroup_update_file_mapped(struct page *page, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07001468{
1469 struct mem_cgroup *mem;
Balbir Singhd69b0422009-06-17 16:26:34 -07001470 struct page_cgroup *pc;
1471
Balbir Singhd69b0422009-06-17 16:26:34 -07001472 pc = lookup_page_cgroup(page);
1473 if (unlikely(!pc))
1474 return;
1475
1476 lock_page_cgroup(pc);
1477 mem = pc->mem_cgroup;
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07001478 if (!mem || !PageCgroupUsed(pc))
Balbir Singhd69b0422009-06-17 16:26:34 -07001479 goto done;
1480
1481 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08001482 * Preemption is already disabled. We can use __this_cpu_xxx
Balbir Singhd69b0422009-06-17 16:26:34 -07001483 */
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07001484 if (val > 0) {
1485 __this_cpu_inc(mem->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
1486 SetPageCgroupFileMapped(pc);
1487 } else {
1488 __this_cpu_dec(mem->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
1489 ClearPageCgroupFileMapped(pc);
1490 }
Balbir Singhd69b0422009-06-17 16:26:34 -07001491
Balbir Singhd69b0422009-06-17 16:26:34 -07001492done:
1493 unlock_page_cgroup(pc);
1494}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001495
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001496/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001497 * size of first charge trial. "32" comes from vmscan.c's magic value.
1498 * TODO: maybe necessary to use big numbers in big irons.
1499 */
1500#define CHARGE_SIZE (32 * PAGE_SIZE)
1501struct memcg_stock_pcp {
1502 struct mem_cgroup *cached; /* this never be root cgroup */
1503 int charge;
1504 struct work_struct work;
1505};
1506static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
1507static atomic_t memcg_drain_count;
1508
1509/*
1510 * Try to consume stocked charge on this cpu. If success, PAGE_SIZE is consumed
1511 * from local stock and true is returned. If the stock is 0 or charges from a
1512 * cgroup which is not current target, returns false. This stock will be
1513 * refilled.
1514 */
1515static bool consume_stock(struct mem_cgroup *mem)
1516{
1517 struct memcg_stock_pcp *stock;
1518 bool ret = true;
1519
1520 stock = &get_cpu_var(memcg_stock);
1521 if (mem == stock->cached && stock->charge)
1522 stock->charge -= PAGE_SIZE;
1523 else /* need to call res_counter_charge */
1524 ret = false;
1525 put_cpu_var(memcg_stock);
1526 return ret;
1527}
1528
1529/*
1530 * Returns stocks cached in percpu to res_counter and reset cached information.
1531 */
1532static void drain_stock(struct memcg_stock_pcp *stock)
1533{
1534 struct mem_cgroup *old = stock->cached;
1535
1536 if (stock->charge) {
1537 res_counter_uncharge(&old->res, stock->charge);
1538 if (do_swap_account)
1539 res_counter_uncharge(&old->memsw, stock->charge);
1540 }
1541 stock->cached = NULL;
1542 stock->charge = 0;
1543}
1544
1545/*
1546 * This must be called under preempt disabled or must be called by
1547 * a thread which is pinned to local cpu.
1548 */
1549static void drain_local_stock(struct work_struct *dummy)
1550{
1551 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
1552 drain_stock(stock);
1553}
1554
1555/*
1556 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01001557 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001558 */
1559static void refill_stock(struct mem_cgroup *mem, int val)
1560{
1561 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
1562
1563 if (stock->cached != mem) { /* reset if necessary */
1564 drain_stock(stock);
1565 stock->cached = mem;
1566 }
1567 stock->charge += val;
1568 put_cpu_var(memcg_stock);
1569}
1570
1571/*
1572 * Tries to drain stocked charges in other cpus. This function is asynchronous
1573 * and just put a work per cpu for draining localy on each cpu. Caller can
1574 * expects some charges will be back to res_counter later but cannot wait for
1575 * it.
1576 */
1577static void drain_all_stock_async(void)
1578{
1579 int cpu;
1580 /* This function is for scheduling "drain" in asynchronous way.
1581 * The result of "drain" is not directly handled by callers. Then,
1582 * if someone is calling drain, we don't have to call drain more.
1583 * Anyway, WORK_STRUCT_PENDING check in queue_work_on() will catch if
1584 * there is a race. We just do loose check here.
1585 */
1586 if (atomic_read(&memcg_drain_count))
1587 return;
1588 /* Notify other cpus that system-wide "drain" is running */
1589 atomic_inc(&memcg_drain_count);
1590 get_online_cpus();
1591 for_each_online_cpu(cpu) {
1592 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
1593 schedule_work_on(cpu, &stock->work);
1594 }
1595 put_online_cpus();
1596 atomic_dec(&memcg_drain_count);
1597 /* We don't wait for flush_work */
1598}
1599
1600/* This is a synchronous drain interface. */
1601static void drain_all_stock_sync(void)
1602{
1603 /* called when force_empty is called */
1604 atomic_inc(&memcg_drain_count);
1605 schedule_on_each_cpu(drain_local_stock);
1606 atomic_dec(&memcg_drain_count);
1607}
1608
1609static int __cpuinit memcg_stock_cpu_callback(struct notifier_block *nb,
1610 unsigned long action,
1611 void *hcpu)
1612{
1613 int cpu = (unsigned long)hcpu;
1614 struct memcg_stock_pcp *stock;
1615
1616 if (action != CPU_DEAD)
1617 return NOTIFY_OK;
1618 stock = &per_cpu(memcg_stock, cpu);
1619 drain_stock(stock);
1620 return NOTIFY_OK;
1621}
1622
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001623
1624/* See __mem_cgroup_try_charge() for details */
1625enum {
1626 CHARGE_OK, /* success */
1627 CHARGE_RETRY, /* need to retry but retry is not bad */
1628 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
1629 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
1630 CHARGE_OOM_DIE, /* the current is killed because of OOM */
1631};
1632
1633static int __mem_cgroup_do_charge(struct mem_cgroup *mem, gfp_t gfp_mask,
1634 int csize, bool oom_check)
1635{
1636 struct mem_cgroup *mem_over_limit;
1637 struct res_counter *fail_res;
1638 unsigned long flags = 0;
1639 int ret;
1640
1641 ret = res_counter_charge(&mem->res, csize, &fail_res);
1642
1643 if (likely(!ret)) {
1644 if (!do_swap_account)
1645 return CHARGE_OK;
1646 ret = res_counter_charge(&mem->memsw, csize, &fail_res);
1647 if (likely(!ret))
1648 return CHARGE_OK;
1649
1650 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
1651 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
1652 } else
1653 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
1654
1655 if (csize > PAGE_SIZE) /* change csize and retry */
1656 return CHARGE_RETRY;
1657
1658 if (!(gfp_mask & __GFP_WAIT))
1659 return CHARGE_WOULDBLOCK;
1660
1661 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
1662 gfp_mask, flags);
1663 /*
1664 * try_to_free_mem_cgroup_pages() might not give us a full
1665 * picture of reclaim. Some pages are reclaimed and might be
1666 * moved to swap cache or just unmapped from the cgroup.
1667 * Check the limit again to see if the reclaim reduced the
1668 * current usage of the cgroup before giving up
1669 */
1670 if (ret || mem_cgroup_check_under_limit(mem_over_limit))
1671 return CHARGE_RETRY;
1672
1673 /*
1674 * At task move, charge accounts can be doubly counted. So, it's
1675 * better to wait until the end of task_move if something is going on.
1676 */
1677 if (mem_cgroup_wait_acct_move(mem_over_limit))
1678 return CHARGE_RETRY;
1679
1680 /* If we don't need to call oom-killer at el, return immediately */
1681 if (!oom_check)
1682 return CHARGE_NOMEM;
1683 /* check OOM */
1684 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask))
1685 return CHARGE_OOM_DIE;
1686
1687 return CHARGE_RETRY;
1688}
1689
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001690/*
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001691 * Unlike exported interface, "oom" parameter is added. if oom==true,
1692 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001693 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001694static int __mem_cgroup_try_charge(struct mm_struct *mm,
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001695 gfp_t gfp_mask, struct mem_cgroup **memcg, bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001696{
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001697 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
1698 struct mem_cgroup *mem = NULL;
1699 int ret;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001700 int csize = CHARGE_SIZE;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001701
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001702 /*
1703 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
1704 * in system level. So, allow to go ahead dying process in addition to
1705 * MEMDIE process.
1706 */
1707 if (unlikely(test_thread_flag(TIF_MEMDIE)
1708 || fatal_signal_pending(current)))
1709 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001710
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001711 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08001712 * We always charge the cgroup the mm_struct belongs to.
1713 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001714 * thread group leader migrates. It's possible that mm is not
1715 * set, if so charge the init_mm (happens for pagecache usage).
1716 */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001717 if (!*memcg && !mm)
1718 goto bypass;
1719again:
1720 if (*memcg) { /* css should be a valid one */
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001721 mem = *memcg;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001722 VM_BUG_ON(css_is_removed(&mem->css));
1723 if (mem_cgroup_is_root(mem))
1724 goto done;
1725 if (consume_stock(mem))
1726 goto done;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001727 css_get(&mem->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001728 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001729 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001730
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001731 rcu_read_lock();
1732 p = rcu_dereference(mm->owner);
1733 VM_BUG_ON(!p);
1734 /*
1735 * because we don't have task_lock(), "p" can exit while
1736 * we're here. In that case, "mem" can point to root
1737 * cgroup but never be NULL. (and task_struct itself is freed
1738 * by RCU, cgroup itself is RCU safe.) Then, we have small
1739 * risk here to get wrong cgroup. But such kind of mis-account
1740 * by race always happens because we don't have cgroup_mutex().
1741 * It's overkill and we allow that small race, here.
1742 */
1743 mem = mem_cgroup_from_task(p);
1744 VM_BUG_ON(!mem);
1745 if (mem_cgroup_is_root(mem)) {
1746 rcu_read_unlock();
1747 goto done;
1748 }
1749 if (consume_stock(mem)) {
1750 /*
1751 * It seems dagerous to access memcg without css_get().
1752 * But considering how consume_stok works, it's not
1753 * necessary. If consume_stock success, some charges
1754 * from this memcg are cached on this cpu. So, we
1755 * don't need to call css_get()/css_tryget() before
1756 * calling consume_stock().
1757 */
1758 rcu_read_unlock();
1759 goto done;
1760 }
1761 /* after here, we may be blocked. we need to get refcnt */
1762 if (!css_tryget(&mem->css)) {
1763 rcu_read_unlock();
1764 goto again;
1765 }
1766 rcu_read_unlock();
1767 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001768
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001769 do {
1770 bool oom_check;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001771
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001772 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001773 if (fatal_signal_pending(current)) {
1774 css_put(&mem->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001775 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001776 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001777
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001778 oom_check = false;
1779 if (oom && !nr_oom_retries) {
1780 oom_check = true;
1781 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
1782 }
Balbir Singh6d61ef42009-01-07 18:08:06 -08001783
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001784 ret = __mem_cgroup_do_charge(mem, gfp_mask, csize, oom_check);
1785
1786 switch (ret) {
1787 case CHARGE_OK:
1788 break;
1789 case CHARGE_RETRY: /* not in OOM situation but retry */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001790 csize = PAGE_SIZE;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001791 css_put(&mem->css);
1792 mem = NULL;
1793 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001794 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001795 css_put(&mem->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001796 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001797 case CHARGE_NOMEM: /* OOM routine works */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001798 if (!oom) {
1799 css_put(&mem->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001800 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001801 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001802 /* If oom, we never return -ENOMEM */
1803 nr_oom_retries--;
1804 break;
1805 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001806 css_put(&mem->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001807 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08001808 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001809 } while (ret != CHARGE_OK);
1810
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001811 if (csize > PAGE_SIZE)
1812 refill_stock(mem, csize - PAGE_SIZE);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001813 css_put(&mem->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001814done:
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001815 *memcg = mem;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001816 return 0;
1817nomem:
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001818 *memcg = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001819 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001820bypass:
1821 *memcg = NULL;
1822 return 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001823}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001824
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001825/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001826 * Somemtimes we have to undo a charge we got by try_charge().
1827 * This function is for that and do uncharge, put css's refcnt.
1828 * gotten by try_charge().
1829 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001830static void __mem_cgroup_cancel_charge(struct mem_cgroup *mem,
1831 unsigned long count)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001832{
1833 if (!mem_cgroup_is_root(mem)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001834 res_counter_uncharge(&mem->res, PAGE_SIZE * count);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001835 if (do_swap_account)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001836 res_counter_uncharge(&mem->memsw, PAGE_SIZE * count);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001837 }
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001838}
1839
1840static void mem_cgroup_cancel_charge(struct mem_cgroup *mem)
1841{
1842 __mem_cgroup_cancel_charge(mem, 1);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001843}
1844
1845/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001846 * A helper function to get mem_cgroup from ID. must be called under
1847 * rcu_read_lock(). The caller must check css_is_removed() or some if
1848 * it's concern. (dropping refcnt from swap can be called against removed
1849 * memcg.)
1850 */
1851static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
1852{
1853 struct cgroup_subsys_state *css;
1854
1855 /* ID 0 is unused ID */
1856 if (!id)
1857 return NULL;
1858 css = css_lookup(&mem_cgroup_subsys, id);
1859 if (!css)
1860 return NULL;
1861 return container_of(css, struct mem_cgroup, css);
1862}
1863
Wu Fengguange42d9d52009-12-16 12:19:59 +01001864struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001865{
Wu Fengguange42d9d52009-12-16 12:19:59 +01001866 struct mem_cgroup *mem = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001867 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001868 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001869 swp_entry_t ent;
1870
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001871 VM_BUG_ON(!PageLocked(page));
1872
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001873 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001874 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001875 if (PageCgroupUsed(pc)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001876 mem = pc->mem_cgroup;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001877 if (mem && !css_tryget(&mem->css))
1878 mem = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01001879 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001880 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001881 id = lookup_swap_cgroup(ent);
1882 rcu_read_lock();
1883 mem = mem_cgroup_lookup(id);
1884 if (mem && !css_tryget(&mem->css))
1885 mem = NULL;
1886 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001887 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001888 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001889 return mem;
1890}
1891
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001892/*
Daisuke Nishimuraa5e924f2009-01-07 18:08:28 -08001893 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001894 * USED state. If already USED, uncharge and return.
1895 */
1896
1897static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1898 struct page_cgroup *pc,
1899 enum charge_type ctype)
1900{
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001901 /* try_charge() can return NULL to *memcg, taking care of it. */
1902 if (!mem)
1903 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001904
1905 lock_page_cgroup(pc);
1906 if (unlikely(PageCgroupUsed(pc))) {
1907 unlock_page_cgroup(pc);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001908 mem_cgroup_cancel_charge(mem);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001909 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001910 }
Balbir Singh4b3bde42009-09-23 15:56:32 -07001911
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001912 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07001913 /*
1914 * We access a page_cgroup asynchronously without lock_page_cgroup().
1915 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
1916 * is accessed after testing USED bit. To make pc->mem_cgroup visible
1917 * before USED bit, we need memory barrier here.
1918 * See mem_cgroup_add_lru_list(), etc.
1919 */
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001920 smp_wmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -07001921 switch (ctype) {
1922 case MEM_CGROUP_CHARGE_TYPE_CACHE:
1923 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
1924 SetPageCgroupCache(pc);
1925 SetPageCgroupUsed(pc);
1926 break;
1927 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1928 ClearPageCgroupCache(pc);
1929 SetPageCgroupUsed(pc);
1930 break;
1931 default:
1932 break;
1933 }
Hugh Dickins3be91272008-02-07 00:14:19 -08001934
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001935 mem_cgroup_charge_statistics(mem, pc, true);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001936
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001937 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08001938 /*
1939 * "charge_statistics" updated event counter. Then, check it.
1940 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
1941 * if they exceeds softlimit.
1942 */
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001943 memcg_check_events(mem, pc->page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001944}
1945
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001946/**
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001947 * __mem_cgroup_move_account - move account of the page
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001948 * @pc: page_cgroup of the page.
1949 * @from: mem_cgroup which the page is moved from.
1950 * @to: mem_cgroup which the page is moved to. @from != @to.
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001951 * @uncharge: whether we should call uncharge and css_put against @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001952 *
1953 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001954 * - page is not on LRU (isolate_page() is useful.)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001955 * - the pc is locked, used, and ->mem_cgroup points to @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001956 *
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001957 * This function doesn't do "charge" nor css_get to new cgroup. It should be
1958 * done by a caller(__mem_cgroup_try_charge would be usefull). If @uncharge is
1959 * true, this function does "uncharge" from old cgroup, but it doesn't if
1960 * @uncharge is false, so a caller should do "uncharge".
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001961 */
1962
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001963static void __mem_cgroup_move_account(struct page_cgroup *pc,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001964 struct mem_cgroup *from, struct mem_cgroup *to, bool uncharge)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001965{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001966 VM_BUG_ON(from == to);
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001967 VM_BUG_ON(PageLRU(pc->page));
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001968 VM_BUG_ON(!PageCgroupLocked(pc));
1969 VM_BUG_ON(!PageCgroupUsed(pc));
1970 VM_BUG_ON(pc->mem_cgroup != from);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001971
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07001972 if (PageCgroupFileMapped(pc)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08001973 /* Update mapped_file data for mem_cgroup */
1974 preempt_disable();
1975 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
1976 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
1977 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07001978 }
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001979 mem_cgroup_charge_statistics(from, pc, false);
1980 if (uncharge)
1981 /* This is not "cancel", but cancel_charge does all we need. */
1982 mem_cgroup_cancel_charge(from);
Balbir Singhd69b0422009-06-17 16:26:34 -07001983
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001984 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001985 pc->mem_cgroup = to;
1986 mem_cgroup_charge_statistics(to, pc, true);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001987 /*
1988 * We charges against "to" which may not have any tasks. Then, "to"
1989 * can be under rmdir(). But in current implementation, caller of
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08001990 * this function is just force_empty() and move charge, so it's
1991 * garanteed that "to" is never removed. So, we don't check rmdir
1992 * status here.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001993 */
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001994}
1995
1996/*
1997 * check whether the @pc is valid for moving account and call
1998 * __mem_cgroup_move_account()
1999 */
2000static int mem_cgroup_move_account(struct page_cgroup *pc,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002001 struct mem_cgroup *from, struct mem_cgroup *to, bool uncharge)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002002{
2003 int ret = -EINVAL;
2004 lock_page_cgroup(pc);
2005 if (PageCgroupUsed(pc) && pc->mem_cgroup == from) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002006 __mem_cgroup_move_account(pc, from, to, uncharge);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002007 ret = 0;
2008 }
2009 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002010 /*
2011 * check events
2012 */
2013 memcg_check_events(to, pc->page);
2014 memcg_check_events(from, pc->page);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002015 return ret;
2016}
2017
2018/*
2019 * move charges to its parent.
2020 */
2021
2022static int mem_cgroup_move_parent(struct page_cgroup *pc,
2023 struct mem_cgroup *child,
2024 gfp_t gfp_mask)
2025{
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002026 struct page *page = pc->page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002027 struct cgroup *cg = child->css.cgroup;
2028 struct cgroup *pcg = cg->parent;
2029 struct mem_cgroup *parent;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002030 int ret;
2031
2032 /* Is ROOT ? */
2033 if (!pcg)
2034 return -EINVAL;
2035
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002036 ret = -EBUSY;
2037 if (!get_page_unless_zero(page))
2038 goto out;
2039 if (isolate_lru_page(page))
2040 goto put;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002041
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002042 parent = mem_cgroup_from_cont(pcg);
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002043 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002044 if (ret || !parent)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002045 goto put_back;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002046
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002047 ret = mem_cgroup_move_account(pc, child, parent, true);
2048 if (ret)
2049 mem_cgroup_cancel_charge(parent);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002050put_back:
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002051 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002052put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08002053 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002054out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002055 return ret;
2056}
2057
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002058/*
2059 * Charge the memory controller for page usage.
2060 * Return
2061 * 0 if the charge was successful
2062 * < 0 if the cgroup is over its limit
2063 */
2064static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002065 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002066{
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002067 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002068 struct page_cgroup *pc;
2069 int ret;
2070
2071 pc = lookup_page_cgroup(page);
2072 /* can happen at boot */
2073 if (unlikely(!pc))
2074 return 0;
2075 prefetchw(pc);
2076
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002077 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002078 if (ret || !mem)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002079 return ret;
2080
2081 __mem_cgroup_commit_charge(mem, pc, ctype);
2082 return 0;
2083}
2084
2085int mem_cgroup_newpage_charge(struct page *page,
2086 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002087{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002088 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002089 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002090 if (PageCompound(page))
2091 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002092 /*
2093 * If already mapped, we don't have to account.
2094 * If page cache, page->mapping has address_space.
2095 * But page->mapping may have out-of-use anon_vma pointer,
2096 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
2097 * is NULL.
2098 */
2099 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
2100 return 0;
2101 if (unlikely(!mm))
2102 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002103 return mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002104 MEM_CGROUP_CHARGE_TYPE_MAPPED);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002105}
2106
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002107static void
2108__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2109 enum charge_type ctype);
2110
Balbir Singhe1a1cd52008-02-07 00:14:02 -08002111int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
2112 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08002113{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002114 int ret;
2115
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002116 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002117 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002118 if (PageCompound(page))
2119 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002120 /*
2121 * Corner case handling. This is called from add_to_page_cache()
2122 * in usual. But some FS (shmem) precharges this page before calling it
2123 * and call add_to_page_cache() with GFP_NOWAIT.
2124 *
2125 * For GFP_NOWAIT case, the page may be pre-charged before calling
2126 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
2127 * charge twice. (It works but has to pay a bit larger cost.)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002128 * And when the page is SwapCache, it should take swap information
2129 * into account. This is under lock_page() now.
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002130 */
2131 if (!(gfp_mask & __GFP_WAIT)) {
2132 struct page_cgroup *pc;
2133
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002134 pc = lookup_page_cgroup(page);
2135 if (!pc)
2136 return 0;
2137 lock_page_cgroup(pc);
2138 if (PageCgroupUsed(pc)) {
2139 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002140 return 0;
2141 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002142 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002143 }
2144
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002145 if (unlikely(!mm))
Balbir Singh8697d332008-02-07 00:13:59 -08002146 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002147
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -07002148 if (page_is_file_cache(page))
2149 return mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002150 MEM_CGROUP_CHARGE_TYPE_CACHE);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002151
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002152 /* shmem */
2153 if (PageSwapCache(page)) {
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002154 struct mem_cgroup *mem = NULL;
2155
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002156 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2157 if (!ret)
2158 __mem_cgroup_commit_charge_swapin(page, mem,
2159 MEM_CGROUP_CHARGE_TYPE_SHMEM);
2160 } else
2161 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002162 MEM_CGROUP_CHARGE_TYPE_SHMEM);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002163
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002164 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002165}
2166
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002167/*
2168 * While swap-in, try_charge -> commit or cancel, the page is locked.
2169 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02002170 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002171 * "commit()" or removed by "cancel()"
2172 */
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002173int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
2174 struct page *page,
2175 gfp_t mask, struct mem_cgroup **ptr)
2176{
2177 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002178 int ret;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002179
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002180 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002181 return 0;
2182
2183 if (!do_swap_account)
2184 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002185 /*
2186 * A racing thread's fault, or swapoff, may have already updated
Hugh Dickins407f9c82009-12-14 17:59:30 -08002187 * the pte, and even removed page from swap cache: in those cases
2188 * do_swap_page()'s pte_same() test will fail; but there's also a
2189 * KSM case which does need to charge the page.
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002190 */
2191 if (!PageSwapCache(page))
Hugh Dickins407f9c82009-12-14 17:59:30 -08002192 goto charge_cur_mm;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002193 mem = try_get_mem_cgroup_from_page(page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002194 if (!mem)
2195 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002196 *ptr = mem;
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002197 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002198 css_put(&mem->css);
2199 return ret;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002200charge_cur_mm:
2201 if (unlikely(!mm))
2202 mm = &init_mm;
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002203 return __mem_cgroup_try_charge(mm, mask, ptr, true);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002204}
2205
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002206static void
2207__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2208 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002209{
2210 struct page_cgroup *pc;
2211
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002212 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002213 return;
2214 if (!ptr)
2215 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002216 cgroup_exclude_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002217 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08002218 mem_cgroup_lru_del_before_commit_swapcache(page);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002219 __mem_cgroup_commit_charge(ptr, pc, ctype);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08002220 mem_cgroup_lru_add_after_commit_swapcache(page);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002221 /*
2222 * Now swap is on-memory. This means this page may be
2223 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002224 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
2225 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
2226 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002227 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002228 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002229 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002230 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002231 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002232
2233 id = swap_cgroup_record(ent, 0);
2234 rcu_read_lock();
2235 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002236 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002237 /*
2238 * This recorded memcg can be obsolete one. So, avoid
2239 * calling css_tryget
2240 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002241 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002242 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002243 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002244 mem_cgroup_put(memcg);
2245 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002246 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002247 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002248 /*
2249 * At swapin, we may charge account against cgroup which has no tasks.
2250 * So, rmdir()->pre_destroy() can be called while we do this charge.
2251 * In that case, we need to call pre_destroy() again. check it here.
2252 */
2253 cgroup_release_and_wakeup_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002254}
2255
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002256void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
2257{
2258 __mem_cgroup_commit_charge_swapin(page, ptr,
2259 MEM_CGROUP_CHARGE_TYPE_MAPPED);
2260}
2261
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002262void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
2263{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002264 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002265 return;
2266 if (!mem)
2267 return;
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002268 mem_cgroup_cancel_charge(mem);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002269}
2270
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002271static void
2272__do_uncharge(struct mem_cgroup *mem, const enum charge_type ctype)
2273{
2274 struct memcg_batch_info *batch = NULL;
2275 bool uncharge_memsw = true;
2276 /* If swapout, usage of swap doesn't decrease */
2277 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2278 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002279
2280 batch = &current->memcg_batch;
2281 /*
2282 * In usual, we do css_get() when we remember memcg pointer.
2283 * But in this case, we keep res->usage until end of a series of
2284 * uncharges. Then, it's ok to ignore memcg's refcnt.
2285 */
2286 if (!batch->memcg)
2287 batch->memcg = mem;
2288 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002289 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
2290 * In those cases, all pages freed continously can be expected to be in
2291 * the same cgroup and we have chance to coalesce uncharges.
2292 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
2293 * because we want to do uncharge as soon as possible.
2294 */
2295
2296 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
2297 goto direct_uncharge;
2298
2299 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002300 * In typical case, batch->memcg == mem. This means we can
2301 * merge a series of uncharges to an uncharge of res_counter.
2302 * If not, we uncharge res_counter ony by one.
2303 */
2304 if (batch->memcg != mem)
2305 goto direct_uncharge;
2306 /* remember freed charge and uncharge it later */
2307 batch->bytes += PAGE_SIZE;
2308 if (uncharge_memsw)
2309 batch->memsw_bytes += PAGE_SIZE;
2310 return;
2311direct_uncharge:
2312 res_counter_uncharge(&mem->res, PAGE_SIZE);
2313 if (uncharge_memsw)
2314 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002315 if (unlikely(batch->memcg != mem))
2316 memcg_oom_recover(mem);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002317 return;
2318}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002319
Balbir Singh8697d332008-02-07 00:13:59 -08002320/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002321 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002322 */
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002323static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002324__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002325{
Hugh Dickins82895462008-03-04 14:29:08 -08002326 struct page_cgroup *pc;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002327 struct mem_cgroup *mem = NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002328
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002329 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002330 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07002331
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002332 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002333 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002334
Balbir Singh8697d332008-02-07 00:13:59 -08002335 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08002336 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08002337 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002338 pc = lookup_page_cgroup(page);
2339 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002340 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002341
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002342 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002343
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002344 mem = pc->mem_cgroup;
2345
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002346 if (!PageCgroupUsed(pc))
2347 goto unlock_out;
2348
2349 switch (ctype) {
2350 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002351 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002352 /* See mem_cgroup_prepare_migration() */
2353 if (page_mapped(page) || PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002354 goto unlock_out;
2355 break;
2356 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
2357 if (!PageAnon(page)) { /* Shared memory */
2358 if (page->mapping && !page_is_file_cache(page))
2359 goto unlock_out;
2360 } else if (page_mapped(page)) /* Anon */
2361 goto unlock_out;
2362 break;
2363 default:
2364 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002365 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002366
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002367 mem_cgroup_charge_statistics(mem, pc, false);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002368
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002369 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08002370 /*
2371 * pc->mem_cgroup is not cleared here. It will be accessed when it's
2372 * freed from LRU. This is safe because uncharged page is expected not
2373 * to be reused (freed soon). Exception is SwapCache, it's handled by
2374 * special functions.
2375 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08002376
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002377 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002378 /*
2379 * even after unlock, we have mem->res.usage here and this memcg
2380 * will never be freed.
2381 */
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002382 memcg_check_events(mem, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002383 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
2384 mem_cgroup_swap_statistics(mem, true);
2385 mem_cgroup_get(mem);
2386 }
2387 if (!mem_cgroup_is_root(mem))
2388 __do_uncharge(mem, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002389
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002390 return mem;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002391
2392unlock_out:
2393 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002394 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08002395}
2396
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002397void mem_cgroup_uncharge_page(struct page *page)
2398{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002399 /* early check. */
2400 if (page_mapped(page))
2401 return;
2402 if (page->mapping && !PageAnon(page))
2403 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002404 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
2405}
2406
2407void mem_cgroup_uncharge_cache_page(struct page *page)
2408{
2409 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07002410 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002411 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
2412}
2413
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002414/*
2415 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
2416 * In that cases, pages are freed continuously and we can expect pages
2417 * are in the same memcg. All these calls itself limits the number of
2418 * pages freed at once, then uncharge_start/end() is called properly.
2419 * This may be called prural(2) times in a context,
2420 */
2421
2422void mem_cgroup_uncharge_start(void)
2423{
2424 current->memcg_batch.do_batch++;
2425 /* We can do nest. */
2426 if (current->memcg_batch.do_batch == 1) {
2427 current->memcg_batch.memcg = NULL;
2428 current->memcg_batch.bytes = 0;
2429 current->memcg_batch.memsw_bytes = 0;
2430 }
2431}
2432
2433void mem_cgroup_uncharge_end(void)
2434{
2435 struct memcg_batch_info *batch = &current->memcg_batch;
2436
2437 if (!batch->do_batch)
2438 return;
2439
2440 batch->do_batch--;
2441 if (batch->do_batch) /* If stacked, do nothing. */
2442 return;
2443
2444 if (!batch->memcg)
2445 return;
2446 /*
2447 * This "batch->memcg" is valid without any css_get/put etc...
2448 * bacause we hide charges behind us.
2449 */
2450 if (batch->bytes)
2451 res_counter_uncharge(&batch->memcg->res, batch->bytes);
2452 if (batch->memsw_bytes)
2453 res_counter_uncharge(&batch->memcg->memsw, batch->memsw_bytes);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002454 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002455 /* forget this pointer (for sanity check) */
2456 batch->memcg = NULL;
2457}
2458
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002459#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002460/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002461 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002462 * memcg information is recorded to swap_cgroup of "ent"
2463 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002464void
2465mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002466{
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002467 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002468 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002469
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002470 if (!swapout) /* this was a swap cache but the swap is unused ! */
2471 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
2472
2473 memcg = __mem_cgroup_uncharge_common(page, ctype);
2474
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002475 /*
2476 * record memcg information, if swapout && memcg != NULL,
2477 * mem_cgroup_get() was called in uncharge().
2478 */
2479 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002480 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002481}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002482#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002483
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002484#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2485/*
2486 * called from swap_entry_free(). remove record in swap_cgroup and
2487 * uncharge "memsw" account.
2488 */
2489void mem_cgroup_uncharge_swap(swp_entry_t ent)
2490{
2491 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002492 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002493
2494 if (!do_swap_account)
2495 return;
2496
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002497 id = swap_cgroup_record(ent, 0);
2498 rcu_read_lock();
2499 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002500 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002501 /*
2502 * We uncharge this because swap is freed.
2503 * This memcg can be obsolete one. We avoid calling css_tryget
2504 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002505 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002506 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002507 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002508 mem_cgroup_put(memcg);
2509 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002510 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002511}
Daisuke Nishimura02491442010-03-10 15:22:17 -08002512
2513/**
2514 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
2515 * @entry: swap entry to be moved
2516 * @from: mem_cgroup which the entry is moved from
2517 * @to: mem_cgroup which the entry is moved to
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002518 * @need_fixup: whether we should fixup res_counters and refcounts.
Daisuke Nishimura02491442010-03-10 15:22:17 -08002519 *
2520 * It succeeds only when the swap_cgroup's record for this entry is the same
2521 * as the mem_cgroup's id of @from.
2522 *
2523 * Returns 0 on success, -EINVAL on failure.
2524 *
2525 * The caller must have charged to @to, IOW, called res_counter_charge() about
2526 * both res and memsw, and called css_get().
2527 */
2528static int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002529 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08002530{
2531 unsigned short old_id, new_id;
2532
2533 old_id = css_id(&from->css);
2534 new_id = css_id(&to->css);
2535
2536 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08002537 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08002538 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002539 /*
2540 * This function is only called from task migration context now.
2541 * It postpones res_counter and refcount handling till the end
2542 * of task migration(mem_cgroup_clear_mc()) for performance
2543 * improvement. But we cannot postpone mem_cgroup_get(to)
2544 * because if the process that has been moved to @to does
2545 * swap-in, the refcount of @to might be decreased to 0.
2546 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08002547 mem_cgroup_get(to);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002548 if (need_fixup) {
2549 if (!mem_cgroup_is_root(from))
2550 res_counter_uncharge(&from->memsw, PAGE_SIZE);
2551 mem_cgroup_put(from);
2552 /*
2553 * we charged both to->res and to->memsw, so we should
2554 * uncharge to->res.
2555 */
2556 if (!mem_cgroup_is_root(to))
2557 res_counter_uncharge(&to->res, PAGE_SIZE);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002558 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08002559 return 0;
2560 }
2561 return -EINVAL;
2562}
2563#else
2564static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002565 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08002566{
2567 return -EINVAL;
2568}
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002569#endif
2570
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002571/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002572 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
2573 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002574 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002575int mem_cgroup_prepare_migration(struct page *page,
2576 struct page *newpage, struct mem_cgroup **ptr)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002577{
2578 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002579 struct mem_cgroup *mem = NULL;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002580 enum charge_type ctype;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002581 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08002582
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002583 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07002584 return 0;
2585
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002586 pc = lookup_page_cgroup(page);
2587 lock_page_cgroup(pc);
2588 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002589 mem = pc->mem_cgroup;
2590 css_get(&mem->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002591 /*
2592 * At migrating an anonymous page, its mapcount goes down
2593 * to 0 and uncharge() will be called. But, even if it's fully
2594 * unmapped, migration may fail and this page has to be
2595 * charged again. We set MIGRATION flag here and delay uncharge
2596 * until end_migration() is called
2597 *
2598 * Corner Case Thinking
2599 * A)
2600 * When the old page was mapped as Anon and it's unmap-and-freed
2601 * while migration was ongoing.
2602 * If unmap finds the old page, uncharge() of it will be delayed
2603 * until end_migration(). If unmap finds a new page, it's
2604 * uncharged when it make mapcount to be 1->0. If unmap code
2605 * finds swap_migration_entry, the new page will not be mapped
2606 * and end_migration() will find it(mapcount==0).
2607 *
2608 * B)
2609 * When the old page was mapped but migraion fails, the kernel
2610 * remaps it. A charge for it is kept by MIGRATION flag even
2611 * if mapcount goes down to 0. We can do remap successfully
2612 * without charging it again.
2613 *
2614 * C)
2615 * The "old" page is under lock_page() until the end of
2616 * migration, so, the old page itself will not be swapped-out.
2617 * If the new page is swapped out before end_migraton, our
2618 * hook to usual swap-out path will catch the event.
2619 */
2620 if (PageAnon(page))
2621 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08002622 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002623 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002624 /*
2625 * If the page is not charged at this point,
2626 * we return here.
2627 */
2628 if (!mem)
2629 return 0;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002630
Andrea Arcangeli93d5c9b2010-04-23 13:17:39 -04002631 *ptr = mem;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002632 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, ptr, false);
2633 css_put(&mem->css);/* drop extra refcnt */
2634 if (ret || *ptr == NULL) {
2635 if (PageAnon(page)) {
2636 lock_page_cgroup(pc);
2637 ClearPageCgroupMigration(pc);
2638 unlock_page_cgroup(pc);
2639 /*
2640 * The old page may be fully unmapped while we kept it.
2641 */
2642 mem_cgroup_uncharge_page(page);
2643 }
2644 return -ENOMEM;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002645 }
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002646 /*
2647 * We charge new page before it's used/mapped. So, even if unlock_page()
2648 * is called before end_migration, we can catch all events on this new
2649 * page. In the case new page is migrated but not remapped, new page's
2650 * mapcount will be finally 0 and we call uncharge in end_migration().
2651 */
2652 pc = lookup_page_cgroup(newpage);
2653 if (PageAnon(page))
2654 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
2655 else if (page_is_file_cache(page))
2656 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
2657 else
2658 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
2659 __mem_cgroup_commit_charge(mem, pc, ctype);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002660 return ret;
2661}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08002662
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002663/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002664void mem_cgroup_end_migration(struct mem_cgroup *mem,
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002665 struct page *oldpage, struct page *newpage)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002666{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002667 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002668 struct page_cgroup *pc;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002669
2670 if (!mem)
2671 return;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002672 /* blocks rmdir() */
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002673 cgroup_exclude_rmdir(&mem->css);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002674 /* at migration success, oldpage->mapping is NULL. */
2675 if (oldpage->mapping) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002676 used = oldpage;
2677 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002678 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002679 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002680 unused = oldpage;
2681 }
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002682 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002683 * We disallowed uncharge of pages under migration because mapcount
2684 * of the page goes down to zero, temporarly.
2685 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002686 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002687 pc = lookup_page_cgroup(oldpage);
2688 lock_page_cgroup(pc);
2689 ClearPageCgroupMigration(pc);
2690 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002691
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002692 __mem_cgroup_uncharge_common(unused, MEM_CGROUP_CHARGE_TYPE_FORCE);
2693
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002694 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002695 * If a page is a file cache, radix-tree replacement is very atomic
2696 * and we can skip this check. When it was an Anon page, its mapcount
2697 * goes down to 0. But because we added MIGRATION flage, it's not
2698 * uncharged yet. There are several case but page->mapcount check
2699 * and USED bit check in mem_cgroup_uncharge_page() will do enough
2700 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002701 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002702 if (PageAnon(used))
2703 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002704 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002705 * At migration, we may charge account against cgroup which has no
2706 * tasks.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002707 * So, rmdir()->pre_destroy() can be called while we do this charge.
2708 * In that case, we need to call pre_destroy() again. check it here.
2709 */
2710 cgroup_release_and_wakeup_rmdir(&mem->css);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002711}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08002712
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002713/*
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002714 * A call to try to shrink memory usage on charge failure at shmem's swapin.
2715 * Calling hierarchical_reclaim is not enough because we should update
2716 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
2717 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
2718 * not from the memcg which this page would be charged to.
2719 * try_charge_swapin does all of these works properly.
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002720 */
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002721int mem_cgroup_shmem_charge_fallback(struct page *page,
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002722 struct mm_struct *mm,
2723 gfp_t gfp_mask)
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002724{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002725 struct mem_cgroup *mem = NULL;
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002726 int ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002727
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002728 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002729 return 0;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002730
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002731 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2732 if (!ret)
2733 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002734
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002735 return ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002736}
2737
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002738static DEFINE_MUTEX(set_limit_mutex);
2739
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08002740static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002741 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002742{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002743 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002744 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002745 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002746 int children = mem_cgroup_count_children(memcg);
2747 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002748 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002749
2750 /*
2751 * For keeping hierarchical_reclaim simple, how long we should retry
2752 * is depends on callers. We set our retry-count to be function
2753 * of # of children which we should visit in this loop.
2754 */
2755 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
2756
2757 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002758
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002759 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002760 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002761 if (signal_pending(current)) {
2762 ret = -EINTR;
2763 break;
2764 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002765 /*
2766 * Rather than hide all in some function, I do this in
2767 * open coded manner. You see what this really does.
2768 * We have to guarantee mem->res.limit < mem->memsw.limit.
2769 */
2770 mutex_lock(&set_limit_mutex);
2771 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2772 if (memswlimit < val) {
2773 ret = -EINVAL;
2774 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002775 break;
2776 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002777
2778 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2779 if (memlimit < val)
2780 enlarge = 1;
2781
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002782 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07002783 if (!ret) {
2784 if (memswlimit == val)
2785 memcg->memsw_is_minimum = true;
2786 else
2787 memcg->memsw_is_minimum = false;
2788 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002789 mutex_unlock(&set_limit_mutex);
2790
2791 if (!ret)
2792 break;
2793
Bob Liuaa20d482009-12-15 16:47:14 -08002794 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh4e416952009-09-23 15:56:39 -07002795 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002796 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
2797 /* Usage is reduced ? */
2798 if (curusage >= oldusage)
2799 retry_count--;
2800 else
2801 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002802 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002803 if (!ret && enlarge)
2804 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002805
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002806 return ret;
2807}
2808
Li Zefan338c8432009-06-17 16:27:15 -07002809static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
2810 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002811{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002812 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002813 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002814 int children = mem_cgroup_count_children(memcg);
2815 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002816 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002817
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002818 /* see mem_cgroup_resize_res_limit */
2819 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
2820 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002821 while (retry_count) {
2822 if (signal_pending(current)) {
2823 ret = -EINTR;
2824 break;
2825 }
2826 /*
2827 * Rather than hide all in some function, I do this in
2828 * open coded manner. You see what this really does.
2829 * We have to guarantee mem->res.limit < mem->memsw.limit.
2830 */
2831 mutex_lock(&set_limit_mutex);
2832 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2833 if (memlimit > val) {
2834 ret = -EINVAL;
2835 mutex_unlock(&set_limit_mutex);
2836 break;
2837 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002838 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2839 if (memswlimit < val)
2840 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002841 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07002842 if (!ret) {
2843 if (memlimit == val)
2844 memcg->memsw_is_minimum = true;
2845 else
2846 memcg->memsw_is_minimum = false;
2847 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002848 mutex_unlock(&set_limit_mutex);
2849
2850 if (!ret)
2851 break;
2852
Balbir Singh4e416952009-09-23 15:56:39 -07002853 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh75822b42009-09-23 15:56:38 -07002854 MEM_CGROUP_RECLAIM_NOSWAP |
2855 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002856 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002857 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002858 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002859 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002860 else
2861 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002862 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002863 if (!ret && enlarge)
2864 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002865 return ret;
2866}
2867
Balbir Singh4e416952009-09-23 15:56:39 -07002868unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
2869 gfp_t gfp_mask, int nid,
2870 int zid)
2871{
2872 unsigned long nr_reclaimed = 0;
2873 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
2874 unsigned long reclaimed;
2875 int loop = 0;
2876 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002877 unsigned long long excess;
Balbir Singh4e416952009-09-23 15:56:39 -07002878
2879 if (order > 0)
2880 return 0;
2881
2882 mctz = soft_limit_tree_node_zone(nid, zid);
2883 /*
2884 * This loop can run a while, specially if mem_cgroup's continuously
2885 * keep exceeding their soft limit and putting the system under
2886 * pressure
2887 */
2888 do {
2889 if (next_mz)
2890 mz = next_mz;
2891 else
2892 mz = mem_cgroup_largest_soft_limit_node(mctz);
2893 if (!mz)
2894 break;
2895
2896 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
2897 gfp_mask,
2898 MEM_CGROUP_RECLAIM_SOFT);
2899 nr_reclaimed += reclaimed;
2900 spin_lock(&mctz->lock);
2901
2902 /*
2903 * If we failed to reclaim anything from this memory cgroup
2904 * it is time to move on to the next cgroup
2905 */
2906 next_mz = NULL;
2907 if (!reclaimed) {
2908 do {
2909 /*
2910 * Loop until we find yet another one.
2911 *
2912 * By the time we get the soft_limit lock
2913 * again, someone might have aded the
2914 * group back on the RB tree. Iterate to
2915 * make sure we get a different mem.
2916 * mem_cgroup_largest_soft_limit_node returns
2917 * NULL if no other cgroup is present on
2918 * the tree
2919 */
2920 next_mz =
2921 __mem_cgroup_largest_soft_limit_node(mctz);
2922 if (next_mz == mz) {
2923 css_put(&next_mz->mem->css);
2924 next_mz = NULL;
2925 } else /* next_mz == NULL or other memcg */
2926 break;
2927 } while (1);
2928 }
Balbir Singh4e416952009-09-23 15:56:39 -07002929 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002930 excess = res_counter_soft_limit_excess(&mz->mem->res);
Balbir Singh4e416952009-09-23 15:56:39 -07002931 /*
2932 * One school of thought says that we should not add
2933 * back the node to the tree if reclaim returns 0.
2934 * But our reclaim could return 0, simply because due
2935 * to priority we are exposing a smaller subset of
2936 * memory to reclaim from. Consider this as a longer
2937 * term TODO.
2938 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002939 /* If excess == 0, no tree ops */
2940 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07002941 spin_unlock(&mctz->lock);
2942 css_put(&mz->mem->css);
2943 loop++;
2944 /*
2945 * Could not reclaim anything and there are no more
2946 * mem cgroups to try or we seem to be looping without
2947 * reclaiming anything.
2948 */
2949 if (!nr_reclaimed &&
2950 (next_mz == NULL ||
2951 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
2952 break;
2953 } while (!nr_reclaimed);
2954 if (next_mz)
2955 css_put(&next_mz->mem->css);
2956 return nr_reclaimed;
2957}
2958
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002959/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002960 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002961 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
2962 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002963static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002964 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002965{
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002966 struct zone *zone;
2967 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002968 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002969 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002970 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002971 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002972
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002973 zone = &NODE_DATA(node)->node_zones[zid];
2974 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07002975 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002976
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002977 loop = MEM_CGROUP_ZSTAT(mz, lru);
2978 /* give some margin against EBUSY etc...*/
2979 loop += 256;
2980 busy = NULL;
2981 while (loop--) {
2982 ret = 0;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002983 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002984 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002985 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002986 break;
2987 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002988 pc = list_entry(list->prev, struct page_cgroup, lru);
2989 if (busy == pc) {
2990 list_move(&pc->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08002991 busy = NULL;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002992 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002993 continue;
2994 }
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002995 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002996
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -08002997 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002998 if (ret == -ENOMEM)
2999 break;
3000
3001 if (ret == -EBUSY || ret == -EINVAL) {
3002 /* found lock contention or "pc" is obsolete. */
3003 busy = pc;
3004 cond_resched();
3005 } else
3006 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003007 }
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003008
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003009 if (!ret && !list_empty(list))
3010 return -EBUSY;
3011 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003012}
3013
3014/*
3015 * make mem_cgroup's charge to be 0 if there is no task.
3016 * This enables deleting this mem_cgroup.
3017 */
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003018static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003019{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003020 int ret;
3021 int node, zid, shrink;
3022 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003023 struct cgroup *cgrp = mem->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003024
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003025 css_get(&mem->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003026
3027 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003028 /* should free all ? */
3029 if (free_all)
3030 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003031move_account:
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003032 do {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003033 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003034 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003035 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003036 ret = -EINTR;
3037 if (signal_pending(current))
3038 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003039 /* This is for making all *used* pages to be on LRU. */
3040 lru_add_drain_all();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08003041 drain_all_stock_sync();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003042 ret = 0;
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08003043 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003044 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07003045 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003046 for_each_lru(l) {
3047 ret = mem_cgroup_force_empty_list(mem,
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003048 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003049 if (ret)
3050 break;
3051 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003052 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003053 if (ret)
3054 break;
3055 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003056 memcg_oom_recover(mem);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003057 /* it seems parent cgroup doesn't have enough mem */
3058 if (ret == -ENOMEM)
3059 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003060 cond_resched();
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003061 /* "ret" should also be checked to ensure all lists are empty. */
3062 } while (mem->res.usage > 0 || ret);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003063out:
3064 css_put(&mem->css);
3065 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003066
3067try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003068 /* returns EBUSY if there is a task or if we come here twice. */
3069 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003070 ret = -EBUSY;
3071 goto out;
3072 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003073 /* we call try-to-free pages for make this cgroup empty */
3074 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003075 /* try to free all pages in this cgroup */
3076 shrink = 1;
3077 while (nr_retries && mem->res.usage > 0) {
3078 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003079
3080 if (signal_pending(current)) {
3081 ret = -EINTR;
3082 goto out;
3083 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003084 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
3085 false, get_swappiness(mem));
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003086 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003087 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003088 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02003089 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003090 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003091
3092 }
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003093 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003094 /* try move_account...there may be some *locked* pages. */
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003095 goto move_account;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003096}
3097
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003098int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
3099{
3100 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
3101}
3102
3103
Balbir Singh18f59ea2009-01-07 18:08:07 -08003104static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
3105{
3106 return mem_cgroup_from_cont(cont)->use_hierarchy;
3107}
3108
3109static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
3110 u64 val)
3111{
3112 int retval = 0;
3113 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
3114 struct cgroup *parent = cont->parent;
3115 struct mem_cgroup *parent_mem = NULL;
3116
3117 if (parent)
3118 parent_mem = mem_cgroup_from_cont(parent);
3119
3120 cgroup_lock();
3121 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003122 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08003123 * in the child subtrees. If it is unset, then the change can
3124 * occur, provided the current cgroup has no children.
3125 *
3126 * For the root cgroup, parent_mem is NULL, we allow value to be
3127 * set if there are no children.
3128 */
3129 if ((!parent_mem || !parent_mem->use_hierarchy) &&
3130 (val == 1 || val == 0)) {
3131 if (list_empty(&cont->children))
3132 mem->use_hierarchy = val;
3133 else
3134 retval = -EBUSY;
3135 } else
3136 retval = -EINVAL;
3137 cgroup_unlock();
3138
3139 return retval;
3140}
3141
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003142struct mem_cgroup_idx_data {
3143 s64 val;
3144 enum mem_cgroup_stat_index idx;
3145};
3146
3147static int
3148mem_cgroup_get_idx_stat(struct mem_cgroup *mem, void *data)
3149{
3150 struct mem_cgroup_idx_data *d = data;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003151 d->val += mem_cgroup_read_stat(mem, d->idx);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003152 return 0;
3153}
3154
3155static void
3156mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem,
3157 enum mem_cgroup_stat_index idx, s64 *val)
3158{
3159 struct mem_cgroup_idx_data d;
3160 d.idx = idx;
3161 d.val = 0;
3162 mem_cgroup_walk_tree(mem, &d, mem_cgroup_get_idx_stat);
3163 *val = d.val;
3164}
3165
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003166static inline u64 mem_cgroup_usage(struct mem_cgroup *mem, bool swap)
3167{
3168 u64 idx_val, val;
3169
3170 if (!mem_cgroup_is_root(mem)) {
3171 if (!swap)
3172 return res_counter_read_u64(&mem->res, RES_USAGE);
3173 else
3174 return res_counter_read_u64(&mem->memsw, RES_USAGE);
3175 }
3176
3177 mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_CACHE, &idx_val);
3178 val = idx_val;
3179 mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_RSS, &idx_val);
3180 val += idx_val;
3181
3182 if (swap) {
3183 mem_cgroup_get_recursive_idx_stat(mem,
3184 MEM_CGROUP_STAT_SWAPOUT, &idx_val);
3185 val += idx_val;
3186 }
3187
3188 return val << PAGE_SHIFT;
3189}
3190
Paul Menage2c3daa72008-04-29 00:59:58 -07003191static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003192{
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003193 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003194 u64 val;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003195 int type, name;
3196
3197 type = MEMFILE_TYPE(cft->private);
3198 name = MEMFILE_ATTR(cft->private);
3199 switch (type) {
3200 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003201 if (name == RES_USAGE)
3202 val = mem_cgroup_usage(mem, false);
3203 else
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003204 val = res_counter_read_u64(&mem->res, name);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003205 break;
3206 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003207 if (name == RES_USAGE)
3208 val = mem_cgroup_usage(mem, true);
3209 else
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003210 val = res_counter_read_u64(&mem->memsw, name);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003211 break;
3212 default:
3213 BUG();
3214 break;
3215 }
3216 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003217}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003218/*
3219 * The user of this function is...
3220 * RES_LIMIT.
3221 */
Paul Menage856c13a2008-07-25 01:47:04 -07003222static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
3223 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003224{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003225 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003226 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003227 unsigned long long val;
3228 int ret;
3229
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003230 type = MEMFILE_TYPE(cft->private);
3231 name = MEMFILE_ATTR(cft->private);
3232 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003233 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07003234 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
3235 ret = -EINVAL;
3236 break;
3237 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003238 /* This function does all necessary parse...reuse it */
3239 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003240 if (ret)
3241 break;
3242 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003243 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003244 else
3245 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003246 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07003247 case RES_SOFT_LIMIT:
3248 ret = res_counter_memparse_write_strategy(buffer, &val);
3249 if (ret)
3250 break;
3251 /*
3252 * For memsw, soft limits are hard to implement in terms
3253 * of semantics, for now, we support soft limits for
3254 * control without swap
3255 */
3256 if (type == _MEM)
3257 ret = res_counter_set_soft_limit(&memcg->res, val);
3258 else
3259 ret = -EINVAL;
3260 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003261 default:
3262 ret = -EINVAL; /* should be BUG() ? */
3263 break;
3264 }
3265 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003266}
3267
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08003268static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
3269 unsigned long long *mem_limit, unsigned long long *memsw_limit)
3270{
3271 struct cgroup *cgroup;
3272 unsigned long long min_limit, min_memsw_limit, tmp;
3273
3274 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3275 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3276 cgroup = memcg->css.cgroup;
3277 if (!memcg->use_hierarchy)
3278 goto out;
3279
3280 while (cgroup->parent) {
3281 cgroup = cgroup->parent;
3282 memcg = mem_cgroup_from_cont(cgroup);
3283 if (!memcg->use_hierarchy)
3284 break;
3285 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
3286 min_limit = min(min_limit, tmp);
3287 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3288 min_memsw_limit = min(min_memsw_limit, tmp);
3289 }
3290out:
3291 *mem_limit = min_limit;
3292 *memsw_limit = min_memsw_limit;
3293 return;
3294}
3295
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003296static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003297{
3298 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003299 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003300
3301 mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003302 type = MEMFILE_TYPE(event);
3303 name = MEMFILE_ATTR(event);
3304 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003305 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003306 if (type == _MEM)
3307 res_counter_reset_max(&mem->res);
3308 else
3309 res_counter_reset_max(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003310 break;
3311 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003312 if (type == _MEM)
3313 res_counter_reset_failcnt(&mem->res);
3314 else
3315 res_counter_reset_failcnt(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003316 break;
3317 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07003318
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07003319 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003320}
3321
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003322static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
3323 struct cftype *cft)
3324{
3325 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
3326}
3327
Daisuke Nishimura02491442010-03-10 15:22:17 -08003328#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003329static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
3330 struct cftype *cft, u64 val)
3331{
3332 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3333
3334 if (val >= (1 << NR_MOVE_TYPE))
3335 return -EINVAL;
3336 /*
3337 * We check this value several times in both in can_attach() and
3338 * attach(), so we need cgroup lock to prevent this value from being
3339 * inconsistent.
3340 */
3341 cgroup_lock();
3342 mem->move_charge_at_immigrate = val;
3343 cgroup_unlock();
3344
3345 return 0;
3346}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003347#else
3348static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
3349 struct cftype *cft, u64 val)
3350{
3351 return -ENOSYS;
3352}
3353#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003354
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003355
3356/* For read statistics */
3357enum {
3358 MCS_CACHE,
3359 MCS_RSS,
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08003360 MCS_FILE_MAPPED,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003361 MCS_PGPGIN,
3362 MCS_PGPGOUT,
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003363 MCS_SWAP,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003364 MCS_INACTIVE_ANON,
3365 MCS_ACTIVE_ANON,
3366 MCS_INACTIVE_FILE,
3367 MCS_ACTIVE_FILE,
3368 MCS_UNEVICTABLE,
3369 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003370};
3371
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003372struct mcs_total_stat {
3373 s64 stat[NR_MCS_STAT];
3374};
3375
3376struct {
3377 char *local_name;
3378 char *total_name;
3379} memcg_stat_strings[NR_MCS_STAT] = {
3380 {"cache", "total_cache"},
3381 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07003382 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003383 {"pgpgin", "total_pgpgin"},
3384 {"pgpgout", "total_pgpgout"},
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003385 {"swap", "total_swap"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003386 {"inactive_anon", "total_inactive_anon"},
3387 {"active_anon", "total_active_anon"},
3388 {"inactive_file", "total_inactive_file"},
3389 {"active_file", "total_active_file"},
3390 {"unevictable", "total_unevictable"}
3391};
3392
3393
3394static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
3395{
3396 struct mcs_total_stat *s = data;
3397 s64 val;
3398
3399 /* per cpu stat */
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003400 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003401 s->stat[MCS_CACHE] += val * PAGE_SIZE;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003402 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003403 s->stat[MCS_RSS] += val * PAGE_SIZE;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003404 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_FILE_MAPPED);
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08003405 s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003406 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_PGPGIN_COUNT);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003407 s->stat[MCS_PGPGIN] += val;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003408 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_PGPGOUT_COUNT);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003409 s->stat[MCS_PGPGOUT] += val;
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003410 if (do_swap_account) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003411 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_SWAPOUT);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003412 s->stat[MCS_SWAP] += val * PAGE_SIZE;
3413 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003414
3415 /* per zone stat */
3416 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
3417 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
3418 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
3419 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
3420 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
3421 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
3422 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
3423 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
3424 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
3425 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
3426 return 0;
3427}
3428
3429static void
3430mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
3431{
3432 mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
3433}
3434
Paul Menagec64745c2008-04-29 01:00:02 -07003435static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
3436 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003437{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003438 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003439 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003440 int i;
3441
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003442 memset(&mystat, 0, sizeof(mystat));
3443 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003444
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003445 for (i = 0; i < NR_MCS_STAT; i++) {
3446 if (i == MCS_SWAP && !do_swap_account)
3447 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003448 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003449 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003450
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003451 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08003452 {
3453 unsigned long long limit, memsw_limit;
3454 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
3455 cb->fill(cb, "hierarchical_memory_limit", limit);
3456 if (do_swap_account)
3457 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
3458 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003459
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003460 memset(&mystat, 0, sizeof(mystat));
3461 mem_cgroup_get_total_stat(mem_cont, &mystat);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003462 for (i = 0; i < NR_MCS_STAT; i++) {
3463 if (i == MCS_SWAP && !do_swap_account)
3464 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003465 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003466 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003467
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003468#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08003469 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003470
3471 {
3472 int nid, zid;
3473 struct mem_cgroup_per_zone *mz;
3474 unsigned long recent_rotated[2] = {0, 0};
3475 unsigned long recent_scanned[2] = {0, 0};
3476
3477 for_each_online_node(nid)
3478 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
3479 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
3480
3481 recent_rotated[0] +=
3482 mz->reclaim_stat.recent_rotated[0];
3483 recent_rotated[1] +=
3484 mz->reclaim_stat.recent_rotated[1];
3485 recent_scanned[0] +=
3486 mz->reclaim_stat.recent_scanned[0];
3487 recent_scanned[1] +=
3488 mz->reclaim_stat.recent_scanned[1];
3489 }
3490 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
3491 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
3492 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
3493 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
3494 }
3495#endif
3496
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003497 return 0;
3498}
3499
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003500static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
3501{
3502 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3503
3504 return get_swappiness(memcg);
3505}
3506
3507static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
3508 u64 val)
3509{
3510 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3511 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08003512
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003513 if (val > 100)
3514 return -EINVAL;
3515
3516 if (cgrp->parent == NULL)
3517 return -EINVAL;
3518
3519 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08003520
3521 cgroup_lock();
3522
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003523 /* If under hierarchy, only empty-root can set this value */
3524 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08003525 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
3526 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003527 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08003528 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003529
3530 spin_lock(&memcg->reclaim_param_lock);
3531 memcg->swappiness = val;
3532 spin_unlock(&memcg->reclaim_param_lock);
3533
Li Zefan068b38c2009-01-15 13:51:26 -08003534 cgroup_unlock();
3535
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003536 return 0;
3537}
3538
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003539static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
3540{
3541 struct mem_cgroup_threshold_ary *t;
3542 u64 usage;
3543 int i;
3544
3545 rcu_read_lock();
3546 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003547 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003548 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003549 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003550
3551 if (!t)
3552 goto unlock;
3553
3554 usage = mem_cgroup_usage(memcg, swap);
3555
3556 /*
3557 * current_threshold points to threshold just below usage.
3558 * If it's not true, a threshold was crossed after last
3559 * call of __mem_cgroup_threshold().
3560 */
Phil Carmody5407a562010-05-26 14:42:42 -07003561 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003562
3563 /*
3564 * Iterate backward over array of thresholds starting from
3565 * current_threshold and check if a threshold is crossed.
3566 * If none of thresholds below usage is crossed, we read
3567 * only one element of the array here.
3568 */
3569 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
3570 eventfd_signal(t->entries[i].eventfd, 1);
3571
3572 /* i = current_threshold + 1 */
3573 i++;
3574
3575 /*
3576 * Iterate forward over array of thresholds starting from
3577 * current_threshold+1 and check if a threshold is crossed.
3578 * If none of thresholds above usage is crossed, we read
3579 * only one element of the array here.
3580 */
3581 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
3582 eventfd_signal(t->entries[i].eventfd, 1);
3583
3584 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07003585 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003586unlock:
3587 rcu_read_unlock();
3588}
3589
3590static void mem_cgroup_threshold(struct mem_cgroup *memcg)
3591{
3592 __mem_cgroup_threshold(memcg, false);
3593 if (do_swap_account)
3594 __mem_cgroup_threshold(memcg, true);
3595}
3596
3597static int compare_thresholds(const void *a, const void *b)
3598{
3599 const struct mem_cgroup_threshold *_a = a;
3600 const struct mem_cgroup_threshold *_b = b;
3601
3602 return _a->threshold - _b->threshold;
3603}
3604
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003605static int mem_cgroup_oom_notify_cb(struct mem_cgroup *mem, void *data)
3606{
3607 struct mem_cgroup_eventfd_list *ev;
3608
3609 list_for_each_entry(ev, &mem->oom_notify, list)
3610 eventfd_signal(ev->eventfd, 1);
3611 return 0;
3612}
3613
3614static void mem_cgroup_oom_notify(struct mem_cgroup *mem)
3615{
3616 mem_cgroup_walk_tree(mem, NULL, mem_cgroup_oom_notify_cb);
3617}
3618
3619static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
3620 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003621{
3622 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003623 struct mem_cgroup_thresholds *thresholds;
3624 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003625 int type = MEMFILE_TYPE(cft->private);
3626 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003627 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003628
3629 ret = res_counter_memparse_write_strategy(args, &threshold);
3630 if (ret)
3631 return ret;
3632
3633 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003634
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003635 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003636 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003637 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003638 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003639 else
3640 BUG();
3641
3642 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
3643
3644 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003645 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003646 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
3647
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003648 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003649
3650 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003651 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003652 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003653 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003654 ret = -ENOMEM;
3655 goto unlock;
3656 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003657 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003658
3659 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003660 if (thresholds->primary) {
3661 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003662 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003663 }
3664
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003665 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003666 new->entries[size - 1].eventfd = eventfd;
3667 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003668
3669 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003670 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003671 compare_thresholds, NULL);
3672
3673 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003674 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003675 for (i = 0; i < size; i++) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003676 if (new->entries[i].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003677 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003678 * new->current_threshold will not be used until
3679 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003680 * it here.
3681 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003682 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003683 }
3684 }
3685
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003686 /* Free old spare buffer and save old primary buffer as spare */
3687 kfree(thresholds->spare);
3688 thresholds->spare = thresholds->primary;
3689
3690 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003691
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003692 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003693 synchronize_rcu();
3694
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003695unlock:
3696 mutex_unlock(&memcg->thresholds_lock);
3697
3698 return ret;
3699}
3700
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003701static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003702 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003703{
3704 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003705 struct mem_cgroup_thresholds *thresholds;
3706 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003707 int type = MEMFILE_TYPE(cft->private);
3708 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003709 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003710
3711 mutex_lock(&memcg->thresholds_lock);
3712 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003713 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003714 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003715 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003716 else
3717 BUG();
3718
3719 /*
3720 * Something went wrong if we trying to unregister a threshold
3721 * if we don't have thresholds
3722 */
3723 BUG_ON(!thresholds);
3724
3725 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
3726
3727 /* Check if a threshold crossed before removing */
3728 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
3729
3730 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003731 size = 0;
3732 for (i = 0; i < thresholds->primary->size; i++) {
3733 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003734 size++;
3735 }
3736
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003737 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003738
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003739 /* Set thresholds array to NULL if we don't have thresholds */
3740 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003741 kfree(new);
3742 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003743 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003744 }
3745
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003746 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003747
3748 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003749 new->current_threshold = -1;
3750 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
3751 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003752 continue;
3753
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003754 new->entries[j] = thresholds->primary->entries[i];
3755 if (new->entries[j].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003756 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003757 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003758 * until rcu_assign_pointer(), so it's safe to increment
3759 * it here.
3760 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003761 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003762 }
3763 j++;
3764 }
3765
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003766swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003767 /* Swap primary and spare array */
3768 thresholds->spare = thresholds->primary;
3769 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003770
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003771 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003772 synchronize_rcu();
3773
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003774 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003775}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003776
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003777static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
3778 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
3779{
3780 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3781 struct mem_cgroup_eventfd_list *event;
3782 int type = MEMFILE_TYPE(cft->private);
3783
3784 BUG_ON(type != _OOM_TYPE);
3785 event = kmalloc(sizeof(*event), GFP_KERNEL);
3786 if (!event)
3787 return -ENOMEM;
3788
3789 mutex_lock(&memcg_oom_mutex);
3790
3791 event->eventfd = eventfd;
3792 list_add(&event->list, &memcg->oom_notify);
3793
3794 /* already in OOM ? */
3795 if (atomic_read(&memcg->oom_lock))
3796 eventfd_signal(eventfd, 1);
3797 mutex_unlock(&memcg_oom_mutex);
3798
3799 return 0;
3800}
3801
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003802static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003803 struct cftype *cft, struct eventfd_ctx *eventfd)
3804{
3805 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3806 struct mem_cgroup_eventfd_list *ev, *tmp;
3807 int type = MEMFILE_TYPE(cft->private);
3808
3809 BUG_ON(type != _OOM_TYPE);
3810
3811 mutex_lock(&memcg_oom_mutex);
3812
3813 list_for_each_entry_safe(ev, tmp, &mem->oom_notify, list) {
3814 if (ev->eventfd == eventfd) {
3815 list_del(&ev->list);
3816 kfree(ev);
3817 }
3818 }
3819
3820 mutex_unlock(&memcg_oom_mutex);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003821}
3822
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003823static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
3824 struct cftype *cft, struct cgroup_map_cb *cb)
3825{
3826 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3827
3828 cb->fill(cb, "oom_kill_disable", mem->oom_kill_disable);
3829
3830 if (atomic_read(&mem->oom_lock))
3831 cb->fill(cb, "under_oom", 1);
3832 else
3833 cb->fill(cb, "under_oom", 0);
3834 return 0;
3835}
3836
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003837static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
3838 struct cftype *cft, u64 val)
3839{
3840 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3841 struct mem_cgroup *parent;
3842
3843 /* cannot set to root cgroup and only 0 and 1 are allowed */
3844 if (!cgrp->parent || !((val == 0) || (val == 1)))
3845 return -EINVAL;
3846
3847 parent = mem_cgroup_from_cont(cgrp->parent);
3848
3849 cgroup_lock();
3850 /* oom-kill-disable is a flag for subhierarchy. */
3851 if ((parent->use_hierarchy) ||
3852 (mem->use_hierarchy && !list_empty(&cgrp->children))) {
3853 cgroup_unlock();
3854 return -EINVAL;
3855 }
3856 mem->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07003857 if (!val)
3858 memcg_oom_recover(mem);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003859 cgroup_unlock();
3860 return 0;
3861}
3862
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003863static struct cftype mem_cgroup_files[] = {
3864 {
Balbir Singh0eea1032008-02-07 00:13:57 -08003865 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003866 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07003867 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003868 .register_event = mem_cgroup_usage_register_event,
3869 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003870 },
3871 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003872 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003873 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003874 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003875 .read_u64 = mem_cgroup_read,
3876 },
3877 {
Balbir Singh0eea1032008-02-07 00:13:57 -08003878 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003879 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07003880 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07003881 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003882 },
3883 {
Balbir Singh296c81d2009-09-23 15:56:36 -07003884 .name = "soft_limit_in_bytes",
3885 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
3886 .write_string = mem_cgroup_write,
3887 .read_u64 = mem_cgroup_read,
3888 },
3889 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003890 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003891 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003892 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07003893 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003894 },
Balbir Singh8697d332008-02-07 00:13:59 -08003895 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003896 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07003897 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003898 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003899 {
3900 .name = "force_empty",
3901 .trigger = mem_cgroup_force_empty_write,
3902 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08003903 {
3904 .name = "use_hierarchy",
3905 .write_u64 = mem_cgroup_hierarchy_write,
3906 .read_u64 = mem_cgroup_hierarchy_read,
3907 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003908 {
3909 .name = "swappiness",
3910 .read_u64 = mem_cgroup_swappiness_read,
3911 .write_u64 = mem_cgroup_swappiness_write,
3912 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003913 {
3914 .name = "move_charge_at_immigrate",
3915 .read_u64 = mem_cgroup_move_charge_read,
3916 .write_u64 = mem_cgroup_move_charge_write,
3917 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003918 {
3919 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003920 .read_map = mem_cgroup_oom_control_read,
3921 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003922 .register_event = mem_cgroup_oom_register_event,
3923 .unregister_event = mem_cgroup_oom_unregister_event,
3924 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
3925 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003926};
3927
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003928#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3929static struct cftype memsw_cgroup_files[] = {
3930 {
3931 .name = "memsw.usage_in_bytes",
3932 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
3933 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003934 .register_event = mem_cgroup_usage_register_event,
3935 .unregister_event = mem_cgroup_usage_unregister_event,
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003936 },
3937 {
3938 .name = "memsw.max_usage_in_bytes",
3939 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
3940 .trigger = mem_cgroup_reset,
3941 .read_u64 = mem_cgroup_read,
3942 },
3943 {
3944 .name = "memsw.limit_in_bytes",
3945 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
3946 .write_string = mem_cgroup_write,
3947 .read_u64 = mem_cgroup_read,
3948 },
3949 {
3950 .name = "memsw.failcnt",
3951 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
3952 .trigger = mem_cgroup_reset,
3953 .read_u64 = mem_cgroup_read,
3954 },
3955};
3956
3957static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
3958{
3959 if (!do_swap_account)
3960 return 0;
3961 return cgroup_add_files(cont, ss, memsw_cgroup_files,
3962 ARRAY_SIZE(memsw_cgroup_files));
3963};
3964#else
3965static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
3966{
3967 return 0;
3968}
3969#endif
3970
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003971static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
3972{
3973 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003974 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07003975 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07003976 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003977 /*
3978 * This routine is called against possible nodes.
3979 * But it's BUG to call kmalloc() against offline node.
3980 *
3981 * TODO: this routine can waste much memory for nodes which will
3982 * never be onlined. It's better to use memory hotplug callback
3983 * function.
3984 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07003985 if (!node_state(node, N_NORMAL_MEMORY))
3986 tmp = -1;
3987 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003988 if (!pn)
3989 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003990
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003991 mem->info.nodeinfo[node] = pn;
3992 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003993
3994 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
3995 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07003996 for_each_lru(l)
3997 INIT_LIST_HEAD(&mz->lists[l]);
Balbir Singhf64c3f52009-09-23 15:56:37 -07003998 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07003999 mz->on_tree = false;
4000 mz->mem = mem;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004001 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004002 return 0;
4003}
4004
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004005static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
4006{
4007 kfree(mem->info.nodeinfo[node]);
4008}
4009
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004010static struct mem_cgroup *mem_cgroup_alloc(void)
4011{
4012 struct mem_cgroup *mem;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004013 int size = sizeof(struct mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004014
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004015 /* Can be very big if MAX_NUMNODES is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08004016 if (size < PAGE_SIZE)
4017 mem = kmalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004018 else
Jan Blunckc8dad2b2009-01-07 18:07:53 -08004019 mem = vmalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004020
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07004021 if (!mem)
4022 return NULL;
4023
4024 memset(mem, 0, size);
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004025 mem->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
4026 if (!mem->stat) {
4027 if (size < PAGE_SIZE)
4028 kfree(mem);
4029 else
4030 vfree(mem);
4031 mem = NULL;
4032 }
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004033 return mem;
4034}
4035
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004036/*
4037 * At destroying mem_cgroup, references from swap_cgroup can remain.
4038 * (scanning all at force_empty is too costly...)
4039 *
4040 * Instead of clearing all references at force_empty, we remember
4041 * the number of reference from swap_cgroup and free mem_cgroup when
4042 * it goes down to 0.
4043 *
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004044 * Removal of cgroup itself succeeds regardless of refs from swap.
4045 */
4046
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004047static void __mem_cgroup_free(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004048{
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08004049 int node;
4050
Balbir Singhf64c3f52009-09-23 15:56:37 -07004051 mem_cgroup_remove_from_trees(mem);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004052 free_css_id(&mem_cgroup_subsys, &mem->css);
4053
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08004054 for_each_node_state(node, N_POSSIBLE)
4055 free_mem_cgroup_per_zone_info(mem, node);
4056
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004057 free_percpu(mem->stat);
4058 if (sizeof(struct mem_cgroup) < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004059 kfree(mem);
4060 else
4061 vfree(mem);
4062}
4063
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004064static void mem_cgroup_get(struct mem_cgroup *mem)
4065{
4066 atomic_inc(&mem->refcnt);
4067}
4068
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004069static void __mem_cgroup_put(struct mem_cgroup *mem, int count)
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004070{
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004071 if (atomic_sub_and_test(count, &mem->refcnt)) {
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004072 struct mem_cgroup *parent = parent_mem_cgroup(mem);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004073 __mem_cgroup_free(mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004074 if (parent)
4075 mem_cgroup_put(parent);
4076 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004077}
4078
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004079static void mem_cgroup_put(struct mem_cgroup *mem)
4080{
4081 __mem_cgroup_put(mem, 1);
4082}
4083
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004084/*
4085 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
4086 */
4087static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
4088{
4089 if (!mem->res.parent)
4090 return NULL;
4091 return mem_cgroup_from_res_counter(mem->res.parent, res);
4092}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004093
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004094#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4095static void __init enable_swap_cgroup(void)
4096{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004097 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004098 do_swap_account = 1;
4099}
4100#else
4101static void __init enable_swap_cgroup(void)
4102{
4103}
4104#endif
4105
Balbir Singhf64c3f52009-09-23 15:56:37 -07004106static int mem_cgroup_soft_limit_tree_init(void)
4107{
4108 struct mem_cgroup_tree_per_node *rtpn;
4109 struct mem_cgroup_tree_per_zone *rtpz;
4110 int tmp, node, zone;
4111
4112 for_each_node_state(node, N_POSSIBLE) {
4113 tmp = node;
4114 if (!node_state(node, N_NORMAL_MEMORY))
4115 tmp = -1;
4116 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
4117 if (!rtpn)
4118 return 1;
4119
4120 soft_limit_tree.rb_tree_per_node[node] = rtpn;
4121
4122 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4123 rtpz = &rtpn->rb_tree_per_zone[zone];
4124 rtpz->rb_root = RB_ROOT;
4125 spin_lock_init(&rtpz->lock);
4126 }
4127 }
4128 return 0;
4129}
4130
Li Zefan0eb253e2009-01-15 13:51:25 -08004131static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004132mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
4133{
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004134 struct mem_cgroup *mem, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004135 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004136 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004137
Jan Blunckc8dad2b2009-01-07 18:07:53 -08004138 mem = mem_cgroup_alloc();
4139 if (!mem)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004140 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004141
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004142 for_each_node_state(node, N_POSSIBLE)
4143 if (alloc_mem_cgroup_per_zone_info(mem, node))
4144 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004145
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004146 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004147 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004148 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004149 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004150 parent = NULL;
Balbir Singh4b3bde42009-09-23 15:56:32 -07004151 root_mem_cgroup = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004152 if (mem_cgroup_soft_limit_tree_init())
4153 goto free_out;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004154 for_each_possible_cpu(cpu) {
4155 struct memcg_stock_pcp *stock =
4156 &per_cpu(memcg_stock, cpu);
4157 INIT_WORK(&stock->work, drain_local_stock);
4158 }
4159 hotcpu_notifier(memcg_stock_cpu_callback, 0);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004160 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004161 parent = mem_cgroup_from_cont(cont->parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004162 mem->use_hierarchy = parent->use_hierarchy;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004163 mem->oom_kill_disable = parent->oom_kill_disable;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004164 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004165
Balbir Singh18f59ea2009-01-07 18:08:07 -08004166 if (parent && parent->use_hierarchy) {
4167 res_counter_init(&mem->res, &parent->res);
4168 res_counter_init(&mem->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004169 /*
4170 * We increment refcnt of the parent to ensure that we can
4171 * safely access it on res_counter_charge/uncharge.
4172 * This refcnt will be decremented when freeing this
4173 * mem_cgroup(see mem_cgroup_put).
4174 */
4175 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004176 } else {
4177 res_counter_init(&mem->res, NULL);
4178 res_counter_init(&mem->memsw, NULL);
4179 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004180 mem->last_scanned_child = 0;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -08004181 spin_lock_init(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004182 INIT_LIST_HEAD(&mem->oom_notify);
Balbir Singh6d61ef42009-01-07 18:08:06 -08004183
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004184 if (parent)
4185 mem->swappiness = get_swappiness(parent);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004186 atomic_set(&mem->refcnt, 1);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004187 mem->move_charge_at_immigrate = 0;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004188 mutex_init(&mem->thresholds_lock);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004189 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004190free_out:
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004191 __mem_cgroup_free(mem);
Balbir Singh4b3bde42009-09-23 15:56:32 -07004192 root_mem_cgroup = NULL;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004193 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004194}
4195
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004196static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004197 struct cgroup *cont)
4198{
4199 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004200
4201 return mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004202}
4203
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004204static void mem_cgroup_destroy(struct cgroup_subsys *ss,
4205 struct cgroup *cont)
4206{
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004207 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004208
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004209 mem_cgroup_put(mem);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004210}
4211
4212static int mem_cgroup_populate(struct cgroup_subsys *ss,
4213 struct cgroup *cont)
4214{
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004215 int ret;
4216
4217 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
4218 ARRAY_SIZE(mem_cgroup_files));
4219
4220 if (!ret)
4221 ret = register_memsw_files(cont, ss);
4222 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004223}
4224
Daisuke Nishimura02491442010-03-10 15:22:17 -08004225#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004226/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004227#define PRECHARGE_COUNT_AT_ONCE 256
4228static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004229{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004230 int ret = 0;
4231 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004232 struct mem_cgroup *mem = mc.to;
4233
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004234 if (mem_cgroup_is_root(mem)) {
4235 mc.precharge += count;
4236 /* we don't need css_get for root */
4237 return ret;
4238 }
4239 /* try to charge at once */
4240 if (count > 1) {
4241 struct res_counter *dummy;
4242 /*
4243 * "mem" cannot be under rmdir() because we've already checked
4244 * by cgroup_lock_live_cgroup() that it is not removed and we
4245 * are still under the same cgroup_mutex. So we can postpone
4246 * css_get().
4247 */
4248 if (res_counter_charge(&mem->res, PAGE_SIZE * count, &dummy))
4249 goto one_by_one;
4250 if (do_swap_account && res_counter_charge(&mem->memsw,
4251 PAGE_SIZE * count, &dummy)) {
4252 res_counter_uncharge(&mem->res, PAGE_SIZE * count);
4253 goto one_by_one;
4254 }
4255 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004256 return ret;
4257 }
4258one_by_one:
4259 /* fall back to one by one charge */
4260 while (count--) {
4261 if (signal_pending(current)) {
4262 ret = -EINTR;
4263 break;
4264 }
4265 if (!batch_count--) {
4266 batch_count = PRECHARGE_COUNT_AT_ONCE;
4267 cond_resched();
4268 }
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08004269 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004270 if (ret || !mem)
4271 /* mem_cgroup_clear_mc() will do uncharge later */
4272 return -ENOMEM;
4273 mc.precharge++;
4274 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004275 return ret;
4276}
4277
4278/**
4279 * is_target_pte_for_mc - check a pte whether it is valid for move charge
4280 * @vma: the vma the pte to be checked belongs
4281 * @addr: the address corresponding to the pte to be checked
4282 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08004283 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004284 *
4285 * Returns
4286 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
4287 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
4288 * move charge. if @target is not NULL, the page is stored in target->page
4289 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08004290 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
4291 * target for charge migration. if @target is not NULL, the entry is stored
4292 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004293 *
4294 * Called with pte lock held.
4295 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004296union mc_target {
4297 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004298 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004299};
4300
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004301enum mc_target_type {
4302 MC_TARGET_NONE, /* not used */
4303 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08004304 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004305};
4306
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004307static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
4308 unsigned long addr, pte_t ptent)
4309{
4310 struct page *page = vm_normal_page(vma, addr, ptent);
4311
4312 if (!page || !page_mapped(page))
4313 return NULL;
4314 if (PageAnon(page)) {
4315 /* we don't move shared anon */
4316 if (!move_anon() || page_mapcount(page) > 2)
4317 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004318 } else if (!move_file())
4319 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004320 return NULL;
4321 if (!get_page_unless_zero(page))
4322 return NULL;
4323
4324 return page;
4325}
4326
4327static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
4328 unsigned long addr, pte_t ptent, swp_entry_t *entry)
4329{
4330 int usage_count;
4331 struct page *page = NULL;
4332 swp_entry_t ent = pte_to_swp_entry(ptent);
4333
4334 if (!move_anon() || non_swap_entry(ent))
4335 return NULL;
4336 usage_count = mem_cgroup_count_swap_user(ent, &page);
4337 if (usage_count > 1) { /* we don't move shared anon */
4338 if (page)
4339 put_page(page);
4340 return NULL;
4341 }
4342 if (do_swap_account)
4343 entry->val = ent.val;
4344
4345 return page;
4346}
4347
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004348static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
4349 unsigned long addr, pte_t ptent, swp_entry_t *entry)
4350{
4351 struct page *page = NULL;
4352 struct inode *inode;
4353 struct address_space *mapping;
4354 pgoff_t pgoff;
4355
4356 if (!vma->vm_file) /* anonymous vma */
4357 return NULL;
4358 if (!move_file())
4359 return NULL;
4360
4361 inode = vma->vm_file->f_path.dentry->d_inode;
4362 mapping = vma->vm_file->f_mapping;
4363 if (pte_none(ptent))
4364 pgoff = linear_page_index(vma, addr);
4365 else /* pte_file(ptent) is true */
4366 pgoff = pte_to_pgoff(ptent);
4367
4368 /* page is moved even if it's not RSS of this task(page-faulted). */
4369 if (!mapping_cap_swap_backed(mapping)) { /* normal file */
4370 page = find_get_page(mapping, pgoff);
4371 } else { /* shmem/tmpfs file. we should take account of swap too. */
4372 swp_entry_t ent;
4373 mem_cgroup_get_shmem_target(inode, pgoff, &page, &ent);
4374 if (do_swap_account)
4375 entry->val = ent.val;
4376 }
4377
4378 return page;
4379}
4380
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004381static int is_target_pte_for_mc(struct vm_area_struct *vma,
4382 unsigned long addr, pte_t ptent, union mc_target *target)
4383{
Daisuke Nishimura02491442010-03-10 15:22:17 -08004384 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004385 struct page_cgroup *pc;
4386 int ret = 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004387 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004388
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004389 if (pte_present(ptent))
4390 page = mc_handle_present_pte(vma, addr, ptent);
4391 else if (is_swap_pte(ptent))
4392 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004393 else if (pte_none(ptent) || pte_file(ptent))
4394 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004395
4396 if (!page && !ent.val)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004397 return 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004398 if (page) {
4399 pc = lookup_page_cgroup(page);
4400 /*
4401 * Do only loose check w/o page_cgroup lock.
4402 * mem_cgroup_move_account() checks the pc is valid or not under
4403 * the lock.
4404 */
4405 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
4406 ret = MC_TARGET_PAGE;
4407 if (target)
4408 target->page = page;
4409 }
4410 if (!ret || !target)
4411 put_page(page);
4412 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004413 /* There is a swap entry and a page doesn't exist or isn't charged */
4414 if (ent.val && !ret &&
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07004415 css_id(&mc.from->css) == lookup_swap_cgroup(ent)) {
4416 ret = MC_TARGET_SWAP;
4417 if (target)
4418 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004419 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004420 return ret;
4421}
4422
4423static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
4424 unsigned long addr, unsigned long end,
4425 struct mm_walk *walk)
4426{
4427 struct vm_area_struct *vma = walk->private;
4428 pte_t *pte;
4429 spinlock_t *ptl;
4430
4431 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
4432 for (; addr != end; pte++, addr += PAGE_SIZE)
4433 if (is_target_pte_for_mc(vma, addr, *pte, NULL))
4434 mc.precharge++; /* increment precharge temporarily */
4435 pte_unmap_unlock(pte - 1, ptl);
4436 cond_resched();
4437
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004438 return 0;
4439}
4440
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004441static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
4442{
4443 unsigned long precharge;
4444 struct vm_area_struct *vma;
4445
4446 down_read(&mm->mmap_sem);
4447 for (vma = mm->mmap; vma; vma = vma->vm_next) {
4448 struct mm_walk mem_cgroup_count_precharge_walk = {
4449 .pmd_entry = mem_cgroup_count_precharge_pte_range,
4450 .mm = mm,
4451 .private = vma,
4452 };
4453 if (is_vm_hugetlb_page(vma))
4454 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004455 walk_page_range(vma->vm_start, vma->vm_end,
4456 &mem_cgroup_count_precharge_walk);
4457 }
4458 up_read(&mm->mmap_sem);
4459
4460 precharge = mc.precharge;
4461 mc.precharge = 0;
4462
4463 return precharge;
4464}
4465
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004466static int mem_cgroup_precharge_mc(struct mm_struct *mm)
4467{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004468 return mem_cgroup_do_precharge(mem_cgroup_count_precharge(mm));
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004469}
4470
4471static void mem_cgroup_clear_mc(void)
4472{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07004473 struct mem_cgroup *from = mc.from;
4474 struct mem_cgroup *to = mc.to;
4475
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004476 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004477 if (mc.precharge) {
4478 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
4479 mc.precharge = 0;
4480 }
4481 /*
4482 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
4483 * we must uncharge here.
4484 */
4485 if (mc.moved_charge) {
4486 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
4487 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004488 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004489 /* we must fixup refcnts and charges */
4490 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004491 /* uncharge swap account from the old cgroup */
4492 if (!mem_cgroup_is_root(mc.from))
4493 res_counter_uncharge(&mc.from->memsw,
4494 PAGE_SIZE * mc.moved_swap);
4495 __mem_cgroup_put(mc.from, mc.moved_swap);
4496
4497 if (!mem_cgroup_is_root(mc.to)) {
4498 /*
4499 * we charged both to->res and to->memsw, so we should
4500 * uncharge to->res.
4501 */
4502 res_counter_uncharge(&mc.to->res,
4503 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004504 }
4505 /* we've already done mem_cgroup_get(mc.to) */
4506
4507 mc.moved_swap = 0;
4508 }
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07004509 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004510 mc.from = NULL;
4511 mc.to = NULL;
Daisuke Nishimura8033b972010-03-10 15:22:16 -08004512 mc.moving_task = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07004513 spin_unlock(&mc.lock);
4514 memcg_oom_recover(from);
4515 memcg_oom_recover(to);
Daisuke Nishimura8033b972010-03-10 15:22:16 -08004516 wake_up_all(&mc.waitq);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004517}
4518
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004519static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
4520 struct cgroup *cgroup,
4521 struct task_struct *p,
4522 bool threadgroup)
4523{
4524 int ret = 0;
4525 struct mem_cgroup *mem = mem_cgroup_from_cont(cgroup);
4526
4527 if (mem->move_charge_at_immigrate) {
4528 struct mm_struct *mm;
4529 struct mem_cgroup *from = mem_cgroup_from_task(p);
4530
4531 VM_BUG_ON(from == mem);
4532
4533 mm = get_task_mm(p);
4534 if (!mm)
4535 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004536 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004537 if (mm->owner == p) {
4538 VM_BUG_ON(mc.from);
4539 VM_BUG_ON(mc.to);
4540 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004541 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004542 VM_BUG_ON(mc.moved_swap);
Daisuke Nishimura8033b972010-03-10 15:22:16 -08004543 VM_BUG_ON(mc.moving_task);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07004544 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004545 mc.from = from;
4546 mc.to = mem;
4547 mc.precharge = 0;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004548 mc.moved_charge = 0;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004549 mc.moved_swap = 0;
Daisuke Nishimura8033b972010-03-10 15:22:16 -08004550 mc.moving_task = current;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07004551 spin_unlock(&mc.lock);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004552
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004553 ret = mem_cgroup_precharge_mc(mm);
4554 if (ret)
4555 mem_cgroup_clear_mc();
4556 }
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004557 mmput(mm);
4558 }
4559 return ret;
4560}
4561
4562static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
4563 struct cgroup *cgroup,
4564 struct task_struct *p,
4565 bool threadgroup)
4566{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004567 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004568}
4569
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004570static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
4571 unsigned long addr, unsigned long end,
4572 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004573{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004574 int ret = 0;
4575 struct vm_area_struct *vma = walk->private;
4576 pte_t *pte;
4577 spinlock_t *ptl;
4578
4579retry:
4580 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
4581 for (; addr != end; addr += PAGE_SIZE) {
4582 pte_t ptent = *(pte++);
4583 union mc_target target;
4584 int type;
4585 struct page *page;
4586 struct page_cgroup *pc;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004587 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004588
4589 if (!mc.precharge)
4590 break;
4591
4592 type = is_target_pte_for_mc(vma, addr, ptent, &target);
4593 switch (type) {
4594 case MC_TARGET_PAGE:
4595 page = target.page;
4596 if (isolate_lru_page(page))
4597 goto put;
4598 pc = lookup_page_cgroup(page);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004599 if (!mem_cgroup_move_account(pc,
4600 mc.from, mc.to, false)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004601 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004602 /* we uncharge from mc.from later. */
4603 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004604 }
4605 putback_lru_page(page);
4606put: /* is_target_pte_for_mc() gets the page */
4607 put_page(page);
4608 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004609 case MC_TARGET_SWAP:
4610 ent = target.ent;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004611 if (!mem_cgroup_move_swap_account(ent,
4612 mc.from, mc.to, false)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08004613 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004614 /* we fixup refcnts and charges later. */
4615 mc.moved_swap++;
4616 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08004617 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004618 default:
4619 break;
4620 }
4621 }
4622 pte_unmap_unlock(pte - 1, ptl);
4623 cond_resched();
4624
4625 if (addr != end) {
4626 /*
4627 * We have consumed all precharges we got in can_attach().
4628 * We try charge one by one, but don't do any additional
4629 * charges to mc.to if we have failed in charge once in attach()
4630 * phase.
4631 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004632 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004633 if (!ret)
4634 goto retry;
4635 }
4636
4637 return ret;
4638}
4639
4640static void mem_cgroup_move_charge(struct mm_struct *mm)
4641{
4642 struct vm_area_struct *vma;
4643
4644 lru_add_drain_all();
4645 down_read(&mm->mmap_sem);
4646 for (vma = mm->mmap; vma; vma = vma->vm_next) {
4647 int ret;
4648 struct mm_walk mem_cgroup_move_charge_walk = {
4649 .pmd_entry = mem_cgroup_move_charge_pte_range,
4650 .mm = mm,
4651 .private = vma,
4652 };
4653 if (is_vm_hugetlb_page(vma))
4654 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004655 ret = walk_page_range(vma->vm_start, vma->vm_end,
4656 &mem_cgroup_move_charge_walk);
4657 if (ret)
4658 /*
4659 * means we have consumed all precharges and failed in
4660 * doing additional charge. Just abandon here.
4661 */
4662 break;
4663 }
4664 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004665}
4666
Balbir Singh67e465a2008-02-07 00:13:54 -08004667static void mem_cgroup_move_task(struct cgroup_subsys *ss,
4668 struct cgroup *cont,
4669 struct cgroup *old_cont,
Ben Blumbe367d02009-09-23 15:56:31 -07004670 struct task_struct *p,
4671 bool threadgroup)
Balbir Singh67e465a2008-02-07 00:13:54 -08004672{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004673 struct mm_struct *mm;
4674
4675 if (!mc.to)
4676 /* no need to move charge */
4677 return;
4678
4679 mm = get_task_mm(p);
4680 if (mm) {
4681 mem_cgroup_move_charge(mm);
4682 mmput(mm);
4683 }
4684 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08004685}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07004686#else /* !CONFIG_MMU */
4687static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
4688 struct cgroup *cgroup,
4689 struct task_struct *p,
4690 bool threadgroup)
4691{
4692 return 0;
4693}
4694static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
4695 struct cgroup *cgroup,
4696 struct task_struct *p,
4697 bool threadgroup)
4698{
4699}
4700static void mem_cgroup_move_task(struct cgroup_subsys *ss,
4701 struct cgroup *cont,
4702 struct cgroup *old_cont,
4703 struct task_struct *p,
4704 bool threadgroup)
4705{
4706}
4707#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08004708
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004709struct cgroup_subsys mem_cgroup_subsys = {
4710 .name = "memory",
4711 .subsys_id = mem_cgroup_subsys_id,
4712 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004713 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004714 .destroy = mem_cgroup_destroy,
4715 .populate = mem_cgroup_populate,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004716 .can_attach = mem_cgroup_can_attach,
4717 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08004718 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004719 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004720 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004721};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004722
4723#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4724
4725static int __init disable_swap_account(char *s)
4726{
4727 really_do_swap_account = 0;
4728 return 1;
4729}
4730__setup("noswapaccount", disable_swap_account);
4731#endif