blob: bd9052a5d3ad74aa2db023c953f0b498e5ab7dd0 [file] [log] [blame]
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001/* memcontrol.c - Memory Controller
2 *
3 * Copyright IBM Corporation, 2007
4 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
5 *
Pavel Emelianov78fb7462008-02-07 00:13:51 -08006 * Copyright 2007 OpenVZ SWsoft Inc
7 * Author: Pavel Emelianov <xemul@openvz.org>
8 *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08009 * Memory thresholds
10 * Copyright (C) 2009 Nokia Corporation
11 * Author: Kirill A. Shutemov
12 *
Balbir Singh8cdea7c2008-02-07 00:13:50 -080013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 */
23
24#include <linux/res_counter.h>
25#include <linux/memcontrol.h>
26#include <linux/cgroup.h>
Pavel Emelianov78fb7462008-02-07 00:13:51 -080027#include <linux/mm.h>
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -080028#include <linux/hugetlb.h>
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -080029#include <linux/pagemap.h>
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080030#include <linux/smp.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080031#include <linux/page-flags.h>
Balbir Singh66e17072008-02-07 00:13:56 -080032#include <linux/backing-dev.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080033#include <linux/bit_spinlock.h>
34#include <linux/rcupdate.h>
Balbir Singhe2224322009-04-02 16:57:39 -070035#include <linux/limits.h>
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -080036#include <linux/mutex.h>
Balbir Singhf64c3f52009-09-23 15:56:37 -070037#include <linux/rbtree.h>
Balbir Singhb6ac57d2008-04-29 01:00:19 -070038#include <linux/slab.h>
Balbir Singh66e17072008-02-07 00:13:56 -080039#include <linux/swap.h>
Daisuke Nishimura02491442010-03-10 15:22:17 -080040#include <linux/swapops.h>
Balbir Singh66e17072008-02-07 00:13:56 -080041#include <linux/spinlock.h>
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -080042#include <linux/eventfd.h>
43#include <linux/sort.h>
Balbir Singh66e17072008-02-07 00:13:56 -080044#include <linux/fs.h>
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -080045#include <linux/seq_file.h>
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -070046#include <linux/vmalloc.h>
Christoph Lameterb69408e2008-10-18 20:26:14 -070047#include <linux/mm_inline.h>
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070048#include <linux/page_cgroup.h>
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -080049#include <linux/cpu.h>
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -070050#include <linux/oom.h>
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -080051#include "internal.h"
Balbir Singh8cdea7c2008-02-07 00:13:50 -080052
Balbir Singh8697d332008-02-07 00:13:59 -080053#include <asm/uaccess.h>
54
KOSAKI Motohirocc8e9702010-08-09 17:19:57 -070055#include <trace/events/vmscan.h>
56
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070057struct cgroup_subsys mem_cgroup_subsys __read_mostly;
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070058#define MEM_CGROUP_RECLAIM_RETRIES 5
Balbir Singh4b3bde42009-09-23 15:56:32 -070059struct mem_cgroup *root_mem_cgroup __read_mostly;
Balbir Singh8cdea7c2008-02-07 00:13:50 -080060
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080061#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
Li Zefan338c8432009-06-17 16:27:15 -070062/* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080063int do_swap_account __read_mostly;
Michal Hockoa42c3902010-11-24 12:57:08 -080064
65/* for remember boot option*/
66#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP_ENABLED
67static int really_do_swap_account __initdata = 1;
68#else
69static int really_do_swap_account __initdata = 0;
70#endif
71
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080072#else
73#define do_swap_account (0)
74#endif
75
76
Balbir Singh8cdea7c2008-02-07 00:13:50 -080077/*
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080078 * Statistics for memory cgroup.
79 */
80enum mem_cgroup_stat_index {
81 /*
82 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
83 */
84 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
Balbir Singhd69b0422009-06-17 16:26:34 -070085 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -080086 MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */
Balbir Singh0c3e73e2009-09-23 15:56:42 -070087 MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -070088 MEM_CGROUP_STAT_DATA, /* end of data requires synchronization */
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -070089 MEM_CGROUP_ON_MOVE, /* someone is moving account between groups */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080090 MEM_CGROUP_STAT_NSTATS,
91};
92
Johannes Weinere9f89742011-03-23 16:42:37 -070093enum mem_cgroup_events_index {
94 MEM_CGROUP_EVENTS_PGPGIN, /* # of pages paged in */
95 MEM_CGROUP_EVENTS_PGPGOUT, /* # of pages paged out */
96 MEM_CGROUP_EVENTS_COUNT, /* # of pages paged in/out */
Ying Han456f9982011-05-26 16:25:38 -070097 MEM_CGROUP_EVENTS_PGFAULT, /* # of page-faults */
98 MEM_CGROUP_EVENTS_PGMAJFAULT, /* # of major page-faults */
Johannes Weinere9f89742011-03-23 16:42:37 -070099 MEM_CGROUP_EVENTS_NSTATS,
100};
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700101/*
102 * Per memcg event counter is incremented at every pagein/pageout. With THP,
103 * it will be incremated by the number of pages. This counter is used for
104 * for trigger some periodic events. This is straightforward and better
105 * than using jiffies etc. to handle periodic memcg event.
106 */
107enum mem_cgroup_events_target {
108 MEM_CGROUP_TARGET_THRESH,
109 MEM_CGROUP_TARGET_SOFTLIMIT,
110 MEM_CGROUP_NTARGETS,
111};
112#define THRESHOLDS_EVENTS_TARGET (128)
113#define SOFTLIMIT_EVENTS_TARGET (1024)
Johannes Weinere9f89742011-03-23 16:42:37 -0700114
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800115struct mem_cgroup_stat_cpu {
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700116 long count[MEM_CGROUP_STAT_NSTATS];
Johannes Weinere9f89742011-03-23 16:42:37 -0700117 unsigned long events[MEM_CGROUP_EVENTS_NSTATS];
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700118 unsigned long targets[MEM_CGROUP_NTARGETS];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800119};
120
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800121/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800122 * per-zone information in memory controller.
123 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800124struct mem_cgroup_per_zone {
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800125 /*
126 * spin_lock to protect the per cgroup LRU
127 */
Christoph Lameterb69408e2008-10-18 20:26:14 -0700128 struct list_head lists[NR_LRU_LISTS];
129 unsigned long count[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800130
131 struct zone_reclaim_stat reclaim_stat;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700132 struct rb_node tree_node; /* RB tree node */
133 unsigned long long usage_in_excess;/* Set to the value by which */
134 /* the soft limit is exceeded*/
135 bool on_tree;
Balbir Singh4e416952009-09-23 15:56:39 -0700136 struct mem_cgroup *mem; /* Back pointer, we cannot */
137 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800138};
139/* Macro for accessing counter */
140#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
141
142struct mem_cgroup_per_node {
143 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
144};
145
146struct mem_cgroup_lru_info {
147 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
148};
149
150/*
Balbir Singhf64c3f52009-09-23 15:56:37 -0700151 * Cgroups above their limits are maintained in a RB-Tree, independent of
152 * their hierarchy representation
153 */
154
155struct mem_cgroup_tree_per_zone {
156 struct rb_root rb_root;
157 spinlock_t lock;
158};
159
160struct mem_cgroup_tree_per_node {
161 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
162};
163
164struct mem_cgroup_tree {
165 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
166};
167
168static struct mem_cgroup_tree soft_limit_tree __read_mostly;
169
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800170struct mem_cgroup_threshold {
171 struct eventfd_ctx *eventfd;
172 u64 threshold;
173};
174
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700175/* For threshold */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800176struct mem_cgroup_threshold_ary {
177 /* An array index points to threshold just below usage. */
Phil Carmody5407a562010-05-26 14:42:42 -0700178 int current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800179 /* Size of entries[] */
180 unsigned int size;
181 /* Array of thresholds */
182 struct mem_cgroup_threshold entries[0];
183};
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700184
185struct mem_cgroup_thresholds {
186 /* Primary thresholds array */
187 struct mem_cgroup_threshold_ary *primary;
188 /*
189 * Spare threshold array.
190 * This is needed to make mem_cgroup_unregister_event() "never fail".
191 * It must be able to store at least primary->size - 1 entries.
192 */
193 struct mem_cgroup_threshold_ary *spare;
194};
195
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700196/* for OOM */
197struct mem_cgroup_eventfd_list {
198 struct list_head list;
199 struct eventfd_ctx *eventfd;
200};
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800201
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800202static void mem_cgroup_threshold(struct mem_cgroup *mem);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700203static void mem_cgroup_oom_notify(struct mem_cgroup *mem);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800204
Balbir Singhf64c3f52009-09-23 15:56:37 -0700205/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800206 * The memory controller data structure. The memory controller controls both
207 * page cache and RSS per cgroup. We would eventually like to provide
208 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
209 * to help the administrator determine what knobs to tune.
210 *
211 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800212 * we hit the water mark. May be even add a low water mark, such that
213 * no reclaim occurs from a cgroup at it's low water mark, this is
214 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800215 */
216struct mem_cgroup {
217 struct cgroup_subsys_state css;
218 /*
219 * the counter to account for memory usage
220 */
221 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800222 /*
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800223 * the counter to account for mem+swap usage.
224 */
225 struct res_counter memsw;
226 /*
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800227 * Per cgroup active and inactive list, similar to the
228 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800229 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800230 struct mem_cgroup_lru_info info;
Balbir Singh6d61ef42009-01-07 18:08:06 -0800231 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200232 * While reclaiming in a hierarchy, we cache the last child we
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700233 * reclaimed from.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800234 */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700235 int last_scanned_child;
Ying Han889976d2011-05-26 16:25:33 -0700236 int last_scanned_node;
237#if MAX_NUMNODES > 1
238 nodemask_t scan_nodes;
239 unsigned long next_scan_node_update;
240#endif
Balbir Singh18f59ea2009-01-07 18:08:07 -0800241 /*
242 * Should the accounting and control be hierarchical, per subtree?
243 */
244 bool use_hierarchy;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -0800245 atomic_t oom_lock;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800246 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800247
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800248 unsigned int swappiness;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -0700249 /* OOM-Killer disable */
250 int oom_kill_disable;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800251
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700252 /* set when res.limit == memsw.limit */
253 bool memsw_is_minimum;
254
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800255 /* protect arrays of thresholds */
256 struct mutex thresholds_lock;
257
258 /* thresholds for memory usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700259 struct mem_cgroup_thresholds thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700260
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800261 /* thresholds for mem+swap usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700262 struct mem_cgroup_thresholds memsw_thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700263
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700264 /* For oom notifier event fd */
265 struct list_head oom_notify;
266
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800267 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800268 * Should we move charges of a task when a task is moved into this
269 * mem_cgroup ? And what type of charges should we move ?
270 */
271 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800272 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800273 * percpu counter.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800274 */
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800275 struct mem_cgroup_stat_cpu *stat;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700276 /*
277 * used when a cpu is offlined or other synchronizations
278 * See mem_cgroup_read_stat().
279 */
280 struct mem_cgroup_stat_cpu nocpu_base;
281 spinlock_t pcp_counter_lock;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800282};
283
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800284/* Stuffs for move charges at task migration. */
285/*
286 * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a
287 * left-shifted bitmap of these types.
288 */
289enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800290 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700291 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800292 NR_MOVE_TYPE,
293};
294
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800295/* "mc" and its members are protected by cgroup_mutex */
296static struct move_charge_struct {
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -0800297 spinlock_t lock; /* for from, to */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800298 struct mem_cgroup *from;
299 struct mem_cgroup *to;
300 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800301 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800302 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800303 struct task_struct *moving_task; /* a task moving charges */
304 wait_queue_head_t waitq; /* a waitq for other context */
305} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700306 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800307 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
308};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800309
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700310static bool move_anon(void)
311{
312 return test_bit(MOVE_CHARGE_TYPE_ANON,
313 &mc.to->move_charge_at_immigrate);
314}
315
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700316static bool move_file(void)
317{
318 return test_bit(MOVE_CHARGE_TYPE_FILE,
319 &mc.to->move_charge_at_immigrate);
320}
321
Balbir Singh4e416952009-09-23 15:56:39 -0700322/*
323 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
324 * limit reclaim to prevent infinite loops, if they ever occur.
325 */
326#define MEM_CGROUP_MAX_RECLAIM_LOOPS (100)
327#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2)
328
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800329enum charge_type {
330 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
331 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700332 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700333 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800334 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700335 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700336 NR_CHARGE_TYPE,
337};
338
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800339/* for encoding cft->private value on file */
340#define _MEM (0)
341#define _MEMSWAP (1)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700342#define _OOM_TYPE (2)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800343#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
344#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
345#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700346/* Used for OOM nofiier */
347#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800348
Balbir Singh75822b42009-09-23 15:56:38 -0700349/*
350 * Reclaim flags for mem_cgroup_hierarchical_reclaim
351 */
352#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
353#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
354#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
355#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
Balbir Singh4e416952009-09-23 15:56:39 -0700356#define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
357#define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
Balbir Singh75822b42009-09-23 15:56:38 -0700358
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800359static void mem_cgroup_get(struct mem_cgroup *mem);
360static void mem_cgroup_put(struct mem_cgroup *mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -0800361static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -0800362static void drain_all_stock_async(void);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800363
Balbir Singhf64c3f52009-09-23 15:56:37 -0700364static struct mem_cgroup_per_zone *
365mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
366{
367 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
368}
369
Wu Fengguangd3242362009-12-16 12:19:59 +0100370struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *mem)
371{
372 return &mem->css;
373}
374
Balbir Singhf64c3f52009-09-23 15:56:37 -0700375static struct mem_cgroup_per_zone *
Johannes Weiner97a6c372011-03-23 16:42:27 -0700376page_cgroup_zoneinfo(struct mem_cgroup *mem, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700377{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700378 int nid = page_to_nid(page);
379 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700380
Balbir Singhf64c3f52009-09-23 15:56:37 -0700381 return mem_cgroup_zoneinfo(mem, nid, zid);
382}
383
384static struct mem_cgroup_tree_per_zone *
385soft_limit_tree_node_zone(int nid, int zid)
386{
387 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
388}
389
390static struct mem_cgroup_tree_per_zone *
391soft_limit_tree_from_page(struct page *page)
392{
393 int nid = page_to_nid(page);
394 int zid = page_zonenum(page);
395
396 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
397}
398
399static void
Balbir Singh4e416952009-09-23 15:56:39 -0700400__mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700401 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700402 struct mem_cgroup_tree_per_zone *mctz,
403 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700404{
405 struct rb_node **p = &mctz->rb_root.rb_node;
406 struct rb_node *parent = NULL;
407 struct mem_cgroup_per_zone *mz_node;
408
409 if (mz->on_tree)
410 return;
411
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700412 mz->usage_in_excess = new_usage_in_excess;
413 if (!mz->usage_in_excess)
414 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700415 while (*p) {
416 parent = *p;
417 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
418 tree_node);
419 if (mz->usage_in_excess < mz_node->usage_in_excess)
420 p = &(*p)->rb_left;
421 /*
422 * We can't avoid mem cgroups that are over their soft
423 * limit by the same amount
424 */
425 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
426 p = &(*p)->rb_right;
427 }
428 rb_link_node(&mz->tree_node, parent, p);
429 rb_insert_color(&mz->tree_node, &mctz->rb_root);
430 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700431}
432
433static void
434__mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
435 struct mem_cgroup_per_zone *mz,
436 struct mem_cgroup_tree_per_zone *mctz)
437{
438 if (!mz->on_tree)
439 return;
440 rb_erase(&mz->tree_node, &mctz->rb_root);
441 mz->on_tree = false;
442}
443
444static void
Balbir Singhf64c3f52009-09-23 15:56:37 -0700445mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
446 struct mem_cgroup_per_zone *mz,
447 struct mem_cgroup_tree_per_zone *mctz)
448{
449 spin_lock(&mctz->lock);
Balbir Singh4e416952009-09-23 15:56:39 -0700450 __mem_cgroup_remove_exceeded(mem, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700451 spin_unlock(&mctz->lock);
452}
453
Balbir Singhf64c3f52009-09-23 15:56:37 -0700454
455static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
456{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700457 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700458 struct mem_cgroup_per_zone *mz;
459 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700460 int nid = page_to_nid(page);
461 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700462 mctz = soft_limit_tree_from_page(page);
463
464 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700465 * Necessary to update all ancestors when hierarchy is used.
466 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700467 */
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700468 for (; mem; mem = parent_mem_cgroup(mem)) {
469 mz = mem_cgroup_zoneinfo(mem, nid, zid);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700470 excess = res_counter_soft_limit_excess(&mem->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700471 /*
472 * We have to update the tree if mz is on RB-tree or
473 * mem is over its softlimit.
474 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700475 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700476 spin_lock(&mctz->lock);
477 /* if on-tree, remove it */
478 if (mz->on_tree)
479 __mem_cgroup_remove_exceeded(mem, mz, mctz);
480 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700481 * Insert again. mz->usage_in_excess will be updated.
482 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700483 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700484 __mem_cgroup_insert_exceeded(mem, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700485 spin_unlock(&mctz->lock);
486 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700487 }
488}
489
490static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
491{
492 int node, zone;
493 struct mem_cgroup_per_zone *mz;
494 struct mem_cgroup_tree_per_zone *mctz;
495
496 for_each_node_state(node, N_POSSIBLE) {
497 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
498 mz = mem_cgroup_zoneinfo(mem, node, zone);
499 mctz = soft_limit_tree_node_zone(node, zone);
500 mem_cgroup_remove_exceeded(mem, mz, mctz);
501 }
502 }
503}
504
Balbir Singh4e416952009-09-23 15:56:39 -0700505static struct mem_cgroup_per_zone *
506__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
507{
508 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700509 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700510
511retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700512 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700513 rightmost = rb_last(&mctz->rb_root);
514 if (!rightmost)
515 goto done; /* Nothing to reclaim from */
516
517 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
518 /*
519 * Remove the node now but someone else can add it back,
520 * we will to add it back at the end of reclaim to its correct
521 * position in the tree.
522 */
523 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
524 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
525 !css_tryget(&mz->mem->css))
526 goto retry;
527done:
528 return mz;
529}
530
531static struct mem_cgroup_per_zone *
532mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
533{
534 struct mem_cgroup_per_zone *mz;
535
536 spin_lock(&mctz->lock);
537 mz = __mem_cgroup_largest_soft_limit_node(mctz);
538 spin_unlock(&mctz->lock);
539 return mz;
540}
541
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700542/*
543 * Implementation Note: reading percpu statistics for memcg.
544 *
545 * Both of vmstat[] and percpu_counter has threshold and do periodic
546 * synchronization to implement "quick" read. There are trade-off between
547 * reading cost and precision of value. Then, we may have a chance to implement
548 * a periodic synchronizion of counter in memcg's counter.
549 *
550 * But this _read() function is used for user interface now. The user accounts
551 * memory usage by memory cgroup and he _always_ requires exact value because
552 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
553 * have to visit all online cpus and make sum. So, for now, unnecessary
554 * synchronization is not implemented. (just implemented for cpu hotplug)
555 *
556 * If there are kernel internal actions which can make use of some not-exact
557 * value, and reading all cpu value can be performance bottleneck in some
558 * common workload, threashold and synchonization as vmstat[] should be
559 * implemented.
560 */
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700561static long mem_cgroup_read_stat(struct mem_cgroup *mem,
562 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800563{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700564 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800565 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800566
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700567 get_online_cpus();
568 for_each_online_cpu(cpu)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800569 val += per_cpu(mem->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700570#ifdef CONFIG_HOTPLUG_CPU
571 spin_lock(&mem->pcp_counter_lock);
572 val += mem->nocpu_base.count[idx];
573 spin_unlock(&mem->pcp_counter_lock);
574#endif
575 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800576 return val;
577}
578
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700579static long mem_cgroup_local_usage(struct mem_cgroup *mem)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800580{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700581 long ret;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800582
583 ret = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS);
584 ret += mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE);
585 return ret;
586}
587
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700588static void mem_cgroup_swap_statistics(struct mem_cgroup *mem,
589 bool charge)
590{
591 int val = (charge) ? 1 : -1;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800592 this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_SWAPOUT], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700593}
594
Ying Han456f9982011-05-26 16:25:38 -0700595void mem_cgroup_pgfault(struct mem_cgroup *mem, int val)
596{
597 this_cpu_add(mem->stat->events[MEM_CGROUP_EVENTS_PGFAULT], val);
598}
599
600void mem_cgroup_pgmajfault(struct mem_cgroup *mem, int val)
601{
602 this_cpu_add(mem->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT], val);
603}
604
Johannes Weinere9f89742011-03-23 16:42:37 -0700605static unsigned long mem_cgroup_read_events(struct mem_cgroup *mem,
606 enum mem_cgroup_events_index idx)
607{
608 unsigned long val = 0;
609 int cpu;
610
611 for_each_online_cpu(cpu)
612 val += per_cpu(mem->stat->events[idx], cpu);
613#ifdef CONFIG_HOTPLUG_CPU
614 spin_lock(&mem->pcp_counter_lock);
615 val += mem->nocpu_base.events[idx];
616 spin_unlock(&mem->pcp_counter_lock);
617#endif
618 return val;
619}
620
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700621static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800622 bool file, int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800623{
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800624 preempt_disable();
625
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800626 if (file)
627 __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_CACHE], nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800628 else
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800629 __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_RSS], nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700630
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800631 /* pagein of a big page is an event. So, ignore page size */
632 if (nr_pages > 0)
Johannes Weinere9f89742011-03-23 16:42:37 -0700633 __this_cpu_inc(mem->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800634 else {
Johannes Weinere9f89742011-03-23 16:42:37 -0700635 __this_cpu_inc(mem->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800636 nr_pages = -nr_pages; /* for event */
637 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800638
Johannes Weinere9f89742011-03-23 16:42:37 -0700639 __this_cpu_add(mem->stat->events[MEM_CGROUP_EVENTS_COUNT], nr_pages);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800640
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800641 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800642}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800643
Ying Han889976d2011-05-26 16:25:33 -0700644static unsigned long
645mem_cgroup_get_zonestat_node(struct mem_cgroup *mem, int nid, enum lru_list idx)
646{
647 struct mem_cgroup_per_zone *mz;
648 u64 total = 0;
649 int zid;
650
651 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
652 mz = mem_cgroup_zoneinfo(mem, nid, zid);
653 total += MEM_CGROUP_ZSTAT(mz, idx);
654 }
655 return total;
656}
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700657static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700658 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800659{
Ying Han889976d2011-05-26 16:25:33 -0700660 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800661 u64 total = 0;
662
663 for_each_online_node(nid)
Ying Han889976d2011-05-26 16:25:33 -0700664 total += mem_cgroup_get_zonestat_node(mem, nid, idx);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800665 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800666}
667
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700668static bool __memcg_event_check(struct mem_cgroup *mem, int target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800669{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700670 unsigned long val, next;
671
672 val = this_cpu_read(mem->stat->events[MEM_CGROUP_EVENTS_COUNT]);
673 next = this_cpu_read(mem->stat->targets[target]);
674 /* from time_after() in jiffies.h */
675 return ((long)next - (long)val < 0);
676}
677
678static void __mem_cgroup_target_update(struct mem_cgroup *mem, int target)
679{
680 unsigned long val, next;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800681
Johannes Weinere9f89742011-03-23 16:42:37 -0700682 val = this_cpu_read(mem->stat->events[MEM_CGROUP_EVENTS_COUNT]);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800683
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700684 switch (target) {
685 case MEM_CGROUP_TARGET_THRESH:
686 next = val + THRESHOLDS_EVENTS_TARGET;
687 break;
688 case MEM_CGROUP_TARGET_SOFTLIMIT:
689 next = val + SOFTLIMIT_EVENTS_TARGET;
690 break;
691 default:
692 return;
693 }
694
695 this_cpu_write(mem->stat->targets[target], next);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800696}
697
698/*
699 * Check events in order.
700 *
701 */
702static void memcg_check_events(struct mem_cgroup *mem, struct page *page)
703{
704 /* threshold event is triggered in finer grain than soft limit */
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700705 if (unlikely(__memcg_event_check(mem, MEM_CGROUP_TARGET_THRESH))) {
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800706 mem_cgroup_threshold(mem);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700707 __mem_cgroup_target_update(mem, MEM_CGROUP_TARGET_THRESH);
708 if (unlikely(__memcg_event_check(mem,
709 MEM_CGROUP_TARGET_SOFTLIMIT))){
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800710 mem_cgroup_update_tree(mem, page);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700711 __mem_cgroup_target_update(mem,
712 MEM_CGROUP_TARGET_SOFTLIMIT);
713 }
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800714 }
715}
716
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800717static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800718{
719 return container_of(cgroup_subsys_state(cont,
720 mem_cgroup_subsys_id), struct mem_cgroup,
721 css);
722}
723
Balbir Singhcf475ad2008-04-29 01:00:16 -0700724struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800725{
Balbir Singh31a78f22008-09-28 23:09:31 +0100726 /*
727 * mm_update_next_owner() may clear mm->owner to NULL
728 * if it races with swapoff, page migration, etc.
729 * So this can be called with p == NULL.
730 */
731 if (unlikely(!p))
732 return NULL;
733
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800734 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
735 struct mem_cgroup, css);
736}
737
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800738static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
739{
740 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700741
742 if (!mm)
743 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800744 /*
745 * Because we have no locks, mm->owner's may be being moved to other
746 * cgroup. We use css_tryget() here even if this looks
747 * pessimistic (rather than adding locks here).
748 */
749 rcu_read_lock();
750 do {
751 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
752 if (unlikely(!mem))
753 break;
754 } while (!css_tryget(&mem->css));
755 rcu_read_unlock();
756 return mem;
757}
758
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700759/* The caller has to guarantee "mem" exists before calling this */
760static struct mem_cgroup *mem_cgroup_start_loop(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700761{
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700762 struct cgroup_subsys_state *css;
763 int found;
764
765 if (!mem) /* ROOT cgroup has the smallest ID */
766 return root_mem_cgroup; /*css_put/get against root is ignored*/
767 if (!mem->use_hierarchy) {
768 if (css_tryget(&mem->css))
769 return mem;
770 return NULL;
771 }
772 rcu_read_lock();
773 /*
774 * searching a memory cgroup which has the smallest ID under given
775 * ROOT cgroup. (ID >= 1)
776 */
777 css = css_get_next(&mem_cgroup_subsys, 1, &mem->css, &found);
778 if (css && css_tryget(css))
779 mem = container_of(css, struct mem_cgroup, css);
780 else
781 mem = NULL;
782 rcu_read_unlock();
783 return mem;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700784}
785
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700786static struct mem_cgroup *mem_cgroup_get_next(struct mem_cgroup *iter,
787 struct mem_cgroup *root,
788 bool cond)
789{
790 int nextid = css_id(&iter->css) + 1;
791 int found;
792 int hierarchy_used;
793 struct cgroup_subsys_state *css;
794
795 hierarchy_used = iter->use_hierarchy;
796
797 css_put(&iter->css);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700798 /* If no ROOT, walk all, ignore hierarchy */
799 if (!cond || (root && !hierarchy_used))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700800 return NULL;
801
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700802 if (!root)
803 root = root_mem_cgroup;
804
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700805 do {
806 iter = NULL;
807 rcu_read_lock();
808
809 css = css_get_next(&mem_cgroup_subsys, nextid,
810 &root->css, &found);
811 if (css && css_tryget(css))
812 iter = container_of(css, struct mem_cgroup, css);
813 rcu_read_unlock();
814 /* If css is NULL, no more cgroups will be found */
815 nextid = found + 1;
816 } while (css && !iter);
817
818 return iter;
819}
820/*
821 * for_eacn_mem_cgroup_tree() for visiting all cgroup under tree. Please
822 * be careful that "break" loop is not allowed. We have reference count.
823 * Instead of that modify "cond" to be false and "continue" to exit the loop.
824 */
825#define for_each_mem_cgroup_tree_cond(iter, root, cond) \
826 for (iter = mem_cgroup_start_loop(root);\
827 iter != NULL;\
828 iter = mem_cgroup_get_next(iter, root, cond))
829
830#define for_each_mem_cgroup_tree(iter, root) \
831 for_each_mem_cgroup_tree_cond(iter, root, true)
832
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700833#define for_each_mem_cgroup_all(iter) \
834 for_each_mem_cgroup_tree_cond(iter, NULL, true)
835
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700836
Balbir Singh4b3bde42009-09-23 15:56:32 -0700837static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
838{
839 return (mem == root_mem_cgroup);
840}
841
Ying Han456f9982011-05-26 16:25:38 -0700842void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
843{
844 struct mem_cgroup *mem;
845
846 if (!mm)
847 return;
848
849 rcu_read_lock();
850 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
851 if (unlikely(!mem))
852 goto out;
853
854 switch (idx) {
855 case PGMAJFAULT:
856 mem_cgroup_pgmajfault(mem, 1);
857 break;
858 case PGFAULT:
859 mem_cgroup_pgfault(mem, 1);
860 break;
861 default:
862 BUG();
863 }
864out:
865 rcu_read_unlock();
866}
867EXPORT_SYMBOL(mem_cgroup_count_vm_event);
868
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800869/*
870 * Following LRU functions are allowed to be used without PCG_LOCK.
871 * Operations are called by routine of global LRU independently from memcg.
872 * What we have to take care of here is validness of pc->mem_cgroup.
873 *
874 * Changes to pc->mem_cgroup happens when
875 * 1. charge
876 * 2. moving account
877 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
878 * It is added to LRU before charge.
879 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
880 * When moving account, the page is not on LRU. It's isolated.
881 */
882
883void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800884{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800885 struct page_cgroup *pc;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800886 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700887
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800888 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800889 return;
890 pc = lookup_page_cgroup(page);
891 /* can happen while we handle swapcache. */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700892 if (!TestClearPageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800893 return;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700894 VM_BUG_ON(!pc->mem_cgroup);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800895 /*
896 * We don't check PCG_USED bit. It's cleared when the "page" is finally
897 * removed from global LRU.
898 */
Johannes Weiner97a6c372011-03-23 16:42:27 -0700899 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -0800900 /* huge page split is done under lru_lock. so, we have no races. */
901 MEM_CGROUP_ZSTAT(mz, lru) -= 1 << compound_order(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700902 if (mem_cgroup_is_root(pc->mem_cgroup))
903 return;
904 VM_BUG_ON(list_empty(&pc->lru));
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800905 list_del_init(&pc->lru);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800906}
907
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800908void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800909{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800910 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800911}
912
Minchan Kim3f58a822011-03-22 16:32:53 -0700913/*
914 * Writeback is about to end against a page which has been marked for immediate
915 * reclaim. If it still appears to be reclaimable, move it to the tail of the
916 * inactive list.
917 */
918void mem_cgroup_rotate_reclaimable_page(struct page *page)
919{
920 struct mem_cgroup_per_zone *mz;
921 struct page_cgroup *pc;
922 enum lru_list lru = page_lru(page);
923
924 if (mem_cgroup_disabled())
925 return;
926
927 pc = lookup_page_cgroup(page);
928 /* unused or root page is not rotated. */
929 if (!PageCgroupUsed(pc))
930 return;
931 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
932 smp_rmb();
933 if (mem_cgroup_is_root(pc->mem_cgroup))
934 return;
Johannes Weiner97a6c372011-03-23 16:42:27 -0700935 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
Minchan Kim3f58a822011-03-22 16:32:53 -0700936 list_move_tail(&pc->lru, &mz->lists[lru]);
937}
938
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800939void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800940{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800941 struct mem_cgroup_per_zone *mz;
942 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800943
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800944 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700945 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700946
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800947 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700948 /* unused or root page is not rotated. */
Johannes Weiner713735b42011-01-20 14:44:31 -0800949 if (!PageCgroupUsed(pc))
950 return;
951 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
952 smp_rmb();
953 if (mem_cgroup_is_root(pc->mem_cgroup))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800954 return;
Johannes Weiner97a6c372011-03-23 16:42:27 -0700955 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700956 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800957}
958
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800959void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
960{
961 struct page_cgroup *pc;
962 struct mem_cgroup_per_zone *mz;
963
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800964 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800965 return;
966 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700967 VM_BUG_ON(PageCgroupAcctLRU(pc));
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800968 if (!PageCgroupUsed(pc))
969 return;
Johannes Weiner713735b42011-01-20 14:44:31 -0800970 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
971 smp_rmb();
Johannes Weiner97a6c372011-03-23 16:42:27 -0700972 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -0800973 /* huge page split is done under lru_lock. so, we have no races. */
974 MEM_CGROUP_ZSTAT(mz, lru) += 1 << compound_order(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700975 SetPageCgroupAcctLRU(pc);
976 if (mem_cgroup_is_root(pc->mem_cgroup))
977 return;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800978 list_add(&pc->lru, &mz->lists[lru]);
979}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800980
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800981/*
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -0700982 * At handling SwapCache and other FUSE stuff, pc->mem_cgroup may be changed
983 * while it's linked to lru because the page may be reused after it's fully
984 * uncharged. To handle that, unlink page_cgroup from LRU when charge it again.
985 * It's done under lock_page and expected that zone->lru_lock isnever held.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800986 */
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -0700987static void mem_cgroup_lru_del_before_commit(struct page *page)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800988{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800989 unsigned long flags;
990 struct zone *zone = page_zone(page);
991 struct page_cgroup *pc = lookup_page_cgroup(page);
992
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -0700993 /*
994 * Doing this check without taking ->lru_lock seems wrong but this
995 * is safe. Because if page_cgroup's USED bit is unset, the page
996 * will not be added to any memcg's LRU. If page_cgroup's USED bit is
997 * set, the commit after this will fail, anyway.
998 * This all charge/uncharge is done under some mutual execustion.
999 * So, we don't need to taking care of changes in USED bit.
1000 */
1001 if (likely(!PageLRU(page)))
1002 return;
1003
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001004 spin_lock_irqsave(&zone->lru_lock, flags);
1005 /*
1006 * Forget old LRU when this page_cgroup is *not* used. This Used bit
1007 * is guarded by lock_page() because the page is SwapCache.
1008 */
1009 if (!PageCgroupUsed(pc))
1010 mem_cgroup_del_lru_list(page, page_lru(page));
1011 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001012}
1013
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001014static void mem_cgroup_lru_add_after_commit(struct page *page)
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001015{
1016 unsigned long flags;
1017 struct zone *zone = page_zone(page);
1018 struct page_cgroup *pc = lookup_page_cgroup(page);
1019
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001020 /* taking care of that the page is added to LRU while we commit it */
1021 if (likely(!PageLRU(page)))
1022 return;
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001023 spin_lock_irqsave(&zone->lru_lock, flags);
1024 /* link when the page is linked to LRU but page_cgroup isn't */
Balbir Singh4b3bde42009-09-23 15:56:32 -07001025 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001026 mem_cgroup_add_lru_list(page, page_lru(page));
1027 spin_unlock_irqrestore(&zone->lru_lock, flags);
1028}
1029
1030
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001031void mem_cgroup_move_lists(struct page *page,
1032 enum lru_list from, enum lru_list to)
1033{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001034 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001035 return;
1036 mem_cgroup_del_lru_list(page, from);
1037 mem_cgroup_add_lru_list(page, to);
1038}
1039
David Rientjes4c4a2212008-02-07 00:14:06 -08001040int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
1041{
1042 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001043 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001044 struct task_struct *p;
David Rientjes4c4a2212008-02-07 00:14:06 -08001045
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001046 p = find_lock_task_mm(task);
1047 if (!p)
1048 return 0;
1049 curr = try_get_mem_cgroup_from_mm(p->mm);
1050 task_unlock(p);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001051 if (!curr)
1052 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001053 /*
1054 * We should check use_hierarchy of "mem" not "curr". Because checking
1055 * use_hierarchy of "curr" here make this function true if hierarchy is
1056 * enabled in "curr" and "curr" is a child of "mem" in *cgroup*
1057 * hierarchy(even if use_hierarchy is disabled in "mem").
1058 */
1059 if (mem->use_hierarchy)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001060 ret = css_is_ancestor(&curr->css, &mem->css);
1061 else
1062 ret = (curr == mem);
1063 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001064 return ret;
1065}
1066
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001067static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001068{
1069 unsigned long active;
1070 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001071 unsigned long gb;
1072 unsigned long inactive_ratio;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001073
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07001074 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
1075 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001076
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001077 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1078 if (gb)
1079 inactive_ratio = int_sqrt(10 * gb);
1080 else
1081 inactive_ratio = 1;
1082
1083 if (present_pages) {
1084 present_pages[0] = inactive;
1085 present_pages[1] = active;
1086 }
1087
1088 return inactive_ratio;
1089}
1090
1091int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
1092{
1093 unsigned long active;
1094 unsigned long inactive;
1095 unsigned long present_pages[2];
1096 unsigned long inactive_ratio;
1097
1098 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
1099
1100 inactive = present_pages[0];
1101 active = present_pages[1];
1102
1103 if (inactive * inactive_ratio < active)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001104 return 1;
1105
1106 return 0;
1107}
1108
Rik van Riel56e49d22009-06-16 15:32:28 -07001109int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
1110{
1111 unsigned long active;
1112 unsigned long inactive;
1113
1114 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
1115 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
1116
1117 return (active > inactive);
1118}
1119
Ying Han1bac1802011-05-26 16:25:36 -07001120unsigned long mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg,
1121 struct zone *zone,
1122 enum lru_list lru)
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -08001123{
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -07001124 int nid = zone_to_nid(zone);
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -08001125 int zid = zone_idx(zone);
1126 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
1127
1128 return MEM_CGROUP_ZSTAT(mz, lru);
1129}
1130
Ying Han406eb0c2011-05-26 16:25:37 -07001131#ifdef CONFIG_NUMA
1132static unsigned long mem_cgroup_node_nr_file_lru_pages(struct mem_cgroup *memcg,
1133 int nid)
1134{
1135 unsigned long ret;
1136
1137 ret = mem_cgroup_get_zonestat_node(memcg, nid, LRU_INACTIVE_FILE) +
1138 mem_cgroup_get_zonestat_node(memcg, nid, LRU_ACTIVE_FILE);
1139
1140 return ret;
1141}
1142
1143static unsigned long mem_cgroup_nr_file_lru_pages(struct mem_cgroup *memcg)
1144{
1145 u64 total = 0;
1146 int nid;
1147
1148 for_each_node_state(nid, N_HIGH_MEMORY)
1149 total += mem_cgroup_node_nr_file_lru_pages(memcg, nid);
1150
1151 return total;
1152}
1153
1154static unsigned long mem_cgroup_node_nr_anon_lru_pages(struct mem_cgroup *memcg,
1155 int nid)
1156{
1157 unsigned long ret;
1158
1159 ret = mem_cgroup_get_zonestat_node(memcg, nid, LRU_INACTIVE_ANON) +
1160 mem_cgroup_get_zonestat_node(memcg, nid, LRU_ACTIVE_ANON);
1161
1162 return ret;
1163}
1164
1165static unsigned long mem_cgroup_nr_anon_lru_pages(struct mem_cgroup *memcg)
1166{
1167 u64 total = 0;
1168 int nid;
1169
1170 for_each_node_state(nid, N_HIGH_MEMORY)
1171 total += mem_cgroup_node_nr_anon_lru_pages(memcg, nid);
1172
1173 return total;
1174}
1175
1176static unsigned long
1177mem_cgroup_node_nr_unevictable_lru_pages(struct mem_cgroup *memcg, int nid)
1178{
1179 return mem_cgroup_get_zonestat_node(memcg, nid, LRU_UNEVICTABLE);
1180}
1181
1182static unsigned long
1183mem_cgroup_nr_unevictable_lru_pages(struct mem_cgroup *memcg)
1184{
1185 u64 total = 0;
1186 int nid;
1187
1188 for_each_node_state(nid, N_HIGH_MEMORY)
1189 total += mem_cgroup_node_nr_unevictable_lru_pages(memcg, nid);
1190
1191 return total;
1192}
1193
1194static unsigned long mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
1195 int nid)
1196{
1197 enum lru_list l;
1198 u64 total = 0;
1199
1200 for_each_lru(l)
1201 total += mem_cgroup_get_zonestat_node(memcg, nid, l);
1202
1203 return total;
1204}
1205
1206static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg)
1207{
1208 u64 total = 0;
1209 int nid;
1210
1211 for_each_node_state(nid, N_HIGH_MEMORY)
1212 total += mem_cgroup_node_nr_lru_pages(memcg, nid);
1213
1214 return total;
1215}
1216#endif /* CONFIG_NUMA */
1217
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001218struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
1219 struct zone *zone)
1220{
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -07001221 int nid = zone_to_nid(zone);
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001222 int zid = zone_idx(zone);
1223 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
1224
1225 return &mz->reclaim_stat;
1226}
1227
1228struct zone_reclaim_stat *
1229mem_cgroup_get_reclaim_stat_from_page(struct page *page)
1230{
1231 struct page_cgroup *pc;
1232 struct mem_cgroup_per_zone *mz;
1233
1234 if (mem_cgroup_disabled())
1235 return NULL;
1236
1237 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -08001238 if (!PageCgroupUsed(pc))
1239 return NULL;
Johannes Weiner713735b42011-01-20 14:44:31 -08001240 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
1241 smp_rmb();
Johannes Weiner97a6c372011-03-23 16:42:27 -07001242 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001243 return &mz->reclaim_stat;
1244}
1245
Balbir Singh66e17072008-02-07 00:13:56 -08001246unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
1247 struct list_head *dst,
1248 unsigned long *scanned, int order,
1249 int mode, struct zone *z,
1250 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001251 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -08001252{
1253 unsigned long nr_taken = 0;
1254 struct page *page;
1255 unsigned long scan;
1256 LIST_HEAD(pc_list);
1257 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001258 struct page_cgroup *pc, *tmp;
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -07001259 int nid = zone_to_nid(z);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001260 int zid = zone_idx(z);
1261 struct mem_cgroup_per_zone *mz;
Johannes Weinerb7c46d12009-09-21 17:02:56 -07001262 int lru = LRU_FILE * file + active;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001263 int ret;
Balbir Singh66e17072008-02-07 00:13:56 -08001264
Balbir Singhcf475ad2008-04-29 01:00:16 -07001265 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001266 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001267 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -08001268
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001269 scan = 0;
1270 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -08001271 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001272 break;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001273
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001274 if (unlikely(!PageCgroupUsed(pc)))
1275 continue;
Johannes Weiner5564e882011-03-23 16:42:29 -07001276
Johannes Weiner6b3ae582011-03-23 16:42:30 -07001277 page = lookup_cgroup_page(pc);
Johannes Weiner5564e882011-03-23 16:42:29 -07001278
Hugh Dickins436c65412008-02-07 00:14:12 -08001279 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001280 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001281
Hugh Dickins436c65412008-02-07 00:14:12 -08001282 scan++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001283 ret = __isolate_lru_page(page, mode, file);
1284 switch (ret) {
1285 case 0:
Balbir Singh66e17072008-02-07 00:13:56 -08001286 list_move(&page->lru, dst);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001287 mem_cgroup_del_lru(page);
Rik van Riel2c888cf2011-01-13 15:47:13 -08001288 nr_taken += hpage_nr_pages(page);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001289 break;
1290 case -EBUSY:
1291 /* we don't affect global LRU but rotate in our LRU */
1292 mem_cgroup_rotate_lru_list(page, page_lru(page));
1293 break;
1294 default:
1295 break;
Balbir Singh66e17072008-02-07 00:13:56 -08001296 }
1297 }
1298
Balbir Singh66e17072008-02-07 00:13:56 -08001299 *scanned = scan;
KOSAKI Motohirocc8e9702010-08-09 17:19:57 -07001300
1301 trace_mm_vmscan_memcg_isolate(0, nr_to_scan, scan, nr_taken,
1302 0, 0, 0, mode);
1303
Balbir Singh66e17072008-02-07 00:13:56 -08001304 return nr_taken;
1305}
1306
Balbir Singh6d61ef42009-01-07 18:08:06 -08001307#define mem_cgroup_from_res_counter(counter, member) \
1308 container_of(counter, struct mem_cgroup, member)
1309
Johannes Weiner19942822011-02-01 15:52:43 -08001310/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001311 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
1312 * @mem: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001313 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001314 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001315 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001316 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001317static unsigned long mem_cgroup_margin(struct mem_cgroup *mem)
Johannes Weiner19942822011-02-01 15:52:43 -08001318{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001319 unsigned long long margin;
1320
1321 margin = res_counter_margin(&mem->res);
1322 if (do_swap_account)
1323 margin = min(margin, res_counter_margin(&mem->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001324 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001325}
1326
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001327static unsigned int get_swappiness(struct mem_cgroup *memcg)
1328{
1329 struct cgroup *cgrp = memcg->css.cgroup;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001330
1331 /* root ? */
1332 if (cgrp->parent == NULL)
1333 return vm_swappiness;
1334
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001335 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001336}
1337
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001338static void mem_cgroup_start_move(struct mem_cgroup *mem)
1339{
1340 int cpu;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001341
1342 get_online_cpus();
1343 spin_lock(&mem->pcp_counter_lock);
1344 for_each_online_cpu(cpu)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001345 per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) += 1;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001346 mem->nocpu_base.count[MEM_CGROUP_ON_MOVE] += 1;
1347 spin_unlock(&mem->pcp_counter_lock);
1348 put_online_cpus();
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001349
1350 synchronize_rcu();
1351}
1352
1353static void mem_cgroup_end_move(struct mem_cgroup *mem)
1354{
1355 int cpu;
1356
1357 if (!mem)
1358 return;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001359 get_online_cpus();
1360 spin_lock(&mem->pcp_counter_lock);
1361 for_each_online_cpu(cpu)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001362 per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) -= 1;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001363 mem->nocpu_base.count[MEM_CGROUP_ON_MOVE] -= 1;
1364 spin_unlock(&mem->pcp_counter_lock);
1365 put_online_cpus();
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001366}
1367/*
1368 * 2 routines for checking "mem" is under move_account() or not.
1369 *
1370 * mem_cgroup_stealed() - checking a cgroup is mc.from or not. This is used
1371 * for avoiding race in accounting. If true,
1372 * pc->mem_cgroup may be overwritten.
1373 *
1374 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1375 * under hierarchy of moving cgroups. This is for
1376 * waiting at hith-memory prressure caused by "move".
1377 */
1378
1379static bool mem_cgroup_stealed(struct mem_cgroup *mem)
1380{
1381 VM_BUG_ON(!rcu_read_lock_held());
1382 return this_cpu_read(mem->stat->count[MEM_CGROUP_ON_MOVE]) > 0;
1383}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001384
1385static bool mem_cgroup_under_move(struct mem_cgroup *mem)
1386{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001387 struct mem_cgroup *from;
1388 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001389 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001390 /*
1391 * Unlike task_move routines, we access mc.to, mc.from not under
1392 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1393 */
1394 spin_lock(&mc.lock);
1395 from = mc.from;
1396 to = mc.to;
1397 if (!from)
1398 goto unlock;
1399 if (from == mem || to == mem
1400 || (mem->use_hierarchy && css_is_ancestor(&from->css, &mem->css))
1401 || (mem->use_hierarchy && css_is_ancestor(&to->css, &mem->css)))
1402 ret = true;
1403unlock:
1404 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001405 return ret;
1406}
1407
1408static bool mem_cgroup_wait_acct_move(struct mem_cgroup *mem)
1409{
1410 if (mc.moving_task && current != mc.moving_task) {
1411 if (mem_cgroup_under_move(mem)) {
1412 DEFINE_WAIT(wait);
1413 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1414 /* moving charge context might have finished. */
1415 if (mc.moving_task)
1416 schedule();
1417 finish_wait(&mc.waitq, &wait);
1418 return true;
1419 }
1420 }
1421 return false;
1422}
1423
Balbir Singhe2224322009-04-02 16:57:39 -07001424/**
Kirill A. Shutemov6a6135b2010-03-10 15:22:25 -08001425 * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode.
Balbir Singhe2224322009-04-02 16:57:39 -07001426 * @memcg: The memory cgroup that went over limit
1427 * @p: Task that is going to be killed
1428 *
1429 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1430 * enabled
1431 */
1432void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1433{
1434 struct cgroup *task_cgrp;
1435 struct cgroup *mem_cgrp;
1436 /*
1437 * Need a buffer in BSS, can't rely on allocations. The code relies
1438 * on the assumption that OOM is serialized for memory controller.
1439 * If this assumption is broken, revisit this code.
1440 */
1441 static char memcg_name[PATH_MAX];
1442 int ret;
1443
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001444 if (!memcg || !p)
Balbir Singhe2224322009-04-02 16:57:39 -07001445 return;
1446
1447
1448 rcu_read_lock();
1449
1450 mem_cgrp = memcg->css.cgroup;
1451 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1452
1453 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1454 if (ret < 0) {
1455 /*
1456 * Unfortunately, we are unable to convert to a useful name
1457 * But we'll still print out the usage information
1458 */
1459 rcu_read_unlock();
1460 goto done;
1461 }
1462 rcu_read_unlock();
1463
1464 printk(KERN_INFO "Task in %s killed", memcg_name);
1465
1466 rcu_read_lock();
1467 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1468 if (ret < 0) {
1469 rcu_read_unlock();
1470 goto done;
1471 }
1472 rcu_read_unlock();
1473
1474 /*
1475 * Continues from above, so we don't need an KERN_ level
1476 */
1477 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1478done:
1479
1480 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1481 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1482 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1483 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1484 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1485 "failcnt %llu\n",
1486 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1487 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1488 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1489}
1490
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001491/*
1492 * This function returns the number of memcg under hierarchy tree. Returns
1493 * 1(self count) if no children.
1494 */
1495static int mem_cgroup_count_children(struct mem_cgroup *mem)
1496{
1497 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001498 struct mem_cgroup *iter;
1499
1500 for_each_mem_cgroup_tree(iter, mem)
1501 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001502 return num;
1503}
1504
Balbir Singh6d61ef42009-01-07 18:08:06 -08001505/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001506 * Return the memory (and swap, if configured) limit for a memcg.
1507 */
1508u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
1509{
1510 u64 limit;
1511 u64 memsw;
1512
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001513 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
1514 limit += total_swap_pages << PAGE_SHIFT;
1515
David Rientjesa63d83f2010-08-09 17:19:46 -07001516 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1517 /*
1518 * If memsw is finite and limits the amount of swap space available
1519 * to this memcg, return that limit.
1520 */
1521 return min(limit, memsw);
1522}
1523
1524/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001525 * Visit the first child (need not be the first child as per the ordering
1526 * of the cgroup list, since we track last_scanned_child) of @mem and use
1527 * that to reclaim free pages from.
1528 */
1529static struct mem_cgroup *
1530mem_cgroup_select_victim(struct mem_cgroup *root_mem)
1531{
1532 struct mem_cgroup *ret = NULL;
1533 struct cgroup_subsys_state *css;
1534 int nextid, found;
1535
1536 if (!root_mem->use_hierarchy) {
1537 css_get(&root_mem->css);
1538 ret = root_mem;
1539 }
1540
1541 while (!ret) {
1542 rcu_read_lock();
1543 nextid = root_mem->last_scanned_child + 1;
1544 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
1545 &found);
1546 if (css && css_tryget(css))
1547 ret = container_of(css, struct mem_cgroup, css);
1548
1549 rcu_read_unlock();
1550 /* Updates scanning parameter */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001551 if (!css) {
1552 /* this means start scan from ID:1 */
1553 root_mem->last_scanned_child = 0;
1554 } else
1555 root_mem->last_scanned_child = found;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001556 }
1557
1558 return ret;
1559}
1560
Ying Han889976d2011-05-26 16:25:33 -07001561#if MAX_NUMNODES > 1
1562
1563/*
1564 * Always updating the nodemask is not very good - even if we have an empty
1565 * list or the wrong list here, we can start from some node and traverse all
1566 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1567 *
1568 */
1569static void mem_cgroup_may_update_nodemask(struct mem_cgroup *mem)
1570{
1571 int nid;
1572
1573 if (time_after(mem->next_scan_node_update, jiffies))
1574 return;
1575
1576 mem->next_scan_node_update = jiffies + 10*HZ;
1577 /* make a nodemask where this memcg uses memory from */
1578 mem->scan_nodes = node_states[N_HIGH_MEMORY];
1579
1580 for_each_node_mask(nid, node_states[N_HIGH_MEMORY]) {
1581
1582 if (mem_cgroup_get_zonestat_node(mem, nid, LRU_INACTIVE_FILE) ||
1583 mem_cgroup_get_zonestat_node(mem, nid, LRU_ACTIVE_FILE))
1584 continue;
1585
1586 if (total_swap_pages &&
1587 (mem_cgroup_get_zonestat_node(mem, nid, LRU_INACTIVE_ANON) ||
1588 mem_cgroup_get_zonestat_node(mem, nid, LRU_ACTIVE_ANON)))
1589 continue;
1590 node_clear(nid, mem->scan_nodes);
1591 }
1592}
1593
1594/*
1595 * Selecting a node where we start reclaim from. Because what we need is just
1596 * reducing usage counter, start from anywhere is O,K. Considering
1597 * memory reclaim from current node, there are pros. and cons.
1598 *
1599 * Freeing memory from current node means freeing memory from a node which
1600 * we'll use or we've used. So, it may make LRU bad. And if several threads
1601 * hit limits, it will see a contention on a node. But freeing from remote
1602 * node means more costs for memory reclaim because of memory latency.
1603 *
1604 * Now, we use round-robin. Better algorithm is welcomed.
1605 */
1606int mem_cgroup_select_victim_node(struct mem_cgroup *mem)
1607{
1608 int node;
1609
1610 mem_cgroup_may_update_nodemask(mem);
1611 node = mem->last_scanned_node;
1612
1613 node = next_node(node, mem->scan_nodes);
1614 if (node == MAX_NUMNODES)
1615 node = first_node(mem->scan_nodes);
1616 /*
1617 * We call this when we hit limit, not when pages are added to LRU.
1618 * No LRU may hold pages because all pages are UNEVICTABLE or
1619 * memcg is too small and all pages are not on LRU. In that case,
1620 * we use curret node.
1621 */
1622 if (unlikely(node == MAX_NUMNODES))
1623 node = numa_node_id();
1624
1625 mem->last_scanned_node = node;
1626 return node;
1627}
1628
1629#else
1630int mem_cgroup_select_victim_node(struct mem_cgroup *mem)
1631{
1632 return 0;
1633}
1634#endif
1635
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001636/*
1637 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1638 * we reclaimed from, so that we don't end up penalizing one child extensively
1639 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001640 *
1641 * root_mem is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001642 *
1643 * We give up and return to the caller when we visit root_mem twice.
1644 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001645 *
1646 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001647 */
1648static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
Balbir Singh4e416952009-09-23 15:56:39 -07001649 struct zone *zone,
Balbir Singh75822b42009-09-23 15:56:38 -07001650 gfp_t gfp_mask,
Ying Han0ae5e892011-05-26 16:25:25 -07001651 unsigned long reclaim_options,
1652 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001653{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001654 struct mem_cgroup *victim;
1655 int ret, total = 0;
1656 int loop = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001657 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1658 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
Balbir Singh4e416952009-09-23 15:56:39 -07001659 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001660 unsigned long excess;
Ying Han0ae5e892011-05-26 16:25:25 -07001661 unsigned long nr_scanned;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001662
1663 excess = res_counter_soft_limit_excess(&root_mem->res) >> PAGE_SHIFT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001664
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001665 /* If memsw_is_minimum==1, swap-out is of-no-use. */
1666 if (root_mem->memsw_is_minimum)
1667 noswap = true;
1668
Balbir Singh4e416952009-09-23 15:56:39 -07001669 while (1) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001670 victim = mem_cgroup_select_victim(root_mem);
Balbir Singh4e416952009-09-23 15:56:39 -07001671 if (victim == root_mem) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001672 loop++;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001673 if (loop >= 1)
1674 drain_all_stock_async();
Balbir Singh4e416952009-09-23 15:56:39 -07001675 if (loop >= 2) {
1676 /*
1677 * If we have not been able to reclaim
1678 * anything, it might because there are
1679 * no reclaimable pages under this hierarchy
1680 */
1681 if (!check_soft || !total) {
1682 css_put(&victim->css);
1683 break;
1684 }
1685 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001686 * We want to do more targeted reclaim.
Balbir Singh4e416952009-09-23 15:56:39 -07001687 * excess >> 2 is not to excessive so as to
1688 * reclaim too much, nor too less that we keep
1689 * coming back to reclaim from this cgroup
1690 */
1691 if (total >= (excess >> 2) ||
1692 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1693 css_put(&victim->css);
1694 break;
1695 }
1696 }
1697 }
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08001698 if (!mem_cgroup_local_usage(victim)) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001699 /* this cgroup's local usage == 0 */
1700 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001701 continue;
1702 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001703 /* we use swappiness of local cgroup */
Ying Han0ae5e892011-05-26 16:25:25 -07001704 if (check_soft) {
Balbir Singh4e416952009-09-23 15:56:39 -07001705 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
Ying Han0ae5e892011-05-26 16:25:25 -07001706 noswap, get_swappiness(victim), zone,
1707 &nr_scanned);
1708 *total_scanned += nr_scanned;
1709 } else
Balbir Singh4e416952009-09-23 15:56:39 -07001710 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
1711 noswap, get_swappiness(victim));
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001712 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001713 /*
1714 * At shrinking usage, we can't check we should stop here or
1715 * reclaim more. It's depends on callers. last_scanned_child
1716 * will work enough for keeping fairness under tree.
1717 */
1718 if (shrink)
1719 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001720 total += ret;
Balbir Singh4e416952009-09-23 15:56:39 -07001721 if (check_soft) {
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001722 if (!res_counter_soft_limit_excess(&root_mem->res))
Balbir Singh4e416952009-09-23 15:56:39 -07001723 return total;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001724 } else if (mem_cgroup_margin(root_mem))
Johannes Weiner4fd14eb2011-05-26 16:25:35 -07001725 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001726 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001727 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001728}
1729
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001730/*
1731 * Check OOM-Killer is already running under our hierarchy.
1732 * If someone is running, return false.
1733 */
1734static bool mem_cgroup_oom_lock(struct mem_cgroup *mem)
1735{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001736 int x, lock_count = 0;
1737 struct mem_cgroup *iter;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001738
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001739 for_each_mem_cgroup_tree(iter, mem) {
1740 x = atomic_inc_return(&iter->oom_lock);
1741 lock_count = max(x, lock_count);
1742 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001743
1744 if (lock_count == 1)
1745 return true;
1746 return false;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001747}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001748
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001749static int mem_cgroup_oom_unlock(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001750{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001751 struct mem_cgroup *iter;
1752
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001753 /*
1754 * When a new child is created while the hierarchy is under oom,
1755 * mem_cgroup_oom_lock() may not be called. We have to use
1756 * atomic_add_unless() here.
1757 */
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001758 for_each_mem_cgroup_tree(iter, mem)
1759 atomic_add_unless(&iter->oom_lock, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001760 return 0;
1761}
1762
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001763
1764static DEFINE_MUTEX(memcg_oom_mutex);
1765static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1766
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001767struct oom_wait_info {
1768 struct mem_cgroup *mem;
1769 wait_queue_t wait;
1770};
1771
1772static int memcg_oom_wake_function(wait_queue_t *wait,
1773 unsigned mode, int sync, void *arg)
1774{
1775 struct mem_cgroup *wake_mem = (struct mem_cgroup *)arg;
1776 struct oom_wait_info *oom_wait_info;
1777
1778 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
1779
1780 if (oom_wait_info->mem == wake_mem)
1781 goto wakeup;
1782 /* if no hierarchy, no match */
1783 if (!oom_wait_info->mem->use_hierarchy || !wake_mem->use_hierarchy)
1784 return 0;
1785 /*
1786 * Both of oom_wait_info->mem and wake_mem are stable under us.
1787 * Then we can use css_is_ancestor without taking care of RCU.
1788 */
1789 if (!css_is_ancestor(&oom_wait_info->mem->css, &wake_mem->css) &&
1790 !css_is_ancestor(&wake_mem->css, &oom_wait_info->mem->css))
1791 return 0;
1792
1793wakeup:
1794 return autoremove_wake_function(wait, mode, sync, arg);
1795}
1796
1797static void memcg_wakeup_oom(struct mem_cgroup *mem)
1798{
1799 /* for filtering, pass "mem" as argument. */
1800 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, mem);
1801}
1802
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001803static void memcg_oom_recover(struct mem_cgroup *mem)
1804{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001805 if (mem && atomic_read(&mem->oom_lock))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001806 memcg_wakeup_oom(mem);
1807}
1808
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001809/*
1810 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
1811 */
1812bool mem_cgroup_handle_oom(struct mem_cgroup *mem, gfp_t mask)
1813{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001814 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001815 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001816
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001817 owait.mem = mem;
1818 owait.wait.flags = 0;
1819 owait.wait.func = memcg_oom_wake_function;
1820 owait.wait.private = current;
1821 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001822 need_to_kill = true;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001823 /* At first, try to OOM lock hierarchy under mem.*/
1824 mutex_lock(&memcg_oom_mutex);
1825 locked = mem_cgroup_oom_lock(mem);
1826 /*
1827 * Even if signal_pending(), we can't quit charge() loop without
1828 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
1829 * under OOM is always welcomed, use TASK_KILLABLE here.
1830 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001831 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
1832 if (!locked || mem->oom_kill_disable)
1833 need_to_kill = false;
1834 if (locked)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07001835 mem_cgroup_oom_notify(mem);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001836 mutex_unlock(&memcg_oom_mutex);
1837
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001838 if (need_to_kill) {
1839 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001840 mem_cgroup_out_of_memory(mem, mask);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001841 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001842 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001843 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001844 }
1845 mutex_lock(&memcg_oom_mutex);
1846 mem_cgroup_oom_unlock(mem);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001847 memcg_wakeup_oom(mem);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001848 mutex_unlock(&memcg_oom_mutex);
1849
1850 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
1851 return false;
1852 /* Give chance to dying process */
1853 schedule_timeout(1);
1854 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001855}
1856
Balbir Singhd69b0422009-06-17 16:26:34 -07001857/*
1858 * Currently used to update mapped file statistics, but the routine can be
1859 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001860 *
1861 * Notes: Race condition
1862 *
1863 * We usually use page_cgroup_lock() for accessing page_cgroup member but
1864 * it tends to be costly. But considering some conditions, we doesn't need
1865 * to do so _always_.
1866 *
1867 * Considering "charge", lock_page_cgroup() is not required because all
1868 * file-stat operations happen after a page is attached to radix-tree. There
1869 * are no race with "charge".
1870 *
1871 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
1872 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
1873 * if there are race with "uncharge". Statistics itself is properly handled
1874 * by flags.
1875 *
1876 * Considering "move", this is an only case we see a race. To make the race
1877 * small, we check MEM_CGROUP_ON_MOVE percpu value and detect there are
1878 * possibility of race condition. If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07001879 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001880
Greg Thelen2a7106f2011-01-13 15:47:37 -08001881void mem_cgroup_update_page_stat(struct page *page,
1882 enum mem_cgroup_page_stat_item idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07001883{
1884 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001885 struct page_cgroup *pc = lookup_page_cgroup(page);
1886 bool need_unlock = false;
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08001887 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07001888
Balbir Singhd69b0422009-06-17 16:26:34 -07001889 if (unlikely(!pc))
1890 return;
1891
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001892 rcu_read_lock();
Balbir Singhd69b0422009-06-17 16:26:34 -07001893 mem = pc->mem_cgroup;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001894 if (unlikely(!mem || !PageCgroupUsed(pc)))
1895 goto out;
1896 /* pc->mem_cgroup is unstable ? */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08001897 if (unlikely(mem_cgroup_stealed(mem)) || PageTransHuge(page)) {
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001898 /* take a lock against to access pc->mem_cgroup */
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08001899 move_lock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001900 need_unlock = true;
1901 mem = pc->mem_cgroup;
1902 if (!mem || !PageCgroupUsed(pc))
1903 goto out;
1904 }
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001905
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001906 switch (idx) {
Greg Thelen2a7106f2011-01-13 15:47:37 -08001907 case MEMCG_NR_FILE_MAPPED:
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001908 if (val > 0)
1909 SetPageCgroupFileMapped(pc);
1910 else if (!page_mapped(page))
KAMEZAWA Hiroyuki0c270f82010-10-27 15:33:39 -07001911 ClearPageCgroupFileMapped(pc);
Greg Thelen2a7106f2011-01-13 15:47:37 -08001912 idx = MEM_CGROUP_STAT_FILE_MAPPED;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001913 break;
1914 default:
1915 BUG();
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07001916 }
Balbir Singhd69b0422009-06-17 16:26:34 -07001917
Greg Thelen2a7106f2011-01-13 15:47:37 -08001918 this_cpu_add(mem->stat->count[idx], val);
1919
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001920out:
1921 if (unlikely(need_unlock))
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08001922 move_unlock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001923 rcu_read_unlock();
1924 return;
Balbir Singhd69b0422009-06-17 16:26:34 -07001925}
Greg Thelen2a7106f2011-01-13 15:47:37 -08001926EXPORT_SYMBOL(mem_cgroup_update_page_stat);
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001927
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001928/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001929 * size of first charge trial. "32" comes from vmscan.c's magic value.
1930 * TODO: maybe necessary to use big numbers in big irons.
1931 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001932#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001933struct memcg_stock_pcp {
1934 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07001935 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001936 struct work_struct work;
1937};
1938static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
1939static atomic_t memcg_drain_count;
1940
1941/*
Johannes Weiner11c9ea42011-03-23 16:42:34 -07001942 * Try to consume stocked charge on this cpu. If success, one page is consumed
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001943 * from local stock and true is returned. If the stock is 0 or charges from a
1944 * cgroup which is not current target, returns false. This stock will be
1945 * refilled.
1946 */
1947static bool consume_stock(struct mem_cgroup *mem)
1948{
1949 struct memcg_stock_pcp *stock;
1950 bool ret = true;
1951
1952 stock = &get_cpu_var(memcg_stock);
Johannes Weiner11c9ea42011-03-23 16:42:34 -07001953 if (mem == stock->cached && stock->nr_pages)
1954 stock->nr_pages--;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001955 else /* need to call res_counter_charge */
1956 ret = false;
1957 put_cpu_var(memcg_stock);
1958 return ret;
1959}
1960
1961/*
1962 * Returns stocks cached in percpu to res_counter and reset cached information.
1963 */
1964static void drain_stock(struct memcg_stock_pcp *stock)
1965{
1966 struct mem_cgroup *old = stock->cached;
1967
Johannes Weiner11c9ea42011-03-23 16:42:34 -07001968 if (stock->nr_pages) {
1969 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
1970
1971 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001972 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07001973 res_counter_uncharge(&old->memsw, bytes);
1974 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001975 }
1976 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001977}
1978
1979/*
1980 * This must be called under preempt disabled or must be called by
1981 * a thread which is pinned to local cpu.
1982 */
1983static void drain_local_stock(struct work_struct *dummy)
1984{
1985 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
1986 drain_stock(stock);
1987}
1988
1989/*
1990 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01001991 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001992 */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07001993static void refill_stock(struct mem_cgroup *mem, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001994{
1995 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
1996
1997 if (stock->cached != mem) { /* reset if necessary */
1998 drain_stock(stock);
1999 stock->cached = mem;
2000 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002001 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002002 put_cpu_var(memcg_stock);
2003}
2004
2005/*
2006 * Tries to drain stocked charges in other cpus. This function is asynchronous
2007 * and just put a work per cpu for draining localy on each cpu. Caller can
2008 * expects some charges will be back to res_counter later but cannot wait for
2009 * it.
2010 */
2011static void drain_all_stock_async(void)
2012{
2013 int cpu;
2014 /* This function is for scheduling "drain" in asynchronous way.
2015 * The result of "drain" is not directly handled by callers. Then,
2016 * if someone is calling drain, we don't have to call drain more.
2017 * Anyway, WORK_STRUCT_PENDING check in queue_work_on() will catch if
2018 * there is a race. We just do loose check here.
2019 */
2020 if (atomic_read(&memcg_drain_count))
2021 return;
2022 /* Notify other cpus that system-wide "drain" is running */
2023 atomic_inc(&memcg_drain_count);
2024 get_online_cpus();
2025 for_each_online_cpu(cpu) {
2026 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
2027 schedule_work_on(cpu, &stock->work);
2028 }
2029 put_online_cpus();
2030 atomic_dec(&memcg_drain_count);
2031 /* We don't wait for flush_work */
2032}
2033
2034/* This is a synchronous drain interface. */
2035static void drain_all_stock_sync(void)
2036{
2037 /* called when force_empty is called */
2038 atomic_inc(&memcg_drain_count);
2039 schedule_on_each_cpu(drain_local_stock);
2040 atomic_dec(&memcg_drain_count);
2041}
2042
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002043/*
2044 * This function drains percpu counter value from DEAD cpu and
2045 * move it to local cpu. Note that this function can be preempted.
2046 */
2047static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *mem, int cpu)
2048{
2049 int i;
2050
2051 spin_lock(&mem->pcp_counter_lock);
2052 for (i = 0; i < MEM_CGROUP_STAT_DATA; i++) {
Johannes Weiner7a159cc2011-03-23 16:42:38 -07002053 long x = per_cpu(mem->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002054
2055 per_cpu(mem->stat->count[i], cpu) = 0;
2056 mem->nocpu_base.count[i] += x;
2057 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002058 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
2059 unsigned long x = per_cpu(mem->stat->events[i], cpu);
2060
2061 per_cpu(mem->stat->events[i], cpu) = 0;
2062 mem->nocpu_base.events[i] += x;
2063 }
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002064 /* need to clear ON_MOVE value, works as a kind of lock. */
2065 per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) = 0;
2066 spin_unlock(&mem->pcp_counter_lock);
2067}
2068
2069static void synchronize_mem_cgroup_on_move(struct mem_cgroup *mem, int cpu)
2070{
2071 int idx = MEM_CGROUP_ON_MOVE;
2072
2073 spin_lock(&mem->pcp_counter_lock);
2074 per_cpu(mem->stat->count[idx], cpu) = mem->nocpu_base.count[idx];
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002075 spin_unlock(&mem->pcp_counter_lock);
2076}
2077
2078static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002079 unsigned long action,
2080 void *hcpu)
2081{
2082 int cpu = (unsigned long)hcpu;
2083 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002084 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002085
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002086 if ((action == CPU_ONLINE)) {
2087 for_each_mem_cgroup_all(iter)
2088 synchronize_mem_cgroup_on_move(iter, cpu);
2089 return NOTIFY_OK;
2090 }
2091
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002092 if ((action != CPU_DEAD) || action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002093 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002094
2095 for_each_mem_cgroup_all(iter)
2096 mem_cgroup_drain_pcp_counter(iter, cpu);
2097
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002098 stock = &per_cpu(memcg_stock, cpu);
2099 drain_stock(stock);
2100 return NOTIFY_OK;
2101}
2102
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002103
2104/* See __mem_cgroup_try_charge() for details */
2105enum {
2106 CHARGE_OK, /* success */
2107 CHARGE_RETRY, /* need to retry but retry is not bad */
2108 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2109 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
2110 CHARGE_OOM_DIE, /* the current is killed because of OOM */
2111};
2112
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002113static int mem_cgroup_do_charge(struct mem_cgroup *mem, gfp_t gfp_mask,
2114 unsigned int nr_pages, bool oom_check)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002115{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002116 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002117 struct mem_cgroup *mem_over_limit;
2118 struct res_counter *fail_res;
2119 unsigned long flags = 0;
2120 int ret;
2121
2122 ret = res_counter_charge(&mem->res, csize, &fail_res);
2123
2124 if (likely(!ret)) {
2125 if (!do_swap_account)
2126 return CHARGE_OK;
2127 ret = res_counter_charge(&mem->memsw, csize, &fail_res);
2128 if (likely(!ret))
2129 return CHARGE_OK;
2130
KAMEZAWA Hiroyuki01c88e22011-01-25 15:07:27 -08002131 res_counter_uncharge(&mem->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002132 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2133 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2134 } else
2135 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002136 /*
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002137 * nr_pages can be either a huge page (HPAGE_PMD_NR), a batch
2138 * of regular pages (CHARGE_BATCH), or a single regular page (1).
Johannes Weiner9221edb2011-02-01 15:52:42 -08002139 *
2140 * Never reclaim on behalf of optional batching, retry with a
2141 * single page instead.
2142 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002143 if (nr_pages == CHARGE_BATCH)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002144 return CHARGE_RETRY;
2145
2146 if (!(gfp_mask & __GFP_WAIT))
2147 return CHARGE_WOULDBLOCK;
2148
2149 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
Ying Han0ae5e892011-05-26 16:25:25 -07002150 gfp_mask, flags, NULL);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002151 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002152 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002153 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002154 * Even though the limit is exceeded at this point, reclaim
2155 * may have been able to free some pages. Retry the charge
2156 * before killing the task.
2157 *
2158 * Only for regular pages, though: huge pages are rather
2159 * unlikely to succeed so close to the limit, and we fall back
2160 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002161 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002162 if (nr_pages == 1 && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002163 return CHARGE_RETRY;
2164
2165 /*
2166 * At task move, charge accounts can be doubly counted. So, it's
2167 * better to wait until the end of task_move if something is going on.
2168 */
2169 if (mem_cgroup_wait_acct_move(mem_over_limit))
2170 return CHARGE_RETRY;
2171
2172 /* If we don't need to call oom-killer at el, return immediately */
2173 if (!oom_check)
2174 return CHARGE_NOMEM;
2175 /* check OOM */
2176 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask))
2177 return CHARGE_OOM_DIE;
2178
2179 return CHARGE_RETRY;
2180}
2181
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002182/*
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002183 * Unlike exported interface, "oom" parameter is added. if oom==true,
2184 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002185 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002186static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002187 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002188 unsigned int nr_pages,
2189 struct mem_cgroup **memcg,
2190 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002191{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002192 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002193 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2194 struct mem_cgroup *mem = NULL;
2195 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002196
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002197 /*
2198 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2199 * in system level. So, allow to go ahead dying process in addition to
2200 * MEMDIE process.
2201 */
2202 if (unlikely(test_thread_flag(TIF_MEMDIE)
2203 || fatal_signal_pending(current)))
2204 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002205
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002206 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08002207 * We always charge the cgroup the mm_struct belongs to.
2208 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002209 * thread group leader migrates. It's possible that mm is not
2210 * set, if so charge the init_mm (happens for pagecache usage).
2211 */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002212 if (!*memcg && !mm)
2213 goto bypass;
2214again:
2215 if (*memcg) { /* css should be a valid one */
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002216 mem = *memcg;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002217 VM_BUG_ON(css_is_removed(&mem->css));
2218 if (mem_cgroup_is_root(mem))
2219 goto done;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002220 if (nr_pages == 1 && consume_stock(mem))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002221 goto done;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002222 css_get(&mem->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002223 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002224 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002225
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002226 rcu_read_lock();
2227 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002228 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002229 * Because we don't have task_lock(), "p" can exit.
2230 * In that case, "mem" can point to root or p can be NULL with
2231 * race with swapoff. Then, we have small risk of mis-accouning.
2232 * But such kind of mis-account by race always happens because
2233 * we don't have cgroup_mutex(). It's overkill and we allo that
2234 * small race, here.
2235 * (*) swapoff at el will charge against mm-struct not against
2236 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002237 */
2238 mem = mem_cgroup_from_task(p);
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002239 if (!mem || mem_cgroup_is_root(mem)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002240 rcu_read_unlock();
2241 goto done;
2242 }
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002243 if (nr_pages == 1 && consume_stock(mem)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002244 /*
2245 * It seems dagerous to access memcg without css_get().
2246 * But considering how consume_stok works, it's not
2247 * necessary. If consume_stock success, some charges
2248 * from this memcg are cached on this cpu. So, we
2249 * don't need to call css_get()/css_tryget() before
2250 * calling consume_stock().
2251 */
2252 rcu_read_unlock();
2253 goto done;
2254 }
2255 /* after here, we may be blocked. we need to get refcnt */
2256 if (!css_tryget(&mem->css)) {
2257 rcu_read_unlock();
2258 goto again;
2259 }
2260 rcu_read_unlock();
2261 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002262
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002263 do {
2264 bool oom_check;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002265
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002266 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002267 if (fatal_signal_pending(current)) {
2268 css_put(&mem->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002269 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002270 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002271
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002272 oom_check = false;
2273 if (oom && !nr_oom_retries) {
2274 oom_check = true;
2275 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2276 }
Balbir Singh6d61ef42009-01-07 18:08:06 -08002277
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002278 ret = mem_cgroup_do_charge(mem, gfp_mask, batch, oom_check);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002279 switch (ret) {
2280 case CHARGE_OK:
2281 break;
2282 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002283 batch = nr_pages;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002284 css_put(&mem->css);
2285 mem = NULL;
2286 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002287 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002288 css_put(&mem->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002289 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002290 case CHARGE_NOMEM: /* OOM routine works */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002291 if (!oom) {
2292 css_put(&mem->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002293 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002294 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002295 /* If oom, we never return -ENOMEM */
2296 nr_oom_retries--;
2297 break;
2298 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002299 css_put(&mem->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002300 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08002301 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002302 } while (ret != CHARGE_OK);
2303
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002304 if (batch > nr_pages)
2305 refill_stock(mem, batch - nr_pages);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002306 css_put(&mem->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002307done:
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002308 *memcg = mem;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002309 return 0;
2310nomem:
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002311 *memcg = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002312 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002313bypass:
2314 *memcg = NULL;
2315 return 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002316}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002317
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002318/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002319 * Somemtimes we have to undo a charge we got by try_charge().
2320 * This function is for that and do uncharge, put css's refcnt.
2321 * gotten by try_charge().
2322 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002323static void __mem_cgroup_cancel_charge(struct mem_cgroup *mem,
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002324 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002325{
2326 if (!mem_cgroup_is_root(mem)) {
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002327 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002328
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002329 res_counter_uncharge(&mem->res, bytes);
2330 if (do_swap_account)
2331 res_counter_uncharge(&mem->memsw, bytes);
2332 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002333}
2334
2335/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002336 * A helper function to get mem_cgroup from ID. must be called under
2337 * rcu_read_lock(). The caller must check css_is_removed() or some if
2338 * it's concern. (dropping refcnt from swap can be called against removed
2339 * memcg.)
2340 */
2341static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2342{
2343 struct cgroup_subsys_state *css;
2344
2345 /* ID 0 is unused ID */
2346 if (!id)
2347 return NULL;
2348 css = css_lookup(&mem_cgroup_subsys, id);
2349 if (!css)
2350 return NULL;
2351 return container_of(css, struct mem_cgroup, css);
2352}
2353
Wu Fengguange42d9d52009-12-16 12:19:59 +01002354struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002355{
Wu Fengguange42d9d52009-12-16 12:19:59 +01002356 struct mem_cgroup *mem = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002357 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002358 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002359 swp_entry_t ent;
2360
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002361 VM_BUG_ON(!PageLocked(page));
2362
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002363 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002364 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002365 if (PageCgroupUsed(pc)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002366 mem = pc->mem_cgroup;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002367 if (mem && !css_tryget(&mem->css))
2368 mem = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002369 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002370 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002371 id = lookup_swap_cgroup(ent);
2372 rcu_read_lock();
2373 mem = mem_cgroup_lookup(id);
2374 if (mem && !css_tryget(&mem->css))
2375 mem = NULL;
2376 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002377 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002378 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002379 return mem;
2380}
2381
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002382static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
Johannes Weiner5564e882011-03-23 16:42:29 -07002383 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002384 unsigned int nr_pages,
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002385 struct page_cgroup *pc,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002386 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002387{
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002388 lock_page_cgroup(pc);
2389 if (unlikely(PageCgroupUsed(pc))) {
2390 unlock_page_cgroup(pc);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002391 __mem_cgroup_cancel_charge(mem, nr_pages);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002392 return;
2393 }
2394 /*
2395 * we don't need page_cgroup_lock about tail pages, becase they are not
2396 * accessed by any other context at this point.
2397 */
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002398 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002399 /*
2400 * We access a page_cgroup asynchronously without lock_page_cgroup().
2401 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2402 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2403 * before USED bit, we need memory barrier here.
2404 * See mem_cgroup_add_lru_list(), etc.
2405 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002406 smp_wmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -07002407 switch (ctype) {
2408 case MEM_CGROUP_CHARGE_TYPE_CACHE:
2409 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
2410 SetPageCgroupCache(pc);
2411 SetPageCgroupUsed(pc);
2412 break;
2413 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
2414 ClearPageCgroupCache(pc);
2415 SetPageCgroupUsed(pc);
2416 break;
2417 default:
2418 break;
2419 }
Hugh Dickins3be91272008-02-07 00:14:19 -08002420
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002421 mem_cgroup_charge_statistics(mem, PageCgroupCache(pc), nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002422 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002423 /*
2424 * "charge_statistics" updated event counter. Then, check it.
2425 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2426 * if they exceeds softlimit.
2427 */
Johannes Weiner5564e882011-03-23 16:42:29 -07002428 memcg_check_events(mem, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002429}
2430
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002431#ifdef CONFIG_TRANSPARENT_HUGEPAGE
2432
2433#define PCGF_NOCOPY_AT_SPLIT ((1 << PCG_LOCK) | (1 << PCG_MOVE_LOCK) |\
2434 (1 << PCG_ACCT_LRU) | (1 << PCG_MIGRATION))
2435/*
2436 * Because tail pages are not marked as "used", set it. We're under
2437 * zone->lru_lock, 'splitting on pmd' and compund_lock.
2438 */
2439void mem_cgroup_split_huge_fixup(struct page *head, struct page *tail)
2440{
2441 struct page_cgroup *head_pc = lookup_page_cgroup(head);
2442 struct page_cgroup *tail_pc = lookup_page_cgroup(tail);
2443 unsigned long flags;
2444
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08002445 if (mem_cgroup_disabled())
2446 return;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002447 /*
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002448 * We have no races with charge/uncharge but will have races with
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002449 * page state accounting.
2450 */
2451 move_lock_page_cgroup(head_pc, &flags);
2452
2453 tail_pc->mem_cgroup = head_pc->mem_cgroup;
2454 smp_wmb(); /* see __commit_charge() */
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002455 if (PageCgroupAcctLRU(head_pc)) {
2456 enum lru_list lru;
2457 struct mem_cgroup_per_zone *mz;
2458
2459 /*
2460 * LRU flags cannot be copied because we need to add tail
2461 *.page to LRU by generic call and our hook will be called.
2462 * We hold lru_lock, then, reduce counter directly.
2463 */
2464 lru = page_lru(head);
Johannes Weiner97a6c372011-03-23 16:42:27 -07002465 mz = page_cgroup_zoneinfo(head_pc->mem_cgroup, head);
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002466 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
2467 }
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002468 tail_pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
2469 move_unlock_page_cgroup(head_pc, &flags);
2470}
2471#endif
2472
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002473/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07002474 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07002475 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002476 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002477 * @pc: page_cgroup of the page.
2478 * @from: mem_cgroup which the page is moved from.
2479 * @to: mem_cgroup which the page is moved to. @from != @to.
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002480 * @uncharge: whether we should call uncharge and css_put against @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002481 *
2482 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002483 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002484 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002485 *
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002486 * This function doesn't do "charge" nor css_get to new cgroup. It should be
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002487 * done by a caller(__mem_cgroup_try_charge would be useful). If @uncharge is
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002488 * true, this function does "uncharge" from old cgroup, but it doesn't if
2489 * @uncharge is false, so a caller should do "uncharge".
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002490 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002491static int mem_cgroup_move_account(struct page *page,
2492 unsigned int nr_pages,
2493 struct page_cgroup *pc,
2494 struct mem_cgroup *from,
2495 struct mem_cgroup *to,
2496 bool uncharge)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002497{
Johannes Weinerde3638d2011-03-23 16:42:28 -07002498 unsigned long flags;
2499 int ret;
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002500
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002501 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07002502 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07002503 /*
2504 * The page is isolated from LRU. So, collapse function
2505 * will not handle this page. But page splitting can happen.
2506 * Do this check under compound_page_lock(). The caller should
2507 * hold it.
2508 */
2509 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002510 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07002511 goto out;
2512
2513 lock_page_cgroup(pc);
2514
2515 ret = -EINVAL;
2516 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
2517 goto unlock;
2518
2519 move_lock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002520
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002521 if (PageCgroupFileMapped(pc)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08002522 /* Update mapped_file data for mem_cgroup */
2523 preempt_disable();
2524 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2525 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2526 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07002527 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002528 mem_cgroup_charge_statistics(from, PageCgroupCache(pc), -nr_pages);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002529 if (uncharge)
2530 /* This is not "cancel", but cancel_charge does all we need. */
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002531 __mem_cgroup_cancel_charge(from, nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07002532
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002533 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002534 pc->mem_cgroup = to;
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002535 mem_cgroup_charge_statistics(to, PageCgroupCache(pc), nr_pages);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002536 /*
2537 * We charges against "to" which may not have any tasks. Then, "to"
2538 * can be under rmdir(). But in current implementation, caller of
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08002539 * this function is just force_empty() and move charge, so it's
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002540 * guaranteed that "to" is never removed. So, we don't check rmdir
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08002541 * status here.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002542 */
Johannes Weinerde3638d2011-03-23 16:42:28 -07002543 move_unlock_page_cgroup(pc, &flags);
2544 ret = 0;
2545unlock:
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002546 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002547 /*
2548 * check events
2549 */
Johannes Weiner5564e882011-03-23 16:42:29 -07002550 memcg_check_events(to, page);
2551 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07002552out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002553 return ret;
2554}
2555
2556/*
2557 * move charges to its parent.
2558 */
2559
Johannes Weiner5564e882011-03-23 16:42:29 -07002560static int mem_cgroup_move_parent(struct page *page,
2561 struct page_cgroup *pc,
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002562 struct mem_cgroup *child,
2563 gfp_t gfp_mask)
2564{
2565 struct cgroup *cg = child->css.cgroup;
2566 struct cgroup *pcg = cg->parent;
2567 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002568 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07002569 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002570 int ret;
2571
2572 /* Is ROOT ? */
2573 if (!pcg)
2574 return -EINVAL;
2575
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002576 ret = -EBUSY;
2577 if (!get_page_unless_zero(page))
2578 goto out;
2579 if (isolate_lru_page(page))
2580 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08002581
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002582 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002583
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002584 parent = mem_cgroup_from_cont(pcg);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002585 ret = __mem_cgroup_try_charge(NULL, gfp_mask, nr_pages, &parent, false);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002586 if (ret || !parent)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002587 goto put_back;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002588
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002589 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002590 flags = compound_lock_irqsave(page);
2591
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002592 ret = mem_cgroup_move_account(page, nr_pages, pc, child, parent, true);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002593 if (ret)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002594 __mem_cgroup_cancel_charge(parent, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08002595
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002596 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002597 compound_unlock_irqrestore(page, flags);
Jesper Juhl8dba4742011-01-25 15:07:24 -08002598put_back:
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002599 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002600put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08002601 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002602out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002603 return ret;
2604}
2605
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002606/*
2607 * Charge the memory controller for page usage.
2608 * Return
2609 * 0 if the charge was successful
2610 * < 0 if the cgroup is over its limit
2611 */
2612static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002613 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002614{
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002615 struct mem_cgroup *mem = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002616 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08002617 struct page_cgroup *pc;
2618 bool oom = true;
2619 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08002620
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002621 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002622 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002623 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08002624 /*
2625 * Never OOM-kill a process for a huge page. The
2626 * fault handler will fall back to regular pages.
2627 */
2628 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002629 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002630
2631 pc = lookup_page_cgroup(page);
Johannes Weineraf4a6622011-03-23 16:42:24 -07002632 BUG_ON(!pc); /* XXX: remove this and move pc lookup into commit */
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002633
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002634 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &mem, oom);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002635 if (ret || !mem)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002636 return ret;
2637
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002638 __mem_cgroup_commit_charge(mem, page, nr_pages, pc, ctype);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002639 return 0;
2640}
2641
2642int mem_cgroup_newpage_charge(struct page *page,
2643 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002644{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002645 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002646 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002647 /*
2648 * If already mapped, we don't have to account.
2649 * If page cache, page->mapping has address_space.
2650 * But page->mapping may have out-of-use anon_vma pointer,
2651 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
2652 * is NULL.
2653 */
2654 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
2655 return 0;
2656 if (unlikely(!mm))
2657 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002658 return mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002659 MEM_CGROUP_CHARGE_TYPE_MAPPED);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002660}
2661
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002662static void
2663__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2664 enum charge_type ctype);
2665
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002666static void
2667__mem_cgroup_commit_charge_lrucare(struct page *page, struct mem_cgroup *mem,
2668 enum charge_type ctype)
2669{
2670 struct page_cgroup *pc = lookup_page_cgroup(page);
2671 /*
2672 * In some case, SwapCache, FUSE(splice_buf->radixtree), the page
2673 * is already on LRU. It means the page may on some other page_cgroup's
2674 * LRU. Take care of it.
2675 */
2676 mem_cgroup_lru_del_before_commit(page);
2677 __mem_cgroup_commit_charge(mem, page, 1, pc, ctype);
2678 mem_cgroup_lru_add_after_commit(page);
2679 return;
2680}
2681
Balbir Singhe1a1cd52008-02-07 00:14:02 -08002682int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
2683 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08002684{
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002685 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002686 int ret;
2687
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002688 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002689 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002690 if (PageCompound(page))
2691 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002692 /*
2693 * Corner case handling. This is called from add_to_page_cache()
2694 * in usual. But some FS (shmem) precharges this page before calling it
2695 * and call add_to_page_cache() with GFP_NOWAIT.
2696 *
2697 * For GFP_NOWAIT case, the page may be pre-charged before calling
2698 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
2699 * charge twice. (It works but has to pay a bit larger cost.)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002700 * And when the page is SwapCache, it should take swap information
2701 * into account. This is under lock_page() now.
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002702 */
2703 if (!(gfp_mask & __GFP_WAIT)) {
2704 struct page_cgroup *pc;
2705
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002706 pc = lookup_page_cgroup(page);
2707 if (!pc)
2708 return 0;
2709 lock_page_cgroup(pc);
2710 if (PageCgroupUsed(pc)) {
2711 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002712 return 0;
2713 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002714 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002715 }
2716
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002717 if (unlikely(!mm))
Balbir Singh8697d332008-02-07 00:13:59 -08002718 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002719
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002720 if (page_is_file_cache(page)) {
2721 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, &mem, true);
2722 if (ret || !mem)
2723 return ret;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002724
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002725 /*
2726 * FUSE reuses pages without going through the final
2727 * put that would remove them from the LRU list, make
2728 * sure that they get relinked properly.
2729 */
2730 __mem_cgroup_commit_charge_lrucare(page, mem,
2731 MEM_CGROUP_CHARGE_TYPE_CACHE);
2732 return ret;
2733 }
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002734 /* shmem */
2735 if (PageSwapCache(page)) {
2736 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2737 if (!ret)
2738 __mem_cgroup_commit_charge_swapin(page, mem,
2739 MEM_CGROUP_CHARGE_TYPE_SHMEM);
2740 } else
2741 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002742 MEM_CGROUP_CHARGE_TYPE_SHMEM);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002743
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002744 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002745}
2746
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002747/*
2748 * While swap-in, try_charge -> commit or cancel, the page is locked.
2749 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02002750 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002751 * "commit()" or removed by "cancel()"
2752 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002753int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
2754 struct page *page,
2755 gfp_t mask, struct mem_cgroup **ptr)
2756{
2757 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002758 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002759
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07002760 *ptr = NULL;
2761
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002762 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002763 return 0;
2764
2765 if (!do_swap_account)
2766 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002767 /*
2768 * A racing thread's fault, or swapoff, may have already updated
Hugh Dickins407f9c82009-12-14 17:59:30 -08002769 * the pte, and even removed page from swap cache: in those cases
2770 * do_swap_page()'s pte_same() test will fail; but there's also a
2771 * KSM case which does need to charge the page.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002772 */
2773 if (!PageSwapCache(page))
Hugh Dickins407f9c82009-12-14 17:59:30 -08002774 goto charge_cur_mm;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002775 mem = try_get_mem_cgroup_from_page(page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002776 if (!mem)
2777 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002778 *ptr = mem;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002779 ret = __mem_cgroup_try_charge(NULL, mask, 1, ptr, true);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002780 css_put(&mem->css);
2781 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002782charge_cur_mm:
2783 if (unlikely(!mm))
2784 mm = &init_mm;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002785 return __mem_cgroup_try_charge(mm, mask, 1, ptr, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002786}
2787
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002788static void
2789__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2790 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002791{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002792 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002793 return;
2794 if (!ptr)
2795 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002796 cgroup_exclude_rmdir(&ptr->css);
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002797
2798 __mem_cgroup_commit_charge_lrucare(page, ptr, ctype);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002799 /*
2800 * Now swap is on-memory. This means this page may be
2801 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002802 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
2803 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
2804 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002805 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002806 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002807 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002808 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002809 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002810
2811 id = swap_cgroup_record(ent, 0);
2812 rcu_read_lock();
2813 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002814 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002815 /*
2816 * This recorded memcg can be obsolete one. So, avoid
2817 * calling css_tryget
2818 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002819 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002820 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002821 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002822 mem_cgroup_put(memcg);
2823 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002824 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002825 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002826 /*
2827 * At swapin, we may charge account against cgroup which has no tasks.
2828 * So, rmdir()->pre_destroy() can be called while we do this charge.
2829 * In that case, we need to call pre_destroy() again. check it here.
2830 */
2831 cgroup_release_and_wakeup_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002832}
2833
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002834void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
2835{
2836 __mem_cgroup_commit_charge_swapin(page, ptr,
2837 MEM_CGROUP_CHARGE_TYPE_MAPPED);
2838}
2839
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002840void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
2841{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002842 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002843 return;
2844 if (!mem)
2845 return;
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002846 __mem_cgroup_cancel_charge(mem, 1);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002847}
2848
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002849static void mem_cgroup_do_uncharge(struct mem_cgroup *mem,
2850 unsigned int nr_pages,
2851 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002852{
2853 struct memcg_batch_info *batch = NULL;
2854 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002855
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002856 /* If swapout, usage of swap doesn't decrease */
2857 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2858 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002859
2860 batch = &current->memcg_batch;
2861 /*
2862 * In usual, we do css_get() when we remember memcg pointer.
2863 * But in this case, we keep res->usage until end of a series of
2864 * uncharges. Then, it's ok to ignore memcg's refcnt.
2865 */
2866 if (!batch->memcg)
2867 batch->memcg = mem;
2868 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002869 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002870 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002871 * the same cgroup and we have chance to coalesce uncharges.
2872 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
2873 * because we want to do uncharge as soon as possible.
2874 */
2875
2876 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
2877 goto direct_uncharge;
2878
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002879 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08002880 goto direct_uncharge;
2881
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002882 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002883 * In typical case, batch->memcg == mem. This means we can
2884 * merge a series of uncharges to an uncharge of res_counter.
2885 * If not, we uncharge res_counter ony by one.
2886 */
2887 if (batch->memcg != mem)
2888 goto direct_uncharge;
2889 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07002890 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002891 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07002892 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002893 return;
2894direct_uncharge:
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002895 res_counter_uncharge(&mem->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002896 if (uncharge_memsw)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002897 res_counter_uncharge(&mem->memsw, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002898 if (unlikely(batch->memcg != mem))
2899 memcg_oom_recover(mem);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002900 return;
2901}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002902
Balbir Singh8697d332008-02-07 00:13:59 -08002903/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002904 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002905 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002906static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002907__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002908{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002909 struct mem_cgroup *mem = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002910 unsigned int nr_pages = 1;
2911 struct page_cgroup *pc;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002912
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002913 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002914 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07002915
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002916 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002917 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002918
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002919 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002920 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002921 VM_BUG_ON(!PageTransHuge(page));
2922 }
Balbir Singh8697d332008-02-07 00:13:59 -08002923 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08002924 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08002925 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002926 pc = lookup_page_cgroup(page);
2927 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002928 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002929
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002930 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002931
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002932 mem = pc->mem_cgroup;
2933
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002934 if (!PageCgroupUsed(pc))
2935 goto unlock_out;
2936
2937 switch (ctype) {
2938 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002939 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002940 /* See mem_cgroup_prepare_migration() */
2941 if (page_mapped(page) || PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002942 goto unlock_out;
2943 break;
2944 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
2945 if (!PageAnon(page)) { /* Shared memory */
2946 if (page->mapping && !page_is_file_cache(page))
2947 goto unlock_out;
2948 } else if (page_mapped(page)) /* Anon */
2949 goto unlock_out;
2950 break;
2951 default:
2952 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002953 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002954
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002955 mem_cgroup_charge_statistics(mem, PageCgroupCache(pc), -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002956
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002957 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08002958 /*
2959 * pc->mem_cgroup is not cleared here. It will be accessed when it's
2960 * freed from LRU. This is safe because uncharged page is expected not
2961 * to be reused (freed soon). Exception is SwapCache, it's handled by
2962 * special functions.
2963 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08002964
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002965 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002966 /*
2967 * even after unlock, we have mem->res.usage here and this memcg
2968 * will never be freed.
2969 */
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002970 memcg_check_events(mem, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002971 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
2972 mem_cgroup_swap_statistics(mem, true);
2973 mem_cgroup_get(mem);
2974 }
2975 if (!mem_cgroup_is_root(mem))
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002976 mem_cgroup_do_uncharge(mem, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002977
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002978 return mem;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002979
2980unlock_out:
2981 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002982 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08002983}
2984
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002985void mem_cgroup_uncharge_page(struct page *page)
2986{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002987 /* early check. */
2988 if (page_mapped(page))
2989 return;
2990 if (page->mapping && !PageAnon(page))
2991 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002992 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
2993}
2994
2995void mem_cgroup_uncharge_cache_page(struct page *page)
2996{
2997 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07002998 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002999 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
3000}
3001
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003002/*
3003 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
3004 * In that cases, pages are freed continuously and we can expect pages
3005 * are in the same memcg. All these calls itself limits the number of
3006 * pages freed at once, then uncharge_start/end() is called properly.
3007 * This may be called prural(2) times in a context,
3008 */
3009
3010void mem_cgroup_uncharge_start(void)
3011{
3012 current->memcg_batch.do_batch++;
3013 /* We can do nest. */
3014 if (current->memcg_batch.do_batch == 1) {
3015 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003016 current->memcg_batch.nr_pages = 0;
3017 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003018 }
3019}
3020
3021void mem_cgroup_uncharge_end(void)
3022{
3023 struct memcg_batch_info *batch = &current->memcg_batch;
3024
3025 if (!batch->do_batch)
3026 return;
3027
3028 batch->do_batch--;
3029 if (batch->do_batch) /* If stacked, do nothing. */
3030 return;
3031
3032 if (!batch->memcg)
3033 return;
3034 /*
3035 * This "batch->memcg" is valid without any css_get/put etc...
3036 * bacause we hide charges behind us.
3037 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003038 if (batch->nr_pages)
3039 res_counter_uncharge(&batch->memcg->res,
3040 batch->nr_pages * PAGE_SIZE);
3041 if (batch->memsw_nr_pages)
3042 res_counter_uncharge(&batch->memcg->memsw,
3043 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003044 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003045 /* forget this pointer (for sanity check) */
3046 batch->memcg = NULL;
3047}
3048
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003049#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003050/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003051 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003052 * memcg information is recorded to swap_cgroup of "ent"
3053 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003054void
3055mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003056{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003057 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003058 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003059
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003060 if (!swapout) /* this was a swap cache but the swap is unused ! */
3061 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
3062
3063 memcg = __mem_cgroup_uncharge_common(page, ctype);
3064
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003065 /*
3066 * record memcg information, if swapout && memcg != NULL,
3067 * mem_cgroup_get() was called in uncharge().
3068 */
3069 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003070 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003071}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003072#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003073
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003074#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3075/*
3076 * called from swap_entry_free(). remove record in swap_cgroup and
3077 * uncharge "memsw" account.
3078 */
3079void mem_cgroup_uncharge_swap(swp_entry_t ent)
3080{
3081 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003082 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003083
3084 if (!do_swap_account)
3085 return;
3086
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003087 id = swap_cgroup_record(ent, 0);
3088 rcu_read_lock();
3089 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003090 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003091 /*
3092 * We uncharge this because swap is freed.
3093 * This memcg can be obsolete one. We avoid calling css_tryget
3094 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003095 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07003096 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003097 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003098 mem_cgroup_put(memcg);
3099 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003100 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003101}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003102
3103/**
3104 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
3105 * @entry: swap entry to be moved
3106 * @from: mem_cgroup which the entry is moved from
3107 * @to: mem_cgroup which the entry is moved to
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003108 * @need_fixup: whether we should fixup res_counters and refcounts.
Daisuke Nishimura02491442010-03-10 15:22:17 -08003109 *
3110 * It succeeds only when the swap_cgroup's record for this entry is the same
3111 * as the mem_cgroup's id of @from.
3112 *
3113 * Returns 0 on success, -EINVAL on failure.
3114 *
3115 * The caller must have charged to @to, IOW, called res_counter_charge() about
3116 * both res and memsw, and called css_get().
3117 */
3118static int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003119 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003120{
3121 unsigned short old_id, new_id;
3122
3123 old_id = css_id(&from->css);
3124 new_id = css_id(&to->css);
3125
3126 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08003127 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003128 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003129 /*
3130 * This function is only called from task migration context now.
3131 * It postpones res_counter and refcount handling till the end
3132 * of task migration(mem_cgroup_clear_mc()) for performance
3133 * improvement. But we cannot postpone mem_cgroup_get(to)
3134 * because if the process that has been moved to @to does
3135 * swap-in, the refcount of @to might be decreased to 0.
3136 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08003137 mem_cgroup_get(to);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003138 if (need_fixup) {
3139 if (!mem_cgroup_is_root(from))
3140 res_counter_uncharge(&from->memsw, PAGE_SIZE);
3141 mem_cgroup_put(from);
3142 /*
3143 * we charged both to->res and to->memsw, so we should
3144 * uncharge to->res.
3145 */
3146 if (!mem_cgroup_is_root(to))
3147 res_counter_uncharge(&to->res, PAGE_SIZE);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003148 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08003149 return 0;
3150 }
3151 return -EINVAL;
3152}
3153#else
3154static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003155 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003156{
3157 return -EINVAL;
3158}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003159#endif
3160
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003161/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003162 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
3163 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003164 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003165int mem_cgroup_prepare_migration(struct page *page,
Miklos Szeredief6a3c62011-03-22 16:30:52 -07003166 struct page *newpage, struct mem_cgroup **ptr, gfp_t gfp_mask)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003167{
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003168 struct mem_cgroup *mem = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003169 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003170 enum charge_type ctype;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003171 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003172
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07003173 *ptr = NULL;
3174
Andrea Arcangeliec168512011-01-13 15:46:56 -08003175 VM_BUG_ON(PageTransHuge(page));
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003176 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07003177 return 0;
3178
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003179 pc = lookup_page_cgroup(page);
3180 lock_page_cgroup(pc);
3181 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003182 mem = pc->mem_cgroup;
3183 css_get(&mem->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003184 /*
3185 * At migrating an anonymous page, its mapcount goes down
3186 * to 0 and uncharge() will be called. But, even if it's fully
3187 * unmapped, migration may fail and this page has to be
3188 * charged again. We set MIGRATION flag here and delay uncharge
3189 * until end_migration() is called
3190 *
3191 * Corner Case Thinking
3192 * A)
3193 * When the old page was mapped as Anon and it's unmap-and-freed
3194 * while migration was ongoing.
3195 * If unmap finds the old page, uncharge() of it will be delayed
3196 * until end_migration(). If unmap finds a new page, it's
3197 * uncharged when it make mapcount to be 1->0. If unmap code
3198 * finds swap_migration_entry, the new page will not be mapped
3199 * and end_migration() will find it(mapcount==0).
3200 *
3201 * B)
3202 * When the old page was mapped but migraion fails, the kernel
3203 * remaps it. A charge for it is kept by MIGRATION flag even
3204 * if mapcount goes down to 0. We can do remap successfully
3205 * without charging it again.
3206 *
3207 * C)
3208 * The "old" page is under lock_page() until the end of
3209 * migration, so, the old page itself will not be swapped-out.
3210 * If the new page is swapped out before end_migraton, our
3211 * hook to usual swap-out path will catch the event.
3212 */
3213 if (PageAnon(page))
3214 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003215 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003216 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003217 /*
3218 * If the page is not charged at this point,
3219 * we return here.
3220 */
3221 if (!mem)
3222 return 0;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003223
Andrea Arcangeli93d5c9b2010-04-23 13:17:39 -04003224 *ptr = mem;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003225 ret = __mem_cgroup_try_charge(NULL, gfp_mask, 1, ptr, false);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003226 css_put(&mem->css);/* drop extra refcnt */
3227 if (ret || *ptr == NULL) {
3228 if (PageAnon(page)) {
3229 lock_page_cgroup(pc);
3230 ClearPageCgroupMigration(pc);
3231 unlock_page_cgroup(pc);
3232 /*
3233 * The old page may be fully unmapped while we kept it.
3234 */
3235 mem_cgroup_uncharge_page(page);
3236 }
3237 return -ENOMEM;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003238 }
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003239 /*
3240 * We charge new page before it's used/mapped. So, even if unlock_page()
3241 * is called before end_migration, we can catch all events on this new
3242 * page. In the case new page is migrated but not remapped, new page's
3243 * mapcount will be finally 0 and we call uncharge in end_migration().
3244 */
3245 pc = lookup_page_cgroup(newpage);
3246 if (PageAnon(page))
3247 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
3248 else if (page_is_file_cache(page))
3249 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
3250 else
3251 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003252 __mem_cgroup_commit_charge(mem, page, 1, pc, ctype);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003253 return ret;
3254}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08003255
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003256/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003257void mem_cgroup_end_migration(struct mem_cgroup *mem,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003258 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003259{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003260 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003261 struct page_cgroup *pc;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003262
3263 if (!mem)
3264 return;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003265 /* blocks rmdir() */
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003266 cgroup_exclude_rmdir(&mem->css);
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003267 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003268 used = oldpage;
3269 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003270 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003271 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003272 unused = oldpage;
3273 }
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003274 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003275 * We disallowed uncharge of pages under migration because mapcount
3276 * of the page goes down to zero, temporarly.
3277 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003278 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003279 pc = lookup_page_cgroup(oldpage);
3280 lock_page_cgroup(pc);
3281 ClearPageCgroupMigration(pc);
3282 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003283
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003284 __mem_cgroup_uncharge_common(unused, MEM_CGROUP_CHARGE_TYPE_FORCE);
3285
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003286 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003287 * If a page is a file cache, radix-tree replacement is very atomic
3288 * and we can skip this check. When it was an Anon page, its mapcount
3289 * goes down to 0. But because we added MIGRATION flage, it's not
3290 * uncharged yet. There are several case but page->mapcount check
3291 * and USED bit check in mem_cgroup_uncharge_page() will do enough
3292 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003293 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003294 if (PageAnon(used))
3295 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003296 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003297 * At migration, we may charge account against cgroup which has no
3298 * tasks.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003299 * So, rmdir()->pre_destroy() can be called while we do this charge.
3300 * In that case, we need to call pre_destroy() again. check it here.
3301 */
3302 cgroup_release_and_wakeup_rmdir(&mem->css);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003303}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08003304
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003305/*
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07003306 * A call to try to shrink memory usage on charge failure at shmem's swapin.
3307 * Calling hierarchical_reclaim is not enough because we should update
3308 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
3309 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
3310 * not from the memcg which this page would be charged to.
3311 * try_charge_swapin does all of these works properly.
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003312 */
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07003313int mem_cgroup_shmem_charge_fallback(struct page *page,
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08003314 struct mm_struct *mm,
3315 gfp_t gfp_mask)
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003316{
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07003317 struct mem_cgroup *mem;
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07003318 int ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003319
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003320 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07003321 return 0;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003322
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07003323 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
3324 if (!ret)
3325 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003326
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07003327 return ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003328}
3329
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003330#ifdef CONFIG_DEBUG_VM
3331static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
3332{
3333 struct page_cgroup *pc;
3334
3335 pc = lookup_page_cgroup(page);
3336 if (likely(pc) && PageCgroupUsed(pc))
3337 return pc;
3338 return NULL;
3339}
3340
3341bool mem_cgroup_bad_page_check(struct page *page)
3342{
3343 if (mem_cgroup_disabled())
3344 return false;
3345
3346 return lookup_page_cgroup_used(page) != NULL;
3347}
3348
3349void mem_cgroup_print_bad_page(struct page *page)
3350{
3351 struct page_cgroup *pc;
3352
3353 pc = lookup_page_cgroup_used(page);
3354 if (pc) {
3355 int ret = -1;
3356 char *path;
3357
3358 printk(KERN_ALERT "pc:%p pc->flags:%lx pc->mem_cgroup:%p",
3359 pc, pc->flags, pc->mem_cgroup);
3360
3361 path = kmalloc(PATH_MAX, GFP_KERNEL);
3362 if (path) {
3363 rcu_read_lock();
3364 ret = cgroup_path(pc->mem_cgroup->css.cgroup,
3365 path, PATH_MAX);
3366 rcu_read_unlock();
3367 }
3368
3369 printk(KERN_CONT "(%s)\n",
3370 (ret < 0) ? "cannot get the path" : path);
3371 kfree(path);
3372 }
3373}
3374#endif
3375
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003376static DEFINE_MUTEX(set_limit_mutex);
3377
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08003378static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003379 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003380{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003381 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003382 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003383 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003384 int children = mem_cgroup_count_children(memcg);
3385 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003386 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003387
3388 /*
3389 * For keeping hierarchical_reclaim simple, how long we should retry
3390 * is depends on callers. We set our retry-count to be function
3391 * of # of children which we should visit in this loop.
3392 */
3393 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
3394
3395 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003396
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003397 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003398 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003399 if (signal_pending(current)) {
3400 ret = -EINTR;
3401 break;
3402 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003403 /*
3404 * Rather than hide all in some function, I do this in
3405 * open coded manner. You see what this really does.
3406 * We have to guarantee mem->res.limit < mem->memsw.limit.
3407 */
3408 mutex_lock(&set_limit_mutex);
3409 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3410 if (memswlimit < val) {
3411 ret = -EINVAL;
3412 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003413 break;
3414 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003415
3416 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3417 if (memlimit < val)
3418 enlarge = 1;
3419
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003420 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003421 if (!ret) {
3422 if (memswlimit == val)
3423 memcg->memsw_is_minimum = true;
3424 else
3425 memcg->memsw_is_minimum = false;
3426 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003427 mutex_unlock(&set_limit_mutex);
3428
3429 if (!ret)
3430 break;
3431
Bob Liuaa20d482009-12-15 16:47:14 -08003432 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Ying Han0ae5e892011-05-26 16:25:25 -07003433 MEM_CGROUP_RECLAIM_SHRINK,
3434 NULL);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003435 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
3436 /* Usage is reduced ? */
3437 if (curusage >= oldusage)
3438 retry_count--;
3439 else
3440 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003441 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003442 if (!ret && enlarge)
3443 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08003444
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003445 return ret;
3446}
3447
Li Zefan338c8432009-06-17 16:27:15 -07003448static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
3449 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003450{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003451 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003452 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003453 int children = mem_cgroup_count_children(memcg);
3454 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003455 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003456
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003457 /* see mem_cgroup_resize_res_limit */
3458 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
3459 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003460 while (retry_count) {
3461 if (signal_pending(current)) {
3462 ret = -EINTR;
3463 break;
3464 }
3465 /*
3466 * Rather than hide all in some function, I do this in
3467 * open coded manner. You see what this really does.
3468 * We have to guarantee mem->res.limit < mem->memsw.limit.
3469 */
3470 mutex_lock(&set_limit_mutex);
3471 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3472 if (memlimit > val) {
3473 ret = -EINVAL;
3474 mutex_unlock(&set_limit_mutex);
3475 break;
3476 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003477 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3478 if (memswlimit < val)
3479 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003480 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003481 if (!ret) {
3482 if (memlimit == val)
3483 memcg->memsw_is_minimum = true;
3484 else
3485 memcg->memsw_is_minimum = false;
3486 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003487 mutex_unlock(&set_limit_mutex);
3488
3489 if (!ret)
3490 break;
3491
Balbir Singh4e416952009-09-23 15:56:39 -07003492 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh75822b42009-09-23 15:56:38 -07003493 MEM_CGROUP_RECLAIM_NOSWAP |
Ying Han0ae5e892011-05-26 16:25:25 -07003494 MEM_CGROUP_RECLAIM_SHRINK,
3495 NULL);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003496 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003497 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003498 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003499 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003500 else
3501 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003502 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003503 if (!ret && enlarge)
3504 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003505 return ret;
3506}
3507
Balbir Singh4e416952009-09-23 15:56:39 -07003508unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
Ying Han0ae5e892011-05-26 16:25:25 -07003509 gfp_t gfp_mask,
3510 unsigned long *total_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07003511{
3512 unsigned long nr_reclaimed = 0;
3513 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
3514 unsigned long reclaimed;
3515 int loop = 0;
3516 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003517 unsigned long long excess;
Ying Han0ae5e892011-05-26 16:25:25 -07003518 unsigned long nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003519
3520 if (order > 0)
3521 return 0;
3522
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07003523 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
Balbir Singh4e416952009-09-23 15:56:39 -07003524 /*
3525 * This loop can run a while, specially if mem_cgroup's continuously
3526 * keep exceeding their soft limit and putting the system under
3527 * pressure
3528 */
3529 do {
3530 if (next_mz)
3531 mz = next_mz;
3532 else
3533 mz = mem_cgroup_largest_soft_limit_node(mctz);
3534 if (!mz)
3535 break;
3536
Ying Han0ae5e892011-05-26 16:25:25 -07003537 nr_scanned = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07003538 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
3539 gfp_mask,
Ying Han0ae5e892011-05-26 16:25:25 -07003540 MEM_CGROUP_RECLAIM_SOFT,
3541 &nr_scanned);
Balbir Singh4e416952009-09-23 15:56:39 -07003542 nr_reclaimed += reclaimed;
Ying Han0ae5e892011-05-26 16:25:25 -07003543 *total_scanned += nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003544 spin_lock(&mctz->lock);
3545
3546 /*
3547 * If we failed to reclaim anything from this memory cgroup
3548 * it is time to move on to the next cgroup
3549 */
3550 next_mz = NULL;
3551 if (!reclaimed) {
3552 do {
3553 /*
3554 * Loop until we find yet another one.
3555 *
3556 * By the time we get the soft_limit lock
3557 * again, someone might have aded the
3558 * group back on the RB tree. Iterate to
3559 * make sure we get a different mem.
3560 * mem_cgroup_largest_soft_limit_node returns
3561 * NULL if no other cgroup is present on
3562 * the tree
3563 */
3564 next_mz =
3565 __mem_cgroup_largest_soft_limit_node(mctz);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003566 if (next_mz == mz)
Balbir Singh4e416952009-09-23 15:56:39 -07003567 css_put(&next_mz->mem->css);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003568 else /* next_mz == NULL or other memcg */
Balbir Singh4e416952009-09-23 15:56:39 -07003569 break;
3570 } while (1);
3571 }
Balbir Singh4e416952009-09-23 15:56:39 -07003572 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003573 excess = res_counter_soft_limit_excess(&mz->mem->res);
Balbir Singh4e416952009-09-23 15:56:39 -07003574 /*
3575 * One school of thought says that we should not add
3576 * back the node to the tree if reclaim returns 0.
3577 * But our reclaim could return 0, simply because due
3578 * to priority we are exposing a smaller subset of
3579 * memory to reclaim from. Consider this as a longer
3580 * term TODO.
3581 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003582 /* If excess == 0, no tree ops */
3583 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07003584 spin_unlock(&mctz->lock);
3585 css_put(&mz->mem->css);
3586 loop++;
3587 /*
3588 * Could not reclaim anything and there are no more
3589 * mem cgroups to try or we seem to be looping without
3590 * reclaiming anything.
3591 */
3592 if (!nr_reclaimed &&
3593 (next_mz == NULL ||
3594 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3595 break;
3596 } while (!nr_reclaimed);
3597 if (next_mz)
3598 css_put(&next_mz->mem->css);
3599 return nr_reclaimed;
3600}
3601
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003602/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003603 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003604 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
3605 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003606static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003607 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003608{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003609 struct zone *zone;
3610 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003611 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003612 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003613 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003614 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003615
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003616 zone = &NODE_DATA(node)->node_zones[zid];
3617 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07003618 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003619
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003620 loop = MEM_CGROUP_ZSTAT(mz, lru);
3621 /* give some margin against EBUSY etc...*/
3622 loop += 256;
3623 busy = NULL;
3624 while (loop--) {
Johannes Weiner5564e882011-03-23 16:42:29 -07003625 struct page *page;
3626
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003627 ret = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003628 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003629 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003630 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003631 break;
3632 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003633 pc = list_entry(list->prev, struct page_cgroup, lru);
3634 if (busy == pc) {
3635 list_move(&pc->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08003636 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003637 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003638 continue;
3639 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003640 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003641
Johannes Weiner6b3ae582011-03-23 16:42:30 -07003642 page = lookup_cgroup_page(pc);
Johannes Weiner5564e882011-03-23 16:42:29 -07003643
3644 ret = mem_cgroup_move_parent(page, pc, mem, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003645 if (ret == -ENOMEM)
3646 break;
3647
3648 if (ret == -EBUSY || ret == -EINVAL) {
3649 /* found lock contention or "pc" is obsolete. */
3650 busy = pc;
3651 cond_resched();
3652 } else
3653 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003654 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003655
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003656 if (!ret && !list_empty(list))
3657 return -EBUSY;
3658 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003659}
3660
3661/*
3662 * make mem_cgroup's charge to be 0 if there is no task.
3663 * This enables deleting this mem_cgroup.
3664 */
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003665static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003666{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003667 int ret;
3668 int node, zid, shrink;
3669 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003670 struct cgroup *cgrp = mem->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003671
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003672 css_get(&mem->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003673
3674 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003675 /* should free all ? */
3676 if (free_all)
3677 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003678move_account:
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003679 do {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003680 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003681 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003682 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003683 ret = -EINTR;
3684 if (signal_pending(current))
3685 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003686 /* This is for making all *used* pages to be on LRU. */
3687 lru_add_drain_all();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08003688 drain_all_stock_sync();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003689 ret = 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07003690 mem_cgroup_start_move(mem);
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08003691 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003692 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07003693 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003694 for_each_lru(l) {
3695 ret = mem_cgroup_force_empty_list(mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003696 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003697 if (ret)
3698 break;
3699 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003700 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003701 if (ret)
3702 break;
3703 }
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07003704 mem_cgroup_end_move(mem);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003705 memcg_oom_recover(mem);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003706 /* it seems parent cgroup doesn't have enough mem */
3707 if (ret == -ENOMEM)
3708 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003709 cond_resched();
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003710 /* "ret" should also be checked to ensure all lists are empty. */
3711 } while (mem->res.usage > 0 || ret);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003712out:
3713 css_put(&mem->css);
3714 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003715
3716try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003717 /* returns EBUSY if there is a task or if we come here twice. */
3718 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003719 ret = -EBUSY;
3720 goto out;
3721 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003722 /* we call try-to-free pages for make this cgroup empty */
3723 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003724 /* try to free all pages in this cgroup */
3725 shrink = 1;
3726 while (nr_retries && mem->res.usage > 0) {
3727 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003728
3729 if (signal_pending(current)) {
3730 ret = -EINTR;
3731 goto out;
3732 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003733 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
3734 false, get_swappiness(mem));
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003735 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003736 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003737 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02003738 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003739 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003740
3741 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003742 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003743 /* try move_account...there may be some *locked* pages. */
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003744 goto move_account;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003745}
3746
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003747int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
3748{
3749 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
3750}
3751
3752
Balbir Singh18f59ea2009-01-07 18:08:07 -08003753static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
3754{
3755 return mem_cgroup_from_cont(cont)->use_hierarchy;
3756}
3757
3758static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
3759 u64 val)
3760{
3761 int retval = 0;
3762 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
3763 struct cgroup *parent = cont->parent;
3764 struct mem_cgroup *parent_mem = NULL;
3765
3766 if (parent)
3767 parent_mem = mem_cgroup_from_cont(parent);
3768
3769 cgroup_lock();
3770 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003771 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08003772 * in the child subtrees. If it is unset, then the change can
3773 * occur, provided the current cgroup has no children.
3774 *
3775 * For the root cgroup, parent_mem is NULL, we allow value to be
3776 * set if there are no children.
3777 */
3778 if ((!parent_mem || !parent_mem->use_hierarchy) &&
3779 (val == 1 || val == 0)) {
3780 if (list_empty(&cont->children))
3781 mem->use_hierarchy = val;
3782 else
3783 retval = -EBUSY;
3784 } else
3785 retval = -EINVAL;
3786 cgroup_unlock();
3787
3788 return retval;
3789}
3790
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003791
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003792static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *mem,
3793 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003794{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003795 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003796 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003797
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003798 /* Per-cpu values can be negative, use a signed accumulator */
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003799 for_each_mem_cgroup_tree(iter, mem)
3800 val += mem_cgroup_read_stat(iter, idx);
3801
3802 if (val < 0) /* race ? */
3803 val = 0;
3804 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003805}
3806
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003807static inline u64 mem_cgroup_usage(struct mem_cgroup *mem, bool swap)
3808{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003809 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003810
3811 if (!mem_cgroup_is_root(mem)) {
3812 if (!swap)
3813 return res_counter_read_u64(&mem->res, RES_USAGE);
3814 else
3815 return res_counter_read_u64(&mem->memsw, RES_USAGE);
3816 }
3817
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003818 val = mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_CACHE);
3819 val += mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003820
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003821 if (swap)
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003822 val += mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_SWAPOUT);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003823
3824 return val << PAGE_SHIFT;
3825}
3826
Paul Menage2c3daa72008-04-29 00:59:58 -07003827static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003828{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003829 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003830 u64 val;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003831 int type, name;
3832
3833 type = MEMFILE_TYPE(cft->private);
3834 name = MEMFILE_ATTR(cft->private);
3835 switch (type) {
3836 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003837 if (name == RES_USAGE)
3838 val = mem_cgroup_usage(mem, false);
3839 else
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003840 val = res_counter_read_u64(&mem->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003841 break;
3842 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003843 if (name == RES_USAGE)
3844 val = mem_cgroup_usage(mem, true);
3845 else
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003846 val = res_counter_read_u64(&mem->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003847 break;
3848 default:
3849 BUG();
3850 break;
3851 }
3852 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003853}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003854/*
3855 * The user of this function is...
3856 * RES_LIMIT.
3857 */
Paul Menage856c13a2008-07-25 01:47:04 -07003858static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
3859 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003860{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003861 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003862 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003863 unsigned long long val;
3864 int ret;
3865
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003866 type = MEMFILE_TYPE(cft->private);
3867 name = MEMFILE_ATTR(cft->private);
3868 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003869 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07003870 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
3871 ret = -EINVAL;
3872 break;
3873 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003874 /* This function does all necessary parse...reuse it */
3875 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003876 if (ret)
3877 break;
3878 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003879 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003880 else
3881 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003882 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07003883 case RES_SOFT_LIMIT:
3884 ret = res_counter_memparse_write_strategy(buffer, &val);
3885 if (ret)
3886 break;
3887 /*
3888 * For memsw, soft limits are hard to implement in terms
3889 * of semantics, for now, we support soft limits for
3890 * control without swap
3891 */
3892 if (type == _MEM)
3893 ret = res_counter_set_soft_limit(&memcg->res, val);
3894 else
3895 ret = -EINVAL;
3896 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003897 default:
3898 ret = -EINVAL; /* should be BUG() ? */
3899 break;
3900 }
3901 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003902}
3903
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08003904static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
3905 unsigned long long *mem_limit, unsigned long long *memsw_limit)
3906{
3907 struct cgroup *cgroup;
3908 unsigned long long min_limit, min_memsw_limit, tmp;
3909
3910 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3911 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3912 cgroup = memcg->css.cgroup;
3913 if (!memcg->use_hierarchy)
3914 goto out;
3915
3916 while (cgroup->parent) {
3917 cgroup = cgroup->parent;
3918 memcg = mem_cgroup_from_cont(cgroup);
3919 if (!memcg->use_hierarchy)
3920 break;
3921 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
3922 min_limit = min(min_limit, tmp);
3923 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3924 min_memsw_limit = min(min_memsw_limit, tmp);
3925 }
3926out:
3927 *mem_limit = min_limit;
3928 *memsw_limit = min_memsw_limit;
3929 return;
3930}
3931
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003932static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003933{
3934 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003935 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003936
3937 mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003938 type = MEMFILE_TYPE(event);
3939 name = MEMFILE_ATTR(event);
3940 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003941 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003942 if (type == _MEM)
3943 res_counter_reset_max(&mem->res);
3944 else
3945 res_counter_reset_max(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003946 break;
3947 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003948 if (type == _MEM)
3949 res_counter_reset_failcnt(&mem->res);
3950 else
3951 res_counter_reset_failcnt(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003952 break;
3953 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07003954
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07003955 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003956}
3957
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003958static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
3959 struct cftype *cft)
3960{
3961 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
3962}
3963
Daisuke Nishimura02491442010-03-10 15:22:17 -08003964#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003965static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
3966 struct cftype *cft, u64 val)
3967{
3968 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3969
3970 if (val >= (1 << NR_MOVE_TYPE))
3971 return -EINVAL;
3972 /*
3973 * We check this value several times in both in can_attach() and
3974 * attach(), so we need cgroup lock to prevent this value from being
3975 * inconsistent.
3976 */
3977 cgroup_lock();
3978 mem->move_charge_at_immigrate = val;
3979 cgroup_unlock();
3980
3981 return 0;
3982}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003983#else
3984static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
3985 struct cftype *cft, u64 val)
3986{
3987 return -ENOSYS;
3988}
3989#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003990
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003991
3992/* For read statistics */
3993enum {
3994 MCS_CACHE,
3995 MCS_RSS,
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08003996 MCS_FILE_MAPPED,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003997 MCS_PGPGIN,
3998 MCS_PGPGOUT,
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003999 MCS_SWAP,
Ying Han456f9982011-05-26 16:25:38 -07004000 MCS_PGFAULT,
4001 MCS_PGMAJFAULT,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004002 MCS_INACTIVE_ANON,
4003 MCS_ACTIVE_ANON,
4004 MCS_INACTIVE_FILE,
4005 MCS_ACTIVE_FILE,
4006 MCS_UNEVICTABLE,
4007 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004008};
4009
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004010struct mcs_total_stat {
4011 s64 stat[NR_MCS_STAT];
4012};
4013
4014struct {
4015 char *local_name;
4016 char *total_name;
4017} memcg_stat_strings[NR_MCS_STAT] = {
4018 {"cache", "total_cache"},
4019 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07004020 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004021 {"pgpgin", "total_pgpgin"},
4022 {"pgpgout", "total_pgpgout"},
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004023 {"swap", "total_swap"},
Ying Han456f9982011-05-26 16:25:38 -07004024 {"pgfault", "total_pgfault"},
4025 {"pgmajfault", "total_pgmajfault"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004026 {"inactive_anon", "total_inactive_anon"},
4027 {"active_anon", "total_active_anon"},
4028 {"inactive_file", "total_inactive_file"},
4029 {"active_file", "total_active_file"},
4030 {"unevictable", "total_unevictable"}
4031};
4032
4033
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004034static void
4035mem_cgroup_get_local_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004036{
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004037 s64 val;
4038
4039 /* per cpu stat */
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004040 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004041 s->stat[MCS_CACHE] += val * PAGE_SIZE;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004042 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004043 s->stat[MCS_RSS] += val * PAGE_SIZE;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004044 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_FILE_MAPPED);
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08004045 s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
Johannes Weinere9f89742011-03-23 16:42:37 -07004046 val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGPGIN);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004047 s->stat[MCS_PGPGIN] += val;
Johannes Weinere9f89742011-03-23 16:42:37 -07004048 val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGPGOUT);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004049 s->stat[MCS_PGPGOUT] += val;
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004050 if (do_swap_account) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004051 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_SWAPOUT);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004052 s->stat[MCS_SWAP] += val * PAGE_SIZE;
4053 }
Ying Han456f9982011-05-26 16:25:38 -07004054 val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGFAULT);
4055 s->stat[MCS_PGFAULT] += val;
4056 val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGMAJFAULT);
4057 s->stat[MCS_PGMAJFAULT] += val;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004058
4059 /* per zone stat */
4060 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
4061 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
4062 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
4063 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
4064 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
4065 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
4066 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
4067 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
4068 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
4069 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004070}
4071
4072static void
4073mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
4074{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004075 struct mem_cgroup *iter;
4076
4077 for_each_mem_cgroup_tree(iter, mem)
4078 mem_cgroup_get_local_stat(iter, s);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004079}
4080
Ying Han406eb0c2011-05-26 16:25:37 -07004081#ifdef CONFIG_NUMA
4082static int mem_control_numa_stat_show(struct seq_file *m, void *arg)
4083{
4084 int nid;
4085 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
4086 unsigned long node_nr;
4087 struct cgroup *cont = m->private;
4088 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
4089
4090 total_nr = mem_cgroup_nr_lru_pages(mem_cont);
4091 seq_printf(m, "total=%lu", total_nr);
4092 for_each_node_state(nid, N_HIGH_MEMORY) {
4093 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid);
4094 seq_printf(m, " N%d=%lu", nid, node_nr);
4095 }
4096 seq_putc(m, '\n');
4097
4098 file_nr = mem_cgroup_nr_file_lru_pages(mem_cont);
4099 seq_printf(m, "file=%lu", file_nr);
4100 for_each_node_state(nid, N_HIGH_MEMORY) {
4101 node_nr = mem_cgroup_node_nr_file_lru_pages(mem_cont, nid);
4102 seq_printf(m, " N%d=%lu", nid, node_nr);
4103 }
4104 seq_putc(m, '\n');
4105
4106 anon_nr = mem_cgroup_nr_anon_lru_pages(mem_cont);
4107 seq_printf(m, "anon=%lu", anon_nr);
4108 for_each_node_state(nid, N_HIGH_MEMORY) {
4109 node_nr = mem_cgroup_node_nr_anon_lru_pages(mem_cont, nid);
4110 seq_printf(m, " N%d=%lu", nid, node_nr);
4111 }
4112 seq_putc(m, '\n');
4113
4114 unevictable_nr = mem_cgroup_nr_unevictable_lru_pages(mem_cont);
4115 seq_printf(m, "unevictable=%lu", unevictable_nr);
4116 for_each_node_state(nid, N_HIGH_MEMORY) {
4117 node_nr = mem_cgroup_node_nr_unevictable_lru_pages(mem_cont,
4118 nid);
4119 seq_printf(m, " N%d=%lu", nid, node_nr);
4120 }
4121 seq_putc(m, '\n');
4122 return 0;
4123}
4124#endif /* CONFIG_NUMA */
4125
Paul Menagec64745c2008-04-29 01:00:02 -07004126static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
4127 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004128{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004129 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004130 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004131 int i;
4132
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004133 memset(&mystat, 0, sizeof(mystat));
4134 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004135
Ying Han406eb0c2011-05-26 16:25:37 -07004136
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004137 for (i = 0; i < NR_MCS_STAT; i++) {
4138 if (i == MCS_SWAP && !do_swap_account)
4139 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004140 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004141 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004142
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004143 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004144 {
4145 unsigned long long limit, memsw_limit;
4146 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
4147 cb->fill(cb, "hierarchical_memory_limit", limit);
4148 if (do_swap_account)
4149 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
4150 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004151
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004152 memset(&mystat, 0, sizeof(mystat));
4153 mem_cgroup_get_total_stat(mem_cont, &mystat);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004154 for (i = 0; i < NR_MCS_STAT; i++) {
4155 if (i == MCS_SWAP && !do_swap_account)
4156 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004157 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004158 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004159
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004160#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08004161 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004162
4163 {
4164 int nid, zid;
4165 struct mem_cgroup_per_zone *mz;
4166 unsigned long recent_rotated[2] = {0, 0};
4167 unsigned long recent_scanned[2] = {0, 0};
4168
4169 for_each_online_node(nid)
4170 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
4171 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
4172
4173 recent_rotated[0] +=
4174 mz->reclaim_stat.recent_rotated[0];
4175 recent_rotated[1] +=
4176 mz->reclaim_stat.recent_rotated[1];
4177 recent_scanned[0] +=
4178 mz->reclaim_stat.recent_scanned[0];
4179 recent_scanned[1] +=
4180 mz->reclaim_stat.recent_scanned[1];
4181 }
4182 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
4183 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
4184 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
4185 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
4186 }
4187#endif
4188
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004189 return 0;
4190}
4191
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004192static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
4193{
4194 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4195
4196 return get_swappiness(memcg);
4197}
4198
4199static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
4200 u64 val)
4201{
4202 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4203 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08004204
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004205 if (val > 100)
4206 return -EINVAL;
4207
4208 if (cgrp->parent == NULL)
4209 return -EINVAL;
4210
4211 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08004212
4213 cgroup_lock();
4214
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004215 /* If under hierarchy, only empty-root can set this value */
4216 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08004217 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
4218 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004219 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08004220 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004221
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004222 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004223
Li Zefan068b38c2009-01-15 13:51:26 -08004224 cgroup_unlock();
4225
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004226 return 0;
4227}
4228
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004229static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
4230{
4231 struct mem_cgroup_threshold_ary *t;
4232 u64 usage;
4233 int i;
4234
4235 rcu_read_lock();
4236 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004237 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004238 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004239 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004240
4241 if (!t)
4242 goto unlock;
4243
4244 usage = mem_cgroup_usage(memcg, swap);
4245
4246 /*
4247 * current_threshold points to threshold just below usage.
4248 * If it's not true, a threshold was crossed after last
4249 * call of __mem_cgroup_threshold().
4250 */
Phil Carmody5407a562010-05-26 14:42:42 -07004251 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004252
4253 /*
4254 * Iterate backward over array of thresholds starting from
4255 * current_threshold and check if a threshold is crossed.
4256 * If none of thresholds below usage is crossed, we read
4257 * only one element of the array here.
4258 */
4259 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
4260 eventfd_signal(t->entries[i].eventfd, 1);
4261
4262 /* i = current_threshold + 1 */
4263 i++;
4264
4265 /*
4266 * Iterate forward over array of thresholds starting from
4267 * current_threshold+1 and check if a threshold is crossed.
4268 * If none of thresholds above usage is crossed, we read
4269 * only one element of the array here.
4270 */
4271 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
4272 eventfd_signal(t->entries[i].eventfd, 1);
4273
4274 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07004275 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004276unlock:
4277 rcu_read_unlock();
4278}
4279
4280static void mem_cgroup_threshold(struct mem_cgroup *memcg)
4281{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07004282 while (memcg) {
4283 __mem_cgroup_threshold(memcg, false);
4284 if (do_swap_account)
4285 __mem_cgroup_threshold(memcg, true);
4286
4287 memcg = parent_mem_cgroup(memcg);
4288 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004289}
4290
4291static int compare_thresholds(const void *a, const void *b)
4292{
4293 const struct mem_cgroup_threshold *_a = a;
4294 const struct mem_cgroup_threshold *_b = b;
4295
4296 return _a->threshold - _b->threshold;
4297}
4298
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004299static int mem_cgroup_oom_notify_cb(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004300{
4301 struct mem_cgroup_eventfd_list *ev;
4302
4303 list_for_each_entry(ev, &mem->oom_notify, list)
4304 eventfd_signal(ev->eventfd, 1);
4305 return 0;
4306}
4307
4308static void mem_cgroup_oom_notify(struct mem_cgroup *mem)
4309{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004310 struct mem_cgroup *iter;
4311
4312 for_each_mem_cgroup_tree(iter, mem)
4313 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004314}
4315
4316static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
4317 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004318{
4319 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004320 struct mem_cgroup_thresholds *thresholds;
4321 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004322 int type = MEMFILE_TYPE(cft->private);
4323 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004324 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004325
4326 ret = res_counter_memparse_write_strategy(args, &threshold);
4327 if (ret)
4328 return ret;
4329
4330 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004331
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004332 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004333 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004334 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004335 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004336 else
4337 BUG();
4338
4339 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4340
4341 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004342 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004343 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4344
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004345 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004346
4347 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004348 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004349 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004350 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004351 ret = -ENOMEM;
4352 goto unlock;
4353 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004354 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004355
4356 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004357 if (thresholds->primary) {
4358 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004359 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004360 }
4361
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004362 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004363 new->entries[size - 1].eventfd = eventfd;
4364 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004365
4366 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004367 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004368 compare_thresholds, NULL);
4369
4370 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004371 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004372 for (i = 0; i < size; i++) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004373 if (new->entries[i].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004374 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004375 * new->current_threshold will not be used until
4376 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004377 * it here.
4378 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004379 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004380 }
4381 }
4382
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004383 /* Free old spare buffer and save old primary buffer as spare */
4384 kfree(thresholds->spare);
4385 thresholds->spare = thresholds->primary;
4386
4387 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004388
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004389 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004390 synchronize_rcu();
4391
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004392unlock:
4393 mutex_unlock(&memcg->thresholds_lock);
4394
4395 return ret;
4396}
4397
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004398static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004399 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004400{
4401 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004402 struct mem_cgroup_thresholds *thresholds;
4403 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004404 int type = MEMFILE_TYPE(cft->private);
4405 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004406 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004407
4408 mutex_lock(&memcg->thresholds_lock);
4409 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004410 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004411 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004412 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004413 else
4414 BUG();
4415
4416 /*
4417 * Something went wrong if we trying to unregister a threshold
4418 * if we don't have thresholds
4419 */
4420 BUG_ON(!thresholds);
4421
4422 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4423
4424 /* Check if a threshold crossed before removing */
4425 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4426
4427 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004428 size = 0;
4429 for (i = 0; i < thresholds->primary->size; i++) {
4430 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004431 size++;
4432 }
4433
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004434 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004435
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004436 /* Set thresholds array to NULL if we don't have thresholds */
4437 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004438 kfree(new);
4439 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004440 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004441 }
4442
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004443 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004444
4445 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004446 new->current_threshold = -1;
4447 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
4448 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004449 continue;
4450
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004451 new->entries[j] = thresholds->primary->entries[i];
4452 if (new->entries[j].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004453 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004454 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004455 * until rcu_assign_pointer(), so it's safe to increment
4456 * it here.
4457 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004458 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004459 }
4460 j++;
4461 }
4462
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004463swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004464 /* Swap primary and spare array */
4465 thresholds->spare = thresholds->primary;
4466 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004467
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004468 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004469 synchronize_rcu();
4470
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004471 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004472}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004473
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004474static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
4475 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
4476{
4477 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4478 struct mem_cgroup_eventfd_list *event;
4479 int type = MEMFILE_TYPE(cft->private);
4480
4481 BUG_ON(type != _OOM_TYPE);
4482 event = kmalloc(sizeof(*event), GFP_KERNEL);
4483 if (!event)
4484 return -ENOMEM;
4485
4486 mutex_lock(&memcg_oom_mutex);
4487
4488 event->eventfd = eventfd;
4489 list_add(&event->list, &memcg->oom_notify);
4490
4491 /* already in OOM ? */
4492 if (atomic_read(&memcg->oom_lock))
4493 eventfd_signal(eventfd, 1);
4494 mutex_unlock(&memcg_oom_mutex);
4495
4496 return 0;
4497}
4498
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004499static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004500 struct cftype *cft, struct eventfd_ctx *eventfd)
4501{
4502 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
4503 struct mem_cgroup_eventfd_list *ev, *tmp;
4504 int type = MEMFILE_TYPE(cft->private);
4505
4506 BUG_ON(type != _OOM_TYPE);
4507
4508 mutex_lock(&memcg_oom_mutex);
4509
4510 list_for_each_entry_safe(ev, tmp, &mem->oom_notify, list) {
4511 if (ev->eventfd == eventfd) {
4512 list_del(&ev->list);
4513 kfree(ev);
4514 }
4515 }
4516
4517 mutex_unlock(&memcg_oom_mutex);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004518}
4519
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004520static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
4521 struct cftype *cft, struct cgroup_map_cb *cb)
4522{
4523 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
4524
4525 cb->fill(cb, "oom_kill_disable", mem->oom_kill_disable);
4526
4527 if (atomic_read(&mem->oom_lock))
4528 cb->fill(cb, "under_oom", 1);
4529 else
4530 cb->fill(cb, "under_oom", 0);
4531 return 0;
4532}
4533
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004534static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
4535 struct cftype *cft, u64 val)
4536{
4537 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
4538 struct mem_cgroup *parent;
4539
4540 /* cannot set to root cgroup and only 0 and 1 are allowed */
4541 if (!cgrp->parent || !((val == 0) || (val == 1)))
4542 return -EINVAL;
4543
4544 parent = mem_cgroup_from_cont(cgrp->parent);
4545
4546 cgroup_lock();
4547 /* oom-kill-disable is a flag for subhierarchy. */
4548 if ((parent->use_hierarchy) ||
4549 (mem->use_hierarchy && !list_empty(&cgrp->children))) {
4550 cgroup_unlock();
4551 return -EINVAL;
4552 }
4553 mem->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07004554 if (!val)
4555 memcg_oom_recover(mem);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004556 cgroup_unlock();
4557 return 0;
4558}
4559
Ying Han406eb0c2011-05-26 16:25:37 -07004560#ifdef CONFIG_NUMA
4561static const struct file_operations mem_control_numa_stat_file_operations = {
4562 .read = seq_read,
4563 .llseek = seq_lseek,
4564 .release = single_release,
4565};
4566
4567static int mem_control_numa_stat_open(struct inode *unused, struct file *file)
4568{
4569 struct cgroup *cont = file->f_dentry->d_parent->d_fsdata;
4570
4571 file->f_op = &mem_control_numa_stat_file_operations;
4572 return single_open(file, mem_control_numa_stat_show, cont);
4573}
4574#endif /* CONFIG_NUMA */
4575
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004576static struct cftype mem_cgroup_files[] = {
4577 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004578 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004579 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07004580 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004581 .register_event = mem_cgroup_usage_register_event,
4582 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004583 },
4584 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004585 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004586 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004587 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004588 .read_u64 = mem_cgroup_read,
4589 },
4590 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004591 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004592 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07004593 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07004594 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004595 },
4596 {
Balbir Singh296c81d2009-09-23 15:56:36 -07004597 .name = "soft_limit_in_bytes",
4598 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
4599 .write_string = mem_cgroup_write,
4600 .read_u64 = mem_cgroup_read,
4601 },
4602 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004603 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004604 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004605 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07004606 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004607 },
Balbir Singh8697d332008-02-07 00:13:59 -08004608 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004609 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07004610 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004611 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004612 {
4613 .name = "force_empty",
4614 .trigger = mem_cgroup_force_empty_write,
4615 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08004616 {
4617 .name = "use_hierarchy",
4618 .write_u64 = mem_cgroup_hierarchy_write,
4619 .read_u64 = mem_cgroup_hierarchy_read,
4620 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004621 {
4622 .name = "swappiness",
4623 .read_u64 = mem_cgroup_swappiness_read,
4624 .write_u64 = mem_cgroup_swappiness_write,
4625 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004626 {
4627 .name = "move_charge_at_immigrate",
4628 .read_u64 = mem_cgroup_move_charge_read,
4629 .write_u64 = mem_cgroup_move_charge_write,
4630 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004631 {
4632 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004633 .read_map = mem_cgroup_oom_control_read,
4634 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004635 .register_event = mem_cgroup_oom_register_event,
4636 .unregister_event = mem_cgroup_oom_unregister_event,
4637 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
4638 },
Ying Han406eb0c2011-05-26 16:25:37 -07004639#ifdef CONFIG_NUMA
4640 {
4641 .name = "numa_stat",
4642 .open = mem_control_numa_stat_open,
4643 },
4644#endif
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004645};
4646
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004647#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4648static struct cftype memsw_cgroup_files[] = {
4649 {
4650 .name = "memsw.usage_in_bytes",
4651 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
4652 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004653 .register_event = mem_cgroup_usage_register_event,
4654 .unregister_event = mem_cgroup_usage_unregister_event,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004655 },
4656 {
4657 .name = "memsw.max_usage_in_bytes",
4658 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
4659 .trigger = mem_cgroup_reset,
4660 .read_u64 = mem_cgroup_read,
4661 },
4662 {
4663 .name = "memsw.limit_in_bytes",
4664 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
4665 .write_string = mem_cgroup_write,
4666 .read_u64 = mem_cgroup_read,
4667 },
4668 {
4669 .name = "memsw.failcnt",
4670 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
4671 .trigger = mem_cgroup_reset,
4672 .read_u64 = mem_cgroup_read,
4673 },
4674};
4675
4676static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
4677{
4678 if (!do_swap_account)
4679 return 0;
4680 return cgroup_add_files(cont, ss, memsw_cgroup_files,
4681 ARRAY_SIZE(memsw_cgroup_files));
4682};
4683#else
4684static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
4685{
4686 return 0;
4687}
4688#endif
4689
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004690static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
4691{
4692 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004693 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07004694 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004695 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004696 /*
4697 * This routine is called against possible nodes.
4698 * But it's BUG to call kmalloc() against offline node.
4699 *
4700 * TODO: this routine can waste much memory for nodes which will
4701 * never be onlined. It's better to use memory hotplug callback
4702 * function.
4703 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004704 if (!node_state(node, N_NORMAL_MEMORY))
4705 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08004706 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004707 if (!pn)
4708 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004709
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004710 mem->info.nodeinfo[node] = pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004711 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4712 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07004713 for_each_lru(l)
4714 INIT_LIST_HEAD(&mz->lists[l]);
Balbir Singhf64c3f52009-09-23 15:56:37 -07004715 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07004716 mz->on_tree = false;
4717 mz->mem = mem;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004718 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004719 return 0;
4720}
4721
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004722static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
4723{
4724 kfree(mem->info.nodeinfo[node]);
4725}
4726
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004727static struct mem_cgroup *mem_cgroup_alloc(void)
4728{
4729 struct mem_cgroup *mem;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004730 int size = sizeof(struct mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004731
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004732 /* Can be very big if MAX_NUMNODES is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08004733 if (size < PAGE_SIZE)
Jesper Juhl17295c82011-01-13 15:47:42 -08004734 mem = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004735 else
Jesper Juhl17295c82011-01-13 15:47:42 -08004736 mem = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004737
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07004738 if (!mem)
4739 return NULL;
4740
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004741 mem->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004742 if (!mem->stat)
4743 goto out_free;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07004744 spin_lock_init(&mem->pcp_counter_lock);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004745 return mem;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004746
4747out_free:
4748 if (size < PAGE_SIZE)
4749 kfree(mem);
4750 else
4751 vfree(mem);
4752 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004753}
4754
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004755/*
4756 * At destroying mem_cgroup, references from swap_cgroup can remain.
4757 * (scanning all at force_empty is too costly...)
4758 *
4759 * Instead of clearing all references at force_empty, we remember
4760 * the number of reference from swap_cgroup and free mem_cgroup when
4761 * it goes down to 0.
4762 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004763 * Removal of cgroup itself succeeds regardless of refs from swap.
4764 */
4765
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004766static void __mem_cgroup_free(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004767{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004768 int node;
4769
Balbir Singhf64c3f52009-09-23 15:56:37 -07004770 mem_cgroup_remove_from_trees(mem);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004771 free_css_id(&mem_cgroup_subsys, &mem->css);
4772
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004773 for_each_node_state(node, N_POSSIBLE)
4774 free_mem_cgroup_per_zone_info(mem, node);
4775
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004776 free_percpu(mem->stat);
4777 if (sizeof(struct mem_cgroup) < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004778 kfree(mem);
4779 else
4780 vfree(mem);
4781}
4782
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004783static void mem_cgroup_get(struct mem_cgroup *mem)
4784{
4785 atomic_inc(&mem->refcnt);
4786}
4787
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004788static void __mem_cgroup_put(struct mem_cgroup *mem, int count)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004789{
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004790 if (atomic_sub_and_test(count, &mem->refcnt)) {
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004791 struct mem_cgroup *parent = parent_mem_cgroup(mem);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004792 __mem_cgroup_free(mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004793 if (parent)
4794 mem_cgroup_put(parent);
4795 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004796}
4797
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004798static void mem_cgroup_put(struct mem_cgroup *mem)
4799{
4800 __mem_cgroup_put(mem, 1);
4801}
4802
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004803/*
4804 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
4805 */
4806static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
4807{
4808 if (!mem->res.parent)
4809 return NULL;
4810 return mem_cgroup_from_res_counter(mem->res.parent, res);
4811}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004812
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004813#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4814static void __init enable_swap_cgroup(void)
4815{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004816 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004817 do_swap_account = 1;
4818}
4819#else
4820static void __init enable_swap_cgroup(void)
4821{
4822}
4823#endif
4824
Balbir Singhf64c3f52009-09-23 15:56:37 -07004825static int mem_cgroup_soft_limit_tree_init(void)
4826{
4827 struct mem_cgroup_tree_per_node *rtpn;
4828 struct mem_cgroup_tree_per_zone *rtpz;
4829 int tmp, node, zone;
4830
4831 for_each_node_state(node, N_POSSIBLE) {
4832 tmp = node;
4833 if (!node_state(node, N_NORMAL_MEMORY))
4834 tmp = -1;
4835 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
4836 if (!rtpn)
4837 return 1;
4838
4839 soft_limit_tree.rb_tree_per_node[node] = rtpn;
4840
4841 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4842 rtpz = &rtpn->rb_tree_per_zone[zone];
4843 rtpz->rb_root = RB_ROOT;
4844 spin_lock_init(&rtpz->lock);
4845 }
4846 }
4847 return 0;
4848}
4849
Li Zefan0eb253e2009-01-15 13:51:25 -08004850static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004851mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
4852{
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004853 struct mem_cgroup *mem, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004854 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004855 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004856
Jan Blunckc8dad2b2009-01-07 18:07:53 -08004857 mem = mem_cgroup_alloc();
4858 if (!mem)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004859 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004860
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004861 for_each_node_state(node, N_POSSIBLE)
4862 if (alloc_mem_cgroup_per_zone_info(mem, node))
4863 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004864
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004865 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004866 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004867 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004868 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004869 parent = NULL;
Balbir Singh4b3bde42009-09-23 15:56:32 -07004870 root_mem_cgroup = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004871 if (mem_cgroup_soft_limit_tree_init())
4872 goto free_out;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004873 for_each_possible_cpu(cpu) {
4874 struct memcg_stock_pcp *stock =
4875 &per_cpu(memcg_stock, cpu);
4876 INIT_WORK(&stock->work, drain_local_stock);
4877 }
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07004878 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004879 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004880 parent = mem_cgroup_from_cont(cont->parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004881 mem->use_hierarchy = parent->use_hierarchy;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004882 mem->oom_kill_disable = parent->oom_kill_disable;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004883 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004884
Balbir Singh18f59ea2009-01-07 18:08:07 -08004885 if (parent && parent->use_hierarchy) {
4886 res_counter_init(&mem->res, &parent->res);
4887 res_counter_init(&mem->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004888 /*
4889 * We increment refcnt of the parent to ensure that we can
4890 * safely access it on res_counter_charge/uncharge.
4891 * This refcnt will be decremented when freeing this
4892 * mem_cgroup(see mem_cgroup_put).
4893 */
4894 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004895 } else {
4896 res_counter_init(&mem->res, NULL);
4897 res_counter_init(&mem->memsw, NULL);
4898 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004899 mem->last_scanned_child = 0;
Ying Han889976d2011-05-26 16:25:33 -07004900 mem->last_scanned_node = MAX_NUMNODES;
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004901 INIT_LIST_HEAD(&mem->oom_notify);
Balbir Singh6d61ef42009-01-07 18:08:06 -08004902
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004903 if (parent)
4904 mem->swappiness = get_swappiness(parent);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004905 atomic_set(&mem->refcnt, 1);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004906 mem->move_charge_at_immigrate = 0;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004907 mutex_init(&mem->thresholds_lock);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004908 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004909free_out:
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004910 __mem_cgroup_free(mem);
Balbir Singh4b3bde42009-09-23 15:56:32 -07004911 root_mem_cgroup = NULL;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004912 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004913}
4914
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004915static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004916 struct cgroup *cont)
4917{
4918 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004919
4920 return mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004921}
4922
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004923static void mem_cgroup_destroy(struct cgroup_subsys *ss,
4924 struct cgroup *cont)
4925{
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004926 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004927
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004928 mem_cgroup_put(mem);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004929}
4930
4931static int mem_cgroup_populate(struct cgroup_subsys *ss,
4932 struct cgroup *cont)
4933{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004934 int ret;
4935
4936 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
4937 ARRAY_SIZE(mem_cgroup_files));
4938
4939 if (!ret)
4940 ret = register_memsw_files(cont, ss);
4941 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004942}
4943
Daisuke Nishimura02491442010-03-10 15:22:17 -08004944#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004945/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004946#define PRECHARGE_COUNT_AT_ONCE 256
4947static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004948{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004949 int ret = 0;
4950 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004951 struct mem_cgroup *mem = mc.to;
4952
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004953 if (mem_cgroup_is_root(mem)) {
4954 mc.precharge += count;
4955 /* we don't need css_get for root */
4956 return ret;
4957 }
4958 /* try to charge at once */
4959 if (count > 1) {
4960 struct res_counter *dummy;
4961 /*
4962 * "mem" cannot be under rmdir() because we've already checked
4963 * by cgroup_lock_live_cgroup() that it is not removed and we
4964 * are still under the same cgroup_mutex. So we can postpone
4965 * css_get().
4966 */
4967 if (res_counter_charge(&mem->res, PAGE_SIZE * count, &dummy))
4968 goto one_by_one;
4969 if (do_swap_account && res_counter_charge(&mem->memsw,
4970 PAGE_SIZE * count, &dummy)) {
4971 res_counter_uncharge(&mem->res, PAGE_SIZE * count);
4972 goto one_by_one;
4973 }
4974 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004975 return ret;
4976 }
4977one_by_one:
4978 /* fall back to one by one charge */
4979 while (count--) {
4980 if (signal_pending(current)) {
4981 ret = -EINTR;
4982 break;
4983 }
4984 if (!batch_count--) {
4985 batch_count = PRECHARGE_COUNT_AT_ONCE;
4986 cond_resched();
4987 }
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004988 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, 1, &mem, false);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004989 if (ret || !mem)
4990 /* mem_cgroup_clear_mc() will do uncharge later */
4991 return -ENOMEM;
4992 mc.precharge++;
4993 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004994 return ret;
4995}
4996
4997/**
4998 * is_target_pte_for_mc - check a pte whether it is valid for move charge
4999 * @vma: the vma the pte to be checked belongs
5000 * @addr: the address corresponding to the pte to be checked
5001 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08005002 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005003 *
5004 * Returns
5005 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
5006 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
5007 * move charge. if @target is not NULL, the page is stored in target->page
5008 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08005009 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
5010 * target for charge migration. if @target is not NULL, the entry is stored
5011 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005012 *
5013 * Called with pte lock held.
5014 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005015union mc_target {
5016 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005017 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005018};
5019
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005020enum mc_target_type {
5021 MC_TARGET_NONE, /* not used */
5022 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08005023 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005024};
5025
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005026static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
5027 unsigned long addr, pte_t ptent)
5028{
5029 struct page *page = vm_normal_page(vma, addr, ptent);
5030
5031 if (!page || !page_mapped(page))
5032 return NULL;
5033 if (PageAnon(page)) {
5034 /* we don't move shared anon */
5035 if (!move_anon() || page_mapcount(page) > 2)
5036 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005037 } else if (!move_file())
5038 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005039 return NULL;
5040 if (!get_page_unless_zero(page))
5041 return NULL;
5042
5043 return page;
5044}
5045
5046static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5047 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5048{
5049 int usage_count;
5050 struct page *page = NULL;
5051 swp_entry_t ent = pte_to_swp_entry(ptent);
5052
5053 if (!move_anon() || non_swap_entry(ent))
5054 return NULL;
5055 usage_count = mem_cgroup_count_swap_user(ent, &page);
5056 if (usage_count > 1) { /* we don't move shared anon */
5057 if (page)
5058 put_page(page);
5059 return NULL;
5060 }
5061 if (do_swap_account)
5062 entry->val = ent.val;
5063
5064 return page;
5065}
5066
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005067static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
5068 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5069{
5070 struct page *page = NULL;
5071 struct inode *inode;
5072 struct address_space *mapping;
5073 pgoff_t pgoff;
5074
5075 if (!vma->vm_file) /* anonymous vma */
5076 return NULL;
5077 if (!move_file())
5078 return NULL;
5079
5080 inode = vma->vm_file->f_path.dentry->d_inode;
5081 mapping = vma->vm_file->f_mapping;
5082 if (pte_none(ptent))
5083 pgoff = linear_page_index(vma, addr);
5084 else /* pte_file(ptent) is true */
5085 pgoff = pte_to_pgoff(ptent);
5086
5087 /* page is moved even if it's not RSS of this task(page-faulted). */
5088 if (!mapping_cap_swap_backed(mapping)) { /* normal file */
5089 page = find_get_page(mapping, pgoff);
5090 } else { /* shmem/tmpfs file. we should take account of swap too. */
5091 swp_entry_t ent;
5092 mem_cgroup_get_shmem_target(inode, pgoff, &page, &ent);
5093 if (do_swap_account)
5094 entry->val = ent.val;
5095 }
5096
5097 return page;
5098}
5099
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005100static int is_target_pte_for_mc(struct vm_area_struct *vma,
5101 unsigned long addr, pte_t ptent, union mc_target *target)
5102{
Daisuke Nishimura02491442010-03-10 15:22:17 -08005103 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005104 struct page_cgroup *pc;
5105 int ret = 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005106 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005107
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005108 if (pte_present(ptent))
5109 page = mc_handle_present_pte(vma, addr, ptent);
5110 else if (is_swap_pte(ptent))
5111 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005112 else if (pte_none(ptent) || pte_file(ptent))
5113 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005114
5115 if (!page && !ent.val)
Daisuke Nishimura02491442010-03-10 15:22:17 -08005116 return 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005117 if (page) {
5118 pc = lookup_page_cgroup(page);
5119 /*
5120 * Do only loose check w/o page_cgroup lock.
5121 * mem_cgroup_move_account() checks the pc is valid or not under
5122 * the lock.
5123 */
5124 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
5125 ret = MC_TARGET_PAGE;
5126 if (target)
5127 target->page = page;
5128 }
5129 if (!ret || !target)
5130 put_page(page);
5131 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005132 /* There is a swap entry and a page doesn't exist or isn't charged */
5133 if (ent.val && !ret &&
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005134 css_id(&mc.from->css) == lookup_swap_cgroup(ent)) {
5135 ret = MC_TARGET_SWAP;
5136 if (target)
5137 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005138 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005139 return ret;
5140}
5141
5142static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
5143 unsigned long addr, unsigned long end,
5144 struct mm_walk *walk)
5145{
5146 struct vm_area_struct *vma = walk->private;
5147 pte_t *pte;
5148 spinlock_t *ptl;
5149
Dave Hansen03319322011-03-22 16:32:56 -07005150 split_huge_page_pmd(walk->mm, pmd);
5151
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005152 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5153 for (; addr != end; pte++, addr += PAGE_SIZE)
5154 if (is_target_pte_for_mc(vma, addr, *pte, NULL))
5155 mc.precharge++; /* increment precharge temporarily */
5156 pte_unmap_unlock(pte - 1, ptl);
5157 cond_resched();
5158
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005159 return 0;
5160}
5161
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005162static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
5163{
5164 unsigned long precharge;
5165 struct vm_area_struct *vma;
5166
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005167 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005168 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5169 struct mm_walk mem_cgroup_count_precharge_walk = {
5170 .pmd_entry = mem_cgroup_count_precharge_pte_range,
5171 .mm = mm,
5172 .private = vma,
5173 };
5174 if (is_vm_hugetlb_page(vma))
5175 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005176 walk_page_range(vma->vm_start, vma->vm_end,
5177 &mem_cgroup_count_precharge_walk);
5178 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005179 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005180
5181 precharge = mc.precharge;
5182 mc.precharge = 0;
5183
5184 return precharge;
5185}
5186
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005187static int mem_cgroup_precharge_mc(struct mm_struct *mm)
5188{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005189 unsigned long precharge = mem_cgroup_count_precharge(mm);
5190
5191 VM_BUG_ON(mc.moving_task);
5192 mc.moving_task = current;
5193 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005194}
5195
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005196/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
5197static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005198{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005199 struct mem_cgroup *from = mc.from;
5200 struct mem_cgroup *to = mc.to;
5201
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005202 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005203 if (mc.precharge) {
5204 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
5205 mc.precharge = 0;
5206 }
5207 /*
5208 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
5209 * we must uncharge here.
5210 */
5211 if (mc.moved_charge) {
5212 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
5213 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005214 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005215 /* we must fixup refcnts and charges */
5216 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005217 /* uncharge swap account from the old cgroup */
5218 if (!mem_cgroup_is_root(mc.from))
5219 res_counter_uncharge(&mc.from->memsw,
5220 PAGE_SIZE * mc.moved_swap);
5221 __mem_cgroup_put(mc.from, mc.moved_swap);
5222
5223 if (!mem_cgroup_is_root(mc.to)) {
5224 /*
5225 * we charged both to->res and to->memsw, so we should
5226 * uncharge to->res.
5227 */
5228 res_counter_uncharge(&mc.to->res,
5229 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005230 }
5231 /* we've already done mem_cgroup_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005232 mc.moved_swap = 0;
5233 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005234 memcg_oom_recover(from);
5235 memcg_oom_recover(to);
5236 wake_up_all(&mc.waitq);
5237}
5238
5239static void mem_cgroup_clear_mc(void)
5240{
5241 struct mem_cgroup *from = mc.from;
5242
5243 /*
5244 * we must clear moving_task before waking up waiters at the end of
5245 * task migration.
5246 */
5247 mc.moving_task = NULL;
5248 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005249 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005250 mc.from = NULL;
5251 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005252 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005253 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005254}
5255
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005256static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
5257 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005258 struct task_struct *p)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005259{
5260 int ret = 0;
5261 struct mem_cgroup *mem = mem_cgroup_from_cont(cgroup);
5262
5263 if (mem->move_charge_at_immigrate) {
5264 struct mm_struct *mm;
5265 struct mem_cgroup *from = mem_cgroup_from_task(p);
5266
5267 VM_BUG_ON(from == mem);
5268
5269 mm = get_task_mm(p);
5270 if (!mm)
5271 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005272 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005273 if (mm->owner == p) {
5274 VM_BUG_ON(mc.from);
5275 VM_BUG_ON(mc.to);
5276 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005277 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005278 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005279 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005280 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005281 mc.from = from;
5282 mc.to = mem;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005283 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005284 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005285
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005286 ret = mem_cgroup_precharge_mc(mm);
5287 if (ret)
5288 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005289 }
5290 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005291 }
5292 return ret;
5293}
5294
5295static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
5296 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005297 struct task_struct *p)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005298{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005299 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005300}
5301
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005302static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
5303 unsigned long addr, unsigned long end,
5304 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005305{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005306 int ret = 0;
5307 struct vm_area_struct *vma = walk->private;
5308 pte_t *pte;
5309 spinlock_t *ptl;
5310
Dave Hansen03319322011-03-22 16:32:56 -07005311 split_huge_page_pmd(walk->mm, pmd);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005312retry:
5313 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5314 for (; addr != end; addr += PAGE_SIZE) {
5315 pte_t ptent = *(pte++);
5316 union mc_target target;
5317 int type;
5318 struct page *page;
5319 struct page_cgroup *pc;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005320 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005321
5322 if (!mc.precharge)
5323 break;
5324
5325 type = is_target_pte_for_mc(vma, addr, ptent, &target);
5326 switch (type) {
5327 case MC_TARGET_PAGE:
5328 page = target.page;
5329 if (isolate_lru_page(page))
5330 goto put;
5331 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07005332 if (!mem_cgroup_move_account(page, 1, pc,
5333 mc.from, mc.to, false)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005334 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005335 /* we uncharge from mc.from later. */
5336 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005337 }
5338 putback_lru_page(page);
5339put: /* is_target_pte_for_mc() gets the page */
5340 put_page(page);
5341 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005342 case MC_TARGET_SWAP:
5343 ent = target.ent;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005344 if (!mem_cgroup_move_swap_account(ent,
5345 mc.from, mc.to, false)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08005346 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005347 /* we fixup refcnts and charges later. */
5348 mc.moved_swap++;
5349 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08005350 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005351 default:
5352 break;
5353 }
5354 }
5355 pte_unmap_unlock(pte - 1, ptl);
5356 cond_resched();
5357
5358 if (addr != end) {
5359 /*
5360 * We have consumed all precharges we got in can_attach().
5361 * We try charge one by one, but don't do any additional
5362 * charges to mc.to if we have failed in charge once in attach()
5363 * phase.
5364 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005365 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005366 if (!ret)
5367 goto retry;
5368 }
5369
5370 return ret;
5371}
5372
5373static void mem_cgroup_move_charge(struct mm_struct *mm)
5374{
5375 struct vm_area_struct *vma;
5376
5377 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005378retry:
5379 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
5380 /*
5381 * Someone who are holding the mmap_sem might be waiting in
5382 * waitq. So we cancel all extra charges, wake up all waiters,
5383 * and retry. Because we cancel precharges, we might not be able
5384 * to move enough charges, but moving charge is a best-effort
5385 * feature anyway, so it wouldn't be a big problem.
5386 */
5387 __mem_cgroup_clear_mc();
5388 cond_resched();
5389 goto retry;
5390 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005391 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5392 int ret;
5393 struct mm_walk mem_cgroup_move_charge_walk = {
5394 .pmd_entry = mem_cgroup_move_charge_pte_range,
5395 .mm = mm,
5396 .private = vma,
5397 };
5398 if (is_vm_hugetlb_page(vma))
5399 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005400 ret = walk_page_range(vma->vm_start, vma->vm_end,
5401 &mem_cgroup_move_charge_walk);
5402 if (ret)
5403 /*
5404 * means we have consumed all precharges and failed in
5405 * doing additional charge. Just abandon here.
5406 */
5407 break;
5408 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005409 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005410}
5411
Balbir Singh67e465a2008-02-07 00:13:54 -08005412static void mem_cgroup_move_task(struct cgroup_subsys *ss,
5413 struct cgroup *cont,
5414 struct cgroup *old_cont,
Ben Blumf780bdb2011-05-26 16:25:19 -07005415 struct task_struct *p)
Balbir Singh67e465a2008-02-07 00:13:54 -08005416{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005417 struct mm_struct *mm;
5418
5419 if (!mc.to)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005420 /* no need to move charge */
5421 return;
5422
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005423 mm = get_task_mm(p);
5424 if (mm) {
5425 mem_cgroup_move_charge(mm);
5426 mmput(mm);
5427 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005428 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08005429}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005430#else /* !CONFIG_MMU */
5431static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
5432 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005433 struct task_struct *p)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005434{
5435 return 0;
5436}
5437static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
5438 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005439 struct task_struct *p)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005440{
5441}
5442static 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)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005446{
5447}
5448#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08005449
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005450struct cgroup_subsys mem_cgroup_subsys = {
5451 .name = "memory",
5452 .subsys_id = mem_cgroup_subsys_id,
5453 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005454 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005455 .destroy = mem_cgroup_destroy,
5456 .populate = mem_cgroup_populate,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005457 .can_attach = mem_cgroup_can_attach,
5458 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08005459 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005460 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005461 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005462};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005463
5464#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08005465static int __init enable_swap_account(char *s)
5466{
5467 /* consider enabled if no parameter or 1 is given */
Michal Hockoa2c89902011-05-24 17:12:50 -07005468 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08005469 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07005470 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08005471 really_do_swap_account = 0;
5472 return 1;
5473}
Michal Hockoa2c89902011-05-24 17:12:50 -07005474__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005475
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005476#endif