blob: 5f84d2351ddbe942706ed11a53c0574b71724627 [file] [log] [blame]
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001/* memcontrol.c - Memory Controller
2 *
3 * Copyright IBM Corporation, 2007
4 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
5 *
Pavel Emelianov78fb7462008-02-07 00:13:51 -08006 * Copyright 2007 OpenVZ SWsoft Inc
7 * Author: Pavel Emelianov <xemul@openvz.org>
8 *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08009 * Memory thresholds
10 * Copyright (C) 2009 Nokia Corporation
11 * Author: Kirill A. Shutemov
12 *
Balbir Singh8cdea7c2008-02-07 00:13:50 -080013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 */
23
24#include <linux/res_counter.h>
25#include <linux/memcontrol.h>
26#include <linux/cgroup.h>
Pavel Emelianov78fb7462008-02-07 00:13:51 -080027#include <linux/mm.h>
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -080028#include <linux/hugetlb.h>
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -080029#include <linux/pagemap.h>
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080030#include <linux/smp.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080031#include <linux/page-flags.h>
Balbir Singh66e17072008-02-07 00:13:56 -080032#include <linux/backing-dev.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080033#include <linux/bit_spinlock.h>
34#include <linux/rcupdate.h>
Balbir Singhe2224322009-04-02 16:57:39 -070035#include <linux/limits.h>
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -080036#include <linux/mutex.h>
Balbir Singhf64c3f52009-09-23 15:56:37 -070037#include <linux/rbtree.h>
Hugh Dickins072441e2011-06-27 16:18:02 -070038#include <linux/shmem_fs.h>
Balbir Singhb6ac57d2008-04-29 01:00:19 -070039#include <linux/slab.h>
Balbir Singh66e17072008-02-07 00:13:56 -080040#include <linux/swap.h>
Daisuke Nishimura02491442010-03-10 15:22:17 -080041#include <linux/swapops.h>
Balbir Singh66e17072008-02-07 00:13:56 -080042#include <linux/spinlock.h>
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -080043#include <linux/eventfd.h>
44#include <linux/sort.h>
Balbir Singh66e17072008-02-07 00:13:56 -080045#include <linux/fs.h>
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -080046#include <linux/seq_file.h>
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -070047#include <linux/vmalloc.h>
Christoph Lameterb69408e2008-10-18 20:26:14 -070048#include <linux/mm_inline.h>
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070049#include <linux/page_cgroup.h>
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -080050#include <linux/cpu.h>
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -070051#include <linux/oom.h>
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -080052#include "internal.h"
Balbir Singh8cdea7c2008-02-07 00:13:50 -080053
Balbir Singh8697d332008-02-07 00:13:59 -080054#include <asm/uaccess.h>
55
KOSAKI Motohirocc8e9702010-08-09 17:19:57 -070056#include <trace/events/vmscan.h>
57
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070058struct cgroup_subsys mem_cgroup_subsys __read_mostly;
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070059#define MEM_CGROUP_RECLAIM_RETRIES 5
Balbir Singh4b3bde42009-09-23 15:56:32 -070060struct mem_cgroup *root_mem_cgroup __read_mostly;
Balbir Singh8cdea7c2008-02-07 00:13:50 -080061
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080062#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
Li Zefan338c8432009-06-17 16:27:15 -070063/* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080064int do_swap_account __read_mostly;
Michal Hockoa42c3902010-11-24 12:57:08 -080065
66/* for remember boot option*/
67#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP_ENABLED
68static int really_do_swap_account __initdata = 1;
69#else
70static int really_do_swap_account __initdata = 0;
71#endif
72
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080073#else
74#define do_swap_account (0)
75#endif
76
77
Balbir Singh8cdea7c2008-02-07 00:13:50 -080078/*
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080079 * Statistics for memory cgroup.
80 */
81enum mem_cgroup_stat_index {
82 /*
83 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
84 */
85 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
Balbir Singhd69b0422009-06-17 16:26:34 -070086 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -080087 MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */
Balbir Singh0c3e73e2009-09-23 15:56:42 -070088 MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -070089 MEM_CGROUP_STAT_DATA, /* end of data requires synchronization */
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -070090 MEM_CGROUP_ON_MOVE, /* someone is moving account between groups */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080091 MEM_CGROUP_STAT_NSTATS,
92};
93
Johannes Weinere9f89742011-03-23 16:42:37 -070094enum mem_cgroup_events_index {
95 MEM_CGROUP_EVENTS_PGPGIN, /* # of pages paged in */
96 MEM_CGROUP_EVENTS_PGPGOUT, /* # of pages paged out */
97 MEM_CGROUP_EVENTS_COUNT, /* # of pages paged in/out */
Ying Han456f9982011-05-26 16:25:38 -070098 MEM_CGROUP_EVENTS_PGFAULT, /* # of page-faults */
99 MEM_CGROUP_EVENTS_PGMAJFAULT, /* # of major page-faults */
Johannes Weinere9f89742011-03-23 16:42:37 -0700100 MEM_CGROUP_EVENTS_NSTATS,
101};
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700102/*
103 * Per memcg event counter is incremented at every pagein/pageout. With THP,
104 * it will be incremated by the number of pages. This counter is used for
105 * for trigger some periodic events. This is straightforward and better
106 * than using jiffies etc. to handle periodic memcg event.
107 */
108enum mem_cgroup_events_target {
109 MEM_CGROUP_TARGET_THRESH,
110 MEM_CGROUP_TARGET_SOFTLIMIT,
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700111 MEM_CGROUP_TARGET_NUMAINFO,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700112 MEM_CGROUP_NTARGETS,
113};
114#define THRESHOLDS_EVENTS_TARGET (128)
115#define SOFTLIMIT_EVENTS_TARGET (1024)
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700116#define NUMAINFO_EVENTS_TARGET (1024)
Johannes Weinere9f89742011-03-23 16:42:37 -0700117
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800118struct mem_cgroup_stat_cpu {
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700119 long count[MEM_CGROUP_STAT_NSTATS];
Johannes Weinere9f89742011-03-23 16:42:37 -0700120 unsigned long events[MEM_CGROUP_EVENTS_NSTATS];
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700121 unsigned long targets[MEM_CGROUP_NTARGETS];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800122};
123
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800124/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800125 * per-zone information in memory controller.
126 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800127struct mem_cgroup_per_zone {
KAMEZAWA Hiroyuki072c56c2008-02-07 00:14:39 -0800128 /*
129 * spin_lock to protect the per cgroup LRU
130 */
Christoph Lameterb69408e2008-10-18 20:26:14 -0700131 struct list_head lists[NR_LRU_LISTS];
132 unsigned long count[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800133
134 struct zone_reclaim_stat reclaim_stat;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700135 struct rb_node tree_node; /* RB tree node */
136 unsigned long long usage_in_excess;/* Set to the value by which */
137 /* the soft limit is exceeded*/
138 bool on_tree;
Balbir Singh4e416952009-09-23 15:56:39 -0700139 struct mem_cgroup *mem; /* Back pointer, we cannot */
140 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800141};
142/* Macro for accessing counter */
143#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
144
145struct mem_cgroup_per_node {
146 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
147};
148
149struct mem_cgroup_lru_info {
150 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
151};
152
153/*
Balbir Singhf64c3f52009-09-23 15:56:37 -0700154 * Cgroups above their limits are maintained in a RB-Tree, independent of
155 * their hierarchy representation
156 */
157
158struct mem_cgroup_tree_per_zone {
159 struct rb_root rb_root;
160 spinlock_t lock;
161};
162
163struct mem_cgroup_tree_per_node {
164 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
165};
166
167struct mem_cgroup_tree {
168 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
169};
170
171static struct mem_cgroup_tree soft_limit_tree __read_mostly;
172
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800173struct mem_cgroup_threshold {
174 struct eventfd_ctx *eventfd;
175 u64 threshold;
176};
177
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700178/* For threshold */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800179struct mem_cgroup_threshold_ary {
180 /* An array index points to threshold just below usage. */
Phil Carmody5407a562010-05-26 14:42:42 -0700181 int current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800182 /* Size of entries[] */
183 unsigned int size;
184 /* Array of thresholds */
185 struct mem_cgroup_threshold entries[0];
186};
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700187
188struct mem_cgroup_thresholds {
189 /* Primary thresholds array */
190 struct mem_cgroup_threshold_ary *primary;
191 /*
192 * Spare threshold array.
193 * This is needed to make mem_cgroup_unregister_event() "never fail".
194 * It must be able to store at least primary->size - 1 entries.
195 */
196 struct mem_cgroup_threshold_ary *spare;
197};
198
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700199/* for OOM */
200struct mem_cgroup_eventfd_list {
201 struct list_head list;
202 struct eventfd_ctx *eventfd;
203};
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800204
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800205static void mem_cgroup_threshold(struct mem_cgroup *mem);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700206static void mem_cgroup_oom_notify(struct mem_cgroup *mem);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800207
KAMEZAWA Hiroyuki82f9d482011-07-26 16:08:26 -0700208enum {
209 SCAN_BY_LIMIT,
210 SCAN_BY_SYSTEM,
211 NR_SCAN_CONTEXT,
212 SCAN_BY_SHRINK, /* not recorded now */
213};
214
215enum {
216 SCAN,
217 SCAN_ANON,
218 SCAN_FILE,
219 ROTATE,
220 ROTATE_ANON,
221 ROTATE_FILE,
222 FREED,
223 FREED_ANON,
224 FREED_FILE,
225 ELAPSED,
226 NR_SCANSTATS,
227};
228
229struct scanstat {
230 spinlock_t lock;
231 unsigned long stats[NR_SCAN_CONTEXT][NR_SCANSTATS];
232 unsigned long rootstats[NR_SCAN_CONTEXT][NR_SCANSTATS];
233};
234
235const char *scanstat_string[NR_SCANSTATS] = {
236 "scanned_pages",
237 "scanned_anon_pages",
238 "scanned_file_pages",
239 "rotated_pages",
240 "rotated_anon_pages",
241 "rotated_file_pages",
242 "freed_pages",
243 "freed_anon_pages",
244 "freed_file_pages",
245 "elapsed_ns",
246};
247#define SCANSTAT_WORD_LIMIT "_by_limit"
248#define SCANSTAT_WORD_SYSTEM "_by_system"
249#define SCANSTAT_WORD_HIERARCHY "_under_hierarchy"
250
251
Balbir Singhf64c3f52009-09-23 15:56:37 -0700252/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800253 * The memory controller data structure. The memory controller controls both
254 * page cache and RSS per cgroup. We would eventually like to provide
255 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
256 * to help the administrator determine what knobs to tune.
257 *
258 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800259 * we hit the water mark. May be even add a low water mark, such that
260 * no reclaim occurs from a cgroup at it's low water mark, this is
261 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800262 */
263struct mem_cgroup {
264 struct cgroup_subsys_state css;
265 /*
266 * the counter to account for memory usage
267 */
268 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800269 /*
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800270 * the counter to account for mem+swap usage.
271 */
272 struct res_counter memsw;
273 /*
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800274 * Per cgroup active and inactive list, similar to the
275 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800276 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800277 struct mem_cgroup_lru_info info;
Balbir Singh6d61ef42009-01-07 18:08:06 -0800278 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200279 * While reclaiming in a hierarchy, we cache the last child we
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700280 * reclaimed from.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800281 */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700282 int last_scanned_child;
Ying Han889976d2011-05-26 16:25:33 -0700283 int last_scanned_node;
284#if MAX_NUMNODES > 1
285 nodemask_t scan_nodes;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700286 atomic_t numainfo_events;
287 atomic_t numainfo_updating;
Ying Han889976d2011-05-26 16:25:33 -0700288#endif
Balbir Singh18f59ea2009-01-07 18:08:07 -0800289 /*
290 * Should the accounting and control be hierarchical, per subtree?
291 */
292 bool use_hierarchy;
Michal Hocko79dfdac2011-07-26 16:08:23 -0700293
294 bool oom_lock;
295 atomic_t under_oom;
296
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800297 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800298
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -0700299 int swappiness;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -0700300 /* OOM-Killer disable */
301 int oom_kill_disable;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800302
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700303 /* set when res.limit == memsw.limit */
304 bool memsw_is_minimum;
305
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800306 /* protect arrays of thresholds */
307 struct mutex thresholds_lock;
308
309 /* thresholds for memory usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700310 struct mem_cgroup_thresholds thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700311
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800312 /* thresholds for mem+swap usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700313 struct mem_cgroup_thresholds memsw_thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700314
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700315 /* For oom notifier event fd */
316 struct list_head oom_notify;
KAMEZAWA Hiroyuki82f9d482011-07-26 16:08:26 -0700317 /* For recording LRU-scan statistics */
318 struct scanstat scanstat;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800319 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800320 * Should we move charges of a task when a task is moved into this
321 * mem_cgroup ? And what type of charges should we move ?
322 */
323 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800324 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800325 * percpu counter.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800326 */
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800327 struct mem_cgroup_stat_cpu *stat;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700328 /*
329 * used when a cpu is offlined or other synchronizations
330 * See mem_cgroup_read_stat().
331 */
332 struct mem_cgroup_stat_cpu nocpu_base;
333 spinlock_t pcp_counter_lock;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800334};
335
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800336/* Stuffs for move charges at task migration. */
337/*
338 * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a
339 * left-shifted bitmap of these types.
340 */
341enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800342 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700343 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800344 NR_MOVE_TYPE,
345};
346
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800347/* "mc" and its members are protected by cgroup_mutex */
348static struct move_charge_struct {
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -0800349 spinlock_t lock; /* for from, to */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800350 struct mem_cgroup *from;
351 struct mem_cgroup *to;
352 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800353 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800354 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800355 struct task_struct *moving_task; /* a task moving charges */
356 wait_queue_head_t waitq; /* a waitq for other context */
357} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700358 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800359 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
360};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800361
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700362static bool move_anon(void)
363{
364 return test_bit(MOVE_CHARGE_TYPE_ANON,
365 &mc.to->move_charge_at_immigrate);
366}
367
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700368static bool move_file(void)
369{
370 return test_bit(MOVE_CHARGE_TYPE_FILE,
371 &mc.to->move_charge_at_immigrate);
372}
373
Balbir Singh4e416952009-09-23 15:56:39 -0700374/*
375 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
376 * limit reclaim to prevent infinite loops, if they ever occur.
377 */
378#define MEM_CGROUP_MAX_RECLAIM_LOOPS (100)
379#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2)
380
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800381enum charge_type {
382 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
383 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700384 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700385 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800386 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700387 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700388 NR_CHARGE_TYPE,
389};
390
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800391/* for encoding cft->private value on file */
392#define _MEM (0)
393#define _MEMSWAP (1)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700394#define _OOM_TYPE (2)
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800395#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
396#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
397#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700398/* Used for OOM nofiier */
399#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800400
Balbir Singh75822b42009-09-23 15:56:38 -0700401/*
402 * Reclaim flags for mem_cgroup_hierarchical_reclaim
403 */
404#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
405#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
406#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
407#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
Balbir Singh4e416952009-09-23 15:56:39 -0700408#define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
409#define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
Balbir Singh75822b42009-09-23 15:56:38 -0700410
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800411static void mem_cgroup_get(struct mem_cgroup *mem);
412static void mem_cgroup_put(struct mem_cgroup *mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -0800413static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -0700414static void drain_all_stock_async(struct mem_cgroup *mem);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800415
Balbir Singhf64c3f52009-09-23 15:56:37 -0700416static struct mem_cgroup_per_zone *
417mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
418{
419 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
420}
421
Wu Fengguangd3242362009-12-16 12:19:59 +0100422struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *mem)
423{
424 return &mem->css;
425}
426
Balbir Singhf64c3f52009-09-23 15:56:37 -0700427static struct mem_cgroup_per_zone *
Johannes Weiner97a6c372011-03-23 16:42:27 -0700428page_cgroup_zoneinfo(struct mem_cgroup *mem, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700429{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700430 int nid = page_to_nid(page);
431 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700432
Balbir Singhf64c3f52009-09-23 15:56:37 -0700433 return mem_cgroup_zoneinfo(mem, nid, zid);
434}
435
436static struct mem_cgroup_tree_per_zone *
437soft_limit_tree_node_zone(int nid, int zid)
438{
439 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
440}
441
442static struct mem_cgroup_tree_per_zone *
443soft_limit_tree_from_page(struct page *page)
444{
445 int nid = page_to_nid(page);
446 int zid = page_zonenum(page);
447
448 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
449}
450
451static void
Balbir Singh4e416952009-09-23 15:56:39 -0700452__mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700453 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700454 struct mem_cgroup_tree_per_zone *mctz,
455 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700456{
457 struct rb_node **p = &mctz->rb_root.rb_node;
458 struct rb_node *parent = NULL;
459 struct mem_cgroup_per_zone *mz_node;
460
461 if (mz->on_tree)
462 return;
463
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700464 mz->usage_in_excess = new_usage_in_excess;
465 if (!mz->usage_in_excess)
466 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700467 while (*p) {
468 parent = *p;
469 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
470 tree_node);
471 if (mz->usage_in_excess < mz_node->usage_in_excess)
472 p = &(*p)->rb_left;
473 /*
474 * We can't avoid mem cgroups that are over their soft
475 * limit by the same amount
476 */
477 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
478 p = &(*p)->rb_right;
479 }
480 rb_link_node(&mz->tree_node, parent, p);
481 rb_insert_color(&mz->tree_node, &mctz->rb_root);
482 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700483}
484
485static void
486__mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
487 struct mem_cgroup_per_zone *mz,
488 struct mem_cgroup_tree_per_zone *mctz)
489{
490 if (!mz->on_tree)
491 return;
492 rb_erase(&mz->tree_node, &mctz->rb_root);
493 mz->on_tree = false;
494}
495
496static void
Balbir Singhf64c3f52009-09-23 15:56:37 -0700497mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
498 struct mem_cgroup_per_zone *mz,
499 struct mem_cgroup_tree_per_zone *mctz)
500{
501 spin_lock(&mctz->lock);
Balbir Singh4e416952009-09-23 15:56:39 -0700502 __mem_cgroup_remove_exceeded(mem, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700503 spin_unlock(&mctz->lock);
504}
505
Balbir Singhf64c3f52009-09-23 15:56:37 -0700506
507static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
508{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700509 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700510 struct mem_cgroup_per_zone *mz;
511 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700512 int nid = page_to_nid(page);
513 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700514 mctz = soft_limit_tree_from_page(page);
515
516 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700517 * Necessary to update all ancestors when hierarchy is used.
518 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700519 */
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700520 for (; mem; mem = parent_mem_cgroup(mem)) {
521 mz = mem_cgroup_zoneinfo(mem, nid, zid);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700522 excess = res_counter_soft_limit_excess(&mem->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700523 /*
524 * We have to update the tree if mz is on RB-tree or
525 * mem is over its softlimit.
526 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700527 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700528 spin_lock(&mctz->lock);
529 /* if on-tree, remove it */
530 if (mz->on_tree)
531 __mem_cgroup_remove_exceeded(mem, mz, mctz);
532 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700533 * Insert again. mz->usage_in_excess will be updated.
534 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700535 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700536 __mem_cgroup_insert_exceeded(mem, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700537 spin_unlock(&mctz->lock);
538 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700539 }
540}
541
542static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
543{
544 int node, zone;
545 struct mem_cgroup_per_zone *mz;
546 struct mem_cgroup_tree_per_zone *mctz;
547
548 for_each_node_state(node, N_POSSIBLE) {
549 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
550 mz = mem_cgroup_zoneinfo(mem, node, zone);
551 mctz = soft_limit_tree_node_zone(node, zone);
552 mem_cgroup_remove_exceeded(mem, mz, mctz);
553 }
554 }
555}
556
Balbir Singh4e416952009-09-23 15:56:39 -0700557static struct mem_cgroup_per_zone *
558__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
559{
560 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700561 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700562
563retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700564 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700565 rightmost = rb_last(&mctz->rb_root);
566 if (!rightmost)
567 goto done; /* Nothing to reclaim from */
568
569 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
570 /*
571 * Remove the node now but someone else can add it back,
572 * we will to add it back at the end of reclaim to its correct
573 * position in the tree.
574 */
575 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
576 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
577 !css_tryget(&mz->mem->css))
578 goto retry;
579done:
580 return mz;
581}
582
583static struct mem_cgroup_per_zone *
584mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
585{
586 struct mem_cgroup_per_zone *mz;
587
588 spin_lock(&mctz->lock);
589 mz = __mem_cgroup_largest_soft_limit_node(mctz);
590 spin_unlock(&mctz->lock);
591 return mz;
592}
593
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700594/*
595 * Implementation Note: reading percpu statistics for memcg.
596 *
597 * Both of vmstat[] and percpu_counter has threshold and do periodic
598 * synchronization to implement "quick" read. There are trade-off between
599 * reading cost and precision of value. Then, we may have a chance to implement
600 * a periodic synchronizion of counter in memcg's counter.
601 *
602 * But this _read() function is used for user interface now. The user accounts
603 * memory usage by memory cgroup and he _always_ requires exact value because
604 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
605 * have to visit all online cpus and make sum. So, for now, unnecessary
606 * synchronization is not implemented. (just implemented for cpu hotplug)
607 *
608 * If there are kernel internal actions which can make use of some not-exact
609 * value, and reading all cpu value can be performance bottleneck in some
610 * common workload, threashold and synchonization as vmstat[] should be
611 * implemented.
612 */
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700613static long mem_cgroup_read_stat(struct mem_cgroup *mem,
614 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800615{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700616 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800617 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800618
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700619 get_online_cpus();
620 for_each_online_cpu(cpu)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800621 val += per_cpu(mem->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700622#ifdef CONFIG_HOTPLUG_CPU
623 spin_lock(&mem->pcp_counter_lock);
624 val += mem->nocpu_base.count[idx];
625 spin_unlock(&mem->pcp_counter_lock);
626#endif
627 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800628 return val;
629}
630
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700631static void mem_cgroup_swap_statistics(struct mem_cgroup *mem,
632 bool charge)
633{
634 int val = (charge) ? 1 : -1;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800635 this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_SWAPOUT], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700636}
637
Ying Han456f9982011-05-26 16:25:38 -0700638void mem_cgroup_pgfault(struct mem_cgroup *mem, int val)
639{
640 this_cpu_add(mem->stat->events[MEM_CGROUP_EVENTS_PGFAULT], val);
641}
642
643void mem_cgroup_pgmajfault(struct mem_cgroup *mem, int val)
644{
645 this_cpu_add(mem->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT], val);
646}
647
Johannes Weinere9f89742011-03-23 16:42:37 -0700648static unsigned long mem_cgroup_read_events(struct mem_cgroup *mem,
649 enum mem_cgroup_events_index idx)
650{
651 unsigned long val = 0;
652 int cpu;
653
654 for_each_online_cpu(cpu)
655 val += per_cpu(mem->stat->events[idx], cpu);
656#ifdef CONFIG_HOTPLUG_CPU
657 spin_lock(&mem->pcp_counter_lock);
658 val += mem->nocpu_base.events[idx];
659 spin_unlock(&mem->pcp_counter_lock);
660#endif
661 return val;
662}
663
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700664static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800665 bool file, int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800666{
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800667 preempt_disable();
668
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800669 if (file)
670 __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_CACHE], nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800671 else
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800672 __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_RSS], nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700673
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800674 /* pagein of a big page is an event. So, ignore page size */
675 if (nr_pages > 0)
Johannes Weinere9f89742011-03-23 16:42:37 -0700676 __this_cpu_inc(mem->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800677 else {
Johannes Weinere9f89742011-03-23 16:42:37 -0700678 __this_cpu_inc(mem->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800679 nr_pages = -nr_pages; /* for event */
680 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800681
Johannes Weinere9f89742011-03-23 16:42:37 -0700682 __this_cpu_add(mem->stat->events[MEM_CGROUP_EVENTS_COUNT], nr_pages);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800683
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800684 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800685}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800686
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700687unsigned long
688mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *mem, int nid, int zid,
689 unsigned int lru_mask)
Ying Han889976d2011-05-26 16:25:33 -0700690{
691 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700692 enum lru_list l;
693 unsigned long ret = 0;
694
695 mz = mem_cgroup_zoneinfo(mem, nid, zid);
696
697 for_each_lru(l) {
698 if (BIT(l) & lru_mask)
699 ret += MEM_CGROUP_ZSTAT(mz, l);
700 }
701 return ret;
702}
703
704static unsigned long
705mem_cgroup_node_nr_lru_pages(struct mem_cgroup *mem,
706 int nid, unsigned int lru_mask)
707{
Ying Han889976d2011-05-26 16:25:33 -0700708 u64 total = 0;
709 int zid;
710
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700711 for (zid = 0; zid < MAX_NR_ZONES; zid++)
712 total += mem_cgroup_zone_nr_lru_pages(mem, nid, zid, lru_mask);
713
Ying Han889976d2011-05-26 16:25:33 -0700714 return total;
715}
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700716
717static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *mem,
718 unsigned int lru_mask)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800719{
Ying Han889976d2011-05-26 16:25:33 -0700720 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800721 u64 total = 0;
722
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700723 for_each_node_state(nid, N_HIGH_MEMORY)
724 total += mem_cgroup_node_nr_lru_pages(mem, nid, lru_mask);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800725 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800726}
727
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700728static bool __memcg_event_check(struct mem_cgroup *mem, int target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800729{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700730 unsigned long val, next;
731
732 val = this_cpu_read(mem->stat->events[MEM_CGROUP_EVENTS_COUNT]);
733 next = this_cpu_read(mem->stat->targets[target]);
734 /* from time_after() in jiffies.h */
735 return ((long)next - (long)val < 0);
736}
737
738static void __mem_cgroup_target_update(struct mem_cgroup *mem, int target)
739{
740 unsigned long val, next;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800741
Johannes Weinere9f89742011-03-23 16:42:37 -0700742 val = this_cpu_read(mem->stat->events[MEM_CGROUP_EVENTS_COUNT]);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800743
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700744 switch (target) {
745 case MEM_CGROUP_TARGET_THRESH:
746 next = val + THRESHOLDS_EVENTS_TARGET;
747 break;
748 case MEM_CGROUP_TARGET_SOFTLIMIT:
749 next = val + SOFTLIMIT_EVENTS_TARGET;
750 break;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700751 case MEM_CGROUP_TARGET_NUMAINFO:
752 next = val + NUMAINFO_EVENTS_TARGET;
753 break;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700754 default:
755 return;
756 }
757
758 this_cpu_write(mem->stat->targets[target], next);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800759}
760
761/*
762 * Check events in order.
763 *
764 */
765static void memcg_check_events(struct mem_cgroup *mem, struct page *page)
766{
767 /* threshold event is triggered in finer grain than soft limit */
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700768 if (unlikely(__memcg_event_check(mem, MEM_CGROUP_TARGET_THRESH))) {
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800769 mem_cgroup_threshold(mem);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700770 __mem_cgroup_target_update(mem, MEM_CGROUP_TARGET_THRESH);
771 if (unlikely(__memcg_event_check(mem,
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700772 MEM_CGROUP_TARGET_SOFTLIMIT))) {
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800773 mem_cgroup_update_tree(mem, page);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700774 __mem_cgroup_target_update(mem,
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700775 MEM_CGROUP_TARGET_SOFTLIMIT);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700776 }
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700777#if MAX_NUMNODES > 1
778 if (unlikely(__memcg_event_check(mem,
779 MEM_CGROUP_TARGET_NUMAINFO))) {
780 atomic_inc(&mem->numainfo_events);
781 __mem_cgroup_target_update(mem,
782 MEM_CGROUP_TARGET_NUMAINFO);
783 }
784#endif
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800785 }
786}
787
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800788static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800789{
790 return container_of(cgroup_subsys_state(cont,
791 mem_cgroup_subsys_id), struct mem_cgroup,
792 css);
793}
794
Balbir Singhcf475ad2008-04-29 01:00:16 -0700795struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800796{
Balbir Singh31a78f22008-09-28 23:09:31 +0100797 /*
798 * mm_update_next_owner() may clear mm->owner to NULL
799 * if it races with swapoff, page migration, etc.
800 * So this can be called with p == NULL.
801 */
802 if (unlikely(!p))
803 return NULL;
804
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800805 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
806 struct mem_cgroup, css);
807}
808
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700809struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800810{
811 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700812
813 if (!mm)
814 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800815 /*
816 * Because we have no locks, mm->owner's may be being moved to other
817 * cgroup. We use css_tryget() here even if this looks
818 * pessimistic (rather than adding locks here).
819 */
820 rcu_read_lock();
821 do {
822 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
823 if (unlikely(!mem))
824 break;
825 } while (!css_tryget(&mem->css));
826 rcu_read_unlock();
827 return mem;
828}
829
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700830/* The caller has to guarantee "mem" exists before calling this */
831static struct mem_cgroup *mem_cgroup_start_loop(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700832{
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700833 struct cgroup_subsys_state *css;
834 int found;
835
836 if (!mem) /* ROOT cgroup has the smallest ID */
837 return root_mem_cgroup; /*css_put/get against root is ignored*/
838 if (!mem->use_hierarchy) {
839 if (css_tryget(&mem->css))
840 return mem;
841 return NULL;
842 }
843 rcu_read_lock();
844 /*
845 * searching a memory cgroup which has the smallest ID under given
846 * ROOT cgroup. (ID >= 1)
847 */
848 css = css_get_next(&mem_cgroup_subsys, 1, &mem->css, &found);
849 if (css && css_tryget(css))
850 mem = container_of(css, struct mem_cgroup, css);
851 else
852 mem = NULL;
853 rcu_read_unlock();
854 return mem;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700855}
856
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700857static struct mem_cgroup *mem_cgroup_get_next(struct mem_cgroup *iter,
858 struct mem_cgroup *root,
859 bool cond)
860{
861 int nextid = css_id(&iter->css) + 1;
862 int found;
863 int hierarchy_used;
864 struct cgroup_subsys_state *css;
865
866 hierarchy_used = iter->use_hierarchy;
867
868 css_put(&iter->css);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700869 /* If no ROOT, walk all, ignore hierarchy */
870 if (!cond || (root && !hierarchy_used))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700871 return NULL;
872
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700873 if (!root)
874 root = root_mem_cgroup;
875
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700876 do {
877 iter = NULL;
878 rcu_read_lock();
879
880 css = css_get_next(&mem_cgroup_subsys, nextid,
881 &root->css, &found);
882 if (css && css_tryget(css))
883 iter = container_of(css, struct mem_cgroup, css);
884 rcu_read_unlock();
885 /* If css is NULL, no more cgroups will be found */
886 nextid = found + 1;
887 } while (css && !iter);
888
889 return iter;
890}
891/*
892 * for_eacn_mem_cgroup_tree() for visiting all cgroup under tree. Please
893 * be careful that "break" loop is not allowed. We have reference count.
894 * Instead of that modify "cond" to be false and "continue" to exit the loop.
895 */
896#define for_each_mem_cgroup_tree_cond(iter, root, cond) \
897 for (iter = mem_cgroup_start_loop(root);\
898 iter != NULL;\
899 iter = mem_cgroup_get_next(iter, root, cond))
900
901#define for_each_mem_cgroup_tree(iter, root) \
902 for_each_mem_cgroup_tree_cond(iter, root, true)
903
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700904#define for_each_mem_cgroup_all(iter) \
905 for_each_mem_cgroup_tree_cond(iter, NULL, true)
906
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700907
Balbir Singh4b3bde42009-09-23 15:56:32 -0700908static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
909{
910 return (mem == root_mem_cgroup);
911}
912
Ying Han456f9982011-05-26 16:25:38 -0700913void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
914{
915 struct mem_cgroup *mem;
916
917 if (!mm)
918 return;
919
920 rcu_read_lock();
921 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
922 if (unlikely(!mem))
923 goto out;
924
925 switch (idx) {
926 case PGMAJFAULT:
927 mem_cgroup_pgmajfault(mem, 1);
928 break;
929 case PGFAULT:
930 mem_cgroup_pgfault(mem, 1);
931 break;
932 default:
933 BUG();
934 }
935out:
936 rcu_read_unlock();
937}
938EXPORT_SYMBOL(mem_cgroup_count_vm_event);
939
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800940/*
941 * Following LRU functions are allowed to be used without PCG_LOCK.
942 * Operations are called by routine of global LRU independently from memcg.
943 * What we have to take care of here is validness of pc->mem_cgroup.
944 *
945 * Changes to pc->mem_cgroup happens when
946 * 1. charge
947 * 2. moving account
948 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
949 * It is added to LRU before charge.
950 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
951 * When moving account, the page is not on LRU. It's isolated.
952 */
953
954void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800955{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800956 struct page_cgroup *pc;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800957 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700958
Hirokazu Takahashif8d66542009-01-07 18:08:02 -0800959 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800960 return;
961 pc = lookup_page_cgroup(page);
962 /* can happen while we handle swapcache. */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700963 if (!TestClearPageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800964 return;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700965 VM_BUG_ON(!pc->mem_cgroup);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800966 /*
967 * We don't check PCG_USED bit. It's cleared when the "page" is finally
968 * removed from global LRU.
969 */
Johannes Weiner97a6c372011-03-23 16:42:27 -0700970 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -0800971 /* huge page split is done under lru_lock. so, we have no races. */
972 MEM_CGROUP_ZSTAT(mz, lru) -= 1 << compound_order(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700973 if (mem_cgroup_is_root(pc->mem_cgroup))
974 return;
975 VM_BUG_ON(list_empty(&pc->lru));
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800976 list_del_init(&pc->lru);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800977}
978
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800979void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800980{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800981 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800982}
983
Minchan Kim3f58a822011-03-22 16:32:53 -0700984/*
985 * Writeback is about to end against a page which has been marked for immediate
986 * reclaim. If it still appears to be reclaimable, move it to the tail of the
987 * inactive list.
988 */
989void mem_cgroup_rotate_reclaimable_page(struct page *page)
990{
991 struct mem_cgroup_per_zone *mz;
992 struct page_cgroup *pc;
993 enum lru_list lru = page_lru(page);
994
995 if (mem_cgroup_disabled())
996 return;
997
998 pc = lookup_page_cgroup(page);
999 /* unused or root page is not rotated. */
1000 if (!PageCgroupUsed(pc))
1001 return;
1002 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
1003 smp_rmb();
1004 if (mem_cgroup_is_root(pc->mem_cgroup))
1005 return;
Johannes Weiner97a6c372011-03-23 16:42:27 -07001006 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
Minchan Kim3f58a822011-03-22 16:32:53 -07001007 list_move_tail(&pc->lru, &mz->lists[lru]);
1008}
1009
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001010void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -08001011{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001012 struct mem_cgroup_per_zone *mz;
1013 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001014
Hirokazu Takahashif8d66542009-01-07 18:08:02 -08001015 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001016 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -07001017
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001018 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -07001019 /* unused or root page is not rotated. */
Johannes Weiner713735b2011-01-20 14:44:31 -08001020 if (!PageCgroupUsed(pc))
1021 return;
1022 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
1023 smp_rmb();
1024 if (mem_cgroup_is_root(pc->mem_cgroup))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001025 return;
Johannes Weiner97a6c372011-03-23 16:42:27 -07001026 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001027 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -08001028}
1029
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001030void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
1031{
1032 struct page_cgroup *pc;
1033 struct mem_cgroup_per_zone *mz;
1034
Hirokazu Takahashif8d66542009-01-07 18:08:02 -08001035 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001036 return;
1037 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -07001038 VM_BUG_ON(PageCgroupAcctLRU(pc));
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001039 if (!PageCgroupUsed(pc))
1040 return;
Johannes Weiner713735b2011-01-20 14:44:31 -08001041 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
1042 smp_rmb();
Johannes Weiner97a6c372011-03-23 16:42:27 -07001043 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08001044 /* huge page split is done under lru_lock. so, we have no races. */
1045 MEM_CGROUP_ZSTAT(mz, lru) += 1 << compound_order(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -07001046 SetPageCgroupAcctLRU(pc);
1047 if (mem_cgroup_is_root(pc->mem_cgroup))
1048 return;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001049 list_add(&pc->lru, &mz->lists[lru]);
1050}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001051
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001052/*
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001053 * At handling SwapCache and other FUSE stuff, pc->mem_cgroup may be changed
1054 * while it's linked to lru because the page may be reused after it's fully
1055 * uncharged. To handle that, unlink page_cgroup from LRU when charge it again.
1056 * It's done under lock_page and expected that zone->lru_lock isnever held.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001057 */
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001058static void mem_cgroup_lru_del_before_commit(struct page *page)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001059{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001060 unsigned long flags;
1061 struct zone *zone = page_zone(page);
1062 struct page_cgroup *pc = lookup_page_cgroup(page);
1063
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001064 /*
1065 * Doing this check without taking ->lru_lock seems wrong but this
1066 * is safe. Because if page_cgroup's USED bit is unset, the page
1067 * will not be added to any memcg's LRU. If page_cgroup's USED bit is
1068 * set, the commit after this will fail, anyway.
1069 * This all charge/uncharge is done under some mutual execustion.
1070 * So, we don't need to taking care of changes in USED bit.
1071 */
1072 if (likely(!PageLRU(page)))
1073 return;
1074
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001075 spin_lock_irqsave(&zone->lru_lock, flags);
1076 /*
1077 * Forget old LRU when this page_cgroup is *not* used. This Used bit
1078 * is guarded by lock_page() because the page is SwapCache.
1079 */
1080 if (!PageCgroupUsed(pc))
1081 mem_cgroup_del_lru_list(page, page_lru(page));
1082 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001083}
1084
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001085static void mem_cgroup_lru_add_after_commit(struct page *page)
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001086{
1087 unsigned long flags;
1088 struct zone *zone = page_zone(page);
1089 struct page_cgroup *pc = lookup_page_cgroup(page);
1090
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001091 /* taking care of that the page is added to LRU while we commit it */
1092 if (likely(!PageLRU(page)))
1093 return;
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001094 spin_lock_irqsave(&zone->lru_lock, flags);
1095 /* link when the page is linked to LRU but page_cgroup isn't */
Balbir Singh4b3bde42009-09-23 15:56:32 -07001096 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001097 mem_cgroup_add_lru_list(page, page_lru(page));
1098 spin_unlock_irqrestore(&zone->lru_lock, flags);
1099}
1100
1101
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001102void mem_cgroup_move_lists(struct page *page,
1103 enum lru_list from, enum lru_list to)
1104{
Hirokazu Takahashif8d66542009-01-07 18:08:02 -08001105 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001106 return;
1107 mem_cgroup_del_lru_list(page, from);
1108 mem_cgroup_add_lru_list(page, to);
1109}
1110
Michal Hocko3e920412011-07-26 16:08:29 -07001111/*
1112 * Checks whether given mem is same or in the root_mem's
1113 * hierarchy subtree
1114 */
1115static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_mem,
1116 struct mem_cgroup *mem)
1117{
1118 if (root_mem != mem) {
1119 return (root_mem->use_hierarchy &&
1120 css_is_ancestor(&mem->css, &root_mem->css));
1121 }
1122
1123 return true;
1124}
1125
David Rientjes4c4a2212008-02-07 00:14:06 -08001126int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
1127{
1128 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001129 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001130 struct task_struct *p;
David Rientjes4c4a2212008-02-07 00:14:06 -08001131
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001132 p = find_lock_task_mm(task);
1133 if (!p)
1134 return 0;
1135 curr = try_get_mem_cgroup_from_mm(p->mm);
1136 task_unlock(p);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001137 if (!curr)
1138 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001139 /*
1140 * We should check use_hierarchy of "mem" not "curr". Because checking
1141 * use_hierarchy of "curr" here make this function true if hierarchy is
1142 * enabled in "curr" and "curr" is a child of "mem" in *cgroup*
1143 * hierarchy(even if use_hierarchy is disabled in "mem").
1144 */
Michal Hocko3e920412011-07-26 16:08:29 -07001145 ret = mem_cgroup_same_or_subtree(mem, curr);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001146 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001147 return ret;
1148}
1149
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001150static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001151{
1152 unsigned long active;
1153 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001154 unsigned long gb;
1155 unsigned long inactive_ratio;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001156
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07001157 inactive = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_ANON));
1158 active = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_ANON));
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001159
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001160 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1161 if (gb)
1162 inactive_ratio = int_sqrt(10 * gb);
1163 else
1164 inactive_ratio = 1;
1165
1166 if (present_pages) {
1167 present_pages[0] = inactive;
1168 present_pages[1] = active;
1169 }
1170
1171 return inactive_ratio;
1172}
1173
1174int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
1175{
1176 unsigned long active;
1177 unsigned long inactive;
1178 unsigned long present_pages[2];
1179 unsigned long inactive_ratio;
1180
1181 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
1182
1183 inactive = present_pages[0];
1184 active = present_pages[1];
1185
1186 if (inactive * inactive_ratio < active)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001187 return 1;
1188
1189 return 0;
1190}
1191
Rik van Riel56e49d22009-06-16 15:32:28 -07001192int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
1193{
1194 unsigned long active;
1195 unsigned long inactive;
1196
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07001197 inactive = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_FILE));
1198 active = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_FILE));
Rik van Riel56e49d22009-06-16 15:32:28 -07001199
1200 return (active > inactive);
1201}
1202
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001203struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
1204 struct zone *zone)
1205{
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -07001206 int nid = zone_to_nid(zone);
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001207 int zid = zone_idx(zone);
1208 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
1209
1210 return &mz->reclaim_stat;
1211}
1212
1213struct zone_reclaim_stat *
1214mem_cgroup_get_reclaim_stat_from_page(struct page *page)
1215{
1216 struct page_cgroup *pc;
1217 struct mem_cgroup_per_zone *mz;
1218
1219 if (mem_cgroup_disabled())
1220 return NULL;
1221
1222 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -08001223 if (!PageCgroupUsed(pc))
1224 return NULL;
Johannes Weiner713735b2011-01-20 14:44:31 -08001225 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
1226 smp_rmb();
Johannes Weiner97a6c372011-03-23 16:42:27 -07001227 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001228 return &mz->reclaim_stat;
1229}
1230
Balbir Singh66e17072008-02-07 00:13:56 -08001231unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
1232 struct list_head *dst,
1233 unsigned long *scanned, int order,
1234 int mode, struct zone *z,
1235 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001236 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -08001237{
1238 unsigned long nr_taken = 0;
1239 struct page *page;
1240 unsigned long scan;
1241 LIST_HEAD(pc_list);
1242 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001243 struct page_cgroup *pc, *tmp;
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -07001244 int nid = zone_to_nid(z);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001245 int zid = zone_idx(z);
1246 struct mem_cgroup_per_zone *mz;
Johannes Weinerb7c46d12009-09-21 17:02:56 -07001247 int lru = LRU_FILE * file + active;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001248 int ret;
Balbir Singh66e17072008-02-07 00:13:56 -08001249
Balbir Singhcf475ad2008-04-29 01:00:16 -07001250 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001251 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001252 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -08001253
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001254 scan = 0;
1255 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -08001256 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001257 break;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001258
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001259 if (unlikely(!PageCgroupUsed(pc)))
1260 continue;
Johannes Weiner5564e882011-03-23 16:42:29 -07001261
Johannes Weiner6b3ae582011-03-23 16:42:30 -07001262 page = lookup_cgroup_page(pc);
Johannes Weiner5564e882011-03-23 16:42:29 -07001263
Hugh Dickins436c65412008-02-07 00:14:12 -08001264 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001265 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001266
Hugh Dickins436c65412008-02-07 00:14:12 -08001267 scan++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001268 ret = __isolate_lru_page(page, mode, file);
1269 switch (ret) {
1270 case 0:
Balbir Singh66e17072008-02-07 00:13:56 -08001271 list_move(&page->lru, dst);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001272 mem_cgroup_del_lru(page);
Rik van Riel2c888cf2011-01-13 15:47:13 -08001273 nr_taken += hpage_nr_pages(page);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001274 break;
1275 case -EBUSY:
1276 /* we don't affect global LRU but rotate in our LRU */
1277 mem_cgroup_rotate_lru_list(page, page_lru(page));
1278 break;
1279 default:
1280 break;
Balbir Singh66e17072008-02-07 00:13:56 -08001281 }
1282 }
1283
Balbir Singh66e17072008-02-07 00:13:56 -08001284 *scanned = scan;
KOSAKI Motohirocc8e9702010-08-09 17:19:57 -07001285
1286 trace_mm_vmscan_memcg_isolate(0, nr_to_scan, scan, nr_taken,
1287 0, 0, 0, mode);
1288
Balbir Singh66e17072008-02-07 00:13:56 -08001289 return nr_taken;
1290}
1291
Balbir Singh6d61ef42009-01-07 18:08:06 -08001292#define mem_cgroup_from_res_counter(counter, member) \
1293 container_of(counter, struct mem_cgroup, member)
1294
Johannes Weiner19942822011-02-01 15:52:43 -08001295/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001296 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
1297 * @mem: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001298 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001299 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001300 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001301 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001302static unsigned long mem_cgroup_margin(struct mem_cgroup *mem)
Johannes Weiner19942822011-02-01 15:52:43 -08001303{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001304 unsigned long long margin;
1305
1306 margin = res_counter_margin(&mem->res);
1307 if (do_swap_account)
1308 margin = min(margin, res_counter_margin(&mem->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001309 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001310}
1311
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001312int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001313{
1314 struct cgroup *cgrp = memcg->css.cgroup;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001315
1316 /* root ? */
1317 if (cgrp->parent == NULL)
1318 return vm_swappiness;
1319
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001320 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001321}
1322
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001323static void mem_cgroup_start_move(struct mem_cgroup *mem)
1324{
1325 int cpu;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001326
1327 get_online_cpus();
1328 spin_lock(&mem->pcp_counter_lock);
1329 for_each_online_cpu(cpu)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001330 per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) += 1;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001331 mem->nocpu_base.count[MEM_CGROUP_ON_MOVE] += 1;
1332 spin_unlock(&mem->pcp_counter_lock);
1333 put_online_cpus();
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001334
1335 synchronize_rcu();
1336}
1337
1338static void mem_cgroup_end_move(struct mem_cgroup *mem)
1339{
1340 int cpu;
1341
1342 if (!mem)
1343 return;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001344 get_online_cpus();
1345 spin_lock(&mem->pcp_counter_lock);
1346 for_each_online_cpu(cpu)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001347 per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) -= 1;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001348 mem->nocpu_base.count[MEM_CGROUP_ON_MOVE] -= 1;
1349 spin_unlock(&mem->pcp_counter_lock);
1350 put_online_cpus();
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001351}
1352/*
1353 * 2 routines for checking "mem" is under move_account() or not.
1354 *
1355 * mem_cgroup_stealed() - checking a cgroup is mc.from or not. This is used
1356 * for avoiding race in accounting. If true,
1357 * pc->mem_cgroup may be overwritten.
1358 *
1359 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1360 * under hierarchy of moving cgroups. This is for
1361 * waiting at hith-memory prressure caused by "move".
1362 */
1363
1364static bool mem_cgroup_stealed(struct mem_cgroup *mem)
1365{
1366 VM_BUG_ON(!rcu_read_lock_held());
1367 return this_cpu_read(mem->stat->count[MEM_CGROUP_ON_MOVE]) > 0;
1368}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001369
1370static bool mem_cgroup_under_move(struct mem_cgroup *mem)
1371{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001372 struct mem_cgroup *from;
1373 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001374 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001375 /*
1376 * Unlike task_move routines, we access mc.to, mc.from not under
1377 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1378 */
1379 spin_lock(&mc.lock);
1380 from = mc.from;
1381 to = mc.to;
1382 if (!from)
1383 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001384
1385 ret = mem_cgroup_same_or_subtree(mem, from)
1386 || mem_cgroup_same_or_subtree(mem, to);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001387unlock:
1388 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001389 return ret;
1390}
1391
1392static bool mem_cgroup_wait_acct_move(struct mem_cgroup *mem)
1393{
1394 if (mc.moving_task && current != mc.moving_task) {
1395 if (mem_cgroup_under_move(mem)) {
1396 DEFINE_WAIT(wait);
1397 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1398 /* moving charge context might have finished. */
1399 if (mc.moving_task)
1400 schedule();
1401 finish_wait(&mc.waitq, &wait);
1402 return true;
1403 }
1404 }
1405 return false;
1406}
1407
Balbir Singhe2224322009-04-02 16:57:39 -07001408/**
Kirill A. Shutemov6a6135b2010-03-10 15:22:25 -08001409 * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode.
Balbir Singhe2224322009-04-02 16:57:39 -07001410 * @memcg: The memory cgroup that went over limit
1411 * @p: Task that is going to be killed
1412 *
1413 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1414 * enabled
1415 */
1416void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1417{
1418 struct cgroup *task_cgrp;
1419 struct cgroup *mem_cgrp;
1420 /*
1421 * Need a buffer in BSS, can't rely on allocations. The code relies
1422 * on the assumption that OOM is serialized for memory controller.
1423 * If this assumption is broken, revisit this code.
1424 */
1425 static char memcg_name[PATH_MAX];
1426 int ret;
1427
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001428 if (!memcg || !p)
Balbir Singhe2224322009-04-02 16:57:39 -07001429 return;
1430
1431
1432 rcu_read_lock();
1433
1434 mem_cgrp = memcg->css.cgroup;
1435 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1436
1437 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1438 if (ret < 0) {
1439 /*
1440 * Unfortunately, we are unable to convert to a useful name
1441 * But we'll still print out the usage information
1442 */
1443 rcu_read_unlock();
1444 goto done;
1445 }
1446 rcu_read_unlock();
1447
1448 printk(KERN_INFO "Task in %s killed", memcg_name);
1449
1450 rcu_read_lock();
1451 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1452 if (ret < 0) {
1453 rcu_read_unlock();
1454 goto done;
1455 }
1456 rcu_read_unlock();
1457
1458 /*
1459 * Continues from above, so we don't need an KERN_ level
1460 */
1461 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1462done:
1463
1464 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1465 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1466 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1467 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1468 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1469 "failcnt %llu\n",
1470 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1471 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1472 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1473}
1474
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001475/*
1476 * This function returns the number of memcg under hierarchy tree. Returns
1477 * 1(self count) if no children.
1478 */
1479static int mem_cgroup_count_children(struct mem_cgroup *mem)
1480{
1481 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001482 struct mem_cgroup *iter;
1483
1484 for_each_mem_cgroup_tree(iter, mem)
1485 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001486 return num;
1487}
1488
Balbir Singh6d61ef42009-01-07 18:08:06 -08001489/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001490 * Return the memory (and swap, if configured) limit for a memcg.
1491 */
1492u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
1493{
1494 u64 limit;
1495 u64 memsw;
1496
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001497 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
1498 limit += total_swap_pages << PAGE_SHIFT;
1499
David Rientjesa63d83f2010-08-09 17:19:46 -07001500 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1501 /*
1502 * If memsw is finite and limits the amount of swap space available
1503 * to this memcg, return that limit.
1504 */
1505 return min(limit, memsw);
1506}
1507
1508/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001509 * Visit the first child (need not be the first child as per the ordering
1510 * of the cgroup list, since we track last_scanned_child) of @mem and use
1511 * that to reclaim free pages from.
1512 */
1513static struct mem_cgroup *
1514mem_cgroup_select_victim(struct mem_cgroup *root_mem)
1515{
1516 struct mem_cgroup *ret = NULL;
1517 struct cgroup_subsys_state *css;
1518 int nextid, found;
1519
1520 if (!root_mem->use_hierarchy) {
1521 css_get(&root_mem->css);
1522 ret = root_mem;
1523 }
1524
1525 while (!ret) {
1526 rcu_read_lock();
1527 nextid = root_mem->last_scanned_child + 1;
1528 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
1529 &found);
1530 if (css && css_tryget(css))
1531 ret = container_of(css, struct mem_cgroup, css);
1532
1533 rcu_read_unlock();
1534 /* Updates scanning parameter */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001535 if (!css) {
1536 /* this means start scan from ID:1 */
1537 root_mem->last_scanned_child = 0;
1538 } else
1539 root_mem->last_scanned_child = found;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001540 }
1541
1542 return ret;
1543}
1544
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001545/**
1546 * test_mem_cgroup_node_reclaimable
1547 * @mem: the target memcg
1548 * @nid: the node ID to be checked.
1549 * @noswap : specify true here if the user wants flle only information.
1550 *
1551 * This function returns whether the specified memcg contains any
1552 * reclaimable pages on a node. Returns true if there are any reclaimable
1553 * pages in the node.
1554 */
1555static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *mem,
1556 int nid, bool noswap)
1557{
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07001558 if (mem_cgroup_node_nr_lru_pages(mem, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001559 return true;
1560 if (noswap || !total_swap_pages)
1561 return false;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07001562 if (mem_cgroup_node_nr_lru_pages(mem, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001563 return true;
1564 return false;
1565
1566}
Ying Han889976d2011-05-26 16:25:33 -07001567#if MAX_NUMNODES > 1
1568
1569/*
1570 * Always updating the nodemask is not very good - even if we have an empty
1571 * list or the wrong list here, we can start from some node and traverse all
1572 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1573 *
1574 */
1575static void mem_cgroup_may_update_nodemask(struct mem_cgroup *mem)
1576{
1577 int nid;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001578 /*
1579 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1580 * pagein/pageout changes since the last update.
1581 */
1582 if (!atomic_read(&mem->numainfo_events))
1583 return;
1584 if (atomic_inc_return(&mem->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001585 return;
1586
Ying Han889976d2011-05-26 16:25:33 -07001587 /* make a nodemask where this memcg uses memory from */
1588 mem->scan_nodes = node_states[N_HIGH_MEMORY];
1589
1590 for_each_node_mask(nid, node_states[N_HIGH_MEMORY]) {
1591
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001592 if (!test_mem_cgroup_node_reclaimable(mem, nid, false))
1593 node_clear(nid, mem->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001594 }
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001595
1596 atomic_set(&mem->numainfo_events, 0);
1597 atomic_set(&mem->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001598}
1599
1600/*
1601 * Selecting a node where we start reclaim from. Because what we need is just
1602 * reducing usage counter, start from anywhere is O,K. Considering
1603 * memory reclaim from current node, there are pros. and cons.
1604 *
1605 * Freeing memory from current node means freeing memory from a node which
1606 * we'll use or we've used. So, it may make LRU bad. And if several threads
1607 * hit limits, it will see a contention on a node. But freeing from remote
1608 * node means more costs for memory reclaim because of memory latency.
1609 *
1610 * Now, we use round-robin. Better algorithm is welcomed.
1611 */
1612int mem_cgroup_select_victim_node(struct mem_cgroup *mem)
1613{
1614 int node;
1615
1616 mem_cgroup_may_update_nodemask(mem);
1617 node = mem->last_scanned_node;
1618
1619 node = next_node(node, mem->scan_nodes);
1620 if (node == MAX_NUMNODES)
1621 node = first_node(mem->scan_nodes);
1622 /*
1623 * We call this when we hit limit, not when pages are added to LRU.
1624 * No LRU may hold pages because all pages are UNEVICTABLE or
1625 * memcg is too small and all pages are not on LRU. In that case,
1626 * we use curret node.
1627 */
1628 if (unlikely(node == MAX_NUMNODES))
1629 node = numa_node_id();
1630
1631 mem->last_scanned_node = node;
1632 return node;
1633}
1634
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001635/*
1636 * Check all nodes whether it contains reclaimable pages or not.
1637 * For quick scan, we make use of scan_nodes. This will allow us to skip
1638 * unused nodes. But scan_nodes is lazily updated and may not cotain
1639 * enough new information. We need to do double check.
1640 */
1641bool mem_cgroup_reclaimable(struct mem_cgroup *mem, bool noswap)
1642{
1643 int nid;
1644
1645 /*
1646 * quick check...making use of scan_node.
1647 * We can skip unused nodes.
1648 */
1649 if (!nodes_empty(mem->scan_nodes)) {
1650 for (nid = first_node(mem->scan_nodes);
1651 nid < MAX_NUMNODES;
1652 nid = next_node(nid, mem->scan_nodes)) {
1653
1654 if (test_mem_cgroup_node_reclaimable(mem, nid, noswap))
1655 return true;
1656 }
1657 }
1658 /*
1659 * Check rest of nodes.
1660 */
1661 for_each_node_state(nid, N_HIGH_MEMORY) {
1662 if (node_isset(nid, mem->scan_nodes))
1663 continue;
1664 if (test_mem_cgroup_node_reclaimable(mem, nid, noswap))
1665 return true;
1666 }
1667 return false;
1668}
1669
Ying Han889976d2011-05-26 16:25:33 -07001670#else
1671int mem_cgroup_select_victim_node(struct mem_cgroup *mem)
1672{
1673 return 0;
1674}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001675
1676bool mem_cgroup_reclaimable(struct mem_cgroup *mem, bool noswap)
1677{
1678 return test_mem_cgroup_node_reclaimable(mem, 0, noswap);
1679}
Ying Han889976d2011-05-26 16:25:33 -07001680#endif
1681
KAMEZAWA Hiroyuki82f9d482011-07-26 16:08:26 -07001682static void __mem_cgroup_record_scanstat(unsigned long *stats,
1683 struct memcg_scanrecord *rec)
1684{
1685
1686 stats[SCAN] += rec->nr_scanned[0] + rec->nr_scanned[1];
1687 stats[SCAN_ANON] += rec->nr_scanned[0];
1688 stats[SCAN_FILE] += rec->nr_scanned[1];
1689
1690 stats[ROTATE] += rec->nr_rotated[0] + rec->nr_rotated[1];
1691 stats[ROTATE_ANON] += rec->nr_rotated[0];
1692 stats[ROTATE_FILE] += rec->nr_rotated[1];
1693
1694 stats[FREED] += rec->nr_freed[0] + rec->nr_freed[1];
1695 stats[FREED_ANON] += rec->nr_freed[0];
1696 stats[FREED_FILE] += rec->nr_freed[1];
1697
1698 stats[ELAPSED] += rec->elapsed;
1699}
1700
1701static void mem_cgroup_record_scanstat(struct memcg_scanrecord *rec)
1702{
1703 struct mem_cgroup *mem;
1704 int context = rec->context;
1705
1706 if (context >= NR_SCAN_CONTEXT)
1707 return;
1708
1709 mem = rec->mem;
1710 spin_lock(&mem->scanstat.lock);
1711 __mem_cgroup_record_scanstat(mem->scanstat.stats[context], rec);
1712 spin_unlock(&mem->scanstat.lock);
1713
1714 mem = rec->root;
1715 spin_lock(&mem->scanstat.lock);
1716 __mem_cgroup_record_scanstat(mem->scanstat.rootstats[context], rec);
1717 spin_unlock(&mem->scanstat.lock);
1718}
1719
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001720/*
1721 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1722 * we reclaimed from, so that we don't end up penalizing one child extensively
1723 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001724 *
1725 * root_mem is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001726 *
1727 * We give up and return to the caller when we visit root_mem twice.
1728 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001729 *
1730 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001731 */
1732static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
Balbir Singh4e416952009-09-23 15:56:39 -07001733 struct zone *zone,
Balbir Singh75822b42009-09-23 15:56:38 -07001734 gfp_t gfp_mask,
Ying Han0ae5e892011-05-26 16:25:25 -07001735 unsigned long reclaim_options,
1736 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001737{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001738 struct mem_cgroup *victim;
1739 int ret, total = 0;
1740 int loop = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001741 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1742 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
Balbir Singh4e416952009-09-23 15:56:39 -07001743 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
KAMEZAWA Hiroyuki82f9d482011-07-26 16:08:26 -07001744 struct memcg_scanrecord rec;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001745 unsigned long excess;
KAMEZAWA Hiroyuki82f9d482011-07-26 16:08:26 -07001746 unsigned long scanned;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001747
1748 excess = res_counter_soft_limit_excess(&root_mem->res) >> PAGE_SHIFT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001749
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001750 /* If memsw_is_minimum==1, swap-out is of-no-use. */
Daisuke Nishimura108b6a72011-07-26 16:08:25 -07001751 if (!check_soft && !shrink && root_mem->memsw_is_minimum)
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001752 noswap = true;
1753
KAMEZAWA Hiroyuki82f9d482011-07-26 16:08:26 -07001754 if (shrink)
1755 rec.context = SCAN_BY_SHRINK;
1756 else if (check_soft)
1757 rec.context = SCAN_BY_SYSTEM;
1758 else
1759 rec.context = SCAN_BY_LIMIT;
1760
1761 rec.root = root_mem;
1762
Balbir Singh4e416952009-09-23 15:56:39 -07001763 while (1) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001764 victim = mem_cgroup_select_victim(root_mem);
Balbir Singh4e416952009-09-23 15:56:39 -07001765 if (victim == root_mem) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001766 loop++;
KAMEZAWA Hiroyukifbc29a22011-06-15 15:08:46 -07001767 /*
1768 * We are not draining per cpu cached charges during
1769 * soft limit reclaim because global reclaim doesn't
1770 * care about charges. It tries to free some memory and
1771 * charges will not give any.
1772 */
1773 if (!check_soft && loop >= 1)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07001774 drain_all_stock_async(root_mem);
Balbir Singh4e416952009-09-23 15:56:39 -07001775 if (loop >= 2) {
1776 /*
1777 * If we have not been able to reclaim
1778 * anything, it might because there are
1779 * no reclaimable pages under this hierarchy
1780 */
1781 if (!check_soft || !total) {
1782 css_put(&victim->css);
1783 break;
1784 }
1785 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001786 * We want to do more targeted reclaim.
Balbir Singh4e416952009-09-23 15:56:39 -07001787 * excess >> 2 is not to excessive so as to
1788 * reclaim too much, nor too less that we keep
1789 * coming back to reclaim from this cgroup
1790 */
1791 if (total >= (excess >> 2) ||
1792 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1793 css_put(&victim->css);
1794 break;
1795 }
1796 }
1797 }
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001798 if (!mem_cgroup_reclaimable(victim, noswap)) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001799 /* this cgroup's local usage == 0 */
1800 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001801 continue;
1802 }
KAMEZAWA Hiroyuki82f9d482011-07-26 16:08:26 -07001803 rec.mem = victim;
1804 rec.nr_scanned[0] = 0;
1805 rec.nr_scanned[1] = 0;
1806 rec.nr_rotated[0] = 0;
1807 rec.nr_rotated[1] = 0;
1808 rec.nr_freed[0] = 0;
1809 rec.nr_freed[1] = 0;
1810 rec.elapsed = 0;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001811 /* we use swappiness of local cgroup */
Ying Han0ae5e892011-05-26 16:25:25 -07001812 if (check_soft) {
Balbir Singh4e416952009-09-23 15:56:39 -07001813 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
KAMEZAWA Hiroyuki82f9d482011-07-26 16:08:26 -07001814 noswap, zone, &rec, &scanned);
1815 *total_scanned += scanned;
Ying Han0ae5e892011-05-26 16:25:25 -07001816 } else
Balbir Singh4e416952009-09-23 15:56:39 -07001817 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
KAMEZAWA Hiroyuki82f9d482011-07-26 16:08:26 -07001818 noswap, &rec);
1819 mem_cgroup_record_scanstat(&rec);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001820 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001821 /*
1822 * At shrinking usage, we can't check we should stop here or
1823 * reclaim more. It's depends on callers. last_scanned_child
1824 * will work enough for keeping fairness under tree.
1825 */
1826 if (shrink)
1827 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001828 total += ret;
Balbir Singh4e416952009-09-23 15:56:39 -07001829 if (check_soft) {
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001830 if (!res_counter_soft_limit_excess(&root_mem->res))
Balbir Singh4e416952009-09-23 15:56:39 -07001831 return total;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001832 } else if (mem_cgroup_margin(root_mem))
Johannes Weiner4fd14eb2011-05-26 16:25:35 -07001833 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001834 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001835 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001836}
1837
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001838/*
1839 * Check OOM-Killer is already running under our hierarchy.
1840 * If someone is running, return false.
Michal Hocko1af8efe2011-07-26 16:08:24 -07001841 * Has to be called with memcg_oom_lock
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001842 */
1843static bool mem_cgroup_oom_lock(struct mem_cgroup *mem)
1844{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001845 int lock_count = -1;
1846 struct mem_cgroup *iter, *failed = NULL;
1847 bool cond = true;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001848
Michal Hocko79dfdac2011-07-26 16:08:23 -07001849 for_each_mem_cgroup_tree_cond(iter, mem, cond) {
1850 bool locked = iter->oom_lock;
1851
1852 iter->oom_lock = true;
1853 if (lock_count == -1)
1854 lock_count = iter->oom_lock;
1855 else if (lock_count != locked) {
1856 /*
1857 * this subtree of our hierarchy is already locked
1858 * so we cannot give a lock.
1859 */
1860 lock_count = 0;
1861 failed = iter;
1862 cond = false;
1863 }
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001864 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001865
Michal Hocko79dfdac2011-07-26 16:08:23 -07001866 if (!failed)
1867 goto done;
1868
1869 /*
1870 * OK, we failed to lock the whole subtree so we have to clean up
1871 * what we set up to the failing subtree
1872 */
1873 cond = true;
1874 for_each_mem_cgroup_tree_cond(iter, mem, cond) {
1875 if (iter == failed) {
1876 cond = false;
1877 continue;
1878 }
1879 iter->oom_lock = false;
1880 }
1881done:
1882 return lock_count;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001883}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001884
Michal Hocko79dfdac2011-07-26 16:08:23 -07001885/*
Michal Hocko1af8efe2011-07-26 16:08:24 -07001886 * Has to be called with memcg_oom_lock
Michal Hocko79dfdac2011-07-26 16:08:23 -07001887 */
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001888static int mem_cgroup_oom_unlock(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001889{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001890 struct mem_cgroup *iter;
1891
Michal Hocko79dfdac2011-07-26 16:08:23 -07001892 for_each_mem_cgroup_tree(iter, mem)
1893 iter->oom_lock = false;
1894 return 0;
1895}
1896
1897static void mem_cgroup_mark_under_oom(struct mem_cgroup *mem)
1898{
1899 struct mem_cgroup *iter;
1900
1901 for_each_mem_cgroup_tree(iter, mem)
1902 atomic_inc(&iter->under_oom);
1903}
1904
1905static void mem_cgroup_unmark_under_oom(struct mem_cgroup *mem)
1906{
1907 struct mem_cgroup *iter;
1908
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001909 /*
1910 * When a new child is created while the hierarchy is under oom,
1911 * mem_cgroup_oom_lock() may not be called. We have to use
1912 * atomic_add_unless() here.
1913 */
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001914 for_each_mem_cgroup_tree(iter, mem)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001915 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001916}
1917
Michal Hocko1af8efe2011-07-26 16:08:24 -07001918static DEFINE_SPINLOCK(memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001919static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1920
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001921struct oom_wait_info {
1922 struct mem_cgroup *mem;
1923 wait_queue_t wait;
1924};
1925
1926static int memcg_oom_wake_function(wait_queue_t *wait,
1927 unsigned mode, int sync, void *arg)
1928{
Michal Hocko3e920412011-07-26 16:08:29 -07001929 struct mem_cgroup *wake_mem = (struct mem_cgroup *)arg,
1930 *oom_wait_mem;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001931 struct oom_wait_info *oom_wait_info;
1932
1933 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Michal Hocko3e920412011-07-26 16:08:29 -07001934 oom_wait_mem = oom_wait_info->mem;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001935
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001936 /*
1937 * Both of oom_wait_info->mem and wake_mem are stable under us.
1938 * Then we can use css_is_ancestor without taking care of RCU.
1939 */
Michal Hocko3e920412011-07-26 16:08:29 -07001940 if (!mem_cgroup_same_or_subtree(oom_wait_mem, wake_mem)
1941 && !mem_cgroup_same_or_subtree(wake_mem, oom_wait_mem))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001942 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001943 return autoremove_wake_function(wait, mode, sync, arg);
1944}
1945
1946static void memcg_wakeup_oom(struct mem_cgroup *mem)
1947{
1948 /* for filtering, pass "mem" as argument. */
1949 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, mem);
1950}
1951
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001952static void memcg_oom_recover(struct mem_cgroup *mem)
1953{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001954 if (mem && atomic_read(&mem->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001955 memcg_wakeup_oom(mem);
1956}
1957
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001958/*
1959 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
1960 */
1961bool mem_cgroup_handle_oom(struct mem_cgroup *mem, gfp_t mask)
1962{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001963 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001964 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001965
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001966 owait.mem = mem;
1967 owait.wait.flags = 0;
1968 owait.wait.func = memcg_oom_wake_function;
1969 owait.wait.private = current;
1970 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001971 need_to_kill = true;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001972 mem_cgroup_mark_under_oom(mem);
1973
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001974 /* At first, try to OOM lock hierarchy under mem.*/
Michal Hocko1af8efe2011-07-26 16:08:24 -07001975 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001976 locked = mem_cgroup_oom_lock(mem);
1977 /*
1978 * Even if signal_pending(), we can't quit charge() loop without
1979 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
1980 * under OOM is always welcomed, use TASK_KILLABLE here.
1981 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001982 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
1983 if (!locked || mem->oom_kill_disable)
1984 need_to_kill = false;
1985 if (locked)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07001986 mem_cgroup_oom_notify(mem);
Michal Hocko1af8efe2011-07-26 16:08:24 -07001987 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001988
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001989 if (need_to_kill) {
1990 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001991 mem_cgroup_out_of_memory(mem, mask);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001992 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001993 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001994 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001995 }
Michal Hocko1af8efe2011-07-26 16:08:24 -07001996 spin_lock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001997 if (locked)
1998 mem_cgroup_oom_unlock(mem);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001999 memcg_wakeup_oom(mem);
Michal Hocko1af8efe2011-07-26 16:08:24 -07002000 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002001
Michal Hocko79dfdac2011-07-26 16:08:23 -07002002 mem_cgroup_unmark_under_oom(mem);
2003
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002004 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
2005 return false;
2006 /* Give chance to dying process */
2007 schedule_timeout(1);
2008 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002009}
2010
Balbir Singhd69b0422009-06-17 16:26:34 -07002011/*
2012 * Currently used to update mapped file statistics, but the routine can be
2013 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002014 *
2015 * Notes: Race condition
2016 *
2017 * We usually use page_cgroup_lock() for accessing page_cgroup member but
2018 * it tends to be costly. But considering some conditions, we doesn't need
2019 * to do so _always_.
2020 *
2021 * Considering "charge", lock_page_cgroup() is not required because all
2022 * file-stat operations happen after a page is attached to radix-tree. There
2023 * are no race with "charge".
2024 *
2025 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
2026 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
2027 * if there are race with "uncharge". Statistics itself is properly handled
2028 * by flags.
2029 *
2030 * Considering "move", this is an only case we see a race. To make the race
2031 * small, we check MEM_CGROUP_ON_MOVE percpu value and detect there are
2032 * possibility of race condition. If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07002033 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002034
Greg Thelen2a7106f2011-01-13 15:47:37 -08002035void mem_cgroup_update_page_stat(struct page *page,
2036 enum mem_cgroup_page_stat_item idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07002037{
2038 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002039 struct page_cgroup *pc = lookup_page_cgroup(page);
2040 bool need_unlock = false;
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08002041 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07002042
Balbir Singhd69b0422009-06-17 16:26:34 -07002043 if (unlikely(!pc))
2044 return;
2045
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002046 rcu_read_lock();
Balbir Singhd69b0422009-06-17 16:26:34 -07002047 mem = pc->mem_cgroup;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002048 if (unlikely(!mem || !PageCgroupUsed(pc)))
2049 goto out;
2050 /* pc->mem_cgroup is unstable ? */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002051 if (unlikely(mem_cgroup_stealed(mem)) || PageTransHuge(page)) {
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002052 /* take a lock against to access pc->mem_cgroup */
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08002053 move_lock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002054 need_unlock = true;
2055 mem = pc->mem_cgroup;
2056 if (!mem || !PageCgroupUsed(pc))
2057 goto out;
2058 }
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002059
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002060 switch (idx) {
Greg Thelen2a7106f2011-01-13 15:47:37 -08002061 case MEMCG_NR_FILE_MAPPED:
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002062 if (val > 0)
2063 SetPageCgroupFileMapped(pc);
2064 else if (!page_mapped(page))
KAMEZAWA Hiroyuki0c270f82010-10-27 15:33:39 -07002065 ClearPageCgroupFileMapped(pc);
Greg Thelen2a7106f2011-01-13 15:47:37 -08002066 idx = MEM_CGROUP_STAT_FILE_MAPPED;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002067 break;
2068 default:
2069 BUG();
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002070 }
Balbir Singhd69b0422009-06-17 16:26:34 -07002071
Greg Thelen2a7106f2011-01-13 15:47:37 -08002072 this_cpu_add(mem->stat->count[idx], val);
2073
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002074out:
2075 if (unlikely(need_unlock))
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08002076 move_unlock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002077 rcu_read_unlock();
2078 return;
Balbir Singhd69b0422009-06-17 16:26:34 -07002079}
Greg Thelen2a7106f2011-01-13 15:47:37 -08002080EXPORT_SYMBOL(mem_cgroup_update_page_stat);
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002081
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002082/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002083 * size of first charge trial. "32" comes from vmscan.c's magic value.
2084 * TODO: maybe necessary to use big numbers in big irons.
2085 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002086#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002087struct memcg_stock_pcp {
2088 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002089 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002090 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002091 unsigned long flags;
2092#define FLUSHING_CACHED_CHARGE (0)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002093};
2094static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
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 Hocko8521fc52011-07-26 16:08:29 -07002201 if (mem_cgroup_same_or_subtree(root_mem, stock->cached) &&
2202 test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002203 flush_work(&stock->work);
2204 }
2205out:
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002206 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002207}
2208
2209/*
2210 * Tries to drain stocked charges in other cpus. This function is asynchronous
2211 * and just put a work per cpu for draining localy on each cpu. Caller can
2212 * expects some charges will be back to res_counter later but cannot wait for
2213 * it.
2214 */
2215static void drain_all_stock_async(struct mem_cgroup *root_mem)
2216{
Michal Hockod38144b2011-07-26 16:08:28 -07002217 drain_all_stock(root_mem, false);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002218}
2219
2220/* This is a synchronous drain interface. */
Michal Hockod38144b2011-07-26 16:08:28 -07002221static void drain_all_stock_sync(struct mem_cgroup *root_mem)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002222{
2223 /* called when force_empty is called */
Michal Hockod38144b2011-07-26 16:08:28 -07002224 drain_all_stock(root_mem, true);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002225}
2226
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002227/*
2228 * This function drains percpu counter value from DEAD cpu and
2229 * move it to local cpu. Note that this function can be preempted.
2230 */
2231static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *mem, int cpu)
2232{
2233 int i;
2234
2235 spin_lock(&mem->pcp_counter_lock);
2236 for (i = 0; i < MEM_CGROUP_STAT_DATA; i++) {
Johannes Weiner7a159cc2011-03-23 16:42:38 -07002237 long x = per_cpu(mem->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002238
2239 per_cpu(mem->stat->count[i], cpu) = 0;
2240 mem->nocpu_base.count[i] += x;
2241 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002242 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
2243 unsigned long x = per_cpu(mem->stat->events[i], cpu);
2244
2245 per_cpu(mem->stat->events[i], cpu) = 0;
2246 mem->nocpu_base.events[i] += x;
2247 }
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002248 /* need to clear ON_MOVE value, works as a kind of lock. */
2249 per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) = 0;
2250 spin_unlock(&mem->pcp_counter_lock);
2251}
2252
2253static void synchronize_mem_cgroup_on_move(struct mem_cgroup *mem, int cpu)
2254{
2255 int idx = MEM_CGROUP_ON_MOVE;
2256
2257 spin_lock(&mem->pcp_counter_lock);
2258 per_cpu(mem->stat->count[idx], cpu) = mem->nocpu_base.count[idx];
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002259 spin_unlock(&mem->pcp_counter_lock);
2260}
2261
2262static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002263 unsigned long action,
2264 void *hcpu)
2265{
2266 int cpu = (unsigned long)hcpu;
2267 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002268 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002269
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002270 if ((action == CPU_ONLINE)) {
2271 for_each_mem_cgroup_all(iter)
2272 synchronize_mem_cgroup_on_move(iter, cpu);
2273 return NOTIFY_OK;
2274 }
2275
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002276 if ((action != CPU_DEAD) || action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002277 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002278
2279 for_each_mem_cgroup_all(iter)
2280 mem_cgroup_drain_pcp_counter(iter, cpu);
2281
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002282 stock = &per_cpu(memcg_stock, cpu);
2283 drain_stock(stock);
2284 return NOTIFY_OK;
2285}
2286
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002287
2288/* See __mem_cgroup_try_charge() for details */
2289enum {
2290 CHARGE_OK, /* success */
2291 CHARGE_RETRY, /* need to retry but retry is not bad */
2292 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2293 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
2294 CHARGE_OOM_DIE, /* the current is killed because of OOM */
2295};
2296
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002297static int mem_cgroup_do_charge(struct mem_cgroup *mem, gfp_t gfp_mask,
2298 unsigned int nr_pages, bool oom_check)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002299{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002300 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002301 struct mem_cgroup *mem_over_limit;
2302 struct res_counter *fail_res;
2303 unsigned long flags = 0;
2304 int ret;
2305
2306 ret = res_counter_charge(&mem->res, csize, &fail_res);
2307
2308 if (likely(!ret)) {
2309 if (!do_swap_account)
2310 return CHARGE_OK;
2311 ret = res_counter_charge(&mem->memsw, csize, &fail_res);
2312 if (likely(!ret))
2313 return CHARGE_OK;
2314
KAMEZAWA Hiroyuki01c88e22011-01-25 15:07:27 -08002315 res_counter_uncharge(&mem->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002316 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2317 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2318 } else
2319 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002320 /*
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002321 * nr_pages can be either a huge page (HPAGE_PMD_NR), a batch
2322 * of regular pages (CHARGE_BATCH), or a single regular page (1).
Johannes Weiner9221edb2011-02-01 15:52:42 -08002323 *
2324 * Never reclaim on behalf of optional batching, retry with a
2325 * single page instead.
2326 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002327 if (nr_pages == CHARGE_BATCH)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002328 return CHARGE_RETRY;
2329
2330 if (!(gfp_mask & __GFP_WAIT))
2331 return CHARGE_WOULDBLOCK;
2332
2333 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
Ying Han0ae5e892011-05-26 16:25:25 -07002334 gfp_mask, flags, NULL);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002335 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002336 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002337 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002338 * Even though the limit is exceeded at this point, reclaim
2339 * may have been able to free some pages. Retry the charge
2340 * before killing the task.
2341 *
2342 * Only for regular pages, though: huge pages are rather
2343 * unlikely to succeed so close to the limit, and we fall back
2344 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002345 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002346 if (nr_pages == 1 && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002347 return CHARGE_RETRY;
2348
2349 /*
2350 * At task move, charge accounts can be doubly counted. So, it's
2351 * better to wait until the end of task_move if something is going on.
2352 */
2353 if (mem_cgroup_wait_acct_move(mem_over_limit))
2354 return CHARGE_RETRY;
2355
2356 /* If we don't need to call oom-killer at el, return immediately */
2357 if (!oom_check)
2358 return CHARGE_NOMEM;
2359 /* check OOM */
2360 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask))
2361 return CHARGE_OOM_DIE;
2362
2363 return CHARGE_RETRY;
2364}
2365
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002366/*
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002367 * Unlike exported interface, "oom" parameter is added. if oom==true,
2368 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002369 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002370static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002371 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002372 unsigned int nr_pages,
2373 struct mem_cgroup **memcg,
2374 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002375{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002376 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002377 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2378 struct mem_cgroup *mem = NULL;
2379 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002380
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002381 /*
2382 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2383 * in system level. So, allow to go ahead dying process in addition to
2384 * MEMDIE process.
2385 */
2386 if (unlikely(test_thread_flag(TIF_MEMDIE)
2387 || fatal_signal_pending(current)))
2388 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002389
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002390 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08002391 * We always charge the cgroup the mm_struct belongs to.
2392 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002393 * thread group leader migrates. It's possible that mm is not
2394 * set, if so charge the init_mm (happens for pagecache usage).
2395 */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002396 if (!*memcg && !mm)
2397 goto bypass;
2398again:
2399 if (*memcg) { /* css should be a valid one */
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002400 mem = *memcg;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002401 VM_BUG_ON(css_is_removed(&mem->css));
2402 if (mem_cgroup_is_root(mem))
2403 goto done;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002404 if (nr_pages == 1 && consume_stock(mem))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002405 goto done;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002406 css_get(&mem->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002407 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002408 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002409
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002410 rcu_read_lock();
2411 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002412 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002413 * Because we don't have task_lock(), "p" can exit.
2414 * In that case, "mem" can point to root or p can be NULL with
2415 * race with swapoff. Then, we have small risk of mis-accouning.
2416 * But such kind of mis-account by race always happens because
2417 * we don't have cgroup_mutex(). It's overkill and we allo that
2418 * small race, here.
2419 * (*) swapoff at el will charge against mm-struct not against
2420 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002421 */
2422 mem = mem_cgroup_from_task(p);
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002423 if (!mem || mem_cgroup_is_root(mem)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002424 rcu_read_unlock();
2425 goto done;
2426 }
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002427 if (nr_pages == 1 && consume_stock(mem)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002428 /*
2429 * It seems dagerous to access memcg without css_get().
2430 * But considering how consume_stok works, it's not
2431 * necessary. If consume_stock success, some charges
2432 * from this memcg are cached on this cpu. So, we
2433 * don't need to call css_get()/css_tryget() before
2434 * calling consume_stock().
2435 */
2436 rcu_read_unlock();
2437 goto done;
2438 }
2439 /* after here, we may be blocked. we need to get refcnt */
2440 if (!css_tryget(&mem->css)) {
2441 rcu_read_unlock();
2442 goto again;
2443 }
2444 rcu_read_unlock();
2445 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002446
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002447 do {
2448 bool oom_check;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002449
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002450 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002451 if (fatal_signal_pending(current)) {
2452 css_put(&mem->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002453 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002454 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002455
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002456 oom_check = false;
2457 if (oom && !nr_oom_retries) {
2458 oom_check = true;
2459 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2460 }
Balbir Singh6d61ef42009-01-07 18:08:06 -08002461
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002462 ret = mem_cgroup_do_charge(mem, gfp_mask, batch, oom_check);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002463 switch (ret) {
2464 case CHARGE_OK:
2465 break;
2466 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002467 batch = nr_pages;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002468 css_put(&mem->css);
2469 mem = NULL;
2470 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002471 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002472 css_put(&mem->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002473 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002474 case CHARGE_NOMEM: /* OOM routine works */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002475 if (!oom) {
2476 css_put(&mem->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002477 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002478 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002479 /* If oom, we never return -ENOMEM */
2480 nr_oom_retries--;
2481 break;
2482 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002483 css_put(&mem->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002484 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08002485 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002486 } while (ret != CHARGE_OK);
2487
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002488 if (batch > nr_pages)
2489 refill_stock(mem, batch - nr_pages);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002490 css_put(&mem->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002491done:
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002492 *memcg = mem;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002493 return 0;
2494nomem:
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002495 *memcg = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002496 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002497bypass:
2498 *memcg = NULL;
2499 return 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002500}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002501
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002502/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002503 * Somemtimes we have to undo a charge we got by try_charge().
2504 * This function is for that and do uncharge, put css's refcnt.
2505 * gotten by try_charge().
2506 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002507static void __mem_cgroup_cancel_charge(struct mem_cgroup *mem,
Johannes Weinere7018b82011-03-23 16:42:33 -07002508 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002509{
2510 if (!mem_cgroup_is_root(mem)) {
Johannes Weinere7018b82011-03-23 16:42:33 -07002511 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002512
Johannes Weinere7018b82011-03-23 16:42:33 -07002513 res_counter_uncharge(&mem->res, bytes);
2514 if (do_swap_account)
2515 res_counter_uncharge(&mem->memsw, bytes);
2516 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002517}
2518
2519/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002520 * A helper function to get mem_cgroup from ID. must be called under
2521 * rcu_read_lock(). The caller must check css_is_removed() or some if
2522 * it's concern. (dropping refcnt from swap can be called against removed
2523 * memcg.)
2524 */
2525static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2526{
2527 struct cgroup_subsys_state *css;
2528
2529 /* ID 0 is unused ID */
2530 if (!id)
2531 return NULL;
2532 css = css_lookup(&mem_cgroup_subsys, id);
2533 if (!css)
2534 return NULL;
2535 return container_of(css, struct mem_cgroup, css);
2536}
2537
Wu Fengguange42d9d52009-12-16 12:19:59 +01002538struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002539{
Wu Fengguange42d9d52009-12-16 12:19:59 +01002540 struct mem_cgroup *mem = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002541 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002542 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002543 swp_entry_t ent;
2544
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002545 VM_BUG_ON(!PageLocked(page));
2546
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002547 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002548 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002549 if (PageCgroupUsed(pc)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002550 mem = pc->mem_cgroup;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002551 if (mem && !css_tryget(&mem->css))
2552 mem = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002553 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002554 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002555 id = lookup_swap_cgroup(ent);
2556 rcu_read_lock();
2557 mem = mem_cgroup_lookup(id);
2558 if (mem && !css_tryget(&mem->css))
2559 mem = NULL;
2560 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002561 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002562 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002563 return mem;
2564}
2565
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002566static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
Johannes Weiner5564e882011-03-23 16:42:29 -07002567 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002568 unsigned int nr_pages,
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002569 struct page_cgroup *pc,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002570 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002571{
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002572 lock_page_cgroup(pc);
2573 if (unlikely(PageCgroupUsed(pc))) {
2574 unlock_page_cgroup(pc);
Johannes Weinere7018b82011-03-23 16:42:33 -07002575 __mem_cgroup_cancel_charge(mem, nr_pages);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002576 return;
2577 }
2578 /*
2579 * we don't need page_cgroup_lock about tail pages, becase they are not
2580 * accessed by any other context at this point.
2581 */
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002582 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002583 /*
2584 * We access a page_cgroup asynchronously without lock_page_cgroup().
2585 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2586 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2587 * before USED bit, we need memory barrier here.
2588 * See mem_cgroup_add_lru_list(), etc.
2589 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002590 smp_wmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -07002591 switch (ctype) {
2592 case MEM_CGROUP_CHARGE_TYPE_CACHE:
2593 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
2594 SetPageCgroupCache(pc);
2595 SetPageCgroupUsed(pc);
2596 break;
2597 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
2598 ClearPageCgroupCache(pc);
2599 SetPageCgroupUsed(pc);
2600 break;
2601 default:
2602 break;
2603 }
Hugh Dickins3be91272008-02-07 00:14:19 -08002604
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002605 mem_cgroup_charge_statistics(mem, PageCgroupCache(pc), nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002606 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002607 /*
2608 * "charge_statistics" updated event counter. Then, check it.
2609 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2610 * if they exceeds softlimit.
2611 */
Johannes Weiner5564e882011-03-23 16:42:29 -07002612 memcg_check_events(mem, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002613}
2614
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002615#ifdef CONFIG_TRANSPARENT_HUGEPAGE
2616
2617#define PCGF_NOCOPY_AT_SPLIT ((1 << PCG_LOCK) | (1 << PCG_MOVE_LOCK) |\
2618 (1 << PCG_ACCT_LRU) | (1 << PCG_MIGRATION))
2619/*
2620 * Because tail pages are not marked as "used", set it. We're under
2621 * zone->lru_lock, 'splitting on pmd' and compund_lock.
2622 */
2623void mem_cgroup_split_huge_fixup(struct page *head, struct page *tail)
2624{
2625 struct page_cgroup *head_pc = lookup_page_cgroup(head);
2626 struct page_cgroup *tail_pc = lookup_page_cgroup(tail);
2627 unsigned long flags;
2628
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08002629 if (mem_cgroup_disabled())
2630 return;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002631 /*
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002632 * We have no races with charge/uncharge but will have races with
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002633 * page state accounting.
2634 */
2635 move_lock_page_cgroup(head_pc, &flags);
2636
2637 tail_pc->mem_cgroup = head_pc->mem_cgroup;
2638 smp_wmb(); /* see __commit_charge() */
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002639 if (PageCgroupAcctLRU(head_pc)) {
2640 enum lru_list lru;
2641 struct mem_cgroup_per_zone *mz;
2642
2643 /*
2644 * LRU flags cannot be copied because we need to add tail
2645 *.page to LRU by generic call and our hook will be called.
2646 * We hold lru_lock, then, reduce counter directly.
2647 */
2648 lru = page_lru(head);
Johannes Weiner97a6c372011-03-23 16:42:27 -07002649 mz = page_cgroup_zoneinfo(head_pc->mem_cgroup, head);
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002650 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
2651 }
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002652 tail_pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
2653 move_unlock_page_cgroup(head_pc, &flags);
2654}
2655#endif
2656
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002657/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07002658 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07002659 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002660 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002661 * @pc: page_cgroup of the page.
2662 * @from: mem_cgroup which the page is moved from.
2663 * @to: mem_cgroup which the page is moved to. @from != @to.
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002664 * @uncharge: whether we should call uncharge and css_put against @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002665 *
2666 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002667 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002668 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002669 *
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002670 * This function doesn't do "charge" nor css_get to new cgroup. It should be
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002671 * done by a caller(__mem_cgroup_try_charge would be useful). If @uncharge is
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002672 * true, this function does "uncharge" from old cgroup, but it doesn't if
2673 * @uncharge is false, so a caller should do "uncharge".
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002674 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002675static int mem_cgroup_move_account(struct page *page,
2676 unsigned int nr_pages,
2677 struct page_cgroup *pc,
2678 struct mem_cgroup *from,
2679 struct mem_cgroup *to,
2680 bool uncharge)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002681{
Johannes Weinerde3638d2011-03-23 16:42:28 -07002682 unsigned long flags;
2683 int ret;
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002684
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002685 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07002686 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07002687 /*
2688 * The page is isolated from LRU. So, collapse function
2689 * will not handle this page. But page splitting can happen.
2690 * Do this check under compound_page_lock(). The caller should
2691 * hold it.
2692 */
2693 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002694 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07002695 goto out;
2696
2697 lock_page_cgroup(pc);
2698
2699 ret = -EINVAL;
2700 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
2701 goto unlock;
2702
2703 move_lock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002704
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002705 if (PageCgroupFileMapped(pc)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08002706 /* Update mapped_file data for mem_cgroup */
2707 preempt_disable();
2708 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2709 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2710 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07002711 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002712 mem_cgroup_charge_statistics(from, PageCgroupCache(pc), -nr_pages);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002713 if (uncharge)
2714 /* This is not "cancel", but cancel_charge does all we need. */
Johannes Weinere7018b82011-03-23 16:42:33 -07002715 __mem_cgroup_cancel_charge(from, nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07002716
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002717 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002718 pc->mem_cgroup = to;
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002719 mem_cgroup_charge_statistics(to, PageCgroupCache(pc), nr_pages);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002720 /*
2721 * We charges against "to" which may not have any tasks. Then, "to"
2722 * can be under rmdir(). But in current implementation, caller of
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08002723 * this function is just force_empty() and move charge, so it's
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002724 * guaranteed that "to" is never removed. So, we don't check rmdir
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08002725 * status here.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002726 */
Johannes Weinerde3638d2011-03-23 16:42:28 -07002727 move_unlock_page_cgroup(pc, &flags);
2728 ret = 0;
2729unlock:
Daisuke Nishimura57f9fd72009-12-15 16:47:11 -08002730 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002731 /*
2732 * check events
2733 */
Johannes Weiner5564e882011-03-23 16:42:29 -07002734 memcg_check_events(to, page);
2735 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07002736out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002737 return ret;
2738}
2739
2740/*
2741 * move charges to its parent.
2742 */
2743
Johannes Weiner5564e882011-03-23 16:42:29 -07002744static int mem_cgroup_move_parent(struct page *page,
2745 struct page_cgroup *pc,
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002746 struct mem_cgroup *child,
2747 gfp_t gfp_mask)
2748{
2749 struct cgroup *cg = child->css.cgroup;
2750 struct cgroup *pcg = cg->parent;
2751 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002752 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07002753 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002754 int ret;
2755
2756 /* Is ROOT ? */
2757 if (!pcg)
2758 return -EINVAL;
2759
Daisuke Nishimura57f9fd72009-12-15 16:47:11 -08002760 ret = -EBUSY;
2761 if (!get_page_unless_zero(page))
2762 goto out;
2763 if (isolate_lru_page(page))
2764 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08002765
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002766 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002767
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002768 parent = mem_cgroup_from_cont(pcg);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002769 ret = __mem_cgroup_try_charge(NULL, gfp_mask, nr_pages, &parent, false);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002770 if (ret || !parent)
Daisuke Nishimura57f9fd72009-12-15 16:47:11 -08002771 goto put_back;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002772
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002773 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002774 flags = compound_lock_irqsave(page);
2775
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002776 ret = mem_cgroup_move_account(page, nr_pages, pc, child, parent, true);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002777 if (ret)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002778 __mem_cgroup_cancel_charge(parent, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08002779
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002780 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002781 compound_unlock_irqrestore(page, flags);
Jesper Juhl8dba4742011-01-25 15:07:24 -08002782put_back:
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002783 putback_lru_page(page);
Daisuke Nishimura57f9fd72009-12-15 16:47:11 -08002784put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08002785 put_page(page);
Daisuke Nishimura57f9fd72009-12-15 16:47:11 -08002786out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002787 return ret;
2788}
2789
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002790/*
2791 * Charge the memory controller for page usage.
2792 * Return
2793 * 0 if the charge was successful
2794 * < 0 if the cgroup is over its limit
2795 */
2796static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002797 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002798{
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002799 struct mem_cgroup *mem = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002800 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08002801 struct page_cgroup *pc;
2802 bool oom = true;
2803 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08002804
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002805 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002806 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002807 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08002808 /*
2809 * Never OOM-kill a process for a huge page. The
2810 * fault handler will fall back to regular pages.
2811 */
2812 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002813 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002814
2815 pc = lookup_page_cgroup(page);
Johannes Weineraf4a6622011-03-23 16:42:24 -07002816 BUG_ON(!pc); /* XXX: remove this and move pc lookup into commit */
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002817
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002818 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &mem, oom);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002819 if (ret || !mem)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002820 return ret;
2821
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002822 __mem_cgroup_commit_charge(mem, page, nr_pages, pc, ctype);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002823 return 0;
2824}
2825
2826int mem_cgroup_newpage_charge(struct page *page,
2827 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002828{
Hirokazu Takahashif8d66542009-01-07 18:08:02 -08002829 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002830 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002831 /*
2832 * If already mapped, we don't have to account.
2833 * If page cache, page->mapping has address_space.
2834 * But page->mapping may have out-of-use anon_vma pointer,
2835 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
2836 * is NULL.
2837 */
2838 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
2839 return 0;
2840 if (unlikely(!mm))
2841 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002842 return mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002843 MEM_CGROUP_CHARGE_TYPE_MAPPED);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002844}
2845
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002846static void
2847__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2848 enum charge_type ctype);
2849
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002850static void
2851__mem_cgroup_commit_charge_lrucare(struct page *page, struct mem_cgroup *mem,
2852 enum charge_type ctype)
2853{
2854 struct page_cgroup *pc = lookup_page_cgroup(page);
2855 /*
2856 * In some case, SwapCache, FUSE(splice_buf->radixtree), the page
2857 * is already on LRU. It means the page may on some other page_cgroup's
2858 * LRU. Take care of it.
2859 */
2860 mem_cgroup_lru_del_before_commit(page);
2861 __mem_cgroup_commit_charge(mem, page, 1, pc, ctype);
2862 mem_cgroup_lru_add_after_commit(page);
2863 return;
2864}
2865
Balbir Singhe1a1cd52008-02-07 00:14:02 -08002866int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
2867 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08002868{
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002869 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002870 int ret;
2871
Hirokazu Takahashif8d66542009-01-07 18:08:02 -08002872 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002873 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002874 if (PageCompound(page))
2875 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002876 /*
2877 * Corner case handling. This is called from add_to_page_cache()
2878 * in usual. But some FS (shmem) precharges this page before calling it
2879 * and call add_to_page_cache() with GFP_NOWAIT.
2880 *
2881 * For GFP_NOWAIT case, the page may be pre-charged before calling
2882 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
2883 * charge twice. (It works but has to pay a bit larger cost.)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002884 * And when the page is SwapCache, it should take swap information
2885 * into account. This is under lock_page() now.
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002886 */
2887 if (!(gfp_mask & __GFP_WAIT)) {
2888 struct page_cgroup *pc;
2889
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002890 pc = lookup_page_cgroup(page);
2891 if (!pc)
2892 return 0;
2893 lock_page_cgroup(pc);
2894 if (PageCgroupUsed(pc)) {
2895 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002896 return 0;
2897 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002898 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002899 }
2900
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002901 if (unlikely(!mm))
Balbir Singh8697d332008-02-07 00:13:59 -08002902 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002903
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002904 if (page_is_file_cache(page)) {
2905 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, &mem, true);
2906 if (ret || !mem)
2907 return ret;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002908
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002909 /*
2910 * FUSE reuses pages without going through the final
2911 * put that would remove them from the LRU list, make
2912 * sure that they get relinked properly.
2913 */
2914 __mem_cgroup_commit_charge_lrucare(page, mem,
2915 MEM_CGROUP_CHARGE_TYPE_CACHE);
2916 return ret;
2917 }
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002918 /* shmem */
2919 if (PageSwapCache(page)) {
2920 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2921 if (!ret)
2922 __mem_cgroup_commit_charge_swapin(page, mem,
2923 MEM_CGROUP_CHARGE_TYPE_SHMEM);
2924 } else
2925 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002926 MEM_CGROUP_CHARGE_TYPE_SHMEM);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002927
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002928 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002929}
2930
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002931/*
2932 * While swap-in, try_charge -> commit or cancel, the page is locked.
2933 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02002934 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002935 * "commit()" or removed by "cancel()"
2936 */
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002937int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
2938 struct page *page,
2939 gfp_t mask, struct mem_cgroup **ptr)
2940{
2941 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002942 int ret;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002943
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07002944 *ptr = NULL;
2945
Hirokazu Takahashif8d66542009-01-07 18:08:02 -08002946 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002947 return 0;
2948
2949 if (!do_swap_account)
2950 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002951 /*
2952 * A racing thread's fault, or swapoff, may have already updated
Hugh Dickins407f9c82009-12-14 17:59:30 -08002953 * the pte, and even removed page from swap cache: in those cases
2954 * do_swap_page()'s pte_same() test will fail; but there's also a
2955 * KSM case which does need to charge the page.
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002956 */
2957 if (!PageSwapCache(page))
Hugh Dickins407f9c82009-12-14 17:59:30 -08002958 goto charge_cur_mm;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002959 mem = try_get_mem_cgroup_from_page(page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002960 if (!mem)
2961 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002962 *ptr = mem;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002963 ret = __mem_cgroup_try_charge(NULL, mask, 1, ptr, true);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002964 css_put(&mem->css);
2965 return ret;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002966charge_cur_mm:
2967 if (unlikely(!mm))
2968 mm = &init_mm;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002969 return __mem_cgroup_try_charge(mm, mask, 1, ptr, true);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002970}
2971
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002972static void
2973__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2974 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002975{
Hirokazu Takahashif8d66542009-01-07 18:08:02 -08002976 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002977 return;
2978 if (!ptr)
2979 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002980 cgroup_exclude_rmdir(&ptr->css);
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002981
2982 __mem_cgroup_commit_charge_lrucare(page, ptr, ctype);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002983 /*
2984 * Now swap is on-memory. This means this page may be
2985 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002986 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
2987 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
2988 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002989 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002990 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002991 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002992 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002993 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002994
2995 id = swap_cgroup_record(ent, 0);
2996 rcu_read_lock();
2997 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002998 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002999 /*
3000 * This recorded memcg can be obsolete one. So, avoid
3001 * calling css_tryget
3002 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003003 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07003004 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003005 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003006 mem_cgroup_put(memcg);
3007 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003008 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003009 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003010 /*
3011 * At swapin, we may charge account against cgroup which has no tasks.
3012 * So, rmdir()->pre_destroy() can be called while we do this charge.
3013 * In that case, we need to call pre_destroy() again. check it here.
3014 */
3015 cgroup_release_and_wakeup_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003016}
3017
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003018void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
3019{
3020 __mem_cgroup_commit_charge_swapin(page, ptr,
3021 MEM_CGROUP_CHARGE_TYPE_MAPPED);
3022}
3023
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003024void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
3025{
Hirokazu Takahashif8d66542009-01-07 18:08:02 -08003026 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003027 return;
3028 if (!mem)
3029 return;
Johannes Weinere7018b82011-03-23 16:42:33 -07003030 __mem_cgroup_cancel_charge(mem, 1);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003031}
3032
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003033static void mem_cgroup_do_uncharge(struct mem_cgroup *mem,
3034 unsigned int nr_pages,
3035 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003036{
3037 struct memcg_batch_info *batch = NULL;
3038 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003039
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003040 /* If swapout, usage of swap doesn't decrease */
3041 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
3042 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003043
3044 batch = &current->memcg_batch;
3045 /*
3046 * In usual, we do css_get() when we remember memcg pointer.
3047 * But in this case, we keep res->usage until end of a series of
3048 * uncharges. Then, it's ok to ignore memcg's refcnt.
3049 */
3050 if (!batch->memcg)
3051 batch->memcg = mem;
3052 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003053 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003054 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003055 * the same cgroup and we have chance to coalesce uncharges.
3056 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
3057 * because we want to do uncharge as soon as possible.
3058 */
3059
3060 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
3061 goto direct_uncharge;
3062
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003063 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08003064 goto direct_uncharge;
3065
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003066 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003067 * In typical case, batch->memcg == mem. This means we can
3068 * merge a series of uncharges to an uncharge of res_counter.
3069 * If not, we uncharge res_counter ony by one.
3070 */
3071 if (batch->memcg != mem)
3072 goto direct_uncharge;
3073 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003074 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003075 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003076 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003077 return;
3078direct_uncharge:
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003079 res_counter_uncharge(&mem->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003080 if (uncharge_memsw)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003081 res_counter_uncharge(&mem->memsw, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003082 if (unlikely(batch->memcg != mem))
3083 memcg_oom_recover(mem);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003084 return;
3085}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003086
Balbir Singh8697d332008-02-07 00:13:59 -08003087/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003088 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003089 */
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003090static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003091__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003092{
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003093 struct mem_cgroup *mem = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003094 unsigned int nr_pages = 1;
3095 struct page_cgroup *pc;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003096
Hirokazu Takahashif8d66542009-01-07 18:08:02 -08003097 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003098 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07003099
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003100 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003101 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003102
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003103 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003104 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003105 VM_BUG_ON(!PageTransHuge(page));
3106 }
Balbir Singh8697d332008-02-07 00:13:59 -08003107 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08003108 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08003109 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003110 pc = lookup_page_cgroup(page);
3111 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003112 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003113
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003114 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003115
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003116 mem = pc->mem_cgroup;
3117
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003118 if (!PageCgroupUsed(pc))
3119 goto unlock_out;
3120
3121 switch (ctype) {
3122 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003123 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003124 /* See mem_cgroup_prepare_migration() */
3125 if (page_mapped(page) || PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003126 goto unlock_out;
3127 break;
3128 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
3129 if (!PageAnon(page)) { /* Shared memory */
3130 if (page->mapping && !page_is_file_cache(page))
3131 goto unlock_out;
3132 } else if (page_mapped(page)) /* Anon */
3133 goto unlock_out;
3134 break;
3135 default:
3136 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003137 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003138
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003139 mem_cgroup_charge_statistics(mem, PageCgroupCache(pc), -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003140
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003141 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08003142 /*
3143 * pc->mem_cgroup is not cleared here. It will be accessed when it's
3144 * freed from LRU. This is safe because uncharged page is expected not
3145 * to be reused (freed soon). Exception is SwapCache, it's handled by
3146 * special functions.
3147 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003148
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003149 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003150 /*
3151 * even after unlock, we have mem->res.usage here and this memcg
3152 * will never be freed.
3153 */
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08003154 memcg_check_events(mem, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003155 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
3156 mem_cgroup_swap_statistics(mem, true);
3157 mem_cgroup_get(mem);
3158 }
3159 if (!mem_cgroup_is_root(mem))
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003160 mem_cgroup_do_uncharge(mem, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003161
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003162 return mem;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003163
3164unlock_out:
3165 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003166 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08003167}
3168
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003169void mem_cgroup_uncharge_page(struct page *page)
3170{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003171 /* early check. */
3172 if (page_mapped(page))
3173 return;
3174 if (page->mapping && !PageAnon(page))
3175 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003176 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
3177}
3178
3179void mem_cgroup_uncharge_cache_page(struct page *page)
3180{
3181 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07003182 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003183 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
3184}
3185
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003186/*
3187 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
3188 * In that cases, pages are freed continuously and we can expect pages
3189 * are in the same memcg. All these calls itself limits the number of
3190 * pages freed at once, then uncharge_start/end() is called properly.
3191 * This may be called prural(2) times in a context,
3192 */
3193
3194void mem_cgroup_uncharge_start(void)
3195{
3196 current->memcg_batch.do_batch++;
3197 /* We can do nest. */
3198 if (current->memcg_batch.do_batch == 1) {
3199 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003200 current->memcg_batch.nr_pages = 0;
3201 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003202 }
3203}
3204
3205void mem_cgroup_uncharge_end(void)
3206{
3207 struct memcg_batch_info *batch = &current->memcg_batch;
3208
3209 if (!batch->do_batch)
3210 return;
3211
3212 batch->do_batch--;
3213 if (batch->do_batch) /* If stacked, do nothing. */
3214 return;
3215
3216 if (!batch->memcg)
3217 return;
3218 /*
3219 * This "batch->memcg" is valid without any css_get/put etc...
3220 * bacause we hide charges behind us.
3221 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003222 if (batch->nr_pages)
3223 res_counter_uncharge(&batch->memcg->res,
3224 batch->nr_pages * PAGE_SIZE);
3225 if (batch->memsw_nr_pages)
3226 res_counter_uncharge(&batch->memcg->memsw,
3227 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003228 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003229 /* forget this pointer (for sanity check) */
3230 batch->memcg = NULL;
3231}
3232
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003233#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003234/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003235 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003236 * memcg information is recorded to swap_cgroup of "ent"
3237 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003238void
3239mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003240{
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003241 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003242 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003243
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003244 if (!swapout) /* this was a swap cache but the swap is unused ! */
3245 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
3246
3247 memcg = __mem_cgroup_uncharge_common(page, ctype);
3248
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003249 /*
3250 * record memcg information, if swapout && memcg != NULL,
3251 * mem_cgroup_get() was called in uncharge().
3252 */
3253 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003254 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003255}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003256#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003257
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003258#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3259/*
3260 * called from swap_entry_free(). remove record in swap_cgroup and
3261 * uncharge "memsw" account.
3262 */
3263void mem_cgroup_uncharge_swap(swp_entry_t ent)
3264{
3265 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003266 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003267
3268 if (!do_swap_account)
3269 return;
3270
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003271 id = swap_cgroup_record(ent, 0);
3272 rcu_read_lock();
3273 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003274 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003275 /*
3276 * We uncharge this because swap is freed.
3277 * This memcg can be obsolete one. We avoid calling css_tryget
3278 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003279 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07003280 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003281 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003282 mem_cgroup_put(memcg);
3283 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003284 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003285}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003286
3287/**
3288 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
3289 * @entry: swap entry to be moved
3290 * @from: mem_cgroup which the entry is moved from
3291 * @to: mem_cgroup which the entry is moved to
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003292 * @need_fixup: whether we should fixup res_counters and refcounts.
Daisuke Nishimura02491442010-03-10 15:22:17 -08003293 *
3294 * It succeeds only when the swap_cgroup's record for this entry is the same
3295 * as the mem_cgroup's id of @from.
3296 *
3297 * Returns 0 on success, -EINVAL on failure.
3298 *
3299 * The caller must have charged to @to, IOW, called res_counter_charge() about
3300 * both res and memsw, and called css_get().
3301 */
3302static int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003303 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003304{
3305 unsigned short old_id, new_id;
3306
3307 old_id = css_id(&from->css);
3308 new_id = css_id(&to->css);
3309
3310 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08003311 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003312 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003313 /*
3314 * This function is only called from task migration context now.
3315 * It postpones res_counter and refcount handling till the end
3316 * of task migration(mem_cgroup_clear_mc()) for performance
3317 * improvement. But we cannot postpone mem_cgroup_get(to)
3318 * because if the process that has been moved to @to does
3319 * swap-in, the refcount of @to might be decreased to 0.
3320 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08003321 mem_cgroup_get(to);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003322 if (need_fixup) {
3323 if (!mem_cgroup_is_root(from))
3324 res_counter_uncharge(&from->memsw, PAGE_SIZE);
3325 mem_cgroup_put(from);
3326 /*
3327 * we charged both to->res and to->memsw, so we should
3328 * uncharge to->res.
3329 */
3330 if (!mem_cgroup_is_root(to))
3331 res_counter_uncharge(&to->res, PAGE_SIZE);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003332 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08003333 return 0;
3334 }
3335 return -EINVAL;
3336}
3337#else
3338static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003339 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003340{
3341 return -EINVAL;
3342}
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003343#endif
3344
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003345/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003346 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
3347 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003348 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003349int mem_cgroup_prepare_migration(struct page *page,
Miklos Szeredief6a3c62011-03-22 16:30:52 -07003350 struct page *newpage, struct mem_cgroup **ptr, gfp_t gfp_mask)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003351{
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003352 struct mem_cgroup *mem = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003353 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003354 enum charge_type ctype;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003355 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003356
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07003357 *ptr = NULL;
3358
Andrea Arcangeliec168512011-01-13 15:46:56 -08003359 VM_BUG_ON(PageTransHuge(page));
Hirokazu Takahashif8d66542009-01-07 18:08:02 -08003360 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07003361 return 0;
3362
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003363 pc = lookup_page_cgroup(page);
3364 lock_page_cgroup(pc);
3365 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003366 mem = pc->mem_cgroup;
3367 css_get(&mem->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003368 /*
3369 * At migrating an anonymous page, its mapcount goes down
3370 * to 0 and uncharge() will be called. But, even if it's fully
3371 * unmapped, migration may fail and this page has to be
3372 * charged again. We set MIGRATION flag here and delay uncharge
3373 * until end_migration() is called
3374 *
3375 * Corner Case Thinking
3376 * A)
3377 * When the old page was mapped as Anon and it's unmap-and-freed
3378 * while migration was ongoing.
3379 * If unmap finds the old page, uncharge() of it will be delayed
3380 * until end_migration(). If unmap finds a new page, it's
3381 * uncharged when it make mapcount to be 1->0. If unmap code
3382 * finds swap_migration_entry, the new page will not be mapped
3383 * and end_migration() will find it(mapcount==0).
3384 *
3385 * B)
3386 * When the old page was mapped but migraion fails, the kernel
3387 * remaps it. A charge for it is kept by MIGRATION flag even
3388 * if mapcount goes down to 0. We can do remap successfully
3389 * without charging it again.
3390 *
3391 * C)
3392 * The "old" page is under lock_page() until the end of
3393 * migration, so, the old page itself will not be swapped-out.
3394 * If the new page is swapped out before end_migraton, our
3395 * hook to usual swap-out path will catch the event.
3396 */
3397 if (PageAnon(page))
3398 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003399 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003400 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003401 /*
3402 * If the page is not charged at this point,
3403 * we return here.
3404 */
3405 if (!mem)
3406 return 0;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003407
Andrea Arcangeli93d5c9b2010-04-23 13:17:39 -04003408 *ptr = mem;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003409 ret = __mem_cgroup_try_charge(NULL, gfp_mask, 1, ptr, false);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003410 css_put(&mem->css);/* drop extra refcnt */
3411 if (ret || *ptr == NULL) {
3412 if (PageAnon(page)) {
3413 lock_page_cgroup(pc);
3414 ClearPageCgroupMigration(pc);
3415 unlock_page_cgroup(pc);
3416 /*
3417 * The old page may be fully unmapped while we kept it.
3418 */
3419 mem_cgroup_uncharge_page(page);
3420 }
3421 return -ENOMEM;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003422 }
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003423 /*
3424 * We charge new page before it's used/mapped. So, even if unlock_page()
3425 * is called before end_migration, we can catch all events on this new
3426 * page. In the case new page is migrated but not remapped, new page's
3427 * mapcount will be finally 0 and we call uncharge in end_migration().
3428 */
3429 pc = lookup_page_cgroup(newpage);
3430 if (PageAnon(page))
3431 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
3432 else if (page_is_file_cache(page))
3433 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
3434 else
3435 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003436 __mem_cgroup_commit_charge(mem, page, 1, pc, ctype);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003437 return ret;
3438}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08003439
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003440/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003441void mem_cgroup_end_migration(struct mem_cgroup *mem,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003442 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003443{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003444 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003445 struct page_cgroup *pc;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003446
3447 if (!mem)
3448 return;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003449 /* blocks rmdir() */
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003450 cgroup_exclude_rmdir(&mem->css);
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003451 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003452 used = oldpage;
3453 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003454 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003455 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003456 unused = oldpage;
3457 }
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003458 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003459 * We disallowed uncharge of pages under migration because mapcount
3460 * of the page goes down to zero, temporarly.
3461 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003462 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003463 pc = lookup_page_cgroup(oldpage);
3464 lock_page_cgroup(pc);
3465 ClearPageCgroupMigration(pc);
3466 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003467
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003468 __mem_cgroup_uncharge_common(unused, MEM_CGROUP_CHARGE_TYPE_FORCE);
3469
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003470 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003471 * If a page is a file cache, radix-tree replacement is very atomic
3472 * and we can skip this check. When it was an Anon page, its mapcount
3473 * goes down to 0. But because we added MIGRATION flage, it's not
3474 * uncharged yet. There are several case but page->mapcount check
3475 * and USED bit check in mem_cgroup_uncharge_page() will do enough
3476 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003477 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003478 if (PageAnon(used))
3479 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003480 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003481 * At migration, we may charge account against cgroup which has no
3482 * tasks.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003483 * So, rmdir()->pre_destroy() can be called while we do this charge.
3484 * In that case, we need to call pre_destroy() again. check it here.
3485 */
3486 cgroup_release_and_wakeup_rmdir(&mem->css);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003487}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08003488
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003489/*
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07003490 * A call to try to shrink memory usage on charge failure at shmem's swapin.
3491 * Calling hierarchical_reclaim is not enough because we should update
3492 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
3493 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
3494 * not from the memcg which this page would be charged to.
3495 * try_charge_swapin does all of these works properly.
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003496 */
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07003497int mem_cgroup_shmem_charge_fallback(struct page *page,
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08003498 struct mm_struct *mm,
3499 gfp_t gfp_mask)
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003500{
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07003501 struct mem_cgroup *mem;
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07003502 int ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003503
Hirokazu Takahashif8d66542009-01-07 18:08:02 -08003504 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07003505 return 0;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003506
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07003507 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
3508 if (!ret)
3509 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003510
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07003511 return ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003512}
3513
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003514#ifdef CONFIG_DEBUG_VM
3515static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
3516{
3517 struct page_cgroup *pc;
3518
3519 pc = lookup_page_cgroup(page);
3520 if (likely(pc) && PageCgroupUsed(pc))
3521 return pc;
3522 return NULL;
3523}
3524
3525bool mem_cgroup_bad_page_check(struct page *page)
3526{
3527 if (mem_cgroup_disabled())
3528 return false;
3529
3530 return lookup_page_cgroup_used(page) != NULL;
3531}
3532
3533void mem_cgroup_print_bad_page(struct page *page)
3534{
3535 struct page_cgroup *pc;
3536
3537 pc = lookup_page_cgroup_used(page);
3538 if (pc) {
3539 int ret = -1;
3540 char *path;
3541
3542 printk(KERN_ALERT "pc:%p pc->flags:%lx pc->mem_cgroup:%p",
3543 pc, pc->flags, pc->mem_cgroup);
3544
3545 path = kmalloc(PATH_MAX, GFP_KERNEL);
3546 if (path) {
3547 rcu_read_lock();
3548 ret = cgroup_path(pc->mem_cgroup->css.cgroup,
3549 path, PATH_MAX);
3550 rcu_read_unlock();
3551 }
3552
3553 printk(KERN_CONT "(%s)\n",
3554 (ret < 0) ? "cannot get the path" : path);
3555 kfree(path);
3556 }
3557}
3558#endif
3559
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003560static DEFINE_MUTEX(set_limit_mutex);
3561
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08003562static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003563 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003564{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003565 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003566 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003567 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003568 int children = mem_cgroup_count_children(memcg);
3569 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003570 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003571
3572 /*
3573 * For keeping hierarchical_reclaim simple, how long we should retry
3574 * is depends on callers. We set our retry-count to be function
3575 * of # of children which we should visit in this loop.
3576 */
3577 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
3578
3579 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003580
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003581 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003582 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003583 if (signal_pending(current)) {
3584 ret = -EINTR;
3585 break;
3586 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003587 /*
3588 * Rather than hide all in some function, I do this in
3589 * open coded manner. You see what this really does.
3590 * We have to guarantee mem->res.limit < mem->memsw.limit.
3591 */
3592 mutex_lock(&set_limit_mutex);
3593 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3594 if (memswlimit < val) {
3595 ret = -EINVAL;
3596 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003597 break;
3598 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003599
3600 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3601 if (memlimit < val)
3602 enlarge = 1;
3603
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003604 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003605 if (!ret) {
3606 if (memswlimit == val)
3607 memcg->memsw_is_minimum = true;
3608 else
3609 memcg->memsw_is_minimum = false;
3610 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003611 mutex_unlock(&set_limit_mutex);
3612
3613 if (!ret)
3614 break;
3615
Bob Liuaa20d482009-12-15 16:47:14 -08003616 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Ying Han0ae5e892011-05-26 16:25:25 -07003617 MEM_CGROUP_RECLAIM_SHRINK,
3618 NULL);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003619 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
3620 /* Usage is reduced ? */
3621 if (curusage >= oldusage)
3622 retry_count--;
3623 else
3624 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003625 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003626 if (!ret && enlarge)
3627 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08003628
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003629 return ret;
3630}
3631
Li Zefan338c8432009-06-17 16:27:15 -07003632static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
3633 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003634{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003635 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003636 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003637 int children = mem_cgroup_count_children(memcg);
3638 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003639 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003640
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003641 /* see mem_cgroup_resize_res_limit */
3642 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
3643 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003644 while (retry_count) {
3645 if (signal_pending(current)) {
3646 ret = -EINTR;
3647 break;
3648 }
3649 /*
3650 * Rather than hide all in some function, I do this in
3651 * open coded manner. You see what this really does.
3652 * We have to guarantee mem->res.limit < mem->memsw.limit.
3653 */
3654 mutex_lock(&set_limit_mutex);
3655 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3656 if (memlimit > val) {
3657 ret = -EINVAL;
3658 mutex_unlock(&set_limit_mutex);
3659 break;
3660 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003661 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3662 if (memswlimit < val)
3663 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003664 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003665 if (!ret) {
3666 if (memlimit == val)
3667 memcg->memsw_is_minimum = true;
3668 else
3669 memcg->memsw_is_minimum = false;
3670 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003671 mutex_unlock(&set_limit_mutex);
3672
3673 if (!ret)
3674 break;
3675
Balbir Singh4e416952009-09-23 15:56:39 -07003676 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh75822b42009-09-23 15:56:38 -07003677 MEM_CGROUP_RECLAIM_NOSWAP |
Ying Han0ae5e892011-05-26 16:25:25 -07003678 MEM_CGROUP_RECLAIM_SHRINK,
3679 NULL);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003680 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003681 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003682 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003683 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003684 else
3685 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003686 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003687 if (!ret && enlarge)
3688 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003689 return ret;
3690}
3691
Balbir Singh4e416952009-09-23 15:56:39 -07003692unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
Ying Han0ae5e892011-05-26 16:25:25 -07003693 gfp_t gfp_mask,
3694 unsigned long *total_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07003695{
3696 unsigned long nr_reclaimed = 0;
3697 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
3698 unsigned long reclaimed;
3699 int loop = 0;
3700 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003701 unsigned long long excess;
Ying Han0ae5e892011-05-26 16:25:25 -07003702 unsigned long nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003703
3704 if (order > 0)
3705 return 0;
3706
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07003707 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
Balbir Singh4e416952009-09-23 15:56:39 -07003708 /*
3709 * This loop can run a while, specially if mem_cgroup's continuously
3710 * keep exceeding their soft limit and putting the system under
3711 * pressure
3712 */
3713 do {
3714 if (next_mz)
3715 mz = next_mz;
3716 else
3717 mz = mem_cgroup_largest_soft_limit_node(mctz);
3718 if (!mz)
3719 break;
3720
Ying Han0ae5e892011-05-26 16:25:25 -07003721 nr_scanned = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07003722 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
3723 gfp_mask,
Ying Han0ae5e892011-05-26 16:25:25 -07003724 MEM_CGROUP_RECLAIM_SOFT,
3725 &nr_scanned);
Balbir Singh4e416952009-09-23 15:56:39 -07003726 nr_reclaimed += reclaimed;
Ying Han0ae5e892011-05-26 16:25:25 -07003727 *total_scanned += nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003728 spin_lock(&mctz->lock);
3729
3730 /*
3731 * If we failed to reclaim anything from this memory cgroup
3732 * it is time to move on to the next cgroup
3733 */
3734 next_mz = NULL;
3735 if (!reclaimed) {
3736 do {
3737 /*
3738 * Loop until we find yet another one.
3739 *
3740 * By the time we get the soft_limit lock
3741 * again, someone might have aded the
3742 * group back on the RB tree. Iterate to
3743 * make sure we get a different mem.
3744 * mem_cgroup_largest_soft_limit_node returns
3745 * NULL if no other cgroup is present on
3746 * the tree
3747 */
3748 next_mz =
3749 __mem_cgroup_largest_soft_limit_node(mctz);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003750 if (next_mz == mz)
Balbir Singh4e416952009-09-23 15:56:39 -07003751 css_put(&next_mz->mem->css);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003752 else /* next_mz == NULL or other memcg */
Balbir Singh4e416952009-09-23 15:56:39 -07003753 break;
3754 } while (1);
3755 }
Balbir Singh4e416952009-09-23 15:56:39 -07003756 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003757 excess = res_counter_soft_limit_excess(&mz->mem->res);
Balbir Singh4e416952009-09-23 15:56:39 -07003758 /*
3759 * One school of thought says that we should not add
3760 * back the node to the tree if reclaim returns 0.
3761 * But our reclaim could return 0, simply because due
3762 * to priority we are exposing a smaller subset of
3763 * memory to reclaim from. Consider this as a longer
3764 * term TODO.
3765 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003766 /* If excess == 0, no tree ops */
3767 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07003768 spin_unlock(&mctz->lock);
3769 css_put(&mz->mem->css);
3770 loop++;
3771 /*
3772 * Could not reclaim anything and there are no more
3773 * mem cgroups to try or we seem to be looping without
3774 * reclaiming anything.
3775 */
3776 if (!nr_reclaimed &&
3777 (next_mz == NULL ||
3778 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3779 break;
3780 } while (!nr_reclaimed);
3781 if (next_mz)
3782 css_put(&next_mz->mem->css);
3783 return nr_reclaimed;
3784}
3785
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003786/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003787 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003788 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
3789 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003790static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003791 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003792{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003793 struct zone *zone;
3794 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003795 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003796 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c2008-02-07 00:14:39 -08003797 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003798 int ret = 0;
KAMEZAWA Hiroyuki072c56c2008-02-07 00:14:39 -08003799
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003800 zone = &NODE_DATA(node)->node_zones[zid];
3801 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07003802 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003803
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003804 loop = MEM_CGROUP_ZSTAT(mz, lru);
3805 /* give some margin against EBUSY etc...*/
3806 loop += 256;
3807 busy = NULL;
3808 while (loop--) {
Johannes Weiner5564e882011-03-23 16:42:29 -07003809 struct page *page;
3810
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003811 ret = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003812 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003813 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003814 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003815 break;
3816 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003817 pc = list_entry(list->prev, struct page_cgroup, lru);
3818 if (busy == pc) {
3819 list_move(&pc->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08003820 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003821 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003822 continue;
3823 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003824 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003825
Johannes Weiner6b3ae582011-03-23 16:42:30 -07003826 page = lookup_cgroup_page(pc);
Johannes Weiner5564e882011-03-23 16:42:29 -07003827
3828 ret = mem_cgroup_move_parent(page, pc, mem, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003829 if (ret == -ENOMEM)
3830 break;
3831
3832 if (ret == -EBUSY || ret == -EINVAL) {
3833 /* found lock contention or "pc" is obsolete. */
3834 busy = pc;
3835 cond_resched();
3836 } else
3837 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003838 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003839
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003840 if (!ret && !list_empty(list))
3841 return -EBUSY;
3842 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003843}
3844
3845/*
3846 * make mem_cgroup's charge to be 0 if there is no task.
3847 * This enables deleting this mem_cgroup.
3848 */
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003849static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003850{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003851 int ret;
3852 int node, zid, shrink;
3853 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003854 struct cgroup *cgrp = mem->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003855
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003856 css_get(&mem->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003857
3858 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003859 /* should free all ? */
3860 if (free_all)
3861 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003862move_account:
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003863 do {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003864 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003865 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003866 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003867 ret = -EINTR;
3868 if (signal_pending(current))
3869 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003870 /* This is for making all *used* pages to be on LRU. */
3871 lru_add_drain_all();
Michal Hockod38144b2011-07-26 16:08:28 -07003872 drain_all_stock_sync(mem);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003873 ret = 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07003874 mem_cgroup_start_move(mem);
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08003875 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003876 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07003877 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003878 for_each_lru(l) {
3879 ret = mem_cgroup_force_empty_list(mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003880 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003881 if (ret)
3882 break;
3883 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003884 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003885 if (ret)
3886 break;
3887 }
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07003888 mem_cgroup_end_move(mem);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003889 memcg_oom_recover(mem);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003890 /* it seems parent cgroup doesn't have enough mem */
3891 if (ret == -ENOMEM)
3892 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003893 cond_resched();
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003894 /* "ret" should also be checked to ensure all lists are empty. */
3895 } while (mem->res.usage > 0 || ret);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003896out:
3897 css_put(&mem->css);
3898 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003899
3900try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003901 /* returns EBUSY if there is a task or if we come here twice. */
3902 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003903 ret = -EBUSY;
3904 goto out;
3905 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003906 /* we call try-to-free pages for make this cgroup empty */
3907 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003908 /* try to free all pages in this cgroup */
3909 shrink = 1;
3910 while (nr_retries && mem->res.usage > 0) {
KAMEZAWA Hiroyuki82f9d482011-07-26 16:08:26 -07003911 struct memcg_scanrecord rec;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003912 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003913
3914 if (signal_pending(current)) {
3915 ret = -EINTR;
3916 goto out;
3917 }
KAMEZAWA Hiroyuki82f9d482011-07-26 16:08:26 -07003918 rec.context = SCAN_BY_SHRINK;
3919 rec.mem = mem;
3920 rec.root = mem;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003921 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
KAMEZAWA Hiroyuki82f9d482011-07-26 16:08:26 -07003922 false, &rec);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003923 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003924 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003925 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02003926 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003927 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003928
3929 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003930 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003931 /* try move_account...there may be some *locked* pages. */
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003932 goto move_account;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003933}
3934
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003935int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
3936{
3937 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
3938}
3939
3940
Balbir Singh18f59ea2009-01-07 18:08:07 -08003941static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
3942{
3943 return mem_cgroup_from_cont(cont)->use_hierarchy;
3944}
3945
3946static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
3947 u64 val)
3948{
3949 int retval = 0;
3950 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
3951 struct cgroup *parent = cont->parent;
3952 struct mem_cgroup *parent_mem = NULL;
3953
3954 if (parent)
3955 parent_mem = mem_cgroup_from_cont(parent);
3956
3957 cgroup_lock();
3958 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003959 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08003960 * in the child subtrees. If it is unset, then the change can
3961 * occur, provided the current cgroup has no children.
3962 *
3963 * For the root cgroup, parent_mem is NULL, we allow value to be
3964 * set if there are no children.
3965 */
3966 if ((!parent_mem || !parent_mem->use_hierarchy) &&
3967 (val == 1 || val == 0)) {
3968 if (list_empty(&cont->children))
3969 mem->use_hierarchy = val;
3970 else
3971 retval = -EBUSY;
3972 } else
3973 retval = -EINVAL;
3974 cgroup_unlock();
3975
3976 return retval;
3977}
3978
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003979
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003980static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *mem,
3981 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003982{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003983 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003984 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003985
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003986 /* Per-cpu values can be negative, use a signed accumulator */
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003987 for_each_mem_cgroup_tree(iter, mem)
3988 val += mem_cgroup_read_stat(iter, idx);
3989
3990 if (val < 0) /* race ? */
3991 val = 0;
3992 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003993}
3994
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003995static inline u64 mem_cgroup_usage(struct mem_cgroup *mem, bool swap)
3996{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003997 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003998
3999 if (!mem_cgroup_is_root(mem)) {
4000 if (!swap)
4001 return res_counter_read_u64(&mem->res, RES_USAGE);
4002 else
4003 return res_counter_read_u64(&mem->memsw, RES_USAGE);
4004 }
4005
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004006 val = mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_CACHE);
4007 val += mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004008
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004009 if (swap)
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004010 val += mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_SWAPOUT);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004011
4012 return val << PAGE_SHIFT;
4013}
4014
Paul Menage2c3daa72008-04-29 00:59:58 -07004015static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004016{
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004017 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004018 u64 val;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004019 int type, name;
4020
4021 type = MEMFILE_TYPE(cft->private);
4022 name = MEMFILE_ATTR(cft->private);
4023 switch (type) {
4024 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004025 if (name == RES_USAGE)
4026 val = mem_cgroup_usage(mem, false);
4027 else
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004028 val = res_counter_read_u64(&mem->res, name);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004029 break;
4030 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004031 if (name == RES_USAGE)
4032 val = mem_cgroup_usage(mem, true);
4033 else
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004034 val = res_counter_read_u64(&mem->memsw, name);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004035 break;
4036 default:
4037 BUG();
4038 break;
4039 }
4040 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004041}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004042/*
4043 * The user of this function is...
4044 * RES_LIMIT.
4045 */
Paul Menage856c13a2008-07-25 01:47:04 -07004046static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
4047 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004048{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004049 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004050 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004051 unsigned long long val;
4052 int ret;
4053
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004054 type = MEMFILE_TYPE(cft->private);
4055 name = MEMFILE_ATTR(cft->private);
4056 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004057 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07004058 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
4059 ret = -EINVAL;
4060 break;
4061 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004062 /* This function does all necessary parse...reuse it */
4063 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004064 if (ret)
4065 break;
4066 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004067 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004068 else
4069 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004070 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07004071 case RES_SOFT_LIMIT:
4072 ret = res_counter_memparse_write_strategy(buffer, &val);
4073 if (ret)
4074 break;
4075 /*
4076 * For memsw, soft limits are hard to implement in terms
4077 * of semantics, for now, we support soft limits for
4078 * control without swap
4079 */
4080 if (type == _MEM)
4081 ret = res_counter_set_soft_limit(&memcg->res, val);
4082 else
4083 ret = -EINVAL;
4084 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004085 default:
4086 ret = -EINVAL; /* should be BUG() ? */
4087 break;
4088 }
4089 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004090}
4091
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004092static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
4093 unsigned long long *mem_limit, unsigned long long *memsw_limit)
4094{
4095 struct cgroup *cgroup;
4096 unsigned long long min_limit, min_memsw_limit, tmp;
4097
4098 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4099 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4100 cgroup = memcg->css.cgroup;
4101 if (!memcg->use_hierarchy)
4102 goto out;
4103
4104 while (cgroup->parent) {
4105 cgroup = cgroup->parent;
4106 memcg = mem_cgroup_from_cont(cgroup);
4107 if (!memcg->use_hierarchy)
4108 break;
4109 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
4110 min_limit = min(min_limit, tmp);
4111 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4112 min_memsw_limit = min(min_memsw_limit, tmp);
4113 }
4114out:
4115 *mem_limit = min_limit;
4116 *memsw_limit = min_memsw_limit;
4117 return;
4118}
4119
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004120static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004121{
4122 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004123 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004124
4125 mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004126 type = MEMFILE_TYPE(event);
4127 name = MEMFILE_ATTR(event);
4128 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004129 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004130 if (type == _MEM)
4131 res_counter_reset_max(&mem->res);
4132 else
4133 res_counter_reset_max(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004134 break;
4135 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004136 if (type == _MEM)
4137 res_counter_reset_failcnt(&mem->res);
4138 else
4139 res_counter_reset_failcnt(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004140 break;
4141 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07004142
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07004143 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004144}
4145
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004146static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
4147 struct cftype *cft)
4148{
4149 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
4150}
4151
Daisuke Nishimura02491442010-03-10 15:22:17 -08004152#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004153static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
4154 struct cftype *cft, u64 val)
4155{
4156 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
4157
4158 if (val >= (1 << NR_MOVE_TYPE))
4159 return -EINVAL;
4160 /*
4161 * We check this value several times in both in can_attach() and
4162 * attach(), so we need cgroup lock to prevent this value from being
4163 * inconsistent.
4164 */
4165 cgroup_lock();
4166 mem->move_charge_at_immigrate = val;
4167 cgroup_unlock();
4168
4169 return 0;
4170}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004171#else
4172static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
4173 struct cftype *cft, u64 val)
4174{
4175 return -ENOSYS;
4176}
4177#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004178
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004179
4180/* For read statistics */
4181enum {
4182 MCS_CACHE,
4183 MCS_RSS,
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08004184 MCS_FILE_MAPPED,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004185 MCS_PGPGIN,
4186 MCS_PGPGOUT,
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004187 MCS_SWAP,
Ying Han456f9982011-05-26 16:25:38 -07004188 MCS_PGFAULT,
4189 MCS_PGMAJFAULT,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004190 MCS_INACTIVE_ANON,
4191 MCS_ACTIVE_ANON,
4192 MCS_INACTIVE_FILE,
4193 MCS_ACTIVE_FILE,
4194 MCS_UNEVICTABLE,
4195 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004196};
4197
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004198struct mcs_total_stat {
4199 s64 stat[NR_MCS_STAT];
4200};
4201
4202struct {
4203 char *local_name;
4204 char *total_name;
4205} memcg_stat_strings[NR_MCS_STAT] = {
4206 {"cache", "total_cache"},
4207 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07004208 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004209 {"pgpgin", "total_pgpgin"},
4210 {"pgpgout", "total_pgpgout"},
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004211 {"swap", "total_swap"},
Ying Han456f9982011-05-26 16:25:38 -07004212 {"pgfault", "total_pgfault"},
4213 {"pgmajfault", "total_pgmajfault"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004214 {"inactive_anon", "total_inactive_anon"},
4215 {"active_anon", "total_active_anon"},
4216 {"inactive_file", "total_inactive_file"},
4217 {"active_file", "total_active_file"},
4218 {"unevictable", "total_unevictable"}
4219};
4220
4221
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004222static void
4223mem_cgroup_get_local_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004224{
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004225 s64 val;
4226
4227 /* per cpu stat */
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004228 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004229 s->stat[MCS_CACHE] += val * PAGE_SIZE;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004230 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004231 s->stat[MCS_RSS] += val * PAGE_SIZE;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004232 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_FILE_MAPPED);
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08004233 s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
Johannes Weinere9f89742011-03-23 16:42:37 -07004234 val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGPGIN);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004235 s->stat[MCS_PGPGIN] += val;
Johannes Weinere9f89742011-03-23 16:42:37 -07004236 val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGPGOUT);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004237 s->stat[MCS_PGPGOUT] += val;
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004238 if (do_swap_account) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004239 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_SWAPOUT);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004240 s->stat[MCS_SWAP] += val * PAGE_SIZE;
4241 }
Ying Han456f9982011-05-26 16:25:38 -07004242 val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGFAULT);
4243 s->stat[MCS_PGFAULT] += val;
4244 val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGMAJFAULT);
4245 s->stat[MCS_PGMAJFAULT] += val;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004246
4247 /* per zone stat */
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004248 val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_INACTIVE_ANON));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004249 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004250 val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_ACTIVE_ANON));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004251 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004252 val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_INACTIVE_FILE));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004253 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004254 val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_ACTIVE_FILE));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004255 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004256 val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_UNEVICTABLE));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004257 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004258}
4259
4260static void
4261mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
4262{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004263 struct mem_cgroup *iter;
4264
4265 for_each_mem_cgroup_tree(iter, mem)
4266 mem_cgroup_get_local_stat(iter, s);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004267}
4268
Ying Han406eb0c2011-05-26 16:25:37 -07004269#ifdef CONFIG_NUMA
4270static int mem_control_numa_stat_show(struct seq_file *m, void *arg)
4271{
4272 int nid;
4273 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
4274 unsigned long node_nr;
4275 struct cgroup *cont = m->private;
4276 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
4277
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004278 total_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07004279 seq_printf(m, "total=%lu", total_nr);
4280 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004281 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07004282 seq_printf(m, " N%d=%lu", nid, node_nr);
4283 }
4284 seq_putc(m, '\n');
4285
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004286 file_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07004287 seq_printf(m, "file=%lu", file_nr);
4288 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004289 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
4290 LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07004291 seq_printf(m, " N%d=%lu", nid, node_nr);
4292 }
4293 seq_putc(m, '\n');
4294
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004295 anon_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07004296 seq_printf(m, "anon=%lu", anon_nr);
4297 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004298 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
4299 LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07004300 seq_printf(m, " N%d=%lu", nid, node_nr);
4301 }
4302 seq_putc(m, '\n');
4303
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004304 unevictable_nr = mem_cgroup_nr_lru_pages(mem_cont, BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07004305 seq_printf(m, "unevictable=%lu", unevictable_nr);
4306 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004307 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
4308 BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07004309 seq_printf(m, " N%d=%lu", nid, node_nr);
4310 }
4311 seq_putc(m, '\n');
4312 return 0;
4313}
4314#endif /* CONFIG_NUMA */
4315
Paul Menagec64745c2008-04-29 01:00:02 -07004316static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
4317 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004318{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004319 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004320 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004321 int i;
4322
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004323 memset(&mystat, 0, sizeof(mystat));
4324 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004325
Ying Han406eb0c2011-05-26 16:25:37 -07004326
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004327 for (i = 0; i < NR_MCS_STAT; i++) {
4328 if (i == MCS_SWAP && !do_swap_account)
4329 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004330 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004331 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004332
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004333 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004334 {
4335 unsigned long long limit, memsw_limit;
4336 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
4337 cb->fill(cb, "hierarchical_memory_limit", limit);
4338 if (do_swap_account)
4339 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
4340 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004341
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004342 memset(&mystat, 0, sizeof(mystat));
4343 mem_cgroup_get_total_stat(mem_cont, &mystat);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004344 for (i = 0; i < NR_MCS_STAT; i++) {
4345 if (i == MCS_SWAP && !do_swap_account)
4346 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004347 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004348 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004349
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004350#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08004351 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004352
4353 {
4354 int nid, zid;
4355 struct mem_cgroup_per_zone *mz;
4356 unsigned long recent_rotated[2] = {0, 0};
4357 unsigned long recent_scanned[2] = {0, 0};
4358
4359 for_each_online_node(nid)
4360 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
4361 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
4362
4363 recent_rotated[0] +=
4364 mz->reclaim_stat.recent_rotated[0];
4365 recent_rotated[1] +=
4366 mz->reclaim_stat.recent_rotated[1];
4367 recent_scanned[0] +=
4368 mz->reclaim_stat.recent_scanned[0];
4369 recent_scanned[1] +=
4370 mz->reclaim_stat.recent_scanned[1];
4371 }
4372 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
4373 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
4374 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
4375 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
4376 }
4377#endif
4378
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004379 return 0;
4380}
4381
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004382static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
4383{
4384 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4385
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07004386 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004387}
4388
4389static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
4390 u64 val)
4391{
4392 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4393 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08004394
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004395 if (val > 100)
4396 return -EINVAL;
4397
4398 if (cgrp->parent == NULL)
4399 return -EINVAL;
4400
4401 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08004402
4403 cgroup_lock();
4404
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004405 /* If under hierarchy, only empty-root can set this value */
4406 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08004407 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
4408 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004409 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08004410 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004411
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004412 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004413
Li Zefan068b38c2009-01-15 13:51:26 -08004414 cgroup_unlock();
4415
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004416 return 0;
4417}
4418
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004419static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
4420{
4421 struct mem_cgroup_threshold_ary *t;
4422 u64 usage;
4423 int i;
4424
4425 rcu_read_lock();
4426 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004427 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004428 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004429 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004430
4431 if (!t)
4432 goto unlock;
4433
4434 usage = mem_cgroup_usage(memcg, swap);
4435
4436 /*
4437 * current_threshold points to threshold just below usage.
4438 * If it's not true, a threshold was crossed after last
4439 * call of __mem_cgroup_threshold().
4440 */
Phil Carmody5407a562010-05-26 14:42:42 -07004441 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004442
4443 /*
4444 * Iterate backward over array of thresholds starting from
4445 * current_threshold and check if a threshold is crossed.
4446 * If none of thresholds below usage is crossed, we read
4447 * only one element of the array here.
4448 */
4449 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
4450 eventfd_signal(t->entries[i].eventfd, 1);
4451
4452 /* i = current_threshold + 1 */
4453 i++;
4454
4455 /*
4456 * Iterate forward over array of thresholds starting from
4457 * current_threshold+1 and check if a threshold is crossed.
4458 * If none of thresholds above usage is crossed, we read
4459 * only one element of the array here.
4460 */
4461 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
4462 eventfd_signal(t->entries[i].eventfd, 1);
4463
4464 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07004465 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004466unlock:
4467 rcu_read_unlock();
4468}
4469
4470static void mem_cgroup_threshold(struct mem_cgroup *memcg)
4471{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07004472 while (memcg) {
4473 __mem_cgroup_threshold(memcg, false);
4474 if (do_swap_account)
4475 __mem_cgroup_threshold(memcg, true);
4476
4477 memcg = parent_mem_cgroup(memcg);
4478 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004479}
4480
4481static int compare_thresholds(const void *a, const void *b)
4482{
4483 const struct mem_cgroup_threshold *_a = a;
4484 const struct mem_cgroup_threshold *_b = b;
4485
4486 return _a->threshold - _b->threshold;
4487}
4488
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004489static int mem_cgroup_oom_notify_cb(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004490{
4491 struct mem_cgroup_eventfd_list *ev;
4492
4493 list_for_each_entry(ev, &mem->oom_notify, list)
4494 eventfd_signal(ev->eventfd, 1);
4495 return 0;
4496}
4497
4498static void mem_cgroup_oom_notify(struct mem_cgroup *mem)
4499{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004500 struct mem_cgroup *iter;
4501
4502 for_each_mem_cgroup_tree(iter, mem)
4503 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004504}
4505
4506static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
4507 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004508{
4509 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004510 struct mem_cgroup_thresholds *thresholds;
4511 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004512 int type = MEMFILE_TYPE(cft->private);
4513 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004514 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004515
4516 ret = res_counter_memparse_write_strategy(args, &threshold);
4517 if (ret)
4518 return ret;
4519
4520 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004521
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004522 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004523 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004524 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004525 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004526 else
4527 BUG();
4528
4529 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4530
4531 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004532 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004533 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4534
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004535 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004536
4537 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004538 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004539 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004540 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004541 ret = -ENOMEM;
4542 goto unlock;
4543 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004544 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004545
4546 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004547 if (thresholds->primary) {
4548 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004549 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004550 }
4551
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004552 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004553 new->entries[size - 1].eventfd = eventfd;
4554 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004555
4556 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004557 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004558 compare_thresholds, NULL);
4559
4560 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004561 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004562 for (i = 0; i < size; i++) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004563 if (new->entries[i].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004564 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004565 * new->current_threshold will not be used until
4566 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004567 * it here.
4568 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004569 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004570 }
4571 }
4572
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004573 /* Free old spare buffer and save old primary buffer as spare */
4574 kfree(thresholds->spare);
4575 thresholds->spare = thresholds->primary;
4576
4577 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004578
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004579 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004580 synchronize_rcu();
4581
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004582unlock:
4583 mutex_unlock(&memcg->thresholds_lock);
4584
4585 return ret;
4586}
4587
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004588static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004589 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004590{
4591 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004592 struct mem_cgroup_thresholds *thresholds;
4593 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004594 int type = MEMFILE_TYPE(cft->private);
4595 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004596 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004597
4598 mutex_lock(&memcg->thresholds_lock);
4599 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004600 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004601 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004602 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004603 else
4604 BUG();
4605
4606 /*
4607 * Something went wrong if we trying to unregister a threshold
4608 * if we don't have thresholds
4609 */
4610 BUG_ON(!thresholds);
4611
4612 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4613
4614 /* Check if a threshold crossed before removing */
4615 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4616
4617 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004618 size = 0;
4619 for (i = 0; i < thresholds->primary->size; i++) {
4620 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004621 size++;
4622 }
4623
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004624 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004625
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004626 /* Set thresholds array to NULL if we don't have thresholds */
4627 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004628 kfree(new);
4629 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004630 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004631 }
4632
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004633 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004634
4635 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004636 new->current_threshold = -1;
4637 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
4638 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004639 continue;
4640
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004641 new->entries[j] = thresholds->primary->entries[i];
4642 if (new->entries[j].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004643 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004644 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004645 * until rcu_assign_pointer(), so it's safe to increment
4646 * it here.
4647 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004648 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004649 }
4650 j++;
4651 }
4652
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004653swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004654 /* Swap primary and spare array */
4655 thresholds->spare = thresholds->primary;
4656 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004657
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004658 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004659 synchronize_rcu();
4660
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004661 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004662}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004663
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004664static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
4665 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
4666{
4667 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4668 struct mem_cgroup_eventfd_list *event;
4669 int type = MEMFILE_TYPE(cft->private);
4670
4671 BUG_ON(type != _OOM_TYPE);
4672 event = kmalloc(sizeof(*event), GFP_KERNEL);
4673 if (!event)
4674 return -ENOMEM;
4675
Michal Hocko1af8efe2011-07-26 16:08:24 -07004676 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004677
4678 event->eventfd = eventfd;
4679 list_add(&event->list, &memcg->oom_notify);
4680
4681 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07004682 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004683 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07004684 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004685
4686 return 0;
4687}
4688
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004689static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004690 struct cftype *cft, struct eventfd_ctx *eventfd)
4691{
4692 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
4693 struct mem_cgroup_eventfd_list *ev, *tmp;
4694 int type = MEMFILE_TYPE(cft->private);
4695
4696 BUG_ON(type != _OOM_TYPE);
4697
Michal Hocko1af8efe2011-07-26 16:08:24 -07004698 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004699
4700 list_for_each_entry_safe(ev, tmp, &mem->oom_notify, list) {
4701 if (ev->eventfd == eventfd) {
4702 list_del(&ev->list);
4703 kfree(ev);
4704 }
4705 }
4706
Michal Hocko1af8efe2011-07-26 16:08:24 -07004707 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004708}
4709
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004710static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
4711 struct cftype *cft, struct cgroup_map_cb *cb)
4712{
4713 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
4714
4715 cb->fill(cb, "oom_kill_disable", mem->oom_kill_disable);
4716
Michal Hocko79dfdac2011-07-26 16:08:23 -07004717 if (atomic_read(&mem->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004718 cb->fill(cb, "under_oom", 1);
4719 else
4720 cb->fill(cb, "under_oom", 0);
4721 return 0;
4722}
4723
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004724static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
4725 struct cftype *cft, u64 val)
4726{
4727 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
4728 struct mem_cgroup *parent;
4729
4730 /* cannot set to root cgroup and only 0 and 1 are allowed */
4731 if (!cgrp->parent || !((val == 0) || (val == 1)))
4732 return -EINVAL;
4733
4734 parent = mem_cgroup_from_cont(cgrp->parent);
4735
4736 cgroup_lock();
4737 /* oom-kill-disable is a flag for subhierarchy. */
4738 if ((parent->use_hierarchy) ||
4739 (mem->use_hierarchy && !list_empty(&cgrp->children))) {
4740 cgroup_unlock();
4741 return -EINVAL;
4742 }
4743 mem->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07004744 if (!val)
4745 memcg_oom_recover(mem);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004746 cgroup_unlock();
4747 return 0;
4748}
4749
Ying Han406eb0c2011-05-26 16:25:37 -07004750#ifdef CONFIG_NUMA
4751static const struct file_operations mem_control_numa_stat_file_operations = {
4752 .read = seq_read,
4753 .llseek = seq_lseek,
4754 .release = single_release,
4755};
4756
4757static int mem_control_numa_stat_open(struct inode *unused, struct file *file)
4758{
4759 struct cgroup *cont = file->f_dentry->d_parent->d_fsdata;
4760
4761 file->f_op = &mem_control_numa_stat_file_operations;
4762 return single_open(file, mem_control_numa_stat_show, cont);
4763}
4764#endif /* CONFIG_NUMA */
4765
KAMEZAWA Hiroyuki82f9d482011-07-26 16:08:26 -07004766static int mem_cgroup_vmscan_stat_read(struct cgroup *cgrp,
4767 struct cftype *cft,
4768 struct cgroup_map_cb *cb)
4769{
4770 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
4771 char string[64];
4772 int i;
4773
4774 for (i = 0; i < NR_SCANSTATS; i++) {
4775 strcpy(string, scanstat_string[i]);
4776 strcat(string, SCANSTAT_WORD_LIMIT);
4777 cb->fill(cb, string, mem->scanstat.stats[SCAN_BY_LIMIT][i]);
4778 }
4779
4780 for (i = 0; i < NR_SCANSTATS; i++) {
4781 strcpy(string, scanstat_string[i]);
4782 strcat(string, SCANSTAT_WORD_SYSTEM);
4783 cb->fill(cb, string, mem->scanstat.stats[SCAN_BY_SYSTEM][i]);
4784 }
4785
4786 for (i = 0; i < NR_SCANSTATS; i++) {
4787 strcpy(string, scanstat_string[i]);
4788 strcat(string, SCANSTAT_WORD_LIMIT);
4789 strcat(string, SCANSTAT_WORD_HIERARCHY);
4790 cb->fill(cb, string, mem->scanstat.rootstats[SCAN_BY_LIMIT][i]);
4791 }
4792 for (i = 0; i < NR_SCANSTATS; i++) {
4793 strcpy(string, scanstat_string[i]);
4794 strcat(string, SCANSTAT_WORD_SYSTEM);
4795 strcat(string, SCANSTAT_WORD_HIERARCHY);
4796 cb->fill(cb, string, mem->scanstat.rootstats[SCAN_BY_SYSTEM][i]);
4797 }
4798 return 0;
4799}
4800
4801static int mem_cgroup_reset_vmscan_stat(struct cgroup *cgrp,
4802 unsigned int event)
4803{
4804 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
4805
4806 spin_lock(&mem->scanstat.lock);
4807 memset(&mem->scanstat.stats, 0, sizeof(mem->scanstat.stats));
4808 memset(&mem->scanstat.rootstats, 0, sizeof(mem->scanstat.rootstats));
4809 spin_unlock(&mem->scanstat.lock);
4810 return 0;
4811}
4812
4813
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004814static struct cftype mem_cgroup_files[] = {
4815 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004816 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004817 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07004818 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004819 .register_event = mem_cgroup_usage_register_event,
4820 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004821 },
4822 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004823 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004824 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004825 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004826 .read_u64 = mem_cgroup_read,
4827 },
4828 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004829 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004830 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07004831 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07004832 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004833 },
4834 {
Balbir Singh296c81d2009-09-23 15:56:36 -07004835 .name = "soft_limit_in_bytes",
4836 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
4837 .write_string = mem_cgroup_write,
4838 .read_u64 = mem_cgroup_read,
4839 },
4840 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004841 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004842 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004843 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07004844 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004845 },
Balbir Singh8697d332008-02-07 00:13:59 -08004846 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004847 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07004848 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004849 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004850 {
4851 .name = "force_empty",
4852 .trigger = mem_cgroup_force_empty_write,
4853 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08004854 {
4855 .name = "use_hierarchy",
4856 .write_u64 = mem_cgroup_hierarchy_write,
4857 .read_u64 = mem_cgroup_hierarchy_read,
4858 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004859 {
4860 .name = "swappiness",
4861 .read_u64 = mem_cgroup_swappiness_read,
4862 .write_u64 = mem_cgroup_swappiness_write,
4863 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004864 {
4865 .name = "move_charge_at_immigrate",
4866 .read_u64 = mem_cgroup_move_charge_read,
4867 .write_u64 = mem_cgroup_move_charge_write,
4868 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004869 {
4870 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004871 .read_map = mem_cgroup_oom_control_read,
4872 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004873 .register_event = mem_cgroup_oom_register_event,
4874 .unregister_event = mem_cgroup_oom_unregister_event,
4875 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
4876 },
Ying Han406eb0c2011-05-26 16:25:37 -07004877#ifdef CONFIG_NUMA
4878 {
4879 .name = "numa_stat",
4880 .open = mem_control_numa_stat_open,
KAMEZAWA Hiroyuki89577122011-06-15 15:08:41 -07004881 .mode = S_IRUGO,
Ying Han406eb0c2011-05-26 16:25:37 -07004882 },
4883#endif
KAMEZAWA Hiroyuki82f9d482011-07-26 16:08:26 -07004884 {
4885 .name = "vmscan_stat",
4886 .read_map = mem_cgroup_vmscan_stat_read,
4887 .trigger = mem_cgroup_reset_vmscan_stat,
4888 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004889};
4890
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004891#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4892static struct cftype memsw_cgroup_files[] = {
4893 {
4894 .name = "memsw.usage_in_bytes",
4895 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
4896 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004897 .register_event = mem_cgroup_usage_register_event,
4898 .unregister_event = mem_cgroup_usage_unregister_event,
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004899 },
4900 {
4901 .name = "memsw.max_usage_in_bytes",
4902 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
4903 .trigger = mem_cgroup_reset,
4904 .read_u64 = mem_cgroup_read,
4905 },
4906 {
4907 .name = "memsw.limit_in_bytes",
4908 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
4909 .write_string = mem_cgroup_write,
4910 .read_u64 = mem_cgroup_read,
4911 },
4912 {
4913 .name = "memsw.failcnt",
4914 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
4915 .trigger = mem_cgroup_reset,
4916 .read_u64 = mem_cgroup_read,
4917 },
4918};
4919
4920static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
4921{
4922 if (!do_swap_account)
4923 return 0;
4924 return cgroup_add_files(cont, ss, memsw_cgroup_files,
4925 ARRAY_SIZE(memsw_cgroup_files));
4926};
4927#else
4928static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
4929{
4930 return 0;
4931}
4932#endif
4933
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004934static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
4935{
4936 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004937 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07004938 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004939 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004940 /*
4941 * This routine is called against possible nodes.
4942 * But it's BUG to call kmalloc() against offline node.
4943 *
4944 * TODO: this routine can waste much memory for nodes which will
4945 * never be onlined. It's better to use memory hotplug callback
4946 * function.
4947 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004948 if (!node_state(node, N_NORMAL_MEMORY))
4949 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08004950 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004951 if (!pn)
4952 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004953
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004954 mem->info.nodeinfo[node] = pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004955 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4956 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07004957 for_each_lru(l)
4958 INIT_LIST_HEAD(&mz->lists[l]);
Balbir Singhf64c3f52009-09-23 15:56:37 -07004959 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07004960 mz->on_tree = false;
4961 mz->mem = mem;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004962 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004963 return 0;
4964}
4965
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004966static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
4967{
4968 kfree(mem->info.nodeinfo[node]);
4969}
4970
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004971static struct mem_cgroup *mem_cgroup_alloc(void)
4972{
4973 struct mem_cgroup *mem;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004974 int size = sizeof(struct mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004975
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004976 /* Can be very big if MAX_NUMNODES is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08004977 if (size < PAGE_SIZE)
Jesper Juhl17295c82011-01-13 15:47:42 -08004978 mem = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004979 else
Jesper Juhl17295c82011-01-13 15:47:42 -08004980 mem = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004981
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07004982 if (!mem)
4983 return NULL;
4984
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004985 mem->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004986 if (!mem->stat)
4987 goto out_free;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07004988 spin_lock_init(&mem->pcp_counter_lock);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004989 return mem;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004990
4991out_free:
4992 if (size < PAGE_SIZE)
4993 kfree(mem);
4994 else
4995 vfree(mem);
4996 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004997}
4998
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004999/*
5000 * At destroying mem_cgroup, references from swap_cgroup can remain.
5001 * (scanning all at force_empty is too costly...)
5002 *
5003 * Instead of clearing all references at force_empty, we remember
5004 * the number of reference from swap_cgroup and free mem_cgroup when
5005 * it goes down to 0.
5006 *
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08005007 * Removal of cgroup itself succeeds regardless of refs from swap.
5008 */
5009
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08005010static void __mem_cgroup_free(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005011{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08005012 int node;
5013
Balbir Singhf64c3f52009-09-23 15:56:37 -07005014 mem_cgroup_remove_from_trees(mem);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005015 free_css_id(&mem_cgroup_subsys, &mem->css);
5016
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08005017 for_each_node_state(node, N_POSSIBLE)
5018 free_mem_cgroup_per_zone_info(mem, node);
5019
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08005020 free_percpu(mem->stat);
5021 if (sizeof(struct mem_cgroup) < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005022 kfree(mem);
5023 else
5024 vfree(mem);
5025}
5026
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08005027static void mem_cgroup_get(struct mem_cgroup *mem)
5028{
5029 atomic_inc(&mem->refcnt);
5030}
5031
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005032static void __mem_cgroup_put(struct mem_cgroup *mem, int count)
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08005033{
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005034 if (atomic_sub_and_test(count, &mem->refcnt)) {
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005035 struct mem_cgroup *parent = parent_mem_cgroup(mem);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08005036 __mem_cgroup_free(mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005037 if (parent)
5038 mem_cgroup_put(parent);
5039 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08005040}
5041
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005042static void mem_cgroup_put(struct mem_cgroup *mem)
5043{
5044 __mem_cgroup_put(mem, 1);
5045}
5046
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005047/*
5048 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
5049 */
5050static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
5051{
5052 if (!mem->res.parent)
5053 return NULL;
5054 return mem_cgroup_from_res_counter(mem->res.parent, res);
5055}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005056
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005057#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
5058static void __init enable_swap_cgroup(void)
5059{
Hirokazu Takahashif8d66542009-01-07 18:08:02 -08005060 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005061 do_swap_account = 1;
5062}
5063#else
5064static void __init enable_swap_cgroup(void)
5065{
5066}
5067#endif
5068
Balbir Singhf64c3f52009-09-23 15:56:37 -07005069static int mem_cgroup_soft_limit_tree_init(void)
5070{
5071 struct mem_cgroup_tree_per_node *rtpn;
5072 struct mem_cgroup_tree_per_zone *rtpz;
5073 int tmp, node, zone;
5074
5075 for_each_node_state(node, N_POSSIBLE) {
5076 tmp = node;
5077 if (!node_state(node, N_NORMAL_MEMORY))
5078 tmp = -1;
5079 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
5080 if (!rtpn)
5081 return 1;
5082
5083 soft_limit_tree.rb_tree_per_node[node] = rtpn;
5084
5085 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
5086 rtpz = &rtpn->rb_tree_per_zone[zone];
5087 rtpz->rb_root = RB_ROOT;
5088 spin_lock_init(&rtpz->lock);
5089 }
5090 }
5091 return 0;
5092}
5093
Li Zefan0eb253e2009-01-15 13:51:25 -08005094static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005095mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
5096{
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005097 struct mem_cgroup *mem, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005098 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005099 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005100
Jan Blunckc8dad2b2009-01-07 18:07:53 -08005101 mem = mem_cgroup_alloc();
5102 if (!mem)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005103 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08005104
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005105 for_each_node_state(node, N_POSSIBLE)
5106 if (alloc_mem_cgroup_per_zone_info(mem, node))
5107 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07005108
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005109 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005110 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08005111 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005112 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005113 parent = NULL;
Balbir Singh4b3bde42009-09-23 15:56:32 -07005114 root_mem_cgroup = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07005115 if (mem_cgroup_soft_limit_tree_init())
5116 goto free_out;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08005117 for_each_possible_cpu(cpu) {
5118 struct memcg_stock_pcp *stock =
5119 &per_cpu(memcg_stock, cpu);
5120 INIT_WORK(&stock->work, drain_local_stock);
5121 }
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07005122 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005123 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005124 parent = mem_cgroup_from_cont(cont->parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005125 mem->use_hierarchy = parent->use_hierarchy;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005126 mem->oom_kill_disable = parent->oom_kill_disable;
Balbir Singh18f59ea2009-01-07 18:08:07 -08005127 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005128
Balbir Singh18f59ea2009-01-07 18:08:07 -08005129 if (parent && parent->use_hierarchy) {
5130 res_counter_init(&mem->res, &parent->res);
5131 res_counter_init(&mem->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005132 /*
5133 * We increment refcnt of the parent to ensure that we can
5134 * safely access it on res_counter_charge/uncharge.
5135 * This refcnt will be decremented when freeing this
5136 * mem_cgroup(see mem_cgroup_put).
5137 */
5138 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005139 } else {
5140 res_counter_init(&mem->res, NULL);
5141 res_counter_init(&mem->memsw, NULL);
5142 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005143 mem->last_scanned_child = 0;
Ying Han889976d2011-05-26 16:25:33 -07005144 mem->last_scanned_node = MAX_NUMNODES;
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005145 INIT_LIST_HEAD(&mem->oom_notify);
Balbir Singh6d61ef42009-01-07 18:08:06 -08005146
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005147 if (parent)
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07005148 mem->swappiness = mem_cgroup_swappiness(parent);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08005149 atomic_set(&mem->refcnt, 1);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005150 mem->move_charge_at_immigrate = 0;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005151 mutex_init(&mem->thresholds_lock);
KAMEZAWA Hiroyuki82f9d482011-07-26 16:08:26 -07005152 spin_lock_init(&mem->scanstat.lock);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005153 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005154free_out:
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08005155 __mem_cgroup_free(mem);
Balbir Singh4b3bde42009-09-23 15:56:32 -07005156 root_mem_cgroup = NULL;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005157 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005158}
5159
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07005160static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005161 struct cgroup *cont)
5162{
5163 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07005164
5165 return mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005166}
5167
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005168static void mem_cgroup_destroy(struct cgroup_subsys *ss,
5169 struct cgroup *cont)
5170{
Daisuke Nishimurac268e992009-01-15 13:51:13 -08005171 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08005172
Daisuke Nishimurac268e992009-01-15 13:51:13 -08005173 mem_cgroup_put(mem);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005174}
5175
5176static int mem_cgroup_populate(struct cgroup_subsys *ss,
5177 struct cgroup *cont)
5178{
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08005179 int ret;
5180
5181 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
5182 ARRAY_SIZE(mem_cgroup_files));
5183
5184 if (!ret)
5185 ret = register_memsw_files(cont, ss);
5186 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005187}
5188
Daisuke Nishimura02491442010-03-10 15:22:17 -08005189#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005190/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005191#define PRECHARGE_COUNT_AT_ONCE 256
5192static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005193{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005194 int ret = 0;
5195 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005196 struct mem_cgroup *mem = mc.to;
5197
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005198 if (mem_cgroup_is_root(mem)) {
5199 mc.precharge += count;
5200 /* we don't need css_get for root */
5201 return ret;
5202 }
5203 /* try to charge at once */
5204 if (count > 1) {
5205 struct res_counter *dummy;
5206 /*
5207 * "mem" cannot be under rmdir() because we've already checked
5208 * by cgroup_lock_live_cgroup() that it is not removed and we
5209 * are still under the same cgroup_mutex. So we can postpone
5210 * css_get().
5211 */
5212 if (res_counter_charge(&mem->res, PAGE_SIZE * count, &dummy))
5213 goto one_by_one;
5214 if (do_swap_account && res_counter_charge(&mem->memsw,
5215 PAGE_SIZE * count, &dummy)) {
5216 res_counter_uncharge(&mem->res, PAGE_SIZE * count);
5217 goto one_by_one;
5218 }
5219 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005220 return ret;
5221 }
5222one_by_one:
5223 /* fall back to one by one charge */
5224 while (count--) {
5225 if (signal_pending(current)) {
5226 ret = -EINTR;
5227 break;
5228 }
5229 if (!batch_count--) {
5230 batch_count = PRECHARGE_COUNT_AT_ONCE;
5231 cond_resched();
5232 }
Johannes Weiner7ec99d62011-03-23 16:42:36 -07005233 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, 1, &mem, false);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005234 if (ret || !mem)
5235 /* mem_cgroup_clear_mc() will do uncharge later */
5236 return -ENOMEM;
5237 mc.precharge++;
5238 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005239 return ret;
5240}
5241
5242/**
5243 * is_target_pte_for_mc - check a pte whether it is valid for move charge
5244 * @vma: the vma the pte to be checked belongs
5245 * @addr: the address corresponding to the pte to be checked
5246 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08005247 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005248 *
5249 * Returns
5250 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
5251 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
5252 * move charge. if @target is not NULL, the page is stored in target->page
5253 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08005254 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
5255 * target for charge migration. if @target is not NULL, the entry is stored
5256 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005257 *
5258 * Called with pte lock held.
5259 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005260union mc_target {
5261 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005262 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005263};
5264
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005265enum mc_target_type {
5266 MC_TARGET_NONE, /* not used */
5267 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08005268 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005269};
5270
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005271static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
5272 unsigned long addr, pte_t ptent)
5273{
5274 struct page *page = vm_normal_page(vma, addr, ptent);
5275
5276 if (!page || !page_mapped(page))
5277 return NULL;
5278 if (PageAnon(page)) {
5279 /* we don't move shared anon */
5280 if (!move_anon() || page_mapcount(page) > 2)
5281 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005282 } else if (!move_file())
5283 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005284 return NULL;
5285 if (!get_page_unless_zero(page))
5286 return NULL;
5287
5288 return page;
5289}
5290
5291static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5292 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5293{
5294 int usage_count;
5295 struct page *page = NULL;
5296 swp_entry_t ent = pte_to_swp_entry(ptent);
5297
5298 if (!move_anon() || non_swap_entry(ent))
5299 return NULL;
5300 usage_count = mem_cgroup_count_swap_user(ent, &page);
5301 if (usage_count > 1) { /* we don't move shared anon */
5302 if (page)
5303 put_page(page);
5304 return NULL;
5305 }
5306 if (do_swap_account)
5307 entry->val = ent.val;
5308
5309 return page;
5310}
5311
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005312static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
5313 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5314{
5315 struct page *page = NULL;
5316 struct inode *inode;
5317 struct address_space *mapping;
5318 pgoff_t pgoff;
5319
5320 if (!vma->vm_file) /* anonymous vma */
5321 return NULL;
5322 if (!move_file())
5323 return NULL;
5324
5325 inode = vma->vm_file->f_path.dentry->d_inode;
5326 mapping = vma->vm_file->f_mapping;
5327 if (pte_none(ptent))
5328 pgoff = linear_page_index(vma, addr);
5329 else /* pte_file(ptent) is true */
5330 pgoff = pte_to_pgoff(ptent);
5331
5332 /* page is moved even if it's not RSS of this task(page-faulted). */
5333 if (!mapping_cap_swap_backed(mapping)) { /* normal file */
5334 page = find_get_page(mapping, pgoff);
5335 } else { /* shmem/tmpfs file. we should take account of swap too. */
5336 swp_entry_t ent;
5337 mem_cgroup_get_shmem_target(inode, pgoff, &page, &ent);
5338 if (do_swap_account)
5339 entry->val = ent.val;
5340 }
5341
5342 return page;
5343}
5344
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005345static int is_target_pte_for_mc(struct vm_area_struct *vma,
5346 unsigned long addr, pte_t ptent, union mc_target *target)
5347{
Daisuke Nishimura02491442010-03-10 15:22:17 -08005348 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005349 struct page_cgroup *pc;
5350 int ret = 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005351 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005352
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005353 if (pte_present(ptent))
5354 page = mc_handle_present_pte(vma, addr, ptent);
5355 else if (is_swap_pte(ptent))
5356 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005357 else if (pte_none(ptent) || pte_file(ptent))
5358 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005359
5360 if (!page && !ent.val)
Daisuke Nishimura02491442010-03-10 15:22:17 -08005361 return 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005362 if (page) {
5363 pc = lookup_page_cgroup(page);
5364 /*
5365 * Do only loose check w/o page_cgroup lock.
5366 * mem_cgroup_move_account() checks the pc is valid or not under
5367 * the lock.
5368 */
5369 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
5370 ret = MC_TARGET_PAGE;
5371 if (target)
5372 target->page = page;
5373 }
5374 if (!ret || !target)
5375 put_page(page);
5376 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005377 /* There is a swap entry and a page doesn't exist or isn't charged */
5378 if (ent.val && !ret &&
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005379 css_id(&mc.from->css) == lookup_swap_cgroup(ent)) {
5380 ret = MC_TARGET_SWAP;
5381 if (target)
5382 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005383 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005384 return ret;
5385}
5386
5387static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
5388 unsigned long addr, unsigned long end,
5389 struct mm_walk *walk)
5390{
5391 struct vm_area_struct *vma = walk->private;
5392 pte_t *pte;
5393 spinlock_t *ptl;
5394
Dave Hansen03319322011-03-22 16:32:56 -07005395 split_huge_page_pmd(walk->mm, pmd);
5396
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005397 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5398 for (; addr != end; pte++, addr += PAGE_SIZE)
5399 if (is_target_pte_for_mc(vma, addr, *pte, NULL))
5400 mc.precharge++; /* increment precharge temporarily */
5401 pte_unmap_unlock(pte - 1, ptl);
5402 cond_resched();
5403
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005404 return 0;
5405}
5406
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005407static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
5408{
5409 unsigned long precharge;
5410 struct vm_area_struct *vma;
5411
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005412 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005413 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5414 struct mm_walk mem_cgroup_count_precharge_walk = {
5415 .pmd_entry = mem_cgroup_count_precharge_pte_range,
5416 .mm = mm,
5417 .private = vma,
5418 };
5419 if (is_vm_hugetlb_page(vma))
5420 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005421 walk_page_range(vma->vm_start, vma->vm_end,
5422 &mem_cgroup_count_precharge_walk);
5423 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005424 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005425
5426 precharge = mc.precharge;
5427 mc.precharge = 0;
5428
5429 return precharge;
5430}
5431
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005432static int mem_cgroup_precharge_mc(struct mm_struct *mm)
5433{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005434 unsigned long precharge = mem_cgroup_count_precharge(mm);
5435
5436 VM_BUG_ON(mc.moving_task);
5437 mc.moving_task = current;
5438 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005439}
5440
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005441/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
5442static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005443{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005444 struct mem_cgroup *from = mc.from;
5445 struct mem_cgroup *to = mc.to;
5446
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005447 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005448 if (mc.precharge) {
5449 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
5450 mc.precharge = 0;
5451 }
5452 /*
5453 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
5454 * we must uncharge here.
5455 */
5456 if (mc.moved_charge) {
5457 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
5458 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005459 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005460 /* we must fixup refcnts and charges */
5461 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005462 /* uncharge swap account from the old cgroup */
5463 if (!mem_cgroup_is_root(mc.from))
5464 res_counter_uncharge(&mc.from->memsw,
5465 PAGE_SIZE * mc.moved_swap);
5466 __mem_cgroup_put(mc.from, mc.moved_swap);
5467
5468 if (!mem_cgroup_is_root(mc.to)) {
5469 /*
5470 * we charged both to->res and to->memsw, so we should
5471 * uncharge to->res.
5472 */
5473 res_counter_uncharge(&mc.to->res,
5474 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005475 }
5476 /* we've already done mem_cgroup_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005477 mc.moved_swap = 0;
5478 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005479 memcg_oom_recover(from);
5480 memcg_oom_recover(to);
5481 wake_up_all(&mc.waitq);
5482}
5483
5484static void mem_cgroup_clear_mc(void)
5485{
5486 struct mem_cgroup *from = mc.from;
5487
5488 /*
5489 * we must clear moving_task before waking up waiters at the end of
5490 * task migration.
5491 */
5492 mc.moving_task = NULL;
5493 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005494 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005495 mc.from = NULL;
5496 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005497 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005498 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005499}
5500
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005501static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
5502 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005503 struct task_struct *p)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005504{
5505 int ret = 0;
5506 struct mem_cgroup *mem = mem_cgroup_from_cont(cgroup);
5507
5508 if (mem->move_charge_at_immigrate) {
5509 struct mm_struct *mm;
5510 struct mem_cgroup *from = mem_cgroup_from_task(p);
5511
5512 VM_BUG_ON(from == mem);
5513
5514 mm = get_task_mm(p);
5515 if (!mm)
5516 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005517 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005518 if (mm->owner == p) {
5519 VM_BUG_ON(mc.from);
5520 VM_BUG_ON(mc.to);
5521 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005522 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005523 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005524 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005525 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005526 mc.from = from;
5527 mc.to = mem;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005528 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005529 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005530
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005531 ret = mem_cgroup_precharge_mc(mm);
5532 if (ret)
5533 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005534 }
5535 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005536 }
5537 return ret;
5538}
5539
5540static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
5541 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005542 struct task_struct *p)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005543{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005544 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005545}
5546
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005547static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
5548 unsigned long addr, unsigned long end,
5549 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005550{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005551 int ret = 0;
5552 struct vm_area_struct *vma = walk->private;
5553 pte_t *pte;
5554 spinlock_t *ptl;
5555
Dave Hansen03319322011-03-22 16:32:56 -07005556 split_huge_page_pmd(walk->mm, pmd);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005557retry:
5558 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5559 for (; addr != end; addr += PAGE_SIZE) {
5560 pte_t ptent = *(pte++);
5561 union mc_target target;
5562 int type;
5563 struct page *page;
5564 struct page_cgroup *pc;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005565 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005566
5567 if (!mc.precharge)
5568 break;
5569
5570 type = is_target_pte_for_mc(vma, addr, ptent, &target);
5571 switch (type) {
5572 case MC_TARGET_PAGE:
5573 page = target.page;
5574 if (isolate_lru_page(page))
5575 goto put;
5576 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07005577 if (!mem_cgroup_move_account(page, 1, pc,
5578 mc.from, mc.to, false)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005579 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005580 /* we uncharge from mc.from later. */
5581 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005582 }
5583 putback_lru_page(page);
5584put: /* is_target_pte_for_mc() gets the page */
5585 put_page(page);
5586 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005587 case MC_TARGET_SWAP:
5588 ent = target.ent;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005589 if (!mem_cgroup_move_swap_account(ent,
5590 mc.from, mc.to, false)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08005591 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005592 /* we fixup refcnts and charges later. */
5593 mc.moved_swap++;
5594 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08005595 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005596 default:
5597 break;
5598 }
5599 }
5600 pte_unmap_unlock(pte - 1, ptl);
5601 cond_resched();
5602
5603 if (addr != end) {
5604 /*
5605 * We have consumed all precharges we got in can_attach().
5606 * We try charge one by one, but don't do any additional
5607 * charges to mc.to if we have failed in charge once in attach()
5608 * phase.
5609 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005610 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005611 if (!ret)
5612 goto retry;
5613 }
5614
5615 return ret;
5616}
5617
5618static void mem_cgroup_move_charge(struct mm_struct *mm)
5619{
5620 struct vm_area_struct *vma;
5621
5622 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005623retry:
5624 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
5625 /*
5626 * Someone who are holding the mmap_sem might be waiting in
5627 * waitq. So we cancel all extra charges, wake up all waiters,
5628 * and retry. Because we cancel precharges, we might not be able
5629 * to move enough charges, but moving charge is a best-effort
5630 * feature anyway, so it wouldn't be a big problem.
5631 */
5632 __mem_cgroup_clear_mc();
5633 cond_resched();
5634 goto retry;
5635 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005636 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5637 int ret;
5638 struct mm_walk mem_cgroup_move_charge_walk = {
5639 .pmd_entry = mem_cgroup_move_charge_pte_range,
5640 .mm = mm,
5641 .private = vma,
5642 };
5643 if (is_vm_hugetlb_page(vma))
5644 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005645 ret = walk_page_range(vma->vm_start, vma->vm_end,
5646 &mem_cgroup_move_charge_walk);
5647 if (ret)
5648 /*
5649 * means we have consumed all precharges and failed in
5650 * doing additional charge. Just abandon here.
5651 */
5652 break;
5653 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005654 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005655}
5656
Balbir Singh67e465a2008-02-07 00:13:54 -08005657static void mem_cgroup_move_task(struct cgroup_subsys *ss,
5658 struct cgroup *cont,
5659 struct cgroup *old_cont,
Ben Blumf780bdb2011-05-26 16:25:19 -07005660 struct task_struct *p)
Balbir Singh67e465a2008-02-07 00:13:54 -08005661{
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005662 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005663
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005664 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005665 if (mc.to)
5666 mem_cgroup_move_charge(mm);
5667 put_swap_token(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005668 mmput(mm);
5669 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005670 if (mc.to)
5671 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08005672}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005673#else /* !CONFIG_MMU */
5674static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
5675 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005676 struct task_struct *p)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005677{
5678 return 0;
5679}
5680static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
5681 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005682 struct task_struct *p)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005683{
5684}
5685static void mem_cgroup_move_task(struct cgroup_subsys *ss,
5686 struct cgroup *cont,
5687 struct cgroup *old_cont,
Ben Blumf780bdb2011-05-26 16:25:19 -07005688 struct task_struct *p)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005689{
5690}
5691#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08005692
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005693struct cgroup_subsys mem_cgroup_subsys = {
5694 .name = "memory",
5695 .subsys_id = mem_cgroup_subsys_id,
5696 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005697 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005698 .destroy = mem_cgroup_destroy,
5699 .populate = mem_cgroup_populate,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005700 .can_attach = mem_cgroup_can_attach,
5701 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08005702 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005703 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005704 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005705};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005706
5707#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08005708static int __init enable_swap_account(char *s)
5709{
5710 /* consider enabled if no parameter or 1 is given */
Michal Hockoa2c89902011-05-24 17:12:50 -07005711 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08005712 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07005713 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08005714 really_do_swap_account = 0;
5715 return 1;
5716}
Michal Hockoa2c89902011-05-24 17:12:50 -07005717__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005718
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005719#endif