blob: bec451da7def42ac44c5487528ced2f15b25c120 [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"
Glauber Costad1a4c0b2011-12-11 21:47:04 +000053#include <net/sock.h>
54#include <net/tcp_memcontrol.h>
Balbir Singh8cdea7c2008-02-07 00:13:50 -080055
Balbir Singh8697d332008-02-07 00:13:59 -080056#include <asm/uaccess.h>
57
KOSAKI Motohirocc8e9702010-08-09 17:19:57 -070058#include <trace/events/vmscan.h>
59
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070060struct cgroup_subsys mem_cgroup_subsys __read_mostly;
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070061#define MEM_CGROUP_RECLAIM_RETRIES 5
Balbir Singh4b3bde42009-09-23 15:56:32 -070062struct mem_cgroup *root_mem_cgroup __read_mostly;
Balbir Singh8cdea7c2008-02-07 00:13:50 -080063
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080064#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
Li Zefan338c8432009-06-17 16:27:15 -070065/* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080066int do_swap_account __read_mostly;
Michal Hockoa42c3902010-11-24 12:57:08 -080067
68/* for remember boot option*/
69#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP_ENABLED
70static int really_do_swap_account __initdata = 1;
71#else
72static int really_do_swap_account __initdata = 0;
73#endif
74
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080075#else
76#define do_swap_account (0)
77#endif
78
79
Balbir Singh8cdea7c2008-02-07 00:13:50 -080080/*
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080081 * Statistics for memory cgroup.
82 */
83enum mem_cgroup_stat_index {
84 /*
85 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
86 */
87 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
Balbir Singhd69b0422009-06-17 16:26:34 -070088 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -080089 MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */
Balbir Singh0c3e73e2009-09-23 15:56:42 -070090 MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -070091 MEM_CGROUP_STAT_DATA, /* end of data requires synchronization */
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -070092 MEM_CGROUP_ON_MOVE, /* someone is moving account between groups */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080093 MEM_CGROUP_STAT_NSTATS,
94};
95
Johannes Weinere9f89742011-03-23 16:42:37 -070096enum mem_cgroup_events_index {
97 MEM_CGROUP_EVENTS_PGPGIN, /* # of pages paged in */
98 MEM_CGROUP_EVENTS_PGPGOUT, /* # of pages paged out */
99 MEM_CGROUP_EVENTS_COUNT, /* # of pages paged in/out */
Ying Han456f9982011-05-26 16:25:38 -0700100 MEM_CGROUP_EVENTS_PGFAULT, /* # of page-faults */
101 MEM_CGROUP_EVENTS_PGMAJFAULT, /* # of major page-faults */
Johannes Weinere9f89742011-03-23 16:42:37 -0700102 MEM_CGROUP_EVENTS_NSTATS,
103};
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700104/*
105 * Per memcg event counter is incremented at every pagein/pageout. With THP,
106 * it will be incremated by the number of pages. This counter is used for
107 * for trigger some periodic events. This is straightforward and better
108 * than using jiffies etc. to handle periodic memcg event.
109 */
110enum mem_cgroup_events_target {
111 MEM_CGROUP_TARGET_THRESH,
112 MEM_CGROUP_TARGET_SOFTLIMIT,
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700113 MEM_CGROUP_TARGET_NUMAINFO,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700114 MEM_CGROUP_NTARGETS,
115};
116#define THRESHOLDS_EVENTS_TARGET (128)
117#define SOFTLIMIT_EVENTS_TARGET (1024)
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700118#define NUMAINFO_EVENTS_TARGET (1024)
Johannes Weinere9f89742011-03-23 16:42:37 -0700119
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800120struct mem_cgroup_stat_cpu {
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700121 long count[MEM_CGROUP_STAT_NSTATS];
Johannes Weinere9f89742011-03-23 16:42:37 -0700122 unsigned long events[MEM_CGROUP_EVENTS_NSTATS];
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700123 unsigned long targets[MEM_CGROUP_NTARGETS];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800124};
125
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800126struct mem_cgroup_reclaim_iter {
127 /* css_id of the last scanned hierarchy member */
128 int position;
129 /* scan generation, increased every round-trip */
130 unsigned int generation;
131};
132
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800133/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800134 * per-zone information in memory controller.
135 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800136struct mem_cgroup_per_zone {
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800137 /*
138 * spin_lock to protect the per cgroup LRU
139 */
Christoph Lameterb69408e2008-10-18 20:26:14 -0700140 struct list_head lists[NR_LRU_LISTS];
141 unsigned long count[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800142
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800143 struct mem_cgroup_reclaim_iter reclaim_iter[DEF_PRIORITY + 1];
144
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800145 struct zone_reclaim_stat reclaim_stat;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700146 struct rb_node tree_node; /* RB tree node */
147 unsigned long long usage_in_excess;/* Set to the value by which */
148 /* the soft limit is exceeded*/
149 bool on_tree;
Balbir Singh4e416952009-09-23 15:56:39 -0700150 struct mem_cgroup *mem; /* Back pointer, we cannot */
151 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800152};
153/* Macro for accessing counter */
154#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
155
156struct mem_cgroup_per_node {
157 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
158};
159
160struct mem_cgroup_lru_info {
161 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
162};
163
164/*
Balbir Singhf64c3f52009-09-23 15:56:37 -0700165 * Cgroups above their limits are maintained in a RB-Tree, independent of
166 * their hierarchy representation
167 */
168
169struct mem_cgroup_tree_per_zone {
170 struct rb_root rb_root;
171 spinlock_t lock;
172};
173
174struct mem_cgroup_tree_per_node {
175 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
176};
177
178struct mem_cgroup_tree {
179 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
180};
181
182static struct mem_cgroup_tree soft_limit_tree __read_mostly;
183
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800184struct mem_cgroup_threshold {
185 struct eventfd_ctx *eventfd;
186 u64 threshold;
187};
188
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700189/* For threshold */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800190struct mem_cgroup_threshold_ary {
191 /* An array index points to threshold just below usage. */
Phil Carmody5407a562010-05-26 14:42:42 -0700192 int current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800193 /* Size of entries[] */
194 unsigned int size;
195 /* Array of thresholds */
196 struct mem_cgroup_threshold entries[0];
197};
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700198
199struct mem_cgroup_thresholds {
200 /* Primary thresholds array */
201 struct mem_cgroup_threshold_ary *primary;
202 /*
203 * Spare threshold array.
204 * This is needed to make mem_cgroup_unregister_event() "never fail".
205 * It must be able to store at least primary->size - 1 entries.
206 */
207 struct mem_cgroup_threshold_ary *spare;
208};
209
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700210/* for OOM */
211struct mem_cgroup_eventfd_list {
212 struct list_head list;
213 struct eventfd_ctx *eventfd;
214};
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800215
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700216static void mem_cgroup_threshold(struct mem_cgroup *memcg);
217static void mem_cgroup_oom_notify(struct mem_cgroup *memcg);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800218
Balbir Singhf64c3f52009-09-23 15:56:37 -0700219/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800220 * The memory controller data structure. The memory controller controls both
221 * page cache and RSS per cgroup. We would eventually like to provide
222 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
223 * to help the administrator determine what knobs to tune.
224 *
225 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800226 * we hit the water mark. May be even add a low water mark, such that
227 * no reclaim occurs from a cgroup at it's low water mark, this is
228 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800229 */
230struct mem_cgroup {
231 struct cgroup_subsys_state css;
232 /*
233 * the counter to account for memory usage
234 */
235 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800236 /*
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800237 * the counter to account for mem+swap usage.
238 */
239 struct res_counter memsw;
240 /*
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800241 * Per cgroup active and inactive list, similar to the
242 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800243 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800244 struct mem_cgroup_lru_info info;
Ying Han889976d2011-05-26 16:25:33 -0700245 int last_scanned_node;
246#if MAX_NUMNODES > 1
247 nodemask_t scan_nodes;
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700248 atomic_t numainfo_events;
249 atomic_t numainfo_updating;
Ying Han889976d2011-05-26 16:25:33 -0700250#endif
Balbir Singh18f59ea2009-01-07 18:08:07 -0800251 /*
252 * Should the accounting and control be hierarchical, per subtree?
253 */
254 bool use_hierarchy;
Michal Hocko79dfdac2011-07-26 16:08:23 -0700255
256 bool oom_lock;
257 atomic_t under_oom;
258
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800259 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800260
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -0700261 int swappiness;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -0700262 /* OOM-Killer disable */
263 int oom_kill_disable;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800264
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700265 /* set when res.limit == memsw.limit */
266 bool memsw_is_minimum;
267
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800268 /* protect arrays of thresholds */
269 struct mutex thresholds_lock;
270
271 /* thresholds for memory usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700272 struct mem_cgroup_thresholds thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700273
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800274 /* thresholds for mem+swap usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700275 struct mem_cgroup_thresholds memsw_thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700276
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700277 /* For oom notifier event fd */
278 struct list_head oom_notify;
Johannes Weiner185efc02011-09-14 16:21:58 -0700279
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800280 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800281 * Should we move charges of a task when a task is moved into this
282 * mem_cgroup ? And what type of charges should we move ?
283 */
284 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800285 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800286 * percpu counter.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800287 */
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800288 struct mem_cgroup_stat_cpu *stat;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700289 /*
290 * used when a cpu is offlined or other synchronizations
291 * See mem_cgroup_read_stat().
292 */
293 struct mem_cgroup_stat_cpu nocpu_base;
294 spinlock_t pcp_counter_lock;
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000295
296#ifdef CONFIG_INET
297 struct tcp_memcontrol tcp_mem;
298#endif
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800299};
300
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800301/* Stuffs for move charges at task migration. */
302/*
303 * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a
304 * left-shifted bitmap of these types.
305 */
306enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800307 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700308 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800309 NR_MOVE_TYPE,
310};
311
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800312/* "mc" and its members are protected by cgroup_mutex */
313static struct move_charge_struct {
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -0800314 spinlock_t lock; /* for from, to */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800315 struct mem_cgroup *from;
316 struct mem_cgroup *to;
317 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800318 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800319 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800320 struct task_struct *moving_task; /* a task moving charges */
321 wait_queue_head_t waitq; /* a waitq for other context */
322} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700323 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800324 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
325};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800326
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700327static bool move_anon(void)
328{
329 return test_bit(MOVE_CHARGE_TYPE_ANON,
330 &mc.to->move_charge_at_immigrate);
331}
332
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700333static bool move_file(void)
334{
335 return test_bit(MOVE_CHARGE_TYPE_FILE,
336 &mc.to->move_charge_at_immigrate);
337}
338
Balbir Singh4e416952009-09-23 15:56:39 -0700339/*
340 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
341 * limit reclaim to prevent infinite loops, if they ever occur.
342 */
343#define MEM_CGROUP_MAX_RECLAIM_LOOPS (100)
344#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2)
345
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800346enum charge_type {
347 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
348 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700349 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700350 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800351 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700352 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700353 NR_CHARGE_TYPE,
354};
355
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800356/* for encoding cft->private value on file */
Glauber Costa65c64ce2011-12-22 01:02:27 +0000357#define _MEM (0)
358#define _MEMSWAP (1)
359#define _OOM_TYPE (2)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800360#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
361#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
362#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700363/* Used for OOM nofiier */
364#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800365
Balbir Singh75822b42009-09-23 15:56:38 -0700366/*
367 * Reclaim flags for mem_cgroup_hierarchical_reclaim
368 */
369#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
370#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
371#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
372#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
Balbir Singh4e416952009-09-23 15:56:39 -0700373#define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
374#define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
Balbir Singh75822b42009-09-23 15:56:38 -0700375
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700376static void mem_cgroup_get(struct mem_cgroup *memcg);
377static void mem_cgroup_put(struct mem_cgroup *memcg);
Glauber Costae1aab162011-12-11 21:47:03 +0000378
379/* Writing them here to avoid exposing memcg's inner layout */
380#ifdef CONFIG_CGROUP_MEM_RES_CTLR_KMEM
381#ifdef CONFIG_INET
382#include <net/sock.h>
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000383#include <net/ip.h>
Glauber Costae1aab162011-12-11 21:47:03 +0000384
385static bool mem_cgroup_is_root(struct mem_cgroup *memcg);
386void sock_update_memcg(struct sock *sk)
387{
Glauber Costae1aab162011-12-11 21:47:03 +0000388 if (static_branch(&memcg_socket_limit_enabled)) {
389 struct mem_cgroup *memcg;
390
391 BUG_ON(!sk->sk_prot->proto_cgroup);
392
Glauber Costaf3f511e2012-01-05 20:16:39 +0000393 /* Socket cloning can throw us here with sk_cgrp already
394 * filled. It won't however, necessarily happen from
395 * process context. So the test for root memcg given
396 * the current task's memcg won't help us in this case.
397 *
398 * Respecting the original socket's memcg is a better
399 * decision in this case.
400 */
401 if (sk->sk_cgrp) {
402 BUG_ON(mem_cgroup_is_root(sk->sk_cgrp->memcg));
403 mem_cgroup_get(sk->sk_cgrp->memcg);
404 return;
405 }
406
Glauber Costae1aab162011-12-11 21:47:03 +0000407 rcu_read_lock();
408 memcg = mem_cgroup_from_task(current);
409 if (!mem_cgroup_is_root(memcg)) {
410 mem_cgroup_get(memcg);
411 sk->sk_cgrp = sk->sk_prot->proto_cgroup(memcg);
412 }
413 rcu_read_unlock();
414 }
415}
416EXPORT_SYMBOL(sock_update_memcg);
417
418void sock_release_memcg(struct sock *sk)
419{
420 if (static_branch(&memcg_socket_limit_enabled) && sk->sk_cgrp) {
421 struct mem_cgroup *memcg;
422 WARN_ON(!sk->sk_cgrp->memcg);
423 memcg = sk->sk_cgrp->memcg;
424 mem_cgroup_put(memcg);
425 }
426}
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000427
428struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg)
429{
430 if (!memcg || mem_cgroup_is_root(memcg))
431 return NULL;
432
433 return &memcg->tcp_mem.cg_proto;
434}
435EXPORT_SYMBOL(tcp_proto_cgroup);
Glauber Costae1aab162011-12-11 21:47:03 +0000436#endif /* CONFIG_INET */
437#endif /* CONFIG_CGROUP_MEM_RES_CTLR_KMEM */
438
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700439static void drain_all_stock_async(struct mem_cgroup *memcg);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800440
Balbir Singhf64c3f52009-09-23 15:56:37 -0700441static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700442mem_cgroup_zoneinfo(struct mem_cgroup *memcg, int nid, int zid)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700443{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700444 return &memcg->info.nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700445}
446
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700447struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100448{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700449 return &memcg->css;
Wu Fengguangd3242362009-12-16 12:19:59 +0100450}
451
Balbir Singhf64c3f52009-09-23 15:56:37 -0700452static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700453page_cgroup_zoneinfo(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700454{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700455 int nid = page_to_nid(page);
456 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700457
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700458 return mem_cgroup_zoneinfo(memcg, nid, zid);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700459}
460
461static struct mem_cgroup_tree_per_zone *
462soft_limit_tree_node_zone(int nid, int zid)
463{
464 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
465}
466
467static struct mem_cgroup_tree_per_zone *
468soft_limit_tree_from_page(struct page *page)
469{
470 int nid = page_to_nid(page);
471 int zid = page_zonenum(page);
472
473 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
474}
475
476static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700477__mem_cgroup_insert_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700478 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700479 struct mem_cgroup_tree_per_zone *mctz,
480 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700481{
482 struct rb_node **p = &mctz->rb_root.rb_node;
483 struct rb_node *parent = NULL;
484 struct mem_cgroup_per_zone *mz_node;
485
486 if (mz->on_tree)
487 return;
488
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700489 mz->usage_in_excess = new_usage_in_excess;
490 if (!mz->usage_in_excess)
491 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700492 while (*p) {
493 parent = *p;
494 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
495 tree_node);
496 if (mz->usage_in_excess < mz_node->usage_in_excess)
497 p = &(*p)->rb_left;
498 /*
499 * We can't avoid mem cgroups that are over their soft
500 * limit by the same amount
501 */
502 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
503 p = &(*p)->rb_right;
504 }
505 rb_link_node(&mz->tree_node, parent, p);
506 rb_insert_color(&mz->tree_node, &mctz->rb_root);
507 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700508}
509
510static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700511__mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singh4e416952009-09-23 15:56:39 -0700512 struct mem_cgroup_per_zone *mz,
513 struct mem_cgroup_tree_per_zone *mctz)
514{
515 if (!mz->on_tree)
516 return;
517 rb_erase(&mz->tree_node, &mctz->rb_root);
518 mz->on_tree = false;
519}
520
521static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700522mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700523 struct mem_cgroup_per_zone *mz,
524 struct mem_cgroup_tree_per_zone *mctz)
525{
526 spin_lock(&mctz->lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700527 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700528 spin_unlock(&mctz->lock);
529}
530
Balbir Singhf64c3f52009-09-23 15:56:37 -0700531
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700532static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700533{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700534 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700535 struct mem_cgroup_per_zone *mz;
536 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700537 int nid = page_to_nid(page);
538 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700539 mctz = soft_limit_tree_from_page(page);
540
541 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700542 * Necessary to update all ancestors when hierarchy is used.
543 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700544 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700545 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
546 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
547 excess = res_counter_soft_limit_excess(&memcg->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700548 /*
549 * We have to update the tree if mz is on RB-tree or
550 * mem is over its softlimit.
551 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700552 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700553 spin_lock(&mctz->lock);
554 /* if on-tree, remove it */
555 if (mz->on_tree)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700556 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700557 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700558 * Insert again. mz->usage_in_excess will be updated.
559 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700560 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700561 __mem_cgroup_insert_exceeded(memcg, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700562 spin_unlock(&mctz->lock);
563 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700564 }
565}
566
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700567static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700568{
569 int node, zone;
570 struct mem_cgroup_per_zone *mz;
571 struct mem_cgroup_tree_per_zone *mctz;
572
573 for_each_node_state(node, N_POSSIBLE) {
574 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700575 mz = mem_cgroup_zoneinfo(memcg, node, zone);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700576 mctz = soft_limit_tree_node_zone(node, zone);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700577 mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700578 }
579 }
580}
581
Balbir Singh4e416952009-09-23 15:56:39 -0700582static struct mem_cgroup_per_zone *
583__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
584{
585 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700586 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700587
588retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700589 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700590 rightmost = rb_last(&mctz->rb_root);
591 if (!rightmost)
592 goto done; /* Nothing to reclaim from */
593
594 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
595 /*
596 * Remove the node now but someone else can add it back,
597 * we will to add it back at the end of reclaim to its correct
598 * position in the tree.
599 */
600 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
601 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
602 !css_tryget(&mz->mem->css))
603 goto retry;
604done:
605 return mz;
606}
607
608static struct mem_cgroup_per_zone *
609mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
610{
611 struct mem_cgroup_per_zone *mz;
612
613 spin_lock(&mctz->lock);
614 mz = __mem_cgroup_largest_soft_limit_node(mctz);
615 spin_unlock(&mctz->lock);
616 return mz;
617}
618
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700619/*
620 * Implementation Note: reading percpu statistics for memcg.
621 *
622 * Both of vmstat[] and percpu_counter has threshold and do periodic
623 * synchronization to implement "quick" read. There are trade-off between
624 * reading cost and precision of value. Then, we may have a chance to implement
625 * a periodic synchronizion of counter in memcg's counter.
626 *
627 * But this _read() function is used for user interface now. The user accounts
628 * memory usage by memory cgroup and he _always_ requires exact value because
629 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
630 * have to visit all online cpus and make sum. So, for now, unnecessary
631 * synchronization is not implemented. (just implemented for cpu hotplug)
632 *
633 * If there are kernel internal actions which can make use of some not-exact
634 * value, and reading all cpu value can be performance bottleneck in some
635 * common workload, threashold and synchonization as vmstat[] should be
636 * implemented.
637 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700638static long mem_cgroup_read_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700639 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800640{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700641 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800642 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800643
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700644 get_online_cpus();
645 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700646 val += per_cpu(memcg->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700647#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700648 spin_lock(&memcg->pcp_counter_lock);
649 val += memcg->nocpu_base.count[idx];
650 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700651#endif
652 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800653 return val;
654}
655
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700656static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700657 bool charge)
658{
659 int val = (charge) ? 1 : -1;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700660 this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAPOUT], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700661}
662
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700663void mem_cgroup_pgfault(struct mem_cgroup *memcg, int val)
Ying Han456f9982011-05-26 16:25:38 -0700664{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700665 this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT], val);
Ying Han456f9982011-05-26 16:25:38 -0700666}
667
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700668void mem_cgroup_pgmajfault(struct mem_cgroup *memcg, int val)
Ying Han456f9982011-05-26 16:25:38 -0700669{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700670 this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT], val);
Ying Han456f9982011-05-26 16:25:38 -0700671}
672
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700673static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg,
Johannes Weinere9f89742011-03-23 16:42:37 -0700674 enum mem_cgroup_events_index idx)
675{
676 unsigned long val = 0;
677 int cpu;
678
679 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700680 val += per_cpu(memcg->stat->events[idx], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -0700681#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700682 spin_lock(&memcg->pcp_counter_lock);
683 val += memcg->nocpu_base.events[idx];
684 spin_unlock(&memcg->pcp_counter_lock);
Johannes Weinere9f89742011-03-23 16:42:37 -0700685#endif
686 return val;
687}
688
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700689static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800690 bool file, int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800691{
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800692 preempt_disable();
693
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800694 if (file)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700695 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE],
696 nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800697 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700698 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS],
699 nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700700
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800701 /* pagein of a big page is an event. So, ignore page size */
702 if (nr_pages > 0)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700703 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800704 else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700705 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800706 nr_pages = -nr_pages; /* for event */
707 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800708
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700709 __this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_COUNT], nr_pages);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800710
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800711 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800712}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800713
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700714unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700715mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg, int nid, int zid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700716 unsigned int lru_mask)
Ying Han889976d2011-05-26 16:25:33 -0700717{
718 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700719 enum lru_list l;
720 unsigned long ret = 0;
721
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700722 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700723
724 for_each_lru(l) {
725 if (BIT(l) & lru_mask)
726 ret += MEM_CGROUP_ZSTAT(mz, l);
727 }
728 return ret;
729}
730
731static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700732mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700733 int nid, unsigned int lru_mask)
734{
Ying Han889976d2011-05-26 16:25:33 -0700735 u64 total = 0;
736 int zid;
737
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700738 for (zid = 0; zid < MAX_NR_ZONES; zid++)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700739 total += mem_cgroup_zone_nr_lru_pages(memcg,
740 nid, zid, lru_mask);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700741
Ying Han889976d2011-05-26 16:25:33 -0700742 return total;
743}
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700744
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700745static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700746 unsigned int lru_mask)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800747{
Ying Han889976d2011-05-26 16:25:33 -0700748 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800749 u64 total = 0;
750
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700751 for_each_node_state(nid, N_HIGH_MEMORY)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700752 total += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800753 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800754}
755
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700756static bool __memcg_event_check(struct mem_cgroup *memcg, int target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800757{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700758 unsigned long val, next;
759
Steven Rostedt47994012011-11-02 13:38:33 -0700760 val = __this_cpu_read(memcg->stat->events[MEM_CGROUP_EVENTS_COUNT]);
761 next = __this_cpu_read(memcg->stat->targets[target]);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700762 /* from time_after() in jiffies.h */
763 return ((long)next - (long)val < 0);
764}
765
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700766static void __mem_cgroup_target_update(struct mem_cgroup *memcg, int target)
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700767{
768 unsigned long val, next;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800769
Steven Rostedt47994012011-11-02 13:38:33 -0700770 val = __this_cpu_read(memcg->stat->events[MEM_CGROUP_EVENTS_COUNT]);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800771
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700772 switch (target) {
773 case MEM_CGROUP_TARGET_THRESH:
774 next = val + THRESHOLDS_EVENTS_TARGET;
775 break;
776 case MEM_CGROUP_TARGET_SOFTLIMIT:
777 next = val + SOFTLIMIT_EVENTS_TARGET;
778 break;
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700779 case MEM_CGROUP_TARGET_NUMAINFO:
780 next = val + NUMAINFO_EVENTS_TARGET;
781 break;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700782 default:
783 return;
784 }
785
Steven Rostedt47994012011-11-02 13:38:33 -0700786 __this_cpu_write(memcg->stat->targets[target], next);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800787}
788
789/*
790 * Check events in order.
791 *
792 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700793static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800794{
Steven Rostedt47994012011-11-02 13:38:33 -0700795 preempt_disable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800796 /* threshold event is triggered in finer grain than soft limit */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700797 if (unlikely(__memcg_event_check(memcg, MEM_CGROUP_TARGET_THRESH))) {
798 mem_cgroup_threshold(memcg);
799 __mem_cgroup_target_update(memcg, MEM_CGROUP_TARGET_THRESH);
800 if (unlikely(__memcg_event_check(memcg,
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700801 MEM_CGROUP_TARGET_SOFTLIMIT))) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700802 mem_cgroup_update_tree(memcg, page);
803 __mem_cgroup_target_update(memcg,
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700804 MEM_CGROUP_TARGET_SOFTLIMIT);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700805 }
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700806#if MAX_NUMNODES > 1
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700807 if (unlikely(__memcg_event_check(memcg,
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700808 MEM_CGROUP_TARGET_NUMAINFO))) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700809 atomic_inc(&memcg->numainfo_events);
810 __mem_cgroup_target_update(memcg,
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700811 MEM_CGROUP_TARGET_NUMAINFO);
812 }
813#endif
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800814 }
Steven Rostedt47994012011-11-02 13:38:33 -0700815 preempt_enable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800816}
817
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000818struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800819{
820 return container_of(cgroup_subsys_state(cont,
821 mem_cgroup_subsys_id), struct mem_cgroup,
822 css);
823}
824
Balbir Singhcf475ad2008-04-29 01:00:16 -0700825struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800826{
Balbir Singh31a78f22008-09-28 23:09:31 +0100827 /*
828 * mm_update_next_owner() may clear mm->owner to NULL
829 * if it races with swapoff, page migration, etc.
830 * So this can be called with p == NULL.
831 */
832 if (unlikely(!p))
833 return NULL;
834
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800835 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
836 struct mem_cgroup, css);
837}
838
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700839struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800840{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700841 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700842
843 if (!mm)
844 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800845 /*
846 * Because we have no locks, mm->owner's may be being moved to other
847 * cgroup. We use css_tryget() here even if this looks
848 * pessimistic (rather than adding locks here).
849 */
850 rcu_read_lock();
851 do {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700852 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
853 if (unlikely(!memcg))
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800854 break;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700855 } while (!css_tryget(&memcg->css));
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800856 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700857 return memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800858}
859
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800860struct mem_cgroup_reclaim_cookie {
861 struct zone *zone;
862 int priority;
863 unsigned int generation;
864};
865
866static struct mem_cgroup *
867mem_cgroup_iter(struct mem_cgroup *root,
868 struct mem_cgroup *prev,
869 struct mem_cgroup_reclaim_cookie *reclaim)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700870{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800871 struct mem_cgroup *memcg = NULL;
872 int id = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700873
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700874 if (!root)
875 root = root_mem_cgroup;
876
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800877 if (prev && !reclaim)
878 id = css_id(&prev->css);
879
880 if (prev && prev != root)
881 css_put(&prev->css);
882
883 if (!root->use_hierarchy && root != root_mem_cgroup) {
884 if (prev)
885 return NULL;
886 return root;
887 }
888
889 while (!memcg) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800890 struct mem_cgroup_reclaim_iter *uninitialized_var(iter);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800891 struct cgroup_subsys_state *css;
892
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800893 if (reclaim) {
894 int nid = zone_to_nid(reclaim->zone);
895 int zid = zone_idx(reclaim->zone);
896 struct mem_cgroup_per_zone *mz;
897
898 mz = mem_cgroup_zoneinfo(root, nid, zid);
899 iter = &mz->reclaim_iter[reclaim->priority];
900 if (prev && reclaim->generation != iter->generation)
901 return NULL;
902 id = iter->position;
903 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800904
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700905 rcu_read_lock();
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800906 css = css_get_next(&mem_cgroup_subsys, id + 1, &root->css, &id);
907 if (css) {
908 if (css == &root->css || css_tryget(css))
909 memcg = container_of(css,
910 struct mem_cgroup, css);
911 } else
912 id = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700913 rcu_read_unlock();
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700914
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800915 if (reclaim) {
916 iter->position = id;
917 if (!css)
918 iter->generation++;
919 else if (!prev && memcg)
920 reclaim->generation = iter->generation;
921 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800922
923 if (prev && !css)
924 return NULL;
925 }
926 return memcg;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700927}
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800928
929static void mem_cgroup_iter_break(struct mem_cgroup *root,
930 struct mem_cgroup *prev)
931{
932 if (!root)
933 root = root_mem_cgroup;
934 if (prev && prev != root)
935 css_put(&prev->css);
936}
937
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700938/*
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800939 * Iteration constructs for visiting all cgroups (under a tree). If
940 * loops are exited prematurely (break), mem_cgroup_iter_break() must
941 * be used for reference counting.
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700942 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800943#define for_each_mem_cgroup_tree(iter, root) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800944 for (iter = mem_cgroup_iter(root, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800945 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800946 iter = mem_cgroup_iter(root, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700947
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800948#define for_each_mem_cgroup(iter) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800949 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800950 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800951 iter = mem_cgroup_iter(NULL, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700952
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700953static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
Balbir Singh4b3bde42009-09-23 15:56:32 -0700954{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700955 return (memcg == root_mem_cgroup);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700956}
957
Ying Han456f9982011-05-26 16:25:38 -0700958void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
959{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700960 struct mem_cgroup *memcg;
Ying Han456f9982011-05-26 16:25:38 -0700961
962 if (!mm)
963 return;
964
965 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700966 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
967 if (unlikely(!memcg))
Ying Han456f9982011-05-26 16:25:38 -0700968 goto out;
969
970 switch (idx) {
971 case PGMAJFAULT:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700972 mem_cgroup_pgmajfault(memcg, 1);
Ying Han456f9982011-05-26 16:25:38 -0700973 break;
974 case PGFAULT:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700975 mem_cgroup_pgfault(memcg, 1);
Ying Han456f9982011-05-26 16:25:38 -0700976 break;
977 default:
978 BUG();
979 }
980out:
981 rcu_read_unlock();
982}
983EXPORT_SYMBOL(mem_cgroup_count_vm_event);
984
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800985/*
986 * Following LRU functions are allowed to be used without PCG_LOCK.
987 * Operations are called by routine of global LRU independently from memcg.
988 * What we have to take care of here is validness of pc->mem_cgroup.
989 *
990 * Changes to pc->mem_cgroup happens when
991 * 1. charge
992 * 2. moving account
993 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
994 * It is added to LRU before charge.
995 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
996 * When moving account, the page is not on LRU. It's isolated.
997 */
998
999void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001000{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001001 struct page_cgroup *pc;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001002 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001003
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001004 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001005 return;
1006 pc = lookup_page_cgroup(page);
1007 /* can happen while we handle swapcache. */
Balbir Singh4b3bde42009-09-23 15:56:32 -07001008 if (!TestClearPageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001009 return;
Balbir Singh4b3bde42009-09-23 15:56:32 -07001010 VM_BUG_ON(!pc->mem_cgroup);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001011 /*
1012 * We don't check PCG_USED bit. It's cleared when the "page" is finally
1013 * removed from global LRU.
1014 */
Johannes Weiner97a6c372011-03-23 16:42:27 -07001015 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08001016 /* huge page split is done under lru_lock. so, we have no races. */
1017 MEM_CGROUP_ZSTAT(mz, lru) -= 1 << compound_order(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -07001018 if (mem_cgroup_is_root(pc->mem_cgroup))
1019 return;
1020 VM_BUG_ON(list_empty(&pc->lru));
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001021 list_del_init(&pc->lru);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001022}
1023
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001024void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001025{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001026 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001027}
1028
Minchan Kim3f58a822011-03-22 16:32:53 -07001029/*
1030 * Writeback is about to end against a page which has been marked for immediate
1031 * reclaim. If it still appears to be reclaimable, move it to the tail of the
1032 * inactive list.
1033 */
1034void mem_cgroup_rotate_reclaimable_page(struct page *page)
1035{
1036 struct mem_cgroup_per_zone *mz;
1037 struct page_cgroup *pc;
1038 enum lru_list lru = page_lru(page);
1039
1040 if (mem_cgroup_disabled())
1041 return;
1042
1043 pc = lookup_page_cgroup(page);
1044 /* unused or root page is not rotated. */
1045 if (!PageCgroupUsed(pc))
1046 return;
1047 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
1048 smp_rmb();
1049 if (mem_cgroup_is_root(pc->mem_cgroup))
1050 return;
Johannes Weiner97a6c372011-03-23 16:42:27 -07001051 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
Minchan Kim3f58a822011-03-22 16:32:53 -07001052 list_move_tail(&pc->lru, &mz->lists[lru]);
1053}
1054
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001055void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -08001056{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001057 struct mem_cgroup_per_zone *mz;
1058 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001059
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001060 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -07001061 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -07001062
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001063 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -07001064 /* unused or root page is not rotated. */
Johannes Weiner713735b2011-01-20 14:44:31 -08001065 if (!PageCgroupUsed(pc))
1066 return;
1067 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
1068 smp_rmb();
1069 if (mem_cgroup_is_root(pc->mem_cgroup))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001070 return;
Johannes Weiner97a6c372011-03-23 16:42:27 -07001071 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001072 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -08001073}
1074
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001075void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
1076{
1077 struct page_cgroup *pc;
1078 struct mem_cgroup_per_zone *mz;
1079
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001080 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001081 return;
1082 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -07001083 VM_BUG_ON(PageCgroupAcctLRU(pc));
Johannes Weinera61ed3c2011-11-02 13:38:29 -07001084 /*
1085 * putback: charge:
1086 * SetPageLRU SetPageCgroupUsed
1087 * smp_mb smp_mb
1088 * PageCgroupUsed && add to memcg LRU PageLRU && add to memcg LRU
1089 *
1090 * Ensure that one of the two sides adds the page to the memcg
1091 * LRU during a race.
1092 */
1093 smp_mb();
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001094 if (!PageCgroupUsed(pc))
1095 return;
Johannes Weiner713735b2011-01-20 14:44:31 -08001096 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
1097 smp_rmb();
Johannes Weiner97a6c372011-03-23 16:42:27 -07001098 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08001099 /* huge page split is done under lru_lock. so, we have no races. */
1100 MEM_CGROUP_ZSTAT(mz, lru) += 1 << compound_order(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -07001101 SetPageCgroupAcctLRU(pc);
1102 if (mem_cgroup_is_root(pc->mem_cgroup))
1103 return;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001104 list_add(&pc->lru, &mz->lists[lru]);
1105}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001106
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001107/*
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001108 * At handling SwapCache and other FUSE stuff, pc->mem_cgroup may be changed
1109 * while it's linked to lru because the page may be reused after it's fully
1110 * uncharged. To handle that, unlink page_cgroup from LRU when charge it again.
1111 * It's done under lock_page and expected that zone->lru_lock isnever held.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001112 */
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001113static void mem_cgroup_lru_del_before_commit(struct page *page)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001114{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001115 unsigned long flags;
1116 struct zone *zone = page_zone(page);
1117 struct page_cgroup *pc = lookup_page_cgroup(page);
1118
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001119 /*
1120 * Doing this check without taking ->lru_lock seems wrong but this
1121 * is safe. Because if page_cgroup's USED bit is unset, the page
1122 * will not be added to any memcg's LRU. If page_cgroup's USED bit is
1123 * set, the commit after this will fail, anyway.
1124 * This all charge/uncharge is done under some mutual execustion.
1125 * So, we don't need to taking care of changes in USED bit.
1126 */
1127 if (likely(!PageLRU(page)))
1128 return;
1129
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001130 spin_lock_irqsave(&zone->lru_lock, flags);
1131 /*
1132 * Forget old LRU when this page_cgroup is *not* used. This Used bit
1133 * is guarded by lock_page() because the page is SwapCache.
1134 */
1135 if (!PageCgroupUsed(pc))
1136 mem_cgroup_del_lru_list(page, page_lru(page));
1137 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001138}
1139
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001140static void mem_cgroup_lru_add_after_commit(struct page *page)
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001141{
1142 unsigned long flags;
1143 struct zone *zone = page_zone(page);
1144 struct page_cgroup *pc = lookup_page_cgroup(page);
Johannes Weinera61ed3c2011-11-02 13:38:29 -07001145 /*
1146 * putback: charge:
1147 * SetPageLRU SetPageCgroupUsed
1148 * smp_mb smp_mb
1149 * PageCgroupUsed && add to memcg LRU PageLRU && add to memcg LRU
1150 *
1151 * Ensure that one of the two sides adds the page to the memcg
1152 * LRU during a race.
1153 */
1154 smp_mb();
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001155 /* taking care of that the page is added to LRU while we commit it */
1156 if (likely(!PageLRU(page)))
1157 return;
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001158 spin_lock_irqsave(&zone->lru_lock, flags);
1159 /* link when the page is linked to LRU but page_cgroup isn't */
Balbir Singh4b3bde42009-09-23 15:56:32 -07001160 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001161 mem_cgroup_add_lru_list(page, page_lru(page));
1162 spin_unlock_irqrestore(&zone->lru_lock, flags);
1163}
1164
1165
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001166void mem_cgroup_move_lists(struct page *page,
1167 enum lru_list from, enum lru_list to)
1168{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001169 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001170 return;
1171 mem_cgroup_del_lru_list(page, from);
1172 mem_cgroup_add_lru_list(page, to);
1173}
1174
Michal Hocko3e920412011-07-26 16:08:29 -07001175/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001176 * Checks whether given mem is same or in the root_mem_cgroup's
Michal Hocko3e920412011-07-26 16:08:29 -07001177 * hierarchy subtree
1178 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001179static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1180 struct mem_cgroup *memcg)
Michal Hocko3e920412011-07-26 16:08:29 -07001181{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001182 if (root_memcg != memcg) {
1183 return (root_memcg->use_hierarchy &&
1184 css_is_ancestor(&memcg->css, &root_memcg->css));
Michal Hocko3e920412011-07-26 16:08:29 -07001185 }
1186
1187 return true;
1188}
1189
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001190int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -08001191{
1192 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001193 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001194 struct task_struct *p;
David Rientjes4c4a2212008-02-07 00:14:06 -08001195
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001196 p = find_lock_task_mm(task);
1197 if (!p)
1198 return 0;
1199 curr = try_get_mem_cgroup_from_mm(p->mm);
1200 task_unlock(p);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001201 if (!curr)
1202 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001203 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001204 * We should check use_hierarchy of "memcg" not "curr". Because checking
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001205 * use_hierarchy of "curr" here make this function true if hierarchy is
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001206 * enabled in "curr" and "curr" is a child of "memcg" in *cgroup*
1207 * hierarchy(even if use_hierarchy is disabled in "memcg").
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001208 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001209 ret = mem_cgroup_same_or_subtree(memcg, curr);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001210 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001211 return ret;
1212}
1213
Johannes Weiner9b272972011-11-02 13:38:23 -07001214int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg, struct zone *zone)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001215{
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001216 unsigned long inactive_ratio;
Johannes Weiner9b272972011-11-02 13:38:23 -07001217 int nid = zone_to_nid(zone);
1218 int zid = zone_idx(zone);
1219 unsigned long inactive;
1220 unsigned long active;
1221 unsigned long gb;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001222
Johannes Weiner9b272972011-11-02 13:38:23 -07001223 inactive = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid,
1224 BIT(LRU_INACTIVE_ANON));
1225 active = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid,
1226 BIT(LRU_ACTIVE_ANON));
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001227
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001228 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1229 if (gb)
1230 inactive_ratio = int_sqrt(10 * gb);
1231 else
1232 inactive_ratio = 1;
1233
Johannes Weiner9b272972011-11-02 13:38:23 -07001234 return inactive * inactive_ratio < active;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001235}
1236
Johannes Weiner9b272972011-11-02 13:38:23 -07001237int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg, struct zone *zone)
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001238{
1239 unsigned long active;
1240 unsigned long inactive;
Johannes Weiner9b272972011-11-02 13:38:23 -07001241 int zid = zone_idx(zone);
1242 int nid = zone_to_nid(zone);
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001243
Johannes Weiner9b272972011-11-02 13:38:23 -07001244 inactive = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid,
1245 BIT(LRU_INACTIVE_FILE));
1246 active = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid,
1247 BIT(LRU_ACTIVE_FILE));
Rik van Riel56e49d22009-06-16 15:32:28 -07001248
1249 return (active > inactive);
1250}
1251
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001252struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
1253 struct zone *zone)
1254{
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -07001255 int nid = zone_to_nid(zone);
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001256 int zid = zone_idx(zone);
1257 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
1258
1259 return &mz->reclaim_stat;
1260}
1261
1262struct zone_reclaim_stat *
1263mem_cgroup_get_reclaim_stat_from_page(struct page *page)
1264{
1265 struct page_cgroup *pc;
1266 struct mem_cgroup_per_zone *mz;
1267
1268 if (mem_cgroup_disabled())
1269 return NULL;
1270
1271 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -08001272 if (!PageCgroupUsed(pc))
1273 return NULL;
Johannes Weiner713735b2011-01-20 14:44:31 -08001274 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
1275 smp_rmb();
Johannes Weiner97a6c372011-03-23 16:42:27 -07001276 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001277 return &mz->reclaim_stat;
1278}
1279
Balbir Singh66e17072008-02-07 00:13:56 -08001280unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
1281 struct list_head *dst,
1282 unsigned long *scanned, int order,
Minchan Kim4356f212011-10-31 17:06:47 -07001283 isolate_mode_t mode,
1284 struct zone *z,
Balbir Singh66e17072008-02-07 00:13:56 -08001285 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001286 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -08001287{
1288 unsigned long nr_taken = 0;
1289 struct page *page;
1290 unsigned long scan;
1291 LIST_HEAD(pc_list);
1292 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001293 struct page_cgroup *pc, *tmp;
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -07001294 int nid = zone_to_nid(z);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001295 int zid = zone_idx(z);
1296 struct mem_cgroup_per_zone *mz;
Johannes Weinerb7c46d12009-09-21 17:02:56 -07001297 int lru = LRU_FILE * file + active;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001298 int ret;
Balbir Singh66e17072008-02-07 00:13:56 -08001299
Balbir Singhcf475ad2008-04-29 01:00:16 -07001300 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001301 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001302 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -08001303
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001304 scan = 0;
1305 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -08001306 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001307 break;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001308
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001309 if (unlikely(!PageCgroupUsed(pc)))
1310 continue;
Johannes Weiner5564e882011-03-23 16:42:29 -07001311
Johannes Weiner6b3ae582011-03-23 16:42:30 -07001312 page = lookup_cgroup_page(pc);
Johannes Weiner5564e882011-03-23 16:42:29 -07001313
Hugh Dickins436c65412008-02-07 00:14:12 -08001314 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001315 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001316
Hugh Dickins436c65412008-02-07 00:14:12 -08001317 scan++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001318 ret = __isolate_lru_page(page, mode, file);
1319 switch (ret) {
1320 case 0:
Balbir Singh66e17072008-02-07 00:13:56 -08001321 list_move(&page->lru, dst);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001322 mem_cgroup_del_lru(page);
Rik van Riel2c888cf2011-01-13 15:47:13 -08001323 nr_taken += hpage_nr_pages(page);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001324 break;
1325 case -EBUSY:
1326 /* we don't affect global LRU but rotate in our LRU */
1327 mem_cgroup_rotate_lru_list(page, page_lru(page));
1328 break;
1329 default:
1330 break;
Balbir Singh66e17072008-02-07 00:13:56 -08001331 }
1332 }
1333
Balbir Singh66e17072008-02-07 00:13:56 -08001334 *scanned = scan;
KOSAKI Motohirocc8e9702010-08-09 17:19:57 -07001335
1336 trace_mm_vmscan_memcg_isolate(0, nr_to_scan, scan, nr_taken,
1337 0, 0, 0, mode);
1338
Balbir Singh66e17072008-02-07 00:13:56 -08001339 return nr_taken;
1340}
1341
Balbir Singh6d61ef42009-01-07 18:08:06 -08001342#define mem_cgroup_from_res_counter(counter, member) \
1343 container_of(counter, struct mem_cgroup, member)
1344
Johannes Weiner19942822011-02-01 15:52:43 -08001345/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001346 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
1347 * @mem: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001348 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001349 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001350 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001351 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001352static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001353{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001354 unsigned long long margin;
1355
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001356 margin = res_counter_margin(&memcg->res);
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001357 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001358 margin = min(margin, res_counter_margin(&memcg->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001359 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001360}
1361
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001362int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001363{
1364 struct cgroup *cgrp = memcg->css.cgroup;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001365
1366 /* root ? */
1367 if (cgrp->parent == NULL)
1368 return vm_swappiness;
1369
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001370 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001371}
1372
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001373static void mem_cgroup_start_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001374{
1375 int cpu;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001376
1377 get_online_cpus();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001378 spin_lock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001379 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001380 per_cpu(memcg->stat->count[MEM_CGROUP_ON_MOVE], cpu) += 1;
1381 memcg->nocpu_base.count[MEM_CGROUP_ON_MOVE] += 1;
1382 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001383 put_online_cpus();
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001384
1385 synchronize_rcu();
1386}
1387
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001388static void mem_cgroup_end_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001389{
1390 int cpu;
1391
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001392 if (!memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001393 return;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001394 get_online_cpus();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001395 spin_lock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001396 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001397 per_cpu(memcg->stat->count[MEM_CGROUP_ON_MOVE], cpu) -= 1;
1398 memcg->nocpu_base.count[MEM_CGROUP_ON_MOVE] -= 1;
1399 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001400 put_online_cpus();
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001401}
1402/*
1403 * 2 routines for checking "mem" is under move_account() or not.
1404 *
1405 * mem_cgroup_stealed() - checking a cgroup is mc.from or not. This is used
1406 * for avoiding race in accounting. If true,
1407 * pc->mem_cgroup may be overwritten.
1408 *
1409 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1410 * under hierarchy of moving cgroups. This is for
1411 * waiting at hith-memory prressure caused by "move".
1412 */
1413
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001414static bool mem_cgroup_stealed(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001415{
1416 VM_BUG_ON(!rcu_read_lock_held());
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001417 return this_cpu_read(memcg->stat->count[MEM_CGROUP_ON_MOVE]) > 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001418}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001419
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001420static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001421{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001422 struct mem_cgroup *from;
1423 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001424 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001425 /*
1426 * Unlike task_move routines, we access mc.to, mc.from not under
1427 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1428 */
1429 spin_lock(&mc.lock);
1430 from = mc.from;
1431 to = mc.to;
1432 if (!from)
1433 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001434
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001435 ret = mem_cgroup_same_or_subtree(memcg, from)
1436 || mem_cgroup_same_or_subtree(memcg, to);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001437unlock:
1438 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001439 return ret;
1440}
1441
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001442static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001443{
1444 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001445 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001446 DEFINE_WAIT(wait);
1447 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1448 /* moving charge context might have finished. */
1449 if (mc.moving_task)
1450 schedule();
1451 finish_wait(&mc.waitq, &wait);
1452 return true;
1453 }
1454 }
1455 return false;
1456}
1457
Balbir Singhe2224322009-04-02 16:57:39 -07001458/**
Kirill A. Shutemov6a6135b2010-03-10 15:22:25 -08001459 * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode.
Balbir Singhe2224322009-04-02 16:57:39 -07001460 * @memcg: The memory cgroup that went over limit
1461 * @p: Task that is going to be killed
1462 *
1463 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1464 * enabled
1465 */
1466void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1467{
1468 struct cgroup *task_cgrp;
1469 struct cgroup *mem_cgrp;
1470 /*
1471 * Need a buffer in BSS, can't rely on allocations. The code relies
1472 * on the assumption that OOM is serialized for memory controller.
1473 * If this assumption is broken, revisit this code.
1474 */
1475 static char memcg_name[PATH_MAX];
1476 int ret;
1477
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001478 if (!memcg || !p)
Balbir Singhe2224322009-04-02 16:57:39 -07001479 return;
1480
1481
1482 rcu_read_lock();
1483
1484 mem_cgrp = memcg->css.cgroup;
1485 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1486
1487 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1488 if (ret < 0) {
1489 /*
1490 * Unfortunately, we are unable to convert to a useful name
1491 * But we'll still print out the usage information
1492 */
1493 rcu_read_unlock();
1494 goto done;
1495 }
1496 rcu_read_unlock();
1497
1498 printk(KERN_INFO "Task in %s killed", memcg_name);
1499
1500 rcu_read_lock();
1501 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1502 if (ret < 0) {
1503 rcu_read_unlock();
1504 goto done;
1505 }
1506 rcu_read_unlock();
1507
1508 /*
1509 * Continues from above, so we don't need an KERN_ level
1510 */
1511 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1512done:
1513
1514 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1515 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1516 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1517 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1518 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1519 "failcnt %llu\n",
1520 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1521 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1522 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1523}
1524
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001525/*
1526 * This function returns the number of memcg under hierarchy tree. Returns
1527 * 1(self count) if no children.
1528 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001529static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001530{
1531 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001532 struct mem_cgroup *iter;
1533
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001534 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001535 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001536 return num;
1537}
1538
Balbir Singh6d61ef42009-01-07 18:08:06 -08001539/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001540 * Return the memory (and swap, if configured) limit for a memcg.
1541 */
1542u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
1543{
1544 u64 limit;
1545 u64 memsw;
1546
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001547 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
1548 limit += total_swap_pages << PAGE_SHIFT;
1549
David Rientjesa63d83f2010-08-09 17:19:46 -07001550 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1551 /*
1552 * If memsw is finite and limits the amount of swap space available
1553 * to this memcg, return that limit.
1554 */
1555 return min(limit, memsw);
1556}
1557
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001558/**
1559 * test_mem_cgroup_node_reclaimable
1560 * @mem: the target memcg
1561 * @nid: the node ID to be checked.
1562 * @noswap : specify true here if the user wants flle only information.
1563 *
1564 * This function returns whether the specified memcg contains any
1565 * reclaimable pages on a node. Returns true if there are any reclaimable
1566 * pages in the node.
1567 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001568static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001569 int nid, bool noswap)
1570{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001571 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001572 return true;
1573 if (noswap || !total_swap_pages)
1574 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001575 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001576 return true;
1577 return false;
1578
1579}
Ying Han889976d2011-05-26 16:25:33 -07001580#if MAX_NUMNODES > 1
1581
1582/*
1583 * Always updating the nodemask is not very good - even if we have an empty
1584 * list or the wrong list here, we can start from some node and traverse all
1585 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1586 *
1587 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001588static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001589{
1590 int nid;
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001591 /*
1592 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1593 * pagein/pageout changes since the last update.
1594 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001595 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001596 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001597 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001598 return;
1599
Ying Han889976d2011-05-26 16:25:33 -07001600 /* make a nodemask where this memcg uses memory from */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001601 memcg->scan_nodes = node_states[N_HIGH_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001602
1603 for_each_node_mask(nid, node_states[N_HIGH_MEMORY]) {
1604
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001605 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1606 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001607 }
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001608
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001609 atomic_set(&memcg->numainfo_events, 0);
1610 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001611}
1612
1613/*
1614 * Selecting a node where we start reclaim from. Because what we need is just
1615 * reducing usage counter, start from anywhere is O,K. Considering
1616 * memory reclaim from current node, there are pros. and cons.
1617 *
1618 * Freeing memory from current node means freeing memory from a node which
1619 * we'll use or we've used. So, it may make LRU bad. And if several threads
1620 * hit limits, it will see a contention on a node. But freeing from remote
1621 * node means more costs for memory reclaim because of memory latency.
1622 *
1623 * Now, we use round-robin. Better algorithm is welcomed.
1624 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001625int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001626{
1627 int node;
1628
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001629 mem_cgroup_may_update_nodemask(memcg);
1630 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001631
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001632 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001633 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001634 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001635 /*
1636 * We call this when we hit limit, not when pages are added to LRU.
1637 * No LRU may hold pages because all pages are UNEVICTABLE or
1638 * memcg is too small and all pages are not on LRU. In that case,
1639 * we use curret node.
1640 */
1641 if (unlikely(node == MAX_NUMNODES))
1642 node = numa_node_id();
1643
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001644 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001645 return node;
1646}
1647
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001648/*
1649 * Check all nodes whether it contains reclaimable pages or not.
1650 * For quick scan, we make use of scan_nodes. This will allow us to skip
1651 * unused nodes. But scan_nodes is lazily updated and may not cotain
1652 * enough new information. We need to do double check.
1653 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001654bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001655{
1656 int nid;
1657
1658 /*
1659 * quick check...making use of scan_node.
1660 * We can skip unused nodes.
1661 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001662 if (!nodes_empty(memcg->scan_nodes)) {
1663 for (nid = first_node(memcg->scan_nodes);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001664 nid < MAX_NUMNODES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001665 nid = next_node(nid, memcg->scan_nodes)) {
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001666
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001667 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001668 return true;
1669 }
1670 }
1671 /*
1672 * Check rest of nodes.
1673 */
1674 for_each_node_state(nid, N_HIGH_MEMORY) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001675 if (node_isset(nid, memcg->scan_nodes))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001676 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001677 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001678 return true;
1679 }
1680 return false;
1681}
1682
Ying Han889976d2011-05-26 16:25:33 -07001683#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001684int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001685{
1686 return 0;
1687}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001688
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001689bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001690{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001691 return test_mem_cgroup_node_reclaimable(memcg, 0, noswap);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001692}
Ying Han889976d2011-05-26 16:25:33 -07001693#endif
1694
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001695/*
1696 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1697 * we reclaimed from, so that we don't end up penalizing one child extensively
1698 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001699 *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001700 * root_memcg is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001701 *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001702 * We give up and return to the caller when we visit root_memcg twice.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001703 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001704 *
1705 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001706 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001707static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_memcg,
Balbir Singh4e416952009-09-23 15:56:39 -07001708 struct zone *zone,
Balbir Singh75822b42009-09-23 15:56:38 -07001709 gfp_t gfp_mask,
Ying Han0ae5e892011-05-26 16:25:25 -07001710 unsigned long reclaim_options,
1711 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001712{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001713 struct mem_cgroup *victim = NULL;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001714 int ret, total = 0;
1715 int loop = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001716 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1717 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
Balbir Singh4e416952009-09-23 15:56:39 -07001718 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001719 unsigned long excess;
Johannes Weiner185efc02011-09-14 16:21:58 -07001720 unsigned long nr_scanned;
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001721 struct mem_cgroup_reclaim_cookie reclaim = {
1722 .zone = zone,
1723 .priority = 0,
1724 };
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001725
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001726 excess = res_counter_soft_limit_excess(&root_memcg->res) >> PAGE_SHIFT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001727
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001728 /* If memsw_is_minimum==1, swap-out is of-no-use. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001729 if (!check_soft && !shrink && root_memcg->memsw_is_minimum)
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001730 noswap = true;
1731
Balbir Singh4e416952009-09-23 15:56:39 -07001732 while (1) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001733 victim = mem_cgroup_iter(root_memcg, victim, &reclaim);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001734 if (!victim) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001735 loop++;
KAMEZAWA Hiroyukifbc29a22011-06-15 15:08:46 -07001736 /*
1737 * We are not draining per cpu cached charges during
1738 * soft limit reclaim because global reclaim doesn't
1739 * care about charges. It tries to free some memory and
1740 * charges will not give any.
1741 */
1742 if (!check_soft && loop >= 1)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001743 drain_all_stock_async(root_memcg);
Balbir Singh4e416952009-09-23 15:56:39 -07001744 if (loop >= 2) {
1745 /*
1746 * If we have not been able to reclaim
1747 * anything, it might because there are
1748 * no reclaimable pages under this hierarchy
1749 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001750 if (!check_soft || !total)
Balbir Singh4e416952009-09-23 15:56:39 -07001751 break;
Balbir Singh4e416952009-09-23 15:56:39 -07001752 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001753 * We want to do more targeted reclaim.
Balbir Singh4e416952009-09-23 15:56:39 -07001754 * excess >> 2 is not to excessive so as to
1755 * reclaim too much, nor too less that we keep
1756 * coming back to reclaim from this cgroup
1757 */
1758 if (total >= (excess >> 2) ||
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001759 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS))
Balbir Singh4e416952009-09-23 15:56:39 -07001760 break;
Balbir Singh4e416952009-09-23 15:56:39 -07001761 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001762 continue;
Balbir Singh4e416952009-09-23 15:56:39 -07001763 }
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001764 if (!mem_cgroup_reclaimable(victim, noswap)) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001765 /* this cgroup's local usage == 0 */
Balbir Singh6d61ef42009-01-07 18:08:06 -08001766 continue;
1767 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001768 /* we use swappiness of local cgroup */
Ying Han0ae5e892011-05-26 16:25:25 -07001769 if (check_soft) {
Balbir Singh4e416952009-09-23 15:56:39 -07001770 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
Johannes Weiner185efc02011-09-14 16:21:58 -07001771 noswap, zone, &nr_scanned);
1772 *total_scanned += nr_scanned;
Ying Han0ae5e892011-05-26 16:25:25 -07001773 } else
Balbir Singh4e416952009-09-23 15:56:39 -07001774 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
Johannes Weiner185efc02011-09-14 16:21:58 -07001775 noswap);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001776 total += ret;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001777 /*
1778 * At shrinking usage, we can't check we should stop here or
1779 * reclaim more. It's depends on callers. last_scanned_child
1780 * will work enough for keeping fairness under tree.
1781 */
1782 if (shrink)
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001783 break;
Balbir Singh4e416952009-09-23 15:56:39 -07001784 if (check_soft) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001785 if (!res_counter_soft_limit_excess(&root_memcg->res))
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001786 break;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001787 } else if (mem_cgroup_margin(root_memcg))
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001788 break;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001789 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001790 mem_cgroup_iter_break(root_memcg, victim);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001791 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001792}
1793
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001794/*
1795 * Check OOM-Killer is already running under our hierarchy.
1796 * If someone is running, return false.
Michal Hocko1af8efe2011-07-26 16:08:24 -07001797 * Has to be called with memcg_oom_lock
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001798 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001799static bool mem_cgroup_oom_lock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001800{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001801 struct mem_cgroup *iter, *failed = NULL;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001802
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001803 for_each_mem_cgroup_tree(iter, memcg) {
Johannes Weiner23751be2011-08-25 15:59:16 -07001804 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001805 /*
1806 * this subtree of our hierarchy is already locked
1807 * so we cannot give a lock.
1808 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07001809 failed = iter;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001810 mem_cgroup_iter_break(memcg, iter);
1811 break;
Johannes Weiner23751be2011-08-25 15:59:16 -07001812 } else
1813 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001814 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001815
Michal Hocko79dfdac2011-07-26 16:08:23 -07001816 if (!failed)
Johannes Weiner23751be2011-08-25 15:59:16 -07001817 return true;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001818
1819 /*
1820 * OK, we failed to lock the whole subtree so we have to clean up
1821 * what we set up to the failing subtree
1822 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001823 for_each_mem_cgroup_tree(iter, memcg) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001824 if (iter == failed) {
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001825 mem_cgroup_iter_break(memcg, iter);
1826 break;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001827 }
1828 iter->oom_lock = false;
1829 }
Johannes Weiner23751be2011-08-25 15:59:16 -07001830 return false;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001831}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001832
Michal Hocko79dfdac2011-07-26 16:08:23 -07001833/*
Michal Hocko1af8efe2011-07-26 16:08:24 -07001834 * Has to be called with memcg_oom_lock
Michal Hocko79dfdac2011-07-26 16:08:23 -07001835 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001836static int mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001837{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001838 struct mem_cgroup *iter;
1839
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001840 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001841 iter->oom_lock = false;
1842 return 0;
1843}
1844
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001845static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001846{
1847 struct mem_cgroup *iter;
1848
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001849 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001850 atomic_inc(&iter->under_oom);
1851}
1852
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001853static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001854{
1855 struct mem_cgroup *iter;
1856
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001857 /*
1858 * When a new child is created while the hierarchy is under oom,
1859 * mem_cgroup_oom_lock() may not be called. We have to use
1860 * atomic_add_unless() here.
1861 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001862 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001863 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001864}
1865
Michal Hocko1af8efe2011-07-26 16:08:24 -07001866static DEFINE_SPINLOCK(memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001867static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1868
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001869struct oom_wait_info {
1870 struct mem_cgroup *mem;
1871 wait_queue_t wait;
1872};
1873
1874static int memcg_oom_wake_function(wait_queue_t *wait,
1875 unsigned mode, int sync, void *arg)
1876{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001877 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg,
1878 *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001879 struct oom_wait_info *oom_wait_info;
1880
1881 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001882 oom_wait_memcg = oom_wait_info->mem;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001883
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001884 /*
1885 * Both of oom_wait_info->mem and wake_mem are stable under us.
1886 * Then we can use css_is_ancestor without taking care of RCU.
1887 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001888 if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg)
1889 && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001890 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001891 return autoremove_wake_function(wait, mode, sync, arg);
1892}
1893
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001894static void memcg_wakeup_oom(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001895{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001896 /* for filtering, pass "memcg" as argument. */
1897 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001898}
1899
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001900static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001901{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001902 if (memcg && atomic_read(&memcg->under_oom))
1903 memcg_wakeup_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001904}
1905
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001906/*
1907 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
1908 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001909bool mem_cgroup_handle_oom(struct mem_cgroup *memcg, gfp_t mask)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001910{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001911 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001912 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001913
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001914 owait.mem = memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001915 owait.wait.flags = 0;
1916 owait.wait.func = memcg_oom_wake_function;
1917 owait.wait.private = current;
1918 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001919 need_to_kill = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001920 mem_cgroup_mark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001921
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001922 /* At first, try to OOM lock hierarchy under memcg.*/
Michal Hocko1af8efe2011-07-26 16:08:24 -07001923 spin_lock(&memcg_oom_lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001924 locked = mem_cgroup_oom_lock(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001925 /*
1926 * Even if signal_pending(), we can't quit charge() loop without
1927 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
1928 * under OOM is always welcomed, use TASK_KILLABLE here.
1929 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001930 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001931 if (!locked || memcg->oom_kill_disable)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001932 need_to_kill = false;
1933 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001934 mem_cgroup_oom_notify(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07001935 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001936
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001937 if (need_to_kill) {
1938 finish_wait(&memcg_oom_waitq, &owait.wait);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001939 mem_cgroup_out_of_memory(memcg, mask);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001940 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001941 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001942 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001943 }
Michal Hocko1af8efe2011-07-26 16:08:24 -07001944 spin_lock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001945 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001946 mem_cgroup_oom_unlock(memcg);
1947 memcg_wakeup_oom(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07001948 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001949
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001950 mem_cgroup_unmark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001951
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001952 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
1953 return false;
1954 /* Give chance to dying process */
KAMEZAWA Hiroyuki715a5ee2011-11-02 13:38:18 -07001955 schedule_timeout_uninterruptible(1);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001956 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001957}
1958
Balbir Singhd69b0422009-06-17 16:26:34 -07001959/*
1960 * Currently used to update mapped file statistics, but the routine can be
1961 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001962 *
1963 * Notes: Race condition
1964 *
1965 * We usually use page_cgroup_lock() for accessing page_cgroup member but
1966 * it tends to be costly. But considering some conditions, we doesn't need
1967 * to do so _always_.
1968 *
1969 * Considering "charge", lock_page_cgroup() is not required because all
1970 * file-stat operations happen after a page is attached to radix-tree. There
1971 * are no race with "charge".
1972 *
1973 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
1974 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
1975 * if there are race with "uncharge". Statistics itself is properly handled
1976 * by flags.
1977 *
1978 * Considering "move", this is an only case we see a race. To make the race
1979 * small, we check MEM_CGROUP_ON_MOVE percpu value and detect there are
1980 * possibility of race condition. If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07001981 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001982
Greg Thelen2a7106f2011-01-13 15:47:37 -08001983void mem_cgroup_update_page_stat(struct page *page,
1984 enum mem_cgroup_page_stat_item idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07001985{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001986 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001987 struct page_cgroup *pc = lookup_page_cgroup(page);
1988 bool need_unlock = false;
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08001989 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07001990
Balbir Singhd69b0422009-06-17 16:26:34 -07001991 if (unlikely(!pc))
1992 return;
1993
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001994 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001995 memcg = pc->mem_cgroup;
1996 if (unlikely(!memcg || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001997 goto out;
1998 /* pc->mem_cgroup is unstable ? */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001999 if (unlikely(mem_cgroup_stealed(memcg)) || PageTransHuge(page)) {
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002000 /* take a lock against to access pc->mem_cgroup */
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08002001 move_lock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002002 need_unlock = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002003 memcg = pc->mem_cgroup;
2004 if (!memcg || !PageCgroupUsed(pc))
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002005 goto out;
2006 }
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002007
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002008 switch (idx) {
Greg Thelen2a7106f2011-01-13 15:47:37 -08002009 case MEMCG_NR_FILE_MAPPED:
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002010 if (val > 0)
2011 SetPageCgroupFileMapped(pc);
2012 else if (!page_mapped(page))
KAMEZAWA Hiroyuki0c270f82010-10-27 15:33:39 -07002013 ClearPageCgroupFileMapped(pc);
Greg Thelen2a7106f2011-01-13 15:47:37 -08002014 idx = MEM_CGROUP_STAT_FILE_MAPPED;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002015 break;
2016 default:
2017 BUG();
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002018 }
Balbir Singhd69b0422009-06-17 16:26:34 -07002019
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002020 this_cpu_add(memcg->stat->count[idx], val);
Greg Thelen2a7106f2011-01-13 15:47:37 -08002021
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002022out:
2023 if (unlikely(need_unlock))
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08002024 move_unlock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002025 rcu_read_unlock();
2026 return;
Balbir Singhd69b0422009-06-17 16:26:34 -07002027}
Greg Thelen2a7106f2011-01-13 15:47:37 -08002028EXPORT_SYMBOL(mem_cgroup_update_page_stat);
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002029
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002030/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002031 * size of first charge trial. "32" comes from vmscan.c's magic value.
2032 * TODO: maybe necessary to use big numbers in big irons.
2033 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002034#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002035struct memcg_stock_pcp {
2036 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002037 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002038 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002039 unsigned long flags;
2040#define FLUSHING_CACHED_CHARGE (0)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002041};
2042static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002043static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002044
2045/*
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002046 * Try to consume stocked charge on this cpu. If success, one page is consumed
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002047 * from local stock and true is returned. If the stock is 0 or charges from a
2048 * cgroup which is not current target, returns false. This stock will be
2049 * refilled.
2050 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002051static bool consume_stock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002052{
2053 struct memcg_stock_pcp *stock;
2054 bool ret = true;
2055
2056 stock = &get_cpu_var(memcg_stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002057 if (memcg == stock->cached && stock->nr_pages)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002058 stock->nr_pages--;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002059 else /* need to call res_counter_charge */
2060 ret = false;
2061 put_cpu_var(memcg_stock);
2062 return ret;
2063}
2064
2065/*
2066 * Returns stocks cached in percpu to res_counter and reset cached information.
2067 */
2068static void drain_stock(struct memcg_stock_pcp *stock)
2069{
2070 struct mem_cgroup *old = stock->cached;
2071
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002072 if (stock->nr_pages) {
2073 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2074
2075 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002076 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002077 res_counter_uncharge(&old->memsw, bytes);
2078 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002079 }
2080 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002081}
2082
2083/*
2084 * This must be called under preempt disabled or must be called by
2085 * a thread which is pinned to local cpu.
2086 */
2087static void drain_local_stock(struct work_struct *dummy)
2088{
2089 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
2090 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002091 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002092}
2093
2094/*
2095 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002096 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002097 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002098static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002099{
2100 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2101
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002102 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002103 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002104 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002105 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002106 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002107 put_cpu_var(memcg_stock);
2108}
2109
2110/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002111 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Michal Hockod38144b2011-07-26 16:08:28 -07002112 * of the hierarchy under it. sync flag says whether we should block
2113 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002114 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002115static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002116{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002117 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002118
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002119 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002120 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002121 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002122 for_each_online_cpu(cpu) {
2123 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002124 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002125
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002126 memcg = stock->cached;
2127 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002128 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002129 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002130 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002131 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2132 if (cpu == curcpu)
2133 drain_local_stock(&stock->work);
2134 else
2135 schedule_work_on(cpu, &stock->work);
2136 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002137 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002138 put_cpu();
Michal Hockod38144b2011-07-26 16:08:28 -07002139
2140 if (!sync)
2141 goto out;
2142
2143 for_each_online_cpu(cpu) {
2144 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002145 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002146 flush_work(&stock->work);
2147 }
2148out:
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002149 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002150}
2151
2152/*
2153 * Tries to drain stocked charges in other cpus. This function is asynchronous
2154 * and just put a work per cpu for draining localy on each cpu. Caller can
2155 * expects some charges will be back to res_counter later but cannot wait for
2156 * it.
2157 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002158static void drain_all_stock_async(struct mem_cgroup *root_memcg)
Michal Hockod38144b2011-07-26 16:08:28 -07002159{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002160 /*
2161 * If someone calls draining, avoid adding more kworker runs.
2162 */
2163 if (!mutex_trylock(&percpu_charge_mutex))
2164 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002165 drain_all_stock(root_memcg, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002166 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002167}
2168
2169/* This is a synchronous drain interface. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002170static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002171{
2172 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002173 mutex_lock(&percpu_charge_mutex);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002174 drain_all_stock(root_memcg, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002175 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002176}
2177
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002178/*
2179 * This function drains percpu counter value from DEAD cpu and
2180 * move it to local cpu. Note that this function can be preempted.
2181 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002182static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002183{
2184 int i;
2185
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002186 spin_lock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002187 for (i = 0; i < MEM_CGROUP_STAT_DATA; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002188 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002189
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002190 per_cpu(memcg->stat->count[i], cpu) = 0;
2191 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002192 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002193 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002194 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002195
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002196 per_cpu(memcg->stat->events[i], cpu) = 0;
2197 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002198 }
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002199 /* need to clear ON_MOVE value, works as a kind of lock. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002200 per_cpu(memcg->stat->count[MEM_CGROUP_ON_MOVE], cpu) = 0;
2201 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002202}
2203
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002204static void synchronize_mem_cgroup_on_move(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002205{
2206 int idx = MEM_CGROUP_ON_MOVE;
2207
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002208 spin_lock(&memcg->pcp_counter_lock);
2209 per_cpu(memcg->stat->count[idx], cpu) = memcg->nocpu_base.count[idx];
2210 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002211}
2212
2213static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002214 unsigned long action,
2215 void *hcpu)
2216{
2217 int cpu = (unsigned long)hcpu;
2218 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002219 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002220
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002221 if ((action == CPU_ONLINE)) {
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002222 for_each_mem_cgroup(iter)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002223 synchronize_mem_cgroup_on_move(iter, cpu);
2224 return NOTIFY_OK;
2225 }
2226
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002227 if ((action != CPU_DEAD) || action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002228 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002229
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002230 for_each_mem_cgroup(iter)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002231 mem_cgroup_drain_pcp_counter(iter, cpu);
2232
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002233 stock = &per_cpu(memcg_stock, cpu);
2234 drain_stock(stock);
2235 return NOTIFY_OK;
2236}
2237
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002238
2239/* See __mem_cgroup_try_charge() for details */
2240enum {
2241 CHARGE_OK, /* success */
2242 CHARGE_RETRY, /* need to retry but retry is not bad */
2243 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2244 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
2245 CHARGE_OOM_DIE, /* the current is killed because of OOM */
2246};
2247
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002248static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002249 unsigned int nr_pages, bool oom_check)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002250{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002251 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002252 struct mem_cgroup *mem_over_limit;
2253 struct res_counter *fail_res;
2254 unsigned long flags = 0;
2255 int ret;
2256
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002257 ret = res_counter_charge(&memcg->res, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002258
2259 if (likely(!ret)) {
2260 if (!do_swap_account)
2261 return CHARGE_OK;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002262 ret = res_counter_charge(&memcg->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002263 if (likely(!ret))
2264 return CHARGE_OK;
2265
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002266 res_counter_uncharge(&memcg->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002267 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2268 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2269 } else
2270 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002271 /*
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002272 * nr_pages can be either a huge page (HPAGE_PMD_NR), a batch
2273 * of regular pages (CHARGE_BATCH), or a single regular page (1).
Johannes Weiner9221edb2011-02-01 15:52:42 -08002274 *
2275 * Never reclaim on behalf of optional batching, retry with a
2276 * single page instead.
2277 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002278 if (nr_pages == CHARGE_BATCH)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002279 return CHARGE_RETRY;
2280
2281 if (!(gfp_mask & __GFP_WAIT))
2282 return CHARGE_WOULDBLOCK;
2283
2284 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
Ying Han0ae5e892011-05-26 16:25:25 -07002285 gfp_mask, flags, NULL);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002286 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002287 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002288 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002289 * Even though the limit is exceeded at this point, reclaim
2290 * may have been able to free some pages. Retry the charge
2291 * before killing the task.
2292 *
2293 * Only for regular pages, though: huge pages are rather
2294 * unlikely to succeed so close to the limit, and we fall back
2295 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002296 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002297 if (nr_pages == 1 && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002298 return CHARGE_RETRY;
2299
2300 /*
2301 * At task move, charge accounts can be doubly counted. So, it's
2302 * better to wait until the end of task_move if something is going on.
2303 */
2304 if (mem_cgroup_wait_acct_move(mem_over_limit))
2305 return CHARGE_RETRY;
2306
2307 /* If we don't need to call oom-killer at el, return immediately */
2308 if (!oom_check)
2309 return CHARGE_NOMEM;
2310 /* check OOM */
2311 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask))
2312 return CHARGE_OOM_DIE;
2313
2314 return CHARGE_RETRY;
2315}
2316
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002317/*
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002318 * Unlike exported interface, "oom" parameter is added. if oom==true,
2319 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002320 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002321static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002322 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002323 unsigned int nr_pages,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002324 struct mem_cgroup **ptr,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002325 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002326{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002327 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002328 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002329 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002330 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002331
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002332 /*
2333 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2334 * in system level. So, allow to go ahead dying process in addition to
2335 * MEMDIE process.
2336 */
2337 if (unlikely(test_thread_flag(TIF_MEMDIE)
2338 || fatal_signal_pending(current)))
2339 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002340
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002341 /*
Hugh Dickins3be912772008-02-07 00:14:19 -08002342 * We always charge the cgroup the mm_struct belongs to.
2343 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002344 * thread group leader migrates. It's possible that mm is not
2345 * set, if so charge the init_mm (happens for pagecache usage).
2346 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002347 if (!*ptr && !mm)
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002348 goto bypass;
2349again:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002350 if (*ptr) { /* css should be a valid one */
2351 memcg = *ptr;
2352 VM_BUG_ON(css_is_removed(&memcg->css));
2353 if (mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002354 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002355 if (nr_pages == 1 && consume_stock(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002356 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002357 css_get(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002358 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002359 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002360
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002361 rcu_read_lock();
2362 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002363 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002364 * Because we don't have task_lock(), "p" can exit.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002365 * In that case, "memcg" can point to root or p can be NULL with
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002366 * race with swapoff. Then, we have small risk of mis-accouning.
2367 * But such kind of mis-account by race always happens because
2368 * we don't have cgroup_mutex(). It's overkill and we allo that
2369 * small race, here.
2370 * (*) swapoff at el will charge against mm-struct not against
2371 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002372 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002373 memcg = mem_cgroup_from_task(p);
2374 if (!memcg || mem_cgroup_is_root(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002375 rcu_read_unlock();
2376 goto done;
2377 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002378 if (nr_pages == 1 && consume_stock(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002379 /*
2380 * It seems dagerous to access memcg without css_get().
2381 * But considering how consume_stok works, it's not
2382 * necessary. If consume_stock success, some charges
2383 * from this memcg are cached on this cpu. So, we
2384 * don't need to call css_get()/css_tryget() before
2385 * calling consume_stock().
2386 */
2387 rcu_read_unlock();
2388 goto done;
2389 }
2390 /* after here, we may be blocked. we need to get refcnt */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002391 if (!css_tryget(&memcg->css)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002392 rcu_read_unlock();
2393 goto again;
2394 }
2395 rcu_read_unlock();
2396 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002397
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002398 do {
2399 bool oom_check;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002400
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002401 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002402 if (fatal_signal_pending(current)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002403 css_put(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002404 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002405 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002406
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002407 oom_check = false;
2408 if (oom && !nr_oom_retries) {
2409 oom_check = true;
2410 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2411 }
Balbir Singh6d61ef42009-01-07 18:08:06 -08002412
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002413 ret = mem_cgroup_do_charge(memcg, gfp_mask, batch, oom_check);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002414 switch (ret) {
2415 case CHARGE_OK:
2416 break;
2417 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002418 batch = nr_pages;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002419 css_put(&memcg->css);
2420 memcg = NULL;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002421 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002422 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002423 css_put(&memcg->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002424 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002425 case CHARGE_NOMEM: /* OOM routine works */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002426 if (!oom) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002427 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002428 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002429 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002430 /* If oom, we never return -ENOMEM */
2431 nr_oom_retries--;
2432 break;
2433 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002434 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002435 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08002436 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002437 } while (ret != CHARGE_OK);
2438
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002439 if (batch > nr_pages)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002440 refill_stock(memcg, batch - nr_pages);
2441 css_put(&memcg->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002442done:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002443 *ptr = memcg;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002444 return 0;
2445nomem:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002446 *ptr = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002447 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002448bypass:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002449 *ptr = NULL;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002450 return 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002451}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002452
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002453/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002454 * Somemtimes we have to undo a charge we got by try_charge().
2455 * This function is for that and do uncharge, put css's refcnt.
2456 * gotten by try_charge().
2457 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002458static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002459 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002460{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002461 if (!mem_cgroup_is_root(memcg)) {
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002462 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002463
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002464 res_counter_uncharge(&memcg->res, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002465 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002466 res_counter_uncharge(&memcg->memsw, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002467 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002468}
2469
2470/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002471 * A helper function to get mem_cgroup from ID. must be called under
2472 * rcu_read_lock(). The caller must check css_is_removed() or some if
2473 * it's concern. (dropping refcnt from swap can be called against removed
2474 * memcg.)
2475 */
2476static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2477{
2478 struct cgroup_subsys_state *css;
2479
2480 /* ID 0 is unused ID */
2481 if (!id)
2482 return NULL;
2483 css = css_lookup(&mem_cgroup_subsys, id);
2484 if (!css)
2485 return NULL;
2486 return container_of(css, struct mem_cgroup, css);
2487}
2488
Wu Fengguange42d9d52009-12-16 12:19:59 +01002489struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002490{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002491 struct mem_cgroup *memcg = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002492 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002493 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002494 swp_entry_t ent;
2495
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002496 VM_BUG_ON(!PageLocked(page));
2497
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002498 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f632009-04-30 15:08:11 -07002499 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002500 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002501 memcg = pc->mem_cgroup;
2502 if (memcg && !css_tryget(&memcg->css))
2503 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002504 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002505 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002506 id = lookup_swap_cgroup(ent);
2507 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002508 memcg = mem_cgroup_lookup(id);
2509 if (memcg && !css_tryget(&memcg->css))
2510 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002511 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002512 }
Daisuke Nishimurac0bd3f632009-04-30 15:08:11 -07002513 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002514 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002515}
2516
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002517static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,
Johannes Weiner5564e882011-03-23 16:42:29 -07002518 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002519 unsigned int nr_pages,
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002520 struct page_cgroup *pc,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002521 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002522{
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002523 lock_page_cgroup(pc);
2524 if (unlikely(PageCgroupUsed(pc))) {
2525 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002526 __mem_cgroup_cancel_charge(memcg, nr_pages);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002527 return;
2528 }
2529 /*
2530 * we don't need page_cgroup_lock about tail pages, becase they are not
2531 * accessed by any other context at this point.
2532 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002533 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002534 /*
2535 * We access a page_cgroup asynchronously without lock_page_cgroup().
2536 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2537 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2538 * before USED bit, we need memory barrier here.
2539 * See mem_cgroup_add_lru_list(), etc.
2540 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002541 smp_wmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -07002542 switch (ctype) {
2543 case MEM_CGROUP_CHARGE_TYPE_CACHE:
2544 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
2545 SetPageCgroupCache(pc);
2546 SetPageCgroupUsed(pc);
2547 break;
2548 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
2549 ClearPageCgroupCache(pc);
2550 SetPageCgroupUsed(pc);
2551 break;
2552 default:
2553 break;
2554 }
Hugh Dickins3be912772008-02-07 00:14:19 -08002555
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002556 mem_cgroup_charge_statistics(memcg, PageCgroupCache(pc), nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002557 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002558 /*
2559 * "charge_statistics" updated event counter. Then, check it.
2560 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2561 * if they exceeds softlimit.
2562 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002563 memcg_check_events(memcg, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002564}
2565
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002566#ifdef CONFIG_TRANSPARENT_HUGEPAGE
2567
2568#define PCGF_NOCOPY_AT_SPLIT ((1 << PCG_LOCK) | (1 << PCG_MOVE_LOCK) |\
2569 (1 << PCG_ACCT_LRU) | (1 << PCG_MIGRATION))
2570/*
2571 * Because tail pages are not marked as "used", set it. We're under
2572 * zone->lru_lock, 'splitting on pmd' and compund_lock.
2573 */
2574void mem_cgroup_split_huge_fixup(struct page *head, struct page *tail)
2575{
2576 struct page_cgroup *head_pc = lookup_page_cgroup(head);
2577 struct page_cgroup *tail_pc = lookup_page_cgroup(tail);
2578 unsigned long flags;
2579
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08002580 if (mem_cgroup_disabled())
2581 return;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002582 /*
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002583 * We have no races with charge/uncharge but will have races with
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002584 * page state accounting.
2585 */
2586 move_lock_page_cgroup(head_pc, &flags);
2587
2588 tail_pc->mem_cgroup = head_pc->mem_cgroup;
2589 smp_wmb(); /* see __commit_charge() */
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002590 if (PageCgroupAcctLRU(head_pc)) {
2591 enum lru_list lru;
2592 struct mem_cgroup_per_zone *mz;
2593
2594 /*
2595 * LRU flags cannot be copied because we need to add tail
2596 *.page to LRU by generic call and our hook will be called.
2597 * We hold lru_lock, then, reduce counter directly.
2598 */
2599 lru = page_lru(head);
Johannes Weiner97a6c372011-03-23 16:42:27 -07002600 mz = page_cgroup_zoneinfo(head_pc->mem_cgroup, head);
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002601 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
2602 }
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002603 tail_pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
2604 move_unlock_page_cgroup(head_pc, &flags);
2605}
2606#endif
2607
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002608/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07002609 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07002610 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002611 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002612 * @pc: page_cgroup of the page.
2613 * @from: mem_cgroup which the page is moved from.
2614 * @to: mem_cgroup which the page is moved to. @from != @to.
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002615 * @uncharge: whether we should call uncharge and css_put against @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002616 *
2617 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002618 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002619 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002620 *
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002621 * This function doesn't do "charge" nor css_get to new cgroup. It should be
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002622 * done by a caller(__mem_cgroup_try_charge would be useful). If @uncharge is
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002623 * true, this function does "uncharge" from old cgroup, but it doesn't if
2624 * @uncharge is false, so a caller should do "uncharge".
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002625 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002626static int mem_cgroup_move_account(struct page *page,
2627 unsigned int nr_pages,
2628 struct page_cgroup *pc,
2629 struct mem_cgroup *from,
2630 struct mem_cgroup *to,
2631 bool uncharge)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002632{
Johannes Weinerde3638d2011-03-23 16:42:28 -07002633 unsigned long flags;
2634 int ret;
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002635
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002636 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07002637 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07002638 /*
2639 * The page is isolated from LRU. So, collapse function
2640 * will not handle this page. But page splitting can happen.
2641 * Do this check under compound_page_lock(). The caller should
2642 * hold it.
2643 */
2644 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002645 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07002646 goto out;
2647
2648 lock_page_cgroup(pc);
2649
2650 ret = -EINVAL;
2651 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
2652 goto unlock;
2653
2654 move_lock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002655
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002656 if (PageCgroupFileMapped(pc)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08002657 /* Update mapped_file data for mem_cgroup */
2658 preempt_disable();
2659 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2660 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2661 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07002662 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002663 mem_cgroup_charge_statistics(from, PageCgroupCache(pc), -nr_pages);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002664 if (uncharge)
2665 /* This is not "cancel", but cancel_charge does all we need. */
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002666 __mem_cgroup_cancel_charge(from, nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07002667
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002668 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002669 pc->mem_cgroup = to;
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002670 mem_cgroup_charge_statistics(to, PageCgroupCache(pc), nr_pages);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002671 /*
2672 * We charges against "to" which may not have any tasks. Then, "to"
2673 * can be under rmdir(). But in current implementation, caller of
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08002674 * this function is just force_empty() and move charge, so it's
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002675 * guaranteed that "to" is never removed. So, we don't check rmdir
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08002676 * status here.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002677 */
Johannes Weinerde3638d2011-03-23 16:42:28 -07002678 move_unlock_page_cgroup(pc, &flags);
2679 ret = 0;
2680unlock:
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002681 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002682 /*
2683 * check events
2684 */
Johannes Weiner5564e882011-03-23 16:42:29 -07002685 memcg_check_events(to, page);
2686 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07002687out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002688 return ret;
2689}
2690
2691/*
2692 * move charges to its parent.
2693 */
2694
Johannes Weiner5564e882011-03-23 16:42:29 -07002695static int mem_cgroup_move_parent(struct page *page,
2696 struct page_cgroup *pc,
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002697 struct mem_cgroup *child,
2698 gfp_t gfp_mask)
2699{
2700 struct cgroup *cg = child->css.cgroup;
2701 struct cgroup *pcg = cg->parent;
2702 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002703 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07002704 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002705 int ret;
2706
2707 /* Is ROOT ? */
2708 if (!pcg)
2709 return -EINVAL;
2710
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002711 ret = -EBUSY;
2712 if (!get_page_unless_zero(page))
2713 goto out;
2714 if (isolate_lru_page(page))
2715 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08002716
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002717 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002718
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002719 parent = mem_cgroup_from_cont(pcg);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002720 ret = __mem_cgroup_try_charge(NULL, gfp_mask, nr_pages, &parent, false);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002721 if (ret || !parent)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002722 goto put_back;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002723
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002724 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002725 flags = compound_lock_irqsave(page);
2726
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002727 ret = mem_cgroup_move_account(page, nr_pages, pc, child, parent, true);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002728 if (ret)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002729 __mem_cgroup_cancel_charge(parent, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08002730
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002731 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002732 compound_unlock_irqrestore(page, flags);
Jesper Juhl8dba4742011-01-25 15:07:24 -08002733put_back:
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002734 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002735put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08002736 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002737out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002738 return ret;
2739}
2740
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002741/*
2742 * Charge the memory controller for page usage.
2743 * Return
2744 * 0 if the charge was successful
2745 * < 0 if the cgroup is over its limit
2746 */
2747static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002748 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002749{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002750 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002751 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08002752 struct page_cgroup *pc;
2753 bool oom = true;
2754 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08002755
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002756 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002757 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002758 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08002759 /*
2760 * Never OOM-kill a process for a huge page. The
2761 * fault handler will fall back to regular pages.
2762 */
2763 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002764 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002765
2766 pc = lookup_page_cgroup(page);
Johannes Weineraf4a6622011-03-23 16:42:24 -07002767 BUG_ON(!pc); /* XXX: remove this and move pc lookup into commit */
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002768
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002769 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom);
2770 if (ret || !memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002771 return ret;
2772
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002773 __mem_cgroup_commit_charge(memcg, page, nr_pages, pc, ctype);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002774 return 0;
2775}
2776
2777int mem_cgroup_newpage_charge(struct page *page,
2778 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002779{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002780 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002781 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002782 /*
2783 * If already mapped, we don't have to account.
2784 * If page cache, page->mapping has address_space.
2785 * But page->mapping may have out-of-use anon_vma pointer,
2786 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
2787 * is NULL.
2788 */
2789 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
2790 return 0;
2791 if (unlikely(!mm))
2792 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002793 return mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002794 MEM_CGROUP_CHARGE_TYPE_MAPPED);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002795}
2796
Daisuke Nishimura83aae4c72009-04-02 16:57:48 -07002797static void
2798__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2799 enum charge_type ctype);
2800
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002801static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002802__mem_cgroup_commit_charge_lrucare(struct page *page, struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002803 enum charge_type ctype)
2804{
2805 struct page_cgroup *pc = lookup_page_cgroup(page);
2806 /*
2807 * In some case, SwapCache, FUSE(splice_buf->radixtree), the page
2808 * is already on LRU. It means the page may on some other page_cgroup's
2809 * LRU. Take care of it.
2810 */
2811 mem_cgroup_lru_del_before_commit(page);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002812 __mem_cgroup_commit_charge(memcg, page, 1, pc, ctype);
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002813 mem_cgroup_lru_add_after_commit(page);
2814 return;
2815}
2816
Balbir Singhe1a1cd52008-02-07 00:14:02 -08002817int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
2818 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08002819{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002820 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002821 int ret;
2822
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002823 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002824 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002825 if (PageCompound(page))
2826 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002827
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002828 if (unlikely(!mm))
Balbir Singh8697d332008-02-07 00:13:59 -08002829 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002830
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002831 if (page_is_file_cache(page)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002832 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, &memcg, true);
2833 if (ret || !memcg)
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002834 return ret;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002835
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002836 /*
2837 * FUSE reuses pages without going through the final
2838 * put that would remove them from the LRU list, make
2839 * sure that they get relinked properly.
2840 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002841 __mem_cgroup_commit_charge_lrucare(page, memcg,
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002842 MEM_CGROUP_CHARGE_TYPE_CACHE);
2843 return ret;
2844 }
Daisuke Nishimura83aae4c72009-04-02 16:57:48 -07002845 /* shmem */
2846 if (PageSwapCache(page)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002847 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &memcg);
Daisuke Nishimura83aae4c72009-04-02 16:57:48 -07002848 if (!ret)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002849 __mem_cgroup_commit_charge_swapin(page, memcg,
Daisuke Nishimura83aae4c72009-04-02 16:57:48 -07002850 MEM_CGROUP_CHARGE_TYPE_SHMEM);
2851 } else
2852 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002853 MEM_CGROUP_CHARGE_TYPE_SHMEM);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002854
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002855 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002856}
2857
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002858/*
2859 * While swap-in, try_charge -> commit or cancel, the page is locked.
2860 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02002861 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002862 * "commit()" or removed by "cancel()"
2863 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002864int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
2865 struct page *page,
2866 gfp_t mask, struct mem_cgroup **ptr)
2867{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002868 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002869 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002870
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07002871 *ptr = NULL;
2872
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002873 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002874 return 0;
2875
2876 if (!do_swap_account)
2877 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002878 /*
2879 * A racing thread's fault, or swapoff, may have already updated
Hugh Dickins407f9c82009-12-14 17:59:30 -08002880 * the pte, and even removed page from swap cache: in those cases
2881 * do_swap_page()'s pte_same() test will fail; but there's also a
2882 * KSM case which does need to charge the page.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002883 */
2884 if (!PageSwapCache(page))
Hugh Dickins407f9c82009-12-14 17:59:30 -08002885 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002886 memcg = try_get_mem_cgroup_from_page(page);
2887 if (!memcg)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002888 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002889 *ptr = memcg;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002890 ret = __mem_cgroup_try_charge(NULL, mask, 1, ptr, true);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002891 css_put(&memcg->css);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002892 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002893charge_cur_mm:
2894 if (unlikely(!mm))
2895 mm = &init_mm;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002896 return __mem_cgroup_try_charge(mm, mask, 1, ptr, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002897}
2898
Daisuke Nishimura83aae4c72009-04-02 16:57:48 -07002899static void
2900__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2901 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002902{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002903 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002904 return;
2905 if (!ptr)
2906 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002907 cgroup_exclude_rmdir(&ptr->css);
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002908
2909 __mem_cgroup_commit_charge_lrucare(page, ptr, ctype);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002910 /*
2911 * Now swap is on-memory. This means this page may be
2912 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002913 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
2914 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
2915 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002916 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002917 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002918 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002919 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002920 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002921
2922 id = swap_cgroup_record(ent, 0);
2923 rcu_read_lock();
2924 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002925 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002926 /*
2927 * This recorded memcg can be obsolete one. So, avoid
2928 * calling css_tryget
2929 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002930 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002931 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002932 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002933 mem_cgroup_put(memcg);
2934 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002935 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002936 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002937 /*
2938 * At swapin, we may charge account against cgroup which has no tasks.
2939 * So, rmdir()->pre_destroy() can be called while we do this charge.
2940 * In that case, we need to call pre_destroy() again. check it here.
2941 */
2942 cgroup_release_and_wakeup_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002943}
2944
Daisuke Nishimura83aae4c72009-04-02 16:57:48 -07002945void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
2946{
2947 __mem_cgroup_commit_charge_swapin(page, ptr,
2948 MEM_CGROUP_CHARGE_TYPE_MAPPED);
2949}
2950
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002951void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002952{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002953 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002954 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002955 if (!memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002956 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002957 __mem_cgroup_cancel_charge(memcg, 1);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002958}
2959
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002960static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002961 unsigned int nr_pages,
2962 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002963{
2964 struct memcg_batch_info *batch = NULL;
2965 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002966
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002967 /* If swapout, usage of swap doesn't decrease */
2968 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2969 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002970
2971 batch = &current->memcg_batch;
2972 /*
2973 * In usual, we do css_get() when we remember memcg pointer.
2974 * But in this case, we keep res->usage until end of a series of
2975 * uncharges. Then, it's ok to ignore memcg's refcnt.
2976 */
2977 if (!batch->memcg)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002978 batch->memcg = memcg;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002979 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002980 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002981 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002982 * the same cgroup and we have chance to coalesce uncharges.
2983 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
2984 * because we want to do uncharge as soon as possible.
2985 */
2986
2987 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
2988 goto direct_uncharge;
2989
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002990 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08002991 goto direct_uncharge;
2992
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002993 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002994 * In typical case, batch->memcg == mem. This means we can
2995 * merge a series of uncharges to an uncharge of res_counter.
2996 * If not, we uncharge res_counter ony by one.
2997 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002998 if (batch->memcg != memcg)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002999 goto direct_uncharge;
3000 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003001 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003002 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003003 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003004 return;
3005direct_uncharge:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003006 res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003007 if (uncharge_memsw)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003008 res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE);
3009 if (unlikely(batch->memcg != memcg))
3010 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003011 return;
3012}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003013
Balbir Singh8697d332008-02-07 00:13:59 -08003014/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003015 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003016 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003017static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003018__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003019{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003020 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003021 unsigned int nr_pages = 1;
3022 struct page_cgroup *pc;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003023
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003024 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003025 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07003026
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003027 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003028 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003029
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003030 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003031 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003032 VM_BUG_ON(!PageTransHuge(page));
3033 }
Balbir Singh8697d332008-02-07 00:13:59 -08003034 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08003035 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08003036 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003037 pc = lookup_page_cgroup(page);
3038 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003039 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003040
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003041 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003042
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003043 memcg = pc->mem_cgroup;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003044
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003045 if (!PageCgroupUsed(pc))
3046 goto unlock_out;
3047
3048 switch (ctype) {
3049 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003050 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003051 /* See mem_cgroup_prepare_migration() */
3052 if (page_mapped(page) || PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003053 goto unlock_out;
3054 break;
3055 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
3056 if (!PageAnon(page)) { /* Shared memory */
3057 if (page->mapping && !page_is_file_cache(page))
3058 goto unlock_out;
3059 } else if (page_mapped(page)) /* Anon */
3060 goto unlock_out;
3061 break;
3062 default:
3063 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003064 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003065
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003066 mem_cgroup_charge_statistics(memcg, PageCgroupCache(pc), -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003067
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003068 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08003069 /*
3070 * pc->mem_cgroup is not cleared here. It will be accessed when it's
3071 * freed from LRU. This is safe because uncharged page is expected not
3072 * to be reused (freed soon). Exception is SwapCache, it's handled by
3073 * special functions.
3074 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003075
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003076 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003077 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003078 * even after unlock, we have memcg->res.usage here and this memcg
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003079 * will never be freed.
3080 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003081 memcg_check_events(memcg, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003082 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003083 mem_cgroup_swap_statistics(memcg, true);
3084 mem_cgroup_get(memcg);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003085 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003086 if (!mem_cgroup_is_root(memcg))
3087 mem_cgroup_do_uncharge(memcg, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003088
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003089 return memcg;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003090
3091unlock_out:
3092 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003093 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08003094}
3095
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003096void mem_cgroup_uncharge_page(struct page *page)
3097{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003098 /* early check. */
3099 if (page_mapped(page))
3100 return;
3101 if (page->mapping && !PageAnon(page))
3102 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003103 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
3104}
3105
3106void mem_cgroup_uncharge_cache_page(struct page *page)
3107{
3108 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07003109 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003110 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
3111}
3112
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003113/*
3114 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
3115 * In that cases, pages are freed continuously and we can expect pages
3116 * are in the same memcg. All these calls itself limits the number of
3117 * pages freed at once, then uncharge_start/end() is called properly.
3118 * This may be called prural(2) times in a context,
3119 */
3120
3121void mem_cgroup_uncharge_start(void)
3122{
3123 current->memcg_batch.do_batch++;
3124 /* We can do nest. */
3125 if (current->memcg_batch.do_batch == 1) {
3126 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003127 current->memcg_batch.nr_pages = 0;
3128 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003129 }
3130}
3131
3132void mem_cgroup_uncharge_end(void)
3133{
3134 struct memcg_batch_info *batch = &current->memcg_batch;
3135
3136 if (!batch->do_batch)
3137 return;
3138
3139 batch->do_batch--;
3140 if (batch->do_batch) /* If stacked, do nothing. */
3141 return;
3142
3143 if (!batch->memcg)
3144 return;
3145 /*
3146 * This "batch->memcg" is valid without any css_get/put etc...
3147 * bacause we hide charges behind us.
3148 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003149 if (batch->nr_pages)
3150 res_counter_uncharge(&batch->memcg->res,
3151 batch->nr_pages * PAGE_SIZE);
3152 if (batch->memsw_nr_pages)
3153 res_counter_uncharge(&batch->memcg->memsw,
3154 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003155 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003156 /* forget this pointer (for sanity check) */
3157 batch->memcg = NULL;
3158}
3159
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003160#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003161/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003162 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003163 * memcg information is recorded to swap_cgroup of "ent"
3164 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003165void
3166mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003167{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003168 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003169 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003170
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003171 if (!swapout) /* this was a swap cache but the swap is unused ! */
3172 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
3173
3174 memcg = __mem_cgroup_uncharge_common(page, ctype);
3175
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003176 /*
3177 * record memcg information, if swapout && memcg != NULL,
3178 * mem_cgroup_get() was called in uncharge().
3179 */
3180 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003181 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003182}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003183#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003184
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003185#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3186/*
3187 * called from swap_entry_free(). remove record in swap_cgroup and
3188 * uncharge "memsw" account.
3189 */
3190void mem_cgroup_uncharge_swap(swp_entry_t ent)
3191{
3192 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003193 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003194
3195 if (!do_swap_account)
3196 return;
3197
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003198 id = swap_cgroup_record(ent, 0);
3199 rcu_read_lock();
3200 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003201 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003202 /*
3203 * We uncharge this because swap is freed.
3204 * This memcg can be obsolete one. We avoid calling css_tryget
3205 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003206 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07003207 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003208 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003209 mem_cgroup_put(memcg);
3210 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003211 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003212}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003213
3214/**
3215 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
3216 * @entry: swap entry to be moved
3217 * @from: mem_cgroup which the entry is moved from
3218 * @to: mem_cgroup which the entry is moved to
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003219 * @need_fixup: whether we should fixup res_counters and refcounts.
Daisuke Nishimura02491442010-03-10 15:22:17 -08003220 *
3221 * It succeeds only when the swap_cgroup's record for this entry is the same
3222 * as the mem_cgroup's id of @from.
3223 *
3224 * Returns 0 on success, -EINVAL on failure.
3225 *
3226 * The caller must have charged to @to, IOW, called res_counter_charge() about
3227 * both res and memsw, and called css_get().
3228 */
3229static int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003230 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003231{
3232 unsigned short old_id, new_id;
3233
3234 old_id = css_id(&from->css);
3235 new_id = css_id(&to->css);
3236
3237 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08003238 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003239 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003240 /*
3241 * This function is only called from task migration context now.
3242 * It postpones res_counter and refcount handling till the end
3243 * of task migration(mem_cgroup_clear_mc()) for performance
3244 * improvement. But we cannot postpone mem_cgroup_get(to)
3245 * because if the process that has been moved to @to does
3246 * swap-in, the refcount of @to might be decreased to 0.
3247 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08003248 mem_cgroup_get(to);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003249 if (need_fixup) {
3250 if (!mem_cgroup_is_root(from))
3251 res_counter_uncharge(&from->memsw, PAGE_SIZE);
3252 mem_cgroup_put(from);
3253 /*
3254 * we charged both to->res and to->memsw, so we should
3255 * uncharge to->res.
3256 */
3257 if (!mem_cgroup_is_root(to))
3258 res_counter_uncharge(&to->res, PAGE_SIZE);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003259 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08003260 return 0;
3261 }
3262 return -EINVAL;
3263}
3264#else
3265static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003266 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003267{
3268 return -EINVAL;
3269}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003270#endif
3271
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003272/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003273 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
3274 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003275 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003276int mem_cgroup_prepare_migration(struct page *page,
Miklos Szeredief6a3c62011-03-22 16:30:52 -07003277 struct page *newpage, struct mem_cgroup **ptr, gfp_t gfp_mask)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003278{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003279 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003280 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003281 enum charge_type ctype;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003282 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003283
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07003284 *ptr = NULL;
3285
Andrea Arcangeliec168512011-01-13 15:46:56 -08003286 VM_BUG_ON(PageTransHuge(page));
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003287 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07003288 return 0;
3289
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003290 pc = lookup_page_cgroup(page);
3291 lock_page_cgroup(pc);
3292 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003293 memcg = pc->mem_cgroup;
3294 css_get(&memcg->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003295 /*
3296 * At migrating an anonymous page, its mapcount goes down
3297 * to 0 and uncharge() will be called. But, even if it's fully
3298 * unmapped, migration may fail and this page has to be
3299 * charged again. We set MIGRATION flag here and delay uncharge
3300 * until end_migration() is called
3301 *
3302 * Corner Case Thinking
3303 * A)
3304 * When the old page was mapped as Anon and it's unmap-and-freed
3305 * while migration was ongoing.
3306 * If unmap finds the old page, uncharge() of it will be delayed
3307 * until end_migration(). If unmap finds a new page, it's
3308 * uncharged when it make mapcount to be 1->0. If unmap code
3309 * finds swap_migration_entry, the new page will not be mapped
3310 * and end_migration() will find it(mapcount==0).
3311 *
3312 * B)
3313 * When the old page was mapped but migraion fails, the kernel
3314 * remaps it. A charge for it is kept by MIGRATION flag even
3315 * if mapcount goes down to 0. We can do remap successfully
3316 * without charging it again.
3317 *
3318 * C)
3319 * The "old" page is under lock_page() until the end of
3320 * migration, so, the old page itself will not be swapped-out.
3321 * If the new page is swapped out before end_migraton, our
3322 * hook to usual swap-out path will catch the event.
3323 */
3324 if (PageAnon(page))
3325 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003326 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003327 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003328 /*
3329 * If the page is not charged at this point,
3330 * we return here.
3331 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003332 if (!memcg)
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003333 return 0;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003334
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003335 *ptr = memcg;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003336 ret = __mem_cgroup_try_charge(NULL, gfp_mask, 1, ptr, false);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003337 css_put(&memcg->css);/* drop extra refcnt */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003338 if (ret || *ptr == NULL) {
3339 if (PageAnon(page)) {
3340 lock_page_cgroup(pc);
3341 ClearPageCgroupMigration(pc);
3342 unlock_page_cgroup(pc);
3343 /*
3344 * The old page may be fully unmapped while we kept it.
3345 */
3346 mem_cgroup_uncharge_page(page);
3347 }
3348 return -ENOMEM;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003349 }
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003350 /*
3351 * We charge new page before it's used/mapped. So, even if unlock_page()
3352 * is called before end_migration, we can catch all events on this new
3353 * page. In the case new page is migrated but not remapped, new page's
3354 * mapcount will be finally 0 and we call uncharge in end_migration().
3355 */
3356 pc = lookup_page_cgroup(newpage);
3357 if (PageAnon(page))
3358 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
3359 else if (page_is_file_cache(page))
3360 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
3361 else
3362 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003363 __mem_cgroup_commit_charge(memcg, page, 1, pc, ctype);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003364 return ret;
3365}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08003366
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003367/* remove redundant charge if migration failed*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003368void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003369 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003370{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003371 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003372 struct page_cgroup *pc;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003373
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003374 if (!memcg)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003375 return;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003376 /* blocks rmdir() */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003377 cgroup_exclude_rmdir(&memcg->css);
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003378 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003379 used = oldpage;
3380 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003381 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003382 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003383 unused = oldpage;
3384 }
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003385 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003386 * We disallowed uncharge of pages under migration because mapcount
3387 * of the page goes down to zero, temporarly.
3388 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003389 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003390 pc = lookup_page_cgroup(oldpage);
3391 lock_page_cgroup(pc);
3392 ClearPageCgroupMigration(pc);
3393 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003394
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003395 __mem_cgroup_uncharge_common(unused, MEM_CGROUP_CHARGE_TYPE_FORCE);
3396
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003397 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003398 * If a page is a file cache, radix-tree replacement is very atomic
3399 * and we can skip this check. When it was an Anon page, its mapcount
3400 * goes down to 0. But because we added MIGRATION flage, it's not
3401 * uncharged yet. There are several case but page->mapcount check
3402 * and USED bit check in mem_cgroup_uncharge_page() will do enough
3403 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003404 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003405 if (PageAnon(used))
3406 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003407 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003408 * At migration, we may charge account against cgroup which has no
3409 * tasks.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003410 * So, rmdir()->pre_destroy() can be called while we do this charge.
3411 * In that case, we need to call pre_destroy() again. check it here.
3412 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003413 cgroup_release_and_wakeup_rmdir(&memcg->css);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003414}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08003415
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003416/*
3417 * At replace page cache, newpage is not under any memcg but it's on
3418 * LRU. So, this function doesn't touch res_counter but handles LRU
3419 * in correct way. Both pages are locked so we cannot race with uncharge.
3420 */
3421void mem_cgroup_replace_page_cache(struct page *oldpage,
3422 struct page *newpage)
3423{
3424 struct mem_cgroup *memcg;
3425 struct page_cgroup *pc;
3426 struct zone *zone;
3427 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
3428 unsigned long flags;
3429
3430 if (mem_cgroup_disabled())
3431 return;
3432
3433 pc = lookup_page_cgroup(oldpage);
3434 /* fix accounting on old pages */
3435 lock_page_cgroup(pc);
3436 memcg = pc->mem_cgroup;
3437 mem_cgroup_charge_statistics(memcg, PageCgroupCache(pc), -1);
3438 ClearPageCgroupUsed(pc);
3439 unlock_page_cgroup(pc);
3440
3441 if (PageSwapBacked(oldpage))
3442 type = MEM_CGROUP_CHARGE_TYPE_SHMEM;
3443
3444 zone = page_zone(newpage);
3445 pc = lookup_page_cgroup(newpage);
3446 /*
3447 * Even if newpage->mapping was NULL before starting replacement,
3448 * the newpage may be on LRU(or pagevec for LRU) already. We lock
3449 * LRU while we overwrite pc->mem_cgroup.
3450 */
3451 spin_lock_irqsave(&zone->lru_lock, flags);
3452 if (PageLRU(newpage))
3453 del_page_from_lru_list(zone, newpage, page_lru(newpage));
3454 __mem_cgroup_commit_charge(memcg, newpage, 1, pc, type);
3455 if (PageLRU(newpage))
3456 add_page_to_lru_list(zone, newpage, page_lru(newpage));
3457 spin_unlock_irqrestore(&zone->lru_lock, flags);
3458}
3459
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003460#ifdef CONFIG_DEBUG_VM
3461static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
3462{
3463 struct page_cgroup *pc;
3464
3465 pc = lookup_page_cgroup(page);
3466 if (likely(pc) && PageCgroupUsed(pc))
3467 return pc;
3468 return NULL;
3469}
3470
3471bool mem_cgroup_bad_page_check(struct page *page)
3472{
3473 if (mem_cgroup_disabled())
3474 return false;
3475
3476 return lookup_page_cgroup_used(page) != NULL;
3477}
3478
3479void mem_cgroup_print_bad_page(struct page *page)
3480{
3481 struct page_cgroup *pc;
3482
3483 pc = lookup_page_cgroup_used(page);
3484 if (pc) {
3485 int ret = -1;
3486 char *path;
3487
3488 printk(KERN_ALERT "pc:%p pc->flags:%lx pc->mem_cgroup:%p",
3489 pc, pc->flags, pc->mem_cgroup);
3490
3491 path = kmalloc(PATH_MAX, GFP_KERNEL);
3492 if (path) {
3493 rcu_read_lock();
3494 ret = cgroup_path(pc->mem_cgroup->css.cgroup,
3495 path, PATH_MAX);
3496 rcu_read_unlock();
3497 }
3498
3499 printk(KERN_CONT "(%s)\n",
3500 (ret < 0) ? "cannot get the path" : path);
3501 kfree(path);
3502 }
3503}
3504#endif
3505
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003506static DEFINE_MUTEX(set_limit_mutex);
3507
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08003508static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003509 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003510{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003511 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003512 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003513 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003514 int children = mem_cgroup_count_children(memcg);
3515 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003516 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003517
3518 /*
3519 * For keeping hierarchical_reclaim simple, how long we should retry
3520 * is depends on callers. We set our retry-count to be function
3521 * of # of children which we should visit in this loop.
3522 */
3523 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
3524
3525 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003526
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003527 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003528 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003529 if (signal_pending(current)) {
3530 ret = -EINTR;
3531 break;
3532 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003533 /*
3534 * Rather than hide all in some function, I do this in
3535 * open coded manner. You see what this really does.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003536 * We have to guarantee memcg->res.limit < memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003537 */
3538 mutex_lock(&set_limit_mutex);
3539 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3540 if (memswlimit < val) {
3541 ret = -EINVAL;
3542 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003543 break;
3544 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003545
3546 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3547 if (memlimit < val)
3548 enlarge = 1;
3549
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003550 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003551 if (!ret) {
3552 if (memswlimit == val)
3553 memcg->memsw_is_minimum = true;
3554 else
3555 memcg->memsw_is_minimum = false;
3556 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003557 mutex_unlock(&set_limit_mutex);
3558
3559 if (!ret)
3560 break;
3561
Bob Liuaa20d482009-12-15 16:47:14 -08003562 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Ying Han0ae5e892011-05-26 16:25:25 -07003563 MEM_CGROUP_RECLAIM_SHRINK,
3564 NULL);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003565 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
3566 /* Usage is reduced ? */
3567 if (curusage >= oldusage)
3568 retry_count--;
3569 else
3570 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003571 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003572 if (!ret && enlarge)
3573 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08003574
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003575 return ret;
3576}
3577
Li Zefan338c8432009-06-17 16:27:15 -07003578static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
3579 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003580{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003581 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003582 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003583 int children = mem_cgroup_count_children(memcg);
3584 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003585 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003586
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003587 /* see mem_cgroup_resize_res_limit */
3588 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
3589 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003590 while (retry_count) {
3591 if (signal_pending(current)) {
3592 ret = -EINTR;
3593 break;
3594 }
3595 /*
3596 * Rather than hide all in some function, I do this in
3597 * open coded manner. You see what this really does.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003598 * We have to guarantee memcg->res.limit < memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003599 */
3600 mutex_lock(&set_limit_mutex);
3601 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3602 if (memlimit > val) {
3603 ret = -EINVAL;
3604 mutex_unlock(&set_limit_mutex);
3605 break;
3606 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003607 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3608 if (memswlimit < val)
3609 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003610 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003611 if (!ret) {
3612 if (memlimit == val)
3613 memcg->memsw_is_minimum = true;
3614 else
3615 memcg->memsw_is_minimum = false;
3616 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003617 mutex_unlock(&set_limit_mutex);
3618
3619 if (!ret)
3620 break;
3621
Balbir Singh4e416952009-09-23 15:56:39 -07003622 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh75822b42009-09-23 15:56:38 -07003623 MEM_CGROUP_RECLAIM_NOSWAP |
Ying Han0ae5e892011-05-26 16:25:25 -07003624 MEM_CGROUP_RECLAIM_SHRINK,
3625 NULL);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003626 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003627 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003628 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003629 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003630 else
3631 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003632 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003633 if (!ret && enlarge)
3634 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003635 return ret;
3636}
3637
Balbir Singh4e416952009-09-23 15:56:39 -07003638unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
Ying Han0ae5e892011-05-26 16:25:25 -07003639 gfp_t gfp_mask,
3640 unsigned long *total_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07003641{
3642 unsigned long nr_reclaimed = 0;
3643 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
3644 unsigned long reclaimed;
3645 int loop = 0;
3646 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003647 unsigned long long excess;
Ying Han0ae5e892011-05-26 16:25:25 -07003648 unsigned long nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003649
3650 if (order > 0)
3651 return 0;
3652
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07003653 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
Balbir Singh4e416952009-09-23 15:56:39 -07003654 /*
3655 * This loop can run a while, specially if mem_cgroup's continuously
3656 * keep exceeding their soft limit and putting the system under
3657 * pressure
3658 */
3659 do {
3660 if (next_mz)
3661 mz = next_mz;
3662 else
3663 mz = mem_cgroup_largest_soft_limit_node(mctz);
3664 if (!mz)
3665 break;
3666
Ying Han0ae5e892011-05-26 16:25:25 -07003667 nr_scanned = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07003668 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
3669 gfp_mask,
Ying Han0ae5e892011-05-26 16:25:25 -07003670 MEM_CGROUP_RECLAIM_SOFT,
3671 &nr_scanned);
Balbir Singh4e416952009-09-23 15:56:39 -07003672 nr_reclaimed += reclaimed;
Ying Han0ae5e892011-05-26 16:25:25 -07003673 *total_scanned += nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003674 spin_lock(&mctz->lock);
3675
3676 /*
3677 * If we failed to reclaim anything from this memory cgroup
3678 * it is time to move on to the next cgroup
3679 */
3680 next_mz = NULL;
3681 if (!reclaimed) {
3682 do {
3683 /*
3684 * Loop until we find yet another one.
3685 *
3686 * By the time we get the soft_limit lock
3687 * again, someone might have aded the
3688 * group back on the RB tree. Iterate to
3689 * make sure we get a different mem.
3690 * mem_cgroup_largest_soft_limit_node returns
3691 * NULL if no other cgroup is present on
3692 * the tree
3693 */
3694 next_mz =
3695 __mem_cgroup_largest_soft_limit_node(mctz);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003696 if (next_mz == mz)
Balbir Singh4e416952009-09-23 15:56:39 -07003697 css_put(&next_mz->mem->css);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003698 else /* next_mz == NULL or other memcg */
Balbir Singh4e416952009-09-23 15:56:39 -07003699 break;
3700 } while (1);
3701 }
Balbir Singh4e416952009-09-23 15:56:39 -07003702 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003703 excess = res_counter_soft_limit_excess(&mz->mem->res);
Balbir Singh4e416952009-09-23 15:56:39 -07003704 /*
3705 * One school of thought says that we should not add
3706 * back the node to the tree if reclaim returns 0.
3707 * But our reclaim could return 0, simply because due
3708 * to priority we are exposing a smaller subset of
3709 * memory to reclaim from. Consider this as a longer
3710 * term TODO.
3711 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003712 /* If excess == 0, no tree ops */
3713 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07003714 spin_unlock(&mctz->lock);
3715 css_put(&mz->mem->css);
3716 loop++;
3717 /*
3718 * Could not reclaim anything and there are no more
3719 * mem cgroups to try or we seem to be looping without
3720 * reclaiming anything.
3721 */
3722 if (!nr_reclaimed &&
3723 (next_mz == NULL ||
3724 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3725 break;
3726 } while (!nr_reclaimed);
3727 if (next_mz)
3728 css_put(&next_mz->mem->css);
3729 return nr_reclaimed;
3730}
3731
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003732/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003733 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003734 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
3735 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003736static int mem_cgroup_force_empty_list(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003737 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003738{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003739 struct zone *zone;
3740 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003741 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003742 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003743 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003744 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003745
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003746 zone = &NODE_DATA(node)->node_zones[zid];
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003747 mz = mem_cgroup_zoneinfo(memcg, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07003748 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003749
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003750 loop = MEM_CGROUP_ZSTAT(mz, lru);
3751 /* give some margin against EBUSY etc...*/
3752 loop += 256;
3753 busy = NULL;
3754 while (loop--) {
Johannes Weiner5564e882011-03-23 16:42:29 -07003755 struct page *page;
3756
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003757 ret = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003758 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003759 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003760 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003761 break;
3762 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003763 pc = list_entry(list->prev, struct page_cgroup, lru);
3764 if (busy == pc) {
3765 list_move(&pc->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08003766 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003767 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003768 continue;
3769 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003770 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003771
Johannes Weiner6b3ae582011-03-23 16:42:30 -07003772 page = lookup_cgroup_page(pc);
Johannes Weiner5564e882011-03-23 16:42:29 -07003773
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003774 ret = mem_cgroup_move_parent(page, pc, memcg, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003775 if (ret == -ENOMEM)
3776 break;
3777
3778 if (ret == -EBUSY || ret == -EINVAL) {
3779 /* found lock contention or "pc" is obsolete. */
3780 busy = pc;
3781 cond_resched();
3782 } else
3783 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003784 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003785
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003786 if (!ret && !list_empty(list))
3787 return -EBUSY;
3788 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003789}
3790
3791/*
3792 * make mem_cgroup's charge to be 0 if there is no task.
3793 * This enables deleting this mem_cgroup.
3794 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003795static int mem_cgroup_force_empty(struct mem_cgroup *memcg, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003796{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003797 int ret;
3798 int node, zid, shrink;
3799 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003800 struct cgroup *cgrp = memcg->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003801
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003802 css_get(&memcg->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003803
3804 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003805 /* should free all ? */
3806 if (free_all)
3807 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003808move_account:
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003809 do {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003810 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003811 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003812 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003813 ret = -EINTR;
3814 if (signal_pending(current))
3815 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003816 /* This is for making all *used* pages to be on LRU. */
3817 lru_add_drain_all();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003818 drain_all_stock_sync(memcg);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003819 ret = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003820 mem_cgroup_start_move(memcg);
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08003821 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003822 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07003823 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003824 for_each_lru(l) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003825 ret = mem_cgroup_force_empty_list(memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003826 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003827 if (ret)
3828 break;
3829 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003830 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003831 if (ret)
3832 break;
3833 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003834 mem_cgroup_end_move(memcg);
3835 memcg_oom_recover(memcg);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003836 /* it seems parent cgroup doesn't have enough mem */
3837 if (ret == -ENOMEM)
3838 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003839 cond_resched();
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003840 /* "ret" should also be checked to ensure all lists are empty. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003841 } while (memcg->res.usage > 0 || ret);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003842out:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003843 css_put(&memcg->css);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003844 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003845
3846try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003847 /* returns EBUSY if there is a task or if we come here twice. */
3848 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003849 ret = -EBUSY;
3850 goto out;
3851 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003852 /* we call try-to-free pages for make this cgroup empty */
3853 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003854 /* try to free all pages in this cgroup */
3855 shrink = 1;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003856 while (nr_retries && memcg->res.usage > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003857 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003858
3859 if (signal_pending(current)) {
3860 ret = -EINTR;
3861 goto out;
3862 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003863 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
Johannes Weiner185efc02011-09-14 16:21:58 -07003864 false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003865 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003866 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003867 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02003868 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003869 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003870
3871 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003872 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003873 /* try move_account...there may be some *locked* pages. */
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003874 goto move_account;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003875}
3876
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003877int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
3878{
3879 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
3880}
3881
3882
Balbir Singh18f59ea2009-01-07 18:08:07 -08003883static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
3884{
3885 return mem_cgroup_from_cont(cont)->use_hierarchy;
3886}
3887
3888static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
3889 u64 val)
3890{
3891 int retval = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003892 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003893 struct cgroup *parent = cont->parent;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003894 struct mem_cgroup *parent_memcg = NULL;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003895
3896 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003897 parent_memcg = mem_cgroup_from_cont(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003898
3899 cgroup_lock();
3900 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003901 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08003902 * in the child subtrees. If it is unset, then the change can
3903 * occur, provided the current cgroup has no children.
3904 *
3905 * For the root cgroup, parent_mem is NULL, we allow value to be
3906 * set if there are no children.
3907 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003908 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08003909 (val == 1 || val == 0)) {
3910 if (list_empty(&cont->children))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003911 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003912 else
3913 retval = -EBUSY;
3914 } else
3915 retval = -EINVAL;
3916 cgroup_unlock();
3917
3918 return retval;
3919}
3920
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003921
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003922static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003923 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003924{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003925 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003926 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003927
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003928 /* Per-cpu values can be negative, use a signed accumulator */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003929 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003930 val += mem_cgroup_read_stat(iter, idx);
3931
3932 if (val < 0) /* race ? */
3933 val = 0;
3934 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003935}
3936
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003937static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003938{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003939 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003940
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003941 if (!mem_cgroup_is_root(memcg)) {
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003942 if (!swap)
Glauber Costa65c64ce2011-12-22 01:02:27 +00003943 return res_counter_read_u64(&memcg->res, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003944 else
Glauber Costa65c64ce2011-12-22 01:02:27 +00003945 return res_counter_read_u64(&memcg->memsw, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003946 }
3947
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003948 val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
3949 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003950
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003951 if (swap)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003952 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAPOUT);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003953
3954 return val << PAGE_SHIFT;
3955}
3956
Paul Menage2c3daa72008-04-29 00:59:58 -07003957static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003958{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003959 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003960 u64 val;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003961 int type, name;
3962
3963 type = MEMFILE_TYPE(cft->private);
3964 name = MEMFILE_ATTR(cft->private);
3965 switch (type) {
3966 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003967 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003968 val = mem_cgroup_usage(memcg, false);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003969 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003970 val = res_counter_read_u64(&memcg->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003971 break;
3972 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003973 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003974 val = mem_cgroup_usage(memcg, true);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003975 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003976 val = res_counter_read_u64(&memcg->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003977 break;
3978 default:
3979 BUG();
3980 break;
3981 }
3982 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003983}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003984/*
3985 * The user of this function is...
3986 * RES_LIMIT.
3987 */
Paul Menage856c13a2008-07-25 01:47:04 -07003988static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
3989 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003990{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003991 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003992 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003993 unsigned long long val;
3994 int ret;
3995
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003996 type = MEMFILE_TYPE(cft->private);
3997 name = MEMFILE_ATTR(cft->private);
3998 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003999 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07004000 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
4001 ret = -EINVAL;
4002 break;
4003 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004004 /* This function does all necessary parse...reuse it */
4005 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004006 if (ret)
4007 break;
4008 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004009 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004010 else
4011 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004012 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07004013 case RES_SOFT_LIMIT:
4014 ret = res_counter_memparse_write_strategy(buffer, &val);
4015 if (ret)
4016 break;
4017 /*
4018 * For memsw, soft limits are hard to implement in terms
4019 * of semantics, for now, we support soft limits for
4020 * control without swap
4021 */
4022 if (type == _MEM)
4023 ret = res_counter_set_soft_limit(&memcg->res, val);
4024 else
4025 ret = -EINVAL;
4026 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004027 default:
4028 ret = -EINVAL; /* should be BUG() ? */
4029 break;
4030 }
4031 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004032}
4033
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004034static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
4035 unsigned long long *mem_limit, unsigned long long *memsw_limit)
4036{
4037 struct cgroup *cgroup;
4038 unsigned long long min_limit, min_memsw_limit, tmp;
4039
4040 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4041 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4042 cgroup = memcg->css.cgroup;
4043 if (!memcg->use_hierarchy)
4044 goto out;
4045
4046 while (cgroup->parent) {
4047 cgroup = cgroup->parent;
4048 memcg = mem_cgroup_from_cont(cgroup);
4049 if (!memcg->use_hierarchy)
4050 break;
4051 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
4052 min_limit = min(min_limit, tmp);
4053 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4054 min_memsw_limit = min(min_memsw_limit, tmp);
4055 }
4056out:
4057 *mem_limit = min_limit;
4058 *memsw_limit = min_memsw_limit;
4059 return;
4060}
4061
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004062static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004063{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004064 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004065 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004066
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004067 memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004068 type = MEMFILE_TYPE(event);
4069 name = MEMFILE_ATTR(event);
4070 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004071 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004072 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004073 res_counter_reset_max(&memcg->res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004074 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004075 res_counter_reset_max(&memcg->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004076 break;
4077 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004078 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004079 res_counter_reset_failcnt(&memcg->res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004080 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004081 res_counter_reset_failcnt(&memcg->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004082 break;
4083 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07004084
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07004085 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004086}
4087
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004088static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
4089 struct cftype *cft)
4090{
4091 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
4092}
4093
Daisuke Nishimura02491442010-03-10 15:22:17 -08004094#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004095static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
4096 struct cftype *cft, u64 val)
4097{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004098 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004099
4100 if (val >= (1 << NR_MOVE_TYPE))
4101 return -EINVAL;
4102 /*
4103 * We check this value several times in both in can_attach() and
4104 * attach(), so we need cgroup lock to prevent this value from being
4105 * inconsistent.
4106 */
4107 cgroup_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004108 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004109 cgroup_unlock();
4110
4111 return 0;
4112}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004113#else
4114static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
4115 struct cftype *cft, u64 val)
4116{
4117 return -ENOSYS;
4118}
4119#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004120
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004121
4122/* For read statistics */
4123enum {
4124 MCS_CACHE,
4125 MCS_RSS,
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08004126 MCS_FILE_MAPPED,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004127 MCS_PGPGIN,
4128 MCS_PGPGOUT,
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004129 MCS_SWAP,
Ying Han456f9982011-05-26 16:25:38 -07004130 MCS_PGFAULT,
4131 MCS_PGMAJFAULT,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004132 MCS_INACTIVE_ANON,
4133 MCS_ACTIVE_ANON,
4134 MCS_INACTIVE_FILE,
4135 MCS_ACTIVE_FILE,
4136 MCS_UNEVICTABLE,
4137 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004138};
4139
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004140struct mcs_total_stat {
4141 s64 stat[NR_MCS_STAT];
4142};
4143
4144struct {
4145 char *local_name;
4146 char *total_name;
4147} memcg_stat_strings[NR_MCS_STAT] = {
4148 {"cache", "total_cache"},
4149 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07004150 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004151 {"pgpgin", "total_pgpgin"},
4152 {"pgpgout", "total_pgpgout"},
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004153 {"swap", "total_swap"},
Ying Han456f9982011-05-26 16:25:38 -07004154 {"pgfault", "total_pgfault"},
4155 {"pgmajfault", "total_pgmajfault"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004156 {"inactive_anon", "total_inactive_anon"},
4157 {"active_anon", "total_active_anon"},
4158 {"inactive_file", "total_inactive_file"},
4159 {"active_file", "total_active_file"},
4160 {"unevictable", "total_unevictable"}
4161};
4162
4163
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004164static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004165mem_cgroup_get_local_stat(struct mem_cgroup *memcg, struct mcs_total_stat *s)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004166{
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004167 s64 val;
4168
4169 /* per cpu stat */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004170 val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_CACHE);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004171 s->stat[MCS_CACHE] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004172 val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_RSS);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004173 s->stat[MCS_RSS] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004174 val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_FILE_MAPPED);
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08004175 s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004176 val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGPGIN);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004177 s->stat[MCS_PGPGIN] += val;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004178 val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGPGOUT);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004179 s->stat[MCS_PGPGOUT] += val;
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004180 if (do_swap_account) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004181 val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_SWAPOUT);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004182 s->stat[MCS_SWAP] += val * PAGE_SIZE;
4183 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004184 val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGFAULT);
Ying Han456f9982011-05-26 16:25:38 -07004185 s->stat[MCS_PGFAULT] += val;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004186 val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGMAJFAULT);
Ying Han456f9982011-05-26 16:25:38 -07004187 s->stat[MCS_PGMAJFAULT] += val;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004188
4189 /* per zone stat */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004190 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_ANON));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004191 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004192 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_ANON));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004193 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004194 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_FILE));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004195 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004196 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_FILE));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004197 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004198 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004199 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004200}
4201
4202static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004203mem_cgroup_get_total_stat(struct mem_cgroup *memcg, struct mcs_total_stat *s)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004204{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004205 struct mem_cgroup *iter;
4206
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004207 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004208 mem_cgroup_get_local_stat(iter, s);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004209}
4210
Ying Han406eb0c2011-05-26 16:25:37 -07004211#ifdef CONFIG_NUMA
4212static int mem_control_numa_stat_show(struct seq_file *m, void *arg)
4213{
4214 int nid;
4215 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
4216 unsigned long node_nr;
4217 struct cgroup *cont = m->private;
4218 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
4219
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004220 total_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07004221 seq_printf(m, "total=%lu", total_nr);
4222 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004223 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07004224 seq_printf(m, " N%d=%lu", nid, node_nr);
4225 }
4226 seq_putc(m, '\n');
4227
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004228 file_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07004229 seq_printf(m, "file=%lu", file_nr);
4230 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004231 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
4232 LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07004233 seq_printf(m, " N%d=%lu", nid, node_nr);
4234 }
4235 seq_putc(m, '\n');
4236
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004237 anon_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07004238 seq_printf(m, "anon=%lu", anon_nr);
4239 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004240 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
4241 LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07004242 seq_printf(m, " N%d=%lu", nid, node_nr);
4243 }
4244 seq_putc(m, '\n');
4245
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004246 unevictable_nr = mem_cgroup_nr_lru_pages(mem_cont, BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07004247 seq_printf(m, "unevictable=%lu", unevictable_nr);
4248 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004249 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
4250 BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07004251 seq_printf(m, " N%d=%lu", nid, node_nr);
4252 }
4253 seq_putc(m, '\n');
4254 return 0;
4255}
4256#endif /* CONFIG_NUMA */
4257
Paul Menagec64745c2008-04-29 01:00:02 -07004258static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
4259 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004260{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004261 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004262 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004263 int i;
4264
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004265 memset(&mystat, 0, sizeof(mystat));
4266 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004267
Ying Han406eb0c2011-05-26 16:25:37 -07004268
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004269 for (i = 0; i < NR_MCS_STAT; i++) {
4270 if (i == MCS_SWAP && !do_swap_account)
4271 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004272 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004273 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004274
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004275 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004276 {
4277 unsigned long long limit, memsw_limit;
4278 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
4279 cb->fill(cb, "hierarchical_memory_limit", limit);
4280 if (do_swap_account)
4281 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
4282 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004283
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004284 memset(&mystat, 0, sizeof(mystat));
4285 mem_cgroup_get_total_stat(mem_cont, &mystat);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004286 for (i = 0; i < NR_MCS_STAT; i++) {
4287 if (i == MCS_SWAP && !do_swap_account)
4288 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004289 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004290 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004291
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004292#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004293 {
4294 int nid, zid;
4295 struct mem_cgroup_per_zone *mz;
4296 unsigned long recent_rotated[2] = {0, 0};
4297 unsigned long recent_scanned[2] = {0, 0};
4298
4299 for_each_online_node(nid)
4300 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
4301 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
4302
4303 recent_rotated[0] +=
4304 mz->reclaim_stat.recent_rotated[0];
4305 recent_rotated[1] +=
4306 mz->reclaim_stat.recent_rotated[1];
4307 recent_scanned[0] +=
4308 mz->reclaim_stat.recent_scanned[0];
4309 recent_scanned[1] +=
4310 mz->reclaim_stat.recent_scanned[1];
4311 }
4312 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
4313 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
4314 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
4315 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
4316 }
4317#endif
4318
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004319 return 0;
4320}
4321
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004322static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
4323{
4324 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4325
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07004326 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004327}
4328
4329static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
4330 u64 val)
4331{
4332 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4333 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08004334
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004335 if (val > 100)
4336 return -EINVAL;
4337
4338 if (cgrp->parent == NULL)
4339 return -EINVAL;
4340
4341 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08004342
4343 cgroup_lock();
4344
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004345 /* If under hierarchy, only empty-root can set this value */
4346 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08004347 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
4348 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004349 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08004350 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004351
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004352 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004353
Li Zefan068b38c2009-01-15 13:51:26 -08004354 cgroup_unlock();
4355
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004356 return 0;
4357}
4358
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004359static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
4360{
4361 struct mem_cgroup_threshold_ary *t;
4362 u64 usage;
4363 int i;
4364
4365 rcu_read_lock();
4366 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004367 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004368 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004369 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004370
4371 if (!t)
4372 goto unlock;
4373
4374 usage = mem_cgroup_usage(memcg, swap);
4375
4376 /*
4377 * current_threshold points to threshold just below usage.
4378 * If it's not true, a threshold was crossed after last
4379 * call of __mem_cgroup_threshold().
4380 */
Phil Carmody5407a562010-05-26 14:42:42 -07004381 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004382
4383 /*
4384 * Iterate backward over array of thresholds starting from
4385 * current_threshold and check if a threshold is crossed.
4386 * If none of thresholds below usage is crossed, we read
4387 * only one element of the array here.
4388 */
4389 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
4390 eventfd_signal(t->entries[i].eventfd, 1);
4391
4392 /* i = current_threshold + 1 */
4393 i++;
4394
4395 /*
4396 * Iterate forward over array of thresholds starting from
4397 * current_threshold+1 and check if a threshold is crossed.
4398 * If none of thresholds above usage is crossed, we read
4399 * only one element of the array here.
4400 */
4401 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
4402 eventfd_signal(t->entries[i].eventfd, 1);
4403
4404 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07004405 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004406unlock:
4407 rcu_read_unlock();
4408}
4409
4410static void mem_cgroup_threshold(struct mem_cgroup *memcg)
4411{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07004412 while (memcg) {
4413 __mem_cgroup_threshold(memcg, false);
4414 if (do_swap_account)
4415 __mem_cgroup_threshold(memcg, true);
4416
4417 memcg = parent_mem_cgroup(memcg);
4418 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004419}
4420
4421static int compare_thresholds(const void *a, const void *b)
4422{
4423 const struct mem_cgroup_threshold *_a = a;
4424 const struct mem_cgroup_threshold *_b = b;
4425
4426 return _a->threshold - _b->threshold;
4427}
4428
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004429static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004430{
4431 struct mem_cgroup_eventfd_list *ev;
4432
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004433 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004434 eventfd_signal(ev->eventfd, 1);
4435 return 0;
4436}
4437
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004438static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004439{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004440 struct mem_cgroup *iter;
4441
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004442 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004443 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004444}
4445
4446static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
4447 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004448{
4449 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004450 struct mem_cgroup_thresholds *thresholds;
4451 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004452 int type = MEMFILE_TYPE(cft->private);
4453 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004454 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004455
4456 ret = res_counter_memparse_write_strategy(args, &threshold);
4457 if (ret)
4458 return ret;
4459
4460 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004461
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004462 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004463 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004464 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004465 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004466 else
4467 BUG();
4468
4469 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4470
4471 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004472 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004473 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4474
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004475 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004476
4477 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004478 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004479 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004480 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004481 ret = -ENOMEM;
4482 goto unlock;
4483 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004484 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004485
4486 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004487 if (thresholds->primary) {
4488 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004489 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004490 }
4491
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004492 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004493 new->entries[size - 1].eventfd = eventfd;
4494 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004495
4496 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004497 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004498 compare_thresholds, NULL);
4499
4500 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004501 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004502 for (i = 0; i < size; i++) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004503 if (new->entries[i].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004504 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004505 * new->current_threshold will not be used until
4506 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004507 * it here.
4508 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004509 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004510 }
4511 }
4512
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004513 /* Free old spare buffer and save old primary buffer as spare */
4514 kfree(thresholds->spare);
4515 thresholds->spare = thresholds->primary;
4516
4517 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004518
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004519 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004520 synchronize_rcu();
4521
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004522unlock:
4523 mutex_unlock(&memcg->thresholds_lock);
4524
4525 return ret;
4526}
4527
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004528static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004529 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004530{
4531 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004532 struct mem_cgroup_thresholds *thresholds;
4533 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004534 int type = MEMFILE_TYPE(cft->private);
4535 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004536 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004537
4538 mutex_lock(&memcg->thresholds_lock);
4539 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004540 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004541 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004542 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004543 else
4544 BUG();
4545
4546 /*
4547 * Something went wrong if we trying to unregister a threshold
4548 * if we don't have thresholds
4549 */
4550 BUG_ON(!thresholds);
4551
4552 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4553
4554 /* Check if a threshold crossed before removing */
4555 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4556
4557 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004558 size = 0;
4559 for (i = 0; i < thresholds->primary->size; i++) {
4560 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004561 size++;
4562 }
4563
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004564 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004565
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004566 /* Set thresholds array to NULL if we don't have thresholds */
4567 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004568 kfree(new);
4569 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004570 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004571 }
4572
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004573 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004574
4575 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004576 new->current_threshold = -1;
4577 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
4578 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004579 continue;
4580
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004581 new->entries[j] = thresholds->primary->entries[i];
4582 if (new->entries[j].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004583 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004584 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004585 * until rcu_assign_pointer(), so it's safe to increment
4586 * it here.
4587 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004588 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004589 }
4590 j++;
4591 }
4592
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004593swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004594 /* Swap primary and spare array */
4595 thresholds->spare = thresholds->primary;
4596 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004597
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004598 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004599 synchronize_rcu();
4600
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004601 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004602}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004603
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004604static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
4605 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
4606{
4607 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4608 struct mem_cgroup_eventfd_list *event;
4609 int type = MEMFILE_TYPE(cft->private);
4610
4611 BUG_ON(type != _OOM_TYPE);
4612 event = kmalloc(sizeof(*event), GFP_KERNEL);
4613 if (!event)
4614 return -ENOMEM;
4615
Michal Hocko1af8efe2011-07-26 16:08:24 -07004616 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004617
4618 event->eventfd = eventfd;
4619 list_add(&event->list, &memcg->oom_notify);
4620
4621 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07004622 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004623 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07004624 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004625
4626 return 0;
4627}
4628
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004629static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004630 struct cftype *cft, struct eventfd_ctx *eventfd)
4631{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004632 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004633 struct mem_cgroup_eventfd_list *ev, *tmp;
4634 int type = MEMFILE_TYPE(cft->private);
4635
4636 BUG_ON(type != _OOM_TYPE);
4637
Michal Hocko1af8efe2011-07-26 16:08:24 -07004638 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004639
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004640 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004641 if (ev->eventfd == eventfd) {
4642 list_del(&ev->list);
4643 kfree(ev);
4644 }
4645 }
4646
Michal Hocko1af8efe2011-07-26 16:08:24 -07004647 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004648}
4649
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004650static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
4651 struct cftype *cft, struct cgroup_map_cb *cb)
4652{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004653 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004654
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004655 cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004656
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004657 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004658 cb->fill(cb, "under_oom", 1);
4659 else
4660 cb->fill(cb, "under_oom", 0);
4661 return 0;
4662}
4663
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004664static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
4665 struct cftype *cft, u64 val)
4666{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004667 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004668 struct mem_cgroup *parent;
4669
4670 /* cannot set to root cgroup and only 0 and 1 are allowed */
4671 if (!cgrp->parent || !((val == 0) || (val == 1)))
4672 return -EINVAL;
4673
4674 parent = mem_cgroup_from_cont(cgrp->parent);
4675
4676 cgroup_lock();
4677 /* oom-kill-disable is a flag for subhierarchy. */
4678 if ((parent->use_hierarchy) ||
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004679 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004680 cgroup_unlock();
4681 return -EINVAL;
4682 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004683 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07004684 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004685 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004686 cgroup_unlock();
4687 return 0;
4688}
4689
Ying Han406eb0c2011-05-26 16:25:37 -07004690#ifdef CONFIG_NUMA
4691static const struct file_operations mem_control_numa_stat_file_operations = {
4692 .read = seq_read,
4693 .llseek = seq_lseek,
4694 .release = single_release,
4695};
4696
4697static int mem_control_numa_stat_open(struct inode *unused, struct file *file)
4698{
4699 struct cgroup *cont = file->f_dentry->d_parent->d_fsdata;
4700
4701 file->f_op = &mem_control_numa_stat_file_operations;
4702 return single_open(file, mem_control_numa_stat_show, cont);
4703}
4704#endif /* CONFIG_NUMA */
4705
Glauber Costae5671df2011-12-11 21:47:01 +00004706#ifdef CONFIG_CGROUP_MEM_RES_CTLR_KMEM
Glauber Costae5671df2011-12-11 21:47:01 +00004707static int register_kmem_files(struct cgroup *cont, struct cgroup_subsys *ss)
4708{
Glauber Costad1a4c0b2011-12-11 21:47:04 +00004709 /*
4710 * Part of this would be better living in a separate allocation
4711 * function, leaving us with just the cgroup tree population work.
4712 * We, however, depend on state such as network's proto_list that
4713 * is only initialized after cgroup creation. I found the less
4714 * cumbersome way to deal with it to defer it all to populate time
4715 */
Glauber Costa65c64ce2011-12-22 01:02:27 +00004716 return mem_cgroup_sockets_init(cont, ss);
Glauber Costae5671df2011-12-11 21:47:01 +00004717};
4718
Glauber Costad1a4c0b2011-12-11 21:47:04 +00004719static void kmem_cgroup_destroy(struct cgroup_subsys *ss,
4720 struct cgroup *cont)
4721{
4722 mem_cgroup_sockets_destroy(cont, ss);
4723}
Glauber Costae5671df2011-12-11 21:47:01 +00004724#else
4725static int register_kmem_files(struct cgroup *cont, struct cgroup_subsys *ss)
4726{
4727 return 0;
4728}
Glauber Costad1a4c0b2011-12-11 21:47:04 +00004729
4730static void kmem_cgroup_destroy(struct cgroup_subsys *ss,
4731 struct cgroup *cont)
4732{
4733}
Glauber Costae5671df2011-12-11 21:47:01 +00004734#endif
4735
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004736static struct cftype mem_cgroup_files[] = {
4737 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004738 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004739 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07004740 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004741 .register_event = mem_cgroup_usage_register_event,
4742 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004743 },
4744 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004745 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004746 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004747 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004748 .read_u64 = mem_cgroup_read,
4749 },
4750 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004751 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004752 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07004753 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07004754 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004755 },
4756 {
Balbir Singh296c81d2009-09-23 15:56:36 -07004757 .name = "soft_limit_in_bytes",
4758 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
4759 .write_string = mem_cgroup_write,
4760 .read_u64 = mem_cgroup_read,
4761 },
4762 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004763 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004764 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004765 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07004766 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004767 },
Balbir Singh8697d332008-02-07 00:13:59 -08004768 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004769 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07004770 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004771 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004772 {
4773 .name = "force_empty",
4774 .trigger = mem_cgroup_force_empty_write,
4775 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08004776 {
4777 .name = "use_hierarchy",
4778 .write_u64 = mem_cgroup_hierarchy_write,
4779 .read_u64 = mem_cgroup_hierarchy_read,
4780 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004781 {
4782 .name = "swappiness",
4783 .read_u64 = mem_cgroup_swappiness_read,
4784 .write_u64 = mem_cgroup_swappiness_write,
4785 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004786 {
4787 .name = "move_charge_at_immigrate",
4788 .read_u64 = mem_cgroup_move_charge_read,
4789 .write_u64 = mem_cgroup_move_charge_write,
4790 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004791 {
4792 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004793 .read_map = mem_cgroup_oom_control_read,
4794 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004795 .register_event = mem_cgroup_oom_register_event,
4796 .unregister_event = mem_cgroup_oom_unregister_event,
4797 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
4798 },
Ying Han406eb0c2011-05-26 16:25:37 -07004799#ifdef CONFIG_NUMA
4800 {
4801 .name = "numa_stat",
4802 .open = mem_control_numa_stat_open,
KAMEZAWA Hiroyuki89577122011-06-15 15:08:41 -07004803 .mode = S_IRUGO,
Ying Han406eb0c2011-05-26 16:25:37 -07004804 },
4805#endif
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004806};
4807
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004808#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4809static struct cftype memsw_cgroup_files[] = {
4810 {
4811 .name = "memsw.usage_in_bytes",
4812 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
4813 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004814 .register_event = mem_cgroup_usage_register_event,
4815 .unregister_event = mem_cgroup_usage_unregister_event,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004816 },
4817 {
4818 .name = "memsw.max_usage_in_bytes",
4819 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
4820 .trigger = mem_cgroup_reset,
4821 .read_u64 = mem_cgroup_read,
4822 },
4823 {
4824 .name = "memsw.limit_in_bytes",
4825 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
4826 .write_string = mem_cgroup_write,
4827 .read_u64 = mem_cgroup_read,
4828 },
4829 {
4830 .name = "memsw.failcnt",
4831 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
4832 .trigger = mem_cgroup_reset,
4833 .read_u64 = mem_cgroup_read,
4834 },
4835};
4836
4837static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
4838{
4839 if (!do_swap_account)
4840 return 0;
4841 return cgroup_add_files(cont, ss, memsw_cgroup_files,
4842 ARRAY_SIZE(memsw_cgroup_files));
4843};
4844#else
4845static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
4846{
4847 return 0;
4848}
4849#endif
4850
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004851static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004852{
4853 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004854 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07004855 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004856 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004857 /*
4858 * This routine is called against possible nodes.
4859 * But it's BUG to call kmalloc() against offline node.
4860 *
4861 * TODO: this routine can waste much memory for nodes which will
4862 * never be onlined. It's better to use memory hotplug callback
4863 * function.
4864 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004865 if (!node_state(node, N_NORMAL_MEMORY))
4866 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08004867 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004868 if (!pn)
4869 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004870
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004871 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4872 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07004873 for_each_lru(l)
4874 INIT_LIST_HEAD(&mz->lists[l]);
Balbir Singhf64c3f52009-09-23 15:56:37 -07004875 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07004876 mz->on_tree = false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004877 mz->mem = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004878 }
Igor Mammedov0a619e52011-11-02 13:38:21 -07004879 memcg->info.nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004880 return 0;
4881}
4882
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004883static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004884{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004885 kfree(memcg->info.nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004886}
4887
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004888static struct mem_cgroup *mem_cgroup_alloc(void)
4889{
4890 struct mem_cgroup *mem;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004891 int size = sizeof(struct mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004892
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004893 /* Can be very big if MAX_NUMNODES is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08004894 if (size < PAGE_SIZE)
Jesper Juhl17295c82011-01-13 15:47:42 -08004895 mem = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004896 else
Jesper Juhl17295c82011-01-13 15:47:42 -08004897 mem = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004898
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07004899 if (!mem)
4900 return NULL;
4901
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004902 mem->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004903 if (!mem->stat)
4904 goto out_free;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07004905 spin_lock_init(&mem->pcp_counter_lock);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004906 return mem;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004907
4908out_free:
4909 if (size < PAGE_SIZE)
4910 kfree(mem);
4911 else
4912 vfree(mem);
4913 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004914}
4915
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004916/*
4917 * At destroying mem_cgroup, references from swap_cgroup can remain.
4918 * (scanning all at force_empty is too costly...)
4919 *
4920 * Instead of clearing all references at force_empty, we remember
4921 * the number of reference from swap_cgroup and free mem_cgroup when
4922 * it goes down to 0.
4923 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004924 * Removal of cgroup itself succeeds regardless of refs from swap.
4925 */
4926
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004927static void __mem_cgroup_free(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004928{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004929 int node;
4930
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004931 mem_cgroup_remove_from_trees(memcg);
4932 free_css_id(&mem_cgroup_subsys, &memcg->css);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004933
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004934 for_each_node_state(node, N_POSSIBLE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004935 free_mem_cgroup_per_zone_info(memcg, node);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004936
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004937 free_percpu(memcg->stat);
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004938 if (sizeof(struct mem_cgroup) < PAGE_SIZE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004939 kfree(memcg);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004940 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004941 vfree(memcg);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004942}
4943
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004944static void mem_cgroup_get(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004945{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004946 atomic_inc(&memcg->refcnt);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004947}
4948
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004949static void __mem_cgroup_put(struct mem_cgroup *memcg, int count)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004950{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004951 if (atomic_sub_and_test(count, &memcg->refcnt)) {
4952 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
4953 __mem_cgroup_free(memcg);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004954 if (parent)
4955 mem_cgroup_put(parent);
4956 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004957}
4958
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004959static void mem_cgroup_put(struct mem_cgroup *memcg)
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004960{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004961 __mem_cgroup_put(memcg, 1);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004962}
4963
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004964/*
4965 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
4966 */
Glauber Costae1aab162011-12-11 21:47:03 +00004967struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004968{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004969 if (!memcg->res.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004970 return NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004971 return mem_cgroup_from_res_counter(memcg->res.parent, res);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004972}
Glauber Costae1aab162011-12-11 21:47:03 +00004973EXPORT_SYMBOL(parent_mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004974
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004975#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4976static void __init enable_swap_cgroup(void)
4977{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004978 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004979 do_swap_account = 1;
4980}
4981#else
4982static void __init enable_swap_cgroup(void)
4983{
4984}
4985#endif
4986
Balbir Singhf64c3f52009-09-23 15:56:37 -07004987static int mem_cgroup_soft_limit_tree_init(void)
4988{
4989 struct mem_cgroup_tree_per_node *rtpn;
4990 struct mem_cgroup_tree_per_zone *rtpz;
4991 int tmp, node, zone;
4992
4993 for_each_node_state(node, N_POSSIBLE) {
4994 tmp = node;
4995 if (!node_state(node, N_NORMAL_MEMORY))
4996 tmp = -1;
4997 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
4998 if (!rtpn)
4999 return 1;
5000
5001 soft_limit_tree.rb_tree_per_node[node] = rtpn;
5002
5003 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
5004 rtpz = &rtpn->rb_tree_per_zone[zone];
5005 rtpz->rb_root = RB_ROOT;
5006 spin_lock_init(&rtpz->lock);
5007 }
5008 }
5009 return 0;
5010}
5011
Li Zefan0eb253e2009-01-15 13:51:25 -08005012static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005013mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
5014{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005015 struct mem_cgroup *memcg, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005016 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005017 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005018
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005019 memcg = mem_cgroup_alloc();
5020 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005021 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08005022
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005023 for_each_node_state(node, N_POSSIBLE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005024 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005025 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07005026
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005027 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005028 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08005029 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005030 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005031 parent = NULL;
Balbir Singhf64c3f52009-09-23 15:56:37 -07005032 if (mem_cgroup_soft_limit_tree_init())
5033 goto free_out;
Hillf Dantona41c58a2011-12-19 17:11:57 -08005034 root_mem_cgroup = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08005035 for_each_possible_cpu(cpu) {
5036 struct memcg_stock_pcp *stock =
5037 &per_cpu(memcg_stock, cpu);
5038 INIT_WORK(&stock->work, drain_local_stock);
5039 }
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07005040 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005041 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005042 parent = mem_cgroup_from_cont(cont->parent);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005043 memcg->use_hierarchy = parent->use_hierarchy;
5044 memcg->oom_kill_disable = parent->oom_kill_disable;
Balbir Singh18f59ea2009-01-07 18:08:07 -08005045 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005046
Balbir Singh18f59ea2009-01-07 18:08:07 -08005047 if (parent && parent->use_hierarchy) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005048 res_counter_init(&memcg->res, &parent->res);
5049 res_counter_init(&memcg->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005050 /*
5051 * We increment refcnt of the parent to ensure that we can
5052 * safely access it on res_counter_charge/uncharge.
5053 * This refcnt will be decremented when freeing this
5054 * mem_cgroup(see mem_cgroup_put).
5055 */
5056 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005057 } else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005058 res_counter_init(&memcg->res, NULL);
5059 res_counter_init(&memcg->memsw, NULL);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005060 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005061 memcg->last_scanned_node = MAX_NUMNODES;
5062 INIT_LIST_HEAD(&memcg->oom_notify);
Balbir Singh6d61ef42009-01-07 18:08:06 -08005063
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005064 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005065 memcg->swappiness = mem_cgroup_swappiness(parent);
5066 atomic_set(&memcg->refcnt, 1);
5067 memcg->move_charge_at_immigrate = 0;
5068 mutex_init(&memcg->thresholds_lock);
5069 return &memcg->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005070free_out:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005071 __mem_cgroup_free(memcg);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005072 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005073}
5074
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07005075static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005076 struct cgroup *cont)
5077{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005078 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07005079
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005080 return mem_cgroup_force_empty(memcg, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005081}
5082
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005083static void mem_cgroup_destroy(struct cgroup_subsys *ss,
5084 struct cgroup *cont)
5085{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005086 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08005087
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005088 kmem_cgroup_destroy(ss, cont);
5089
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005090 mem_cgroup_put(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005091}
5092
5093static int mem_cgroup_populate(struct cgroup_subsys *ss,
5094 struct cgroup *cont)
5095{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005096 int ret;
5097
5098 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
5099 ARRAY_SIZE(mem_cgroup_files));
5100
5101 if (!ret)
5102 ret = register_memsw_files(cont, ss);
Glauber Costae5671df2011-12-11 21:47:01 +00005103
5104 if (!ret)
5105 ret = register_kmem_files(cont, ss);
5106
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005107 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005108}
5109
Daisuke Nishimura02491442010-03-10 15:22:17 -08005110#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005111/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005112#define PRECHARGE_COUNT_AT_ONCE 256
5113static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005114{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005115 int ret = 0;
5116 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005117 struct mem_cgroup *memcg = mc.to;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005118
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005119 if (mem_cgroup_is_root(memcg)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005120 mc.precharge += count;
5121 /* we don't need css_get for root */
5122 return ret;
5123 }
5124 /* try to charge at once */
5125 if (count > 1) {
5126 struct res_counter *dummy;
5127 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005128 * "memcg" cannot be under rmdir() because we've already checked
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005129 * by cgroup_lock_live_cgroup() that it is not removed and we
5130 * are still under the same cgroup_mutex. So we can postpone
5131 * css_get().
5132 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005133 if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy))
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005134 goto one_by_one;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005135 if (do_swap_account && res_counter_charge(&memcg->memsw,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005136 PAGE_SIZE * count, &dummy)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005137 res_counter_uncharge(&memcg->res, PAGE_SIZE * count);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005138 goto one_by_one;
5139 }
5140 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005141 return ret;
5142 }
5143one_by_one:
5144 /* fall back to one by one charge */
5145 while (count--) {
5146 if (signal_pending(current)) {
5147 ret = -EINTR;
5148 break;
5149 }
5150 if (!batch_count--) {
5151 batch_count = PRECHARGE_COUNT_AT_ONCE;
5152 cond_resched();
5153 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005154 ret = __mem_cgroup_try_charge(NULL,
5155 GFP_KERNEL, 1, &memcg, false);
5156 if (ret || !memcg)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005157 /* mem_cgroup_clear_mc() will do uncharge later */
5158 return -ENOMEM;
5159 mc.precharge++;
5160 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005161 return ret;
5162}
5163
5164/**
5165 * is_target_pte_for_mc - check a pte whether it is valid for move charge
5166 * @vma: the vma the pte to be checked belongs
5167 * @addr: the address corresponding to the pte to be checked
5168 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08005169 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005170 *
5171 * Returns
5172 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
5173 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
5174 * move charge. if @target is not NULL, the page is stored in target->page
5175 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08005176 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
5177 * target for charge migration. if @target is not NULL, the entry is stored
5178 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005179 *
5180 * Called with pte lock held.
5181 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005182union mc_target {
5183 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005184 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005185};
5186
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005187enum mc_target_type {
5188 MC_TARGET_NONE, /* not used */
5189 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08005190 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005191};
5192
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005193static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
5194 unsigned long addr, pte_t ptent)
5195{
5196 struct page *page = vm_normal_page(vma, addr, ptent);
5197
5198 if (!page || !page_mapped(page))
5199 return NULL;
5200 if (PageAnon(page)) {
5201 /* we don't move shared anon */
5202 if (!move_anon() || page_mapcount(page) > 2)
5203 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005204 } else if (!move_file())
5205 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005206 return NULL;
5207 if (!get_page_unless_zero(page))
5208 return NULL;
5209
5210 return page;
5211}
5212
5213static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5214 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5215{
5216 int usage_count;
5217 struct page *page = NULL;
5218 swp_entry_t ent = pte_to_swp_entry(ptent);
5219
5220 if (!move_anon() || non_swap_entry(ent))
5221 return NULL;
5222 usage_count = mem_cgroup_count_swap_user(ent, &page);
5223 if (usage_count > 1) { /* we don't move shared anon */
5224 if (page)
5225 put_page(page);
5226 return NULL;
5227 }
5228 if (do_swap_account)
5229 entry->val = ent.val;
5230
5231 return page;
5232}
5233
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005234static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
5235 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5236{
5237 struct page *page = NULL;
5238 struct inode *inode;
5239 struct address_space *mapping;
5240 pgoff_t pgoff;
5241
5242 if (!vma->vm_file) /* anonymous vma */
5243 return NULL;
5244 if (!move_file())
5245 return NULL;
5246
5247 inode = vma->vm_file->f_path.dentry->d_inode;
5248 mapping = vma->vm_file->f_mapping;
5249 if (pte_none(ptent))
5250 pgoff = linear_page_index(vma, addr);
5251 else /* pte_file(ptent) is true */
5252 pgoff = pte_to_pgoff(ptent);
5253
5254 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005255 page = find_get_page(mapping, pgoff);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005256
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005257#ifdef CONFIG_SWAP
5258 /* shmem/tmpfs may report page out on swap: account for that too. */
5259 if (radix_tree_exceptional_entry(page)) {
5260 swp_entry_t swap = radix_to_swp_entry(page);
5261 if (do_swap_account)
5262 *entry = swap;
5263 page = find_get_page(&swapper_space, swap.val);
5264 }
5265#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005266 return page;
5267}
5268
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005269static int is_target_pte_for_mc(struct vm_area_struct *vma,
5270 unsigned long addr, pte_t ptent, union mc_target *target)
5271{
Daisuke Nishimura02491442010-03-10 15:22:17 -08005272 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005273 struct page_cgroup *pc;
5274 int ret = 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005275 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005276
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005277 if (pte_present(ptent))
5278 page = mc_handle_present_pte(vma, addr, ptent);
5279 else if (is_swap_pte(ptent))
5280 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005281 else if (pte_none(ptent) || pte_file(ptent))
5282 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005283
5284 if (!page && !ent.val)
Daisuke Nishimura02491442010-03-10 15:22:17 -08005285 return 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005286 if (page) {
5287 pc = lookup_page_cgroup(page);
5288 /*
5289 * Do only loose check w/o page_cgroup lock.
5290 * mem_cgroup_move_account() checks the pc is valid or not under
5291 * the lock.
5292 */
5293 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
5294 ret = MC_TARGET_PAGE;
5295 if (target)
5296 target->page = page;
5297 }
5298 if (!ret || !target)
5299 put_page(page);
5300 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005301 /* There is a swap entry and a page doesn't exist or isn't charged */
5302 if (ent.val && !ret &&
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005303 css_id(&mc.from->css) == lookup_swap_cgroup(ent)) {
5304 ret = MC_TARGET_SWAP;
5305 if (target)
5306 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005307 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005308 return ret;
5309}
5310
5311static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
5312 unsigned long addr, unsigned long end,
5313 struct mm_walk *walk)
5314{
5315 struct vm_area_struct *vma = walk->private;
5316 pte_t *pte;
5317 spinlock_t *ptl;
5318
Dave Hansen03319322011-03-22 16:32:56 -07005319 split_huge_page_pmd(walk->mm, pmd);
5320
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005321 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5322 for (; addr != end; pte++, addr += PAGE_SIZE)
5323 if (is_target_pte_for_mc(vma, addr, *pte, NULL))
5324 mc.precharge++; /* increment precharge temporarily */
5325 pte_unmap_unlock(pte - 1, ptl);
5326 cond_resched();
5327
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005328 return 0;
5329}
5330
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005331static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
5332{
5333 unsigned long precharge;
5334 struct vm_area_struct *vma;
5335
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005336 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005337 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5338 struct mm_walk mem_cgroup_count_precharge_walk = {
5339 .pmd_entry = mem_cgroup_count_precharge_pte_range,
5340 .mm = mm,
5341 .private = vma,
5342 };
5343 if (is_vm_hugetlb_page(vma))
5344 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005345 walk_page_range(vma->vm_start, vma->vm_end,
5346 &mem_cgroup_count_precharge_walk);
5347 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005348 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005349
5350 precharge = mc.precharge;
5351 mc.precharge = 0;
5352
5353 return precharge;
5354}
5355
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005356static int mem_cgroup_precharge_mc(struct mm_struct *mm)
5357{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005358 unsigned long precharge = mem_cgroup_count_precharge(mm);
5359
5360 VM_BUG_ON(mc.moving_task);
5361 mc.moving_task = current;
5362 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005363}
5364
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005365/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
5366static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005367{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005368 struct mem_cgroup *from = mc.from;
5369 struct mem_cgroup *to = mc.to;
5370
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005371 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005372 if (mc.precharge) {
5373 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
5374 mc.precharge = 0;
5375 }
5376 /*
5377 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
5378 * we must uncharge here.
5379 */
5380 if (mc.moved_charge) {
5381 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
5382 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005383 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005384 /* we must fixup refcnts and charges */
5385 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005386 /* uncharge swap account from the old cgroup */
5387 if (!mem_cgroup_is_root(mc.from))
5388 res_counter_uncharge(&mc.from->memsw,
5389 PAGE_SIZE * mc.moved_swap);
5390 __mem_cgroup_put(mc.from, mc.moved_swap);
5391
5392 if (!mem_cgroup_is_root(mc.to)) {
5393 /*
5394 * we charged both to->res and to->memsw, so we should
5395 * uncharge to->res.
5396 */
5397 res_counter_uncharge(&mc.to->res,
5398 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005399 }
5400 /* we've already done mem_cgroup_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005401 mc.moved_swap = 0;
5402 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005403 memcg_oom_recover(from);
5404 memcg_oom_recover(to);
5405 wake_up_all(&mc.waitq);
5406}
5407
5408static void mem_cgroup_clear_mc(void)
5409{
5410 struct mem_cgroup *from = mc.from;
5411
5412 /*
5413 * we must clear moving_task before waking up waiters at the end of
5414 * task migration.
5415 */
5416 mc.moving_task = NULL;
5417 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005418 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005419 mc.from = NULL;
5420 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005421 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005422 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005423}
5424
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005425static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
5426 struct cgroup *cgroup,
Tejun Heo2f7ee562011-12-12 18:12:21 -08005427 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005428{
Tejun Heo2f7ee562011-12-12 18:12:21 -08005429 struct task_struct *p = cgroup_taskset_first(tset);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005430 int ret = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005431 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005432
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005433 if (memcg->move_charge_at_immigrate) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005434 struct mm_struct *mm;
5435 struct mem_cgroup *from = mem_cgroup_from_task(p);
5436
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005437 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005438
5439 mm = get_task_mm(p);
5440 if (!mm)
5441 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005442 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005443 if (mm->owner == p) {
5444 VM_BUG_ON(mc.from);
5445 VM_BUG_ON(mc.to);
5446 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005447 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005448 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005449 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005450 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005451 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005452 mc.to = memcg;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005453 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005454 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005455
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005456 ret = mem_cgroup_precharge_mc(mm);
5457 if (ret)
5458 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005459 }
5460 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005461 }
5462 return ret;
5463}
5464
5465static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
5466 struct cgroup *cgroup,
Tejun Heo2f7ee562011-12-12 18:12:21 -08005467 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005468{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005469 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005470}
5471
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005472static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
5473 unsigned long addr, unsigned long end,
5474 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005475{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005476 int ret = 0;
5477 struct vm_area_struct *vma = walk->private;
5478 pte_t *pte;
5479 spinlock_t *ptl;
5480
Dave Hansen03319322011-03-22 16:32:56 -07005481 split_huge_page_pmd(walk->mm, pmd);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005482retry:
5483 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5484 for (; addr != end; addr += PAGE_SIZE) {
5485 pte_t ptent = *(pte++);
5486 union mc_target target;
5487 int type;
5488 struct page *page;
5489 struct page_cgroup *pc;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005490 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005491
5492 if (!mc.precharge)
5493 break;
5494
5495 type = is_target_pte_for_mc(vma, addr, ptent, &target);
5496 switch (type) {
5497 case MC_TARGET_PAGE:
5498 page = target.page;
5499 if (isolate_lru_page(page))
5500 goto put;
5501 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07005502 if (!mem_cgroup_move_account(page, 1, pc,
5503 mc.from, mc.to, false)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005504 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005505 /* we uncharge from mc.from later. */
5506 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005507 }
5508 putback_lru_page(page);
5509put: /* is_target_pte_for_mc() gets the page */
5510 put_page(page);
5511 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005512 case MC_TARGET_SWAP:
5513 ent = target.ent;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005514 if (!mem_cgroup_move_swap_account(ent,
5515 mc.from, mc.to, false)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08005516 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005517 /* we fixup refcnts and charges later. */
5518 mc.moved_swap++;
5519 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08005520 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005521 default:
5522 break;
5523 }
5524 }
5525 pte_unmap_unlock(pte - 1, ptl);
5526 cond_resched();
5527
5528 if (addr != end) {
5529 /*
5530 * We have consumed all precharges we got in can_attach().
5531 * We try charge one by one, but don't do any additional
5532 * charges to mc.to if we have failed in charge once in attach()
5533 * phase.
5534 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005535 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005536 if (!ret)
5537 goto retry;
5538 }
5539
5540 return ret;
5541}
5542
5543static void mem_cgroup_move_charge(struct mm_struct *mm)
5544{
5545 struct vm_area_struct *vma;
5546
5547 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005548retry:
5549 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
5550 /*
5551 * Someone who are holding the mmap_sem might be waiting in
5552 * waitq. So we cancel all extra charges, wake up all waiters,
5553 * and retry. Because we cancel precharges, we might not be able
5554 * to move enough charges, but moving charge is a best-effort
5555 * feature anyway, so it wouldn't be a big problem.
5556 */
5557 __mem_cgroup_clear_mc();
5558 cond_resched();
5559 goto retry;
5560 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005561 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5562 int ret;
5563 struct mm_walk mem_cgroup_move_charge_walk = {
5564 .pmd_entry = mem_cgroup_move_charge_pte_range,
5565 .mm = mm,
5566 .private = vma,
5567 };
5568 if (is_vm_hugetlb_page(vma))
5569 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005570 ret = walk_page_range(vma->vm_start, vma->vm_end,
5571 &mem_cgroup_move_charge_walk);
5572 if (ret)
5573 /*
5574 * means we have consumed all precharges and failed in
5575 * doing additional charge. Just abandon here.
5576 */
5577 break;
5578 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005579 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005580}
5581
Balbir Singh67e465a2008-02-07 00:13:54 -08005582static void mem_cgroup_move_task(struct cgroup_subsys *ss,
5583 struct cgroup *cont,
Tejun Heo2f7ee562011-12-12 18:12:21 -08005584 struct cgroup_taskset *tset)
Balbir Singh67e465a2008-02-07 00:13:54 -08005585{
Tejun Heo2f7ee562011-12-12 18:12:21 -08005586 struct task_struct *p = cgroup_taskset_first(tset);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005587 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005588
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005589 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005590 if (mc.to)
5591 mem_cgroup_move_charge(mm);
5592 put_swap_token(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005593 mmput(mm);
5594 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005595 if (mc.to)
5596 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08005597}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005598#else /* !CONFIG_MMU */
5599static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
5600 struct cgroup *cgroup,
Tejun Heo2f7ee562011-12-12 18:12:21 -08005601 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005602{
5603 return 0;
5604}
5605static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
5606 struct cgroup *cgroup,
Tejun Heo2f7ee562011-12-12 18:12:21 -08005607 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005608{
5609}
5610static void mem_cgroup_move_task(struct cgroup_subsys *ss,
5611 struct cgroup *cont,
Tejun Heo2f7ee562011-12-12 18:12:21 -08005612 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005613{
5614}
5615#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08005616
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005617struct cgroup_subsys mem_cgroup_subsys = {
5618 .name = "memory",
5619 .subsys_id = mem_cgroup_subsys_id,
5620 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005621 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005622 .destroy = mem_cgroup_destroy,
5623 .populate = mem_cgroup_populate,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005624 .can_attach = mem_cgroup_can_attach,
5625 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08005626 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005627 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005628 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005629};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005630
5631#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08005632static int __init enable_swap_account(char *s)
5633{
5634 /* consider enabled if no parameter or 1 is given */
Michal Hockoa2c89902011-05-24 17:12:50 -07005635 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08005636 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07005637 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08005638 really_do_swap_account = 0;
5639 return 1;
5640}
Michal Hockoa2c89902011-05-24 17:12:50 -07005641__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005642
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005643#endif