blob: 4c53e971749ee9d420f71210a25749cf15d25d35 [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 Hiroyuki8c7c6e32009-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 Hiroyuki08e552c62009-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 Hiroyuki453a9bf2011-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 Hiroyuki453a9bf2011-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 {
Johannes Weiner6290df52012-01-12 17:18:10 -0800137 struct lruvec lruvec;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700138 unsigned long count[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800139
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800140 struct mem_cgroup_reclaim_iter reclaim_iter[DEF_PRIORITY + 1];
141
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800142 struct zone_reclaim_stat reclaim_stat;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700143 struct rb_node tree_node; /* RB tree node */
144 unsigned long long usage_in_excess;/* Set to the value by which */
145 /* the soft limit is exceeded*/
146 bool on_tree;
Balbir Singh4e416952009-09-23 15:56:39 -0700147 struct mem_cgroup *mem; /* Back pointer, we cannot */
148 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800149};
150/* Macro for accessing counter */
151#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
152
153struct mem_cgroup_per_node {
154 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
155};
156
157struct mem_cgroup_lru_info {
158 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
159};
160
161/*
Balbir Singhf64c3f52009-09-23 15:56:37 -0700162 * Cgroups above their limits are maintained in a RB-Tree, independent of
163 * their hierarchy representation
164 */
165
166struct mem_cgroup_tree_per_zone {
167 struct rb_root rb_root;
168 spinlock_t lock;
169};
170
171struct mem_cgroup_tree_per_node {
172 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
173};
174
175struct mem_cgroup_tree {
176 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
177};
178
179static struct mem_cgroup_tree soft_limit_tree __read_mostly;
180
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800181struct mem_cgroup_threshold {
182 struct eventfd_ctx *eventfd;
183 u64 threshold;
184};
185
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700186/* For threshold */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800187struct mem_cgroup_threshold_ary {
188 /* An array index points to threshold just below usage. */
Phil Carmody5407a562010-05-26 14:42:42 -0700189 int current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800190 /* Size of entries[] */
191 unsigned int size;
192 /* Array of thresholds */
193 struct mem_cgroup_threshold entries[0];
194};
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700195
196struct mem_cgroup_thresholds {
197 /* Primary thresholds array */
198 struct mem_cgroup_threshold_ary *primary;
199 /*
200 * Spare threshold array.
201 * This is needed to make mem_cgroup_unregister_event() "never fail".
202 * It must be able to store at least primary->size - 1 entries.
203 */
204 struct mem_cgroup_threshold_ary *spare;
205};
206
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700207/* for OOM */
208struct mem_cgroup_eventfd_list {
209 struct list_head list;
210 struct eventfd_ctx *eventfd;
211};
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800212
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700213static void mem_cgroup_threshold(struct mem_cgroup *memcg);
214static void mem_cgroup_oom_notify(struct mem_cgroup *memcg);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800215
Balbir Singhf64c3f52009-09-23 15:56:37 -0700216/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800217 * The memory controller data structure. The memory controller controls both
218 * page cache and RSS per cgroup. We would eventually like to provide
219 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
220 * to help the administrator determine what knobs to tune.
221 *
222 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800223 * we hit the water mark. May be even add a low water mark, such that
224 * no reclaim occurs from a cgroup at it's low water mark, this is
225 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800226 */
227struct mem_cgroup {
228 struct cgroup_subsys_state css;
229 /*
230 * the counter to account for memory usage
231 */
232 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800233 /*
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800234 * the counter to account for mem+swap usage.
235 */
236 struct res_counter memsw;
237 /*
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800238 * Per cgroup active and inactive list, similar to the
239 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800240 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800241 struct mem_cgroup_lru_info info;
Ying Han889976d2011-05-26 16:25:33 -0700242 int last_scanned_node;
243#if MAX_NUMNODES > 1
244 nodemask_t scan_nodes;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700245 atomic_t numainfo_events;
246 atomic_t numainfo_updating;
Ying Han889976d2011-05-26 16:25:33 -0700247#endif
Balbir Singh18f59ea2009-01-07 18:08:07 -0800248 /*
249 * Should the accounting and control be hierarchical, per subtree?
250 */
251 bool use_hierarchy;
Michal Hocko79dfdac2011-07-26 16:08:23 -0700252
253 bool oom_lock;
254 atomic_t under_oom;
255
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800256 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800257
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -0700258 int swappiness;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -0700259 /* OOM-Killer disable */
260 int oom_kill_disable;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800261
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700262 /* set when res.limit == memsw.limit */
263 bool memsw_is_minimum;
264
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800265 /* protect arrays of thresholds */
266 struct mutex thresholds_lock;
267
268 /* thresholds for memory usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700269 struct mem_cgroup_thresholds thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700270
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800271 /* thresholds for mem+swap usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700272 struct mem_cgroup_thresholds memsw_thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700273
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700274 /* For oom notifier event fd */
275 struct list_head oom_notify;
Johannes Weiner185efc02011-09-14 16:21:58 -0700276
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800277 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800278 * Should we move charges of a task when a task is moved into this
279 * mem_cgroup ? And what type of charges should we move ?
280 */
281 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800282 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800283 * percpu counter.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800284 */
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800285 struct mem_cgroup_stat_cpu *stat;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700286 /*
287 * used when a cpu is offlined or other synchronizations
288 * See mem_cgroup_read_stat().
289 */
290 struct mem_cgroup_stat_cpu nocpu_base;
291 spinlock_t pcp_counter_lock;
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000292
293#ifdef CONFIG_INET
294 struct tcp_memcontrol tcp_mem;
295#endif
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800296};
297
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800298/* Stuffs for move charges at task migration. */
299/*
300 * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a
301 * left-shifted bitmap of these types.
302 */
303enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800304 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700305 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800306 NR_MOVE_TYPE,
307};
308
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800309/* "mc" and its members are protected by cgroup_mutex */
310static struct move_charge_struct {
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -0800311 spinlock_t lock; /* for from, to */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800312 struct mem_cgroup *from;
313 struct mem_cgroup *to;
314 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800315 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800316 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800317 struct task_struct *moving_task; /* a task moving charges */
318 wait_queue_head_t waitq; /* a waitq for other context */
319} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700320 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800321 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
322};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800323
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700324static bool move_anon(void)
325{
326 return test_bit(MOVE_CHARGE_TYPE_ANON,
327 &mc.to->move_charge_at_immigrate);
328}
329
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700330static bool move_file(void)
331{
332 return test_bit(MOVE_CHARGE_TYPE_FILE,
333 &mc.to->move_charge_at_immigrate);
334}
335
Balbir Singh4e416952009-09-23 15:56:39 -0700336/*
337 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
338 * limit reclaim to prevent infinite loops, if they ever occur.
339 */
340#define MEM_CGROUP_MAX_RECLAIM_LOOPS (100)
341#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2)
342
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800343enum charge_type {
344 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
345 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700346 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700347 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800348 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700349 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700350 NR_CHARGE_TYPE,
351};
352
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800353/* for encoding cft->private value on file */
Glauber Costa65c64ce2011-12-22 01:02:27 +0000354#define _MEM (0)
355#define _MEMSWAP (1)
356#define _OOM_TYPE (2)
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800357#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
358#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
359#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700360/* Used for OOM nofiier */
361#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800362
Balbir Singh75822b42009-09-23 15:56:38 -0700363/*
364 * Reclaim flags for mem_cgroup_hierarchical_reclaim
365 */
366#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
367#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
368#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
369#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
370
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700371static void mem_cgroup_get(struct mem_cgroup *memcg);
372static void mem_cgroup_put(struct mem_cgroup *memcg);
Glauber Costae1aab162011-12-11 21:47:03 +0000373
374/* Writing them here to avoid exposing memcg's inner layout */
375#ifdef CONFIG_CGROUP_MEM_RES_CTLR_KMEM
376#ifdef CONFIG_INET
377#include <net/sock.h>
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000378#include <net/ip.h>
Glauber Costae1aab162011-12-11 21:47:03 +0000379
380static bool mem_cgroup_is_root(struct mem_cgroup *memcg);
381void sock_update_memcg(struct sock *sk)
382{
Glauber Costae1aab162011-12-11 21:47:03 +0000383 if (static_branch(&memcg_socket_limit_enabled)) {
384 struct mem_cgroup *memcg;
385
386 BUG_ON(!sk->sk_prot->proto_cgroup);
387
Glauber Costaf3f511e2012-01-05 20:16:39 +0000388 /* Socket cloning can throw us here with sk_cgrp already
389 * filled. It won't however, necessarily happen from
390 * process context. So the test for root memcg given
391 * the current task's memcg won't help us in this case.
392 *
393 * Respecting the original socket's memcg is a better
394 * decision in this case.
395 */
396 if (sk->sk_cgrp) {
397 BUG_ON(mem_cgroup_is_root(sk->sk_cgrp->memcg));
398 mem_cgroup_get(sk->sk_cgrp->memcg);
399 return;
400 }
401
Glauber Costae1aab162011-12-11 21:47:03 +0000402 rcu_read_lock();
403 memcg = mem_cgroup_from_task(current);
404 if (!mem_cgroup_is_root(memcg)) {
405 mem_cgroup_get(memcg);
406 sk->sk_cgrp = sk->sk_prot->proto_cgroup(memcg);
407 }
408 rcu_read_unlock();
409 }
410}
411EXPORT_SYMBOL(sock_update_memcg);
412
413void sock_release_memcg(struct sock *sk)
414{
415 if (static_branch(&memcg_socket_limit_enabled) && sk->sk_cgrp) {
416 struct mem_cgroup *memcg;
417 WARN_ON(!sk->sk_cgrp->memcg);
418 memcg = sk->sk_cgrp->memcg;
419 mem_cgroup_put(memcg);
420 }
421}
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000422
423struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg)
424{
425 if (!memcg || mem_cgroup_is_root(memcg))
426 return NULL;
427
428 return &memcg->tcp_mem.cg_proto;
429}
430EXPORT_SYMBOL(tcp_proto_cgroup);
Glauber Costae1aab162011-12-11 21:47:03 +0000431#endif /* CONFIG_INET */
432#endif /* CONFIG_CGROUP_MEM_RES_CTLR_KMEM */
433
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700434static void drain_all_stock_async(struct mem_cgroup *memcg);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800435
Balbir Singhf64c3f52009-09-23 15:56:37 -0700436static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700437mem_cgroup_zoneinfo(struct mem_cgroup *memcg, int nid, int zid)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700438{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700439 return &memcg->info.nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700440}
441
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700442struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100443{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700444 return &memcg->css;
Wu Fengguangd3242362009-12-16 12:19:59 +0100445}
446
Balbir Singhf64c3f52009-09-23 15:56:37 -0700447static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700448page_cgroup_zoneinfo(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700449{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700450 int nid = page_to_nid(page);
451 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700452
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700453 return mem_cgroup_zoneinfo(memcg, nid, zid);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700454}
455
456static struct mem_cgroup_tree_per_zone *
457soft_limit_tree_node_zone(int nid, int zid)
458{
459 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
460}
461
462static struct mem_cgroup_tree_per_zone *
463soft_limit_tree_from_page(struct page *page)
464{
465 int nid = page_to_nid(page);
466 int zid = page_zonenum(page);
467
468 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
469}
470
471static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700472__mem_cgroup_insert_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700473 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700474 struct mem_cgroup_tree_per_zone *mctz,
475 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700476{
477 struct rb_node **p = &mctz->rb_root.rb_node;
478 struct rb_node *parent = NULL;
479 struct mem_cgroup_per_zone *mz_node;
480
481 if (mz->on_tree)
482 return;
483
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700484 mz->usage_in_excess = new_usage_in_excess;
485 if (!mz->usage_in_excess)
486 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700487 while (*p) {
488 parent = *p;
489 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
490 tree_node);
491 if (mz->usage_in_excess < mz_node->usage_in_excess)
492 p = &(*p)->rb_left;
493 /*
494 * We can't avoid mem cgroups that are over their soft
495 * limit by the same amount
496 */
497 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
498 p = &(*p)->rb_right;
499 }
500 rb_link_node(&mz->tree_node, parent, p);
501 rb_insert_color(&mz->tree_node, &mctz->rb_root);
502 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700503}
504
505static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700506__mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singh4e416952009-09-23 15:56:39 -0700507 struct mem_cgroup_per_zone *mz,
508 struct mem_cgroup_tree_per_zone *mctz)
509{
510 if (!mz->on_tree)
511 return;
512 rb_erase(&mz->tree_node, &mctz->rb_root);
513 mz->on_tree = false;
514}
515
516static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700517mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700518 struct mem_cgroup_per_zone *mz,
519 struct mem_cgroup_tree_per_zone *mctz)
520{
521 spin_lock(&mctz->lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700522 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700523 spin_unlock(&mctz->lock);
524}
525
Balbir Singhf64c3f52009-09-23 15:56:37 -0700526
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700527static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700528{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700529 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700530 struct mem_cgroup_per_zone *mz;
531 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700532 int nid = page_to_nid(page);
533 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700534 mctz = soft_limit_tree_from_page(page);
535
536 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700537 * Necessary to update all ancestors when hierarchy is used.
538 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700539 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700540 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
541 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
542 excess = res_counter_soft_limit_excess(&memcg->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700543 /*
544 * We have to update the tree if mz is on RB-tree or
545 * mem is over its softlimit.
546 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700547 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700548 spin_lock(&mctz->lock);
549 /* if on-tree, remove it */
550 if (mz->on_tree)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700551 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700552 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700553 * Insert again. mz->usage_in_excess will be updated.
554 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700555 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700556 __mem_cgroup_insert_exceeded(memcg, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700557 spin_unlock(&mctz->lock);
558 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700559 }
560}
561
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700562static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700563{
564 int node, zone;
565 struct mem_cgroup_per_zone *mz;
566 struct mem_cgroup_tree_per_zone *mctz;
567
568 for_each_node_state(node, N_POSSIBLE) {
569 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700570 mz = mem_cgroup_zoneinfo(memcg, node, zone);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700571 mctz = soft_limit_tree_node_zone(node, zone);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700572 mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700573 }
574 }
575}
576
Balbir Singh4e416952009-09-23 15:56:39 -0700577static struct mem_cgroup_per_zone *
578__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
579{
580 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700581 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700582
583retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700584 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700585 rightmost = rb_last(&mctz->rb_root);
586 if (!rightmost)
587 goto done; /* Nothing to reclaim from */
588
589 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
590 /*
591 * Remove the node now but someone else can add it back,
592 * we will to add it back at the end of reclaim to its correct
593 * position in the tree.
594 */
595 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
596 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
597 !css_tryget(&mz->mem->css))
598 goto retry;
599done:
600 return mz;
601}
602
603static struct mem_cgroup_per_zone *
604mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
605{
606 struct mem_cgroup_per_zone *mz;
607
608 spin_lock(&mctz->lock);
609 mz = __mem_cgroup_largest_soft_limit_node(mctz);
610 spin_unlock(&mctz->lock);
611 return mz;
612}
613
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700614/*
615 * Implementation Note: reading percpu statistics for memcg.
616 *
617 * Both of vmstat[] and percpu_counter has threshold and do periodic
618 * synchronization to implement "quick" read. There are trade-off between
619 * reading cost and precision of value. Then, we may have a chance to implement
620 * a periodic synchronizion of counter in memcg's counter.
621 *
622 * But this _read() function is used for user interface now. The user accounts
623 * memory usage by memory cgroup and he _always_ requires exact value because
624 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
625 * have to visit all online cpus and make sum. So, for now, unnecessary
626 * synchronization is not implemented. (just implemented for cpu hotplug)
627 *
628 * If there are kernel internal actions which can make use of some not-exact
629 * value, and reading all cpu value can be performance bottleneck in some
630 * common workload, threashold and synchonization as vmstat[] should be
631 * implemented.
632 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700633static long mem_cgroup_read_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700634 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800635{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700636 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800637 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800638
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700639 get_online_cpus();
640 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700641 val += per_cpu(memcg->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700642#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700643 spin_lock(&memcg->pcp_counter_lock);
644 val += memcg->nocpu_base.count[idx];
645 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700646#endif
647 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800648 return val;
649}
650
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700651static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700652 bool charge)
653{
654 int val = (charge) ? 1 : -1;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700655 this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAPOUT], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700656}
657
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700658static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg,
Johannes Weinere9f89742011-03-23 16:42:37 -0700659 enum mem_cgroup_events_index idx)
660{
661 unsigned long val = 0;
662 int cpu;
663
664 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700665 val += per_cpu(memcg->stat->events[idx], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -0700666#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700667 spin_lock(&memcg->pcp_counter_lock);
668 val += memcg->nocpu_base.events[idx];
669 spin_unlock(&memcg->pcp_counter_lock);
Johannes Weinere9f89742011-03-23 16:42:37 -0700670#endif
671 return val;
672}
673
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700674static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800675 bool file, int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800676{
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800677 preempt_disable();
678
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800679 if (file)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700680 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE],
681 nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800682 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700683 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS],
684 nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700685
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800686 /* pagein of a big page is an event. So, ignore page size */
687 if (nr_pages > 0)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700688 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800689 else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700690 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800691 nr_pages = -nr_pages; /* for event */
692 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800693
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700694 __this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_COUNT], nr_pages);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800695
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800696 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800697}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800698
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700699unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700700mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg, int nid, int zid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700701 unsigned int lru_mask)
Ying Han889976d2011-05-26 16:25:33 -0700702{
703 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700704 enum lru_list l;
705 unsigned long ret = 0;
706
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700707 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700708
709 for_each_lru(l) {
710 if (BIT(l) & lru_mask)
711 ret += MEM_CGROUP_ZSTAT(mz, l);
712 }
713 return ret;
714}
715
716static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700717mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700718 int nid, unsigned int lru_mask)
719{
Ying Han889976d2011-05-26 16:25:33 -0700720 u64 total = 0;
721 int zid;
722
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700723 for (zid = 0; zid < MAX_NR_ZONES; zid++)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700724 total += mem_cgroup_zone_nr_lru_pages(memcg,
725 nid, zid, lru_mask);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700726
Ying Han889976d2011-05-26 16:25:33 -0700727 return total;
728}
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700729
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700730static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700731 unsigned int lru_mask)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800732{
Ying Han889976d2011-05-26 16:25:33 -0700733 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800734 u64 total = 0;
735
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700736 for_each_node_state(nid, N_HIGH_MEMORY)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700737 total += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800738 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800739}
740
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800741static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg,
742 enum mem_cgroup_events_target target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800743{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700744 unsigned long val, next;
745
Steven Rostedt47994012011-11-02 13:38:33 -0700746 val = __this_cpu_read(memcg->stat->events[MEM_CGROUP_EVENTS_COUNT]);
747 next = __this_cpu_read(memcg->stat->targets[target]);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700748 /* from time_after() in jiffies.h */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800749 if ((long)next - (long)val < 0) {
750 switch (target) {
751 case MEM_CGROUP_TARGET_THRESH:
752 next = val + THRESHOLDS_EVENTS_TARGET;
753 break;
754 case MEM_CGROUP_TARGET_SOFTLIMIT:
755 next = val + SOFTLIMIT_EVENTS_TARGET;
756 break;
757 case MEM_CGROUP_TARGET_NUMAINFO:
758 next = val + NUMAINFO_EVENTS_TARGET;
759 break;
760 default:
761 break;
762 }
763 __this_cpu_write(memcg->stat->targets[target], next);
764 return true;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700765 }
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800766 return false;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800767}
768
769/*
770 * Check events in order.
771 *
772 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700773static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800774{
Steven Rostedt47994012011-11-02 13:38:33 -0700775 preempt_disable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800776 /* threshold event is triggered in finer grain than soft limit */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800777 if (unlikely(mem_cgroup_event_ratelimit(memcg,
778 MEM_CGROUP_TARGET_THRESH))) {
779 bool do_softlimit, do_numainfo;
780
781 do_softlimit = mem_cgroup_event_ratelimit(memcg,
782 MEM_CGROUP_TARGET_SOFTLIMIT);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700783#if MAX_NUMNODES > 1
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800784 do_numainfo = mem_cgroup_event_ratelimit(memcg,
785 MEM_CGROUP_TARGET_NUMAINFO);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700786#endif
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800787 preempt_enable();
788
789 mem_cgroup_threshold(memcg);
790 if (unlikely(do_softlimit))
791 mem_cgroup_update_tree(memcg, page);
792#if MAX_NUMNODES > 1
793 if (unlikely(do_numainfo))
794 atomic_inc(&memcg->numainfo_events);
795#endif
796 } else
797 preempt_enable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800798}
799
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000800struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800801{
802 return container_of(cgroup_subsys_state(cont,
803 mem_cgroup_subsys_id), struct mem_cgroup,
804 css);
805}
806
Balbir Singhcf475ad2008-04-29 01:00:16 -0700807struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800808{
Balbir Singh31a78f22008-09-28 23:09:31 +0100809 /*
810 * mm_update_next_owner() may clear mm->owner to NULL
811 * if it races with swapoff, page migration, etc.
812 * So this can be called with p == NULL.
813 */
814 if (unlikely(!p))
815 return NULL;
816
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800817 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
818 struct mem_cgroup, css);
819}
820
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700821struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800822{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700823 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700824
825 if (!mm)
826 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800827 /*
828 * Because we have no locks, mm->owner's may be being moved to other
829 * cgroup. We use css_tryget() here even if this looks
830 * pessimistic (rather than adding locks here).
831 */
832 rcu_read_lock();
833 do {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700834 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
835 if (unlikely(!memcg))
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800836 break;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700837 } while (!css_tryget(&memcg->css));
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800838 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700839 return memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800840}
841
Johannes Weiner56600482012-01-12 17:17:59 -0800842/**
843 * mem_cgroup_iter - iterate over memory cgroup hierarchy
844 * @root: hierarchy root
845 * @prev: previously returned memcg, NULL on first invocation
846 * @reclaim: cookie for shared reclaim walks, NULL for full walks
847 *
848 * Returns references to children of the hierarchy below @root, or
849 * @root itself, or %NULL after a full round-trip.
850 *
851 * Caller must pass the return value in @prev on subsequent
852 * invocations for reference counting, or use mem_cgroup_iter_break()
853 * to cancel a hierarchy walk before the round-trip is complete.
854 *
855 * Reclaimers can specify a zone and a priority level in @reclaim to
856 * divide up the memcgs in the hierarchy among all concurrent
857 * reclaimers operating on the same zone and priority.
858 */
859struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
860 struct mem_cgroup *prev,
861 struct mem_cgroup_reclaim_cookie *reclaim)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700862{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800863 struct mem_cgroup *memcg = NULL;
864 int id = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700865
Johannes Weiner56600482012-01-12 17:17:59 -0800866 if (mem_cgroup_disabled())
867 return NULL;
868
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700869 if (!root)
870 root = root_mem_cgroup;
871
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800872 if (prev && !reclaim)
873 id = css_id(&prev->css);
874
875 if (prev && prev != root)
876 css_put(&prev->css);
877
878 if (!root->use_hierarchy && root != root_mem_cgroup) {
879 if (prev)
880 return NULL;
881 return root;
882 }
883
884 while (!memcg) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800885 struct mem_cgroup_reclaim_iter *uninitialized_var(iter);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800886 struct cgroup_subsys_state *css;
887
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800888 if (reclaim) {
889 int nid = zone_to_nid(reclaim->zone);
890 int zid = zone_idx(reclaim->zone);
891 struct mem_cgroup_per_zone *mz;
892
893 mz = mem_cgroup_zoneinfo(root, nid, zid);
894 iter = &mz->reclaim_iter[reclaim->priority];
895 if (prev && reclaim->generation != iter->generation)
896 return NULL;
897 id = iter->position;
898 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800899
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700900 rcu_read_lock();
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800901 css = css_get_next(&mem_cgroup_subsys, id + 1, &root->css, &id);
902 if (css) {
903 if (css == &root->css || css_tryget(css))
904 memcg = container_of(css,
905 struct mem_cgroup, css);
906 } else
907 id = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700908 rcu_read_unlock();
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700909
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800910 if (reclaim) {
911 iter->position = id;
912 if (!css)
913 iter->generation++;
914 else if (!prev && memcg)
915 reclaim->generation = iter->generation;
916 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800917
918 if (prev && !css)
919 return NULL;
920 }
921 return memcg;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700922}
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800923
Johannes Weiner56600482012-01-12 17:17:59 -0800924/**
925 * mem_cgroup_iter_break - abort a hierarchy walk prematurely
926 * @root: hierarchy root
927 * @prev: last visited hierarchy member as returned by mem_cgroup_iter()
928 */
929void mem_cgroup_iter_break(struct mem_cgroup *root,
930 struct mem_cgroup *prev)
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800931{
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) {
Ying Han456f9982011-05-26 16:25:38 -0700971 case PGFAULT:
Johannes Weiner0e574a92012-01-12 17:18:35 -0800972 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]);
973 break;
974 case PGMAJFAULT:
975 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]);
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
Johannes Weiner925b7672012-01-12 17:18:15 -0800985/**
986 * mem_cgroup_zone_lruvec - get the lru list vector for a zone and memcg
987 * @zone: zone of the wanted lruvec
988 * @mem: memcg of the wanted lruvec
989 *
990 * Returns the lru list vector holding pages for the given @zone and
991 * @mem. This can be the global zone lruvec, if the memory controller
992 * is disabled.
993 */
994struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
995 struct mem_cgroup *memcg)
996{
997 struct mem_cgroup_per_zone *mz;
998
999 if (mem_cgroup_disabled())
1000 return &zone->lruvec;
1001
1002 mz = mem_cgroup_zoneinfo(memcg, zone_to_nid(zone), zone_idx(zone));
1003 return &mz->lruvec;
1004}
1005
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001006/*
1007 * Following LRU functions are allowed to be used without PCG_LOCK.
1008 * Operations are called by routine of global LRU independently from memcg.
1009 * What we have to take care of here is validness of pc->mem_cgroup.
1010 *
1011 * Changes to pc->mem_cgroup happens when
1012 * 1. charge
1013 * 2. moving account
1014 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
1015 * It is added to LRU before charge.
1016 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
1017 * When moving account, the page is not on LRU. It's isolated.
1018 */
1019
Johannes Weiner925b7672012-01-12 17:18:15 -08001020/**
1021 * mem_cgroup_lru_add_list - account for adding an lru page and return lruvec
1022 * @zone: zone of the page
1023 * @page: the page
1024 * @lru: current lru
1025 *
1026 * This function accounts for @page being added to @lru, and returns
1027 * the lruvec for the given @zone and the memcg @page is charged to.
1028 *
1029 * The callsite is then responsible for physically linking the page to
1030 * the returned lruvec->lists[@lru].
Minchan Kim3f58a822011-03-22 16:32:53 -07001031 */
Johannes Weiner925b7672012-01-12 17:18:15 -08001032struct lruvec *mem_cgroup_lru_add_list(struct zone *zone, struct page *page,
1033 enum lru_list lru)
Minchan Kim3f58a822011-03-22 16:32:53 -07001034{
1035 struct mem_cgroup_per_zone *mz;
Johannes Weiner925b7672012-01-12 17:18:15 -08001036 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001037 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001038
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001039 if (mem_cgroup_disabled())
Johannes Weiner925b7672012-01-12 17:18:15 -08001040 return &zone->lruvec;
Christoph Lameterb69408e2008-10-18 20:26:14 -07001041
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001042 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -07001043 VM_BUG_ON(PageCgroupAcctLRU(pc));
Johannes Weinera61ed3c2011-11-02 13:38:29 -07001044 /*
1045 * putback: charge:
1046 * SetPageLRU SetPageCgroupUsed
1047 * smp_mb smp_mb
1048 * PageCgroupUsed && add to memcg LRU PageLRU && add to memcg LRU
1049 *
1050 * Ensure that one of the two sides adds the page to the memcg
1051 * LRU during a race.
1052 */
1053 smp_mb();
Johannes Weiner925b7672012-01-12 17:18:15 -08001054 /*
1055 * If the page is uncharged, it may be freed soon, but it
1056 * could also be swap cache (readahead, swapoff) that needs to
1057 * be reclaimable in the future. root_mem_cgroup will babysit
1058 * it for the time being.
1059 */
1060 if (PageCgroupUsed(pc)) {
1061 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
1062 smp_rmb();
1063 memcg = pc->mem_cgroup;
1064 SetPageCgroupAcctLRU(pc);
1065 } else
1066 memcg = root_mem_cgroup;
1067 mz = page_cgroup_zoneinfo(memcg, page);
1068 /* compound_order() is stabilized through lru_lock */
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08001069 MEM_CGROUP_ZSTAT(mz, lru) += 1 << compound_order(page);
Johannes Weiner925b7672012-01-12 17:18:15 -08001070 return &mz->lruvec;
1071}
1072
1073/**
1074 * mem_cgroup_lru_del_list - account for removing an lru page
1075 * @page: the page
1076 * @lru: target lru
1077 *
1078 * This function accounts for @page being removed from @lru.
1079 *
1080 * The callsite is then responsible for physically unlinking
1081 * @page->lru.
1082 */
1083void mem_cgroup_lru_del_list(struct page *page, enum lru_list lru)
1084{
1085 struct mem_cgroup_per_zone *mz;
1086 struct mem_cgroup *memcg;
1087 struct page_cgroup *pc;
1088
1089 if (mem_cgroup_disabled())
1090 return;
1091
1092 pc = lookup_page_cgroup(page);
1093 /*
1094 * root_mem_cgroup babysits uncharged LRU pages, but
1095 * PageCgroupUsed is cleared when the page is about to get
1096 * freed. PageCgroupAcctLRU remembers whether the
1097 * LRU-accounting happened against pc->mem_cgroup or
1098 * root_mem_cgroup.
1099 */
1100 if (TestClearPageCgroupAcctLRU(pc)) {
1101 VM_BUG_ON(!pc->mem_cgroup);
1102 memcg = pc->mem_cgroup;
1103 } else
1104 memcg = root_mem_cgroup;
1105 mz = page_cgroup_zoneinfo(memcg, page);
1106 /* huge page split is done under lru_lock. so, we have no races. */
1107 MEM_CGROUP_ZSTAT(mz, lru) -= 1 << compound_order(page);
1108}
1109
1110void mem_cgroup_lru_del(struct page *page)
1111{
1112 mem_cgroup_lru_del_list(page, page_lru(page));
1113}
1114
1115/**
1116 * mem_cgroup_lru_move_lists - account for moving a page between lrus
1117 * @zone: zone of the page
1118 * @page: the page
1119 * @from: current lru
1120 * @to: target lru
1121 *
1122 * This function accounts for @page being moved between the lrus @from
1123 * and @to, and returns the lruvec for the given @zone and the memcg
1124 * @page is charged to.
1125 *
1126 * The callsite is then responsible for physically relinking
1127 * @page->lru to the returned lruvec->lists[@to].
1128 */
1129struct lruvec *mem_cgroup_lru_move_lists(struct zone *zone,
1130 struct page *page,
1131 enum lru_list from,
1132 enum lru_list to)
1133{
1134 /* XXX: Optimize this, especially for @from == @to */
1135 mem_cgroup_lru_del_list(page, from);
1136 return mem_cgroup_lru_add_list(zone, page, to);
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001137}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001138
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001139/*
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001140 * At handling SwapCache and other FUSE stuff, pc->mem_cgroup may be changed
1141 * while it's linked to lru because the page may be reused after it's fully
1142 * uncharged. To handle that, unlink page_cgroup from LRU when charge it again.
1143 * It's done under lock_page and expected that zone->lru_lock isnever held.
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001144 */
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001145static void mem_cgroup_lru_del_before_commit(struct page *page)
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001146{
Johannes Weiner925b7672012-01-12 17:18:15 -08001147 enum lru_list lru;
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001148 unsigned long flags;
1149 struct zone *zone = page_zone(page);
1150 struct page_cgroup *pc = lookup_page_cgroup(page);
1151
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001152 /*
1153 * Doing this check without taking ->lru_lock seems wrong but this
1154 * is safe. Because if page_cgroup's USED bit is unset, the page
1155 * will not be added to any memcg's LRU. If page_cgroup's USED bit is
1156 * set, the commit after this will fail, anyway.
1157 * This all charge/uncharge is done under some mutual execustion.
1158 * So, we don't need to taking care of changes in USED bit.
1159 */
1160 if (likely(!PageLRU(page)))
1161 return;
1162
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001163 spin_lock_irqsave(&zone->lru_lock, flags);
Johannes Weiner925b7672012-01-12 17:18:15 -08001164 lru = page_lru(page);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001165 /*
Johannes Weiner925b7672012-01-12 17:18:15 -08001166 * The uncharged page could still be registered to the LRU of
1167 * the stale pc->mem_cgroup.
1168 *
1169 * As pc->mem_cgroup is about to get overwritten, the old LRU
1170 * accounting needs to be taken care of. Let root_mem_cgroup
1171 * babysit the page until the new memcg is responsible for it.
1172 *
1173 * The PCG_USED bit is guarded by lock_page() as the page is
1174 * swapcache/pagecache.
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001175 */
Johannes Weiner925b7672012-01-12 17:18:15 -08001176 if (PageLRU(page) && PageCgroupAcctLRU(pc) && !PageCgroupUsed(pc)) {
1177 del_page_from_lru_list(zone, page, lru);
1178 add_page_to_lru_list(zone, page, lru);
1179 }
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001180 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001181}
1182
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001183static void mem_cgroup_lru_add_after_commit(struct page *page)
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001184{
Johannes Weiner925b7672012-01-12 17:18:15 -08001185 enum lru_list lru;
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001186 unsigned long flags;
1187 struct zone *zone = page_zone(page);
1188 struct page_cgroup *pc = lookup_page_cgroup(page);
Johannes Weinera61ed3c2011-11-02 13:38:29 -07001189 /*
1190 * putback: charge:
1191 * SetPageLRU SetPageCgroupUsed
1192 * smp_mb smp_mb
1193 * PageCgroupUsed && add to memcg LRU PageLRU && add to memcg LRU
1194 *
1195 * Ensure that one of the two sides adds the page to the memcg
1196 * LRU during a race.
1197 */
1198 smp_mb();
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001199 /* taking care of that the page is added to LRU while we commit it */
1200 if (likely(!PageLRU(page)))
1201 return;
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001202 spin_lock_irqsave(&zone->lru_lock, flags);
Johannes Weiner925b7672012-01-12 17:18:15 -08001203 lru = page_lru(page);
1204 /*
1205 * If the page is not on the LRU, someone will soon put it
1206 * there. If it is, and also already accounted for on the
1207 * memcg-side, it must be on the right lruvec as setting
1208 * pc->mem_cgroup and PageCgroupUsed is properly ordered.
1209 * Otherwise, root_mem_cgroup has been babysitting the page
1210 * during the charge. Move it to the new memcg now.
1211 */
1212 if (PageLRU(page) && !PageCgroupAcctLRU(pc)) {
1213 del_page_from_lru_list(zone, page, lru);
1214 add_page_to_lru_list(zone, page, lru);
1215 }
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001216 spin_unlock_irqrestore(&zone->lru_lock, flags);
1217}
1218
Michal Hocko3e920412011-07-26 16:08:29 -07001219/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001220 * Checks whether given mem is same or in the root_mem_cgroup's
Michal Hocko3e920412011-07-26 16:08:29 -07001221 * hierarchy subtree
1222 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001223static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1224 struct mem_cgroup *memcg)
Michal Hocko3e920412011-07-26 16:08:29 -07001225{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001226 if (root_memcg != memcg) {
1227 return (root_memcg->use_hierarchy &&
1228 css_is_ancestor(&memcg->css, &root_memcg->css));
Michal Hocko3e920412011-07-26 16:08:29 -07001229 }
1230
1231 return true;
1232}
1233
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001234int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -08001235{
1236 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001237 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001238 struct task_struct *p;
David Rientjes4c4a2212008-02-07 00:14:06 -08001239
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001240 p = find_lock_task_mm(task);
1241 if (!p)
1242 return 0;
1243 curr = try_get_mem_cgroup_from_mm(p->mm);
1244 task_unlock(p);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001245 if (!curr)
1246 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001247 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001248 * We should check use_hierarchy of "memcg" not "curr". Because checking
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001249 * use_hierarchy of "curr" here make this function true if hierarchy is
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001250 * enabled in "curr" and "curr" is a child of "memcg" in *cgroup*
1251 * hierarchy(even if use_hierarchy is disabled in "memcg").
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001252 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001253 ret = mem_cgroup_same_or_subtree(memcg, curr);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001254 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001255 return ret;
1256}
1257
Johannes Weiner9b272972011-11-02 13:38:23 -07001258int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg, struct zone *zone)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001259{
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001260 unsigned long inactive_ratio;
Johannes Weiner9b272972011-11-02 13:38:23 -07001261 int nid = zone_to_nid(zone);
1262 int zid = zone_idx(zone);
1263 unsigned long inactive;
1264 unsigned long active;
1265 unsigned long gb;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001266
Johannes Weiner9b272972011-11-02 13:38:23 -07001267 inactive = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid,
1268 BIT(LRU_INACTIVE_ANON));
1269 active = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid,
1270 BIT(LRU_ACTIVE_ANON));
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001271
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001272 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1273 if (gb)
1274 inactive_ratio = int_sqrt(10 * gb);
1275 else
1276 inactive_ratio = 1;
1277
Johannes Weiner9b272972011-11-02 13:38:23 -07001278 return inactive * inactive_ratio < active;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001279}
1280
Johannes Weiner9b272972011-11-02 13:38:23 -07001281int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg, struct zone *zone)
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001282{
1283 unsigned long active;
1284 unsigned long inactive;
Johannes Weiner9b272972011-11-02 13:38:23 -07001285 int zid = zone_idx(zone);
1286 int nid = zone_to_nid(zone);
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001287
Johannes Weiner9b272972011-11-02 13:38:23 -07001288 inactive = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid,
1289 BIT(LRU_INACTIVE_FILE));
1290 active = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid,
1291 BIT(LRU_ACTIVE_FILE));
Rik van Riel56e49d22009-06-16 15:32:28 -07001292
1293 return (active > inactive);
1294}
1295
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001296struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
1297 struct zone *zone)
1298{
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -07001299 int nid = zone_to_nid(zone);
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001300 int zid = zone_idx(zone);
1301 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
1302
1303 return &mz->reclaim_stat;
1304}
1305
1306struct zone_reclaim_stat *
1307mem_cgroup_get_reclaim_stat_from_page(struct page *page)
1308{
1309 struct page_cgroup *pc;
1310 struct mem_cgroup_per_zone *mz;
1311
1312 if (mem_cgroup_disabled())
1313 return NULL;
1314
1315 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -08001316 if (!PageCgroupUsed(pc))
1317 return NULL;
Johannes Weiner713735b42011-01-20 14:44:31 -08001318 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
1319 smp_rmb();
Johannes Weiner97a6c372011-03-23 16:42:27 -07001320 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001321 return &mz->reclaim_stat;
1322}
1323
Balbir Singh6d61ef42009-01-07 18:08:06 -08001324#define mem_cgroup_from_res_counter(counter, member) \
1325 container_of(counter, struct mem_cgroup, member)
1326
Johannes Weiner19942822011-02-01 15:52:43 -08001327/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001328 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
1329 * @mem: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001330 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001331 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001332 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001333 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001334static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001335{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001336 unsigned long long margin;
1337
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001338 margin = res_counter_margin(&memcg->res);
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001339 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001340 margin = min(margin, res_counter_margin(&memcg->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001341 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001342}
1343
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001344int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001345{
1346 struct cgroup *cgrp = memcg->css.cgroup;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001347
1348 /* root ? */
1349 if (cgrp->parent == NULL)
1350 return vm_swappiness;
1351
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001352 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001353}
1354
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001355static void mem_cgroup_start_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001356{
1357 int cpu;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001358
1359 get_online_cpus();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001360 spin_lock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001361 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001362 per_cpu(memcg->stat->count[MEM_CGROUP_ON_MOVE], cpu) += 1;
1363 memcg->nocpu_base.count[MEM_CGROUP_ON_MOVE] += 1;
1364 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001365 put_online_cpus();
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001366
1367 synchronize_rcu();
1368}
1369
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001370static void mem_cgroup_end_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001371{
1372 int cpu;
1373
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001374 if (!memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001375 return;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001376 get_online_cpus();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001377 spin_lock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001378 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001379 per_cpu(memcg->stat->count[MEM_CGROUP_ON_MOVE], cpu) -= 1;
1380 memcg->nocpu_base.count[MEM_CGROUP_ON_MOVE] -= 1;
1381 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001382 put_online_cpus();
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001383}
1384/*
1385 * 2 routines for checking "mem" is under move_account() or not.
1386 *
1387 * mem_cgroup_stealed() - checking a cgroup is mc.from or not. This is used
1388 * for avoiding race in accounting. If true,
1389 * pc->mem_cgroup may be overwritten.
1390 *
1391 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1392 * under hierarchy of moving cgroups. This is for
1393 * waiting at hith-memory prressure caused by "move".
1394 */
1395
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001396static bool mem_cgroup_stealed(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001397{
1398 VM_BUG_ON(!rcu_read_lock_held());
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001399 return this_cpu_read(memcg->stat->count[MEM_CGROUP_ON_MOVE]) > 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001400}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001401
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001402static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001403{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001404 struct mem_cgroup *from;
1405 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001406 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001407 /*
1408 * Unlike task_move routines, we access mc.to, mc.from not under
1409 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1410 */
1411 spin_lock(&mc.lock);
1412 from = mc.from;
1413 to = mc.to;
1414 if (!from)
1415 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001416
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001417 ret = mem_cgroup_same_or_subtree(memcg, from)
1418 || mem_cgroup_same_or_subtree(memcg, to);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001419unlock:
1420 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001421 return ret;
1422}
1423
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001424static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001425{
1426 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001427 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001428 DEFINE_WAIT(wait);
1429 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1430 /* moving charge context might have finished. */
1431 if (mc.moving_task)
1432 schedule();
1433 finish_wait(&mc.waitq, &wait);
1434 return true;
1435 }
1436 }
1437 return false;
1438}
1439
Balbir Singhe2224322009-04-02 16:57:39 -07001440/**
Kirill A. Shutemov6a6135b2010-03-10 15:22:25 -08001441 * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode.
Balbir Singhe2224322009-04-02 16:57:39 -07001442 * @memcg: The memory cgroup that went over limit
1443 * @p: Task that is going to be killed
1444 *
1445 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1446 * enabled
1447 */
1448void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1449{
1450 struct cgroup *task_cgrp;
1451 struct cgroup *mem_cgrp;
1452 /*
1453 * Need a buffer in BSS, can't rely on allocations. The code relies
1454 * on the assumption that OOM is serialized for memory controller.
1455 * If this assumption is broken, revisit this code.
1456 */
1457 static char memcg_name[PATH_MAX];
1458 int ret;
1459
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001460 if (!memcg || !p)
Balbir Singhe2224322009-04-02 16:57:39 -07001461 return;
1462
1463
1464 rcu_read_lock();
1465
1466 mem_cgrp = memcg->css.cgroup;
1467 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1468
1469 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1470 if (ret < 0) {
1471 /*
1472 * Unfortunately, we are unable to convert to a useful name
1473 * But we'll still print out the usage information
1474 */
1475 rcu_read_unlock();
1476 goto done;
1477 }
1478 rcu_read_unlock();
1479
1480 printk(KERN_INFO "Task in %s killed", memcg_name);
1481
1482 rcu_read_lock();
1483 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1484 if (ret < 0) {
1485 rcu_read_unlock();
1486 goto done;
1487 }
1488 rcu_read_unlock();
1489
1490 /*
1491 * Continues from above, so we don't need an KERN_ level
1492 */
1493 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1494done:
1495
1496 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1497 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1498 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1499 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1500 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1501 "failcnt %llu\n",
1502 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1503 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1504 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1505}
1506
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001507/*
1508 * This function returns the number of memcg under hierarchy tree. Returns
1509 * 1(self count) if no children.
1510 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001511static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001512{
1513 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001514 struct mem_cgroup *iter;
1515
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001516 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001517 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001518 return num;
1519}
1520
Balbir Singh6d61ef42009-01-07 18:08:06 -08001521/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001522 * Return the memory (and swap, if configured) limit for a memcg.
1523 */
1524u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
1525{
1526 u64 limit;
1527 u64 memsw;
1528
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001529 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
1530 limit += total_swap_pages << PAGE_SHIFT;
1531
David Rientjesa63d83f2010-08-09 17:19:46 -07001532 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1533 /*
1534 * If memsw is finite and limits the amount of swap space available
1535 * to this memcg, return that limit.
1536 */
1537 return min(limit, memsw);
1538}
1539
Johannes Weiner56600482012-01-12 17:17:59 -08001540static unsigned long mem_cgroup_reclaim(struct mem_cgroup *memcg,
1541 gfp_t gfp_mask,
1542 unsigned long flags)
1543{
1544 unsigned long total = 0;
1545 bool noswap = false;
1546 int loop;
1547
1548 if (flags & MEM_CGROUP_RECLAIM_NOSWAP)
1549 noswap = true;
1550 if (!(flags & MEM_CGROUP_RECLAIM_SHRINK) && memcg->memsw_is_minimum)
1551 noswap = true;
1552
1553 for (loop = 0; loop < MEM_CGROUP_MAX_RECLAIM_LOOPS; loop++) {
1554 if (loop)
1555 drain_all_stock_async(memcg);
1556 total += try_to_free_mem_cgroup_pages(memcg, gfp_mask, noswap);
1557 /*
1558 * Allow limit shrinkers, which are triggered directly
1559 * by userspace, to catch signals and stop reclaim
1560 * after minimal progress, regardless of the margin.
1561 */
1562 if (total && (flags & MEM_CGROUP_RECLAIM_SHRINK))
1563 break;
1564 if (mem_cgroup_margin(memcg))
1565 break;
1566 /*
1567 * If nothing was reclaimed after two attempts, there
1568 * may be no reclaimable pages in this hierarchy.
1569 */
1570 if (loop && !total)
1571 break;
1572 }
1573 return total;
1574}
1575
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001576/**
1577 * test_mem_cgroup_node_reclaimable
1578 * @mem: the target memcg
1579 * @nid: the node ID to be checked.
1580 * @noswap : specify true here if the user wants flle only information.
1581 *
1582 * This function returns whether the specified memcg contains any
1583 * reclaimable pages on a node. Returns true if there are any reclaimable
1584 * pages in the node.
1585 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001586static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001587 int nid, bool noswap)
1588{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001589 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001590 return true;
1591 if (noswap || !total_swap_pages)
1592 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001593 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001594 return true;
1595 return false;
1596
1597}
Ying Han889976d2011-05-26 16:25:33 -07001598#if MAX_NUMNODES > 1
1599
1600/*
1601 * Always updating the nodemask is not very good - even if we have an empty
1602 * list or the wrong list here, we can start from some node and traverse all
1603 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1604 *
1605 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001606static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001607{
1608 int nid;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001609 /*
1610 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1611 * pagein/pageout changes since the last update.
1612 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001613 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001614 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001615 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001616 return;
1617
Ying Han889976d2011-05-26 16:25:33 -07001618 /* make a nodemask where this memcg uses memory from */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001619 memcg->scan_nodes = node_states[N_HIGH_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001620
1621 for_each_node_mask(nid, node_states[N_HIGH_MEMORY]) {
1622
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001623 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1624 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001625 }
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001626
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001627 atomic_set(&memcg->numainfo_events, 0);
1628 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001629}
1630
1631/*
1632 * Selecting a node where we start reclaim from. Because what we need is just
1633 * reducing usage counter, start from anywhere is O,K. Considering
1634 * memory reclaim from current node, there are pros. and cons.
1635 *
1636 * Freeing memory from current node means freeing memory from a node which
1637 * we'll use or we've used. So, it may make LRU bad. And if several threads
1638 * hit limits, it will see a contention on a node. But freeing from remote
1639 * node means more costs for memory reclaim because of memory latency.
1640 *
1641 * Now, we use round-robin. Better algorithm is welcomed.
1642 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001643int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001644{
1645 int node;
1646
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001647 mem_cgroup_may_update_nodemask(memcg);
1648 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001649
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001650 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001651 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001652 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001653 /*
1654 * We call this when we hit limit, not when pages are added to LRU.
1655 * No LRU may hold pages because all pages are UNEVICTABLE or
1656 * memcg is too small and all pages are not on LRU. In that case,
1657 * we use curret node.
1658 */
1659 if (unlikely(node == MAX_NUMNODES))
1660 node = numa_node_id();
1661
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001662 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001663 return node;
1664}
1665
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001666/*
1667 * Check all nodes whether it contains reclaimable pages or not.
1668 * For quick scan, we make use of scan_nodes. This will allow us to skip
1669 * unused nodes. But scan_nodes is lazily updated and may not cotain
1670 * enough new information. We need to do double check.
1671 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001672bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001673{
1674 int nid;
1675
1676 /*
1677 * quick check...making use of scan_node.
1678 * We can skip unused nodes.
1679 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001680 if (!nodes_empty(memcg->scan_nodes)) {
1681 for (nid = first_node(memcg->scan_nodes);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001682 nid < MAX_NUMNODES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001683 nid = next_node(nid, memcg->scan_nodes)) {
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001684
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001685 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001686 return true;
1687 }
1688 }
1689 /*
1690 * Check rest of nodes.
1691 */
1692 for_each_node_state(nid, N_HIGH_MEMORY) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001693 if (node_isset(nid, memcg->scan_nodes))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001694 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001695 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001696 return true;
1697 }
1698 return false;
1699}
1700
Ying Han889976d2011-05-26 16:25:33 -07001701#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001702int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001703{
1704 return 0;
1705}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001706
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001707bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001708{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001709 return test_mem_cgroup_node_reclaimable(memcg, 0, noswap);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001710}
Ying Han889976d2011-05-26 16:25:33 -07001711#endif
1712
Johannes Weiner56600482012-01-12 17:17:59 -08001713static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg,
1714 struct zone *zone,
1715 gfp_t gfp_mask,
1716 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001717{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001718 struct mem_cgroup *victim = NULL;
Johannes Weiner56600482012-01-12 17:17:59 -08001719 int total = 0;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001720 int loop = 0;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001721 unsigned long excess;
Johannes Weiner185efc02011-09-14 16:21:58 -07001722 unsigned long nr_scanned;
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001723 struct mem_cgroup_reclaim_cookie reclaim = {
1724 .zone = zone,
1725 .priority = 0,
1726 };
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001727
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001728 excess = res_counter_soft_limit_excess(&root_memcg->res) >> PAGE_SHIFT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001729
Balbir Singh4e416952009-09-23 15:56:39 -07001730 while (1) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001731 victim = mem_cgroup_iter(root_memcg, victim, &reclaim);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001732 if (!victim) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001733 loop++;
Balbir Singh4e416952009-09-23 15:56:39 -07001734 if (loop >= 2) {
1735 /*
1736 * If we have not been able to reclaim
1737 * anything, it might because there are
1738 * no reclaimable pages under this hierarchy
1739 */
Johannes Weiner56600482012-01-12 17:17:59 -08001740 if (!total)
Balbir Singh4e416952009-09-23 15:56:39 -07001741 break;
Balbir Singh4e416952009-09-23 15:56:39 -07001742 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001743 * We want to do more targeted reclaim.
Balbir Singh4e416952009-09-23 15:56:39 -07001744 * excess >> 2 is not to excessive so as to
1745 * reclaim too much, nor too less that we keep
1746 * coming back to reclaim from this cgroup
1747 */
1748 if (total >= (excess >> 2) ||
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001749 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS))
Balbir Singh4e416952009-09-23 15:56:39 -07001750 break;
Balbir Singh4e416952009-09-23 15:56:39 -07001751 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001752 continue;
Balbir Singh4e416952009-09-23 15:56:39 -07001753 }
Johannes Weiner56600482012-01-12 17:17:59 -08001754 if (!mem_cgroup_reclaimable(victim, false))
Balbir Singh6d61ef42009-01-07 18:08:06 -08001755 continue;
Johannes Weiner56600482012-01-12 17:17:59 -08001756 total += mem_cgroup_shrink_node_zone(victim, gfp_mask, false,
1757 zone, &nr_scanned);
1758 *total_scanned += nr_scanned;
1759 if (!res_counter_soft_limit_excess(&root_memcg->res))
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001760 break;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001761 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001762 mem_cgroup_iter_break(root_memcg, victim);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001763 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001764}
1765
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001766/*
1767 * Check OOM-Killer is already running under our hierarchy.
1768 * If someone is running, return false.
Michal Hocko1af8efe2011-07-26 16:08:24 -07001769 * Has to be called with memcg_oom_lock
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001770 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001771static bool mem_cgroup_oom_lock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001772{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001773 struct mem_cgroup *iter, *failed = NULL;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001774
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001775 for_each_mem_cgroup_tree(iter, memcg) {
Johannes Weiner23751be2011-08-25 15:59:16 -07001776 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001777 /*
1778 * this subtree of our hierarchy is already locked
1779 * so we cannot give a lock.
1780 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07001781 failed = iter;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001782 mem_cgroup_iter_break(memcg, iter);
1783 break;
Johannes Weiner23751be2011-08-25 15:59:16 -07001784 } else
1785 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001786 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001787
Michal Hocko79dfdac2011-07-26 16:08:23 -07001788 if (!failed)
Johannes Weiner23751be2011-08-25 15:59:16 -07001789 return true;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001790
1791 /*
1792 * OK, we failed to lock the whole subtree so we have to clean up
1793 * what we set up to the failing subtree
1794 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001795 for_each_mem_cgroup_tree(iter, memcg) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001796 if (iter == failed) {
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001797 mem_cgroup_iter_break(memcg, iter);
1798 break;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001799 }
1800 iter->oom_lock = false;
1801 }
Johannes Weiner23751be2011-08-25 15:59:16 -07001802 return false;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001803}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001804
Michal Hocko79dfdac2011-07-26 16:08:23 -07001805/*
Michal Hocko1af8efe2011-07-26 16:08:24 -07001806 * Has to be called with memcg_oom_lock
Michal Hocko79dfdac2011-07-26 16:08:23 -07001807 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001808static int mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001809{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001810 struct mem_cgroup *iter;
1811
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001812 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001813 iter->oom_lock = false;
1814 return 0;
1815}
1816
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001817static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001818{
1819 struct mem_cgroup *iter;
1820
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001821 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001822 atomic_inc(&iter->under_oom);
1823}
1824
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001825static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001826{
1827 struct mem_cgroup *iter;
1828
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001829 /*
1830 * When a new child is created while the hierarchy is under oom,
1831 * mem_cgroup_oom_lock() may not be called. We have to use
1832 * atomic_add_unless() here.
1833 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001834 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001835 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001836}
1837
Michal Hocko1af8efe2011-07-26 16:08:24 -07001838static DEFINE_SPINLOCK(memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001839static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1840
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001841struct oom_wait_info {
1842 struct mem_cgroup *mem;
1843 wait_queue_t wait;
1844};
1845
1846static int memcg_oom_wake_function(wait_queue_t *wait,
1847 unsigned mode, int sync, void *arg)
1848{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001849 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg,
1850 *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001851 struct oom_wait_info *oom_wait_info;
1852
1853 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001854 oom_wait_memcg = oom_wait_info->mem;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001855
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001856 /*
1857 * Both of oom_wait_info->mem and wake_mem are stable under us.
1858 * Then we can use css_is_ancestor without taking care of RCU.
1859 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001860 if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg)
1861 && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001862 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001863 return autoremove_wake_function(wait, mode, sync, arg);
1864}
1865
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001866static void memcg_wakeup_oom(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001867{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001868 /* for filtering, pass "memcg" as argument. */
1869 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001870}
1871
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001872static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001873{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001874 if (memcg && atomic_read(&memcg->under_oom))
1875 memcg_wakeup_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001876}
1877
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001878/*
1879 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
1880 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001881bool mem_cgroup_handle_oom(struct mem_cgroup *memcg, gfp_t mask)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001882{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001883 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001884 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001885
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001886 owait.mem = memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001887 owait.wait.flags = 0;
1888 owait.wait.func = memcg_oom_wake_function;
1889 owait.wait.private = current;
1890 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001891 need_to_kill = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001892 mem_cgroup_mark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001893
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001894 /* At first, try to OOM lock hierarchy under memcg.*/
Michal Hocko1af8efe2011-07-26 16:08:24 -07001895 spin_lock(&memcg_oom_lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001896 locked = mem_cgroup_oom_lock(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001897 /*
1898 * Even if signal_pending(), we can't quit charge() loop without
1899 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
1900 * under OOM is always welcomed, use TASK_KILLABLE here.
1901 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001902 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001903 if (!locked || memcg->oom_kill_disable)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001904 need_to_kill = false;
1905 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001906 mem_cgroup_oom_notify(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07001907 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001908
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001909 if (need_to_kill) {
1910 finish_wait(&memcg_oom_waitq, &owait.wait);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001911 mem_cgroup_out_of_memory(memcg, mask);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001912 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001913 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001914 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001915 }
Michal Hocko1af8efe2011-07-26 16:08:24 -07001916 spin_lock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001917 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001918 mem_cgroup_oom_unlock(memcg);
1919 memcg_wakeup_oom(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07001920 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001921
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001922 mem_cgroup_unmark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001923
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001924 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
1925 return false;
1926 /* Give chance to dying process */
KAMEZAWA Hiroyuki715a5ee2011-11-02 13:38:18 -07001927 schedule_timeout_uninterruptible(1);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001928 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001929}
1930
Balbir Singhd69b0422009-06-17 16:26:34 -07001931/*
1932 * Currently used to update mapped file statistics, but the routine can be
1933 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001934 *
1935 * Notes: Race condition
1936 *
1937 * We usually use page_cgroup_lock() for accessing page_cgroup member but
1938 * it tends to be costly. But considering some conditions, we doesn't need
1939 * to do so _always_.
1940 *
1941 * Considering "charge", lock_page_cgroup() is not required because all
1942 * file-stat operations happen after a page is attached to radix-tree. There
1943 * are no race with "charge".
1944 *
1945 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
1946 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
1947 * if there are race with "uncharge". Statistics itself is properly handled
1948 * by flags.
1949 *
1950 * Considering "move", this is an only case we see a race. To make the race
1951 * small, we check MEM_CGROUP_ON_MOVE percpu value and detect there are
1952 * possibility of race condition. If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07001953 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001954
Greg Thelen2a7106f2011-01-13 15:47:37 -08001955void mem_cgroup_update_page_stat(struct page *page,
1956 enum mem_cgroup_page_stat_item idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07001957{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001958 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001959 struct page_cgroup *pc = lookup_page_cgroup(page);
1960 bool need_unlock = false;
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08001961 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07001962
Johannes Weinercfa44942012-01-12 17:18:38 -08001963 if (mem_cgroup_disabled())
Balbir Singhd69b0422009-06-17 16:26:34 -07001964 return;
1965
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001966 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001967 memcg = pc->mem_cgroup;
1968 if (unlikely(!memcg || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001969 goto out;
1970 /* pc->mem_cgroup is unstable ? */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001971 if (unlikely(mem_cgroup_stealed(memcg)) || PageTransHuge(page)) {
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001972 /* take a lock against to access pc->mem_cgroup */
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08001973 move_lock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001974 need_unlock = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001975 memcg = pc->mem_cgroup;
1976 if (!memcg || !PageCgroupUsed(pc))
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001977 goto out;
1978 }
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001979
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001980 switch (idx) {
Greg Thelen2a7106f2011-01-13 15:47:37 -08001981 case MEMCG_NR_FILE_MAPPED:
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001982 if (val > 0)
1983 SetPageCgroupFileMapped(pc);
1984 else if (!page_mapped(page))
KAMEZAWA Hiroyuki0c270f82010-10-27 15:33:39 -07001985 ClearPageCgroupFileMapped(pc);
Greg Thelen2a7106f2011-01-13 15:47:37 -08001986 idx = MEM_CGROUP_STAT_FILE_MAPPED;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001987 break;
1988 default:
1989 BUG();
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07001990 }
Balbir Singhd69b0422009-06-17 16:26:34 -07001991
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001992 this_cpu_add(memcg->stat->count[idx], val);
Greg Thelen2a7106f2011-01-13 15:47:37 -08001993
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001994out:
1995 if (unlikely(need_unlock))
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08001996 move_unlock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001997 rcu_read_unlock();
1998 return;
Balbir Singhd69b0422009-06-17 16:26:34 -07001999}
Greg Thelen2a7106f2011-01-13 15:47:37 -08002000EXPORT_SYMBOL(mem_cgroup_update_page_stat);
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002001
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002002/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002003 * size of first charge trial. "32" comes from vmscan.c's magic value.
2004 * TODO: maybe necessary to use big numbers in big irons.
2005 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002006#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002007struct memcg_stock_pcp {
2008 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002009 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002010 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002011 unsigned long flags;
2012#define FLUSHING_CACHED_CHARGE (0)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002013};
2014static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002015static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002016
2017/*
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002018 * Try to consume stocked charge on this cpu. If success, one page is consumed
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002019 * from local stock and true is returned. If the stock is 0 or charges from a
2020 * cgroup which is not current target, returns false. This stock will be
2021 * refilled.
2022 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002023static bool consume_stock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002024{
2025 struct memcg_stock_pcp *stock;
2026 bool ret = true;
2027
2028 stock = &get_cpu_var(memcg_stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002029 if (memcg == stock->cached && stock->nr_pages)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002030 stock->nr_pages--;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002031 else /* need to call res_counter_charge */
2032 ret = false;
2033 put_cpu_var(memcg_stock);
2034 return ret;
2035}
2036
2037/*
2038 * Returns stocks cached in percpu to res_counter and reset cached information.
2039 */
2040static void drain_stock(struct memcg_stock_pcp *stock)
2041{
2042 struct mem_cgroup *old = stock->cached;
2043
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002044 if (stock->nr_pages) {
2045 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2046
2047 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002048 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002049 res_counter_uncharge(&old->memsw, bytes);
2050 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002051 }
2052 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002053}
2054
2055/*
2056 * This must be called under preempt disabled or must be called by
2057 * a thread which is pinned to local cpu.
2058 */
2059static void drain_local_stock(struct work_struct *dummy)
2060{
2061 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
2062 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002063 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002064}
2065
2066/*
2067 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002068 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002069 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002070static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002071{
2072 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2073
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002074 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002075 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002076 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002077 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002078 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002079 put_cpu_var(memcg_stock);
2080}
2081
2082/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002083 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Michal Hockod38144b2011-07-26 16:08:28 -07002084 * of the hierarchy under it. sync flag says whether we should block
2085 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002086 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002087static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002088{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002089 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002090
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002091 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002092 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002093 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002094 for_each_online_cpu(cpu) {
2095 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002096 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002097
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002098 memcg = stock->cached;
2099 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002100 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002101 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002102 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002103 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2104 if (cpu == curcpu)
2105 drain_local_stock(&stock->work);
2106 else
2107 schedule_work_on(cpu, &stock->work);
2108 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002109 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002110 put_cpu();
Michal Hockod38144b2011-07-26 16:08:28 -07002111
2112 if (!sync)
2113 goto out;
2114
2115 for_each_online_cpu(cpu) {
2116 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002117 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002118 flush_work(&stock->work);
2119 }
2120out:
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002121 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002122}
2123
2124/*
2125 * Tries to drain stocked charges in other cpus. This function is asynchronous
2126 * and just put a work per cpu for draining localy on each cpu. Caller can
2127 * expects some charges will be back to res_counter later but cannot wait for
2128 * it.
2129 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002130static void drain_all_stock_async(struct mem_cgroup *root_memcg)
Michal Hockod38144b2011-07-26 16:08:28 -07002131{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002132 /*
2133 * If someone calls draining, avoid adding more kworker runs.
2134 */
2135 if (!mutex_trylock(&percpu_charge_mutex))
2136 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002137 drain_all_stock(root_memcg, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002138 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002139}
2140
2141/* This is a synchronous drain interface. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002142static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002143{
2144 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002145 mutex_lock(&percpu_charge_mutex);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002146 drain_all_stock(root_memcg, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002147 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002148}
2149
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002150/*
2151 * This function drains percpu counter value from DEAD cpu and
2152 * move it to local cpu. Note that this function can be preempted.
2153 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002154static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002155{
2156 int i;
2157
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002158 spin_lock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002159 for (i = 0; i < MEM_CGROUP_STAT_DATA; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002160 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002161
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002162 per_cpu(memcg->stat->count[i], cpu) = 0;
2163 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002164 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002165 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002166 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002167
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002168 per_cpu(memcg->stat->events[i], cpu) = 0;
2169 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002170 }
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002171 /* need to clear ON_MOVE value, works as a kind of lock. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002172 per_cpu(memcg->stat->count[MEM_CGROUP_ON_MOVE], cpu) = 0;
2173 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002174}
2175
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002176static void synchronize_mem_cgroup_on_move(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002177{
2178 int idx = MEM_CGROUP_ON_MOVE;
2179
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002180 spin_lock(&memcg->pcp_counter_lock);
2181 per_cpu(memcg->stat->count[idx], cpu) = memcg->nocpu_base.count[idx];
2182 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002183}
2184
2185static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002186 unsigned long action,
2187 void *hcpu)
2188{
2189 int cpu = (unsigned long)hcpu;
2190 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002191 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002192
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002193 if ((action == CPU_ONLINE)) {
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002194 for_each_mem_cgroup(iter)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002195 synchronize_mem_cgroup_on_move(iter, cpu);
2196 return NOTIFY_OK;
2197 }
2198
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002199 if ((action != CPU_DEAD) || action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002200 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002201
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002202 for_each_mem_cgroup(iter)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002203 mem_cgroup_drain_pcp_counter(iter, cpu);
2204
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002205 stock = &per_cpu(memcg_stock, cpu);
2206 drain_stock(stock);
2207 return NOTIFY_OK;
2208}
2209
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002210
2211/* See __mem_cgroup_try_charge() for details */
2212enum {
2213 CHARGE_OK, /* success */
2214 CHARGE_RETRY, /* need to retry but retry is not bad */
2215 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2216 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
2217 CHARGE_OOM_DIE, /* the current is killed because of OOM */
2218};
2219
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002220static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002221 unsigned int nr_pages, bool oom_check)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002222{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002223 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002224 struct mem_cgroup *mem_over_limit;
2225 struct res_counter *fail_res;
2226 unsigned long flags = 0;
2227 int ret;
2228
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002229 ret = res_counter_charge(&memcg->res, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002230
2231 if (likely(!ret)) {
2232 if (!do_swap_account)
2233 return CHARGE_OK;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002234 ret = res_counter_charge(&memcg->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002235 if (likely(!ret))
2236 return CHARGE_OK;
2237
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002238 res_counter_uncharge(&memcg->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002239 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2240 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2241 } else
2242 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002243 /*
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002244 * nr_pages can be either a huge page (HPAGE_PMD_NR), a batch
2245 * of regular pages (CHARGE_BATCH), or a single regular page (1).
Johannes Weiner9221edb2011-02-01 15:52:42 -08002246 *
2247 * Never reclaim on behalf of optional batching, retry with a
2248 * single page instead.
2249 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002250 if (nr_pages == CHARGE_BATCH)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002251 return CHARGE_RETRY;
2252
2253 if (!(gfp_mask & __GFP_WAIT))
2254 return CHARGE_WOULDBLOCK;
2255
Johannes Weiner56600482012-01-12 17:17:59 -08002256 ret = mem_cgroup_reclaim(mem_over_limit, gfp_mask, flags);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002257 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002258 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002259 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002260 * Even though the limit is exceeded at this point, reclaim
2261 * may have been able to free some pages. Retry the charge
2262 * before killing the task.
2263 *
2264 * Only for regular pages, though: huge pages are rather
2265 * unlikely to succeed so close to the limit, and we fall back
2266 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002267 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002268 if (nr_pages == 1 && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002269 return CHARGE_RETRY;
2270
2271 /*
2272 * At task move, charge accounts can be doubly counted. So, it's
2273 * better to wait until the end of task_move if something is going on.
2274 */
2275 if (mem_cgroup_wait_acct_move(mem_over_limit))
2276 return CHARGE_RETRY;
2277
2278 /* If we don't need to call oom-killer at el, return immediately */
2279 if (!oom_check)
2280 return CHARGE_NOMEM;
2281 /* check OOM */
2282 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask))
2283 return CHARGE_OOM_DIE;
2284
2285 return CHARGE_RETRY;
2286}
2287
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002288/*
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002289 * Unlike exported interface, "oom" parameter is added. if oom==true,
2290 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002291 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002292static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002293 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002294 unsigned int nr_pages,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002295 struct mem_cgroup **ptr,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002296 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002297{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002298 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002299 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002300 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002301 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002302
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002303 /*
2304 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2305 * in system level. So, allow to go ahead dying process in addition to
2306 * MEMDIE process.
2307 */
2308 if (unlikely(test_thread_flag(TIF_MEMDIE)
2309 || fatal_signal_pending(current)))
2310 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002311
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002312 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08002313 * We always charge the cgroup the mm_struct belongs to.
2314 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002315 * thread group leader migrates. It's possible that mm is not
2316 * set, if so charge the init_mm (happens for pagecache usage).
2317 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002318 if (!*ptr && !mm)
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002319 goto bypass;
2320again:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002321 if (*ptr) { /* css should be a valid one */
2322 memcg = *ptr;
2323 VM_BUG_ON(css_is_removed(&memcg->css));
2324 if (mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002325 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002326 if (nr_pages == 1 && consume_stock(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002327 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002328 css_get(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002329 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002330 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002331
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002332 rcu_read_lock();
2333 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002334 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002335 * Because we don't have task_lock(), "p" can exit.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002336 * In that case, "memcg" can point to root or p can be NULL with
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002337 * race with swapoff. Then, we have small risk of mis-accouning.
2338 * But such kind of mis-account by race always happens because
2339 * we don't have cgroup_mutex(). It's overkill and we allo that
2340 * small race, here.
2341 * (*) swapoff at el will charge against mm-struct not against
2342 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002343 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002344 memcg = mem_cgroup_from_task(p);
2345 if (!memcg || mem_cgroup_is_root(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002346 rcu_read_unlock();
2347 goto done;
2348 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002349 if (nr_pages == 1 && consume_stock(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002350 /*
2351 * It seems dagerous to access memcg without css_get().
2352 * But considering how consume_stok works, it's not
2353 * necessary. If consume_stock success, some charges
2354 * from this memcg are cached on this cpu. So, we
2355 * don't need to call css_get()/css_tryget() before
2356 * calling consume_stock().
2357 */
2358 rcu_read_unlock();
2359 goto done;
2360 }
2361 /* after here, we may be blocked. we need to get refcnt */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002362 if (!css_tryget(&memcg->css)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002363 rcu_read_unlock();
2364 goto again;
2365 }
2366 rcu_read_unlock();
2367 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002368
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002369 do {
2370 bool oom_check;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002371
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002372 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002373 if (fatal_signal_pending(current)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002374 css_put(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002375 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002376 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002377
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002378 oom_check = false;
2379 if (oom && !nr_oom_retries) {
2380 oom_check = true;
2381 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2382 }
Balbir Singh6d61ef42009-01-07 18:08:06 -08002383
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002384 ret = mem_cgroup_do_charge(memcg, gfp_mask, batch, oom_check);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002385 switch (ret) {
2386 case CHARGE_OK:
2387 break;
2388 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002389 batch = nr_pages;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002390 css_put(&memcg->css);
2391 memcg = NULL;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002392 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002393 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002394 css_put(&memcg->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002395 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002396 case CHARGE_NOMEM: /* OOM routine works */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002397 if (!oom) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002398 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002399 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002400 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002401 /* If oom, we never return -ENOMEM */
2402 nr_oom_retries--;
2403 break;
2404 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002405 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002406 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08002407 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002408 } while (ret != CHARGE_OK);
2409
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002410 if (batch > nr_pages)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002411 refill_stock(memcg, batch - nr_pages);
2412 css_put(&memcg->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002413done:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002414 *ptr = memcg;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002415 return 0;
2416nomem:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002417 *ptr = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002418 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002419bypass:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002420 *ptr = NULL;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002421 return 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002422}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002423
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002424/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002425 * Somemtimes we have to undo a charge we got by try_charge().
2426 * This function is for that and do uncharge, put css's refcnt.
2427 * gotten by try_charge().
2428 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002429static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002430 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002431{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002432 if (!mem_cgroup_is_root(memcg)) {
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002433 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002434
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002435 res_counter_uncharge(&memcg->res, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002436 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002437 res_counter_uncharge(&memcg->memsw, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002438 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002439}
2440
2441/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002442 * A helper function to get mem_cgroup from ID. must be called under
2443 * rcu_read_lock(). The caller must check css_is_removed() or some if
2444 * it's concern. (dropping refcnt from swap can be called against removed
2445 * memcg.)
2446 */
2447static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2448{
2449 struct cgroup_subsys_state *css;
2450
2451 /* ID 0 is unused ID */
2452 if (!id)
2453 return NULL;
2454 css = css_lookup(&mem_cgroup_subsys, id);
2455 if (!css)
2456 return NULL;
2457 return container_of(css, struct mem_cgroup, css);
2458}
2459
Wu Fengguange42d9d52009-12-16 12:19:59 +01002460struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002461{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002462 struct mem_cgroup *memcg = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002463 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002464 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002465 swp_entry_t ent;
2466
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002467 VM_BUG_ON(!PageLocked(page));
2468
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002469 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002470 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002471 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002472 memcg = pc->mem_cgroup;
2473 if (memcg && !css_tryget(&memcg->css))
2474 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002475 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002476 ent.val = page_private(page);
Bob Liu9fb4b7c2012-01-12 17:18:48 -08002477 id = lookup_swap_cgroup_id(ent);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002478 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002479 memcg = mem_cgroup_lookup(id);
2480 if (memcg && !css_tryget(&memcg->css))
2481 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002482 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002483 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002484 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002485 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002486}
2487
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002488static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,
Johannes Weiner5564e882011-03-23 16:42:29 -07002489 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002490 unsigned int nr_pages,
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002491 struct page_cgroup *pc,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002492 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002493{
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002494 lock_page_cgroup(pc);
2495 if (unlikely(PageCgroupUsed(pc))) {
2496 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002497 __mem_cgroup_cancel_charge(memcg, nr_pages);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002498 return;
2499 }
2500 /*
2501 * we don't need page_cgroup_lock about tail pages, becase they are not
2502 * accessed by any other context at this point.
2503 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002504 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002505 /*
2506 * We access a page_cgroup asynchronously without lock_page_cgroup().
2507 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2508 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2509 * before USED bit, we need memory barrier here.
2510 * See mem_cgroup_add_lru_list(), etc.
2511 */
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002512 smp_wmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -07002513 switch (ctype) {
2514 case MEM_CGROUP_CHARGE_TYPE_CACHE:
2515 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
2516 SetPageCgroupCache(pc);
2517 SetPageCgroupUsed(pc);
2518 break;
2519 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
2520 ClearPageCgroupCache(pc);
2521 SetPageCgroupUsed(pc);
2522 break;
2523 default:
2524 break;
2525 }
Hugh Dickins3be91272008-02-07 00:14:19 -08002526
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002527 mem_cgroup_charge_statistics(memcg, PageCgroupCache(pc), nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002528 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002529 /*
2530 * "charge_statistics" updated event counter. Then, check it.
2531 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2532 * if they exceeds softlimit.
2533 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002534 memcg_check_events(memcg, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002535}
2536
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002537#ifdef CONFIG_TRANSPARENT_HUGEPAGE
2538
2539#define PCGF_NOCOPY_AT_SPLIT ((1 << PCG_LOCK) | (1 << PCG_MOVE_LOCK) |\
2540 (1 << PCG_ACCT_LRU) | (1 << PCG_MIGRATION))
2541/*
2542 * Because tail pages are not marked as "used", set it. We're under
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002543 * zone->lru_lock, 'splitting on pmd' and compound_lock.
2544 * charge/uncharge will be never happen and move_account() is done under
2545 * compound_lock(), so we don't have to take care of races.
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002546 */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002547void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002548{
2549 struct page_cgroup *head_pc = lookup_page_cgroup(head);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002550 struct page_cgroup *pc;
2551 int i;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002552
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08002553 if (mem_cgroup_disabled())
2554 return;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002555 for (i = 1; i < HPAGE_PMD_NR; i++) {
2556 pc = head_pc + i;
2557 pc->mem_cgroup = head_pc->mem_cgroup;
2558 smp_wmb();/* see __commit_charge() */
2559 /*
2560 * LRU flags cannot be copied because we need to add tail
2561 * page to LRU by generic call and our hooks will be called.
2562 */
2563 pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
2564 }
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002565
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002566 if (PageCgroupAcctLRU(head_pc)) {
2567 enum lru_list lru;
2568 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002569 /*
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002570 * We hold lru_lock, then, reduce counter directly.
2571 */
2572 lru = page_lru(head);
Johannes Weiner97a6c372011-03-23 16:42:27 -07002573 mz = page_cgroup_zoneinfo(head_pc->mem_cgroup, head);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002574 MEM_CGROUP_ZSTAT(mz, lru) -= HPAGE_PMD_NR - 1;
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002575 }
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002576}
2577#endif
2578
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002579/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07002580 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07002581 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002582 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002583 * @pc: page_cgroup of the page.
2584 * @from: mem_cgroup which the page is moved from.
2585 * @to: mem_cgroup which the page is moved to. @from != @to.
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002586 * @uncharge: whether we should call uncharge and css_put against @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002587 *
2588 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002589 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002590 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002591 *
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002592 * This function doesn't do "charge" nor css_get to new cgroup. It should be
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002593 * done by a caller(__mem_cgroup_try_charge would be useful). If @uncharge is
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002594 * true, this function does "uncharge" from old cgroup, but it doesn't if
2595 * @uncharge is false, so a caller should do "uncharge".
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002596 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002597static int mem_cgroup_move_account(struct page *page,
2598 unsigned int nr_pages,
2599 struct page_cgroup *pc,
2600 struct mem_cgroup *from,
2601 struct mem_cgroup *to,
2602 bool uncharge)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002603{
Johannes Weinerde3638d2011-03-23 16:42:28 -07002604 unsigned long flags;
2605 int ret;
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002606
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002607 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07002608 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07002609 /*
2610 * The page is isolated from LRU. So, collapse function
2611 * will not handle this page. But page splitting can happen.
2612 * Do this check under compound_page_lock(). The caller should
2613 * hold it.
2614 */
2615 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002616 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07002617 goto out;
2618
2619 lock_page_cgroup(pc);
2620
2621 ret = -EINVAL;
2622 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
2623 goto unlock;
2624
2625 move_lock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002626
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002627 if (PageCgroupFileMapped(pc)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08002628 /* Update mapped_file data for mem_cgroup */
2629 preempt_disable();
2630 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2631 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2632 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07002633 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002634 mem_cgroup_charge_statistics(from, PageCgroupCache(pc), -nr_pages);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002635 if (uncharge)
2636 /* This is not "cancel", but cancel_charge does all we need. */
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002637 __mem_cgroup_cancel_charge(from, nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07002638
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002639 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002640 pc->mem_cgroup = to;
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002641 mem_cgroup_charge_statistics(to, PageCgroupCache(pc), nr_pages);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002642 /*
2643 * We charges against "to" which may not have any tasks. Then, "to"
2644 * can be under rmdir(). But in current implementation, caller of
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08002645 * this function is just force_empty() and move charge, so it's
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002646 * guaranteed that "to" is never removed. So, we don't check rmdir
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08002647 * status here.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002648 */
Johannes Weinerde3638d2011-03-23 16:42:28 -07002649 move_unlock_page_cgroup(pc, &flags);
2650 ret = 0;
2651unlock:
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002652 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002653 /*
2654 * check events
2655 */
Johannes Weiner5564e882011-03-23 16:42:29 -07002656 memcg_check_events(to, page);
2657 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07002658out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002659 return ret;
2660}
2661
2662/*
2663 * move charges to its parent.
2664 */
2665
Johannes Weiner5564e882011-03-23 16:42:29 -07002666static int mem_cgroup_move_parent(struct page *page,
2667 struct page_cgroup *pc,
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002668 struct mem_cgroup *child,
2669 gfp_t gfp_mask)
2670{
2671 struct cgroup *cg = child->css.cgroup;
2672 struct cgroup *pcg = cg->parent;
2673 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002674 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07002675 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002676 int ret;
2677
2678 /* Is ROOT ? */
2679 if (!pcg)
2680 return -EINVAL;
2681
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002682 ret = -EBUSY;
2683 if (!get_page_unless_zero(page))
2684 goto out;
2685 if (isolate_lru_page(page))
2686 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08002687
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002688 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002689
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002690 parent = mem_cgroup_from_cont(pcg);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002691 ret = __mem_cgroup_try_charge(NULL, gfp_mask, nr_pages, &parent, false);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002692 if (ret || !parent)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002693 goto put_back;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002694
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002695 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002696 flags = compound_lock_irqsave(page);
2697
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002698 ret = mem_cgroup_move_account(page, nr_pages, pc, child, parent, true);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002699 if (ret)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002700 __mem_cgroup_cancel_charge(parent, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08002701
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002702 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002703 compound_unlock_irqrestore(page, flags);
Jesper Juhl8dba4742011-01-25 15:07:24 -08002704put_back:
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002705 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002706put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08002707 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002708out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002709 return ret;
2710}
2711
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002712/*
2713 * Charge the memory controller for page usage.
2714 * Return
2715 * 0 if the charge was successful
2716 * < 0 if the cgroup is over its limit
2717 */
2718static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002719 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002720{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002721 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002722 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08002723 struct page_cgroup *pc;
2724 bool oom = true;
2725 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08002726
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002727 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002728 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002729 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08002730 /*
2731 * Never OOM-kill a process for a huge page. The
2732 * fault handler will fall back to regular pages.
2733 */
2734 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002735 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002736
2737 pc = lookup_page_cgroup(page);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002738 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom);
2739 if (ret || !memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002740 return ret;
2741
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002742 __mem_cgroup_commit_charge(memcg, page, nr_pages, pc, ctype);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002743 return 0;
2744}
2745
2746int mem_cgroup_newpage_charge(struct page *page,
2747 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002748{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002749 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002750 return 0;
Johannes Weiner7a0524c2012-01-12 17:18:43 -08002751 VM_BUG_ON(page_mapped(page));
2752 VM_BUG_ON(page->mapping && !PageAnon(page));
2753 VM_BUG_ON(!mm);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002754 return mem_cgroup_charge_common(page, mm, gfp_mask,
Johannes Weiner7a0524c2012-01-12 17:18:43 -08002755 MEM_CGROUP_CHARGE_TYPE_MAPPED);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002756}
2757
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002758static void
2759__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2760 enum charge_type ctype);
2761
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002762static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002763__mem_cgroup_commit_charge_lrucare(struct page *page, struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002764 enum charge_type ctype)
2765{
2766 struct page_cgroup *pc = lookup_page_cgroup(page);
2767 /*
2768 * In some case, SwapCache, FUSE(splice_buf->radixtree), the page
2769 * is already on LRU. It means the page may on some other page_cgroup's
2770 * LRU. Take care of it.
2771 */
2772 mem_cgroup_lru_del_before_commit(page);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002773 __mem_cgroup_commit_charge(memcg, page, 1, pc, ctype);
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002774 mem_cgroup_lru_add_after_commit(page);
2775 return;
2776}
2777
Balbir Singhe1a1cd52008-02-07 00:14:02 -08002778int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
2779 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08002780{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002781 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002782 int ret;
2783
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002784 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002785 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002786 if (PageCompound(page))
2787 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002788
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002789 if (unlikely(!mm))
Balbir Singh8697d332008-02-07 00:13:59 -08002790 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002791
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002792 if (page_is_file_cache(page)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002793 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, &memcg, true);
2794 if (ret || !memcg)
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002795 return ret;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002796
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002797 /*
2798 * FUSE reuses pages without going through the final
2799 * put that would remove them from the LRU list, make
2800 * sure that they get relinked properly.
2801 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002802 __mem_cgroup_commit_charge_lrucare(page, memcg,
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002803 MEM_CGROUP_CHARGE_TYPE_CACHE);
2804 return ret;
2805 }
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002806 /* shmem */
2807 if (PageSwapCache(page)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002808 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &memcg);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002809 if (!ret)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002810 __mem_cgroup_commit_charge_swapin(page, memcg,
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002811 MEM_CGROUP_CHARGE_TYPE_SHMEM);
2812 } else
2813 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002814 MEM_CGROUP_CHARGE_TYPE_SHMEM);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002815
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002816 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002817}
2818
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002819/*
2820 * While swap-in, try_charge -> commit or cancel, the page is locked.
2821 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02002822 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002823 * "commit()" or removed by "cancel()"
2824 */
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002825int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
2826 struct page *page,
Johannes Weiner72835c82012-01-12 17:18:32 -08002827 gfp_t mask, struct mem_cgroup **memcgp)
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002828{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002829 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002830 int ret;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002831
Johannes Weiner72835c82012-01-12 17:18:32 -08002832 *memcgp = NULL;
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07002833
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002834 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002835 return 0;
2836
2837 if (!do_swap_account)
2838 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002839 /*
2840 * A racing thread's fault, or swapoff, may have already updated
Hugh Dickins407f9c82009-12-14 17:59:30 -08002841 * the pte, and even removed page from swap cache: in those cases
2842 * do_swap_page()'s pte_same() test will fail; but there's also a
2843 * KSM case which does need to charge the page.
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002844 */
2845 if (!PageSwapCache(page))
Hugh Dickins407f9c82009-12-14 17:59:30 -08002846 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002847 memcg = try_get_mem_cgroup_from_page(page);
2848 if (!memcg)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002849 goto charge_cur_mm;
Johannes Weiner72835c82012-01-12 17:18:32 -08002850 *memcgp = memcg;
2851 ret = __mem_cgroup_try_charge(NULL, mask, 1, memcgp, true);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002852 css_put(&memcg->css);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002853 return ret;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002854charge_cur_mm:
2855 if (unlikely(!mm))
2856 mm = &init_mm;
Johannes Weiner72835c82012-01-12 17:18:32 -08002857 return __mem_cgroup_try_charge(mm, mask, 1, memcgp, true);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002858}
2859
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002860static void
Johannes Weiner72835c82012-01-12 17:18:32 -08002861__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *memcg,
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002862 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002863{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002864 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002865 return;
Johannes Weiner72835c82012-01-12 17:18:32 -08002866 if (!memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002867 return;
Johannes Weiner72835c82012-01-12 17:18:32 -08002868 cgroup_exclude_rmdir(&memcg->css);
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002869
Johannes Weiner72835c82012-01-12 17:18:32 -08002870 __mem_cgroup_commit_charge_lrucare(page, memcg, ctype);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002871 /*
2872 * Now swap is on-memory. This means this page may be
2873 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002874 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
2875 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
2876 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002877 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002878 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002879 swp_entry_t ent = {.val = page_private(page)};
Johannes Weiner72835c82012-01-12 17:18:32 -08002880 struct mem_cgroup *swap_memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002881 unsigned short id;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002882
2883 id = swap_cgroup_record(ent, 0);
2884 rcu_read_lock();
Johannes Weiner72835c82012-01-12 17:18:32 -08002885 swap_memcg = mem_cgroup_lookup(id);
2886 if (swap_memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002887 /*
2888 * This recorded memcg can be obsolete one. So, avoid
2889 * calling css_tryget
2890 */
Johannes Weiner72835c82012-01-12 17:18:32 -08002891 if (!mem_cgroup_is_root(swap_memcg))
2892 res_counter_uncharge(&swap_memcg->memsw,
2893 PAGE_SIZE);
2894 mem_cgroup_swap_statistics(swap_memcg, false);
2895 mem_cgroup_put(swap_memcg);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002896 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002897 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002898 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002899 /*
2900 * At swapin, we may charge account against cgroup which has no tasks.
2901 * So, rmdir()->pre_destroy() can be called while we do this charge.
2902 * In that case, we need to call pre_destroy() again. check it here.
2903 */
Johannes Weiner72835c82012-01-12 17:18:32 -08002904 cgroup_release_and_wakeup_rmdir(&memcg->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002905}
2906
Johannes Weiner72835c82012-01-12 17:18:32 -08002907void mem_cgroup_commit_charge_swapin(struct page *page,
2908 struct mem_cgroup *memcg)
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002909{
Johannes Weiner72835c82012-01-12 17:18:32 -08002910 __mem_cgroup_commit_charge_swapin(page, memcg,
2911 MEM_CGROUP_CHARGE_TYPE_MAPPED);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002912}
2913
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002914void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002915{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002916 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002917 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002918 if (!memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002919 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002920 __mem_cgroup_cancel_charge(memcg, 1);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002921}
2922
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002923static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002924 unsigned int nr_pages,
2925 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002926{
2927 struct memcg_batch_info *batch = NULL;
2928 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002929
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002930 /* If swapout, usage of swap doesn't decrease */
2931 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2932 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002933
2934 batch = &current->memcg_batch;
2935 /*
2936 * In usual, we do css_get() when we remember memcg pointer.
2937 * But in this case, we keep res->usage until end of a series of
2938 * uncharges. Then, it's ok to ignore memcg's refcnt.
2939 */
2940 if (!batch->memcg)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002941 batch->memcg = memcg;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002942 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002943 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002944 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002945 * the same cgroup and we have chance to coalesce uncharges.
2946 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
2947 * because we want to do uncharge as soon as possible.
2948 */
2949
2950 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
2951 goto direct_uncharge;
2952
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002953 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08002954 goto direct_uncharge;
2955
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002956 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002957 * In typical case, batch->memcg == mem. This means we can
2958 * merge a series of uncharges to an uncharge of res_counter.
2959 * If not, we uncharge res_counter ony by one.
2960 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002961 if (batch->memcg != memcg)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002962 goto direct_uncharge;
2963 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07002964 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002965 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07002966 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002967 return;
2968direct_uncharge:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002969 res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002970 if (uncharge_memsw)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002971 res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE);
2972 if (unlikely(batch->memcg != memcg))
2973 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002974 return;
2975}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002976
Balbir Singh8697d332008-02-07 00:13:59 -08002977/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002978 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002979 */
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002980static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002981__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002982{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002983 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002984 unsigned int nr_pages = 1;
2985 struct page_cgroup *pc;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002986
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002987 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002988 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07002989
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002990 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002991 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002992
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002993 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002994 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002995 VM_BUG_ON(!PageTransHuge(page));
2996 }
Balbir Singh8697d332008-02-07 00:13:59 -08002997 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08002998 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08002999 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003000 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08003001 if (unlikely(!PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003002 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003003
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003004 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003005
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003006 memcg = pc->mem_cgroup;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003007
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003008 if (!PageCgroupUsed(pc))
3009 goto unlock_out;
3010
3011 switch (ctype) {
3012 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003013 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003014 /* See mem_cgroup_prepare_migration() */
3015 if (page_mapped(page) || PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003016 goto unlock_out;
3017 break;
3018 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
3019 if (!PageAnon(page)) { /* Shared memory */
3020 if (page->mapping && !page_is_file_cache(page))
3021 goto unlock_out;
3022 } else if (page_mapped(page)) /* Anon */
3023 goto unlock_out;
3024 break;
3025 default:
3026 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003027 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003028
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003029 mem_cgroup_charge_statistics(memcg, PageCgroupCache(pc), -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003030
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003031 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08003032 /*
3033 * pc->mem_cgroup is not cleared here. It will be accessed when it's
3034 * freed from LRU. This is safe because uncharged page is expected not
3035 * to be reused (freed soon). Exception is SwapCache, it's handled by
3036 * special functions.
3037 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003038
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003039 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003040 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003041 * even after unlock, we have memcg->res.usage here and this memcg
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003042 * will never be freed.
3043 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003044 memcg_check_events(memcg, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003045 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003046 mem_cgroup_swap_statistics(memcg, true);
3047 mem_cgroup_get(memcg);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003048 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003049 if (!mem_cgroup_is_root(memcg))
3050 mem_cgroup_do_uncharge(memcg, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003051
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003052 return memcg;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003053
3054unlock_out:
3055 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003056 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08003057}
3058
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003059void mem_cgroup_uncharge_page(struct page *page)
3060{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003061 /* early check. */
3062 if (page_mapped(page))
3063 return;
Johannes Weiner40f23a22012-01-12 17:18:45 -08003064 VM_BUG_ON(page->mapping && !PageAnon(page));
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003065 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
3066}
3067
3068void mem_cgroup_uncharge_cache_page(struct page *page)
3069{
3070 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07003071 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003072 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
3073}
3074
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003075/*
3076 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
3077 * In that cases, pages are freed continuously and we can expect pages
3078 * are in the same memcg. All these calls itself limits the number of
3079 * pages freed at once, then uncharge_start/end() is called properly.
3080 * This may be called prural(2) times in a context,
3081 */
3082
3083void mem_cgroup_uncharge_start(void)
3084{
3085 current->memcg_batch.do_batch++;
3086 /* We can do nest. */
3087 if (current->memcg_batch.do_batch == 1) {
3088 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003089 current->memcg_batch.nr_pages = 0;
3090 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003091 }
3092}
3093
3094void mem_cgroup_uncharge_end(void)
3095{
3096 struct memcg_batch_info *batch = &current->memcg_batch;
3097
3098 if (!batch->do_batch)
3099 return;
3100
3101 batch->do_batch--;
3102 if (batch->do_batch) /* If stacked, do nothing. */
3103 return;
3104
3105 if (!batch->memcg)
3106 return;
3107 /*
3108 * This "batch->memcg" is valid without any css_get/put etc...
3109 * bacause we hide charges behind us.
3110 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003111 if (batch->nr_pages)
3112 res_counter_uncharge(&batch->memcg->res,
3113 batch->nr_pages * PAGE_SIZE);
3114 if (batch->memsw_nr_pages)
3115 res_counter_uncharge(&batch->memcg->memsw,
3116 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003117 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003118 /* forget this pointer (for sanity check) */
3119 batch->memcg = NULL;
3120}
3121
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003122#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003123/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003124 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003125 * memcg information is recorded to swap_cgroup of "ent"
3126 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003127void
3128mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003129{
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003130 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003131 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003132
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003133 if (!swapout) /* this was a swap cache but the swap is unused ! */
3134 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
3135
3136 memcg = __mem_cgroup_uncharge_common(page, ctype);
3137
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003138 /*
3139 * record memcg information, if swapout && memcg != NULL,
3140 * mem_cgroup_get() was called in uncharge().
3141 */
3142 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003143 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003144}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003145#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003146
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003147#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3148/*
3149 * called from swap_entry_free(). remove record in swap_cgroup and
3150 * uncharge "memsw" account.
3151 */
3152void mem_cgroup_uncharge_swap(swp_entry_t ent)
3153{
3154 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003155 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003156
3157 if (!do_swap_account)
3158 return;
3159
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003160 id = swap_cgroup_record(ent, 0);
3161 rcu_read_lock();
3162 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003163 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003164 /*
3165 * We uncharge this because swap is freed.
3166 * This memcg can be obsolete one. We avoid calling css_tryget
3167 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003168 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07003169 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003170 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003171 mem_cgroup_put(memcg);
3172 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003173 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003174}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003175
3176/**
3177 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
3178 * @entry: swap entry to be moved
3179 * @from: mem_cgroup which the entry is moved from
3180 * @to: mem_cgroup which the entry is moved to
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003181 * @need_fixup: whether we should fixup res_counters and refcounts.
Daisuke Nishimura02491442010-03-10 15:22:17 -08003182 *
3183 * It succeeds only when the swap_cgroup's record for this entry is the same
3184 * as the mem_cgroup's id of @from.
3185 *
3186 * Returns 0 on success, -EINVAL on failure.
3187 *
3188 * The caller must have charged to @to, IOW, called res_counter_charge() about
3189 * both res and memsw, and called css_get().
3190 */
3191static int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003192 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003193{
3194 unsigned short old_id, new_id;
3195
3196 old_id = css_id(&from->css);
3197 new_id = css_id(&to->css);
3198
3199 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08003200 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003201 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003202 /*
3203 * This function is only called from task migration context now.
3204 * It postpones res_counter and refcount handling till the end
3205 * of task migration(mem_cgroup_clear_mc()) for performance
3206 * improvement. But we cannot postpone mem_cgroup_get(to)
3207 * because if the process that has been moved to @to does
3208 * swap-in, the refcount of @to might be decreased to 0.
3209 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08003210 mem_cgroup_get(to);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003211 if (need_fixup) {
3212 if (!mem_cgroup_is_root(from))
3213 res_counter_uncharge(&from->memsw, PAGE_SIZE);
3214 mem_cgroup_put(from);
3215 /*
3216 * we charged both to->res and to->memsw, so we should
3217 * uncharge to->res.
3218 */
3219 if (!mem_cgroup_is_root(to))
3220 res_counter_uncharge(&to->res, PAGE_SIZE);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003221 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08003222 return 0;
3223 }
3224 return -EINVAL;
3225}
3226#else
3227static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003228 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003229{
3230 return -EINVAL;
3231}
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003232#endif
3233
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003234/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003235 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
3236 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003237 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003238int mem_cgroup_prepare_migration(struct page *page,
Johannes Weiner72835c82012-01-12 17:18:32 -08003239 struct page *newpage, struct mem_cgroup **memcgp, gfp_t gfp_mask)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003240{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003241 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003242 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003243 enum charge_type ctype;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003244 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003245
Johannes Weiner72835c82012-01-12 17:18:32 -08003246 *memcgp = NULL;
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07003247
Andrea Arcangeliec168512011-01-13 15:46:56 -08003248 VM_BUG_ON(PageTransHuge(page));
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003249 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07003250 return 0;
3251
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003252 pc = lookup_page_cgroup(page);
3253 lock_page_cgroup(pc);
3254 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003255 memcg = pc->mem_cgroup;
3256 css_get(&memcg->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003257 /*
3258 * At migrating an anonymous page, its mapcount goes down
3259 * to 0 and uncharge() will be called. But, even if it's fully
3260 * unmapped, migration may fail and this page has to be
3261 * charged again. We set MIGRATION flag here and delay uncharge
3262 * until end_migration() is called
3263 *
3264 * Corner Case Thinking
3265 * A)
3266 * When the old page was mapped as Anon and it's unmap-and-freed
3267 * while migration was ongoing.
3268 * If unmap finds the old page, uncharge() of it will be delayed
3269 * until end_migration(). If unmap finds a new page, it's
3270 * uncharged when it make mapcount to be 1->0. If unmap code
3271 * finds swap_migration_entry, the new page will not be mapped
3272 * and end_migration() will find it(mapcount==0).
3273 *
3274 * B)
3275 * When the old page was mapped but migraion fails, the kernel
3276 * remaps it. A charge for it is kept by MIGRATION flag even
3277 * if mapcount goes down to 0. We can do remap successfully
3278 * without charging it again.
3279 *
3280 * C)
3281 * The "old" page is under lock_page() until the end of
3282 * migration, so, the old page itself will not be swapped-out.
3283 * If the new page is swapped out before end_migraton, our
3284 * hook to usual swap-out path will catch the event.
3285 */
3286 if (PageAnon(page))
3287 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003288 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003289 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003290 /*
3291 * If the page is not charged at this point,
3292 * we return here.
3293 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003294 if (!memcg)
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003295 return 0;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003296
Johannes Weiner72835c82012-01-12 17:18:32 -08003297 *memcgp = memcg;
3298 ret = __mem_cgroup_try_charge(NULL, gfp_mask, 1, memcgp, false);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003299 css_put(&memcg->css);/* drop extra refcnt */
Johannes Weiner72835c82012-01-12 17:18:32 -08003300 if (ret || *memcgp == NULL) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003301 if (PageAnon(page)) {
3302 lock_page_cgroup(pc);
3303 ClearPageCgroupMigration(pc);
3304 unlock_page_cgroup(pc);
3305 /*
3306 * The old page may be fully unmapped while we kept it.
3307 */
3308 mem_cgroup_uncharge_page(page);
3309 }
3310 return -ENOMEM;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003311 }
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003312 /*
3313 * We charge new page before it's used/mapped. So, even if unlock_page()
3314 * is called before end_migration, we can catch all events on this new
3315 * page. In the case new page is migrated but not remapped, new page's
3316 * mapcount will be finally 0 and we call uncharge in end_migration().
3317 */
3318 pc = lookup_page_cgroup(newpage);
3319 if (PageAnon(page))
3320 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
3321 else if (page_is_file_cache(page))
3322 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
3323 else
3324 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003325 __mem_cgroup_commit_charge(memcg, page, 1, pc, ctype);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003326 return ret;
3327}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08003328
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003329/* remove redundant charge if migration failed*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003330void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003331 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003332{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003333 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003334 struct page_cgroup *pc;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003335
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003336 if (!memcg)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003337 return;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003338 /* blocks rmdir() */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003339 cgroup_exclude_rmdir(&memcg->css);
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003340 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003341 used = oldpage;
3342 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003343 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003344 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003345 unused = oldpage;
3346 }
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003347 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003348 * We disallowed uncharge of pages under migration because mapcount
3349 * of the page goes down to zero, temporarly.
3350 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003351 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003352 pc = lookup_page_cgroup(oldpage);
3353 lock_page_cgroup(pc);
3354 ClearPageCgroupMigration(pc);
3355 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003356
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003357 __mem_cgroup_uncharge_common(unused, MEM_CGROUP_CHARGE_TYPE_FORCE);
3358
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003359 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003360 * If a page is a file cache, radix-tree replacement is very atomic
3361 * and we can skip this check. When it was an Anon page, its mapcount
3362 * goes down to 0. But because we added MIGRATION flage, it's not
3363 * uncharged yet. There are several case but page->mapcount check
3364 * and USED bit check in mem_cgroup_uncharge_page() will do enough
3365 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003366 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003367 if (PageAnon(used))
3368 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003369 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003370 * At migration, we may charge account against cgroup which has no
3371 * tasks.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003372 * So, rmdir()->pre_destroy() can be called while we do this charge.
3373 * In that case, we need to call pre_destroy() again. check it here.
3374 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003375 cgroup_release_and_wakeup_rmdir(&memcg->css);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003376}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08003377
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003378/*
3379 * At replace page cache, newpage is not under any memcg but it's on
3380 * LRU. So, this function doesn't touch res_counter but handles LRU
3381 * in correct way. Both pages are locked so we cannot race with uncharge.
3382 */
3383void mem_cgroup_replace_page_cache(struct page *oldpage,
3384 struct page *newpage)
3385{
3386 struct mem_cgroup *memcg;
3387 struct page_cgroup *pc;
3388 struct zone *zone;
3389 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
3390 unsigned long flags;
3391
3392 if (mem_cgroup_disabled())
3393 return;
3394
3395 pc = lookup_page_cgroup(oldpage);
3396 /* fix accounting on old pages */
3397 lock_page_cgroup(pc);
3398 memcg = pc->mem_cgroup;
3399 mem_cgroup_charge_statistics(memcg, PageCgroupCache(pc), -1);
3400 ClearPageCgroupUsed(pc);
3401 unlock_page_cgroup(pc);
3402
3403 if (PageSwapBacked(oldpage))
3404 type = MEM_CGROUP_CHARGE_TYPE_SHMEM;
3405
3406 zone = page_zone(newpage);
3407 pc = lookup_page_cgroup(newpage);
3408 /*
3409 * Even if newpage->mapping was NULL before starting replacement,
3410 * the newpage may be on LRU(or pagevec for LRU) already. We lock
3411 * LRU while we overwrite pc->mem_cgroup.
3412 */
3413 spin_lock_irqsave(&zone->lru_lock, flags);
3414 if (PageLRU(newpage))
3415 del_page_from_lru_list(zone, newpage, page_lru(newpage));
3416 __mem_cgroup_commit_charge(memcg, newpage, 1, pc, type);
3417 if (PageLRU(newpage))
3418 add_page_to_lru_list(zone, newpage, page_lru(newpage));
3419 spin_unlock_irqrestore(&zone->lru_lock, flags);
3420}
3421
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003422#ifdef CONFIG_DEBUG_VM
3423static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
3424{
3425 struct page_cgroup *pc;
3426
3427 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08003428 /*
3429 * Can be NULL while feeding pages into the page allocator for
3430 * the first time, i.e. during boot or memory hotplug;
3431 * or when mem_cgroup_disabled().
3432 */
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003433 if (likely(pc) && PageCgroupUsed(pc))
3434 return pc;
3435 return NULL;
3436}
3437
3438bool mem_cgroup_bad_page_check(struct page *page)
3439{
3440 if (mem_cgroup_disabled())
3441 return false;
3442
3443 return lookup_page_cgroup_used(page) != NULL;
3444}
3445
3446void mem_cgroup_print_bad_page(struct page *page)
3447{
3448 struct page_cgroup *pc;
3449
3450 pc = lookup_page_cgroup_used(page);
3451 if (pc) {
3452 int ret = -1;
3453 char *path;
3454
3455 printk(KERN_ALERT "pc:%p pc->flags:%lx pc->mem_cgroup:%p",
3456 pc, pc->flags, pc->mem_cgroup);
3457
3458 path = kmalloc(PATH_MAX, GFP_KERNEL);
3459 if (path) {
3460 rcu_read_lock();
3461 ret = cgroup_path(pc->mem_cgroup->css.cgroup,
3462 path, PATH_MAX);
3463 rcu_read_unlock();
3464 }
3465
3466 printk(KERN_CONT "(%s)\n",
3467 (ret < 0) ? "cannot get the path" : path);
3468 kfree(path);
3469 }
3470}
3471#endif
3472
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003473static DEFINE_MUTEX(set_limit_mutex);
3474
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08003475static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003476 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003477{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003478 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003479 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003480 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003481 int children = mem_cgroup_count_children(memcg);
3482 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003483 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003484
3485 /*
3486 * For keeping hierarchical_reclaim simple, how long we should retry
3487 * is depends on callers. We set our retry-count to be function
3488 * of # of children which we should visit in this loop.
3489 */
3490 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
3491
3492 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003493
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003494 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003495 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003496 if (signal_pending(current)) {
3497 ret = -EINTR;
3498 break;
3499 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003500 /*
3501 * Rather than hide all in some function, I do this in
3502 * open coded manner. You see what this really does.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003503 * We have to guarantee memcg->res.limit < memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003504 */
3505 mutex_lock(&set_limit_mutex);
3506 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3507 if (memswlimit < val) {
3508 ret = -EINVAL;
3509 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003510 break;
3511 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003512
3513 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3514 if (memlimit < val)
3515 enlarge = 1;
3516
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003517 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003518 if (!ret) {
3519 if (memswlimit == val)
3520 memcg->memsw_is_minimum = true;
3521 else
3522 memcg->memsw_is_minimum = false;
3523 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003524 mutex_unlock(&set_limit_mutex);
3525
3526 if (!ret)
3527 break;
3528
Johannes Weiner56600482012-01-12 17:17:59 -08003529 mem_cgroup_reclaim(memcg, GFP_KERNEL,
3530 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003531 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
3532 /* Usage is reduced ? */
3533 if (curusage >= oldusage)
3534 retry_count--;
3535 else
3536 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003537 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003538 if (!ret && enlarge)
3539 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08003540
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003541 return ret;
3542}
3543
Li Zefan338c8432009-06-17 16:27:15 -07003544static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
3545 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003546{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003547 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003548 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003549 int children = mem_cgroup_count_children(memcg);
3550 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003551 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003552
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003553 /* see mem_cgroup_resize_res_limit */
3554 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
3555 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003556 while (retry_count) {
3557 if (signal_pending(current)) {
3558 ret = -EINTR;
3559 break;
3560 }
3561 /*
3562 * Rather than hide all in some function, I do this in
3563 * open coded manner. You see what this really does.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003564 * We have to guarantee memcg->res.limit < memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003565 */
3566 mutex_lock(&set_limit_mutex);
3567 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3568 if (memlimit > val) {
3569 ret = -EINVAL;
3570 mutex_unlock(&set_limit_mutex);
3571 break;
3572 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003573 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3574 if (memswlimit < val)
3575 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003576 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003577 if (!ret) {
3578 if (memlimit == val)
3579 memcg->memsw_is_minimum = true;
3580 else
3581 memcg->memsw_is_minimum = false;
3582 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003583 mutex_unlock(&set_limit_mutex);
3584
3585 if (!ret)
3586 break;
3587
Johannes Weiner56600482012-01-12 17:17:59 -08003588 mem_cgroup_reclaim(memcg, GFP_KERNEL,
3589 MEM_CGROUP_RECLAIM_NOSWAP |
3590 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003591 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003592 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003593 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003594 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003595 else
3596 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003597 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003598 if (!ret && enlarge)
3599 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003600 return ret;
3601}
3602
Balbir Singh4e416952009-09-23 15:56:39 -07003603unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
Ying Han0ae5e892011-05-26 16:25:25 -07003604 gfp_t gfp_mask,
3605 unsigned long *total_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07003606{
3607 unsigned long nr_reclaimed = 0;
3608 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
3609 unsigned long reclaimed;
3610 int loop = 0;
3611 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003612 unsigned long long excess;
Ying Han0ae5e892011-05-26 16:25:25 -07003613 unsigned long nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003614
3615 if (order > 0)
3616 return 0;
3617
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07003618 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
Balbir Singh4e416952009-09-23 15:56:39 -07003619 /*
3620 * This loop can run a while, specially if mem_cgroup's continuously
3621 * keep exceeding their soft limit and putting the system under
3622 * pressure
3623 */
3624 do {
3625 if (next_mz)
3626 mz = next_mz;
3627 else
3628 mz = mem_cgroup_largest_soft_limit_node(mctz);
3629 if (!mz)
3630 break;
3631
Ying Han0ae5e892011-05-26 16:25:25 -07003632 nr_scanned = 0;
Johannes Weiner56600482012-01-12 17:17:59 -08003633 reclaimed = mem_cgroup_soft_reclaim(mz->mem, zone,
3634 gfp_mask, &nr_scanned);
Balbir Singh4e416952009-09-23 15:56:39 -07003635 nr_reclaimed += reclaimed;
Ying Han0ae5e892011-05-26 16:25:25 -07003636 *total_scanned += nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003637 spin_lock(&mctz->lock);
3638
3639 /*
3640 * If we failed to reclaim anything from this memory cgroup
3641 * it is time to move on to the next cgroup
3642 */
3643 next_mz = NULL;
3644 if (!reclaimed) {
3645 do {
3646 /*
3647 * Loop until we find yet another one.
3648 *
3649 * By the time we get the soft_limit lock
3650 * again, someone might have aded the
3651 * group back on the RB tree. Iterate to
3652 * make sure we get a different mem.
3653 * mem_cgroup_largest_soft_limit_node returns
3654 * NULL if no other cgroup is present on
3655 * the tree
3656 */
3657 next_mz =
3658 __mem_cgroup_largest_soft_limit_node(mctz);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003659 if (next_mz == mz)
Balbir Singh4e416952009-09-23 15:56:39 -07003660 css_put(&next_mz->mem->css);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003661 else /* next_mz == NULL or other memcg */
Balbir Singh4e416952009-09-23 15:56:39 -07003662 break;
3663 } while (1);
3664 }
Balbir Singh4e416952009-09-23 15:56:39 -07003665 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003666 excess = res_counter_soft_limit_excess(&mz->mem->res);
Balbir Singh4e416952009-09-23 15:56:39 -07003667 /*
3668 * One school of thought says that we should not add
3669 * back the node to the tree if reclaim returns 0.
3670 * But our reclaim could return 0, simply because due
3671 * to priority we are exposing a smaller subset of
3672 * memory to reclaim from. Consider this as a longer
3673 * term TODO.
3674 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003675 /* If excess == 0, no tree ops */
3676 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07003677 spin_unlock(&mctz->lock);
3678 css_put(&mz->mem->css);
3679 loop++;
3680 /*
3681 * Could not reclaim anything and there are no more
3682 * mem cgroups to try or we seem to be looping without
3683 * reclaiming anything.
3684 */
3685 if (!nr_reclaimed &&
3686 (next_mz == NULL ||
3687 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3688 break;
3689 } while (!nr_reclaimed);
3690 if (next_mz)
3691 css_put(&next_mz->mem->css);
3692 return nr_reclaimed;
3693}
3694
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003695/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003696 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003697 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
3698 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003699static int mem_cgroup_force_empty_list(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003700 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003701{
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003702 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003703 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003704 struct list_head *list;
Johannes Weiner925b7672012-01-12 17:18:15 -08003705 struct page *busy;
3706 struct zone *zone;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003707 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003708
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003709 zone = &NODE_DATA(node)->node_zones[zid];
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003710 mz = mem_cgroup_zoneinfo(memcg, node, zid);
Johannes Weiner6290df52012-01-12 17:18:10 -08003711 list = &mz->lruvec.lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003712
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003713 loop = MEM_CGROUP_ZSTAT(mz, lru);
3714 /* give some margin against EBUSY etc...*/
3715 loop += 256;
3716 busy = NULL;
3717 while (loop--) {
Johannes Weiner925b7672012-01-12 17:18:15 -08003718 struct page_cgroup *pc;
Johannes Weiner5564e882011-03-23 16:42:29 -07003719 struct page *page;
3720
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003721 ret = 0;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003722 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003723 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003724 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003725 break;
3726 }
Johannes Weiner925b7672012-01-12 17:18:15 -08003727 page = list_entry(list->prev, struct page, lru);
3728 if (busy == page) {
3729 list_move(&page->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08003730 busy = NULL;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003731 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003732 continue;
3733 }
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003734 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003735
Johannes Weiner925b7672012-01-12 17:18:15 -08003736 pc = lookup_page_cgroup(page);
Johannes Weiner5564e882011-03-23 16:42:29 -07003737
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003738 ret = mem_cgroup_move_parent(page, pc, memcg, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003739 if (ret == -ENOMEM)
3740 break;
3741
3742 if (ret == -EBUSY || ret == -EINVAL) {
3743 /* found lock contention or "pc" is obsolete. */
Johannes Weiner925b7672012-01-12 17:18:15 -08003744 busy = page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003745 cond_resched();
3746 } else
3747 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003748 }
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003749
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003750 if (!ret && !list_empty(list))
3751 return -EBUSY;
3752 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003753}
3754
3755/*
3756 * make mem_cgroup's charge to be 0 if there is no task.
3757 * This enables deleting this mem_cgroup.
3758 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003759static int mem_cgroup_force_empty(struct mem_cgroup *memcg, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003760{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003761 int ret;
3762 int node, zid, shrink;
3763 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003764 struct cgroup *cgrp = memcg->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003765
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003766 css_get(&memcg->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003767
3768 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003769 /* should free all ? */
3770 if (free_all)
3771 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003772move_account:
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003773 do {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003774 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003775 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003776 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003777 ret = -EINTR;
3778 if (signal_pending(current))
3779 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003780 /* This is for making all *used* pages to be on LRU. */
3781 lru_add_drain_all();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003782 drain_all_stock_sync(memcg);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003783 ret = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003784 mem_cgroup_start_move(memcg);
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08003785 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003786 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07003787 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003788 for_each_lru(l) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003789 ret = mem_cgroup_force_empty_list(memcg,
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003790 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003791 if (ret)
3792 break;
3793 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003794 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003795 if (ret)
3796 break;
3797 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003798 mem_cgroup_end_move(memcg);
3799 memcg_oom_recover(memcg);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003800 /* it seems parent cgroup doesn't have enough mem */
3801 if (ret == -ENOMEM)
3802 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003803 cond_resched();
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003804 /* "ret" should also be checked to ensure all lists are empty. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003805 } while (memcg->res.usage > 0 || ret);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003806out:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003807 css_put(&memcg->css);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003808 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003809
3810try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003811 /* returns EBUSY if there is a task or if we come here twice. */
3812 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003813 ret = -EBUSY;
3814 goto out;
3815 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003816 /* we call try-to-free pages for make this cgroup empty */
3817 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003818 /* try to free all pages in this cgroup */
3819 shrink = 1;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003820 while (nr_retries && memcg->res.usage > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003821 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003822
3823 if (signal_pending(current)) {
3824 ret = -EINTR;
3825 goto out;
3826 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003827 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
Johannes Weiner185efc02011-09-14 16:21:58 -07003828 false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003829 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003830 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003831 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02003832 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003833 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003834
3835 }
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003836 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003837 /* try move_account...there may be some *locked* pages. */
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003838 goto move_account;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003839}
3840
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003841int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
3842{
3843 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
3844}
3845
3846
Balbir Singh18f59ea2009-01-07 18:08:07 -08003847static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
3848{
3849 return mem_cgroup_from_cont(cont)->use_hierarchy;
3850}
3851
3852static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
3853 u64 val)
3854{
3855 int retval = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003856 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003857 struct cgroup *parent = cont->parent;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003858 struct mem_cgroup *parent_memcg = NULL;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003859
3860 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003861 parent_memcg = mem_cgroup_from_cont(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003862
3863 cgroup_lock();
3864 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003865 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08003866 * in the child subtrees. If it is unset, then the change can
3867 * occur, provided the current cgroup has no children.
3868 *
3869 * For the root cgroup, parent_mem is NULL, we allow value to be
3870 * set if there are no children.
3871 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003872 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08003873 (val == 1 || val == 0)) {
3874 if (list_empty(&cont->children))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003875 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003876 else
3877 retval = -EBUSY;
3878 } else
3879 retval = -EINVAL;
3880 cgroup_unlock();
3881
3882 return retval;
3883}
3884
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003885
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003886static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003887 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003888{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003889 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003890 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003891
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003892 /* Per-cpu values can be negative, use a signed accumulator */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003893 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003894 val += mem_cgroup_read_stat(iter, idx);
3895
3896 if (val < 0) /* race ? */
3897 val = 0;
3898 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003899}
3900
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003901static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003902{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003903 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003904
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003905 if (!mem_cgroup_is_root(memcg)) {
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003906 if (!swap)
Glauber Costa65c64ce2011-12-22 01:02:27 +00003907 return res_counter_read_u64(&memcg->res, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003908 else
Glauber Costa65c64ce2011-12-22 01:02:27 +00003909 return res_counter_read_u64(&memcg->memsw, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003910 }
3911
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003912 val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
3913 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003914
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003915 if (swap)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003916 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAPOUT);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003917
3918 return val << PAGE_SHIFT;
3919}
3920
Paul Menage2c3daa72008-04-29 00:59:58 -07003921static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003922{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003923 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003924 u64 val;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003925 int type, name;
3926
3927 type = MEMFILE_TYPE(cft->private);
3928 name = MEMFILE_ATTR(cft->private);
3929 switch (type) {
3930 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003931 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003932 val = mem_cgroup_usage(memcg, false);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003933 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003934 val = res_counter_read_u64(&memcg->res, name);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003935 break;
3936 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003937 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003938 val = mem_cgroup_usage(memcg, true);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003939 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003940 val = res_counter_read_u64(&memcg->memsw, name);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003941 break;
3942 default:
3943 BUG();
3944 break;
3945 }
3946 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003947}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003948/*
3949 * The user of this function is...
3950 * RES_LIMIT.
3951 */
Paul Menage856c13a2008-07-25 01:47:04 -07003952static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
3953 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003954{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003955 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003956 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003957 unsigned long long val;
3958 int ret;
3959
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003960 type = MEMFILE_TYPE(cft->private);
3961 name = MEMFILE_ATTR(cft->private);
3962 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003963 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07003964 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
3965 ret = -EINVAL;
3966 break;
3967 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003968 /* This function does all necessary parse...reuse it */
3969 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003970 if (ret)
3971 break;
3972 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003973 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003974 else
3975 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003976 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07003977 case RES_SOFT_LIMIT:
3978 ret = res_counter_memparse_write_strategy(buffer, &val);
3979 if (ret)
3980 break;
3981 /*
3982 * For memsw, soft limits are hard to implement in terms
3983 * of semantics, for now, we support soft limits for
3984 * control without swap
3985 */
3986 if (type == _MEM)
3987 ret = res_counter_set_soft_limit(&memcg->res, val);
3988 else
3989 ret = -EINVAL;
3990 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003991 default:
3992 ret = -EINVAL; /* should be BUG() ? */
3993 break;
3994 }
3995 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003996}
3997
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08003998static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
3999 unsigned long long *mem_limit, unsigned long long *memsw_limit)
4000{
4001 struct cgroup *cgroup;
4002 unsigned long long min_limit, min_memsw_limit, tmp;
4003
4004 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4005 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4006 cgroup = memcg->css.cgroup;
4007 if (!memcg->use_hierarchy)
4008 goto out;
4009
4010 while (cgroup->parent) {
4011 cgroup = cgroup->parent;
4012 memcg = mem_cgroup_from_cont(cgroup);
4013 if (!memcg->use_hierarchy)
4014 break;
4015 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
4016 min_limit = min(min_limit, tmp);
4017 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4018 min_memsw_limit = min(min_memsw_limit, tmp);
4019 }
4020out:
4021 *mem_limit = min_limit;
4022 *memsw_limit = min_memsw_limit;
4023 return;
4024}
4025
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004026static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004027{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004028 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004029 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004030
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004031 memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004032 type = MEMFILE_TYPE(event);
4033 name = MEMFILE_ATTR(event);
4034 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004035 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004036 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004037 res_counter_reset_max(&memcg->res);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004038 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004039 res_counter_reset_max(&memcg->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004040 break;
4041 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004042 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004043 res_counter_reset_failcnt(&memcg->res);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004044 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004045 res_counter_reset_failcnt(&memcg->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004046 break;
4047 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07004048
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07004049 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004050}
4051
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004052static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
4053 struct cftype *cft)
4054{
4055 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
4056}
4057
Daisuke Nishimura02491442010-03-10 15:22:17 -08004058#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004059static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
4060 struct cftype *cft, u64 val)
4061{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004062 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004063
4064 if (val >= (1 << NR_MOVE_TYPE))
4065 return -EINVAL;
4066 /*
4067 * We check this value several times in both in can_attach() and
4068 * attach(), so we need cgroup lock to prevent this value from being
4069 * inconsistent.
4070 */
4071 cgroup_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004072 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004073 cgroup_unlock();
4074
4075 return 0;
4076}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004077#else
4078static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
4079 struct cftype *cft, u64 val)
4080{
4081 return -ENOSYS;
4082}
4083#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004084
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004085
4086/* For read statistics */
4087enum {
4088 MCS_CACHE,
4089 MCS_RSS,
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08004090 MCS_FILE_MAPPED,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004091 MCS_PGPGIN,
4092 MCS_PGPGOUT,
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004093 MCS_SWAP,
Ying Han456f9982011-05-26 16:25:38 -07004094 MCS_PGFAULT,
4095 MCS_PGMAJFAULT,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004096 MCS_INACTIVE_ANON,
4097 MCS_ACTIVE_ANON,
4098 MCS_INACTIVE_FILE,
4099 MCS_ACTIVE_FILE,
4100 MCS_UNEVICTABLE,
4101 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004102};
4103
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004104struct mcs_total_stat {
4105 s64 stat[NR_MCS_STAT];
4106};
4107
4108struct {
4109 char *local_name;
4110 char *total_name;
4111} memcg_stat_strings[NR_MCS_STAT] = {
4112 {"cache", "total_cache"},
4113 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07004114 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004115 {"pgpgin", "total_pgpgin"},
4116 {"pgpgout", "total_pgpgout"},
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004117 {"swap", "total_swap"},
Ying Han456f9982011-05-26 16:25:38 -07004118 {"pgfault", "total_pgfault"},
4119 {"pgmajfault", "total_pgmajfault"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004120 {"inactive_anon", "total_inactive_anon"},
4121 {"active_anon", "total_active_anon"},
4122 {"inactive_file", "total_inactive_file"},
4123 {"active_file", "total_active_file"},
4124 {"unevictable", "total_unevictable"}
4125};
4126
4127
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004128static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004129mem_cgroup_get_local_stat(struct mem_cgroup *memcg, struct mcs_total_stat *s)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004130{
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004131 s64 val;
4132
4133 /* per cpu stat */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004134 val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_CACHE);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004135 s->stat[MCS_CACHE] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004136 val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_RSS);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004137 s->stat[MCS_RSS] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004138 val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_FILE_MAPPED);
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08004139 s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004140 val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGPGIN);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004141 s->stat[MCS_PGPGIN] += val;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004142 val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGPGOUT);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004143 s->stat[MCS_PGPGOUT] += val;
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004144 if (do_swap_account) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004145 val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_SWAPOUT);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004146 s->stat[MCS_SWAP] += val * PAGE_SIZE;
4147 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004148 val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGFAULT);
Ying Han456f9982011-05-26 16:25:38 -07004149 s->stat[MCS_PGFAULT] += val;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004150 val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGMAJFAULT);
Ying Han456f9982011-05-26 16:25:38 -07004151 s->stat[MCS_PGMAJFAULT] += val;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004152
4153 /* per zone stat */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004154 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_ANON));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004155 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004156 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_ANON));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004157 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004158 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_FILE));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004159 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004160 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_FILE));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004161 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004162 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004163 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004164}
4165
4166static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004167mem_cgroup_get_total_stat(struct mem_cgroup *memcg, struct mcs_total_stat *s)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004168{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004169 struct mem_cgroup *iter;
4170
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004171 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004172 mem_cgroup_get_local_stat(iter, s);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004173}
4174
Ying Han406eb0c2011-05-26 16:25:37 -07004175#ifdef CONFIG_NUMA
4176static int mem_control_numa_stat_show(struct seq_file *m, void *arg)
4177{
4178 int nid;
4179 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
4180 unsigned long node_nr;
4181 struct cgroup *cont = m->private;
4182 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
4183
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004184 total_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07004185 seq_printf(m, "total=%lu", total_nr);
4186 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004187 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07004188 seq_printf(m, " N%d=%lu", nid, node_nr);
4189 }
4190 seq_putc(m, '\n');
4191
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004192 file_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07004193 seq_printf(m, "file=%lu", file_nr);
4194 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004195 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
4196 LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07004197 seq_printf(m, " N%d=%lu", nid, node_nr);
4198 }
4199 seq_putc(m, '\n');
4200
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004201 anon_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07004202 seq_printf(m, "anon=%lu", anon_nr);
4203 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004204 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
4205 LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07004206 seq_printf(m, " N%d=%lu", nid, node_nr);
4207 }
4208 seq_putc(m, '\n');
4209
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004210 unevictable_nr = mem_cgroup_nr_lru_pages(mem_cont, BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07004211 seq_printf(m, "unevictable=%lu", unevictable_nr);
4212 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004213 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
4214 BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07004215 seq_printf(m, " N%d=%lu", nid, node_nr);
4216 }
4217 seq_putc(m, '\n');
4218 return 0;
4219}
4220#endif /* CONFIG_NUMA */
4221
Paul Menagec64745c2008-04-29 01:00:02 -07004222static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
4223 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004224{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004225 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004226 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004227 int i;
4228
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004229 memset(&mystat, 0, sizeof(mystat));
4230 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004231
Ying Han406eb0c2011-05-26 16:25:37 -07004232
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004233 for (i = 0; i < NR_MCS_STAT; i++) {
4234 if (i == MCS_SWAP && !do_swap_account)
4235 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004236 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004237 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004238
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004239 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004240 {
4241 unsigned long long limit, memsw_limit;
4242 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
4243 cb->fill(cb, "hierarchical_memory_limit", limit);
4244 if (do_swap_account)
4245 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
4246 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004247
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004248 memset(&mystat, 0, sizeof(mystat));
4249 mem_cgroup_get_total_stat(mem_cont, &mystat);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004250 for (i = 0; i < NR_MCS_STAT; i++) {
4251 if (i == MCS_SWAP && !do_swap_account)
4252 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004253 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004254 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004255
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004256#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004257 {
4258 int nid, zid;
4259 struct mem_cgroup_per_zone *mz;
4260 unsigned long recent_rotated[2] = {0, 0};
4261 unsigned long recent_scanned[2] = {0, 0};
4262
4263 for_each_online_node(nid)
4264 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
4265 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
4266
4267 recent_rotated[0] +=
4268 mz->reclaim_stat.recent_rotated[0];
4269 recent_rotated[1] +=
4270 mz->reclaim_stat.recent_rotated[1];
4271 recent_scanned[0] +=
4272 mz->reclaim_stat.recent_scanned[0];
4273 recent_scanned[1] +=
4274 mz->reclaim_stat.recent_scanned[1];
4275 }
4276 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
4277 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
4278 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
4279 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
4280 }
4281#endif
4282
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004283 return 0;
4284}
4285
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004286static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
4287{
4288 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4289
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07004290 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004291}
4292
4293static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
4294 u64 val)
4295{
4296 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4297 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08004298
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004299 if (val > 100)
4300 return -EINVAL;
4301
4302 if (cgrp->parent == NULL)
4303 return -EINVAL;
4304
4305 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08004306
4307 cgroup_lock();
4308
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004309 /* If under hierarchy, only empty-root can set this value */
4310 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08004311 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
4312 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004313 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08004314 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004315
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004316 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004317
Li Zefan068b38c2009-01-15 13:51:26 -08004318 cgroup_unlock();
4319
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004320 return 0;
4321}
4322
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004323static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
4324{
4325 struct mem_cgroup_threshold_ary *t;
4326 u64 usage;
4327 int i;
4328
4329 rcu_read_lock();
4330 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004331 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004332 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004333 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004334
4335 if (!t)
4336 goto unlock;
4337
4338 usage = mem_cgroup_usage(memcg, swap);
4339
4340 /*
4341 * current_threshold points to threshold just below usage.
4342 * If it's not true, a threshold was crossed after last
4343 * call of __mem_cgroup_threshold().
4344 */
Phil Carmody5407a562010-05-26 14:42:42 -07004345 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004346
4347 /*
4348 * Iterate backward over array of thresholds starting from
4349 * current_threshold and check if a threshold is crossed.
4350 * If none of thresholds below usage is crossed, we read
4351 * only one element of the array here.
4352 */
4353 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
4354 eventfd_signal(t->entries[i].eventfd, 1);
4355
4356 /* i = current_threshold + 1 */
4357 i++;
4358
4359 /*
4360 * Iterate forward over array of thresholds starting from
4361 * current_threshold+1 and check if a threshold is crossed.
4362 * If none of thresholds above usage is crossed, we read
4363 * only one element of the array here.
4364 */
4365 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
4366 eventfd_signal(t->entries[i].eventfd, 1);
4367
4368 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07004369 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004370unlock:
4371 rcu_read_unlock();
4372}
4373
4374static void mem_cgroup_threshold(struct mem_cgroup *memcg)
4375{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07004376 while (memcg) {
4377 __mem_cgroup_threshold(memcg, false);
4378 if (do_swap_account)
4379 __mem_cgroup_threshold(memcg, true);
4380
4381 memcg = parent_mem_cgroup(memcg);
4382 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004383}
4384
4385static int compare_thresholds(const void *a, const void *b)
4386{
4387 const struct mem_cgroup_threshold *_a = a;
4388 const struct mem_cgroup_threshold *_b = b;
4389
4390 return _a->threshold - _b->threshold;
4391}
4392
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004393static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004394{
4395 struct mem_cgroup_eventfd_list *ev;
4396
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004397 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004398 eventfd_signal(ev->eventfd, 1);
4399 return 0;
4400}
4401
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004402static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004403{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004404 struct mem_cgroup *iter;
4405
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004406 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004407 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004408}
4409
4410static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
4411 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004412{
4413 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004414 struct mem_cgroup_thresholds *thresholds;
4415 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004416 int type = MEMFILE_TYPE(cft->private);
4417 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004418 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004419
4420 ret = res_counter_memparse_write_strategy(args, &threshold);
4421 if (ret)
4422 return ret;
4423
4424 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004425
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004426 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004427 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004428 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004429 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004430 else
4431 BUG();
4432
4433 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4434
4435 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004436 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004437 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4438
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004439 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004440
4441 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004442 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004443 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004444 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004445 ret = -ENOMEM;
4446 goto unlock;
4447 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004448 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004449
4450 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004451 if (thresholds->primary) {
4452 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004453 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004454 }
4455
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004456 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004457 new->entries[size - 1].eventfd = eventfd;
4458 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004459
4460 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004461 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004462 compare_thresholds, NULL);
4463
4464 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004465 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004466 for (i = 0; i < size; i++) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004467 if (new->entries[i].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004468 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004469 * new->current_threshold will not be used until
4470 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004471 * it here.
4472 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004473 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004474 }
4475 }
4476
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004477 /* Free old spare buffer and save old primary buffer as spare */
4478 kfree(thresholds->spare);
4479 thresholds->spare = thresholds->primary;
4480
4481 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004482
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004483 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004484 synchronize_rcu();
4485
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004486unlock:
4487 mutex_unlock(&memcg->thresholds_lock);
4488
4489 return ret;
4490}
4491
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004492static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004493 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004494{
4495 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004496 struct mem_cgroup_thresholds *thresholds;
4497 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004498 int type = MEMFILE_TYPE(cft->private);
4499 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004500 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004501
4502 mutex_lock(&memcg->thresholds_lock);
4503 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004504 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004505 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004506 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004507 else
4508 BUG();
4509
4510 /*
4511 * Something went wrong if we trying to unregister a threshold
4512 * if we don't have thresholds
4513 */
4514 BUG_ON(!thresholds);
4515
4516 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4517
4518 /* Check if a threshold crossed before removing */
4519 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4520
4521 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004522 size = 0;
4523 for (i = 0; i < thresholds->primary->size; i++) {
4524 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004525 size++;
4526 }
4527
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004528 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004529
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004530 /* Set thresholds array to NULL if we don't have thresholds */
4531 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004532 kfree(new);
4533 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004534 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004535 }
4536
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004537 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004538
4539 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004540 new->current_threshold = -1;
4541 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
4542 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004543 continue;
4544
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004545 new->entries[j] = thresholds->primary->entries[i];
4546 if (new->entries[j].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004547 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004548 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004549 * until rcu_assign_pointer(), so it's safe to increment
4550 * it here.
4551 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004552 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004553 }
4554 j++;
4555 }
4556
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004557swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004558 /* Swap primary and spare array */
4559 thresholds->spare = thresholds->primary;
4560 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004561
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004562 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004563 synchronize_rcu();
4564
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004565 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004566}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004567
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004568static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
4569 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
4570{
4571 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4572 struct mem_cgroup_eventfd_list *event;
4573 int type = MEMFILE_TYPE(cft->private);
4574
4575 BUG_ON(type != _OOM_TYPE);
4576 event = kmalloc(sizeof(*event), GFP_KERNEL);
4577 if (!event)
4578 return -ENOMEM;
4579
Michal Hocko1af8efe2011-07-26 16:08:24 -07004580 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004581
4582 event->eventfd = eventfd;
4583 list_add(&event->list, &memcg->oom_notify);
4584
4585 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07004586 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004587 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07004588 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004589
4590 return 0;
4591}
4592
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004593static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004594 struct cftype *cft, struct eventfd_ctx *eventfd)
4595{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004596 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004597 struct mem_cgroup_eventfd_list *ev, *tmp;
4598 int type = MEMFILE_TYPE(cft->private);
4599
4600 BUG_ON(type != _OOM_TYPE);
4601
Michal Hocko1af8efe2011-07-26 16:08:24 -07004602 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004603
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004604 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004605 if (ev->eventfd == eventfd) {
4606 list_del(&ev->list);
4607 kfree(ev);
4608 }
4609 }
4610
Michal Hocko1af8efe2011-07-26 16:08:24 -07004611 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004612}
4613
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004614static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
4615 struct cftype *cft, struct cgroup_map_cb *cb)
4616{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004617 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004618
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004619 cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004620
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004621 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004622 cb->fill(cb, "under_oom", 1);
4623 else
4624 cb->fill(cb, "under_oom", 0);
4625 return 0;
4626}
4627
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004628static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
4629 struct cftype *cft, u64 val)
4630{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004631 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004632 struct mem_cgroup *parent;
4633
4634 /* cannot set to root cgroup and only 0 and 1 are allowed */
4635 if (!cgrp->parent || !((val == 0) || (val == 1)))
4636 return -EINVAL;
4637
4638 parent = mem_cgroup_from_cont(cgrp->parent);
4639
4640 cgroup_lock();
4641 /* oom-kill-disable is a flag for subhierarchy. */
4642 if ((parent->use_hierarchy) ||
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004643 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004644 cgroup_unlock();
4645 return -EINVAL;
4646 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004647 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07004648 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004649 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004650 cgroup_unlock();
4651 return 0;
4652}
4653
Ying Han406eb0c2011-05-26 16:25:37 -07004654#ifdef CONFIG_NUMA
4655static const struct file_operations mem_control_numa_stat_file_operations = {
4656 .read = seq_read,
4657 .llseek = seq_lseek,
4658 .release = single_release,
4659};
4660
4661static int mem_control_numa_stat_open(struct inode *unused, struct file *file)
4662{
4663 struct cgroup *cont = file->f_dentry->d_parent->d_fsdata;
4664
4665 file->f_op = &mem_control_numa_stat_file_operations;
4666 return single_open(file, mem_control_numa_stat_show, cont);
4667}
4668#endif /* CONFIG_NUMA */
4669
Glauber Costae5671df2011-12-11 21:47:01 +00004670#ifdef CONFIG_CGROUP_MEM_RES_CTLR_KMEM
Glauber Costae5671df2011-12-11 21:47:01 +00004671static int register_kmem_files(struct cgroup *cont, struct cgroup_subsys *ss)
4672{
Glauber Costad1a4c0b2011-12-11 21:47:04 +00004673 /*
4674 * Part of this would be better living in a separate allocation
4675 * function, leaving us with just the cgroup tree population work.
4676 * We, however, depend on state such as network's proto_list that
4677 * is only initialized after cgroup creation. I found the less
4678 * cumbersome way to deal with it to defer it all to populate time
4679 */
Glauber Costa65c64ce2011-12-22 01:02:27 +00004680 return mem_cgroup_sockets_init(cont, ss);
Glauber Costae5671df2011-12-11 21:47:01 +00004681};
4682
Glauber Costad1a4c0b2011-12-11 21:47:04 +00004683static void kmem_cgroup_destroy(struct cgroup_subsys *ss,
4684 struct cgroup *cont)
4685{
4686 mem_cgroup_sockets_destroy(cont, ss);
4687}
Glauber Costae5671df2011-12-11 21:47:01 +00004688#else
4689static int register_kmem_files(struct cgroup *cont, struct cgroup_subsys *ss)
4690{
4691 return 0;
4692}
Glauber Costad1a4c0b2011-12-11 21:47:04 +00004693
4694static void kmem_cgroup_destroy(struct cgroup_subsys *ss,
4695 struct cgroup *cont)
4696{
4697}
Glauber Costae5671df2011-12-11 21:47:01 +00004698#endif
4699
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004700static struct cftype mem_cgroup_files[] = {
4701 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004702 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004703 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07004704 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004705 .register_event = mem_cgroup_usage_register_event,
4706 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004707 },
4708 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004709 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004710 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004711 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004712 .read_u64 = mem_cgroup_read,
4713 },
4714 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004715 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004716 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07004717 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07004718 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004719 },
4720 {
Balbir Singh296c81d2009-09-23 15:56:36 -07004721 .name = "soft_limit_in_bytes",
4722 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
4723 .write_string = mem_cgroup_write,
4724 .read_u64 = mem_cgroup_read,
4725 },
4726 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004727 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004728 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004729 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07004730 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004731 },
Balbir Singh8697d332008-02-07 00:13:59 -08004732 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004733 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07004734 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004735 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004736 {
4737 .name = "force_empty",
4738 .trigger = mem_cgroup_force_empty_write,
4739 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08004740 {
4741 .name = "use_hierarchy",
4742 .write_u64 = mem_cgroup_hierarchy_write,
4743 .read_u64 = mem_cgroup_hierarchy_read,
4744 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004745 {
4746 .name = "swappiness",
4747 .read_u64 = mem_cgroup_swappiness_read,
4748 .write_u64 = mem_cgroup_swappiness_write,
4749 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004750 {
4751 .name = "move_charge_at_immigrate",
4752 .read_u64 = mem_cgroup_move_charge_read,
4753 .write_u64 = mem_cgroup_move_charge_write,
4754 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004755 {
4756 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004757 .read_map = mem_cgroup_oom_control_read,
4758 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004759 .register_event = mem_cgroup_oom_register_event,
4760 .unregister_event = mem_cgroup_oom_unregister_event,
4761 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
4762 },
Ying Han406eb0c2011-05-26 16:25:37 -07004763#ifdef CONFIG_NUMA
4764 {
4765 .name = "numa_stat",
4766 .open = mem_control_numa_stat_open,
KAMEZAWA Hiroyuki89577122011-06-15 15:08:41 -07004767 .mode = S_IRUGO,
Ying Han406eb0c2011-05-26 16:25:37 -07004768 },
4769#endif
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004770};
4771
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004772#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4773static struct cftype memsw_cgroup_files[] = {
4774 {
4775 .name = "memsw.usage_in_bytes",
4776 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
4777 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004778 .register_event = mem_cgroup_usage_register_event,
4779 .unregister_event = mem_cgroup_usage_unregister_event,
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004780 },
4781 {
4782 .name = "memsw.max_usage_in_bytes",
4783 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
4784 .trigger = mem_cgroup_reset,
4785 .read_u64 = mem_cgroup_read,
4786 },
4787 {
4788 .name = "memsw.limit_in_bytes",
4789 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
4790 .write_string = mem_cgroup_write,
4791 .read_u64 = mem_cgroup_read,
4792 },
4793 {
4794 .name = "memsw.failcnt",
4795 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
4796 .trigger = mem_cgroup_reset,
4797 .read_u64 = mem_cgroup_read,
4798 },
4799};
4800
4801static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
4802{
4803 if (!do_swap_account)
4804 return 0;
4805 return cgroup_add_files(cont, ss, memsw_cgroup_files,
4806 ARRAY_SIZE(memsw_cgroup_files));
4807};
4808#else
4809static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
4810{
4811 return 0;
4812}
4813#endif
4814
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004815static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004816{
4817 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004818 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07004819 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004820 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004821 /*
4822 * This routine is called against possible nodes.
4823 * But it's BUG to call kmalloc() against offline node.
4824 *
4825 * TODO: this routine can waste much memory for nodes which will
4826 * never be onlined. It's better to use memory hotplug callback
4827 * function.
4828 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004829 if (!node_state(node, N_NORMAL_MEMORY))
4830 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08004831 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004832 if (!pn)
4833 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004834
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004835 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4836 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07004837 for_each_lru(l)
Johannes Weiner6290df52012-01-12 17:18:10 -08004838 INIT_LIST_HEAD(&mz->lruvec.lists[l]);
Balbir Singhf64c3f52009-09-23 15:56:37 -07004839 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07004840 mz->on_tree = false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004841 mz->mem = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004842 }
Igor Mammedov0a619e52011-11-02 13:38:21 -07004843 memcg->info.nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004844 return 0;
4845}
4846
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004847static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004848{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004849 kfree(memcg->info.nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004850}
4851
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004852static struct mem_cgroup *mem_cgroup_alloc(void)
4853{
4854 struct mem_cgroup *mem;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004855 int size = sizeof(struct mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004856
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004857 /* Can be very big if MAX_NUMNODES is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08004858 if (size < PAGE_SIZE)
Jesper Juhl17295c82011-01-13 15:47:42 -08004859 mem = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004860 else
Jesper Juhl17295c82011-01-13 15:47:42 -08004861 mem = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004862
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07004863 if (!mem)
4864 return NULL;
4865
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004866 mem->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004867 if (!mem->stat)
4868 goto out_free;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07004869 spin_lock_init(&mem->pcp_counter_lock);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004870 return mem;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004871
4872out_free:
4873 if (size < PAGE_SIZE)
4874 kfree(mem);
4875 else
4876 vfree(mem);
4877 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004878}
4879
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004880/*
4881 * At destroying mem_cgroup, references from swap_cgroup can remain.
4882 * (scanning all at force_empty is too costly...)
4883 *
4884 * Instead of clearing all references at force_empty, we remember
4885 * the number of reference from swap_cgroup and free mem_cgroup when
4886 * it goes down to 0.
4887 *
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004888 * Removal of cgroup itself succeeds regardless of refs from swap.
4889 */
4890
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004891static void __mem_cgroup_free(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004892{
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08004893 int node;
4894
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004895 mem_cgroup_remove_from_trees(memcg);
4896 free_css_id(&mem_cgroup_subsys, &memcg->css);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004897
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08004898 for_each_node_state(node, N_POSSIBLE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004899 free_mem_cgroup_per_zone_info(memcg, node);
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08004900
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004901 free_percpu(memcg->stat);
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004902 if (sizeof(struct mem_cgroup) < PAGE_SIZE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004903 kfree(memcg);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004904 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004905 vfree(memcg);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004906}
4907
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004908static void mem_cgroup_get(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004909{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004910 atomic_inc(&memcg->refcnt);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004911}
4912
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004913static void __mem_cgroup_put(struct mem_cgroup *memcg, int count)
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004914{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004915 if (atomic_sub_and_test(count, &memcg->refcnt)) {
4916 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
4917 __mem_cgroup_free(memcg);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004918 if (parent)
4919 mem_cgroup_put(parent);
4920 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004921}
4922
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004923static void mem_cgroup_put(struct mem_cgroup *memcg)
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004924{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004925 __mem_cgroup_put(memcg, 1);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004926}
4927
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004928/*
4929 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
4930 */
Glauber Costae1aab162011-12-11 21:47:03 +00004931struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004932{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004933 if (!memcg->res.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004934 return NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004935 return mem_cgroup_from_res_counter(memcg->res.parent, res);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004936}
Glauber Costae1aab162011-12-11 21:47:03 +00004937EXPORT_SYMBOL(parent_mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004938
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004939#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4940static void __init enable_swap_cgroup(void)
4941{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004942 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004943 do_swap_account = 1;
4944}
4945#else
4946static void __init enable_swap_cgroup(void)
4947{
4948}
4949#endif
4950
Balbir Singhf64c3f52009-09-23 15:56:37 -07004951static int mem_cgroup_soft_limit_tree_init(void)
4952{
4953 struct mem_cgroup_tree_per_node *rtpn;
4954 struct mem_cgroup_tree_per_zone *rtpz;
4955 int tmp, node, zone;
4956
4957 for_each_node_state(node, N_POSSIBLE) {
4958 tmp = node;
4959 if (!node_state(node, N_NORMAL_MEMORY))
4960 tmp = -1;
4961 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
4962 if (!rtpn)
4963 return 1;
4964
4965 soft_limit_tree.rb_tree_per_node[node] = rtpn;
4966
4967 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4968 rtpz = &rtpn->rb_tree_per_zone[zone];
4969 rtpz->rb_root = RB_ROOT;
4970 spin_lock_init(&rtpz->lock);
4971 }
4972 }
4973 return 0;
4974}
4975
Li Zefan0eb253e2009-01-15 13:51:25 -08004976static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004977mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
4978{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004979 struct mem_cgroup *memcg, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004980 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004981 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004982
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004983 memcg = mem_cgroup_alloc();
4984 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004985 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004986
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004987 for_each_node_state(node, N_POSSIBLE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004988 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004989 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004990
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004991 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004992 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004993 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004994 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004995 parent = NULL;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004996 if (mem_cgroup_soft_limit_tree_init())
4997 goto free_out;
Hillf Dantona41c58a2011-12-19 17:11:57 -08004998 root_mem_cgroup = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004999 for_each_possible_cpu(cpu) {
5000 struct memcg_stock_pcp *stock =
5001 &per_cpu(memcg_stock, cpu);
5002 INIT_WORK(&stock->work, drain_local_stock);
5003 }
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07005004 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005005 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005006 parent = mem_cgroup_from_cont(cont->parent);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005007 memcg->use_hierarchy = parent->use_hierarchy;
5008 memcg->oom_kill_disable = parent->oom_kill_disable;
Balbir Singh18f59ea2009-01-07 18:08:07 -08005009 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005010
Balbir Singh18f59ea2009-01-07 18:08:07 -08005011 if (parent && parent->use_hierarchy) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005012 res_counter_init(&memcg->res, &parent->res);
5013 res_counter_init(&memcg->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005014 /*
5015 * We increment refcnt of the parent to ensure that we can
5016 * safely access it on res_counter_charge/uncharge.
5017 * This refcnt will be decremented when freeing this
5018 * mem_cgroup(see mem_cgroup_put).
5019 */
5020 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005021 } else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005022 res_counter_init(&memcg->res, NULL);
5023 res_counter_init(&memcg->memsw, NULL);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005024 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005025 memcg->last_scanned_node = MAX_NUMNODES;
5026 INIT_LIST_HEAD(&memcg->oom_notify);
Balbir Singh6d61ef42009-01-07 18:08:06 -08005027
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005028 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005029 memcg->swappiness = mem_cgroup_swappiness(parent);
5030 atomic_set(&memcg->refcnt, 1);
5031 memcg->move_charge_at_immigrate = 0;
5032 mutex_init(&memcg->thresholds_lock);
5033 return &memcg->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005034free_out:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005035 __mem_cgroup_free(memcg);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005036 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005037}
5038
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07005039static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005040 struct cgroup *cont)
5041{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005042 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07005043
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005044 return mem_cgroup_force_empty(memcg, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005045}
5046
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005047static void mem_cgroup_destroy(struct cgroup_subsys *ss,
5048 struct cgroup *cont)
5049{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005050 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08005051
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005052 kmem_cgroup_destroy(ss, cont);
5053
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005054 mem_cgroup_put(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005055}
5056
5057static int mem_cgroup_populate(struct cgroup_subsys *ss,
5058 struct cgroup *cont)
5059{
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08005060 int ret;
5061
5062 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
5063 ARRAY_SIZE(mem_cgroup_files));
5064
5065 if (!ret)
5066 ret = register_memsw_files(cont, ss);
Glauber Costae5671df2011-12-11 21:47:01 +00005067
5068 if (!ret)
5069 ret = register_kmem_files(cont, ss);
5070
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08005071 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005072}
5073
Daisuke Nishimura02491442010-03-10 15:22:17 -08005074#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005075/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005076#define PRECHARGE_COUNT_AT_ONCE 256
5077static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005078{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005079 int ret = 0;
5080 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005081 struct mem_cgroup *memcg = mc.to;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005082
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005083 if (mem_cgroup_is_root(memcg)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005084 mc.precharge += count;
5085 /* we don't need css_get for root */
5086 return ret;
5087 }
5088 /* try to charge at once */
5089 if (count > 1) {
5090 struct res_counter *dummy;
5091 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005092 * "memcg" cannot be under rmdir() because we've already checked
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005093 * by cgroup_lock_live_cgroup() that it is not removed and we
5094 * are still under the same cgroup_mutex. So we can postpone
5095 * css_get().
5096 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005097 if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy))
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005098 goto one_by_one;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005099 if (do_swap_account && res_counter_charge(&memcg->memsw,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005100 PAGE_SIZE * count, &dummy)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005101 res_counter_uncharge(&memcg->res, PAGE_SIZE * count);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005102 goto one_by_one;
5103 }
5104 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005105 return ret;
5106 }
5107one_by_one:
5108 /* fall back to one by one charge */
5109 while (count--) {
5110 if (signal_pending(current)) {
5111 ret = -EINTR;
5112 break;
5113 }
5114 if (!batch_count--) {
5115 batch_count = PRECHARGE_COUNT_AT_ONCE;
5116 cond_resched();
5117 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005118 ret = __mem_cgroup_try_charge(NULL,
5119 GFP_KERNEL, 1, &memcg, false);
5120 if (ret || !memcg)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005121 /* mem_cgroup_clear_mc() will do uncharge later */
5122 return -ENOMEM;
5123 mc.precharge++;
5124 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005125 return ret;
5126}
5127
5128/**
5129 * is_target_pte_for_mc - check a pte whether it is valid for move charge
5130 * @vma: the vma the pte to be checked belongs
5131 * @addr: the address corresponding to the pte to be checked
5132 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08005133 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005134 *
5135 * Returns
5136 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
5137 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
5138 * move charge. if @target is not NULL, the page is stored in target->page
5139 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08005140 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
5141 * target for charge migration. if @target is not NULL, the entry is stored
5142 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005143 *
5144 * Called with pte lock held.
5145 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005146union mc_target {
5147 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005148 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005149};
5150
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005151enum mc_target_type {
5152 MC_TARGET_NONE, /* not used */
5153 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08005154 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005155};
5156
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005157static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
5158 unsigned long addr, pte_t ptent)
5159{
5160 struct page *page = vm_normal_page(vma, addr, ptent);
5161
5162 if (!page || !page_mapped(page))
5163 return NULL;
5164 if (PageAnon(page)) {
5165 /* we don't move shared anon */
5166 if (!move_anon() || page_mapcount(page) > 2)
5167 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005168 } else if (!move_file())
5169 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005170 return NULL;
5171 if (!get_page_unless_zero(page))
5172 return NULL;
5173
5174 return page;
5175}
5176
5177static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5178 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5179{
5180 int usage_count;
5181 struct page *page = NULL;
5182 swp_entry_t ent = pte_to_swp_entry(ptent);
5183
5184 if (!move_anon() || non_swap_entry(ent))
5185 return NULL;
5186 usage_count = mem_cgroup_count_swap_user(ent, &page);
5187 if (usage_count > 1) { /* we don't move shared anon */
5188 if (page)
5189 put_page(page);
5190 return NULL;
5191 }
5192 if (do_swap_account)
5193 entry->val = ent.val;
5194
5195 return page;
5196}
5197
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005198static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
5199 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5200{
5201 struct page *page = NULL;
5202 struct inode *inode;
5203 struct address_space *mapping;
5204 pgoff_t pgoff;
5205
5206 if (!vma->vm_file) /* anonymous vma */
5207 return NULL;
5208 if (!move_file())
5209 return NULL;
5210
5211 inode = vma->vm_file->f_path.dentry->d_inode;
5212 mapping = vma->vm_file->f_mapping;
5213 if (pte_none(ptent))
5214 pgoff = linear_page_index(vma, addr);
5215 else /* pte_file(ptent) is true */
5216 pgoff = pte_to_pgoff(ptent);
5217
5218 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005219 page = find_get_page(mapping, pgoff);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005220
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005221#ifdef CONFIG_SWAP
5222 /* shmem/tmpfs may report page out on swap: account for that too. */
5223 if (radix_tree_exceptional_entry(page)) {
5224 swp_entry_t swap = radix_to_swp_entry(page);
5225 if (do_swap_account)
5226 *entry = swap;
5227 page = find_get_page(&swapper_space, swap.val);
5228 }
5229#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005230 return page;
5231}
5232
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005233static int is_target_pte_for_mc(struct vm_area_struct *vma,
5234 unsigned long addr, pte_t ptent, union mc_target *target)
5235{
Daisuke Nishimura02491442010-03-10 15:22:17 -08005236 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005237 struct page_cgroup *pc;
5238 int ret = 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005239 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005240
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005241 if (pte_present(ptent))
5242 page = mc_handle_present_pte(vma, addr, ptent);
5243 else if (is_swap_pte(ptent))
5244 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005245 else if (pte_none(ptent) || pte_file(ptent))
5246 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005247
5248 if (!page && !ent.val)
Daisuke Nishimura02491442010-03-10 15:22:17 -08005249 return 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005250 if (page) {
5251 pc = lookup_page_cgroup(page);
5252 /*
5253 * Do only loose check w/o page_cgroup lock.
5254 * mem_cgroup_move_account() checks the pc is valid or not under
5255 * the lock.
5256 */
5257 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
5258 ret = MC_TARGET_PAGE;
5259 if (target)
5260 target->page = page;
5261 }
5262 if (!ret || !target)
5263 put_page(page);
5264 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005265 /* There is a swap entry and a page doesn't exist or isn't charged */
5266 if (ent.val && !ret &&
Bob Liu9fb4b7c2012-01-12 17:18:48 -08005267 css_id(&mc.from->css) == lookup_swap_cgroup_id(ent)) {
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005268 ret = MC_TARGET_SWAP;
5269 if (target)
5270 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005271 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005272 return ret;
5273}
5274
5275static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
5276 unsigned long addr, unsigned long end,
5277 struct mm_walk *walk)
5278{
5279 struct vm_area_struct *vma = walk->private;
5280 pte_t *pte;
5281 spinlock_t *ptl;
5282
Dave Hansen03319322011-03-22 16:32:56 -07005283 split_huge_page_pmd(walk->mm, pmd);
5284
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005285 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5286 for (; addr != end; pte++, addr += PAGE_SIZE)
5287 if (is_target_pte_for_mc(vma, addr, *pte, NULL))
5288 mc.precharge++; /* increment precharge temporarily */
5289 pte_unmap_unlock(pte - 1, ptl);
5290 cond_resched();
5291
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005292 return 0;
5293}
5294
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005295static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
5296{
5297 unsigned long precharge;
5298 struct vm_area_struct *vma;
5299
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005300 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005301 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5302 struct mm_walk mem_cgroup_count_precharge_walk = {
5303 .pmd_entry = mem_cgroup_count_precharge_pte_range,
5304 .mm = mm,
5305 .private = vma,
5306 };
5307 if (is_vm_hugetlb_page(vma))
5308 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005309 walk_page_range(vma->vm_start, vma->vm_end,
5310 &mem_cgroup_count_precharge_walk);
5311 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005312 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005313
5314 precharge = mc.precharge;
5315 mc.precharge = 0;
5316
5317 return precharge;
5318}
5319
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005320static int mem_cgroup_precharge_mc(struct mm_struct *mm)
5321{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005322 unsigned long precharge = mem_cgroup_count_precharge(mm);
5323
5324 VM_BUG_ON(mc.moving_task);
5325 mc.moving_task = current;
5326 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005327}
5328
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005329/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
5330static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005331{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005332 struct mem_cgroup *from = mc.from;
5333 struct mem_cgroup *to = mc.to;
5334
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005335 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005336 if (mc.precharge) {
5337 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
5338 mc.precharge = 0;
5339 }
5340 /*
5341 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
5342 * we must uncharge here.
5343 */
5344 if (mc.moved_charge) {
5345 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
5346 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005347 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005348 /* we must fixup refcnts and charges */
5349 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005350 /* uncharge swap account from the old cgroup */
5351 if (!mem_cgroup_is_root(mc.from))
5352 res_counter_uncharge(&mc.from->memsw,
5353 PAGE_SIZE * mc.moved_swap);
5354 __mem_cgroup_put(mc.from, mc.moved_swap);
5355
5356 if (!mem_cgroup_is_root(mc.to)) {
5357 /*
5358 * we charged both to->res and to->memsw, so we should
5359 * uncharge to->res.
5360 */
5361 res_counter_uncharge(&mc.to->res,
5362 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005363 }
5364 /* we've already done mem_cgroup_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005365 mc.moved_swap = 0;
5366 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005367 memcg_oom_recover(from);
5368 memcg_oom_recover(to);
5369 wake_up_all(&mc.waitq);
5370}
5371
5372static void mem_cgroup_clear_mc(void)
5373{
5374 struct mem_cgroup *from = mc.from;
5375
5376 /*
5377 * we must clear moving_task before waking up waiters at the end of
5378 * task migration.
5379 */
5380 mc.moving_task = NULL;
5381 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005382 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005383 mc.from = NULL;
5384 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005385 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005386 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005387}
5388
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005389static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
5390 struct cgroup *cgroup,
Tejun Heo2f7ee562011-12-12 18:12:21 -08005391 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005392{
Tejun Heo2f7ee562011-12-12 18:12:21 -08005393 struct task_struct *p = cgroup_taskset_first(tset);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005394 int ret = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005395 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005396
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005397 if (memcg->move_charge_at_immigrate) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005398 struct mm_struct *mm;
5399 struct mem_cgroup *from = mem_cgroup_from_task(p);
5400
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005401 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005402
5403 mm = get_task_mm(p);
5404 if (!mm)
5405 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005406 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005407 if (mm->owner == p) {
5408 VM_BUG_ON(mc.from);
5409 VM_BUG_ON(mc.to);
5410 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005411 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005412 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005413 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005414 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005415 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005416 mc.to = memcg;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005417 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005418 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005419
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005420 ret = mem_cgroup_precharge_mc(mm);
5421 if (ret)
5422 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005423 }
5424 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005425 }
5426 return ret;
5427}
5428
5429static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
5430 struct cgroup *cgroup,
Tejun Heo2f7ee562011-12-12 18:12:21 -08005431 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005432{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005433 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005434}
5435
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005436static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
5437 unsigned long addr, unsigned long end,
5438 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005439{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005440 int ret = 0;
5441 struct vm_area_struct *vma = walk->private;
5442 pte_t *pte;
5443 spinlock_t *ptl;
5444
Dave Hansen03319322011-03-22 16:32:56 -07005445 split_huge_page_pmd(walk->mm, pmd);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005446retry:
5447 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5448 for (; addr != end; addr += PAGE_SIZE) {
5449 pte_t ptent = *(pte++);
5450 union mc_target target;
5451 int type;
5452 struct page *page;
5453 struct page_cgroup *pc;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005454 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005455
5456 if (!mc.precharge)
5457 break;
5458
5459 type = is_target_pte_for_mc(vma, addr, ptent, &target);
5460 switch (type) {
5461 case MC_TARGET_PAGE:
5462 page = target.page;
5463 if (isolate_lru_page(page))
5464 goto put;
5465 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07005466 if (!mem_cgroup_move_account(page, 1, pc,
5467 mc.from, mc.to, false)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005468 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005469 /* we uncharge from mc.from later. */
5470 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005471 }
5472 putback_lru_page(page);
5473put: /* is_target_pte_for_mc() gets the page */
5474 put_page(page);
5475 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005476 case MC_TARGET_SWAP:
5477 ent = target.ent;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005478 if (!mem_cgroup_move_swap_account(ent,
5479 mc.from, mc.to, false)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08005480 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005481 /* we fixup refcnts and charges later. */
5482 mc.moved_swap++;
5483 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08005484 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005485 default:
5486 break;
5487 }
5488 }
5489 pte_unmap_unlock(pte - 1, ptl);
5490 cond_resched();
5491
5492 if (addr != end) {
5493 /*
5494 * We have consumed all precharges we got in can_attach().
5495 * We try charge one by one, but don't do any additional
5496 * charges to mc.to if we have failed in charge once in attach()
5497 * phase.
5498 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005499 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005500 if (!ret)
5501 goto retry;
5502 }
5503
5504 return ret;
5505}
5506
5507static void mem_cgroup_move_charge(struct mm_struct *mm)
5508{
5509 struct vm_area_struct *vma;
5510
5511 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005512retry:
5513 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
5514 /*
5515 * Someone who are holding the mmap_sem might be waiting in
5516 * waitq. So we cancel all extra charges, wake up all waiters,
5517 * and retry. Because we cancel precharges, we might not be able
5518 * to move enough charges, but moving charge is a best-effort
5519 * feature anyway, so it wouldn't be a big problem.
5520 */
5521 __mem_cgroup_clear_mc();
5522 cond_resched();
5523 goto retry;
5524 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005525 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5526 int ret;
5527 struct mm_walk mem_cgroup_move_charge_walk = {
5528 .pmd_entry = mem_cgroup_move_charge_pte_range,
5529 .mm = mm,
5530 .private = vma,
5531 };
5532 if (is_vm_hugetlb_page(vma))
5533 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005534 ret = walk_page_range(vma->vm_start, vma->vm_end,
5535 &mem_cgroup_move_charge_walk);
5536 if (ret)
5537 /*
5538 * means we have consumed all precharges and failed in
5539 * doing additional charge. Just abandon here.
5540 */
5541 break;
5542 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005543 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005544}
5545
Balbir Singh67e465a2008-02-07 00:13:54 -08005546static void mem_cgroup_move_task(struct cgroup_subsys *ss,
5547 struct cgroup *cont,
Tejun Heo2f7ee562011-12-12 18:12:21 -08005548 struct cgroup_taskset *tset)
Balbir Singh67e465a2008-02-07 00:13:54 -08005549{
Tejun Heo2f7ee562011-12-12 18:12:21 -08005550 struct task_struct *p = cgroup_taskset_first(tset);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005551 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005552
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005553 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005554 if (mc.to)
5555 mem_cgroup_move_charge(mm);
5556 put_swap_token(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005557 mmput(mm);
5558 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005559 if (mc.to)
5560 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08005561}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005562#else /* !CONFIG_MMU */
5563static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
5564 struct cgroup *cgroup,
Tejun Heo2f7ee562011-12-12 18:12:21 -08005565 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005566{
5567 return 0;
5568}
5569static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
5570 struct cgroup *cgroup,
Tejun Heo2f7ee562011-12-12 18:12:21 -08005571 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005572{
5573}
5574static void mem_cgroup_move_task(struct cgroup_subsys *ss,
5575 struct cgroup *cont,
Tejun Heo2f7ee562011-12-12 18:12:21 -08005576 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005577{
5578}
5579#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08005580
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005581struct cgroup_subsys mem_cgroup_subsys = {
5582 .name = "memory",
5583 .subsys_id = mem_cgroup_subsys_id,
5584 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005585 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005586 .destroy = mem_cgroup_destroy,
5587 .populate = mem_cgroup_populate,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005588 .can_attach = mem_cgroup_can_attach,
5589 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08005590 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005591 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005592 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005593};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005594
5595#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08005596static int __init enable_swap_account(char *s)
5597{
5598 /* consider enabled if no parameter or 1 is given */
Michal Hockoa2c89902011-05-24 17:12:50 -07005599 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08005600 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07005601 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08005602 really_do_swap_account = 0;
5603 return 1;
5604}
Michal Hockoa2c89902011-05-24 17:12:50 -07005605__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005606
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005607#endif