blob: ddffc74cdebe50f1d83a592a6d905ad0ff011f16 [file] [log] [blame]
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001/* memcontrol.c - Memory Controller
2 *
3 * Copyright IBM Corporation, 2007
4 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
5 *
Pavel Emelianov78fb7462008-02-07 00:13:51 -08006 * Copyright 2007 OpenVZ SWsoft Inc
7 * Author: Pavel Emelianov <xemul@openvz.org>
8 *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08009 * Memory thresholds
10 * Copyright (C) 2009 Nokia Corporation
11 * Author: Kirill A. Shutemov
12 *
Balbir Singh8cdea7c2008-02-07 00:13:50 -080013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 */
23
24#include <linux/res_counter.h>
25#include <linux/memcontrol.h>
26#include <linux/cgroup.h>
Pavel Emelianov78fb7462008-02-07 00:13:51 -080027#include <linux/mm.h>
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -080028#include <linux/hugetlb.h>
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -080029#include <linux/pagemap.h>
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080030#include <linux/smp.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080031#include <linux/page-flags.h>
Balbir Singh66e17072008-02-07 00:13:56 -080032#include <linux/backing-dev.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080033#include <linux/bit_spinlock.h>
34#include <linux/rcupdate.h>
Balbir Singhe2224322009-04-02 16:57:39 -070035#include <linux/limits.h>
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -080036#include <linux/mutex.h>
Balbir Singhf64c3f52009-09-23 15:56:37 -070037#include <linux/rbtree.h>
Hugh Dickins072441e2011-06-27 16:18:02 -070038#include <linux/shmem_fs.h>
Balbir Singhb6ac57d2008-04-29 01:00:19 -070039#include <linux/slab.h>
Balbir Singh66e17072008-02-07 00:13:56 -080040#include <linux/swap.h>
Daisuke Nishimura02491442010-03-10 15:22:17 -080041#include <linux/swapops.h>
Balbir Singh66e17072008-02-07 00:13:56 -080042#include <linux/spinlock.h>
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -080043#include <linux/eventfd.h>
44#include <linux/sort.h>
Balbir Singh66e17072008-02-07 00:13:56 -080045#include <linux/fs.h>
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -080046#include <linux/seq_file.h>
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -070047#include <linux/vmalloc.h>
Christoph Lameterb69408e2008-10-18 20:26:14 -070048#include <linux/mm_inline.h>
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070049#include <linux/page_cgroup.h>
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -080050#include <linux/cpu.h>
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -070051#include <linux/oom.h>
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -080052#include "internal.h"
Balbir Singh8cdea7c2008-02-07 00:13:50 -080053
Balbir Singh8697d332008-02-07 00:13:59 -080054#include <asm/uaccess.h>
55
KOSAKI Motohirocc8e9702010-08-09 17:19:57 -070056#include <trace/events/vmscan.h>
57
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070058struct cgroup_subsys mem_cgroup_subsys __read_mostly;
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070059#define MEM_CGROUP_RECLAIM_RETRIES 5
Balbir Singh4b3bde42009-09-23 15:56:32 -070060struct mem_cgroup *root_mem_cgroup __read_mostly;
Balbir Singh8cdea7c2008-02-07 00:13:50 -080061
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080062#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
Li Zefan338c8432009-06-17 16:27:15 -070063/* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080064int do_swap_account __read_mostly;
Michal Hockoa42c3902010-11-24 12:57:08 -080065
66/* for remember boot option*/
67#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP_ENABLED
68static int really_do_swap_account __initdata = 1;
69#else
70static int really_do_swap_account __initdata = 0;
71#endif
72
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080073#else
74#define do_swap_account (0)
75#endif
76
77
Balbir Singh8cdea7c2008-02-07 00:13:50 -080078/*
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080079 * Statistics for memory cgroup.
80 */
81enum mem_cgroup_stat_index {
82 /*
83 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
84 */
85 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
Balbir Singhd69b0422009-06-17 16:26:34 -070086 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -080087 MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */
Balbir Singh0c3e73e2009-09-23 15:56:42 -070088 MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -070089 MEM_CGROUP_STAT_DATA, /* end of data requires synchronization */
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -070090 MEM_CGROUP_ON_MOVE, /* someone is moving account between groups */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080091 MEM_CGROUP_STAT_NSTATS,
92};
93
Johannes Weinere9f89742011-03-23 16:42:37 -070094enum mem_cgroup_events_index {
95 MEM_CGROUP_EVENTS_PGPGIN, /* # of pages paged in */
96 MEM_CGROUP_EVENTS_PGPGOUT, /* # of pages paged out */
97 MEM_CGROUP_EVENTS_COUNT, /* # of pages paged in/out */
Ying Han456f9982011-05-26 16:25:38 -070098 MEM_CGROUP_EVENTS_PGFAULT, /* # of page-faults */
99 MEM_CGROUP_EVENTS_PGMAJFAULT, /* # of major page-faults */
Johannes Weinere9f89742011-03-23 16:42:37 -0700100 MEM_CGROUP_EVENTS_NSTATS,
101};
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700102/*
103 * Per memcg event counter is incremented at every pagein/pageout. With THP,
104 * it will be incremated by the number of pages. This counter is used for
105 * for trigger some periodic events. This is straightforward and better
106 * than using jiffies etc. to handle periodic memcg event.
107 */
108enum mem_cgroup_events_target {
109 MEM_CGROUP_TARGET_THRESH,
110 MEM_CGROUP_TARGET_SOFTLIMIT,
111 MEM_CGROUP_NTARGETS,
112};
113#define THRESHOLDS_EVENTS_TARGET (128)
114#define SOFTLIMIT_EVENTS_TARGET (1024)
Johannes Weinere9f89742011-03-23 16:42:37 -0700115
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800116struct mem_cgroup_stat_cpu {
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700117 long count[MEM_CGROUP_STAT_NSTATS];
Johannes Weinere9f89742011-03-23 16:42:37 -0700118 unsigned long events[MEM_CGROUP_EVENTS_NSTATS];
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700119 unsigned long targets[MEM_CGROUP_NTARGETS];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800120};
121
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800122/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800123 * per-zone information in memory controller.
124 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800125struct mem_cgroup_per_zone {
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800126 /*
127 * spin_lock to protect the per cgroup LRU
128 */
Christoph Lameterb69408e2008-10-18 20:26:14 -0700129 struct list_head lists[NR_LRU_LISTS];
130 unsigned long count[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800131
132 struct zone_reclaim_stat reclaim_stat;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700133 struct rb_node tree_node; /* RB tree node */
134 unsigned long long usage_in_excess;/* Set to the value by which */
135 /* the soft limit is exceeded*/
136 bool on_tree;
Balbir Singh4e416952009-09-23 15:56:39 -0700137 struct mem_cgroup *mem; /* Back pointer, we cannot */
138 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800139};
140/* Macro for accessing counter */
141#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
142
143struct mem_cgroup_per_node {
144 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
145};
146
147struct mem_cgroup_lru_info {
148 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
149};
150
151/*
Balbir Singhf64c3f52009-09-23 15:56:37 -0700152 * Cgroups above their limits are maintained in a RB-Tree, independent of
153 * their hierarchy representation
154 */
155
156struct mem_cgroup_tree_per_zone {
157 struct rb_root rb_root;
158 spinlock_t lock;
159};
160
161struct mem_cgroup_tree_per_node {
162 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
163};
164
165struct mem_cgroup_tree {
166 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
167};
168
169static struct mem_cgroup_tree soft_limit_tree __read_mostly;
170
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800171struct mem_cgroup_threshold {
172 struct eventfd_ctx *eventfd;
173 u64 threshold;
174};
175
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700176/* For threshold */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800177struct mem_cgroup_threshold_ary {
178 /* An array index points to threshold just below usage. */
Phil Carmody5407a562010-05-26 14:42:42 -0700179 int current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800180 /* Size of entries[] */
181 unsigned int size;
182 /* Array of thresholds */
183 struct mem_cgroup_threshold entries[0];
184};
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700185
186struct mem_cgroup_thresholds {
187 /* Primary thresholds array */
188 struct mem_cgroup_threshold_ary *primary;
189 /*
190 * Spare threshold array.
191 * This is needed to make mem_cgroup_unregister_event() "never fail".
192 * It must be able to store at least primary->size - 1 entries.
193 */
194 struct mem_cgroup_threshold_ary *spare;
195};
196
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700197/* for OOM */
198struct mem_cgroup_eventfd_list {
199 struct list_head list;
200 struct eventfd_ctx *eventfd;
201};
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800202
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800203static void mem_cgroup_threshold(struct mem_cgroup *mem);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700204static void mem_cgroup_oom_notify(struct mem_cgroup *mem);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800205
Balbir Singhf64c3f52009-09-23 15:56:37 -0700206/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800207 * The memory controller data structure. The memory controller controls both
208 * page cache and RSS per cgroup. We would eventually like to provide
209 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
210 * to help the administrator determine what knobs to tune.
211 *
212 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800213 * we hit the water mark. May be even add a low water mark, such that
214 * no reclaim occurs from a cgroup at it's low water mark, this is
215 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800216 */
217struct mem_cgroup {
218 struct cgroup_subsys_state css;
219 /*
220 * the counter to account for memory usage
221 */
222 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800223 /*
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800224 * the counter to account for mem+swap usage.
225 */
226 struct res_counter memsw;
227 /*
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800228 * Per cgroup active and inactive list, similar to the
229 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800230 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800231 struct mem_cgroup_lru_info info;
Balbir Singh6d61ef42009-01-07 18:08:06 -0800232 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200233 * While reclaiming in a hierarchy, we cache the last child we
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700234 * reclaimed from.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800235 */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700236 int last_scanned_child;
Ying Han889976d2011-05-26 16:25:33 -0700237 int last_scanned_node;
238#if MAX_NUMNODES > 1
239 nodemask_t scan_nodes;
240 unsigned long next_scan_node_update;
241#endif
Balbir Singh18f59ea2009-01-07 18:08:07 -0800242 /*
243 * Should the accounting and control be hierarchical, per subtree?
244 */
245 bool use_hierarchy;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -0800246 atomic_t oom_lock;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800247 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800248
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800249 unsigned int swappiness;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -0700250 /* OOM-Killer disable */
251 int oom_kill_disable;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800252
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700253 /* set when res.limit == memsw.limit */
254 bool memsw_is_minimum;
255
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800256 /* protect arrays of thresholds */
257 struct mutex thresholds_lock;
258
259 /* thresholds for memory usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700260 struct mem_cgroup_thresholds thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700261
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800262 /* thresholds for mem+swap usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700263 struct mem_cgroup_thresholds memsw_thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700264
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700265 /* For oom notifier event fd */
266 struct list_head oom_notify;
267
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800268 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800269 * Should we move charges of a task when a task is moved into this
270 * mem_cgroup ? And what type of charges should we move ?
271 */
272 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800273 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800274 * percpu counter.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800275 */
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800276 struct mem_cgroup_stat_cpu *stat;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700277 /*
278 * used when a cpu is offlined or other synchronizations
279 * See mem_cgroup_read_stat().
280 */
281 struct mem_cgroup_stat_cpu nocpu_base;
282 spinlock_t pcp_counter_lock;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800283};
284
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800285/* Stuffs for move charges at task migration. */
286/*
287 * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a
288 * left-shifted bitmap of these types.
289 */
290enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800291 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700292 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800293 NR_MOVE_TYPE,
294};
295
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800296/* "mc" and its members are protected by cgroup_mutex */
297static struct move_charge_struct {
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -0800298 spinlock_t lock; /* for from, to */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800299 struct mem_cgroup *from;
300 struct mem_cgroup *to;
301 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800302 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800303 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800304 struct task_struct *moving_task; /* a task moving charges */
305 wait_queue_head_t waitq; /* a waitq for other context */
306} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700307 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800308 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
309};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800310
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700311static bool move_anon(void)
312{
313 return test_bit(MOVE_CHARGE_TYPE_ANON,
314 &mc.to->move_charge_at_immigrate);
315}
316
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700317static bool move_file(void)
318{
319 return test_bit(MOVE_CHARGE_TYPE_FILE,
320 &mc.to->move_charge_at_immigrate);
321}
322
Balbir Singh4e416952009-09-23 15:56:39 -0700323/*
324 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
325 * limit reclaim to prevent infinite loops, if they ever occur.
326 */
327#define MEM_CGROUP_MAX_RECLAIM_LOOPS (100)
328#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2)
329
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800330enum charge_type {
331 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
332 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700333 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700334 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800335 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700336 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700337 NR_CHARGE_TYPE,
338};
339
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800340/* for encoding cft->private value on file */
341#define _MEM (0)
342#define _MEMSWAP (1)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700343#define _OOM_TYPE (2)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800344#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
345#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
346#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700347/* Used for OOM nofiier */
348#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800349
Balbir Singh75822b42009-09-23 15:56:38 -0700350/*
351 * Reclaim flags for mem_cgroup_hierarchical_reclaim
352 */
353#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
354#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
355#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
356#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
Balbir Singh4e416952009-09-23 15:56:39 -0700357#define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
358#define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
Balbir Singh75822b42009-09-23 15:56:38 -0700359
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800360static void mem_cgroup_get(struct mem_cgroup *mem);
361static void mem_cgroup_put(struct mem_cgroup *mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -0800362static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -0700363static void drain_all_stock_async(struct mem_cgroup *mem);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800364
Balbir Singhf64c3f52009-09-23 15:56:37 -0700365static struct mem_cgroup_per_zone *
366mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
367{
368 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
369}
370
Wu Fengguangd3242362009-12-16 12:19:59 +0100371struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *mem)
372{
373 return &mem->css;
374}
375
Balbir Singhf64c3f52009-09-23 15:56:37 -0700376static struct mem_cgroup_per_zone *
Johannes Weiner97a6c372011-03-23 16:42:27 -0700377page_cgroup_zoneinfo(struct mem_cgroup *mem, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700378{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700379 int nid = page_to_nid(page);
380 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700381
Balbir Singhf64c3f52009-09-23 15:56:37 -0700382 return mem_cgroup_zoneinfo(mem, nid, zid);
383}
384
385static struct mem_cgroup_tree_per_zone *
386soft_limit_tree_node_zone(int nid, int zid)
387{
388 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
389}
390
391static struct mem_cgroup_tree_per_zone *
392soft_limit_tree_from_page(struct page *page)
393{
394 int nid = page_to_nid(page);
395 int zid = page_zonenum(page);
396
397 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
398}
399
400static void
Balbir Singh4e416952009-09-23 15:56:39 -0700401__mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700402 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700403 struct mem_cgroup_tree_per_zone *mctz,
404 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700405{
406 struct rb_node **p = &mctz->rb_root.rb_node;
407 struct rb_node *parent = NULL;
408 struct mem_cgroup_per_zone *mz_node;
409
410 if (mz->on_tree)
411 return;
412
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700413 mz->usage_in_excess = new_usage_in_excess;
414 if (!mz->usage_in_excess)
415 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700416 while (*p) {
417 parent = *p;
418 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
419 tree_node);
420 if (mz->usage_in_excess < mz_node->usage_in_excess)
421 p = &(*p)->rb_left;
422 /*
423 * We can't avoid mem cgroups that are over their soft
424 * limit by the same amount
425 */
426 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
427 p = &(*p)->rb_right;
428 }
429 rb_link_node(&mz->tree_node, parent, p);
430 rb_insert_color(&mz->tree_node, &mctz->rb_root);
431 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700432}
433
434static void
435__mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
436 struct mem_cgroup_per_zone *mz,
437 struct mem_cgroup_tree_per_zone *mctz)
438{
439 if (!mz->on_tree)
440 return;
441 rb_erase(&mz->tree_node, &mctz->rb_root);
442 mz->on_tree = false;
443}
444
445static void
Balbir Singhf64c3f52009-09-23 15:56:37 -0700446mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
447 struct mem_cgroup_per_zone *mz,
448 struct mem_cgroup_tree_per_zone *mctz)
449{
450 spin_lock(&mctz->lock);
Balbir Singh4e416952009-09-23 15:56:39 -0700451 __mem_cgroup_remove_exceeded(mem, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700452 spin_unlock(&mctz->lock);
453}
454
Balbir Singhf64c3f52009-09-23 15:56:37 -0700455
456static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
457{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700458 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700459 struct mem_cgroup_per_zone *mz;
460 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700461 int nid = page_to_nid(page);
462 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700463 mctz = soft_limit_tree_from_page(page);
464
465 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700466 * Necessary to update all ancestors when hierarchy is used.
467 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700468 */
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700469 for (; mem; mem = parent_mem_cgroup(mem)) {
470 mz = mem_cgroup_zoneinfo(mem, nid, zid);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700471 excess = res_counter_soft_limit_excess(&mem->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700472 /*
473 * We have to update the tree if mz is on RB-tree or
474 * mem is over its softlimit.
475 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700476 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700477 spin_lock(&mctz->lock);
478 /* if on-tree, remove it */
479 if (mz->on_tree)
480 __mem_cgroup_remove_exceeded(mem, mz, mctz);
481 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700482 * Insert again. mz->usage_in_excess will be updated.
483 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700484 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700485 __mem_cgroup_insert_exceeded(mem, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700486 spin_unlock(&mctz->lock);
487 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700488 }
489}
490
491static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
492{
493 int node, zone;
494 struct mem_cgroup_per_zone *mz;
495 struct mem_cgroup_tree_per_zone *mctz;
496
497 for_each_node_state(node, N_POSSIBLE) {
498 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
499 mz = mem_cgroup_zoneinfo(mem, node, zone);
500 mctz = soft_limit_tree_node_zone(node, zone);
501 mem_cgroup_remove_exceeded(mem, mz, mctz);
502 }
503 }
504}
505
Balbir Singh4e416952009-09-23 15:56:39 -0700506static struct mem_cgroup_per_zone *
507__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
508{
509 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700510 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700511
512retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700513 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700514 rightmost = rb_last(&mctz->rb_root);
515 if (!rightmost)
516 goto done; /* Nothing to reclaim from */
517
518 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
519 /*
520 * Remove the node now but someone else can add it back,
521 * we will to add it back at the end of reclaim to its correct
522 * position in the tree.
523 */
524 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
525 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
526 !css_tryget(&mz->mem->css))
527 goto retry;
528done:
529 return mz;
530}
531
532static struct mem_cgroup_per_zone *
533mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
534{
535 struct mem_cgroup_per_zone *mz;
536
537 spin_lock(&mctz->lock);
538 mz = __mem_cgroup_largest_soft_limit_node(mctz);
539 spin_unlock(&mctz->lock);
540 return mz;
541}
542
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700543/*
544 * Implementation Note: reading percpu statistics for memcg.
545 *
546 * Both of vmstat[] and percpu_counter has threshold and do periodic
547 * synchronization to implement "quick" read. There are trade-off between
548 * reading cost and precision of value. Then, we may have a chance to implement
549 * a periodic synchronizion of counter in memcg's counter.
550 *
551 * But this _read() function is used for user interface now. The user accounts
552 * memory usage by memory cgroup and he _always_ requires exact value because
553 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
554 * have to visit all online cpus and make sum. So, for now, unnecessary
555 * synchronization is not implemented. (just implemented for cpu hotplug)
556 *
557 * If there are kernel internal actions which can make use of some not-exact
558 * value, and reading all cpu value can be performance bottleneck in some
559 * common workload, threashold and synchonization as vmstat[] should be
560 * implemented.
561 */
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700562static long mem_cgroup_read_stat(struct mem_cgroup *mem,
563 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800564{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700565 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800566 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800567
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700568 get_online_cpus();
569 for_each_online_cpu(cpu)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800570 val += per_cpu(mem->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700571#ifdef CONFIG_HOTPLUG_CPU
572 spin_lock(&mem->pcp_counter_lock);
573 val += mem->nocpu_base.count[idx];
574 spin_unlock(&mem->pcp_counter_lock);
575#endif
576 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800577 return val;
578}
579
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700580static long mem_cgroup_local_usage(struct mem_cgroup *mem)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800581{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700582 long ret;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800583
584 ret = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS);
585 ret += mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE);
586 return ret;
587}
588
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700589static void mem_cgroup_swap_statistics(struct mem_cgroup *mem,
590 bool charge)
591{
592 int val = (charge) ? 1 : -1;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800593 this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_SWAPOUT], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700594}
595
Ying Han456f9982011-05-26 16:25:38 -0700596void mem_cgroup_pgfault(struct mem_cgroup *mem, int val)
597{
598 this_cpu_add(mem->stat->events[MEM_CGROUP_EVENTS_PGFAULT], val);
599}
600
601void mem_cgroup_pgmajfault(struct mem_cgroup *mem, int val)
602{
603 this_cpu_add(mem->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT], val);
604}
605
Johannes Weinere9f89742011-03-23 16:42:37 -0700606static unsigned long mem_cgroup_read_events(struct mem_cgroup *mem,
607 enum mem_cgroup_events_index idx)
608{
609 unsigned long val = 0;
610 int cpu;
611
612 for_each_online_cpu(cpu)
613 val += per_cpu(mem->stat->events[idx], cpu);
614#ifdef CONFIG_HOTPLUG_CPU
615 spin_lock(&mem->pcp_counter_lock);
616 val += mem->nocpu_base.events[idx];
617 spin_unlock(&mem->pcp_counter_lock);
618#endif
619 return val;
620}
621
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700622static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800623 bool file, int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800624{
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800625 preempt_disable();
626
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800627 if (file)
628 __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_CACHE], nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800629 else
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800630 __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_RSS], nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700631
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800632 /* pagein of a big page is an event. So, ignore page size */
633 if (nr_pages > 0)
Johannes Weinere9f89742011-03-23 16:42:37 -0700634 __this_cpu_inc(mem->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800635 else {
Johannes Weinere9f89742011-03-23 16:42:37 -0700636 __this_cpu_inc(mem->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800637 nr_pages = -nr_pages; /* for event */
638 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800639
Johannes Weinere9f89742011-03-23 16:42:37 -0700640 __this_cpu_add(mem->stat->events[MEM_CGROUP_EVENTS_COUNT], nr_pages);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800641
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800642 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800643}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800644
Ying Han889976d2011-05-26 16:25:33 -0700645static unsigned long
646mem_cgroup_get_zonestat_node(struct mem_cgroup *mem, int nid, enum lru_list idx)
647{
648 struct mem_cgroup_per_zone *mz;
649 u64 total = 0;
650 int zid;
651
652 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
653 mz = mem_cgroup_zoneinfo(mem, nid, zid);
654 total += MEM_CGROUP_ZSTAT(mz, idx);
655 }
656 return total;
657}
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700658static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700659 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800660{
Ying Han889976d2011-05-26 16:25:33 -0700661 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800662 u64 total = 0;
663
664 for_each_online_node(nid)
Ying Han889976d2011-05-26 16:25:33 -0700665 total += mem_cgroup_get_zonestat_node(mem, nid, idx);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800666 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800667}
668
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700669static bool __memcg_event_check(struct mem_cgroup *mem, int target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800670{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700671 unsigned long val, next;
672
673 val = this_cpu_read(mem->stat->events[MEM_CGROUP_EVENTS_COUNT]);
674 next = this_cpu_read(mem->stat->targets[target]);
675 /* from time_after() in jiffies.h */
676 return ((long)next - (long)val < 0);
677}
678
679static void __mem_cgroup_target_update(struct mem_cgroup *mem, int target)
680{
681 unsigned long val, next;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800682
Johannes Weinere9f89742011-03-23 16:42:37 -0700683 val = this_cpu_read(mem->stat->events[MEM_CGROUP_EVENTS_COUNT]);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800684
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700685 switch (target) {
686 case MEM_CGROUP_TARGET_THRESH:
687 next = val + THRESHOLDS_EVENTS_TARGET;
688 break;
689 case MEM_CGROUP_TARGET_SOFTLIMIT:
690 next = val + SOFTLIMIT_EVENTS_TARGET;
691 break;
692 default:
693 return;
694 }
695
696 this_cpu_write(mem->stat->targets[target], next);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800697}
698
699/*
700 * Check events in order.
701 *
702 */
703static void memcg_check_events(struct mem_cgroup *mem, struct page *page)
704{
705 /* threshold event is triggered in finer grain than soft limit */
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700706 if (unlikely(__memcg_event_check(mem, MEM_CGROUP_TARGET_THRESH))) {
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800707 mem_cgroup_threshold(mem);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700708 __mem_cgroup_target_update(mem, MEM_CGROUP_TARGET_THRESH);
709 if (unlikely(__memcg_event_check(mem,
710 MEM_CGROUP_TARGET_SOFTLIMIT))){
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800711 mem_cgroup_update_tree(mem, page);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700712 __mem_cgroup_target_update(mem,
713 MEM_CGROUP_TARGET_SOFTLIMIT);
714 }
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800715 }
716}
717
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800718static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800719{
720 return container_of(cgroup_subsys_state(cont,
721 mem_cgroup_subsys_id), struct mem_cgroup,
722 css);
723}
724
Balbir Singhcf475ad2008-04-29 01:00:16 -0700725struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800726{
Balbir Singh31a78f22008-09-28 23:09:31 +0100727 /*
728 * mm_update_next_owner() may clear mm->owner to NULL
729 * if it races with swapoff, page migration, etc.
730 * So this can be called with p == NULL.
731 */
732 if (unlikely(!p))
733 return NULL;
734
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800735 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
736 struct mem_cgroup, css);
737}
738
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700739struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800740{
741 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700742
743 if (!mm)
744 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800745 /*
746 * Because we have no locks, mm->owner's may be being moved to other
747 * cgroup. We use css_tryget() here even if this looks
748 * pessimistic (rather than adding locks here).
749 */
750 rcu_read_lock();
751 do {
752 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
753 if (unlikely(!mem))
754 break;
755 } while (!css_tryget(&mem->css));
756 rcu_read_unlock();
757 return mem;
758}
759
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700760/* The caller has to guarantee "mem" exists before calling this */
761static struct mem_cgroup *mem_cgroup_start_loop(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700762{
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700763 struct cgroup_subsys_state *css;
764 int found;
765
766 if (!mem) /* ROOT cgroup has the smallest ID */
767 return root_mem_cgroup; /*css_put/get against root is ignored*/
768 if (!mem->use_hierarchy) {
769 if (css_tryget(&mem->css))
770 return mem;
771 return NULL;
772 }
773 rcu_read_lock();
774 /*
775 * searching a memory cgroup which has the smallest ID under given
776 * ROOT cgroup. (ID >= 1)
777 */
778 css = css_get_next(&mem_cgroup_subsys, 1, &mem->css, &found);
779 if (css && css_tryget(css))
780 mem = container_of(css, struct mem_cgroup, css);
781 else
782 mem = NULL;
783 rcu_read_unlock();
784 return mem;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700785}
786
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700787static struct mem_cgroup *mem_cgroup_get_next(struct mem_cgroup *iter,
788 struct mem_cgroup *root,
789 bool cond)
790{
791 int nextid = css_id(&iter->css) + 1;
792 int found;
793 int hierarchy_used;
794 struct cgroup_subsys_state *css;
795
796 hierarchy_used = iter->use_hierarchy;
797
798 css_put(&iter->css);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700799 /* If no ROOT, walk all, ignore hierarchy */
800 if (!cond || (root && !hierarchy_used))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700801 return NULL;
802
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700803 if (!root)
804 root = root_mem_cgroup;
805
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700806 do {
807 iter = NULL;
808 rcu_read_lock();
809
810 css = css_get_next(&mem_cgroup_subsys, nextid,
811 &root->css, &found);
812 if (css && css_tryget(css))
813 iter = container_of(css, struct mem_cgroup, css);
814 rcu_read_unlock();
815 /* If css is NULL, no more cgroups will be found */
816 nextid = found + 1;
817 } while (css && !iter);
818
819 return iter;
820}
821/*
822 * for_eacn_mem_cgroup_tree() for visiting all cgroup under tree. Please
823 * be careful that "break" loop is not allowed. We have reference count.
824 * Instead of that modify "cond" to be false and "continue" to exit the loop.
825 */
826#define for_each_mem_cgroup_tree_cond(iter, root, cond) \
827 for (iter = mem_cgroup_start_loop(root);\
828 iter != NULL;\
829 iter = mem_cgroup_get_next(iter, root, cond))
830
831#define for_each_mem_cgroup_tree(iter, root) \
832 for_each_mem_cgroup_tree_cond(iter, root, true)
833
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700834#define for_each_mem_cgroup_all(iter) \
835 for_each_mem_cgroup_tree_cond(iter, NULL, true)
836
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700837
Balbir Singh4b3bde42009-09-23 15:56:32 -0700838static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
839{
840 return (mem == root_mem_cgroup);
841}
842
Ying Han456f9982011-05-26 16:25:38 -0700843void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
844{
845 struct mem_cgroup *mem;
846
847 if (!mm)
848 return;
849
850 rcu_read_lock();
851 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
852 if (unlikely(!mem))
853 goto out;
854
855 switch (idx) {
856 case PGMAJFAULT:
857 mem_cgroup_pgmajfault(mem, 1);
858 break;
859 case PGFAULT:
860 mem_cgroup_pgfault(mem, 1);
861 break;
862 default:
863 BUG();
864 }
865out:
866 rcu_read_unlock();
867}
868EXPORT_SYMBOL(mem_cgroup_count_vm_event);
869
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800870/*
871 * Following LRU functions are allowed to be used without PCG_LOCK.
872 * Operations are called by routine of global LRU independently from memcg.
873 * What we have to take care of here is validness of pc->mem_cgroup.
874 *
875 * Changes to pc->mem_cgroup happens when
876 * 1. charge
877 * 2. moving account
878 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
879 * It is added to LRU before charge.
880 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
881 * When moving account, the page is not on LRU. It's isolated.
882 */
883
884void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800885{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800886 struct page_cgroup *pc;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800887 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700888
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800889 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800890 return;
891 pc = lookup_page_cgroup(page);
892 /* can happen while we handle swapcache. */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700893 if (!TestClearPageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800894 return;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700895 VM_BUG_ON(!pc->mem_cgroup);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800896 /*
897 * We don't check PCG_USED bit. It's cleared when the "page" is finally
898 * removed from global LRU.
899 */
Johannes Weiner97a6c372011-03-23 16:42:27 -0700900 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -0800901 /* huge page split is done under lru_lock. so, we have no races. */
902 MEM_CGROUP_ZSTAT(mz, lru) -= 1 << compound_order(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700903 if (mem_cgroup_is_root(pc->mem_cgroup))
904 return;
905 VM_BUG_ON(list_empty(&pc->lru));
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800906 list_del_init(&pc->lru);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800907}
908
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800909void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800910{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800911 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800912}
913
Minchan Kim3f58a822011-03-22 16:32:53 -0700914/*
915 * Writeback is about to end against a page which has been marked for immediate
916 * reclaim. If it still appears to be reclaimable, move it to the tail of the
917 * inactive list.
918 */
919void mem_cgroup_rotate_reclaimable_page(struct page *page)
920{
921 struct mem_cgroup_per_zone *mz;
922 struct page_cgroup *pc;
923 enum lru_list lru = page_lru(page);
924
925 if (mem_cgroup_disabled())
926 return;
927
928 pc = lookup_page_cgroup(page);
929 /* unused or root page is not rotated. */
930 if (!PageCgroupUsed(pc))
931 return;
932 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
933 smp_rmb();
934 if (mem_cgroup_is_root(pc->mem_cgroup))
935 return;
Johannes Weiner97a6c372011-03-23 16:42:27 -0700936 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
Minchan Kim3f58a822011-03-22 16:32:53 -0700937 list_move_tail(&pc->lru, &mz->lists[lru]);
938}
939
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800940void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800941{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800942 struct mem_cgroup_per_zone *mz;
943 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800944
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800945 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700946 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700947
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800948 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700949 /* unused or root page is not rotated. */
Johannes Weiner713735b42011-01-20 14:44:31 -0800950 if (!PageCgroupUsed(pc))
951 return;
952 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
953 smp_rmb();
954 if (mem_cgroup_is_root(pc->mem_cgroup))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800955 return;
Johannes Weiner97a6c372011-03-23 16:42:27 -0700956 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700957 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800958}
959
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800960void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
961{
962 struct page_cgroup *pc;
963 struct mem_cgroup_per_zone *mz;
964
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800965 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800966 return;
967 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700968 VM_BUG_ON(PageCgroupAcctLRU(pc));
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800969 if (!PageCgroupUsed(pc))
970 return;
Johannes Weiner713735b42011-01-20 14:44:31 -0800971 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
972 smp_rmb();
Johannes Weiner97a6c372011-03-23 16:42:27 -0700973 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -0800974 /* huge page split is done under lru_lock. so, we have no races. */
975 MEM_CGROUP_ZSTAT(mz, lru) += 1 << compound_order(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700976 SetPageCgroupAcctLRU(pc);
977 if (mem_cgroup_is_root(pc->mem_cgroup))
978 return;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800979 list_add(&pc->lru, &mz->lists[lru]);
980}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800981
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800982/*
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -0700983 * At handling SwapCache and other FUSE stuff, pc->mem_cgroup may be changed
984 * while it's linked to lru because the page may be reused after it's fully
985 * uncharged. To handle that, unlink page_cgroup from LRU when charge it again.
986 * It's done under lock_page and expected that zone->lru_lock isnever held.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800987 */
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -0700988static void mem_cgroup_lru_del_before_commit(struct page *page)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800989{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800990 unsigned long flags;
991 struct zone *zone = page_zone(page);
992 struct page_cgroup *pc = lookup_page_cgroup(page);
993
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -0700994 /*
995 * Doing this check without taking ->lru_lock seems wrong but this
996 * is safe. Because if page_cgroup's USED bit is unset, the page
997 * will not be added to any memcg's LRU. If page_cgroup's USED bit is
998 * set, the commit after this will fail, anyway.
999 * This all charge/uncharge is done under some mutual execustion.
1000 * So, we don't need to taking care of changes in USED bit.
1001 */
1002 if (likely(!PageLRU(page)))
1003 return;
1004
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001005 spin_lock_irqsave(&zone->lru_lock, flags);
1006 /*
1007 * Forget old LRU when this page_cgroup is *not* used. This Used bit
1008 * is guarded by lock_page() because the page is SwapCache.
1009 */
1010 if (!PageCgroupUsed(pc))
1011 mem_cgroup_del_lru_list(page, page_lru(page));
1012 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001013}
1014
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001015static void mem_cgroup_lru_add_after_commit(struct page *page)
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001016{
1017 unsigned long flags;
1018 struct zone *zone = page_zone(page);
1019 struct page_cgroup *pc = lookup_page_cgroup(page);
1020
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001021 /* taking care of that the page is added to LRU while we commit it */
1022 if (likely(!PageLRU(page)))
1023 return;
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001024 spin_lock_irqsave(&zone->lru_lock, flags);
1025 /* link when the page is linked to LRU but page_cgroup isn't */
Balbir Singh4b3bde42009-09-23 15:56:32 -07001026 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001027 mem_cgroup_add_lru_list(page, page_lru(page));
1028 spin_unlock_irqrestore(&zone->lru_lock, flags);
1029}
1030
1031
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001032void mem_cgroup_move_lists(struct page *page,
1033 enum lru_list from, enum lru_list to)
1034{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001035 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001036 return;
1037 mem_cgroup_del_lru_list(page, from);
1038 mem_cgroup_add_lru_list(page, to);
1039}
1040
David Rientjes4c4a2212008-02-07 00:14:06 -08001041int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
1042{
1043 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001044 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001045 struct task_struct *p;
David Rientjes4c4a2212008-02-07 00:14:06 -08001046
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001047 p = find_lock_task_mm(task);
1048 if (!p)
1049 return 0;
1050 curr = try_get_mem_cgroup_from_mm(p->mm);
1051 task_unlock(p);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001052 if (!curr)
1053 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001054 /*
1055 * We should check use_hierarchy of "mem" not "curr". Because checking
1056 * use_hierarchy of "curr" here make this function true if hierarchy is
1057 * enabled in "curr" and "curr" is a child of "mem" in *cgroup*
1058 * hierarchy(even if use_hierarchy is disabled in "mem").
1059 */
1060 if (mem->use_hierarchy)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001061 ret = css_is_ancestor(&curr->css, &mem->css);
1062 else
1063 ret = (curr == mem);
1064 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001065 return ret;
1066}
1067
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001068static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001069{
1070 unsigned long active;
1071 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001072 unsigned long gb;
1073 unsigned long inactive_ratio;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001074
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07001075 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
1076 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001077
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001078 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1079 if (gb)
1080 inactive_ratio = int_sqrt(10 * gb);
1081 else
1082 inactive_ratio = 1;
1083
1084 if (present_pages) {
1085 present_pages[0] = inactive;
1086 present_pages[1] = active;
1087 }
1088
1089 return inactive_ratio;
1090}
1091
1092int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
1093{
1094 unsigned long active;
1095 unsigned long inactive;
1096 unsigned long present_pages[2];
1097 unsigned long inactive_ratio;
1098
1099 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
1100
1101 inactive = present_pages[0];
1102 active = present_pages[1];
1103
1104 if (inactive * inactive_ratio < active)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001105 return 1;
1106
1107 return 0;
1108}
1109
Rik van Riel56e49d22009-06-16 15:32:28 -07001110int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
1111{
1112 unsigned long active;
1113 unsigned long inactive;
1114
1115 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
1116 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
1117
1118 return (active > inactive);
1119}
1120
Ying Han1bac1802011-05-26 16:25:36 -07001121unsigned long mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg,
1122 struct zone *zone,
1123 enum lru_list lru)
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -08001124{
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -07001125 int nid = zone_to_nid(zone);
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -08001126 int zid = zone_idx(zone);
1127 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
1128
1129 return MEM_CGROUP_ZSTAT(mz, lru);
1130}
1131
Ying Han406eb0c2011-05-26 16:25:37 -07001132#ifdef CONFIG_NUMA
1133static unsigned long mem_cgroup_node_nr_file_lru_pages(struct mem_cgroup *memcg,
1134 int nid)
1135{
1136 unsigned long ret;
1137
1138 ret = mem_cgroup_get_zonestat_node(memcg, nid, LRU_INACTIVE_FILE) +
1139 mem_cgroup_get_zonestat_node(memcg, nid, LRU_ACTIVE_FILE);
1140
1141 return ret;
1142}
1143
1144static unsigned long mem_cgroup_nr_file_lru_pages(struct mem_cgroup *memcg)
1145{
1146 u64 total = 0;
1147 int nid;
1148
1149 for_each_node_state(nid, N_HIGH_MEMORY)
1150 total += mem_cgroup_node_nr_file_lru_pages(memcg, nid);
1151
1152 return total;
1153}
1154
1155static unsigned long mem_cgroup_node_nr_anon_lru_pages(struct mem_cgroup *memcg,
1156 int nid)
1157{
1158 unsigned long ret;
1159
1160 ret = mem_cgroup_get_zonestat_node(memcg, nid, LRU_INACTIVE_ANON) +
1161 mem_cgroup_get_zonestat_node(memcg, nid, LRU_ACTIVE_ANON);
1162
1163 return ret;
1164}
1165
1166static unsigned long mem_cgroup_nr_anon_lru_pages(struct mem_cgroup *memcg)
1167{
1168 u64 total = 0;
1169 int nid;
1170
1171 for_each_node_state(nid, N_HIGH_MEMORY)
1172 total += mem_cgroup_node_nr_anon_lru_pages(memcg, nid);
1173
1174 return total;
1175}
1176
1177static unsigned long
1178mem_cgroup_node_nr_unevictable_lru_pages(struct mem_cgroup *memcg, int nid)
1179{
1180 return mem_cgroup_get_zonestat_node(memcg, nid, LRU_UNEVICTABLE);
1181}
1182
1183static unsigned long
1184mem_cgroup_nr_unevictable_lru_pages(struct mem_cgroup *memcg)
1185{
1186 u64 total = 0;
1187 int nid;
1188
1189 for_each_node_state(nid, N_HIGH_MEMORY)
1190 total += mem_cgroup_node_nr_unevictable_lru_pages(memcg, nid);
1191
1192 return total;
1193}
1194
1195static unsigned long mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
1196 int nid)
1197{
1198 enum lru_list l;
1199 u64 total = 0;
1200
1201 for_each_lru(l)
1202 total += mem_cgroup_get_zonestat_node(memcg, nid, l);
1203
1204 return total;
1205}
1206
1207static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg)
1208{
1209 u64 total = 0;
1210 int nid;
1211
1212 for_each_node_state(nid, N_HIGH_MEMORY)
1213 total += mem_cgroup_node_nr_lru_pages(memcg, nid);
1214
1215 return total;
1216}
1217#endif /* CONFIG_NUMA */
1218
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001219struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
1220 struct zone *zone)
1221{
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -07001222 int nid = zone_to_nid(zone);
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001223 int zid = zone_idx(zone);
1224 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
1225
1226 return &mz->reclaim_stat;
1227}
1228
1229struct zone_reclaim_stat *
1230mem_cgroup_get_reclaim_stat_from_page(struct page *page)
1231{
1232 struct page_cgroup *pc;
1233 struct mem_cgroup_per_zone *mz;
1234
1235 if (mem_cgroup_disabled())
1236 return NULL;
1237
1238 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -08001239 if (!PageCgroupUsed(pc))
1240 return NULL;
Johannes Weiner713735b42011-01-20 14:44:31 -08001241 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
1242 smp_rmb();
Johannes Weiner97a6c372011-03-23 16:42:27 -07001243 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001244 return &mz->reclaim_stat;
1245}
1246
Balbir Singh66e17072008-02-07 00:13:56 -08001247unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
1248 struct list_head *dst,
1249 unsigned long *scanned, int order,
1250 int mode, struct zone *z,
1251 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001252 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -08001253{
1254 unsigned long nr_taken = 0;
1255 struct page *page;
1256 unsigned long scan;
1257 LIST_HEAD(pc_list);
1258 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001259 struct page_cgroup *pc, *tmp;
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -07001260 int nid = zone_to_nid(z);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001261 int zid = zone_idx(z);
1262 struct mem_cgroup_per_zone *mz;
Johannes Weinerb7c46d12009-09-21 17:02:56 -07001263 int lru = LRU_FILE * file + active;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001264 int ret;
Balbir Singh66e17072008-02-07 00:13:56 -08001265
Balbir Singhcf475ad2008-04-29 01:00:16 -07001266 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001267 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001268 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -08001269
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001270 scan = 0;
1271 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -08001272 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001273 break;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001274
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001275 if (unlikely(!PageCgroupUsed(pc)))
1276 continue;
Johannes Weiner5564e882011-03-23 16:42:29 -07001277
Johannes Weiner6b3ae582011-03-23 16:42:30 -07001278 page = lookup_cgroup_page(pc);
Johannes Weiner5564e882011-03-23 16:42:29 -07001279
Hugh Dickins436c65412008-02-07 00:14:12 -08001280 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001281 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001282
Hugh Dickins436c65412008-02-07 00:14:12 -08001283 scan++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001284 ret = __isolate_lru_page(page, mode, file);
1285 switch (ret) {
1286 case 0:
Balbir Singh66e17072008-02-07 00:13:56 -08001287 list_move(&page->lru, dst);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001288 mem_cgroup_del_lru(page);
Rik van Riel2c888cf2011-01-13 15:47:13 -08001289 nr_taken += hpage_nr_pages(page);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001290 break;
1291 case -EBUSY:
1292 /* we don't affect global LRU but rotate in our LRU */
1293 mem_cgroup_rotate_lru_list(page, page_lru(page));
1294 break;
1295 default:
1296 break;
Balbir Singh66e17072008-02-07 00:13:56 -08001297 }
1298 }
1299
Balbir Singh66e17072008-02-07 00:13:56 -08001300 *scanned = scan;
KOSAKI Motohirocc8e9702010-08-09 17:19:57 -07001301
1302 trace_mm_vmscan_memcg_isolate(0, nr_to_scan, scan, nr_taken,
1303 0, 0, 0, mode);
1304
Balbir Singh66e17072008-02-07 00:13:56 -08001305 return nr_taken;
1306}
1307
Balbir Singh6d61ef42009-01-07 18:08:06 -08001308#define mem_cgroup_from_res_counter(counter, member) \
1309 container_of(counter, struct mem_cgroup, member)
1310
Johannes Weiner19942822011-02-01 15:52:43 -08001311/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001312 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
1313 * @mem: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001314 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001315 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001316 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001317 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001318static unsigned long mem_cgroup_margin(struct mem_cgroup *mem)
Johannes Weiner19942822011-02-01 15:52:43 -08001319{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001320 unsigned long long margin;
1321
1322 margin = res_counter_margin(&mem->res);
1323 if (do_swap_account)
1324 margin = min(margin, res_counter_margin(&mem->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001325 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001326}
1327
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001328static unsigned int get_swappiness(struct mem_cgroup *memcg)
1329{
1330 struct cgroup *cgrp = memcg->css.cgroup;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001331
1332 /* root ? */
1333 if (cgrp->parent == NULL)
1334 return vm_swappiness;
1335
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001336 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001337}
1338
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001339static void mem_cgroup_start_move(struct mem_cgroup *mem)
1340{
1341 int cpu;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001342
1343 get_online_cpus();
1344 spin_lock(&mem->pcp_counter_lock);
1345 for_each_online_cpu(cpu)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001346 per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) += 1;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001347 mem->nocpu_base.count[MEM_CGROUP_ON_MOVE] += 1;
1348 spin_unlock(&mem->pcp_counter_lock);
1349 put_online_cpus();
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001350
1351 synchronize_rcu();
1352}
1353
1354static void mem_cgroup_end_move(struct mem_cgroup *mem)
1355{
1356 int cpu;
1357
1358 if (!mem)
1359 return;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001360 get_online_cpus();
1361 spin_lock(&mem->pcp_counter_lock);
1362 for_each_online_cpu(cpu)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001363 per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) -= 1;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001364 mem->nocpu_base.count[MEM_CGROUP_ON_MOVE] -= 1;
1365 spin_unlock(&mem->pcp_counter_lock);
1366 put_online_cpus();
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001367}
1368/*
1369 * 2 routines for checking "mem" is under move_account() or not.
1370 *
1371 * mem_cgroup_stealed() - checking a cgroup is mc.from or not. This is used
1372 * for avoiding race in accounting. If true,
1373 * pc->mem_cgroup may be overwritten.
1374 *
1375 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1376 * under hierarchy of moving cgroups. This is for
1377 * waiting at hith-memory prressure caused by "move".
1378 */
1379
1380static bool mem_cgroup_stealed(struct mem_cgroup *mem)
1381{
1382 VM_BUG_ON(!rcu_read_lock_held());
1383 return this_cpu_read(mem->stat->count[MEM_CGROUP_ON_MOVE]) > 0;
1384}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001385
1386static bool mem_cgroup_under_move(struct mem_cgroup *mem)
1387{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001388 struct mem_cgroup *from;
1389 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001390 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001391 /*
1392 * Unlike task_move routines, we access mc.to, mc.from not under
1393 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1394 */
1395 spin_lock(&mc.lock);
1396 from = mc.from;
1397 to = mc.to;
1398 if (!from)
1399 goto unlock;
1400 if (from == mem || to == mem
1401 || (mem->use_hierarchy && css_is_ancestor(&from->css, &mem->css))
1402 || (mem->use_hierarchy && css_is_ancestor(&to->css, &mem->css)))
1403 ret = true;
1404unlock:
1405 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001406 return ret;
1407}
1408
1409static bool mem_cgroup_wait_acct_move(struct mem_cgroup *mem)
1410{
1411 if (mc.moving_task && current != mc.moving_task) {
1412 if (mem_cgroup_under_move(mem)) {
1413 DEFINE_WAIT(wait);
1414 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1415 /* moving charge context might have finished. */
1416 if (mc.moving_task)
1417 schedule();
1418 finish_wait(&mc.waitq, &wait);
1419 return true;
1420 }
1421 }
1422 return false;
1423}
1424
Balbir Singhe2224322009-04-02 16:57:39 -07001425/**
Kirill A. Shutemov6a6135b2010-03-10 15:22:25 -08001426 * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode.
Balbir Singhe2224322009-04-02 16:57:39 -07001427 * @memcg: The memory cgroup that went over limit
1428 * @p: Task that is going to be killed
1429 *
1430 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1431 * enabled
1432 */
1433void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1434{
1435 struct cgroup *task_cgrp;
1436 struct cgroup *mem_cgrp;
1437 /*
1438 * Need a buffer in BSS, can't rely on allocations. The code relies
1439 * on the assumption that OOM is serialized for memory controller.
1440 * If this assumption is broken, revisit this code.
1441 */
1442 static char memcg_name[PATH_MAX];
1443 int ret;
1444
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001445 if (!memcg || !p)
Balbir Singhe2224322009-04-02 16:57:39 -07001446 return;
1447
1448
1449 rcu_read_lock();
1450
1451 mem_cgrp = memcg->css.cgroup;
1452 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1453
1454 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1455 if (ret < 0) {
1456 /*
1457 * Unfortunately, we are unable to convert to a useful name
1458 * But we'll still print out the usage information
1459 */
1460 rcu_read_unlock();
1461 goto done;
1462 }
1463 rcu_read_unlock();
1464
1465 printk(KERN_INFO "Task in %s killed", memcg_name);
1466
1467 rcu_read_lock();
1468 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1469 if (ret < 0) {
1470 rcu_read_unlock();
1471 goto done;
1472 }
1473 rcu_read_unlock();
1474
1475 /*
1476 * Continues from above, so we don't need an KERN_ level
1477 */
1478 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1479done:
1480
1481 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1482 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1483 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1484 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1485 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1486 "failcnt %llu\n",
1487 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1488 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1489 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1490}
1491
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001492/*
1493 * This function returns the number of memcg under hierarchy tree. Returns
1494 * 1(self count) if no children.
1495 */
1496static int mem_cgroup_count_children(struct mem_cgroup *mem)
1497{
1498 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001499 struct mem_cgroup *iter;
1500
1501 for_each_mem_cgroup_tree(iter, mem)
1502 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001503 return num;
1504}
1505
Balbir Singh6d61ef42009-01-07 18:08:06 -08001506/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001507 * Return the memory (and swap, if configured) limit for a memcg.
1508 */
1509u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
1510{
1511 u64 limit;
1512 u64 memsw;
1513
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001514 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
1515 limit += total_swap_pages << PAGE_SHIFT;
1516
David Rientjesa63d83f2010-08-09 17:19:46 -07001517 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1518 /*
1519 * If memsw is finite and limits the amount of swap space available
1520 * to this memcg, return that limit.
1521 */
1522 return min(limit, memsw);
1523}
1524
1525/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001526 * Visit the first child (need not be the first child as per the ordering
1527 * of the cgroup list, since we track last_scanned_child) of @mem and use
1528 * that to reclaim free pages from.
1529 */
1530static struct mem_cgroup *
1531mem_cgroup_select_victim(struct mem_cgroup *root_mem)
1532{
1533 struct mem_cgroup *ret = NULL;
1534 struct cgroup_subsys_state *css;
1535 int nextid, found;
1536
1537 if (!root_mem->use_hierarchy) {
1538 css_get(&root_mem->css);
1539 ret = root_mem;
1540 }
1541
1542 while (!ret) {
1543 rcu_read_lock();
1544 nextid = root_mem->last_scanned_child + 1;
1545 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
1546 &found);
1547 if (css && css_tryget(css))
1548 ret = container_of(css, struct mem_cgroup, css);
1549
1550 rcu_read_unlock();
1551 /* Updates scanning parameter */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001552 if (!css) {
1553 /* this means start scan from ID:1 */
1554 root_mem->last_scanned_child = 0;
1555 } else
1556 root_mem->last_scanned_child = found;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001557 }
1558
1559 return ret;
1560}
1561
Ying Han889976d2011-05-26 16:25:33 -07001562#if MAX_NUMNODES > 1
1563
1564/*
1565 * Always updating the nodemask is not very good - even if we have an empty
1566 * list or the wrong list here, we can start from some node and traverse all
1567 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1568 *
1569 */
1570static void mem_cgroup_may_update_nodemask(struct mem_cgroup *mem)
1571{
1572 int nid;
1573
1574 if (time_after(mem->next_scan_node_update, jiffies))
1575 return;
1576
1577 mem->next_scan_node_update = jiffies + 10*HZ;
1578 /* make a nodemask where this memcg uses memory from */
1579 mem->scan_nodes = node_states[N_HIGH_MEMORY];
1580
1581 for_each_node_mask(nid, node_states[N_HIGH_MEMORY]) {
1582
1583 if (mem_cgroup_get_zonestat_node(mem, nid, LRU_INACTIVE_FILE) ||
1584 mem_cgroup_get_zonestat_node(mem, nid, LRU_ACTIVE_FILE))
1585 continue;
1586
1587 if (total_swap_pages &&
1588 (mem_cgroup_get_zonestat_node(mem, nid, LRU_INACTIVE_ANON) ||
1589 mem_cgroup_get_zonestat_node(mem, nid, LRU_ACTIVE_ANON)))
1590 continue;
1591 node_clear(nid, mem->scan_nodes);
1592 }
1593}
1594
1595/*
1596 * Selecting a node where we start reclaim from. Because what we need is just
1597 * reducing usage counter, start from anywhere is O,K. Considering
1598 * memory reclaim from current node, there are pros. and cons.
1599 *
1600 * Freeing memory from current node means freeing memory from a node which
1601 * we'll use or we've used. So, it may make LRU bad. And if several threads
1602 * hit limits, it will see a contention on a node. But freeing from remote
1603 * node means more costs for memory reclaim because of memory latency.
1604 *
1605 * Now, we use round-robin. Better algorithm is welcomed.
1606 */
1607int mem_cgroup_select_victim_node(struct mem_cgroup *mem)
1608{
1609 int node;
1610
1611 mem_cgroup_may_update_nodemask(mem);
1612 node = mem->last_scanned_node;
1613
1614 node = next_node(node, mem->scan_nodes);
1615 if (node == MAX_NUMNODES)
1616 node = first_node(mem->scan_nodes);
1617 /*
1618 * We call this when we hit limit, not when pages are added to LRU.
1619 * No LRU may hold pages because all pages are UNEVICTABLE or
1620 * memcg is too small and all pages are not on LRU. In that case,
1621 * we use curret node.
1622 */
1623 if (unlikely(node == MAX_NUMNODES))
1624 node = numa_node_id();
1625
1626 mem->last_scanned_node = node;
1627 return node;
1628}
1629
1630#else
1631int mem_cgroup_select_victim_node(struct mem_cgroup *mem)
1632{
1633 return 0;
1634}
1635#endif
1636
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001637/*
1638 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1639 * we reclaimed from, so that we don't end up penalizing one child extensively
1640 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001641 *
1642 * root_mem is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001643 *
1644 * We give up and return to the caller when we visit root_mem twice.
1645 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001646 *
1647 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001648 */
1649static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
Balbir Singh4e416952009-09-23 15:56:39 -07001650 struct zone *zone,
Balbir Singh75822b42009-09-23 15:56:38 -07001651 gfp_t gfp_mask,
Ying Han0ae5e892011-05-26 16:25:25 -07001652 unsigned long reclaim_options,
1653 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001654{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001655 struct mem_cgroup *victim;
1656 int ret, total = 0;
1657 int loop = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001658 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1659 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
Balbir Singh4e416952009-09-23 15:56:39 -07001660 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001661 unsigned long excess;
Ying Han0ae5e892011-05-26 16:25:25 -07001662 unsigned long nr_scanned;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001663
1664 excess = res_counter_soft_limit_excess(&root_mem->res) >> PAGE_SHIFT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001665
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001666 /* If memsw_is_minimum==1, swap-out is of-no-use. */
KAMEZAWA Hiroyuki7ae534d2011-06-15 15:08:44 -07001667 if (!check_soft && root_mem->memsw_is_minimum)
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001668 noswap = true;
1669
Balbir Singh4e416952009-09-23 15:56:39 -07001670 while (1) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001671 victim = mem_cgroup_select_victim(root_mem);
Balbir Singh4e416952009-09-23 15:56:39 -07001672 if (victim == root_mem) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001673 loop++;
KAMEZAWA Hiroyukifbc29a22011-06-15 15:08:46 -07001674 /*
1675 * We are not draining per cpu cached charges during
1676 * soft limit reclaim because global reclaim doesn't
1677 * care about charges. It tries to free some memory and
1678 * charges will not give any.
1679 */
1680 if (!check_soft && loop >= 1)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07001681 drain_all_stock_async(root_mem);
Balbir Singh4e416952009-09-23 15:56:39 -07001682 if (loop >= 2) {
1683 /*
1684 * If we have not been able to reclaim
1685 * anything, it might because there are
1686 * no reclaimable pages under this hierarchy
1687 */
1688 if (!check_soft || !total) {
1689 css_put(&victim->css);
1690 break;
1691 }
1692 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001693 * We want to do more targeted reclaim.
Balbir Singh4e416952009-09-23 15:56:39 -07001694 * excess >> 2 is not to excessive so as to
1695 * reclaim too much, nor too less that we keep
1696 * coming back to reclaim from this cgroup
1697 */
1698 if (total >= (excess >> 2) ||
1699 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1700 css_put(&victim->css);
1701 break;
1702 }
1703 }
1704 }
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08001705 if (!mem_cgroup_local_usage(victim)) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001706 /* this cgroup's local usage == 0 */
1707 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001708 continue;
1709 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001710 /* we use swappiness of local cgroup */
Ying Han0ae5e892011-05-26 16:25:25 -07001711 if (check_soft) {
Balbir Singh4e416952009-09-23 15:56:39 -07001712 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
Ying Han0ae5e892011-05-26 16:25:25 -07001713 noswap, get_swappiness(victim), zone,
1714 &nr_scanned);
1715 *total_scanned += nr_scanned;
1716 } else
Balbir Singh4e416952009-09-23 15:56:39 -07001717 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
1718 noswap, get_swappiness(victim));
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001719 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001720 /*
1721 * At shrinking usage, we can't check we should stop here or
1722 * reclaim more. It's depends on callers. last_scanned_child
1723 * will work enough for keeping fairness under tree.
1724 */
1725 if (shrink)
1726 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001727 total += ret;
Balbir Singh4e416952009-09-23 15:56:39 -07001728 if (check_soft) {
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001729 if (!res_counter_soft_limit_excess(&root_mem->res))
Balbir Singh4e416952009-09-23 15:56:39 -07001730 return total;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001731 } else if (mem_cgroup_margin(root_mem))
Johannes Weiner4fd14eb2011-05-26 16:25:35 -07001732 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001733 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001734 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001735}
1736
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001737/*
1738 * Check OOM-Killer is already running under our hierarchy.
1739 * If someone is running, return false.
1740 */
1741static bool mem_cgroup_oom_lock(struct mem_cgroup *mem)
1742{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001743 int x, lock_count = 0;
1744 struct mem_cgroup *iter;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001745
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001746 for_each_mem_cgroup_tree(iter, mem) {
1747 x = atomic_inc_return(&iter->oom_lock);
1748 lock_count = max(x, lock_count);
1749 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001750
1751 if (lock_count == 1)
1752 return true;
1753 return false;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001754}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001755
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001756static int mem_cgroup_oom_unlock(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001757{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001758 struct mem_cgroup *iter;
1759
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001760 /*
1761 * When a new child is created while the hierarchy is under oom,
1762 * mem_cgroup_oom_lock() may not be called. We have to use
1763 * atomic_add_unless() here.
1764 */
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001765 for_each_mem_cgroup_tree(iter, mem)
1766 atomic_add_unless(&iter->oom_lock, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001767 return 0;
1768}
1769
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001770
1771static DEFINE_MUTEX(memcg_oom_mutex);
1772static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1773
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001774struct oom_wait_info {
1775 struct mem_cgroup *mem;
1776 wait_queue_t wait;
1777};
1778
1779static int memcg_oom_wake_function(wait_queue_t *wait,
1780 unsigned mode, int sync, void *arg)
1781{
1782 struct mem_cgroup *wake_mem = (struct mem_cgroup *)arg;
1783 struct oom_wait_info *oom_wait_info;
1784
1785 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
1786
1787 if (oom_wait_info->mem == wake_mem)
1788 goto wakeup;
1789 /* if no hierarchy, no match */
1790 if (!oom_wait_info->mem->use_hierarchy || !wake_mem->use_hierarchy)
1791 return 0;
1792 /*
1793 * Both of oom_wait_info->mem and wake_mem are stable under us.
1794 * Then we can use css_is_ancestor without taking care of RCU.
1795 */
1796 if (!css_is_ancestor(&oom_wait_info->mem->css, &wake_mem->css) &&
1797 !css_is_ancestor(&wake_mem->css, &oom_wait_info->mem->css))
1798 return 0;
1799
1800wakeup:
1801 return autoremove_wake_function(wait, mode, sync, arg);
1802}
1803
1804static void memcg_wakeup_oom(struct mem_cgroup *mem)
1805{
1806 /* for filtering, pass "mem" as argument. */
1807 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, mem);
1808}
1809
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001810static void memcg_oom_recover(struct mem_cgroup *mem)
1811{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001812 if (mem && atomic_read(&mem->oom_lock))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001813 memcg_wakeup_oom(mem);
1814}
1815
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001816/*
1817 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
1818 */
1819bool mem_cgroup_handle_oom(struct mem_cgroup *mem, gfp_t mask)
1820{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001821 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001822 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001823
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001824 owait.mem = mem;
1825 owait.wait.flags = 0;
1826 owait.wait.func = memcg_oom_wake_function;
1827 owait.wait.private = current;
1828 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001829 need_to_kill = true;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001830 /* At first, try to OOM lock hierarchy under mem.*/
1831 mutex_lock(&memcg_oom_mutex);
1832 locked = mem_cgroup_oom_lock(mem);
1833 /*
1834 * Even if signal_pending(), we can't quit charge() loop without
1835 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
1836 * under OOM is always welcomed, use TASK_KILLABLE here.
1837 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001838 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
1839 if (!locked || mem->oom_kill_disable)
1840 need_to_kill = false;
1841 if (locked)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07001842 mem_cgroup_oom_notify(mem);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001843 mutex_unlock(&memcg_oom_mutex);
1844
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001845 if (need_to_kill) {
1846 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001847 mem_cgroup_out_of_memory(mem, mask);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001848 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001849 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001850 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001851 }
1852 mutex_lock(&memcg_oom_mutex);
1853 mem_cgroup_oom_unlock(mem);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001854 memcg_wakeup_oom(mem);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001855 mutex_unlock(&memcg_oom_mutex);
1856
1857 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
1858 return false;
1859 /* Give chance to dying process */
1860 schedule_timeout(1);
1861 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001862}
1863
Balbir Singhd69b0422009-06-17 16:26:34 -07001864/*
1865 * Currently used to update mapped file statistics, but the routine can be
1866 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001867 *
1868 * Notes: Race condition
1869 *
1870 * We usually use page_cgroup_lock() for accessing page_cgroup member but
1871 * it tends to be costly. But considering some conditions, we doesn't need
1872 * to do so _always_.
1873 *
1874 * Considering "charge", lock_page_cgroup() is not required because all
1875 * file-stat operations happen after a page is attached to radix-tree. There
1876 * are no race with "charge".
1877 *
1878 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
1879 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
1880 * if there are race with "uncharge". Statistics itself is properly handled
1881 * by flags.
1882 *
1883 * Considering "move", this is an only case we see a race. To make the race
1884 * small, we check MEM_CGROUP_ON_MOVE percpu value and detect there are
1885 * possibility of race condition. If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07001886 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001887
Greg Thelen2a7106f2011-01-13 15:47:37 -08001888void mem_cgroup_update_page_stat(struct page *page,
1889 enum mem_cgroup_page_stat_item idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07001890{
1891 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001892 struct page_cgroup *pc = lookup_page_cgroup(page);
1893 bool need_unlock = false;
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08001894 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07001895
Balbir Singhd69b0422009-06-17 16:26:34 -07001896 if (unlikely(!pc))
1897 return;
1898
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001899 rcu_read_lock();
Balbir Singhd69b0422009-06-17 16:26:34 -07001900 mem = pc->mem_cgroup;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001901 if (unlikely(!mem || !PageCgroupUsed(pc)))
1902 goto out;
1903 /* pc->mem_cgroup is unstable ? */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08001904 if (unlikely(mem_cgroup_stealed(mem)) || PageTransHuge(page)) {
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001905 /* take a lock against to access pc->mem_cgroup */
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08001906 move_lock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001907 need_unlock = true;
1908 mem = pc->mem_cgroup;
1909 if (!mem || !PageCgroupUsed(pc))
1910 goto out;
1911 }
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001912
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001913 switch (idx) {
Greg Thelen2a7106f2011-01-13 15:47:37 -08001914 case MEMCG_NR_FILE_MAPPED:
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001915 if (val > 0)
1916 SetPageCgroupFileMapped(pc);
1917 else if (!page_mapped(page))
KAMEZAWA Hiroyuki0c270f82010-10-27 15:33:39 -07001918 ClearPageCgroupFileMapped(pc);
Greg Thelen2a7106f2011-01-13 15:47:37 -08001919 idx = MEM_CGROUP_STAT_FILE_MAPPED;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001920 break;
1921 default:
1922 BUG();
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07001923 }
Balbir Singhd69b0422009-06-17 16:26:34 -07001924
Greg Thelen2a7106f2011-01-13 15:47:37 -08001925 this_cpu_add(mem->stat->count[idx], val);
1926
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001927out:
1928 if (unlikely(need_unlock))
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08001929 move_unlock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001930 rcu_read_unlock();
1931 return;
Balbir Singhd69b0422009-06-17 16:26:34 -07001932}
Greg Thelen2a7106f2011-01-13 15:47:37 -08001933EXPORT_SYMBOL(mem_cgroup_update_page_stat);
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001934
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001935/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001936 * size of first charge trial. "32" comes from vmscan.c's magic value.
1937 * TODO: maybe necessary to use big numbers in big irons.
1938 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001939#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001940struct memcg_stock_pcp {
1941 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07001942 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001943 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07001944 unsigned long flags;
1945#define FLUSHING_CACHED_CHARGE (0)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001946};
1947static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07001948static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001949
1950/*
Johannes Weiner11c9ea42011-03-23 16:42:34 -07001951 * Try to consume stocked charge on this cpu. If success, one page is consumed
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001952 * from local stock and true is returned. If the stock is 0 or charges from a
1953 * cgroup which is not current target, returns false. This stock will be
1954 * refilled.
1955 */
1956static bool consume_stock(struct mem_cgroup *mem)
1957{
1958 struct memcg_stock_pcp *stock;
1959 bool ret = true;
1960
1961 stock = &get_cpu_var(memcg_stock);
Johannes Weiner11c9ea42011-03-23 16:42:34 -07001962 if (mem == stock->cached && stock->nr_pages)
1963 stock->nr_pages--;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001964 else /* need to call res_counter_charge */
1965 ret = false;
1966 put_cpu_var(memcg_stock);
1967 return ret;
1968}
1969
1970/*
1971 * Returns stocks cached in percpu to res_counter and reset cached information.
1972 */
1973static void drain_stock(struct memcg_stock_pcp *stock)
1974{
1975 struct mem_cgroup *old = stock->cached;
1976
Johannes Weiner11c9ea42011-03-23 16:42:34 -07001977 if (stock->nr_pages) {
1978 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
1979
1980 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001981 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07001982 res_counter_uncharge(&old->memsw, bytes);
1983 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001984 }
1985 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001986}
1987
1988/*
1989 * This must be called under preempt disabled or must be called by
1990 * a thread which is pinned to local cpu.
1991 */
1992static void drain_local_stock(struct work_struct *dummy)
1993{
1994 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
1995 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07001996 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001997}
1998
1999/*
2000 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002001 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002002 */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002003static void refill_stock(struct mem_cgroup *mem, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002004{
2005 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2006
2007 if (stock->cached != mem) { /* reset if necessary */
2008 drain_stock(stock);
2009 stock->cached = mem;
2010 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002011 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002012 put_cpu_var(memcg_stock);
2013}
2014
2015/*
2016 * Tries to drain stocked charges in other cpus. This function is asynchronous
2017 * and just put a work per cpu for draining localy on each cpu. Caller can
2018 * expects some charges will be back to res_counter later but cannot wait for
2019 * it.
2020 */
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002021static void drain_all_stock_async(struct mem_cgroup *root_mem)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002022{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002023 int cpu, curcpu;
2024 /*
2025 * If someone calls draining, avoid adding more kworker runs.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002026 */
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002027 if (!mutex_trylock(&percpu_charge_mutex))
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002028 return;
2029 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002030 get_online_cpus();
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002031 /*
2032 * Get a hint for avoiding draining charges on the current cpu,
2033 * which must be exhausted by our charging. It is not required that
2034 * this be a precise check, so we use raw_smp_processor_id() instead of
2035 * getcpu()/putcpu().
2036 */
2037 curcpu = raw_smp_processor_id();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002038 for_each_online_cpu(cpu) {
2039 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002040 struct mem_cgroup *mem;
2041
2042 if (cpu == curcpu)
2043 continue;
2044
2045 mem = stock->cached;
2046 if (!mem)
2047 continue;
2048 if (mem != root_mem) {
2049 if (!root_mem->use_hierarchy)
2050 continue;
2051 /* check whether "mem" is under tree of "root_mem" */
2052 if (!css_is_ancestor(&mem->css, &root_mem->css))
2053 continue;
2054 }
2055 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
2056 schedule_work_on(cpu, &stock->work);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002057 }
2058 put_online_cpus();
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002059 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002060 /* We don't wait for flush_work */
2061}
2062
2063/* This is a synchronous drain interface. */
2064static void drain_all_stock_sync(void)
2065{
2066 /* called when force_empty is called */
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002067 mutex_lock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002068 schedule_on_each_cpu(drain_local_stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002069 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002070}
2071
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002072/*
2073 * This function drains percpu counter value from DEAD cpu and
2074 * move it to local cpu. Note that this function can be preempted.
2075 */
2076static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *mem, int cpu)
2077{
2078 int i;
2079
2080 spin_lock(&mem->pcp_counter_lock);
2081 for (i = 0; i < MEM_CGROUP_STAT_DATA; i++) {
Johannes Weiner7a159cc2011-03-23 16:42:38 -07002082 long x = per_cpu(mem->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002083
2084 per_cpu(mem->stat->count[i], cpu) = 0;
2085 mem->nocpu_base.count[i] += x;
2086 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002087 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
2088 unsigned long x = per_cpu(mem->stat->events[i], cpu);
2089
2090 per_cpu(mem->stat->events[i], cpu) = 0;
2091 mem->nocpu_base.events[i] += x;
2092 }
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002093 /* need to clear ON_MOVE value, works as a kind of lock. */
2094 per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) = 0;
2095 spin_unlock(&mem->pcp_counter_lock);
2096}
2097
2098static void synchronize_mem_cgroup_on_move(struct mem_cgroup *mem, int cpu)
2099{
2100 int idx = MEM_CGROUP_ON_MOVE;
2101
2102 spin_lock(&mem->pcp_counter_lock);
2103 per_cpu(mem->stat->count[idx], cpu) = mem->nocpu_base.count[idx];
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002104 spin_unlock(&mem->pcp_counter_lock);
2105}
2106
2107static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002108 unsigned long action,
2109 void *hcpu)
2110{
2111 int cpu = (unsigned long)hcpu;
2112 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002113 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002114
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002115 if ((action == CPU_ONLINE)) {
2116 for_each_mem_cgroup_all(iter)
2117 synchronize_mem_cgroup_on_move(iter, cpu);
2118 return NOTIFY_OK;
2119 }
2120
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002121 if ((action != CPU_DEAD) || action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002122 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002123
2124 for_each_mem_cgroup_all(iter)
2125 mem_cgroup_drain_pcp_counter(iter, cpu);
2126
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002127 stock = &per_cpu(memcg_stock, cpu);
2128 drain_stock(stock);
2129 return NOTIFY_OK;
2130}
2131
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002132
2133/* See __mem_cgroup_try_charge() for details */
2134enum {
2135 CHARGE_OK, /* success */
2136 CHARGE_RETRY, /* need to retry but retry is not bad */
2137 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2138 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
2139 CHARGE_OOM_DIE, /* the current is killed because of OOM */
2140};
2141
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002142static int mem_cgroup_do_charge(struct mem_cgroup *mem, gfp_t gfp_mask,
2143 unsigned int nr_pages, bool oom_check)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002144{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002145 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002146 struct mem_cgroup *mem_over_limit;
2147 struct res_counter *fail_res;
2148 unsigned long flags = 0;
2149 int ret;
2150
2151 ret = res_counter_charge(&mem->res, csize, &fail_res);
2152
2153 if (likely(!ret)) {
2154 if (!do_swap_account)
2155 return CHARGE_OK;
2156 ret = res_counter_charge(&mem->memsw, csize, &fail_res);
2157 if (likely(!ret))
2158 return CHARGE_OK;
2159
KAMEZAWA Hiroyuki01c88e22011-01-25 15:07:27 -08002160 res_counter_uncharge(&mem->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002161 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2162 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2163 } else
2164 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002165 /*
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002166 * nr_pages can be either a huge page (HPAGE_PMD_NR), a batch
2167 * of regular pages (CHARGE_BATCH), or a single regular page (1).
Johannes Weiner9221edb2011-02-01 15:52:42 -08002168 *
2169 * Never reclaim on behalf of optional batching, retry with a
2170 * single page instead.
2171 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002172 if (nr_pages == CHARGE_BATCH)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002173 return CHARGE_RETRY;
2174
2175 if (!(gfp_mask & __GFP_WAIT))
2176 return CHARGE_WOULDBLOCK;
2177
2178 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
Ying Han0ae5e892011-05-26 16:25:25 -07002179 gfp_mask, flags, NULL);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002180 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002181 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002182 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002183 * Even though the limit is exceeded at this point, reclaim
2184 * may have been able to free some pages. Retry the charge
2185 * before killing the task.
2186 *
2187 * Only for regular pages, though: huge pages are rather
2188 * unlikely to succeed so close to the limit, and we fall back
2189 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002190 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002191 if (nr_pages == 1 && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002192 return CHARGE_RETRY;
2193
2194 /*
2195 * At task move, charge accounts can be doubly counted. So, it's
2196 * better to wait until the end of task_move if something is going on.
2197 */
2198 if (mem_cgroup_wait_acct_move(mem_over_limit))
2199 return CHARGE_RETRY;
2200
2201 /* If we don't need to call oom-killer at el, return immediately */
2202 if (!oom_check)
2203 return CHARGE_NOMEM;
2204 /* check OOM */
2205 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask))
2206 return CHARGE_OOM_DIE;
2207
2208 return CHARGE_RETRY;
2209}
2210
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002211/*
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002212 * Unlike exported interface, "oom" parameter is added. if oom==true,
2213 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002214 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002215static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002216 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002217 unsigned int nr_pages,
2218 struct mem_cgroup **memcg,
2219 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002220{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002221 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002222 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2223 struct mem_cgroup *mem = NULL;
2224 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002225
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002226 /*
2227 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2228 * in system level. So, allow to go ahead dying process in addition to
2229 * MEMDIE process.
2230 */
2231 if (unlikely(test_thread_flag(TIF_MEMDIE)
2232 || fatal_signal_pending(current)))
2233 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002234
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002235 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08002236 * We always charge the cgroup the mm_struct belongs to.
2237 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002238 * thread group leader migrates. It's possible that mm is not
2239 * set, if so charge the init_mm (happens for pagecache usage).
2240 */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002241 if (!*memcg && !mm)
2242 goto bypass;
2243again:
2244 if (*memcg) { /* css should be a valid one */
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002245 mem = *memcg;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002246 VM_BUG_ON(css_is_removed(&mem->css));
2247 if (mem_cgroup_is_root(mem))
2248 goto done;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002249 if (nr_pages == 1 && consume_stock(mem))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002250 goto done;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002251 css_get(&mem->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002252 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002253 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002254
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002255 rcu_read_lock();
2256 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002257 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002258 * Because we don't have task_lock(), "p" can exit.
2259 * In that case, "mem" can point to root or p can be NULL with
2260 * race with swapoff. Then, we have small risk of mis-accouning.
2261 * But such kind of mis-account by race always happens because
2262 * we don't have cgroup_mutex(). It's overkill and we allo that
2263 * small race, here.
2264 * (*) swapoff at el will charge against mm-struct not against
2265 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002266 */
2267 mem = mem_cgroup_from_task(p);
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002268 if (!mem || mem_cgroup_is_root(mem)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002269 rcu_read_unlock();
2270 goto done;
2271 }
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002272 if (nr_pages == 1 && consume_stock(mem)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002273 /*
2274 * It seems dagerous to access memcg without css_get().
2275 * But considering how consume_stok works, it's not
2276 * necessary. If consume_stock success, some charges
2277 * from this memcg are cached on this cpu. So, we
2278 * don't need to call css_get()/css_tryget() before
2279 * calling consume_stock().
2280 */
2281 rcu_read_unlock();
2282 goto done;
2283 }
2284 /* after here, we may be blocked. we need to get refcnt */
2285 if (!css_tryget(&mem->css)) {
2286 rcu_read_unlock();
2287 goto again;
2288 }
2289 rcu_read_unlock();
2290 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002291
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002292 do {
2293 bool oom_check;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002294
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002295 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002296 if (fatal_signal_pending(current)) {
2297 css_put(&mem->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002298 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002299 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002300
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002301 oom_check = false;
2302 if (oom && !nr_oom_retries) {
2303 oom_check = true;
2304 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2305 }
Balbir Singh6d61ef42009-01-07 18:08:06 -08002306
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002307 ret = mem_cgroup_do_charge(mem, gfp_mask, batch, oom_check);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002308 switch (ret) {
2309 case CHARGE_OK:
2310 break;
2311 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002312 batch = nr_pages;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002313 css_put(&mem->css);
2314 mem = NULL;
2315 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002316 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002317 css_put(&mem->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002318 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002319 case CHARGE_NOMEM: /* OOM routine works */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002320 if (!oom) {
2321 css_put(&mem->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002322 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002323 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002324 /* If oom, we never return -ENOMEM */
2325 nr_oom_retries--;
2326 break;
2327 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002328 css_put(&mem->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002329 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08002330 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002331 } while (ret != CHARGE_OK);
2332
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002333 if (batch > nr_pages)
2334 refill_stock(mem, batch - nr_pages);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002335 css_put(&mem->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002336done:
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002337 *memcg = mem;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002338 return 0;
2339nomem:
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002340 *memcg = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002341 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002342bypass:
2343 *memcg = NULL;
2344 return 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002345}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002346
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002347/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002348 * Somemtimes we have to undo a charge we got by try_charge().
2349 * This function is for that and do uncharge, put css's refcnt.
2350 * gotten by try_charge().
2351 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002352static void __mem_cgroup_cancel_charge(struct mem_cgroup *mem,
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002353 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002354{
2355 if (!mem_cgroup_is_root(mem)) {
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002356 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002357
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002358 res_counter_uncharge(&mem->res, bytes);
2359 if (do_swap_account)
2360 res_counter_uncharge(&mem->memsw, bytes);
2361 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002362}
2363
2364/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002365 * A helper function to get mem_cgroup from ID. must be called under
2366 * rcu_read_lock(). The caller must check css_is_removed() or some if
2367 * it's concern. (dropping refcnt from swap can be called against removed
2368 * memcg.)
2369 */
2370static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2371{
2372 struct cgroup_subsys_state *css;
2373
2374 /* ID 0 is unused ID */
2375 if (!id)
2376 return NULL;
2377 css = css_lookup(&mem_cgroup_subsys, id);
2378 if (!css)
2379 return NULL;
2380 return container_of(css, struct mem_cgroup, css);
2381}
2382
Wu Fengguange42d9d52009-12-16 12:19:59 +01002383struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002384{
Wu Fengguange42d9d52009-12-16 12:19:59 +01002385 struct mem_cgroup *mem = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002386 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002387 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002388 swp_entry_t ent;
2389
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002390 VM_BUG_ON(!PageLocked(page));
2391
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002392 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002393 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002394 if (PageCgroupUsed(pc)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002395 mem = pc->mem_cgroup;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002396 if (mem && !css_tryget(&mem->css))
2397 mem = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002398 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002399 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002400 id = lookup_swap_cgroup(ent);
2401 rcu_read_lock();
2402 mem = mem_cgroup_lookup(id);
2403 if (mem && !css_tryget(&mem->css))
2404 mem = NULL;
2405 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002406 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002407 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002408 return mem;
2409}
2410
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002411static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
Johannes Weiner5564e882011-03-23 16:42:29 -07002412 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002413 unsigned int nr_pages,
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002414 struct page_cgroup *pc,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002415 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002416{
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002417 lock_page_cgroup(pc);
2418 if (unlikely(PageCgroupUsed(pc))) {
2419 unlock_page_cgroup(pc);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002420 __mem_cgroup_cancel_charge(mem, nr_pages);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002421 return;
2422 }
2423 /*
2424 * we don't need page_cgroup_lock about tail pages, becase they are not
2425 * accessed by any other context at this point.
2426 */
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002427 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002428 /*
2429 * We access a page_cgroup asynchronously without lock_page_cgroup().
2430 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2431 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2432 * before USED bit, we need memory barrier here.
2433 * See mem_cgroup_add_lru_list(), etc.
2434 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002435 smp_wmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -07002436 switch (ctype) {
2437 case MEM_CGROUP_CHARGE_TYPE_CACHE:
2438 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
2439 SetPageCgroupCache(pc);
2440 SetPageCgroupUsed(pc);
2441 break;
2442 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
2443 ClearPageCgroupCache(pc);
2444 SetPageCgroupUsed(pc);
2445 break;
2446 default:
2447 break;
2448 }
Hugh Dickins3be91272008-02-07 00:14:19 -08002449
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002450 mem_cgroup_charge_statistics(mem, PageCgroupCache(pc), nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002451 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002452 /*
2453 * "charge_statistics" updated event counter. Then, check it.
2454 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2455 * if they exceeds softlimit.
2456 */
Johannes Weiner5564e882011-03-23 16:42:29 -07002457 memcg_check_events(mem, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002458}
2459
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002460#ifdef CONFIG_TRANSPARENT_HUGEPAGE
2461
2462#define PCGF_NOCOPY_AT_SPLIT ((1 << PCG_LOCK) | (1 << PCG_MOVE_LOCK) |\
2463 (1 << PCG_ACCT_LRU) | (1 << PCG_MIGRATION))
2464/*
2465 * Because tail pages are not marked as "used", set it. We're under
2466 * zone->lru_lock, 'splitting on pmd' and compund_lock.
2467 */
2468void mem_cgroup_split_huge_fixup(struct page *head, struct page *tail)
2469{
2470 struct page_cgroup *head_pc = lookup_page_cgroup(head);
2471 struct page_cgroup *tail_pc = lookup_page_cgroup(tail);
2472 unsigned long flags;
2473
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08002474 if (mem_cgroup_disabled())
2475 return;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002476 /*
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002477 * We have no races with charge/uncharge but will have races with
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002478 * page state accounting.
2479 */
2480 move_lock_page_cgroup(head_pc, &flags);
2481
2482 tail_pc->mem_cgroup = head_pc->mem_cgroup;
2483 smp_wmb(); /* see __commit_charge() */
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002484 if (PageCgroupAcctLRU(head_pc)) {
2485 enum lru_list lru;
2486 struct mem_cgroup_per_zone *mz;
2487
2488 /*
2489 * LRU flags cannot be copied because we need to add tail
2490 *.page to LRU by generic call and our hook will be called.
2491 * We hold lru_lock, then, reduce counter directly.
2492 */
2493 lru = page_lru(head);
Johannes Weiner97a6c372011-03-23 16:42:27 -07002494 mz = page_cgroup_zoneinfo(head_pc->mem_cgroup, head);
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002495 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
2496 }
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002497 tail_pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
2498 move_unlock_page_cgroup(head_pc, &flags);
2499}
2500#endif
2501
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002502/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07002503 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07002504 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002505 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002506 * @pc: page_cgroup of the page.
2507 * @from: mem_cgroup which the page is moved from.
2508 * @to: mem_cgroup which the page is moved to. @from != @to.
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002509 * @uncharge: whether we should call uncharge and css_put against @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002510 *
2511 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002512 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002513 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002514 *
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002515 * This function doesn't do "charge" nor css_get to new cgroup. It should be
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002516 * done by a caller(__mem_cgroup_try_charge would be useful). If @uncharge is
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002517 * true, this function does "uncharge" from old cgroup, but it doesn't if
2518 * @uncharge is false, so a caller should do "uncharge".
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002519 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002520static int mem_cgroup_move_account(struct page *page,
2521 unsigned int nr_pages,
2522 struct page_cgroup *pc,
2523 struct mem_cgroup *from,
2524 struct mem_cgroup *to,
2525 bool uncharge)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002526{
Johannes Weinerde3638d2011-03-23 16:42:28 -07002527 unsigned long flags;
2528 int ret;
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002529
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002530 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07002531 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07002532 /*
2533 * The page is isolated from LRU. So, collapse function
2534 * will not handle this page. But page splitting can happen.
2535 * Do this check under compound_page_lock(). The caller should
2536 * hold it.
2537 */
2538 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002539 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07002540 goto out;
2541
2542 lock_page_cgroup(pc);
2543
2544 ret = -EINVAL;
2545 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
2546 goto unlock;
2547
2548 move_lock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002549
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002550 if (PageCgroupFileMapped(pc)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08002551 /* Update mapped_file data for mem_cgroup */
2552 preempt_disable();
2553 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2554 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2555 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07002556 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002557 mem_cgroup_charge_statistics(from, PageCgroupCache(pc), -nr_pages);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002558 if (uncharge)
2559 /* This is not "cancel", but cancel_charge does all we need. */
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002560 __mem_cgroup_cancel_charge(from, nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07002561
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002562 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002563 pc->mem_cgroup = to;
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002564 mem_cgroup_charge_statistics(to, PageCgroupCache(pc), nr_pages);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002565 /*
2566 * We charges against "to" which may not have any tasks. Then, "to"
2567 * can be under rmdir(). But in current implementation, caller of
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08002568 * this function is just force_empty() and move charge, so it's
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002569 * guaranteed that "to" is never removed. So, we don't check rmdir
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08002570 * status here.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002571 */
Johannes Weinerde3638d2011-03-23 16:42:28 -07002572 move_unlock_page_cgroup(pc, &flags);
2573 ret = 0;
2574unlock:
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002575 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002576 /*
2577 * check events
2578 */
Johannes Weiner5564e882011-03-23 16:42:29 -07002579 memcg_check_events(to, page);
2580 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07002581out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002582 return ret;
2583}
2584
2585/*
2586 * move charges to its parent.
2587 */
2588
Johannes Weiner5564e882011-03-23 16:42:29 -07002589static int mem_cgroup_move_parent(struct page *page,
2590 struct page_cgroup *pc,
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002591 struct mem_cgroup *child,
2592 gfp_t gfp_mask)
2593{
2594 struct cgroup *cg = child->css.cgroup;
2595 struct cgroup *pcg = cg->parent;
2596 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002597 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07002598 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002599 int ret;
2600
2601 /* Is ROOT ? */
2602 if (!pcg)
2603 return -EINVAL;
2604
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002605 ret = -EBUSY;
2606 if (!get_page_unless_zero(page))
2607 goto out;
2608 if (isolate_lru_page(page))
2609 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08002610
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002611 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002612
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002613 parent = mem_cgroup_from_cont(pcg);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002614 ret = __mem_cgroup_try_charge(NULL, gfp_mask, nr_pages, &parent, false);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002615 if (ret || !parent)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002616 goto put_back;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002617
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002618 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002619 flags = compound_lock_irqsave(page);
2620
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002621 ret = mem_cgroup_move_account(page, nr_pages, pc, child, parent, true);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002622 if (ret)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002623 __mem_cgroup_cancel_charge(parent, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08002624
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002625 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002626 compound_unlock_irqrestore(page, flags);
Jesper Juhl8dba4742011-01-25 15:07:24 -08002627put_back:
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002628 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002629put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08002630 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002631out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002632 return ret;
2633}
2634
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002635/*
2636 * Charge the memory controller for page usage.
2637 * Return
2638 * 0 if the charge was successful
2639 * < 0 if the cgroup is over its limit
2640 */
2641static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002642 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002643{
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002644 struct mem_cgroup *mem = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002645 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08002646 struct page_cgroup *pc;
2647 bool oom = true;
2648 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08002649
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002650 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002651 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002652 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08002653 /*
2654 * Never OOM-kill a process for a huge page. The
2655 * fault handler will fall back to regular pages.
2656 */
2657 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002658 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002659
2660 pc = lookup_page_cgroup(page);
Johannes Weineraf4a6622011-03-23 16:42:24 -07002661 BUG_ON(!pc); /* XXX: remove this and move pc lookup into commit */
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002662
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002663 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &mem, oom);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002664 if (ret || !mem)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002665 return ret;
2666
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002667 __mem_cgroup_commit_charge(mem, page, nr_pages, pc, ctype);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002668 return 0;
2669}
2670
2671int mem_cgroup_newpage_charge(struct page *page,
2672 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002673{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002674 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002675 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002676 /*
2677 * If already mapped, we don't have to account.
2678 * If page cache, page->mapping has address_space.
2679 * But page->mapping may have out-of-use anon_vma pointer,
2680 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
2681 * is NULL.
2682 */
2683 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
2684 return 0;
2685 if (unlikely(!mm))
2686 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002687 return mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002688 MEM_CGROUP_CHARGE_TYPE_MAPPED);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002689}
2690
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002691static void
2692__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2693 enum charge_type ctype);
2694
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002695static void
2696__mem_cgroup_commit_charge_lrucare(struct page *page, struct mem_cgroup *mem,
2697 enum charge_type ctype)
2698{
2699 struct page_cgroup *pc = lookup_page_cgroup(page);
2700 /*
2701 * In some case, SwapCache, FUSE(splice_buf->radixtree), the page
2702 * is already on LRU. It means the page may on some other page_cgroup's
2703 * LRU. Take care of it.
2704 */
2705 mem_cgroup_lru_del_before_commit(page);
2706 __mem_cgroup_commit_charge(mem, page, 1, pc, ctype);
2707 mem_cgroup_lru_add_after_commit(page);
2708 return;
2709}
2710
Balbir Singhe1a1cd52008-02-07 00:14:02 -08002711int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
2712 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08002713{
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002714 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002715 int ret;
2716
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002717 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002718 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002719 if (PageCompound(page))
2720 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002721 /*
2722 * Corner case handling. This is called from add_to_page_cache()
2723 * in usual. But some FS (shmem) precharges this page before calling it
2724 * and call add_to_page_cache() with GFP_NOWAIT.
2725 *
2726 * For GFP_NOWAIT case, the page may be pre-charged before calling
2727 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
2728 * charge twice. (It works but has to pay a bit larger cost.)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002729 * And when the page is SwapCache, it should take swap information
2730 * into account. This is under lock_page() now.
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002731 */
2732 if (!(gfp_mask & __GFP_WAIT)) {
2733 struct page_cgroup *pc;
2734
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002735 pc = lookup_page_cgroup(page);
2736 if (!pc)
2737 return 0;
2738 lock_page_cgroup(pc);
2739 if (PageCgroupUsed(pc)) {
2740 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002741 return 0;
2742 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002743 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002744 }
2745
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002746 if (unlikely(!mm))
Balbir Singh8697d332008-02-07 00:13:59 -08002747 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002748
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002749 if (page_is_file_cache(page)) {
2750 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, &mem, true);
2751 if (ret || !mem)
2752 return ret;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002753
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002754 /*
2755 * FUSE reuses pages without going through the final
2756 * put that would remove them from the LRU list, make
2757 * sure that they get relinked properly.
2758 */
2759 __mem_cgroup_commit_charge_lrucare(page, mem,
2760 MEM_CGROUP_CHARGE_TYPE_CACHE);
2761 return ret;
2762 }
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002763 /* shmem */
2764 if (PageSwapCache(page)) {
2765 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2766 if (!ret)
2767 __mem_cgroup_commit_charge_swapin(page, mem,
2768 MEM_CGROUP_CHARGE_TYPE_SHMEM);
2769 } else
2770 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002771 MEM_CGROUP_CHARGE_TYPE_SHMEM);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002772
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002773 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002774}
2775
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002776/*
2777 * While swap-in, try_charge -> commit or cancel, the page is locked.
2778 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02002779 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002780 * "commit()" or removed by "cancel()"
2781 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002782int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
2783 struct page *page,
2784 gfp_t mask, struct mem_cgroup **ptr)
2785{
2786 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002787 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002788
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07002789 *ptr = NULL;
2790
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002791 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002792 return 0;
2793
2794 if (!do_swap_account)
2795 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002796 /*
2797 * A racing thread's fault, or swapoff, may have already updated
Hugh Dickins407f9c82009-12-14 17:59:30 -08002798 * the pte, and even removed page from swap cache: in those cases
2799 * do_swap_page()'s pte_same() test will fail; but there's also a
2800 * KSM case which does need to charge the page.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002801 */
2802 if (!PageSwapCache(page))
Hugh Dickins407f9c82009-12-14 17:59:30 -08002803 goto charge_cur_mm;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002804 mem = try_get_mem_cgroup_from_page(page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002805 if (!mem)
2806 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002807 *ptr = mem;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002808 ret = __mem_cgroup_try_charge(NULL, mask, 1, ptr, true);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002809 css_put(&mem->css);
2810 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002811charge_cur_mm:
2812 if (unlikely(!mm))
2813 mm = &init_mm;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002814 return __mem_cgroup_try_charge(mm, mask, 1, ptr, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002815}
2816
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002817static void
2818__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2819 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002820{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002821 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002822 return;
2823 if (!ptr)
2824 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002825 cgroup_exclude_rmdir(&ptr->css);
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002826
2827 __mem_cgroup_commit_charge_lrucare(page, ptr, ctype);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002828 /*
2829 * Now swap is on-memory. This means this page may be
2830 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002831 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
2832 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
2833 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002834 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002835 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002836 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002837 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002838 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002839
2840 id = swap_cgroup_record(ent, 0);
2841 rcu_read_lock();
2842 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002843 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002844 /*
2845 * This recorded memcg can be obsolete one. So, avoid
2846 * calling css_tryget
2847 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002848 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002849 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002850 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002851 mem_cgroup_put(memcg);
2852 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002853 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002854 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002855 /*
2856 * At swapin, we may charge account against cgroup which has no tasks.
2857 * So, rmdir()->pre_destroy() can be called while we do this charge.
2858 * In that case, we need to call pre_destroy() again. check it here.
2859 */
2860 cgroup_release_and_wakeup_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002861}
2862
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002863void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
2864{
2865 __mem_cgroup_commit_charge_swapin(page, ptr,
2866 MEM_CGROUP_CHARGE_TYPE_MAPPED);
2867}
2868
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002869void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
2870{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002871 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002872 return;
2873 if (!mem)
2874 return;
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002875 __mem_cgroup_cancel_charge(mem, 1);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002876}
2877
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002878static void mem_cgroup_do_uncharge(struct mem_cgroup *mem,
2879 unsigned int nr_pages,
2880 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002881{
2882 struct memcg_batch_info *batch = NULL;
2883 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002884
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002885 /* If swapout, usage of swap doesn't decrease */
2886 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2887 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002888
2889 batch = &current->memcg_batch;
2890 /*
2891 * In usual, we do css_get() when we remember memcg pointer.
2892 * But in this case, we keep res->usage until end of a series of
2893 * uncharges. Then, it's ok to ignore memcg's refcnt.
2894 */
2895 if (!batch->memcg)
2896 batch->memcg = mem;
2897 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002898 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002899 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002900 * the same cgroup and we have chance to coalesce uncharges.
2901 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
2902 * because we want to do uncharge as soon as possible.
2903 */
2904
2905 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
2906 goto direct_uncharge;
2907
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002908 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08002909 goto direct_uncharge;
2910
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002911 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002912 * In typical case, batch->memcg == mem. This means we can
2913 * merge a series of uncharges to an uncharge of res_counter.
2914 * If not, we uncharge res_counter ony by one.
2915 */
2916 if (batch->memcg != mem)
2917 goto direct_uncharge;
2918 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07002919 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002920 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07002921 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002922 return;
2923direct_uncharge:
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002924 res_counter_uncharge(&mem->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002925 if (uncharge_memsw)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002926 res_counter_uncharge(&mem->memsw, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002927 if (unlikely(batch->memcg != mem))
2928 memcg_oom_recover(mem);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002929 return;
2930}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002931
Balbir Singh8697d332008-02-07 00:13:59 -08002932/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002933 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002934 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002935static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002936__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002937{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002938 struct mem_cgroup *mem = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002939 unsigned int nr_pages = 1;
2940 struct page_cgroup *pc;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002941
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002942 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002943 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07002944
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002945 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002946 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002947
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002948 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002949 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002950 VM_BUG_ON(!PageTransHuge(page));
2951 }
Balbir Singh8697d332008-02-07 00:13:59 -08002952 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08002953 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08002954 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002955 pc = lookup_page_cgroup(page);
2956 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002957 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002958
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002959 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002960
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002961 mem = pc->mem_cgroup;
2962
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002963 if (!PageCgroupUsed(pc))
2964 goto unlock_out;
2965
2966 switch (ctype) {
2967 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002968 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002969 /* See mem_cgroup_prepare_migration() */
2970 if (page_mapped(page) || PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002971 goto unlock_out;
2972 break;
2973 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
2974 if (!PageAnon(page)) { /* Shared memory */
2975 if (page->mapping && !page_is_file_cache(page))
2976 goto unlock_out;
2977 } else if (page_mapped(page)) /* Anon */
2978 goto unlock_out;
2979 break;
2980 default:
2981 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002982 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002983
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002984 mem_cgroup_charge_statistics(mem, PageCgroupCache(pc), -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002985
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002986 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08002987 /*
2988 * pc->mem_cgroup is not cleared here. It will be accessed when it's
2989 * freed from LRU. This is safe because uncharged page is expected not
2990 * to be reused (freed soon). Exception is SwapCache, it's handled by
2991 * special functions.
2992 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08002993
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002994 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002995 /*
2996 * even after unlock, we have mem->res.usage here and this memcg
2997 * will never be freed.
2998 */
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002999 memcg_check_events(mem, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003000 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
3001 mem_cgroup_swap_statistics(mem, true);
3002 mem_cgroup_get(mem);
3003 }
3004 if (!mem_cgroup_is_root(mem))
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003005 mem_cgroup_do_uncharge(mem, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003006
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003007 return mem;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003008
3009unlock_out:
3010 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003011 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08003012}
3013
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003014void mem_cgroup_uncharge_page(struct page *page)
3015{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003016 /* early check. */
3017 if (page_mapped(page))
3018 return;
3019 if (page->mapping && !PageAnon(page))
3020 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003021 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
3022}
3023
3024void mem_cgroup_uncharge_cache_page(struct page *page)
3025{
3026 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07003027 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003028 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
3029}
3030
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003031/*
3032 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
3033 * In that cases, pages are freed continuously and we can expect pages
3034 * are in the same memcg. All these calls itself limits the number of
3035 * pages freed at once, then uncharge_start/end() is called properly.
3036 * This may be called prural(2) times in a context,
3037 */
3038
3039void mem_cgroup_uncharge_start(void)
3040{
3041 current->memcg_batch.do_batch++;
3042 /* We can do nest. */
3043 if (current->memcg_batch.do_batch == 1) {
3044 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003045 current->memcg_batch.nr_pages = 0;
3046 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003047 }
3048}
3049
3050void mem_cgroup_uncharge_end(void)
3051{
3052 struct memcg_batch_info *batch = &current->memcg_batch;
3053
3054 if (!batch->do_batch)
3055 return;
3056
3057 batch->do_batch--;
3058 if (batch->do_batch) /* If stacked, do nothing. */
3059 return;
3060
3061 if (!batch->memcg)
3062 return;
3063 /*
3064 * This "batch->memcg" is valid without any css_get/put etc...
3065 * bacause we hide charges behind us.
3066 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003067 if (batch->nr_pages)
3068 res_counter_uncharge(&batch->memcg->res,
3069 batch->nr_pages * PAGE_SIZE);
3070 if (batch->memsw_nr_pages)
3071 res_counter_uncharge(&batch->memcg->memsw,
3072 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003073 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003074 /* forget this pointer (for sanity check) */
3075 batch->memcg = NULL;
3076}
3077
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003078#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003079/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003080 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003081 * memcg information is recorded to swap_cgroup of "ent"
3082 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003083void
3084mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003085{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003086 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003087 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003088
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003089 if (!swapout) /* this was a swap cache but the swap is unused ! */
3090 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
3091
3092 memcg = __mem_cgroup_uncharge_common(page, ctype);
3093
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003094 /*
3095 * record memcg information, if swapout && memcg != NULL,
3096 * mem_cgroup_get() was called in uncharge().
3097 */
3098 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003099 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003100}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003101#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003102
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003103#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3104/*
3105 * called from swap_entry_free(). remove record in swap_cgroup and
3106 * uncharge "memsw" account.
3107 */
3108void mem_cgroup_uncharge_swap(swp_entry_t ent)
3109{
3110 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003111 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003112
3113 if (!do_swap_account)
3114 return;
3115
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003116 id = swap_cgroup_record(ent, 0);
3117 rcu_read_lock();
3118 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003119 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003120 /*
3121 * We uncharge this because swap is freed.
3122 * This memcg can be obsolete one. We avoid calling css_tryget
3123 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003124 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07003125 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003126 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003127 mem_cgroup_put(memcg);
3128 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003129 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003130}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003131
3132/**
3133 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
3134 * @entry: swap entry to be moved
3135 * @from: mem_cgroup which the entry is moved from
3136 * @to: mem_cgroup which the entry is moved to
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003137 * @need_fixup: whether we should fixup res_counters and refcounts.
Daisuke Nishimura02491442010-03-10 15:22:17 -08003138 *
3139 * It succeeds only when the swap_cgroup's record for this entry is the same
3140 * as the mem_cgroup's id of @from.
3141 *
3142 * Returns 0 on success, -EINVAL on failure.
3143 *
3144 * The caller must have charged to @to, IOW, called res_counter_charge() about
3145 * both res and memsw, and called css_get().
3146 */
3147static int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003148 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003149{
3150 unsigned short old_id, new_id;
3151
3152 old_id = css_id(&from->css);
3153 new_id = css_id(&to->css);
3154
3155 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08003156 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003157 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003158 /*
3159 * This function is only called from task migration context now.
3160 * It postpones res_counter and refcount handling till the end
3161 * of task migration(mem_cgroup_clear_mc()) for performance
3162 * improvement. But we cannot postpone mem_cgroup_get(to)
3163 * because if the process that has been moved to @to does
3164 * swap-in, the refcount of @to might be decreased to 0.
3165 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08003166 mem_cgroup_get(to);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003167 if (need_fixup) {
3168 if (!mem_cgroup_is_root(from))
3169 res_counter_uncharge(&from->memsw, PAGE_SIZE);
3170 mem_cgroup_put(from);
3171 /*
3172 * we charged both to->res and to->memsw, so we should
3173 * uncharge to->res.
3174 */
3175 if (!mem_cgroup_is_root(to))
3176 res_counter_uncharge(&to->res, PAGE_SIZE);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003177 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08003178 return 0;
3179 }
3180 return -EINVAL;
3181}
3182#else
3183static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003184 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003185{
3186 return -EINVAL;
3187}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003188#endif
3189
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003190/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003191 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
3192 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003193 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003194int mem_cgroup_prepare_migration(struct page *page,
Miklos Szeredief6a3c62011-03-22 16:30:52 -07003195 struct page *newpage, struct mem_cgroup **ptr, gfp_t gfp_mask)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003196{
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003197 struct mem_cgroup *mem = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003198 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003199 enum charge_type ctype;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003200 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003201
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07003202 *ptr = NULL;
3203
Andrea Arcangeliec168512011-01-13 15:46:56 -08003204 VM_BUG_ON(PageTransHuge(page));
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003205 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07003206 return 0;
3207
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003208 pc = lookup_page_cgroup(page);
3209 lock_page_cgroup(pc);
3210 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003211 mem = pc->mem_cgroup;
3212 css_get(&mem->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003213 /*
3214 * At migrating an anonymous page, its mapcount goes down
3215 * to 0 and uncharge() will be called. But, even if it's fully
3216 * unmapped, migration may fail and this page has to be
3217 * charged again. We set MIGRATION flag here and delay uncharge
3218 * until end_migration() is called
3219 *
3220 * Corner Case Thinking
3221 * A)
3222 * When the old page was mapped as Anon and it's unmap-and-freed
3223 * while migration was ongoing.
3224 * If unmap finds the old page, uncharge() of it will be delayed
3225 * until end_migration(). If unmap finds a new page, it's
3226 * uncharged when it make mapcount to be 1->0. If unmap code
3227 * finds swap_migration_entry, the new page will not be mapped
3228 * and end_migration() will find it(mapcount==0).
3229 *
3230 * B)
3231 * When the old page was mapped but migraion fails, the kernel
3232 * remaps it. A charge for it is kept by MIGRATION flag even
3233 * if mapcount goes down to 0. We can do remap successfully
3234 * without charging it again.
3235 *
3236 * C)
3237 * The "old" page is under lock_page() until the end of
3238 * migration, so, the old page itself will not be swapped-out.
3239 * If the new page is swapped out before end_migraton, our
3240 * hook to usual swap-out path will catch the event.
3241 */
3242 if (PageAnon(page))
3243 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003244 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003245 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003246 /*
3247 * If the page is not charged at this point,
3248 * we return here.
3249 */
3250 if (!mem)
3251 return 0;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003252
Andrea Arcangeli93d5c9b2010-04-23 13:17:39 -04003253 *ptr = mem;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003254 ret = __mem_cgroup_try_charge(NULL, gfp_mask, 1, ptr, false);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003255 css_put(&mem->css);/* drop extra refcnt */
3256 if (ret || *ptr == NULL) {
3257 if (PageAnon(page)) {
3258 lock_page_cgroup(pc);
3259 ClearPageCgroupMigration(pc);
3260 unlock_page_cgroup(pc);
3261 /*
3262 * The old page may be fully unmapped while we kept it.
3263 */
3264 mem_cgroup_uncharge_page(page);
3265 }
3266 return -ENOMEM;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003267 }
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003268 /*
3269 * We charge new page before it's used/mapped. So, even if unlock_page()
3270 * is called before end_migration, we can catch all events on this new
3271 * page. In the case new page is migrated but not remapped, new page's
3272 * mapcount will be finally 0 and we call uncharge in end_migration().
3273 */
3274 pc = lookup_page_cgroup(newpage);
3275 if (PageAnon(page))
3276 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
3277 else if (page_is_file_cache(page))
3278 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
3279 else
3280 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003281 __mem_cgroup_commit_charge(mem, page, 1, pc, ctype);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003282 return ret;
3283}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08003284
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003285/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003286void mem_cgroup_end_migration(struct mem_cgroup *mem,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003287 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003288{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003289 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003290 struct page_cgroup *pc;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003291
3292 if (!mem)
3293 return;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003294 /* blocks rmdir() */
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003295 cgroup_exclude_rmdir(&mem->css);
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003296 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003297 used = oldpage;
3298 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003299 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003300 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003301 unused = oldpage;
3302 }
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003303 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003304 * We disallowed uncharge of pages under migration because mapcount
3305 * of the page goes down to zero, temporarly.
3306 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003307 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003308 pc = lookup_page_cgroup(oldpage);
3309 lock_page_cgroup(pc);
3310 ClearPageCgroupMigration(pc);
3311 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003312
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003313 __mem_cgroup_uncharge_common(unused, MEM_CGROUP_CHARGE_TYPE_FORCE);
3314
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003315 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003316 * If a page is a file cache, radix-tree replacement is very atomic
3317 * and we can skip this check. When it was an Anon page, its mapcount
3318 * goes down to 0. But because we added MIGRATION flage, it's not
3319 * uncharged yet. There are several case but page->mapcount check
3320 * and USED bit check in mem_cgroup_uncharge_page() will do enough
3321 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003322 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003323 if (PageAnon(used))
3324 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003325 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003326 * At migration, we may charge account against cgroup which has no
3327 * tasks.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003328 * So, rmdir()->pre_destroy() can be called while we do this charge.
3329 * In that case, we need to call pre_destroy() again. check it here.
3330 */
3331 cgroup_release_and_wakeup_rmdir(&mem->css);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003332}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08003333
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003334/*
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07003335 * A call to try to shrink memory usage on charge failure at shmem's swapin.
3336 * Calling hierarchical_reclaim is not enough because we should update
3337 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
3338 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
3339 * not from the memcg which this page would be charged to.
3340 * try_charge_swapin does all of these works properly.
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003341 */
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07003342int mem_cgroup_shmem_charge_fallback(struct page *page,
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08003343 struct mm_struct *mm,
3344 gfp_t gfp_mask)
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003345{
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07003346 struct mem_cgroup *mem;
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07003347 int ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003348
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003349 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07003350 return 0;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003351
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07003352 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
3353 if (!ret)
3354 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003355
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07003356 return ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003357}
3358
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003359#ifdef CONFIG_DEBUG_VM
3360static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
3361{
3362 struct page_cgroup *pc;
3363
3364 pc = lookup_page_cgroup(page);
3365 if (likely(pc) && PageCgroupUsed(pc))
3366 return pc;
3367 return NULL;
3368}
3369
3370bool mem_cgroup_bad_page_check(struct page *page)
3371{
3372 if (mem_cgroup_disabled())
3373 return false;
3374
3375 return lookup_page_cgroup_used(page) != NULL;
3376}
3377
3378void mem_cgroup_print_bad_page(struct page *page)
3379{
3380 struct page_cgroup *pc;
3381
3382 pc = lookup_page_cgroup_used(page);
3383 if (pc) {
3384 int ret = -1;
3385 char *path;
3386
3387 printk(KERN_ALERT "pc:%p pc->flags:%lx pc->mem_cgroup:%p",
3388 pc, pc->flags, pc->mem_cgroup);
3389
3390 path = kmalloc(PATH_MAX, GFP_KERNEL);
3391 if (path) {
3392 rcu_read_lock();
3393 ret = cgroup_path(pc->mem_cgroup->css.cgroup,
3394 path, PATH_MAX);
3395 rcu_read_unlock();
3396 }
3397
3398 printk(KERN_CONT "(%s)\n",
3399 (ret < 0) ? "cannot get the path" : path);
3400 kfree(path);
3401 }
3402}
3403#endif
3404
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003405static DEFINE_MUTEX(set_limit_mutex);
3406
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08003407static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003408 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003409{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003410 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003411 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003412 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003413 int children = mem_cgroup_count_children(memcg);
3414 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003415 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003416
3417 /*
3418 * For keeping hierarchical_reclaim simple, how long we should retry
3419 * is depends on callers. We set our retry-count to be function
3420 * of # of children which we should visit in this loop.
3421 */
3422 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
3423
3424 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003425
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003426 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003427 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003428 if (signal_pending(current)) {
3429 ret = -EINTR;
3430 break;
3431 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003432 /*
3433 * Rather than hide all in some function, I do this in
3434 * open coded manner. You see what this really does.
3435 * We have to guarantee mem->res.limit < mem->memsw.limit.
3436 */
3437 mutex_lock(&set_limit_mutex);
3438 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3439 if (memswlimit < val) {
3440 ret = -EINVAL;
3441 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003442 break;
3443 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003444
3445 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3446 if (memlimit < val)
3447 enlarge = 1;
3448
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003449 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003450 if (!ret) {
3451 if (memswlimit == val)
3452 memcg->memsw_is_minimum = true;
3453 else
3454 memcg->memsw_is_minimum = false;
3455 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003456 mutex_unlock(&set_limit_mutex);
3457
3458 if (!ret)
3459 break;
3460
Bob Liuaa20d482009-12-15 16:47:14 -08003461 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Ying Han0ae5e892011-05-26 16:25:25 -07003462 MEM_CGROUP_RECLAIM_SHRINK,
3463 NULL);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003464 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
3465 /* Usage is reduced ? */
3466 if (curusage >= oldusage)
3467 retry_count--;
3468 else
3469 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003470 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003471 if (!ret && enlarge)
3472 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08003473
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003474 return ret;
3475}
3476
Li Zefan338c8432009-06-17 16:27:15 -07003477static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
3478 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003479{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003480 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003481 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003482 int children = mem_cgroup_count_children(memcg);
3483 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003484 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003485
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003486 /* see mem_cgroup_resize_res_limit */
3487 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
3488 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003489 while (retry_count) {
3490 if (signal_pending(current)) {
3491 ret = -EINTR;
3492 break;
3493 }
3494 /*
3495 * Rather than hide all in some function, I do this in
3496 * open coded manner. You see what this really does.
3497 * We have to guarantee mem->res.limit < mem->memsw.limit.
3498 */
3499 mutex_lock(&set_limit_mutex);
3500 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3501 if (memlimit > val) {
3502 ret = -EINVAL;
3503 mutex_unlock(&set_limit_mutex);
3504 break;
3505 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003506 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3507 if (memswlimit < val)
3508 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003509 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003510 if (!ret) {
3511 if (memlimit == val)
3512 memcg->memsw_is_minimum = true;
3513 else
3514 memcg->memsw_is_minimum = false;
3515 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003516 mutex_unlock(&set_limit_mutex);
3517
3518 if (!ret)
3519 break;
3520
Balbir Singh4e416952009-09-23 15:56:39 -07003521 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh75822b42009-09-23 15:56:38 -07003522 MEM_CGROUP_RECLAIM_NOSWAP |
Ying Han0ae5e892011-05-26 16:25:25 -07003523 MEM_CGROUP_RECLAIM_SHRINK,
3524 NULL);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003525 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003526 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003527 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003528 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003529 else
3530 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003531 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003532 if (!ret && enlarge)
3533 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003534 return ret;
3535}
3536
Balbir Singh4e416952009-09-23 15:56:39 -07003537unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
Ying Han0ae5e892011-05-26 16:25:25 -07003538 gfp_t gfp_mask,
3539 unsigned long *total_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07003540{
3541 unsigned long nr_reclaimed = 0;
3542 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
3543 unsigned long reclaimed;
3544 int loop = 0;
3545 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003546 unsigned long long excess;
Ying Han0ae5e892011-05-26 16:25:25 -07003547 unsigned long nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003548
3549 if (order > 0)
3550 return 0;
3551
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07003552 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
Balbir Singh4e416952009-09-23 15:56:39 -07003553 /*
3554 * This loop can run a while, specially if mem_cgroup's continuously
3555 * keep exceeding their soft limit and putting the system under
3556 * pressure
3557 */
3558 do {
3559 if (next_mz)
3560 mz = next_mz;
3561 else
3562 mz = mem_cgroup_largest_soft_limit_node(mctz);
3563 if (!mz)
3564 break;
3565
Ying Han0ae5e892011-05-26 16:25:25 -07003566 nr_scanned = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07003567 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
3568 gfp_mask,
Ying Han0ae5e892011-05-26 16:25:25 -07003569 MEM_CGROUP_RECLAIM_SOFT,
3570 &nr_scanned);
Balbir Singh4e416952009-09-23 15:56:39 -07003571 nr_reclaimed += reclaimed;
Ying Han0ae5e892011-05-26 16:25:25 -07003572 *total_scanned += nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003573 spin_lock(&mctz->lock);
3574
3575 /*
3576 * If we failed to reclaim anything from this memory cgroup
3577 * it is time to move on to the next cgroup
3578 */
3579 next_mz = NULL;
3580 if (!reclaimed) {
3581 do {
3582 /*
3583 * Loop until we find yet another one.
3584 *
3585 * By the time we get the soft_limit lock
3586 * again, someone might have aded the
3587 * group back on the RB tree. Iterate to
3588 * make sure we get a different mem.
3589 * mem_cgroup_largest_soft_limit_node returns
3590 * NULL if no other cgroup is present on
3591 * the tree
3592 */
3593 next_mz =
3594 __mem_cgroup_largest_soft_limit_node(mctz);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003595 if (next_mz == mz)
Balbir Singh4e416952009-09-23 15:56:39 -07003596 css_put(&next_mz->mem->css);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003597 else /* next_mz == NULL or other memcg */
Balbir Singh4e416952009-09-23 15:56:39 -07003598 break;
3599 } while (1);
3600 }
Balbir Singh4e416952009-09-23 15:56:39 -07003601 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003602 excess = res_counter_soft_limit_excess(&mz->mem->res);
Balbir Singh4e416952009-09-23 15:56:39 -07003603 /*
3604 * One school of thought says that we should not add
3605 * back the node to the tree if reclaim returns 0.
3606 * But our reclaim could return 0, simply because due
3607 * to priority we are exposing a smaller subset of
3608 * memory to reclaim from. Consider this as a longer
3609 * term TODO.
3610 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003611 /* If excess == 0, no tree ops */
3612 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07003613 spin_unlock(&mctz->lock);
3614 css_put(&mz->mem->css);
3615 loop++;
3616 /*
3617 * Could not reclaim anything and there are no more
3618 * mem cgroups to try or we seem to be looping without
3619 * reclaiming anything.
3620 */
3621 if (!nr_reclaimed &&
3622 (next_mz == NULL ||
3623 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3624 break;
3625 } while (!nr_reclaimed);
3626 if (next_mz)
3627 css_put(&next_mz->mem->css);
3628 return nr_reclaimed;
3629}
3630
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003631/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003632 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003633 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
3634 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003635static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003636 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003637{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003638 struct zone *zone;
3639 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003640 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003641 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003642 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003643 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003644
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003645 zone = &NODE_DATA(node)->node_zones[zid];
3646 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07003647 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003648
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003649 loop = MEM_CGROUP_ZSTAT(mz, lru);
3650 /* give some margin against EBUSY etc...*/
3651 loop += 256;
3652 busy = NULL;
3653 while (loop--) {
Johannes Weiner5564e882011-03-23 16:42:29 -07003654 struct page *page;
3655
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003656 ret = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003657 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003658 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003659 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003660 break;
3661 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003662 pc = list_entry(list->prev, struct page_cgroup, lru);
3663 if (busy == pc) {
3664 list_move(&pc->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08003665 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003666 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003667 continue;
3668 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003669 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003670
Johannes Weiner6b3ae582011-03-23 16:42:30 -07003671 page = lookup_cgroup_page(pc);
Johannes Weiner5564e882011-03-23 16:42:29 -07003672
3673 ret = mem_cgroup_move_parent(page, pc, mem, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003674 if (ret == -ENOMEM)
3675 break;
3676
3677 if (ret == -EBUSY || ret == -EINVAL) {
3678 /* found lock contention or "pc" is obsolete. */
3679 busy = pc;
3680 cond_resched();
3681 } else
3682 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003683 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003684
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003685 if (!ret && !list_empty(list))
3686 return -EBUSY;
3687 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003688}
3689
3690/*
3691 * make mem_cgroup's charge to be 0 if there is no task.
3692 * This enables deleting this mem_cgroup.
3693 */
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003694static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003695{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003696 int ret;
3697 int node, zid, shrink;
3698 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003699 struct cgroup *cgrp = mem->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003700
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003701 css_get(&mem->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003702
3703 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003704 /* should free all ? */
3705 if (free_all)
3706 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003707move_account:
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003708 do {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003709 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003710 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003711 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003712 ret = -EINTR;
3713 if (signal_pending(current))
3714 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003715 /* This is for making all *used* pages to be on LRU. */
3716 lru_add_drain_all();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08003717 drain_all_stock_sync();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003718 ret = 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07003719 mem_cgroup_start_move(mem);
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08003720 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003721 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07003722 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003723 for_each_lru(l) {
3724 ret = mem_cgroup_force_empty_list(mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003725 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003726 if (ret)
3727 break;
3728 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003729 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003730 if (ret)
3731 break;
3732 }
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07003733 mem_cgroup_end_move(mem);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003734 memcg_oom_recover(mem);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003735 /* it seems parent cgroup doesn't have enough mem */
3736 if (ret == -ENOMEM)
3737 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003738 cond_resched();
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003739 /* "ret" should also be checked to ensure all lists are empty. */
3740 } while (mem->res.usage > 0 || ret);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003741out:
3742 css_put(&mem->css);
3743 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003744
3745try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003746 /* returns EBUSY if there is a task or if we come here twice. */
3747 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003748 ret = -EBUSY;
3749 goto out;
3750 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003751 /* we call try-to-free pages for make this cgroup empty */
3752 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003753 /* try to free all pages in this cgroup */
3754 shrink = 1;
3755 while (nr_retries && mem->res.usage > 0) {
3756 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003757
3758 if (signal_pending(current)) {
3759 ret = -EINTR;
3760 goto out;
3761 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003762 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
3763 false, get_swappiness(mem));
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003764 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003765 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003766 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02003767 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003768 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003769
3770 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003771 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003772 /* try move_account...there may be some *locked* pages. */
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003773 goto move_account;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003774}
3775
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003776int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
3777{
3778 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
3779}
3780
3781
Balbir Singh18f59ea2009-01-07 18:08:07 -08003782static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
3783{
3784 return mem_cgroup_from_cont(cont)->use_hierarchy;
3785}
3786
3787static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
3788 u64 val)
3789{
3790 int retval = 0;
3791 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
3792 struct cgroup *parent = cont->parent;
3793 struct mem_cgroup *parent_mem = NULL;
3794
3795 if (parent)
3796 parent_mem = mem_cgroup_from_cont(parent);
3797
3798 cgroup_lock();
3799 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003800 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08003801 * in the child subtrees. If it is unset, then the change can
3802 * occur, provided the current cgroup has no children.
3803 *
3804 * For the root cgroup, parent_mem is NULL, we allow value to be
3805 * set if there are no children.
3806 */
3807 if ((!parent_mem || !parent_mem->use_hierarchy) &&
3808 (val == 1 || val == 0)) {
3809 if (list_empty(&cont->children))
3810 mem->use_hierarchy = val;
3811 else
3812 retval = -EBUSY;
3813 } else
3814 retval = -EINVAL;
3815 cgroup_unlock();
3816
3817 return retval;
3818}
3819
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003820
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003821static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *mem,
3822 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003823{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003824 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003825 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003826
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003827 /* Per-cpu values can be negative, use a signed accumulator */
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003828 for_each_mem_cgroup_tree(iter, mem)
3829 val += mem_cgroup_read_stat(iter, idx);
3830
3831 if (val < 0) /* race ? */
3832 val = 0;
3833 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003834}
3835
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003836static inline u64 mem_cgroup_usage(struct mem_cgroup *mem, bool swap)
3837{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003838 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003839
3840 if (!mem_cgroup_is_root(mem)) {
3841 if (!swap)
3842 return res_counter_read_u64(&mem->res, RES_USAGE);
3843 else
3844 return res_counter_read_u64(&mem->memsw, RES_USAGE);
3845 }
3846
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003847 val = mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_CACHE);
3848 val += mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003849
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003850 if (swap)
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003851 val += mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_SWAPOUT);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003852
3853 return val << PAGE_SHIFT;
3854}
3855
Paul Menage2c3daa72008-04-29 00:59:58 -07003856static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003857{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003858 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003859 u64 val;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003860 int type, name;
3861
3862 type = MEMFILE_TYPE(cft->private);
3863 name = MEMFILE_ATTR(cft->private);
3864 switch (type) {
3865 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003866 if (name == RES_USAGE)
3867 val = mem_cgroup_usage(mem, false);
3868 else
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003869 val = res_counter_read_u64(&mem->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003870 break;
3871 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003872 if (name == RES_USAGE)
3873 val = mem_cgroup_usage(mem, true);
3874 else
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003875 val = res_counter_read_u64(&mem->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003876 break;
3877 default:
3878 BUG();
3879 break;
3880 }
3881 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003882}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003883/*
3884 * The user of this function is...
3885 * RES_LIMIT.
3886 */
Paul Menage856c13a2008-07-25 01:47:04 -07003887static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
3888 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003889{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003890 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003891 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003892 unsigned long long val;
3893 int ret;
3894
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003895 type = MEMFILE_TYPE(cft->private);
3896 name = MEMFILE_ATTR(cft->private);
3897 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003898 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07003899 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
3900 ret = -EINVAL;
3901 break;
3902 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003903 /* This function does all necessary parse...reuse it */
3904 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003905 if (ret)
3906 break;
3907 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003908 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003909 else
3910 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003911 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07003912 case RES_SOFT_LIMIT:
3913 ret = res_counter_memparse_write_strategy(buffer, &val);
3914 if (ret)
3915 break;
3916 /*
3917 * For memsw, soft limits are hard to implement in terms
3918 * of semantics, for now, we support soft limits for
3919 * control without swap
3920 */
3921 if (type == _MEM)
3922 ret = res_counter_set_soft_limit(&memcg->res, val);
3923 else
3924 ret = -EINVAL;
3925 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003926 default:
3927 ret = -EINVAL; /* should be BUG() ? */
3928 break;
3929 }
3930 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003931}
3932
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08003933static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
3934 unsigned long long *mem_limit, unsigned long long *memsw_limit)
3935{
3936 struct cgroup *cgroup;
3937 unsigned long long min_limit, min_memsw_limit, tmp;
3938
3939 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3940 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3941 cgroup = memcg->css.cgroup;
3942 if (!memcg->use_hierarchy)
3943 goto out;
3944
3945 while (cgroup->parent) {
3946 cgroup = cgroup->parent;
3947 memcg = mem_cgroup_from_cont(cgroup);
3948 if (!memcg->use_hierarchy)
3949 break;
3950 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
3951 min_limit = min(min_limit, tmp);
3952 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3953 min_memsw_limit = min(min_memsw_limit, tmp);
3954 }
3955out:
3956 *mem_limit = min_limit;
3957 *memsw_limit = min_memsw_limit;
3958 return;
3959}
3960
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003961static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003962{
3963 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003964 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003965
3966 mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003967 type = MEMFILE_TYPE(event);
3968 name = MEMFILE_ATTR(event);
3969 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003970 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003971 if (type == _MEM)
3972 res_counter_reset_max(&mem->res);
3973 else
3974 res_counter_reset_max(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003975 break;
3976 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003977 if (type == _MEM)
3978 res_counter_reset_failcnt(&mem->res);
3979 else
3980 res_counter_reset_failcnt(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003981 break;
3982 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07003983
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07003984 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003985}
3986
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003987static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
3988 struct cftype *cft)
3989{
3990 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
3991}
3992
Daisuke Nishimura02491442010-03-10 15:22:17 -08003993#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003994static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
3995 struct cftype *cft, u64 val)
3996{
3997 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3998
3999 if (val >= (1 << NR_MOVE_TYPE))
4000 return -EINVAL;
4001 /*
4002 * We check this value several times in both in can_attach() and
4003 * attach(), so we need cgroup lock to prevent this value from being
4004 * inconsistent.
4005 */
4006 cgroup_lock();
4007 mem->move_charge_at_immigrate = val;
4008 cgroup_unlock();
4009
4010 return 0;
4011}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004012#else
4013static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
4014 struct cftype *cft, u64 val)
4015{
4016 return -ENOSYS;
4017}
4018#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004019
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004020
4021/* For read statistics */
4022enum {
4023 MCS_CACHE,
4024 MCS_RSS,
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08004025 MCS_FILE_MAPPED,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004026 MCS_PGPGIN,
4027 MCS_PGPGOUT,
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004028 MCS_SWAP,
Ying Han456f9982011-05-26 16:25:38 -07004029 MCS_PGFAULT,
4030 MCS_PGMAJFAULT,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004031 MCS_INACTIVE_ANON,
4032 MCS_ACTIVE_ANON,
4033 MCS_INACTIVE_FILE,
4034 MCS_ACTIVE_FILE,
4035 MCS_UNEVICTABLE,
4036 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004037};
4038
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004039struct mcs_total_stat {
4040 s64 stat[NR_MCS_STAT];
4041};
4042
4043struct {
4044 char *local_name;
4045 char *total_name;
4046} memcg_stat_strings[NR_MCS_STAT] = {
4047 {"cache", "total_cache"},
4048 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07004049 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004050 {"pgpgin", "total_pgpgin"},
4051 {"pgpgout", "total_pgpgout"},
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004052 {"swap", "total_swap"},
Ying Han456f9982011-05-26 16:25:38 -07004053 {"pgfault", "total_pgfault"},
4054 {"pgmajfault", "total_pgmajfault"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004055 {"inactive_anon", "total_inactive_anon"},
4056 {"active_anon", "total_active_anon"},
4057 {"inactive_file", "total_inactive_file"},
4058 {"active_file", "total_active_file"},
4059 {"unevictable", "total_unevictable"}
4060};
4061
4062
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004063static void
4064mem_cgroup_get_local_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004065{
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004066 s64 val;
4067
4068 /* per cpu stat */
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004069 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004070 s->stat[MCS_CACHE] += val * PAGE_SIZE;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004071 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004072 s->stat[MCS_RSS] += val * PAGE_SIZE;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004073 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_FILE_MAPPED);
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08004074 s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
Johannes Weinere9f89742011-03-23 16:42:37 -07004075 val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGPGIN);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004076 s->stat[MCS_PGPGIN] += val;
Johannes Weinere9f89742011-03-23 16:42:37 -07004077 val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGPGOUT);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004078 s->stat[MCS_PGPGOUT] += val;
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004079 if (do_swap_account) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004080 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_SWAPOUT);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004081 s->stat[MCS_SWAP] += val * PAGE_SIZE;
4082 }
Ying Han456f9982011-05-26 16:25:38 -07004083 val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGFAULT);
4084 s->stat[MCS_PGFAULT] += val;
4085 val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGMAJFAULT);
4086 s->stat[MCS_PGMAJFAULT] += val;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004087
4088 /* per zone stat */
4089 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
4090 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
4091 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
4092 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
4093 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
4094 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
4095 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
4096 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
4097 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
4098 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004099}
4100
4101static void
4102mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
4103{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004104 struct mem_cgroup *iter;
4105
4106 for_each_mem_cgroup_tree(iter, mem)
4107 mem_cgroup_get_local_stat(iter, s);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004108}
4109
Ying Han406eb0c2011-05-26 16:25:37 -07004110#ifdef CONFIG_NUMA
4111static int mem_control_numa_stat_show(struct seq_file *m, void *arg)
4112{
4113 int nid;
4114 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
4115 unsigned long node_nr;
4116 struct cgroup *cont = m->private;
4117 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
4118
4119 total_nr = mem_cgroup_nr_lru_pages(mem_cont);
4120 seq_printf(m, "total=%lu", total_nr);
4121 for_each_node_state(nid, N_HIGH_MEMORY) {
4122 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid);
4123 seq_printf(m, " N%d=%lu", nid, node_nr);
4124 }
4125 seq_putc(m, '\n');
4126
4127 file_nr = mem_cgroup_nr_file_lru_pages(mem_cont);
4128 seq_printf(m, "file=%lu", file_nr);
4129 for_each_node_state(nid, N_HIGH_MEMORY) {
4130 node_nr = mem_cgroup_node_nr_file_lru_pages(mem_cont, nid);
4131 seq_printf(m, " N%d=%lu", nid, node_nr);
4132 }
4133 seq_putc(m, '\n');
4134
4135 anon_nr = mem_cgroup_nr_anon_lru_pages(mem_cont);
4136 seq_printf(m, "anon=%lu", anon_nr);
4137 for_each_node_state(nid, N_HIGH_MEMORY) {
4138 node_nr = mem_cgroup_node_nr_anon_lru_pages(mem_cont, nid);
4139 seq_printf(m, " N%d=%lu", nid, node_nr);
4140 }
4141 seq_putc(m, '\n');
4142
4143 unevictable_nr = mem_cgroup_nr_unevictable_lru_pages(mem_cont);
4144 seq_printf(m, "unevictable=%lu", unevictable_nr);
4145 for_each_node_state(nid, N_HIGH_MEMORY) {
4146 node_nr = mem_cgroup_node_nr_unevictable_lru_pages(mem_cont,
4147 nid);
4148 seq_printf(m, " N%d=%lu", nid, node_nr);
4149 }
4150 seq_putc(m, '\n');
4151 return 0;
4152}
4153#endif /* CONFIG_NUMA */
4154
Paul Menagec64745c2008-04-29 01:00:02 -07004155static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
4156 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004157{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004158 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004159 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004160 int i;
4161
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004162 memset(&mystat, 0, sizeof(mystat));
4163 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004164
Ying Han406eb0c2011-05-26 16:25:37 -07004165
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004166 for (i = 0; i < NR_MCS_STAT; i++) {
4167 if (i == MCS_SWAP && !do_swap_account)
4168 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004169 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004170 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004171
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004172 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004173 {
4174 unsigned long long limit, memsw_limit;
4175 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
4176 cb->fill(cb, "hierarchical_memory_limit", limit);
4177 if (do_swap_account)
4178 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
4179 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004180
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004181 memset(&mystat, 0, sizeof(mystat));
4182 mem_cgroup_get_total_stat(mem_cont, &mystat);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004183 for (i = 0; i < NR_MCS_STAT; i++) {
4184 if (i == MCS_SWAP && !do_swap_account)
4185 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004186 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004187 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004188
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004189#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08004190 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004191
4192 {
4193 int nid, zid;
4194 struct mem_cgroup_per_zone *mz;
4195 unsigned long recent_rotated[2] = {0, 0};
4196 unsigned long recent_scanned[2] = {0, 0};
4197
4198 for_each_online_node(nid)
4199 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
4200 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
4201
4202 recent_rotated[0] +=
4203 mz->reclaim_stat.recent_rotated[0];
4204 recent_rotated[1] +=
4205 mz->reclaim_stat.recent_rotated[1];
4206 recent_scanned[0] +=
4207 mz->reclaim_stat.recent_scanned[0];
4208 recent_scanned[1] +=
4209 mz->reclaim_stat.recent_scanned[1];
4210 }
4211 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
4212 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
4213 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
4214 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
4215 }
4216#endif
4217
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004218 return 0;
4219}
4220
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004221static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
4222{
4223 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4224
4225 return get_swappiness(memcg);
4226}
4227
4228static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
4229 u64 val)
4230{
4231 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4232 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08004233
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004234 if (val > 100)
4235 return -EINVAL;
4236
4237 if (cgrp->parent == NULL)
4238 return -EINVAL;
4239
4240 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08004241
4242 cgroup_lock();
4243
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004244 /* If under hierarchy, only empty-root can set this value */
4245 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08004246 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
4247 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004248 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08004249 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004250
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004251 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004252
Li Zefan068b38c2009-01-15 13:51:26 -08004253 cgroup_unlock();
4254
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004255 return 0;
4256}
4257
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004258static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
4259{
4260 struct mem_cgroup_threshold_ary *t;
4261 u64 usage;
4262 int i;
4263
4264 rcu_read_lock();
4265 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004266 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004267 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004268 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004269
4270 if (!t)
4271 goto unlock;
4272
4273 usage = mem_cgroup_usage(memcg, swap);
4274
4275 /*
4276 * current_threshold points to threshold just below usage.
4277 * If it's not true, a threshold was crossed after last
4278 * call of __mem_cgroup_threshold().
4279 */
Phil Carmody5407a562010-05-26 14:42:42 -07004280 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004281
4282 /*
4283 * Iterate backward over array of thresholds starting from
4284 * current_threshold and check if a threshold is crossed.
4285 * If none of thresholds below usage is crossed, we read
4286 * only one element of the array here.
4287 */
4288 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
4289 eventfd_signal(t->entries[i].eventfd, 1);
4290
4291 /* i = current_threshold + 1 */
4292 i++;
4293
4294 /*
4295 * Iterate forward over array of thresholds starting from
4296 * current_threshold+1 and check if a threshold is crossed.
4297 * If none of thresholds above usage is crossed, we read
4298 * only one element of the array here.
4299 */
4300 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
4301 eventfd_signal(t->entries[i].eventfd, 1);
4302
4303 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07004304 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004305unlock:
4306 rcu_read_unlock();
4307}
4308
4309static void mem_cgroup_threshold(struct mem_cgroup *memcg)
4310{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07004311 while (memcg) {
4312 __mem_cgroup_threshold(memcg, false);
4313 if (do_swap_account)
4314 __mem_cgroup_threshold(memcg, true);
4315
4316 memcg = parent_mem_cgroup(memcg);
4317 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004318}
4319
4320static int compare_thresholds(const void *a, const void *b)
4321{
4322 const struct mem_cgroup_threshold *_a = a;
4323 const struct mem_cgroup_threshold *_b = b;
4324
4325 return _a->threshold - _b->threshold;
4326}
4327
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004328static int mem_cgroup_oom_notify_cb(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004329{
4330 struct mem_cgroup_eventfd_list *ev;
4331
4332 list_for_each_entry(ev, &mem->oom_notify, list)
4333 eventfd_signal(ev->eventfd, 1);
4334 return 0;
4335}
4336
4337static void mem_cgroup_oom_notify(struct mem_cgroup *mem)
4338{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004339 struct mem_cgroup *iter;
4340
4341 for_each_mem_cgroup_tree(iter, mem)
4342 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004343}
4344
4345static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
4346 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004347{
4348 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004349 struct mem_cgroup_thresholds *thresholds;
4350 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004351 int type = MEMFILE_TYPE(cft->private);
4352 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004353 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004354
4355 ret = res_counter_memparse_write_strategy(args, &threshold);
4356 if (ret)
4357 return ret;
4358
4359 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004360
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004361 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004362 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004363 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004364 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004365 else
4366 BUG();
4367
4368 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4369
4370 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004371 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004372 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4373
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004374 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004375
4376 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004377 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004378 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004379 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004380 ret = -ENOMEM;
4381 goto unlock;
4382 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004383 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004384
4385 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004386 if (thresholds->primary) {
4387 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004388 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004389 }
4390
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004391 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004392 new->entries[size - 1].eventfd = eventfd;
4393 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004394
4395 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004396 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004397 compare_thresholds, NULL);
4398
4399 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004400 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004401 for (i = 0; i < size; i++) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004402 if (new->entries[i].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004403 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004404 * new->current_threshold will not be used until
4405 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004406 * it here.
4407 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004408 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004409 }
4410 }
4411
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004412 /* Free old spare buffer and save old primary buffer as spare */
4413 kfree(thresholds->spare);
4414 thresholds->spare = thresholds->primary;
4415
4416 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004417
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004418 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004419 synchronize_rcu();
4420
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004421unlock:
4422 mutex_unlock(&memcg->thresholds_lock);
4423
4424 return ret;
4425}
4426
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004427static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004428 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004429{
4430 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004431 struct mem_cgroup_thresholds *thresholds;
4432 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004433 int type = MEMFILE_TYPE(cft->private);
4434 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004435 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004436
4437 mutex_lock(&memcg->thresholds_lock);
4438 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004439 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004440 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004441 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004442 else
4443 BUG();
4444
4445 /*
4446 * Something went wrong if we trying to unregister a threshold
4447 * if we don't have thresholds
4448 */
4449 BUG_ON(!thresholds);
4450
4451 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4452
4453 /* Check if a threshold crossed before removing */
4454 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4455
4456 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004457 size = 0;
4458 for (i = 0; i < thresholds->primary->size; i++) {
4459 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004460 size++;
4461 }
4462
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004463 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004464
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004465 /* Set thresholds array to NULL if we don't have thresholds */
4466 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004467 kfree(new);
4468 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004469 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004470 }
4471
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004472 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004473
4474 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004475 new->current_threshold = -1;
4476 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
4477 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004478 continue;
4479
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004480 new->entries[j] = thresholds->primary->entries[i];
4481 if (new->entries[j].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004482 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004483 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004484 * until rcu_assign_pointer(), so it's safe to increment
4485 * it here.
4486 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004487 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004488 }
4489 j++;
4490 }
4491
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004492swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004493 /* Swap primary and spare array */
4494 thresholds->spare = thresholds->primary;
4495 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004496
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004497 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004498 synchronize_rcu();
4499
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004500 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004501}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004502
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004503static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
4504 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
4505{
4506 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4507 struct mem_cgroup_eventfd_list *event;
4508 int type = MEMFILE_TYPE(cft->private);
4509
4510 BUG_ON(type != _OOM_TYPE);
4511 event = kmalloc(sizeof(*event), GFP_KERNEL);
4512 if (!event)
4513 return -ENOMEM;
4514
4515 mutex_lock(&memcg_oom_mutex);
4516
4517 event->eventfd = eventfd;
4518 list_add(&event->list, &memcg->oom_notify);
4519
4520 /* already in OOM ? */
4521 if (atomic_read(&memcg->oom_lock))
4522 eventfd_signal(eventfd, 1);
4523 mutex_unlock(&memcg_oom_mutex);
4524
4525 return 0;
4526}
4527
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004528static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004529 struct cftype *cft, struct eventfd_ctx *eventfd)
4530{
4531 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
4532 struct mem_cgroup_eventfd_list *ev, *tmp;
4533 int type = MEMFILE_TYPE(cft->private);
4534
4535 BUG_ON(type != _OOM_TYPE);
4536
4537 mutex_lock(&memcg_oom_mutex);
4538
4539 list_for_each_entry_safe(ev, tmp, &mem->oom_notify, list) {
4540 if (ev->eventfd == eventfd) {
4541 list_del(&ev->list);
4542 kfree(ev);
4543 }
4544 }
4545
4546 mutex_unlock(&memcg_oom_mutex);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004547}
4548
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004549static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
4550 struct cftype *cft, struct cgroup_map_cb *cb)
4551{
4552 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
4553
4554 cb->fill(cb, "oom_kill_disable", mem->oom_kill_disable);
4555
4556 if (atomic_read(&mem->oom_lock))
4557 cb->fill(cb, "under_oom", 1);
4558 else
4559 cb->fill(cb, "under_oom", 0);
4560 return 0;
4561}
4562
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004563static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
4564 struct cftype *cft, u64 val)
4565{
4566 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
4567 struct mem_cgroup *parent;
4568
4569 /* cannot set to root cgroup and only 0 and 1 are allowed */
4570 if (!cgrp->parent || !((val == 0) || (val == 1)))
4571 return -EINVAL;
4572
4573 parent = mem_cgroup_from_cont(cgrp->parent);
4574
4575 cgroup_lock();
4576 /* oom-kill-disable is a flag for subhierarchy. */
4577 if ((parent->use_hierarchy) ||
4578 (mem->use_hierarchy && !list_empty(&cgrp->children))) {
4579 cgroup_unlock();
4580 return -EINVAL;
4581 }
4582 mem->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07004583 if (!val)
4584 memcg_oom_recover(mem);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004585 cgroup_unlock();
4586 return 0;
4587}
4588
Ying Han406eb0c2011-05-26 16:25:37 -07004589#ifdef CONFIG_NUMA
4590static const struct file_operations mem_control_numa_stat_file_operations = {
4591 .read = seq_read,
4592 .llseek = seq_lseek,
4593 .release = single_release,
4594};
4595
4596static int mem_control_numa_stat_open(struct inode *unused, struct file *file)
4597{
4598 struct cgroup *cont = file->f_dentry->d_parent->d_fsdata;
4599
4600 file->f_op = &mem_control_numa_stat_file_operations;
4601 return single_open(file, mem_control_numa_stat_show, cont);
4602}
4603#endif /* CONFIG_NUMA */
4604
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004605static struct cftype mem_cgroup_files[] = {
4606 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004607 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004608 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07004609 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004610 .register_event = mem_cgroup_usage_register_event,
4611 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004612 },
4613 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004614 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004615 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004616 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004617 .read_u64 = mem_cgroup_read,
4618 },
4619 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004620 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004621 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07004622 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07004623 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004624 },
4625 {
Balbir Singh296c81d2009-09-23 15:56:36 -07004626 .name = "soft_limit_in_bytes",
4627 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
4628 .write_string = mem_cgroup_write,
4629 .read_u64 = mem_cgroup_read,
4630 },
4631 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004632 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004633 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004634 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07004635 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004636 },
Balbir Singh8697d332008-02-07 00:13:59 -08004637 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004638 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07004639 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004640 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004641 {
4642 .name = "force_empty",
4643 .trigger = mem_cgroup_force_empty_write,
4644 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08004645 {
4646 .name = "use_hierarchy",
4647 .write_u64 = mem_cgroup_hierarchy_write,
4648 .read_u64 = mem_cgroup_hierarchy_read,
4649 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004650 {
4651 .name = "swappiness",
4652 .read_u64 = mem_cgroup_swappiness_read,
4653 .write_u64 = mem_cgroup_swappiness_write,
4654 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004655 {
4656 .name = "move_charge_at_immigrate",
4657 .read_u64 = mem_cgroup_move_charge_read,
4658 .write_u64 = mem_cgroup_move_charge_write,
4659 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004660 {
4661 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004662 .read_map = mem_cgroup_oom_control_read,
4663 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004664 .register_event = mem_cgroup_oom_register_event,
4665 .unregister_event = mem_cgroup_oom_unregister_event,
4666 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
4667 },
Ying Han406eb0c2011-05-26 16:25:37 -07004668#ifdef CONFIG_NUMA
4669 {
4670 .name = "numa_stat",
4671 .open = mem_control_numa_stat_open,
KAMEZAWA Hiroyuki89577122011-06-15 15:08:41 -07004672 .mode = S_IRUGO,
Ying Han406eb0c2011-05-26 16:25:37 -07004673 },
4674#endif
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004675};
4676
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004677#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4678static struct cftype memsw_cgroup_files[] = {
4679 {
4680 .name = "memsw.usage_in_bytes",
4681 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
4682 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004683 .register_event = mem_cgroup_usage_register_event,
4684 .unregister_event = mem_cgroup_usage_unregister_event,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004685 },
4686 {
4687 .name = "memsw.max_usage_in_bytes",
4688 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
4689 .trigger = mem_cgroup_reset,
4690 .read_u64 = mem_cgroup_read,
4691 },
4692 {
4693 .name = "memsw.limit_in_bytes",
4694 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
4695 .write_string = mem_cgroup_write,
4696 .read_u64 = mem_cgroup_read,
4697 },
4698 {
4699 .name = "memsw.failcnt",
4700 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
4701 .trigger = mem_cgroup_reset,
4702 .read_u64 = mem_cgroup_read,
4703 },
4704};
4705
4706static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
4707{
4708 if (!do_swap_account)
4709 return 0;
4710 return cgroup_add_files(cont, ss, memsw_cgroup_files,
4711 ARRAY_SIZE(memsw_cgroup_files));
4712};
4713#else
4714static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
4715{
4716 return 0;
4717}
4718#endif
4719
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004720static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
4721{
4722 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004723 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07004724 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004725 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004726 /*
4727 * This routine is called against possible nodes.
4728 * But it's BUG to call kmalloc() against offline node.
4729 *
4730 * TODO: this routine can waste much memory for nodes which will
4731 * never be onlined. It's better to use memory hotplug callback
4732 * function.
4733 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004734 if (!node_state(node, N_NORMAL_MEMORY))
4735 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08004736 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004737 if (!pn)
4738 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004739
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004740 mem->info.nodeinfo[node] = pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004741 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4742 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07004743 for_each_lru(l)
4744 INIT_LIST_HEAD(&mz->lists[l]);
Balbir Singhf64c3f52009-09-23 15:56:37 -07004745 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07004746 mz->on_tree = false;
4747 mz->mem = mem;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004748 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004749 return 0;
4750}
4751
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004752static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
4753{
4754 kfree(mem->info.nodeinfo[node]);
4755}
4756
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004757static struct mem_cgroup *mem_cgroup_alloc(void)
4758{
4759 struct mem_cgroup *mem;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004760 int size = sizeof(struct mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004761
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004762 /* Can be very big if MAX_NUMNODES is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08004763 if (size < PAGE_SIZE)
Jesper Juhl17295c82011-01-13 15:47:42 -08004764 mem = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004765 else
Jesper Juhl17295c82011-01-13 15:47:42 -08004766 mem = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004767
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07004768 if (!mem)
4769 return NULL;
4770
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004771 mem->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004772 if (!mem->stat)
4773 goto out_free;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07004774 spin_lock_init(&mem->pcp_counter_lock);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004775 return mem;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004776
4777out_free:
4778 if (size < PAGE_SIZE)
4779 kfree(mem);
4780 else
4781 vfree(mem);
4782 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004783}
4784
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004785/*
4786 * At destroying mem_cgroup, references from swap_cgroup can remain.
4787 * (scanning all at force_empty is too costly...)
4788 *
4789 * Instead of clearing all references at force_empty, we remember
4790 * the number of reference from swap_cgroup and free mem_cgroup when
4791 * it goes down to 0.
4792 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004793 * Removal of cgroup itself succeeds regardless of refs from swap.
4794 */
4795
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004796static void __mem_cgroup_free(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004797{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004798 int node;
4799
Balbir Singhf64c3f52009-09-23 15:56:37 -07004800 mem_cgroup_remove_from_trees(mem);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004801 free_css_id(&mem_cgroup_subsys, &mem->css);
4802
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004803 for_each_node_state(node, N_POSSIBLE)
4804 free_mem_cgroup_per_zone_info(mem, node);
4805
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004806 free_percpu(mem->stat);
4807 if (sizeof(struct mem_cgroup) < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004808 kfree(mem);
4809 else
4810 vfree(mem);
4811}
4812
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004813static void mem_cgroup_get(struct mem_cgroup *mem)
4814{
4815 atomic_inc(&mem->refcnt);
4816}
4817
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004818static void __mem_cgroup_put(struct mem_cgroup *mem, int count)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004819{
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004820 if (atomic_sub_and_test(count, &mem->refcnt)) {
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004821 struct mem_cgroup *parent = parent_mem_cgroup(mem);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004822 __mem_cgroup_free(mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004823 if (parent)
4824 mem_cgroup_put(parent);
4825 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004826}
4827
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004828static void mem_cgroup_put(struct mem_cgroup *mem)
4829{
4830 __mem_cgroup_put(mem, 1);
4831}
4832
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004833/*
4834 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
4835 */
4836static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
4837{
4838 if (!mem->res.parent)
4839 return NULL;
4840 return mem_cgroup_from_res_counter(mem->res.parent, res);
4841}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004842
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004843#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4844static void __init enable_swap_cgroup(void)
4845{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004846 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004847 do_swap_account = 1;
4848}
4849#else
4850static void __init enable_swap_cgroup(void)
4851{
4852}
4853#endif
4854
Balbir Singhf64c3f52009-09-23 15:56:37 -07004855static int mem_cgroup_soft_limit_tree_init(void)
4856{
4857 struct mem_cgroup_tree_per_node *rtpn;
4858 struct mem_cgroup_tree_per_zone *rtpz;
4859 int tmp, node, zone;
4860
4861 for_each_node_state(node, N_POSSIBLE) {
4862 tmp = node;
4863 if (!node_state(node, N_NORMAL_MEMORY))
4864 tmp = -1;
4865 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
4866 if (!rtpn)
4867 return 1;
4868
4869 soft_limit_tree.rb_tree_per_node[node] = rtpn;
4870
4871 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4872 rtpz = &rtpn->rb_tree_per_zone[zone];
4873 rtpz->rb_root = RB_ROOT;
4874 spin_lock_init(&rtpz->lock);
4875 }
4876 }
4877 return 0;
4878}
4879
Li Zefan0eb253e2009-01-15 13:51:25 -08004880static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004881mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
4882{
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004883 struct mem_cgroup *mem, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004884 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004885 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004886
Jan Blunckc8dad2b2009-01-07 18:07:53 -08004887 mem = mem_cgroup_alloc();
4888 if (!mem)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004889 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004890
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004891 for_each_node_state(node, N_POSSIBLE)
4892 if (alloc_mem_cgroup_per_zone_info(mem, node))
4893 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004894
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004895 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004896 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004897 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004898 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004899 parent = NULL;
Balbir Singh4b3bde42009-09-23 15:56:32 -07004900 root_mem_cgroup = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004901 if (mem_cgroup_soft_limit_tree_init())
4902 goto free_out;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004903 for_each_possible_cpu(cpu) {
4904 struct memcg_stock_pcp *stock =
4905 &per_cpu(memcg_stock, cpu);
4906 INIT_WORK(&stock->work, drain_local_stock);
4907 }
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07004908 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004909 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004910 parent = mem_cgroup_from_cont(cont->parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004911 mem->use_hierarchy = parent->use_hierarchy;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004912 mem->oom_kill_disable = parent->oom_kill_disable;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004913 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004914
Balbir Singh18f59ea2009-01-07 18:08:07 -08004915 if (parent && parent->use_hierarchy) {
4916 res_counter_init(&mem->res, &parent->res);
4917 res_counter_init(&mem->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004918 /*
4919 * We increment refcnt of the parent to ensure that we can
4920 * safely access it on res_counter_charge/uncharge.
4921 * This refcnt will be decremented when freeing this
4922 * mem_cgroup(see mem_cgroup_put).
4923 */
4924 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004925 } else {
4926 res_counter_init(&mem->res, NULL);
4927 res_counter_init(&mem->memsw, NULL);
4928 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004929 mem->last_scanned_child = 0;
Ying Han889976d2011-05-26 16:25:33 -07004930 mem->last_scanned_node = MAX_NUMNODES;
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004931 INIT_LIST_HEAD(&mem->oom_notify);
Balbir Singh6d61ef42009-01-07 18:08:06 -08004932
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004933 if (parent)
4934 mem->swappiness = get_swappiness(parent);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004935 atomic_set(&mem->refcnt, 1);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004936 mem->move_charge_at_immigrate = 0;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004937 mutex_init(&mem->thresholds_lock);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004938 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004939free_out:
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004940 __mem_cgroup_free(mem);
Balbir Singh4b3bde42009-09-23 15:56:32 -07004941 root_mem_cgroup = NULL;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004942 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004943}
4944
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004945static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004946 struct cgroup *cont)
4947{
4948 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004949
4950 return mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004951}
4952
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004953static void mem_cgroup_destroy(struct cgroup_subsys *ss,
4954 struct cgroup *cont)
4955{
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004956 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004957
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004958 mem_cgroup_put(mem);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004959}
4960
4961static int mem_cgroup_populate(struct cgroup_subsys *ss,
4962 struct cgroup *cont)
4963{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004964 int ret;
4965
4966 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
4967 ARRAY_SIZE(mem_cgroup_files));
4968
4969 if (!ret)
4970 ret = register_memsw_files(cont, ss);
4971 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004972}
4973
Daisuke Nishimura02491442010-03-10 15:22:17 -08004974#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004975/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004976#define PRECHARGE_COUNT_AT_ONCE 256
4977static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004978{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004979 int ret = 0;
4980 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004981 struct mem_cgroup *mem = mc.to;
4982
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004983 if (mem_cgroup_is_root(mem)) {
4984 mc.precharge += count;
4985 /* we don't need css_get for root */
4986 return ret;
4987 }
4988 /* try to charge at once */
4989 if (count > 1) {
4990 struct res_counter *dummy;
4991 /*
4992 * "mem" cannot be under rmdir() because we've already checked
4993 * by cgroup_lock_live_cgroup() that it is not removed and we
4994 * are still under the same cgroup_mutex. So we can postpone
4995 * css_get().
4996 */
4997 if (res_counter_charge(&mem->res, PAGE_SIZE * count, &dummy))
4998 goto one_by_one;
4999 if (do_swap_account && res_counter_charge(&mem->memsw,
5000 PAGE_SIZE * count, &dummy)) {
5001 res_counter_uncharge(&mem->res, PAGE_SIZE * count);
5002 goto one_by_one;
5003 }
5004 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005005 return ret;
5006 }
5007one_by_one:
5008 /* fall back to one by one charge */
5009 while (count--) {
5010 if (signal_pending(current)) {
5011 ret = -EINTR;
5012 break;
5013 }
5014 if (!batch_count--) {
5015 batch_count = PRECHARGE_COUNT_AT_ONCE;
5016 cond_resched();
5017 }
Johannes Weiner7ec99d62011-03-23 16:42:36 -07005018 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, 1, &mem, false);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005019 if (ret || !mem)
5020 /* mem_cgroup_clear_mc() will do uncharge later */
5021 return -ENOMEM;
5022 mc.precharge++;
5023 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005024 return ret;
5025}
5026
5027/**
5028 * is_target_pte_for_mc - check a pte whether it is valid for move charge
5029 * @vma: the vma the pte to be checked belongs
5030 * @addr: the address corresponding to the pte to be checked
5031 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08005032 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005033 *
5034 * Returns
5035 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
5036 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
5037 * move charge. if @target is not NULL, the page is stored in target->page
5038 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08005039 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
5040 * target for charge migration. if @target is not NULL, the entry is stored
5041 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005042 *
5043 * Called with pte lock held.
5044 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005045union mc_target {
5046 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005047 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005048};
5049
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005050enum mc_target_type {
5051 MC_TARGET_NONE, /* not used */
5052 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08005053 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005054};
5055
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005056static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
5057 unsigned long addr, pte_t ptent)
5058{
5059 struct page *page = vm_normal_page(vma, addr, ptent);
5060
5061 if (!page || !page_mapped(page))
5062 return NULL;
5063 if (PageAnon(page)) {
5064 /* we don't move shared anon */
5065 if (!move_anon() || page_mapcount(page) > 2)
5066 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005067 } else if (!move_file())
5068 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005069 return NULL;
5070 if (!get_page_unless_zero(page))
5071 return NULL;
5072
5073 return page;
5074}
5075
5076static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5077 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5078{
5079 int usage_count;
5080 struct page *page = NULL;
5081 swp_entry_t ent = pte_to_swp_entry(ptent);
5082
5083 if (!move_anon() || non_swap_entry(ent))
5084 return NULL;
5085 usage_count = mem_cgroup_count_swap_user(ent, &page);
5086 if (usage_count > 1) { /* we don't move shared anon */
5087 if (page)
5088 put_page(page);
5089 return NULL;
5090 }
5091 if (do_swap_account)
5092 entry->val = ent.val;
5093
5094 return page;
5095}
5096
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005097static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
5098 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5099{
5100 struct page *page = NULL;
5101 struct inode *inode;
5102 struct address_space *mapping;
5103 pgoff_t pgoff;
5104
5105 if (!vma->vm_file) /* anonymous vma */
5106 return NULL;
5107 if (!move_file())
5108 return NULL;
5109
5110 inode = vma->vm_file->f_path.dentry->d_inode;
5111 mapping = vma->vm_file->f_mapping;
5112 if (pte_none(ptent))
5113 pgoff = linear_page_index(vma, addr);
5114 else /* pte_file(ptent) is true */
5115 pgoff = pte_to_pgoff(ptent);
5116
5117 /* page is moved even if it's not RSS of this task(page-faulted). */
5118 if (!mapping_cap_swap_backed(mapping)) { /* normal file */
5119 page = find_get_page(mapping, pgoff);
5120 } else { /* shmem/tmpfs file. we should take account of swap too. */
5121 swp_entry_t ent;
5122 mem_cgroup_get_shmem_target(inode, pgoff, &page, &ent);
5123 if (do_swap_account)
5124 entry->val = ent.val;
5125 }
5126
5127 return page;
5128}
5129
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005130static int is_target_pte_for_mc(struct vm_area_struct *vma,
5131 unsigned long addr, pte_t ptent, union mc_target *target)
5132{
Daisuke Nishimura02491442010-03-10 15:22:17 -08005133 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005134 struct page_cgroup *pc;
5135 int ret = 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005136 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005137
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005138 if (pte_present(ptent))
5139 page = mc_handle_present_pte(vma, addr, ptent);
5140 else if (is_swap_pte(ptent))
5141 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005142 else if (pte_none(ptent) || pte_file(ptent))
5143 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005144
5145 if (!page && !ent.val)
Daisuke Nishimura02491442010-03-10 15:22:17 -08005146 return 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005147 if (page) {
5148 pc = lookup_page_cgroup(page);
5149 /*
5150 * Do only loose check w/o page_cgroup lock.
5151 * mem_cgroup_move_account() checks the pc is valid or not under
5152 * the lock.
5153 */
5154 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
5155 ret = MC_TARGET_PAGE;
5156 if (target)
5157 target->page = page;
5158 }
5159 if (!ret || !target)
5160 put_page(page);
5161 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005162 /* There is a swap entry and a page doesn't exist or isn't charged */
5163 if (ent.val && !ret &&
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005164 css_id(&mc.from->css) == lookup_swap_cgroup(ent)) {
5165 ret = MC_TARGET_SWAP;
5166 if (target)
5167 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005168 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005169 return ret;
5170}
5171
5172static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
5173 unsigned long addr, unsigned long end,
5174 struct mm_walk *walk)
5175{
5176 struct vm_area_struct *vma = walk->private;
5177 pte_t *pte;
5178 spinlock_t *ptl;
5179
Dave Hansen03319322011-03-22 16:32:56 -07005180 split_huge_page_pmd(walk->mm, pmd);
5181
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005182 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5183 for (; addr != end; pte++, addr += PAGE_SIZE)
5184 if (is_target_pte_for_mc(vma, addr, *pte, NULL))
5185 mc.precharge++; /* increment precharge temporarily */
5186 pte_unmap_unlock(pte - 1, ptl);
5187 cond_resched();
5188
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005189 return 0;
5190}
5191
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005192static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
5193{
5194 unsigned long precharge;
5195 struct vm_area_struct *vma;
5196
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005197 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005198 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5199 struct mm_walk mem_cgroup_count_precharge_walk = {
5200 .pmd_entry = mem_cgroup_count_precharge_pte_range,
5201 .mm = mm,
5202 .private = vma,
5203 };
5204 if (is_vm_hugetlb_page(vma))
5205 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005206 walk_page_range(vma->vm_start, vma->vm_end,
5207 &mem_cgroup_count_precharge_walk);
5208 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005209 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005210
5211 precharge = mc.precharge;
5212 mc.precharge = 0;
5213
5214 return precharge;
5215}
5216
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005217static int mem_cgroup_precharge_mc(struct mm_struct *mm)
5218{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005219 unsigned long precharge = mem_cgroup_count_precharge(mm);
5220
5221 VM_BUG_ON(mc.moving_task);
5222 mc.moving_task = current;
5223 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005224}
5225
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005226/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
5227static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005228{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005229 struct mem_cgroup *from = mc.from;
5230 struct mem_cgroup *to = mc.to;
5231
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005232 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005233 if (mc.precharge) {
5234 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
5235 mc.precharge = 0;
5236 }
5237 /*
5238 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
5239 * we must uncharge here.
5240 */
5241 if (mc.moved_charge) {
5242 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
5243 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005244 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005245 /* we must fixup refcnts and charges */
5246 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005247 /* uncharge swap account from the old cgroup */
5248 if (!mem_cgroup_is_root(mc.from))
5249 res_counter_uncharge(&mc.from->memsw,
5250 PAGE_SIZE * mc.moved_swap);
5251 __mem_cgroup_put(mc.from, mc.moved_swap);
5252
5253 if (!mem_cgroup_is_root(mc.to)) {
5254 /*
5255 * we charged both to->res and to->memsw, so we should
5256 * uncharge to->res.
5257 */
5258 res_counter_uncharge(&mc.to->res,
5259 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005260 }
5261 /* we've already done mem_cgroup_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005262 mc.moved_swap = 0;
5263 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005264 memcg_oom_recover(from);
5265 memcg_oom_recover(to);
5266 wake_up_all(&mc.waitq);
5267}
5268
5269static void mem_cgroup_clear_mc(void)
5270{
5271 struct mem_cgroup *from = mc.from;
5272
5273 /*
5274 * we must clear moving_task before waking up waiters at the end of
5275 * task migration.
5276 */
5277 mc.moving_task = NULL;
5278 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005279 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005280 mc.from = NULL;
5281 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005282 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005283 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005284}
5285
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005286static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
5287 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005288 struct task_struct *p)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005289{
5290 int ret = 0;
5291 struct mem_cgroup *mem = mem_cgroup_from_cont(cgroup);
5292
5293 if (mem->move_charge_at_immigrate) {
5294 struct mm_struct *mm;
5295 struct mem_cgroup *from = mem_cgroup_from_task(p);
5296
5297 VM_BUG_ON(from == mem);
5298
5299 mm = get_task_mm(p);
5300 if (!mm)
5301 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005302 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005303 if (mm->owner == p) {
5304 VM_BUG_ON(mc.from);
5305 VM_BUG_ON(mc.to);
5306 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005307 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005308 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005309 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005310 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005311 mc.from = from;
5312 mc.to = mem;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005313 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005314 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005315
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005316 ret = mem_cgroup_precharge_mc(mm);
5317 if (ret)
5318 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005319 }
5320 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005321 }
5322 return ret;
5323}
5324
5325static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
5326 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005327 struct task_struct *p)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005328{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005329 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005330}
5331
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005332static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
5333 unsigned long addr, unsigned long end,
5334 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005335{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005336 int ret = 0;
5337 struct vm_area_struct *vma = walk->private;
5338 pte_t *pte;
5339 spinlock_t *ptl;
5340
Dave Hansen03319322011-03-22 16:32:56 -07005341 split_huge_page_pmd(walk->mm, pmd);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005342retry:
5343 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5344 for (; addr != end; addr += PAGE_SIZE) {
5345 pte_t ptent = *(pte++);
5346 union mc_target target;
5347 int type;
5348 struct page *page;
5349 struct page_cgroup *pc;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005350 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005351
5352 if (!mc.precharge)
5353 break;
5354
5355 type = is_target_pte_for_mc(vma, addr, ptent, &target);
5356 switch (type) {
5357 case MC_TARGET_PAGE:
5358 page = target.page;
5359 if (isolate_lru_page(page))
5360 goto put;
5361 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07005362 if (!mem_cgroup_move_account(page, 1, pc,
5363 mc.from, mc.to, false)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005364 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005365 /* we uncharge from mc.from later. */
5366 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005367 }
5368 putback_lru_page(page);
5369put: /* is_target_pte_for_mc() gets the page */
5370 put_page(page);
5371 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005372 case MC_TARGET_SWAP:
5373 ent = target.ent;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005374 if (!mem_cgroup_move_swap_account(ent,
5375 mc.from, mc.to, false)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08005376 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005377 /* we fixup refcnts and charges later. */
5378 mc.moved_swap++;
5379 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08005380 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005381 default:
5382 break;
5383 }
5384 }
5385 pte_unmap_unlock(pte - 1, ptl);
5386 cond_resched();
5387
5388 if (addr != end) {
5389 /*
5390 * We have consumed all precharges we got in can_attach().
5391 * We try charge one by one, but don't do any additional
5392 * charges to mc.to if we have failed in charge once in attach()
5393 * phase.
5394 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005395 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005396 if (!ret)
5397 goto retry;
5398 }
5399
5400 return ret;
5401}
5402
5403static void mem_cgroup_move_charge(struct mm_struct *mm)
5404{
5405 struct vm_area_struct *vma;
5406
5407 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005408retry:
5409 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
5410 /*
5411 * Someone who are holding the mmap_sem might be waiting in
5412 * waitq. So we cancel all extra charges, wake up all waiters,
5413 * and retry. Because we cancel precharges, we might not be able
5414 * to move enough charges, but moving charge is a best-effort
5415 * feature anyway, so it wouldn't be a big problem.
5416 */
5417 __mem_cgroup_clear_mc();
5418 cond_resched();
5419 goto retry;
5420 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005421 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5422 int ret;
5423 struct mm_walk mem_cgroup_move_charge_walk = {
5424 .pmd_entry = mem_cgroup_move_charge_pte_range,
5425 .mm = mm,
5426 .private = vma,
5427 };
5428 if (is_vm_hugetlb_page(vma))
5429 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005430 ret = walk_page_range(vma->vm_start, vma->vm_end,
5431 &mem_cgroup_move_charge_walk);
5432 if (ret)
5433 /*
5434 * means we have consumed all precharges and failed in
5435 * doing additional charge. Just abandon here.
5436 */
5437 break;
5438 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005439 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005440}
5441
Balbir Singh67e465a2008-02-07 00:13:54 -08005442static void mem_cgroup_move_task(struct cgroup_subsys *ss,
5443 struct cgroup *cont,
5444 struct cgroup *old_cont,
Ben Blumf780bdb2011-05-26 16:25:19 -07005445 struct task_struct *p)
Balbir Singh67e465a2008-02-07 00:13:54 -08005446{
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005447 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005448
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005449 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005450 if (mc.to)
5451 mem_cgroup_move_charge(mm);
5452 put_swap_token(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005453 mmput(mm);
5454 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005455 if (mc.to)
5456 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08005457}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005458#else /* !CONFIG_MMU */
5459static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
5460 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005461 struct task_struct *p)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005462{
5463 return 0;
5464}
5465static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
5466 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005467 struct task_struct *p)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005468{
5469}
5470static void mem_cgroup_move_task(struct cgroup_subsys *ss,
5471 struct cgroup *cont,
5472 struct cgroup *old_cont,
Ben Blumf780bdb2011-05-26 16:25:19 -07005473 struct task_struct *p)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005474{
5475}
5476#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08005477
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005478struct cgroup_subsys mem_cgroup_subsys = {
5479 .name = "memory",
5480 .subsys_id = mem_cgroup_subsys_id,
5481 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005482 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005483 .destroy = mem_cgroup_destroy,
5484 .populate = mem_cgroup_populate,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005485 .can_attach = mem_cgroup_can_attach,
5486 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08005487 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005488 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005489 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005490};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005491
5492#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08005493static int __init enable_swap_account(char *s)
5494{
5495 /* consider enabled if no parameter or 1 is given */
Michal Hockoa2c89902011-05-24 17:12:50 -07005496 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08005497 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07005498 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08005499 really_do_swap_account = 0;
5500 return 1;
5501}
Michal Hockoa2c89902011-05-24 17:12:50 -07005502__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005503
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005504#endif