blob: 5e7a14d117c7726a21fb58bf430ec9e23896a743 [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 Hiroyuki32047e22010-10-27 15:33:40 -070093 MEM_CGROUP_ON_MOVE, /* someone is moving account between groups */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080094
95 MEM_CGROUP_STAT_NSTATS,
96};
97
98struct mem_cgroup_stat_cpu {
99 s64 count[MEM_CGROUP_STAT_NSTATS];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800100};
101
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800102/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800103 * per-zone information in memory controller.
104 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800105struct mem_cgroup_per_zone {
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800106 /*
107 * spin_lock to protect the per cgroup LRU
108 */
Christoph Lameterb69408e2008-10-18 20:26:14 -0700109 struct list_head lists[NR_LRU_LISTS];
110 unsigned long count[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800111
112 struct zone_reclaim_stat reclaim_stat;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700113 struct rb_node tree_node; /* RB tree node */
114 unsigned long long usage_in_excess;/* Set to the value by which */
115 /* the soft limit is exceeded*/
116 bool on_tree;
Balbir Singh4e416952009-09-23 15:56:39 -0700117 struct mem_cgroup *mem; /* Back pointer, we cannot */
118 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800119};
120/* Macro for accessing counter */
121#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
122
123struct mem_cgroup_per_node {
124 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
125};
126
127struct mem_cgroup_lru_info {
128 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
129};
130
131/*
Balbir Singhf64c3f52009-09-23 15:56:37 -0700132 * Cgroups above their limits are maintained in a RB-Tree, independent of
133 * their hierarchy representation
134 */
135
136struct mem_cgroup_tree_per_zone {
137 struct rb_root rb_root;
138 spinlock_t lock;
139};
140
141struct mem_cgroup_tree_per_node {
142 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
143};
144
145struct mem_cgroup_tree {
146 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
147};
148
149static struct mem_cgroup_tree soft_limit_tree __read_mostly;
150
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800151struct mem_cgroup_threshold {
152 struct eventfd_ctx *eventfd;
153 u64 threshold;
154};
155
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700156/* For threshold */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800157struct mem_cgroup_threshold_ary {
158 /* An array index points to threshold just below usage. */
Phil Carmody5407a562010-05-26 14:42:42 -0700159 int current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800160 /* Size of entries[] */
161 unsigned int size;
162 /* Array of thresholds */
163 struct mem_cgroup_threshold entries[0];
164};
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700165
166struct mem_cgroup_thresholds {
167 /* Primary thresholds array */
168 struct mem_cgroup_threshold_ary *primary;
169 /*
170 * Spare threshold array.
171 * This is needed to make mem_cgroup_unregister_event() "never fail".
172 * It must be able to store at least primary->size - 1 entries.
173 */
174 struct mem_cgroup_threshold_ary *spare;
175};
176
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700177/* for OOM */
178struct mem_cgroup_eventfd_list {
179 struct list_head list;
180 struct eventfd_ctx *eventfd;
181};
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800182
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800183static void mem_cgroup_threshold(struct mem_cgroup *mem);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700184static void mem_cgroup_oom_notify(struct mem_cgroup *mem);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800185
Balbir Singhf64c3f52009-09-23 15:56:37 -0700186/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800187 * The memory controller data structure. The memory controller controls both
188 * page cache and RSS per cgroup. We would eventually like to provide
189 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
190 * to help the administrator determine what knobs to tune.
191 *
192 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800193 * we hit the water mark. May be even add a low water mark, such that
194 * no reclaim occurs from a cgroup at it's low water mark, this is
195 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800196 */
197struct mem_cgroup {
198 struct cgroup_subsys_state css;
199 /*
200 * the counter to account for memory usage
201 */
202 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800203 /*
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800204 * the counter to account for mem+swap usage.
205 */
206 struct res_counter memsw;
207 /*
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800208 * Per cgroup active and inactive list, similar to the
209 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800210 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800211 struct mem_cgroup_lru_info info;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800212
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800213 /*
214 protect against reclaim related member.
215 */
216 spinlock_t reclaim_param_lock;
217
Balbir Singh6d61ef42009-01-07 18:08:06 -0800218 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200219 * While reclaiming in a hierarchy, we cache the last child we
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700220 * reclaimed from.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800221 */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700222 int last_scanned_child;
Balbir Singh18f59ea2009-01-07 18:08:07 -0800223 /*
224 * Should the accounting and control be hierarchical, per subtree?
225 */
226 bool use_hierarchy;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -0800227 atomic_t oom_lock;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800228 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800229
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800230 unsigned int swappiness;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -0700231 /* OOM-Killer disable */
232 int oom_kill_disable;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800233
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700234 /* set when res.limit == memsw.limit */
235 bool memsw_is_minimum;
236
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800237 /* protect arrays of thresholds */
238 struct mutex thresholds_lock;
239
240 /* thresholds for memory usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700241 struct mem_cgroup_thresholds thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700242
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800243 /* thresholds for mem+swap usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700244 struct mem_cgroup_thresholds memsw_thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700245
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700246 /* For oom notifier event fd */
247 struct list_head oom_notify;
248
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800249 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800250 * Should we move charges of a task when a task is moved into this
251 * mem_cgroup ? And what type of charges should we move ?
252 */
253 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800254 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800255 * percpu counter.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800256 */
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800257 struct mem_cgroup_stat_cpu *stat;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800258};
259
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800260/* Stuffs for move charges at task migration. */
261/*
262 * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a
263 * left-shifted bitmap of these types.
264 */
265enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800266 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700267 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800268 NR_MOVE_TYPE,
269};
270
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800271/* "mc" and its members are protected by cgroup_mutex */
272static struct move_charge_struct {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700273 spinlock_t lock; /* for from, to, moving_task */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800274 struct mem_cgroup *from;
275 struct mem_cgroup *to;
276 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800277 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800278 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800279 struct task_struct *moving_task; /* a task moving charges */
280 wait_queue_head_t waitq; /* a waitq for other context */
281} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700282 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800283 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
284};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800285
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700286static bool move_anon(void)
287{
288 return test_bit(MOVE_CHARGE_TYPE_ANON,
289 &mc.to->move_charge_at_immigrate);
290}
291
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700292static bool move_file(void)
293{
294 return test_bit(MOVE_CHARGE_TYPE_FILE,
295 &mc.to->move_charge_at_immigrate);
296}
297
Balbir Singh4e416952009-09-23 15:56:39 -0700298/*
299 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
300 * limit reclaim to prevent infinite loops, if they ever occur.
301 */
302#define MEM_CGROUP_MAX_RECLAIM_LOOPS (100)
303#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2)
304
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800305enum charge_type {
306 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
307 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700308 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700309 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800310 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700311 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700312 NR_CHARGE_TYPE,
313};
314
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700315/* only for here (for easy reading.) */
316#define PCGF_CACHE (1UL << PCG_CACHE)
317#define PCGF_USED (1UL << PCG_USED)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700318#define PCGF_LOCK (1UL << PCG_LOCK)
Balbir Singh4b3bde42009-09-23 15:56:32 -0700319/* Not used, but added here for completeness */
320#define PCGF_ACCT (1UL << PCG_ACCT)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800321
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800322/* for encoding cft->private value on file */
323#define _MEM (0)
324#define _MEMSWAP (1)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700325#define _OOM_TYPE (2)
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800326#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
327#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
328#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700329/* Used for OOM nofiier */
330#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800331
Balbir Singh75822b42009-09-23 15:56:38 -0700332/*
333 * Reclaim flags for mem_cgroup_hierarchical_reclaim
334 */
335#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
336#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
337#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
338#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
Balbir Singh4e416952009-09-23 15:56:39 -0700339#define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
340#define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
Balbir Singh75822b42009-09-23 15:56:38 -0700341
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800342static void mem_cgroup_get(struct mem_cgroup *mem);
343static void mem_cgroup_put(struct mem_cgroup *mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -0800344static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -0800345static void drain_all_stock_async(void);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800346
Balbir Singhf64c3f52009-09-23 15:56:37 -0700347static struct mem_cgroup_per_zone *
348mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
349{
350 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
351}
352
Wu Fengguangd3242362009-12-16 12:19:59 +0100353struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *mem)
354{
355 return &mem->css;
356}
357
Balbir Singhf64c3f52009-09-23 15:56:37 -0700358static struct mem_cgroup_per_zone *
359page_cgroup_zoneinfo(struct page_cgroup *pc)
360{
361 struct mem_cgroup *mem = pc->mem_cgroup;
362 int nid = page_cgroup_nid(pc);
363 int zid = page_cgroup_zid(pc);
364
365 if (!mem)
366 return NULL;
367
368 return mem_cgroup_zoneinfo(mem, nid, zid);
369}
370
371static struct mem_cgroup_tree_per_zone *
372soft_limit_tree_node_zone(int nid, int zid)
373{
374 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
375}
376
377static struct mem_cgroup_tree_per_zone *
378soft_limit_tree_from_page(struct page *page)
379{
380 int nid = page_to_nid(page);
381 int zid = page_zonenum(page);
382
383 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
384}
385
386static void
Balbir Singh4e416952009-09-23 15:56:39 -0700387__mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700388 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700389 struct mem_cgroup_tree_per_zone *mctz,
390 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700391{
392 struct rb_node **p = &mctz->rb_root.rb_node;
393 struct rb_node *parent = NULL;
394 struct mem_cgroup_per_zone *mz_node;
395
396 if (mz->on_tree)
397 return;
398
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700399 mz->usage_in_excess = new_usage_in_excess;
400 if (!mz->usage_in_excess)
401 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700402 while (*p) {
403 parent = *p;
404 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
405 tree_node);
406 if (mz->usage_in_excess < mz_node->usage_in_excess)
407 p = &(*p)->rb_left;
408 /*
409 * We can't avoid mem cgroups that are over their soft
410 * limit by the same amount
411 */
412 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
413 p = &(*p)->rb_right;
414 }
415 rb_link_node(&mz->tree_node, parent, p);
416 rb_insert_color(&mz->tree_node, &mctz->rb_root);
417 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700418}
419
420static void
421__mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
422 struct mem_cgroup_per_zone *mz,
423 struct mem_cgroup_tree_per_zone *mctz)
424{
425 if (!mz->on_tree)
426 return;
427 rb_erase(&mz->tree_node, &mctz->rb_root);
428 mz->on_tree = false;
429}
430
431static void
Balbir Singhf64c3f52009-09-23 15:56:37 -0700432mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
433 struct mem_cgroup_per_zone *mz,
434 struct mem_cgroup_tree_per_zone *mctz)
435{
436 spin_lock(&mctz->lock);
Balbir Singh4e416952009-09-23 15:56:39 -0700437 __mem_cgroup_remove_exceeded(mem, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700438 spin_unlock(&mctz->lock);
439}
440
Balbir Singhf64c3f52009-09-23 15:56:37 -0700441
442static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
443{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700444 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700445 struct mem_cgroup_per_zone *mz;
446 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700447 int nid = page_to_nid(page);
448 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700449 mctz = soft_limit_tree_from_page(page);
450
451 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700452 * Necessary to update all ancestors when hierarchy is used.
453 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700454 */
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700455 for (; mem; mem = parent_mem_cgroup(mem)) {
456 mz = mem_cgroup_zoneinfo(mem, nid, zid);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700457 excess = res_counter_soft_limit_excess(&mem->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700458 /*
459 * We have to update the tree if mz is on RB-tree or
460 * mem is over its softlimit.
461 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700462 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700463 spin_lock(&mctz->lock);
464 /* if on-tree, remove it */
465 if (mz->on_tree)
466 __mem_cgroup_remove_exceeded(mem, mz, mctz);
467 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700468 * Insert again. mz->usage_in_excess will be updated.
469 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700470 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700471 __mem_cgroup_insert_exceeded(mem, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700472 spin_unlock(&mctz->lock);
473 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700474 }
475}
476
477static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
478{
479 int node, zone;
480 struct mem_cgroup_per_zone *mz;
481 struct mem_cgroup_tree_per_zone *mctz;
482
483 for_each_node_state(node, N_POSSIBLE) {
484 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
485 mz = mem_cgroup_zoneinfo(mem, node, zone);
486 mctz = soft_limit_tree_node_zone(node, zone);
487 mem_cgroup_remove_exceeded(mem, mz, mctz);
488 }
489 }
490}
491
Balbir Singh4e416952009-09-23 15:56:39 -0700492static inline unsigned long mem_cgroup_get_excess(struct mem_cgroup *mem)
493{
494 return res_counter_soft_limit_excess(&mem->res) >> PAGE_SHIFT;
495}
496
497static struct mem_cgroup_per_zone *
498__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
499{
500 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700501 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700502
503retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700504 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700505 rightmost = rb_last(&mctz->rb_root);
506 if (!rightmost)
507 goto done; /* Nothing to reclaim from */
508
509 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
510 /*
511 * Remove the node now but someone else can add it back,
512 * we will to add it back at the end of reclaim to its correct
513 * position in the tree.
514 */
515 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
516 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
517 !css_tryget(&mz->mem->css))
518 goto retry;
519done:
520 return mz;
521}
522
523static struct mem_cgroup_per_zone *
524mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
525{
526 struct mem_cgroup_per_zone *mz;
527
528 spin_lock(&mctz->lock);
529 mz = __mem_cgroup_largest_soft_limit_node(mctz);
530 spin_unlock(&mctz->lock);
531 return mz;
532}
533
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800534static s64 mem_cgroup_read_stat(struct mem_cgroup *mem,
535 enum mem_cgroup_stat_index idx)
536{
537 int cpu;
538 s64 val = 0;
539
540 for_each_possible_cpu(cpu)
541 val += per_cpu(mem->stat->count[idx], cpu);
542 return val;
543}
544
545static s64 mem_cgroup_local_usage(struct mem_cgroup *mem)
546{
547 s64 ret;
548
549 ret = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS);
550 ret += mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE);
551 return ret;
552}
553
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700554static void mem_cgroup_swap_statistics(struct mem_cgroup *mem,
555 bool charge)
556{
557 int val = (charge) ? 1 : -1;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800558 this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_SWAPOUT], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700559}
560
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700561static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
562 struct page_cgroup *pc,
563 bool charge)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800564{
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700565 int val = (charge) ? 1 : -1;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800566
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800567 preempt_disable();
568
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700569 if (PageCgroupCache(pc))
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800570 __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_CACHE], val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800571 else
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800572 __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_RSS], val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700573
574 if (charge)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800575 __this_cpu_inc(mem->stat->count[MEM_CGROUP_STAT_PGPGIN_COUNT]);
Balaji Rao55e462b2008-05-01 04:35:12 -0700576 else
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800577 __this_cpu_inc(mem->stat->count[MEM_CGROUP_STAT_PGPGOUT_COUNT]);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800578 __this_cpu_inc(mem->stat->count[MEM_CGROUP_EVENTS]);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800579
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800580 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800581}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800582
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700583static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700584 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800585{
586 int nid, zid;
587 struct mem_cgroup_per_zone *mz;
588 u64 total = 0;
589
590 for_each_online_node(nid)
591 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
592 mz = mem_cgroup_zoneinfo(mem, nid, zid);
593 total += MEM_CGROUP_ZSTAT(mz, idx);
594 }
595 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800596}
597
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800598static bool __memcg_event_check(struct mem_cgroup *mem, int event_mask_shift)
599{
600 s64 val;
601
602 val = this_cpu_read(mem->stat->count[MEM_CGROUP_EVENTS]);
603
604 return !(val & ((1 << event_mask_shift) - 1));
605}
606
607/*
608 * Check events in order.
609 *
610 */
611static void memcg_check_events(struct mem_cgroup *mem, struct page *page)
612{
613 /* threshold event is triggered in finer grain than soft limit */
614 if (unlikely(__memcg_event_check(mem, THRESHOLDS_EVENTS_THRESH))) {
615 mem_cgroup_threshold(mem);
616 if (unlikely(__memcg_event_check(mem, SOFTLIMIT_EVENTS_THRESH)))
617 mem_cgroup_update_tree(mem, page);
618 }
619}
620
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800621static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800622{
623 return container_of(cgroup_subsys_state(cont,
624 mem_cgroup_subsys_id), struct mem_cgroup,
625 css);
626}
627
Balbir Singhcf475ad2008-04-29 01:00:16 -0700628struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800629{
Balbir Singh31a78f22008-09-28 23:09:31 +0100630 /*
631 * mm_update_next_owner() may clear mm->owner to NULL
632 * if it races with swapoff, page migration, etc.
633 * So this can be called with p == NULL.
634 */
635 if (unlikely(!p))
636 return NULL;
637
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800638 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
639 struct mem_cgroup, css);
640}
641
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800642static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
643{
644 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700645
646 if (!mm)
647 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800648 /*
649 * Because we have no locks, mm->owner's may be being moved to other
650 * cgroup. We use css_tryget() here even if this looks
651 * pessimistic (rather than adding locks here).
652 */
653 rcu_read_lock();
654 do {
655 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
656 if (unlikely(!mem))
657 break;
658 } while (!css_tryget(&mem->css));
659 rcu_read_unlock();
660 return mem;
661}
662
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700663/* The caller has to guarantee "mem" exists before calling this */
664static struct mem_cgroup *mem_cgroup_start_loop(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700665{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700666 if (mem && css_tryget(&mem->css))
667 return mem;
668 return NULL;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700669}
670
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700671static struct mem_cgroup *mem_cgroup_get_next(struct mem_cgroup *iter,
672 struct mem_cgroup *root,
673 bool cond)
674{
675 int nextid = css_id(&iter->css) + 1;
676 int found;
677 int hierarchy_used;
678 struct cgroup_subsys_state *css;
679
680 hierarchy_used = iter->use_hierarchy;
681
682 css_put(&iter->css);
683 if (!cond || !hierarchy_used)
684 return NULL;
685
686 do {
687 iter = NULL;
688 rcu_read_lock();
689
690 css = css_get_next(&mem_cgroup_subsys, nextid,
691 &root->css, &found);
692 if (css && css_tryget(css))
693 iter = container_of(css, struct mem_cgroup, css);
694 rcu_read_unlock();
695 /* If css is NULL, no more cgroups will be found */
696 nextid = found + 1;
697 } while (css && !iter);
698
699 return iter;
700}
701/*
702 * for_eacn_mem_cgroup_tree() for visiting all cgroup under tree. Please
703 * be careful that "break" loop is not allowed. We have reference count.
704 * Instead of that modify "cond" to be false and "continue" to exit the loop.
705 */
706#define for_each_mem_cgroup_tree_cond(iter, root, cond) \
707 for (iter = mem_cgroup_start_loop(root);\
708 iter != NULL;\
709 iter = mem_cgroup_get_next(iter, root, cond))
710
711#define for_each_mem_cgroup_tree(iter, root) \
712 for_each_mem_cgroup_tree_cond(iter, root, true)
713
714
Balbir Singh4b3bde42009-09-23 15:56:32 -0700715static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
716{
717 return (mem == root_mem_cgroup);
718}
719
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800720/*
721 * Following LRU functions are allowed to be used without PCG_LOCK.
722 * Operations are called by routine of global LRU independently from memcg.
723 * What we have to take care of here is validness of pc->mem_cgroup.
724 *
725 * Changes to pc->mem_cgroup happens when
726 * 1. charge
727 * 2. moving account
728 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
729 * It is added to LRU before charge.
730 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
731 * When moving account, the page is not on LRU. It's isolated.
732 */
733
734void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800735{
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800736 struct page_cgroup *pc;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800737 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700738
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800739 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800740 return;
741 pc = lookup_page_cgroup(page);
742 /* can happen while we handle swapcache. */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700743 if (!TestClearPageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800744 return;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700745 VM_BUG_ON(!pc->mem_cgroup);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800746 /*
747 * We don't check PCG_USED bit. It's cleared when the "page" is finally
748 * removed from global LRU.
749 */
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800750 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700751 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700752 if (mem_cgroup_is_root(pc->mem_cgroup))
753 return;
754 VM_BUG_ON(list_empty(&pc->lru));
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800755 list_del_init(&pc->lru);
756 return;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800757}
758
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800759void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800760{
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800761 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800762}
763
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800764void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800765{
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800766 struct mem_cgroup_per_zone *mz;
767 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800768
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800769 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700770 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700771
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800772 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800773 /*
774 * Used bit is set without atomic ops but after smp_wmb().
775 * For making pc->mem_cgroup visible, insert smp_rmb() here.
776 */
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800777 smp_rmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -0700778 /* unused or root page is not rotated. */
779 if (!PageCgroupUsed(pc) || mem_cgroup_is_root(pc->mem_cgroup))
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800780 return;
781 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700782 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800783}
784
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800785void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
786{
787 struct page_cgroup *pc;
788 struct mem_cgroup_per_zone *mz;
789
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800790 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800791 return;
792 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700793 VM_BUG_ON(PageCgroupAcctLRU(pc));
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800794 /*
795 * Used bit is set without atomic ops but after smp_wmb().
796 * For making pc->mem_cgroup visible, insert smp_rmb() here.
797 */
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800798 smp_rmb();
799 if (!PageCgroupUsed(pc))
800 return;
801
802 mz = page_cgroup_zoneinfo(pc);
803 MEM_CGROUP_ZSTAT(mz, lru) += 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700804 SetPageCgroupAcctLRU(pc);
805 if (mem_cgroup_is_root(pc->mem_cgroup))
806 return;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800807 list_add(&pc->lru, &mz->lists[lru]);
808}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800809
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800810/*
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800811 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
812 * lru because the page may.be reused after it's fully uncharged (because of
813 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
814 * it again. This function is only used to charge SwapCache. It's done under
815 * lock_page and expected that zone->lru_lock is never held.
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800816 */
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800817static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800818{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800819 unsigned long flags;
820 struct zone *zone = page_zone(page);
821 struct page_cgroup *pc = lookup_page_cgroup(page);
822
823 spin_lock_irqsave(&zone->lru_lock, flags);
824 /*
825 * Forget old LRU when this page_cgroup is *not* used. This Used bit
826 * is guarded by lock_page() because the page is SwapCache.
827 */
828 if (!PageCgroupUsed(pc))
829 mem_cgroup_del_lru_list(page, page_lru(page));
830 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800831}
832
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800833static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
834{
835 unsigned long flags;
836 struct zone *zone = page_zone(page);
837 struct page_cgroup *pc = lookup_page_cgroup(page);
838
839 spin_lock_irqsave(&zone->lru_lock, flags);
840 /* link when the page is linked to LRU but page_cgroup isn't */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700841 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800842 mem_cgroup_add_lru_list(page, page_lru(page));
843 spin_unlock_irqrestore(&zone->lru_lock, flags);
844}
845
846
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800847void mem_cgroup_move_lists(struct page *page,
848 enum lru_list from, enum lru_list to)
849{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800850 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800851 return;
852 mem_cgroup_del_lru_list(page, from);
853 mem_cgroup_add_lru_list(page, to);
854}
855
David Rientjes4c4a2212008-02-07 00:14:06 -0800856int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
857{
858 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700859 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -0700860 struct task_struct *p;
David Rientjes4c4a2212008-02-07 00:14:06 -0800861
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -0700862 p = find_lock_task_mm(task);
863 if (!p)
864 return 0;
865 curr = try_get_mem_cgroup_from_mm(p->mm);
866 task_unlock(p);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700867 if (!curr)
868 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -0800869 /*
870 * We should check use_hierarchy of "mem" not "curr". Because checking
871 * use_hierarchy of "curr" here make this function true if hierarchy is
872 * enabled in "curr" and "curr" is a child of "mem" in *cgroup*
873 * hierarchy(even if use_hierarchy is disabled in "mem").
874 */
875 if (mem->use_hierarchy)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700876 ret = css_is_ancestor(&curr->css, &mem->css);
877 else
878 ret = (curr == mem);
879 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -0800880 return ret;
881}
882
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800883static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800884{
885 unsigned long active;
886 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800887 unsigned long gb;
888 unsigned long inactive_ratio;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800889
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700890 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
891 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800892
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800893 gb = (inactive + active) >> (30 - PAGE_SHIFT);
894 if (gb)
895 inactive_ratio = int_sqrt(10 * gb);
896 else
897 inactive_ratio = 1;
898
899 if (present_pages) {
900 present_pages[0] = inactive;
901 present_pages[1] = active;
902 }
903
904 return inactive_ratio;
905}
906
907int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
908{
909 unsigned long active;
910 unsigned long inactive;
911 unsigned long present_pages[2];
912 unsigned long inactive_ratio;
913
914 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
915
916 inactive = present_pages[0];
917 active = present_pages[1];
918
919 if (inactive * inactive_ratio < active)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800920 return 1;
921
922 return 0;
923}
924
Rik van Riel56e49d22009-06-16 15:32:28 -0700925int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
926{
927 unsigned long active;
928 unsigned long inactive;
929
930 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
931 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
932
933 return (active > inactive);
934}
935
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800936unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
937 struct zone *zone,
938 enum lru_list lru)
939{
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -0700940 int nid = zone_to_nid(zone);
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800941 int zid = zone_idx(zone);
942 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
943
944 return MEM_CGROUP_ZSTAT(mz, lru);
945}
946
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800947struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
948 struct zone *zone)
949{
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -0700950 int nid = zone_to_nid(zone);
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800951 int zid = zone_idx(zone);
952 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
953
954 return &mz->reclaim_stat;
955}
956
957struct zone_reclaim_stat *
958mem_cgroup_get_reclaim_stat_from_page(struct page *page)
959{
960 struct page_cgroup *pc;
961 struct mem_cgroup_per_zone *mz;
962
963 if (mem_cgroup_disabled())
964 return NULL;
965
966 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800967 /*
968 * Used bit is set without atomic ops but after smp_wmb().
969 * For making pc->mem_cgroup visible, insert smp_rmb() here.
970 */
971 smp_rmb();
972 if (!PageCgroupUsed(pc))
973 return NULL;
974
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800975 mz = page_cgroup_zoneinfo(pc);
976 if (!mz)
977 return NULL;
978
979 return &mz->reclaim_stat;
980}
981
Balbir Singh66e17072008-02-07 00:13:56 -0800982unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
983 struct list_head *dst,
984 unsigned long *scanned, int order,
985 int mode, struct zone *z,
986 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700987 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -0800988{
989 unsigned long nr_taken = 0;
990 struct page *page;
991 unsigned long scan;
992 LIST_HEAD(pc_list);
993 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800994 struct page_cgroup *pc, *tmp;
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -0700995 int nid = zone_to_nid(z);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800996 int zid = zone_idx(z);
997 struct mem_cgroup_per_zone *mz;
Johannes Weinerb7c46d12009-09-21 17:02:56 -0700998 int lru = LRU_FILE * file + active;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700999 int ret;
Balbir Singh66e17072008-02-07 00:13:56 -08001000
Balbir Singhcf475ad2008-04-29 01:00:16 -07001001 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001002 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001003 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -08001004
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001005 scan = 0;
1006 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -08001007 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001008 break;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001009
1010 page = pc->page;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001011 if (unlikely(!PageCgroupUsed(pc)))
1012 continue;
Hugh Dickins436c65412008-02-07 00:14:12 -08001013 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001014 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001015
Hugh Dickins436c65412008-02-07 00:14:12 -08001016 scan++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001017 ret = __isolate_lru_page(page, mode, file);
1018 switch (ret) {
1019 case 0:
Balbir Singh66e17072008-02-07 00:13:56 -08001020 list_move(&page->lru, dst);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001021 mem_cgroup_del_lru(page);
Balbir Singh66e17072008-02-07 00:13:56 -08001022 nr_taken++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001023 break;
1024 case -EBUSY:
1025 /* we don't affect global LRU but rotate in our LRU */
1026 mem_cgroup_rotate_lru_list(page, page_lru(page));
1027 break;
1028 default:
1029 break;
Balbir Singh66e17072008-02-07 00:13:56 -08001030 }
1031 }
1032
Balbir Singh66e17072008-02-07 00:13:56 -08001033 *scanned = scan;
KOSAKI Motohirocc8e9702010-08-09 17:19:57 -07001034
1035 trace_mm_vmscan_memcg_isolate(0, nr_to_scan, scan, nr_taken,
1036 0, 0, 0, mode);
1037
Balbir Singh66e17072008-02-07 00:13:56 -08001038 return nr_taken;
1039}
1040
Balbir Singh6d61ef42009-01-07 18:08:06 -08001041#define mem_cgroup_from_res_counter(counter, member) \
1042 container_of(counter, struct mem_cgroup, member)
1043
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -08001044static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
1045{
1046 if (do_swap_account) {
1047 if (res_counter_check_under_limit(&mem->res) &&
1048 res_counter_check_under_limit(&mem->memsw))
1049 return true;
1050 } else
1051 if (res_counter_check_under_limit(&mem->res))
1052 return true;
1053 return false;
1054}
1055
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001056static unsigned int get_swappiness(struct mem_cgroup *memcg)
1057{
1058 struct cgroup *cgrp = memcg->css.cgroup;
1059 unsigned int swappiness;
1060
1061 /* root ? */
1062 if (cgrp->parent == NULL)
1063 return vm_swappiness;
1064
1065 spin_lock(&memcg->reclaim_param_lock);
1066 swappiness = memcg->swappiness;
1067 spin_unlock(&memcg->reclaim_param_lock);
1068
1069 return swappiness;
1070}
1071
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001072static void mem_cgroup_start_move(struct mem_cgroup *mem)
1073{
1074 int cpu;
1075 /* Because this is for moving account, reuse mc.lock */
1076 spin_lock(&mc.lock);
1077 for_each_possible_cpu(cpu)
1078 per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) += 1;
1079 spin_unlock(&mc.lock);
1080
1081 synchronize_rcu();
1082}
1083
1084static void mem_cgroup_end_move(struct mem_cgroup *mem)
1085{
1086 int cpu;
1087
1088 if (!mem)
1089 return;
1090 spin_lock(&mc.lock);
1091 for_each_possible_cpu(cpu)
1092 per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) -= 1;
1093 spin_unlock(&mc.lock);
1094}
1095/*
1096 * 2 routines for checking "mem" is under move_account() or not.
1097 *
1098 * mem_cgroup_stealed() - checking a cgroup is mc.from or not. This is used
1099 * for avoiding race in accounting. If true,
1100 * pc->mem_cgroup may be overwritten.
1101 *
1102 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1103 * under hierarchy of moving cgroups. This is for
1104 * waiting at hith-memory prressure caused by "move".
1105 */
1106
1107static bool mem_cgroup_stealed(struct mem_cgroup *mem)
1108{
1109 VM_BUG_ON(!rcu_read_lock_held());
1110 return this_cpu_read(mem->stat->count[MEM_CGROUP_ON_MOVE]) > 0;
1111}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001112
1113static bool mem_cgroup_under_move(struct mem_cgroup *mem)
1114{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001115 struct mem_cgroup *from;
1116 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001117 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001118 /*
1119 * Unlike task_move routines, we access mc.to, mc.from not under
1120 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1121 */
1122 spin_lock(&mc.lock);
1123 from = mc.from;
1124 to = mc.to;
1125 if (!from)
1126 goto unlock;
1127 if (from == mem || to == mem
1128 || (mem->use_hierarchy && css_is_ancestor(&from->css, &mem->css))
1129 || (mem->use_hierarchy && css_is_ancestor(&to->css, &mem->css)))
1130 ret = true;
1131unlock:
1132 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001133 return ret;
1134}
1135
1136static bool mem_cgroup_wait_acct_move(struct mem_cgroup *mem)
1137{
1138 if (mc.moving_task && current != mc.moving_task) {
1139 if (mem_cgroup_under_move(mem)) {
1140 DEFINE_WAIT(wait);
1141 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1142 /* moving charge context might have finished. */
1143 if (mc.moving_task)
1144 schedule();
1145 finish_wait(&mc.waitq, &wait);
1146 return true;
1147 }
1148 }
1149 return false;
1150}
1151
Balbir Singhe2224322009-04-02 16:57:39 -07001152/**
Kirill A. Shutemov6a6135b2010-03-10 15:22:25 -08001153 * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode.
Balbir Singhe2224322009-04-02 16:57:39 -07001154 * @memcg: The memory cgroup that went over limit
1155 * @p: Task that is going to be killed
1156 *
1157 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1158 * enabled
1159 */
1160void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1161{
1162 struct cgroup *task_cgrp;
1163 struct cgroup *mem_cgrp;
1164 /*
1165 * Need a buffer in BSS, can't rely on allocations. The code relies
1166 * on the assumption that OOM is serialized for memory controller.
1167 * If this assumption is broken, revisit this code.
1168 */
1169 static char memcg_name[PATH_MAX];
1170 int ret;
1171
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001172 if (!memcg || !p)
Balbir Singhe2224322009-04-02 16:57:39 -07001173 return;
1174
1175
1176 rcu_read_lock();
1177
1178 mem_cgrp = memcg->css.cgroup;
1179 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1180
1181 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1182 if (ret < 0) {
1183 /*
1184 * Unfortunately, we are unable to convert to a useful name
1185 * But we'll still print out the usage information
1186 */
1187 rcu_read_unlock();
1188 goto done;
1189 }
1190 rcu_read_unlock();
1191
1192 printk(KERN_INFO "Task in %s killed", memcg_name);
1193
1194 rcu_read_lock();
1195 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1196 if (ret < 0) {
1197 rcu_read_unlock();
1198 goto done;
1199 }
1200 rcu_read_unlock();
1201
1202 /*
1203 * Continues from above, so we don't need an KERN_ level
1204 */
1205 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1206done:
1207
1208 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1209 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1210 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1211 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1212 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1213 "failcnt %llu\n",
1214 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1215 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1216 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1217}
1218
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001219/*
1220 * This function returns the number of memcg under hierarchy tree. Returns
1221 * 1(self count) if no children.
1222 */
1223static int mem_cgroup_count_children(struct mem_cgroup *mem)
1224{
1225 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001226 struct mem_cgroup *iter;
1227
1228 for_each_mem_cgroup_tree(iter, mem)
1229 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001230 return num;
1231}
1232
Balbir Singh6d61ef42009-01-07 18:08:06 -08001233/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001234 * Return the memory (and swap, if configured) limit for a memcg.
1235 */
1236u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
1237{
1238 u64 limit;
1239 u64 memsw;
1240
1241 limit = res_counter_read_u64(&memcg->res, RES_LIMIT) +
1242 total_swap_pages;
1243 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1244 /*
1245 * If memsw is finite and limits the amount of swap space available
1246 * to this memcg, return that limit.
1247 */
1248 return min(limit, memsw);
1249}
1250
1251/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001252 * Visit the first child (need not be the first child as per the ordering
1253 * of the cgroup list, since we track last_scanned_child) of @mem and use
1254 * that to reclaim free pages from.
1255 */
1256static struct mem_cgroup *
1257mem_cgroup_select_victim(struct mem_cgroup *root_mem)
1258{
1259 struct mem_cgroup *ret = NULL;
1260 struct cgroup_subsys_state *css;
1261 int nextid, found;
1262
1263 if (!root_mem->use_hierarchy) {
1264 css_get(&root_mem->css);
1265 ret = root_mem;
1266 }
1267
1268 while (!ret) {
1269 rcu_read_lock();
1270 nextid = root_mem->last_scanned_child + 1;
1271 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
1272 &found);
1273 if (css && css_tryget(css))
1274 ret = container_of(css, struct mem_cgroup, css);
1275
1276 rcu_read_unlock();
1277 /* Updates scanning parameter */
1278 spin_lock(&root_mem->reclaim_param_lock);
1279 if (!css) {
1280 /* this means start scan from ID:1 */
1281 root_mem->last_scanned_child = 0;
1282 } else
1283 root_mem->last_scanned_child = found;
1284 spin_unlock(&root_mem->reclaim_param_lock);
1285 }
1286
1287 return ret;
1288}
1289
1290/*
1291 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1292 * we reclaimed from, so that we don't end up penalizing one child extensively
1293 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001294 *
1295 * root_mem is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001296 *
1297 * We give up and return to the caller when we visit root_mem twice.
1298 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001299 *
1300 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001301 */
1302static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
Balbir Singh4e416952009-09-23 15:56:39 -07001303 struct zone *zone,
Balbir Singh75822b42009-09-23 15:56:38 -07001304 gfp_t gfp_mask,
1305 unsigned long reclaim_options)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001306{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001307 struct mem_cgroup *victim;
1308 int ret, total = 0;
1309 int loop = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001310 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1311 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
Balbir Singh4e416952009-09-23 15:56:39 -07001312 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
1313 unsigned long excess = mem_cgroup_get_excess(root_mem);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001314
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001315 /* If memsw_is_minimum==1, swap-out is of-no-use. */
1316 if (root_mem->memsw_is_minimum)
1317 noswap = true;
1318
Balbir Singh4e416952009-09-23 15:56:39 -07001319 while (1) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001320 victim = mem_cgroup_select_victim(root_mem);
Balbir Singh4e416952009-09-23 15:56:39 -07001321 if (victim == root_mem) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001322 loop++;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001323 if (loop >= 1)
1324 drain_all_stock_async();
Balbir Singh4e416952009-09-23 15:56:39 -07001325 if (loop >= 2) {
1326 /*
1327 * If we have not been able to reclaim
1328 * anything, it might because there are
1329 * no reclaimable pages under this hierarchy
1330 */
1331 if (!check_soft || !total) {
1332 css_put(&victim->css);
1333 break;
1334 }
1335 /*
1336 * We want to do more targetted reclaim.
1337 * excess >> 2 is not to excessive so as to
1338 * reclaim too much, nor too less that we keep
1339 * coming back to reclaim from this cgroup
1340 */
1341 if (total >= (excess >> 2) ||
1342 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1343 css_put(&victim->css);
1344 break;
1345 }
1346 }
1347 }
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08001348 if (!mem_cgroup_local_usage(victim)) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001349 /* this cgroup's local usage == 0 */
1350 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001351 continue;
1352 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001353 /* we use swappiness of local cgroup */
Balbir Singh4e416952009-09-23 15:56:39 -07001354 if (check_soft)
1355 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
KOSAKI Motohiro14fec792010-08-10 18:03:05 -07001356 noswap, get_swappiness(victim), zone);
Balbir Singh4e416952009-09-23 15:56:39 -07001357 else
1358 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
1359 noswap, get_swappiness(victim));
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001360 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001361 /*
1362 * At shrinking usage, we can't check we should stop here or
1363 * reclaim more. It's depends on callers. last_scanned_child
1364 * will work enough for keeping fairness under tree.
1365 */
1366 if (shrink)
1367 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001368 total += ret;
Balbir Singh4e416952009-09-23 15:56:39 -07001369 if (check_soft) {
1370 if (res_counter_check_under_soft_limit(&root_mem->res))
1371 return total;
1372 } else if (mem_cgroup_check_under_limit(root_mem))
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001373 return 1 + total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001374 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001375 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001376}
1377
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001378/*
1379 * Check OOM-Killer is already running under our hierarchy.
1380 * If someone is running, return false.
1381 */
1382static bool mem_cgroup_oom_lock(struct mem_cgroup *mem)
1383{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001384 int x, lock_count = 0;
1385 struct mem_cgroup *iter;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001386
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001387 for_each_mem_cgroup_tree(iter, mem) {
1388 x = atomic_inc_return(&iter->oom_lock);
1389 lock_count = max(x, lock_count);
1390 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001391
1392 if (lock_count == 1)
1393 return true;
1394 return false;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001395}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001396
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001397static int mem_cgroup_oom_unlock(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001398{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001399 struct mem_cgroup *iter;
1400
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001401 /*
1402 * When a new child is created while the hierarchy is under oom,
1403 * mem_cgroup_oom_lock() may not be called. We have to use
1404 * atomic_add_unless() here.
1405 */
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001406 for_each_mem_cgroup_tree(iter, mem)
1407 atomic_add_unless(&iter->oom_lock, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001408 return 0;
1409}
1410
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001411
1412static DEFINE_MUTEX(memcg_oom_mutex);
1413static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1414
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001415struct oom_wait_info {
1416 struct mem_cgroup *mem;
1417 wait_queue_t wait;
1418};
1419
1420static int memcg_oom_wake_function(wait_queue_t *wait,
1421 unsigned mode, int sync, void *arg)
1422{
1423 struct mem_cgroup *wake_mem = (struct mem_cgroup *)arg;
1424 struct oom_wait_info *oom_wait_info;
1425
1426 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
1427
1428 if (oom_wait_info->mem == wake_mem)
1429 goto wakeup;
1430 /* if no hierarchy, no match */
1431 if (!oom_wait_info->mem->use_hierarchy || !wake_mem->use_hierarchy)
1432 return 0;
1433 /*
1434 * Both of oom_wait_info->mem and wake_mem are stable under us.
1435 * Then we can use css_is_ancestor without taking care of RCU.
1436 */
1437 if (!css_is_ancestor(&oom_wait_info->mem->css, &wake_mem->css) &&
1438 !css_is_ancestor(&wake_mem->css, &oom_wait_info->mem->css))
1439 return 0;
1440
1441wakeup:
1442 return autoremove_wake_function(wait, mode, sync, arg);
1443}
1444
1445static void memcg_wakeup_oom(struct mem_cgroup *mem)
1446{
1447 /* for filtering, pass "mem" as argument. */
1448 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, mem);
1449}
1450
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001451static void memcg_oom_recover(struct mem_cgroup *mem)
1452{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001453 if (mem && atomic_read(&mem->oom_lock))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001454 memcg_wakeup_oom(mem);
1455}
1456
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001457/*
1458 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
1459 */
1460bool mem_cgroup_handle_oom(struct mem_cgroup *mem, gfp_t mask)
1461{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001462 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001463 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001464
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001465 owait.mem = mem;
1466 owait.wait.flags = 0;
1467 owait.wait.func = memcg_oom_wake_function;
1468 owait.wait.private = current;
1469 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001470 need_to_kill = true;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001471 /* At first, try to OOM lock hierarchy under mem.*/
1472 mutex_lock(&memcg_oom_mutex);
1473 locked = mem_cgroup_oom_lock(mem);
1474 /*
1475 * Even if signal_pending(), we can't quit charge() loop without
1476 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
1477 * under OOM is always welcomed, use TASK_KILLABLE here.
1478 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001479 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
1480 if (!locked || mem->oom_kill_disable)
1481 need_to_kill = false;
1482 if (locked)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07001483 mem_cgroup_oom_notify(mem);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001484 mutex_unlock(&memcg_oom_mutex);
1485
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001486 if (need_to_kill) {
1487 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001488 mem_cgroup_out_of_memory(mem, mask);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001489 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001490 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001491 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001492 }
1493 mutex_lock(&memcg_oom_mutex);
1494 mem_cgroup_oom_unlock(mem);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001495 memcg_wakeup_oom(mem);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001496 mutex_unlock(&memcg_oom_mutex);
1497
1498 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
1499 return false;
1500 /* Give chance to dying process */
1501 schedule_timeout(1);
1502 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001503}
1504
Balbir Singhd69b0422009-06-17 16:26:34 -07001505/*
1506 * Currently used to update mapped file statistics, but the routine can be
1507 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001508 *
1509 * Notes: Race condition
1510 *
1511 * We usually use page_cgroup_lock() for accessing page_cgroup member but
1512 * it tends to be costly. But considering some conditions, we doesn't need
1513 * to do so _always_.
1514 *
1515 * Considering "charge", lock_page_cgroup() is not required because all
1516 * file-stat operations happen after a page is attached to radix-tree. There
1517 * are no race with "charge".
1518 *
1519 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
1520 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
1521 * if there are race with "uncharge". Statistics itself is properly handled
1522 * by flags.
1523 *
1524 * Considering "move", this is an only case we see a race. To make the race
1525 * small, we check MEM_CGROUP_ON_MOVE percpu value and detect there are
1526 * possibility of race condition. If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07001527 */
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001528void mem_cgroup_update_file_mapped(struct page *page, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07001529{
1530 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001531 struct page_cgroup *pc = lookup_page_cgroup(page);
1532 bool need_unlock = false;
Balbir Singhd69b0422009-06-17 16:26:34 -07001533
Balbir Singhd69b0422009-06-17 16:26:34 -07001534 if (unlikely(!pc))
1535 return;
1536
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001537 rcu_read_lock();
Balbir Singhd69b0422009-06-17 16:26:34 -07001538 mem = pc->mem_cgroup;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001539 if (unlikely(!mem || !PageCgroupUsed(pc)))
1540 goto out;
1541 /* pc->mem_cgroup is unstable ? */
1542 if (unlikely(mem_cgroup_stealed(mem))) {
1543 /* take a lock against to access pc->mem_cgroup */
1544 lock_page_cgroup(pc);
1545 need_unlock = true;
1546 mem = pc->mem_cgroup;
1547 if (!mem || !PageCgroupUsed(pc))
1548 goto out;
1549 }
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07001550 if (val > 0) {
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001551 this_cpu_inc(mem->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07001552 SetPageCgroupFileMapped(pc);
1553 } else {
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001554 this_cpu_dec(mem->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
KAMEZAWA Hiroyuki0c270f82010-10-27 15:33:39 -07001555 if (!page_mapped(page)) /* for race between dec->inc counter */
1556 ClearPageCgroupFileMapped(pc);
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07001557 }
Balbir Singhd69b0422009-06-17 16:26:34 -07001558
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001559out:
1560 if (unlikely(need_unlock))
1561 unlock_page_cgroup(pc);
1562 rcu_read_unlock();
1563 return;
Balbir Singhd69b0422009-06-17 16:26:34 -07001564}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001565
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001566/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001567 * size of first charge trial. "32" comes from vmscan.c's magic value.
1568 * TODO: maybe necessary to use big numbers in big irons.
1569 */
1570#define CHARGE_SIZE (32 * PAGE_SIZE)
1571struct memcg_stock_pcp {
1572 struct mem_cgroup *cached; /* this never be root cgroup */
1573 int charge;
1574 struct work_struct work;
1575};
1576static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
1577static atomic_t memcg_drain_count;
1578
1579/*
1580 * Try to consume stocked charge on this cpu. If success, PAGE_SIZE is consumed
1581 * from local stock and true is returned. If the stock is 0 or charges from a
1582 * cgroup which is not current target, returns false. This stock will be
1583 * refilled.
1584 */
1585static bool consume_stock(struct mem_cgroup *mem)
1586{
1587 struct memcg_stock_pcp *stock;
1588 bool ret = true;
1589
1590 stock = &get_cpu_var(memcg_stock);
1591 if (mem == stock->cached && stock->charge)
1592 stock->charge -= PAGE_SIZE;
1593 else /* need to call res_counter_charge */
1594 ret = false;
1595 put_cpu_var(memcg_stock);
1596 return ret;
1597}
1598
1599/*
1600 * Returns stocks cached in percpu to res_counter and reset cached information.
1601 */
1602static void drain_stock(struct memcg_stock_pcp *stock)
1603{
1604 struct mem_cgroup *old = stock->cached;
1605
1606 if (stock->charge) {
1607 res_counter_uncharge(&old->res, stock->charge);
1608 if (do_swap_account)
1609 res_counter_uncharge(&old->memsw, stock->charge);
1610 }
1611 stock->cached = NULL;
1612 stock->charge = 0;
1613}
1614
1615/*
1616 * This must be called under preempt disabled or must be called by
1617 * a thread which is pinned to local cpu.
1618 */
1619static void drain_local_stock(struct work_struct *dummy)
1620{
1621 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
1622 drain_stock(stock);
1623}
1624
1625/*
1626 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01001627 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001628 */
1629static void refill_stock(struct mem_cgroup *mem, int val)
1630{
1631 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
1632
1633 if (stock->cached != mem) { /* reset if necessary */
1634 drain_stock(stock);
1635 stock->cached = mem;
1636 }
1637 stock->charge += val;
1638 put_cpu_var(memcg_stock);
1639}
1640
1641/*
1642 * Tries to drain stocked charges in other cpus. This function is asynchronous
1643 * and just put a work per cpu for draining localy on each cpu. Caller can
1644 * expects some charges will be back to res_counter later but cannot wait for
1645 * it.
1646 */
1647static void drain_all_stock_async(void)
1648{
1649 int cpu;
1650 /* This function is for scheduling "drain" in asynchronous way.
1651 * The result of "drain" is not directly handled by callers. Then,
1652 * if someone is calling drain, we don't have to call drain more.
1653 * Anyway, WORK_STRUCT_PENDING check in queue_work_on() will catch if
1654 * there is a race. We just do loose check here.
1655 */
1656 if (atomic_read(&memcg_drain_count))
1657 return;
1658 /* Notify other cpus that system-wide "drain" is running */
1659 atomic_inc(&memcg_drain_count);
1660 get_online_cpus();
1661 for_each_online_cpu(cpu) {
1662 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
1663 schedule_work_on(cpu, &stock->work);
1664 }
1665 put_online_cpus();
1666 atomic_dec(&memcg_drain_count);
1667 /* We don't wait for flush_work */
1668}
1669
1670/* This is a synchronous drain interface. */
1671static void drain_all_stock_sync(void)
1672{
1673 /* called when force_empty is called */
1674 atomic_inc(&memcg_drain_count);
1675 schedule_on_each_cpu(drain_local_stock);
1676 atomic_dec(&memcg_drain_count);
1677}
1678
1679static int __cpuinit memcg_stock_cpu_callback(struct notifier_block *nb,
1680 unsigned long action,
1681 void *hcpu)
1682{
1683 int cpu = (unsigned long)hcpu;
1684 struct memcg_stock_pcp *stock;
1685
1686 if (action != CPU_DEAD)
1687 return NOTIFY_OK;
1688 stock = &per_cpu(memcg_stock, cpu);
1689 drain_stock(stock);
1690 return NOTIFY_OK;
1691}
1692
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001693
1694/* See __mem_cgroup_try_charge() for details */
1695enum {
1696 CHARGE_OK, /* success */
1697 CHARGE_RETRY, /* need to retry but retry is not bad */
1698 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
1699 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
1700 CHARGE_OOM_DIE, /* the current is killed because of OOM */
1701};
1702
1703static int __mem_cgroup_do_charge(struct mem_cgroup *mem, gfp_t gfp_mask,
1704 int csize, bool oom_check)
1705{
1706 struct mem_cgroup *mem_over_limit;
1707 struct res_counter *fail_res;
1708 unsigned long flags = 0;
1709 int ret;
1710
1711 ret = res_counter_charge(&mem->res, csize, &fail_res);
1712
1713 if (likely(!ret)) {
1714 if (!do_swap_account)
1715 return CHARGE_OK;
1716 ret = res_counter_charge(&mem->memsw, csize, &fail_res);
1717 if (likely(!ret))
1718 return CHARGE_OK;
1719
1720 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
1721 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
1722 } else
1723 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
1724
1725 if (csize > PAGE_SIZE) /* change csize and retry */
1726 return CHARGE_RETRY;
1727
1728 if (!(gfp_mask & __GFP_WAIT))
1729 return CHARGE_WOULDBLOCK;
1730
1731 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
1732 gfp_mask, flags);
1733 /*
1734 * try_to_free_mem_cgroup_pages() might not give us a full
1735 * picture of reclaim. Some pages are reclaimed and might be
1736 * moved to swap cache or just unmapped from the cgroup.
1737 * Check the limit again to see if the reclaim reduced the
1738 * current usage of the cgroup before giving up
1739 */
1740 if (ret || mem_cgroup_check_under_limit(mem_over_limit))
1741 return CHARGE_RETRY;
1742
1743 /*
1744 * At task move, charge accounts can be doubly counted. So, it's
1745 * better to wait until the end of task_move if something is going on.
1746 */
1747 if (mem_cgroup_wait_acct_move(mem_over_limit))
1748 return CHARGE_RETRY;
1749
1750 /* If we don't need to call oom-killer at el, return immediately */
1751 if (!oom_check)
1752 return CHARGE_NOMEM;
1753 /* check OOM */
1754 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask))
1755 return CHARGE_OOM_DIE;
1756
1757 return CHARGE_RETRY;
1758}
1759
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001760/*
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001761 * Unlike exported interface, "oom" parameter is added. if oom==true,
1762 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001763 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001764static int __mem_cgroup_try_charge(struct mm_struct *mm,
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001765 gfp_t gfp_mask, struct mem_cgroup **memcg, bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001766{
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001767 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
1768 struct mem_cgroup *mem = NULL;
1769 int ret;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001770 int csize = CHARGE_SIZE;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001771
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001772 /*
1773 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
1774 * in system level. So, allow to go ahead dying process in addition to
1775 * MEMDIE process.
1776 */
1777 if (unlikely(test_thread_flag(TIF_MEMDIE)
1778 || fatal_signal_pending(current)))
1779 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001780
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001781 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08001782 * We always charge the cgroup the mm_struct belongs to.
1783 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001784 * thread group leader migrates. It's possible that mm is not
1785 * set, if so charge the init_mm (happens for pagecache usage).
1786 */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001787 if (!*memcg && !mm)
1788 goto bypass;
1789again:
1790 if (*memcg) { /* css should be a valid one */
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001791 mem = *memcg;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001792 VM_BUG_ON(css_is_removed(&mem->css));
1793 if (mem_cgroup_is_root(mem))
1794 goto done;
1795 if (consume_stock(mem))
1796 goto done;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001797 css_get(&mem->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001798 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001799 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001800
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001801 rcu_read_lock();
1802 p = rcu_dereference(mm->owner);
1803 VM_BUG_ON(!p);
1804 /*
1805 * because we don't have task_lock(), "p" can exit while
1806 * we're here. In that case, "mem" can point to root
1807 * cgroup but never be NULL. (and task_struct itself is freed
1808 * by RCU, cgroup itself is RCU safe.) Then, we have small
1809 * risk here to get wrong cgroup. But such kind of mis-account
1810 * by race always happens because we don't have cgroup_mutex().
1811 * It's overkill and we allow that small race, here.
1812 */
1813 mem = mem_cgroup_from_task(p);
1814 VM_BUG_ON(!mem);
1815 if (mem_cgroup_is_root(mem)) {
1816 rcu_read_unlock();
1817 goto done;
1818 }
1819 if (consume_stock(mem)) {
1820 /*
1821 * It seems dagerous to access memcg without css_get().
1822 * But considering how consume_stok works, it's not
1823 * necessary. If consume_stock success, some charges
1824 * from this memcg are cached on this cpu. So, we
1825 * don't need to call css_get()/css_tryget() before
1826 * calling consume_stock().
1827 */
1828 rcu_read_unlock();
1829 goto done;
1830 }
1831 /* after here, we may be blocked. we need to get refcnt */
1832 if (!css_tryget(&mem->css)) {
1833 rcu_read_unlock();
1834 goto again;
1835 }
1836 rcu_read_unlock();
1837 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001838
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001839 do {
1840 bool oom_check;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001841
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001842 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001843 if (fatal_signal_pending(current)) {
1844 css_put(&mem->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001845 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001846 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001847
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001848 oom_check = false;
1849 if (oom && !nr_oom_retries) {
1850 oom_check = true;
1851 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
1852 }
Balbir Singh6d61ef42009-01-07 18:08:06 -08001853
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001854 ret = __mem_cgroup_do_charge(mem, gfp_mask, csize, oom_check);
1855
1856 switch (ret) {
1857 case CHARGE_OK:
1858 break;
1859 case CHARGE_RETRY: /* not in OOM situation but retry */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001860 csize = PAGE_SIZE;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001861 css_put(&mem->css);
1862 mem = NULL;
1863 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001864 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001865 css_put(&mem->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001866 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001867 case CHARGE_NOMEM: /* OOM routine works */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001868 if (!oom) {
1869 css_put(&mem->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001870 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001871 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001872 /* If oom, we never return -ENOMEM */
1873 nr_oom_retries--;
1874 break;
1875 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001876 css_put(&mem->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001877 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08001878 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001879 } while (ret != CHARGE_OK);
1880
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001881 if (csize > PAGE_SIZE)
1882 refill_stock(mem, csize - PAGE_SIZE);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001883 css_put(&mem->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001884done:
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001885 *memcg = mem;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001886 return 0;
1887nomem:
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07001888 *memcg = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001889 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001890bypass:
1891 *memcg = NULL;
1892 return 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001893}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001894
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001895/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001896 * Somemtimes we have to undo a charge we got by try_charge().
1897 * This function is for that and do uncharge, put css's refcnt.
1898 * gotten by try_charge().
1899 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001900static void __mem_cgroup_cancel_charge(struct mem_cgroup *mem,
1901 unsigned long count)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001902{
1903 if (!mem_cgroup_is_root(mem)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001904 res_counter_uncharge(&mem->res, PAGE_SIZE * count);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001905 if (do_swap_account)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001906 res_counter_uncharge(&mem->memsw, PAGE_SIZE * count);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001907 }
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001908}
1909
1910static void mem_cgroup_cancel_charge(struct mem_cgroup *mem)
1911{
1912 __mem_cgroup_cancel_charge(mem, 1);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001913}
1914
1915/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001916 * A helper function to get mem_cgroup from ID. must be called under
1917 * rcu_read_lock(). The caller must check css_is_removed() or some if
1918 * it's concern. (dropping refcnt from swap can be called against removed
1919 * memcg.)
1920 */
1921static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
1922{
1923 struct cgroup_subsys_state *css;
1924
1925 /* ID 0 is unused ID */
1926 if (!id)
1927 return NULL;
1928 css = css_lookup(&mem_cgroup_subsys, id);
1929 if (!css)
1930 return NULL;
1931 return container_of(css, struct mem_cgroup, css);
1932}
1933
Wu Fengguange42d9d52009-12-16 12:19:59 +01001934struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001935{
Wu Fengguange42d9d52009-12-16 12:19:59 +01001936 struct mem_cgroup *mem = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001937 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001938 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001939 swp_entry_t ent;
1940
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001941 VM_BUG_ON(!PageLocked(page));
1942
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001943 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001944 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001945 if (PageCgroupUsed(pc)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001946 mem = pc->mem_cgroup;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001947 if (mem && !css_tryget(&mem->css))
1948 mem = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01001949 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001950 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001951 id = lookup_swap_cgroup(ent);
1952 rcu_read_lock();
1953 mem = mem_cgroup_lookup(id);
1954 if (mem && !css_tryget(&mem->css))
1955 mem = NULL;
1956 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001957 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001958 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001959 return mem;
1960}
1961
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001962/*
Daisuke Nishimuraa5e924f2009-01-07 18:08:28 -08001963 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001964 * USED state. If already USED, uncharge and return.
1965 */
1966
1967static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1968 struct page_cgroup *pc,
1969 enum charge_type ctype)
1970{
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001971 /* try_charge() can return NULL to *memcg, taking care of it. */
1972 if (!mem)
1973 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001974
1975 lock_page_cgroup(pc);
1976 if (unlikely(PageCgroupUsed(pc))) {
1977 unlock_page_cgroup(pc);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001978 mem_cgroup_cancel_charge(mem);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001979 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001980 }
Balbir Singh4b3bde42009-09-23 15:56:32 -07001981
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001982 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07001983 /*
1984 * We access a page_cgroup asynchronously without lock_page_cgroup().
1985 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
1986 * is accessed after testing USED bit. To make pc->mem_cgroup visible
1987 * before USED bit, we need memory barrier here.
1988 * See mem_cgroup_add_lru_list(), etc.
1989 */
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001990 smp_wmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -07001991 switch (ctype) {
1992 case MEM_CGROUP_CHARGE_TYPE_CACHE:
1993 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
1994 SetPageCgroupCache(pc);
1995 SetPageCgroupUsed(pc);
1996 break;
1997 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1998 ClearPageCgroupCache(pc);
1999 SetPageCgroupUsed(pc);
2000 break;
2001 default:
2002 break;
2003 }
Hugh Dickins3be91272008-02-07 00:14:19 -08002004
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002005 mem_cgroup_charge_statistics(mem, pc, true);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002006
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002007 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002008 /*
2009 * "charge_statistics" updated event counter. Then, check it.
2010 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2011 * if they exceeds softlimit.
2012 */
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002013 memcg_check_events(mem, pc->page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002014}
2015
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002016/**
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002017 * __mem_cgroup_move_account - move account of the page
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002018 * @pc: page_cgroup of the page.
2019 * @from: mem_cgroup which the page is moved from.
2020 * @to: mem_cgroup which the page is moved to. @from != @to.
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002021 * @uncharge: whether we should call uncharge and css_put against @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002022 *
2023 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002024 * - page is not on LRU (isolate_page() is useful.)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002025 * - the pc is locked, used, and ->mem_cgroup points to @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002026 *
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002027 * This function doesn't do "charge" nor css_get to new cgroup. It should be
2028 * done by a caller(__mem_cgroup_try_charge would be usefull). If @uncharge is
2029 * true, this function does "uncharge" from old cgroup, but it doesn't if
2030 * @uncharge is false, so a caller should do "uncharge".
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002031 */
2032
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002033static void __mem_cgroup_move_account(struct page_cgroup *pc,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002034 struct mem_cgroup *from, struct mem_cgroup *to, bool uncharge)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002035{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002036 VM_BUG_ON(from == to);
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002037 VM_BUG_ON(PageLRU(pc->page));
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002038 VM_BUG_ON(!PageCgroupLocked(pc));
2039 VM_BUG_ON(!PageCgroupUsed(pc));
2040 VM_BUG_ON(pc->mem_cgroup != from);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002041
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002042 if (PageCgroupFileMapped(pc)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08002043 /* Update mapped_file data for mem_cgroup */
2044 preempt_disable();
2045 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2046 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2047 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07002048 }
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002049 mem_cgroup_charge_statistics(from, pc, false);
2050 if (uncharge)
2051 /* This is not "cancel", but cancel_charge does all we need. */
2052 mem_cgroup_cancel_charge(from);
Balbir Singhd69b0422009-06-17 16:26:34 -07002053
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002054 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002055 pc->mem_cgroup = to;
2056 mem_cgroup_charge_statistics(to, pc, true);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002057 /*
2058 * We charges against "to" which may not have any tasks. Then, "to"
2059 * can be under rmdir(). But in current implementation, caller of
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08002060 * this function is just force_empty() and move charge, so it's
2061 * garanteed that "to" is never removed. So, we don't check rmdir
2062 * status here.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002063 */
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002064}
2065
2066/*
2067 * check whether the @pc is valid for moving account and call
2068 * __mem_cgroup_move_account()
2069 */
2070static int mem_cgroup_move_account(struct page_cgroup *pc,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002071 struct mem_cgroup *from, struct mem_cgroup *to, bool uncharge)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002072{
2073 int ret = -EINVAL;
2074 lock_page_cgroup(pc);
2075 if (PageCgroupUsed(pc) && pc->mem_cgroup == from) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002076 __mem_cgroup_move_account(pc, from, to, uncharge);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002077 ret = 0;
2078 }
2079 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002080 /*
2081 * check events
2082 */
2083 memcg_check_events(to, pc->page);
2084 memcg_check_events(from, pc->page);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002085 return ret;
2086}
2087
2088/*
2089 * move charges to its parent.
2090 */
2091
2092static int mem_cgroup_move_parent(struct page_cgroup *pc,
2093 struct mem_cgroup *child,
2094 gfp_t gfp_mask)
2095{
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002096 struct page *page = pc->page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002097 struct cgroup *cg = child->css.cgroup;
2098 struct cgroup *pcg = cg->parent;
2099 struct mem_cgroup *parent;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002100 int ret;
2101
2102 /* Is ROOT ? */
2103 if (!pcg)
2104 return -EINVAL;
2105
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002106 ret = -EBUSY;
2107 if (!get_page_unless_zero(page))
2108 goto out;
2109 if (isolate_lru_page(page))
2110 goto put;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002111
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002112 parent = mem_cgroup_from_cont(pcg);
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002113 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002114 if (ret || !parent)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002115 goto put_back;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002116
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002117 ret = mem_cgroup_move_account(pc, child, parent, true);
2118 if (ret)
2119 mem_cgroup_cancel_charge(parent);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002120put_back:
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002121 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002122put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08002123 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002124out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002125 return ret;
2126}
2127
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002128/*
2129 * Charge the memory controller for page usage.
2130 * Return
2131 * 0 if the charge was successful
2132 * < 0 if the cgroup is over its limit
2133 */
2134static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002135 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002136{
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002137 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002138 struct page_cgroup *pc;
2139 int ret;
2140
2141 pc = lookup_page_cgroup(page);
2142 /* can happen at boot */
2143 if (unlikely(!pc))
2144 return 0;
2145 prefetchw(pc);
2146
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002147 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002148 if (ret || !mem)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002149 return ret;
2150
2151 __mem_cgroup_commit_charge(mem, pc, ctype);
2152 return 0;
2153}
2154
2155int mem_cgroup_newpage_charge(struct page *page,
2156 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002157{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002158 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002159 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002160 if (PageCompound(page))
2161 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002162 /*
2163 * If already mapped, we don't have to account.
2164 * If page cache, page->mapping has address_space.
2165 * But page->mapping may have out-of-use anon_vma pointer,
2166 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
2167 * is NULL.
2168 */
2169 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
2170 return 0;
2171 if (unlikely(!mm))
2172 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002173 return mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002174 MEM_CGROUP_CHARGE_TYPE_MAPPED);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002175}
2176
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002177static void
2178__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2179 enum charge_type ctype);
2180
Balbir Singhe1a1cd52008-02-07 00:14:02 -08002181int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
2182 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08002183{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002184 int ret;
2185
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002186 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002187 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002188 if (PageCompound(page))
2189 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002190 /*
2191 * Corner case handling. This is called from add_to_page_cache()
2192 * in usual. But some FS (shmem) precharges this page before calling it
2193 * and call add_to_page_cache() with GFP_NOWAIT.
2194 *
2195 * For GFP_NOWAIT case, the page may be pre-charged before calling
2196 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
2197 * charge twice. (It works but has to pay a bit larger cost.)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002198 * And when the page is SwapCache, it should take swap information
2199 * into account. This is under lock_page() now.
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002200 */
2201 if (!(gfp_mask & __GFP_WAIT)) {
2202 struct page_cgroup *pc;
2203
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002204 pc = lookup_page_cgroup(page);
2205 if (!pc)
2206 return 0;
2207 lock_page_cgroup(pc);
2208 if (PageCgroupUsed(pc)) {
2209 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002210 return 0;
2211 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002212 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002213 }
2214
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002215 if (unlikely(!mm))
Balbir Singh8697d332008-02-07 00:13:59 -08002216 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002217
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -07002218 if (page_is_file_cache(page))
2219 return mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002220 MEM_CGROUP_CHARGE_TYPE_CACHE);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002221
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002222 /* shmem */
2223 if (PageSwapCache(page)) {
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002224 struct mem_cgroup *mem = NULL;
2225
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002226 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2227 if (!ret)
2228 __mem_cgroup_commit_charge_swapin(page, mem,
2229 MEM_CGROUP_CHARGE_TYPE_SHMEM);
2230 } else
2231 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002232 MEM_CGROUP_CHARGE_TYPE_SHMEM);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002233
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002234 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002235}
2236
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002237/*
2238 * While swap-in, try_charge -> commit or cancel, the page is locked.
2239 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02002240 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002241 * "commit()" or removed by "cancel()"
2242 */
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002243int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
2244 struct page *page,
2245 gfp_t mask, struct mem_cgroup **ptr)
2246{
2247 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002248 int ret;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002249
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002250 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002251 return 0;
2252
2253 if (!do_swap_account)
2254 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002255 /*
2256 * A racing thread's fault, or swapoff, may have already updated
Hugh Dickins407f9c82009-12-14 17:59:30 -08002257 * the pte, and even removed page from swap cache: in those cases
2258 * do_swap_page()'s pte_same() test will fail; but there's also a
2259 * KSM case which does need to charge the page.
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002260 */
2261 if (!PageSwapCache(page))
Hugh Dickins407f9c82009-12-14 17:59:30 -08002262 goto charge_cur_mm;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002263 mem = try_get_mem_cgroup_from_page(page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002264 if (!mem)
2265 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002266 *ptr = mem;
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002267 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002268 css_put(&mem->css);
2269 return ret;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002270charge_cur_mm:
2271 if (unlikely(!mm))
2272 mm = &init_mm;
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002273 return __mem_cgroup_try_charge(mm, mask, ptr, true);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002274}
2275
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002276static void
2277__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2278 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002279{
2280 struct page_cgroup *pc;
2281
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002282 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002283 return;
2284 if (!ptr)
2285 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002286 cgroup_exclude_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002287 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08002288 mem_cgroup_lru_del_before_commit_swapcache(page);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002289 __mem_cgroup_commit_charge(ptr, pc, ctype);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08002290 mem_cgroup_lru_add_after_commit_swapcache(page);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002291 /*
2292 * Now swap is on-memory. This means this page may be
2293 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002294 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
2295 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
2296 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002297 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002298 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002299 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002300 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002301 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002302
2303 id = swap_cgroup_record(ent, 0);
2304 rcu_read_lock();
2305 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002306 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002307 /*
2308 * This recorded memcg can be obsolete one. So, avoid
2309 * calling css_tryget
2310 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002311 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002312 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002313 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002314 mem_cgroup_put(memcg);
2315 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002316 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002317 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002318 /*
2319 * At swapin, we may charge account against cgroup which has no tasks.
2320 * So, rmdir()->pre_destroy() can be called while we do this charge.
2321 * In that case, we need to call pre_destroy() again. check it here.
2322 */
2323 cgroup_release_and_wakeup_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002324}
2325
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002326void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
2327{
2328 __mem_cgroup_commit_charge_swapin(page, ptr,
2329 MEM_CGROUP_CHARGE_TYPE_MAPPED);
2330}
2331
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002332void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
2333{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002334 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002335 return;
2336 if (!mem)
2337 return;
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002338 mem_cgroup_cancel_charge(mem);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002339}
2340
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002341static void
2342__do_uncharge(struct mem_cgroup *mem, const enum charge_type ctype)
2343{
2344 struct memcg_batch_info *batch = NULL;
2345 bool uncharge_memsw = true;
2346 /* If swapout, usage of swap doesn't decrease */
2347 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2348 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002349
2350 batch = &current->memcg_batch;
2351 /*
2352 * In usual, we do css_get() when we remember memcg pointer.
2353 * But in this case, we keep res->usage until end of a series of
2354 * uncharges. Then, it's ok to ignore memcg's refcnt.
2355 */
2356 if (!batch->memcg)
2357 batch->memcg = mem;
2358 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002359 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
2360 * In those cases, all pages freed continously can be expected to be in
2361 * the same cgroup and we have chance to coalesce uncharges.
2362 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
2363 * because we want to do uncharge as soon as possible.
2364 */
2365
2366 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
2367 goto direct_uncharge;
2368
2369 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002370 * In typical case, batch->memcg == mem. This means we can
2371 * merge a series of uncharges to an uncharge of res_counter.
2372 * If not, we uncharge res_counter ony by one.
2373 */
2374 if (batch->memcg != mem)
2375 goto direct_uncharge;
2376 /* remember freed charge and uncharge it later */
2377 batch->bytes += PAGE_SIZE;
2378 if (uncharge_memsw)
2379 batch->memsw_bytes += PAGE_SIZE;
2380 return;
2381direct_uncharge:
2382 res_counter_uncharge(&mem->res, PAGE_SIZE);
2383 if (uncharge_memsw)
2384 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002385 if (unlikely(batch->memcg != mem))
2386 memcg_oom_recover(mem);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002387 return;
2388}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002389
Balbir Singh8697d332008-02-07 00:13:59 -08002390/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002391 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002392 */
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002393static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002394__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002395{
Hugh Dickins82895462008-03-04 14:29:08 -08002396 struct page_cgroup *pc;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002397 struct mem_cgroup *mem = NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002398
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002399 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002400 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07002401
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002402 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002403 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002404
Balbir Singh8697d332008-02-07 00:13:59 -08002405 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08002406 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08002407 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002408 pc = lookup_page_cgroup(page);
2409 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002410 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002411
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002412 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002413
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002414 mem = pc->mem_cgroup;
2415
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002416 if (!PageCgroupUsed(pc))
2417 goto unlock_out;
2418
2419 switch (ctype) {
2420 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002421 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002422 /* See mem_cgroup_prepare_migration() */
2423 if (page_mapped(page) || PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002424 goto unlock_out;
2425 break;
2426 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
2427 if (!PageAnon(page)) { /* Shared memory */
2428 if (page->mapping && !page_is_file_cache(page))
2429 goto unlock_out;
2430 } else if (page_mapped(page)) /* Anon */
2431 goto unlock_out;
2432 break;
2433 default:
2434 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002435 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002436
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002437 mem_cgroup_charge_statistics(mem, pc, false);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002438
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002439 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08002440 /*
2441 * pc->mem_cgroup is not cleared here. It will be accessed when it's
2442 * freed from LRU. This is safe because uncharged page is expected not
2443 * to be reused (freed soon). Exception is SwapCache, it's handled by
2444 * special functions.
2445 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08002446
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002447 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002448 /*
2449 * even after unlock, we have mem->res.usage here and this memcg
2450 * will never be freed.
2451 */
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002452 memcg_check_events(mem, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002453 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
2454 mem_cgroup_swap_statistics(mem, true);
2455 mem_cgroup_get(mem);
2456 }
2457 if (!mem_cgroup_is_root(mem))
2458 __do_uncharge(mem, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002459
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002460 return mem;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002461
2462unlock_out:
2463 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002464 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08002465}
2466
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002467void mem_cgroup_uncharge_page(struct page *page)
2468{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002469 /* early check. */
2470 if (page_mapped(page))
2471 return;
2472 if (page->mapping && !PageAnon(page))
2473 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002474 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
2475}
2476
2477void mem_cgroup_uncharge_cache_page(struct page *page)
2478{
2479 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07002480 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002481 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
2482}
2483
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002484/*
2485 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
2486 * In that cases, pages are freed continuously and we can expect pages
2487 * are in the same memcg. All these calls itself limits the number of
2488 * pages freed at once, then uncharge_start/end() is called properly.
2489 * This may be called prural(2) times in a context,
2490 */
2491
2492void mem_cgroup_uncharge_start(void)
2493{
2494 current->memcg_batch.do_batch++;
2495 /* We can do nest. */
2496 if (current->memcg_batch.do_batch == 1) {
2497 current->memcg_batch.memcg = NULL;
2498 current->memcg_batch.bytes = 0;
2499 current->memcg_batch.memsw_bytes = 0;
2500 }
2501}
2502
2503void mem_cgroup_uncharge_end(void)
2504{
2505 struct memcg_batch_info *batch = &current->memcg_batch;
2506
2507 if (!batch->do_batch)
2508 return;
2509
2510 batch->do_batch--;
2511 if (batch->do_batch) /* If stacked, do nothing. */
2512 return;
2513
2514 if (!batch->memcg)
2515 return;
2516 /*
2517 * This "batch->memcg" is valid without any css_get/put etc...
2518 * bacause we hide charges behind us.
2519 */
2520 if (batch->bytes)
2521 res_counter_uncharge(&batch->memcg->res, batch->bytes);
2522 if (batch->memsw_bytes)
2523 res_counter_uncharge(&batch->memcg->memsw, batch->memsw_bytes);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002524 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002525 /* forget this pointer (for sanity check) */
2526 batch->memcg = NULL;
2527}
2528
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002529#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002530/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002531 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002532 * memcg information is recorded to swap_cgroup of "ent"
2533 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002534void
2535mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002536{
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002537 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002538 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002539
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002540 if (!swapout) /* this was a swap cache but the swap is unused ! */
2541 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
2542
2543 memcg = __mem_cgroup_uncharge_common(page, ctype);
2544
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002545 /*
2546 * record memcg information, if swapout && memcg != NULL,
2547 * mem_cgroup_get() was called in uncharge().
2548 */
2549 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002550 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002551}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002552#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002553
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002554#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2555/*
2556 * called from swap_entry_free(). remove record in swap_cgroup and
2557 * uncharge "memsw" account.
2558 */
2559void mem_cgroup_uncharge_swap(swp_entry_t ent)
2560{
2561 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002562 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002563
2564 if (!do_swap_account)
2565 return;
2566
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002567 id = swap_cgroup_record(ent, 0);
2568 rcu_read_lock();
2569 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002570 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002571 /*
2572 * We uncharge this because swap is freed.
2573 * This memcg can be obsolete one. We avoid calling css_tryget
2574 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002575 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002576 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002577 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002578 mem_cgroup_put(memcg);
2579 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002580 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002581}
Daisuke Nishimura02491442010-03-10 15:22:17 -08002582
2583/**
2584 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
2585 * @entry: swap entry to be moved
2586 * @from: mem_cgroup which the entry is moved from
2587 * @to: mem_cgroup which the entry is moved to
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002588 * @need_fixup: whether we should fixup res_counters and refcounts.
Daisuke Nishimura02491442010-03-10 15:22:17 -08002589 *
2590 * It succeeds only when the swap_cgroup's record for this entry is the same
2591 * as the mem_cgroup's id of @from.
2592 *
2593 * Returns 0 on success, -EINVAL on failure.
2594 *
2595 * The caller must have charged to @to, IOW, called res_counter_charge() about
2596 * both res and memsw, and called css_get().
2597 */
2598static int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002599 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08002600{
2601 unsigned short old_id, new_id;
2602
2603 old_id = css_id(&from->css);
2604 new_id = css_id(&to->css);
2605
2606 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08002607 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08002608 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002609 /*
2610 * This function is only called from task migration context now.
2611 * It postpones res_counter and refcount handling till the end
2612 * of task migration(mem_cgroup_clear_mc()) for performance
2613 * improvement. But we cannot postpone mem_cgroup_get(to)
2614 * because if the process that has been moved to @to does
2615 * swap-in, the refcount of @to might be decreased to 0.
2616 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08002617 mem_cgroup_get(to);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002618 if (need_fixup) {
2619 if (!mem_cgroup_is_root(from))
2620 res_counter_uncharge(&from->memsw, PAGE_SIZE);
2621 mem_cgroup_put(from);
2622 /*
2623 * we charged both to->res and to->memsw, so we should
2624 * uncharge to->res.
2625 */
2626 if (!mem_cgroup_is_root(to))
2627 res_counter_uncharge(&to->res, PAGE_SIZE);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002628 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08002629 return 0;
2630 }
2631 return -EINVAL;
2632}
2633#else
2634static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002635 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08002636{
2637 return -EINVAL;
2638}
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002639#endif
2640
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002641/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002642 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
2643 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002644 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002645int mem_cgroup_prepare_migration(struct page *page,
2646 struct page *newpage, struct mem_cgroup **ptr)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002647{
2648 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002649 struct mem_cgroup *mem = NULL;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002650 enum charge_type ctype;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002651 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08002652
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002653 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07002654 return 0;
2655
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002656 pc = lookup_page_cgroup(page);
2657 lock_page_cgroup(pc);
2658 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002659 mem = pc->mem_cgroup;
2660 css_get(&mem->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002661 /*
2662 * At migrating an anonymous page, its mapcount goes down
2663 * to 0 and uncharge() will be called. But, even if it's fully
2664 * unmapped, migration may fail and this page has to be
2665 * charged again. We set MIGRATION flag here and delay uncharge
2666 * until end_migration() is called
2667 *
2668 * Corner Case Thinking
2669 * A)
2670 * When the old page was mapped as Anon and it's unmap-and-freed
2671 * while migration was ongoing.
2672 * If unmap finds the old page, uncharge() of it will be delayed
2673 * until end_migration(). If unmap finds a new page, it's
2674 * uncharged when it make mapcount to be 1->0. If unmap code
2675 * finds swap_migration_entry, the new page will not be mapped
2676 * and end_migration() will find it(mapcount==0).
2677 *
2678 * B)
2679 * When the old page was mapped but migraion fails, the kernel
2680 * remaps it. A charge for it is kept by MIGRATION flag even
2681 * if mapcount goes down to 0. We can do remap successfully
2682 * without charging it again.
2683 *
2684 * C)
2685 * The "old" page is under lock_page() until the end of
2686 * migration, so, the old page itself will not be swapped-out.
2687 * If the new page is swapped out before end_migraton, our
2688 * hook to usual swap-out path will catch the event.
2689 */
2690 if (PageAnon(page))
2691 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08002692 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002693 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002694 /*
2695 * If the page is not charged at this point,
2696 * we return here.
2697 */
2698 if (!mem)
2699 return 0;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002700
Andrea Arcangeli93d5c9b2010-04-23 13:17:39 -04002701 *ptr = mem;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002702 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, ptr, false);
2703 css_put(&mem->css);/* drop extra refcnt */
2704 if (ret || *ptr == NULL) {
2705 if (PageAnon(page)) {
2706 lock_page_cgroup(pc);
2707 ClearPageCgroupMigration(pc);
2708 unlock_page_cgroup(pc);
2709 /*
2710 * The old page may be fully unmapped while we kept it.
2711 */
2712 mem_cgroup_uncharge_page(page);
2713 }
2714 return -ENOMEM;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002715 }
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002716 /*
2717 * We charge new page before it's used/mapped. So, even if unlock_page()
2718 * is called before end_migration, we can catch all events on this new
2719 * page. In the case new page is migrated but not remapped, new page's
2720 * mapcount will be finally 0 and we call uncharge in end_migration().
2721 */
2722 pc = lookup_page_cgroup(newpage);
2723 if (PageAnon(page))
2724 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
2725 else if (page_is_file_cache(page))
2726 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
2727 else
2728 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
2729 __mem_cgroup_commit_charge(mem, pc, ctype);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002730 return ret;
2731}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08002732
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002733/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002734void mem_cgroup_end_migration(struct mem_cgroup *mem,
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002735 struct page *oldpage, struct page *newpage)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002736{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002737 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002738 struct page_cgroup *pc;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002739
2740 if (!mem)
2741 return;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002742 /* blocks rmdir() */
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002743 cgroup_exclude_rmdir(&mem->css);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002744 /* at migration success, oldpage->mapping is NULL. */
2745 if (oldpage->mapping) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002746 used = oldpage;
2747 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002748 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002749 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002750 unused = oldpage;
2751 }
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002752 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002753 * We disallowed uncharge of pages under migration because mapcount
2754 * of the page goes down to zero, temporarly.
2755 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002756 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002757 pc = lookup_page_cgroup(oldpage);
2758 lock_page_cgroup(pc);
2759 ClearPageCgroupMigration(pc);
2760 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002761
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002762 __mem_cgroup_uncharge_common(unused, MEM_CGROUP_CHARGE_TYPE_FORCE);
2763
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002764 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002765 * If a page is a file cache, radix-tree replacement is very atomic
2766 * and we can skip this check. When it was an Anon page, its mapcount
2767 * goes down to 0. But because we added MIGRATION flage, it's not
2768 * uncharged yet. There are several case but page->mapcount check
2769 * and USED bit check in mem_cgroup_uncharge_page() will do enough
2770 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002771 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002772 if (PageAnon(used))
2773 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002774 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002775 * At migration, we may charge account against cgroup which has no
2776 * tasks.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002777 * So, rmdir()->pre_destroy() can be called while we do this charge.
2778 * In that case, we need to call pre_destroy() again. check it here.
2779 */
2780 cgroup_release_and_wakeup_rmdir(&mem->css);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002781}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08002782
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002783/*
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002784 * A call to try to shrink memory usage on charge failure at shmem's swapin.
2785 * Calling hierarchical_reclaim is not enough because we should update
2786 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
2787 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
2788 * not from the memcg which this page would be charged to.
2789 * try_charge_swapin does all of these works properly.
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002790 */
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002791int mem_cgroup_shmem_charge_fallback(struct page *page,
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002792 struct mm_struct *mm,
2793 gfp_t gfp_mask)
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002794{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002795 struct mem_cgroup *mem = NULL;
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002796 int ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002797
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002798 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002799 return 0;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002800
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002801 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2802 if (!ret)
2803 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002804
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002805 return ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002806}
2807
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002808static DEFINE_MUTEX(set_limit_mutex);
2809
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08002810static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002811 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002812{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002813 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002814 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002815 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002816 int children = mem_cgroup_count_children(memcg);
2817 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002818 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002819
2820 /*
2821 * For keeping hierarchical_reclaim simple, how long we should retry
2822 * is depends on callers. We set our retry-count to be function
2823 * of # of children which we should visit in this loop.
2824 */
2825 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
2826
2827 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002828
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002829 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002830 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002831 if (signal_pending(current)) {
2832 ret = -EINTR;
2833 break;
2834 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002835 /*
2836 * Rather than hide all in some function, I do this in
2837 * open coded manner. You see what this really does.
2838 * We have to guarantee mem->res.limit < mem->memsw.limit.
2839 */
2840 mutex_lock(&set_limit_mutex);
2841 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2842 if (memswlimit < val) {
2843 ret = -EINVAL;
2844 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002845 break;
2846 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002847
2848 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2849 if (memlimit < val)
2850 enlarge = 1;
2851
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002852 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07002853 if (!ret) {
2854 if (memswlimit == val)
2855 memcg->memsw_is_minimum = true;
2856 else
2857 memcg->memsw_is_minimum = false;
2858 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002859 mutex_unlock(&set_limit_mutex);
2860
2861 if (!ret)
2862 break;
2863
Bob Liuaa20d482009-12-15 16:47:14 -08002864 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh4e416952009-09-23 15:56:39 -07002865 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002866 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
2867 /* Usage is reduced ? */
2868 if (curusage >= oldusage)
2869 retry_count--;
2870 else
2871 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002872 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002873 if (!ret && enlarge)
2874 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002875
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002876 return ret;
2877}
2878
Li Zefan338c8432009-06-17 16:27:15 -07002879static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
2880 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002881{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002882 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002883 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002884 int children = mem_cgroup_count_children(memcg);
2885 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002886 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002887
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002888 /* see mem_cgroup_resize_res_limit */
2889 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
2890 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002891 while (retry_count) {
2892 if (signal_pending(current)) {
2893 ret = -EINTR;
2894 break;
2895 }
2896 /*
2897 * Rather than hide all in some function, I do this in
2898 * open coded manner. You see what this really does.
2899 * We have to guarantee mem->res.limit < mem->memsw.limit.
2900 */
2901 mutex_lock(&set_limit_mutex);
2902 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2903 if (memlimit > val) {
2904 ret = -EINVAL;
2905 mutex_unlock(&set_limit_mutex);
2906 break;
2907 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002908 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2909 if (memswlimit < val)
2910 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002911 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07002912 if (!ret) {
2913 if (memlimit == val)
2914 memcg->memsw_is_minimum = true;
2915 else
2916 memcg->memsw_is_minimum = false;
2917 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002918 mutex_unlock(&set_limit_mutex);
2919
2920 if (!ret)
2921 break;
2922
Balbir Singh4e416952009-09-23 15:56:39 -07002923 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh75822b42009-09-23 15:56:38 -07002924 MEM_CGROUP_RECLAIM_NOSWAP |
2925 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002926 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002927 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002928 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002929 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002930 else
2931 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002932 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002933 if (!ret && enlarge)
2934 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002935 return ret;
2936}
2937
Balbir Singh4e416952009-09-23 15:56:39 -07002938unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07002939 gfp_t gfp_mask)
Balbir Singh4e416952009-09-23 15:56:39 -07002940{
2941 unsigned long nr_reclaimed = 0;
2942 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
2943 unsigned long reclaimed;
2944 int loop = 0;
2945 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002946 unsigned long long excess;
Balbir Singh4e416952009-09-23 15:56:39 -07002947
2948 if (order > 0)
2949 return 0;
2950
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07002951 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
Balbir Singh4e416952009-09-23 15:56:39 -07002952 /*
2953 * This loop can run a while, specially if mem_cgroup's continuously
2954 * keep exceeding their soft limit and putting the system under
2955 * pressure
2956 */
2957 do {
2958 if (next_mz)
2959 mz = next_mz;
2960 else
2961 mz = mem_cgroup_largest_soft_limit_node(mctz);
2962 if (!mz)
2963 break;
2964
2965 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
2966 gfp_mask,
2967 MEM_CGROUP_RECLAIM_SOFT);
2968 nr_reclaimed += reclaimed;
2969 spin_lock(&mctz->lock);
2970
2971 /*
2972 * If we failed to reclaim anything from this memory cgroup
2973 * it is time to move on to the next cgroup
2974 */
2975 next_mz = NULL;
2976 if (!reclaimed) {
2977 do {
2978 /*
2979 * Loop until we find yet another one.
2980 *
2981 * By the time we get the soft_limit lock
2982 * again, someone might have aded the
2983 * group back on the RB tree. Iterate to
2984 * make sure we get a different mem.
2985 * mem_cgroup_largest_soft_limit_node returns
2986 * NULL if no other cgroup is present on
2987 * the tree
2988 */
2989 next_mz =
2990 __mem_cgroup_largest_soft_limit_node(mctz);
2991 if (next_mz == mz) {
2992 css_put(&next_mz->mem->css);
2993 next_mz = NULL;
2994 } else /* next_mz == NULL or other memcg */
2995 break;
2996 } while (1);
2997 }
Balbir Singh4e416952009-09-23 15:56:39 -07002998 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002999 excess = res_counter_soft_limit_excess(&mz->mem->res);
Balbir Singh4e416952009-09-23 15:56:39 -07003000 /*
3001 * One school of thought says that we should not add
3002 * back the node to the tree if reclaim returns 0.
3003 * But our reclaim could return 0, simply because due
3004 * to priority we are exposing a smaller subset of
3005 * memory to reclaim from. Consider this as a longer
3006 * term TODO.
3007 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003008 /* If excess == 0, no tree ops */
3009 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07003010 spin_unlock(&mctz->lock);
3011 css_put(&mz->mem->css);
3012 loop++;
3013 /*
3014 * Could not reclaim anything and there are no more
3015 * mem cgroups to try or we seem to be looping without
3016 * reclaiming anything.
3017 */
3018 if (!nr_reclaimed &&
3019 (next_mz == NULL ||
3020 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3021 break;
3022 } while (!nr_reclaimed);
3023 if (next_mz)
3024 css_put(&next_mz->mem->css);
3025 return nr_reclaimed;
3026}
3027
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003028/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003029 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003030 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
3031 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003032static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003033 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003034{
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003035 struct zone *zone;
3036 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003037 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003038 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003039 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003040 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003041
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003042 zone = &NODE_DATA(node)->node_zones[zid];
3043 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07003044 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003045
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003046 loop = MEM_CGROUP_ZSTAT(mz, lru);
3047 /* give some margin against EBUSY etc...*/
3048 loop += 256;
3049 busy = NULL;
3050 while (loop--) {
3051 ret = 0;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003052 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003053 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003054 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003055 break;
3056 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003057 pc = list_entry(list->prev, struct page_cgroup, lru);
3058 if (busy == pc) {
3059 list_move(&pc->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08003060 busy = NULL;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003061 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003062 continue;
3063 }
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003064 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003065
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -08003066 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003067 if (ret == -ENOMEM)
3068 break;
3069
3070 if (ret == -EBUSY || ret == -EINVAL) {
3071 /* found lock contention or "pc" is obsolete. */
3072 busy = pc;
3073 cond_resched();
3074 } else
3075 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003076 }
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003077
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003078 if (!ret && !list_empty(list))
3079 return -EBUSY;
3080 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003081}
3082
3083/*
3084 * make mem_cgroup's charge to be 0 if there is no task.
3085 * This enables deleting this mem_cgroup.
3086 */
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003087static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003088{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003089 int ret;
3090 int node, zid, shrink;
3091 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003092 struct cgroup *cgrp = mem->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003093
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003094 css_get(&mem->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003095
3096 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003097 /* should free all ? */
3098 if (free_all)
3099 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003100move_account:
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003101 do {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003102 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003103 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003104 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003105 ret = -EINTR;
3106 if (signal_pending(current))
3107 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003108 /* This is for making all *used* pages to be on LRU. */
3109 lru_add_drain_all();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08003110 drain_all_stock_sync();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003111 ret = 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07003112 mem_cgroup_start_move(mem);
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08003113 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003114 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07003115 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003116 for_each_lru(l) {
3117 ret = mem_cgroup_force_empty_list(mem,
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003118 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003119 if (ret)
3120 break;
3121 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003122 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003123 if (ret)
3124 break;
3125 }
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07003126 mem_cgroup_end_move(mem);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003127 memcg_oom_recover(mem);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003128 /* it seems parent cgroup doesn't have enough mem */
3129 if (ret == -ENOMEM)
3130 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003131 cond_resched();
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003132 /* "ret" should also be checked to ensure all lists are empty. */
3133 } while (mem->res.usage > 0 || ret);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003134out:
3135 css_put(&mem->css);
3136 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003137
3138try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003139 /* returns EBUSY if there is a task or if we come here twice. */
3140 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003141 ret = -EBUSY;
3142 goto out;
3143 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003144 /* we call try-to-free pages for make this cgroup empty */
3145 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003146 /* try to free all pages in this cgroup */
3147 shrink = 1;
3148 while (nr_retries && mem->res.usage > 0) {
3149 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003150
3151 if (signal_pending(current)) {
3152 ret = -EINTR;
3153 goto out;
3154 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003155 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
3156 false, get_swappiness(mem));
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003157 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003158 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003159 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02003160 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003161 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003162
3163 }
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003164 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003165 /* try move_account...there may be some *locked* pages. */
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003166 goto move_account;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003167}
3168
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003169int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
3170{
3171 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
3172}
3173
3174
Balbir Singh18f59ea2009-01-07 18:08:07 -08003175static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
3176{
3177 return mem_cgroup_from_cont(cont)->use_hierarchy;
3178}
3179
3180static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
3181 u64 val)
3182{
3183 int retval = 0;
3184 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
3185 struct cgroup *parent = cont->parent;
3186 struct mem_cgroup *parent_mem = NULL;
3187
3188 if (parent)
3189 parent_mem = mem_cgroup_from_cont(parent);
3190
3191 cgroup_lock();
3192 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003193 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08003194 * in the child subtrees. If it is unset, then the change can
3195 * occur, provided the current cgroup has no children.
3196 *
3197 * For the root cgroup, parent_mem is NULL, we allow value to be
3198 * set if there are no children.
3199 */
3200 if ((!parent_mem || !parent_mem->use_hierarchy) &&
3201 (val == 1 || val == 0)) {
3202 if (list_empty(&cont->children))
3203 mem->use_hierarchy = val;
3204 else
3205 retval = -EBUSY;
3206 } else
3207 retval = -EINVAL;
3208 cgroup_unlock();
3209
3210 return retval;
3211}
3212
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003213
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003214static u64 mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem,
3215 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003216{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003217 struct mem_cgroup *iter;
3218 s64 val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003219
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003220 /* each per cpu's value can be minus.Then, use s64 */
3221 for_each_mem_cgroup_tree(iter, mem)
3222 val += mem_cgroup_read_stat(iter, idx);
3223
3224 if (val < 0) /* race ? */
3225 val = 0;
3226 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003227}
3228
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003229static inline u64 mem_cgroup_usage(struct mem_cgroup *mem, bool swap)
3230{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003231 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003232
3233 if (!mem_cgroup_is_root(mem)) {
3234 if (!swap)
3235 return res_counter_read_u64(&mem->res, RES_USAGE);
3236 else
3237 return res_counter_read_u64(&mem->memsw, RES_USAGE);
3238 }
3239
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003240 val = mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_CACHE);
3241 val += mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003242
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003243 if (swap)
3244 val += mem_cgroup_get_recursive_idx_stat(mem,
3245 MEM_CGROUP_STAT_SWAPOUT);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003246
3247 return val << PAGE_SHIFT;
3248}
3249
Paul Menage2c3daa72008-04-29 00:59:58 -07003250static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003251{
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003252 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003253 u64 val;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003254 int type, name;
3255
3256 type = MEMFILE_TYPE(cft->private);
3257 name = MEMFILE_ATTR(cft->private);
3258 switch (type) {
3259 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003260 if (name == RES_USAGE)
3261 val = mem_cgroup_usage(mem, false);
3262 else
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003263 val = res_counter_read_u64(&mem->res, name);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003264 break;
3265 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003266 if (name == RES_USAGE)
3267 val = mem_cgroup_usage(mem, true);
3268 else
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003269 val = res_counter_read_u64(&mem->memsw, name);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003270 break;
3271 default:
3272 BUG();
3273 break;
3274 }
3275 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003276}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003277/*
3278 * The user of this function is...
3279 * RES_LIMIT.
3280 */
Paul Menage856c13a2008-07-25 01:47:04 -07003281static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
3282 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003283{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003284 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003285 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003286 unsigned long long val;
3287 int ret;
3288
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003289 type = MEMFILE_TYPE(cft->private);
3290 name = MEMFILE_ATTR(cft->private);
3291 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003292 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07003293 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
3294 ret = -EINVAL;
3295 break;
3296 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003297 /* This function does all necessary parse...reuse it */
3298 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003299 if (ret)
3300 break;
3301 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003302 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003303 else
3304 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003305 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07003306 case RES_SOFT_LIMIT:
3307 ret = res_counter_memparse_write_strategy(buffer, &val);
3308 if (ret)
3309 break;
3310 /*
3311 * For memsw, soft limits are hard to implement in terms
3312 * of semantics, for now, we support soft limits for
3313 * control without swap
3314 */
3315 if (type == _MEM)
3316 ret = res_counter_set_soft_limit(&memcg->res, val);
3317 else
3318 ret = -EINVAL;
3319 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003320 default:
3321 ret = -EINVAL; /* should be BUG() ? */
3322 break;
3323 }
3324 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003325}
3326
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08003327static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
3328 unsigned long long *mem_limit, unsigned long long *memsw_limit)
3329{
3330 struct cgroup *cgroup;
3331 unsigned long long min_limit, min_memsw_limit, tmp;
3332
3333 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3334 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3335 cgroup = memcg->css.cgroup;
3336 if (!memcg->use_hierarchy)
3337 goto out;
3338
3339 while (cgroup->parent) {
3340 cgroup = cgroup->parent;
3341 memcg = mem_cgroup_from_cont(cgroup);
3342 if (!memcg->use_hierarchy)
3343 break;
3344 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
3345 min_limit = min(min_limit, tmp);
3346 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3347 min_memsw_limit = min(min_memsw_limit, tmp);
3348 }
3349out:
3350 *mem_limit = min_limit;
3351 *memsw_limit = min_memsw_limit;
3352 return;
3353}
3354
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003355static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003356{
3357 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003358 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003359
3360 mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003361 type = MEMFILE_TYPE(event);
3362 name = MEMFILE_ATTR(event);
3363 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003364 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003365 if (type == _MEM)
3366 res_counter_reset_max(&mem->res);
3367 else
3368 res_counter_reset_max(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003369 break;
3370 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003371 if (type == _MEM)
3372 res_counter_reset_failcnt(&mem->res);
3373 else
3374 res_counter_reset_failcnt(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003375 break;
3376 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07003377
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07003378 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003379}
3380
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003381static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
3382 struct cftype *cft)
3383{
3384 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
3385}
3386
Daisuke Nishimura02491442010-03-10 15:22:17 -08003387#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003388static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
3389 struct cftype *cft, u64 val)
3390{
3391 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3392
3393 if (val >= (1 << NR_MOVE_TYPE))
3394 return -EINVAL;
3395 /*
3396 * We check this value several times in both in can_attach() and
3397 * attach(), so we need cgroup lock to prevent this value from being
3398 * inconsistent.
3399 */
3400 cgroup_lock();
3401 mem->move_charge_at_immigrate = val;
3402 cgroup_unlock();
3403
3404 return 0;
3405}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003406#else
3407static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
3408 struct cftype *cft, u64 val)
3409{
3410 return -ENOSYS;
3411}
3412#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003413
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003414
3415/* For read statistics */
3416enum {
3417 MCS_CACHE,
3418 MCS_RSS,
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08003419 MCS_FILE_MAPPED,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003420 MCS_PGPGIN,
3421 MCS_PGPGOUT,
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003422 MCS_SWAP,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003423 MCS_INACTIVE_ANON,
3424 MCS_ACTIVE_ANON,
3425 MCS_INACTIVE_FILE,
3426 MCS_ACTIVE_FILE,
3427 MCS_UNEVICTABLE,
3428 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003429};
3430
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003431struct mcs_total_stat {
3432 s64 stat[NR_MCS_STAT];
3433};
3434
3435struct {
3436 char *local_name;
3437 char *total_name;
3438} memcg_stat_strings[NR_MCS_STAT] = {
3439 {"cache", "total_cache"},
3440 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07003441 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003442 {"pgpgin", "total_pgpgin"},
3443 {"pgpgout", "total_pgpgout"},
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003444 {"swap", "total_swap"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003445 {"inactive_anon", "total_inactive_anon"},
3446 {"active_anon", "total_active_anon"},
3447 {"inactive_file", "total_inactive_file"},
3448 {"active_file", "total_active_file"},
3449 {"unevictable", "total_unevictable"}
3450};
3451
3452
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003453static void
3454mem_cgroup_get_local_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003455{
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003456 s64 val;
3457
3458 /* per cpu stat */
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003459 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003460 s->stat[MCS_CACHE] += val * PAGE_SIZE;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003461 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003462 s->stat[MCS_RSS] += val * PAGE_SIZE;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003463 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_FILE_MAPPED);
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08003464 s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003465 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_PGPGIN_COUNT);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003466 s->stat[MCS_PGPGIN] += val;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003467 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_PGPGOUT_COUNT);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003468 s->stat[MCS_PGPGOUT] += val;
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003469 if (do_swap_account) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003470 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_SWAPOUT);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003471 s->stat[MCS_SWAP] += val * PAGE_SIZE;
3472 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003473
3474 /* per zone stat */
3475 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
3476 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
3477 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
3478 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
3479 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
3480 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
3481 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
3482 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
3483 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
3484 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003485}
3486
3487static void
3488mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
3489{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003490 struct mem_cgroup *iter;
3491
3492 for_each_mem_cgroup_tree(iter, mem)
3493 mem_cgroup_get_local_stat(iter, s);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003494}
3495
Paul Menagec64745c2008-04-29 01:00:02 -07003496static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
3497 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003498{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003499 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003500 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003501 int i;
3502
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003503 memset(&mystat, 0, sizeof(mystat));
3504 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003505
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003506 for (i = 0; i < NR_MCS_STAT; i++) {
3507 if (i == MCS_SWAP && !do_swap_account)
3508 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003509 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003510 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003511
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003512 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08003513 {
3514 unsigned long long limit, memsw_limit;
3515 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
3516 cb->fill(cb, "hierarchical_memory_limit", limit);
3517 if (do_swap_account)
3518 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
3519 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003520
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003521 memset(&mystat, 0, sizeof(mystat));
3522 mem_cgroup_get_total_stat(mem_cont, &mystat);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003523 for (i = 0; i < NR_MCS_STAT; i++) {
3524 if (i == MCS_SWAP && !do_swap_account)
3525 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003526 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003527 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003528
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003529#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08003530 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003531
3532 {
3533 int nid, zid;
3534 struct mem_cgroup_per_zone *mz;
3535 unsigned long recent_rotated[2] = {0, 0};
3536 unsigned long recent_scanned[2] = {0, 0};
3537
3538 for_each_online_node(nid)
3539 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
3540 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
3541
3542 recent_rotated[0] +=
3543 mz->reclaim_stat.recent_rotated[0];
3544 recent_rotated[1] +=
3545 mz->reclaim_stat.recent_rotated[1];
3546 recent_scanned[0] +=
3547 mz->reclaim_stat.recent_scanned[0];
3548 recent_scanned[1] +=
3549 mz->reclaim_stat.recent_scanned[1];
3550 }
3551 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
3552 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
3553 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
3554 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
3555 }
3556#endif
3557
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003558 return 0;
3559}
3560
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003561static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
3562{
3563 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3564
3565 return get_swappiness(memcg);
3566}
3567
3568static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
3569 u64 val)
3570{
3571 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3572 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08003573
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003574 if (val > 100)
3575 return -EINVAL;
3576
3577 if (cgrp->parent == NULL)
3578 return -EINVAL;
3579
3580 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08003581
3582 cgroup_lock();
3583
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003584 /* If under hierarchy, only empty-root can set this value */
3585 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08003586 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
3587 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003588 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08003589 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003590
3591 spin_lock(&memcg->reclaim_param_lock);
3592 memcg->swappiness = val;
3593 spin_unlock(&memcg->reclaim_param_lock);
3594
Li Zefan068b38c2009-01-15 13:51:26 -08003595 cgroup_unlock();
3596
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003597 return 0;
3598}
3599
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003600static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
3601{
3602 struct mem_cgroup_threshold_ary *t;
3603 u64 usage;
3604 int i;
3605
3606 rcu_read_lock();
3607 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003608 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003609 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003610 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003611
3612 if (!t)
3613 goto unlock;
3614
3615 usage = mem_cgroup_usage(memcg, swap);
3616
3617 /*
3618 * current_threshold points to threshold just below usage.
3619 * If it's not true, a threshold was crossed after last
3620 * call of __mem_cgroup_threshold().
3621 */
Phil Carmody5407a562010-05-26 14:42:42 -07003622 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003623
3624 /*
3625 * Iterate backward over array of thresholds starting from
3626 * current_threshold and check if a threshold is crossed.
3627 * If none of thresholds below usage is crossed, we read
3628 * only one element of the array here.
3629 */
3630 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
3631 eventfd_signal(t->entries[i].eventfd, 1);
3632
3633 /* i = current_threshold + 1 */
3634 i++;
3635
3636 /*
3637 * Iterate forward over array of thresholds starting from
3638 * current_threshold+1 and check if a threshold is crossed.
3639 * If none of thresholds above usage is crossed, we read
3640 * only one element of the array here.
3641 */
3642 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
3643 eventfd_signal(t->entries[i].eventfd, 1);
3644
3645 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07003646 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003647unlock:
3648 rcu_read_unlock();
3649}
3650
3651static void mem_cgroup_threshold(struct mem_cgroup *memcg)
3652{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07003653 while (memcg) {
3654 __mem_cgroup_threshold(memcg, false);
3655 if (do_swap_account)
3656 __mem_cgroup_threshold(memcg, true);
3657
3658 memcg = parent_mem_cgroup(memcg);
3659 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003660}
3661
3662static int compare_thresholds(const void *a, const void *b)
3663{
3664 const struct mem_cgroup_threshold *_a = a;
3665 const struct mem_cgroup_threshold *_b = b;
3666
3667 return _a->threshold - _b->threshold;
3668}
3669
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003670static int mem_cgroup_oom_notify_cb(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003671{
3672 struct mem_cgroup_eventfd_list *ev;
3673
3674 list_for_each_entry(ev, &mem->oom_notify, list)
3675 eventfd_signal(ev->eventfd, 1);
3676 return 0;
3677}
3678
3679static void mem_cgroup_oom_notify(struct mem_cgroup *mem)
3680{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003681 struct mem_cgroup *iter;
3682
3683 for_each_mem_cgroup_tree(iter, mem)
3684 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003685}
3686
3687static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
3688 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003689{
3690 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003691 struct mem_cgroup_thresholds *thresholds;
3692 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003693 int type = MEMFILE_TYPE(cft->private);
3694 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003695 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003696
3697 ret = res_counter_memparse_write_strategy(args, &threshold);
3698 if (ret)
3699 return ret;
3700
3701 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003702
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003703 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003704 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003705 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003706 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003707 else
3708 BUG();
3709
3710 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
3711
3712 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003713 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003714 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
3715
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003716 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003717
3718 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003719 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003720 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003721 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003722 ret = -ENOMEM;
3723 goto unlock;
3724 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003725 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003726
3727 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003728 if (thresholds->primary) {
3729 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003730 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003731 }
3732
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003733 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003734 new->entries[size - 1].eventfd = eventfd;
3735 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003736
3737 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003738 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003739 compare_thresholds, NULL);
3740
3741 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003742 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003743 for (i = 0; i < size; i++) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003744 if (new->entries[i].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003745 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003746 * new->current_threshold will not be used until
3747 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003748 * it here.
3749 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003750 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003751 }
3752 }
3753
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003754 /* Free old spare buffer and save old primary buffer as spare */
3755 kfree(thresholds->spare);
3756 thresholds->spare = thresholds->primary;
3757
3758 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003759
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003760 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003761 synchronize_rcu();
3762
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003763unlock:
3764 mutex_unlock(&memcg->thresholds_lock);
3765
3766 return ret;
3767}
3768
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003769static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003770 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003771{
3772 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003773 struct mem_cgroup_thresholds *thresholds;
3774 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003775 int type = MEMFILE_TYPE(cft->private);
3776 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003777 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003778
3779 mutex_lock(&memcg->thresholds_lock);
3780 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003781 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003782 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003783 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003784 else
3785 BUG();
3786
3787 /*
3788 * Something went wrong if we trying to unregister a threshold
3789 * if we don't have thresholds
3790 */
3791 BUG_ON(!thresholds);
3792
3793 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
3794
3795 /* Check if a threshold crossed before removing */
3796 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
3797
3798 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003799 size = 0;
3800 for (i = 0; i < thresholds->primary->size; i++) {
3801 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003802 size++;
3803 }
3804
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003805 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003806
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003807 /* Set thresholds array to NULL if we don't have thresholds */
3808 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003809 kfree(new);
3810 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003811 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003812 }
3813
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003814 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003815
3816 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003817 new->current_threshold = -1;
3818 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
3819 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003820 continue;
3821
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003822 new->entries[j] = thresholds->primary->entries[i];
3823 if (new->entries[j].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003824 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003825 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003826 * until rcu_assign_pointer(), so it's safe to increment
3827 * it here.
3828 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003829 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003830 }
3831 j++;
3832 }
3833
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003834swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003835 /* Swap primary and spare array */
3836 thresholds->spare = thresholds->primary;
3837 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003838
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003839 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003840 synchronize_rcu();
3841
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003842 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003843}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003844
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003845static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
3846 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
3847{
3848 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3849 struct mem_cgroup_eventfd_list *event;
3850 int type = MEMFILE_TYPE(cft->private);
3851
3852 BUG_ON(type != _OOM_TYPE);
3853 event = kmalloc(sizeof(*event), GFP_KERNEL);
3854 if (!event)
3855 return -ENOMEM;
3856
3857 mutex_lock(&memcg_oom_mutex);
3858
3859 event->eventfd = eventfd;
3860 list_add(&event->list, &memcg->oom_notify);
3861
3862 /* already in OOM ? */
3863 if (atomic_read(&memcg->oom_lock))
3864 eventfd_signal(eventfd, 1);
3865 mutex_unlock(&memcg_oom_mutex);
3866
3867 return 0;
3868}
3869
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003870static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003871 struct cftype *cft, struct eventfd_ctx *eventfd)
3872{
3873 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3874 struct mem_cgroup_eventfd_list *ev, *tmp;
3875 int type = MEMFILE_TYPE(cft->private);
3876
3877 BUG_ON(type != _OOM_TYPE);
3878
3879 mutex_lock(&memcg_oom_mutex);
3880
3881 list_for_each_entry_safe(ev, tmp, &mem->oom_notify, list) {
3882 if (ev->eventfd == eventfd) {
3883 list_del(&ev->list);
3884 kfree(ev);
3885 }
3886 }
3887
3888 mutex_unlock(&memcg_oom_mutex);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003889}
3890
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003891static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
3892 struct cftype *cft, struct cgroup_map_cb *cb)
3893{
3894 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3895
3896 cb->fill(cb, "oom_kill_disable", mem->oom_kill_disable);
3897
3898 if (atomic_read(&mem->oom_lock))
3899 cb->fill(cb, "under_oom", 1);
3900 else
3901 cb->fill(cb, "under_oom", 0);
3902 return 0;
3903}
3904
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003905static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
3906 struct cftype *cft, u64 val)
3907{
3908 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3909 struct mem_cgroup *parent;
3910
3911 /* cannot set to root cgroup and only 0 and 1 are allowed */
3912 if (!cgrp->parent || !((val == 0) || (val == 1)))
3913 return -EINVAL;
3914
3915 parent = mem_cgroup_from_cont(cgrp->parent);
3916
3917 cgroup_lock();
3918 /* oom-kill-disable is a flag for subhierarchy. */
3919 if ((parent->use_hierarchy) ||
3920 (mem->use_hierarchy && !list_empty(&cgrp->children))) {
3921 cgroup_unlock();
3922 return -EINVAL;
3923 }
3924 mem->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07003925 if (!val)
3926 memcg_oom_recover(mem);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003927 cgroup_unlock();
3928 return 0;
3929}
3930
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003931static struct cftype mem_cgroup_files[] = {
3932 {
Balbir Singh0eea1032008-02-07 00:13:57 -08003933 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003934 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07003935 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003936 .register_event = mem_cgroup_usage_register_event,
3937 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003938 },
3939 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003940 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003941 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003942 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003943 .read_u64 = mem_cgroup_read,
3944 },
3945 {
Balbir Singh0eea1032008-02-07 00:13:57 -08003946 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003947 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07003948 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07003949 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003950 },
3951 {
Balbir Singh296c81d2009-09-23 15:56:36 -07003952 .name = "soft_limit_in_bytes",
3953 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
3954 .write_string = mem_cgroup_write,
3955 .read_u64 = mem_cgroup_read,
3956 },
3957 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003958 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003959 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003960 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07003961 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003962 },
Balbir Singh8697d332008-02-07 00:13:59 -08003963 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003964 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07003965 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003966 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003967 {
3968 .name = "force_empty",
3969 .trigger = mem_cgroup_force_empty_write,
3970 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08003971 {
3972 .name = "use_hierarchy",
3973 .write_u64 = mem_cgroup_hierarchy_write,
3974 .read_u64 = mem_cgroup_hierarchy_read,
3975 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003976 {
3977 .name = "swappiness",
3978 .read_u64 = mem_cgroup_swappiness_read,
3979 .write_u64 = mem_cgroup_swappiness_write,
3980 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003981 {
3982 .name = "move_charge_at_immigrate",
3983 .read_u64 = mem_cgroup_move_charge_read,
3984 .write_u64 = mem_cgroup_move_charge_write,
3985 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003986 {
3987 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003988 .read_map = mem_cgroup_oom_control_read,
3989 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003990 .register_event = mem_cgroup_oom_register_event,
3991 .unregister_event = mem_cgroup_oom_unregister_event,
3992 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
3993 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003994};
3995
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003996#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3997static struct cftype memsw_cgroup_files[] = {
3998 {
3999 .name = "memsw.usage_in_bytes",
4000 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
4001 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004002 .register_event = mem_cgroup_usage_register_event,
4003 .unregister_event = mem_cgroup_usage_unregister_event,
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004004 },
4005 {
4006 .name = "memsw.max_usage_in_bytes",
4007 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
4008 .trigger = mem_cgroup_reset,
4009 .read_u64 = mem_cgroup_read,
4010 },
4011 {
4012 .name = "memsw.limit_in_bytes",
4013 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
4014 .write_string = mem_cgroup_write,
4015 .read_u64 = mem_cgroup_read,
4016 },
4017 {
4018 .name = "memsw.failcnt",
4019 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
4020 .trigger = mem_cgroup_reset,
4021 .read_u64 = mem_cgroup_read,
4022 },
4023};
4024
4025static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
4026{
4027 if (!do_swap_account)
4028 return 0;
4029 return cgroup_add_files(cont, ss, memsw_cgroup_files,
4030 ARRAY_SIZE(memsw_cgroup_files));
4031};
4032#else
4033static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
4034{
4035 return 0;
4036}
4037#endif
4038
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004039static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
4040{
4041 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004042 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07004043 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004044 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004045 /*
4046 * This routine is called against possible nodes.
4047 * But it's BUG to call kmalloc() against offline node.
4048 *
4049 * TODO: this routine can waste much memory for nodes which will
4050 * never be onlined. It's better to use memory hotplug callback
4051 * function.
4052 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004053 if (!node_state(node, N_NORMAL_MEMORY))
4054 tmp = -1;
4055 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004056 if (!pn)
4057 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004058
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004059 mem->info.nodeinfo[node] = pn;
4060 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004061
4062 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4063 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07004064 for_each_lru(l)
4065 INIT_LIST_HEAD(&mz->lists[l]);
Balbir Singhf64c3f52009-09-23 15:56:37 -07004066 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07004067 mz->on_tree = false;
4068 mz->mem = mem;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004069 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004070 return 0;
4071}
4072
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004073static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
4074{
4075 kfree(mem->info.nodeinfo[node]);
4076}
4077
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004078static struct mem_cgroup *mem_cgroup_alloc(void)
4079{
4080 struct mem_cgroup *mem;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004081 int size = sizeof(struct mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004082
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004083 /* Can be very big if MAX_NUMNODES is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08004084 if (size < PAGE_SIZE)
4085 mem = kmalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004086 else
Jan Blunckc8dad2b2009-01-07 18:07:53 -08004087 mem = vmalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004088
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07004089 if (!mem)
4090 return NULL;
4091
4092 memset(mem, 0, size);
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004093 mem->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
4094 if (!mem->stat) {
4095 if (size < PAGE_SIZE)
4096 kfree(mem);
4097 else
4098 vfree(mem);
4099 mem = NULL;
4100 }
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004101 return mem;
4102}
4103
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004104/*
4105 * At destroying mem_cgroup, references from swap_cgroup can remain.
4106 * (scanning all at force_empty is too costly...)
4107 *
4108 * Instead of clearing all references at force_empty, we remember
4109 * the number of reference from swap_cgroup and free mem_cgroup when
4110 * it goes down to 0.
4111 *
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004112 * Removal of cgroup itself succeeds regardless of refs from swap.
4113 */
4114
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004115static void __mem_cgroup_free(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004116{
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08004117 int node;
4118
Balbir Singhf64c3f52009-09-23 15:56:37 -07004119 mem_cgroup_remove_from_trees(mem);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004120 free_css_id(&mem_cgroup_subsys, &mem->css);
4121
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08004122 for_each_node_state(node, N_POSSIBLE)
4123 free_mem_cgroup_per_zone_info(mem, node);
4124
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004125 free_percpu(mem->stat);
4126 if (sizeof(struct mem_cgroup) < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004127 kfree(mem);
4128 else
4129 vfree(mem);
4130}
4131
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004132static void mem_cgroup_get(struct mem_cgroup *mem)
4133{
4134 atomic_inc(&mem->refcnt);
4135}
4136
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004137static void __mem_cgroup_put(struct mem_cgroup *mem, int count)
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004138{
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004139 if (atomic_sub_and_test(count, &mem->refcnt)) {
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004140 struct mem_cgroup *parent = parent_mem_cgroup(mem);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004141 __mem_cgroup_free(mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004142 if (parent)
4143 mem_cgroup_put(parent);
4144 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004145}
4146
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004147static void mem_cgroup_put(struct mem_cgroup *mem)
4148{
4149 __mem_cgroup_put(mem, 1);
4150}
4151
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004152/*
4153 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
4154 */
4155static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
4156{
4157 if (!mem->res.parent)
4158 return NULL;
4159 return mem_cgroup_from_res_counter(mem->res.parent, res);
4160}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004161
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004162#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4163static void __init enable_swap_cgroup(void)
4164{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004165 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004166 do_swap_account = 1;
4167}
4168#else
4169static void __init enable_swap_cgroup(void)
4170{
4171}
4172#endif
4173
Balbir Singhf64c3f52009-09-23 15:56:37 -07004174static int mem_cgroup_soft_limit_tree_init(void)
4175{
4176 struct mem_cgroup_tree_per_node *rtpn;
4177 struct mem_cgroup_tree_per_zone *rtpz;
4178 int tmp, node, zone;
4179
4180 for_each_node_state(node, N_POSSIBLE) {
4181 tmp = node;
4182 if (!node_state(node, N_NORMAL_MEMORY))
4183 tmp = -1;
4184 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
4185 if (!rtpn)
4186 return 1;
4187
4188 soft_limit_tree.rb_tree_per_node[node] = rtpn;
4189
4190 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4191 rtpz = &rtpn->rb_tree_per_zone[zone];
4192 rtpz->rb_root = RB_ROOT;
4193 spin_lock_init(&rtpz->lock);
4194 }
4195 }
4196 return 0;
4197}
4198
Li Zefan0eb253e2009-01-15 13:51:25 -08004199static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004200mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
4201{
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004202 struct mem_cgroup *mem, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004203 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004204 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004205
Jan Blunckc8dad2b2009-01-07 18:07:53 -08004206 mem = mem_cgroup_alloc();
4207 if (!mem)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004208 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004209
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004210 for_each_node_state(node, N_POSSIBLE)
4211 if (alloc_mem_cgroup_per_zone_info(mem, node))
4212 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004213
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004214 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004215 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004216 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004217 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004218 parent = NULL;
Balbir Singh4b3bde42009-09-23 15:56:32 -07004219 root_mem_cgroup = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004220 if (mem_cgroup_soft_limit_tree_init())
4221 goto free_out;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004222 for_each_possible_cpu(cpu) {
4223 struct memcg_stock_pcp *stock =
4224 &per_cpu(memcg_stock, cpu);
4225 INIT_WORK(&stock->work, drain_local_stock);
4226 }
4227 hotcpu_notifier(memcg_stock_cpu_callback, 0);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004228 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004229 parent = mem_cgroup_from_cont(cont->parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004230 mem->use_hierarchy = parent->use_hierarchy;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004231 mem->oom_kill_disable = parent->oom_kill_disable;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004232 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004233
Balbir Singh18f59ea2009-01-07 18:08:07 -08004234 if (parent && parent->use_hierarchy) {
4235 res_counter_init(&mem->res, &parent->res);
4236 res_counter_init(&mem->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004237 /*
4238 * We increment refcnt of the parent to ensure that we can
4239 * safely access it on res_counter_charge/uncharge.
4240 * This refcnt will be decremented when freeing this
4241 * mem_cgroup(see mem_cgroup_put).
4242 */
4243 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004244 } else {
4245 res_counter_init(&mem->res, NULL);
4246 res_counter_init(&mem->memsw, NULL);
4247 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004248 mem->last_scanned_child = 0;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -08004249 spin_lock_init(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004250 INIT_LIST_HEAD(&mem->oom_notify);
Balbir Singh6d61ef42009-01-07 18:08:06 -08004251
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004252 if (parent)
4253 mem->swappiness = get_swappiness(parent);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004254 atomic_set(&mem->refcnt, 1);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004255 mem->move_charge_at_immigrate = 0;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004256 mutex_init(&mem->thresholds_lock);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004257 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004258free_out:
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004259 __mem_cgroup_free(mem);
Balbir Singh4b3bde42009-09-23 15:56:32 -07004260 root_mem_cgroup = NULL;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004261 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004262}
4263
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004264static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004265 struct cgroup *cont)
4266{
4267 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004268
4269 return mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004270}
4271
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004272static void mem_cgroup_destroy(struct cgroup_subsys *ss,
4273 struct cgroup *cont)
4274{
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004275 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004276
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004277 mem_cgroup_put(mem);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004278}
4279
4280static int mem_cgroup_populate(struct cgroup_subsys *ss,
4281 struct cgroup *cont)
4282{
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004283 int ret;
4284
4285 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
4286 ARRAY_SIZE(mem_cgroup_files));
4287
4288 if (!ret)
4289 ret = register_memsw_files(cont, ss);
4290 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004291}
4292
Daisuke Nishimura02491442010-03-10 15:22:17 -08004293#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004294/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004295#define PRECHARGE_COUNT_AT_ONCE 256
4296static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004297{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004298 int ret = 0;
4299 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004300 struct mem_cgroup *mem = mc.to;
4301
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004302 if (mem_cgroup_is_root(mem)) {
4303 mc.precharge += count;
4304 /* we don't need css_get for root */
4305 return ret;
4306 }
4307 /* try to charge at once */
4308 if (count > 1) {
4309 struct res_counter *dummy;
4310 /*
4311 * "mem" cannot be under rmdir() because we've already checked
4312 * by cgroup_lock_live_cgroup() that it is not removed and we
4313 * are still under the same cgroup_mutex. So we can postpone
4314 * css_get().
4315 */
4316 if (res_counter_charge(&mem->res, PAGE_SIZE * count, &dummy))
4317 goto one_by_one;
4318 if (do_swap_account && res_counter_charge(&mem->memsw,
4319 PAGE_SIZE * count, &dummy)) {
4320 res_counter_uncharge(&mem->res, PAGE_SIZE * count);
4321 goto one_by_one;
4322 }
4323 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004324 return ret;
4325 }
4326one_by_one:
4327 /* fall back to one by one charge */
4328 while (count--) {
4329 if (signal_pending(current)) {
4330 ret = -EINTR;
4331 break;
4332 }
4333 if (!batch_count--) {
4334 batch_count = PRECHARGE_COUNT_AT_ONCE;
4335 cond_resched();
4336 }
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08004337 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004338 if (ret || !mem)
4339 /* mem_cgroup_clear_mc() will do uncharge later */
4340 return -ENOMEM;
4341 mc.precharge++;
4342 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004343 return ret;
4344}
4345
4346/**
4347 * is_target_pte_for_mc - check a pte whether it is valid for move charge
4348 * @vma: the vma the pte to be checked belongs
4349 * @addr: the address corresponding to the pte to be checked
4350 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08004351 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004352 *
4353 * Returns
4354 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
4355 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
4356 * move charge. if @target is not NULL, the page is stored in target->page
4357 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08004358 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
4359 * target for charge migration. if @target is not NULL, the entry is stored
4360 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004361 *
4362 * Called with pte lock held.
4363 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004364union mc_target {
4365 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004366 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004367};
4368
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004369enum mc_target_type {
4370 MC_TARGET_NONE, /* not used */
4371 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08004372 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004373};
4374
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004375static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
4376 unsigned long addr, pte_t ptent)
4377{
4378 struct page *page = vm_normal_page(vma, addr, ptent);
4379
4380 if (!page || !page_mapped(page))
4381 return NULL;
4382 if (PageAnon(page)) {
4383 /* we don't move shared anon */
4384 if (!move_anon() || page_mapcount(page) > 2)
4385 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004386 } else if (!move_file())
4387 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004388 return NULL;
4389 if (!get_page_unless_zero(page))
4390 return NULL;
4391
4392 return page;
4393}
4394
4395static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
4396 unsigned long addr, pte_t ptent, swp_entry_t *entry)
4397{
4398 int usage_count;
4399 struct page *page = NULL;
4400 swp_entry_t ent = pte_to_swp_entry(ptent);
4401
4402 if (!move_anon() || non_swap_entry(ent))
4403 return NULL;
4404 usage_count = mem_cgroup_count_swap_user(ent, &page);
4405 if (usage_count > 1) { /* we don't move shared anon */
4406 if (page)
4407 put_page(page);
4408 return NULL;
4409 }
4410 if (do_swap_account)
4411 entry->val = ent.val;
4412
4413 return page;
4414}
4415
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004416static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
4417 unsigned long addr, pte_t ptent, swp_entry_t *entry)
4418{
4419 struct page *page = NULL;
4420 struct inode *inode;
4421 struct address_space *mapping;
4422 pgoff_t pgoff;
4423
4424 if (!vma->vm_file) /* anonymous vma */
4425 return NULL;
4426 if (!move_file())
4427 return NULL;
4428
4429 inode = vma->vm_file->f_path.dentry->d_inode;
4430 mapping = vma->vm_file->f_mapping;
4431 if (pte_none(ptent))
4432 pgoff = linear_page_index(vma, addr);
4433 else /* pte_file(ptent) is true */
4434 pgoff = pte_to_pgoff(ptent);
4435
4436 /* page is moved even if it's not RSS of this task(page-faulted). */
4437 if (!mapping_cap_swap_backed(mapping)) { /* normal file */
4438 page = find_get_page(mapping, pgoff);
4439 } else { /* shmem/tmpfs file. we should take account of swap too. */
4440 swp_entry_t ent;
4441 mem_cgroup_get_shmem_target(inode, pgoff, &page, &ent);
4442 if (do_swap_account)
4443 entry->val = ent.val;
4444 }
4445
4446 return page;
4447}
4448
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004449static int is_target_pte_for_mc(struct vm_area_struct *vma,
4450 unsigned long addr, pte_t ptent, union mc_target *target)
4451{
Daisuke Nishimura02491442010-03-10 15:22:17 -08004452 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004453 struct page_cgroup *pc;
4454 int ret = 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004455 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004456
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004457 if (pte_present(ptent))
4458 page = mc_handle_present_pte(vma, addr, ptent);
4459 else if (is_swap_pte(ptent))
4460 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004461 else if (pte_none(ptent) || pte_file(ptent))
4462 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004463
4464 if (!page && !ent.val)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004465 return 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004466 if (page) {
4467 pc = lookup_page_cgroup(page);
4468 /*
4469 * Do only loose check w/o page_cgroup lock.
4470 * mem_cgroup_move_account() checks the pc is valid or not under
4471 * the lock.
4472 */
4473 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
4474 ret = MC_TARGET_PAGE;
4475 if (target)
4476 target->page = page;
4477 }
4478 if (!ret || !target)
4479 put_page(page);
4480 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004481 /* There is a swap entry and a page doesn't exist or isn't charged */
4482 if (ent.val && !ret &&
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07004483 css_id(&mc.from->css) == lookup_swap_cgroup(ent)) {
4484 ret = MC_TARGET_SWAP;
4485 if (target)
4486 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004487 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004488 return ret;
4489}
4490
4491static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
4492 unsigned long addr, unsigned long end,
4493 struct mm_walk *walk)
4494{
4495 struct vm_area_struct *vma = walk->private;
4496 pte_t *pte;
4497 spinlock_t *ptl;
4498
4499 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
4500 for (; addr != end; pte++, addr += PAGE_SIZE)
4501 if (is_target_pte_for_mc(vma, addr, *pte, NULL))
4502 mc.precharge++; /* increment precharge temporarily */
4503 pte_unmap_unlock(pte - 1, ptl);
4504 cond_resched();
4505
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004506 return 0;
4507}
4508
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004509static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
4510{
4511 unsigned long precharge;
4512 struct vm_area_struct *vma;
4513
4514 down_read(&mm->mmap_sem);
4515 for (vma = mm->mmap; vma; vma = vma->vm_next) {
4516 struct mm_walk mem_cgroup_count_precharge_walk = {
4517 .pmd_entry = mem_cgroup_count_precharge_pte_range,
4518 .mm = mm,
4519 .private = vma,
4520 };
4521 if (is_vm_hugetlb_page(vma))
4522 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004523 walk_page_range(vma->vm_start, vma->vm_end,
4524 &mem_cgroup_count_precharge_walk);
4525 }
4526 up_read(&mm->mmap_sem);
4527
4528 precharge = mc.precharge;
4529 mc.precharge = 0;
4530
4531 return precharge;
4532}
4533
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004534static int mem_cgroup_precharge_mc(struct mm_struct *mm)
4535{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004536 return mem_cgroup_do_precharge(mem_cgroup_count_precharge(mm));
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004537}
4538
4539static void mem_cgroup_clear_mc(void)
4540{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07004541 struct mem_cgroup *from = mc.from;
4542 struct mem_cgroup *to = mc.to;
4543
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004544 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004545 if (mc.precharge) {
4546 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
4547 mc.precharge = 0;
4548 }
4549 /*
4550 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
4551 * we must uncharge here.
4552 */
4553 if (mc.moved_charge) {
4554 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
4555 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004556 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004557 /* we must fixup refcnts and charges */
4558 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004559 /* uncharge swap account from the old cgroup */
4560 if (!mem_cgroup_is_root(mc.from))
4561 res_counter_uncharge(&mc.from->memsw,
4562 PAGE_SIZE * mc.moved_swap);
4563 __mem_cgroup_put(mc.from, mc.moved_swap);
4564
4565 if (!mem_cgroup_is_root(mc.to)) {
4566 /*
4567 * we charged both to->res and to->memsw, so we should
4568 * uncharge to->res.
4569 */
4570 res_counter_uncharge(&mc.to->res,
4571 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004572 }
4573 /* we've already done mem_cgroup_get(mc.to) */
4574
4575 mc.moved_swap = 0;
4576 }
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07004577 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004578 mc.from = NULL;
4579 mc.to = NULL;
Daisuke Nishimura8033b972010-03-10 15:22:16 -08004580 mc.moving_task = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07004581 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07004582 mem_cgroup_end_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07004583 memcg_oom_recover(from);
4584 memcg_oom_recover(to);
Daisuke Nishimura8033b972010-03-10 15:22:16 -08004585 wake_up_all(&mc.waitq);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004586}
4587
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004588static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
4589 struct cgroup *cgroup,
4590 struct task_struct *p,
4591 bool threadgroup)
4592{
4593 int ret = 0;
4594 struct mem_cgroup *mem = mem_cgroup_from_cont(cgroup);
4595
4596 if (mem->move_charge_at_immigrate) {
4597 struct mm_struct *mm;
4598 struct mem_cgroup *from = mem_cgroup_from_task(p);
4599
4600 VM_BUG_ON(from == mem);
4601
4602 mm = get_task_mm(p);
4603 if (!mm)
4604 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004605 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004606 if (mm->owner == p) {
4607 VM_BUG_ON(mc.from);
4608 VM_BUG_ON(mc.to);
4609 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004610 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004611 VM_BUG_ON(mc.moved_swap);
Daisuke Nishimura8033b972010-03-10 15:22:16 -08004612 VM_BUG_ON(mc.moving_task);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07004613 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07004614 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004615 mc.from = from;
4616 mc.to = mem;
4617 mc.precharge = 0;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004618 mc.moved_charge = 0;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004619 mc.moved_swap = 0;
Daisuke Nishimura8033b972010-03-10 15:22:16 -08004620 mc.moving_task = current;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07004621 spin_unlock(&mc.lock);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004622
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004623 ret = mem_cgroup_precharge_mc(mm);
4624 if (ret)
4625 mem_cgroup_clear_mc();
4626 }
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004627 mmput(mm);
4628 }
4629 return ret;
4630}
4631
4632static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
4633 struct cgroup *cgroup,
4634 struct task_struct *p,
4635 bool threadgroup)
4636{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004637 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004638}
4639
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004640static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
4641 unsigned long addr, unsigned long end,
4642 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004643{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004644 int ret = 0;
4645 struct vm_area_struct *vma = walk->private;
4646 pte_t *pte;
4647 spinlock_t *ptl;
4648
4649retry:
4650 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
4651 for (; addr != end; addr += PAGE_SIZE) {
4652 pte_t ptent = *(pte++);
4653 union mc_target target;
4654 int type;
4655 struct page *page;
4656 struct page_cgroup *pc;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004657 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004658
4659 if (!mc.precharge)
4660 break;
4661
4662 type = is_target_pte_for_mc(vma, addr, ptent, &target);
4663 switch (type) {
4664 case MC_TARGET_PAGE:
4665 page = target.page;
4666 if (isolate_lru_page(page))
4667 goto put;
4668 pc = lookup_page_cgroup(page);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004669 if (!mem_cgroup_move_account(pc,
4670 mc.from, mc.to, false)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004671 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004672 /* we uncharge from mc.from later. */
4673 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004674 }
4675 putback_lru_page(page);
4676put: /* is_target_pte_for_mc() gets the page */
4677 put_page(page);
4678 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004679 case MC_TARGET_SWAP:
4680 ent = target.ent;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004681 if (!mem_cgroup_move_swap_account(ent,
4682 mc.from, mc.to, false)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08004683 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004684 /* we fixup refcnts and charges later. */
4685 mc.moved_swap++;
4686 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08004687 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004688 default:
4689 break;
4690 }
4691 }
4692 pte_unmap_unlock(pte - 1, ptl);
4693 cond_resched();
4694
4695 if (addr != end) {
4696 /*
4697 * We have consumed all precharges we got in can_attach().
4698 * We try charge one by one, but don't do any additional
4699 * charges to mc.to if we have failed in charge once in attach()
4700 * phase.
4701 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004702 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004703 if (!ret)
4704 goto retry;
4705 }
4706
4707 return ret;
4708}
4709
4710static void mem_cgroup_move_charge(struct mm_struct *mm)
4711{
4712 struct vm_area_struct *vma;
4713
4714 lru_add_drain_all();
4715 down_read(&mm->mmap_sem);
4716 for (vma = mm->mmap; vma; vma = vma->vm_next) {
4717 int ret;
4718 struct mm_walk mem_cgroup_move_charge_walk = {
4719 .pmd_entry = mem_cgroup_move_charge_pte_range,
4720 .mm = mm,
4721 .private = vma,
4722 };
4723 if (is_vm_hugetlb_page(vma))
4724 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004725 ret = walk_page_range(vma->vm_start, vma->vm_end,
4726 &mem_cgroup_move_charge_walk);
4727 if (ret)
4728 /*
4729 * means we have consumed all precharges and failed in
4730 * doing additional charge. Just abandon here.
4731 */
4732 break;
4733 }
4734 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004735}
4736
Balbir Singh67e465a2008-02-07 00:13:54 -08004737static void mem_cgroup_move_task(struct cgroup_subsys *ss,
4738 struct cgroup *cont,
4739 struct cgroup *old_cont,
Ben Blumbe367d02009-09-23 15:56:31 -07004740 struct task_struct *p,
4741 bool threadgroup)
Balbir Singh67e465a2008-02-07 00:13:54 -08004742{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004743 struct mm_struct *mm;
4744
4745 if (!mc.to)
4746 /* no need to move charge */
4747 return;
4748
4749 mm = get_task_mm(p);
4750 if (mm) {
4751 mem_cgroup_move_charge(mm);
4752 mmput(mm);
4753 }
4754 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08004755}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07004756#else /* !CONFIG_MMU */
4757static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
4758 struct cgroup *cgroup,
4759 struct task_struct *p,
4760 bool threadgroup)
4761{
4762 return 0;
4763}
4764static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
4765 struct cgroup *cgroup,
4766 struct task_struct *p,
4767 bool threadgroup)
4768{
4769}
4770static void mem_cgroup_move_task(struct cgroup_subsys *ss,
4771 struct cgroup *cont,
4772 struct cgroup *old_cont,
4773 struct task_struct *p,
4774 bool threadgroup)
4775{
4776}
4777#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08004778
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004779struct cgroup_subsys mem_cgroup_subsys = {
4780 .name = "memory",
4781 .subsys_id = mem_cgroup_subsys_id,
4782 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004783 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004784 .destroy = mem_cgroup_destroy,
4785 .populate = mem_cgroup_populate,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004786 .can_attach = mem_cgroup_can_attach,
4787 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08004788 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004789 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004790 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004791};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004792
4793#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4794
4795static int __init disable_swap_account(char *s)
4796{
4797 really_do_swap_account = 0;
4798 return 1;
4799}
4800__setup("noswapaccount", disable_swap_account);
4801#endif