blob: 930de943727117043c0274f35cbe6e70d5eb8160 [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,
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700110 MEM_CGROUP_TARGET_NUMAINFO,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700111 MEM_CGROUP_NTARGETS,
112};
113#define THRESHOLDS_EVENTS_TARGET (128)
114#define SOFTLIMIT_EVENTS_TARGET (1024)
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700115#define NUMAINFO_EVENTS_TARGET (1024)
Johannes Weinere9f89742011-03-23 16:42:37 -0700116
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800117struct mem_cgroup_stat_cpu {
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700118 long count[MEM_CGROUP_STAT_NSTATS];
Johannes Weinere9f89742011-03-23 16:42:37 -0700119 unsigned long events[MEM_CGROUP_EVENTS_NSTATS];
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700120 unsigned long targets[MEM_CGROUP_NTARGETS];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800121};
122
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800123/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800124 * per-zone information in memory controller.
125 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800126struct mem_cgroup_per_zone {
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800127 /*
128 * spin_lock to protect the per cgroup LRU
129 */
Christoph Lameterb69408e2008-10-18 20:26:14 -0700130 struct list_head lists[NR_LRU_LISTS];
131 unsigned long count[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800132
133 struct zone_reclaim_stat reclaim_stat;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700134 struct rb_node tree_node; /* RB tree node */
135 unsigned long long usage_in_excess;/* Set to the value by which */
136 /* the soft limit is exceeded*/
137 bool on_tree;
Balbir Singh4e416952009-09-23 15:56:39 -0700138 struct mem_cgroup *mem; /* Back pointer, we cannot */
139 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800140};
141/* Macro for accessing counter */
142#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
143
144struct mem_cgroup_per_node {
145 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
146};
147
148struct mem_cgroup_lru_info {
149 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
150};
151
152/*
Balbir Singhf64c3f52009-09-23 15:56:37 -0700153 * Cgroups above their limits are maintained in a RB-Tree, independent of
154 * their hierarchy representation
155 */
156
157struct mem_cgroup_tree_per_zone {
158 struct rb_root rb_root;
159 spinlock_t lock;
160};
161
162struct mem_cgroup_tree_per_node {
163 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
164};
165
166struct mem_cgroup_tree {
167 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
168};
169
170static struct mem_cgroup_tree soft_limit_tree __read_mostly;
171
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800172struct mem_cgroup_threshold {
173 struct eventfd_ctx *eventfd;
174 u64 threshold;
175};
176
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700177/* For threshold */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800178struct mem_cgroup_threshold_ary {
179 /* An array index points to threshold just below usage. */
Phil Carmody5407a562010-05-26 14:42:42 -0700180 int current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800181 /* Size of entries[] */
182 unsigned int size;
183 /* Array of thresholds */
184 struct mem_cgroup_threshold entries[0];
185};
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700186
187struct mem_cgroup_thresholds {
188 /* Primary thresholds array */
189 struct mem_cgroup_threshold_ary *primary;
190 /*
191 * Spare threshold array.
192 * This is needed to make mem_cgroup_unregister_event() "never fail".
193 * It must be able to store at least primary->size - 1 entries.
194 */
195 struct mem_cgroup_threshold_ary *spare;
196};
197
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700198/* for OOM */
199struct mem_cgroup_eventfd_list {
200 struct list_head list;
201 struct eventfd_ctx *eventfd;
202};
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800203
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800204static void mem_cgroup_threshold(struct mem_cgroup *mem);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700205static void mem_cgroup_oom_notify(struct mem_cgroup *mem);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800206
KAMEZAWA Hiroyuki82f9d482011-07-26 16:08:26 -0700207enum {
208 SCAN_BY_LIMIT,
209 SCAN_BY_SYSTEM,
210 NR_SCAN_CONTEXT,
211 SCAN_BY_SHRINK, /* not recorded now */
212};
213
214enum {
215 SCAN,
216 SCAN_ANON,
217 SCAN_FILE,
218 ROTATE,
219 ROTATE_ANON,
220 ROTATE_FILE,
221 FREED,
222 FREED_ANON,
223 FREED_FILE,
224 ELAPSED,
225 NR_SCANSTATS,
226};
227
228struct scanstat {
229 spinlock_t lock;
230 unsigned long stats[NR_SCAN_CONTEXT][NR_SCANSTATS];
231 unsigned long rootstats[NR_SCAN_CONTEXT][NR_SCANSTATS];
232};
233
234const char *scanstat_string[NR_SCANSTATS] = {
235 "scanned_pages",
236 "scanned_anon_pages",
237 "scanned_file_pages",
238 "rotated_pages",
239 "rotated_anon_pages",
240 "rotated_file_pages",
241 "freed_pages",
242 "freed_anon_pages",
243 "freed_file_pages",
244 "elapsed_ns",
245};
246#define SCANSTAT_WORD_LIMIT "_by_limit"
247#define SCANSTAT_WORD_SYSTEM "_by_system"
248#define SCANSTAT_WORD_HIERARCHY "_under_hierarchy"
249
250
Balbir Singhf64c3f52009-09-23 15:56:37 -0700251/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800252 * The memory controller data structure. The memory controller controls both
253 * page cache and RSS per cgroup. We would eventually like to provide
254 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
255 * to help the administrator determine what knobs to tune.
256 *
257 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800258 * we hit the water mark. May be even add a low water mark, such that
259 * no reclaim occurs from a cgroup at it's low water mark, this is
260 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800261 */
262struct mem_cgroup {
263 struct cgroup_subsys_state css;
264 /*
265 * the counter to account for memory usage
266 */
267 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800268 /*
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800269 * the counter to account for mem+swap usage.
270 */
271 struct res_counter memsw;
272 /*
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800273 * Per cgroup active and inactive list, similar to the
274 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800275 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800276 struct mem_cgroup_lru_info info;
Balbir Singh6d61ef42009-01-07 18:08:06 -0800277 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200278 * While reclaiming in a hierarchy, we cache the last child we
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700279 * reclaimed from.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800280 */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700281 int last_scanned_child;
Ying Han889976d2011-05-26 16:25:33 -0700282 int last_scanned_node;
283#if MAX_NUMNODES > 1
284 nodemask_t scan_nodes;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700285 atomic_t numainfo_events;
286 atomic_t numainfo_updating;
Ying Han889976d2011-05-26 16:25:33 -0700287#endif
Balbir Singh18f59ea2009-01-07 18:08:07 -0800288 /*
289 * Should the accounting and control be hierarchical, per subtree?
290 */
291 bool use_hierarchy;
Michal Hocko79dfdac2011-07-26 16:08:23 -0700292
293 bool oom_lock;
294 atomic_t under_oom;
295
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800296 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800297
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -0700298 int swappiness;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -0700299 /* OOM-Killer disable */
300 int oom_kill_disable;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800301
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700302 /* set when res.limit == memsw.limit */
303 bool memsw_is_minimum;
304
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800305 /* protect arrays of thresholds */
306 struct mutex thresholds_lock;
307
308 /* thresholds for memory usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700309 struct mem_cgroup_thresholds thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700310
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800311 /* thresholds for mem+swap usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700312 struct mem_cgroup_thresholds memsw_thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700313
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700314 /* For oom notifier event fd */
315 struct list_head oom_notify;
KAMEZAWA Hiroyuki82f9d482011-07-26 16:08:26 -0700316 /* For recording LRU-scan statistics */
317 struct scanstat scanstat;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800318 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800319 * Should we move charges of a task when a task is moved into this
320 * mem_cgroup ? And what type of charges should we move ?
321 */
322 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800323 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800324 * percpu counter.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800325 */
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800326 struct mem_cgroup_stat_cpu *stat;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700327 /*
328 * used when a cpu is offlined or other synchronizations
329 * See mem_cgroup_read_stat().
330 */
331 struct mem_cgroup_stat_cpu nocpu_base;
332 spinlock_t pcp_counter_lock;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800333};
334
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800335/* Stuffs for move charges at task migration. */
336/*
337 * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a
338 * left-shifted bitmap of these types.
339 */
340enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800341 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700342 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800343 NR_MOVE_TYPE,
344};
345
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800346/* "mc" and its members are protected by cgroup_mutex */
347static struct move_charge_struct {
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -0800348 spinlock_t lock; /* for from, to */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800349 struct mem_cgroup *from;
350 struct mem_cgroup *to;
351 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800352 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800353 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800354 struct task_struct *moving_task; /* a task moving charges */
355 wait_queue_head_t waitq; /* a waitq for other context */
356} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700357 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800358 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
359};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800360
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700361static bool move_anon(void)
362{
363 return test_bit(MOVE_CHARGE_TYPE_ANON,
364 &mc.to->move_charge_at_immigrate);
365}
366
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700367static bool move_file(void)
368{
369 return test_bit(MOVE_CHARGE_TYPE_FILE,
370 &mc.to->move_charge_at_immigrate);
371}
372
Balbir Singh4e416952009-09-23 15:56:39 -0700373/*
374 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
375 * limit reclaim to prevent infinite loops, if they ever occur.
376 */
377#define MEM_CGROUP_MAX_RECLAIM_LOOPS (100)
378#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2)
379
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800380enum charge_type {
381 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
382 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700383 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700384 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800385 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700386 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700387 NR_CHARGE_TYPE,
388};
389
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800390/* for encoding cft->private value on file */
391#define _MEM (0)
392#define _MEMSWAP (1)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700393#define _OOM_TYPE (2)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800394#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
395#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
396#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700397/* Used for OOM nofiier */
398#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800399
Balbir Singh75822b42009-09-23 15:56:38 -0700400/*
401 * Reclaim flags for mem_cgroup_hierarchical_reclaim
402 */
403#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
404#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
405#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
406#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
Balbir Singh4e416952009-09-23 15:56:39 -0700407#define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
408#define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
Balbir Singh75822b42009-09-23 15:56:38 -0700409
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800410static void mem_cgroup_get(struct mem_cgroup *mem);
411static void mem_cgroup_put(struct mem_cgroup *mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -0800412static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -0700413static void drain_all_stock_async(struct mem_cgroup *mem);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800414
Balbir Singhf64c3f52009-09-23 15:56:37 -0700415static struct mem_cgroup_per_zone *
416mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
417{
418 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
419}
420
Wu Fengguangd3242362009-12-16 12:19:59 +0100421struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *mem)
422{
423 return &mem->css;
424}
425
Balbir Singhf64c3f52009-09-23 15:56:37 -0700426static struct mem_cgroup_per_zone *
Johannes Weiner97a6c372011-03-23 16:42:27 -0700427page_cgroup_zoneinfo(struct mem_cgroup *mem, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700428{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700429 int nid = page_to_nid(page);
430 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700431
Balbir Singhf64c3f52009-09-23 15:56:37 -0700432 return mem_cgroup_zoneinfo(mem, nid, zid);
433}
434
435static struct mem_cgroup_tree_per_zone *
436soft_limit_tree_node_zone(int nid, int zid)
437{
438 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
439}
440
441static struct mem_cgroup_tree_per_zone *
442soft_limit_tree_from_page(struct page *page)
443{
444 int nid = page_to_nid(page);
445 int zid = page_zonenum(page);
446
447 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
448}
449
450static void
Balbir Singh4e416952009-09-23 15:56:39 -0700451__mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700452 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700453 struct mem_cgroup_tree_per_zone *mctz,
454 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700455{
456 struct rb_node **p = &mctz->rb_root.rb_node;
457 struct rb_node *parent = NULL;
458 struct mem_cgroup_per_zone *mz_node;
459
460 if (mz->on_tree)
461 return;
462
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700463 mz->usage_in_excess = new_usage_in_excess;
464 if (!mz->usage_in_excess)
465 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700466 while (*p) {
467 parent = *p;
468 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
469 tree_node);
470 if (mz->usage_in_excess < mz_node->usage_in_excess)
471 p = &(*p)->rb_left;
472 /*
473 * We can't avoid mem cgroups that are over their soft
474 * limit by the same amount
475 */
476 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
477 p = &(*p)->rb_right;
478 }
479 rb_link_node(&mz->tree_node, parent, p);
480 rb_insert_color(&mz->tree_node, &mctz->rb_root);
481 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700482}
483
484static void
485__mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
486 struct mem_cgroup_per_zone *mz,
487 struct mem_cgroup_tree_per_zone *mctz)
488{
489 if (!mz->on_tree)
490 return;
491 rb_erase(&mz->tree_node, &mctz->rb_root);
492 mz->on_tree = false;
493}
494
495static void
Balbir Singhf64c3f52009-09-23 15:56:37 -0700496mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
497 struct mem_cgroup_per_zone *mz,
498 struct mem_cgroup_tree_per_zone *mctz)
499{
500 spin_lock(&mctz->lock);
Balbir Singh4e416952009-09-23 15:56:39 -0700501 __mem_cgroup_remove_exceeded(mem, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700502 spin_unlock(&mctz->lock);
503}
504
Balbir Singhf64c3f52009-09-23 15:56:37 -0700505
506static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
507{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700508 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700509 struct mem_cgroup_per_zone *mz;
510 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700511 int nid = page_to_nid(page);
512 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700513 mctz = soft_limit_tree_from_page(page);
514
515 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700516 * Necessary to update all ancestors when hierarchy is used.
517 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700518 */
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700519 for (; mem; mem = parent_mem_cgroup(mem)) {
520 mz = mem_cgroup_zoneinfo(mem, nid, zid);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700521 excess = res_counter_soft_limit_excess(&mem->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700522 /*
523 * We have to update the tree if mz is on RB-tree or
524 * mem is over its softlimit.
525 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700526 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700527 spin_lock(&mctz->lock);
528 /* if on-tree, remove it */
529 if (mz->on_tree)
530 __mem_cgroup_remove_exceeded(mem, mz, mctz);
531 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700532 * Insert again. mz->usage_in_excess will be updated.
533 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700534 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700535 __mem_cgroup_insert_exceeded(mem, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700536 spin_unlock(&mctz->lock);
537 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700538 }
539}
540
541static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
542{
543 int node, zone;
544 struct mem_cgroup_per_zone *mz;
545 struct mem_cgroup_tree_per_zone *mctz;
546
547 for_each_node_state(node, N_POSSIBLE) {
548 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
549 mz = mem_cgroup_zoneinfo(mem, node, zone);
550 mctz = soft_limit_tree_node_zone(node, zone);
551 mem_cgroup_remove_exceeded(mem, mz, mctz);
552 }
553 }
554}
555
Balbir Singh4e416952009-09-23 15:56:39 -0700556static struct mem_cgroup_per_zone *
557__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
558{
559 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700560 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700561
562retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700563 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700564 rightmost = rb_last(&mctz->rb_root);
565 if (!rightmost)
566 goto done; /* Nothing to reclaim from */
567
568 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
569 /*
570 * Remove the node now but someone else can add it back,
571 * we will to add it back at the end of reclaim to its correct
572 * position in the tree.
573 */
574 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
575 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
576 !css_tryget(&mz->mem->css))
577 goto retry;
578done:
579 return mz;
580}
581
582static struct mem_cgroup_per_zone *
583mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
584{
585 struct mem_cgroup_per_zone *mz;
586
587 spin_lock(&mctz->lock);
588 mz = __mem_cgroup_largest_soft_limit_node(mctz);
589 spin_unlock(&mctz->lock);
590 return mz;
591}
592
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700593/*
594 * Implementation Note: reading percpu statistics for memcg.
595 *
596 * Both of vmstat[] and percpu_counter has threshold and do periodic
597 * synchronization to implement "quick" read. There are trade-off between
598 * reading cost and precision of value. Then, we may have a chance to implement
599 * a periodic synchronizion of counter in memcg's counter.
600 *
601 * But this _read() function is used for user interface now. The user accounts
602 * memory usage by memory cgroup and he _always_ requires exact value because
603 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
604 * have to visit all online cpus and make sum. So, for now, unnecessary
605 * synchronization is not implemented. (just implemented for cpu hotplug)
606 *
607 * If there are kernel internal actions which can make use of some not-exact
608 * value, and reading all cpu value can be performance bottleneck in some
609 * common workload, threashold and synchonization as vmstat[] should be
610 * implemented.
611 */
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700612static long mem_cgroup_read_stat(struct mem_cgroup *mem,
613 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800614{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700615 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800616 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800617
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700618 get_online_cpus();
619 for_each_online_cpu(cpu)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800620 val += per_cpu(mem->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700621#ifdef CONFIG_HOTPLUG_CPU
622 spin_lock(&mem->pcp_counter_lock);
623 val += mem->nocpu_base.count[idx];
624 spin_unlock(&mem->pcp_counter_lock);
625#endif
626 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800627 return val;
628}
629
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700630static void mem_cgroup_swap_statistics(struct mem_cgroup *mem,
631 bool charge)
632{
633 int val = (charge) ? 1 : -1;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800634 this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_SWAPOUT], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700635}
636
Ying Han456f9982011-05-26 16:25:38 -0700637void mem_cgroup_pgfault(struct mem_cgroup *mem, int val)
638{
639 this_cpu_add(mem->stat->events[MEM_CGROUP_EVENTS_PGFAULT], val);
640}
641
642void mem_cgroup_pgmajfault(struct mem_cgroup *mem, int val)
643{
644 this_cpu_add(mem->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT], val);
645}
646
Johannes Weinere9f89742011-03-23 16:42:37 -0700647static unsigned long mem_cgroup_read_events(struct mem_cgroup *mem,
648 enum mem_cgroup_events_index idx)
649{
650 unsigned long val = 0;
651 int cpu;
652
653 for_each_online_cpu(cpu)
654 val += per_cpu(mem->stat->events[idx], cpu);
655#ifdef CONFIG_HOTPLUG_CPU
656 spin_lock(&mem->pcp_counter_lock);
657 val += mem->nocpu_base.events[idx];
658 spin_unlock(&mem->pcp_counter_lock);
659#endif
660 return val;
661}
662
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700663static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
KAMEZAWA Hiroyukie401f1762011-01-20 14:44:23 -0800664 bool file, int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800665{
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800666 preempt_disable();
667
KAMEZAWA Hiroyukie401f1762011-01-20 14:44:23 -0800668 if (file)
669 __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_CACHE], nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800670 else
KAMEZAWA Hiroyukie401f1762011-01-20 14:44:23 -0800671 __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_RSS], nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700672
KAMEZAWA Hiroyukie401f1762011-01-20 14:44:23 -0800673 /* pagein of a big page is an event. So, ignore page size */
674 if (nr_pages > 0)
Johannes Weinere9f89742011-03-23 16:42:37 -0700675 __this_cpu_inc(mem->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800676 else {
Johannes Weinere9f89742011-03-23 16:42:37 -0700677 __this_cpu_inc(mem->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800678 nr_pages = -nr_pages; /* for event */
679 }
KAMEZAWA Hiroyukie401f1762011-01-20 14:44:23 -0800680
Johannes Weinere9f89742011-03-23 16:42:37 -0700681 __this_cpu_add(mem->stat->events[MEM_CGROUP_EVENTS_COUNT], nr_pages);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800682
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800683 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800684}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800685
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700686unsigned long
687mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *mem, int nid, int zid,
688 unsigned int lru_mask)
Ying Han889976d2011-05-26 16:25:33 -0700689{
690 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700691 enum lru_list l;
692 unsigned long ret = 0;
693
694 mz = mem_cgroup_zoneinfo(mem, nid, zid);
695
696 for_each_lru(l) {
697 if (BIT(l) & lru_mask)
698 ret += MEM_CGROUP_ZSTAT(mz, l);
699 }
700 return ret;
701}
702
703static unsigned long
704mem_cgroup_node_nr_lru_pages(struct mem_cgroup *mem,
705 int nid, unsigned int lru_mask)
706{
Ying Han889976d2011-05-26 16:25:33 -0700707 u64 total = 0;
708 int zid;
709
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700710 for (zid = 0; zid < MAX_NR_ZONES; zid++)
711 total += mem_cgroup_zone_nr_lru_pages(mem, nid, zid, lru_mask);
712
Ying Han889976d2011-05-26 16:25:33 -0700713 return total;
714}
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700715
716static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *mem,
717 unsigned int lru_mask)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800718{
Ying Han889976d2011-05-26 16:25:33 -0700719 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800720 u64 total = 0;
721
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700722 for_each_node_state(nid, N_HIGH_MEMORY)
723 total += mem_cgroup_node_nr_lru_pages(mem, nid, lru_mask);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800724 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800725}
726
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700727static bool __memcg_event_check(struct mem_cgroup *mem, int target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800728{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700729 unsigned long val, next;
730
731 val = this_cpu_read(mem->stat->events[MEM_CGROUP_EVENTS_COUNT]);
732 next = this_cpu_read(mem->stat->targets[target]);
733 /* from time_after() in jiffies.h */
734 return ((long)next - (long)val < 0);
735}
736
737static void __mem_cgroup_target_update(struct mem_cgroup *mem, int target)
738{
739 unsigned long val, next;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800740
Johannes Weinere9f89742011-03-23 16:42:37 -0700741 val = this_cpu_read(mem->stat->events[MEM_CGROUP_EVENTS_COUNT]);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800742
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700743 switch (target) {
744 case MEM_CGROUP_TARGET_THRESH:
745 next = val + THRESHOLDS_EVENTS_TARGET;
746 break;
747 case MEM_CGROUP_TARGET_SOFTLIMIT:
748 next = val + SOFTLIMIT_EVENTS_TARGET;
749 break;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700750 case MEM_CGROUP_TARGET_NUMAINFO:
751 next = val + NUMAINFO_EVENTS_TARGET;
752 break;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700753 default:
754 return;
755 }
756
757 this_cpu_write(mem->stat->targets[target], next);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800758}
759
760/*
761 * Check events in order.
762 *
763 */
764static void memcg_check_events(struct mem_cgroup *mem, struct page *page)
765{
766 /* threshold event is triggered in finer grain than soft limit */
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700767 if (unlikely(__memcg_event_check(mem, MEM_CGROUP_TARGET_THRESH))) {
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800768 mem_cgroup_threshold(mem);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700769 __mem_cgroup_target_update(mem, MEM_CGROUP_TARGET_THRESH);
770 if (unlikely(__memcg_event_check(mem,
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700771 MEM_CGROUP_TARGET_SOFTLIMIT))) {
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800772 mem_cgroup_update_tree(mem, page);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700773 __mem_cgroup_target_update(mem,
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700774 MEM_CGROUP_TARGET_SOFTLIMIT);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700775 }
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700776#if MAX_NUMNODES > 1
777 if (unlikely(__memcg_event_check(mem,
778 MEM_CGROUP_TARGET_NUMAINFO))) {
779 atomic_inc(&mem->numainfo_events);
780 __mem_cgroup_target_update(mem,
781 MEM_CGROUP_TARGET_NUMAINFO);
782 }
783#endif
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800784 }
785}
786
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800787static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800788{
789 return container_of(cgroup_subsys_state(cont,
790 mem_cgroup_subsys_id), struct mem_cgroup,
791 css);
792}
793
Balbir Singhcf475ad2008-04-29 01:00:16 -0700794struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800795{
Balbir Singh31a78f22008-09-28 23:09:31 +0100796 /*
797 * mm_update_next_owner() may clear mm->owner to NULL
798 * if it races with swapoff, page migration, etc.
799 * So this can be called with p == NULL.
800 */
801 if (unlikely(!p))
802 return NULL;
803
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800804 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
805 struct mem_cgroup, css);
806}
807
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700808struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800809{
810 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700811
812 if (!mm)
813 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800814 /*
815 * Because we have no locks, mm->owner's may be being moved to other
816 * cgroup. We use css_tryget() here even if this looks
817 * pessimistic (rather than adding locks here).
818 */
819 rcu_read_lock();
820 do {
821 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
822 if (unlikely(!mem))
823 break;
824 } while (!css_tryget(&mem->css));
825 rcu_read_unlock();
826 return mem;
827}
828
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700829/* The caller has to guarantee "mem" exists before calling this */
830static struct mem_cgroup *mem_cgroup_start_loop(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700831{
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700832 struct cgroup_subsys_state *css;
833 int found;
834
835 if (!mem) /* ROOT cgroup has the smallest ID */
836 return root_mem_cgroup; /*css_put/get against root is ignored*/
837 if (!mem->use_hierarchy) {
838 if (css_tryget(&mem->css))
839 return mem;
840 return NULL;
841 }
842 rcu_read_lock();
843 /*
844 * searching a memory cgroup which has the smallest ID under given
845 * ROOT cgroup. (ID >= 1)
846 */
847 css = css_get_next(&mem_cgroup_subsys, 1, &mem->css, &found);
848 if (css && css_tryget(css))
849 mem = container_of(css, struct mem_cgroup, css);
850 else
851 mem = NULL;
852 rcu_read_unlock();
853 return mem;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700854}
855
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700856static struct mem_cgroup *mem_cgroup_get_next(struct mem_cgroup *iter,
857 struct mem_cgroup *root,
858 bool cond)
859{
860 int nextid = css_id(&iter->css) + 1;
861 int found;
862 int hierarchy_used;
863 struct cgroup_subsys_state *css;
864
865 hierarchy_used = iter->use_hierarchy;
866
867 css_put(&iter->css);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700868 /* If no ROOT, walk all, ignore hierarchy */
869 if (!cond || (root && !hierarchy_used))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700870 return NULL;
871
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700872 if (!root)
873 root = root_mem_cgroup;
874
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700875 do {
876 iter = NULL;
877 rcu_read_lock();
878
879 css = css_get_next(&mem_cgroup_subsys, nextid,
880 &root->css, &found);
881 if (css && css_tryget(css))
882 iter = container_of(css, struct mem_cgroup, css);
883 rcu_read_unlock();
884 /* If css is NULL, no more cgroups will be found */
885 nextid = found + 1;
886 } while (css && !iter);
887
888 return iter;
889}
890/*
891 * for_eacn_mem_cgroup_tree() for visiting all cgroup under tree. Please
892 * be careful that "break" loop is not allowed. We have reference count.
893 * Instead of that modify "cond" to be false and "continue" to exit the loop.
894 */
895#define for_each_mem_cgroup_tree_cond(iter, root, cond) \
896 for (iter = mem_cgroup_start_loop(root);\
897 iter != NULL;\
898 iter = mem_cgroup_get_next(iter, root, cond))
899
900#define for_each_mem_cgroup_tree(iter, root) \
901 for_each_mem_cgroup_tree_cond(iter, root, true)
902
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700903#define for_each_mem_cgroup_all(iter) \
904 for_each_mem_cgroup_tree_cond(iter, NULL, true)
905
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700906
Balbir Singh4b3bde42009-09-23 15:56:32 -0700907static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
908{
909 return (mem == root_mem_cgroup);
910}
911
Ying Han456f9982011-05-26 16:25:38 -0700912void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
913{
914 struct mem_cgroup *mem;
915
916 if (!mm)
917 return;
918
919 rcu_read_lock();
920 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
921 if (unlikely(!mem))
922 goto out;
923
924 switch (idx) {
925 case PGMAJFAULT:
926 mem_cgroup_pgmajfault(mem, 1);
927 break;
928 case PGFAULT:
929 mem_cgroup_pgfault(mem, 1);
930 break;
931 default:
932 BUG();
933 }
934out:
935 rcu_read_unlock();
936}
937EXPORT_SYMBOL(mem_cgroup_count_vm_event);
938
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800939/*
940 * Following LRU functions are allowed to be used without PCG_LOCK.
941 * Operations are called by routine of global LRU independently from memcg.
942 * What we have to take care of here is validness of pc->mem_cgroup.
943 *
944 * Changes to pc->mem_cgroup happens when
945 * 1. charge
946 * 2. moving account
947 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
948 * It is added to LRU before charge.
949 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
950 * When moving account, the page is not on LRU. It's isolated.
951 */
952
953void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800954{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800955 struct page_cgroup *pc;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800956 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700957
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800958 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800959 return;
960 pc = lookup_page_cgroup(page);
961 /* can happen while we handle swapcache. */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700962 if (!TestClearPageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800963 return;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700964 VM_BUG_ON(!pc->mem_cgroup);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800965 /*
966 * We don't check PCG_USED bit. It's cleared when the "page" is finally
967 * removed from global LRU.
968 */
Johannes Weiner97a6c372011-03-23 16:42:27 -0700969 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -0800970 /* huge page split is done under lru_lock. so, we have no races. */
971 MEM_CGROUP_ZSTAT(mz, lru) -= 1 << compound_order(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700972 if (mem_cgroup_is_root(pc->mem_cgroup))
973 return;
974 VM_BUG_ON(list_empty(&pc->lru));
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800975 list_del_init(&pc->lru);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800976}
977
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800978void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800979{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800980 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800981}
982
Minchan Kim3f58a822011-03-22 16:32:53 -0700983/*
984 * Writeback is about to end against a page which has been marked for immediate
985 * reclaim. If it still appears to be reclaimable, move it to the tail of the
986 * inactive list.
987 */
988void mem_cgroup_rotate_reclaimable_page(struct page *page)
989{
990 struct mem_cgroup_per_zone *mz;
991 struct page_cgroup *pc;
992 enum lru_list lru = page_lru(page);
993
994 if (mem_cgroup_disabled())
995 return;
996
997 pc = lookup_page_cgroup(page);
998 /* unused or root page is not rotated. */
999 if (!PageCgroupUsed(pc))
1000 return;
1001 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
1002 smp_rmb();
1003 if (mem_cgroup_is_root(pc->mem_cgroup))
1004 return;
Johannes Weiner97a6c372011-03-23 16:42:27 -07001005 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
Minchan Kim3f58a822011-03-22 16:32:53 -07001006 list_move_tail(&pc->lru, &mz->lists[lru]);
1007}
1008
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001009void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -08001010{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001011 struct mem_cgroup_per_zone *mz;
1012 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001013
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001014 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001015 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -07001016
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001017 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -07001018 /* unused or root page is not rotated. */
Johannes Weiner713735b2011-01-20 14:44:31 -08001019 if (!PageCgroupUsed(pc))
1020 return;
1021 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
1022 smp_rmb();
1023 if (mem_cgroup_is_root(pc->mem_cgroup))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001024 return;
Johannes Weiner97a6c372011-03-23 16:42:27 -07001025 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001026 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -08001027}
1028
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001029void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
1030{
1031 struct page_cgroup *pc;
1032 struct mem_cgroup_per_zone *mz;
1033
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001034 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001035 return;
1036 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -07001037 VM_BUG_ON(PageCgroupAcctLRU(pc));
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001038 if (!PageCgroupUsed(pc))
1039 return;
Johannes Weiner713735b2011-01-20 14:44:31 -08001040 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
1041 smp_rmb();
Johannes Weiner97a6c372011-03-23 16:42:27 -07001042 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08001043 /* huge page split is done under lru_lock. so, we have no races. */
1044 MEM_CGROUP_ZSTAT(mz, lru) += 1 << compound_order(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -07001045 SetPageCgroupAcctLRU(pc);
1046 if (mem_cgroup_is_root(pc->mem_cgroup))
1047 return;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001048 list_add(&pc->lru, &mz->lists[lru]);
1049}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001050
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001051/*
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001052 * At handling SwapCache and other FUSE stuff, pc->mem_cgroup may be changed
1053 * while it's linked to lru because the page may be reused after it's fully
1054 * uncharged. To handle that, unlink page_cgroup from LRU when charge it again.
1055 * It's done under lock_page and expected that zone->lru_lock isnever held.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001056 */
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001057static void mem_cgroup_lru_del_before_commit(struct page *page)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001058{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001059 unsigned long flags;
1060 struct zone *zone = page_zone(page);
1061 struct page_cgroup *pc = lookup_page_cgroup(page);
1062
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001063 /*
1064 * Doing this check without taking ->lru_lock seems wrong but this
1065 * is safe. Because if page_cgroup's USED bit is unset, the page
1066 * will not be added to any memcg's LRU. If page_cgroup's USED bit is
1067 * set, the commit after this will fail, anyway.
1068 * This all charge/uncharge is done under some mutual execustion.
1069 * So, we don't need to taking care of changes in USED bit.
1070 */
1071 if (likely(!PageLRU(page)))
1072 return;
1073
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001074 spin_lock_irqsave(&zone->lru_lock, flags);
1075 /*
1076 * Forget old LRU when this page_cgroup is *not* used. This Used bit
1077 * is guarded by lock_page() because the page is SwapCache.
1078 */
1079 if (!PageCgroupUsed(pc))
1080 mem_cgroup_del_lru_list(page, page_lru(page));
1081 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001082}
1083
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001084static void mem_cgroup_lru_add_after_commit(struct page *page)
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001085{
1086 unsigned long flags;
1087 struct zone *zone = page_zone(page);
1088 struct page_cgroup *pc = lookup_page_cgroup(page);
1089
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001090 /* taking care of that the page is added to LRU while we commit it */
1091 if (likely(!PageLRU(page)))
1092 return;
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001093 spin_lock_irqsave(&zone->lru_lock, flags);
1094 /* link when the page is linked to LRU but page_cgroup isn't */
Balbir Singh4b3bde42009-09-23 15:56:32 -07001095 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001096 mem_cgroup_add_lru_list(page, page_lru(page));
1097 spin_unlock_irqrestore(&zone->lru_lock, flags);
1098}
1099
1100
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001101void mem_cgroup_move_lists(struct page *page,
1102 enum lru_list from, enum lru_list to)
1103{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001104 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001105 return;
1106 mem_cgroup_del_lru_list(page, from);
1107 mem_cgroup_add_lru_list(page, to);
1108}
1109
Michal Hocko3e920412011-07-26 16:08:29 -07001110/*
1111 * Checks whether given mem is same or in the root_mem's
1112 * hierarchy subtree
1113 */
1114static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_mem,
1115 struct mem_cgroup *mem)
1116{
1117 if (root_mem != mem) {
1118 return (root_mem->use_hierarchy &&
1119 css_is_ancestor(&mem->css, &root_mem->css));
1120 }
1121
1122 return true;
1123}
1124
David Rientjes4c4a2212008-02-07 00:14:06 -08001125int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
1126{
1127 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001128 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001129 struct task_struct *p;
David Rientjes4c4a2212008-02-07 00:14:06 -08001130
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001131 p = find_lock_task_mm(task);
1132 if (!p)
1133 return 0;
1134 curr = try_get_mem_cgroup_from_mm(p->mm);
1135 task_unlock(p);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001136 if (!curr)
1137 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001138 /*
1139 * We should check use_hierarchy of "mem" not "curr". Because checking
1140 * use_hierarchy of "curr" here make this function true if hierarchy is
1141 * enabled in "curr" and "curr" is a child of "mem" in *cgroup*
1142 * hierarchy(even if use_hierarchy is disabled in "mem").
1143 */
Michal Hocko3e920412011-07-26 16:08:29 -07001144 ret = mem_cgroup_same_or_subtree(mem, curr);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001145 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001146 return ret;
1147}
1148
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001149static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001150{
1151 unsigned long active;
1152 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001153 unsigned long gb;
1154 unsigned long inactive_ratio;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001155
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07001156 inactive = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_ANON));
1157 active = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_ANON));
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001158
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001159 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1160 if (gb)
1161 inactive_ratio = int_sqrt(10 * gb);
1162 else
1163 inactive_ratio = 1;
1164
1165 if (present_pages) {
1166 present_pages[0] = inactive;
1167 present_pages[1] = active;
1168 }
1169
1170 return inactive_ratio;
1171}
1172
1173int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
1174{
1175 unsigned long active;
1176 unsigned long inactive;
1177 unsigned long present_pages[2];
1178 unsigned long inactive_ratio;
1179
1180 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
1181
1182 inactive = present_pages[0];
1183 active = present_pages[1];
1184
1185 if (inactive * inactive_ratio < active)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001186 return 1;
1187
1188 return 0;
1189}
1190
Rik van Riel56e49d22009-06-16 15:32:28 -07001191int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
1192{
1193 unsigned long active;
1194 unsigned long inactive;
1195
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07001196 inactive = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_FILE));
1197 active = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_FILE));
Rik van Riel56e49d22009-06-16 15:32:28 -07001198
1199 return (active > inactive);
1200}
1201
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001202struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
1203 struct zone *zone)
1204{
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -07001205 int nid = zone_to_nid(zone);
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001206 int zid = zone_idx(zone);
1207 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
1208
1209 return &mz->reclaim_stat;
1210}
1211
1212struct zone_reclaim_stat *
1213mem_cgroup_get_reclaim_stat_from_page(struct page *page)
1214{
1215 struct page_cgroup *pc;
1216 struct mem_cgroup_per_zone *mz;
1217
1218 if (mem_cgroup_disabled())
1219 return NULL;
1220
1221 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -08001222 if (!PageCgroupUsed(pc))
1223 return NULL;
Johannes Weiner713735b2011-01-20 14:44:31 -08001224 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
1225 smp_rmb();
Johannes Weiner97a6c372011-03-23 16:42:27 -07001226 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001227 return &mz->reclaim_stat;
1228}
1229
Balbir Singh66e17072008-02-07 00:13:56 -08001230unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
1231 struct list_head *dst,
1232 unsigned long *scanned, int order,
1233 int mode, struct zone *z,
1234 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001235 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -08001236{
1237 unsigned long nr_taken = 0;
1238 struct page *page;
1239 unsigned long scan;
1240 LIST_HEAD(pc_list);
1241 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001242 struct page_cgroup *pc, *tmp;
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -07001243 int nid = zone_to_nid(z);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001244 int zid = zone_idx(z);
1245 struct mem_cgroup_per_zone *mz;
Johannes Weinerb7c46d12009-09-21 17:02:56 -07001246 int lru = LRU_FILE * file + active;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001247 int ret;
Balbir Singh66e17072008-02-07 00:13:56 -08001248
Balbir Singhcf475ad2008-04-29 01:00:16 -07001249 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001250 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001251 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -08001252
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001253 scan = 0;
1254 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -08001255 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001256 break;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001257
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001258 if (unlikely(!PageCgroupUsed(pc)))
1259 continue;
Johannes Weiner5564e882011-03-23 16:42:29 -07001260
Johannes Weiner6b3ae582011-03-23 16:42:30 -07001261 page = lookup_cgroup_page(pc);
Johannes Weiner5564e882011-03-23 16:42:29 -07001262
Hugh Dickins436c65412008-02-07 00:14:12 -08001263 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001264 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001265
Hugh Dickins436c65412008-02-07 00:14:12 -08001266 scan++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001267 ret = __isolate_lru_page(page, mode, file);
1268 switch (ret) {
1269 case 0:
Balbir Singh66e17072008-02-07 00:13:56 -08001270 list_move(&page->lru, dst);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001271 mem_cgroup_del_lru(page);
Rik van Riel2c888cf2011-01-13 15:47:13 -08001272 nr_taken += hpage_nr_pages(page);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001273 break;
1274 case -EBUSY:
1275 /* we don't affect global LRU but rotate in our LRU */
1276 mem_cgroup_rotate_lru_list(page, page_lru(page));
1277 break;
1278 default:
1279 break;
Balbir Singh66e17072008-02-07 00:13:56 -08001280 }
1281 }
1282
Balbir Singh66e17072008-02-07 00:13:56 -08001283 *scanned = scan;
KOSAKI Motohirocc8e9702010-08-09 17:19:57 -07001284
1285 trace_mm_vmscan_memcg_isolate(0, nr_to_scan, scan, nr_taken,
1286 0, 0, 0, mode);
1287
Balbir Singh66e17072008-02-07 00:13:56 -08001288 return nr_taken;
1289}
1290
Balbir Singh6d61ef42009-01-07 18:08:06 -08001291#define mem_cgroup_from_res_counter(counter, member) \
1292 container_of(counter, struct mem_cgroup, member)
1293
Johannes Weiner19942822011-02-01 15:52:43 -08001294/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001295 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
1296 * @mem: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001297 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001298 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001299 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001300 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001301static unsigned long mem_cgroup_margin(struct mem_cgroup *mem)
Johannes Weiner19942822011-02-01 15:52:43 -08001302{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001303 unsigned long long margin;
1304
1305 margin = res_counter_margin(&mem->res);
1306 if (do_swap_account)
1307 margin = min(margin, res_counter_margin(&mem->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001308 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001309}
1310
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001311int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001312{
1313 struct cgroup *cgrp = memcg->css.cgroup;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001314
1315 /* root ? */
1316 if (cgrp->parent == NULL)
1317 return vm_swappiness;
1318
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001319 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001320}
1321
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001322static void mem_cgroup_start_move(struct mem_cgroup *mem)
1323{
1324 int cpu;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001325
1326 get_online_cpus();
1327 spin_lock(&mem->pcp_counter_lock);
1328 for_each_online_cpu(cpu)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001329 per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) += 1;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001330 mem->nocpu_base.count[MEM_CGROUP_ON_MOVE] += 1;
1331 spin_unlock(&mem->pcp_counter_lock);
1332 put_online_cpus();
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001333
1334 synchronize_rcu();
1335}
1336
1337static void mem_cgroup_end_move(struct mem_cgroup *mem)
1338{
1339 int cpu;
1340
1341 if (!mem)
1342 return;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001343 get_online_cpus();
1344 spin_lock(&mem->pcp_counter_lock);
1345 for_each_online_cpu(cpu)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001346 per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) -= 1;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001347 mem->nocpu_base.count[MEM_CGROUP_ON_MOVE] -= 1;
1348 spin_unlock(&mem->pcp_counter_lock);
1349 put_online_cpus();
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001350}
1351/*
1352 * 2 routines for checking "mem" is under move_account() or not.
1353 *
1354 * mem_cgroup_stealed() - checking a cgroup is mc.from or not. This is used
1355 * for avoiding race in accounting. If true,
1356 * pc->mem_cgroup may be overwritten.
1357 *
1358 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1359 * under hierarchy of moving cgroups. This is for
1360 * waiting at hith-memory prressure caused by "move".
1361 */
1362
1363static bool mem_cgroup_stealed(struct mem_cgroup *mem)
1364{
1365 VM_BUG_ON(!rcu_read_lock_held());
1366 return this_cpu_read(mem->stat->count[MEM_CGROUP_ON_MOVE]) > 0;
1367}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001368
1369static bool mem_cgroup_under_move(struct mem_cgroup *mem)
1370{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001371 struct mem_cgroup *from;
1372 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001373 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001374 /*
1375 * Unlike task_move routines, we access mc.to, mc.from not under
1376 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1377 */
1378 spin_lock(&mc.lock);
1379 from = mc.from;
1380 to = mc.to;
1381 if (!from)
1382 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001383
1384 ret = mem_cgroup_same_or_subtree(mem, from)
1385 || mem_cgroup_same_or_subtree(mem, to);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001386unlock:
1387 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001388 return ret;
1389}
1390
1391static bool mem_cgroup_wait_acct_move(struct mem_cgroup *mem)
1392{
1393 if (mc.moving_task && current != mc.moving_task) {
1394 if (mem_cgroup_under_move(mem)) {
1395 DEFINE_WAIT(wait);
1396 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1397 /* moving charge context might have finished. */
1398 if (mc.moving_task)
1399 schedule();
1400 finish_wait(&mc.waitq, &wait);
1401 return true;
1402 }
1403 }
1404 return false;
1405}
1406
Balbir Singhe2224322009-04-02 16:57:39 -07001407/**
Kirill A. Shutemov6a6135b2010-03-10 15:22:25 -08001408 * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode.
Balbir Singhe2224322009-04-02 16:57:39 -07001409 * @memcg: The memory cgroup that went over limit
1410 * @p: Task that is going to be killed
1411 *
1412 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1413 * enabled
1414 */
1415void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1416{
1417 struct cgroup *task_cgrp;
1418 struct cgroup *mem_cgrp;
1419 /*
1420 * Need a buffer in BSS, can't rely on allocations. The code relies
1421 * on the assumption that OOM is serialized for memory controller.
1422 * If this assumption is broken, revisit this code.
1423 */
1424 static char memcg_name[PATH_MAX];
1425 int ret;
1426
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001427 if (!memcg || !p)
Balbir Singhe2224322009-04-02 16:57:39 -07001428 return;
1429
1430
1431 rcu_read_lock();
1432
1433 mem_cgrp = memcg->css.cgroup;
1434 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1435
1436 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1437 if (ret < 0) {
1438 /*
1439 * Unfortunately, we are unable to convert to a useful name
1440 * But we'll still print out the usage information
1441 */
1442 rcu_read_unlock();
1443 goto done;
1444 }
1445 rcu_read_unlock();
1446
1447 printk(KERN_INFO "Task in %s killed", memcg_name);
1448
1449 rcu_read_lock();
1450 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1451 if (ret < 0) {
1452 rcu_read_unlock();
1453 goto done;
1454 }
1455 rcu_read_unlock();
1456
1457 /*
1458 * Continues from above, so we don't need an KERN_ level
1459 */
1460 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1461done:
1462
1463 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1464 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1465 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1466 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1467 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1468 "failcnt %llu\n",
1469 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1470 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1471 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1472}
1473
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001474/*
1475 * This function returns the number of memcg under hierarchy tree. Returns
1476 * 1(self count) if no children.
1477 */
1478static int mem_cgroup_count_children(struct mem_cgroup *mem)
1479{
1480 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001481 struct mem_cgroup *iter;
1482
1483 for_each_mem_cgroup_tree(iter, mem)
1484 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001485 return num;
1486}
1487
Balbir Singh6d61ef42009-01-07 18:08:06 -08001488/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001489 * Return the memory (and swap, if configured) limit for a memcg.
1490 */
1491u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
1492{
1493 u64 limit;
1494 u64 memsw;
1495
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001496 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
1497 limit += total_swap_pages << PAGE_SHIFT;
1498
David Rientjesa63d83f2010-08-09 17:19:46 -07001499 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1500 /*
1501 * If memsw is finite and limits the amount of swap space available
1502 * to this memcg, return that limit.
1503 */
1504 return min(limit, memsw);
1505}
1506
1507/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001508 * Visit the first child (need not be the first child as per the ordering
1509 * of the cgroup list, since we track last_scanned_child) of @mem and use
1510 * that to reclaim free pages from.
1511 */
1512static struct mem_cgroup *
1513mem_cgroup_select_victim(struct mem_cgroup *root_mem)
1514{
1515 struct mem_cgroup *ret = NULL;
1516 struct cgroup_subsys_state *css;
1517 int nextid, found;
1518
1519 if (!root_mem->use_hierarchy) {
1520 css_get(&root_mem->css);
1521 ret = root_mem;
1522 }
1523
1524 while (!ret) {
1525 rcu_read_lock();
1526 nextid = root_mem->last_scanned_child + 1;
1527 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
1528 &found);
1529 if (css && css_tryget(css))
1530 ret = container_of(css, struct mem_cgroup, css);
1531
1532 rcu_read_unlock();
1533 /* Updates scanning parameter */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001534 if (!css) {
1535 /* this means start scan from ID:1 */
1536 root_mem->last_scanned_child = 0;
1537 } else
1538 root_mem->last_scanned_child = found;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001539 }
1540
1541 return ret;
1542}
1543
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001544/**
1545 * test_mem_cgroup_node_reclaimable
1546 * @mem: the target memcg
1547 * @nid: the node ID to be checked.
1548 * @noswap : specify true here if the user wants flle only information.
1549 *
1550 * This function returns whether the specified memcg contains any
1551 * reclaimable pages on a node. Returns true if there are any reclaimable
1552 * pages in the node.
1553 */
1554static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *mem,
1555 int nid, bool noswap)
1556{
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07001557 if (mem_cgroup_node_nr_lru_pages(mem, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001558 return true;
1559 if (noswap || !total_swap_pages)
1560 return false;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07001561 if (mem_cgroup_node_nr_lru_pages(mem, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001562 return true;
1563 return false;
1564
1565}
Ying Han889976d2011-05-26 16:25:33 -07001566#if MAX_NUMNODES > 1
1567
1568/*
1569 * Always updating the nodemask is not very good - even if we have an empty
1570 * list or the wrong list here, we can start from some node and traverse all
1571 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1572 *
1573 */
1574static void mem_cgroup_may_update_nodemask(struct mem_cgroup *mem)
1575{
1576 int nid;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001577 /*
1578 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1579 * pagein/pageout changes since the last update.
1580 */
1581 if (!atomic_read(&mem->numainfo_events))
1582 return;
1583 if (atomic_inc_return(&mem->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001584 return;
1585
Ying Han889976d2011-05-26 16:25:33 -07001586 /* make a nodemask where this memcg uses memory from */
1587 mem->scan_nodes = node_states[N_HIGH_MEMORY];
1588
1589 for_each_node_mask(nid, node_states[N_HIGH_MEMORY]) {
1590
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001591 if (!test_mem_cgroup_node_reclaimable(mem, nid, false))
1592 node_clear(nid, mem->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001593 }
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001594
1595 atomic_set(&mem->numainfo_events, 0);
1596 atomic_set(&mem->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001597}
1598
1599/*
1600 * Selecting a node where we start reclaim from. Because what we need is just
1601 * reducing usage counter, start from anywhere is O,K. Considering
1602 * memory reclaim from current node, there are pros. and cons.
1603 *
1604 * Freeing memory from current node means freeing memory from a node which
1605 * we'll use or we've used. So, it may make LRU bad. And if several threads
1606 * hit limits, it will see a contention on a node. But freeing from remote
1607 * node means more costs for memory reclaim because of memory latency.
1608 *
1609 * Now, we use round-robin. Better algorithm is welcomed.
1610 */
1611int mem_cgroup_select_victim_node(struct mem_cgroup *mem)
1612{
1613 int node;
1614
1615 mem_cgroup_may_update_nodemask(mem);
1616 node = mem->last_scanned_node;
1617
1618 node = next_node(node, mem->scan_nodes);
1619 if (node == MAX_NUMNODES)
1620 node = first_node(mem->scan_nodes);
1621 /*
1622 * We call this when we hit limit, not when pages are added to LRU.
1623 * No LRU may hold pages because all pages are UNEVICTABLE or
1624 * memcg is too small and all pages are not on LRU. In that case,
1625 * we use curret node.
1626 */
1627 if (unlikely(node == MAX_NUMNODES))
1628 node = numa_node_id();
1629
1630 mem->last_scanned_node = node;
1631 return node;
1632}
1633
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001634/*
1635 * Check all nodes whether it contains reclaimable pages or not.
1636 * For quick scan, we make use of scan_nodes. This will allow us to skip
1637 * unused nodes. But scan_nodes is lazily updated and may not cotain
1638 * enough new information. We need to do double check.
1639 */
1640bool mem_cgroup_reclaimable(struct mem_cgroup *mem, bool noswap)
1641{
1642 int nid;
1643
1644 /*
1645 * quick check...making use of scan_node.
1646 * We can skip unused nodes.
1647 */
1648 if (!nodes_empty(mem->scan_nodes)) {
1649 for (nid = first_node(mem->scan_nodes);
1650 nid < MAX_NUMNODES;
1651 nid = next_node(nid, mem->scan_nodes)) {
1652
1653 if (test_mem_cgroup_node_reclaimable(mem, nid, noswap))
1654 return true;
1655 }
1656 }
1657 /*
1658 * Check rest of nodes.
1659 */
1660 for_each_node_state(nid, N_HIGH_MEMORY) {
1661 if (node_isset(nid, mem->scan_nodes))
1662 continue;
1663 if (test_mem_cgroup_node_reclaimable(mem, nid, noswap))
1664 return true;
1665 }
1666 return false;
1667}
1668
Ying Han889976d2011-05-26 16:25:33 -07001669#else
1670int mem_cgroup_select_victim_node(struct mem_cgroup *mem)
1671{
1672 return 0;
1673}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001674
1675bool mem_cgroup_reclaimable(struct mem_cgroup *mem, bool noswap)
1676{
1677 return test_mem_cgroup_node_reclaimable(mem, 0, noswap);
1678}
Ying Han889976d2011-05-26 16:25:33 -07001679#endif
1680
KAMEZAWA Hiroyuki82f9d482011-07-26 16:08:26 -07001681static void __mem_cgroup_record_scanstat(unsigned long *stats,
1682 struct memcg_scanrecord *rec)
1683{
1684
1685 stats[SCAN] += rec->nr_scanned[0] + rec->nr_scanned[1];
1686 stats[SCAN_ANON] += rec->nr_scanned[0];
1687 stats[SCAN_FILE] += rec->nr_scanned[1];
1688
1689 stats[ROTATE] += rec->nr_rotated[0] + rec->nr_rotated[1];
1690 stats[ROTATE_ANON] += rec->nr_rotated[0];
1691 stats[ROTATE_FILE] += rec->nr_rotated[1];
1692
1693 stats[FREED] += rec->nr_freed[0] + rec->nr_freed[1];
1694 stats[FREED_ANON] += rec->nr_freed[0];
1695 stats[FREED_FILE] += rec->nr_freed[1];
1696
1697 stats[ELAPSED] += rec->elapsed;
1698}
1699
1700static void mem_cgroup_record_scanstat(struct memcg_scanrecord *rec)
1701{
1702 struct mem_cgroup *mem;
1703 int context = rec->context;
1704
1705 if (context >= NR_SCAN_CONTEXT)
1706 return;
1707
1708 mem = rec->mem;
1709 spin_lock(&mem->scanstat.lock);
1710 __mem_cgroup_record_scanstat(mem->scanstat.stats[context], rec);
1711 spin_unlock(&mem->scanstat.lock);
1712
1713 mem = rec->root;
1714 spin_lock(&mem->scanstat.lock);
1715 __mem_cgroup_record_scanstat(mem->scanstat.rootstats[context], rec);
1716 spin_unlock(&mem->scanstat.lock);
1717}
1718
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001719/*
1720 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1721 * we reclaimed from, so that we don't end up penalizing one child extensively
1722 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001723 *
1724 * root_mem is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001725 *
1726 * We give up and return to the caller when we visit root_mem twice.
1727 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001728 *
1729 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001730 */
1731static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
Balbir Singh4e416952009-09-23 15:56:39 -07001732 struct zone *zone,
Balbir Singh75822b42009-09-23 15:56:38 -07001733 gfp_t gfp_mask,
Ying Han0ae5e892011-05-26 16:25:25 -07001734 unsigned long reclaim_options,
1735 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001736{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001737 struct mem_cgroup *victim;
1738 int ret, total = 0;
1739 int loop = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001740 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1741 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
Balbir Singh4e416952009-09-23 15:56:39 -07001742 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
KAMEZAWA Hiroyuki82f9d482011-07-26 16:08:26 -07001743 struct memcg_scanrecord rec;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001744 unsigned long excess;
KAMEZAWA Hiroyuki82f9d482011-07-26 16:08:26 -07001745 unsigned long scanned;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001746
1747 excess = res_counter_soft_limit_excess(&root_mem->res) >> PAGE_SHIFT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001748
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001749 /* If memsw_is_minimum==1, swap-out is of-no-use. */
Daisuke Nishimura108b6a72011-07-26 16:08:25 -07001750 if (!check_soft && !shrink && root_mem->memsw_is_minimum)
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001751 noswap = true;
1752
KAMEZAWA Hiroyuki82f9d482011-07-26 16:08:26 -07001753 if (shrink)
1754 rec.context = SCAN_BY_SHRINK;
1755 else if (check_soft)
1756 rec.context = SCAN_BY_SYSTEM;
1757 else
1758 rec.context = SCAN_BY_LIMIT;
1759
1760 rec.root = root_mem;
1761
Balbir Singh4e416952009-09-23 15:56:39 -07001762 while (1) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001763 victim = mem_cgroup_select_victim(root_mem);
Balbir Singh4e416952009-09-23 15:56:39 -07001764 if (victim == root_mem) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001765 loop++;
KAMEZAWA Hiroyukifbc29a22011-06-15 15:08:46 -07001766 /*
1767 * We are not draining per cpu cached charges during
1768 * soft limit reclaim because global reclaim doesn't
1769 * care about charges. It tries to free some memory and
1770 * charges will not give any.
1771 */
1772 if (!check_soft && loop >= 1)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07001773 drain_all_stock_async(root_mem);
Balbir Singh4e416952009-09-23 15:56:39 -07001774 if (loop >= 2) {
1775 /*
1776 * If we have not been able to reclaim
1777 * anything, it might because there are
1778 * no reclaimable pages under this hierarchy
1779 */
1780 if (!check_soft || !total) {
1781 css_put(&victim->css);
1782 break;
1783 }
1784 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001785 * We want to do more targeted reclaim.
Balbir Singh4e416952009-09-23 15:56:39 -07001786 * excess >> 2 is not to excessive so as to
1787 * reclaim too much, nor too less that we keep
1788 * coming back to reclaim from this cgroup
1789 */
1790 if (total >= (excess >> 2) ||
1791 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1792 css_put(&victim->css);
1793 break;
1794 }
1795 }
1796 }
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001797 if (!mem_cgroup_reclaimable(victim, noswap)) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001798 /* this cgroup's local usage == 0 */
1799 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001800 continue;
1801 }
KAMEZAWA Hiroyuki82f9d482011-07-26 16:08:26 -07001802 rec.mem = victim;
1803 rec.nr_scanned[0] = 0;
1804 rec.nr_scanned[1] = 0;
1805 rec.nr_rotated[0] = 0;
1806 rec.nr_rotated[1] = 0;
1807 rec.nr_freed[0] = 0;
1808 rec.nr_freed[1] = 0;
1809 rec.elapsed = 0;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001810 /* we use swappiness of local cgroup */
Ying Han0ae5e892011-05-26 16:25:25 -07001811 if (check_soft) {
Balbir Singh4e416952009-09-23 15:56:39 -07001812 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
KAMEZAWA Hiroyuki82f9d482011-07-26 16:08:26 -07001813 noswap, zone, &rec, &scanned);
1814 *total_scanned += scanned;
Ying Han0ae5e892011-05-26 16:25:25 -07001815 } else
Balbir Singh4e416952009-09-23 15:56:39 -07001816 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
KAMEZAWA Hiroyuki82f9d482011-07-26 16:08:26 -07001817 noswap, &rec);
1818 mem_cgroup_record_scanstat(&rec);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001819 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001820 /*
1821 * At shrinking usage, we can't check we should stop here or
1822 * reclaim more. It's depends on callers. last_scanned_child
1823 * will work enough for keeping fairness under tree.
1824 */
1825 if (shrink)
1826 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001827 total += ret;
Balbir Singh4e416952009-09-23 15:56:39 -07001828 if (check_soft) {
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001829 if (!res_counter_soft_limit_excess(&root_mem->res))
Balbir Singh4e416952009-09-23 15:56:39 -07001830 return total;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001831 } else if (mem_cgroup_margin(root_mem))
Johannes Weiner4fd14eb2011-05-26 16:25:35 -07001832 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001833 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001834 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001835}
1836
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001837/*
1838 * Check OOM-Killer is already running under our hierarchy.
1839 * If someone is running, return false.
Michal Hocko1af8efe2011-07-26 16:08:24 -07001840 * Has to be called with memcg_oom_lock
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001841 */
1842static bool mem_cgroup_oom_lock(struct mem_cgroup *mem)
1843{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001844 int lock_count = -1;
1845 struct mem_cgroup *iter, *failed = NULL;
1846 bool cond = true;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001847
Michal Hocko79dfdac2011-07-26 16:08:23 -07001848 for_each_mem_cgroup_tree_cond(iter, mem, cond) {
1849 bool locked = iter->oom_lock;
1850
1851 iter->oom_lock = true;
1852 if (lock_count == -1)
1853 lock_count = iter->oom_lock;
1854 else if (lock_count != locked) {
1855 /*
1856 * this subtree of our hierarchy is already locked
1857 * so we cannot give a lock.
1858 */
1859 lock_count = 0;
1860 failed = iter;
1861 cond = false;
1862 }
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001863 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001864
Michal Hocko79dfdac2011-07-26 16:08:23 -07001865 if (!failed)
1866 goto done;
1867
1868 /*
1869 * OK, we failed to lock the whole subtree so we have to clean up
1870 * what we set up to the failing subtree
1871 */
1872 cond = true;
1873 for_each_mem_cgroup_tree_cond(iter, mem, cond) {
1874 if (iter == failed) {
1875 cond = false;
1876 continue;
1877 }
1878 iter->oom_lock = false;
1879 }
1880done:
1881 return lock_count;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001882}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001883
Michal Hocko79dfdac2011-07-26 16:08:23 -07001884/*
Michal Hocko1af8efe2011-07-26 16:08:24 -07001885 * Has to be called with memcg_oom_lock
Michal Hocko79dfdac2011-07-26 16:08:23 -07001886 */
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001887static int mem_cgroup_oom_unlock(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001888{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001889 struct mem_cgroup *iter;
1890
Michal Hocko79dfdac2011-07-26 16:08:23 -07001891 for_each_mem_cgroup_tree(iter, mem)
1892 iter->oom_lock = false;
1893 return 0;
1894}
1895
1896static void mem_cgroup_mark_under_oom(struct mem_cgroup *mem)
1897{
1898 struct mem_cgroup *iter;
1899
1900 for_each_mem_cgroup_tree(iter, mem)
1901 atomic_inc(&iter->under_oom);
1902}
1903
1904static void mem_cgroup_unmark_under_oom(struct mem_cgroup *mem)
1905{
1906 struct mem_cgroup *iter;
1907
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001908 /*
1909 * When a new child is created while the hierarchy is under oom,
1910 * mem_cgroup_oom_lock() may not be called. We have to use
1911 * atomic_add_unless() here.
1912 */
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001913 for_each_mem_cgroup_tree(iter, mem)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001914 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001915}
1916
Michal Hocko1af8efe2011-07-26 16:08:24 -07001917static DEFINE_SPINLOCK(memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001918static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1919
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001920struct oom_wait_info {
1921 struct mem_cgroup *mem;
1922 wait_queue_t wait;
1923};
1924
1925static int memcg_oom_wake_function(wait_queue_t *wait,
1926 unsigned mode, int sync, void *arg)
1927{
Michal Hocko3e920412011-07-26 16:08:29 -07001928 struct mem_cgroup *wake_mem = (struct mem_cgroup *)arg,
1929 *oom_wait_mem;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001930 struct oom_wait_info *oom_wait_info;
1931
1932 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Michal Hocko3e920412011-07-26 16:08:29 -07001933 oom_wait_mem = oom_wait_info->mem;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001934
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001935 /*
1936 * Both of oom_wait_info->mem and wake_mem are stable under us.
1937 * Then we can use css_is_ancestor without taking care of RCU.
1938 */
Michal Hocko3e920412011-07-26 16:08:29 -07001939 if (!mem_cgroup_same_or_subtree(oom_wait_mem, wake_mem)
1940 && !mem_cgroup_same_or_subtree(wake_mem, oom_wait_mem))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001941 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001942 return autoremove_wake_function(wait, mode, sync, arg);
1943}
1944
1945static void memcg_wakeup_oom(struct mem_cgroup *mem)
1946{
1947 /* for filtering, pass "mem" as argument. */
1948 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, mem);
1949}
1950
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001951static void memcg_oom_recover(struct mem_cgroup *mem)
1952{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001953 if (mem && atomic_read(&mem->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001954 memcg_wakeup_oom(mem);
1955}
1956
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001957/*
1958 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
1959 */
1960bool mem_cgroup_handle_oom(struct mem_cgroup *mem, gfp_t mask)
1961{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001962 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001963 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001964
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001965 owait.mem = mem;
1966 owait.wait.flags = 0;
1967 owait.wait.func = memcg_oom_wake_function;
1968 owait.wait.private = current;
1969 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001970 need_to_kill = true;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001971 mem_cgroup_mark_under_oom(mem);
1972
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001973 /* At first, try to OOM lock hierarchy under mem.*/
Michal Hocko1af8efe2011-07-26 16:08:24 -07001974 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001975 locked = mem_cgroup_oom_lock(mem);
1976 /*
1977 * Even if signal_pending(), we can't quit charge() loop without
1978 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
1979 * under OOM is always welcomed, use TASK_KILLABLE here.
1980 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001981 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
1982 if (!locked || mem->oom_kill_disable)
1983 need_to_kill = false;
1984 if (locked)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07001985 mem_cgroup_oom_notify(mem);
Michal Hocko1af8efe2011-07-26 16:08:24 -07001986 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001987
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001988 if (need_to_kill) {
1989 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001990 mem_cgroup_out_of_memory(mem, mask);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001991 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001992 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001993 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001994 }
Michal Hocko1af8efe2011-07-26 16:08:24 -07001995 spin_lock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001996 if (locked)
1997 mem_cgroup_oom_unlock(mem);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001998 memcg_wakeup_oom(mem);
Michal Hocko1af8efe2011-07-26 16:08:24 -07001999 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002000
Michal Hocko79dfdac2011-07-26 16:08:23 -07002001 mem_cgroup_unmark_under_oom(mem);
2002
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002003 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
2004 return false;
2005 /* Give chance to dying process */
2006 schedule_timeout(1);
2007 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002008}
2009
Balbir Singhd69b0422009-06-17 16:26:34 -07002010/*
2011 * Currently used to update mapped file statistics, but the routine can be
2012 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002013 *
2014 * Notes: Race condition
2015 *
2016 * We usually use page_cgroup_lock() for accessing page_cgroup member but
2017 * it tends to be costly. But considering some conditions, we doesn't need
2018 * to do so _always_.
2019 *
2020 * Considering "charge", lock_page_cgroup() is not required because all
2021 * file-stat operations happen after a page is attached to radix-tree. There
2022 * are no race with "charge".
2023 *
2024 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
2025 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
2026 * if there are race with "uncharge". Statistics itself is properly handled
2027 * by flags.
2028 *
2029 * Considering "move", this is an only case we see a race. To make the race
2030 * small, we check MEM_CGROUP_ON_MOVE percpu value and detect there are
2031 * possibility of race condition. If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07002032 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002033
Greg Thelen2a7106f2011-01-13 15:47:37 -08002034void mem_cgroup_update_page_stat(struct page *page,
2035 enum mem_cgroup_page_stat_item idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07002036{
2037 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002038 struct page_cgroup *pc = lookup_page_cgroup(page);
2039 bool need_unlock = false;
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08002040 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07002041
Balbir Singhd69b0422009-06-17 16:26:34 -07002042 if (unlikely(!pc))
2043 return;
2044
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002045 rcu_read_lock();
Balbir Singhd69b0422009-06-17 16:26:34 -07002046 mem = pc->mem_cgroup;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002047 if (unlikely(!mem || !PageCgroupUsed(pc)))
2048 goto out;
2049 /* pc->mem_cgroup is unstable ? */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002050 if (unlikely(mem_cgroup_stealed(mem)) || PageTransHuge(page)) {
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002051 /* take a lock against to access pc->mem_cgroup */
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08002052 move_lock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002053 need_unlock = true;
2054 mem = pc->mem_cgroup;
2055 if (!mem || !PageCgroupUsed(pc))
2056 goto out;
2057 }
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002058
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002059 switch (idx) {
Greg Thelen2a7106f2011-01-13 15:47:37 -08002060 case MEMCG_NR_FILE_MAPPED:
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002061 if (val > 0)
2062 SetPageCgroupFileMapped(pc);
2063 else if (!page_mapped(page))
KAMEZAWA Hiroyuki0c270f82010-10-27 15:33:39 -07002064 ClearPageCgroupFileMapped(pc);
Greg Thelen2a7106f2011-01-13 15:47:37 -08002065 idx = MEM_CGROUP_STAT_FILE_MAPPED;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002066 break;
2067 default:
2068 BUG();
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002069 }
Balbir Singhd69b0422009-06-17 16:26:34 -07002070
Greg Thelen2a7106f2011-01-13 15:47:37 -08002071 this_cpu_add(mem->stat->count[idx], val);
2072
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002073out:
2074 if (unlikely(need_unlock))
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08002075 move_unlock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002076 rcu_read_unlock();
2077 return;
Balbir Singhd69b0422009-06-17 16:26:34 -07002078}
Greg Thelen2a7106f2011-01-13 15:47:37 -08002079EXPORT_SYMBOL(mem_cgroup_update_page_stat);
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002080
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002081/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002082 * size of first charge trial. "32" comes from vmscan.c's magic value.
2083 * TODO: maybe necessary to use big numbers in big irons.
2084 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002085#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002086struct memcg_stock_pcp {
2087 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002088 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002089 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002090 unsigned long flags;
2091#define FLUSHING_CACHED_CHARGE (0)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002092};
2093static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002094static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002095
2096/*
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002097 * Try to consume stocked charge on this cpu. If success, one page is consumed
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002098 * from local stock and true is returned. If the stock is 0 or charges from a
2099 * cgroup which is not current target, returns false. This stock will be
2100 * refilled.
2101 */
2102static bool consume_stock(struct mem_cgroup *mem)
2103{
2104 struct memcg_stock_pcp *stock;
2105 bool ret = true;
2106
2107 stock = &get_cpu_var(memcg_stock);
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002108 if (mem == stock->cached && stock->nr_pages)
2109 stock->nr_pages--;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002110 else /* need to call res_counter_charge */
2111 ret = false;
2112 put_cpu_var(memcg_stock);
2113 return ret;
2114}
2115
2116/*
2117 * Returns stocks cached in percpu to res_counter and reset cached information.
2118 */
2119static void drain_stock(struct memcg_stock_pcp *stock)
2120{
2121 struct mem_cgroup *old = stock->cached;
2122
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002123 if (stock->nr_pages) {
2124 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2125
2126 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002127 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002128 res_counter_uncharge(&old->memsw, bytes);
2129 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002130 }
2131 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002132}
2133
2134/*
2135 * This must be called under preempt disabled or must be called by
2136 * a thread which is pinned to local cpu.
2137 */
2138static void drain_local_stock(struct work_struct *dummy)
2139{
2140 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
2141 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002142 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002143}
2144
2145/*
2146 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002147 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002148 */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002149static void refill_stock(struct mem_cgroup *mem, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002150{
2151 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2152
2153 if (stock->cached != mem) { /* reset if necessary */
2154 drain_stock(stock);
2155 stock->cached = mem;
2156 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002157 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002158 put_cpu_var(memcg_stock);
2159}
2160
2161/*
Michal Hockod38144b2011-07-26 16:08:28 -07002162 * Drains all per-CPU charge caches for given root_mem resp. subtree
2163 * of the hierarchy under it. sync flag says whether we should block
2164 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002165 */
Michal Hockod38144b2011-07-26 16:08:28 -07002166static void drain_all_stock(struct mem_cgroup *root_mem, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002167{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002168 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002169
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002170 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002171 get_online_cpus();
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002172 /*
2173 * Get a hint for avoiding draining charges on the current cpu,
2174 * which must be exhausted by our charging. It is not required that
2175 * this be a precise check, so we use raw_smp_processor_id() instead of
2176 * getcpu()/putcpu().
2177 */
2178 curcpu = raw_smp_processor_id();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002179 for_each_online_cpu(cpu) {
2180 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002181 struct mem_cgroup *mem;
2182
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002183 mem = stock->cached;
Michal Hockod1a05b62011-07-26 16:08:27 -07002184 if (!mem || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002185 continue;
Michal Hocko3e920412011-07-26 16:08:29 -07002186 if (!mem_cgroup_same_or_subtree(root_mem, mem))
2187 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002188 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2189 if (cpu == curcpu)
2190 drain_local_stock(&stock->work);
2191 else
2192 schedule_work_on(cpu, &stock->work);
2193 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002194 }
Michal Hockod38144b2011-07-26 16:08:28 -07002195
2196 if (!sync)
2197 goto out;
2198
2199 for_each_online_cpu(cpu) {
2200 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002201 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002202 flush_work(&stock->work);
2203 }
2204out:
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002205 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002206}
2207
2208/*
2209 * Tries to drain stocked charges in other cpus. This function is asynchronous
2210 * and just put a work per cpu for draining localy on each cpu. Caller can
2211 * expects some charges will be back to res_counter later but cannot wait for
2212 * it.
2213 */
2214static void drain_all_stock_async(struct mem_cgroup *root_mem)
2215{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002216 /*
2217 * If someone calls draining, avoid adding more kworker runs.
2218 */
2219 if (!mutex_trylock(&percpu_charge_mutex))
2220 return;
Michal Hockod38144b2011-07-26 16:08:28 -07002221 drain_all_stock(root_mem, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002222 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002223}
2224
2225/* This is a synchronous drain interface. */
Michal Hockod38144b2011-07-26 16:08:28 -07002226static void drain_all_stock_sync(struct mem_cgroup *root_mem)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002227{
2228 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002229 mutex_lock(&percpu_charge_mutex);
Michal Hockod38144b2011-07-26 16:08:28 -07002230 drain_all_stock(root_mem, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002231 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002232}
2233
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002234/*
2235 * This function drains percpu counter value from DEAD cpu and
2236 * move it to local cpu. Note that this function can be preempted.
2237 */
2238static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *mem, int cpu)
2239{
2240 int i;
2241
2242 spin_lock(&mem->pcp_counter_lock);
2243 for (i = 0; i < MEM_CGROUP_STAT_DATA; i++) {
Johannes Weiner7a159cc2011-03-23 16:42:38 -07002244 long x = per_cpu(mem->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002245
2246 per_cpu(mem->stat->count[i], cpu) = 0;
2247 mem->nocpu_base.count[i] += x;
2248 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002249 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
2250 unsigned long x = per_cpu(mem->stat->events[i], cpu);
2251
2252 per_cpu(mem->stat->events[i], cpu) = 0;
2253 mem->nocpu_base.events[i] += x;
2254 }
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002255 /* need to clear ON_MOVE value, works as a kind of lock. */
2256 per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) = 0;
2257 spin_unlock(&mem->pcp_counter_lock);
2258}
2259
2260static void synchronize_mem_cgroup_on_move(struct mem_cgroup *mem, int cpu)
2261{
2262 int idx = MEM_CGROUP_ON_MOVE;
2263
2264 spin_lock(&mem->pcp_counter_lock);
2265 per_cpu(mem->stat->count[idx], cpu) = mem->nocpu_base.count[idx];
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002266 spin_unlock(&mem->pcp_counter_lock);
2267}
2268
2269static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002270 unsigned long action,
2271 void *hcpu)
2272{
2273 int cpu = (unsigned long)hcpu;
2274 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002275 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002276
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002277 if ((action == CPU_ONLINE)) {
2278 for_each_mem_cgroup_all(iter)
2279 synchronize_mem_cgroup_on_move(iter, cpu);
2280 return NOTIFY_OK;
2281 }
2282
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002283 if ((action != CPU_DEAD) || action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002284 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002285
2286 for_each_mem_cgroup_all(iter)
2287 mem_cgroup_drain_pcp_counter(iter, cpu);
2288
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002289 stock = &per_cpu(memcg_stock, cpu);
2290 drain_stock(stock);
2291 return NOTIFY_OK;
2292}
2293
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002294
2295/* See __mem_cgroup_try_charge() for details */
2296enum {
2297 CHARGE_OK, /* success */
2298 CHARGE_RETRY, /* need to retry but retry is not bad */
2299 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2300 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
2301 CHARGE_OOM_DIE, /* the current is killed because of OOM */
2302};
2303
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002304static int mem_cgroup_do_charge(struct mem_cgroup *mem, gfp_t gfp_mask,
2305 unsigned int nr_pages, bool oom_check)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002306{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002307 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002308 struct mem_cgroup *mem_over_limit;
2309 struct res_counter *fail_res;
2310 unsigned long flags = 0;
2311 int ret;
2312
2313 ret = res_counter_charge(&mem->res, csize, &fail_res);
2314
2315 if (likely(!ret)) {
2316 if (!do_swap_account)
2317 return CHARGE_OK;
2318 ret = res_counter_charge(&mem->memsw, csize, &fail_res);
2319 if (likely(!ret))
2320 return CHARGE_OK;
2321
KAMEZAWA Hiroyuki01c88e22011-01-25 15:07:27 -08002322 res_counter_uncharge(&mem->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002323 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2324 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2325 } else
2326 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002327 /*
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002328 * nr_pages can be either a huge page (HPAGE_PMD_NR), a batch
2329 * of regular pages (CHARGE_BATCH), or a single regular page (1).
Johannes Weiner9221edb2011-02-01 15:52:42 -08002330 *
2331 * Never reclaim on behalf of optional batching, retry with a
2332 * single page instead.
2333 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002334 if (nr_pages == CHARGE_BATCH)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002335 return CHARGE_RETRY;
2336
2337 if (!(gfp_mask & __GFP_WAIT))
2338 return CHARGE_WOULDBLOCK;
2339
2340 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
Ying Han0ae5e892011-05-26 16:25:25 -07002341 gfp_mask, flags, NULL);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002342 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002343 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002344 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002345 * Even though the limit is exceeded at this point, reclaim
2346 * may have been able to free some pages. Retry the charge
2347 * before killing the task.
2348 *
2349 * Only for regular pages, though: huge pages are rather
2350 * unlikely to succeed so close to the limit, and we fall back
2351 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002352 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002353 if (nr_pages == 1 && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002354 return CHARGE_RETRY;
2355
2356 /*
2357 * At task move, charge accounts can be doubly counted. So, it's
2358 * better to wait until the end of task_move if something is going on.
2359 */
2360 if (mem_cgroup_wait_acct_move(mem_over_limit))
2361 return CHARGE_RETRY;
2362
2363 /* If we don't need to call oom-killer at el, return immediately */
2364 if (!oom_check)
2365 return CHARGE_NOMEM;
2366 /* check OOM */
2367 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask))
2368 return CHARGE_OOM_DIE;
2369
2370 return CHARGE_RETRY;
2371}
2372
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002373/*
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002374 * Unlike exported interface, "oom" parameter is added. if oom==true,
2375 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002376 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002377static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002378 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002379 unsigned int nr_pages,
2380 struct mem_cgroup **memcg,
2381 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002382{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002383 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002384 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2385 struct mem_cgroup *mem = NULL;
2386 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002387
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002388 /*
2389 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2390 * in system level. So, allow to go ahead dying process in addition to
2391 * MEMDIE process.
2392 */
2393 if (unlikely(test_thread_flag(TIF_MEMDIE)
2394 || fatal_signal_pending(current)))
2395 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002396
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002397 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08002398 * We always charge the cgroup the mm_struct belongs to.
2399 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002400 * thread group leader migrates. It's possible that mm is not
2401 * set, if so charge the init_mm (happens for pagecache usage).
2402 */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002403 if (!*memcg && !mm)
2404 goto bypass;
2405again:
2406 if (*memcg) { /* css should be a valid one */
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002407 mem = *memcg;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002408 VM_BUG_ON(css_is_removed(&mem->css));
2409 if (mem_cgroup_is_root(mem))
2410 goto done;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002411 if (nr_pages == 1 && consume_stock(mem))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002412 goto done;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002413 css_get(&mem->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002414 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002415 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002416
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002417 rcu_read_lock();
2418 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002419 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002420 * Because we don't have task_lock(), "p" can exit.
2421 * In that case, "mem" can point to root or p can be NULL with
2422 * race with swapoff. Then, we have small risk of mis-accouning.
2423 * But such kind of mis-account by race always happens because
2424 * we don't have cgroup_mutex(). It's overkill and we allo that
2425 * small race, here.
2426 * (*) swapoff at el will charge against mm-struct not against
2427 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002428 */
2429 mem = mem_cgroup_from_task(p);
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002430 if (!mem || mem_cgroup_is_root(mem)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002431 rcu_read_unlock();
2432 goto done;
2433 }
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002434 if (nr_pages == 1 && consume_stock(mem)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002435 /*
2436 * It seems dagerous to access memcg without css_get().
2437 * But considering how consume_stok works, it's not
2438 * necessary. If consume_stock success, some charges
2439 * from this memcg are cached on this cpu. So, we
2440 * don't need to call css_get()/css_tryget() before
2441 * calling consume_stock().
2442 */
2443 rcu_read_unlock();
2444 goto done;
2445 }
2446 /* after here, we may be blocked. we need to get refcnt */
2447 if (!css_tryget(&mem->css)) {
2448 rcu_read_unlock();
2449 goto again;
2450 }
2451 rcu_read_unlock();
2452 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002453
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002454 do {
2455 bool oom_check;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002456
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002457 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002458 if (fatal_signal_pending(current)) {
2459 css_put(&mem->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002460 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002461 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002462
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002463 oom_check = false;
2464 if (oom && !nr_oom_retries) {
2465 oom_check = true;
2466 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2467 }
Balbir Singh6d61ef42009-01-07 18:08:06 -08002468
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002469 ret = mem_cgroup_do_charge(mem, gfp_mask, batch, oom_check);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002470 switch (ret) {
2471 case CHARGE_OK:
2472 break;
2473 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002474 batch = nr_pages;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002475 css_put(&mem->css);
2476 mem = NULL;
2477 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002478 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002479 css_put(&mem->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002480 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002481 case CHARGE_NOMEM: /* OOM routine works */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002482 if (!oom) {
2483 css_put(&mem->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002484 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002485 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002486 /* If oom, we never return -ENOMEM */
2487 nr_oom_retries--;
2488 break;
2489 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002490 css_put(&mem->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002491 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08002492 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002493 } while (ret != CHARGE_OK);
2494
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002495 if (batch > nr_pages)
2496 refill_stock(mem, batch - nr_pages);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002497 css_put(&mem->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002498done:
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002499 *memcg = mem;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002500 return 0;
2501nomem:
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002502 *memcg = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002503 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002504bypass:
2505 *memcg = NULL;
2506 return 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002507}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002508
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002509/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002510 * Somemtimes we have to undo a charge we got by try_charge().
2511 * This function is for that and do uncharge, put css's refcnt.
2512 * gotten by try_charge().
2513 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002514static void __mem_cgroup_cancel_charge(struct mem_cgroup *mem,
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002515 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002516{
2517 if (!mem_cgroup_is_root(mem)) {
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002518 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002519
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002520 res_counter_uncharge(&mem->res, bytes);
2521 if (do_swap_account)
2522 res_counter_uncharge(&mem->memsw, bytes);
2523 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002524}
2525
2526/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002527 * A helper function to get mem_cgroup from ID. must be called under
2528 * rcu_read_lock(). The caller must check css_is_removed() or some if
2529 * it's concern. (dropping refcnt from swap can be called against removed
2530 * memcg.)
2531 */
2532static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2533{
2534 struct cgroup_subsys_state *css;
2535
2536 /* ID 0 is unused ID */
2537 if (!id)
2538 return NULL;
2539 css = css_lookup(&mem_cgroup_subsys, id);
2540 if (!css)
2541 return NULL;
2542 return container_of(css, struct mem_cgroup, css);
2543}
2544
Wu Fengguange42d9d52009-12-16 12:19:59 +01002545struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002546{
Wu Fengguange42d9d52009-12-16 12:19:59 +01002547 struct mem_cgroup *mem = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002548 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002549 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002550 swp_entry_t ent;
2551
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002552 VM_BUG_ON(!PageLocked(page));
2553
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002554 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002555 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002556 if (PageCgroupUsed(pc)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002557 mem = pc->mem_cgroup;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002558 if (mem && !css_tryget(&mem->css))
2559 mem = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002560 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002561 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002562 id = lookup_swap_cgroup(ent);
2563 rcu_read_lock();
2564 mem = mem_cgroup_lookup(id);
2565 if (mem && !css_tryget(&mem->css))
2566 mem = NULL;
2567 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002568 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002569 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002570 return mem;
2571}
2572
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002573static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
Johannes Weiner5564e882011-03-23 16:42:29 -07002574 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002575 unsigned int nr_pages,
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002576 struct page_cgroup *pc,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002577 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002578{
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002579 lock_page_cgroup(pc);
2580 if (unlikely(PageCgroupUsed(pc))) {
2581 unlock_page_cgroup(pc);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002582 __mem_cgroup_cancel_charge(mem, nr_pages);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002583 return;
2584 }
2585 /*
2586 * we don't need page_cgroup_lock about tail pages, becase they are not
2587 * accessed by any other context at this point.
2588 */
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002589 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002590 /*
2591 * We access a page_cgroup asynchronously without lock_page_cgroup().
2592 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2593 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2594 * before USED bit, we need memory barrier here.
2595 * See mem_cgroup_add_lru_list(), etc.
2596 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002597 smp_wmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -07002598 switch (ctype) {
2599 case MEM_CGROUP_CHARGE_TYPE_CACHE:
2600 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
2601 SetPageCgroupCache(pc);
2602 SetPageCgroupUsed(pc);
2603 break;
2604 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
2605 ClearPageCgroupCache(pc);
2606 SetPageCgroupUsed(pc);
2607 break;
2608 default:
2609 break;
2610 }
Hugh Dickins3be91272008-02-07 00:14:19 -08002611
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002612 mem_cgroup_charge_statistics(mem, PageCgroupCache(pc), nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002613 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002614 /*
2615 * "charge_statistics" updated event counter. Then, check it.
2616 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2617 * if they exceeds softlimit.
2618 */
Johannes Weiner5564e882011-03-23 16:42:29 -07002619 memcg_check_events(mem, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002620}
2621
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002622#ifdef CONFIG_TRANSPARENT_HUGEPAGE
2623
2624#define PCGF_NOCOPY_AT_SPLIT ((1 << PCG_LOCK) | (1 << PCG_MOVE_LOCK) |\
2625 (1 << PCG_ACCT_LRU) | (1 << PCG_MIGRATION))
2626/*
2627 * Because tail pages are not marked as "used", set it. We're under
2628 * zone->lru_lock, 'splitting on pmd' and compund_lock.
2629 */
2630void mem_cgroup_split_huge_fixup(struct page *head, struct page *tail)
2631{
2632 struct page_cgroup *head_pc = lookup_page_cgroup(head);
2633 struct page_cgroup *tail_pc = lookup_page_cgroup(tail);
2634 unsigned long flags;
2635
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08002636 if (mem_cgroup_disabled())
2637 return;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002638 /*
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002639 * We have no races with charge/uncharge but will have races with
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002640 * page state accounting.
2641 */
2642 move_lock_page_cgroup(head_pc, &flags);
2643
2644 tail_pc->mem_cgroup = head_pc->mem_cgroup;
2645 smp_wmb(); /* see __commit_charge() */
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002646 if (PageCgroupAcctLRU(head_pc)) {
2647 enum lru_list lru;
2648 struct mem_cgroup_per_zone *mz;
2649
2650 /*
2651 * LRU flags cannot be copied because we need to add tail
2652 *.page to LRU by generic call and our hook will be called.
2653 * We hold lru_lock, then, reduce counter directly.
2654 */
2655 lru = page_lru(head);
Johannes Weiner97a6c372011-03-23 16:42:27 -07002656 mz = page_cgroup_zoneinfo(head_pc->mem_cgroup, head);
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002657 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
2658 }
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002659 tail_pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
2660 move_unlock_page_cgroup(head_pc, &flags);
2661}
2662#endif
2663
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002664/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07002665 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07002666 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002667 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002668 * @pc: page_cgroup of the page.
2669 * @from: mem_cgroup which the page is moved from.
2670 * @to: mem_cgroup which the page is moved to. @from != @to.
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002671 * @uncharge: whether we should call uncharge and css_put against @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002672 *
2673 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002674 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002675 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002676 *
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002677 * This function doesn't do "charge" nor css_get to new cgroup. It should be
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002678 * done by a caller(__mem_cgroup_try_charge would be useful). If @uncharge is
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002679 * true, this function does "uncharge" from old cgroup, but it doesn't if
2680 * @uncharge is false, so a caller should do "uncharge".
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002681 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002682static int mem_cgroup_move_account(struct page *page,
2683 unsigned int nr_pages,
2684 struct page_cgroup *pc,
2685 struct mem_cgroup *from,
2686 struct mem_cgroup *to,
2687 bool uncharge)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002688{
Johannes Weinerde3638d2011-03-23 16:42:28 -07002689 unsigned long flags;
2690 int ret;
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002691
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002692 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07002693 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07002694 /*
2695 * The page is isolated from LRU. So, collapse function
2696 * will not handle this page. But page splitting can happen.
2697 * Do this check under compound_page_lock(). The caller should
2698 * hold it.
2699 */
2700 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002701 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07002702 goto out;
2703
2704 lock_page_cgroup(pc);
2705
2706 ret = -EINVAL;
2707 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
2708 goto unlock;
2709
2710 move_lock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002711
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002712 if (PageCgroupFileMapped(pc)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08002713 /* Update mapped_file data for mem_cgroup */
2714 preempt_disable();
2715 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2716 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2717 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07002718 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002719 mem_cgroup_charge_statistics(from, PageCgroupCache(pc), -nr_pages);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002720 if (uncharge)
2721 /* This is not "cancel", but cancel_charge does all we need. */
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002722 __mem_cgroup_cancel_charge(from, nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07002723
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002724 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002725 pc->mem_cgroup = to;
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002726 mem_cgroup_charge_statistics(to, PageCgroupCache(pc), nr_pages);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002727 /*
2728 * We charges against "to" which may not have any tasks. Then, "to"
2729 * can be under rmdir(). But in current implementation, caller of
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08002730 * this function is just force_empty() and move charge, so it's
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002731 * guaranteed that "to" is never removed. So, we don't check rmdir
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08002732 * status here.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002733 */
Johannes Weinerde3638d2011-03-23 16:42:28 -07002734 move_unlock_page_cgroup(pc, &flags);
2735 ret = 0;
2736unlock:
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002737 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002738 /*
2739 * check events
2740 */
Johannes Weiner5564e882011-03-23 16:42:29 -07002741 memcg_check_events(to, page);
2742 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07002743out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002744 return ret;
2745}
2746
2747/*
2748 * move charges to its parent.
2749 */
2750
Johannes Weiner5564e882011-03-23 16:42:29 -07002751static int mem_cgroup_move_parent(struct page *page,
2752 struct page_cgroup *pc,
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002753 struct mem_cgroup *child,
2754 gfp_t gfp_mask)
2755{
2756 struct cgroup *cg = child->css.cgroup;
2757 struct cgroup *pcg = cg->parent;
2758 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002759 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07002760 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002761 int ret;
2762
2763 /* Is ROOT ? */
2764 if (!pcg)
2765 return -EINVAL;
2766
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002767 ret = -EBUSY;
2768 if (!get_page_unless_zero(page))
2769 goto out;
2770 if (isolate_lru_page(page))
2771 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08002772
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002773 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002774
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002775 parent = mem_cgroup_from_cont(pcg);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002776 ret = __mem_cgroup_try_charge(NULL, gfp_mask, nr_pages, &parent, false);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002777 if (ret || !parent)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002778 goto put_back;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002779
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002780 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002781 flags = compound_lock_irqsave(page);
2782
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002783 ret = mem_cgroup_move_account(page, nr_pages, pc, child, parent, true);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002784 if (ret)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002785 __mem_cgroup_cancel_charge(parent, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08002786
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002787 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002788 compound_unlock_irqrestore(page, flags);
Jesper Juhl8dba4742011-01-25 15:07:24 -08002789put_back:
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002790 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002791put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08002792 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002793out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002794 return ret;
2795}
2796
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002797/*
2798 * Charge the memory controller for page usage.
2799 * Return
2800 * 0 if the charge was successful
2801 * < 0 if the cgroup is over its limit
2802 */
2803static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002804 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002805{
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002806 struct mem_cgroup *mem = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002807 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08002808 struct page_cgroup *pc;
2809 bool oom = true;
2810 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08002811
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002812 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002813 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002814 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08002815 /*
2816 * Never OOM-kill a process for a huge page. The
2817 * fault handler will fall back to regular pages.
2818 */
2819 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002820 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002821
2822 pc = lookup_page_cgroup(page);
Johannes Weineraf4a6622011-03-23 16:42:24 -07002823 BUG_ON(!pc); /* XXX: remove this and move pc lookup into commit */
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002824
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002825 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &mem, oom);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002826 if (ret || !mem)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002827 return ret;
2828
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002829 __mem_cgroup_commit_charge(mem, page, nr_pages, pc, ctype);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002830 return 0;
2831}
2832
2833int mem_cgroup_newpage_charge(struct page *page,
2834 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002835{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002836 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002837 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002838 /*
2839 * If already mapped, we don't have to account.
2840 * If page cache, page->mapping has address_space.
2841 * But page->mapping may have out-of-use anon_vma pointer,
2842 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
2843 * is NULL.
2844 */
2845 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
2846 return 0;
2847 if (unlikely(!mm))
2848 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002849 return mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002850 MEM_CGROUP_CHARGE_TYPE_MAPPED);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002851}
2852
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002853static void
2854__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2855 enum charge_type ctype);
2856
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002857static void
2858__mem_cgroup_commit_charge_lrucare(struct page *page, struct mem_cgroup *mem,
2859 enum charge_type ctype)
2860{
2861 struct page_cgroup *pc = lookup_page_cgroup(page);
2862 /*
2863 * In some case, SwapCache, FUSE(splice_buf->radixtree), the page
2864 * is already on LRU. It means the page may on some other page_cgroup's
2865 * LRU. Take care of it.
2866 */
2867 mem_cgroup_lru_del_before_commit(page);
2868 __mem_cgroup_commit_charge(mem, page, 1, pc, ctype);
2869 mem_cgroup_lru_add_after_commit(page);
2870 return;
2871}
2872
Balbir Singhe1a1cd52008-02-07 00:14:02 -08002873int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
2874 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08002875{
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002876 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002877 int ret;
2878
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002879 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002880 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002881 if (PageCompound(page))
2882 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002883
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002884 if (unlikely(!mm))
Balbir Singh8697d332008-02-07 00:13:59 -08002885 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002886
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002887 if (page_is_file_cache(page)) {
2888 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, &mem, true);
2889 if (ret || !mem)
2890 return ret;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002891
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002892 /*
2893 * FUSE reuses pages without going through the final
2894 * put that would remove them from the LRU list, make
2895 * sure that they get relinked properly.
2896 */
2897 __mem_cgroup_commit_charge_lrucare(page, mem,
2898 MEM_CGROUP_CHARGE_TYPE_CACHE);
2899 return ret;
2900 }
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002901 /* shmem */
2902 if (PageSwapCache(page)) {
2903 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2904 if (!ret)
2905 __mem_cgroup_commit_charge_swapin(page, mem,
2906 MEM_CGROUP_CHARGE_TYPE_SHMEM);
2907 } else
2908 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002909 MEM_CGROUP_CHARGE_TYPE_SHMEM);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002910
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002911 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002912}
2913
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002914/*
2915 * While swap-in, try_charge -> commit or cancel, the page is locked.
2916 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02002917 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002918 * "commit()" or removed by "cancel()"
2919 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002920int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
2921 struct page *page,
2922 gfp_t mask, struct mem_cgroup **ptr)
2923{
2924 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002925 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002926
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07002927 *ptr = NULL;
2928
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002929 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002930 return 0;
2931
2932 if (!do_swap_account)
2933 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002934 /*
2935 * A racing thread's fault, or swapoff, may have already updated
Hugh Dickins407f9c82009-12-14 17:59:30 -08002936 * the pte, and even removed page from swap cache: in those cases
2937 * do_swap_page()'s pte_same() test will fail; but there's also a
2938 * KSM case which does need to charge the page.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002939 */
2940 if (!PageSwapCache(page))
Hugh Dickins407f9c82009-12-14 17:59:30 -08002941 goto charge_cur_mm;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002942 mem = try_get_mem_cgroup_from_page(page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002943 if (!mem)
2944 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002945 *ptr = mem;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002946 ret = __mem_cgroup_try_charge(NULL, mask, 1, ptr, true);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002947 css_put(&mem->css);
2948 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002949charge_cur_mm:
2950 if (unlikely(!mm))
2951 mm = &init_mm;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002952 return __mem_cgroup_try_charge(mm, mask, 1, ptr, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002953}
2954
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002955static void
2956__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2957 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002958{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002959 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002960 return;
2961 if (!ptr)
2962 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002963 cgroup_exclude_rmdir(&ptr->css);
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002964
2965 __mem_cgroup_commit_charge_lrucare(page, ptr, ctype);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002966 /*
2967 * Now swap is on-memory. This means this page may be
2968 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002969 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
2970 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
2971 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002972 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002973 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002974 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002975 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002976 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002977
2978 id = swap_cgroup_record(ent, 0);
2979 rcu_read_lock();
2980 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002981 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002982 /*
2983 * This recorded memcg can be obsolete one. So, avoid
2984 * calling css_tryget
2985 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002986 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002987 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002988 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002989 mem_cgroup_put(memcg);
2990 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002991 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002992 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002993 /*
2994 * At swapin, we may charge account against cgroup which has no tasks.
2995 * So, rmdir()->pre_destroy() can be called while we do this charge.
2996 * In that case, we need to call pre_destroy() again. check it here.
2997 */
2998 cgroup_release_and_wakeup_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002999}
3000
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003001void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
3002{
3003 __mem_cgroup_commit_charge_swapin(page, ptr,
3004 MEM_CGROUP_CHARGE_TYPE_MAPPED);
3005}
3006
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003007void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
3008{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003009 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003010 return;
3011 if (!mem)
3012 return;
Johannes Weinere7018b8d2011-03-23 16:42:33 -07003013 __mem_cgroup_cancel_charge(mem, 1);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003014}
3015
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003016static void mem_cgroup_do_uncharge(struct mem_cgroup *mem,
3017 unsigned int nr_pages,
3018 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003019{
3020 struct memcg_batch_info *batch = NULL;
3021 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003022
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003023 /* If swapout, usage of swap doesn't decrease */
3024 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
3025 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003026
3027 batch = &current->memcg_batch;
3028 /*
3029 * In usual, we do css_get() when we remember memcg pointer.
3030 * But in this case, we keep res->usage until end of a series of
3031 * uncharges. Then, it's ok to ignore memcg's refcnt.
3032 */
3033 if (!batch->memcg)
3034 batch->memcg = mem;
3035 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003036 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003037 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003038 * the same cgroup and we have chance to coalesce uncharges.
3039 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
3040 * because we want to do uncharge as soon as possible.
3041 */
3042
3043 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
3044 goto direct_uncharge;
3045
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003046 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08003047 goto direct_uncharge;
3048
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003049 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003050 * In typical case, batch->memcg == mem. This means we can
3051 * merge a series of uncharges to an uncharge of res_counter.
3052 * If not, we uncharge res_counter ony by one.
3053 */
3054 if (batch->memcg != mem)
3055 goto direct_uncharge;
3056 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003057 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003058 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003059 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003060 return;
3061direct_uncharge:
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003062 res_counter_uncharge(&mem->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003063 if (uncharge_memsw)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003064 res_counter_uncharge(&mem->memsw, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003065 if (unlikely(batch->memcg != mem))
3066 memcg_oom_recover(mem);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003067 return;
3068}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003069
Balbir Singh8697d332008-02-07 00:13:59 -08003070/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003071 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003072 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003073static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003074__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003075{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003076 struct mem_cgroup *mem = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003077 unsigned int nr_pages = 1;
3078 struct page_cgroup *pc;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003079
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003080 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003081 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07003082
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003083 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003084 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003085
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003086 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003087 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003088 VM_BUG_ON(!PageTransHuge(page));
3089 }
Balbir Singh8697d332008-02-07 00:13:59 -08003090 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08003091 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08003092 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003093 pc = lookup_page_cgroup(page);
3094 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003095 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003096
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003097 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003098
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003099 mem = pc->mem_cgroup;
3100
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003101 if (!PageCgroupUsed(pc))
3102 goto unlock_out;
3103
3104 switch (ctype) {
3105 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003106 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003107 /* See mem_cgroup_prepare_migration() */
3108 if (page_mapped(page) || PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003109 goto unlock_out;
3110 break;
3111 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
3112 if (!PageAnon(page)) { /* Shared memory */
3113 if (page->mapping && !page_is_file_cache(page))
3114 goto unlock_out;
3115 } else if (page_mapped(page)) /* Anon */
3116 goto unlock_out;
3117 break;
3118 default:
3119 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003120 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003121
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003122 mem_cgroup_charge_statistics(mem, PageCgroupCache(pc), -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003123
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003124 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08003125 /*
3126 * pc->mem_cgroup is not cleared here. It will be accessed when it's
3127 * freed from LRU. This is safe because uncharged page is expected not
3128 * to be reused (freed soon). Exception is SwapCache, it's handled by
3129 * special functions.
3130 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003131
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003132 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003133 /*
3134 * even after unlock, we have mem->res.usage here and this memcg
3135 * will never be freed.
3136 */
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08003137 memcg_check_events(mem, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003138 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
3139 mem_cgroup_swap_statistics(mem, true);
3140 mem_cgroup_get(mem);
3141 }
3142 if (!mem_cgroup_is_root(mem))
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003143 mem_cgroup_do_uncharge(mem, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003144
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003145 return mem;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003146
3147unlock_out:
3148 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003149 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08003150}
3151
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003152void mem_cgroup_uncharge_page(struct page *page)
3153{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003154 /* early check. */
3155 if (page_mapped(page))
3156 return;
3157 if (page->mapping && !PageAnon(page))
3158 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003159 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
3160}
3161
3162void mem_cgroup_uncharge_cache_page(struct page *page)
3163{
3164 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07003165 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003166 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
3167}
3168
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003169/*
3170 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
3171 * In that cases, pages are freed continuously and we can expect pages
3172 * are in the same memcg. All these calls itself limits the number of
3173 * pages freed at once, then uncharge_start/end() is called properly.
3174 * This may be called prural(2) times in a context,
3175 */
3176
3177void mem_cgroup_uncharge_start(void)
3178{
3179 current->memcg_batch.do_batch++;
3180 /* We can do nest. */
3181 if (current->memcg_batch.do_batch == 1) {
3182 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003183 current->memcg_batch.nr_pages = 0;
3184 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003185 }
3186}
3187
3188void mem_cgroup_uncharge_end(void)
3189{
3190 struct memcg_batch_info *batch = &current->memcg_batch;
3191
3192 if (!batch->do_batch)
3193 return;
3194
3195 batch->do_batch--;
3196 if (batch->do_batch) /* If stacked, do nothing. */
3197 return;
3198
3199 if (!batch->memcg)
3200 return;
3201 /*
3202 * This "batch->memcg" is valid without any css_get/put etc...
3203 * bacause we hide charges behind us.
3204 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003205 if (batch->nr_pages)
3206 res_counter_uncharge(&batch->memcg->res,
3207 batch->nr_pages * PAGE_SIZE);
3208 if (batch->memsw_nr_pages)
3209 res_counter_uncharge(&batch->memcg->memsw,
3210 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003211 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003212 /* forget this pointer (for sanity check) */
3213 batch->memcg = NULL;
3214}
3215
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003216#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003217/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003218 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003219 * memcg information is recorded to swap_cgroup of "ent"
3220 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003221void
3222mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003223{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003224 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003225 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003226
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003227 if (!swapout) /* this was a swap cache but the swap is unused ! */
3228 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
3229
3230 memcg = __mem_cgroup_uncharge_common(page, ctype);
3231
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003232 /*
3233 * record memcg information, if swapout && memcg != NULL,
3234 * mem_cgroup_get() was called in uncharge().
3235 */
3236 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003237 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003238}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003239#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003240
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003241#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3242/*
3243 * called from swap_entry_free(). remove record in swap_cgroup and
3244 * uncharge "memsw" account.
3245 */
3246void mem_cgroup_uncharge_swap(swp_entry_t ent)
3247{
3248 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003249 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003250
3251 if (!do_swap_account)
3252 return;
3253
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003254 id = swap_cgroup_record(ent, 0);
3255 rcu_read_lock();
3256 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003257 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003258 /*
3259 * We uncharge this because swap is freed.
3260 * This memcg can be obsolete one. We avoid calling css_tryget
3261 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003262 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07003263 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003264 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003265 mem_cgroup_put(memcg);
3266 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003267 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003268}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003269
3270/**
3271 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
3272 * @entry: swap entry to be moved
3273 * @from: mem_cgroup which the entry is moved from
3274 * @to: mem_cgroup which the entry is moved to
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003275 * @need_fixup: whether we should fixup res_counters and refcounts.
Daisuke Nishimura02491442010-03-10 15:22:17 -08003276 *
3277 * It succeeds only when the swap_cgroup's record for this entry is the same
3278 * as the mem_cgroup's id of @from.
3279 *
3280 * Returns 0 on success, -EINVAL on failure.
3281 *
3282 * The caller must have charged to @to, IOW, called res_counter_charge() about
3283 * both res and memsw, and called css_get().
3284 */
3285static int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003286 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003287{
3288 unsigned short old_id, new_id;
3289
3290 old_id = css_id(&from->css);
3291 new_id = css_id(&to->css);
3292
3293 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08003294 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003295 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003296 /*
3297 * This function is only called from task migration context now.
3298 * It postpones res_counter and refcount handling till the end
3299 * of task migration(mem_cgroup_clear_mc()) for performance
3300 * improvement. But we cannot postpone mem_cgroup_get(to)
3301 * because if the process that has been moved to @to does
3302 * swap-in, the refcount of @to might be decreased to 0.
3303 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08003304 mem_cgroup_get(to);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003305 if (need_fixup) {
3306 if (!mem_cgroup_is_root(from))
3307 res_counter_uncharge(&from->memsw, PAGE_SIZE);
3308 mem_cgroup_put(from);
3309 /*
3310 * we charged both to->res and to->memsw, so we should
3311 * uncharge to->res.
3312 */
3313 if (!mem_cgroup_is_root(to))
3314 res_counter_uncharge(&to->res, PAGE_SIZE);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003315 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08003316 return 0;
3317 }
3318 return -EINVAL;
3319}
3320#else
3321static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003322 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003323{
3324 return -EINVAL;
3325}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003326#endif
3327
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003328/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003329 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
3330 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003331 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003332int mem_cgroup_prepare_migration(struct page *page,
Miklos Szeredief6a3c62011-03-22 16:30:52 -07003333 struct page *newpage, struct mem_cgroup **ptr, gfp_t gfp_mask)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003334{
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003335 struct mem_cgroup *mem = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003336 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003337 enum charge_type ctype;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003338 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003339
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07003340 *ptr = NULL;
3341
Andrea Arcangeliec168512011-01-13 15:46:56 -08003342 VM_BUG_ON(PageTransHuge(page));
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003343 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07003344 return 0;
3345
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003346 pc = lookup_page_cgroup(page);
3347 lock_page_cgroup(pc);
3348 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003349 mem = pc->mem_cgroup;
3350 css_get(&mem->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003351 /*
3352 * At migrating an anonymous page, its mapcount goes down
3353 * to 0 and uncharge() will be called. But, even if it's fully
3354 * unmapped, migration may fail and this page has to be
3355 * charged again. We set MIGRATION flag here and delay uncharge
3356 * until end_migration() is called
3357 *
3358 * Corner Case Thinking
3359 * A)
3360 * When the old page was mapped as Anon and it's unmap-and-freed
3361 * while migration was ongoing.
3362 * If unmap finds the old page, uncharge() of it will be delayed
3363 * until end_migration(). If unmap finds a new page, it's
3364 * uncharged when it make mapcount to be 1->0. If unmap code
3365 * finds swap_migration_entry, the new page will not be mapped
3366 * and end_migration() will find it(mapcount==0).
3367 *
3368 * B)
3369 * When the old page was mapped but migraion fails, the kernel
3370 * remaps it. A charge for it is kept by MIGRATION flag even
3371 * if mapcount goes down to 0. We can do remap successfully
3372 * without charging it again.
3373 *
3374 * C)
3375 * The "old" page is under lock_page() until the end of
3376 * migration, so, the old page itself will not be swapped-out.
3377 * If the new page is swapped out before end_migraton, our
3378 * hook to usual swap-out path will catch the event.
3379 */
3380 if (PageAnon(page))
3381 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003382 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003383 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003384 /*
3385 * If the page is not charged at this point,
3386 * we return here.
3387 */
3388 if (!mem)
3389 return 0;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003390
Andrea Arcangeli93d5c9b2010-04-23 13:17:39 -04003391 *ptr = mem;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003392 ret = __mem_cgroup_try_charge(NULL, gfp_mask, 1, ptr, false);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003393 css_put(&mem->css);/* drop extra refcnt */
3394 if (ret || *ptr == NULL) {
3395 if (PageAnon(page)) {
3396 lock_page_cgroup(pc);
3397 ClearPageCgroupMigration(pc);
3398 unlock_page_cgroup(pc);
3399 /*
3400 * The old page may be fully unmapped while we kept it.
3401 */
3402 mem_cgroup_uncharge_page(page);
3403 }
3404 return -ENOMEM;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003405 }
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003406 /*
3407 * We charge new page before it's used/mapped. So, even if unlock_page()
3408 * is called before end_migration, we can catch all events on this new
3409 * page. In the case new page is migrated but not remapped, new page's
3410 * mapcount will be finally 0 and we call uncharge in end_migration().
3411 */
3412 pc = lookup_page_cgroup(newpage);
3413 if (PageAnon(page))
3414 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
3415 else if (page_is_file_cache(page))
3416 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
3417 else
3418 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003419 __mem_cgroup_commit_charge(mem, page, 1, pc, ctype);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003420 return ret;
3421}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08003422
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003423/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003424void mem_cgroup_end_migration(struct mem_cgroup *mem,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003425 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003426{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003427 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003428 struct page_cgroup *pc;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003429
3430 if (!mem)
3431 return;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003432 /* blocks rmdir() */
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003433 cgroup_exclude_rmdir(&mem->css);
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003434 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003435 used = oldpage;
3436 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003437 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003438 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003439 unused = oldpage;
3440 }
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003441 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003442 * We disallowed uncharge of pages under migration because mapcount
3443 * of the page goes down to zero, temporarly.
3444 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003445 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003446 pc = lookup_page_cgroup(oldpage);
3447 lock_page_cgroup(pc);
3448 ClearPageCgroupMigration(pc);
3449 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003450
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003451 __mem_cgroup_uncharge_common(unused, MEM_CGROUP_CHARGE_TYPE_FORCE);
3452
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003453 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003454 * If a page is a file cache, radix-tree replacement is very atomic
3455 * and we can skip this check. When it was an Anon page, its mapcount
3456 * goes down to 0. But because we added MIGRATION flage, it's not
3457 * uncharged yet. There are several case but page->mapcount check
3458 * and USED bit check in mem_cgroup_uncharge_page() will do enough
3459 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003460 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003461 if (PageAnon(used))
3462 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003463 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003464 * At migration, we may charge account against cgroup which has no
3465 * tasks.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003466 * So, rmdir()->pre_destroy() can be called while we do this charge.
3467 * In that case, we need to call pre_destroy() again. check it here.
3468 */
3469 cgroup_release_and_wakeup_rmdir(&mem->css);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003470}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08003471
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003472#ifdef CONFIG_DEBUG_VM
3473static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
3474{
3475 struct page_cgroup *pc;
3476
3477 pc = lookup_page_cgroup(page);
3478 if (likely(pc) && PageCgroupUsed(pc))
3479 return pc;
3480 return NULL;
3481}
3482
3483bool mem_cgroup_bad_page_check(struct page *page)
3484{
3485 if (mem_cgroup_disabled())
3486 return false;
3487
3488 return lookup_page_cgroup_used(page) != NULL;
3489}
3490
3491void mem_cgroup_print_bad_page(struct page *page)
3492{
3493 struct page_cgroup *pc;
3494
3495 pc = lookup_page_cgroup_used(page);
3496 if (pc) {
3497 int ret = -1;
3498 char *path;
3499
3500 printk(KERN_ALERT "pc:%p pc->flags:%lx pc->mem_cgroup:%p",
3501 pc, pc->flags, pc->mem_cgroup);
3502
3503 path = kmalloc(PATH_MAX, GFP_KERNEL);
3504 if (path) {
3505 rcu_read_lock();
3506 ret = cgroup_path(pc->mem_cgroup->css.cgroup,
3507 path, PATH_MAX);
3508 rcu_read_unlock();
3509 }
3510
3511 printk(KERN_CONT "(%s)\n",
3512 (ret < 0) ? "cannot get the path" : path);
3513 kfree(path);
3514 }
3515}
3516#endif
3517
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003518static DEFINE_MUTEX(set_limit_mutex);
3519
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08003520static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003521 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003522{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003523 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003524 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003525 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003526 int children = mem_cgroup_count_children(memcg);
3527 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003528 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003529
3530 /*
3531 * For keeping hierarchical_reclaim simple, how long we should retry
3532 * is depends on callers. We set our retry-count to be function
3533 * of # of children which we should visit in this loop.
3534 */
3535 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
3536
3537 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003538
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003539 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003540 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003541 if (signal_pending(current)) {
3542 ret = -EINTR;
3543 break;
3544 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003545 /*
3546 * Rather than hide all in some function, I do this in
3547 * open coded manner. You see what this really does.
3548 * We have to guarantee mem->res.limit < mem->memsw.limit.
3549 */
3550 mutex_lock(&set_limit_mutex);
3551 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3552 if (memswlimit < val) {
3553 ret = -EINVAL;
3554 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003555 break;
3556 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003557
3558 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3559 if (memlimit < val)
3560 enlarge = 1;
3561
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003562 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003563 if (!ret) {
3564 if (memswlimit == val)
3565 memcg->memsw_is_minimum = true;
3566 else
3567 memcg->memsw_is_minimum = false;
3568 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003569 mutex_unlock(&set_limit_mutex);
3570
3571 if (!ret)
3572 break;
3573
Bob Liuaa20d482009-12-15 16:47:14 -08003574 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Ying Han0ae5e892011-05-26 16:25:25 -07003575 MEM_CGROUP_RECLAIM_SHRINK,
3576 NULL);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003577 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
3578 /* Usage is reduced ? */
3579 if (curusage >= oldusage)
3580 retry_count--;
3581 else
3582 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003583 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003584 if (!ret && enlarge)
3585 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08003586
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003587 return ret;
3588}
3589
Li Zefan338c8432009-06-17 16:27:15 -07003590static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
3591 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003592{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003593 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003594 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003595 int children = mem_cgroup_count_children(memcg);
3596 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003597 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003598
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003599 /* see mem_cgroup_resize_res_limit */
3600 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
3601 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003602 while (retry_count) {
3603 if (signal_pending(current)) {
3604 ret = -EINTR;
3605 break;
3606 }
3607 /*
3608 * Rather than hide all in some function, I do this in
3609 * open coded manner. You see what this really does.
3610 * We have to guarantee mem->res.limit < mem->memsw.limit.
3611 */
3612 mutex_lock(&set_limit_mutex);
3613 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3614 if (memlimit > val) {
3615 ret = -EINVAL;
3616 mutex_unlock(&set_limit_mutex);
3617 break;
3618 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003619 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3620 if (memswlimit < val)
3621 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003622 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003623 if (!ret) {
3624 if (memlimit == val)
3625 memcg->memsw_is_minimum = true;
3626 else
3627 memcg->memsw_is_minimum = false;
3628 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003629 mutex_unlock(&set_limit_mutex);
3630
3631 if (!ret)
3632 break;
3633
Balbir Singh4e416952009-09-23 15:56:39 -07003634 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh75822b42009-09-23 15:56:38 -07003635 MEM_CGROUP_RECLAIM_NOSWAP |
Ying Han0ae5e892011-05-26 16:25:25 -07003636 MEM_CGROUP_RECLAIM_SHRINK,
3637 NULL);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003638 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003639 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003640 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003641 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003642 else
3643 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003644 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003645 if (!ret && enlarge)
3646 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003647 return ret;
3648}
3649
Balbir Singh4e416952009-09-23 15:56:39 -07003650unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
Ying Han0ae5e892011-05-26 16:25:25 -07003651 gfp_t gfp_mask,
3652 unsigned long *total_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07003653{
3654 unsigned long nr_reclaimed = 0;
3655 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
3656 unsigned long reclaimed;
3657 int loop = 0;
3658 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003659 unsigned long long excess;
Ying Han0ae5e892011-05-26 16:25:25 -07003660 unsigned long nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003661
3662 if (order > 0)
3663 return 0;
3664
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07003665 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
Balbir Singh4e416952009-09-23 15:56:39 -07003666 /*
3667 * This loop can run a while, specially if mem_cgroup's continuously
3668 * keep exceeding their soft limit and putting the system under
3669 * pressure
3670 */
3671 do {
3672 if (next_mz)
3673 mz = next_mz;
3674 else
3675 mz = mem_cgroup_largest_soft_limit_node(mctz);
3676 if (!mz)
3677 break;
3678
Ying Han0ae5e892011-05-26 16:25:25 -07003679 nr_scanned = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07003680 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
3681 gfp_mask,
Ying Han0ae5e892011-05-26 16:25:25 -07003682 MEM_CGROUP_RECLAIM_SOFT,
3683 &nr_scanned);
Balbir Singh4e416952009-09-23 15:56:39 -07003684 nr_reclaimed += reclaimed;
Ying Han0ae5e892011-05-26 16:25:25 -07003685 *total_scanned += nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003686 spin_lock(&mctz->lock);
3687
3688 /*
3689 * If we failed to reclaim anything from this memory cgroup
3690 * it is time to move on to the next cgroup
3691 */
3692 next_mz = NULL;
3693 if (!reclaimed) {
3694 do {
3695 /*
3696 * Loop until we find yet another one.
3697 *
3698 * By the time we get the soft_limit lock
3699 * again, someone might have aded the
3700 * group back on the RB tree. Iterate to
3701 * make sure we get a different mem.
3702 * mem_cgroup_largest_soft_limit_node returns
3703 * NULL if no other cgroup is present on
3704 * the tree
3705 */
3706 next_mz =
3707 __mem_cgroup_largest_soft_limit_node(mctz);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003708 if (next_mz == mz)
Balbir Singh4e416952009-09-23 15:56:39 -07003709 css_put(&next_mz->mem->css);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003710 else /* next_mz == NULL or other memcg */
Balbir Singh4e416952009-09-23 15:56:39 -07003711 break;
3712 } while (1);
3713 }
Balbir Singh4e416952009-09-23 15:56:39 -07003714 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003715 excess = res_counter_soft_limit_excess(&mz->mem->res);
Balbir Singh4e416952009-09-23 15:56:39 -07003716 /*
3717 * One school of thought says that we should not add
3718 * back the node to the tree if reclaim returns 0.
3719 * But our reclaim could return 0, simply because due
3720 * to priority we are exposing a smaller subset of
3721 * memory to reclaim from. Consider this as a longer
3722 * term TODO.
3723 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003724 /* If excess == 0, no tree ops */
3725 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07003726 spin_unlock(&mctz->lock);
3727 css_put(&mz->mem->css);
3728 loop++;
3729 /*
3730 * Could not reclaim anything and there are no more
3731 * mem cgroups to try or we seem to be looping without
3732 * reclaiming anything.
3733 */
3734 if (!nr_reclaimed &&
3735 (next_mz == NULL ||
3736 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3737 break;
3738 } while (!nr_reclaimed);
3739 if (next_mz)
3740 css_put(&next_mz->mem->css);
3741 return nr_reclaimed;
3742}
3743
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003744/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003745 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003746 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
3747 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003748static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003749 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003750{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003751 struct zone *zone;
3752 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003753 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003754 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003755 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003756 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003757
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003758 zone = &NODE_DATA(node)->node_zones[zid];
3759 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07003760 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003761
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003762 loop = MEM_CGROUP_ZSTAT(mz, lru);
3763 /* give some margin against EBUSY etc...*/
3764 loop += 256;
3765 busy = NULL;
3766 while (loop--) {
Johannes Weiner5564e882011-03-23 16:42:29 -07003767 struct page *page;
3768
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003769 ret = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003770 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003771 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003772 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003773 break;
3774 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003775 pc = list_entry(list->prev, struct page_cgroup, lru);
3776 if (busy == pc) {
3777 list_move(&pc->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08003778 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003779 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003780 continue;
3781 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003782 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003783
Johannes Weiner6b3ae582011-03-23 16:42:30 -07003784 page = lookup_cgroup_page(pc);
Johannes Weiner5564e882011-03-23 16:42:29 -07003785
3786 ret = mem_cgroup_move_parent(page, pc, mem, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003787 if (ret == -ENOMEM)
3788 break;
3789
3790 if (ret == -EBUSY || ret == -EINVAL) {
3791 /* found lock contention or "pc" is obsolete. */
3792 busy = pc;
3793 cond_resched();
3794 } else
3795 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003796 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003797
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003798 if (!ret && !list_empty(list))
3799 return -EBUSY;
3800 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003801}
3802
3803/*
3804 * make mem_cgroup's charge to be 0 if there is no task.
3805 * This enables deleting this mem_cgroup.
3806 */
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003807static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003808{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003809 int ret;
3810 int node, zid, shrink;
3811 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003812 struct cgroup *cgrp = mem->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003813
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003814 css_get(&mem->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003815
3816 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003817 /* should free all ? */
3818 if (free_all)
3819 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003820move_account:
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003821 do {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003822 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003823 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003824 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003825 ret = -EINTR;
3826 if (signal_pending(current))
3827 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003828 /* This is for making all *used* pages to be on LRU. */
3829 lru_add_drain_all();
Michal Hockod38144b2011-07-26 16:08:28 -07003830 drain_all_stock_sync(mem);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003831 ret = 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07003832 mem_cgroup_start_move(mem);
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08003833 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003834 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07003835 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003836 for_each_lru(l) {
3837 ret = mem_cgroup_force_empty_list(mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003838 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003839 if (ret)
3840 break;
3841 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003842 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003843 if (ret)
3844 break;
3845 }
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07003846 mem_cgroup_end_move(mem);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003847 memcg_oom_recover(mem);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003848 /* it seems parent cgroup doesn't have enough mem */
3849 if (ret == -ENOMEM)
3850 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003851 cond_resched();
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003852 /* "ret" should also be checked to ensure all lists are empty. */
3853 } while (mem->res.usage > 0 || ret);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003854out:
3855 css_put(&mem->css);
3856 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003857
3858try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003859 /* returns EBUSY if there is a task or if we come here twice. */
3860 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003861 ret = -EBUSY;
3862 goto out;
3863 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003864 /* we call try-to-free pages for make this cgroup empty */
3865 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003866 /* try to free all pages in this cgroup */
3867 shrink = 1;
3868 while (nr_retries && mem->res.usage > 0) {
KAMEZAWA Hiroyuki82f9d482011-07-26 16:08:26 -07003869 struct memcg_scanrecord rec;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003870 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003871
3872 if (signal_pending(current)) {
3873 ret = -EINTR;
3874 goto out;
3875 }
KAMEZAWA Hiroyuki82f9d482011-07-26 16:08:26 -07003876 rec.context = SCAN_BY_SHRINK;
3877 rec.mem = mem;
3878 rec.root = mem;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003879 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
KAMEZAWA Hiroyuki82f9d482011-07-26 16:08:26 -07003880 false, &rec);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003881 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003882 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003883 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02003884 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003885 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003886
3887 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003888 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003889 /* try move_account...there may be some *locked* pages. */
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003890 goto move_account;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003891}
3892
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003893int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
3894{
3895 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
3896}
3897
3898
Balbir Singh18f59ea2009-01-07 18:08:07 -08003899static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
3900{
3901 return mem_cgroup_from_cont(cont)->use_hierarchy;
3902}
3903
3904static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
3905 u64 val)
3906{
3907 int retval = 0;
3908 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
3909 struct cgroup *parent = cont->parent;
3910 struct mem_cgroup *parent_mem = NULL;
3911
3912 if (parent)
3913 parent_mem = mem_cgroup_from_cont(parent);
3914
3915 cgroup_lock();
3916 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003917 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08003918 * in the child subtrees. If it is unset, then the change can
3919 * occur, provided the current cgroup has no children.
3920 *
3921 * For the root cgroup, parent_mem is NULL, we allow value to be
3922 * set if there are no children.
3923 */
3924 if ((!parent_mem || !parent_mem->use_hierarchy) &&
3925 (val == 1 || val == 0)) {
3926 if (list_empty(&cont->children))
3927 mem->use_hierarchy = val;
3928 else
3929 retval = -EBUSY;
3930 } else
3931 retval = -EINVAL;
3932 cgroup_unlock();
3933
3934 return retval;
3935}
3936
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003937
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003938static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *mem,
3939 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003940{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003941 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003942 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003943
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003944 /* Per-cpu values can be negative, use a signed accumulator */
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003945 for_each_mem_cgroup_tree(iter, mem)
3946 val += mem_cgroup_read_stat(iter, idx);
3947
3948 if (val < 0) /* race ? */
3949 val = 0;
3950 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003951}
3952
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003953static inline u64 mem_cgroup_usage(struct mem_cgroup *mem, bool swap)
3954{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003955 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003956
3957 if (!mem_cgroup_is_root(mem)) {
3958 if (!swap)
3959 return res_counter_read_u64(&mem->res, RES_USAGE);
3960 else
3961 return res_counter_read_u64(&mem->memsw, RES_USAGE);
3962 }
3963
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003964 val = mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_CACHE);
3965 val += mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003966
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003967 if (swap)
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003968 val += mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_SWAPOUT);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003969
3970 return val << PAGE_SHIFT;
3971}
3972
Paul Menage2c3daa72008-04-29 00:59:58 -07003973static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003974{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003975 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003976 u64 val;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003977 int type, name;
3978
3979 type = MEMFILE_TYPE(cft->private);
3980 name = MEMFILE_ATTR(cft->private);
3981 switch (type) {
3982 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003983 if (name == RES_USAGE)
3984 val = mem_cgroup_usage(mem, false);
3985 else
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003986 val = res_counter_read_u64(&mem->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003987 break;
3988 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003989 if (name == RES_USAGE)
3990 val = mem_cgroup_usage(mem, true);
3991 else
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003992 val = res_counter_read_u64(&mem->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003993 break;
3994 default:
3995 BUG();
3996 break;
3997 }
3998 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003999}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004000/*
4001 * The user of this function is...
4002 * RES_LIMIT.
4003 */
Paul Menage856c13a2008-07-25 01:47:04 -07004004static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
4005 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004006{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004007 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004008 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004009 unsigned long long val;
4010 int ret;
4011
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004012 type = MEMFILE_TYPE(cft->private);
4013 name = MEMFILE_ATTR(cft->private);
4014 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004015 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07004016 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
4017 ret = -EINVAL;
4018 break;
4019 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004020 /* This function does all necessary parse...reuse it */
4021 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004022 if (ret)
4023 break;
4024 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004025 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004026 else
4027 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004028 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07004029 case RES_SOFT_LIMIT:
4030 ret = res_counter_memparse_write_strategy(buffer, &val);
4031 if (ret)
4032 break;
4033 /*
4034 * For memsw, soft limits are hard to implement in terms
4035 * of semantics, for now, we support soft limits for
4036 * control without swap
4037 */
4038 if (type == _MEM)
4039 ret = res_counter_set_soft_limit(&memcg->res, val);
4040 else
4041 ret = -EINVAL;
4042 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004043 default:
4044 ret = -EINVAL; /* should be BUG() ? */
4045 break;
4046 }
4047 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004048}
4049
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004050static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
4051 unsigned long long *mem_limit, unsigned long long *memsw_limit)
4052{
4053 struct cgroup *cgroup;
4054 unsigned long long min_limit, min_memsw_limit, tmp;
4055
4056 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4057 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4058 cgroup = memcg->css.cgroup;
4059 if (!memcg->use_hierarchy)
4060 goto out;
4061
4062 while (cgroup->parent) {
4063 cgroup = cgroup->parent;
4064 memcg = mem_cgroup_from_cont(cgroup);
4065 if (!memcg->use_hierarchy)
4066 break;
4067 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
4068 min_limit = min(min_limit, tmp);
4069 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4070 min_memsw_limit = min(min_memsw_limit, tmp);
4071 }
4072out:
4073 *mem_limit = min_limit;
4074 *memsw_limit = min_memsw_limit;
4075 return;
4076}
4077
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004078static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004079{
4080 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004081 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004082
4083 mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004084 type = MEMFILE_TYPE(event);
4085 name = MEMFILE_ATTR(event);
4086 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004087 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004088 if (type == _MEM)
4089 res_counter_reset_max(&mem->res);
4090 else
4091 res_counter_reset_max(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004092 break;
4093 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004094 if (type == _MEM)
4095 res_counter_reset_failcnt(&mem->res);
4096 else
4097 res_counter_reset_failcnt(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004098 break;
4099 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07004100
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07004101 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004102}
4103
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004104static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
4105 struct cftype *cft)
4106{
4107 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
4108}
4109
Daisuke Nishimura02491442010-03-10 15:22:17 -08004110#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004111static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
4112 struct cftype *cft, u64 val)
4113{
4114 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
4115
4116 if (val >= (1 << NR_MOVE_TYPE))
4117 return -EINVAL;
4118 /*
4119 * We check this value several times in both in can_attach() and
4120 * attach(), so we need cgroup lock to prevent this value from being
4121 * inconsistent.
4122 */
4123 cgroup_lock();
4124 mem->move_charge_at_immigrate = val;
4125 cgroup_unlock();
4126
4127 return 0;
4128}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004129#else
4130static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
4131 struct cftype *cft, u64 val)
4132{
4133 return -ENOSYS;
4134}
4135#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004136
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004137
4138/* For read statistics */
4139enum {
4140 MCS_CACHE,
4141 MCS_RSS,
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08004142 MCS_FILE_MAPPED,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004143 MCS_PGPGIN,
4144 MCS_PGPGOUT,
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004145 MCS_SWAP,
Ying Han456f9982011-05-26 16:25:38 -07004146 MCS_PGFAULT,
4147 MCS_PGMAJFAULT,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004148 MCS_INACTIVE_ANON,
4149 MCS_ACTIVE_ANON,
4150 MCS_INACTIVE_FILE,
4151 MCS_ACTIVE_FILE,
4152 MCS_UNEVICTABLE,
4153 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004154};
4155
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004156struct mcs_total_stat {
4157 s64 stat[NR_MCS_STAT];
4158};
4159
4160struct {
4161 char *local_name;
4162 char *total_name;
4163} memcg_stat_strings[NR_MCS_STAT] = {
4164 {"cache", "total_cache"},
4165 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07004166 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004167 {"pgpgin", "total_pgpgin"},
4168 {"pgpgout", "total_pgpgout"},
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004169 {"swap", "total_swap"},
Ying Han456f9982011-05-26 16:25:38 -07004170 {"pgfault", "total_pgfault"},
4171 {"pgmajfault", "total_pgmajfault"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004172 {"inactive_anon", "total_inactive_anon"},
4173 {"active_anon", "total_active_anon"},
4174 {"inactive_file", "total_inactive_file"},
4175 {"active_file", "total_active_file"},
4176 {"unevictable", "total_unevictable"}
4177};
4178
4179
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004180static void
4181mem_cgroup_get_local_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004182{
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004183 s64 val;
4184
4185 /* per cpu stat */
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004186 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004187 s->stat[MCS_CACHE] += val * PAGE_SIZE;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004188 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004189 s->stat[MCS_RSS] += val * PAGE_SIZE;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004190 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_FILE_MAPPED);
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08004191 s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
Johannes Weinere9f89742011-03-23 16:42:37 -07004192 val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGPGIN);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004193 s->stat[MCS_PGPGIN] += val;
Johannes Weinere9f89742011-03-23 16:42:37 -07004194 val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGPGOUT);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004195 s->stat[MCS_PGPGOUT] += val;
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004196 if (do_swap_account) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004197 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_SWAPOUT);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004198 s->stat[MCS_SWAP] += val * PAGE_SIZE;
4199 }
Ying Han456f9982011-05-26 16:25:38 -07004200 val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGFAULT);
4201 s->stat[MCS_PGFAULT] += val;
4202 val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGMAJFAULT);
4203 s->stat[MCS_PGMAJFAULT] += val;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004204
4205 /* per zone stat */
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004206 val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_INACTIVE_ANON));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004207 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004208 val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_ACTIVE_ANON));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004209 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004210 val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_INACTIVE_FILE));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004211 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004212 val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_ACTIVE_FILE));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004213 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004214 val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_UNEVICTABLE));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004215 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004216}
4217
4218static void
4219mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
4220{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004221 struct mem_cgroup *iter;
4222
4223 for_each_mem_cgroup_tree(iter, mem)
4224 mem_cgroup_get_local_stat(iter, s);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004225}
4226
Ying Han406eb0c2011-05-26 16:25:37 -07004227#ifdef CONFIG_NUMA
4228static int mem_control_numa_stat_show(struct seq_file *m, void *arg)
4229{
4230 int nid;
4231 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
4232 unsigned long node_nr;
4233 struct cgroup *cont = m->private;
4234 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
4235
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004236 total_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07004237 seq_printf(m, "total=%lu", total_nr);
4238 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004239 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07004240 seq_printf(m, " N%d=%lu", nid, node_nr);
4241 }
4242 seq_putc(m, '\n');
4243
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004244 file_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07004245 seq_printf(m, "file=%lu", file_nr);
4246 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004247 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
4248 LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07004249 seq_printf(m, " N%d=%lu", nid, node_nr);
4250 }
4251 seq_putc(m, '\n');
4252
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004253 anon_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07004254 seq_printf(m, "anon=%lu", anon_nr);
4255 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004256 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
4257 LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07004258 seq_printf(m, " N%d=%lu", nid, node_nr);
4259 }
4260 seq_putc(m, '\n');
4261
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004262 unevictable_nr = mem_cgroup_nr_lru_pages(mem_cont, BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07004263 seq_printf(m, "unevictable=%lu", unevictable_nr);
4264 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004265 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
4266 BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07004267 seq_printf(m, " N%d=%lu", nid, node_nr);
4268 }
4269 seq_putc(m, '\n');
4270 return 0;
4271}
4272#endif /* CONFIG_NUMA */
4273
Paul Menagec64745c2008-04-29 01:00:02 -07004274static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
4275 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004276{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004277 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004278 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004279 int i;
4280
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004281 memset(&mystat, 0, sizeof(mystat));
4282 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004283
Ying Han406eb0c2011-05-26 16:25:37 -07004284
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004285 for (i = 0; i < NR_MCS_STAT; i++) {
4286 if (i == MCS_SWAP && !do_swap_account)
4287 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004288 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004289 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004290
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004291 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004292 {
4293 unsigned long long limit, memsw_limit;
4294 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
4295 cb->fill(cb, "hierarchical_memory_limit", limit);
4296 if (do_swap_account)
4297 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
4298 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004299
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004300 memset(&mystat, 0, sizeof(mystat));
4301 mem_cgroup_get_total_stat(mem_cont, &mystat);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004302 for (i = 0; i < NR_MCS_STAT; i++) {
4303 if (i == MCS_SWAP && !do_swap_account)
4304 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004305 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004306 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004307
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004308#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08004309 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004310
4311 {
4312 int nid, zid;
4313 struct mem_cgroup_per_zone *mz;
4314 unsigned long recent_rotated[2] = {0, 0};
4315 unsigned long recent_scanned[2] = {0, 0};
4316
4317 for_each_online_node(nid)
4318 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
4319 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
4320
4321 recent_rotated[0] +=
4322 mz->reclaim_stat.recent_rotated[0];
4323 recent_rotated[1] +=
4324 mz->reclaim_stat.recent_rotated[1];
4325 recent_scanned[0] +=
4326 mz->reclaim_stat.recent_scanned[0];
4327 recent_scanned[1] +=
4328 mz->reclaim_stat.recent_scanned[1];
4329 }
4330 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
4331 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
4332 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
4333 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
4334 }
4335#endif
4336
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004337 return 0;
4338}
4339
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004340static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
4341{
4342 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4343
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07004344 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004345}
4346
4347static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
4348 u64 val)
4349{
4350 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4351 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08004352
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004353 if (val > 100)
4354 return -EINVAL;
4355
4356 if (cgrp->parent == NULL)
4357 return -EINVAL;
4358
4359 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08004360
4361 cgroup_lock();
4362
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004363 /* If under hierarchy, only empty-root can set this value */
4364 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08004365 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
4366 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004367 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08004368 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004369
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004370 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004371
Li Zefan068b38c2009-01-15 13:51:26 -08004372 cgroup_unlock();
4373
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004374 return 0;
4375}
4376
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004377static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
4378{
4379 struct mem_cgroup_threshold_ary *t;
4380 u64 usage;
4381 int i;
4382
4383 rcu_read_lock();
4384 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004385 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004386 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004387 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004388
4389 if (!t)
4390 goto unlock;
4391
4392 usage = mem_cgroup_usage(memcg, swap);
4393
4394 /*
4395 * current_threshold points to threshold just below usage.
4396 * If it's not true, a threshold was crossed after last
4397 * call of __mem_cgroup_threshold().
4398 */
Phil Carmody5407a562010-05-26 14:42:42 -07004399 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004400
4401 /*
4402 * Iterate backward over array of thresholds starting from
4403 * current_threshold and check if a threshold is crossed.
4404 * If none of thresholds below usage is crossed, we read
4405 * only one element of the array here.
4406 */
4407 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
4408 eventfd_signal(t->entries[i].eventfd, 1);
4409
4410 /* i = current_threshold + 1 */
4411 i++;
4412
4413 /*
4414 * Iterate forward over array of thresholds starting from
4415 * current_threshold+1 and check if a threshold is crossed.
4416 * If none of thresholds above usage is crossed, we read
4417 * only one element of the array here.
4418 */
4419 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
4420 eventfd_signal(t->entries[i].eventfd, 1);
4421
4422 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07004423 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004424unlock:
4425 rcu_read_unlock();
4426}
4427
4428static void mem_cgroup_threshold(struct mem_cgroup *memcg)
4429{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07004430 while (memcg) {
4431 __mem_cgroup_threshold(memcg, false);
4432 if (do_swap_account)
4433 __mem_cgroup_threshold(memcg, true);
4434
4435 memcg = parent_mem_cgroup(memcg);
4436 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004437}
4438
4439static int compare_thresholds(const void *a, const void *b)
4440{
4441 const struct mem_cgroup_threshold *_a = a;
4442 const struct mem_cgroup_threshold *_b = b;
4443
4444 return _a->threshold - _b->threshold;
4445}
4446
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004447static int mem_cgroup_oom_notify_cb(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004448{
4449 struct mem_cgroup_eventfd_list *ev;
4450
4451 list_for_each_entry(ev, &mem->oom_notify, list)
4452 eventfd_signal(ev->eventfd, 1);
4453 return 0;
4454}
4455
4456static void mem_cgroup_oom_notify(struct mem_cgroup *mem)
4457{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004458 struct mem_cgroup *iter;
4459
4460 for_each_mem_cgroup_tree(iter, mem)
4461 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004462}
4463
4464static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
4465 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004466{
4467 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004468 struct mem_cgroup_thresholds *thresholds;
4469 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004470 int type = MEMFILE_TYPE(cft->private);
4471 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004472 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004473
4474 ret = res_counter_memparse_write_strategy(args, &threshold);
4475 if (ret)
4476 return ret;
4477
4478 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004479
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004480 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004481 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004482 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004483 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004484 else
4485 BUG();
4486
4487 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4488
4489 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004490 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004491 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4492
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004493 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004494
4495 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004496 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004497 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004498 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004499 ret = -ENOMEM;
4500 goto unlock;
4501 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004502 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004503
4504 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004505 if (thresholds->primary) {
4506 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004507 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004508 }
4509
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004510 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004511 new->entries[size - 1].eventfd = eventfd;
4512 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004513
4514 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004515 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004516 compare_thresholds, NULL);
4517
4518 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004519 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004520 for (i = 0; i < size; i++) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004521 if (new->entries[i].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004522 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004523 * new->current_threshold will not be used until
4524 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004525 * it here.
4526 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004527 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004528 }
4529 }
4530
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004531 /* Free old spare buffer and save old primary buffer as spare */
4532 kfree(thresholds->spare);
4533 thresholds->spare = thresholds->primary;
4534
4535 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004536
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004537 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004538 synchronize_rcu();
4539
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004540unlock:
4541 mutex_unlock(&memcg->thresholds_lock);
4542
4543 return ret;
4544}
4545
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004546static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004547 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004548{
4549 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004550 struct mem_cgroup_thresholds *thresholds;
4551 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004552 int type = MEMFILE_TYPE(cft->private);
4553 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004554 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004555
4556 mutex_lock(&memcg->thresholds_lock);
4557 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004558 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004559 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004560 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004561 else
4562 BUG();
4563
4564 /*
4565 * Something went wrong if we trying to unregister a threshold
4566 * if we don't have thresholds
4567 */
4568 BUG_ON(!thresholds);
4569
4570 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4571
4572 /* Check if a threshold crossed before removing */
4573 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4574
4575 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004576 size = 0;
4577 for (i = 0; i < thresholds->primary->size; i++) {
4578 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004579 size++;
4580 }
4581
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004582 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004583
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004584 /* Set thresholds array to NULL if we don't have thresholds */
4585 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004586 kfree(new);
4587 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004588 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004589 }
4590
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004591 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004592
4593 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004594 new->current_threshold = -1;
4595 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
4596 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004597 continue;
4598
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004599 new->entries[j] = thresholds->primary->entries[i];
4600 if (new->entries[j].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004601 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004602 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004603 * until rcu_assign_pointer(), so it's safe to increment
4604 * it here.
4605 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004606 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004607 }
4608 j++;
4609 }
4610
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004611swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004612 /* Swap primary and spare array */
4613 thresholds->spare = thresholds->primary;
4614 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004615
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004616 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004617 synchronize_rcu();
4618
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004619 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004620}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004621
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004622static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
4623 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
4624{
4625 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4626 struct mem_cgroup_eventfd_list *event;
4627 int type = MEMFILE_TYPE(cft->private);
4628
4629 BUG_ON(type != _OOM_TYPE);
4630 event = kmalloc(sizeof(*event), GFP_KERNEL);
4631 if (!event)
4632 return -ENOMEM;
4633
Michal Hocko1af8efe2011-07-26 16:08:24 -07004634 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004635
4636 event->eventfd = eventfd;
4637 list_add(&event->list, &memcg->oom_notify);
4638
4639 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07004640 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004641 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07004642 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004643
4644 return 0;
4645}
4646
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004647static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004648 struct cftype *cft, struct eventfd_ctx *eventfd)
4649{
4650 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
4651 struct mem_cgroup_eventfd_list *ev, *tmp;
4652 int type = MEMFILE_TYPE(cft->private);
4653
4654 BUG_ON(type != _OOM_TYPE);
4655
Michal Hocko1af8efe2011-07-26 16:08:24 -07004656 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004657
4658 list_for_each_entry_safe(ev, tmp, &mem->oom_notify, list) {
4659 if (ev->eventfd == eventfd) {
4660 list_del(&ev->list);
4661 kfree(ev);
4662 }
4663 }
4664
Michal Hocko1af8efe2011-07-26 16:08:24 -07004665 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004666}
4667
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004668static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
4669 struct cftype *cft, struct cgroup_map_cb *cb)
4670{
4671 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
4672
4673 cb->fill(cb, "oom_kill_disable", mem->oom_kill_disable);
4674
Michal Hocko79dfdac2011-07-26 16:08:23 -07004675 if (atomic_read(&mem->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004676 cb->fill(cb, "under_oom", 1);
4677 else
4678 cb->fill(cb, "under_oom", 0);
4679 return 0;
4680}
4681
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004682static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
4683 struct cftype *cft, u64 val)
4684{
4685 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
4686 struct mem_cgroup *parent;
4687
4688 /* cannot set to root cgroup and only 0 and 1 are allowed */
4689 if (!cgrp->parent || !((val == 0) || (val == 1)))
4690 return -EINVAL;
4691
4692 parent = mem_cgroup_from_cont(cgrp->parent);
4693
4694 cgroup_lock();
4695 /* oom-kill-disable is a flag for subhierarchy. */
4696 if ((parent->use_hierarchy) ||
4697 (mem->use_hierarchy && !list_empty(&cgrp->children))) {
4698 cgroup_unlock();
4699 return -EINVAL;
4700 }
4701 mem->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07004702 if (!val)
4703 memcg_oom_recover(mem);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004704 cgroup_unlock();
4705 return 0;
4706}
4707
Ying Han406eb0c2011-05-26 16:25:37 -07004708#ifdef CONFIG_NUMA
4709static const struct file_operations mem_control_numa_stat_file_operations = {
4710 .read = seq_read,
4711 .llseek = seq_lseek,
4712 .release = single_release,
4713};
4714
4715static int mem_control_numa_stat_open(struct inode *unused, struct file *file)
4716{
4717 struct cgroup *cont = file->f_dentry->d_parent->d_fsdata;
4718
4719 file->f_op = &mem_control_numa_stat_file_operations;
4720 return single_open(file, mem_control_numa_stat_show, cont);
4721}
4722#endif /* CONFIG_NUMA */
4723
KAMEZAWA Hiroyuki82f9d482011-07-26 16:08:26 -07004724static int mem_cgroup_vmscan_stat_read(struct cgroup *cgrp,
4725 struct cftype *cft,
4726 struct cgroup_map_cb *cb)
4727{
4728 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
4729 char string[64];
4730 int i;
4731
4732 for (i = 0; i < NR_SCANSTATS; i++) {
4733 strcpy(string, scanstat_string[i]);
4734 strcat(string, SCANSTAT_WORD_LIMIT);
4735 cb->fill(cb, string, mem->scanstat.stats[SCAN_BY_LIMIT][i]);
4736 }
4737
4738 for (i = 0; i < NR_SCANSTATS; i++) {
4739 strcpy(string, scanstat_string[i]);
4740 strcat(string, SCANSTAT_WORD_SYSTEM);
4741 cb->fill(cb, string, mem->scanstat.stats[SCAN_BY_SYSTEM][i]);
4742 }
4743
4744 for (i = 0; i < NR_SCANSTATS; i++) {
4745 strcpy(string, scanstat_string[i]);
4746 strcat(string, SCANSTAT_WORD_LIMIT);
4747 strcat(string, SCANSTAT_WORD_HIERARCHY);
4748 cb->fill(cb, string, mem->scanstat.rootstats[SCAN_BY_LIMIT][i]);
4749 }
4750 for (i = 0; i < NR_SCANSTATS; i++) {
4751 strcpy(string, scanstat_string[i]);
4752 strcat(string, SCANSTAT_WORD_SYSTEM);
4753 strcat(string, SCANSTAT_WORD_HIERARCHY);
4754 cb->fill(cb, string, mem->scanstat.rootstats[SCAN_BY_SYSTEM][i]);
4755 }
4756 return 0;
4757}
4758
4759static int mem_cgroup_reset_vmscan_stat(struct cgroup *cgrp,
4760 unsigned int event)
4761{
4762 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
4763
4764 spin_lock(&mem->scanstat.lock);
4765 memset(&mem->scanstat.stats, 0, sizeof(mem->scanstat.stats));
4766 memset(&mem->scanstat.rootstats, 0, sizeof(mem->scanstat.rootstats));
4767 spin_unlock(&mem->scanstat.lock);
4768 return 0;
4769}
4770
4771
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004772static struct cftype mem_cgroup_files[] = {
4773 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004774 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004775 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07004776 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004777 .register_event = mem_cgroup_usage_register_event,
4778 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004779 },
4780 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004781 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004782 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004783 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004784 .read_u64 = mem_cgroup_read,
4785 },
4786 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004787 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004788 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07004789 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07004790 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004791 },
4792 {
Balbir Singh296c81d2009-09-23 15:56:36 -07004793 .name = "soft_limit_in_bytes",
4794 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
4795 .write_string = mem_cgroup_write,
4796 .read_u64 = mem_cgroup_read,
4797 },
4798 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004799 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004800 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004801 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07004802 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004803 },
Balbir Singh8697d332008-02-07 00:13:59 -08004804 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004805 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07004806 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004807 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004808 {
4809 .name = "force_empty",
4810 .trigger = mem_cgroup_force_empty_write,
4811 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08004812 {
4813 .name = "use_hierarchy",
4814 .write_u64 = mem_cgroup_hierarchy_write,
4815 .read_u64 = mem_cgroup_hierarchy_read,
4816 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004817 {
4818 .name = "swappiness",
4819 .read_u64 = mem_cgroup_swappiness_read,
4820 .write_u64 = mem_cgroup_swappiness_write,
4821 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004822 {
4823 .name = "move_charge_at_immigrate",
4824 .read_u64 = mem_cgroup_move_charge_read,
4825 .write_u64 = mem_cgroup_move_charge_write,
4826 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004827 {
4828 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004829 .read_map = mem_cgroup_oom_control_read,
4830 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004831 .register_event = mem_cgroup_oom_register_event,
4832 .unregister_event = mem_cgroup_oom_unregister_event,
4833 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
4834 },
Ying Han406eb0c2011-05-26 16:25:37 -07004835#ifdef CONFIG_NUMA
4836 {
4837 .name = "numa_stat",
4838 .open = mem_control_numa_stat_open,
KAMEZAWA Hiroyuki89577122011-06-15 15:08:41 -07004839 .mode = S_IRUGO,
Ying Han406eb0c2011-05-26 16:25:37 -07004840 },
4841#endif
KAMEZAWA Hiroyuki82f9d482011-07-26 16:08:26 -07004842 {
4843 .name = "vmscan_stat",
4844 .read_map = mem_cgroup_vmscan_stat_read,
4845 .trigger = mem_cgroup_reset_vmscan_stat,
4846 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004847};
4848
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004849#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4850static struct cftype memsw_cgroup_files[] = {
4851 {
4852 .name = "memsw.usage_in_bytes",
4853 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
4854 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004855 .register_event = mem_cgroup_usage_register_event,
4856 .unregister_event = mem_cgroup_usage_unregister_event,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004857 },
4858 {
4859 .name = "memsw.max_usage_in_bytes",
4860 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
4861 .trigger = mem_cgroup_reset,
4862 .read_u64 = mem_cgroup_read,
4863 },
4864 {
4865 .name = "memsw.limit_in_bytes",
4866 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
4867 .write_string = mem_cgroup_write,
4868 .read_u64 = mem_cgroup_read,
4869 },
4870 {
4871 .name = "memsw.failcnt",
4872 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
4873 .trigger = mem_cgroup_reset,
4874 .read_u64 = mem_cgroup_read,
4875 },
4876};
4877
4878static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
4879{
4880 if (!do_swap_account)
4881 return 0;
4882 return cgroup_add_files(cont, ss, memsw_cgroup_files,
4883 ARRAY_SIZE(memsw_cgroup_files));
4884};
4885#else
4886static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
4887{
4888 return 0;
4889}
4890#endif
4891
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004892static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
4893{
4894 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004895 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07004896 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004897 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004898 /*
4899 * This routine is called against possible nodes.
4900 * But it's BUG to call kmalloc() against offline node.
4901 *
4902 * TODO: this routine can waste much memory for nodes which will
4903 * never be onlined. It's better to use memory hotplug callback
4904 * function.
4905 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004906 if (!node_state(node, N_NORMAL_MEMORY))
4907 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08004908 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004909 if (!pn)
4910 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004911
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004912 mem->info.nodeinfo[node] = pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004913 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4914 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07004915 for_each_lru(l)
4916 INIT_LIST_HEAD(&mz->lists[l]);
Balbir Singhf64c3f52009-09-23 15:56:37 -07004917 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07004918 mz->on_tree = false;
4919 mz->mem = mem;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004920 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004921 return 0;
4922}
4923
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004924static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
4925{
4926 kfree(mem->info.nodeinfo[node]);
4927}
4928
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004929static struct mem_cgroup *mem_cgroup_alloc(void)
4930{
4931 struct mem_cgroup *mem;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004932 int size = sizeof(struct mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004933
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004934 /* Can be very big if MAX_NUMNODES is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08004935 if (size < PAGE_SIZE)
Jesper Juhl17295c82011-01-13 15:47:42 -08004936 mem = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004937 else
Jesper Juhl17295c82011-01-13 15:47:42 -08004938 mem = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004939
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07004940 if (!mem)
4941 return NULL;
4942
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004943 mem->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004944 if (!mem->stat)
4945 goto out_free;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07004946 spin_lock_init(&mem->pcp_counter_lock);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004947 return mem;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004948
4949out_free:
4950 if (size < PAGE_SIZE)
4951 kfree(mem);
4952 else
4953 vfree(mem);
4954 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004955}
4956
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004957/*
4958 * At destroying mem_cgroup, references from swap_cgroup can remain.
4959 * (scanning all at force_empty is too costly...)
4960 *
4961 * Instead of clearing all references at force_empty, we remember
4962 * the number of reference from swap_cgroup and free mem_cgroup when
4963 * it goes down to 0.
4964 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004965 * Removal of cgroup itself succeeds regardless of refs from swap.
4966 */
4967
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004968static void __mem_cgroup_free(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004969{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004970 int node;
4971
Balbir Singhf64c3f52009-09-23 15:56:37 -07004972 mem_cgroup_remove_from_trees(mem);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004973 free_css_id(&mem_cgroup_subsys, &mem->css);
4974
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004975 for_each_node_state(node, N_POSSIBLE)
4976 free_mem_cgroup_per_zone_info(mem, node);
4977
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004978 free_percpu(mem->stat);
4979 if (sizeof(struct mem_cgroup) < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004980 kfree(mem);
4981 else
4982 vfree(mem);
4983}
4984
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004985static void mem_cgroup_get(struct mem_cgroup *mem)
4986{
4987 atomic_inc(&mem->refcnt);
4988}
4989
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004990static void __mem_cgroup_put(struct mem_cgroup *mem, int count)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004991{
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004992 if (atomic_sub_and_test(count, &mem->refcnt)) {
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004993 struct mem_cgroup *parent = parent_mem_cgroup(mem);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004994 __mem_cgroup_free(mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004995 if (parent)
4996 mem_cgroup_put(parent);
4997 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004998}
4999
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005000static void mem_cgroup_put(struct mem_cgroup *mem)
5001{
5002 __mem_cgroup_put(mem, 1);
5003}
5004
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005005/*
5006 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
5007 */
5008static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
5009{
5010 if (!mem->res.parent)
5011 return NULL;
5012 return mem_cgroup_from_res_counter(mem->res.parent, res);
5013}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005014
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005015#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
5016static void __init enable_swap_cgroup(void)
5017{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08005018 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005019 do_swap_account = 1;
5020}
5021#else
5022static void __init enable_swap_cgroup(void)
5023{
5024}
5025#endif
5026
Balbir Singhf64c3f52009-09-23 15:56:37 -07005027static int mem_cgroup_soft_limit_tree_init(void)
5028{
5029 struct mem_cgroup_tree_per_node *rtpn;
5030 struct mem_cgroup_tree_per_zone *rtpz;
5031 int tmp, node, zone;
5032
5033 for_each_node_state(node, N_POSSIBLE) {
5034 tmp = node;
5035 if (!node_state(node, N_NORMAL_MEMORY))
5036 tmp = -1;
5037 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
5038 if (!rtpn)
5039 return 1;
5040
5041 soft_limit_tree.rb_tree_per_node[node] = rtpn;
5042
5043 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
5044 rtpz = &rtpn->rb_tree_per_zone[zone];
5045 rtpz->rb_root = RB_ROOT;
5046 spin_lock_init(&rtpz->lock);
5047 }
5048 }
5049 return 0;
5050}
5051
Li Zefan0eb253e2009-01-15 13:51:25 -08005052static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005053mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
5054{
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005055 struct mem_cgroup *mem, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005056 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005057 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005058
Jan Blunckc8dad2b2009-01-07 18:07:53 -08005059 mem = mem_cgroup_alloc();
5060 if (!mem)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005061 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08005062
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005063 for_each_node_state(node, N_POSSIBLE)
5064 if (alloc_mem_cgroup_per_zone_info(mem, node))
5065 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07005066
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005067 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005068 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08005069 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005070 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005071 parent = NULL;
Balbir Singh4b3bde42009-09-23 15:56:32 -07005072 root_mem_cgroup = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07005073 if (mem_cgroup_soft_limit_tree_init())
5074 goto free_out;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08005075 for_each_possible_cpu(cpu) {
5076 struct memcg_stock_pcp *stock =
5077 &per_cpu(memcg_stock, cpu);
5078 INIT_WORK(&stock->work, drain_local_stock);
5079 }
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07005080 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005081 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005082 parent = mem_cgroup_from_cont(cont->parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005083 mem->use_hierarchy = parent->use_hierarchy;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005084 mem->oom_kill_disable = parent->oom_kill_disable;
Balbir Singh18f59ea2009-01-07 18:08:07 -08005085 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005086
Balbir Singh18f59ea2009-01-07 18:08:07 -08005087 if (parent && parent->use_hierarchy) {
5088 res_counter_init(&mem->res, &parent->res);
5089 res_counter_init(&mem->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005090 /*
5091 * We increment refcnt of the parent to ensure that we can
5092 * safely access it on res_counter_charge/uncharge.
5093 * This refcnt will be decremented when freeing this
5094 * mem_cgroup(see mem_cgroup_put).
5095 */
5096 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005097 } else {
5098 res_counter_init(&mem->res, NULL);
5099 res_counter_init(&mem->memsw, NULL);
5100 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005101 mem->last_scanned_child = 0;
Ying Han889976d2011-05-26 16:25:33 -07005102 mem->last_scanned_node = MAX_NUMNODES;
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005103 INIT_LIST_HEAD(&mem->oom_notify);
Balbir Singh6d61ef42009-01-07 18:08:06 -08005104
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005105 if (parent)
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07005106 mem->swappiness = mem_cgroup_swappiness(parent);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08005107 atomic_set(&mem->refcnt, 1);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005108 mem->move_charge_at_immigrate = 0;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005109 mutex_init(&mem->thresholds_lock);
KAMEZAWA Hiroyuki82f9d482011-07-26 16:08:26 -07005110 spin_lock_init(&mem->scanstat.lock);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005111 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005112free_out:
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08005113 __mem_cgroup_free(mem);
Balbir Singh4b3bde42009-09-23 15:56:32 -07005114 root_mem_cgroup = NULL;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005115 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005116}
5117
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07005118static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005119 struct cgroup *cont)
5120{
5121 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07005122
5123 return mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005124}
5125
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005126static void mem_cgroup_destroy(struct cgroup_subsys *ss,
5127 struct cgroup *cont)
5128{
Daisuke Nishimurac268e992009-01-15 13:51:13 -08005129 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08005130
Daisuke Nishimurac268e992009-01-15 13:51:13 -08005131 mem_cgroup_put(mem);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005132}
5133
5134static int mem_cgroup_populate(struct cgroup_subsys *ss,
5135 struct cgroup *cont)
5136{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005137 int ret;
5138
5139 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
5140 ARRAY_SIZE(mem_cgroup_files));
5141
5142 if (!ret)
5143 ret = register_memsw_files(cont, ss);
5144 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005145}
5146
Daisuke Nishimura02491442010-03-10 15:22:17 -08005147#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005148/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005149#define PRECHARGE_COUNT_AT_ONCE 256
5150static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005151{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005152 int ret = 0;
5153 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005154 struct mem_cgroup *mem = mc.to;
5155
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005156 if (mem_cgroup_is_root(mem)) {
5157 mc.precharge += count;
5158 /* we don't need css_get for root */
5159 return ret;
5160 }
5161 /* try to charge at once */
5162 if (count > 1) {
5163 struct res_counter *dummy;
5164 /*
5165 * "mem" cannot be under rmdir() because we've already checked
5166 * by cgroup_lock_live_cgroup() that it is not removed and we
5167 * are still under the same cgroup_mutex. So we can postpone
5168 * css_get().
5169 */
5170 if (res_counter_charge(&mem->res, PAGE_SIZE * count, &dummy))
5171 goto one_by_one;
5172 if (do_swap_account && res_counter_charge(&mem->memsw,
5173 PAGE_SIZE * count, &dummy)) {
5174 res_counter_uncharge(&mem->res, PAGE_SIZE * count);
5175 goto one_by_one;
5176 }
5177 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005178 return ret;
5179 }
5180one_by_one:
5181 /* fall back to one by one charge */
5182 while (count--) {
5183 if (signal_pending(current)) {
5184 ret = -EINTR;
5185 break;
5186 }
5187 if (!batch_count--) {
5188 batch_count = PRECHARGE_COUNT_AT_ONCE;
5189 cond_resched();
5190 }
Johannes Weiner7ec99d62011-03-23 16:42:36 -07005191 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, 1, &mem, false);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005192 if (ret || !mem)
5193 /* mem_cgroup_clear_mc() will do uncharge later */
5194 return -ENOMEM;
5195 mc.precharge++;
5196 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005197 return ret;
5198}
5199
5200/**
5201 * is_target_pte_for_mc - check a pte whether it is valid for move charge
5202 * @vma: the vma the pte to be checked belongs
5203 * @addr: the address corresponding to the pte to be checked
5204 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08005205 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005206 *
5207 * Returns
5208 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
5209 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
5210 * move charge. if @target is not NULL, the page is stored in target->page
5211 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08005212 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
5213 * target for charge migration. if @target is not NULL, the entry is stored
5214 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005215 *
5216 * Called with pte lock held.
5217 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005218union mc_target {
5219 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005220 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005221};
5222
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005223enum mc_target_type {
5224 MC_TARGET_NONE, /* not used */
5225 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08005226 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005227};
5228
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005229static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
5230 unsigned long addr, pte_t ptent)
5231{
5232 struct page *page = vm_normal_page(vma, addr, ptent);
5233
5234 if (!page || !page_mapped(page))
5235 return NULL;
5236 if (PageAnon(page)) {
5237 /* we don't move shared anon */
5238 if (!move_anon() || page_mapcount(page) > 2)
5239 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005240 } else if (!move_file())
5241 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005242 return NULL;
5243 if (!get_page_unless_zero(page))
5244 return NULL;
5245
5246 return page;
5247}
5248
5249static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5250 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5251{
5252 int usage_count;
5253 struct page *page = NULL;
5254 swp_entry_t ent = pte_to_swp_entry(ptent);
5255
5256 if (!move_anon() || non_swap_entry(ent))
5257 return NULL;
5258 usage_count = mem_cgroup_count_swap_user(ent, &page);
5259 if (usage_count > 1) { /* we don't move shared anon */
5260 if (page)
5261 put_page(page);
5262 return NULL;
5263 }
5264 if (do_swap_account)
5265 entry->val = ent.val;
5266
5267 return page;
5268}
5269
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005270static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
5271 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5272{
5273 struct page *page = NULL;
5274 struct inode *inode;
5275 struct address_space *mapping;
5276 pgoff_t pgoff;
5277
5278 if (!vma->vm_file) /* anonymous vma */
5279 return NULL;
5280 if (!move_file())
5281 return NULL;
5282
5283 inode = vma->vm_file->f_path.dentry->d_inode;
5284 mapping = vma->vm_file->f_mapping;
5285 if (pte_none(ptent))
5286 pgoff = linear_page_index(vma, addr);
5287 else /* pte_file(ptent) is true */
5288 pgoff = pte_to_pgoff(ptent);
5289
5290 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005291 page = find_get_page(mapping, pgoff);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005292
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005293#ifdef CONFIG_SWAP
5294 /* shmem/tmpfs may report page out on swap: account for that too. */
5295 if (radix_tree_exceptional_entry(page)) {
5296 swp_entry_t swap = radix_to_swp_entry(page);
5297 if (do_swap_account)
5298 *entry = swap;
5299 page = find_get_page(&swapper_space, swap.val);
5300 }
5301#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005302 return page;
5303}
5304
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005305static int is_target_pte_for_mc(struct vm_area_struct *vma,
5306 unsigned long addr, pte_t ptent, union mc_target *target)
5307{
Daisuke Nishimura02491442010-03-10 15:22:17 -08005308 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005309 struct page_cgroup *pc;
5310 int ret = 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005311 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005312
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005313 if (pte_present(ptent))
5314 page = mc_handle_present_pte(vma, addr, ptent);
5315 else if (is_swap_pte(ptent))
5316 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005317 else if (pte_none(ptent) || pte_file(ptent))
5318 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005319
5320 if (!page && !ent.val)
Daisuke Nishimura02491442010-03-10 15:22:17 -08005321 return 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005322 if (page) {
5323 pc = lookup_page_cgroup(page);
5324 /*
5325 * Do only loose check w/o page_cgroup lock.
5326 * mem_cgroup_move_account() checks the pc is valid or not under
5327 * the lock.
5328 */
5329 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
5330 ret = MC_TARGET_PAGE;
5331 if (target)
5332 target->page = page;
5333 }
5334 if (!ret || !target)
5335 put_page(page);
5336 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005337 /* There is a swap entry and a page doesn't exist or isn't charged */
5338 if (ent.val && !ret &&
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005339 css_id(&mc.from->css) == lookup_swap_cgroup(ent)) {
5340 ret = MC_TARGET_SWAP;
5341 if (target)
5342 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005343 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005344 return ret;
5345}
5346
5347static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
5348 unsigned long addr, unsigned long end,
5349 struct mm_walk *walk)
5350{
5351 struct vm_area_struct *vma = walk->private;
5352 pte_t *pte;
5353 spinlock_t *ptl;
5354
Dave Hansen03319322011-03-22 16:32:56 -07005355 split_huge_page_pmd(walk->mm, pmd);
5356
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005357 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5358 for (; addr != end; pte++, addr += PAGE_SIZE)
5359 if (is_target_pte_for_mc(vma, addr, *pte, NULL))
5360 mc.precharge++; /* increment precharge temporarily */
5361 pte_unmap_unlock(pte - 1, ptl);
5362 cond_resched();
5363
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005364 return 0;
5365}
5366
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005367static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
5368{
5369 unsigned long precharge;
5370 struct vm_area_struct *vma;
5371
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005372 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005373 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5374 struct mm_walk mem_cgroup_count_precharge_walk = {
5375 .pmd_entry = mem_cgroup_count_precharge_pte_range,
5376 .mm = mm,
5377 .private = vma,
5378 };
5379 if (is_vm_hugetlb_page(vma))
5380 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005381 walk_page_range(vma->vm_start, vma->vm_end,
5382 &mem_cgroup_count_precharge_walk);
5383 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005384 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005385
5386 precharge = mc.precharge;
5387 mc.precharge = 0;
5388
5389 return precharge;
5390}
5391
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005392static int mem_cgroup_precharge_mc(struct mm_struct *mm)
5393{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005394 unsigned long precharge = mem_cgroup_count_precharge(mm);
5395
5396 VM_BUG_ON(mc.moving_task);
5397 mc.moving_task = current;
5398 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005399}
5400
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005401/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
5402static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005403{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005404 struct mem_cgroup *from = mc.from;
5405 struct mem_cgroup *to = mc.to;
5406
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005407 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005408 if (mc.precharge) {
5409 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
5410 mc.precharge = 0;
5411 }
5412 /*
5413 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
5414 * we must uncharge here.
5415 */
5416 if (mc.moved_charge) {
5417 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
5418 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005419 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005420 /* we must fixup refcnts and charges */
5421 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005422 /* uncharge swap account from the old cgroup */
5423 if (!mem_cgroup_is_root(mc.from))
5424 res_counter_uncharge(&mc.from->memsw,
5425 PAGE_SIZE * mc.moved_swap);
5426 __mem_cgroup_put(mc.from, mc.moved_swap);
5427
5428 if (!mem_cgroup_is_root(mc.to)) {
5429 /*
5430 * we charged both to->res and to->memsw, so we should
5431 * uncharge to->res.
5432 */
5433 res_counter_uncharge(&mc.to->res,
5434 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005435 }
5436 /* we've already done mem_cgroup_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005437 mc.moved_swap = 0;
5438 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005439 memcg_oom_recover(from);
5440 memcg_oom_recover(to);
5441 wake_up_all(&mc.waitq);
5442}
5443
5444static void mem_cgroup_clear_mc(void)
5445{
5446 struct mem_cgroup *from = mc.from;
5447
5448 /*
5449 * we must clear moving_task before waking up waiters at the end of
5450 * task migration.
5451 */
5452 mc.moving_task = NULL;
5453 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005454 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005455 mc.from = NULL;
5456 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005457 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005458 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005459}
5460
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005461static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
5462 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005463 struct task_struct *p)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005464{
5465 int ret = 0;
5466 struct mem_cgroup *mem = mem_cgroup_from_cont(cgroup);
5467
5468 if (mem->move_charge_at_immigrate) {
5469 struct mm_struct *mm;
5470 struct mem_cgroup *from = mem_cgroup_from_task(p);
5471
5472 VM_BUG_ON(from == mem);
5473
5474 mm = get_task_mm(p);
5475 if (!mm)
5476 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005477 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005478 if (mm->owner == p) {
5479 VM_BUG_ON(mc.from);
5480 VM_BUG_ON(mc.to);
5481 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005482 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005483 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005484 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005485 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005486 mc.from = from;
5487 mc.to = mem;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005488 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005489 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005490
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005491 ret = mem_cgroup_precharge_mc(mm);
5492 if (ret)
5493 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005494 }
5495 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005496 }
5497 return ret;
5498}
5499
5500static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
5501 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005502 struct task_struct *p)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005503{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005504 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005505}
5506
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005507static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
5508 unsigned long addr, unsigned long end,
5509 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005510{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005511 int ret = 0;
5512 struct vm_area_struct *vma = walk->private;
5513 pte_t *pte;
5514 spinlock_t *ptl;
5515
Dave Hansen03319322011-03-22 16:32:56 -07005516 split_huge_page_pmd(walk->mm, pmd);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005517retry:
5518 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5519 for (; addr != end; addr += PAGE_SIZE) {
5520 pte_t ptent = *(pte++);
5521 union mc_target target;
5522 int type;
5523 struct page *page;
5524 struct page_cgroup *pc;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005525 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005526
5527 if (!mc.precharge)
5528 break;
5529
5530 type = is_target_pte_for_mc(vma, addr, ptent, &target);
5531 switch (type) {
5532 case MC_TARGET_PAGE:
5533 page = target.page;
5534 if (isolate_lru_page(page))
5535 goto put;
5536 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07005537 if (!mem_cgroup_move_account(page, 1, pc,
5538 mc.from, mc.to, false)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005539 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005540 /* we uncharge from mc.from later. */
5541 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005542 }
5543 putback_lru_page(page);
5544put: /* is_target_pte_for_mc() gets the page */
5545 put_page(page);
5546 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005547 case MC_TARGET_SWAP:
5548 ent = target.ent;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005549 if (!mem_cgroup_move_swap_account(ent,
5550 mc.from, mc.to, false)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08005551 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005552 /* we fixup refcnts and charges later. */
5553 mc.moved_swap++;
5554 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08005555 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005556 default:
5557 break;
5558 }
5559 }
5560 pte_unmap_unlock(pte - 1, ptl);
5561 cond_resched();
5562
5563 if (addr != end) {
5564 /*
5565 * We have consumed all precharges we got in can_attach().
5566 * We try charge one by one, but don't do any additional
5567 * charges to mc.to if we have failed in charge once in attach()
5568 * phase.
5569 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005570 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005571 if (!ret)
5572 goto retry;
5573 }
5574
5575 return ret;
5576}
5577
5578static void mem_cgroup_move_charge(struct mm_struct *mm)
5579{
5580 struct vm_area_struct *vma;
5581
5582 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005583retry:
5584 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
5585 /*
5586 * Someone who are holding the mmap_sem might be waiting in
5587 * waitq. So we cancel all extra charges, wake up all waiters,
5588 * and retry. Because we cancel precharges, we might not be able
5589 * to move enough charges, but moving charge is a best-effort
5590 * feature anyway, so it wouldn't be a big problem.
5591 */
5592 __mem_cgroup_clear_mc();
5593 cond_resched();
5594 goto retry;
5595 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005596 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5597 int ret;
5598 struct mm_walk mem_cgroup_move_charge_walk = {
5599 .pmd_entry = mem_cgroup_move_charge_pte_range,
5600 .mm = mm,
5601 .private = vma,
5602 };
5603 if (is_vm_hugetlb_page(vma))
5604 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005605 ret = walk_page_range(vma->vm_start, vma->vm_end,
5606 &mem_cgroup_move_charge_walk);
5607 if (ret)
5608 /*
5609 * means we have consumed all precharges and failed in
5610 * doing additional charge. Just abandon here.
5611 */
5612 break;
5613 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005614 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005615}
5616
Balbir Singh67e465a2008-02-07 00:13:54 -08005617static void mem_cgroup_move_task(struct cgroup_subsys *ss,
5618 struct cgroup *cont,
5619 struct cgroup *old_cont,
Ben Blumf780bdb2011-05-26 16:25:19 -07005620 struct task_struct *p)
Balbir Singh67e465a2008-02-07 00:13:54 -08005621{
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005622 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005623
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005624 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005625 if (mc.to)
5626 mem_cgroup_move_charge(mm);
5627 put_swap_token(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005628 mmput(mm);
5629 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005630 if (mc.to)
5631 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08005632}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005633#else /* !CONFIG_MMU */
5634static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
5635 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005636 struct task_struct *p)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005637{
5638 return 0;
5639}
5640static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
5641 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005642 struct task_struct *p)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005643{
5644}
5645static void mem_cgroup_move_task(struct cgroup_subsys *ss,
5646 struct cgroup *cont,
5647 struct cgroup *old_cont,
Ben Blumf780bdb2011-05-26 16:25:19 -07005648 struct task_struct *p)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005649{
5650}
5651#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08005652
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005653struct cgroup_subsys mem_cgroup_subsys = {
5654 .name = "memory",
5655 .subsys_id = mem_cgroup_subsys_id,
5656 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005657 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005658 .destroy = mem_cgroup_destroy,
5659 .populate = mem_cgroup_populate,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005660 .can_attach = mem_cgroup_can_attach,
5661 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08005662 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005663 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005664 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005665};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005666
5667#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08005668static int __init enable_swap_account(char *s)
5669{
5670 /* consider enabled if no parameter or 1 is given */
Michal Hockoa2c89902011-05-24 17:12:50 -07005671 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08005672 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07005673 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08005674 really_do_swap_account = 0;
5675 return 1;
5676}
Michal Hockoa2c89902011-05-24 17:12:50 -07005677__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005678
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005679#endif