blob: 9fbcff71245ed429f58f6cb4eb3c4c81b7da3ee7 [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>
Paul Gortmakerb9e15ba2011-05-26 16:00:52 -040036#include <linux/export.h>
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -080037#include <linux/mutex.h>
Balbir Singhf64c3f52009-09-23 15:56:37 -070038#include <linux/rbtree.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 Hiroyuki453a9bf32011-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 Hiroyuki453a9bf32011-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 Hiroyuki072c56c12008-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
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700205static void mem_cgroup_threshold(struct mem_cgroup *memcg);
206static void mem_cgroup_oom_notify(struct mem_cgroup *memcg);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800207
Balbir Singhf64c3f52009-09-23 15:56:37 -0700208/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800209 * The memory controller data structure. The memory controller controls both
210 * page cache and RSS per cgroup. We would eventually like to provide
211 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
212 * to help the administrator determine what knobs to tune.
213 *
214 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800215 * we hit the water mark. May be even add a low water mark, such that
216 * no reclaim occurs from a cgroup at it's low water mark, this is
217 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800218 */
219struct mem_cgroup {
220 struct cgroup_subsys_state css;
221 /*
222 * the counter to account for memory usage
223 */
224 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800225 /*
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800226 * the counter to account for mem+swap usage.
227 */
228 struct res_counter memsw;
229 /*
Glauber Costae5671df2011-12-11 21:47:01 +0000230 * the counter to account for kmem usage.
231 */
232 struct res_counter kmem;
233 /*
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800234 * Per cgroup active and inactive list, similar to the
235 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800236 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800237 struct mem_cgroup_lru_info info;
Balbir Singh6d61ef42009-01-07 18:08:06 -0800238 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200239 * While reclaiming in a hierarchy, we cache the last child we
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700240 * reclaimed from.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800241 */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700242 int last_scanned_child;
Ying Han889976d2011-05-26 16:25:33 -0700243 int last_scanned_node;
244#if MAX_NUMNODES > 1
245 nodemask_t scan_nodes;
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700246 atomic_t numainfo_events;
247 atomic_t numainfo_updating;
Ying Han889976d2011-05-26 16:25:33 -0700248#endif
Balbir Singh18f59ea2009-01-07 18:08:07 -0800249 /*
250 * Should the accounting and control be hierarchical, per subtree?
251 */
252 bool use_hierarchy;
Michal Hocko79dfdac2011-07-26 16:08:23 -0700253
254 bool oom_lock;
255 atomic_t under_oom;
256
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800257 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800258
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -0700259 int swappiness;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -0700260 /* OOM-Killer disable */
261 int oom_kill_disable;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800262
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700263 /* set when res.limit == memsw.limit */
264 bool memsw_is_minimum;
265
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800266 /* protect arrays of thresholds */
267 struct mutex thresholds_lock;
268
269 /* thresholds for memory usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700270 struct mem_cgroup_thresholds thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700271
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800272 /* thresholds for mem+swap usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700273 struct mem_cgroup_thresholds memsw_thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700274
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700275 /* For oom notifier event fd */
276 struct list_head oom_notify;
Johannes Weiner185efc02011-09-14 16:21:58 -0700277
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800278 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800279 * Should we move charges of a task when a task is moved into this
280 * mem_cgroup ? And what type of charges should we move ?
281 */
282 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800283 /*
Glauber Costae5671df2011-12-11 21:47:01 +0000284 * Should kernel memory limits be stabilished independently
285 * from user memory ?
286 */
287 int kmem_independent_accounting;
288 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800289 * percpu counter.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800290 */
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800291 struct mem_cgroup_stat_cpu *stat;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700292 /*
293 * used when a cpu is offlined or other synchronizations
294 * See mem_cgroup_read_stat().
295 */
296 struct mem_cgroup_stat_cpu nocpu_base;
297 spinlock_t pcp_counter_lock;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800298};
299
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800300/* Stuffs for move charges at task migration. */
301/*
302 * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a
303 * left-shifted bitmap of these types.
304 */
305enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800306 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700307 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800308 NR_MOVE_TYPE,
309};
310
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800311/* "mc" and its members are protected by cgroup_mutex */
312static struct move_charge_struct {
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -0800313 spinlock_t lock; /* for from, to */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800314 struct mem_cgroup *from;
315 struct mem_cgroup *to;
316 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800317 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800318 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800319 struct task_struct *moving_task; /* a task moving charges */
320 wait_queue_head_t waitq; /* a waitq for other context */
321} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700322 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800323 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
324};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800325
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700326static bool move_anon(void)
327{
328 return test_bit(MOVE_CHARGE_TYPE_ANON,
329 &mc.to->move_charge_at_immigrate);
330}
331
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700332static bool move_file(void)
333{
334 return test_bit(MOVE_CHARGE_TYPE_FILE,
335 &mc.to->move_charge_at_immigrate);
336}
337
Balbir Singh4e416952009-09-23 15:56:39 -0700338/*
339 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
340 * limit reclaim to prevent infinite loops, if they ever occur.
341 */
342#define MEM_CGROUP_MAX_RECLAIM_LOOPS (100)
343#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2)
344
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800345enum charge_type {
346 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
347 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700348 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700349 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800350 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700351 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700352 NR_CHARGE_TYPE,
353};
354
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800355/* for encoding cft->private value on file */
Glauber Costae5671df2011-12-11 21:47:01 +0000356
357enum mem_type {
358 _MEM = 0,
359 _MEMSWAP,
360 _OOM_TYPE,
361 _KMEM,
362};
363
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800364#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
365#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
366#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700367/* Used for OOM nofiier */
368#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800369
Balbir Singh75822b42009-09-23 15:56:38 -0700370/*
371 * Reclaim flags for mem_cgroup_hierarchical_reclaim
372 */
373#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
374#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
375#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
376#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
Balbir Singh4e416952009-09-23 15:56:39 -0700377#define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
378#define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
Balbir Singh75822b42009-09-23 15:56:38 -0700379
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700380static void mem_cgroup_get(struct mem_cgroup *memcg);
381static void mem_cgroup_put(struct mem_cgroup *memcg);
382static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg);
383static void drain_all_stock_async(struct mem_cgroup *memcg);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800384
Balbir Singhf64c3f52009-09-23 15:56:37 -0700385static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700386mem_cgroup_zoneinfo(struct mem_cgroup *memcg, int nid, int zid)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700387{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700388 return &memcg->info.nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700389}
390
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700391struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100392{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700393 return &memcg->css;
Wu Fengguangd3242362009-12-16 12:19:59 +0100394}
395
Balbir Singhf64c3f52009-09-23 15:56:37 -0700396static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700397page_cgroup_zoneinfo(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700398{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700399 int nid = page_to_nid(page);
400 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700401
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700402 return mem_cgroup_zoneinfo(memcg, nid, zid);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700403}
404
405static struct mem_cgroup_tree_per_zone *
406soft_limit_tree_node_zone(int nid, int zid)
407{
408 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
409}
410
411static struct mem_cgroup_tree_per_zone *
412soft_limit_tree_from_page(struct page *page)
413{
414 int nid = page_to_nid(page);
415 int zid = page_zonenum(page);
416
417 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
418}
419
420static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700421__mem_cgroup_insert_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700422 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700423 struct mem_cgroup_tree_per_zone *mctz,
424 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700425{
426 struct rb_node **p = &mctz->rb_root.rb_node;
427 struct rb_node *parent = NULL;
428 struct mem_cgroup_per_zone *mz_node;
429
430 if (mz->on_tree)
431 return;
432
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700433 mz->usage_in_excess = new_usage_in_excess;
434 if (!mz->usage_in_excess)
435 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700436 while (*p) {
437 parent = *p;
438 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
439 tree_node);
440 if (mz->usage_in_excess < mz_node->usage_in_excess)
441 p = &(*p)->rb_left;
442 /*
443 * We can't avoid mem cgroups that are over their soft
444 * limit by the same amount
445 */
446 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
447 p = &(*p)->rb_right;
448 }
449 rb_link_node(&mz->tree_node, parent, p);
450 rb_insert_color(&mz->tree_node, &mctz->rb_root);
451 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700452}
453
454static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700455__mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singh4e416952009-09-23 15:56:39 -0700456 struct mem_cgroup_per_zone *mz,
457 struct mem_cgroup_tree_per_zone *mctz)
458{
459 if (!mz->on_tree)
460 return;
461 rb_erase(&mz->tree_node, &mctz->rb_root);
462 mz->on_tree = false;
463}
464
465static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700466mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700467 struct mem_cgroup_per_zone *mz,
468 struct mem_cgroup_tree_per_zone *mctz)
469{
470 spin_lock(&mctz->lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700471 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700472 spin_unlock(&mctz->lock);
473}
474
Balbir Singhf64c3f52009-09-23 15:56:37 -0700475
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700476static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700477{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700478 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700479 struct mem_cgroup_per_zone *mz;
480 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700481 int nid = page_to_nid(page);
482 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700483 mctz = soft_limit_tree_from_page(page);
484
485 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700486 * Necessary to update all ancestors when hierarchy is used.
487 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700488 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700489 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
490 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
491 excess = res_counter_soft_limit_excess(&memcg->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700492 /*
493 * We have to update the tree if mz is on RB-tree or
494 * mem is over its softlimit.
495 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700496 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700497 spin_lock(&mctz->lock);
498 /* if on-tree, remove it */
499 if (mz->on_tree)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700500 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700501 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700502 * Insert again. mz->usage_in_excess will be updated.
503 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700504 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700505 __mem_cgroup_insert_exceeded(memcg, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700506 spin_unlock(&mctz->lock);
507 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700508 }
509}
510
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700511static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700512{
513 int node, zone;
514 struct mem_cgroup_per_zone *mz;
515 struct mem_cgroup_tree_per_zone *mctz;
516
517 for_each_node_state(node, N_POSSIBLE) {
518 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700519 mz = mem_cgroup_zoneinfo(memcg, node, zone);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700520 mctz = soft_limit_tree_node_zone(node, zone);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700521 mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700522 }
523 }
524}
525
Balbir Singh4e416952009-09-23 15:56:39 -0700526static struct mem_cgroup_per_zone *
527__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
528{
529 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700530 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700531
532retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700533 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700534 rightmost = rb_last(&mctz->rb_root);
535 if (!rightmost)
536 goto done; /* Nothing to reclaim from */
537
538 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
539 /*
540 * Remove the node now but someone else can add it back,
541 * we will to add it back at the end of reclaim to its correct
542 * position in the tree.
543 */
544 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
545 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
546 !css_tryget(&mz->mem->css))
547 goto retry;
548done:
549 return mz;
550}
551
552static struct mem_cgroup_per_zone *
553mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
554{
555 struct mem_cgroup_per_zone *mz;
556
557 spin_lock(&mctz->lock);
558 mz = __mem_cgroup_largest_soft_limit_node(mctz);
559 spin_unlock(&mctz->lock);
560 return mz;
561}
562
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700563/*
564 * Implementation Note: reading percpu statistics for memcg.
565 *
566 * Both of vmstat[] and percpu_counter has threshold and do periodic
567 * synchronization to implement "quick" read. There are trade-off between
568 * reading cost and precision of value. Then, we may have a chance to implement
569 * a periodic synchronizion of counter in memcg's counter.
570 *
571 * But this _read() function is used for user interface now. The user accounts
572 * memory usage by memory cgroup and he _always_ requires exact value because
573 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
574 * have to visit all online cpus and make sum. So, for now, unnecessary
575 * synchronization is not implemented. (just implemented for cpu hotplug)
576 *
577 * If there are kernel internal actions which can make use of some not-exact
578 * value, and reading all cpu value can be performance bottleneck in some
579 * common workload, threashold and synchonization as vmstat[] should be
580 * implemented.
581 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700582static long mem_cgroup_read_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700583 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800584{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700585 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800586 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800587
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700588 get_online_cpus();
589 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700590 val += per_cpu(memcg->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700591#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700592 spin_lock(&memcg->pcp_counter_lock);
593 val += memcg->nocpu_base.count[idx];
594 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700595#endif
596 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800597 return val;
598}
599
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700600static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700601 bool charge)
602{
603 int val = (charge) ? 1 : -1;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700604 this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAPOUT], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700605}
606
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700607void mem_cgroup_pgfault(struct mem_cgroup *memcg, int val)
Ying Han456f9982011-05-26 16:25:38 -0700608{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700609 this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT], val);
Ying Han456f9982011-05-26 16:25:38 -0700610}
611
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700612void mem_cgroup_pgmajfault(struct mem_cgroup *memcg, int val)
Ying Han456f9982011-05-26 16:25:38 -0700613{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700614 this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT], val);
Ying Han456f9982011-05-26 16:25:38 -0700615}
616
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700617static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg,
Johannes Weinere9f89742011-03-23 16:42:37 -0700618 enum mem_cgroup_events_index idx)
619{
620 unsigned long val = 0;
621 int cpu;
622
623 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700624 val += per_cpu(memcg->stat->events[idx], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -0700625#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700626 spin_lock(&memcg->pcp_counter_lock);
627 val += memcg->nocpu_base.events[idx];
628 spin_unlock(&memcg->pcp_counter_lock);
Johannes Weinere9f89742011-03-23 16:42:37 -0700629#endif
630 return val;
631}
632
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700633static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800634 bool file, int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800635{
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800636 preempt_disable();
637
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800638 if (file)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700639 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE],
640 nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800641 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700642 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS],
643 nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700644
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800645 /* pagein of a big page is an event. So, ignore page size */
646 if (nr_pages > 0)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700647 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800648 else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700649 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800650 nr_pages = -nr_pages; /* for event */
651 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800652
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700653 __this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_COUNT], nr_pages);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800654
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800655 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800656}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800657
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700658unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700659mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg, int nid, int zid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700660 unsigned int lru_mask)
Ying Han889976d2011-05-26 16:25:33 -0700661{
662 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700663 enum lru_list l;
664 unsigned long ret = 0;
665
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700666 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700667
668 for_each_lru(l) {
669 if (BIT(l) & lru_mask)
670 ret += MEM_CGROUP_ZSTAT(mz, l);
671 }
672 return ret;
673}
674
675static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700676mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700677 int nid, unsigned int lru_mask)
678{
Ying Han889976d2011-05-26 16:25:33 -0700679 u64 total = 0;
680 int zid;
681
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700682 for (zid = 0; zid < MAX_NR_ZONES; zid++)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700683 total += mem_cgroup_zone_nr_lru_pages(memcg,
684 nid, zid, lru_mask);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700685
Ying Han889976d2011-05-26 16:25:33 -0700686 return total;
687}
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700688
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700689static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700690 unsigned int lru_mask)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800691{
Ying Han889976d2011-05-26 16:25:33 -0700692 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800693 u64 total = 0;
694
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700695 for_each_node_state(nid, N_HIGH_MEMORY)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700696 total += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800697 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800698}
699
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700700static bool __memcg_event_check(struct mem_cgroup *memcg, int target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800701{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700702 unsigned long val, next;
703
Steven Rostedt47994012011-11-02 13:38:33 -0700704 val = __this_cpu_read(memcg->stat->events[MEM_CGROUP_EVENTS_COUNT]);
705 next = __this_cpu_read(memcg->stat->targets[target]);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700706 /* from time_after() in jiffies.h */
707 return ((long)next - (long)val < 0);
708}
709
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700710static void __mem_cgroup_target_update(struct mem_cgroup *memcg, int target)
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700711{
712 unsigned long val, next;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800713
Steven Rostedt47994012011-11-02 13:38:33 -0700714 val = __this_cpu_read(memcg->stat->events[MEM_CGROUP_EVENTS_COUNT]);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800715
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700716 switch (target) {
717 case MEM_CGROUP_TARGET_THRESH:
718 next = val + THRESHOLDS_EVENTS_TARGET;
719 break;
720 case MEM_CGROUP_TARGET_SOFTLIMIT:
721 next = val + SOFTLIMIT_EVENTS_TARGET;
722 break;
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700723 case MEM_CGROUP_TARGET_NUMAINFO:
724 next = val + NUMAINFO_EVENTS_TARGET;
725 break;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700726 default:
727 return;
728 }
729
Steven Rostedt47994012011-11-02 13:38:33 -0700730 __this_cpu_write(memcg->stat->targets[target], next);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800731}
732
733/*
734 * Check events in order.
735 *
736 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700737static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800738{
Steven Rostedt47994012011-11-02 13:38:33 -0700739 preempt_disable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800740 /* threshold event is triggered in finer grain than soft limit */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700741 if (unlikely(__memcg_event_check(memcg, MEM_CGROUP_TARGET_THRESH))) {
742 mem_cgroup_threshold(memcg);
743 __mem_cgroup_target_update(memcg, MEM_CGROUP_TARGET_THRESH);
744 if (unlikely(__memcg_event_check(memcg,
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700745 MEM_CGROUP_TARGET_SOFTLIMIT))) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700746 mem_cgroup_update_tree(memcg, page);
747 __mem_cgroup_target_update(memcg,
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700748 MEM_CGROUP_TARGET_SOFTLIMIT);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700749 }
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700750#if MAX_NUMNODES > 1
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700751 if (unlikely(__memcg_event_check(memcg,
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700752 MEM_CGROUP_TARGET_NUMAINFO))) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700753 atomic_inc(&memcg->numainfo_events);
754 __mem_cgroup_target_update(memcg,
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700755 MEM_CGROUP_TARGET_NUMAINFO);
756 }
757#endif
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800758 }
Steven Rostedt47994012011-11-02 13:38:33 -0700759 preempt_enable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800760}
761
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800762static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800763{
764 return container_of(cgroup_subsys_state(cont,
765 mem_cgroup_subsys_id), struct mem_cgroup,
766 css);
767}
768
Balbir Singhcf475ad2008-04-29 01:00:16 -0700769struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800770{
Balbir Singh31a78f22008-09-28 23:09:31 +0100771 /*
772 * mm_update_next_owner() may clear mm->owner to NULL
773 * if it races with swapoff, page migration, etc.
774 * So this can be called with p == NULL.
775 */
776 if (unlikely(!p))
777 return NULL;
778
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800779 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
780 struct mem_cgroup, css);
781}
782
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700783struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800784{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700785 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700786
787 if (!mm)
788 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800789 /*
790 * Because we have no locks, mm->owner's may be being moved to other
791 * cgroup. We use css_tryget() here even if this looks
792 * pessimistic (rather than adding locks here).
793 */
794 rcu_read_lock();
795 do {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700796 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
797 if (unlikely(!memcg))
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800798 break;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700799 } while (!css_tryget(&memcg->css));
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800800 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700801 return memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800802}
803
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700804/* The caller has to guarantee "mem" exists before calling this */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700805static struct mem_cgroup *mem_cgroup_start_loop(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700806{
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700807 struct cgroup_subsys_state *css;
808 int found;
809
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700810 if (!memcg) /* ROOT cgroup has the smallest ID */
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700811 return root_mem_cgroup; /*css_put/get against root is ignored*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700812 if (!memcg->use_hierarchy) {
813 if (css_tryget(&memcg->css))
814 return memcg;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700815 return NULL;
816 }
817 rcu_read_lock();
818 /*
819 * searching a memory cgroup which has the smallest ID under given
820 * ROOT cgroup. (ID >= 1)
821 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700822 css = css_get_next(&mem_cgroup_subsys, 1, &memcg->css, &found);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700823 if (css && css_tryget(css))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700824 memcg = container_of(css, struct mem_cgroup, css);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700825 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700826 memcg = NULL;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700827 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700828 return memcg;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700829}
830
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700831static struct mem_cgroup *mem_cgroup_get_next(struct mem_cgroup *iter,
832 struct mem_cgroup *root,
833 bool cond)
834{
835 int nextid = css_id(&iter->css) + 1;
836 int found;
837 int hierarchy_used;
838 struct cgroup_subsys_state *css;
839
840 hierarchy_used = iter->use_hierarchy;
841
842 css_put(&iter->css);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700843 /* If no ROOT, walk all, ignore hierarchy */
844 if (!cond || (root && !hierarchy_used))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700845 return NULL;
846
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700847 if (!root)
848 root = root_mem_cgroup;
849
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700850 do {
851 iter = NULL;
852 rcu_read_lock();
853
854 css = css_get_next(&mem_cgroup_subsys, nextid,
855 &root->css, &found);
856 if (css && css_tryget(css))
857 iter = container_of(css, struct mem_cgroup, css);
858 rcu_read_unlock();
859 /* If css is NULL, no more cgroups will be found */
860 nextid = found + 1;
861 } while (css && !iter);
862
863 return iter;
864}
865/*
866 * for_eacn_mem_cgroup_tree() for visiting all cgroup under tree. Please
867 * be careful that "break" loop is not allowed. We have reference count.
868 * Instead of that modify "cond" to be false and "continue" to exit the loop.
869 */
870#define for_each_mem_cgroup_tree_cond(iter, root, cond) \
871 for (iter = mem_cgroup_start_loop(root);\
872 iter != NULL;\
873 iter = mem_cgroup_get_next(iter, root, cond))
874
875#define for_each_mem_cgroup_tree(iter, root) \
876 for_each_mem_cgroup_tree_cond(iter, root, true)
877
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700878#define for_each_mem_cgroup_all(iter) \
879 for_each_mem_cgroup_tree_cond(iter, NULL, true)
880
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700881
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700882static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
Balbir Singh4b3bde42009-09-23 15:56:32 -0700883{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700884 return (memcg == root_mem_cgroup);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700885}
886
Ying Han456f9982011-05-26 16:25:38 -0700887void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
888{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700889 struct mem_cgroup *memcg;
Ying Han456f9982011-05-26 16:25:38 -0700890
891 if (!mm)
892 return;
893
894 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700895 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
896 if (unlikely(!memcg))
Ying Han456f9982011-05-26 16:25:38 -0700897 goto out;
898
899 switch (idx) {
900 case PGMAJFAULT:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700901 mem_cgroup_pgmajfault(memcg, 1);
Ying Han456f9982011-05-26 16:25:38 -0700902 break;
903 case PGFAULT:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700904 mem_cgroup_pgfault(memcg, 1);
Ying Han456f9982011-05-26 16:25:38 -0700905 break;
906 default:
907 BUG();
908 }
909out:
910 rcu_read_unlock();
911}
912EXPORT_SYMBOL(mem_cgroup_count_vm_event);
913
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800914/*
915 * Following LRU functions are allowed to be used without PCG_LOCK.
916 * Operations are called by routine of global LRU independently from memcg.
917 * What we have to take care of here is validness of pc->mem_cgroup.
918 *
919 * Changes to pc->mem_cgroup happens when
920 * 1. charge
921 * 2. moving account
922 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
923 * It is added to LRU before charge.
924 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
925 * When moving account, the page is not on LRU. It's isolated.
926 */
927
928void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800929{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800930 struct page_cgroup *pc;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800931 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700932
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800933 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800934 return;
935 pc = lookup_page_cgroup(page);
936 /* can happen while we handle swapcache. */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700937 if (!TestClearPageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800938 return;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700939 VM_BUG_ON(!pc->mem_cgroup);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800940 /*
941 * We don't check PCG_USED bit. It's cleared when the "page" is finally
942 * removed from global LRU.
943 */
Johannes Weiner97a6c372011-03-23 16:42:27 -0700944 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -0800945 /* huge page split is done under lru_lock. so, we have no races. */
946 MEM_CGROUP_ZSTAT(mz, lru) -= 1 << compound_order(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700947 if (mem_cgroup_is_root(pc->mem_cgroup))
948 return;
949 VM_BUG_ON(list_empty(&pc->lru));
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800950 list_del_init(&pc->lru);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800951}
952
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800953void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800954{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800955 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800956}
957
Minchan Kim3f58a822011-03-22 16:32:53 -0700958/*
959 * Writeback is about to end against a page which has been marked for immediate
960 * reclaim. If it still appears to be reclaimable, move it to the tail of the
961 * inactive list.
962 */
963void mem_cgroup_rotate_reclaimable_page(struct page *page)
964{
965 struct mem_cgroup_per_zone *mz;
966 struct page_cgroup *pc;
967 enum lru_list lru = page_lru(page);
968
969 if (mem_cgroup_disabled())
970 return;
971
972 pc = lookup_page_cgroup(page);
973 /* unused or root page is not rotated. */
974 if (!PageCgroupUsed(pc))
975 return;
976 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
977 smp_rmb();
978 if (mem_cgroup_is_root(pc->mem_cgroup))
979 return;
Johannes Weiner97a6c372011-03-23 16:42:27 -0700980 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
Minchan Kim3f58a822011-03-22 16:32:53 -0700981 list_move_tail(&pc->lru, &mz->lists[lru]);
982}
983
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800984void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800985{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800986 struct mem_cgroup_per_zone *mz;
987 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800988
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800989 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700990 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700991
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800992 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700993 /* unused or root page is not rotated. */
Johannes Weiner713735b2011-01-20 14:44:31 -0800994 if (!PageCgroupUsed(pc))
995 return;
996 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
997 smp_rmb();
998 if (mem_cgroup_is_root(pc->mem_cgroup))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800999 return;
Johannes Weiner97a6c372011-03-23 16:42:27 -07001000 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001001 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -08001002}
1003
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001004void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
1005{
1006 struct page_cgroup *pc;
1007 struct mem_cgroup_per_zone *mz;
1008
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001009 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001010 return;
1011 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -07001012 VM_BUG_ON(PageCgroupAcctLRU(pc));
Johannes Weinera61ed3c2011-11-02 13:38:29 -07001013 /*
1014 * putback: charge:
1015 * SetPageLRU SetPageCgroupUsed
1016 * smp_mb smp_mb
1017 * PageCgroupUsed && add to memcg LRU PageLRU && add to memcg LRU
1018 *
1019 * Ensure that one of the two sides adds the page to the memcg
1020 * LRU during a race.
1021 */
1022 smp_mb();
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001023 if (!PageCgroupUsed(pc))
1024 return;
Johannes Weiner713735b2011-01-20 14:44:31 -08001025 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
1026 smp_rmb();
Johannes Weiner97a6c372011-03-23 16:42:27 -07001027 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08001028 /* huge page split is done under lru_lock. so, we have no races. */
1029 MEM_CGROUP_ZSTAT(mz, lru) += 1 << compound_order(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -07001030 SetPageCgroupAcctLRU(pc);
1031 if (mem_cgroup_is_root(pc->mem_cgroup))
1032 return;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001033 list_add(&pc->lru, &mz->lists[lru]);
1034}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001035
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001036/*
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001037 * At handling SwapCache and other FUSE stuff, pc->mem_cgroup may be changed
1038 * while it's linked to lru because the page may be reused after it's fully
1039 * uncharged. To handle that, unlink page_cgroup from LRU when charge it again.
1040 * It's done under lock_page and expected that zone->lru_lock isnever held.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001041 */
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001042static void mem_cgroup_lru_del_before_commit(struct page *page)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001043{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001044 unsigned long flags;
1045 struct zone *zone = page_zone(page);
1046 struct page_cgroup *pc = lookup_page_cgroup(page);
1047
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001048 /*
1049 * Doing this check without taking ->lru_lock seems wrong but this
1050 * is safe. Because if page_cgroup's USED bit is unset, the page
1051 * will not be added to any memcg's LRU. If page_cgroup's USED bit is
1052 * set, the commit after this will fail, anyway.
1053 * This all charge/uncharge is done under some mutual execustion.
1054 * So, we don't need to taking care of changes in USED bit.
1055 */
1056 if (likely(!PageLRU(page)))
1057 return;
1058
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001059 spin_lock_irqsave(&zone->lru_lock, flags);
1060 /*
1061 * Forget old LRU when this page_cgroup is *not* used. This Used bit
1062 * is guarded by lock_page() because the page is SwapCache.
1063 */
1064 if (!PageCgroupUsed(pc))
1065 mem_cgroup_del_lru_list(page, page_lru(page));
1066 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001067}
1068
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001069static void mem_cgroup_lru_add_after_commit(struct page *page)
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001070{
1071 unsigned long flags;
1072 struct zone *zone = page_zone(page);
1073 struct page_cgroup *pc = lookup_page_cgroup(page);
Johannes Weinera61ed3c2011-11-02 13:38:29 -07001074 /*
1075 * putback: charge:
1076 * SetPageLRU SetPageCgroupUsed
1077 * smp_mb smp_mb
1078 * PageCgroupUsed && add to memcg LRU PageLRU && add to memcg LRU
1079 *
1080 * Ensure that one of the two sides adds the page to the memcg
1081 * LRU during a race.
1082 */
1083 smp_mb();
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001084 /* taking care of that the page is added to LRU while we commit it */
1085 if (likely(!PageLRU(page)))
1086 return;
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001087 spin_lock_irqsave(&zone->lru_lock, flags);
1088 /* link when the page is linked to LRU but page_cgroup isn't */
Balbir Singh4b3bde42009-09-23 15:56:32 -07001089 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001090 mem_cgroup_add_lru_list(page, page_lru(page));
1091 spin_unlock_irqrestore(&zone->lru_lock, flags);
1092}
1093
1094
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001095void mem_cgroup_move_lists(struct page *page,
1096 enum lru_list from, enum lru_list to)
1097{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001098 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001099 return;
1100 mem_cgroup_del_lru_list(page, from);
1101 mem_cgroup_add_lru_list(page, to);
1102}
1103
Michal Hocko3e920412011-07-26 16:08:29 -07001104/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001105 * Checks whether given mem is same or in the root_mem_cgroup's
Michal Hocko3e920412011-07-26 16:08:29 -07001106 * hierarchy subtree
1107 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001108static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1109 struct mem_cgroup *memcg)
Michal Hocko3e920412011-07-26 16:08:29 -07001110{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001111 if (root_memcg != memcg) {
1112 return (root_memcg->use_hierarchy &&
1113 css_is_ancestor(&memcg->css, &root_memcg->css));
Michal Hocko3e920412011-07-26 16:08:29 -07001114 }
1115
1116 return true;
1117}
1118
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001119int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -08001120{
1121 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001122 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001123 struct task_struct *p;
David Rientjes4c4a2212008-02-07 00:14:06 -08001124
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001125 p = find_lock_task_mm(task);
1126 if (!p)
1127 return 0;
1128 curr = try_get_mem_cgroup_from_mm(p->mm);
1129 task_unlock(p);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001130 if (!curr)
1131 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001132 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001133 * We should check use_hierarchy of "memcg" not "curr". Because checking
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001134 * use_hierarchy of "curr" here make this function true if hierarchy is
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001135 * enabled in "curr" and "curr" is a child of "memcg" in *cgroup*
1136 * hierarchy(even if use_hierarchy is disabled in "memcg").
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001137 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001138 ret = mem_cgroup_same_or_subtree(memcg, curr);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001139 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001140 return ret;
1141}
1142
Johannes Weiner9b272972011-11-02 13:38:23 -07001143int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg, struct zone *zone)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001144{
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001145 unsigned long inactive_ratio;
Johannes Weiner9b272972011-11-02 13:38:23 -07001146 int nid = zone_to_nid(zone);
1147 int zid = zone_idx(zone);
1148 unsigned long inactive;
1149 unsigned long active;
1150 unsigned long gb;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001151
Johannes Weiner9b272972011-11-02 13:38:23 -07001152 inactive = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid,
1153 BIT(LRU_INACTIVE_ANON));
1154 active = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid,
1155 BIT(LRU_ACTIVE_ANON));
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001156
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001157 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1158 if (gb)
1159 inactive_ratio = int_sqrt(10 * gb);
1160 else
1161 inactive_ratio = 1;
1162
Johannes Weiner9b272972011-11-02 13:38:23 -07001163 return inactive * inactive_ratio < active;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001164}
1165
Johannes Weiner9b272972011-11-02 13:38:23 -07001166int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg, struct zone *zone)
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001167{
1168 unsigned long active;
1169 unsigned long inactive;
Johannes Weiner9b272972011-11-02 13:38:23 -07001170 int zid = zone_idx(zone);
1171 int nid = zone_to_nid(zone);
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001172
Johannes Weiner9b272972011-11-02 13:38:23 -07001173 inactive = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid,
1174 BIT(LRU_INACTIVE_FILE));
1175 active = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid,
1176 BIT(LRU_ACTIVE_FILE));
Rik van Riel56e49d22009-06-16 15:32:28 -07001177
1178 return (active > inactive);
1179}
1180
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001181struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
1182 struct zone *zone)
1183{
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -07001184 int nid = zone_to_nid(zone);
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001185 int zid = zone_idx(zone);
1186 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
1187
1188 return &mz->reclaim_stat;
1189}
1190
1191struct zone_reclaim_stat *
1192mem_cgroup_get_reclaim_stat_from_page(struct page *page)
1193{
1194 struct page_cgroup *pc;
1195 struct mem_cgroup_per_zone *mz;
1196
1197 if (mem_cgroup_disabled())
1198 return NULL;
1199
1200 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -08001201 if (!PageCgroupUsed(pc))
1202 return NULL;
Johannes Weiner713735b2011-01-20 14:44:31 -08001203 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
1204 smp_rmb();
Johannes Weiner97a6c372011-03-23 16:42:27 -07001205 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001206 return &mz->reclaim_stat;
1207}
1208
Balbir Singh66e17072008-02-07 00:13:56 -08001209unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
1210 struct list_head *dst,
1211 unsigned long *scanned, int order,
Minchan Kim4356f212011-10-31 17:06:47 -07001212 isolate_mode_t mode,
1213 struct zone *z,
Balbir Singh66e17072008-02-07 00:13:56 -08001214 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001215 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -08001216{
1217 unsigned long nr_taken = 0;
1218 struct page *page;
1219 unsigned long scan;
1220 LIST_HEAD(pc_list);
1221 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001222 struct page_cgroup *pc, *tmp;
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -07001223 int nid = zone_to_nid(z);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001224 int zid = zone_idx(z);
1225 struct mem_cgroup_per_zone *mz;
Johannes Weinerb7c46d12009-09-21 17:02:56 -07001226 int lru = LRU_FILE * file + active;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001227 int ret;
Balbir Singh66e17072008-02-07 00:13:56 -08001228
Balbir Singhcf475ad2008-04-29 01:00:16 -07001229 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001230 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001231 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -08001232
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001233 scan = 0;
1234 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -08001235 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001236 break;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001237
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001238 if (unlikely(!PageCgroupUsed(pc)))
1239 continue;
Johannes Weiner5564e882011-03-23 16:42:29 -07001240
Johannes Weiner6b3ae582011-03-23 16:42:30 -07001241 page = lookup_cgroup_page(pc);
Johannes Weiner5564e882011-03-23 16:42:29 -07001242
Hugh Dickins436c65412008-02-07 00:14:12 -08001243 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001244 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001245
Hugh Dickins436c65412008-02-07 00:14:12 -08001246 scan++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001247 ret = __isolate_lru_page(page, mode, file);
1248 switch (ret) {
1249 case 0:
Balbir Singh66e17072008-02-07 00:13:56 -08001250 list_move(&page->lru, dst);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001251 mem_cgroup_del_lru(page);
Rik van Riel2c888cf2011-01-13 15:47:13 -08001252 nr_taken += hpage_nr_pages(page);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001253 break;
1254 case -EBUSY:
1255 /* we don't affect global LRU but rotate in our LRU */
1256 mem_cgroup_rotate_lru_list(page, page_lru(page));
1257 break;
1258 default:
1259 break;
Balbir Singh66e17072008-02-07 00:13:56 -08001260 }
1261 }
1262
Balbir Singh66e17072008-02-07 00:13:56 -08001263 *scanned = scan;
KOSAKI Motohirocc8e9702010-08-09 17:19:57 -07001264
1265 trace_mm_vmscan_memcg_isolate(0, nr_to_scan, scan, nr_taken,
1266 0, 0, 0, mode);
1267
Balbir Singh66e17072008-02-07 00:13:56 -08001268 return nr_taken;
1269}
1270
Balbir Singh6d61ef42009-01-07 18:08:06 -08001271#define mem_cgroup_from_res_counter(counter, member) \
1272 container_of(counter, struct mem_cgroup, member)
1273
Johannes Weiner19942822011-02-01 15:52:43 -08001274/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001275 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
1276 * @mem: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001277 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001278 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001279 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001280 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001281static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001282{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001283 unsigned long long margin;
1284
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001285 margin = res_counter_margin(&memcg->res);
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001286 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001287 margin = min(margin, res_counter_margin(&memcg->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001288 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001289}
1290
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001291int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001292{
1293 struct cgroup *cgrp = memcg->css.cgroup;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001294
1295 /* root ? */
1296 if (cgrp->parent == NULL)
1297 return vm_swappiness;
1298
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001299 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001300}
1301
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001302static void mem_cgroup_start_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001303{
1304 int cpu;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001305
1306 get_online_cpus();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001307 spin_lock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001308 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001309 per_cpu(memcg->stat->count[MEM_CGROUP_ON_MOVE], cpu) += 1;
1310 memcg->nocpu_base.count[MEM_CGROUP_ON_MOVE] += 1;
1311 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001312 put_online_cpus();
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001313
1314 synchronize_rcu();
1315}
1316
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001317static void mem_cgroup_end_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001318{
1319 int cpu;
1320
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001321 if (!memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001322 return;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001323 get_online_cpus();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001324 spin_lock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001325 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001326 per_cpu(memcg->stat->count[MEM_CGROUP_ON_MOVE], cpu) -= 1;
1327 memcg->nocpu_base.count[MEM_CGROUP_ON_MOVE] -= 1;
1328 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001329 put_online_cpus();
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001330}
1331/*
1332 * 2 routines for checking "mem" is under move_account() or not.
1333 *
1334 * mem_cgroup_stealed() - checking a cgroup is mc.from or not. This is used
1335 * for avoiding race in accounting. If true,
1336 * pc->mem_cgroup may be overwritten.
1337 *
1338 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1339 * under hierarchy of moving cgroups. This is for
1340 * waiting at hith-memory prressure caused by "move".
1341 */
1342
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001343static bool mem_cgroup_stealed(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001344{
1345 VM_BUG_ON(!rcu_read_lock_held());
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001346 return this_cpu_read(memcg->stat->count[MEM_CGROUP_ON_MOVE]) > 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001347}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001348
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001349static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001350{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001351 struct mem_cgroup *from;
1352 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001353 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001354 /*
1355 * Unlike task_move routines, we access mc.to, mc.from not under
1356 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1357 */
1358 spin_lock(&mc.lock);
1359 from = mc.from;
1360 to = mc.to;
1361 if (!from)
1362 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001363
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001364 ret = mem_cgroup_same_or_subtree(memcg, from)
1365 || mem_cgroup_same_or_subtree(memcg, to);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001366unlock:
1367 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001368 return ret;
1369}
1370
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001371static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001372{
1373 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001374 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001375 DEFINE_WAIT(wait);
1376 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1377 /* moving charge context might have finished. */
1378 if (mc.moving_task)
1379 schedule();
1380 finish_wait(&mc.waitq, &wait);
1381 return true;
1382 }
1383 }
1384 return false;
1385}
1386
Balbir Singhe2224322009-04-02 16:57:39 -07001387/**
Kirill A. Shutemov6a6135b2010-03-10 15:22:25 -08001388 * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode.
Balbir Singhe2224322009-04-02 16:57:39 -07001389 * @memcg: The memory cgroup that went over limit
1390 * @p: Task that is going to be killed
1391 *
1392 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1393 * enabled
1394 */
1395void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1396{
1397 struct cgroup *task_cgrp;
1398 struct cgroup *mem_cgrp;
1399 /*
1400 * Need a buffer in BSS, can't rely on allocations. The code relies
1401 * on the assumption that OOM is serialized for memory controller.
1402 * If this assumption is broken, revisit this code.
1403 */
1404 static char memcg_name[PATH_MAX];
1405 int ret;
1406
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001407 if (!memcg || !p)
Balbir Singhe2224322009-04-02 16:57:39 -07001408 return;
1409
1410
1411 rcu_read_lock();
1412
1413 mem_cgrp = memcg->css.cgroup;
1414 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1415
1416 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1417 if (ret < 0) {
1418 /*
1419 * Unfortunately, we are unable to convert to a useful name
1420 * But we'll still print out the usage information
1421 */
1422 rcu_read_unlock();
1423 goto done;
1424 }
1425 rcu_read_unlock();
1426
1427 printk(KERN_INFO "Task in %s killed", memcg_name);
1428
1429 rcu_read_lock();
1430 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1431 if (ret < 0) {
1432 rcu_read_unlock();
1433 goto done;
1434 }
1435 rcu_read_unlock();
1436
1437 /*
1438 * Continues from above, so we don't need an KERN_ level
1439 */
1440 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1441done:
1442
1443 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1444 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1445 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1446 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1447 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1448 "failcnt %llu\n",
1449 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1450 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1451 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1452}
1453
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001454/*
1455 * This function returns the number of memcg under hierarchy tree. Returns
1456 * 1(self count) if no children.
1457 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001458static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001459{
1460 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001461 struct mem_cgroup *iter;
1462
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001463 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001464 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001465 return num;
1466}
1467
Balbir Singh6d61ef42009-01-07 18:08:06 -08001468/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001469 * Return the memory (and swap, if configured) limit for a memcg.
1470 */
1471u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
1472{
1473 u64 limit;
1474 u64 memsw;
1475
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001476 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
1477 limit += total_swap_pages << PAGE_SHIFT;
1478
David Rientjesa63d83f2010-08-09 17:19:46 -07001479 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1480 /*
1481 * If memsw is finite and limits the amount of swap space available
1482 * to this memcg, return that limit.
1483 */
1484 return min(limit, memsw);
1485}
1486
1487/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001488 * Visit the first child (need not be the first child as per the ordering
1489 * of the cgroup list, since we track last_scanned_child) of @mem and use
1490 * that to reclaim free pages from.
1491 */
1492static struct mem_cgroup *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001493mem_cgroup_select_victim(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001494{
1495 struct mem_cgroup *ret = NULL;
1496 struct cgroup_subsys_state *css;
1497 int nextid, found;
1498
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001499 if (!root_memcg->use_hierarchy) {
1500 css_get(&root_memcg->css);
1501 ret = root_memcg;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001502 }
1503
1504 while (!ret) {
1505 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001506 nextid = root_memcg->last_scanned_child + 1;
1507 css = css_get_next(&mem_cgroup_subsys, nextid, &root_memcg->css,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001508 &found);
1509 if (css && css_tryget(css))
1510 ret = container_of(css, struct mem_cgroup, css);
1511
1512 rcu_read_unlock();
1513 /* Updates scanning parameter */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001514 if (!css) {
1515 /* this means start scan from ID:1 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001516 root_memcg->last_scanned_child = 0;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001517 } else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001518 root_memcg->last_scanned_child = found;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001519 }
1520
1521 return ret;
1522}
1523
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001524/**
1525 * test_mem_cgroup_node_reclaimable
1526 * @mem: the target memcg
1527 * @nid: the node ID to be checked.
1528 * @noswap : specify true here if the user wants flle only information.
1529 *
1530 * This function returns whether the specified memcg contains any
1531 * reclaimable pages on a node. Returns true if there are any reclaimable
1532 * pages in the node.
1533 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001534static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001535 int nid, bool noswap)
1536{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001537 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001538 return true;
1539 if (noswap || !total_swap_pages)
1540 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001541 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001542 return true;
1543 return false;
1544
1545}
Ying Han889976d2011-05-26 16:25:33 -07001546#if MAX_NUMNODES > 1
1547
1548/*
1549 * Always updating the nodemask is not very good - even if we have an empty
1550 * list or the wrong list here, we can start from some node and traverse all
1551 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1552 *
1553 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001554static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001555{
1556 int nid;
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001557 /*
1558 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1559 * pagein/pageout changes since the last update.
1560 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001561 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001562 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001563 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001564 return;
1565
Ying Han889976d2011-05-26 16:25:33 -07001566 /* make a nodemask where this memcg uses memory from */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001567 memcg->scan_nodes = node_states[N_HIGH_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001568
1569 for_each_node_mask(nid, node_states[N_HIGH_MEMORY]) {
1570
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001571 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1572 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001573 }
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001574
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001575 atomic_set(&memcg->numainfo_events, 0);
1576 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001577}
1578
1579/*
1580 * Selecting a node where we start reclaim from. Because what we need is just
1581 * reducing usage counter, start from anywhere is O,K. Considering
1582 * memory reclaim from current node, there are pros. and cons.
1583 *
1584 * Freeing memory from current node means freeing memory from a node which
1585 * we'll use or we've used. So, it may make LRU bad. And if several threads
1586 * hit limits, it will see a contention on a node. But freeing from remote
1587 * node means more costs for memory reclaim because of memory latency.
1588 *
1589 * Now, we use round-robin. Better algorithm is welcomed.
1590 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001591int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001592{
1593 int node;
1594
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001595 mem_cgroup_may_update_nodemask(memcg);
1596 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001597
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001598 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001599 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001600 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001601 /*
1602 * We call this when we hit limit, not when pages are added to LRU.
1603 * No LRU may hold pages because all pages are UNEVICTABLE or
1604 * memcg is too small and all pages are not on LRU. In that case,
1605 * we use curret node.
1606 */
1607 if (unlikely(node == MAX_NUMNODES))
1608 node = numa_node_id();
1609
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001610 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001611 return node;
1612}
1613
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001614/*
1615 * Check all nodes whether it contains reclaimable pages or not.
1616 * For quick scan, we make use of scan_nodes. This will allow us to skip
1617 * unused nodes. But scan_nodes is lazily updated and may not cotain
1618 * enough new information. We need to do double check.
1619 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001620bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001621{
1622 int nid;
1623
1624 /*
1625 * quick check...making use of scan_node.
1626 * We can skip unused nodes.
1627 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001628 if (!nodes_empty(memcg->scan_nodes)) {
1629 for (nid = first_node(memcg->scan_nodes);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001630 nid < MAX_NUMNODES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001631 nid = next_node(nid, memcg->scan_nodes)) {
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001632
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001633 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001634 return true;
1635 }
1636 }
1637 /*
1638 * Check rest of nodes.
1639 */
1640 for_each_node_state(nid, N_HIGH_MEMORY) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001641 if (node_isset(nid, memcg->scan_nodes))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001642 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001643 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001644 return true;
1645 }
1646 return false;
1647}
1648
Ying Han889976d2011-05-26 16:25:33 -07001649#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001650int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001651{
1652 return 0;
1653}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001654
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001655bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001656{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001657 return test_mem_cgroup_node_reclaimable(memcg, 0, noswap);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001658}
Ying Han889976d2011-05-26 16:25:33 -07001659#endif
1660
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001661/*
1662 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1663 * we reclaimed from, so that we don't end up penalizing one child extensively
1664 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001665 *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001666 * root_memcg is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001667 *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001668 * We give up and return to the caller when we visit root_memcg twice.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001669 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001670 *
1671 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001672 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001673static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_memcg,
Balbir Singh4e416952009-09-23 15:56:39 -07001674 struct zone *zone,
Balbir Singh75822b42009-09-23 15:56:38 -07001675 gfp_t gfp_mask,
Ying Han0ae5e892011-05-26 16:25:25 -07001676 unsigned long reclaim_options,
1677 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001678{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001679 struct mem_cgroup *victim;
1680 int ret, total = 0;
1681 int loop = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001682 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1683 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
Balbir Singh4e416952009-09-23 15:56:39 -07001684 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001685 unsigned long excess;
Johannes Weiner185efc02011-09-14 16:21:58 -07001686 unsigned long nr_scanned;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001687
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001688 excess = res_counter_soft_limit_excess(&root_memcg->res) >> PAGE_SHIFT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001689
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001690 /* If memsw_is_minimum==1, swap-out is of-no-use. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001691 if (!check_soft && !shrink && root_memcg->memsw_is_minimum)
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001692 noswap = true;
1693
Balbir Singh4e416952009-09-23 15:56:39 -07001694 while (1) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001695 victim = mem_cgroup_select_victim(root_memcg);
1696 if (victim == root_memcg) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001697 loop++;
KAMEZAWA Hiroyukifbc29a22011-06-15 15:08:46 -07001698 /*
1699 * We are not draining per cpu cached charges during
1700 * soft limit reclaim because global reclaim doesn't
1701 * care about charges. It tries to free some memory and
1702 * charges will not give any.
1703 */
1704 if (!check_soft && loop >= 1)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001705 drain_all_stock_async(root_memcg);
Balbir Singh4e416952009-09-23 15:56:39 -07001706 if (loop >= 2) {
1707 /*
1708 * If we have not been able to reclaim
1709 * anything, it might because there are
1710 * no reclaimable pages under this hierarchy
1711 */
1712 if (!check_soft || !total) {
1713 css_put(&victim->css);
1714 break;
1715 }
1716 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001717 * We want to do more targeted reclaim.
Balbir Singh4e416952009-09-23 15:56:39 -07001718 * excess >> 2 is not to excessive so as to
1719 * reclaim too much, nor too less that we keep
1720 * coming back to reclaim from this cgroup
1721 */
1722 if (total >= (excess >> 2) ||
1723 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1724 css_put(&victim->css);
1725 break;
1726 }
1727 }
1728 }
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001729 if (!mem_cgroup_reclaimable(victim, noswap)) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001730 /* this cgroup's local usage == 0 */
1731 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001732 continue;
1733 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001734 /* we use swappiness of local cgroup */
Ying Han0ae5e892011-05-26 16:25:25 -07001735 if (check_soft) {
Balbir Singh4e416952009-09-23 15:56:39 -07001736 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
Johannes Weiner185efc02011-09-14 16:21:58 -07001737 noswap, zone, &nr_scanned);
1738 *total_scanned += nr_scanned;
Ying Han0ae5e892011-05-26 16:25:25 -07001739 } else
Balbir Singh4e416952009-09-23 15:56:39 -07001740 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
Johannes Weiner185efc02011-09-14 16:21:58 -07001741 noswap);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001742 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001743 /*
1744 * At shrinking usage, we can't check we should stop here or
1745 * reclaim more. It's depends on callers. last_scanned_child
1746 * will work enough for keeping fairness under tree.
1747 */
1748 if (shrink)
1749 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001750 total += ret;
Balbir Singh4e416952009-09-23 15:56:39 -07001751 if (check_soft) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001752 if (!res_counter_soft_limit_excess(&root_memcg->res))
Balbir Singh4e416952009-09-23 15:56:39 -07001753 return total;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001754 } else if (mem_cgroup_margin(root_memcg))
Johannes Weiner4fd14eb2011-05-26 16:25:35 -07001755 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001756 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001757 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001758}
1759
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001760/*
1761 * Check OOM-Killer is already running under our hierarchy.
1762 * If someone is running, return false.
Michal Hocko1af8efe2011-07-26 16:08:24 -07001763 * Has to be called with memcg_oom_lock
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001764 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001765static bool mem_cgroup_oom_lock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001766{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001767 struct mem_cgroup *iter, *failed = NULL;
1768 bool cond = true;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001769
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001770 for_each_mem_cgroup_tree_cond(iter, memcg, cond) {
Johannes Weiner23751be2011-08-25 15:59:16 -07001771 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001772 /*
1773 * this subtree of our hierarchy is already locked
1774 * so we cannot give a lock.
1775 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07001776 failed = iter;
1777 cond = false;
Johannes Weiner23751be2011-08-25 15:59:16 -07001778 } else
1779 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001780 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001781
Michal Hocko79dfdac2011-07-26 16:08:23 -07001782 if (!failed)
Johannes Weiner23751be2011-08-25 15:59:16 -07001783 return true;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001784
1785 /*
1786 * OK, we failed to lock the whole subtree so we have to clean up
1787 * what we set up to the failing subtree
1788 */
1789 cond = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001790 for_each_mem_cgroup_tree_cond(iter, memcg, cond) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001791 if (iter == failed) {
1792 cond = false;
1793 continue;
1794 }
1795 iter->oom_lock = false;
1796 }
Johannes Weiner23751be2011-08-25 15:59:16 -07001797 return false;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001798}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001799
Michal Hocko79dfdac2011-07-26 16:08:23 -07001800/*
Michal Hocko1af8efe2011-07-26 16:08:24 -07001801 * Has to be called with memcg_oom_lock
Michal Hocko79dfdac2011-07-26 16:08:23 -07001802 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001803static int mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001804{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001805 struct mem_cgroup *iter;
1806
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001807 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001808 iter->oom_lock = false;
1809 return 0;
1810}
1811
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001812static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001813{
1814 struct mem_cgroup *iter;
1815
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001816 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001817 atomic_inc(&iter->under_oom);
1818}
1819
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001820static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001821{
1822 struct mem_cgroup *iter;
1823
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001824 /*
1825 * When a new child is created while the hierarchy is under oom,
1826 * mem_cgroup_oom_lock() may not be called. We have to use
1827 * atomic_add_unless() here.
1828 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001829 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001830 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001831}
1832
Michal Hocko1af8efe2011-07-26 16:08:24 -07001833static DEFINE_SPINLOCK(memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001834static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1835
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001836struct oom_wait_info {
1837 struct mem_cgroup *mem;
1838 wait_queue_t wait;
1839};
1840
1841static int memcg_oom_wake_function(wait_queue_t *wait,
1842 unsigned mode, int sync, void *arg)
1843{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001844 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg,
1845 *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001846 struct oom_wait_info *oom_wait_info;
1847
1848 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001849 oom_wait_memcg = oom_wait_info->mem;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001850
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001851 /*
1852 * Both of oom_wait_info->mem and wake_mem are stable under us.
1853 * Then we can use css_is_ancestor without taking care of RCU.
1854 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001855 if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg)
1856 && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001857 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001858 return autoremove_wake_function(wait, mode, sync, arg);
1859}
1860
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001861static void memcg_wakeup_oom(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001862{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001863 /* for filtering, pass "memcg" as argument. */
1864 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001865}
1866
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001867static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001868{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001869 if (memcg && atomic_read(&memcg->under_oom))
1870 memcg_wakeup_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001871}
1872
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001873/*
1874 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
1875 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001876bool mem_cgroup_handle_oom(struct mem_cgroup *memcg, gfp_t mask)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001877{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001878 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001879 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001880
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001881 owait.mem = memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001882 owait.wait.flags = 0;
1883 owait.wait.func = memcg_oom_wake_function;
1884 owait.wait.private = current;
1885 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001886 need_to_kill = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001887 mem_cgroup_mark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001888
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001889 /* At first, try to OOM lock hierarchy under memcg.*/
Michal Hocko1af8efe2011-07-26 16:08:24 -07001890 spin_lock(&memcg_oom_lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001891 locked = mem_cgroup_oom_lock(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001892 /*
1893 * Even if signal_pending(), we can't quit charge() loop without
1894 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
1895 * under OOM is always welcomed, use TASK_KILLABLE here.
1896 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001897 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001898 if (!locked || memcg->oom_kill_disable)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001899 need_to_kill = false;
1900 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001901 mem_cgroup_oom_notify(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07001902 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001903
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001904 if (need_to_kill) {
1905 finish_wait(&memcg_oom_waitq, &owait.wait);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001906 mem_cgroup_out_of_memory(memcg, mask);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001907 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001908 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001909 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001910 }
Michal Hocko1af8efe2011-07-26 16:08:24 -07001911 spin_lock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001912 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001913 mem_cgroup_oom_unlock(memcg);
1914 memcg_wakeup_oom(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07001915 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001916
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001917 mem_cgroup_unmark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001918
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001919 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
1920 return false;
1921 /* Give chance to dying process */
KAMEZAWA Hiroyuki715a5ee2011-11-02 13:38:18 -07001922 schedule_timeout_uninterruptible(1);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001923 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001924}
1925
Balbir Singhd69b0422009-06-17 16:26:34 -07001926/*
1927 * Currently used to update mapped file statistics, but the routine can be
1928 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001929 *
1930 * Notes: Race condition
1931 *
1932 * We usually use page_cgroup_lock() for accessing page_cgroup member but
1933 * it tends to be costly. But considering some conditions, we doesn't need
1934 * to do so _always_.
1935 *
1936 * Considering "charge", lock_page_cgroup() is not required because all
1937 * file-stat operations happen after a page is attached to radix-tree. There
1938 * are no race with "charge".
1939 *
1940 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
1941 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
1942 * if there are race with "uncharge". Statistics itself is properly handled
1943 * by flags.
1944 *
1945 * Considering "move", this is an only case we see a race. To make the race
1946 * small, we check MEM_CGROUP_ON_MOVE percpu value and detect there are
1947 * possibility of race condition. If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07001948 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001949
Greg Thelen2a7106f2011-01-13 15:47:37 -08001950void mem_cgroup_update_page_stat(struct page *page,
1951 enum mem_cgroup_page_stat_item idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07001952{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001953 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001954 struct page_cgroup *pc = lookup_page_cgroup(page);
1955 bool need_unlock = false;
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08001956 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07001957
Balbir Singhd69b0422009-06-17 16:26:34 -07001958 if (unlikely(!pc))
1959 return;
1960
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001961 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001962 memcg = pc->mem_cgroup;
1963 if (unlikely(!memcg || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001964 goto out;
1965 /* pc->mem_cgroup is unstable ? */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001966 if (unlikely(mem_cgroup_stealed(memcg)) || PageTransHuge(page)) {
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001967 /* take a lock against to access pc->mem_cgroup */
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08001968 move_lock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001969 need_unlock = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001970 memcg = pc->mem_cgroup;
1971 if (!memcg || !PageCgroupUsed(pc))
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001972 goto out;
1973 }
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001974
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001975 switch (idx) {
Greg Thelen2a7106f2011-01-13 15:47:37 -08001976 case MEMCG_NR_FILE_MAPPED:
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001977 if (val > 0)
1978 SetPageCgroupFileMapped(pc);
1979 else if (!page_mapped(page))
KAMEZAWA Hiroyuki0c270f82010-10-27 15:33:39 -07001980 ClearPageCgroupFileMapped(pc);
Greg Thelen2a7106f2011-01-13 15:47:37 -08001981 idx = MEM_CGROUP_STAT_FILE_MAPPED;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001982 break;
1983 default:
1984 BUG();
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07001985 }
Balbir Singhd69b0422009-06-17 16:26:34 -07001986
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001987 this_cpu_add(memcg->stat->count[idx], val);
Greg Thelen2a7106f2011-01-13 15:47:37 -08001988
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001989out:
1990 if (unlikely(need_unlock))
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08001991 move_unlock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001992 rcu_read_unlock();
1993 return;
Balbir Singhd69b0422009-06-17 16:26:34 -07001994}
Greg Thelen2a7106f2011-01-13 15:47:37 -08001995EXPORT_SYMBOL(mem_cgroup_update_page_stat);
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001996
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001997/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001998 * size of first charge trial. "32" comes from vmscan.c's magic value.
1999 * TODO: maybe necessary to use big numbers in big irons.
2000 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002001#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002002struct memcg_stock_pcp {
2003 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002004 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002005 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002006 unsigned long flags;
2007#define FLUSHING_CACHED_CHARGE (0)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002008};
2009static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002010static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002011
2012/*
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002013 * Try to consume stocked charge on this cpu. If success, one page is consumed
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002014 * from local stock and true is returned. If the stock is 0 or charges from a
2015 * cgroup which is not current target, returns false. This stock will be
2016 * refilled.
2017 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002018static bool consume_stock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002019{
2020 struct memcg_stock_pcp *stock;
2021 bool ret = true;
2022
2023 stock = &get_cpu_var(memcg_stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002024 if (memcg == stock->cached && stock->nr_pages)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002025 stock->nr_pages--;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002026 else /* need to call res_counter_charge */
2027 ret = false;
2028 put_cpu_var(memcg_stock);
2029 return ret;
2030}
2031
2032/*
2033 * Returns stocks cached in percpu to res_counter and reset cached information.
2034 */
2035static void drain_stock(struct memcg_stock_pcp *stock)
2036{
2037 struct mem_cgroup *old = stock->cached;
2038
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002039 if (stock->nr_pages) {
2040 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2041
2042 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002043 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002044 res_counter_uncharge(&old->memsw, bytes);
2045 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002046 }
2047 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002048}
2049
2050/*
2051 * This must be called under preempt disabled or must be called by
2052 * a thread which is pinned to local cpu.
2053 */
2054static void drain_local_stock(struct work_struct *dummy)
2055{
2056 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
2057 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002058 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002059}
2060
2061/*
2062 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002063 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002064 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002065static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002066{
2067 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2068
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002069 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002070 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002071 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002072 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002073 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002074 put_cpu_var(memcg_stock);
2075}
2076
2077/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002078 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Michal Hockod38144b2011-07-26 16:08:28 -07002079 * of the hierarchy under it. sync flag says whether we should block
2080 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002081 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002082static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002083{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002084 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002085
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002086 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002087 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002088 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002089 for_each_online_cpu(cpu) {
2090 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002091 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002092
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002093 memcg = stock->cached;
2094 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002095 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002096 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002097 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002098 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2099 if (cpu == curcpu)
2100 drain_local_stock(&stock->work);
2101 else
2102 schedule_work_on(cpu, &stock->work);
2103 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002104 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002105 put_cpu();
Michal Hockod38144b2011-07-26 16:08:28 -07002106
2107 if (!sync)
2108 goto out;
2109
2110 for_each_online_cpu(cpu) {
2111 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002112 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002113 flush_work(&stock->work);
2114 }
2115out:
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002116 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002117}
2118
2119/*
2120 * Tries to drain stocked charges in other cpus. This function is asynchronous
2121 * and just put a work per cpu for draining localy on each cpu. Caller can
2122 * expects some charges will be back to res_counter later but cannot wait for
2123 * it.
2124 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002125static void drain_all_stock_async(struct mem_cgroup *root_memcg)
Michal Hockod38144b2011-07-26 16:08:28 -07002126{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002127 /*
2128 * If someone calls draining, avoid adding more kworker runs.
2129 */
2130 if (!mutex_trylock(&percpu_charge_mutex))
2131 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002132 drain_all_stock(root_memcg, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002133 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002134}
2135
2136/* This is a synchronous drain interface. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002137static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002138{
2139 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002140 mutex_lock(&percpu_charge_mutex);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002141 drain_all_stock(root_memcg, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002142 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002143}
2144
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002145/*
2146 * This function drains percpu counter value from DEAD cpu and
2147 * move it to local cpu. Note that this function can be preempted.
2148 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002149static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002150{
2151 int i;
2152
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002153 spin_lock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002154 for (i = 0; i < MEM_CGROUP_STAT_DATA; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002155 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002156
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002157 per_cpu(memcg->stat->count[i], cpu) = 0;
2158 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002159 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002160 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002161 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002162
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002163 per_cpu(memcg->stat->events[i], cpu) = 0;
2164 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002165 }
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002166 /* need to clear ON_MOVE value, works as a kind of lock. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002167 per_cpu(memcg->stat->count[MEM_CGROUP_ON_MOVE], cpu) = 0;
2168 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002169}
2170
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002171static void synchronize_mem_cgroup_on_move(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002172{
2173 int idx = MEM_CGROUP_ON_MOVE;
2174
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002175 spin_lock(&memcg->pcp_counter_lock);
2176 per_cpu(memcg->stat->count[idx], cpu) = memcg->nocpu_base.count[idx];
2177 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002178}
2179
2180static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002181 unsigned long action,
2182 void *hcpu)
2183{
2184 int cpu = (unsigned long)hcpu;
2185 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002186 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002187
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002188 if ((action == CPU_ONLINE)) {
2189 for_each_mem_cgroup_all(iter)
2190 synchronize_mem_cgroup_on_move(iter, cpu);
2191 return NOTIFY_OK;
2192 }
2193
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002194 if ((action != CPU_DEAD) || action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002195 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002196
2197 for_each_mem_cgroup_all(iter)
2198 mem_cgroup_drain_pcp_counter(iter, cpu);
2199
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002200 stock = &per_cpu(memcg_stock, cpu);
2201 drain_stock(stock);
2202 return NOTIFY_OK;
2203}
2204
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002205
2206/* See __mem_cgroup_try_charge() for details */
2207enum {
2208 CHARGE_OK, /* success */
2209 CHARGE_RETRY, /* need to retry but retry is not bad */
2210 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2211 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
2212 CHARGE_OOM_DIE, /* the current is killed because of OOM */
2213};
2214
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002215static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002216 unsigned int nr_pages, bool oom_check)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002217{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002218 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002219 struct mem_cgroup *mem_over_limit;
2220 struct res_counter *fail_res;
2221 unsigned long flags = 0;
2222 int ret;
2223
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002224 ret = res_counter_charge(&memcg->res, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002225
2226 if (likely(!ret)) {
2227 if (!do_swap_account)
2228 return CHARGE_OK;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002229 ret = res_counter_charge(&memcg->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002230 if (likely(!ret))
2231 return CHARGE_OK;
2232
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002233 res_counter_uncharge(&memcg->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002234 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2235 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2236 } else
2237 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002238 /*
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002239 * nr_pages can be either a huge page (HPAGE_PMD_NR), a batch
2240 * of regular pages (CHARGE_BATCH), or a single regular page (1).
Johannes Weiner9221edb2011-02-01 15:52:42 -08002241 *
2242 * Never reclaim on behalf of optional batching, retry with a
2243 * single page instead.
2244 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002245 if (nr_pages == CHARGE_BATCH)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002246 return CHARGE_RETRY;
2247
2248 if (!(gfp_mask & __GFP_WAIT))
2249 return CHARGE_WOULDBLOCK;
2250
2251 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
Ying Han0ae5e892011-05-26 16:25:25 -07002252 gfp_mask, flags, NULL);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002253 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002254 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002255 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002256 * Even though the limit is exceeded at this point, reclaim
2257 * may have been able to free some pages. Retry the charge
2258 * before killing the task.
2259 *
2260 * Only for regular pages, though: huge pages are rather
2261 * unlikely to succeed so close to the limit, and we fall back
2262 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002263 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002264 if (nr_pages == 1 && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002265 return CHARGE_RETRY;
2266
2267 /*
2268 * At task move, charge accounts can be doubly counted. So, it's
2269 * better to wait until the end of task_move if something is going on.
2270 */
2271 if (mem_cgroup_wait_acct_move(mem_over_limit))
2272 return CHARGE_RETRY;
2273
2274 /* If we don't need to call oom-killer at el, return immediately */
2275 if (!oom_check)
2276 return CHARGE_NOMEM;
2277 /* check OOM */
2278 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask))
2279 return CHARGE_OOM_DIE;
2280
2281 return CHARGE_RETRY;
2282}
2283
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002284/*
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002285 * Unlike exported interface, "oom" parameter is added. if oom==true,
2286 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002287 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002288static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002289 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002290 unsigned int nr_pages,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002291 struct mem_cgroup **ptr,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002292 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002293{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002294 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002295 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002296 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002297 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002298
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002299 /*
2300 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2301 * in system level. So, allow to go ahead dying process in addition to
2302 * MEMDIE process.
2303 */
2304 if (unlikely(test_thread_flag(TIF_MEMDIE)
2305 || fatal_signal_pending(current)))
2306 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002307
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002308 /*
Hugh Dickins3be912772008-02-07 00:14:19 -08002309 * We always charge the cgroup the mm_struct belongs to.
2310 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002311 * thread group leader migrates. It's possible that mm is not
2312 * set, if so charge the init_mm (happens for pagecache usage).
2313 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002314 if (!*ptr && !mm)
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002315 goto bypass;
2316again:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002317 if (*ptr) { /* css should be a valid one */
2318 memcg = *ptr;
2319 VM_BUG_ON(css_is_removed(&memcg->css));
2320 if (mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002321 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002322 if (nr_pages == 1 && consume_stock(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002323 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002324 css_get(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002325 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002326 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002327
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002328 rcu_read_lock();
2329 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002330 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002331 * Because we don't have task_lock(), "p" can exit.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002332 * In that case, "memcg" can point to root or p can be NULL with
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002333 * race with swapoff. Then, we have small risk of mis-accouning.
2334 * But such kind of mis-account by race always happens because
2335 * we don't have cgroup_mutex(). It's overkill and we allo that
2336 * small race, here.
2337 * (*) swapoff at el will charge against mm-struct not against
2338 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002339 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002340 memcg = mem_cgroup_from_task(p);
2341 if (!memcg || mem_cgroup_is_root(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002342 rcu_read_unlock();
2343 goto done;
2344 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002345 if (nr_pages == 1 && consume_stock(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002346 /*
2347 * It seems dagerous to access memcg without css_get().
2348 * But considering how consume_stok works, it's not
2349 * necessary. If consume_stock success, some charges
2350 * from this memcg are cached on this cpu. So, we
2351 * don't need to call css_get()/css_tryget() before
2352 * calling consume_stock().
2353 */
2354 rcu_read_unlock();
2355 goto done;
2356 }
2357 /* after here, we may be blocked. we need to get refcnt */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002358 if (!css_tryget(&memcg->css)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002359 rcu_read_unlock();
2360 goto again;
2361 }
2362 rcu_read_unlock();
2363 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002364
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002365 do {
2366 bool oom_check;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002367
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002368 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002369 if (fatal_signal_pending(current)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002370 css_put(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002371 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002372 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002373
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002374 oom_check = false;
2375 if (oom && !nr_oom_retries) {
2376 oom_check = true;
2377 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2378 }
Balbir Singh6d61ef42009-01-07 18:08:06 -08002379
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002380 ret = mem_cgroup_do_charge(memcg, gfp_mask, batch, oom_check);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002381 switch (ret) {
2382 case CHARGE_OK:
2383 break;
2384 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002385 batch = nr_pages;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002386 css_put(&memcg->css);
2387 memcg = NULL;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002388 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002389 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002390 css_put(&memcg->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002391 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002392 case CHARGE_NOMEM: /* OOM routine works */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002393 if (!oom) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002394 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002395 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002396 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002397 /* If oom, we never return -ENOMEM */
2398 nr_oom_retries--;
2399 break;
2400 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002401 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002402 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08002403 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002404 } while (ret != CHARGE_OK);
2405
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002406 if (batch > nr_pages)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002407 refill_stock(memcg, batch - nr_pages);
2408 css_put(&memcg->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002409done:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002410 *ptr = memcg;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002411 return 0;
2412nomem:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002413 *ptr = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002414 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002415bypass:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002416 *ptr = NULL;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002417 return 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002418}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002419
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002420/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002421 * Somemtimes we have to undo a charge we got by try_charge().
2422 * This function is for that and do uncharge, put css's refcnt.
2423 * gotten by try_charge().
2424 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002425static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002426 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002427{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002428 if (!mem_cgroup_is_root(memcg)) {
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002429 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002430
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002431 res_counter_uncharge(&memcg->res, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002432 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002433 res_counter_uncharge(&memcg->memsw, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002434 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002435}
2436
2437/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002438 * A helper function to get mem_cgroup from ID. must be called under
2439 * rcu_read_lock(). The caller must check css_is_removed() or some if
2440 * it's concern. (dropping refcnt from swap can be called against removed
2441 * memcg.)
2442 */
2443static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2444{
2445 struct cgroup_subsys_state *css;
2446
2447 /* ID 0 is unused ID */
2448 if (!id)
2449 return NULL;
2450 css = css_lookup(&mem_cgroup_subsys, id);
2451 if (!css)
2452 return NULL;
2453 return container_of(css, struct mem_cgroup, css);
2454}
2455
Wu Fengguange42d9d52009-12-16 12:19:59 +01002456struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002457{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002458 struct mem_cgroup *memcg = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002459 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002460 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002461 swp_entry_t ent;
2462
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002463 VM_BUG_ON(!PageLocked(page));
2464
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002465 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002466 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002467 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002468 memcg = pc->mem_cgroup;
2469 if (memcg && !css_tryget(&memcg->css))
2470 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002471 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002472 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002473 id = lookup_swap_cgroup(ent);
2474 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002475 memcg = mem_cgroup_lookup(id);
2476 if (memcg && !css_tryget(&memcg->css))
2477 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002478 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002479 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002480 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002481 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002482}
2483
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002484static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,
Johannes Weiner5564e882011-03-23 16:42:29 -07002485 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002486 unsigned int nr_pages,
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002487 struct page_cgroup *pc,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002488 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002489{
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002490 lock_page_cgroup(pc);
2491 if (unlikely(PageCgroupUsed(pc))) {
2492 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002493 __mem_cgroup_cancel_charge(memcg, nr_pages);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002494 return;
2495 }
2496 /*
2497 * we don't need page_cgroup_lock about tail pages, becase they are not
2498 * accessed by any other context at this point.
2499 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002500 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002501 /*
2502 * We access a page_cgroup asynchronously without lock_page_cgroup().
2503 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2504 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2505 * before USED bit, we need memory barrier here.
2506 * See mem_cgroup_add_lru_list(), etc.
2507 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002508 smp_wmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -07002509 switch (ctype) {
2510 case MEM_CGROUP_CHARGE_TYPE_CACHE:
2511 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
2512 SetPageCgroupCache(pc);
2513 SetPageCgroupUsed(pc);
2514 break;
2515 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
2516 ClearPageCgroupCache(pc);
2517 SetPageCgroupUsed(pc);
2518 break;
2519 default:
2520 break;
2521 }
Hugh Dickins3be912772008-02-07 00:14:19 -08002522
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002523 mem_cgroup_charge_statistics(memcg, PageCgroupCache(pc), nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002524 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002525 /*
2526 * "charge_statistics" updated event counter. Then, check it.
2527 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2528 * if they exceeds softlimit.
2529 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002530 memcg_check_events(memcg, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002531}
2532
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002533#ifdef CONFIG_TRANSPARENT_HUGEPAGE
2534
2535#define PCGF_NOCOPY_AT_SPLIT ((1 << PCG_LOCK) | (1 << PCG_MOVE_LOCK) |\
2536 (1 << PCG_ACCT_LRU) | (1 << PCG_MIGRATION))
2537/*
2538 * Because tail pages are not marked as "used", set it. We're under
2539 * zone->lru_lock, 'splitting on pmd' and compund_lock.
2540 */
2541void mem_cgroup_split_huge_fixup(struct page *head, struct page *tail)
2542{
2543 struct page_cgroup *head_pc = lookup_page_cgroup(head);
2544 struct page_cgroup *tail_pc = lookup_page_cgroup(tail);
2545 unsigned long flags;
2546
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08002547 if (mem_cgroup_disabled())
2548 return;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002549 /*
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002550 * We have no races with charge/uncharge but will have races with
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002551 * page state accounting.
2552 */
2553 move_lock_page_cgroup(head_pc, &flags);
2554
2555 tail_pc->mem_cgroup = head_pc->mem_cgroup;
2556 smp_wmb(); /* see __commit_charge() */
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002557 if (PageCgroupAcctLRU(head_pc)) {
2558 enum lru_list lru;
2559 struct mem_cgroup_per_zone *mz;
2560
2561 /*
2562 * LRU flags cannot be copied because we need to add tail
2563 *.page to LRU by generic call and our hook will be called.
2564 * We hold lru_lock, then, reduce counter directly.
2565 */
2566 lru = page_lru(head);
Johannes Weiner97a6c372011-03-23 16:42:27 -07002567 mz = page_cgroup_zoneinfo(head_pc->mem_cgroup, head);
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002568 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
2569 }
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002570 tail_pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
2571 move_unlock_page_cgroup(head_pc, &flags);
2572}
2573#endif
2574
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002575/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07002576 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07002577 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002578 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002579 * @pc: page_cgroup of the page.
2580 * @from: mem_cgroup which the page is moved from.
2581 * @to: mem_cgroup which the page is moved to. @from != @to.
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002582 * @uncharge: whether we should call uncharge and css_put against @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002583 *
2584 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002585 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002586 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002587 *
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002588 * This function doesn't do "charge" nor css_get to new cgroup. It should be
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002589 * done by a caller(__mem_cgroup_try_charge would be useful). If @uncharge is
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002590 * true, this function does "uncharge" from old cgroup, but it doesn't if
2591 * @uncharge is false, so a caller should do "uncharge".
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002592 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002593static int mem_cgroup_move_account(struct page *page,
2594 unsigned int nr_pages,
2595 struct page_cgroup *pc,
2596 struct mem_cgroup *from,
2597 struct mem_cgroup *to,
2598 bool uncharge)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002599{
Johannes Weinerde3638d2011-03-23 16:42:28 -07002600 unsigned long flags;
2601 int ret;
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002602
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002603 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07002604 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07002605 /*
2606 * The page is isolated from LRU. So, collapse function
2607 * will not handle this page. But page splitting can happen.
2608 * Do this check under compound_page_lock(). The caller should
2609 * hold it.
2610 */
2611 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002612 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07002613 goto out;
2614
2615 lock_page_cgroup(pc);
2616
2617 ret = -EINVAL;
2618 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
2619 goto unlock;
2620
2621 move_lock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002622
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002623 if (PageCgroupFileMapped(pc)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08002624 /* Update mapped_file data for mem_cgroup */
2625 preempt_disable();
2626 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2627 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2628 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07002629 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002630 mem_cgroup_charge_statistics(from, PageCgroupCache(pc), -nr_pages);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002631 if (uncharge)
2632 /* This is not "cancel", but cancel_charge does all we need. */
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002633 __mem_cgroup_cancel_charge(from, nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07002634
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002635 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002636 pc->mem_cgroup = to;
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002637 mem_cgroup_charge_statistics(to, PageCgroupCache(pc), nr_pages);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002638 /*
2639 * We charges against "to" which may not have any tasks. Then, "to"
2640 * can be under rmdir(). But in current implementation, caller of
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08002641 * this function is just force_empty() and move charge, so it's
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002642 * guaranteed that "to" is never removed. So, we don't check rmdir
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08002643 * status here.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002644 */
Johannes Weinerde3638d2011-03-23 16:42:28 -07002645 move_unlock_page_cgroup(pc, &flags);
2646 ret = 0;
2647unlock:
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002648 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002649 /*
2650 * check events
2651 */
Johannes Weiner5564e882011-03-23 16:42:29 -07002652 memcg_check_events(to, page);
2653 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07002654out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002655 return ret;
2656}
2657
2658/*
2659 * move charges to its parent.
2660 */
2661
Johannes Weiner5564e882011-03-23 16:42:29 -07002662static int mem_cgroup_move_parent(struct page *page,
2663 struct page_cgroup *pc,
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002664 struct mem_cgroup *child,
2665 gfp_t gfp_mask)
2666{
2667 struct cgroup *cg = child->css.cgroup;
2668 struct cgroup *pcg = cg->parent;
2669 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002670 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07002671 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002672 int ret;
2673
2674 /* Is ROOT ? */
2675 if (!pcg)
2676 return -EINVAL;
2677
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002678 ret = -EBUSY;
2679 if (!get_page_unless_zero(page))
2680 goto out;
2681 if (isolate_lru_page(page))
2682 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08002683
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002684 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002685
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002686 parent = mem_cgroup_from_cont(pcg);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002687 ret = __mem_cgroup_try_charge(NULL, gfp_mask, nr_pages, &parent, false);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002688 if (ret || !parent)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002689 goto put_back;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002690
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002691 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002692 flags = compound_lock_irqsave(page);
2693
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002694 ret = mem_cgroup_move_account(page, nr_pages, pc, child, parent, true);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002695 if (ret)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002696 __mem_cgroup_cancel_charge(parent, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08002697
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002698 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002699 compound_unlock_irqrestore(page, flags);
Jesper Juhl8dba4742011-01-25 15:07:24 -08002700put_back:
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002701 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002702put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08002703 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002704out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002705 return ret;
2706}
2707
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002708/*
2709 * Charge the memory controller for page usage.
2710 * Return
2711 * 0 if the charge was successful
2712 * < 0 if the cgroup is over its limit
2713 */
2714static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002715 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002716{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002717 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002718 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08002719 struct page_cgroup *pc;
2720 bool oom = true;
2721 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08002722
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002723 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002724 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002725 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08002726 /*
2727 * Never OOM-kill a process for a huge page. The
2728 * fault handler will fall back to regular pages.
2729 */
2730 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002731 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002732
2733 pc = lookup_page_cgroup(page);
Johannes Weineraf4a6622011-03-23 16:42:24 -07002734 BUG_ON(!pc); /* XXX: remove this and move pc lookup into commit */
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002735
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002736 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom);
2737 if (ret || !memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002738 return ret;
2739
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002740 __mem_cgroup_commit_charge(memcg, page, nr_pages, pc, ctype);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002741 return 0;
2742}
2743
2744int mem_cgroup_newpage_charge(struct page *page,
2745 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002746{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002747 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002748 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002749 /*
2750 * If already mapped, we don't have to account.
2751 * If page cache, page->mapping has address_space.
2752 * But page->mapping may have out-of-use anon_vma pointer,
2753 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
2754 * is NULL.
2755 */
2756 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
2757 return 0;
2758 if (unlikely(!mm))
2759 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002760 return mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002761 MEM_CGROUP_CHARGE_TYPE_MAPPED);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002762}
2763
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002764static void
2765__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2766 enum charge_type ctype);
2767
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002768static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002769__mem_cgroup_commit_charge_lrucare(struct page *page, struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002770 enum charge_type ctype)
2771{
2772 struct page_cgroup *pc = lookup_page_cgroup(page);
2773 /*
2774 * In some case, SwapCache, FUSE(splice_buf->radixtree), the page
2775 * is already on LRU. It means the page may on some other page_cgroup's
2776 * LRU. Take care of it.
2777 */
2778 mem_cgroup_lru_del_before_commit(page);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002779 __mem_cgroup_commit_charge(memcg, page, 1, pc, ctype);
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002780 mem_cgroup_lru_add_after_commit(page);
2781 return;
2782}
2783
Balbir Singhe1a1cd52008-02-07 00:14:02 -08002784int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
2785 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08002786{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002787 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002788 int ret;
2789
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002790 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002791 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002792 if (PageCompound(page))
2793 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002794
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002795 if (unlikely(!mm))
Balbir Singh8697d332008-02-07 00:13:59 -08002796 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002797
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002798 if (page_is_file_cache(page)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002799 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, &memcg, true);
2800 if (ret || !memcg)
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002801 return ret;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002802
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002803 /*
2804 * FUSE reuses pages without going through the final
2805 * put that would remove them from the LRU list, make
2806 * sure that they get relinked properly.
2807 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002808 __mem_cgroup_commit_charge_lrucare(page, memcg,
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002809 MEM_CGROUP_CHARGE_TYPE_CACHE);
2810 return ret;
2811 }
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002812 /* shmem */
2813 if (PageSwapCache(page)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002814 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &memcg);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002815 if (!ret)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002816 __mem_cgroup_commit_charge_swapin(page, memcg,
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002817 MEM_CGROUP_CHARGE_TYPE_SHMEM);
2818 } else
2819 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002820 MEM_CGROUP_CHARGE_TYPE_SHMEM);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002821
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002822 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002823}
2824
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002825/*
2826 * While swap-in, try_charge -> commit or cancel, the page is locked.
2827 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02002828 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002829 * "commit()" or removed by "cancel()"
2830 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002831int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
2832 struct page *page,
2833 gfp_t mask, struct mem_cgroup **ptr)
2834{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002835 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002836 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002837
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07002838 *ptr = NULL;
2839
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002840 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002841 return 0;
2842
2843 if (!do_swap_account)
2844 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002845 /*
2846 * A racing thread's fault, or swapoff, may have already updated
Hugh Dickins407f9c82009-12-14 17:59:30 -08002847 * the pte, and even removed page from swap cache: in those cases
2848 * do_swap_page()'s pte_same() test will fail; but there's also a
2849 * KSM case which does need to charge the page.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002850 */
2851 if (!PageSwapCache(page))
Hugh Dickins407f9c82009-12-14 17:59:30 -08002852 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002853 memcg = try_get_mem_cgroup_from_page(page);
2854 if (!memcg)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002855 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002856 *ptr = memcg;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002857 ret = __mem_cgroup_try_charge(NULL, mask, 1, ptr, true);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002858 css_put(&memcg->css);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002859 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002860charge_cur_mm:
2861 if (unlikely(!mm))
2862 mm = &init_mm;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002863 return __mem_cgroup_try_charge(mm, mask, 1, ptr, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002864}
2865
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002866static void
2867__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2868 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002869{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002870 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002871 return;
2872 if (!ptr)
2873 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002874 cgroup_exclude_rmdir(&ptr->css);
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002875
2876 __mem_cgroup_commit_charge_lrucare(page, ptr, ctype);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002877 /*
2878 * Now swap is on-memory. This means this page may be
2879 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002880 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
2881 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
2882 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002883 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002884 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002885 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002886 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002887 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002888
2889 id = swap_cgroup_record(ent, 0);
2890 rcu_read_lock();
2891 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002892 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002893 /*
2894 * This recorded memcg can be obsolete one. So, avoid
2895 * calling css_tryget
2896 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002897 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002898 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002899 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002900 mem_cgroup_put(memcg);
2901 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002902 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002903 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002904 /*
2905 * At swapin, we may charge account against cgroup which has no tasks.
2906 * So, rmdir()->pre_destroy() can be called while we do this charge.
2907 * In that case, we need to call pre_destroy() again. check it here.
2908 */
2909 cgroup_release_and_wakeup_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002910}
2911
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002912void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
2913{
2914 __mem_cgroup_commit_charge_swapin(page, ptr,
2915 MEM_CGROUP_CHARGE_TYPE_MAPPED);
2916}
2917
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002918void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002919{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002920 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002921 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002922 if (!memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002923 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002924 __mem_cgroup_cancel_charge(memcg, 1);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002925}
2926
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002927static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002928 unsigned int nr_pages,
2929 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002930{
2931 struct memcg_batch_info *batch = NULL;
2932 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002933
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002934 /* If swapout, usage of swap doesn't decrease */
2935 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2936 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002937
2938 batch = &current->memcg_batch;
2939 /*
2940 * In usual, we do css_get() when we remember memcg pointer.
2941 * But in this case, we keep res->usage until end of a series of
2942 * uncharges. Then, it's ok to ignore memcg's refcnt.
2943 */
2944 if (!batch->memcg)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002945 batch->memcg = memcg;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002946 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002947 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002948 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002949 * the same cgroup and we have chance to coalesce uncharges.
2950 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
2951 * because we want to do uncharge as soon as possible.
2952 */
2953
2954 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
2955 goto direct_uncharge;
2956
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002957 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08002958 goto direct_uncharge;
2959
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002960 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002961 * In typical case, batch->memcg == mem. This means we can
2962 * merge a series of uncharges to an uncharge of res_counter.
2963 * If not, we uncharge res_counter ony by one.
2964 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002965 if (batch->memcg != memcg)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002966 goto direct_uncharge;
2967 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07002968 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002969 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07002970 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002971 return;
2972direct_uncharge:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002973 res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002974 if (uncharge_memsw)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002975 res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE);
2976 if (unlikely(batch->memcg != memcg))
2977 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002978 return;
2979}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002980
Balbir Singh8697d332008-02-07 00:13:59 -08002981/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002982 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002983 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002984static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002985__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002986{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002987 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002988 unsigned int nr_pages = 1;
2989 struct page_cgroup *pc;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002990
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002991 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002992 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07002993
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002994 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002995 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002996
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002997 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002998 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002999 VM_BUG_ON(!PageTransHuge(page));
3000 }
Balbir Singh8697d332008-02-07 00:13:59 -08003001 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08003002 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08003003 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003004 pc = lookup_page_cgroup(page);
3005 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003006 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003007
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003008 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003009
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003010 memcg = pc->mem_cgroup;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003011
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003012 if (!PageCgroupUsed(pc))
3013 goto unlock_out;
3014
3015 switch (ctype) {
3016 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003017 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003018 /* See mem_cgroup_prepare_migration() */
3019 if (page_mapped(page) || PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003020 goto unlock_out;
3021 break;
3022 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
3023 if (!PageAnon(page)) { /* Shared memory */
3024 if (page->mapping && !page_is_file_cache(page))
3025 goto unlock_out;
3026 } else if (page_mapped(page)) /* Anon */
3027 goto unlock_out;
3028 break;
3029 default:
3030 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003031 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003032
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003033 mem_cgroup_charge_statistics(memcg, PageCgroupCache(pc), -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003034
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003035 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08003036 /*
3037 * pc->mem_cgroup is not cleared here. It will be accessed when it's
3038 * freed from LRU. This is safe because uncharged page is expected not
3039 * to be reused (freed soon). Exception is SwapCache, it's handled by
3040 * special functions.
3041 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003042
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003043 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003044 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003045 * even after unlock, we have memcg->res.usage here and this memcg
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003046 * will never be freed.
3047 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003048 memcg_check_events(memcg, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003049 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003050 mem_cgroup_swap_statistics(memcg, true);
3051 mem_cgroup_get(memcg);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003052 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003053 if (!mem_cgroup_is_root(memcg))
3054 mem_cgroup_do_uncharge(memcg, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003055
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003056 return memcg;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003057
3058unlock_out:
3059 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003060 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08003061}
3062
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003063void mem_cgroup_uncharge_page(struct page *page)
3064{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003065 /* early check. */
3066 if (page_mapped(page))
3067 return;
3068 if (page->mapping && !PageAnon(page))
3069 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003070 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
3071}
3072
3073void mem_cgroup_uncharge_cache_page(struct page *page)
3074{
3075 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07003076 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003077 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
3078}
3079
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003080/*
3081 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
3082 * In that cases, pages are freed continuously and we can expect pages
3083 * are in the same memcg. All these calls itself limits the number of
3084 * pages freed at once, then uncharge_start/end() is called properly.
3085 * This may be called prural(2) times in a context,
3086 */
3087
3088void mem_cgroup_uncharge_start(void)
3089{
3090 current->memcg_batch.do_batch++;
3091 /* We can do nest. */
3092 if (current->memcg_batch.do_batch == 1) {
3093 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003094 current->memcg_batch.nr_pages = 0;
3095 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003096 }
3097}
3098
3099void mem_cgroup_uncharge_end(void)
3100{
3101 struct memcg_batch_info *batch = &current->memcg_batch;
3102
3103 if (!batch->do_batch)
3104 return;
3105
3106 batch->do_batch--;
3107 if (batch->do_batch) /* If stacked, do nothing. */
3108 return;
3109
3110 if (!batch->memcg)
3111 return;
3112 /*
3113 * This "batch->memcg" is valid without any css_get/put etc...
3114 * bacause we hide charges behind us.
3115 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003116 if (batch->nr_pages)
3117 res_counter_uncharge(&batch->memcg->res,
3118 batch->nr_pages * PAGE_SIZE);
3119 if (batch->memsw_nr_pages)
3120 res_counter_uncharge(&batch->memcg->memsw,
3121 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003122 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003123 /* forget this pointer (for sanity check) */
3124 batch->memcg = NULL;
3125}
3126
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003127#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003128/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003129 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003130 * memcg information is recorded to swap_cgroup of "ent"
3131 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003132void
3133mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003134{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003135 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003136 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003137
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003138 if (!swapout) /* this was a swap cache but the swap is unused ! */
3139 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
3140
3141 memcg = __mem_cgroup_uncharge_common(page, ctype);
3142
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003143 /*
3144 * record memcg information, if swapout && memcg != NULL,
3145 * mem_cgroup_get() was called in uncharge().
3146 */
3147 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003148 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003149}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003150#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003151
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003152#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3153/*
3154 * called from swap_entry_free(). remove record in swap_cgroup and
3155 * uncharge "memsw" account.
3156 */
3157void mem_cgroup_uncharge_swap(swp_entry_t ent)
3158{
3159 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003160 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003161
3162 if (!do_swap_account)
3163 return;
3164
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003165 id = swap_cgroup_record(ent, 0);
3166 rcu_read_lock();
3167 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003168 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003169 /*
3170 * We uncharge this because swap is freed.
3171 * This memcg can be obsolete one. We avoid calling css_tryget
3172 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003173 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07003174 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003175 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003176 mem_cgroup_put(memcg);
3177 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003178 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003179}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003180
3181/**
3182 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
3183 * @entry: swap entry to be moved
3184 * @from: mem_cgroup which the entry is moved from
3185 * @to: mem_cgroup which the entry is moved to
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003186 * @need_fixup: whether we should fixup res_counters and refcounts.
Daisuke Nishimura02491442010-03-10 15:22:17 -08003187 *
3188 * It succeeds only when the swap_cgroup's record for this entry is the same
3189 * as the mem_cgroup's id of @from.
3190 *
3191 * Returns 0 on success, -EINVAL on failure.
3192 *
3193 * The caller must have charged to @to, IOW, called res_counter_charge() about
3194 * both res and memsw, and called css_get().
3195 */
3196static int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003197 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003198{
3199 unsigned short old_id, new_id;
3200
3201 old_id = css_id(&from->css);
3202 new_id = css_id(&to->css);
3203
3204 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08003205 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003206 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003207 /*
3208 * This function is only called from task migration context now.
3209 * It postpones res_counter and refcount handling till the end
3210 * of task migration(mem_cgroup_clear_mc()) for performance
3211 * improvement. But we cannot postpone mem_cgroup_get(to)
3212 * because if the process that has been moved to @to does
3213 * swap-in, the refcount of @to might be decreased to 0.
3214 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08003215 mem_cgroup_get(to);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003216 if (need_fixup) {
3217 if (!mem_cgroup_is_root(from))
3218 res_counter_uncharge(&from->memsw, PAGE_SIZE);
3219 mem_cgroup_put(from);
3220 /*
3221 * we charged both to->res and to->memsw, so we should
3222 * uncharge to->res.
3223 */
3224 if (!mem_cgroup_is_root(to))
3225 res_counter_uncharge(&to->res, PAGE_SIZE);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003226 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08003227 return 0;
3228 }
3229 return -EINVAL;
3230}
3231#else
3232static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003233 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003234{
3235 return -EINVAL;
3236}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003237#endif
3238
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003239/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003240 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
3241 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003242 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003243int mem_cgroup_prepare_migration(struct page *page,
Miklos Szeredief6a3c62011-03-22 16:30:52 -07003244 struct page *newpage, struct mem_cgroup **ptr, gfp_t gfp_mask)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003245{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003246 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003247 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003248 enum charge_type ctype;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003249 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003250
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07003251 *ptr = NULL;
3252
Andrea Arcangeliec168512011-01-13 15:46:56 -08003253 VM_BUG_ON(PageTransHuge(page));
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003254 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07003255 return 0;
3256
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003257 pc = lookup_page_cgroup(page);
3258 lock_page_cgroup(pc);
3259 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003260 memcg = pc->mem_cgroup;
3261 css_get(&memcg->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003262 /*
3263 * At migrating an anonymous page, its mapcount goes down
3264 * to 0 and uncharge() will be called. But, even if it's fully
3265 * unmapped, migration may fail and this page has to be
3266 * charged again. We set MIGRATION flag here and delay uncharge
3267 * until end_migration() is called
3268 *
3269 * Corner Case Thinking
3270 * A)
3271 * When the old page was mapped as Anon and it's unmap-and-freed
3272 * while migration was ongoing.
3273 * If unmap finds the old page, uncharge() of it will be delayed
3274 * until end_migration(). If unmap finds a new page, it's
3275 * uncharged when it make mapcount to be 1->0. If unmap code
3276 * finds swap_migration_entry, the new page will not be mapped
3277 * and end_migration() will find it(mapcount==0).
3278 *
3279 * B)
3280 * When the old page was mapped but migraion fails, the kernel
3281 * remaps it. A charge for it is kept by MIGRATION flag even
3282 * if mapcount goes down to 0. We can do remap successfully
3283 * without charging it again.
3284 *
3285 * C)
3286 * The "old" page is under lock_page() until the end of
3287 * migration, so, the old page itself will not be swapped-out.
3288 * If the new page is swapped out before end_migraton, our
3289 * hook to usual swap-out path will catch the event.
3290 */
3291 if (PageAnon(page))
3292 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003293 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003294 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003295 /*
3296 * If the page is not charged at this point,
3297 * we return here.
3298 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003299 if (!memcg)
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003300 return 0;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003301
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003302 *ptr = memcg;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003303 ret = __mem_cgroup_try_charge(NULL, gfp_mask, 1, ptr, false);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003304 css_put(&memcg->css);/* drop extra refcnt */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003305 if (ret || *ptr == NULL) {
3306 if (PageAnon(page)) {
3307 lock_page_cgroup(pc);
3308 ClearPageCgroupMigration(pc);
3309 unlock_page_cgroup(pc);
3310 /*
3311 * The old page may be fully unmapped while we kept it.
3312 */
3313 mem_cgroup_uncharge_page(page);
3314 }
3315 return -ENOMEM;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003316 }
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003317 /*
3318 * We charge new page before it's used/mapped. So, even if unlock_page()
3319 * is called before end_migration, we can catch all events on this new
3320 * page. In the case new page is migrated but not remapped, new page's
3321 * mapcount will be finally 0 and we call uncharge in end_migration().
3322 */
3323 pc = lookup_page_cgroup(newpage);
3324 if (PageAnon(page))
3325 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
3326 else if (page_is_file_cache(page))
3327 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
3328 else
3329 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003330 __mem_cgroup_commit_charge(memcg, page, 1, pc, ctype);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003331 return ret;
3332}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08003333
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003334/* remove redundant charge if migration failed*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003335void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003336 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003337{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003338 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003339 struct page_cgroup *pc;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003340
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003341 if (!memcg)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003342 return;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003343 /* blocks rmdir() */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003344 cgroup_exclude_rmdir(&memcg->css);
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003345 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003346 used = oldpage;
3347 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003348 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003349 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003350 unused = oldpage;
3351 }
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003352 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003353 * We disallowed uncharge of pages under migration because mapcount
3354 * of the page goes down to zero, temporarly.
3355 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003356 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003357 pc = lookup_page_cgroup(oldpage);
3358 lock_page_cgroup(pc);
3359 ClearPageCgroupMigration(pc);
3360 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003361
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003362 __mem_cgroup_uncharge_common(unused, MEM_CGROUP_CHARGE_TYPE_FORCE);
3363
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003364 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003365 * If a page is a file cache, radix-tree replacement is very atomic
3366 * and we can skip this check. When it was an Anon page, its mapcount
3367 * goes down to 0. But because we added MIGRATION flage, it's not
3368 * uncharged yet. There are several case but page->mapcount check
3369 * and USED bit check in mem_cgroup_uncharge_page() will do enough
3370 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003371 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003372 if (PageAnon(used))
3373 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003374 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003375 * At migration, we may charge account against cgroup which has no
3376 * tasks.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003377 * So, rmdir()->pre_destroy() can be called while we do this charge.
3378 * In that case, we need to call pre_destroy() again. check it here.
3379 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003380 cgroup_release_and_wakeup_rmdir(&memcg->css);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003381}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08003382
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003383#ifdef CONFIG_DEBUG_VM
3384static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
3385{
3386 struct page_cgroup *pc;
3387
3388 pc = lookup_page_cgroup(page);
3389 if (likely(pc) && PageCgroupUsed(pc))
3390 return pc;
3391 return NULL;
3392}
3393
3394bool mem_cgroup_bad_page_check(struct page *page)
3395{
3396 if (mem_cgroup_disabled())
3397 return false;
3398
3399 return lookup_page_cgroup_used(page) != NULL;
3400}
3401
3402void mem_cgroup_print_bad_page(struct page *page)
3403{
3404 struct page_cgroup *pc;
3405
3406 pc = lookup_page_cgroup_used(page);
3407 if (pc) {
3408 int ret = -1;
3409 char *path;
3410
3411 printk(KERN_ALERT "pc:%p pc->flags:%lx pc->mem_cgroup:%p",
3412 pc, pc->flags, pc->mem_cgroup);
3413
3414 path = kmalloc(PATH_MAX, GFP_KERNEL);
3415 if (path) {
3416 rcu_read_lock();
3417 ret = cgroup_path(pc->mem_cgroup->css.cgroup,
3418 path, PATH_MAX);
3419 rcu_read_unlock();
3420 }
3421
3422 printk(KERN_CONT "(%s)\n",
3423 (ret < 0) ? "cannot get the path" : path);
3424 kfree(path);
3425 }
3426}
3427#endif
3428
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003429static DEFINE_MUTEX(set_limit_mutex);
3430
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08003431static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003432 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003433{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003434 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003435 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003436 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003437 int children = mem_cgroup_count_children(memcg);
3438 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003439 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003440
3441 /*
3442 * For keeping hierarchical_reclaim simple, how long we should retry
3443 * is depends on callers. We set our retry-count to be function
3444 * of # of children which we should visit in this loop.
3445 */
3446 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
3447
3448 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003449
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003450 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003451 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003452 if (signal_pending(current)) {
3453 ret = -EINTR;
3454 break;
3455 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003456 /*
3457 * Rather than hide all in some function, I do this in
3458 * open coded manner. You see what this really does.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003459 * We have to guarantee memcg->res.limit < memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003460 */
3461 mutex_lock(&set_limit_mutex);
3462 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3463 if (memswlimit < val) {
3464 ret = -EINVAL;
3465 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003466 break;
3467 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003468
3469 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3470 if (memlimit < val)
3471 enlarge = 1;
3472
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003473 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003474 if (!ret) {
3475 if (memswlimit == val)
3476 memcg->memsw_is_minimum = true;
3477 else
3478 memcg->memsw_is_minimum = false;
3479 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003480 mutex_unlock(&set_limit_mutex);
3481
3482 if (!ret)
3483 break;
3484
Bob Liuaa20d482009-12-15 16:47:14 -08003485 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Ying Han0ae5e892011-05-26 16:25:25 -07003486 MEM_CGROUP_RECLAIM_SHRINK,
3487 NULL);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003488 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
3489 /* Usage is reduced ? */
3490 if (curusage >= oldusage)
3491 retry_count--;
3492 else
3493 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003494 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003495 if (!ret && enlarge)
3496 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08003497
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003498 return ret;
3499}
3500
Li Zefan338c8432009-06-17 16:27:15 -07003501static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
3502 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003503{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003504 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003505 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003506 int children = mem_cgroup_count_children(memcg);
3507 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003508 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003509
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003510 /* see mem_cgroup_resize_res_limit */
3511 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
3512 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003513 while (retry_count) {
3514 if (signal_pending(current)) {
3515 ret = -EINTR;
3516 break;
3517 }
3518 /*
3519 * Rather than hide all in some function, I do this in
3520 * open coded manner. You see what this really does.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003521 * We have to guarantee memcg->res.limit < memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003522 */
3523 mutex_lock(&set_limit_mutex);
3524 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3525 if (memlimit > val) {
3526 ret = -EINVAL;
3527 mutex_unlock(&set_limit_mutex);
3528 break;
3529 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003530 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3531 if (memswlimit < val)
3532 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003533 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003534 if (!ret) {
3535 if (memlimit == val)
3536 memcg->memsw_is_minimum = true;
3537 else
3538 memcg->memsw_is_minimum = false;
3539 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003540 mutex_unlock(&set_limit_mutex);
3541
3542 if (!ret)
3543 break;
3544
Balbir Singh4e416952009-09-23 15:56:39 -07003545 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh75822b42009-09-23 15:56:38 -07003546 MEM_CGROUP_RECLAIM_NOSWAP |
Ying Han0ae5e892011-05-26 16:25:25 -07003547 MEM_CGROUP_RECLAIM_SHRINK,
3548 NULL);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003549 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003550 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003551 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003552 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003553 else
3554 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003555 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003556 if (!ret && enlarge)
3557 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003558 return ret;
3559}
3560
Balbir Singh4e416952009-09-23 15:56:39 -07003561unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
Ying Han0ae5e892011-05-26 16:25:25 -07003562 gfp_t gfp_mask,
3563 unsigned long *total_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07003564{
3565 unsigned long nr_reclaimed = 0;
3566 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
3567 unsigned long reclaimed;
3568 int loop = 0;
3569 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003570 unsigned long long excess;
Ying Han0ae5e892011-05-26 16:25:25 -07003571 unsigned long nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003572
3573 if (order > 0)
3574 return 0;
3575
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07003576 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
Balbir Singh4e416952009-09-23 15:56:39 -07003577 /*
3578 * This loop can run a while, specially if mem_cgroup's continuously
3579 * keep exceeding their soft limit and putting the system under
3580 * pressure
3581 */
3582 do {
3583 if (next_mz)
3584 mz = next_mz;
3585 else
3586 mz = mem_cgroup_largest_soft_limit_node(mctz);
3587 if (!mz)
3588 break;
3589
Ying Han0ae5e892011-05-26 16:25:25 -07003590 nr_scanned = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07003591 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
3592 gfp_mask,
Ying Han0ae5e892011-05-26 16:25:25 -07003593 MEM_CGROUP_RECLAIM_SOFT,
3594 &nr_scanned);
Balbir Singh4e416952009-09-23 15:56:39 -07003595 nr_reclaimed += reclaimed;
Ying Han0ae5e892011-05-26 16:25:25 -07003596 *total_scanned += nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003597 spin_lock(&mctz->lock);
3598
3599 /*
3600 * If we failed to reclaim anything from this memory cgroup
3601 * it is time to move on to the next cgroup
3602 */
3603 next_mz = NULL;
3604 if (!reclaimed) {
3605 do {
3606 /*
3607 * Loop until we find yet another one.
3608 *
3609 * By the time we get the soft_limit lock
3610 * again, someone might have aded the
3611 * group back on the RB tree. Iterate to
3612 * make sure we get a different mem.
3613 * mem_cgroup_largest_soft_limit_node returns
3614 * NULL if no other cgroup is present on
3615 * the tree
3616 */
3617 next_mz =
3618 __mem_cgroup_largest_soft_limit_node(mctz);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003619 if (next_mz == mz)
Balbir Singh4e416952009-09-23 15:56:39 -07003620 css_put(&next_mz->mem->css);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003621 else /* next_mz == NULL or other memcg */
Balbir Singh4e416952009-09-23 15:56:39 -07003622 break;
3623 } while (1);
3624 }
Balbir Singh4e416952009-09-23 15:56:39 -07003625 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003626 excess = res_counter_soft_limit_excess(&mz->mem->res);
Balbir Singh4e416952009-09-23 15:56:39 -07003627 /*
3628 * One school of thought says that we should not add
3629 * back the node to the tree if reclaim returns 0.
3630 * But our reclaim could return 0, simply because due
3631 * to priority we are exposing a smaller subset of
3632 * memory to reclaim from. Consider this as a longer
3633 * term TODO.
3634 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003635 /* If excess == 0, no tree ops */
3636 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07003637 spin_unlock(&mctz->lock);
3638 css_put(&mz->mem->css);
3639 loop++;
3640 /*
3641 * Could not reclaim anything and there are no more
3642 * mem cgroups to try or we seem to be looping without
3643 * reclaiming anything.
3644 */
3645 if (!nr_reclaimed &&
3646 (next_mz == NULL ||
3647 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3648 break;
3649 } while (!nr_reclaimed);
3650 if (next_mz)
3651 css_put(&next_mz->mem->css);
3652 return nr_reclaimed;
3653}
3654
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003655/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003656 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003657 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
3658 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003659static int mem_cgroup_force_empty_list(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003660 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003661{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003662 struct zone *zone;
3663 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003664 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003665 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003666 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003667 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003668
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003669 zone = &NODE_DATA(node)->node_zones[zid];
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003670 mz = mem_cgroup_zoneinfo(memcg, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07003671 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003672
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003673 loop = MEM_CGROUP_ZSTAT(mz, lru);
3674 /* give some margin against EBUSY etc...*/
3675 loop += 256;
3676 busy = NULL;
3677 while (loop--) {
Johannes Weiner5564e882011-03-23 16:42:29 -07003678 struct page *page;
3679
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003680 ret = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003681 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003682 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003683 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003684 break;
3685 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003686 pc = list_entry(list->prev, struct page_cgroup, lru);
3687 if (busy == pc) {
3688 list_move(&pc->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08003689 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003690 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003691 continue;
3692 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003693 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003694
Johannes Weiner6b3ae582011-03-23 16:42:30 -07003695 page = lookup_cgroup_page(pc);
Johannes Weiner5564e882011-03-23 16:42:29 -07003696
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003697 ret = mem_cgroup_move_parent(page, pc, memcg, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003698 if (ret == -ENOMEM)
3699 break;
3700
3701 if (ret == -EBUSY || ret == -EINVAL) {
3702 /* found lock contention or "pc" is obsolete. */
3703 busy = pc;
3704 cond_resched();
3705 } else
3706 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003707 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003708
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003709 if (!ret && !list_empty(list))
3710 return -EBUSY;
3711 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003712}
3713
3714/*
3715 * make mem_cgroup's charge to be 0 if there is no task.
3716 * This enables deleting this mem_cgroup.
3717 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003718static int mem_cgroup_force_empty(struct mem_cgroup *memcg, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003719{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003720 int ret;
3721 int node, zid, shrink;
3722 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003723 struct cgroup *cgrp = memcg->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003724
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003725 css_get(&memcg->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003726
3727 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003728 /* should free all ? */
3729 if (free_all)
3730 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003731move_account:
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003732 do {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003733 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003734 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003735 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003736 ret = -EINTR;
3737 if (signal_pending(current))
3738 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003739 /* This is for making all *used* pages to be on LRU. */
3740 lru_add_drain_all();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003741 drain_all_stock_sync(memcg);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003742 ret = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003743 mem_cgroup_start_move(memcg);
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08003744 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003745 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07003746 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003747 for_each_lru(l) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003748 ret = mem_cgroup_force_empty_list(memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003749 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003750 if (ret)
3751 break;
3752 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003753 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003754 if (ret)
3755 break;
3756 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003757 mem_cgroup_end_move(memcg);
3758 memcg_oom_recover(memcg);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003759 /* it seems parent cgroup doesn't have enough mem */
3760 if (ret == -ENOMEM)
3761 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003762 cond_resched();
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003763 /* "ret" should also be checked to ensure all lists are empty. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003764 } while (memcg->res.usage > 0 || ret);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003765out:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003766 css_put(&memcg->css);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003767 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003768
3769try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003770 /* returns EBUSY if there is a task or if we come here twice. */
3771 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003772 ret = -EBUSY;
3773 goto out;
3774 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003775 /* we call try-to-free pages for make this cgroup empty */
3776 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003777 /* try to free all pages in this cgroup */
3778 shrink = 1;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003779 while (nr_retries && memcg->res.usage > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003780 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003781
3782 if (signal_pending(current)) {
3783 ret = -EINTR;
3784 goto out;
3785 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003786 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
Johannes Weiner185efc02011-09-14 16:21:58 -07003787 false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003788 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003789 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003790 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02003791 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003792 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003793
3794 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003795 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003796 /* try move_account...there may be some *locked* pages. */
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003797 goto move_account;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003798}
3799
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003800int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
3801{
3802 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
3803}
3804
3805
Balbir Singh18f59ea2009-01-07 18:08:07 -08003806static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
3807{
3808 return mem_cgroup_from_cont(cont)->use_hierarchy;
3809}
3810
3811static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
3812 u64 val)
3813{
3814 int retval = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003815 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003816 struct cgroup *parent = cont->parent;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003817 struct mem_cgroup *parent_memcg = NULL;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003818
3819 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003820 parent_memcg = mem_cgroup_from_cont(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003821
3822 cgroup_lock();
3823 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003824 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08003825 * in the child subtrees. If it is unset, then the change can
3826 * occur, provided the current cgroup has no children.
3827 *
3828 * For the root cgroup, parent_mem is NULL, we allow value to be
3829 * set if there are no children.
3830 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003831 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08003832 (val == 1 || val == 0)) {
3833 if (list_empty(&cont->children))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003834 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003835 else
3836 retval = -EBUSY;
3837 } else
3838 retval = -EINVAL;
3839 cgroup_unlock();
3840
3841 return retval;
3842}
3843
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003844
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003845static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003846 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003847{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003848 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003849 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003850
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003851 /* Per-cpu values can be negative, use a signed accumulator */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003852 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003853 val += mem_cgroup_read_stat(iter, idx);
3854
3855 if (val < 0) /* race ? */
3856 val = 0;
3857 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003858}
3859
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003860static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003861{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003862 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003863
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003864 if (!mem_cgroup_is_root(memcg)) {
Glauber Costae5671df2011-12-11 21:47:01 +00003865 val = 0;
3866#ifdef CONFIG_CGROUP_MEM_RES_CTLR_KMEM
3867 if (!memcg->kmem_independent_accounting)
3868 val = res_counter_read_u64(&memcg->kmem, RES_USAGE);
3869#endif
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003870 if (!swap)
Glauber Costae5671df2011-12-11 21:47:01 +00003871 val += res_counter_read_u64(&memcg->res, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003872 else
Glauber Costae5671df2011-12-11 21:47:01 +00003873 val += res_counter_read_u64(&memcg->memsw, RES_USAGE);
3874
3875 return val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003876 }
3877
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003878 val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
3879 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003880
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003881 if (swap)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003882 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAPOUT);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003883
3884 return val << PAGE_SHIFT;
3885}
3886
Paul Menage2c3daa72008-04-29 00:59:58 -07003887static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003888{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003889 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003890 u64 val;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003891 int type, name;
3892
3893 type = MEMFILE_TYPE(cft->private);
3894 name = MEMFILE_ATTR(cft->private);
3895 switch (type) {
3896 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003897 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003898 val = mem_cgroup_usage(memcg, false);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003899 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003900 val = res_counter_read_u64(&memcg->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003901 break;
3902 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003903 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003904 val = mem_cgroup_usage(memcg, true);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003905 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003906 val = res_counter_read_u64(&memcg->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003907 break;
Glauber Costae5671df2011-12-11 21:47:01 +00003908#ifdef CONFIG_CGROUP_MEM_RES_CTLR_KMEM
3909 case _KMEM:
3910 val = res_counter_read_u64(&memcg->kmem, name);
3911 break;
3912#endif
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003913 default:
3914 BUG();
3915 break;
3916 }
3917 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003918}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003919/*
3920 * The user of this function is...
3921 * RES_LIMIT.
3922 */
Paul Menage856c13a2008-07-25 01:47:04 -07003923static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
3924 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003925{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003926 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003927 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003928 unsigned long long val;
3929 int ret;
3930
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003931 type = MEMFILE_TYPE(cft->private);
3932 name = MEMFILE_ATTR(cft->private);
3933 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003934 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07003935 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
3936 ret = -EINVAL;
3937 break;
3938 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003939 /* This function does all necessary parse...reuse it */
3940 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003941 if (ret)
3942 break;
3943 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003944 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003945 else
3946 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003947 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07003948 case RES_SOFT_LIMIT:
3949 ret = res_counter_memparse_write_strategy(buffer, &val);
3950 if (ret)
3951 break;
3952 /*
3953 * For memsw, soft limits are hard to implement in terms
3954 * of semantics, for now, we support soft limits for
3955 * control without swap
3956 */
3957 if (type == _MEM)
3958 ret = res_counter_set_soft_limit(&memcg->res, val);
3959 else
3960 ret = -EINVAL;
3961 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003962 default:
3963 ret = -EINVAL; /* should be BUG() ? */
3964 break;
3965 }
3966 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003967}
3968
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08003969static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
3970 unsigned long long *mem_limit, unsigned long long *memsw_limit)
3971{
3972 struct cgroup *cgroup;
3973 unsigned long long min_limit, min_memsw_limit, tmp;
3974
3975 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3976 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3977 cgroup = memcg->css.cgroup;
3978 if (!memcg->use_hierarchy)
3979 goto out;
3980
3981 while (cgroup->parent) {
3982 cgroup = cgroup->parent;
3983 memcg = mem_cgroup_from_cont(cgroup);
3984 if (!memcg->use_hierarchy)
3985 break;
3986 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
3987 min_limit = min(min_limit, tmp);
3988 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3989 min_memsw_limit = min(min_memsw_limit, tmp);
3990 }
3991out:
3992 *mem_limit = min_limit;
3993 *memsw_limit = min_memsw_limit;
3994 return;
3995}
3996
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003997static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003998{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003999 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004000 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004001
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004002 memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004003 type = MEMFILE_TYPE(event);
4004 name = MEMFILE_ATTR(event);
4005 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004006 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004007 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004008 res_counter_reset_max(&memcg->res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004009 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004010 res_counter_reset_max(&memcg->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004011 break;
4012 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004013 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004014 res_counter_reset_failcnt(&memcg->res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004015 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004016 res_counter_reset_failcnt(&memcg->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004017 break;
4018 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07004019
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07004020 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004021}
4022
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004023static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
4024 struct cftype *cft)
4025{
4026 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
4027}
4028
Daisuke Nishimura02491442010-03-10 15:22:17 -08004029#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004030static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
4031 struct cftype *cft, u64 val)
4032{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004033 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004034
4035 if (val >= (1 << NR_MOVE_TYPE))
4036 return -EINVAL;
4037 /*
4038 * We check this value several times in both in can_attach() and
4039 * attach(), so we need cgroup lock to prevent this value from being
4040 * inconsistent.
4041 */
4042 cgroup_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004043 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004044 cgroup_unlock();
4045
4046 return 0;
4047}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004048#else
4049static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
4050 struct cftype *cft, u64 val)
4051{
4052 return -ENOSYS;
4053}
4054#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004055
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004056
4057/* For read statistics */
4058enum {
4059 MCS_CACHE,
4060 MCS_RSS,
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08004061 MCS_FILE_MAPPED,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004062 MCS_PGPGIN,
4063 MCS_PGPGOUT,
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004064 MCS_SWAP,
Ying Han456f9982011-05-26 16:25:38 -07004065 MCS_PGFAULT,
4066 MCS_PGMAJFAULT,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004067 MCS_INACTIVE_ANON,
4068 MCS_ACTIVE_ANON,
4069 MCS_INACTIVE_FILE,
4070 MCS_ACTIVE_FILE,
4071 MCS_UNEVICTABLE,
4072 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004073};
4074
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004075struct mcs_total_stat {
4076 s64 stat[NR_MCS_STAT];
4077};
4078
4079struct {
4080 char *local_name;
4081 char *total_name;
4082} memcg_stat_strings[NR_MCS_STAT] = {
4083 {"cache", "total_cache"},
4084 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07004085 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004086 {"pgpgin", "total_pgpgin"},
4087 {"pgpgout", "total_pgpgout"},
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004088 {"swap", "total_swap"},
Ying Han456f9982011-05-26 16:25:38 -07004089 {"pgfault", "total_pgfault"},
4090 {"pgmajfault", "total_pgmajfault"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004091 {"inactive_anon", "total_inactive_anon"},
4092 {"active_anon", "total_active_anon"},
4093 {"inactive_file", "total_inactive_file"},
4094 {"active_file", "total_active_file"},
4095 {"unevictable", "total_unevictable"}
4096};
4097
4098
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004099static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004100mem_cgroup_get_local_stat(struct mem_cgroup *memcg, struct mcs_total_stat *s)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004101{
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004102 s64 val;
4103
4104 /* per cpu stat */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004105 val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_CACHE);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004106 s->stat[MCS_CACHE] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004107 val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_RSS);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004108 s->stat[MCS_RSS] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004109 val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_FILE_MAPPED);
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08004110 s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004111 val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGPGIN);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004112 s->stat[MCS_PGPGIN] += val;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004113 val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGPGOUT);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004114 s->stat[MCS_PGPGOUT] += val;
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004115 if (do_swap_account) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004116 val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_SWAPOUT);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004117 s->stat[MCS_SWAP] += val * PAGE_SIZE;
4118 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004119 val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGFAULT);
Ying Han456f9982011-05-26 16:25:38 -07004120 s->stat[MCS_PGFAULT] += val;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004121 val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGMAJFAULT);
Ying Han456f9982011-05-26 16:25:38 -07004122 s->stat[MCS_PGMAJFAULT] += val;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004123
4124 /* per zone stat */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004125 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_ANON));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004126 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004127 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_ANON));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004128 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004129 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_FILE));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004130 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004131 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_FILE));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004132 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004133 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004134 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004135}
4136
4137static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004138mem_cgroup_get_total_stat(struct mem_cgroup *memcg, struct mcs_total_stat *s)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004139{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004140 struct mem_cgroup *iter;
4141
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004142 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004143 mem_cgroup_get_local_stat(iter, s);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004144}
4145
Ying Han406eb0c2011-05-26 16:25:37 -07004146#ifdef CONFIG_NUMA
4147static int mem_control_numa_stat_show(struct seq_file *m, void *arg)
4148{
4149 int nid;
4150 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
4151 unsigned long node_nr;
4152 struct cgroup *cont = m->private;
4153 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
4154
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004155 total_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07004156 seq_printf(m, "total=%lu", total_nr);
4157 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004158 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07004159 seq_printf(m, " N%d=%lu", nid, node_nr);
4160 }
4161 seq_putc(m, '\n');
4162
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004163 file_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07004164 seq_printf(m, "file=%lu", file_nr);
4165 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004166 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
4167 LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07004168 seq_printf(m, " N%d=%lu", nid, node_nr);
4169 }
4170 seq_putc(m, '\n');
4171
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004172 anon_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07004173 seq_printf(m, "anon=%lu", anon_nr);
4174 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004175 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
4176 LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07004177 seq_printf(m, " N%d=%lu", nid, node_nr);
4178 }
4179 seq_putc(m, '\n');
4180
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004181 unevictable_nr = mem_cgroup_nr_lru_pages(mem_cont, BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07004182 seq_printf(m, "unevictable=%lu", unevictable_nr);
4183 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004184 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
4185 BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07004186 seq_printf(m, " N%d=%lu", nid, node_nr);
4187 }
4188 seq_putc(m, '\n');
4189 return 0;
4190}
4191#endif /* CONFIG_NUMA */
4192
Paul Menagec64745c2008-04-29 01:00:02 -07004193static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
4194 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004195{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004196 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004197 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004198 int i;
4199
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004200 memset(&mystat, 0, sizeof(mystat));
4201 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004202
Ying Han406eb0c2011-05-26 16:25:37 -07004203
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004204 for (i = 0; i < NR_MCS_STAT; i++) {
4205 if (i == MCS_SWAP && !do_swap_account)
4206 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004207 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004208 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004209
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004210 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004211 {
4212 unsigned long long limit, memsw_limit;
4213 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
4214 cb->fill(cb, "hierarchical_memory_limit", limit);
4215 if (do_swap_account)
4216 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
4217 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004218
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004219 memset(&mystat, 0, sizeof(mystat));
4220 mem_cgroup_get_total_stat(mem_cont, &mystat);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004221 for (i = 0; i < NR_MCS_STAT; i++) {
4222 if (i == MCS_SWAP && !do_swap_account)
4223 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004224 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004225 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004226
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004227#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004228 {
4229 int nid, zid;
4230 struct mem_cgroup_per_zone *mz;
4231 unsigned long recent_rotated[2] = {0, 0};
4232 unsigned long recent_scanned[2] = {0, 0};
4233
4234 for_each_online_node(nid)
4235 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
4236 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
4237
4238 recent_rotated[0] +=
4239 mz->reclaim_stat.recent_rotated[0];
4240 recent_rotated[1] +=
4241 mz->reclaim_stat.recent_rotated[1];
4242 recent_scanned[0] +=
4243 mz->reclaim_stat.recent_scanned[0];
4244 recent_scanned[1] +=
4245 mz->reclaim_stat.recent_scanned[1];
4246 }
4247 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
4248 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
4249 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
4250 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
4251 }
4252#endif
4253
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004254 return 0;
4255}
4256
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004257static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
4258{
4259 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4260
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07004261 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004262}
4263
4264static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
4265 u64 val)
4266{
4267 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4268 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08004269
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004270 if (val > 100)
4271 return -EINVAL;
4272
4273 if (cgrp->parent == NULL)
4274 return -EINVAL;
4275
4276 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08004277
4278 cgroup_lock();
4279
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004280 /* If under hierarchy, only empty-root can set this value */
4281 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08004282 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
4283 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004284 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08004285 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004286
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004287 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004288
Li Zefan068b38c2009-01-15 13:51:26 -08004289 cgroup_unlock();
4290
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004291 return 0;
4292}
4293
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004294static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
4295{
4296 struct mem_cgroup_threshold_ary *t;
4297 u64 usage;
4298 int i;
4299
4300 rcu_read_lock();
4301 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004302 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004303 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004304 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004305
4306 if (!t)
4307 goto unlock;
4308
4309 usage = mem_cgroup_usage(memcg, swap);
4310
4311 /*
4312 * current_threshold points to threshold just below usage.
4313 * If it's not true, a threshold was crossed after last
4314 * call of __mem_cgroup_threshold().
4315 */
Phil Carmody5407a562010-05-26 14:42:42 -07004316 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004317
4318 /*
4319 * Iterate backward over array of thresholds starting from
4320 * current_threshold and check if a threshold is crossed.
4321 * If none of thresholds below usage is crossed, we read
4322 * only one element of the array here.
4323 */
4324 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
4325 eventfd_signal(t->entries[i].eventfd, 1);
4326
4327 /* i = current_threshold + 1 */
4328 i++;
4329
4330 /*
4331 * Iterate forward over array of thresholds starting from
4332 * current_threshold+1 and check if a threshold is crossed.
4333 * If none of thresholds above usage is crossed, we read
4334 * only one element of the array here.
4335 */
4336 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
4337 eventfd_signal(t->entries[i].eventfd, 1);
4338
4339 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07004340 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004341unlock:
4342 rcu_read_unlock();
4343}
4344
4345static void mem_cgroup_threshold(struct mem_cgroup *memcg)
4346{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07004347 while (memcg) {
4348 __mem_cgroup_threshold(memcg, false);
4349 if (do_swap_account)
4350 __mem_cgroup_threshold(memcg, true);
4351
4352 memcg = parent_mem_cgroup(memcg);
4353 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004354}
4355
4356static int compare_thresholds(const void *a, const void *b)
4357{
4358 const struct mem_cgroup_threshold *_a = a;
4359 const struct mem_cgroup_threshold *_b = b;
4360
4361 return _a->threshold - _b->threshold;
4362}
4363
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004364static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004365{
4366 struct mem_cgroup_eventfd_list *ev;
4367
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004368 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004369 eventfd_signal(ev->eventfd, 1);
4370 return 0;
4371}
4372
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004373static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004374{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004375 struct mem_cgroup *iter;
4376
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004377 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004378 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004379}
4380
4381static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
4382 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004383{
4384 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004385 struct mem_cgroup_thresholds *thresholds;
4386 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004387 int type = MEMFILE_TYPE(cft->private);
4388 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004389 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004390
4391 ret = res_counter_memparse_write_strategy(args, &threshold);
4392 if (ret)
4393 return ret;
4394
4395 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004396
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004397 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004398 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004399 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004400 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004401 else
4402 BUG();
4403
4404 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4405
4406 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004407 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004408 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4409
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004410 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004411
4412 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004413 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004414 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004415 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004416 ret = -ENOMEM;
4417 goto unlock;
4418 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004419 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004420
4421 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004422 if (thresholds->primary) {
4423 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004424 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004425 }
4426
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004427 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004428 new->entries[size - 1].eventfd = eventfd;
4429 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004430
4431 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004432 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004433 compare_thresholds, NULL);
4434
4435 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004436 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004437 for (i = 0; i < size; i++) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004438 if (new->entries[i].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004439 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004440 * new->current_threshold will not be used until
4441 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004442 * it here.
4443 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004444 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004445 }
4446 }
4447
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004448 /* Free old spare buffer and save old primary buffer as spare */
4449 kfree(thresholds->spare);
4450 thresholds->spare = thresholds->primary;
4451
4452 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004453
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004454 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004455 synchronize_rcu();
4456
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004457unlock:
4458 mutex_unlock(&memcg->thresholds_lock);
4459
4460 return ret;
4461}
4462
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004463static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004464 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004465{
4466 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004467 struct mem_cgroup_thresholds *thresholds;
4468 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004469 int type = MEMFILE_TYPE(cft->private);
4470 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004471 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004472
4473 mutex_lock(&memcg->thresholds_lock);
4474 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004475 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004476 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004477 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004478 else
4479 BUG();
4480
4481 /*
4482 * Something went wrong if we trying to unregister a threshold
4483 * if we don't have thresholds
4484 */
4485 BUG_ON(!thresholds);
4486
4487 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4488
4489 /* Check if a threshold crossed before removing */
4490 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4491
4492 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004493 size = 0;
4494 for (i = 0; i < thresholds->primary->size; i++) {
4495 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004496 size++;
4497 }
4498
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004499 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004500
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004501 /* Set thresholds array to NULL if we don't have thresholds */
4502 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004503 kfree(new);
4504 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004505 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004506 }
4507
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004508 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004509
4510 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004511 new->current_threshold = -1;
4512 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
4513 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004514 continue;
4515
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004516 new->entries[j] = thresholds->primary->entries[i];
4517 if (new->entries[j].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004518 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004519 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004520 * until rcu_assign_pointer(), so it's safe to increment
4521 * it here.
4522 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004523 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004524 }
4525 j++;
4526 }
4527
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004528swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004529 /* Swap primary and spare array */
4530 thresholds->spare = thresholds->primary;
4531 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004532
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004533 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004534 synchronize_rcu();
4535
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004536 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004537}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004538
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004539static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
4540 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
4541{
4542 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4543 struct mem_cgroup_eventfd_list *event;
4544 int type = MEMFILE_TYPE(cft->private);
4545
4546 BUG_ON(type != _OOM_TYPE);
4547 event = kmalloc(sizeof(*event), GFP_KERNEL);
4548 if (!event)
4549 return -ENOMEM;
4550
Michal Hocko1af8efe2011-07-26 16:08:24 -07004551 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004552
4553 event->eventfd = eventfd;
4554 list_add(&event->list, &memcg->oom_notify);
4555
4556 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07004557 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004558 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07004559 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004560
4561 return 0;
4562}
4563
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004564static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004565 struct cftype *cft, struct eventfd_ctx *eventfd)
4566{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004567 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004568 struct mem_cgroup_eventfd_list *ev, *tmp;
4569 int type = MEMFILE_TYPE(cft->private);
4570
4571 BUG_ON(type != _OOM_TYPE);
4572
Michal Hocko1af8efe2011-07-26 16:08:24 -07004573 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004574
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004575 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004576 if (ev->eventfd == eventfd) {
4577 list_del(&ev->list);
4578 kfree(ev);
4579 }
4580 }
4581
Michal Hocko1af8efe2011-07-26 16:08:24 -07004582 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004583}
4584
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004585static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
4586 struct cftype *cft, struct cgroup_map_cb *cb)
4587{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004588 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004589
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004590 cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004591
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004592 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004593 cb->fill(cb, "under_oom", 1);
4594 else
4595 cb->fill(cb, "under_oom", 0);
4596 return 0;
4597}
4598
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004599static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
4600 struct cftype *cft, u64 val)
4601{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004602 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004603 struct mem_cgroup *parent;
4604
4605 /* cannot set to root cgroup and only 0 and 1 are allowed */
4606 if (!cgrp->parent || !((val == 0) || (val == 1)))
4607 return -EINVAL;
4608
4609 parent = mem_cgroup_from_cont(cgrp->parent);
4610
4611 cgroup_lock();
4612 /* oom-kill-disable is a flag for subhierarchy. */
4613 if ((parent->use_hierarchy) ||
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004614 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004615 cgroup_unlock();
4616 return -EINVAL;
4617 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004618 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07004619 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004620 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004621 cgroup_unlock();
4622 return 0;
4623}
4624
Ying Han406eb0c2011-05-26 16:25:37 -07004625#ifdef CONFIG_NUMA
4626static const struct file_operations mem_control_numa_stat_file_operations = {
4627 .read = seq_read,
4628 .llseek = seq_lseek,
4629 .release = single_release,
4630};
4631
4632static int mem_control_numa_stat_open(struct inode *unused, struct file *file)
4633{
4634 struct cgroup *cont = file->f_dentry->d_parent->d_fsdata;
4635
4636 file->f_op = &mem_control_numa_stat_file_operations;
4637 return single_open(file, mem_control_numa_stat_show, cont);
4638}
4639#endif /* CONFIG_NUMA */
4640
Glauber Costae5671df2011-12-11 21:47:01 +00004641#ifdef CONFIG_CGROUP_MEM_RES_CTLR_KMEM
4642static u64 kmem_limit_independent_read(struct cgroup *cgroup, struct cftype *cft)
4643{
4644 return mem_cgroup_from_cont(cgroup)->kmem_independent_accounting;
4645}
4646
4647static int kmem_limit_independent_write(struct cgroup *cgroup, struct cftype *cft,
4648 u64 val)
4649{
4650 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup);
4651 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
4652
4653 val = !!val;
4654
4655 /*
4656 * This follows the same hierarchy restrictions than
4657 * mem_cgroup_hierarchy_write()
4658 */
4659 if (!parent || !parent->use_hierarchy) {
4660 if (list_empty(&cgroup->children))
4661 memcg->kmem_independent_accounting = val;
4662 else
4663 return -EBUSY;
4664 }
4665 else
4666 return -EINVAL;
4667
4668 return 0;
4669}
4670static struct cftype kmem_cgroup_files[] = {
4671 {
4672 .name = "independent_kmem_limit",
4673 .read_u64 = kmem_limit_independent_read,
4674 .write_u64 = kmem_limit_independent_write,
4675 },
4676 {
4677 .name = "kmem.usage_in_bytes",
4678 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
4679 .read_u64 = mem_cgroup_read,
4680 },
4681 {
4682 .name = "kmem.limit_in_bytes",
4683 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
4684 .read_u64 = mem_cgroup_read,
4685 },
4686};
4687
4688static int register_kmem_files(struct cgroup *cont, struct cgroup_subsys *ss)
4689{
4690 int ret = 0;
4691
4692 ret = cgroup_add_files(cont, ss, kmem_cgroup_files,
4693 ARRAY_SIZE(kmem_cgroup_files));
4694 return ret;
4695};
4696
4697#else
4698static int register_kmem_files(struct cgroup *cont, struct cgroup_subsys *ss)
4699{
4700 return 0;
4701}
4702#endif
4703
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004704static struct cftype mem_cgroup_files[] = {
4705 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004706 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004707 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07004708 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004709 .register_event = mem_cgroup_usage_register_event,
4710 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004711 },
4712 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004713 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004714 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004715 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004716 .read_u64 = mem_cgroup_read,
4717 },
4718 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004719 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004720 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07004721 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07004722 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004723 },
4724 {
Balbir Singh296c81d2009-09-23 15:56:36 -07004725 .name = "soft_limit_in_bytes",
4726 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
4727 .write_string = mem_cgroup_write,
4728 .read_u64 = mem_cgroup_read,
4729 },
4730 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004731 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004732 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004733 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07004734 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004735 },
Balbir Singh8697d332008-02-07 00:13:59 -08004736 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004737 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07004738 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004739 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004740 {
4741 .name = "force_empty",
4742 .trigger = mem_cgroup_force_empty_write,
4743 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08004744 {
4745 .name = "use_hierarchy",
4746 .write_u64 = mem_cgroup_hierarchy_write,
4747 .read_u64 = mem_cgroup_hierarchy_read,
4748 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004749 {
4750 .name = "swappiness",
4751 .read_u64 = mem_cgroup_swappiness_read,
4752 .write_u64 = mem_cgroup_swappiness_write,
4753 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004754 {
4755 .name = "move_charge_at_immigrate",
4756 .read_u64 = mem_cgroup_move_charge_read,
4757 .write_u64 = mem_cgroup_move_charge_write,
4758 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004759 {
4760 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004761 .read_map = mem_cgroup_oom_control_read,
4762 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004763 .register_event = mem_cgroup_oom_register_event,
4764 .unregister_event = mem_cgroup_oom_unregister_event,
4765 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
4766 },
Ying Han406eb0c2011-05-26 16:25:37 -07004767#ifdef CONFIG_NUMA
4768 {
4769 .name = "numa_stat",
4770 .open = mem_control_numa_stat_open,
KAMEZAWA Hiroyuki89577122011-06-15 15:08:41 -07004771 .mode = S_IRUGO,
Ying Han406eb0c2011-05-26 16:25:37 -07004772 },
4773#endif
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004774};
4775
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004776#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4777static struct cftype memsw_cgroup_files[] = {
4778 {
4779 .name = "memsw.usage_in_bytes",
4780 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
4781 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004782 .register_event = mem_cgroup_usage_register_event,
4783 .unregister_event = mem_cgroup_usage_unregister_event,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004784 },
4785 {
4786 .name = "memsw.max_usage_in_bytes",
4787 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
4788 .trigger = mem_cgroup_reset,
4789 .read_u64 = mem_cgroup_read,
4790 },
4791 {
4792 .name = "memsw.limit_in_bytes",
4793 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
4794 .write_string = mem_cgroup_write,
4795 .read_u64 = mem_cgroup_read,
4796 },
4797 {
4798 .name = "memsw.failcnt",
4799 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
4800 .trigger = mem_cgroup_reset,
4801 .read_u64 = mem_cgroup_read,
4802 },
4803};
4804
4805static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
4806{
4807 if (!do_swap_account)
4808 return 0;
4809 return cgroup_add_files(cont, ss, memsw_cgroup_files,
4810 ARRAY_SIZE(memsw_cgroup_files));
4811};
4812#else
4813static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
4814{
4815 return 0;
4816}
4817#endif
4818
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004819static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004820{
4821 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004822 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07004823 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004824 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004825 /*
4826 * This routine is called against possible nodes.
4827 * But it's BUG to call kmalloc() against offline node.
4828 *
4829 * TODO: this routine can waste much memory for nodes which will
4830 * never be onlined. It's better to use memory hotplug callback
4831 * function.
4832 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004833 if (!node_state(node, N_NORMAL_MEMORY))
4834 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08004835 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004836 if (!pn)
4837 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004838
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004839 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4840 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07004841 for_each_lru(l)
4842 INIT_LIST_HEAD(&mz->lists[l]);
Balbir Singhf64c3f52009-09-23 15:56:37 -07004843 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07004844 mz->on_tree = false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004845 mz->mem = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004846 }
Igor Mammedov0a619e52011-11-02 13:38:21 -07004847 memcg->info.nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004848 return 0;
4849}
4850
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004851static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004852{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004853 kfree(memcg->info.nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004854}
4855
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004856static struct mem_cgroup *mem_cgroup_alloc(void)
4857{
4858 struct mem_cgroup *mem;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004859 int size = sizeof(struct mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004860
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004861 /* Can be very big if MAX_NUMNODES is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08004862 if (size < PAGE_SIZE)
Jesper Juhl17295c82011-01-13 15:47:42 -08004863 mem = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004864 else
Jesper Juhl17295c82011-01-13 15:47:42 -08004865 mem = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004866
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07004867 if (!mem)
4868 return NULL;
4869
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004870 mem->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004871 if (!mem->stat)
4872 goto out_free;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07004873 spin_lock_init(&mem->pcp_counter_lock);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004874 return mem;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004875
4876out_free:
4877 if (size < PAGE_SIZE)
4878 kfree(mem);
4879 else
4880 vfree(mem);
4881 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004882}
4883
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004884/*
4885 * At destroying mem_cgroup, references from swap_cgroup can remain.
4886 * (scanning all at force_empty is too costly...)
4887 *
4888 * Instead of clearing all references at force_empty, we remember
4889 * the number of reference from swap_cgroup and free mem_cgroup when
4890 * it goes down to 0.
4891 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004892 * Removal of cgroup itself succeeds regardless of refs from swap.
4893 */
4894
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004895static void __mem_cgroup_free(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004896{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004897 int node;
4898
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004899 mem_cgroup_remove_from_trees(memcg);
4900 free_css_id(&mem_cgroup_subsys, &memcg->css);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004901
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004902 for_each_node_state(node, N_POSSIBLE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004903 free_mem_cgroup_per_zone_info(memcg, node);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004904
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004905 free_percpu(memcg->stat);
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004906 if (sizeof(struct mem_cgroup) < PAGE_SIZE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004907 kfree(memcg);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004908 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004909 vfree(memcg);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004910}
4911
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004912static void mem_cgroup_get(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004913{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004914 atomic_inc(&memcg->refcnt);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004915}
4916
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004917static void __mem_cgroup_put(struct mem_cgroup *memcg, int count)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004918{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004919 if (atomic_sub_and_test(count, &memcg->refcnt)) {
4920 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
4921 __mem_cgroup_free(memcg);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004922 if (parent)
4923 mem_cgroup_put(parent);
4924 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004925}
4926
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004927static void mem_cgroup_put(struct mem_cgroup *memcg)
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004928{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004929 __mem_cgroup_put(memcg, 1);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004930}
4931
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004932/*
4933 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
4934 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004935static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004936{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004937 if (!memcg->res.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004938 return NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004939 return mem_cgroup_from_res_counter(memcg->res.parent, res);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004940}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004941
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004942#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4943static void __init enable_swap_cgroup(void)
4944{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004945 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004946 do_swap_account = 1;
4947}
4948#else
4949static void __init enable_swap_cgroup(void)
4950{
4951}
4952#endif
4953
Balbir Singhf64c3f52009-09-23 15:56:37 -07004954static int mem_cgroup_soft_limit_tree_init(void)
4955{
4956 struct mem_cgroup_tree_per_node *rtpn;
4957 struct mem_cgroup_tree_per_zone *rtpz;
4958 int tmp, node, zone;
4959
4960 for_each_node_state(node, N_POSSIBLE) {
4961 tmp = node;
4962 if (!node_state(node, N_NORMAL_MEMORY))
4963 tmp = -1;
4964 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
4965 if (!rtpn)
4966 return 1;
4967
4968 soft_limit_tree.rb_tree_per_node[node] = rtpn;
4969
4970 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4971 rtpz = &rtpn->rb_tree_per_zone[zone];
4972 rtpz->rb_root = RB_ROOT;
4973 spin_lock_init(&rtpz->lock);
4974 }
4975 }
4976 return 0;
4977}
4978
Li Zefan0eb253e2009-01-15 13:51:25 -08004979static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004980mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
4981{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004982 struct mem_cgroup *memcg, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004983 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004984 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004985
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004986 memcg = mem_cgroup_alloc();
4987 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004988 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004989
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004990 for_each_node_state(node, N_POSSIBLE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004991 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004992 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004993
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004994 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004995 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004996 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004997 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004998 parent = NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004999 root_mem_cgroup = memcg;
Balbir Singhf64c3f52009-09-23 15:56:37 -07005000 if (mem_cgroup_soft_limit_tree_init())
5001 goto free_out;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08005002 for_each_possible_cpu(cpu) {
5003 struct memcg_stock_pcp *stock =
5004 &per_cpu(memcg_stock, cpu);
5005 INIT_WORK(&stock->work, drain_local_stock);
5006 }
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07005007 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005008 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005009 parent = mem_cgroup_from_cont(cont->parent);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005010 memcg->use_hierarchy = parent->use_hierarchy;
5011 memcg->oom_kill_disable = parent->oom_kill_disable;
Balbir Singh18f59ea2009-01-07 18:08:07 -08005012 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005013
Balbir Singh18f59ea2009-01-07 18:08:07 -08005014 if (parent && parent->use_hierarchy) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005015 res_counter_init(&memcg->res, &parent->res);
5016 res_counter_init(&memcg->memsw, &parent->memsw);
Glauber Costae5671df2011-12-11 21:47:01 +00005017 res_counter_init(&memcg->kmem, &parent->kmem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005018 /*
5019 * We increment refcnt of the parent to ensure that we can
5020 * safely access it on res_counter_charge/uncharge.
5021 * This refcnt will be decremented when freeing this
5022 * mem_cgroup(see mem_cgroup_put).
5023 */
5024 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005025 } else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005026 res_counter_init(&memcg->res, NULL);
5027 res_counter_init(&memcg->memsw, NULL);
Glauber Costae5671df2011-12-11 21:47:01 +00005028 res_counter_init(&memcg->kmem, NULL);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005029 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005030 memcg->last_scanned_child = 0;
5031 memcg->last_scanned_node = MAX_NUMNODES;
5032 INIT_LIST_HEAD(&memcg->oom_notify);
Balbir Singh6d61ef42009-01-07 18:08:06 -08005033
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005034 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005035 memcg->swappiness = mem_cgroup_swappiness(parent);
5036 atomic_set(&memcg->refcnt, 1);
5037 memcg->move_charge_at_immigrate = 0;
5038 mutex_init(&memcg->thresholds_lock);
5039 return &memcg->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005040free_out:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005041 __mem_cgroup_free(memcg);
Balbir Singh4b3bde42009-09-23 15:56:32 -07005042 root_mem_cgroup = NULL;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005043 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005044}
5045
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07005046static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005047 struct cgroup *cont)
5048{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005049 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07005050
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005051 return mem_cgroup_force_empty(memcg, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005052}
5053
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005054static void mem_cgroup_destroy(struct cgroup_subsys *ss,
5055 struct cgroup *cont)
5056{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005057 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08005058
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005059 mem_cgroup_put(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005060}
5061
5062static int mem_cgroup_populate(struct cgroup_subsys *ss,
5063 struct cgroup *cont)
5064{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005065 int ret;
5066
5067 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
5068 ARRAY_SIZE(mem_cgroup_files));
5069
5070 if (!ret)
5071 ret = register_memsw_files(cont, ss);
Glauber Costae5671df2011-12-11 21:47:01 +00005072
5073 if (!ret)
5074 ret = register_kmem_files(cont, ss);
5075
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005076 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005077}
5078
Daisuke Nishimura02491442010-03-10 15:22:17 -08005079#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005080/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005081#define PRECHARGE_COUNT_AT_ONCE 256
5082static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005083{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005084 int ret = 0;
5085 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005086 struct mem_cgroup *memcg = mc.to;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005087
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005088 if (mem_cgroup_is_root(memcg)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005089 mc.precharge += count;
5090 /* we don't need css_get for root */
5091 return ret;
5092 }
5093 /* try to charge at once */
5094 if (count > 1) {
5095 struct res_counter *dummy;
5096 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005097 * "memcg" cannot be under rmdir() because we've already checked
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005098 * by cgroup_lock_live_cgroup() that it is not removed and we
5099 * are still under the same cgroup_mutex. So we can postpone
5100 * css_get().
5101 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005102 if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy))
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005103 goto one_by_one;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005104 if (do_swap_account && res_counter_charge(&memcg->memsw,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005105 PAGE_SIZE * count, &dummy)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005106 res_counter_uncharge(&memcg->res, PAGE_SIZE * count);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005107 goto one_by_one;
5108 }
5109 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005110 return ret;
5111 }
5112one_by_one:
5113 /* fall back to one by one charge */
5114 while (count--) {
5115 if (signal_pending(current)) {
5116 ret = -EINTR;
5117 break;
5118 }
5119 if (!batch_count--) {
5120 batch_count = PRECHARGE_COUNT_AT_ONCE;
5121 cond_resched();
5122 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005123 ret = __mem_cgroup_try_charge(NULL,
5124 GFP_KERNEL, 1, &memcg, false);
5125 if (ret || !memcg)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005126 /* mem_cgroup_clear_mc() will do uncharge later */
5127 return -ENOMEM;
5128 mc.precharge++;
5129 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005130 return ret;
5131}
5132
5133/**
5134 * is_target_pte_for_mc - check a pte whether it is valid for move charge
5135 * @vma: the vma the pte to be checked belongs
5136 * @addr: the address corresponding to the pte to be checked
5137 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08005138 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005139 *
5140 * Returns
5141 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
5142 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
5143 * move charge. if @target is not NULL, the page is stored in target->page
5144 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08005145 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
5146 * target for charge migration. if @target is not NULL, the entry is stored
5147 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005148 *
5149 * Called with pte lock held.
5150 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005151union mc_target {
5152 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005153 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005154};
5155
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005156enum mc_target_type {
5157 MC_TARGET_NONE, /* not used */
5158 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08005159 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005160};
5161
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005162static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
5163 unsigned long addr, pte_t ptent)
5164{
5165 struct page *page = vm_normal_page(vma, addr, ptent);
5166
5167 if (!page || !page_mapped(page))
5168 return NULL;
5169 if (PageAnon(page)) {
5170 /* we don't move shared anon */
5171 if (!move_anon() || page_mapcount(page) > 2)
5172 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005173 } else if (!move_file())
5174 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005175 return NULL;
5176 if (!get_page_unless_zero(page))
5177 return NULL;
5178
5179 return page;
5180}
5181
5182static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5183 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5184{
5185 int usage_count;
5186 struct page *page = NULL;
5187 swp_entry_t ent = pte_to_swp_entry(ptent);
5188
5189 if (!move_anon() || non_swap_entry(ent))
5190 return NULL;
5191 usage_count = mem_cgroup_count_swap_user(ent, &page);
5192 if (usage_count > 1) { /* we don't move shared anon */
5193 if (page)
5194 put_page(page);
5195 return NULL;
5196 }
5197 if (do_swap_account)
5198 entry->val = ent.val;
5199
5200 return page;
5201}
5202
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005203static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
5204 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5205{
5206 struct page *page = NULL;
5207 struct inode *inode;
5208 struct address_space *mapping;
5209 pgoff_t pgoff;
5210
5211 if (!vma->vm_file) /* anonymous vma */
5212 return NULL;
5213 if (!move_file())
5214 return NULL;
5215
5216 inode = vma->vm_file->f_path.dentry->d_inode;
5217 mapping = vma->vm_file->f_mapping;
5218 if (pte_none(ptent))
5219 pgoff = linear_page_index(vma, addr);
5220 else /* pte_file(ptent) is true */
5221 pgoff = pte_to_pgoff(ptent);
5222
5223 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005224 page = find_get_page(mapping, pgoff);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005225
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005226#ifdef CONFIG_SWAP
5227 /* shmem/tmpfs may report page out on swap: account for that too. */
5228 if (radix_tree_exceptional_entry(page)) {
5229 swp_entry_t swap = radix_to_swp_entry(page);
5230 if (do_swap_account)
5231 *entry = swap;
5232 page = find_get_page(&swapper_space, swap.val);
5233 }
5234#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005235 return page;
5236}
5237
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005238static int is_target_pte_for_mc(struct vm_area_struct *vma,
5239 unsigned long addr, pte_t ptent, union mc_target *target)
5240{
Daisuke Nishimura02491442010-03-10 15:22:17 -08005241 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005242 struct page_cgroup *pc;
5243 int ret = 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005244 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005245
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005246 if (pte_present(ptent))
5247 page = mc_handle_present_pte(vma, addr, ptent);
5248 else if (is_swap_pte(ptent))
5249 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005250 else if (pte_none(ptent) || pte_file(ptent))
5251 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005252
5253 if (!page && !ent.val)
Daisuke Nishimura02491442010-03-10 15:22:17 -08005254 return 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005255 if (page) {
5256 pc = lookup_page_cgroup(page);
5257 /*
5258 * Do only loose check w/o page_cgroup lock.
5259 * mem_cgroup_move_account() checks the pc is valid or not under
5260 * the lock.
5261 */
5262 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
5263 ret = MC_TARGET_PAGE;
5264 if (target)
5265 target->page = page;
5266 }
5267 if (!ret || !target)
5268 put_page(page);
5269 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005270 /* There is a swap entry and a page doesn't exist or isn't charged */
5271 if (ent.val && !ret &&
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005272 css_id(&mc.from->css) == lookup_swap_cgroup(ent)) {
5273 ret = MC_TARGET_SWAP;
5274 if (target)
5275 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005276 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005277 return ret;
5278}
5279
5280static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
5281 unsigned long addr, unsigned long end,
5282 struct mm_walk *walk)
5283{
5284 struct vm_area_struct *vma = walk->private;
5285 pte_t *pte;
5286 spinlock_t *ptl;
5287
Dave Hansen03319322011-03-22 16:32:56 -07005288 split_huge_page_pmd(walk->mm, pmd);
5289
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005290 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5291 for (; addr != end; pte++, addr += PAGE_SIZE)
5292 if (is_target_pte_for_mc(vma, addr, *pte, NULL))
5293 mc.precharge++; /* increment precharge temporarily */
5294 pte_unmap_unlock(pte - 1, ptl);
5295 cond_resched();
5296
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005297 return 0;
5298}
5299
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005300static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
5301{
5302 unsigned long precharge;
5303 struct vm_area_struct *vma;
5304
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005305 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005306 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5307 struct mm_walk mem_cgroup_count_precharge_walk = {
5308 .pmd_entry = mem_cgroup_count_precharge_pte_range,
5309 .mm = mm,
5310 .private = vma,
5311 };
5312 if (is_vm_hugetlb_page(vma))
5313 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005314 walk_page_range(vma->vm_start, vma->vm_end,
5315 &mem_cgroup_count_precharge_walk);
5316 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005317 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005318
5319 precharge = mc.precharge;
5320 mc.precharge = 0;
5321
5322 return precharge;
5323}
5324
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005325static int mem_cgroup_precharge_mc(struct mm_struct *mm)
5326{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005327 unsigned long precharge = mem_cgroup_count_precharge(mm);
5328
5329 VM_BUG_ON(mc.moving_task);
5330 mc.moving_task = current;
5331 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005332}
5333
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005334/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
5335static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005336{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005337 struct mem_cgroup *from = mc.from;
5338 struct mem_cgroup *to = mc.to;
5339
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005340 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005341 if (mc.precharge) {
5342 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
5343 mc.precharge = 0;
5344 }
5345 /*
5346 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
5347 * we must uncharge here.
5348 */
5349 if (mc.moved_charge) {
5350 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
5351 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005352 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005353 /* we must fixup refcnts and charges */
5354 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005355 /* uncharge swap account from the old cgroup */
5356 if (!mem_cgroup_is_root(mc.from))
5357 res_counter_uncharge(&mc.from->memsw,
5358 PAGE_SIZE * mc.moved_swap);
5359 __mem_cgroup_put(mc.from, mc.moved_swap);
5360
5361 if (!mem_cgroup_is_root(mc.to)) {
5362 /*
5363 * we charged both to->res and to->memsw, so we should
5364 * uncharge to->res.
5365 */
5366 res_counter_uncharge(&mc.to->res,
5367 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005368 }
5369 /* we've already done mem_cgroup_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005370 mc.moved_swap = 0;
5371 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005372 memcg_oom_recover(from);
5373 memcg_oom_recover(to);
5374 wake_up_all(&mc.waitq);
5375}
5376
5377static void mem_cgroup_clear_mc(void)
5378{
5379 struct mem_cgroup *from = mc.from;
5380
5381 /*
5382 * we must clear moving_task before waking up waiters at the end of
5383 * task migration.
5384 */
5385 mc.moving_task = NULL;
5386 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005387 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005388 mc.from = NULL;
5389 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005390 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005391 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005392}
5393
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005394static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
5395 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005396 struct task_struct *p)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005397{
5398 int ret = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005399 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005400
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005401 if (memcg->move_charge_at_immigrate) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005402 struct mm_struct *mm;
5403 struct mem_cgroup *from = mem_cgroup_from_task(p);
5404
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005405 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005406
5407 mm = get_task_mm(p);
5408 if (!mm)
5409 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005410 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005411 if (mm->owner == p) {
5412 VM_BUG_ON(mc.from);
5413 VM_BUG_ON(mc.to);
5414 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005415 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005416 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005417 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005418 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005419 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005420 mc.to = memcg;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005421 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005422 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005423
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005424 ret = mem_cgroup_precharge_mc(mm);
5425 if (ret)
5426 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005427 }
5428 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005429 }
5430 return ret;
5431}
5432
5433static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
5434 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005435 struct task_struct *p)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005436{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005437 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005438}
5439
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005440static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
5441 unsigned long addr, unsigned long end,
5442 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005443{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005444 int ret = 0;
5445 struct vm_area_struct *vma = walk->private;
5446 pte_t *pte;
5447 spinlock_t *ptl;
5448
Dave Hansen03319322011-03-22 16:32:56 -07005449 split_huge_page_pmd(walk->mm, pmd);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005450retry:
5451 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5452 for (; addr != end; addr += PAGE_SIZE) {
5453 pte_t ptent = *(pte++);
5454 union mc_target target;
5455 int type;
5456 struct page *page;
5457 struct page_cgroup *pc;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005458 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005459
5460 if (!mc.precharge)
5461 break;
5462
5463 type = is_target_pte_for_mc(vma, addr, ptent, &target);
5464 switch (type) {
5465 case MC_TARGET_PAGE:
5466 page = target.page;
5467 if (isolate_lru_page(page))
5468 goto put;
5469 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07005470 if (!mem_cgroup_move_account(page, 1, pc,
5471 mc.from, mc.to, false)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005472 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005473 /* we uncharge from mc.from later. */
5474 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005475 }
5476 putback_lru_page(page);
5477put: /* is_target_pte_for_mc() gets the page */
5478 put_page(page);
5479 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005480 case MC_TARGET_SWAP:
5481 ent = target.ent;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005482 if (!mem_cgroup_move_swap_account(ent,
5483 mc.from, mc.to, false)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08005484 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005485 /* we fixup refcnts and charges later. */
5486 mc.moved_swap++;
5487 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08005488 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005489 default:
5490 break;
5491 }
5492 }
5493 pte_unmap_unlock(pte - 1, ptl);
5494 cond_resched();
5495
5496 if (addr != end) {
5497 /*
5498 * We have consumed all precharges we got in can_attach().
5499 * We try charge one by one, but don't do any additional
5500 * charges to mc.to if we have failed in charge once in attach()
5501 * phase.
5502 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005503 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005504 if (!ret)
5505 goto retry;
5506 }
5507
5508 return ret;
5509}
5510
5511static void mem_cgroup_move_charge(struct mm_struct *mm)
5512{
5513 struct vm_area_struct *vma;
5514
5515 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005516retry:
5517 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
5518 /*
5519 * Someone who are holding the mmap_sem might be waiting in
5520 * waitq. So we cancel all extra charges, wake up all waiters,
5521 * and retry. Because we cancel precharges, we might not be able
5522 * to move enough charges, but moving charge is a best-effort
5523 * feature anyway, so it wouldn't be a big problem.
5524 */
5525 __mem_cgroup_clear_mc();
5526 cond_resched();
5527 goto retry;
5528 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005529 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5530 int ret;
5531 struct mm_walk mem_cgroup_move_charge_walk = {
5532 .pmd_entry = mem_cgroup_move_charge_pte_range,
5533 .mm = mm,
5534 .private = vma,
5535 };
5536 if (is_vm_hugetlb_page(vma))
5537 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005538 ret = walk_page_range(vma->vm_start, vma->vm_end,
5539 &mem_cgroup_move_charge_walk);
5540 if (ret)
5541 /*
5542 * means we have consumed all precharges and failed in
5543 * doing additional charge. Just abandon here.
5544 */
5545 break;
5546 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005547 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005548}
5549
Balbir Singh67e465a2008-02-07 00:13:54 -08005550static void mem_cgroup_move_task(struct cgroup_subsys *ss,
5551 struct cgroup *cont,
5552 struct cgroup *old_cont,
Ben Blumf780bdb2011-05-26 16:25:19 -07005553 struct task_struct *p)
Balbir Singh67e465a2008-02-07 00:13:54 -08005554{
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005555 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005556
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005557 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005558 if (mc.to)
5559 mem_cgroup_move_charge(mm);
5560 put_swap_token(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005561 mmput(mm);
5562 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005563 if (mc.to)
5564 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08005565}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005566#else /* !CONFIG_MMU */
5567static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
5568 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005569 struct task_struct *p)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005570{
5571 return 0;
5572}
5573static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
5574 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005575 struct task_struct *p)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005576{
5577}
5578static void mem_cgroup_move_task(struct cgroup_subsys *ss,
5579 struct cgroup *cont,
5580 struct cgroup *old_cont,
Ben Blumf780bdb2011-05-26 16:25:19 -07005581 struct task_struct *p)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005582{
5583}
5584#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08005585
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005586struct cgroup_subsys mem_cgroup_subsys = {
5587 .name = "memory",
5588 .subsys_id = mem_cgroup_subsys_id,
5589 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005590 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005591 .destroy = mem_cgroup_destroy,
5592 .populate = mem_cgroup_populate,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005593 .can_attach = mem_cgroup_can_attach,
5594 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08005595 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005596 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005597 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005598};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005599
5600#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08005601static int __init enable_swap_account(char *s)
5602{
5603 /* consider enabled if no parameter or 1 is given */
Michal Hockoa2c89902011-05-24 17:12:50 -07005604 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08005605 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07005606 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08005607 really_do_swap_account = 0;
5608 return 1;
5609}
Michal Hockoa2c89902011-05-24 17:12:50 -07005610__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005611
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005612#endif