blob: 7af1d5ee1598d485ced61f57c1b165fac4e0808d [file] [log] [blame]
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001/* memcontrol.c - Memory Controller
2 *
3 * Copyright IBM Corporation, 2007
4 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
5 *
Pavel Emelianov78fb7462008-02-07 00:13:51 -08006 * Copyright 2007 OpenVZ SWsoft Inc
7 * Author: Pavel Emelianov <xemul@openvz.org>
8 *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08009 * Memory thresholds
10 * Copyright (C) 2009 Nokia Corporation
11 * Author: Kirill A. Shutemov
12 *
Balbir Singh8cdea7c2008-02-07 00:13:50 -080013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 */
23
24#include <linux/res_counter.h>
25#include <linux/memcontrol.h>
26#include <linux/cgroup.h>
Pavel Emelianov78fb7462008-02-07 00:13:51 -080027#include <linux/mm.h>
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -080028#include <linux/hugetlb.h>
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -080029#include <linux/pagemap.h>
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080030#include <linux/smp.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080031#include <linux/page-flags.h>
Balbir Singh66e17072008-02-07 00:13:56 -080032#include <linux/backing-dev.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080033#include <linux/bit_spinlock.h>
34#include <linux/rcupdate.h>
Balbir Singhe2224322009-04-02 16:57:39 -070035#include <linux/limits.h>
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -080036#include <linux/mutex.h>
Balbir Singhf64c3f52009-09-23 15:56:37 -070037#include <linux/rbtree.h>
Balbir Singhb6ac57d2008-04-29 01:00:19 -070038#include <linux/slab.h>
Balbir Singh66e17072008-02-07 00:13:56 -080039#include <linux/swap.h>
Daisuke Nishimura02491442010-03-10 15:22:17 -080040#include <linux/swapops.h>
Balbir Singh66e17072008-02-07 00:13:56 -080041#include <linux/spinlock.h>
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -080042#include <linux/eventfd.h>
43#include <linux/sort.h>
Balbir Singh66e17072008-02-07 00:13:56 -080044#include <linux/fs.h>
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -080045#include <linux/seq_file.h>
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -070046#include <linux/vmalloc.h>
Christoph Lameterb69408e2008-10-18 20:26:14 -070047#include <linux/mm_inline.h>
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070048#include <linux/page_cgroup.h>
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -080049#include <linux/cpu.h>
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -070050#include <linux/oom.h>
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -080051#include "internal.h"
Balbir Singh8cdea7c2008-02-07 00:13:50 -080052
Balbir Singh8697d332008-02-07 00:13:59 -080053#include <asm/uaccess.h>
54
KOSAKI Motohirocc8e9702010-08-09 17:19:57 -070055#include <trace/events/vmscan.h>
56
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070057struct cgroup_subsys mem_cgroup_subsys __read_mostly;
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070058#define MEM_CGROUP_RECLAIM_RETRIES 5
Balbir Singh4b3bde42009-09-23 15:56:32 -070059struct mem_cgroup *root_mem_cgroup __read_mostly;
Balbir Singh8cdea7c2008-02-07 00:13:50 -080060
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080061#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
Li Zefan338c8432009-06-17 16:27:15 -070062/* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080063int do_swap_account __read_mostly;
Michal Hockoa42c3902010-11-24 12:57:08 -080064
65/* for remember boot option*/
66#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP_ENABLED
67static int really_do_swap_account __initdata = 1;
68#else
69static int really_do_swap_account __initdata = 0;
70#endif
71
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080072#else
73#define do_swap_account (0)
74#endif
75
76
Balbir Singh8cdea7c2008-02-07 00:13:50 -080077/*
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080078 * Statistics for memory cgroup.
79 */
80enum mem_cgroup_stat_index {
81 /*
82 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
83 */
84 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
Balbir Singhd69b0422009-06-17 16:26:34 -070085 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -080086 MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */
Balbir Singh0c3e73e2009-09-23 15:56:42 -070087 MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -070088 MEM_CGROUP_STAT_DATA, /* end of data requires synchronization */
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -070089 MEM_CGROUP_ON_MOVE, /* someone is moving account between groups */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080090 MEM_CGROUP_STAT_NSTATS,
91};
92
Johannes Weinere9f89742011-03-23 16:42:37 -070093enum mem_cgroup_events_index {
94 MEM_CGROUP_EVENTS_PGPGIN, /* # of pages paged in */
95 MEM_CGROUP_EVENTS_PGPGOUT, /* # of pages paged out */
96 MEM_CGROUP_EVENTS_COUNT, /* # of pages paged in/out */
Ying Han456f9982011-05-26 16:25:38 -070097 MEM_CGROUP_EVENTS_PGFAULT, /* # of page-faults */
98 MEM_CGROUP_EVENTS_PGMAJFAULT, /* # of major page-faults */
Johannes Weinere9f89742011-03-23 16:42:37 -070099 MEM_CGROUP_EVENTS_NSTATS,
100};
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700101/*
102 * Per memcg event counter is incremented at every pagein/pageout. With THP,
103 * it will be incremated by the number of pages. This counter is used for
104 * for trigger some periodic events. This is straightforward and better
105 * than using jiffies etc. to handle periodic memcg event.
106 */
107enum mem_cgroup_events_target {
108 MEM_CGROUP_TARGET_THRESH,
109 MEM_CGROUP_TARGET_SOFTLIMIT,
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700110 MEM_CGROUP_TARGET_NUMAINFO,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700111 MEM_CGROUP_NTARGETS,
112};
113#define THRESHOLDS_EVENTS_TARGET (128)
114#define SOFTLIMIT_EVENTS_TARGET (1024)
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700115#define NUMAINFO_EVENTS_TARGET (1024)
Johannes Weinere9f89742011-03-23 16:42:37 -0700116
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800117struct mem_cgroup_stat_cpu {
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700118 long count[MEM_CGROUP_STAT_NSTATS];
Johannes Weinere9f89742011-03-23 16:42:37 -0700119 unsigned long events[MEM_CGROUP_EVENTS_NSTATS];
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700120 unsigned long targets[MEM_CGROUP_NTARGETS];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800121};
122
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800123/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800124 * per-zone information in memory controller.
125 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800126struct mem_cgroup_per_zone {
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800127 /*
128 * spin_lock to protect the per cgroup LRU
129 */
Christoph Lameterb69408e2008-10-18 20:26:14 -0700130 struct list_head lists[NR_LRU_LISTS];
131 unsigned long count[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800132
133 struct zone_reclaim_stat reclaim_stat;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700134 struct rb_node tree_node; /* RB tree node */
135 unsigned long long usage_in_excess;/* Set to the value by which */
136 /* the soft limit is exceeded*/
137 bool on_tree;
Balbir Singh4e416952009-09-23 15:56:39 -0700138 struct mem_cgroup *mem; /* Back pointer, we cannot */
139 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800140};
141/* Macro for accessing counter */
142#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
143
144struct mem_cgroup_per_node {
145 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
146};
147
148struct mem_cgroup_lru_info {
149 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
150};
151
152/*
Balbir Singhf64c3f52009-09-23 15:56:37 -0700153 * Cgroups above their limits are maintained in a RB-Tree, independent of
154 * their hierarchy representation
155 */
156
157struct mem_cgroup_tree_per_zone {
158 struct rb_root rb_root;
159 spinlock_t lock;
160};
161
162struct mem_cgroup_tree_per_node {
163 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
164};
165
166struct mem_cgroup_tree {
167 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
168};
169
170static struct mem_cgroup_tree soft_limit_tree __read_mostly;
171
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800172struct mem_cgroup_threshold {
173 struct eventfd_ctx *eventfd;
174 u64 threshold;
175};
176
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700177/* For threshold */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800178struct mem_cgroup_threshold_ary {
179 /* An array index points to threshold just below usage. */
Phil Carmody5407a562010-05-26 14:42:42 -0700180 int current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800181 /* Size of entries[] */
182 unsigned int size;
183 /* Array of thresholds */
184 struct mem_cgroup_threshold entries[0];
185};
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700186
187struct mem_cgroup_thresholds {
188 /* Primary thresholds array */
189 struct mem_cgroup_threshold_ary *primary;
190 /*
191 * Spare threshold array.
192 * This is needed to make mem_cgroup_unregister_event() "never fail".
193 * It must be able to store at least primary->size - 1 entries.
194 */
195 struct mem_cgroup_threshold_ary *spare;
196};
197
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700198/* for OOM */
199struct mem_cgroup_eventfd_list {
200 struct list_head list;
201 struct eventfd_ctx *eventfd;
202};
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800203
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700204static void mem_cgroup_threshold(struct mem_cgroup *memcg);
205static void mem_cgroup_oom_notify(struct mem_cgroup *memcg);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800206
Balbir Singhf64c3f52009-09-23 15:56:37 -0700207/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800208 * The memory controller data structure. The memory controller controls both
209 * page cache and RSS per cgroup. We would eventually like to provide
210 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
211 * to help the administrator determine what knobs to tune.
212 *
213 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800214 * we hit the water mark. May be even add a low water mark, such that
215 * no reclaim occurs from a cgroup at it's low water mark, this is
216 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800217 */
218struct mem_cgroup {
219 struct cgroup_subsys_state css;
220 /*
221 * the counter to account for memory usage
222 */
223 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800224 /*
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800225 * the counter to account for mem+swap usage.
226 */
227 struct res_counter memsw;
228 /*
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800229 * Per cgroup active and inactive list, similar to the
230 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800231 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800232 struct mem_cgroup_lru_info info;
Balbir Singh6d61ef42009-01-07 18:08:06 -0800233 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200234 * While reclaiming in a hierarchy, we cache the last child we
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700235 * reclaimed from.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800236 */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700237 int last_scanned_child;
Ying Han889976d2011-05-26 16:25:33 -0700238 int last_scanned_node;
239#if MAX_NUMNODES > 1
240 nodemask_t scan_nodes;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700241 atomic_t numainfo_events;
242 atomic_t numainfo_updating;
Ying Han889976d2011-05-26 16:25:33 -0700243#endif
Balbir Singh18f59ea2009-01-07 18:08:07 -0800244 /*
245 * Should the accounting and control be hierarchical, per subtree?
246 */
247 bool use_hierarchy;
Michal Hocko79dfdac2011-07-26 16:08:23 -0700248
249 bool oom_lock;
250 atomic_t under_oom;
251
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800252 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800253
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -0700254 int swappiness;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -0700255 /* OOM-Killer disable */
256 int oom_kill_disable;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800257
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700258 /* set when res.limit == memsw.limit */
259 bool memsw_is_minimum;
260
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800261 /* protect arrays of thresholds */
262 struct mutex thresholds_lock;
263
264 /* thresholds for memory usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700265 struct mem_cgroup_thresholds thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700266
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800267 /* thresholds for mem+swap usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700268 struct mem_cgroup_thresholds memsw_thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700269
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700270 /* For oom notifier event fd */
271 struct list_head oom_notify;
Johannes Weiner185efc02011-09-14 16:21:58 -0700272
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800273 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800274 * Should we move charges of a task when a task is moved into this
275 * mem_cgroup ? And what type of charges should we move ?
276 */
277 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800278 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800279 * percpu counter.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800280 */
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800281 struct mem_cgroup_stat_cpu *stat;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700282 /*
283 * used when a cpu is offlined or other synchronizations
284 * See mem_cgroup_read_stat().
285 */
286 struct mem_cgroup_stat_cpu nocpu_base;
287 spinlock_t pcp_counter_lock;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800288};
289
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800290/* Stuffs for move charges at task migration. */
291/*
292 * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a
293 * left-shifted bitmap of these types.
294 */
295enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800296 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700297 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800298 NR_MOVE_TYPE,
299};
300
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800301/* "mc" and its members are protected by cgroup_mutex */
302static struct move_charge_struct {
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -0800303 spinlock_t lock; /* for from, to */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800304 struct mem_cgroup *from;
305 struct mem_cgroup *to;
306 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800307 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800308 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800309 struct task_struct *moving_task; /* a task moving charges */
310 wait_queue_head_t waitq; /* a waitq for other context */
311} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700312 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800313 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
314};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800315
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700316static bool move_anon(void)
317{
318 return test_bit(MOVE_CHARGE_TYPE_ANON,
319 &mc.to->move_charge_at_immigrate);
320}
321
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700322static bool move_file(void)
323{
324 return test_bit(MOVE_CHARGE_TYPE_FILE,
325 &mc.to->move_charge_at_immigrate);
326}
327
Balbir Singh4e416952009-09-23 15:56:39 -0700328/*
329 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
330 * limit reclaim to prevent infinite loops, if they ever occur.
331 */
332#define MEM_CGROUP_MAX_RECLAIM_LOOPS (100)
333#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2)
334
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800335enum charge_type {
336 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
337 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700338 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700339 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800340 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700341 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700342 NR_CHARGE_TYPE,
343};
344
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800345/* for encoding cft->private value on file */
346#define _MEM (0)
347#define _MEMSWAP (1)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700348#define _OOM_TYPE (2)
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800349#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
350#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
351#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700352/* Used for OOM nofiier */
353#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800354
Balbir Singh75822b42009-09-23 15:56:38 -0700355/*
356 * Reclaim flags for mem_cgroup_hierarchical_reclaim
357 */
358#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
359#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
360#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
361#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
Balbir Singh4e416952009-09-23 15:56:39 -0700362#define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
363#define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
Balbir Singh75822b42009-09-23 15:56:38 -0700364
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700365static void mem_cgroup_get(struct mem_cgroup *memcg);
366static void mem_cgroup_put(struct mem_cgroup *memcg);
367static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg);
368static void drain_all_stock_async(struct mem_cgroup *memcg);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800369
Balbir Singhf64c3f52009-09-23 15:56:37 -0700370static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700371mem_cgroup_zoneinfo(struct mem_cgroup *memcg, int nid, int zid)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700372{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700373 return &memcg->info.nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700374}
375
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700376struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100377{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700378 return &memcg->css;
Wu Fengguangd3242362009-12-16 12:19:59 +0100379}
380
Balbir Singhf64c3f52009-09-23 15:56:37 -0700381static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700382page_cgroup_zoneinfo(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700383{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700384 int nid = page_to_nid(page);
385 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700386
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700387 return mem_cgroup_zoneinfo(memcg, nid, zid);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700388}
389
390static struct mem_cgroup_tree_per_zone *
391soft_limit_tree_node_zone(int nid, int zid)
392{
393 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
394}
395
396static struct mem_cgroup_tree_per_zone *
397soft_limit_tree_from_page(struct page *page)
398{
399 int nid = page_to_nid(page);
400 int zid = page_zonenum(page);
401
402 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
403}
404
405static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700406__mem_cgroup_insert_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700407 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700408 struct mem_cgroup_tree_per_zone *mctz,
409 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700410{
411 struct rb_node **p = &mctz->rb_root.rb_node;
412 struct rb_node *parent = NULL;
413 struct mem_cgroup_per_zone *mz_node;
414
415 if (mz->on_tree)
416 return;
417
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700418 mz->usage_in_excess = new_usage_in_excess;
419 if (!mz->usage_in_excess)
420 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700421 while (*p) {
422 parent = *p;
423 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
424 tree_node);
425 if (mz->usage_in_excess < mz_node->usage_in_excess)
426 p = &(*p)->rb_left;
427 /*
428 * We can't avoid mem cgroups that are over their soft
429 * limit by the same amount
430 */
431 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
432 p = &(*p)->rb_right;
433 }
434 rb_link_node(&mz->tree_node, parent, p);
435 rb_insert_color(&mz->tree_node, &mctz->rb_root);
436 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700437}
438
439static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700440__mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singh4e416952009-09-23 15:56:39 -0700441 struct mem_cgroup_per_zone *mz,
442 struct mem_cgroup_tree_per_zone *mctz)
443{
444 if (!mz->on_tree)
445 return;
446 rb_erase(&mz->tree_node, &mctz->rb_root);
447 mz->on_tree = false;
448}
449
450static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700451mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700452 struct mem_cgroup_per_zone *mz,
453 struct mem_cgroup_tree_per_zone *mctz)
454{
455 spin_lock(&mctz->lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700456 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700457 spin_unlock(&mctz->lock);
458}
459
Balbir Singhf64c3f52009-09-23 15:56:37 -0700460
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700461static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700462{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700463 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700464 struct mem_cgroup_per_zone *mz;
465 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700466 int nid = page_to_nid(page);
467 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700468 mctz = soft_limit_tree_from_page(page);
469
470 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700471 * Necessary to update all ancestors when hierarchy is used.
472 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700473 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700474 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
475 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
476 excess = res_counter_soft_limit_excess(&memcg->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700477 /*
478 * We have to update the tree if mz is on RB-tree or
479 * mem is over its softlimit.
480 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700481 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700482 spin_lock(&mctz->lock);
483 /* if on-tree, remove it */
484 if (mz->on_tree)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700485 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700486 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700487 * Insert again. mz->usage_in_excess will be updated.
488 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700489 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700490 __mem_cgroup_insert_exceeded(memcg, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700491 spin_unlock(&mctz->lock);
492 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700493 }
494}
495
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700496static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700497{
498 int node, zone;
499 struct mem_cgroup_per_zone *mz;
500 struct mem_cgroup_tree_per_zone *mctz;
501
502 for_each_node_state(node, N_POSSIBLE) {
503 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700504 mz = mem_cgroup_zoneinfo(memcg, node, zone);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700505 mctz = soft_limit_tree_node_zone(node, zone);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700506 mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700507 }
508 }
509}
510
Balbir Singh4e416952009-09-23 15:56:39 -0700511static struct mem_cgroup_per_zone *
512__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
513{
514 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700515 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700516
517retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700518 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700519 rightmost = rb_last(&mctz->rb_root);
520 if (!rightmost)
521 goto done; /* Nothing to reclaim from */
522
523 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
524 /*
525 * Remove the node now but someone else can add it back,
526 * we will to add it back at the end of reclaim to its correct
527 * position in the tree.
528 */
529 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
530 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
531 !css_tryget(&mz->mem->css))
532 goto retry;
533done:
534 return mz;
535}
536
537static struct mem_cgroup_per_zone *
538mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
539{
540 struct mem_cgroup_per_zone *mz;
541
542 spin_lock(&mctz->lock);
543 mz = __mem_cgroup_largest_soft_limit_node(mctz);
544 spin_unlock(&mctz->lock);
545 return mz;
546}
547
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700548/*
549 * Implementation Note: reading percpu statistics for memcg.
550 *
551 * Both of vmstat[] and percpu_counter has threshold and do periodic
552 * synchronization to implement "quick" read. There are trade-off between
553 * reading cost and precision of value. Then, we may have a chance to implement
554 * a periodic synchronizion of counter in memcg's counter.
555 *
556 * But this _read() function is used for user interface now. The user accounts
557 * memory usage by memory cgroup and he _always_ requires exact value because
558 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
559 * have to visit all online cpus and make sum. So, for now, unnecessary
560 * synchronization is not implemented. (just implemented for cpu hotplug)
561 *
562 * If there are kernel internal actions which can make use of some not-exact
563 * value, and reading all cpu value can be performance bottleneck in some
564 * common workload, threashold and synchonization as vmstat[] should be
565 * implemented.
566 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700567static long mem_cgroup_read_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700568 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800569{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700570 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800571 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800572
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700573 get_online_cpus();
574 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700575 val += per_cpu(memcg->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700576#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700577 spin_lock(&memcg->pcp_counter_lock);
578 val += memcg->nocpu_base.count[idx];
579 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700580#endif
581 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800582 return val;
583}
584
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700585static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700586 bool charge)
587{
588 int val = (charge) ? 1 : -1;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700589 this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAPOUT], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700590}
591
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700592void mem_cgroup_pgfault(struct mem_cgroup *memcg, int val)
Ying Han456f9982011-05-26 16:25:38 -0700593{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700594 this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT], val);
Ying Han456f9982011-05-26 16:25:38 -0700595}
596
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700597void mem_cgroup_pgmajfault(struct mem_cgroup *memcg, int val)
Ying Han456f9982011-05-26 16:25:38 -0700598{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700599 this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT], val);
Ying Han456f9982011-05-26 16:25:38 -0700600}
601
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700602static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg,
Johannes Weinere9f89742011-03-23 16:42:37 -0700603 enum mem_cgroup_events_index idx)
604{
605 unsigned long val = 0;
606 int cpu;
607
608 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700609 val += per_cpu(memcg->stat->events[idx], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -0700610#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700611 spin_lock(&memcg->pcp_counter_lock);
612 val += memcg->nocpu_base.events[idx];
613 spin_unlock(&memcg->pcp_counter_lock);
Johannes Weinere9f89742011-03-23 16:42:37 -0700614#endif
615 return val;
616}
617
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700618static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800619 bool file, int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800620{
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800621 preempt_disable();
622
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800623 if (file)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700624 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE],
625 nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800626 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700627 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS],
628 nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700629
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800630 /* pagein of a big page is an event. So, ignore page size */
631 if (nr_pages > 0)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700632 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800633 else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700634 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800635 nr_pages = -nr_pages; /* for event */
636 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800637
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700638 __this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_COUNT], nr_pages);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800639
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800640 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800641}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800642
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700643unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700644mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg, int nid, int zid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700645 unsigned int lru_mask)
Ying Han889976d2011-05-26 16:25:33 -0700646{
647 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700648 enum lru_list l;
649 unsigned long ret = 0;
650
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700651 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700652
653 for_each_lru(l) {
654 if (BIT(l) & lru_mask)
655 ret += MEM_CGROUP_ZSTAT(mz, l);
656 }
657 return ret;
658}
659
660static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700661mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700662 int nid, unsigned int lru_mask)
663{
Ying Han889976d2011-05-26 16:25:33 -0700664 u64 total = 0;
665 int zid;
666
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700667 for (zid = 0; zid < MAX_NR_ZONES; zid++)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700668 total += mem_cgroup_zone_nr_lru_pages(memcg,
669 nid, zid, lru_mask);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700670
Ying Han889976d2011-05-26 16:25:33 -0700671 return total;
672}
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700673
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700674static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700675 unsigned int lru_mask)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800676{
Ying Han889976d2011-05-26 16:25:33 -0700677 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800678 u64 total = 0;
679
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700680 for_each_node_state(nid, N_HIGH_MEMORY)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700681 total += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800682 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800683}
684
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700685static bool __memcg_event_check(struct mem_cgroup *memcg, int target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800686{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700687 unsigned long val, next;
688
Steven Rostedt47994012011-11-02 13:38:33 -0700689 val = __this_cpu_read(memcg->stat->events[MEM_CGROUP_EVENTS_COUNT]);
690 next = __this_cpu_read(memcg->stat->targets[target]);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700691 /* from time_after() in jiffies.h */
692 return ((long)next - (long)val < 0);
693}
694
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700695static void __mem_cgroup_target_update(struct mem_cgroup *memcg, int target)
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700696{
697 unsigned long val, next;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800698
Steven Rostedt47994012011-11-02 13:38:33 -0700699 val = __this_cpu_read(memcg->stat->events[MEM_CGROUP_EVENTS_COUNT]);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800700
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700701 switch (target) {
702 case MEM_CGROUP_TARGET_THRESH:
703 next = val + THRESHOLDS_EVENTS_TARGET;
704 break;
705 case MEM_CGROUP_TARGET_SOFTLIMIT:
706 next = val + SOFTLIMIT_EVENTS_TARGET;
707 break;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700708 case MEM_CGROUP_TARGET_NUMAINFO:
709 next = val + NUMAINFO_EVENTS_TARGET;
710 break;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700711 default:
712 return;
713 }
714
Steven Rostedt47994012011-11-02 13:38:33 -0700715 __this_cpu_write(memcg->stat->targets[target], next);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800716}
717
718/*
719 * Check events in order.
720 *
721 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700722static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800723{
Steven Rostedt47994012011-11-02 13:38:33 -0700724 preempt_disable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800725 /* threshold event is triggered in finer grain than soft limit */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700726 if (unlikely(__memcg_event_check(memcg, MEM_CGROUP_TARGET_THRESH))) {
727 mem_cgroup_threshold(memcg);
728 __mem_cgroup_target_update(memcg, MEM_CGROUP_TARGET_THRESH);
729 if (unlikely(__memcg_event_check(memcg,
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700730 MEM_CGROUP_TARGET_SOFTLIMIT))) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700731 mem_cgroup_update_tree(memcg, page);
732 __mem_cgroup_target_update(memcg,
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700733 MEM_CGROUP_TARGET_SOFTLIMIT);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700734 }
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700735#if MAX_NUMNODES > 1
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700736 if (unlikely(__memcg_event_check(memcg,
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700737 MEM_CGROUP_TARGET_NUMAINFO))) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700738 atomic_inc(&memcg->numainfo_events);
739 __mem_cgroup_target_update(memcg,
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700740 MEM_CGROUP_TARGET_NUMAINFO);
741 }
742#endif
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800743 }
Steven Rostedt47994012011-11-02 13:38:33 -0700744 preempt_enable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800745}
746
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800747static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800748{
749 return container_of(cgroup_subsys_state(cont,
750 mem_cgroup_subsys_id), struct mem_cgroup,
751 css);
752}
753
Balbir Singhcf475ad2008-04-29 01:00:16 -0700754struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800755{
Balbir Singh31a78f22008-09-28 23:09:31 +0100756 /*
757 * mm_update_next_owner() may clear mm->owner to NULL
758 * if it races with swapoff, page migration, etc.
759 * So this can be called with p == NULL.
760 */
761 if (unlikely(!p))
762 return NULL;
763
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800764 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
765 struct mem_cgroup, css);
766}
767
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700768struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800769{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700770 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700771
772 if (!mm)
773 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800774 /*
775 * Because we have no locks, mm->owner's may be being moved to other
776 * cgroup. We use css_tryget() here even if this looks
777 * pessimistic (rather than adding locks here).
778 */
779 rcu_read_lock();
780 do {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700781 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
782 if (unlikely(!memcg))
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800783 break;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700784 } while (!css_tryget(&memcg->css));
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800785 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700786 return memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800787}
788
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700789/* The caller has to guarantee "mem" exists before calling this */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700790static struct mem_cgroup *mem_cgroup_start_loop(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700791{
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700792 struct cgroup_subsys_state *css;
793 int found;
794
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700795 if (!memcg) /* ROOT cgroup has the smallest ID */
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700796 return root_mem_cgroup; /*css_put/get against root is ignored*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700797 if (!memcg->use_hierarchy) {
798 if (css_tryget(&memcg->css))
799 return memcg;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700800 return NULL;
801 }
802 rcu_read_lock();
803 /*
804 * searching a memory cgroup which has the smallest ID under given
805 * ROOT cgroup. (ID >= 1)
806 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700807 css = css_get_next(&mem_cgroup_subsys, 1, &memcg->css, &found);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700808 if (css && css_tryget(css))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700809 memcg = container_of(css, struct mem_cgroup, css);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700810 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700811 memcg = NULL;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700812 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700813 return memcg;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700814}
815
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700816static struct mem_cgroup *mem_cgroup_get_next(struct mem_cgroup *iter,
817 struct mem_cgroup *root,
818 bool cond)
819{
820 int nextid = css_id(&iter->css) + 1;
821 int found;
822 int hierarchy_used;
823 struct cgroup_subsys_state *css;
824
825 hierarchy_used = iter->use_hierarchy;
826
827 css_put(&iter->css);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700828 /* If no ROOT, walk all, ignore hierarchy */
829 if (!cond || (root && !hierarchy_used))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700830 return NULL;
831
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700832 if (!root)
833 root = root_mem_cgroup;
834
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700835 do {
836 iter = NULL;
837 rcu_read_lock();
838
839 css = css_get_next(&mem_cgroup_subsys, nextid,
840 &root->css, &found);
841 if (css && css_tryget(css))
842 iter = container_of(css, struct mem_cgroup, css);
843 rcu_read_unlock();
844 /* If css is NULL, no more cgroups will be found */
845 nextid = found + 1;
846 } while (css && !iter);
847
848 return iter;
849}
850/*
851 * for_eacn_mem_cgroup_tree() for visiting all cgroup under tree. Please
852 * be careful that "break" loop is not allowed. We have reference count.
853 * Instead of that modify "cond" to be false and "continue" to exit the loop.
854 */
855#define for_each_mem_cgroup_tree_cond(iter, root, cond) \
856 for (iter = mem_cgroup_start_loop(root);\
857 iter != NULL;\
858 iter = mem_cgroup_get_next(iter, root, cond))
859
860#define for_each_mem_cgroup_tree(iter, root) \
861 for_each_mem_cgroup_tree_cond(iter, root, true)
862
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700863#define for_each_mem_cgroup_all(iter) \
864 for_each_mem_cgroup_tree_cond(iter, NULL, true)
865
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700866
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700867static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
Balbir Singh4b3bde42009-09-23 15:56:32 -0700868{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700869 return (memcg == root_mem_cgroup);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700870}
871
Ying Han456f9982011-05-26 16:25:38 -0700872void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
873{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700874 struct mem_cgroup *memcg;
Ying Han456f9982011-05-26 16:25:38 -0700875
876 if (!mm)
877 return;
878
879 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700880 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
881 if (unlikely(!memcg))
Ying Han456f9982011-05-26 16:25:38 -0700882 goto out;
883
884 switch (idx) {
885 case PGMAJFAULT:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700886 mem_cgroup_pgmajfault(memcg, 1);
Ying Han456f9982011-05-26 16:25:38 -0700887 break;
888 case PGFAULT:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700889 mem_cgroup_pgfault(memcg, 1);
Ying Han456f9982011-05-26 16:25:38 -0700890 break;
891 default:
892 BUG();
893 }
894out:
895 rcu_read_unlock();
896}
897EXPORT_SYMBOL(mem_cgroup_count_vm_event);
898
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800899/*
900 * Following LRU functions are allowed to be used without PCG_LOCK.
901 * Operations are called by routine of global LRU independently from memcg.
902 * What we have to take care of here is validness of pc->mem_cgroup.
903 *
904 * Changes to pc->mem_cgroup happens when
905 * 1. charge
906 * 2. moving account
907 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
908 * It is added to LRU before charge.
909 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
910 * When moving account, the page is not on LRU. It's isolated.
911 */
912
913void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800914{
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800915 struct page_cgroup *pc;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800916 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700917
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800918 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800919 return;
920 pc = lookup_page_cgroup(page);
921 /* can happen while we handle swapcache. */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700922 if (!TestClearPageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800923 return;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700924 VM_BUG_ON(!pc->mem_cgroup);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800925 /*
926 * We don't check PCG_USED bit. It's cleared when the "page" is finally
927 * removed from global LRU.
928 */
Johannes Weiner97a6c372011-03-23 16:42:27 -0700929 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -0800930 /* huge page split is done under lru_lock. so, we have no races. */
931 MEM_CGROUP_ZSTAT(mz, lru) -= 1 << compound_order(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700932 if (mem_cgroup_is_root(pc->mem_cgroup))
933 return;
934 VM_BUG_ON(list_empty(&pc->lru));
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800935 list_del_init(&pc->lru);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800936}
937
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800938void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800939{
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800940 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800941}
942
Minchan Kim3f58a822011-03-22 16:32:53 -0700943/*
944 * Writeback is about to end against a page which has been marked for immediate
945 * reclaim. If it still appears to be reclaimable, move it to the tail of the
946 * inactive list.
947 */
948void mem_cgroup_rotate_reclaimable_page(struct page *page)
949{
950 struct mem_cgroup_per_zone *mz;
951 struct page_cgroup *pc;
952 enum lru_list lru = page_lru(page);
953
954 if (mem_cgroup_disabled())
955 return;
956
957 pc = lookup_page_cgroup(page);
958 /* unused or root page is not rotated. */
959 if (!PageCgroupUsed(pc))
960 return;
961 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
962 smp_rmb();
963 if (mem_cgroup_is_root(pc->mem_cgroup))
964 return;
Johannes Weiner97a6c372011-03-23 16:42:27 -0700965 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
Minchan Kim3f58a822011-03-22 16:32:53 -0700966 list_move_tail(&pc->lru, &mz->lists[lru]);
967}
968
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800969void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800970{
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800971 struct mem_cgroup_per_zone *mz;
972 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800973
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800974 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700975 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700976
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800977 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700978 /* unused or root page is not rotated. */
Johannes Weiner713735b42011-01-20 14:44:31 -0800979 if (!PageCgroupUsed(pc))
980 return;
981 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
982 smp_rmb();
983 if (mem_cgroup_is_root(pc->mem_cgroup))
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800984 return;
Johannes Weiner97a6c372011-03-23 16:42:27 -0700985 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700986 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800987}
988
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800989void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
990{
991 struct page_cgroup *pc;
992 struct mem_cgroup_per_zone *mz;
993
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800994 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -0800995 return;
996 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700997 VM_BUG_ON(PageCgroupAcctLRU(pc));
Johannes Weinera61ed3c2011-11-02 13:38:29 -0700998 /*
999 * putback: charge:
1000 * SetPageLRU SetPageCgroupUsed
1001 * smp_mb smp_mb
1002 * PageCgroupUsed && add to memcg LRU PageLRU && add to memcg LRU
1003 *
1004 * Ensure that one of the two sides adds the page to the memcg
1005 * LRU during a race.
1006 */
1007 smp_mb();
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001008 if (!PageCgroupUsed(pc))
1009 return;
Johannes Weiner713735b42011-01-20 14:44:31 -08001010 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
1011 smp_rmb();
Johannes Weiner97a6c372011-03-23 16:42:27 -07001012 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08001013 /* huge page split is done under lru_lock. so, we have no races. */
1014 MEM_CGROUP_ZSTAT(mz, lru) += 1 << compound_order(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -07001015 SetPageCgroupAcctLRU(pc);
1016 if (mem_cgroup_is_root(pc->mem_cgroup))
1017 return;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001018 list_add(&pc->lru, &mz->lists[lru]);
1019}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001020
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001021/*
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001022 * At handling SwapCache and other FUSE stuff, pc->mem_cgroup may be changed
1023 * while it's linked to lru because the page may be reused after it's fully
1024 * uncharged. To handle that, unlink page_cgroup from LRU when charge it again.
1025 * It's done under lock_page and expected that zone->lru_lock isnever held.
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001026 */
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001027static void mem_cgroup_lru_del_before_commit(struct page *page)
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001028{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001029 unsigned long flags;
1030 struct zone *zone = page_zone(page);
1031 struct page_cgroup *pc = lookup_page_cgroup(page);
1032
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001033 /*
1034 * Doing this check without taking ->lru_lock seems wrong but this
1035 * is safe. Because if page_cgroup's USED bit is unset, the page
1036 * will not be added to any memcg's LRU. If page_cgroup's USED bit is
1037 * set, the commit after this will fail, anyway.
1038 * This all charge/uncharge is done under some mutual execustion.
1039 * So, we don't need to taking care of changes in USED bit.
1040 */
1041 if (likely(!PageLRU(page)))
1042 return;
1043
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001044 spin_lock_irqsave(&zone->lru_lock, flags);
1045 /*
1046 * Forget old LRU when this page_cgroup is *not* used. This Used bit
1047 * is guarded by lock_page() because the page is SwapCache.
1048 */
1049 if (!PageCgroupUsed(pc))
1050 mem_cgroup_del_lru_list(page, page_lru(page));
1051 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001052}
1053
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001054static void mem_cgroup_lru_add_after_commit(struct page *page)
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001055{
1056 unsigned long flags;
1057 struct zone *zone = page_zone(page);
1058 struct page_cgroup *pc = lookup_page_cgroup(page);
Johannes Weinera61ed3c2011-11-02 13:38:29 -07001059 /*
1060 * putback: charge:
1061 * SetPageLRU SetPageCgroupUsed
1062 * smp_mb smp_mb
1063 * PageCgroupUsed && add to memcg LRU PageLRU && add to memcg LRU
1064 *
1065 * Ensure that one of the two sides adds the page to the memcg
1066 * LRU during a race.
1067 */
1068 smp_mb();
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001069 /* taking care of that the page is added to LRU while we commit it */
1070 if (likely(!PageLRU(page)))
1071 return;
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001072 spin_lock_irqsave(&zone->lru_lock, flags);
1073 /* link when the page is linked to LRU but page_cgroup isn't */
Balbir Singh4b3bde42009-09-23 15:56:32 -07001074 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001075 mem_cgroup_add_lru_list(page, page_lru(page));
1076 spin_unlock_irqrestore(&zone->lru_lock, flags);
1077}
1078
1079
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001080void mem_cgroup_move_lists(struct page *page,
1081 enum lru_list from, enum lru_list to)
1082{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001083 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001084 return;
1085 mem_cgroup_del_lru_list(page, from);
1086 mem_cgroup_add_lru_list(page, to);
1087}
1088
Michal Hocko3e920412011-07-26 16:08:29 -07001089/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001090 * Checks whether given mem is same or in the root_mem_cgroup's
Michal Hocko3e920412011-07-26 16:08:29 -07001091 * hierarchy subtree
1092 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001093static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1094 struct mem_cgroup *memcg)
Michal Hocko3e920412011-07-26 16:08:29 -07001095{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001096 if (root_memcg != memcg) {
1097 return (root_memcg->use_hierarchy &&
1098 css_is_ancestor(&memcg->css, &root_memcg->css));
Michal Hocko3e920412011-07-26 16:08:29 -07001099 }
1100
1101 return true;
1102}
1103
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001104int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -08001105{
1106 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001107 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001108 struct task_struct *p;
David Rientjes4c4a2212008-02-07 00:14:06 -08001109
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001110 p = find_lock_task_mm(task);
1111 if (!p)
1112 return 0;
1113 curr = try_get_mem_cgroup_from_mm(p->mm);
1114 task_unlock(p);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001115 if (!curr)
1116 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001117 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001118 * We should check use_hierarchy of "memcg" not "curr". Because checking
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001119 * use_hierarchy of "curr" here make this function true if hierarchy is
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001120 * enabled in "curr" and "curr" is a child of "memcg" in *cgroup*
1121 * hierarchy(even if use_hierarchy is disabled in "memcg").
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001122 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001123 ret = mem_cgroup_same_or_subtree(memcg, curr);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001124 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001125 return ret;
1126}
1127
Johannes Weiner9b272972011-11-02 13:38:23 -07001128int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg, struct zone *zone)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001129{
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001130 unsigned long inactive_ratio;
Johannes Weiner9b272972011-11-02 13:38:23 -07001131 int nid = zone_to_nid(zone);
1132 int zid = zone_idx(zone);
1133 unsigned long inactive;
1134 unsigned long active;
1135 unsigned long gb;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001136
Johannes Weiner9b272972011-11-02 13:38:23 -07001137 inactive = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid,
1138 BIT(LRU_INACTIVE_ANON));
1139 active = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid,
1140 BIT(LRU_ACTIVE_ANON));
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001141
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001142 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1143 if (gb)
1144 inactive_ratio = int_sqrt(10 * gb);
1145 else
1146 inactive_ratio = 1;
1147
Johannes Weiner9b272972011-11-02 13:38:23 -07001148 return inactive * inactive_ratio < active;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001149}
1150
Johannes Weiner9b272972011-11-02 13:38:23 -07001151int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg, struct zone *zone)
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001152{
1153 unsigned long active;
1154 unsigned long inactive;
Johannes Weiner9b272972011-11-02 13:38:23 -07001155 int zid = zone_idx(zone);
1156 int nid = zone_to_nid(zone);
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001157
Johannes Weiner9b272972011-11-02 13:38:23 -07001158 inactive = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid,
1159 BIT(LRU_INACTIVE_FILE));
1160 active = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid,
1161 BIT(LRU_ACTIVE_FILE));
Rik van Riel56e49d22009-06-16 15:32:28 -07001162
1163 return (active > inactive);
1164}
1165
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001166struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
1167 struct zone *zone)
1168{
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -07001169 int nid = zone_to_nid(zone);
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001170 int zid = zone_idx(zone);
1171 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
1172
1173 return &mz->reclaim_stat;
1174}
1175
1176struct zone_reclaim_stat *
1177mem_cgroup_get_reclaim_stat_from_page(struct page *page)
1178{
1179 struct page_cgroup *pc;
1180 struct mem_cgroup_per_zone *mz;
1181
1182 if (mem_cgroup_disabled())
1183 return NULL;
1184
1185 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -08001186 if (!PageCgroupUsed(pc))
1187 return NULL;
Johannes Weiner713735b42011-01-20 14:44:31 -08001188 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
1189 smp_rmb();
Johannes Weiner97a6c372011-03-23 16:42:27 -07001190 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001191 return &mz->reclaim_stat;
1192}
1193
Balbir Singh66e17072008-02-07 00:13:56 -08001194unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
1195 struct list_head *dst,
1196 unsigned long *scanned, int order,
Minchan Kim4356f212011-10-31 17:06:47 -07001197 isolate_mode_t mode,
1198 struct zone *z,
Balbir Singh66e17072008-02-07 00:13:56 -08001199 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001200 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -08001201{
1202 unsigned long nr_taken = 0;
1203 struct page *page;
1204 unsigned long scan;
1205 LIST_HEAD(pc_list);
1206 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001207 struct page_cgroup *pc, *tmp;
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -07001208 int nid = zone_to_nid(z);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001209 int zid = zone_idx(z);
1210 struct mem_cgroup_per_zone *mz;
Johannes Weinerb7c46d12009-09-21 17:02:56 -07001211 int lru = LRU_FILE * file + active;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001212 int ret;
Balbir Singh66e17072008-02-07 00:13:56 -08001213
Balbir Singhcf475ad2008-04-29 01:00:16 -07001214 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001215 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001216 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -08001217
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001218 scan = 0;
1219 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -08001220 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001221 break;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08001222
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001223 if (unlikely(!PageCgroupUsed(pc)))
1224 continue;
Johannes Weiner5564e882011-03-23 16:42:29 -07001225
Johannes Weiner6b3ae582011-03-23 16:42:30 -07001226 page = lookup_cgroup_page(pc);
Johannes Weiner5564e882011-03-23 16:42:29 -07001227
Hugh Dickins436c65412008-02-07 00:14:12 -08001228 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001229 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001230
Hugh Dickins436c65412008-02-07 00:14:12 -08001231 scan++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001232 ret = __isolate_lru_page(page, mode, file);
1233 switch (ret) {
1234 case 0:
Balbir Singh66e17072008-02-07 00:13:56 -08001235 list_move(&page->lru, dst);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001236 mem_cgroup_del_lru(page);
Rik van Riel2c888cf2011-01-13 15:47:13 -08001237 nr_taken += hpage_nr_pages(page);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001238 break;
1239 case -EBUSY:
1240 /* we don't affect global LRU but rotate in our LRU */
1241 mem_cgroup_rotate_lru_list(page, page_lru(page));
1242 break;
1243 default:
1244 break;
Balbir Singh66e17072008-02-07 00:13:56 -08001245 }
1246 }
1247
Balbir Singh66e17072008-02-07 00:13:56 -08001248 *scanned = scan;
KOSAKI Motohirocc8e9702010-08-09 17:19:57 -07001249
1250 trace_mm_vmscan_memcg_isolate(0, nr_to_scan, scan, nr_taken,
1251 0, 0, 0, mode);
1252
Balbir Singh66e17072008-02-07 00:13:56 -08001253 return nr_taken;
1254}
1255
Balbir Singh6d61ef42009-01-07 18:08:06 -08001256#define mem_cgroup_from_res_counter(counter, member) \
1257 container_of(counter, struct mem_cgroup, member)
1258
Johannes Weiner19942822011-02-01 15:52:43 -08001259/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001260 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
1261 * @mem: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001262 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001263 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001264 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001265 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001266static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001267{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001268 unsigned long long margin;
1269
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001270 margin = res_counter_margin(&memcg->res);
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001271 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001272 margin = min(margin, res_counter_margin(&memcg->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001273 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001274}
1275
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001276int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001277{
1278 struct cgroup *cgrp = memcg->css.cgroup;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001279
1280 /* root ? */
1281 if (cgrp->parent == NULL)
1282 return vm_swappiness;
1283
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001284 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001285}
1286
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001287static void mem_cgroup_start_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001288{
1289 int cpu;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001290
1291 get_online_cpus();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001292 spin_lock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001293 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001294 per_cpu(memcg->stat->count[MEM_CGROUP_ON_MOVE], cpu) += 1;
1295 memcg->nocpu_base.count[MEM_CGROUP_ON_MOVE] += 1;
1296 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001297 put_online_cpus();
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001298
1299 synchronize_rcu();
1300}
1301
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001302static void mem_cgroup_end_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001303{
1304 int cpu;
1305
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001306 if (!memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001307 return;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001308 get_online_cpus();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001309 spin_lock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001310 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001311 per_cpu(memcg->stat->count[MEM_CGROUP_ON_MOVE], cpu) -= 1;
1312 memcg->nocpu_base.count[MEM_CGROUP_ON_MOVE] -= 1;
1313 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001314 put_online_cpus();
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001315}
1316/*
1317 * 2 routines for checking "mem" is under move_account() or not.
1318 *
1319 * mem_cgroup_stealed() - checking a cgroup is mc.from or not. This is used
1320 * for avoiding race in accounting. If true,
1321 * pc->mem_cgroup may be overwritten.
1322 *
1323 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1324 * under hierarchy of moving cgroups. This is for
1325 * waiting at hith-memory prressure caused by "move".
1326 */
1327
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001328static bool mem_cgroup_stealed(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001329{
1330 VM_BUG_ON(!rcu_read_lock_held());
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001331 return this_cpu_read(memcg->stat->count[MEM_CGROUP_ON_MOVE]) > 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001332}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001333
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001334static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001335{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001336 struct mem_cgroup *from;
1337 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001338 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001339 /*
1340 * Unlike task_move routines, we access mc.to, mc.from not under
1341 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1342 */
1343 spin_lock(&mc.lock);
1344 from = mc.from;
1345 to = mc.to;
1346 if (!from)
1347 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001348
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001349 ret = mem_cgroup_same_or_subtree(memcg, from)
1350 || mem_cgroup_same_or_subtree(memcg, to);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001351unlock:
1352 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001353 return ret;
1354}
1355
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001356static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001357{
1358 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001359 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001360 DEFINE_WAIT(wait);
1361 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1362 /* moving charge context might have finished. */
1363 if (mc.moving_task)
1364 schedule();
1365 finish_wait(&mc.waitq, &wait);
1366 return true;
1367 }
1368 }
1369 return false;
1370}
1371
Balbir Singhe2224322009-04-02 16:57:39 -07001372/**
Kirill A. Shutemov6a6135b2010-03-10 15:22:25 -08001373 * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode.
Balbir Singhe2224322009-04-02 16:57:39 -07001374 * @memcg: The memory cgroup that went over limit
1375 * @p: Task that is going to be killed
1376 *
1377 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1378 * enabled
1379 */
1380void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1381{
1382 struct cgroup *task_cgrp;
1383 struct cgroup *mem_cgrp;
1384 /*
1385 * Need a buffer in BSS, can't rely on allocations. The code relies
1386 * on the assumption that OOM is serialized for memory controller.
1387 * If this assumption is broken, revisit this code.
1388 */
1389 static char memcg_name[PATH_MAX];
1390 int ret;
1391
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001392 if (!memcg || !p)
Balbir Singhe2224322009-04-02 16:57:39 -07001393 return;
1394
1395
1396 rcu_read_lock();
1397
1398 mem_cgrp = memcg->css.cgroup;
1399 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1400
1401 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1402 if (ret < 0) {
1403 /*
1404 * Unfortunately, we are unable to convert to a useful name
1405 * But we'll still print out the usage information
1406 */
1407 rcu_read_unlock();
1408 goto done;
1409 }
1410 rcu_read_unlock();
1411
1412 printk(KERN_INFO "Task in %s killed", memcg_name);
1413
1414 rcu_read_lock();
1415 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1416 if (ret < 0) {
1417 rcu_read_unlock();
1418 goto done;
1419 }
1420 rcu_read_unlock();
1421
1422 /*
1423 * Continues from above, so we don't need an KERN_ level
1424 */
1425 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1426done:
1427
1428 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1429 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1430 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1431 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1432 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1433 "failcnt %llu\n",
1434 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1435 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1436 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1437}
1438
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001439/*
1440 * This function returns the number of memcg under hierarchy tree. Returns
1441 * 1(self count) if no children.
1442 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001443static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001444{
1445 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001446 struct mem_cgroup *iter;
1447
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001448 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001449 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001450 return num;
1451}
1452
Balbir Singh6d61ef42009-01-07 18:08:06 -08001453/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001454 * Return the memory (and swap, if configured) limit for a memcg.
1455 */
1456u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
1457{
1458 u64 limit;
1459 u64 memsw;
1460
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001461 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
1462 limit += total_swap_pages << PAGE_SHIFT;
1463
David Rientjesa63d83f2010-08-09 17:19:46 -07001464 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1465 /*
1466 * If memsw is finite and limits the amount of swap space available
1467 * to this memcg, return that limit.
1468 */
1469 return min(limit, memsw);
1470}
1471
1472/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001473 * Visit the first child (need not be the first child as per the ordering
1474 * of the cgroup list, since we track last_scanned_child) of @mem and use
1475 * that to reclaim free pages from.
1476 */
1477static struct mem_cgroup *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001478mem_cgroup_select_victim(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001479{
1480 struct mem_cgroup *ret = NULL;
1481 struct cgroup_subsys_state *css;
1482 int nextid, found;
1483
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001484 if (!root_memcg->use_hierarchy) {
1485 css_get(&root_memcg->css);
1486 ret = root_memcg;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001487 }
1488
1489 while (!ret) {
1490 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001491 nextid = root_memcg->last_scanned_child + 1;
1492 css = css_get_next(&mem_cgroup_subsys, nextid, &root_memcg->css,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001493 &found);
1494 if (css && css_tryget(css))
1495 ret = container_of(css, struct mem_cgroup, css);
1496
1497 rcu_read_unlock();
1498 /* Updates scanning parameter */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001499 if (!css) {
1500 /* this means start scan from ID:1 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001501 root_memcg->last_scanned_child = 0;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001502 } else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001503 root_memcg->last_scanned_child = found;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001504 }
1505
1506 return ret;
1507}
1508
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001509/**
1510 * test_mem_cgroup_node_reclaimable
1511 * @mem: the target memcg
1512 * @nid: the node ID to be checked.
1513 * @noswap : specify true here if the user wants flle only information.
1514 *
1515 * This function returns whether the specified memcg contains any
1516 * reclaimable pages on a node. Returns true if there are any reclaimable
1517 * pages in the node.
1518 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001519static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001520 int nid, bool noswap)
1521{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001522 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001523 return true;
1524 if (noswap || !total_swap_pages)
1525 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001526 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001527 return true;
1528 return false;
1529
1530}
Ying Han889976d2011-05-26 16:25:33 -07001531#if MAX_NUMNODES > 1
1532
1533/*
1534 * Always updating the nodemask is not very good - even if we have an empty
1535 * list or the wrong list here, we can start from some node and traverse all
1536 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1537 *
1538 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001539static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001540{
1541 int nid;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001542 /*
1543 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1544 * pagein/pageout changes since the last update.
1545 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001546 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001547 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001548 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001549 return;
1550
Ying Han889976d2011-05-26 16:25:33 -07001551 /* make a nodemask where this memcg uses memory from */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001552 memcg->scan_nodes = node_states[N_HIGH_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001553
1554 for_each_node_mask(nid, node_states[N_HIGH_MEMORY]) {
1555
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001556 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1557 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001558 }
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001559
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001560 atomic_set(&memcg->numainfo_events, 0);
1561 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001562}
1563
1564/*
1565 * Selecting a node where we start reclaim from. Because what we need is just
1566 * reducing usage counter, start from anywhere is O,K. Considering
1567 * memory reclaim from current node, there are pros. and cons.
1568 *
1569 * Freeing memory from current node means freeing memory from a node which
1570 * we'll use or we've used. So, it may make LRU bad. And if several threads
1571 * hit limits, it will see a contention on a node. But freeing from remote
1572 * node means more costs for memory reclaim because of memory latency.
1573 *
1574 * Now, we use round-robin. Better algorithm is welcomed.
1575 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001576int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001577{
1578 int node;
1579
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001580 mem_cgroup_may_update_nodemask(memcg);
1581 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001582
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001583 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001584 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001585 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001586 /*
1587 * We call this when we hit limit, not when pages are added to LRU.
1588 * No LRU may hold pages because all pages are UNEVICTABLE or
1589 * memcg is too small and all pages are not on LRU. In that case,
1590 * we use curret node.
1591 */
1592 if (unlikely(node == MAX_NUMNODES))
1593 node = numa_node_id();
1594
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001595 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001596 return node;
1597}
1598
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001599/*
1600 * Check all nodes whether it contains reclaimable pages or not.
1601 * For quick scan, we make use of scan_nodes. This will allow us to skip
1602 * unused nodes. But scan_nodes is lazily updated and may not cotain
1603 * enough new information. We need to do double check.
1604 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001605bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001606{
1607 int nid;
1608
1609 /*
1610 * quick check...making use of scan_node.
1611 * We can skip unused nodes.
1612 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001613 if (!nodes_empty(memcg->scan_nodes)) {
1614 for (nid = first_node(memcg->scan_nodes);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001615 nid < MAX_NUMNODES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001616 nid = next_node(nid, memcg->scan_nodes)) {
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001617
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001618 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001619 return true;
1620 }
1621 }
1622 /*
1623 * Check rest of nodes.
1624 */
1625 for_each_node_state(nid, N_HIGH_MEMORY) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001626 if (node_isset(nid, memcg->scan_nodes))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001627 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001628 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001629 return true;
1630 }
1631 return false;
1632}
1633
Ying Han889976d2011-05-26 16:25:33 -07001634#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001635int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001636{
1637 return 0;
1638}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001639
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001640bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001641{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001642 return test_mem_cgroup_node_reclaimable(memcg, 0, noswap);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001643}
Ying Han889976d2011-05-26 16:25:33 -07001644#endif
1645
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001646/*
1647 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1648 * we reclaimed from, so that we don't end up penalizing one child extensively
1649 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001650 *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001651 * root_memcg is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001652 *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001653 * We give up and return to the caller when we visit root_memcg twice.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001654 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001655 *
1656 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001657 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001658static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_memcg,
Balbir Singh4e416952009-09-23 15:56:39 -07001659 struct zone *zone,
Balbir Singh75822b42009-09-23 15:56:38 -07001660 gfp_t gfp_mask,
Ying Han0ae5e892011-05-26 16:25:25 -07001661 unsigned long reclaim_options,
1662 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001663{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001664 struct mem_cgroup *victim;
1665 int ret, total = 0;
1666 int loop = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001667 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1668 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
Balbir Singh4e416952009-09-23 15:56:39 -07001669 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001670 unsigned long excess;
Johannes Weiner185efc02011-09-14 16:21:58 -07001671 unsigned long nr_scanned;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001672
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001673 excess = res_counter_soft_limit_excess(&root_memcg->res) >> PAGE_SHIFT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001674
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001675 /* If memsw_is_minimum==1, swap-out is of-no-use. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001676 if (!check_soft && !shrink && root_memcg->memsw_is_minimum)
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001677 noswap = true;
1678
Balbir Singh4e416952009-09-23 15:56:39 -07001679 while (1) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001680 victim = mem_cgroup_select_victim(root_memcg);
1681 if (victim == root_memcg) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001682 loop++;
KAMEZAWA Hiroyukifbc29a22011-06-15 15:08:46 -07001683 /*
1684 * We are not draining per cpu cached charges during
1685 * soft limit reclaim because global reclaim doesn't
1686 * care about charges. It tries to free some memory and
1687 * charges will not give any.
1688 */
1689 if (!check_soft && loop >= 1)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001690 drain_all_stock_async(root_memcg);
Balbir Singh4e416952009-09-23 15:56:39 -07001691 if (loop >= 2) {
1692 /*
1693 * If we have not been able to reclaim
1694 * anything, it might because there are
1695 * no reclaimable pages under this hierarchy
1696 */
1697 if (!check_soft || !total) {
1698 css_put(&victim->css);
1699 break;
1700 }
1701 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001702 * We want to do more targeted reclaim.
Balbir Singh4e416952009-09-23 15:56:39 -07001703 * excess >> 2 is not to excessive so as to
1704 * reclaim too much, nor too less that we keep
1705 * coming back to reclaim from this cgroup
1706 */
1707 if (total >= (excess >> 2) ||
1708 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1709 css_put(&victim->css);
1710 break;
1711 }
1712 }
1713 }
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001714 if (!mem_cgroup_reclaimable(victim, noswap)) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001715 /* this cgroup's local usage == 0 */
1716 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001717 continue;
1718 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001719 /* we use swappiness of local cgroup */
Ying Han0ae5e892011-05-26 16:25:25 -07001720 if (check_soft) {
Balbir Singh4e416952009-09-23 15:56:39 -07001721 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
Johannes Weiner185efc02011-09-14 16:21:58 -07001722 noswap, zone, &nr_scanned);
1723 *total_scanned += nr_scanned;
Ying Han0ae5e892011-05-26 16:25:25 -07001724 } else
Balbir Singh4e416952009-09-23 15:56:39 -07001725 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
Johannes Weiner185efc02011-09-14 16:21:58 -07001726 noswap);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001727 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001728 /*
1729 * At shrinking usage, we can't check we should stop here or
1730 * reclaim more. It's depends on callers. last_scanned_child
1731 * will work enough for keeping fairness under tree.
1732 */
1733 if (shrink)
1734 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001735 total += ret;
Balbir Singh4e416952009-09-23 15:56:39 -07001736 if (check_soft) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001737 if (!res_counter_soft_limit_excess(&root_memcg->res))
Balbir Singh4e416952009-09-23 15:56:39 -07001738 return total;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001739 } else if (mem_cgroup_margin(root_memcg))
Johannes Weiner4fd14eb2011-05-26 16:25:35 -07001740 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001741 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001742 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001743}
1744
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001745/*
1746 * Check OOM-Killer is already running under our hierarchy.
1747 * If someone is running, return false.
Michal Hocko1af8efe2011-07-26 16:08:24 -07001748 * Has to be called with memcg_oom_lock
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001749 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001750static bool mem_cgroup_oom_lock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001751{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001752 struct mem_cgroup *iter, *failed = NULL;
1753 bool cond = true;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001754
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001755 for_each_mem_cgroup_tree_cond(iter, memcg, cond) {
Johannes Weiner23751be2011-08-25 15:59:16 -07001756 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001757 /*
1758 * this subtree of our hierarchy is already locked
1759 * so we cannot give a lock.
1760 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07001761 failed = iter;
1762 cond = false;
Johannes Weiner23751be2011-08-25 15:59:16 -07001763 } else
1764 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001765 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001766
Michal Hocko79dfdac2011-07-26 16:08:23 -07001767 if (!failed)
Johannes Weiner23751be2011-08-25 15:59:16 -07001768 return true;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001769
1770 /*
1771 * OK, we failed to lock the whole subtree so we have to clean up
1772 * what we set up to the failing subtree
1773 */
1774 cond = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001775 for_each_mem_cgroup_tree_cond(iter, memcg, cond) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001776 if (iter == failed) {
1777 cond = false;
1778 continue;
1779 }
1780 iter->oom_lock = false;
1781 }
Johannes Weiner23751be2011-08-25 15:59:16 -07001782 return false;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001783}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001784
Michal Hocko79dfdac2011-07-26 16:08:23 -07001785/*
Michal Hocko1af8efe2011-07-26 16:08:24 -07001786 * Has to be called with memcg_oom_lock
Michal Hocko79dfdac2011-07-26 16:08:23 -07001787 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001788static int mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001789{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001790 struct mem_cgroup *iter;
1791
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001792 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001793 iter->oom_lock = false;
1794 return 0;
1795}
1796
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001797static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001798{
1799 struct mem_cgroup *iter;
1800
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001801 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001802 atomic_inc(&iter->under_oom);
1803}
1804
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001805static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001806{
1807 struct mem_cgroup *iter;
1808
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001809 /*
1810 * When a new child is created while the hierarchy is under oom,
1811 * mem_cgroup_oom_lock() may not be called. We have to use
1812 * atomic_add_unless() here.
1813 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001814 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001815 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001816}
1817
Michal Hocko1af8efe2011-07-26 16:08:24 -07001818static DEFINE_SPINLOCK(memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001819static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1820
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001821struct oom_wait_info {
1822 struct mem_cgroup *mem;
1823 wait_queue_t wait;
1824};
1825
1826static int memcg_oom_wake_function(wait_queue_t *wait,
1827 unsigned mode, int sync, void *arg)
1828{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001829 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg,
1830 *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001831 struct oom_wait_info *oom_wait_info;
1832
1833 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001834 oom_wait_memcg = oom_wait_info->mem;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001835
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001836 /*
1837 * Both of oom_wait_info->mem and wake_mem are stable under us.
1838 * Then we can use css_is_ancestor without taking care of RCU.
1839 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001840 if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg)
1841 && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001842 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001843 return autoremove_wake_function(wait, mode, sync, arg);
1844}
1845
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001846static void memcg_wakeup_oom(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001847{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001848 /* for filtering, pass "memcg" as argument. */
1849 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001850}
1851
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001852static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001853{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001854 if (memcg && atomic_read(&memcg->under_oom))
1855 memcg_wakeup_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001856}
1857
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001858/*
1859 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
1860 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001861bool mem_cgroup_handle_oom(struct mem_cgroup *memcg, gfp_t mask)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001862{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001863 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001864 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001865
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001866 owait.mem = memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001867 owait.wait.flags = 0;
1868 owait.wait.func = memcg_oom_wake_function;
1869 owait.wait.private = current;
1870 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001871 need_to_kill = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001872 mem_cgroup_mark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001873
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001874 /* At first, try to OOM lock hierarchy under memcg.*/
Michal Hocko1af8efe2011-07-26 16:08:24 -07001875 spin_lock(&memcg_oom_lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001876 locked = mem_cgroup_oom_lock(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001877 /*
1878 * Even if signal_pending(), we can't quit charge() loop without
1879 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
1880 * under OOM is always welcomed, use TASK_KILLABLE here.
1881 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001882 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001883 if (!locked || memcg->oom_kill_disable)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001884 need_to_kill = false;
1885 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001886 mem_cgroup_oom_notify(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07001887 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001888
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001889 if (need_to_kill) {
1890 finish_wait(&memcg_oom_waitq, &owait.wait);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001891 mem_cgroup_out_of_memory(memcg, mask);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001892 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001893 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001894 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001895 }
Michal Hocko1af8efe2011-07-26 16:08:24 -07001896 spin_lock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001897 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001898 mem_cgroup_oom_unlock(memcg);
1899 memcg_wakeup_oom(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07001900 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001901
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001902 mem_cgroup_unmark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001903
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001904 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
1905 return false;
1906 /* Give chance to dying process */
KAMEZAWA Hiroyuki715a5ee2011-11-02 13:38:18 -07001907 schedule_timeout_uninterruptible(1);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001908 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001909}
1910
Balbir Singhd69b0422009-06-17 16:26:34 -07001911/*
1912 * Currently used to update mapped file statistics, but the routine can be
1913 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001914 *
1915 * Notes: Race condition
1916 *
1917 * We usually use page_cgroup_lock() for accessing page_cgroup member but
1918 * it tends to be costly. But considering some conditions, we doesn't need
1919 * to do so _always_.
1920 *
1921 * Considering "charge", lock_page_cgroup() is not required because all
1922 * file-stat operations happen after a page is attached to radix-tree. There
1923 * are no race with "charge".
1924 *
1925 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
1926 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
1927 * if there are race with "uncharge". Statistics itself is properly handled
1928 * by flags.
1929 *
1930 * Considering "move", this is an only case we see a race. To make the race
1931 * small, we check MEM_CGROUP_ON_MOVE percpu value and detect there are
1932 * possibility of race condition. If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07001933 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001934
Greg Thelen2a7106f2011-01-13 15:47:37 -08001935void mem_cgroup_update_page_stat(struct page *page,
1936 enum mem_cgroup_page_stat_item idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07001937{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001938 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001939 struct page_cgroup *pc = lookup_page_cgroup(page);
1940 bool need_unlock = false;
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08001941 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07001942
Balbir Singhd69b0422009-06-17 16:26:34 -07001943 if (unlikely(!pc))
1944 return;
1945
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001946 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001947 memcg = pc->mem_cgroup;
1948 if (unlikely(!memcg || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001949 goto out;
1950 /* pc->mem_cgroup is unstable ? */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001951 if (unlikely(mem_cgroup_stealed(memcg)) || PageTransHuge(page)) {
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001952 /* take a lock against to access pc->mem_cgroup */
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08001953 move_lock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001954 need_unlock = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001955 memcg = pc->mem_cgroup;
1956 if (!memcg || !PageCgroupUsed(pc))
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001957 goto out;
1958 }
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001959
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001960 switch (idx) {
Greg Thelen2a7106f2011-01-13 15:47:37 -08001961 case MEMCG_NR_FILE_MAPPED:
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001962 if (val > 0)
1963 SetPageCgroupFileMapped(pc);
1964 else if (!page_mapped(page))
KAMEZAWA Hiroyuki0c270f82010-10-27 15:33:39 -07001965 ClearPageCgroupFileMapped(pc);
Greg Thelen2a7106f2011-01-13 15:47:37 -08001966 idx = MEM_CGROUP_STAT_FILE_MAPPED;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001967 break;
1968 default:
1969 BUG();
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07001970 }
Balbir Singhd69b0422009-06-17 16:26:34 -07001971
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001972 this_cpu_add(memcg->stat->count[idx], val);
Greg Thelen2a7106f2011-01-13 15:47:37 -08001973
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001974out:
1975 if (unlikely(need_unlock))
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08001976 move_unlock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001977 rcu_read_unlock();
1978 return;
Balbir Singhd69b0422009-06-17 16:26:34 -07001979}
Greg Thelen2a7106f2011-01-13 15:47:37 -08001980EXPORT_SYMBOL(mem_cgroup_update_page_stat);
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001981
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001982/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001983 * size of first charge trial. "32" comes from vmscan.c's magic value.
1984 * TODO: maybe necessary to use big numbers in big irons.
1985 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001986#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001987struct memcg_stock_pcp {
1988 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07001989 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001990 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07001991 unsigned long flags;
1992#define FLUSHING_CACHED_CHARGE (0)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001993};
1994static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02001995static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001996
1997/*
Johannes Weiner11c9ea42011-03-23 16:42:34 -07001998 * Try to consume stocked charge on this cpu. If success, one page is consumed
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001999 * from local stock and true is returned. If the stock is 0 or charges from a
2000 * cgroup which is not current target, returns false. This stock will be
2001 * refilled.
2002 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002003static bool consume_stock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002004{
2005 struct memcg_stock_pcp *stock;
2006 bool ret = true;
2007
2008 stock = &get_cpu_var(memcg_stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002009 if (memcg == stock->cached && stock->nr_pages)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002010 stock->nr_pages--;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002011 else /* need to call res_counter_charge */
2012 ret = false;
2013 put_cpu_var(memcg_stock);
2014 return ret;
2015}
2016
2017/*
2018 * Returns stocks cached in percpu to res_counter and reset cached information.
2019 */
2020static void drain_stock(struct memcg_stock_pcp *stock)
2021{
2022 struct mem_cgroup *old = stock->cached;
2023
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002024 if (stock->nr_pages) {
2025 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2026
2027 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002028 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002029 res_counter_uncharge(&old->memsw, bytes);
2030 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002031 }
2032 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002033}
2034
2035/*
2036 * This must be called under preempt disabled or must be called by
2037 * a thread which is pinned to local cpu.
2038 */
2039static void drain_local_stock(struct work_struct *dummy)
2040{
2041 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
2042 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002043 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002044}
2045
2046/*
2047 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002048 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002049 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002050static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002051{
2052 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2053
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002054 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002055 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002056 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002057 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002058 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002059 put_cpu_var(memcg_stock);
2060}
2061
2062/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002063 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Michal Hockod38144b2011-07-26 16:08:28 -07002064 * of the hierarchy under it. sync flag says whether we should block
2065 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002066 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002067static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002068{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002069 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002070
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002071 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002072 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002073 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002074 for_each_online_cpu(cpu) {
2075 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002076 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002077
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002078 memcg = stock->cached;
2079 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002080 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002081 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002082 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002083 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2084 if (cpu == curcpu)
2085 drain_local_stock(&stock->work);
2086 else
2087 schedule_work_on(cpu, &stock->work);
2088 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002089 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002090 put_cpu();
Michal Hockod38144b2011-07-26 16:08:28 -07002091
2092 if (!sync)
2093 goto out;
2094
2095 for_each_online_cpu(cpu) {
2096 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002097 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002098 flush_work(&stock->work);
2099 }
2100out:
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002101 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002102}
2103
2104/*
2105 * Tries to drain stocked charges in other cpus. This function is asynchronous
2106 * and just put a work per cpu for draining localy on each cpu. Caller can
2107 * expects some charges will be back to res_counter later but cannot wait for
2108 * it.
2109 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002110static void drain_all_stock_async(struct mem_cgroup *root_memcg)
Michal Hockod38144b2011-07-26 16:08:28 -07002111{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002112 /*
2113 * If someone calls draining, avoid adding more kworker runs.
2114 */
2115 if (!mutex_trylock(&percpu_charge_mutex))
2116 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002117 drain_all_stock(root_memcg, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002118 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002119}
2120
2121/* This is a synchronous drain interface. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002122static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002123{
2124 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002125 mutex_lock(&percpu_charge_mutex);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002126 drain_all_stock(root_memcg, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002127 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002128}
2129
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002130/*
2131 * This function drains percpu counter value from DEAD cpu and
2132 * move it to local cpu. Note that this function can be preempted.
2133 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002134static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002135{
2136 int i;
2137
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002138 spin_lock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002139 for (i = 0; i < MEM_CGROUP_STAT_DATA; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002140 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002141
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002142 per_cpu(memcg->stat->count[i], cpu) = 0;
2143 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002144 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002145 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002146 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002147
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002148 per_cpu(memcg->stat->events[i], cpu) = 0;
2149 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002150 }
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002151 /* need to clear ON_MOVE value, works as a kind of lock. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002152 per_cpu(memcg->stat->count[MEM_CGROUP_ON_MOVE], cpu) = 0;
2153 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002154}
2155
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002156static void synchronize_mem_cgroup_on_move(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002157{
2158 int idx = MEM_CGROUP_ON_MOVE;
2159
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002160 spin_lock(&memcg->pcp_counter_lock);
2161 per_cpu(memcg->stat->count[idx], cpu) = memcg->nocpu_base.count[idx];
2162 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002163}
2164
2165static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002166 unsigned long action,
2167 void *hcpu)
2168{
2169 int cpu = (unsigned long)hcpu;
2170 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002171 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002172
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002173 if ((action == CPU_ONLINE)) {
2174 for_each_mem_cgroup_all(iter)
2175 synchronize_mem_cgroup_on_move(iter, cpu);
2176 return NOTIFY_OK;
2177 }
2178
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002179 if ((action != CPU_DEAD) || action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002180 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002181
2182 for_each_mem_cgroup_all(iter)
2183 mem_cgroup_drain_pcp_counter(iter, cpu);
2184
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002185 stock = &per_cpu(memcg_stock, cpu);
2186 drain_stock(stock);
2187 return NOTIFY_OK;
2188}
2189
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002190
2191/* See __mem_cgroup_try_charge() for details */
2192enum {
2193 CHARGE_OK, /* success */
2194 CHARGE_RETRY, /* need to retry but retry is not bad */
2195 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2196 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
2197 CHARGE_OOM_DIE, /* the current is killed because of OOM */
2198};
2199
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002200static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002201 unsigned int nr_pages, bool oom_check)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002202{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002203 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002204 struct mem_cgroup *mem_over_limit;
2205 struct res_counter *fail_res;
2206 unsigned long flags = 0;
2207 int ret;
2208
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002209 ret = res_counter_charge(&memcg->res, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002210
2211 if (likely(!ret)) {
2212 if (!do_swap_account)
2213 return CHARGE_OK;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002214 ret = res_counter_charge(&memcg->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002215 if (likely(!ret))
2216 return CHARGE_OK;
2217
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002218 res_counter_uncharge(&memcg->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002219 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2220 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2221 } else
2222 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002223 /*
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002224 * nr_pages can be either a huge page (HPAGE_PMD_NR), a batch
2225 * of regular pages (CHARGE_BATCH), or a single regular page (1).
Johannes Weiner9221edb2011-02-01 15:52:42 -08002226 *
2227 * Never reclaim on behalf of optional batching, retry with a
2228 * single page instead.
2229 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002230 if (nr_pages == CHARGE_BATCH)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002231 return CHARGE_RETRY;
2232
2233 if (!(gfp_mask & __GFP_WAIT))
2234 return CHARGE_WOULDBLOCK;
2235
2236 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
Ying Han0ae5e892011-05-26 16:25:25 -07002237 gfp_mask, flags, NULL);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002238 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002239 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002240 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002241 * Even though the limit is exceeded at this point, reclaim
2242 * may have been able to free some pages. Retry the charge
2243 * before killing the task.
2244 *
2245 * Only for regular pages, though: huge pages are rather
2246 * unlikely to succeed so close to the limit, and we fall back
2247 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002248 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002249 if (nr_pages == 1 && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002250 return CHARGE_RETRY;
2251
2252 /*
2253 * At task move, charge accounts can be doubly counted. So, it's
2254 * better to wait until the end of task_move if something is going on.
2255 */
2256 if (mem_cgroup_wait_acct_move(mem_over_limit))
2257 return CHARGE_RETRY;
2258
2259 /* If we don't need to call oom-killer at el, return immediately */
2260 if (!oom_check)
2261 return CHARGE_NOMEM;
2262 /* check OOM */
2263 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask))
2264 return CHARGE_OOM_DIE;
2265
2266 return CHARGE_RETRY;
2267}
2268
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002269/*
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002270 * Unlike exported interface, "oom" parameter is added. if oom==true,
2271 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002272 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002273static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002274 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002275 unsigned int nr_pages,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002276 struct mem_cgroup **ptr,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002277 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002278{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002279 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002280 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002281 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002282 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002283
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002284 /*
2285 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2286 * in system level. So, allow to go ahead dying process in addition to
2287 * MEMDIE process.
2288 */
2289 if (unlikely(test_thread_flag(TIF_MEMDIE)
2290 || fatal_signal_pending(current)))
2291 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002292
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002293 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08002294 * We always charge the cgroup the mm_struct belongs to.
2295 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002296 * thread group leader migrates. It's possible that mm is not
2297 * set, if so charge the init_mm (happens for pagecache usage).
2298 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002299 if (!*ptr && !mm)
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002300 goto bypass;
2301again:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002302 if (*ptr) { /* css should be a valid one */
2303 memcg = *ptr;
2304 VM_BUG_ON(css_is_removed(&memcg->css));
2305 if (mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002306 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002307 if (nr_pages == 1 && consume_stock(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002308 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002309 css_get(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002310 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002311 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002312
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002313 rcu_read_lock();
2314 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002315 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002316 * Because we don't have task_lock(), "p" can exit.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002317 * In that case, "memcg" can point to root or p can be NULL with
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002318 * race with swapoff. Then, we have small risk of mis-accouning.
2319 * But such kind of mis-account by race always happens because
2320 * we don't have cgroup_mutex(). It's overkill and we allo that
2321 * small race, here.
2322 * (*) swapoff at el will charge against mm-struct not against
2323 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002324 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002325 memcg = mem_cgroup_from_task(p);
2326 if (!memcg || mem_cgroup_is_root(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002327 rcu_read_unlock();
2328 goto done;
2329 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002330 if (nr_pages == 1 && consume_stock(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002331 /*
2332 * It seems dagerous to access memcg without css_get().
2333 * But considering how consume_stok works, it's not
2334 * necessary. If consume_stock success, some charges
2335 * from this memcg are cached on this cpu. So, we
2336 * don't need to call css_get()/css_tryget() before
2337 * calling consume_stock().
2338 */
2339 rcu_read_unlock();
2340 goto done;
2341 }
2342 /* after here, we may be blocked. we need to get refcnt */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002343 if (!css_tryget(&memcg->css)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002344 rcu_read_unlock();
2345 goto again;
2346 }
2347 rcu_read_unlock();
2348 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002349
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002350 do {
2351 bool oom_check;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002352
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002353 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002354 if (fatal_signal_pending(current)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002355 css_put(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002356 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002357 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002358
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002359 oom_check = false;
2360 if (oom && !nr_oom_retries) {
2361 oom_check = true;
2362 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2363 }
Balbir Singh6d61ef42009-01-07 18:08:06 -08002364
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002365 ret = mem_cgroup_do_charge(memcg, gfp_mask, batch, oom_check);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002366 switch (ret) {
2367 case CHARGE_OK:
2368 break;
2369 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002370 batch = nr_pages;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002371 css_put(&memcg->css);
2372 memcg = NULL;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002373 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002374 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002375 css_put(&memcg->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002376 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002377 case CHARGE_NOMEM: /* OOM routine works */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002378 if (!oom) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002379 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002380 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002381 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002382 /* If oom, we never return -ENOMEM */
2383 nr_oom_retries--;
2384 break;
2385 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002386 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002387 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08002388 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002389 } while (ret != CHARGE_OK);
2390
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002391 if (batch > nr_pages)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002392 refill_stock(memcg, batch - nr_pages);
2393 css_put(&memcg->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002394done:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002395 *ptr = memcg;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002396 return 0;
2397nomem:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002398 *ptr = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002399 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002400bypass:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002401 *ptr = NULL;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002402 return 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002403}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002404
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002405/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002406 * Somemtimes we have to undo a charge we got by try_charge().
2407 * This function is for that and do uncharge, put css's refcnt.
2408 * gotten by try_charge().
2409 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002410static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002411 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002412{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002413 if (!mem_cgroup_is_root(memcg)) {
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002414 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002415
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002416 res_counter_uncharge(&memcg->res, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002417 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002418 res_counter_uncharge(&memcg->memsw, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002419 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002420}
2421
2422/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002423 * A helper function to get mem_cgroup from ID. must be called under
2424 * rcu_read_lock(). The caller must check css_is_removed() or some if
2425 * it's concern. (dropping refcnt from swap can be called against removed
2426 * memcg.)
2427 */
2428static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2429{
2430 struct cgroup_subsys_state *css;
2431
2432 /* ID 0 is unused ID */
2433 if (!id)
2434 return NULL;
2435 css = css_lookup(&mem_cgroup_subsys, id);
2436 if (!css)
2437 return NULL;
2438 return container_of(css, struct mem_cgroup, css);
2439}
2440
Wu Fengguange42d9d52009-12-16 12:19:59 +01002441struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002442{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002443 struct mem_cgroup *memcg = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002444 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002445 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002446 swp_entry_t ent;
2447
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002448 VM_BUG_ON(!PageLocked(page));
2449
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002450 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002451 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002452 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002453 memcg = pc->mem_cgroup;
2454 if (memcg && !css_tryget(&memcg->css))
2455 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002456 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002457 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002458 id = lookup_swap_cgroup(ent);
2459 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002460 memcg = mem_cgroup_lookup(id);
2461 if (memcg && !css_tryget(&memcg->css))
2462 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002463 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002464 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002465 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002466 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002467}
2468
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002469static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,
Johannes Weiner5564e882011-03-23 16:42:29 -07002470 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002471 unsigned int nr_pages,
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002472 struct page_cgroup *pc,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002473 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002474{
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002475 lock_page_cgroup(pc);
2476 if (unlikely(PageCgroupUsed(pc))) {
2477 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002478 __mem_cgroup_cancel_charge(memcg, nr_pages);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002479 return;
2480 }
2481 /*
2482 * we don't need page_cgroup_lock about tail pages, becase they are not
2483 * accessed by any other context at this point.
2484 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002485 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002486 /*
2487 * We access a page_cgroup asynchronously without lock_page_cgroup().
2488 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2489 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2490 * before USED bit, we need memory barrier here.
2491 * See mem_cgroup_add_lru_list(), etc.
2492 */
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002493 smp_wmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -07002494 switch (ctype) {
2495 case MEM_CGROUP_CHARGE_TYPE_CACHE:
2496 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
2497 SetPageCgroupCache(pc);
2498 SetPageCgroupUsed(pc);
2499 break;
2500 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
2501 ClearPageCgroupCache(pc);
2502 SetPageCgroupUsed(pc);
2503 break;
2504 default:
2505 break;
2506 }
Hugh Dickins3be91272008-02-07 00:14:19 -08002507
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002508 mem_cgroup_charge_statistics(memcg, PageCgroupCache(pc), nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002509 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002510 /*
2511 * "charge_statistics" updated event counter. Then, check it.
2512 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2513 * if they exceeds softlimit.
2514 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002515 memcg_check_events(memcg, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002516}
2517
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002518#ifdef CONFIG_TRANSPARENT_HUGEPAGE
2519
2520#define PCGF_NOCOPY_AT_SPLIT ((1 << PCG_LOCK) | (1 << PCG_MOVE_LOCK) |\
2521 (1 << PCG_ACCT_LRU) | (1 << PCG_MIGRATION))
2522/*
2523 * Because tail pages are not marked as "used", set it. We're under
2524 * zone->lru_lock, 'splitting on pmd' and compund_lock.
2525 */
2526void mem_cgroup_split_huge_fixup(struct page *head, struct page *tail)
2527{
2528 struct page_cgroup *head_pc = lookup_page_cgroup(head);
2529 struct page_cgroup *tail_pc = lookup_page_cgroup(tail);
2530 unsigned long flags;
2531
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08002532 if (mem_cgroup_disabled())
2533 return;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002534 /*
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002535 * We have no races with charge/uncharge but will have races with
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002536 * page state accounting.
2537 */
2538 move_lock_page_cgroup(head_pc, &flags);
2539
2540 tail_pc->mem_cgroup = head_pc->mem_cgroup;
2541 smp_wmb(); /* see __commit_charge() */
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002542 if (PageCgroupAcctLRU(head_pc)) {
2543 enum lru_list lru;
2544 struct mem_cgroup_per_zone *mz;
2545
2546 /*
2547 * LRU flags cannot be copied because we need to add tail
2548 *.page to LRU by generic call and our hook will be called.
2549 * We hold lru_lock, then, reduce counter directly.
2550 */
2551 lru = page_lru(head);
Johannes Weiner97a6c372011-03-23 16:42:27 -07002552 mz = page_cgroup_zoneinfo(head_pc->mem_cgroup, head);
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002553 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
2554 }
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002555 tail_pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
2556 move_unlock_page_cgroup(head_pc, &flags);
2557}
2558#endif
2559
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002560/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07002561 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07002562 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002563 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002564 * @pc: page_cgroup of the page.
2565 * @from: mem_cgroup which the page is moved from.
2566 * @to: mem_cgroup which the page is moved to. @from != @to.
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002567 * @uncharge: whether we should call uncharge and css_put against @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002568 *
2569 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002570 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002571 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002572 *
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002573 * This function doesn't do "charge" nor css_get to new cgroup. It should be
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002574 * done by a caller(__mem_cgroup_try_charge would be useful). If @uncharge is
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002575 * true, this function does "uncharge" from old cgroup, but it doesn't if
2576 * @uncharge is false, so a caller should do "uncharge".
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002577 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002578static int mem_cgroup_move_account(struct page *page,
2579 unsigned int nr_pages,
2580 struct page_cgroup *pc,
2581 struct mem_cgroup *from,
2582 struct mem_cgroup *to,
2583 bool uncharge)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002584{
Johannes Weinerde3638d2011-03-23 16:42:28 -07002585 unsigned long flags;
2586 int ret;
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002587
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002588 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07002589 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07002590 /*
2591 * The page is isolated from LRU. So, collapse function
2592 * will not handle this page. But page splitting can happen.
2593 * Do this check under compound_page_lock(). The caller should
2594 * hold it.
2595 */
2596 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002597 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07002598 goto out;
2599
2600 lock_page_cgroup(pc);
2601
2602 ret = -EINVAL;
2603 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
2604 goto unlock;
2605
2606 move_lock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002607
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002608 if (PageCgroupFileMapped(pc)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08002609 /* Update mapped_file data for mem_cgroup */
2610 preempt_disable();
2611 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2612 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2613 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07002614 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002615 mem_cgroup_charge_statistics(from, PageCgroupCache(pc), -nr_pages);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002616 if (uncharge)
2617 /* This is not "cancel", but cancel_charge does all we need. */
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002618 __mem_cgroup_cancel_charge(from, nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07002619
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002620 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002621 pc->mem_cgroup = to;
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002622 mem_cgroup_charge_statistics(to, PageCgroupCache(pc), nr_pages);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002623 /*
2624 * We charges against "to" which may not have any tasks. Then, "to"
2625 * can be under rmdir(). But in current implementation, caller of
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08002626 * this function is just force_empty() and move charge, so it's
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002627 * guaranteed that "to" is never removed. So, we don't check rmdir
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08002628 * status here.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002629 */
Johannes Weinerde3638d2011-03-23 16:42:28 -07002630 move_unlock_page_cgroup(pc, &flags);
2631 ret = 0;
2632unlock:
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002633 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002634 /*
2635 * check events
2636 */
Johannes Weiner5564e882011-03-23 16:42:29 -07002637 memcg_check_events(to, page);
2638 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07002639out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002640 return ret;
2641}
2642
2643/*
2644 * move charges to its parent.
2645 */
2646
Johannes Weiner5564e882011-03-23 16:42:29 -07002647static int mem_cgroup_move_parent(struct page *page,
2648 struct page_cgroup *pc,
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002649 struct mem_cgroup *child,
2650 gfp_t gfp_mask)
2651{
2652 struct cgroup *cg = child->css.cgroup;
2653 struct cgroup *pcg = cg->parent;
2654 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002655 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07002656 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002657 int ret;
2658
2659 /* Is ROOT ? */
2660 if (!pcg)
2661 return -EINVAL;
2662
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002663 ret = -EBUSY;
2664 if (!get_page_unless_zero(page))
2665 goto out;
2666 if (isolate_lru_page(page))
2667 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08002668
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002669 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002670
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002671 parent = mem_cgroup_from_cont(pcg);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002672 ret = __mem_cgroup_try_charge(NULL, gfp_mask, nr_pages, &parent, false);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002673 if (ret || !parent)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002674 goto put_back;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002675
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002676 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002677 flags = compound_lock_irqsave(page);
2678
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002679 ret = mem_cgroup_move_account(page, nr_pages, pc, child, parent, true);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002680 if (ret)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002681 __mem_cgroup_cancel_charge(parent, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08002682
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002683 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002684 compound_unlock_irqrestore(page, flags);
Jesper Juhl8dba4742011-01-25 15:07:24 -08002685put_back:
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08002686 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002687put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08002688 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002689out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002690 return ret;
2691}
2692
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002693/*
2694 * Charge the memory controller for page usage.
2695 * Return
2696 * 0 if the charge was successful
2697 * < 0 if the cgroup is over its limit
2698 */
2699static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002700 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002701{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002702 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002703 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08002704 struct page_cgroup *pc;
2705 bool oom = true;
2706 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08002707
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002708 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002709 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002710 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08002711 /*
2712 * Never OOM-kill a process for a huge page. The
2713 * fault handler will fall back to regular pages.
2714 */
2715 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002716 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002717
2718 pc = lookup_page_cgroup(page);
Johannes Weineraf4a6622011-03-23 16:42:24 -07002719 BUG_ON(!pc); /* XXX: remove this and move pc lookup into commit */
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002720
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002721 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom);
2722 if (ret || !memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002723 return ret;
2724
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002725 __mem_cgroup_commit_charge(memcg, page, nr_pages, pc, ctype);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002726 return 0;
2727}
2728
2729int mem_cgroup_newpage_charge(struct page *page,
2730 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002731{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002732 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002733 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002734 /*
2735 * If already mapped, we don't have to account.
2736 * If page cache, page->mapping has address_space.
2737 * But page->mapping may have out-of-use anon_vma pointer,
2738 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
2739 * is NULL.
2740 */
2741 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
2742 return 0;
2743 if (unlikely(!mm))
2744 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002745 return mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002746 MEM_CGROUP_CHARGE_TYPE_MAPPED);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002747}
2748
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002749static void
2750__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2751 enum charge_type ctype);
2752
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002753static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002754__mem_cgroup_commit_charge_lrucare(struct page *page, struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002755 enum charge_type ctype)
2756{
2757 struct page_cgroup *pc = lookup_page_cgroup(page);
2758 /*
2759 * In some case, SwapCache, FUSE(splice_buf->radixtree), the page
2760 * is already on LRU. It means the page may on some other page_cgroup's
2761 * LRU. Take care of it.
2762 */
2763 mem_cgroup_lru_del_before_commit(page);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002764 __mem_cgroup_commit_charge(memcg, page, 1, pc, ctype);
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002765 mem_cgroup_lru_add_after_commit(page);
2766 return;
2767}
2768
Balbir Singhe1a1cd52008-02-07 00:14:02 -08002769int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
2770 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08002771{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002772 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002773 int ret;
2774
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002775 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002776 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002777 if (PageCompound(page))
2778 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002779
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002780 if (unlikely(!mm))
Balbir Singh8697d332008-02-07 00:13:59 -08002781 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002782
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002783 if (page_is_file_cache(page)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002784 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, &memcg, true);
2785 if (ret || !memcg)
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002786 return ret;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002787
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002788 /*
2789 * FUSE reuses pages without going through the final
2790 * put that would remove them from the LRU list, make
2791 * sure that they get relinked properly.
2792 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002793 __mem_cgroup_commit_charge_lrucare(page, memcg,
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002794 MEM_CGROUP_CHARGE_TYPE_CACHE);
2795 return ret;
2796 }
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002797 /* shmem */
2798 if (PageSwapCache(page)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002799 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &memcg);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002800 if (!ret)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002801 __mem_cgroup_commit_charge_swapin(page, memcg,
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002802 MEM_CGROUP_CHARGE_TYPE_SHMEM);
2803 } else
2804 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002805 MEM_CGROUP_CHARGE_TYPE_SHMEM);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002806
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002807 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002808}
2809
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002810/*
2811 * While swap-in, try_charge -> commit or cancel, the page is locked.
2812 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02002813 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002814 * "commit()" or removed by "cancel()"
2815 */
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002816int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
2817 struct page *page,
2818 gfp_t mask, struct mem_cgroup **ptr)
2819{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002820 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002821 int ret;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002822
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07002823 *ptr = NULL;
2824
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002825 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002826 return 0;
2827
2828 if (!do_swap_account)
2829 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002830 /*
2831 * A racing thread's fault, or swapoff, may have already updated
Hugh Dickins407f9c82009-12-14 17:59:30 -08002832 * the pte, and even removed page from swap cache: in those cases
2833 * do_swap_page()'s pte_same() test will fail; but there's also a
2834 * KSM case which does need to charge the page.
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002835 */
2836 if (!PageSwapCache(page))
Hugh Dickins407f9c82009-12-14 17:59:30 -08002837 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002838 memcg = try_get_mem_cgroup_from_page(page);
2839 if (!memcg)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002840 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002841 *ptr = memcg;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002842 ret = __mem_cgroup_try_charge(NULL, mask, 1, ptr, true);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002843 css_put(&memcg->css);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002844 return ret;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002845charge_cur_mm:
2846 if (unlikely(!mm))
2847 mm = &init_mm;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002848 return __mem_cgroup_try_charge(mm, mask, 1, ptr, true);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002849}
2850
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002851static void
2852__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2853 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002854{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002855 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002856 return;
2857 if (!ptr)
2858 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002859 cgroup_exclude_rmdir(&ptr->css);
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002860
2861 __mem_cgroup_commit_charge_lrucare(page, ptr, ctype);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002862 /*
2863 * Now swap is on-memory. This means this page may be
2864 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002865 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
2866 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
2867 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002868 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002869 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002870 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002871 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002872 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002873
2874 id = swap_cgroup_record(ent, 0);
2875 rcu_read_lock();
2876 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002877 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002878 /*
2879 * This recorded memcg can be obsolete one. So, avoid
2880 * calling css_tryget
2881 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002882 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002883 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002884 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002885 mem_cgroup_put(memcg);
2886 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002887 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002888 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002889 /*
2890 * At swapin, we may charge account against cgroup which has no tasks.
2891 * So, rmdir()->pre_destroy() can be called while we do this charge.
2892 * In that case, we need to call pre_destroy() again. check it here.
2893 */
2894 cgroup_release_and_wakeup_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002895}
2896
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002897void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
2898{
2899 __mem_cgroup_commit_charge_swapin(page, ptr,
2900 MEM_CGROUP_CHARGE_TYPE_MAPPED);
2901}
2902
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002903void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002904{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002905 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002906 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002907 if (!memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002908 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002909 __mem_cgroup_cancel_charge(memcg, 1);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002910}
2911
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002912static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002913 unsigned int nr_pages,
2914 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002915{
2916 struct memcg_batch_info *batch = NULL;
2917 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002918
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002919 /* If swapout, usage of swap doesn't decrease */
2920 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2921 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002922
2923 batch = &current->memcg_batch;
2924 /*
2925 * In usual, we do css_get() when we remember memcg pointer.
2926 * But in this case, we keep res->usage until end of a series of
2927 * uncharges. Then, it's ok to ignore memcg's refcnt.
2928 */
2929 if (!batch->memcg)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002930 batch->memcg = memcg;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002931 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002932 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002933 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002934 * the same cgroup and we have chance to coalesce uncharges.
2935 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
2936 * because we want to do uncharge as soon as possible.
2937 */
2938
2939 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
2940 goto direct_uncharge;
2941
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002942 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08002943 goto direct_uncharge;
2944
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002945 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002946 * In typical case, batch->memcg == mem. This means we can
2947 * merge a series of uncharges to an uncharge of res_counter.
2948 * If not, we uncharge res_counter ony by one.
2949 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002950 if (batch->memcg != memcg)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002951 goto direct_uncharge;
2952 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07002953 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002954 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07002955 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002956 return;
2957direct_uncharge:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002958 res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002959 if (uncharge_memsw)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002960 res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE);
2961 if (unlikely(batch->memcg != memcg))
2962 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002963 return;
2964}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002965
Balbir Singh8697d332008-02-07 00:13:59 -08002966/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002967 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002968 */
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002969static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002970__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002971{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002972 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002973 unsigned int nr_pages = 1;
2974 struct page_cgroup *pc;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002975
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002976 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002977 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07002978
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002979 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002980 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002981
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002982 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002983 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002984 VM_BUG_ON(!PageTransHuge(page));
2985 }
Balbir Singh8697d332008-02-07 00:13:59 -08002986 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08002987 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08002988 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002989 pc = lookup_page_cgroup(page);
2990 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002991 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002992
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002993 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002994
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002995 memcg = pc->mem_cgroup;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002996
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002997 if (!PageCgroupUsed(pc))
2998 goto unlock_out;
2999
3000 switch (ctype) {
3001 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003002 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003003 /* See mem_cgroup_prepare_migration() */
3004 if (page_mapped(page) || PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003005 goto unlock_out;
3006 break;
3007 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
3008 if (!PageAnon(page)) { /* Shared memory */
3009 if (page->mapping && !page_is_file_cache(page))
3010 goto unlock_out;
3011 } else if (page_mapped(page)) /* Anon */
3012 goto unlock_out;
3013 break;
3014 default:
3015 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003016 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003017
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003018 mem_cgroup_charge_statistics(memcg, PageCgroupCache(pc), -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003019
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003020 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08003021 /*
3022 * pc->mem_cgroup is not cleared here. It will be accessed when it's
3023 * freed from LRU. This is safe because uncharged page is expected not
3024 * to be reused (freed soon). Exception is SwapCache, it's handled by
3025 * special functions.
3026 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003027
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003028 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003029 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003030 * even after unlock, we have memcg->res.usage here and this memcg
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003031 * will never be freed.
3032 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003033 memcg_check_events(memcg, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003034 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003035 mem_cgroup_swap_statistics(memcg, true);
3036 mem_cgroup_get(memcg);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003037 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003038 if (!mem_cgroup_is_root(memcg))
3039 mem_cgroup_do_uncharge(memcg, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003040
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003041 return memcg;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003042
3043unlock_out:
3044 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003045 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08003046}
3047
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003048void mem_cgroup_uncharge_page(struct page *page)
3049{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003050 /* early check. */
3051 if (page_mapped(page))
3052 return;
3053 if (page->mapping && !PageAnon(page))
3054 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003055 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
3056}
3057
3058void mem_cgroup_uncharge_cache_page(struct page *page)
3059{
3060 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07003061 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003062 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
3063}
3064
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003065/*
3066 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
3067 * In that cases, pages are freed continuously and we can expect pages
3068 * are in the same memcg. All these calls itself limits the number of
3069 * pages freed at once, then uncharge_start/end() is called properly.
3070 * This may be called prural(2) times in a context,
3071 */
3072
3073void mem_cgroup_uncharge_start(void)
3074{
3075 current->memcg_batch.do_batch++;
3076 /* We can do nest. */
3077 if (current->memcg_batch.do_batch == 1) {
3078 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003079 current->memcg_batch.nr_pages = 0;
3080 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003081 }
3082}
3083
3084void mem_cgroup_uncharge_end(void)
3085{
3086 struct memcg_batch_info *batch = &current->memcg_batch;
3087
3088 if (!batch->do_batch)
3089 return;
3090
3091 batch->do_batch--;
3092 if (batch->do_batch) /* If stacked, do nothing. */
3093 return;
3094
3095 if (!batch->memcg)
3096 return;
3097 /*
3098 * This "batch->memcg" is valid without any css_get/put etc...
3099 * bacause we hide charges behind us.
3100 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003101 if (batch->nr_pages)
3102 res_counter_uncharge(&batch->memcg->res,
3103 batch->nr_pages * PAGE_SIZE);
3104 if (batch->memsw_nr_pages)
3105 res_counter_uncharge(&batch->memcg->memsw,
3106 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003107 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003108 /* forget this pointer (for sanity check) */
3109 batch->memcg = NULL;
3110}
3111
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003112#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003113/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003114 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003115 * memcg information is recorded to swap_cgroup of "ent"
3116 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003117void
3118mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003119{
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003120 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003121 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003122
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003123 if (!swapout) /* this was a swap cache but the swap is unused ! */
3124 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
3125
3126 memcg = __mem_cgroup_uncharge_common(page, ctype);
3127
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003128 /*
3129 * record memcg information, if swapout && memcg != NULL,
3130 * mem_cgroup_get() was called in uncharge().
3131 */
3132 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003133 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003134}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003135#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003136
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003137#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3138/*
3139 * called from swap_entry_free(). remove record in swap_cgroup and
3140 * uncharge "memsw" account.
3141 */
3142void mem_cgroup_uncharge_swap(swp_entry_t ent)
3143{
3144 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003145 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003146
3147 if (!do_swap_account)
3148 return;
3149
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003150 id = swap_cgroup_record(ent, 0);
3151 rcu_read_lock();
3152 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003153 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003154 /*
3155 * We uncharge this because swap is freed.
3156 * This memcg can be obsolete one. We avoid calling css_tryget
3157 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003158 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07003159 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003160 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003161 mem_cgroup_put(memcg);
3162 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003163 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003164}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003165
3166/**
3167 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
3168 * @entry: swap entry to be moved
3169 * @from: mem_cgroup which the entry is moved from
3170 * @to: mem_cgroup which the entry is moved to
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003171 * @need_fixup: whether we should fixup res_counters and refcounts.
Daisuke Nishimura02491442010-03-10 15:22:17 -08003172 *
3173 * It succeeds only when the swap_cgroup's record for this entry is the same
3174 * as the mem_cgroup's id of @from.
3175 *
3176 * Returns 0 on success, -EINVAL on failure.
3177 *
3178 * The caller must have charged to @to, IOW, called res_counter_charge() about
3179 * both res and memsw, and called css_get().
3180 */
3181static int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003182 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003183{
3184 unsigned short old_id, new_id;
3185
3186 old_id = css_id(&from->css);
3187 new_id = css_id(&to->css);
3188
3189 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08003190 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003191 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003192 /*
3193 * This function is only called from task migration context now.
3194 * It postpones res_counter and refcount handling till the end
3195 * of task migration(mem_cgroup_clear_mc()) for performance
3196 * improvement. But we cannot postpone mem_cgroup_get(to)
3197 * because if the process that has been moved to @to does
3198 * swap-in, the refcount of @to might be decreased to 0.
3199 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08003200 mem_cgroup_get(to);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003201 if (need_fixup) {
3202 if (!mem_cgroup_is_root(from))
3203 res_counter_uncharge(&from->memsw, PAGE_SIZE);
3204 mem_cgroup_put(from);
3205 /*
3206 * we charged both to->res and to->memsw, so we should
3207 * uncharge to->res.
3208 */
3209 if (!mem_cgroup_is_root(to))
3210 res_counter_uncharge(&to->res, PAGE_SIZE);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003211 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08003212 return 0;
3213 }
3214 return -EINVAL;
3215}
3216#else
3217static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003218 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003219{
3220 return -EINVAL;
3221}
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003222#endif
3223
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003224/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003225 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
3226 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003227 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003228int mem_cgroup_prepare_migration(struct page *page,
Miklos Szeredief6a3c62011-03-22 16:30:52 -07003229 struct page *newpage, struct mem_cgroup **ptr, gfp_t gfp_mask)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003230{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003231 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003232 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003233 enum charge_type ctype;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003234 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003235
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07003236 *ptr = NULL;
3237
Andrea Arcangeliec168512011-01-13 15:46:56 -08003238 VM_BUG_ON(PageTransHuge(page));
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003239 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07003240 return 0;
3241
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003242 pc = lookup_page_cgroup(page);
3243 lock_page_cgroup(pc);
3244 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003245 memcg = pc->mem_cgroup;
3246 css_get(&memcg->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003247 /*
3248 * At migrating an anonymous page, its mapcount goes down
3249 * to 0 and uncharge() will be called. But, even if it's fully
3250 * unmapped, migration may fail and this page has to be
3251 * charged again. We set MIGRATION flag here and delay uncharge
3252 * until end_migration() is called
3253 *
3254 * Corner Case Thinking
3255 * A)
3256 * When the old page was mapped as Anon and it's unmap-and-freed
3257 * while migration was ongoing.
3258 * If unmap finds the old page, uncharge() of it will be delayed
3259 * until end_migration(). If unmap finds a new page, it's
3260 * uncharged when it make mapcount to be 1->0. If unmap code
3261 * finds swap_migration_entry, the new page will not be mapped
3262 * and end_migration() will find it(mapcount==0).
3263 *
3264 * B)
3265 * When the old page was mapped but migraion fails, the kernel
3266 * remaps it. A charge for it is kept by MIGRATION flag even
3267 * if mapcount goes down to 0. We can do remap successfully
3268 * without charging it again.
3269 *
3270 * C)
3271 * The "old" page is under lock_page() until the end of
3272 * migration, so, the old page itself will not be swapped-out.
3273 * If the new page is swapped out before end_migraton, our
3274 * hook to usual swap-out path will catch the event.
3275 */
3276 if (PageAnon(page))
3277 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003278 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003279 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003280 /*
3281 * If the page is not charged at this point,
3282 * we return here.
3283 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003284 if (!memcg)
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003285 return 0;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003286
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003287 *ptr = memcg;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003288 ret = __mem_cgroup_try_charge(NULL, gfp_mask, 1, ptr, false);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003289 css_put(&memcg->css);/* drop extra refcnt */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003290 if (ret || *ptr == NULL) {
3291 if (PageAnon(page)) {
3292 lock_page_cgroup(pc);
3293 ClearPageCgroupMigration(pc);
3294 unlock_page_cgroup(pc);
3295 /*
3296 * The old page may be fully unmapped while we kept it.
3297 */
3298 mem_cgroup_uncharge_page(page);
3299 }
3300 return -ENOMEM;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003301 }
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003302 /*
3303 * We charge new page before it's used/mapped. So, even if unlock_page()
3304 * is called before end_migration, we can catch all events on this new
3305 * page. In the case new page is migrated but not remapped, new page's
3306 * mapcount will be finally 0 and we call uncharge in end_migration().
3307 */
3308 pc = lookup_page_cgroup(newpage);
3309 if (PageAnon(page))
3310 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
3311 else if (page_is_file_cache(page))
3312 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
3313 else
3314 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003315 __mem_cgroup_commit_charge(memcg, page, 1, pc, ctype);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003316 return ret;
3317}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08003318
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003319/* remove redundant charge if migration failed*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003320void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003321 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003322{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003323 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003324 struct page_cgroup *pc;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003325
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003326 if (!memcg)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003327 return;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003328 /* blocks rmdir() */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003329 cgroup_exclude_rmdir(&memcg->css);
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003330 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003331 used = oldpage;
3332 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003333 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003334 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003335 unused = oldpage;
3336 }
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003337 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003338 * We disallowed uncharge of pages under migration because mapcount
3339 * of the page goes down to zero, temporarly.
3340 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003341 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003342 pc = lookup_page_cgroup(oldpage);
3343 lock_page_cgroup(pc);
3344 ClearPageCgroupMigration(pc);
3345 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003346
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003347 __mem_cgroup_uncharge_common(unused, MEM_CGROUP_CHARGE_TYPE_FORCE);
3348
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003349 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003350 * If a page is a file cache, radix-tree replacement is very atomic
3351 * and we can skip this check. When it was an Anon page, its mapcount
3352 * goes down to 0. But because we added MIGRATION flage, it's not
3353 * uncharged yet. There are several case but page->mapcount check
3354 * and USED bit check in mem_cgroup_uncharge_page() will do enough
3355 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003356 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003357 if (PageAnon(used))
3358 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003359 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003360 * At migration, we may charge account against cgroup which has no
3361 * tasks.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003362 * So, rmdir()->pre_destroy() can be called while we do this charge.
3363 * In that case, we need to call pre_destroy() again. check it here.
3364 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003365 cgroup_release_and_wakeup_rmdir(&memcg->css);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003366}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08003367
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003368#ifdef CONFIG_DEBUG_VM
3369static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
3370{
3371 struct page_cgroup *pc;
3372
3373 pc = lookup_page_cgroup(page);
3374 if (likely(pc) && PageCgroupUsed(pc))
3375 return pc;
3376 return NULL;
3377}
3378
3379bool mem_cgroup_bad_page_check(struct page *page)
3380{
3381 if (mem_cgroup_disabled())
3382 return false;
3383
3384 return lookup_page_cgroup_used(page) != NULL;
3385}
3386
3387void mem_cgroup_print_bad_page(struct page *page)
3388{
3389 struct page_cgroup *pc;
3390
3391 pc = lookup_page_cgroup_used(page);
3392 if (pc) {
3393 int ret = -1;
3394 char *path;
3395
3396 printk(KERN_ALERT "pc:%p pc->flags:%lx pc->mem_cgroup:%p",
3397 pc, pc->flags, pc->mem_cgroup);
3398
3399 path = kmalloc(PATH_MAX, GFP_KERNEL);
3400 if (path) {
3401 rcu_read_lock();
3402 ret = cgroup_path(pc->mem_cgroup->css.cgroup,
3403 path, PATH_MAX);
3404 rcu_read_unlock();
3405 }
3406
3407 printk(KERN_CONT "(%s)\n",
3408 (ret < 0) ? "cannot get the path" : path);
3409 kfree(path);
3410 }
3411}
3412#endif
3413
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003414static DEFINE_MUTEX(set_limit_mutex);
3415
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08003416static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003417 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003418{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003419 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003420 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003421 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003422 int children = mem_cgroup_count_children(memcg);
3423 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003424 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003425
3426 /*
3427 * For keeping hierarchical_reclaim simple, how long we should retry
3428 * is depends on callers. We set our retry-count to be function
3429 * of # of children which we should visit in this loop.
3430 */
3431 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
3432
3433 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003434
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003435 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003436 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003437 if (signal_pending(current)) {
3438 ret = -EINTR;
3439 break;
3440 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003441 /*
3442 * Rather than hide all in some function, I do this in
3443 * open coded manner. You see what this really does.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003444 * We have to guarantee memcg->res.limit < memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003445 */
3446 mutex_lock(&set_limit_mutex);
3447 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3448 if (memswlimit < val) {
3449 ret = -EINVAL;
3450 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003451 break;
3452 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003453
3454 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3455 if (memlimit < val)
3456 enlarge = 1;
3457
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003458 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003459 if (!ret) {
3460 if (memswlimit == val)
3461 memcg->memsw_is_minimum = true;
3462 else
3463 memcg->memsw_is_minimum = false;
3464 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003465 mutex_unlock(&set_limit_mutex);
3466
3467 if (!ret)
3468 break;
3469
Bob Liuaa20d482009-12-15 16:47:14 -08003470 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Ying Han0ae5e892011-05-26 16:25:25 -07003471 MEM_CGROUP_RECLAIM_SHRINK,
3472 NULL);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003473 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
3474 /* Usage is reduced ? */
3475 if (curusage >= oldusage)
3476 retry_count--;
3477 else
3478 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003479 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003480 if (!ret && enlarge)
3481 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08003482
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003483 return ret;
3484}
3485
Li Zefan338c8432009-06-17 16:27:15 -07003486static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
3487 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003488{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003489 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003490 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003491 int children = mem_cgroup_count_children(memcg);
3492 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003493 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003494
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003495 /* see mem_cgroup_resize_res_limit */
3496 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
3497 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003498 while (retry_count) {
3499 if (signal_pending(current)) {
3500 ret = -EINTR;
3501 break;
3502 }
3503 /*
3504 * Rather than hide all in some function, I do this in
3505 * open coded manner. You see what this really does.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003506 * We have to guarantee memcg->res.limit < memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003507 */
3508 mutex_lock(&set_limit_mutex);
3509 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3510 if (memlimit > val) {
3511 ret = -EINVAL;
3512 mutex_unlock(&set_limit_mutex);
3513 break;
3514 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003515 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3516 if (memswlimit < val)
3517 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003518 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003519 if (!ret) {
3520 if (memlimit == val)
3521 memcg->memsw_is_minimum = true;
3522 else
3523 memcg->memsw_is_minimum = false;
3524 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003525 mutex_unlock(&set_limit_mutex);
3526
3527 if (!ret)
3528 break;
3529
Balbir Singh4e416952009-09-23 15:56:39 -07003530 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh75822b42009-09-23 15:56:38 -07003531 MEM_CGROUP_RECLAIM_NOSWAP |
Ying Han0ae5e892011-05-26 16:25:25 -07003532 MEM_CGROUP_RECLAIM_SHRINK,
3533 NULL);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003534 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003535 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003536 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003537 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003538 else
3539 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003540 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003541 if (!ret && enlarge)
3542 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003543 return ret;
3544}
3545
Balbir Singh4e416952009-09-23 15:56:39 -07003546unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
Ying Han0ae5e892011-05-26 16:25:25 -07003547 gfp_t gfp_mask,
3548 unsigned long *total_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07003549{
3550 unsigned long nr_reclaimed = 0;
3551 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
3552 unsigned long reclaimed;
3553 int loop = 0;
3554 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003555 unsigned long long excess;
Ying Han0ae5e892011-05-26 16:25:25 -07003556 unsigned long nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003557
3558 if (order > 0)
3559 return 0;
3560
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07003561 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
Balbir Singh4e416952009-09-23 15:56:39 -07003562 /*
3563 * This loop can run a while, specially if mem_cgroup's continuously
3564 * keep exceeding their soft limit and putting the system under
3565 * pressure
3566 */
3567 do {
3568 if (next_mz)
3569 mz = next_mz;
3570 else
3571 mz = mem_cgroup_largest_soft_limit_node(mctz);
3572 if (!mz)
3573 break;
3574
Ying Han0ae5e892011-05-26 16:25:25 -07003575 nr_scanned = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07003576 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
3577 gfp_mask,
Ying Han0ae5e892011-05-26 16:25:25 -07003578 MEM_CGROUP_RECLAIM_SOFT,
3579 &nr_scanned);
Balbir Singh4e416952009-09-23 15:56:39 -07003580 nr_reclaimed += reclaimed;
Ying Han0ae5e892011-05-26 16:25:25 -07003581 *total_scanned += nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003582 spin_lock(&mctz->lock);
3583
3584 /*
3585 * If we failed to reclaim anything from this memory cgroup
3586 * it is time to move on to the next cgroup
3587 */
3588 next_mz = NULL;
3589 if (!reclaimed) {
3590 do {
3591 /*
3592 * Loop until we find yet another one.
3593 *
3594 * By the time we get the soft_limit lock
3595 * again, someone might have aded the
3596 * group back on the RB tree. Iterate to
3597 * make sure we get a different mem.
3598 * mem_cgroup_largest_soft_limit_node returns
3599 * NULL if no other cgroup is present on
3600 * the tree
3601 */
3602 next_mz =
3603 __mem_cgroup_largest_soft_limit_node(mctz);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003604 if (next_mz == mz)
Balbir Singh4e416952009-09-23 15:56:39 -07003605 css_put(&next_mz->mem->css);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003606 else /* next_mz == NULL or other memcg */
Balbir Singh4e416952009-09-23 15:56:39 -07003607 break;
3608 } while (1);
3609 }
Balbir Singh4e416952009-09-23 15:56:39 -07003610 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003611 excess = res_counter_soft_limit_excess(&mz->mem->res);
Balbir Singh4e416952009-09-23 15:56:39 -07003612 /*
3613 * One school of thought says that we should not add
3614 * back the node to the tree if reclaim returns 0.
3615 * But our reclaim could return 0, simply because due
3616 * to priority we are exposing a smaller subset of
3617 * memory to reclaim from. Consider this as a longer
3618 * term TODO.
3619 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003620 /* If excess == 0, no tree ops */
3621 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07003622 spin_unlock(&mctz->lock);
3623 css_put(&mz->mem->css);
3624 loop++;
3625 /*
3626 * Could not reclaim anything and there are no more
3627 * mem cgroups to try or we seem to be looping without
3628 * reclaiming anything.
3629 */
3630 if (!nr_reclaimed &&
3631 (next_mz == NULL ||
3632 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3633 break;
3634 } while (!nr_reclaimed);
3635 if (next_mz)
3636 css_put(&next_mz->mem->css);
3637 return nr_reclaimed;
3638}
3639
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003640/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003641 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003642 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
3643 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003644static int mem_cgroup_force_empty_list(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003645 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003646{
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003647 struct zone *zone;
3648 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003649 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003650 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003651 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003652 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003653
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003654 zone = &NODE_DATA(node)->node_zones[zid];
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003655 mz = mem_cgroup_zoneinfo(memcg, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07003656 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003657
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003658 loop = MEM_CGROUP_ZSTAT(mz, lru);
3659 /* give some margin against EBUSY etc...*/
3660 loop += 256;
3661 busy = NULL;
3662 while (loop--) {
Johannes Weiner5564e882011-03-23 16:42:29 -07003663 struct page *page;
3664
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003665 ret = 0;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003666 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003667 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003668 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003669 break;
3670 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003671 pc = list_entry(list->prev, struct page_cgroup, lru);
3672 if (busy == pc) {
3673 list_move(&pc->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08003674 busy = NULL;
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003675 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003676 continue;
3677 }
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003678 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003679
Johannes Weiner6b3ae582011-03-23 16:42:30 -07003680 page = lookup_cgroup_page(pc);
Johannes Weiner5564e882011-03-23 16:42:29 -07003681
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003682 ret = mem_cgroup_move_parent(page, pc, memcg, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003683 if (ret == -ENOMEM)
3684 break;
3685
3686 if (ret == -EBUSY || ret == -EINVAL) {
3687 /* found lock contention or "pc" is obsolete. */
3688 busy = pc;
3689 cond_resched();
3690 } else
3691 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003692 }
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003693
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003694 if (!ret && !list_empty(list))
3695 return -EBUSY;
3696 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003697}
3698
3699/*
3700 * make mem_cgroup's charge to be 0 if there is no task.
3701 * This enables deleting this mem_cgroup.
3702 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003703static int mem_cgroup_force_empty(struct mem_cgroup *memcg, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003704{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003705 int ret;
3706 int node, zid, shrink;
3707 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003708 struct cgroup *cgrp = memcg->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003709
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003710 css_get(&memcg->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003711
3712 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003713 /* should free all ? */
3714 if (free_all)
3715 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003716move_account:
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003717 do {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003718 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003719 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003720 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003721 ret = -EINTR;
3722 if (signal_pending(current))
3723 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003724 /* This is for making all *used* pages to be on LRU. */
3725 lru_add_drain_all();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003726 drain_all_stock_sync(memcg);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003727 ret = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003728 mem_cgroup_start_move(memcg);
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08003729 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003730 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07003731 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003732 for_each_lru(l) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003733 ret = mem_cgroup_force_empty_list(memcg,
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003734 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003735 if (ret)
3736 break;
3737 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003738 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003739 if (ret)
3740 break;
3741 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003742 mem_cgroup_end_move(memcg);
3743 memcg_oom_recover(memcg);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003744 /* it seems parent cgroup doesn't have enough mem */
3745 if (ret == -ENOMEM)
3746 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003747 cond_resched();
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003748 /* "ret" should also be checked to ensure all lists are empty. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003749 } while (memcg->res.usage > 0 || ret);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003750out:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003751 css_put(&memcg->css);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003752 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003753
3754try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003755 /* returns EBUSY if there is a task or if we come here twice. */
3756 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003757 ret = -EBUSY;
3758 goto out;
3759 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003760 /* we call try-to-free pages for make this cgroup empty */
3761 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003762 /* try to free all pages in this cgroup */
3763 shrink = 1;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003764 while (nr_retries && memcg->res.usage > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003765 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003766
3767 if (signal_pending(current)) {
3768 ret = -EINTR;
3769 goto out;
3770 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003771 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
Johannes Weiner185efc02011-09-14 16:21:58 -07003772 false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003773 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003774 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003775 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02003776 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003777 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003778
3779 }
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08003780 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003781 /* try move_account...there may be some *locked* pages. */
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003782 goto move_account;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003783}
3784
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003785int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
3786{
3787 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
3788}
3789
3790
Balbir Singh18f59ea2009-01-07 18:08:07 -08003791static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
3792{
3793 return mem_cgroup_from_cont(cont)->use_hierarchy;
3794}
3795
3796static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
3797 u64 val)
3798{
3799 int retval = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003800 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003801 struct cgroup *parent = cont->parent;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003802 struct mem_cgroup *parent_memcg = NULL;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003803
3804 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003805 parent_memcg = mem_cgroup_from_cont(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003806
3807 cgroup_lock();
3808 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003809 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08003810 * in the child subtrees. If it is unset, then the change can
3811 * occur, provided the current cgroup has no children.
3812 *
3813 * For the root cgroup, parent_mem is NULL, we allow value to be
3814 * set if there are no children.
3815 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003816 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08003817 (val == 1 || val == 0)) {
3818 if (list_empty(&cont->children))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003819 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003820 else
3821 retval = -EBUSY;
3822 } else
3823 retval = -EINVAL;
3824 cgroup_unlock();
3825
3826 return retval;
3827}
3828
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003829
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003830static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003831 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003832{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003833 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003834 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003835
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003836 /* Per-cpu values can be negative, use a signed accumulator */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003837 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003838 val += mem_cgroup_read_stat(iter, idx);
3839
3840 if (val < 0) /* race ? */
3841 val = 0;
3842 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003843}
3844
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003845static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003846{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003847 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003848
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003849 if (!mem_cgroup_is_root(memcg)) {
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003850 if (!swap)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003851 return res_counter_read_u64(&memcg->res, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003852 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003853 return res_counter_read_u64(&memcg->memsw, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003854 }
3855
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003856 val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
3857 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003858
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003859 if (swap)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003860 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAPOUT);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003861
3862 return val << PAGE_SHIFT;
3863}
3864
Paul Menage2c3daa72008-04-29 00:59:58 -07003865static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003866{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003867 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003868 u64 val;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003869 int type, name;
3870
3871 type = MEMFILE_TYPE(cft->private);
3872 name = MEMFILE_ATTR(cft->private);
3873 switch (type) {
3874 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003875 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003876 val = mem_cgroup_usage(memcg, false);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003877 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003878 val = res_counter_read_u64(&memcg->res, name);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003879 break;
3880 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003881 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003882 val = mem_cgroup_usage(memcg, true);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003883 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003884 val = res_counter_read_u64(&memcg->memsw, name);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003885 break;
3886 default:
3887 BUG();
3888 break;
3889 }
3890 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003891}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003892/*
3893 * The user of this function is...
3894 * RES_LIMIT.
3895 */
Paul Menage856c13a2008-07-25 01:47:04 -07003896static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
3897 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003898{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003899 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003900 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003901 unsigned long long val;
3902 int ret;
3903
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003904 type = MEMFILE_TYPE(cft->private);
3905 name = MEMFILE_ATTR(cft->private);
3906 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003907 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07003908 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
3909 ret = -EINVAL;
3910 break;
3911 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003912 /* This function does all necessary parse...reuse it */
3913 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003914 if (ret)
3915 break;
3916 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003917 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003918 else
3919 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003920 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07003921 case RES_SOFT_LIMIT:
3922 ret = res_counter_memparse_write_strategy(buffer, &val);
3923 if (ret)
3924 break;
3925 /*
3926 * For memsw, soft limits are hard to implement in terms
3927 * of semantics, for now, we support soft limits for
3928 * control without swap
3929 */
3930 if (type == _MEM)
3931 ret = res_counter_set_soft_limit(&memcg->res, val);
3932 else
3933 ret = -EINVAL;
3934 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003935 default:
3936 ret = -EINVAL; /* should be BUG() ? */
3937 break;
3938 }
3939 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003940}
3941
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08003942static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
3943 unsigned long long *mem_limit, unsigned long long *memsw_limit)
3944{
3945 struct cgroup *cgroup;
3946 unsigned long long min_limit, min_memsw_limit, tmp;
3947
3948 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3949 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3950 cgroup = memcg->css.cgroup;
3951 if (!memcg->use_hierarchy)
3952 goto out;
3953
3954 while (cgroup->parent) {
3955 cgroup = cgroup->parent;
3956 memcg = mem_cgroup_from_cont(cgroup);
3957 if (!memcg->use_hierarchy)
3958 break;
3959 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
3960 min_limit = min(min_limit, tmp);
3961 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3962 min_memsw_limit = min(min_memsw_limit, tmp);
3963 }
3964out:
3965 *mem_limit = min_limit;
3966 *memsw_limit = min_memsw_limit;
3967 return;
3968}
3969
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003970static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003971{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003972 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003973 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003974
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003975 memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003976 type = MEMFILE_TYPE(event);
3977 name = MEMFILE_ATTR(event);
3978 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003979 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003980 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003981 res_counter_reset_max(&memcg->res);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003982 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003983 res_counter_reset_max(&memcg->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003984 break;
3985 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003986 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003987 res_counter_reset_failcnt(&memcg->res);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003988 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003989 res_counter_reset_failcnt(&memcg->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003990 break;
3991 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07003992
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07003993 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003994}
3995
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003996static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
3997 struct cftype *cft)
3998{
3999 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
4000}
4001
Daisuke Nishimura02491442010-03-10 15:22:17 -08004002#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004003static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
4004 struct cftype *cft, u64 val)
4005{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004006 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004007
4008 if (val >= (1 << NR_MOVE_TYPE))
4009 return -EINVAL;
4010 /*
4011 * We check this value several times in both in can_attach() and
4012 * attach(), so we need cgroup lock to prevent this value from being
4013 * inconsistent.
4014 */
4015 cgroup_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004016 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004017 cgroup_unlock();
4018
4019 return 0;
4020}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004021#else
4022static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
4023 struct cftype *cft, u64 val)
4024{
4025 return -ENOSYS;
4026}
4027#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004028
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004029
4030/* For read statistics */
4031enum {
4032 MCS_CACHE,
4033 MCS_RSS,
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08004034 MCS_FILE_MAPPED,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004035 MCS_PGPGIN,
4036 MCS_PGPGOUT,
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004037 MCS_SWAP,
Ying Han456f9982011-05-26 16:25:38 -07004038 MCS_PGFAULT,
4039 MCS_PGMAJFAULT,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004040 MCS_INACTIVE_ANON,
4041 MCS_ACTIVE_ANON,
4042 MCS_INACTIVE_FILE,
4043 MCS_ACTIVE_FILE,
4044 MCS_UNEVICTABLE,
4045 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004046};
4047
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004048struct mcs_total_stat {
4049 s64 stat[NR_MCS_STAT];
4050};
4051
4052struct {
4053 char *local_name;
4054 char *total_name;
4055} memcg_stat_strings[NR_MCS_STAT] = {
4056 {"cache", "total_cache"},
4057 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07004058 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004059 {"pgpgin", "total_pgpgin"},
4060 {"pgpgout", "total_pgpgout"},
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004061 {"swap", "total_swap"},
Ying Han456f9982011-05-26 16:25:38 -07004062 {"pgfault", "total_pgfault"},
4063 {"pgmajfault", "total_pgmajfault"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004064 {"inactive_anon", "total_inactive_anon"},
4065 {"active_anon", "total_active_anon"},
4066 {"inactive_file", "total_inactive_file"},
4067 {"active_file", "total_active_file"},
4068 {"unevictable", "total_unevictable"}
4069};
4070
4071
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004072static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004073mem_cgroup_get_local_stat(struct mem_cgroup *memcg, struct mcs_total_stat *s)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004074{
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004075 s64 val;
4076
4077 /* per cpu stat */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004078 val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_CACHE);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004079 s->stat[MCS_CACHE] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004080 val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_RSS);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004081 s->stat[MCS_RSS] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004082 val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_FILE_MAPPED);
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08004083 s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004084 val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGPGIN);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004085 s->stat[MCS_PGPGIN] += val;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004086 val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGPGOUT);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004087 s->stat[MCS_PGPGOUT] += val;
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004088 if (do_swap_account) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004089 val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_SWAPOUT);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004090 s->stat[MCS_SWAP] += val * PAGE_SIZE;
4091 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004092 val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGFAULT);
Ying Han456f9982011-05-26 16:25:38 -07004093 s->stat[MCS_PGFAULT] += val;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004094 val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGMAJFAULT);
Ying Han456f9982011-05-26 16:25:38 -07004095 s->stat[MCS_PGMAJFAULT] += val;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004096
4097 /* per zone stat */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004098 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_ANON));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004099 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004100 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_ANON));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004101 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004102 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_FILE));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004103 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004104 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_FILE));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004105 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004106 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004107 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004108}
4109
4110static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004111mem_cgroup_get_total_stat(struct mem_cgroup *memcg, struct mcs_total_stat *s)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004112{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004113 struct mem_cgroup *iter;
4114
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004115 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004116 mem_cgroup_get_local_stat(iter, s);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004117}
4118
Ying Han406eb0c2011-05-26 16:25:37 -07004119#ifdef CONFIG_NUMA
4120static int mem_control_numa_stat_show(struct seq_file *m, void *arg)
4121{
4122 int nid;
4123 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
4124 unsigned long node_nr;
4125 struct cgroup *cont = m->private;
4126 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
4127
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004128 total_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07004129 seq_printf(m, "total=%lu", total_nr);
4130 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004131 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07004132 seq_printf(m, " N%d=%lu", nid, node_nr);
4133 }
4134 seq_putc(m, '\n');
4135
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004136 file_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07004137 seq_printf(m, "file=%lu", file_nr);
4138 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004139 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
4140 LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07004141 seq_printf(m, " N%d=%lu", nid, node_nr);
4142 }
4143 seq_putc(m, '\n');
4144
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004145 anon_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07004146 seq_printf(m, "anon=%lu", anon_nr);
4147 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004148 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
4149 LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07004150 seq_printf(m, " N%d=%lu", nid, node_nr);
4151 }
4152 seq_putc(m, '\n');
4153
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004154 unevictable_nr = mem_cgroup_nr_lru_pages(mem_cont, BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07004155 seq_printf(m, "unevictable=%lu", unevictable_nr);
4156 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004157 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
4158 BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07004159 seq_printf(m, " N%d=%lu", nid, node_nr);
4160 }
4161 seq_putc(m, '\n');
4162 return 0;
4163}
4164#endif /* CONFIG_NUMA */
4165
Paul Menagec64745c2008-04-29 01:00:02 -07004166static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
4167 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004168{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004169 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004170 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004171 int i;
4172
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004173 memset(&mystat, 0, sizeof(mystat));
4174 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004175
Ying Han406eb0c2011-05-26 16:25:37 -07004176
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004177 for (i = 0; i < NR_MCS_STAT; i++) {
4178 if (i == MCS_SWAP && !do_swap_account)
4179 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004180 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004181 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004182
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004183 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004184 {
4185 unsigned long long limit, memsw_limit;
4186 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
4187 cb->fill(cb, "hierarchical_memory_limit", limit);
4188 if (do_swap_account)
4189 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
4190 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004191
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004192 memset(&mystat, 0, sizeof(mystat));
4193 mem_cgroup_get_total_stat(mem_cont, &mystat);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004194 for (i = 0; i < NR_MCS_STAT; i++) {
4195 if (i == MCS_SWAP && !do_swap_account)
4196 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004197 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004198 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004199
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004200#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004201 {
4202 int nid, zid;
4203 struct mem_cgroup_per_zone *mz;
4204 unsigned long recent_rotated[2] = {0, 0};
4205 unsigned long recent_scanned[2] = {0, 0};
4206
4207 for_each_online_node(nid)
4208 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
4209 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
4210
4211 recent_rotated[0] +=
4212 mz->reclaim_stat.recent_rotated[0];
4213 recent_rotated[1] +=
4214 mz->reclaim_stat.recent_rotated[1];
4215 recent_scanned[0] +=
4216 mz->reclaim_stat.recent_scanned[0];
4217 recent_scanned[1] +=
4218 mz->reclaim_stat.recent_scanned[1];
4219 }
4220 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
4221 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
4222 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
4223 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
4224 }
4225#endif
4226
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004227 return 0;
4228}
4229
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004230static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
4231{
4232 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4233
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07004234 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004235}
4236
4237static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
4238 u64 val)
4239{
4240 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4241 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08004242
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004243 if (val > 100)
4244 return -EINVAL;
4245
4246 if (cgrp->parent == NULL)
4247 return -EINVAL;
4248
4249 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08004250
4251 cgroup_lock();
4252
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004253 /* If under hierarchy, only empty-root can set this value */
4254 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08004255 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
4256 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004257 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08004258 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004259
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004260 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004261
Li Zefan068b38c2009-01-15 13:51:26 -08004262 cgroup_unlock();
4263
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004264 return 0;
4265}
4266
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004267static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
4268{
4269 struct mem_cgroup_threshold_ary *t;
4270 u64 usage;
4271 int i;
4272
4273 rcu_read_lock();
4274 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004275 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004276 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004277 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004278
4279 if (!t)
4280 goto unlock;
4281
4282 usage = mem_cgroup_usage(memcg, swap);
4283
4284 /*
4285 * current_threshold points to threshold just below usage.
4286 * If it's not true, a threshold was crossed after last
4287 * call of __mem_cgroup_threshold().
4288 */
Phil Carmody5407a562010-05-26 14:42:42 -07004289 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004290
4291 /*
4292 * Iterate backward over array of thresholds starting from
4293 * current_threshold and check if a threshold is crossed.
4294 * If none of thresholds below usage is crossed, we read
4295 * only one element of the array here.
4296 */
4297 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
4298 eventfd_signal(t->entries[i].eventfd, 1);
4299
4300 /* i = current_threshold + 1 */
4301 i++;
4302
4303 /*
4304 * Iterate forward over array of thresholds starting from
4305 * current_threshold+1 and check if a threshold is crossed.
4306 * If none of thresholds above usage is crossed, we read
4307 * only one element of the array here.
4308 */
4309 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
4310 eventfd_signal(t->entries[i].eventfd, 1);
4311
4312 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07004313 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004314unlock:
4315 rcu_read_unlock();
4316}
4317
4318static void mem_cgroup_threshold(struct mem_cgroup *memcg)
4319{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07004320 while (memcg) {
4321 __mem_cgroup_threshold(memcg, false);
4322 if (do_swap_account)
4323 __mem_cgroup_threshold(memcg, true);
4324
4325 memcg = parent_mem_cgroup(memcg);
4326 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004327}
4328
4329static int compare_thresholds(const void *a, const void *b)
4330{
4331 const struct mem_cgroup_threshold *_a = a;
4332 const struct mem_cgroup_threshold *_b = b;
4333
4334 return _a->threshold - _b->threshold;
4335}
4336
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004337static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004338{
4339 struct mem_cgroup_eventfd_list *ev;
4340
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004341 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004342 eventfd_signal(ev->eventfd, 1);
4343 return 0;
4344}
4345
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004346static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004347{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004348 struct mem_cgroup *iter;
4349
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004350 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004351 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004352}
4353
4354static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
4355 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004356{
4357 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004358 struct mem_cgroup_thresholds *thresholds;
4359 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004360 int type = MEMFILE_TYPE(cft->private);
4361 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004362 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004363
4364 ret = res_counter_memparse_write_strategy(args, &threshold);
4365 if (ret)
4366 return ret;
4367
4368 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004369
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004370 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004371 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004372 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004373 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004374 else
4375 BUG();
4376
4377 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4378
4379 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004380 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004381 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4382
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004383 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004384
4385 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004386 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004387 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004388 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004389 ret = -ENOMEM;
4390 goto unlock;
4391 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004392 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004393
4394 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004395 if (thresholds->primary) {
4396 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004397 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004398 }
4399
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004400 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004401 new->entries[size - 1].eventfd = eventfd;
4402 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004403
4404 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004405 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004406 compare_thresholds, NULL);
4407
4408 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004409 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004410 for (i = 0; i < size; i++) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004411 if (new->entries[i].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004412 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004413 * new->current_threshold will not be used until
4414 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004415 * it here.
4416 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004417 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004418 }
4419 }
4420
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004421 /* Free old spare buffer and save old primary buffer as spare */
4422 kfree(thresholds->spare);
4423 thresholds->spare = thresholds->primary;
4424
4425 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004426
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004427 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004428 synchronize_rcu();
4429
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004430unlock:
4431 mutex_unlock(&memcg->thresholds_lock);
4432
4433 return ret;
4434}
4435
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004436static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004437 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004438{
4439 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004440 struct mem_cgroup_thresholds *thresholds;
4441 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004442 int type = MEMFILE_TYPE(cft->private);
4443 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004444 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004445
4446 mutex_lock(&memcg->thresholds_lock);
4447 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004448 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004449 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004450 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004451 else
4452 BUG();
4453
4454 /*
4455 * Something went wrong if we trying to unregister a threshold
4456 * if we don't have thresholds
4457 */
4458 BUG_ON(!thresholds);
4459
4460 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4461
4462 /* Check if a threshold crossed before removing */
4463 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4464
4465 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004466 size = 0;
4467 for (i = 0; i < thresholds->primary->size; i++) {
4468 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004469 size++;
4470 }
4471
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004472 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004473
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004474 /* Set thresholds array to NULL if we don't have thresholds */
4475 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004476 kfree(new);
4477 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004478 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004479 }
4480
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004481 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004482
4483 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004484 new->current_threshold = -1;
4485 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
4486 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004487 continue;
4488
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004489 new->entries[j] = thresholds->primary->entries[i];
4490 if (new->entries[j].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004491 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004492 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004493 * until rcu_assign_pointer(), so it's safe to increment
4494 * it here.
4495 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004496 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004497 }
4498 j++;
4499 }
4500
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004501swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004502 /* Swap primary and spare array */
4503 thresholds->spare = thresholds->primary;
4504 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004505
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004506 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004507 synchronize_rcu();
4508
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004509 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004510}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004511
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004512static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
4513 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
4514{
4515 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4516 struct mem_cgroup_eventfd_list *event;
4517 int type = MEMFILE_TYPE(cft->private);
4518
4519 BUG_ON(type != _OOM_TYPE);
4520 event = kmalloc(sizeof(*event), GFP_KERNEL);
4521 if (!event)
4522 return -ENOMEM;
4523
Michal Hocko1af8efe2011-07-26 16:08:24 -07004524 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004525
4526 event->eventfd = eventfd;
4527 list_add(&event->list, &memcg->oom_notify);
4528
4529 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07004530 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004531 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07004532 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004533
4534 return 0;
4535}
4536
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004537static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004538 struct cftype *cft, struct eventfd_ctx *eventfd)
4539{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004540 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004541 struct mem_cgroup_eventfd_list *ev, *tmp;
4542 int type = MEMFILE_TYPE(cft->private);
4543
4544 BUG_ON(type != _OOM_TYPE);
4545
Michal Hocko1af8efe2011-07-26 16:08:24 -07004546 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004547
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004548 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004549 if (ev->eventfd == eventfd) {
4550 list_del(&ev->list);
4551 kfree(ev);
4552 }
4553 }
4554
Michal Hocko1af8efe2011-07-26 16:08:24 -07004555 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004556}
4557
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004558static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
4559 struct cftype *cft, struct cgroup_map_cb *cb)
4560{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004561 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004562
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004563 cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004564
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004565 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004566 cb->fill(cb, "under_oom", 1);
4567 else
4568 cb->fill(cb, "under_oom", 0);
4569 return 0;
4570}
4571
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004572static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
4573 struct cftype *cft, u64 val)
4574{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004575 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004576 struct mem_cgroup *parent;
4577
4578 /* cannot set to root cgroup and only 0 and 1 are allowed */
4579 if (!cgrp->parent || !((val == 0) || (val == 1)))
4580 return -EINVAL;
4581
4582 parent = mem_cgroup_from_cont(cgrp->parent);
4583
4584 cgroup_lock();
4585 /* oom-kill-disable is a flag for subhierarchy. */
4586 if ((parent->use_hierarchy) ||
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004587 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004588 cgroup_unlock();
4589 return -EINVAL;
4590 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004591 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07004592 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004593 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004594 cgroup_unlock();
4595 return 0;
4596}
4597
Ying Han406eb0c2011-05-26 16:25:37 -07004598#ifdef CONFIG_NUMA
4599static const struct file_operations mem_control_numa_stat_file_operations = {
4600 .read = seq_read,
4601 .llseek = seq_lseek,
4602 .release = single_release,
4603};
4604
4605static int mem_control_numa_stat_open(struct inode *unused, struct file *file)
4606{
4607 struct cgroup *cont = file->f_dentry->d_parent->d_fsdata;
4608
4609 file->f_op = &mem_control_numa_stat_file_operations;
4610 return single_open(file, mem_control_numa_stat_show, cont);
4611}
4612#endif /* CONFIG_NUMA */
4613
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004614static struct cftype mem_cgroup_files[] = {
4615 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004616 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004617 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07004618 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004619 .register_event = mem_cgroup_usage_register_event,
4620 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004621 },
4622 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004623 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004624 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004625 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004626 .read_u64 = mem_cgroup_read,
4627 },
4628 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004629 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004630 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07004631 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07004632 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004633 },
4634 {
Balbir Singh296c81d2009-09-23 15:56:36 -07004635 .name = "soft_limit_in_bytes",
4636 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
4637 .write_string = mem_cgroup_write,
4638 .read_u64 = mem_cgroup_read,
4639 },
4640 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004641 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004642 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004643 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07004644 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004645 },
Balbir Singh8697d332008-02-07 00:13:59 -08004646 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004647 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07004648 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004649 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004650 {
4651 .name = "force_empty",
4652 .trigger = mem_cgroup_force_empty_write,
4653 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08004654 {
4655 .name = "use_hierarchy",
4656 .write_u64 = mem_cgroup_hierarchy_write,
4657 .read_u64 = mem_cgroup_hierarchy_read,
4658 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004659 {
4660 .name = "swappiness",
4661 .read_u64 = mem_cgroup_swappiness_read,
4662 .write_u64 = mem_cgroup_swappiness_write,
4663 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004664 {
4665 .name = "move_charge_at_immigrate",
4666 .read_u64 = mem_cgroup_move_charge_read,
4667 .write_u64 = mem_cgroup_move_charge_write,
4668 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004669 {
4670 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004671 .read_map = mem_cgroup_oom_control_read,
4672 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004673 .register_event = mem_cgroup_oom_register_event,
4674 .unregister_event = mem_cgroup_oom_unregister_event,
4675 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
4676 },
Ying Han406eb0c2011-05-26 16:25:37 -07004677#ifdef CONFIG_NUMA
4678 {
4679 .name = "numa_stat",
4680 .open = mem_control_numa_stat_open,
KAMEZAWA Hiroyuki89577122011-06-15 15:08:41 -07004681 .mode = S_IRUGO,
Ying Han406eb0c2011-05-26 16:25:37 -07004682 },
4683#endif
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004684};
4685
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004686#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4687static struct cftype memsw_cgroup_files[] = {
4688 {
4689 .name = "memsw.usage_in_bytes",
4690 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
4691 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004692 .register_event = mem_cgroup_usage_register_event,
4693 .unregister_event = mem_cgroup_usage_unregister_event,
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004694 },
4695 {
4696 .name = "memsw.max_usage_in_bytes",
4697 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
4698 .trigger = mem_cgroup_reset,
4699 .read_u64 = mem_cgroup_read,
4700 },
4701 {
4702 .name = "memsw.limit_in_bytes",
4703 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
4704 .write_string = mem_cgroup_write,
4705 .read_u64 = mem_cgroup_read,
4706 },
4707 {
4708 .name = "memsw.failcnt",
4709 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
4710 .trigger = mem_cgroup_reset,
4711 .read_u64 = mem_cgroup_read,
4712 },
4713};
4714
4715static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
4716{
4717 if (!do_swap_account)
4718 return 0;
4719 return cgroup_add_files(cont, ss, memsw_cgroup_files,
4720 ARRAY_SIZE(memsw_cgroup_files));
4721};
4722#else
4723static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
4724{
4725 return 0;
4726}
4727#endif
4728
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004729static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004730{
4731 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004732 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07004733 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004734 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004735 /*
4736 * This routine is called against possible nodes.
4737 * But it's BUG to call kmalloc() against offline node.
4738 *
4739 * TODO: this routine can waste much memory for nodes which will
4740 * never be onlined. It's better to use memory hotplug callback
4741 * function.
4742 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004743 if (!node_state(node, N_NORMAL_MEMORY))
4744 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08004745 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004746 if (!pn)
4747 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004748
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004749 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4750 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07004751 for_each_lru(l)
4752 INIT_LIST_HEAD(&mz->lists[l]);
Balbir Singhf64c3f52009-09-23 15:56:37 -07004753 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07004754 mz->on_tree = false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004755 mz->mem = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004756 }
Igor Mammedov0a619e52011-11-02 13:38:21 -07004757 memcg->info.nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004758 return 0;
4759}
4760
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004761static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004762{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004763 kfree(memcg->info.nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004764}
4765
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004766static struct mem_cgroup *mem_cgroup_alloc(void)
4767{
4768 struct mem_cgroup *mem;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004769 int size = sizeof(struct mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004770
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004771 /* Can be very big if MAX_NUMNODES is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08004772 if (size < PAGE_SIZE)
Jesper Juhl17295c82011-01-13 15:47:42 -08004773 mem = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004774 else
Jesper Juhl17295c82011-01-13 15:47:42 -08004775 mem = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004776
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07004777 if (!mem)
4778 return NULL;
4779
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004780 mem->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004781 if (!mem->stat)
4782 goto out_free;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07004783 spin_lock_init(&mem->pcp_counter_lock);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004784 return mem;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004785
4786out_free:
4787 if (size < PAGE_SIZE)
4788 kfree(mem);
4789 else
4790 vfree(mem);
4791 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004792}
4793
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004794/*
4795 * At destroying mem_cgroup, references from swap_cgroup can remain.
4796 * (scanning all at force_empty is too costly...)
4797 *
4798 * Instead of clearing all references at force_empty, we remember
4799 * the number of reference from swap_cgroup and free mem_cgroup when
4800 * it goes down to 0.
4801 *
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004802 * Removal of cgroup itself succeeds regardless of refs from swap.
4803 */
4804
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004805static void __mem_cgroup_free(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004806{
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08004807 int node;
4808
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004809 mem_cgroup_remove_from_trees(memcg);
4810 free_css_id(&mem_cgroup_subsys, &memcg->css);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004811
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08004812 for_each_node_state(node, N_POSSIBLE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004813 free_mem_cgroup_per_zone_info(memcg, node);
KAMEZAWA Hiroyuki08e552c62009-01-07 18:08:01 -08004814
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004815 free_percpu(memcg->stat);
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004816 if (sizeof(struct mem_cgroup) < PAGE_SIZE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004817 kfree(memcg);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004818 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004819 vfree(memcg);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004820}
4821
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004822static void mem_cgroup_get(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004823{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004824 atomic_inc(&memcg->refcnt);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004825}
4826
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004827static void __mem_cgroup_put(struct mem_cgroup *memcg, int count)
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004828{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004829 if (atomic_sub_and_test(count, &memcg->refcnt)) {
4830 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
4831 __mem_cgroup_free(memcg);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004832 if (parent)
4833 mem_cgroup_put(parent);
4834 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004835}
4836
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004837static void mem_cgroup_put(struct mem_cgroup *memcg)
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004838{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004839 __mem_cgroup_put(memcg, 1);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004840}
4841
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004842/*
4843 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
4844 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004845static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004846{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004847 if (!memcg->res.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004848 return NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004849 return mem_cgroup_from_res_counter(memcg->res.parent, res);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004850}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004851
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004852#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4853static void __init enable_swap_cgroup(void)
4854{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004855 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004856 do_swap_account = 1;
4857}
4858#else
4859static void __init enable_swap_cgroup(void)
4860{
4861}
4862#endif
4863
Balbir Singhf64c3f52009-09-23 15:56:37 -07004864static int mem_cgroup_soft_limit_tree_init(void)
4865{
4866 struct mem_cgroup_tree_per_node *rtpn;
4867 struct mem_cgroup_tree_per_zone *rtpz;
4868 int tmp, node, zone;
4869
4870 for_each_node_state(node, N_POSSIBLE) {
4871 tmp = node;
4872 if (!node_state(node, N_NORMAL_MEMORY))
4873 tmp = -1;
4874 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
4875 if (!rtpn)
4876 return 1;
4877
4878 soft_limit_tree.rb_tree_per_node[node] = rtpn;
4879
4880 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4881 rtpz = &rtpn->rb_tree_per_zone[zone];
4882 rtpz->rb_root = RB_ROOT;
4883 spin_lock_init(&rtpz->lock);
4884 }
4885 }
4886 return 0;
4887}
4888
Li Zefan0eb253e2009-01-15 13:51:25 -08004889static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004890mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
4891{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004892 struct mem_cgroup *memcg, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004893 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004894 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004895
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004896 memcg = mem_cgroup_alloc();
4897 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004898 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004899
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004900 for_each_node_state(node, N_POSSIBLE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004901 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004902 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004903
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004904 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004905 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004906 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004907 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004908 parent = NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004909 root_mem_cgroup = memcg;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004910 if (mem_cgroup_soft_limit_tree_init())
4911 goto free_out;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004912 for_each_possible_cpu(cpu) {
4913 struct memcg_stock_pcp *stock =
4914 &per_cpu(memcg_stock, cpu);
4915 INIT_WORK(&stock->work, drain_local_stock);
4916 }
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07004917 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004918 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004919 parent = mem_cgroup_from_cont(cont->parent);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004920 memcg->use_hierarchy = parent->use_hierarchy;
4921 memcg->oom_kill_disable = parent->oom_kill_disable;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004922 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004923
Balbir Singh18f59ea2009-01-07 18:08:07 -08004924 if (parent && parent->use_hierarchy) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004925 res_counter_init(&memcg->res, &parent->res);
4926 res_counter_init(&memcg->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004927 /*
4928 * We increment refcnt of the parent to ensure that we can
4929 * safely access it on res_counter_charge/uncharge.
4930 * This refcnt will be decremented when freeing this
4931 * mem_cgroup(see mem_cgroup_put).
4932 */
4933 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004934 } else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004935 res_counter_init(&memcg->res, NULL);
4936 res_counter_init(&memcg->memsw, NULL);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004937 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004938 memcg->last_scanned_child = 0;
4939 memcg->last_scanned_node = MAX_NUMNODES;
4940 INIT_LIST_HEAD(&memcg->oom_notify);
Balbir Singh6d61ef42009-01-07 18:08:06 -08004941
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004942 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004943 memcg->swappiness = mem_cgroup_swappiness(parent);
4944 atomic_set(&memcg->refcnt, 1);
4945 memcg->move_charge_at_immigrate = 0;
4946 mutex_init(&memcg->thresholds_lock);
4947 return &memcg->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004948free_out:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004949 __mem_cgroup_free(memcg);
Balbir Singh4b3bde42009-09-23 15:56:32 -07004950 root_mem_cgroup = NULL;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004951 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004952}
4953
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004954static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004955 struct cgroup *cont)
4956{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004957 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004958
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004959 return mem_cgroup_force_empty(memcg, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004960}
4961
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004962static void mem_cgroup_destroy(struct cgroup_subsys *ss,
4963 struct cgroup *cont)
4964{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004965 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004966
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004967 mem_cgroup_put(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004968}
4969
4970static int mem_cgroup_populate(struct cgroup_subsys *ss,
4971 struct cgroup *cont)
4972{
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004973 int ret;
4974
4975 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
4976 ARRAY_SIZE(mem_cgroup_files));
4977
4978 if (!ret)
4979 ret = register_memsw_files(cont, ss);
4980 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004981}
4982
Daisuke Nishimura02491442010-03-10 15:22:17 -08004983#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004984/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004985#define PRECHARGE_COUNT_AT_ONCE 256
4986static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004987{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004988 int ret = 0;
4989 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004990 struct mem_cgroup *memcg = mc.to;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004991
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004992 if (mem_cgroup_is_root(memcg)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004993 mc.precharge += count;
4994 /* we don't need css_get for root */
4995 return ret;
4996 }
4997 /* try to charge at once */
4998 if (count > 1) {
4999 struct res_counter *dummy;
5000 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005001 * "memcg" cannot be under rmdir() because we've already checked
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005002 * by cgroup_lock_live_cgroup() that it is not removed and we
5003 * are still under the same cgroup_mutex. So we can postpone
5004 * css_get().
5005 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005006 if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy))
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005007 goto one_by_one;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005008 if (do_swap_account && res_counter_charge(&memcg->memsw,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005009 PAGE_SIZE * count, &dummy)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005010 res_counter_uncharge(&memcg->res, PAGE_SIZE * count);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005011 goto one_by_one;
5012 }
5013 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005014 return ret;
5015 }
5016one_by_one:
5017 /* fall back to one by one charge */
5018 while (count--) {
5019 if (signal_pending(current)) {
5020 ret = -EINTR;
5021 break;
5022 }
5023 if (!batch_count--) {
5024 batch_count = PRECHARGE_COUNT_AT_ONCE;
5025 cond_resched();
5026 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005027 ret = __mem_cgroup_try_charge(NULL,
5028 GFP_KERNEL, 1, &memcg, false);
5029 if (ret || !memcg)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005030 /* mem_cgroup_clear_mc() will do uncharge later */
5031 return -ENOMEM;
5032 mc.precharge++;
5033 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005034 return ret;
5035}
5036
5037/**
5038 * is_target_pte_for_mc - check a pte whether it is valid for move charge
5039 * @vma: the vma the pte to be checked belongs
5040 * @addr: the address corresponding to the pte to be checked
5041 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08005042 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005043 *
5044 * Returns
5045 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
5046 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
5047 * move charge. if @target is not NULL, the page is stored in target->page
5048 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08005049 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
5050 * target for charge migration. if @target is not NULL, the entry is stored
5051 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005052 *
5053 * Called with pte lock held.
5054 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005055union mc_target {
5056 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005057 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005058};
5059
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005060enum mc_target_type {
5061 MC_TARGET_NONE, /* not used */
5062 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08005063 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005064};
5065
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005066static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
5067 unsigned long addr, pte_t ptent)
5068{
5069 struct page *page = vm_normal_page(vma, addr, ptent);
5070
5071 if (!page || !page_mapped(page))
5072 return NULL;
5073 if (PageAnon(page)) {
5074 /* we don't move shared anon */
5075 if (!move_anon() || page_mapcount(page) > 2)
5076 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005077 } else if (!move_file())
5078 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005079 return NULL;
5080 if (!get_page_unless_zero(page))
5081 return NULL;
5082
5083 return page;
5084}
5085
5086static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5087 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5088{
5089 int usage_count;
5090 struct page *page = NULL;
5091 swp_entry_t ent = pte_to_swp_entry(ptent);
5092
5093 if (!move_anon() || non_swap_entry(ent))
5094 return NULL;
5095 usage_count = mem_cgroup_count_swap_user(ent, &page);
5096 if (usage_count > 1) { /* we don't move shared anon */
5097 if (page)
5098 put_page(page);
5099 return NULL;
5100 }
5101 if (do_swap_account)
5102 entry->val = ent.val;
5103
5104 return page;
5105}
5106
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005107static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
5108 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5109{
5110 struct page *page = NULL;
5111 struct inode *inode;
5112 struct address_space *mapping;
5113 pgoff_t pgoff;
5114
5115 if (!vma->vm_file) /* anonymous vma */
5116 return NULL;
5117 if (!move_file())
5118 return NULL;
5119
5120 inode = vma->vm_file->f_path.dentry->d_inode;
5121 mapping = vma->vm_file->f_mapping;
5122 if (pte_none(ptent))
5123 pgoff = linear_page_index(vma, addr);
5124 else /* pte_file(ptent) is true */
5125 pgoff = pte_to_pgoff(ptent);
5126
5127 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005128 page = find_get_page(mapping, pgoff);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005129
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005130#ifdef CONFIG_SWAP
5131 /* shmem/tmpfs may report page out on swap: account for that too. */
5132 if (radix_tree_exceptional_entry(page)) {
5133 swp_entry_t swap = radix_to_swp_entry(page);
5134 if (do_swap_account)
5135 *entry = swap;
5136 page = find_get_page(&swapper_space, swap.val);
5137 }
5138#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005139 return page;
5140}
5141
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005142static int is_target_pte_for_mc(struct vm_area_struct *vma,
5143 unsigned long addr, pte_t ptent, union mc_target *target)
5144{
Daisuke Nishimura02491442010-03-10 15:22:17 -08005145 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005146 struct page_cgroup *pc;
5147 int ret = 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005148 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005149
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005150 if (pte_present(ptent))
5151 page = mc_handle_present_pte(vma, addr, ptent);
5152 else if (is_swap_pte(ptent))
5153 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005154 else if (pte_none(ptent) || pte_file(ptent))
5155 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005156
5157 if (!page && !ent.val)
Daisuke Nishimura02491442010-03-10 15:22:17 -08005158 return 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005159 if (page) {
5160 pc = lookup_page_cgroup(page);
5161 /*
5162 * Do only loose check w/o page_cgroup lock.
5163 * mem_cgroup_move_account() checks the pc is valid or not under
5164 * the lock.
5165 */
5166 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
5167 ret = MC_TARGET_PAGE;
5168 if (target)
5169 target->page = page;
5170 }
5171 if (!ret || !target)
5172 put_page(page);
5173 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005174 /* There is a swap entry and a page doesn't exist or isn't charged */
5175 if (ent.val && !ret &&
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005176 css_id(&mc.from->css) == lookup_swap_cgroup(ent)) {
5177 ret = MC_TARGET_SWAP;
5178 if (target)
5179 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005180 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005181 return ret;
5182}
5183
5184static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
5185 unsigned long addr, unsigned long end,
5186 struct mm_walk *walk)
5187{
5188 struct vm_area_struct *vma = walk->private;
5189 pte_t *pte;
5190 spinlock_t *ptl;
5191
Dave Hansen03319322011-03-22 16:32:56 -07005192 split_huge_page_pmd(walk->mm, pmd);
5193
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005194 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5195 for (; addr != end; pte++, addr += PAGE_SIZE)
5196 if (is_target_pte_for_mc(vma, addr, *pte, NULL))
5197 mc.precharge++; /* increment precharge temporarily */
5198 pte_unmap_unlock(pte - 1, ptl);
5199 cond_resched();
5200
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005201 return 0;
5202}
5203
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005204static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
5205{
5206 unsigned long precharge;
5207 struct vm_area_struct *vma;
5208
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005209 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005210 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5211 struct mm_walk mem_cgroup_count_precharge_walk = {
5212 .pmd_entry = mem_cgroup_count_precharge_pte_range,
5213 .mm = mm,
5214 .private = vma,
5215 };
5216 if (is_vm_hugetlb_page(vma))
5217 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005218 walk_page_range(vma->vm_start, vma->vm_end,
5219 &mem_cgroup_count_precharge_walk);
5220 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005221 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005222
5223 precharge = mc.precharge;
5224 mc.precharge = 0;
5225
5226 return precharge;
5227}
5228
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005229static int mem_cgroup_precharge_mc(struct mm_struct *mm)
5230{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005231 unsigned long precharge = mem_cgroup_count_precharge(mm);
5232
5233 VM_BUG_ON(mc.moving_task);
5234 mc.moving_task = current;
5235 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005236}
5237
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005238/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
5239static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005240{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005241 struct mem_cgroup *from = mc.from;
5242 struct mem_cgroup *to = mc.to;
5243
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005244 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005245 if (mc.precharge) {
5246 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
5247 mc.precharge = 0;
5248 }
5249 /*
5250 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
5251 * we must uncharge here.
5252 */
5253 if (mc.moved_charge) {
5254 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
5255 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005256 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005257 /* we must fixup refcnts and charges */
5258 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005259 /* uncharge swap account from the old cgroup */
5260 if (!mem_cgroup_is_root(mc.from))
5261 res_counter_uncharge(&mc.from->memsw,
5262 PAGE_SIZE * mc.moved_swap);
5263 __mem_cgroup_put(mc.from, mc.moved_swap);
5264
5265 if (!mem_cgroup_is_root(mc.to)) {
5266 /*
5267 * we charged both to->res and to->memsw, so we should
5268 * uncharge to->res.
5269 */
5270 res_counter_uncharge(&mc.to->res,
5271 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005272 }
5273 /* we've already done mem_cgroup_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005274 mc.moved_swap = 0;
5275 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005276 memcg_oom_recover(from);
5277 memcg_oom_recover(to);
5278 wake_up_all(&mc.waitq);
5279}
5280
5281static void mem_cgroup_clear_mc(void)
5282{
5283 struct mem_cgroup *from = mc.from;
5284
5285 /*
5286 * we must clear moving_task before waking up waiters at the end of
5287 * task migration.
5288 */
5289 mc.moving_task = NULL;
5290 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005291 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005292 mc.from = NULL;
5293 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005294 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005295 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005296}
5297
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005298static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
5299 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005300 struct task_struct *p)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005301{
5302 int ret = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005303 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005304
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005305 if (memcg->move_charge_at_immigrate) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005306 struct mm_struct *mm;
5307 struct mem_cgroup *from = mem_cgroup_from_task(p);
5308
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005309 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005310
5311 mm = get_task_mm(p);
5312 if (!mm)
5313 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005314 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005315 if (mm->owner == p) {
5316 VM_BUG_ON(mc.from);
5317 VM_BUG_ON(mc.to);
5318 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005319 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005320 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005321 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005322 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005323 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005324 mc.to = memcg;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005325 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005326 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005327
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005328 ret = mem_cgroup_precharge_mc(mm);
5329 if (ret)
5330 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005331 }
5332 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005333 }
5334 return ret;
5335}
5336
5337static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
5338 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005339 struct task_struct *p)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005340{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005341 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005342}
5343
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005344static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
5345 unsigned long addr, unsigned long end,
5346 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005347{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005348 int ret = 0;
5349 struct vm_area_struct *vma = walk->private;
5350 pte_t *pte;
5351 spinlock_t *ptl;
5352
Dave Hansen03319322011-03-22 16:32:56 -07005353 split_huge_page_pmd(walk->mm, pmd);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005354retry:
5355 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5356 for (; addr != end; addr += PAGE_SIZE) {
5357 pte_t ptent = *(pte++);
5358 union mc_target target;
5359 int type;
5360 struct page *page;
5361 struct page_cgroup *pc;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005362 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005363
5364 if (!mc.precharge)
5365 break;
5366
5367 type = is_target_pte_for_mc(vma, addr, ptent, &target);
5368 switch (type) {
5369 case MC_TARGET_PAGE:
5370 page = target.page;
5371 if (isolate_lru_page(page))
5372 goto put;
5373 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07005374 if (!mem_cgroup_move_account(page, 1, pc,
5375 mc.from, mc.to, false)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005376 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005377 /* we uncharge from mc.from later. */
5378 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005379 }
5380 putback_lru_page(page);
5381put: /* is_target_pte_for_mc() gets the page */
5382 put_page(page);
5383 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005384 case MC_TARGET_SWAP:
5385 ent = target.ent;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005386 if (!mem_cgroup_move_swap_account(ent,
5387 mc.from, mc.to, false)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08005388 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005389 /* we fixup refcnts and charges later. */
5390 mc.moved_swap++;
5391 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08005392 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005393 default:
5394 break;
5395 }
5396 }
5397 pte_unmap_unlock(pte - 1, ptl);
5398 cond_resched();
5399
5400 if (addr != end) {
5401 /*
5402 * We have consumed all precharges we got in can_attach().
5403 * We try charge one by one, but don't do any additional
5404 * charges to mc.to if we have failed in charge once in attach()
5405 * phase.
5406 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005407 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005408 if (!ret)
5409 goto retry;
5410 }
5411
5412 return ret;
5413}
5414
5415static void mem_cgroup_move_charge(struct mm_struct *mm)
5416{
5417 struct vm_area_struct *vma;
5418
5419 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005420retry:
5421 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
5422 /*
5423 * Someone who are holding the mmap_sem might be waiting in
5424 * waitq. So we cancel all extra charges, wake up all waiters,
5425 * and retry. Because we cancel precharges, we might not be able
5426 * to move enough charges, but moving charge is a best-effort
5427 * feature anyway, so it wouldn't be a big problem.
5428 */
5429 __mem_cgroup_clear_mc();
5430 cond_resched();
5431 goto retry;
5432 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005433 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5434 int ret;
5435 struct mm_walk mem_cgroup_move_charge_walk = {
5436 .pmd_entry = mem_cgroup_move_charge_pte_range,
5437 .mm = mm,
5438 .private = vma,
5439 };
5440 if (is_vm_hugetlb_page(vma))
5441 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005442 ret = walk_page_range(vma->vm_start, vma->vm_end,
5443 &mem_cgroup_move_charge_walk);
5444 if (ret)
5445 /*
5446 * means we have consumed all precharges and failed in
5447 * doing additional charge. Just abandon here.
5448 */
5449 break;
5450 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005451 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005452}
5453
Balbir Singh67e465a2008-02-07 00:13:54 -08005454static void mem_cgroup_move_task(struct cgroup_subsys *ss,
5455 struct cgroup *cont,
5456 struct cgroup *old_cont,
Ben Blumf780bdb2011-05-26 16:25:19 -07005457 struct task_struct *p)
Balbir Singh67e465a2008-02-07 00:13:54 -08005458{
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005459 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005460
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005461 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005462 if (mc.to)
5463 mem_cgroup_move_charge(mm);
5464 put_swap_token(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005465 mmput(mm);
5466 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005467 if (mc.to)
5468 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08005469}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005470#else /* !CONFIG_MMU */
5471static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
5472 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005473 struct task_struct *p)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005474{
5475 return 0;
5476}
5477static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
5478 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005479 struct task_struct *p)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005480{
5481}
5482static void mem_cgroup_move_task(struct cgroup_subsys *ss,
5483 struct cgroup *cont,
5484 struct cgroup *old_cont,
Ben Blumf780bdb2011-05-26 16:25:19 -07005485 struct task_struct *p)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005486{
5487}
5488#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08005489
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005490struct cgroup_subsys mem_cgroup_subsys = {
5491 .name = "memory",
5492 .subsys_id = mem_cgroup_subsys_id,
5493 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005494 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005495 .destroy = mem_cgroup_destroy,
5496 .populate = mem_cgroup_populate,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005497 .can_attach = mem_cgroup_can_attach,
5498 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08005499 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005500 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005501 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005502};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005503
5504#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08005505static int __init enable_swap_account(char *s)
5506{
5507 /* consider enabled if no parameter or 1 is given */
Michal Hockoa2c89902011-05-24 17:12:50 -07005508 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08005509 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07005510 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08005511 really_do_swap_account = 0;
5512 return 1;
5513}
Michal Hockoa2c89902011-05-24 17:12:50 -07005514__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005515
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005516#endif