blob: 9e38abdbfd95db8b2358269652055b39fdd7b87f [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 Hiroyuki8c7c6e342009-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 Hiroyuki08e552c2009-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 Hiroyuki453a9bf32011-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 Hiroyuki453a9bf32011-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 Hiroyuki8c7c6e342009-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 Hiroyuki453a9bf32011-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 Hiroyuki8c7c6e342009-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 Hiroyuki8a9478ca2009-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 Hiroyuki8c7c6e342009-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 Hiroyuki8c7c6e342009-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 Hiroyuki8c7c6e342009-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 Hiroyuki8c7c6e342009-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
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -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
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -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 Hiroyuki453a9bf32011-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
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -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{
724 /* threshold event is triggered in finer grain than soft limit */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700725 if (unlikely(__memcg_event_check(memcg, MEM_CGROUP_TARGET_THRESH))) {
726 mem_cgroup_threshold(memcg);
727 __mem_cgroup_target_update(memcg, MEM_CGROUP_TARGET_THRESH);
728 if (unlikely(__memcg_event_check(memcg,
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700729 MEM_CGROUP_TARGET_SOFTLIMIT))) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700730 mem_cgroup_update_tree(memcg, page);
731 __mem_cgroup_target_update(memcg,
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700732 MEM_CGROUP_TARGET_SOFTLIMIT);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700733 }
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700734#if MAX_NUMNODES > 1
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700735 if (unlikely(__memcg_event_check(memcg,
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700736 MEM_CGROUP_TARGET_NUMAINFO))) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700737 atomic_inc(&memcg->numainfo_events);
738 __mem_cgroup_target_update(memcg,
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700739 MEM_CGROUP_TARGET_NUMAINFO);
740 }
741#endif
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800742 }
743}
744
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800745static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800746{
747 return container_of(cgroup_subsys_state(cont,
748 mem_cgroup_subsys_id), struct mem_cgroup,
749 css);
750}
751
Balbir Singhcf475ad2008-04-29 01:00:16 -0700752struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800753{
Balbir Singh31a78f22008-09-28 23:09:31 +0100754 /*
755 * mm_update_next_owner() may clear mm->owner to NULL
756 * if it races with swapoff, page migration, etc.
757 * So this can be called with p == NULL.
758 */
759 if (unlikely(!p))
760 return NULL;
761
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800762 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
763 struct mem_cgroup, css);
764}
765
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700766struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800767{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700768 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700769
770 if (!mm)
771 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800772 /*
773 * Because we have no locks, mm->owner's may be being moved to other
774 * cgroup. We use css_tryget() here even if this looks
775 * pessimistic (rather than adding locks here).
776 */
777 rcu_read_lock();
778 do {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700779 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
780 if (unlikely(!memcg))
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800781 break;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700782 } while (!css_tryget(&memcg->css));
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800783 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700784 return memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800785}
786
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700787/* The caller has to guarantee "mem" exists before calling this */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700788static struct mem_cgroup *mem_cgroup_start_loop(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700789{
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700790 struct cgroup_subsys_state *css;
791 int found;
792
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700793 if (!memcg) /* ROOT cgroup has the smallest ID */
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700794 return root_mem_cgroup; /*css_put/get against root is ignored*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700795 if (!memcg->use_hierarchy) {
796 if (css_tryget(&memcg->css))
797 return memcg;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700798 return NULL;
799 }
800 rcu_read_lock();
801 /*
802 * searching a memory cgroup which has the smallest ID under given
803 * ROOT cgroup. (ID >= 1)
804 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700805 css = css_get_next(&mem_cgroup_subsys, 1, &memcg->css, &found);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700806 if (css && css_tryget(css))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700807 memcg = container_of(css, struct mem_cgroup, css);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700808 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700809 memcg = NULL;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700810 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700811 return memcg;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700812}
813
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700814static struct mem_cgroup *mem_cgroup_get_next(struct mem_cgroup *iter,
815 struct mem_cgroup *root,
816 bool cond)
817{
818 int nextid = css_id(&iter->css) + 1;
819 int found;
820 int hierarchy_used;
821 struct cgroup_subsys_state *css;
822
823 hierarchy_used = iter->use_hierarchy;
824
825 css_put(&iter->css);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700826 /* If no ROOT, walk all, ignore hierarchy */
827 if (!cond || (root && !hierarchy_used))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700828 return NULL;
829
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700830 if (!root)
831 root = root_mem_cgroup;
832
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700833 do {
834 iter = NULL;
835 rcu_read_lock();
836
837 css = css_get_next(&mem_cgroup_subsys, nextid,
838 &root->css, &found);
839 if (css && css_tryget(css))
840 iter = container_of(css, struct mem_cgroup, css);
841 rcu_read_unlock();
842 /* If css is NULL, no more cgroups will be found */
843 nextid = found + 1;
844 } while (css && !iter);
845
846 return iter;
847}
848/*
849 * for_eacn_mem_cgroup_tree() for visiting all cgroup under tree. Please
850 * be careful that "break" loop is not allowed. We have reference count.
851 * Instead of that modify "cond" to be false and "continue" to exit the loop.
852 */
853#define for_each_mem_cgroup_tree_cond(iter, root, cond) \
854 for (iter = mem_cgroup_start_loop(root);\
855 iter != NULL;\
856 iter = mem_cgroup_get_next(iter, root, cond))
857
858#define for_each_mem_cgroup_tree(iter, root) \
859 for_each_mem_cgroup_tree_cond(iter, root, true)
860
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700861#define for_each_mem_cgroup_all(iter) \
862 for_each_mem_cgroup_tree_cond(iter, NULL, true)
863
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700864
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700865static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
Balbir Singh4b3bde42009-09-23 15:56:32 -0700866{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700867 return (memcg == root_mem_cgroup);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700868}
869
Ying Han456f9982011-05-26 16:25:38 -0700870void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
871{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700872 struct mem_cgroup *memcg;
Ying Han456f9982011-05-26 16:25:38 -0700873
874 if (!mm)
875 return;
876
877 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700878 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
879 if (unlikely(!memcg))
Ying Han456f9982011-05-26 16:25:38 -0700880 goto out;
881
882 switch (idx) {
883 case PGMAJFAULT:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700884 mem_cgroup_pgmajfault(memcg, 1);
Ying Han456f9982011-05-26 16:25:38 -0700885 break;
886 case PGFAULT:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700887 mem_cgroup_pgfault(memcg, 1);
Ying Han456f9982011-05-26 16:25:38 -0700888 break;
889 default:
890 BUG();
891 }
892out:
893 rcu_read_unlock();
894}
895EXPORT_SYMBOL(mem_cgroup_count_vm_event);
896
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800897/*
898 * Following LRU functions are allowed to be used without PCG_LOCK.
899 * Operations are called by routine of global LRU independently from memcg.
900 * What we have to take care of here is validness of pc->mem_cgroup.
901 *
902 * Changes to pc->mem_cgroup happens when
903 * 1. charge
904 * 2. moving account
905 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
906 * It is added to LRU before charge.
907 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
908 * When moving account, the page is not on LRU. It's isolated.
909 */
910
911void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800912{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800913 struct page_cgroup *pc;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800914 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700915
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800916 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800917 return;
918 pc = lookup_page_cgroup(page);
919 /* can happen while we handle swapcache. */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700920 if (!TestClearPageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800921 return;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700922 VM_BUG_ON(!pc->mem_cgroup);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800923 /*
924 * We don't check PCG_USED bit. It's cleared when the "page" is finally
925 * removed from global LRU.
926 */
Johannes Weiner97a6c372011-03-23 16:42:27 -0700927 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -0800928 /* huge page split is done under lru_lock. so, we have no races. */
929 MEM_CGROUP_ZSTAT(mz, lru) -= 1 << compound_order(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700930 if (mem_cgroup_is_root(pc->mem_cgroup))
931 return;
932 VM_BUG_ON(list_empty(&pc->lru));
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800933 list_del_init(&pc->lru);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800934}
935
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800936void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800937{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800938 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800939}
940
Minchan Kim3f58a822011-03-22 16:32:53 -0700941/*
942 * Writeback is about to end against a page which has been marked for immediate
943 * reclaim. If it still appears to be reclaimable, move it to the tail of the
944 * inactive list.
945 */
946void mem_cgroup_rotate_reclaimable_page(struct page *page)
947{
948 struct mem_cgroup_per_zone *mz;
949 struct page_cgroup *pc;
950 enum lru_list lru = page_lru(page);
951
952 if (mem_cgroup_disabled())
953 return;
954
955 pc = lookup_page_cgroup(page);
956 /* unused or root page is not rotated. */
957 if (!PageCgroupUsed(pc))
958 return;
959 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
960 smp_rmb();
961 if (mem_cgroup_is_root(pc->mem_cgroup))
962 return;
Johannes Weiner97a6c372011-03-23 16:42:27 -0700963 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
Minchan Kim3f58a822011-03-22 16:32:53 -0700964 list_move_tail(&pc->lru, &mz->lists[lru]);
965}
966
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800967void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800968{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800969 struct mem_cgroup_per_zone *mz;
970 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800971
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800972 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700973 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700974
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800975 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700976 /* unused or root page is not rotated. */
Johannes Weiner713735b2011-01-20 14:44:31 -0800977 if (!PageCgroupUsed(pc))
978 return;
979 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
980 smp_rmb();
981 if (mem_cgroup_is_root(pc->mem_cgroup))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800982 return;
Johannes Weiner97a6c372011-03-23 16:42:27 -0700983 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700984 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800985}
986
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800987void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
988{
989 struct page_cgroup *pc;
990 struct mem_cgroup_per_zone *mz;
991
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800992 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800993 return;
994 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700995 VM_BUG_ON(PageCgroupAcctLRU(pc));
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800996 if (!PageCgroupUsed(pc))
997 return;
Johannes Weiner713735b2011-01-20 14:44:31 -0800998 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
999 smp_rmb();
Johannes Weiner97a6c372011-03-23 16:42:27 -07001000 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08001001 /* huge page split is done under lru_lock. so, we have no races. */
1002 MEM_CGROUP_ZSTAT(mz, lru) += 1 << compound_order(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -07001003 SetPageCgroupAcctLRU(pc);
1004 if (mem_cgroup_is_root(pc->mem_cgroup))
1005 return;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001006 list_add(&pc->lru, &mz->lists[lru]);
1007}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001008
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001009/*
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001010 * At handling SwapCache and other FUSE stuff, pc->mem_cgroup may be changed
1011 * while it's linked to lru because the page may be reused after it's fully
1012 * uncharged. To handle that, unlink page_cgroup from LRU when charge it again.
1013 * It's done under lock_page and expected that zone->lru_lock isnever held.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001014 */
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001015static void mem_cgroup_lru_del_before_commit(struct page *page)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001016{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001017 unsigned long flags;
1018 struct zone *zone = page_zone(page);
1019 struct page_cgroup *pc = lookup_page_cgroup(page);
1020
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001021 /*
1022 * Doing this check without taking ->lru_lock seems wrong but this
1023 * is safe. Because if page_cgroup's USED bit is unset, the page
1024 * will not be added to any memcg's LRU. If page_cgroup's USED bit is
1025 * set, the commit after this will fail, anyway.
1026 * This all charge/uncharge is done under some mutual execustion.
1027 * So, we don't need to taking care of changes in USED bit.
1028 */
1029 if (likely(!PageLRU(page)))
1030 return;
1031
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001032 spin_lock_irqsave(&zone->lru_lock, flags);
1033 /*
1034 * Forget old LRU when this page_cgroup is *not* used. This Used bit
1035 * is guarded by lock_page() because the page is SwapCache.
1036 */
1037 if (!PageCgroupUsed(pc))
1038 mem_cgroup_del_lru_list(page, page_lru(page));
1039 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001040}
1041
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001042static void mem_cgroup_lru_add_after_commit(struct page *page)
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001043{
1044 unsigned long flags;
1045 struct zone *zone = page_zone(page);
1046 struct page_cgroup *pc = lookup_page_cgroup(page);
1047
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001048 /* taking care of that the page is added to LRU while we commit it */
1049 if (likely(!PageLRU(page)))
1050 return;
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001051 spin_lock_irqsave(&zone->lru_lock, flags);
1052 /* link when the page is linked to LRU but page_cgroup isn't */
Balbir Singh4b3bde42009-09-23 15:56:32 -07001053 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001054 mem_cgroup_add_lru_list(page, page_lru(page));
1055 spin_unlock_irqrestore(&zone->lru_lock, flags);
1056}
1057
1058
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001059void mem_cgroup_move_lists(struct page *page,
1060 enum lru_list from, enum lru_list to)
1061{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001062 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001063 return;
1064 mem_cgroup_del_lru_list(page, from);
1065 mem_cgroup_add_lru_list(page, to);
1066}
1067
Michal Hocko3e920412011-07-26 16:08:29 -07001068/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001069 * Checks whether given mem is same or in the root_mem_cgroup's
Michal Hocko3e920412011-07-26 16:08:29 -07001070 * hierarchy subtree
1071 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001072static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1073 struct mem_cgroup *memcg)
Michal Hocko3e920412011-07-26 16:08:29 -07001074{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001075 if (root_memcg != memcg) {
1076 return (root_memcg->use_hierarchy &&
1077 css_is_ancestor(&memcg->css, &root_memcg->css));
Michal Hocko3e920412011-07-26 16:08:29 -07001078 }
1079
1080 return true;
1081}
1082
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001083int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -08001084{
1085 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001086 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001087 struct task_struct *p;
David Rientjes4c4a2212008-02-07 00:14:06 -08001088
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001089 p = find_lock_task_mm(task);
1090 if (!p)
1091 return 0;
1092 curr = try_get_mem_cgroup_from_mm(p->mm);
1093 task_unlock(p);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001094 if (!curr)
1095 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001096 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001097 * We should check use_hierarchy of "memcg" not "curr". Because checking
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001098 * use_hierarchy of "curr" here make this function true if hierarchy is
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001099 * enabled in "curr" and "curr" is a child of "memcg" in *cgroup*
1100 * hierarchy(even if use_hierarchy is disabled in "memcg").
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001101 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001102 ret = mem_cgroup_same_or_subtree(memcg, curr);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001103 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001104 return ret;
1105}
1106
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001107static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001108{
1109 unsigned long active;
1110 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001111 unsigned long gb;
1112 unsigned long inactive_ratio;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001113
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07001114 inactive = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_ANON));
1115 active = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_ANON));
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001116
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001117 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1118 if (gb)
1119 inactive_ratio = int_sqrt(10 * gb);
1120 else
1121 inactive_ratio = 1;
1122
1123 if (present_pages) {
1124 present_pages[0] = inactive;
1125 present_pages[1] = active;
1126 }
1127
1128 return inactive_ratio;
1129}
1130
1131int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
1132{
1133 unsigned long active;
1134 unsigned long inactive;
1135 unsigned long present_pages[2];
1136 unsigned long inactive_ratio;
1137
1138 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
1139
1140 inactive = present_pages[0];
1141 active = present_pages[1];
1142
1143 if (inactive * inactive_ratio < active)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001144 return 1;
1145
1146 return 0;
1147}
1148
Rik van Riel56e49d22009-06-16 15:32:28 -07001149int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
1150{
1151 unsigned long active;
1152 unsigned long inactive;
1153
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07001154 inactive = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_FILE));
1155 active = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_FILE));
Rik van Riel56e49d22009-06-16 15:32:28 -07001156
1157 return (active > inactive);
1158}
1159
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001160struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
1161 struct zone *zone)
1162{
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -07001163 int nid = zone_to_nid(zone);
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001164 int zid = zone_idx(zone);
1165 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
1166
1167 return &mz->reclaim_stat;
1168}
1169
1170struct zone_reclaim_stat *
1171mem_cgroup_get_reclaim_stat_from_page(struct page *page)
1172{
1173 struct page_cgroup *pc;
1174 struct mem_cgroup_per_zone *mz;
1175
1176 if (mem_cgroup_disabled())
1177 return NULL;
1178
1179 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -08001180 if (!PageCgroupUsed(pc))
1181 return NULL;
Johannes Weiner713735b2011-01-20 14:44:31 -08001182 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
1183 smp_rmb();
Johannes Weiner97a6c372011-03-23 16:42:27 -07001184 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001185 return &mz->reclaim_stat;
1186}
1187
Balbir Singh66e17072008-02-07 00:13:56 -08001188unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
1189 struct list_head *dst,
1190 unsigned long *scanned, int order,
Minchan Kim4356f212011-10-31 17:06:47 -07001191 isolate_mode_t mode,
1192 struct zone *z,
Balbir Singh66e17072008-02-07 00:13:56 -08001193 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001194 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -08001195{
1196 unsigned long nr_taken = 0;
1197 struct page *page;
1198 unsigned long scan;
1199 LIST_HEAD(pc_list);
1200 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001201 struct page_cgroup *pc, *tmp;
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -07001202 int nid = zone_to_nid(z);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001203 int zid = zone_idx(z);
1204 struct mem_cgroup_per_zone *mz;
Johannes Weinerb7c46d12009-09-21 17:02:56 -07001205 int lru = LRU_FILE * file + active;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001206 int ret;
Balbir Singh66e17072008-02-07 00:13:56 -08001207
Balbir Singhcf475ad2008-04-29 01:00:16 -07001208 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001209 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001210 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -08001211
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001212 scan = 0;
1213 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -08001214 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001215 break;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001216
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001217 if (unlikely(!PageCgroupUsed(pc)))
1218 continue;
Johannes Weiner5564e882011-03-23 16:42:29 -07001219
Johannes Weiner6b3ae582011-03-23 16:42:30 -07001220 page = lookup_cgroup_page(pc);
Johannes Weiner5564e882011-03-23 16:42:29 -07001221
Hugh Dickins436c65412008-02-07 00:14:12 -08001222 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001223 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001224
Hugh Dickins436c65412008-02-07 00:14:12 -08001225 scan++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001226 ret = __isolate_lru_page(page, mode, file);
1227 switch (ret) {
1228 case 0:
Balbir Singh66e17072008-02-07 00:13:56 -08001229 list_move(&page->lru, dst);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001230 mem_cgroup_del_lru(page);
Rik van Riel2c888cf2011-01-13 15:47:13 -08001231 nr_taken += hpage_nr_pages(page);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001232 break;
1233 case -EBUSY:
1234 /* we don't affect global LRU but rotate in our LRU */
1235 mem_cgroup_rotate_lru_list(page, page_lru(page));
1236 break;
1237 default:
1238 break;
Balbir Singh66e17072008-02-07 00:13:56 -08001239 }
1240 }
1241
Balbir Singh66e17072008-02-07 00:13:56 -08001242 *scanned = scan;
KOSAKI Motohirocc8e9702010-08-09 17:19:57 -07001243
1244 trace_mm_vmscan_memcg_isolate(0, nr_to_scan, scan, nr_taken,
1245 0, 0, 0, mode);
1246
Balbir Singh66e17072008-02-07 00:13:56 -08001247 return nr_taken;
1248}
1249
Balbir Singh6d61ef42009-01-07 18:08:06 -08001250#define mem_cgroup_from_res_counter(counter, member) \
1251 container_of(counter, struct mem_cgroup, member)
1252
Johannes Weiner19942822011-02-01 15:52:43 -08001253/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001254 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
1255 * @mem: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001256 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001257 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001258 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001259 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001260static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001261{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001262 unsigned long long margin;
1263
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001264 margin = res_counter_margin(&memcg->res);
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001265 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001266 margin = min(margin, res_counter_margin(&memcg->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001267 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001268}
1269
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001270int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001271{
1272 struct cgroup *cgrp = memcg->css.cgroup;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001273
1274 /* root ? */
1275 if (cgrp->parent == NULL)
1276 return vm_swappiness;
1277
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001278 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001279}
1280
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001281static void mem_cgroup_start_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001282{
1283 int cpu;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001284
1285 get_online_cpus();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001286 spin_lock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001287 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001288 per_cpu(memcg->stat->count[MEM_CGROUP_ON_MOVE], cpu) += 1;
1289 memcg->nocpu_base.count[MEM_CGROUP_ON_MOVE] += 1;
1290 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001291 put_online_cpus();
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001292
1293 synchronize_rcu();
1294}
1295
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001296static void mem_cgroup_end_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001297{
1298 int cpu;
1299
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001300 if (!memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001301 return;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001302 get_online_cpus();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001303 spin_lock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001304 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001305 per_cpu(memcg->stat->count[MEM_CGROUP_ON_MOVE], cpu) -= 1;
1306 memcg->nocpu_base.count[MEM_CGROUP_ON_MOVE] -= 1;
1307 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001308 put_online_cpus();
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001309}
1310/*
1311 * 2 routines for checking "mem" is under move_account() or not.
1312 *
1313 * mem_cgroup_stealed() - checking a cgroup is mc.from or not. This is used
1314 * for avoiding race in accounting. If true,
1315 * pc->mem_cgroup may be overwritten.
1316 *
1317 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1318 * under hierarchy of moving cgroups. This is for
1319 * waiting at hith-memory prressure caused by "move".
1320 */
1321
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001322static bool mem_cgroup_stealed(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001323{
1324 VM_BUG_ON(!rcu_read_lock_held());
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001325 return this_cpu_read(memcg->stat->count[MEM_CGROUP_ON_MOVE]) > 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001326}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001327
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001328static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001329{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001330 struct mem_cgroup *from;
1331 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001332 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001333 /*
1334 * Unlike task_move routines, we access mc.to, mc.from not under
1335 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1336 */
1337 spin_lock(&mc.lock);
1338 from = mc.from;
1339 to = mc.to;
1340 if (!from)
1341 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001342
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001343 ret = mem_cgroup_same_or_subtree(memcg, from)
1344 || mem_cgroup_same_or_subtree(memcg, to);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001345unlock:
1346 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001347 return ret;
1348}
1349
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001350static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001351{
1352 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001353 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001354 DEFINE_WAIT(wait);
1355 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1356 /* moving charge context might have finished. */
1357 if (mc.moving_task)
1358 schedule();
1359 finish_wait(&mc.waitq, &wait);
1360 return true;
1361 }
1362 }
1363 return false;
1364}
1365
Balbir Singhe2224322009-04-02 16:57:39 -07001366/**
Kirill A. Shutemov6a6135b2010-03-10 15:22:25 -08001367 * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode.
Balbir Singhe2224322009-04-02 16:57:39 -07001368 * @memcg: The memory cgroup that went over limit
1369 * @p: Task that is going to be killed
1370 *
1371 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1372 * enabled
1373 */
1374void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1375{
1376 struct cgroup *task_cgrp;
1377 struct cgroup *mem_cgrp;
1378 /*
1379 * Need a buffer in BSS, can't rely on allocations. The code relies
1380 * on the assumption that OOM is serialized for memory controller.
1381 * If this assumption is broken, revisit this code.
1382 */
1383 static char memcg_name[PATH_MAX];
1384 int ret;
1385
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001386 if (!memcg || !p)
Balbir Singhe2224322009-04-02 16:57:39 -07001387 return;
1388
1389
1390 rcu_read_lock();
1391
1392 mem_cgrp = memcg->css.cgroup;
1393 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1394
1395 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1396 if (ret < 0) {
1397 /*
1398 * Unfortunately, we are unable to convert to a useful name
1399 * But we'll still print out the usage information
1400 */
1401 rcu_read_unlock();
1402 goto done;
1403 }
1404 rcu_read_unlock();
1405
1406 printk(KERN_INFO "Task in %s killed", memcg_name);
1407
1408 rcu_read_lock();
1409 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1410 if (ret < 0) {
1411 rcu_read_unlock();
1412 goto done;
1413 }
1414 rcu_read_unlock();
1415
1416 /*
1417 * Continues from above, so we don't need an KERN_ level
1418 */
1419 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1420done:
1421
1422 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1423 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1424 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1425 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1426 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1427 "failcnt %llu\n",
1428 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1429 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1430 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1431}
1432
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001433/*
1434 * This function returns the number of memcg under hierarchy tree. Returns
1435 * 1(self count) if no children.
1436 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001437static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001438{
1439 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001440 struct mem_cgroup *iter;
1441
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001442 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001443 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001444 return num;
1445}
1446
Balbir Singh6d61ef42009-01-07 18:08:06 -08001447/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001448 * Return the memory (and swap, if configured) limit for a memcg.
1449 */
1450u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
1451{
1452 u64 limit;
1453 u64 memsw;
1454
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001455 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
1456 limit += total_swap_pages << PAGE_SHIFT;
1457
David Rientjesa63d83f2010-08-09 17:19:46 -07001458 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1459 /*
1460 * If memsw is finite and limits the amount of swap space available
1461 * to this memcg, return that limit.
1462 */
1463 return min(limit, memsw);
1464}
1465
1466/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001467 * Visit the first child (need not be the first child as per the ordering
1468 * of the cgroup list, since we track last_scanned_child) of @mem and use
1469 * that to reclaim free pages from.
1470 */
1471static struct mem_cgroup *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001472mem_cgroup_select_victim(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001473{
1474 struct mem_cgroup *ret = NULL;
1475 struct cgroup_subsys_state *css;
1476 int nextid, found;
1477
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001478 if (!root_memcg->use_hierarchy) {
1479 css_get(&root_memcg->css);
1480 ret = root_memcg;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001481 }
1482
1483 while (!ret) {
1484 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001485 nextid = root_memcg->last_scanned_child + 1;
1486 css = css_get_next(&mem_cgroup_subsys, nextid, &root_memcg->css,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001487 &found);
1488 if (css && css_tryget(css))
1489 ret = container_of(css, struct mem_cgroup, css);
1490
1491 rcu_read_unlock();
1492 /* Updates scanning parameter */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001493 if (!css) {
1494 /* this means start scan from ID:1 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001495 root_memcg->last_scanned_child = 0;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001496 } else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001497 root_memcg->last_scanned_child = found;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001498 }
1499
1500 return ret;
1501}
1502
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001503/**
1504 * test_mem_cgroup_node_reclaimable
1505 * @mem: the target memcg
1506 * @nid: the node ID to be checked.
1507 * @noswap : specify true here if the user wants flle only information.
1508 *
1509 * This function returns whether the specified memcg contains any
1510 * reclaimable pages on a node. Returns true if there are any reclaimable
1511 * pages in the node.
1512 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001513static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001514 int nid, bool noswap)
1515{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001516 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001517 return true;
1518 if (noswap || !total_swap_pages)
1519 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001520 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001521 return true;
1522 return false;
1523
1524}
Ying Han889976d2011-05-26 16:25:33 -07001525#if MAX_NUMNODES > 1
1526
1527/*
1528 * Always updating the nodemask is not very good - even if we have an empty
1529 * list or the wrong list here, we can start from some node and traverse all
1530 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1531 *
1532 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001533static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001534{
1535 int nid;
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001536 /*
1537 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1538 * pagein/pageout changes since the last update.
1539 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001540 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001541 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001542 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001543 return;
1544
Ying Han889976d2011-05-26 16:25:33 -07001545 /* make a nodemask where this memcg uses memory from */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001546 memcg->scan_nodes = node_states[N_HIGH_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001547
1548 for_each_node_mask(nid, node_states[N_HIGH_MEMORY]) {
1549
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001550 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1551 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001552 }
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001553
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001554 atomic_set(&memcg->numainfo_events, 0);
1555 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001556}
1557
1558/*
1559 * Selecting a node where we start reclaim from. Because what we need is just
1560 * reducing usage counter, start from anywhere is O,K. Considering
1561 * memory reclaim from current node, there are pros. and cons.
1562 *
1563 * Freeing memory from current node means freeing memory from a node which
1564 * we'll use or we've used. So, it may make LRU bad. And if several threads
1565 * hit limits, it will see a contention on a node. But freeing from remote
1566 * node means more costs for memory reclaim because of memory latency.
1567 *
1568 * Now, we use round-robin. Better algorithm is welcomed.
1569 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001570int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001571{
1572 int node;
1573
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001574 mem_cgroup_may_update_nodemask(memcg);
1575 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001576
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001577 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001578 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001579 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001580 /*
1581 * We call this when we hit limit, not when pages are added to LRU.
1582 * No LRU may hold pages because all pages are UNEVICTABLE or
1583 * memcg is too small and all pages are not on LRU. In that case,
1584 * we use curret node.
1585 */
1586 if (unlikely(node == MAX_NUMNODES))
1587 node = numa_node_id();
1588
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001589 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001590 return node;
1591}
1592
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001593/*
1594 * Check all nodes whether it contains reclaimable pages or not.
1595 * For quick scan, we make use of scan_nodes. This will allow us to skip
1596 * unused nodes. But scan_nodes is lazily updated and may not cotain
1597 * enough new information. We need to do double check.
1598 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001599bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001600{
1601 int nid;
1602
1603 /*
1604 * quick check...making use of scan_node.
1605 * We can skip unused nodes.
1606 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001607 if (!nodes_empty(memcg->scan_nodes)) {
1608 for (nid = first_node(memcg->scan_nodes);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001609 nid < MAX_NUMNODES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001610 nid = next_node(nid, memcg->scan_nodes)) {
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001611
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001612 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001613 return true;
1614 }
1615 }
1616 /*
1617 * Check rest of nodes.
1618 */
1619 for_each_node_state(nid, N_HIGH_MEMORY) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001620 if (node_isset(nid, memcg->scan_nodes))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001621 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001622 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001623 return true;
1624 }
1625 return false;
1626}
1627
Ying Han889976d2011-05-26 16:25:33 -07001628#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001629int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001630{
1631 return 0;
1632}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001633
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001634bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001635{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001636 return test_mem_cgroup_node_reclaimable(memcg, 0, noswap);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001637}
Ying Han889976d2011-05-26 16:25:33 -07001638#endif
1639
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001640/*
1641 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1642 * we reclaimed from, so that we don't end up penalizing one child extensively
1643 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001644 *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001645 * root_memcg is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001646 *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001647 * We give up and return to the caller when we visit root_memcg twice.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001648 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001649 *
1650 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001651 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001652static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_memcg,
Balbir Singh4e416952009-09-23 15:56:39 -07001653 struct zone *zone,
Balbir Singh75822b42009-09-23 15:56:38 -07001654 gfp_t gfp_mask,
Ying Han0ae5e892011-05-26 16:25:25 -07001655 unsigned long reclaim_options,
1656 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001657{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001658 struct mem_cgroup *victim;
1659 int ret, total = 0;
1660 int loop = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001661 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1662 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
Balbir Singh4e416952009-09-23 15:56:39 -07001663 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001664 unsigned long excess;
Johannes Weiner185efc02011-09-14 16:21:58 -07001665 unsigned long nr_scanned;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001666
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001667 excess = res_counter_soft_limit_excess(&root_memcg->res) >> PAGE_SHIFT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001668
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001669 /* If memsw_is_minimum==1, swap-out is of-no-use. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001670 if (!check_soft && !shrink && root_memcg->memsw_is_minimum)
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001671 noswap = true;
1672
Balbir Singh4e416952009-09-23 15:56:39 -07001673 while (1) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001674 victim = mem_cgroup_select_victim(root_memcg);
1675 if (victim == root_memcg) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001676 loop++;
KAMEZAWA Hiroyukifbc29a22011-06-15 15:08:46 -07001677 /*
1678 * We are not draining per cpu cached charges during
1679 * soft limit reclaim because global reclaim doesn't
1680 * care about charges. It tries to free some memory and
1681 * charges will not give any.
1682 */
1683 if (!check_soft && loop >= 1)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001684 drain_all_stock_async(root_memcg);
Balbir Singh4e416952009-09-23 15:56:39 -07001685 if (loop >= 2) {
1686 /*
1687 * If we have not been able to reclaim
1688 * anything, it might because there are
1689 * no reclaimable pages under this hierarchy
1690 */
1691 if (!check_soft || !total) {
1692 css_put(&victim->css);
1693 break;
1694 }
1695 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001696 * We want to do more targeted reclaim.
Balbir Singh4e416952009-09-23 15:56:39 -07001697 * excess >> 2 is not to excessive so as to
1698 * reclaim too much, nor too less that we keep
1699 * coming back to reclaim from this cgroup
1700 */
1701 if (total >= (excess >> 2) ||
1702 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1703 css_put(&victim->css);
1704 break;
1705 }
1706 }
1707 }
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001708 if (!mem_cgroup_reclaimable(victim, noswap)) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001709 /* this cgroup's local usage == 0 */
1710 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001711 continue;
1712 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001713 /* we use swappiness of local cgroup */
Ying Han0ae5e892011-05-26 16:25:25 -07001714 if (check_soft) {
Balbir Singh4e416952009-09-23 15:56:39 -07001715 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
Johannes Weiner185efc02011-09-14 16:21:58 -07001716 noswap, zone, &nr_scanned);
1717 *total_scanned += nr_scanned;
Ying Han0ae5e892011-05-26 16:25:25 -07001718 } else
Balbir Singh4e416952009-09-23 15:56:39 -07001719 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
Johannes Weiner185efc02011-09-14 16:21:58 -07001720 noswap);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001721 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001722 /*
1723 * At shrinking usage, we can't check we should stop here or
1724 * reclaim more. It's depends on callers. last_scanned_child
1725 * will work enough for keeping fairness under tree.
1726 */
1727 if (shrink)
1728 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001729 total += ret;
Balbir Singh4e416952009-09-23 15:56:39 -07001730 if (check_soft) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001731 if (!res_counter_soft_limit_excess(&root_memcg->res))
Balbir Singh4e416952009-09-23 15:56:39 -07001732 return total;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001733 } else if (mem_cgroup_margin(root_memcg))
Johannes Weiner4fd14eb2011-05-26 16:25:35 -07001734 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001735 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001736 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001737}
1738
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001739/*
1740 * Check OOM-Killer is already running under our hierarchy.
1741 * If someone is running, return false.
Michal Hocko1af8efe2011-07-26 16:08:24 -07001742 * Has to be called with memcg_oom_lock
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001743 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001744static bool mem_cgroup_oom_lock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001745{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001746 struct mem_cgroup *iter, *failed = NULL;
1747 bool cond = true;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001748
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001749 for_each_mem_cgroup_tree_cond(iter, memcg, cond) {
Johannes Weiner23751be2011-08-25 15:59:16 -07001750 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001751 /*
1752 * this subtree of our hierarchy is already locked
1753 * so we cannot give a lock.
1754 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07001755 failed = iter;
1756 cond = false;
Johannes Weiner23751be2011-08-25 15:59:16 -07001757 } else
1758 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001759 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001760
Michal Hocko79dfdac2011-07-26 16:08:23 -07001761 if (!failed)
Johannes Weiner23751be2011-08-25 15:59:16 -07001762 return true;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001763
1764 /*
1765 * OK, we failed to lock the whole subtree so we have to clean up
1766 * what we set up to the failing subtree
1767 */
1768 cond = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001769 for_each_mem_cgroup_tree_cond(iter, memcg, cond) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001770 if (iter == failed) {
1771 cond = false;
1772 continue;
1773 }
1774 iter->oom_lock = false;
1775 }
Johannes Weiner23751be2011-08-25 15:59:16 -07001776 return false;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001777}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001778
Michal Hocko79dfdac2011-07-26 16:08:23 -07001779/*
Michal Hocko1af8efe2011-07-26 16:08:24 -07001780 * Has to be called with memcg_oom_lock
Michal Hocko79dfdac2011-07-26 16:08:23 -07001781 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001782static int mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001783{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001784 struct mem_cgroup *iter;
1785
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001786 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001787 iter->oom_lock = false;
1788 return 0;
1789}
1790
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001791static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001792{
1793 struct mem_cgroup *iter;
1794
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001795 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001796 atomic_inc(&iter->under_oom);
1797}
1798
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001799static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001800{
1801 struct mem_cgroup *iter;
1802
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001803 /*
1804 * When a new child is created while the hierarchy is under oom,
1805 * mem_cgroup_oom_lock() may not be called. We have to use
1806 * atomic_add_unless() here.
1807 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001808 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001809 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001810}
1811
Michal Hocko1af8efe2011-07-26 16:08:24 -07001812static DEFINE_SPINLOCK(memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001813static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1814
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001815struct oom_wait_info {
1816 struct mem_cgroup *mem;
1817 wait_queue_t wait;
1818};
1819
1820static int memcg_oom_wake_function(wait_queue_t *wait,
1821 unsigned mode, int sync, void *arg)
1822{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001823 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg,
1824 *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001825 struct oom_wait_info *oom_wait_info;
1826
1827 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001828 oom_wait_memcg = oom_wait_info->mem;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001829
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001830 /*
1831 * Both of oom_wait_info->mem and wake_mem are stable under us.
1832 * Then we can use css_is_ancestor without taking care of RCU.
1833 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001834 if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg)
1835 && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001836 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001837 return autoremove_wake_function(wait, mode, sync, arg);
1838}
1839
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001840static void memcg_wakeup_oom(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001841{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001842 /* for filtering, pass "memcg" as argument. */
1843 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001844}
1845
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001846static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001847{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001848 if (memcg && atomic_read(&memcg->under_oom))
1849 memcg_wakeup_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001850}
1851
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001852/*
1853 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
1854 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001855bool mem_cgroup_handle_oom(struct mem_cgroup *memcg, gfp_t mask)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001856{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001857 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001858 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001859
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001860 owait.mem = memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001861 owait.wait.flags = 0;
1862 owait.wait.func = memcg_oom_wake_function;
1863 owait.wait.private = current;
1864 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001865 need_to_kill = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001866 mem_cgroup_mark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001867
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001868 /* At first, try to OOM lock hierarchy under memcg.*/
Michal Hocko1af8efe2011-07-26 16:08:24 -07001869 spin_lock(&memcg_oom_lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001870 locked = mem_cgroup_oom_lock(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001871 /*
1872 * Even if signal_pending(), we can't quit charge() loop without
1873 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
1874 * under OOM is always welcomed, use TASK_KILLABLE here.
1875 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001876 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001877 if (!locked || memcg->oom_kill_disable)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001878 need_to_kill = false;
1879 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001880 mem_cgroup_oom_notify(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07001881 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001882
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001883 if (need_to_kill) {
1884 finish_wait(&memcg_oom_waitq, &owait.wait);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001885 mem_cgroup_out_of_memory(memcg, mask);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001886 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001887 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001888 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001889 }
Michal Hocko1af8efe2011-07-26 16:08:24 -07001890 spin_lock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001891 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001892 mem_cgroup_oom_unlock(memcg);
1893 memcg_wakeup_oom(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07001894 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001895
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001896 mem_cgroup_unmark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001897
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001898 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
1899 return false;
1900 /* Give chance to dying process */
1901 schedule_timeout(1);
1902 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001903}
1904
Balbir Singhd69b0422009-06-17 16:26:34 -07001905/*
1906 * Currently used to update mapped file statistics, but the routine can be
1907 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001908 *
1909 * Notes: Race condition
1910 *
1911 * We usually use page_cgroup_lock() for accessing page_cgroup member but
1912 * it tends to be costly. But considering some conditions, we doesn't need
1913 * to do so _always_.
1914 *
1915 * Considering "charge", lock_page_cgroup() is not required because all
1916 * file-stat operations happen after a page is attached to radix-tree. There
1917 * are no race with "charge".
1918 *
1919 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
1920 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
1921 * if there are race with "uncharge". Statistics itself is properly handled
1922 * by flags.
1923 *
1924 * Considering "move", this is an only case we see a race. To make the race
1925 * small, we check MEM_CGROUP_ON_MOVE percpu value and detect there are
1926 * possibility of race condition. If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07001927 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001928
Greg Thelen2a7106f2011-01-13 15:47:37 -08001929void mem_cgroup_update_page_stat(struct page *page,
1930 enum mem_cgroup_page_stat_item idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07001931{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001932 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001933 struct page_cgroup *pc = lookup_page_cgroup(page);
1934 bool need_unlock = false;
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08001935 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07001936
Balbir Singhd69b0422009-06-17 16:26:34 -07001937 if (unlikely(!pc))
1938 return;
1939
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001940 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001941 memcg = pc->mem_cgroup;
1942 if (unlikely(!memcg || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001943 goto out;
1944 /* pc->mem_cgroup is unstable ? */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001945 if (unlikely(mem_cgroup_stealed(memcg)) || PageTransHuge(page)) {
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001946 /* take a lock against to access pc->mem_cgroup */
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08001947 move_lock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001948 need_unlock = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001949 memcg = pc->mem_cgroup;
1950 if (!memcg || !PageCgroupUsed(pc))
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001951 goto out;
1952 }
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001953
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001954 switch (idx) {
Greg Thelen2a7106f2011-01-13 15:47:37 -08001955 case MEMCG_NR_FILE_MAPPED:
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001956 if (val > 0)
1957 SetPageCgroupFileMapped(pc);
1958 else if (!page_mapped(page))
KAMEZAWA Hiroyuki0c270f82010-10-27 15:33:39 -07001959 ClearPageCgroupFileMapped(pc);
Greg Thelen2a7106f2011-01-13 15:47:37 -08001960 idx = MEM_CGROUP_STAT_FILE_MAPPED;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001961 break;
1962 default:
1963 BUG();
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07001964 }
Balbir Singhd69b0422009-06-17 16:26:34 -07001965
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001966 this_cpu_add(memcg->stat->count[idx], val);
Greg Thelen2a7106f2011-01-13 15:47:37 -08001967
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001968out:
1969 if (unlikely(need_unlock))
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08001970 move_unlock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001971 rcu_read_unlock();
1972 return;
Balbir Singhd69b0422009-06-17 16:26:34 -07001973}
Greg Thelen2a7106f2011-01-13 15:47:37 -08001974EXPORT_SYMBOL(mem_cgroup_update_page_stat);
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001975
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001976/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001977 * size of first charge trial. "32" comes from vmscan.c's magic value.
1978 * TODO: maybe necessary to use big numbers in big irons.
1979 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001980#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001981struct memcg_stock_pcp {
1982 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07001983 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001984 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07001985 unsigned long flags;
1986#define FLUSHING_CACHED_CHARGE (0)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001987};
1988static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02001989static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001990
1991/*
Johannes Weiner11c9ea42011-03-23 16:42:34 -07001992 * Try to consume stocked charge on this cpu. If success, one page is consumed
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001993 * from local stock and true is returned. If the stock is 0 or charges from a
1994 * cgroup which is not current target, returns false. This stock will be
1995 * refilled.
1996 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001997static bool consume_stock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001998{
1999 struct memcg_stock_pcp *stock;
2000 bool ret = true;
2001
2002 stock = &get_cpu_var(memcg_stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002003 if (memcg == stock->cached && stock->nr_pages)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002004 stock->nr_pages--;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002005 else /* need to call res_counter_charge */
2006 ret = false;
2007 put_cpu_var(memcg_stock);
2008 return ret;
2009}
2010
2011/*
2012 * Returns stocks cached in percpu to res_counter and reset cached information.
2013 */
2014static void drain_stock(struct memcg_stock_pcp *stock)
2015{
2016 struct mem_cgroup *old = stock->cached;
2017
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002018 if (stock->nr_pages) {
2019 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2020
2021 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002022 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002023 res_counter_uncharge(&old->memsw, bytes);
2024 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002025 }
2026 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002027}
2028
2029/*
2030 * This must be called under preempt disabled or must be called by
2031 * a thread which is pinned to local cpu.
2032 */
2033static void drain_local_stock(struct work_struct *dummy)
2034{
2035 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
2036 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002037 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002038}
2039
2040/*
2041 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002042 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002043 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002044static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002045{
2046 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2047
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002048 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002049 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002050 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002051 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002052 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002053 put_cpu_var(memcg_stock);
2054}
2055
2056/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002057 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Michal Hockod38144b2011-07-26 16:08:28 -07002058 * of the hierarchy under it. sync flag says whether we should block
2059 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002060 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002061static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002062{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002063 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002064
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002065 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002066 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002067 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002068 for_each_online_cpu(cpu) {
2069 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002070 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002071
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002072 memcg = stock->cached;
2073 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002074 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002075 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002076 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002077 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2078 if (cpu == curcpu)
2079 drain_local_stock(&stock->work);
2080 else
2081 schedule_work_on(cpu, &stock->work);
2082 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002083 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002084 put_cpu();
Michal Hockod38144b2011-07-26 16:08:28 -07002085
2086 if (!sync)
2087 goto out;
2088
2089 for_each_online_cpu(cpu) {
2090 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002091 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002092 flush_work(&stock->work);
2093 }
2094out:
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002095 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002096}
2097
2098/*
2099 * Tries to drain stocked charges in other cpus. This function is asynchronous
2100 * and just put a work per cpu for draining localy on each cpu. Caller can
2101 * expects some charges will be back to res_counter later but cannot wait for
2102 * it.
2103 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002104static void drain_all_stock_async(struct mem_cgroup *root_memcg)
Michal Hockod38144b2011-07-26 16:08:28 -07002105{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002106 /*
2107 * If someone calls draining, avoid adding more kworker runs.
2108 */
2109 if (!mutex_trylock(&percpu_charge_mutex))
2110 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002111 drain_all_stock(root_memcg, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002112 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002113}
2114
2115/* This is a synchronous drain interface. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002116static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002117{
2118 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002119 mutex_lock(&percpu_charge_mutex);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002120 drain_all_stock(root_memcg, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002121 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002122}
2123
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002124/*
2125 * This function drains percpu counter value from DEAD cpu and
2126 * move it to local cpu. Note that this function can be preempted.
2127 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002128static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002129{
2130 int i;
2131
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002132 spin_lock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002133 for (i = 0; i < MEM_CGROUP_STAT_DATA; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002134 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002135
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002136 per_cpu(memcg->stat->count[i], cpu) = 0;
2137 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002138 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002139 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002140 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002141
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002142 per_cpu(memcg->stat->events[i], cpu) = 0;
2143 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002144 }
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002145 /* need to clear ON_MOVE value, works as a kind of lock. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002146 per_cpu(memcg->stat->count[MEM_CGROUP_ON_MOVE], cpu) = 0;
2147 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002148}
2149
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002150static void synchronize_mem_cgroup_on_move(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002151{
2152 int idx = MEM_CGROUP_ON_MOVE;
2153
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002154 spin_lock(&memcg->pcp_counter_lock);
2155 per_cpu(memcg->stat->count[idx], cpu) = memcg->nocpu_base.count[idx];
2156 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002157}
2158
2159static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002160 unsigned long action,
2161 void *hcpu)
2162{
2163 int cpu = (unsigned long)hcpu;
2164 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002165 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002166
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002167 if ((action == CPU_ONLINE)) {
2168 for_each_mem_cgroup_all(iter)
2169 synchronize_mem_cgroup_on_move(iter, cpu);
2170 return NOTIFY_OK;
2171 }
2172
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002173 if ((action != CPU_DEAD) || action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002174 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002175
2176 for_each_mem_cgroup_all(iter)
2177 mem_cgroup_drain_pcp_counter(iter, cpu);
2178
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002179 stock = &per_cpu(memcg_stock, cpu);
2180 drain_stock(stock);
2181 return NOTIFY_OK;
2182}
2183
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002184
2185/* See __mem_cgroup_try_charge() for details */
2186enum {
2187 CHARGE_OK, /* success */
2188 CHARGE_RETRY, /* need to retry but retry is not bad */
2189 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2190 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
2191 CHARGE_OOM_DIE, /* the current is killed because of OOM */
2192};
2193
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002194static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002195 unsigned int nr_pages, bool oom_check)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002196{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002197 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002198 struct mem_cgroup *mem_over_limit;
2199 struct res_counter *fail_res;
2200 unsigned long flags = 0;
2201 int ret;
2202
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002203 ret = res_counter_charge(&memcg->res, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002204
2205 if (likely(!ret)) {
2206 if (!do_swap_account)
2207 return CHARGE_OK;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002208 ret = res_counter_charge(&memcg->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002209 if (likely(!ret))
2210 return CHARGE_OK;
2211
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002212 res_counter_uncharge(&memcg->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002213 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2214 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2215 } else
2216 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002217 /*
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002218 * nr_pages can be either a huge page (HPAGE_PMD_NR), a batch
2219 * of regular pages (CHARGE_BATCH), or a single regular page (1).
Johannes Weiner9221edb2011-02-01 15:52:42 -08002220 *
2221 * Never reclaim on behalf of optional batching, retry with a
2222 * single page instead.
2223 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002224 if (nr_pages == CHARGE_BATCH)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002225 return CHARGE_RETRY;
2226
2227 if (!(gfp_mask & __GFP_WAIT))
2228 return CHARGE_WOULDBLOCK;
2229
2230 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
Ying Han0ae5e892011-05-26 16:25:25 -07002231 gfp_mask, flags, NULL);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002232 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002233 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002234 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002235 * Even though the limit is exceeded at this point, reclaim
2236 * may have been able to free some pages. Retry the charge
2237 * before killing the task.
2238 *
2239 * Only for regular pages, though: huge pages are rather
2240 * unlikely to succeed so close to the limit, and we fall back
2241 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002242 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002243 if (nr_pages == 1 && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002244 return CHARGE_RETRY;
2245
2246 /*
2247 * At task move, charge accounts can be doubly counted. So, it's
2248 * better to wait until the end of task_move if something is going on.
2249 */
2250 if (mem_cgroup_wait_acct_move(mem_over_limit))
2251 return CHARGE_RETRY;
2252
2253 /* If we don't need to call oom-killer at el, return immediately */
2254 if (!oom_check)
2255 return CHARGE_NOMEM;
2256 /* check OOM */
2257 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask))
2258 return CHARGE_OOM_DIE;
2259
2260 return CHARGE_RETRY;
2261}
2262
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002263/*
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002264 * Unlike exported interface, "oom" parameter is added. if oom==true,
2265 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002266 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002267static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002268 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002269 unsigned int nr_pages,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002270 struct mem_cgroup **ptr,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002271 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002272{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002273 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002274 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002275 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002276 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002277
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002278 /*
2279 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2280 * in system level. So, allow to go ahead dying process in addition to
2281 * MEMDIE process.
2282 */
2283 if (unlikely(test_thread_flag(TIF_MEMDIE)
2284 || fatal_signal_pending(current)))
2285 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002286
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002287 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08002288 * We always charge the cgroup the mm_struct belongs to.
2289 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002290 * thread group leader migrates. It's possible that mm is not
2291 * set, if so charge the init_mm (happens for pagecache usage).
2292 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002293 if (!*ptr && !mm)
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002294 goto bypass;
2295again:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002296 if (*ptr) { /* css should be a valid one */
2297 memcg = *ptr;
2298 VM_BUG_ON(css_is_removed(&memcg->css));
2299 if (mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002300 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002301 if (nr_pages == 1 && consume_stock(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002302 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002303 css_get(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002304 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002305 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002306
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002307 rcu_read_lock();
2308 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002309 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002310 * Because we don't have task_lock(), "p" can exit.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002311 * In that case, "memcg" can point to root or p can be NULL with
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002312 * race with swapoff. Then, we have small risk of mis-accouning.
2313 * But such kind of mis-account by race always happens because
2314 * we don't have cgroup_mutex(). It's overkill and we allo that
2315 * small race, here.
2316 * (*) swapoff at el will charge against mm-struct not against
2317 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002318 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002319 memcg = mem_cgroup_from_task(p);
2320 if (!memcg || mem_cgroup_is_root(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002321 rcu_read_unlock();
2322 goto done;
2323 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002324 if (nr_pages == 1 && consume_stock(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002325 /*
2326 * It seems dagerous to access memcg without css_get().
2327 * But considering how consume_stok works, it's not
2328 * necessary. If consume_stock success, some charges
2329 * from this memcg are cached on this cpu. So, we
2330 * don't need to call css_get()/css_tryget() before
2331 * calling consume_stock().
2332 */
2333 rcu_read_unlock();
2334 goto done;
2335 }
2336 /* after here, we may be blocked. we need to get refcnt */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002337 if (!css_tryget(&memcg->css)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002338 rcu_read_unlock();
2339 goto again;
2340 }
2341 rcu_read_unlock();
2342 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002343
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002344 do {
2345 bool oom_check;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002346
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002347 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002348 if (fatal_signal_pending(current)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002349 css_put(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002350 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002351 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002352
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002353 oom_check = false;
2354 if (oom && !nr_oom_retries) {
2355 oom_check = true;
2356 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2357 }
Balbir Singh6d61ef42009-01-07 18:08:06 -08002358
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002359 ret = mem_cgroup_do_charge(memcg, gfp_mask, batch, oom_check);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002360 switch (ret) {
2361 case CHARGE_OK:
2362 break;
2363 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002364 batch = nr_pages;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002365 css_put(&memcg->css);
2366 memcg = NULL;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002367 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002368 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002369 css_put(&memcg->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002370 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002371 case CHARGE_NOMEM: /* OOM routine works */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002372 if (!oom) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002373 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002374 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002375 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002376 /* If oom, we never return -ENOMEM */
2377 nr_oom_retries--;
2378 break;
2379 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002380 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002381 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08002382 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002383 } while (ret != CHARGE_OK);
2384
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002385 if (batch > nr_pages)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002386 refill_stock(memcg, batch - nr_pages);
2387 css_put(&memcg->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002388done:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002389 *ptr = memcg;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002390 return 0;
2391nomem:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002392 *ptr = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002393 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002394bypass:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002395 *ptr = NULL;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002396 return 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002397}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002398
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002399/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002400 * Somemtimes we have to undo a charge we got by try_charge().
2401 * This function is for that and do uncharge, put css's refcnt.
2402 * gotten by try_charge().
2403 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002404static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002405 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002406{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002407 if (!mem_cgroup_is_root(memcg)) {
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002408 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002409
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002410 res_counter_uncharge(&memcg->res, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002411 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002412 res_counter_uncharge(&memcg->memsw, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002413 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002414}
2415
2416/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002417 * A helper function to get mem_cgroup from ID. must be called under
2418 * rcu_read_lock(). The caller must check css_is_removed() or some if
2419 * it's concern. (dropping refcnt from swap can be called against removed
2420 * memcg.)
2421 */
2422static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2423{
2424 struct cgroup_subsys_state *css;
2425
2426 /* ID 0 is unused ID */
2427 if (!id)
2428 return NULL;
2429 css = css_lookup(&mem_cgroup_subsys, id);
2430 if (!css)
2431 return NULL;
2432 return container_of(css, struct mem_cgroup, css);
2433}
2434
Wu Fengguange42d9d52009-12-16 12:19:59 +01002435struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002436{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002437 struct mem_cgroup *memcg = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002438 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002439 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002440 swp_entry_t ent;
2441
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002442 VM_BUG_ON(!PageLocked(page));
2443
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002444 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002445 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002446 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002447 memcg = pc->mem_cgroup;
2448 if (memcg && !css_tryget(&memcg->css))
2449 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002450 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002451 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002452 id = lookup_swap_cgroup(ent);
2453 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002454 memcg = mem_cgroup_lookup(id);
2455 if (memcg && !css_tryget(&memcg->css))
2456 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002457 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002458 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002459 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002460 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002461}
2462
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002463static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,
Johannes Weiner5564e882011-03-23 16:42:29 -07002464 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002465 unsigned int nr_pages,
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002466 struct page_cgroup *pc,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002467 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002468{
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002469 lock_page_cgroup(pc);
2470 if (unlikely(PageCgroupUsed(pc))) {
2471 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002472 __mem_cgroup_cancel_charge(memcg, nr_pages);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002473 return;
2474 }
2475 /*
2476 * we don't need page_cgroup_lock about tail pages, becase they are not
2477 * accessed by any other context at this point.
2478 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002479 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002480 /*
2481 * We access a page_cgroup asynchronously without lock_page_cgroup().
2482 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2483 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2484 * before USED bit, we need memory barrier here.
2485 * See mem_cgroup_add_lru_list(), etc.
2486 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002487 smp_wmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -07002488 switch (ctype) {
2489 case MEM_CGROUP_CHARGE_TYPE_CACHE:
2490 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
2491 SetPageCgroupCache(pc);
2492 SetPageCgroupUsed(pc);
2493 break;
2494 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
2495 ClearPageCgroupCache(pc);
2496 SetPageCgroupUsed(pc);
2497 break;
2498 default:
2499 break;
2500 }
Hugh Dickins3be91272008-02-07 00:14:19 -08002501
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002502 mem_cgroup_charge_statistics(memcg, PageCgroupCache(pc), nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002503 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002504 /*
2505 * "charge_statistics" updated event counter. Then, check it.
2506 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2507 * if they exceeds softlimit.
2508 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002509 memcg_check_events(memcg, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002510}
2511
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002512#ifdef CONFIG_TRANSPARENT_HUGEPAGE
2513
2514#define PCGF_NOCOPY_AT_SPLIT ((1 << PCG_LOCK) | (1 << PCG_MOVE_LOCK) |\
2515 (1 << PCG_ACCT_LRU) | (1 << PCG_MIGRATION))
2516/*
2517 * Because tail pages are not marked as "used", set it. We're under
2518 * zone->lru_lock, 'splitting on pmd' and compund_lock.
2519 */
2520void mem_cgroup_split_huge_fixup(struct page *head, struct page *tail)
2521{
2522 struct page_cgroup *head_pc = lookup_page_cgroup(head);
2523 struct page_cgroup *tail_pc = lookup_page_cgroup(tail);
2524 unsigned long flags;
2525
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08002526 if (mem_cgroup_disabled())
2527 return;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002528 /*
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002529 * We have no races with charge/uncharge but will have races with
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002530 * page state accounting.
2531 */
2532 move_lock_page_cgroup(head_pc, &flags);
2533
2534 tail_pc->mem_cgroup = head_pc->mem_cgroup;
2535 smp_wmb(); /* see __commit_charge() */
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002536 if (PageCgroupAcctLRU(head_pc)) {
2537 enum lru_list lru;
2538 struct mem_cgroup_per_zone *mz;
2539
2540 /*
2541 * LRU flags cannot be copied because we need to add tail
2542 *.page to LRU by generic call and our hook will be called.
2543 * We hold lru_lock, then, reduce counter directly.
2544 */
2545 lru = page_lru(head);
Johannes Weiner97a6c372011-03-23 16:42:27 -07002546 mz = page_cgroup_zoneinfo(head_pc->mem_cgroup, head);
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002547 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
2548 }
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002549 tail_pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
2550 move_unlock_page_cgroup(head_pc, &flags);
2551}
2552#endif
2553
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002554/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07002555 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07002556 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002557 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002558 * @pc: page_cgroup of the page.
2559 * @from: mem_cgroup which the page is moved from.
2560 * @to: mem_cgroup which the page is moved to. @from != @to.
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002561 * @uncharge: whether we should call uncharge and css_put against @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002562 *
2563 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002564 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002565 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002566 *
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002567 * This function doesn't do "charge" nor css_get to new cgroup. It should be
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002568 * done by a caller(__mem_cgroup_try_charge would be useful). If @uncharge is
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002569 * true, this function does "uncharge" from old cgroup, but it doesn't if
2570 * @uncharge is false, so a caller should do "uncharge".
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002571 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002572static int mem_cgroup_move_account(struct page *page,
2573 unsigned int nr_pages,
2574 struct page_cgroup *pc,
2575 struct mem_cgroup *from,
2576 struct mem_cgroup *to,
2577 bool uncharge)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002578{
Johannes Weinerde3638d2011-03-23 16:42:28 -07002579 unsigned long flags;
2580 int ret;
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002581
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002582 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07002583 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07002584 /*
2585 * The page is isolated from LRU. So, collapse function
2586 * will not handle this page. But page splitting can happen.
2587 * Do this check under compound_page_lock(). The caller should
2588 * hold it.
2589 */
2590 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002591 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07002592 goto out;
2593
2594 lock_page_cgroup(pc);
2595
2596 ret = -EINVAL;
2597 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
2598 goto unlock;
2599
2600 move_lock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002601
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002602 if (PageCgroupFileMapped(pc)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08002603 /* Update mapped_file data for mem_cgroup */
2604 preempt_disable();
2605 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2606 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2607 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07002608 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002609 mem_cgroup_charge_statistics(from, PageCgroupCache(pc), -nr_pages);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002610 if (uncharge)
2611 /* This is not "cancel", but cancel_charge does all we need. */
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002612 __mem_cgroup_cancel_charge(from, nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07002613
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002614 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002615 pc->mem_cgroup = to;
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002616 mem_cgroup_charge_statistics(to, PageCgroupCache(pc), nr_pages);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002617 /*
2618 * We charges against "to" which may not have any tasks. Then, "to"
2619 * can be under rmdir(). But in current implementation, caller of
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08002620 * this function is just force_empty() and move charge, so it's
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002621 * guaranteed that "to" is never removed. So, we don't check rmdir
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08002622 * status here.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002623 */
Johannes Weinerde3638d2011-03-23 16:42:28 -07002624 move_unlock_page_cgroup(pc, &flags);
2625 ret = 0;
2626unlock:
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002627 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002628 /*
2629 * check events
2630 */
Johannes Weiner5564e882011-03-23 16:42:29 -07002631 memcg_check_events(to, page);
2632 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07002633out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002634 return ret;
2635}
2636
2637/*
2638 * move charges to its parent.
2639 */
2640
Johannes Weiner5564e882011-03-23 16:42:29 -07002641static int mem_cgroup_move_parent(struct page *page,
2642 struct page_cgroup *pc,
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002643 struct mem_cgroup *child,
2644 gfp_t gfp_mask)
2645{
2646 struct cgroup *cg = child->css.cgroup;
2647 struct cgroup *pcg = cg->parent;
2648 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002649 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07002650 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002651 int ret;
2652
2653 /* Is ROOT ? */
2654 if (!pcg)
2655 return -EINVAL;
2656
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002657 ret = -EBUSY;
2658 if (!get_page_unless_zero(page))
2659 goto out;
2660 if (isolate_lru_page(page))
2661 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08002662
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002663 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002664
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002665 parent = mem_cgroup_from_cont(pcg);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002666 ret = __mem_cgroup_try_charge(NULL, gfp_mask, nr_pages, &parent, false);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002667 if (ret || !parent)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002668 goto put_back;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002669
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002670 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002671 flags = compound_lock_irqsave(page);
2672
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002673 ret = mem_cgroup_move_account(page, nr_pages, pc, child, parent, true);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002674 if (ret)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002675 __mem_cgroup_cancel_charge(parent, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08002676
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002677 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002678 compound_unlock_irqrestore(page, flags);
Jesper Juhl8dba4742011-01-25 15:07:24 -08002679put_back:
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002680 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002681put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08002682 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002683out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002684 return ret;
2685}
2686
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002687/*
2688 * Charge the memory controller for page usage.
2689 * Return
2690 * 0 if the charge was successful
2691 * < 0 if the cgroup is over its limit
2692 */
2693static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002694 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002695{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002696 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002697 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08002698 struct page_cgroup *pc;
2699 bool oom = true;
2700 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08002701
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002702 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002703 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002704 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08002705 /*
2706 * Never OOM-kill a process for a huge page. The
2707 * fault handler will fall back to regular pages.
2708 */
2709 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002710 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002711
2712 pc = lookup_page_cgroup(page);
Johannes Weineraf4a6622011-03-23 16:42:24 -07002713 BUG_ON(!pc); /* XXX: remove this and move pc lookup into commit */
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002714
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002715 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom);
2716 if (ret || !memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002717 return ret;
2718
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002719 __mem_cgroup_commit_charge(memcg, page, nr_pages, pc, ctype);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002720 return 0;
2721}
2722
2723int mem_cgroup_newpage_charge(struct page *page,
2724 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002725{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002726 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002727 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002728 /*
2729 * If already mapped, we don't have to account.
2730 * If page cache, page->mapping has address_space.
2731 * But page->mapping may have out-of-use anon_vma pointer,
2732 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
2733 * is NULL.
2734 */
2735 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
2736 return 0;
2737 if (unlikely(!mm))
2738 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002739 return mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002740 MEM_CGROUP_CHARGE_TYPE_MAPPED);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002741}
2742
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002743static void
2744__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2745 enum charge_type ctype);
2746
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002747static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002748__mem_cgroup_commit_charge_lrucare(struct page *page, struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002749 enum charge_type ctype)
2750{
2751 struct page_cgroup *pc = lookup_page_cgroup(page);
2752 /*
2753 * In some case, SwapCache, FUSE(splice_buf->radixtree), the page
2754 * is already on LRU. It means the page may on some other page_cgroup's
2755 * LRU. Take care of it.
2756 */
2757 mem_cgroup_lru_del_before_commit(page);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002758 __mem_cgroup_commit_charge(memcg, page, 1, pc, ctype);
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002759 mem_cgroup_lru_add_after_commit(page);
2760 return;
2761}
2762
Balbir Singhe1a1cd52008-02-07 00:14:02 -08002763int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
2764 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08002765{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002766 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002767 int ret;
2768
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002769 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002770 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002771 if (PageCompound(page))
2772 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002773
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002774 if (unlikely(!mm))
Balbir Singh8697d332008-02-07 00:13:59 -08002775 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002776
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002777 if (page_is_file_cache(page)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002778 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, &memcg, true);
2779 if (ret || !memcg)
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002780 return ret;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002781
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002782 /*
2783 * FUSE reuses pages without going through the final
2784 * put that would remove them from the LRU list, make
2785 * sure that they get relinked properly.
2786 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002787 __mem_cgroup_commit_charge_lrucare(page, memcg,
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002788 MEM_CGROUP_CHARGE_TYPE_CACHE);
2789 return ret;
2790 }
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002791 /* shmem */
2792 if (PageSwapCache(page)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002793 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &memcg);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002794 if (!ret)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002795 __mem_cgroup_commit_charge_swapin(page, memcg,
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002796 MEM_CGROUP_CHARGE_TYPE_SHMEM);
2797 } else
2798 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002799 MEM_CGROUP_CHARGE_TYPE_SHMEM);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002800
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002801 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002802}
2803
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002804/*
2805 * While swap-in, try_charge -> commit or cancel, the page is locked.
2806 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02002807 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002808 * "commit()" or removed by "cancel()"
2809 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002810int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
2811 struct page *page,
2812 gfp_t mask, struct mem_cgroup **ptr)
2813{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002814 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002815 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002816
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07002817 *ptr = NULL;
2818
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002819 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002820 return 0;
2821
2822 if (!do_swap_account)
2823 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002824 /*
2825 * A racing thread's fault, or swapoff, may have already updated
Hugh Dickins407f9c82009-12-14 17:59:30 -08002826 * the pte, and even removed page from swap cache: in those cases
2827 * do_swap_page()'s pte_same() test will fail; but there's also a
2828 * KSM case which does need to charge the page.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002829 */
2830 if (!PageSwapCache(page))
Hugh Dickins407f9c82009-12-14 17:59:30 -08002831 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002832 memcg = try_get_mem_cgroup_from_page(page);
2833 if (!memcg)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002834 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002835 *ptr = memcg;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002836 ret = __mem_cgroup_try_charge(NULL, mask, 1, ptr, true);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002837 css_put(&memcg->css);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002838 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002839charge_cur_mm:
2840 if (unlikely(!mm))
2841 mm = &init_mm;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002842 return __mem_cgroup_try_charge(mm, mask, 1, ptr, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002843}
2844
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002845static void
2846__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2847 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002848{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002849 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002850 return;
2851 if (!ptr)
2852 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002853 cgroup_exclude_rmdir(&ptr->css);
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002854
2855 __mem_cgroup_commit_charge_lrucare(page, ptr, ctype);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002856 /*
2857 * Now swap is on-memory. This means this page may be
2858 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002859 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
2860 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
2861 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002862 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002863 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002864 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002865 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002866 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002867
2868 id = swap_cgroup_record(ent, 0);
2869 rcu_read_lock();
2870 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002871 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002872 /*
2873 * This recorded memcg can be obsolete one. So, avoid
2874 * calling css_tryget
2875 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002876 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002877 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002878 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002879 mem_cgroup_put(memcg);
2880 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002881 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002882 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002883 /*
2884 * At swapin, we may charge account against cgroup which has no tasks.
2885 * So, rmdir()->pre_destroy() can be called while we do this charge.
2886 * In that case, we need to call pre_destroy() again. check it here.
2887 */
2888 cgroup_release_and_wakeup_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002889}
2890
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002891void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
2892{
2893 __mem_cgroup_commit_charge_swapin(page, ptr,
2894 MEM_CGROUP_CHARGE_TYPE_MAPPED);
2895}
2896
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002897void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002898{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002899 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002900 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002901 if (!memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002902 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002903 __mem_cgroup_cancel_charge(memcg, 1);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002904}
2905
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002906static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002907 unsigned int nr_pages,
2908 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002909{
2910 struct memcg_batch_info *batch = NULL;
2911 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002912
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002913 /* If swapout, usage of swap doesn't decrease */
2914 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2915 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002916
2917 batch = &current->memcg_batch;
2918 /*
2919 * In usual, we do css_get() when we remember memcg pointer.
2920 * But in this case, we keep res->usage until end of a series of
2921 * uncharges. Then, it's ok to ignore memcg's refcnt.
2922 */
2923 if (!batch->memcg)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002924 batch->memcg = memcg;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002925 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002926 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002927 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002928 * the same cgroup and we have chance to coalesce uncharges.
2929 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
2930 * because we want to do uncharge as soon as possible.
2931 */
2932
2933 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
2934 goto direct_uncharge;
2935
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002936 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08002937 goto direct_uncharge;
2938
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002939 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002940 * In typical case, batch->memcg == mem. This means we can
2941 * merge a series of uncharges to an uncharge of res_counter.
2942 * If not, we uncharge res_counter ony by one.
2943 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002944 if (batch->memcg != memcg)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002945 goto direct_uncharge;
2946 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07002947 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002948 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07002949 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002950 return;
2951direct_uncharge:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002952 res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002953 if (uncharge_memsw)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002954 res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE);
2955 if (unlikely(batch->memcg != memcg))
2956 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002957 return;
2958}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002959
Balbir Singh8697d332008-02-07 00:13:59 -08002960/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002961 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002962 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002963static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002964__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002965{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002966 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002967 unsigned int nr_pages = 1;
2968 struct page_cgroup *pc;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002969
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002970 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002971 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07002972
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002973 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002974 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002975
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002976 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002977 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002978 VM_BUG_ON(!PageTransHuge(page));
2979 }
Balbir Singh8697d332008-02-07 00:13:59 -08002980 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08002981 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08002982 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002983 pc = lookup_page_cgroup(page);
2984 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002985 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002986
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002987 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002988
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002989 memcg = pc->mem_cgroup;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002990
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002991 if (!PageCgroupUsed(pc))
2992 goto unlock_out;
2993
2994 switch (ctype) {
2995 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07002996 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002997 /* See mem_cgroup_prepare_migration() */
2998 if (page_mapped(page) || PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002999 goto unlock_out;
3000 break;
3001 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
3002 if (!PageAnon(page)) { /* Shared memory */
3003 if (page->mapping && !page_is_file_cache(page))
3004 goto unlock_out;
3005 } else if (page_mapped(page)) /* Anon */
3006 goto unlock_out;
3007 break;
3008 default:
3009 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003010 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003011
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003012 mem_cgroup_charge_statistics(memcg, PageCgroupCache(pc), -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003013
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003014 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08003015 /*
3016 * pc->mem_cgroup is not cleared here. It will be accessed when it's
3017 * freed from LRU. This is safe because uncharged page is expected not
3018 * to be reused (freed soon). Exception is SwapCache, it's handled by
3019 * special functions.
3020 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003021
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003022 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003023 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003024 * even after unlock, we have memcg->res.usage here and this memcg
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003025 * will never be freed.
3026 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003027 memcg_check_events(memcg, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003028 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003029 mem_cgroup_swap_statistics(memcg, true);
3030 mem_cgroup_get(memcg);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003031 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003032 if (!mem_cgroup_is_root(memcg))
3033 mem_cgroup_do_uncharge(memcg, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003034
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003035 return memcg;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003036
3037unlock_out:
3038 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003039 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08003040}
3041
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003042void mem_cgroup_uncharge_page(struct page *page)
3043{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003044 /* early check. */
3045 if (page_mapped(page))
3046 return;
3047 if (page->mapping && !PageAnon(page))
3048 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003049 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
3050}
3051
3052void mem_cgroup_uncharge_cache_page(struct page *page)
3053{
3054 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07003055 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003056 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
3057}
3058
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003059/*
3060 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
3061 * In that cases, pages are freed continuously and we can expect pages
3062 * are in the same memcg. All these calls itself limits the number of
3063 * pages freed at once, then uncharge_start/end() is called properly.
3064 * This may be called prural(2) times in a context,
3065 */
3066
3067void mem_cgroup_uncharge_start(void)
3068{
3069 current->memcg_batch.do_batch++;
3070 /* We can do nest. */
3071 if (current->memcg_batch.do_batch == 1) {
3072 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003073 current->memcg_batch.nr_pages = 0;
3074 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003075 }
3076}
3077
3078void mem_cgroup_uncharge_end(void)
3079{
3080 struct memcg_batch_info *batch = &current->memcg_batch;
3081
3082 if (!batch->do_batch)
3083 return;
3084
3085 batch->do_batch--;
3086 if (batch->do_batch) /* If stacked, do nothing. */
3087 return;
3088
3089 if (!batch->memcg)
3090 return;
3091 /*
3092 * This "batch->memcg" is valid without any css_get/put etc...
3093 * bacause we hide charges behind us.
3094 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003095 if (batch->nr_pages)
3096 res_counter_uncharge(&batch->memcg->res,
3097 batch->nr_pages * PAGE_SIZE);
3098 if (batch->memsw_nr_pages)
3099 res_counter_uncharge(&batch->memcg->memsw,
3100 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003101 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003102 /* forget this pointer (for sanity check) */
3103 batch->memcg = NULL;
3104}
3105
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003106#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003107/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003108 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003109 * memcg information is recorded to swap_cgroup of "ent"
3110 */
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07003111void
3112mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003113{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003114 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07003115 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003116
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07003117 if (!swapout) /* this was a swap cache but the swap is unused ! */
3118 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
3119
3120 memcg = __mem_cgroup_uncharge_common(page, ctype);
3121
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003122 /*
3123 * record memcg information, if swapout && memcg != NULL,
3124 * mem_cgroup_get() was called in uncharge().
3125 */
3126 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003127 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003128}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003129#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003130
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003131#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3132/*
3133 * called from swap_entry_free(). remove record in swap_cgroup and
3134 * uncharge "memsw" account.
3135 */
3136void mem_cgroup_uncharge_swap(swp_entry_t ent)
3137{
3138 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003139 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003140
3141 if (!do_swap_account)
3142 return;
3143
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003144 id = swap_cgroup_record(ent, 0);
3145 rcu_read_lock();
3146 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003147 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003148 /*
3149 * We uncharge this because swap is freed.
3150 * This memcg can be obsolete one. We avoid calling css_tryget
3151 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003152 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07003153 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003154 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003155 mem_cgroup_put(memcg);
3156 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003157 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003158}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003159
3160/**
3161 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
3162 * @entry: swap entry to be moved
3163 * @from: mem_cgroup which the entry is moved from
3164 * @to: mem_cgroup which the entry is moved to
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003165 * @need_fixup: whether we should fixup res_counters and refcounts.
Daisuke Nishimura02491442010-03-10 15:22:17 -08003166 *
3167 * It succeeds only when the swap_cgroup's record for this entry is the same
3168 * as the mem_cgroup's id of @from.
3169 *
3170 * Returns 0 on success, -EINVAL on failure.
3171 *
3172 * The caller must have charged to @to, IOW, called res_counter_charge() about
3173 * both res and memsw, and called css_get().
3174 */
3175static int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003176 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003177{
3178 unsigned short old_id, new_id;
3179
3180 old_id = css_id(&from->css);
3181 new_id = css_id(&to->css);
3182
3183 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08003184 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003185 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003186 /*
3187 * This function is only called from task migration context now.
3188 * It postpones res_counter and refcount handling till the end
3189 * of task migration(mem_cgroup_clear_mc()) for performance
3190 * improvement. But we cannot postpone mem_cgroup_get(to)
3191 * because if the process that has been moved to @to does
3192 * swap-in, the refcount of @to might be decreased to 0.
3193 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08003194 mem_cgroup_get(to);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003195 if (need_fixup) {
3196 if (!mem_cgroup_is_root(from))
3197 res_counter_uncharge(&from->memsw, PAGE_SIZE);
3198 mem_cgroup_put(from);
3199 /*
3200 * we charged both to->res and to->memsw, so we should
3201 * uncharge to->res.
3202 */
3203 if (!mem_cgroup_is_root(to))
3204 res_counter_uncharge(&to->res, PAGE_SIZE);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003205 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08003206 return 0;
3207 }
3208 return -EINVAL;
3209}
3210#else
3211static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003212 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003213{
3214 return -EINVAL;
3215}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003216#endif
3217
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003218/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003219 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
3220 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003221 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003222int mem_cgroup_prepare_migration(struct page *page,
Miklos Szeredief6a3c62011-03-22 16:30:52 -07003223 struct page *newpage, struct mem_cgroup **ptr, gfp_t gfp_mask)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003224{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003225 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003226 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003227 enum charge_type ctype;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003228 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003229
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07003230 *ptr = NULL;
3231
Andrea Arcangeliec168512011-01-13 15:46:56 -08003232 VM_BUG_ON(PageTransHuge(page));
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003233 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07003234 return 0;
3235
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003236 pc = lookup_page_cgroup(page);
3237 lock_page_cgroup(pc);
3238 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003239 memcg = pc->mem_cgroup;
3240 css_get(&memcg->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003241 /*
3242 * At migrating an anonymous page, its mapcount goes down
3243 * to 0 and uncharge() will be called. But, even if it's fully
3244 * unmapped, migration may fail and this page has to be
3245 * charged again. We set MIGRATION flag here and delay uncharge
3246 * until end_migration() is called
3247 *
3248 * Corner Case Thinking
3249 * A)
3250 * When the old page was mapped as Anon and it's unmap-and-freed
3251 * while migration was ongoing.
3252 * If unmap finds the old page, uncharge() of it will be delayed
3253 * until end_migration(). If unmap finds a new page, it's
3254 * uncharged when it make mapcount to be 1->0. If unmap code
3255 * finds swap_migration_entry, the new page will not be mapped
3256 * and end_migration() will find it(mapcount==0).
3257 *
3258 * B)
3259 * When the old page was mapped but migraion fails, the kernel
3260 * remaps it. A charge for it is kept by MIGRATION flag even
3261 * if mapcount goes down to 0. We can do remap successfully
3262 * without charging it again.
3263 *
3264 * C)
3265 * The "old" page is under lock_page() until the end of
3266 * migration, so, the old page itself will not be swapped-out.
3267 * If the new page is swapped out before end_migraton, our
3268 * hook to usual swap-out path will catch the event.
3269 */
3270 if (PageAnon(page))
3271 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003272 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003273 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003274 /*
3275 * If the page is not charged at this point,
3276 * we return here.
3277 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003278 if (!memcg)
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003279 return 0;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003280
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003281 *ptr = memcg;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003282 ret = __mem_cgroup_try_charge(NULL, gfp_mask, 1, ptr, false);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003283 css_put(&memcg->css);/* drop extra refcnt */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003284 if (ret || *ptr == NULL) {
3285 if (PageAnon(page)) {
3286 lock_page_cgroup(pc);
3287 ClearPageCgroupMigration(pc);
3288 unlock_page_cgroup(pc);
3289 /*
3290 * The old page may be fully unmapped while we kept it.
3291 */
3292 mem_cgroup_uncharge_page(page);
3293 }
3294 return -ENOMEM;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003295 }
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003296 /*
3297 * We charge new page before it's used/mapped. So, even if unlock_page()
3298 * is called before end_migration, we can catch all events on this new
3299 * page. In the case new page is migrated but not remapped, new page's
3300 * mapcount will be finally 0 and we call uncharge in end_migration().
3301 */
3302 pc = lookup_page_cgroup(newpage);
3303 if (PageAnon(page))
3304 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
3305 else if (page_is_file_cache(page))
3306 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
3307 else
3308 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003309 __mem_cgroup_commit_charge(memcg, page, 1, pc, ctype);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003310 return ret;
3311}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08003312
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003313/* remove redundant charge if migration failed*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003314void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003315 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003316{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003317 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003318 struct page_cgroup *pc;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003319
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003320 if (!memcg)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003321 return;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003322 /* blocks rmdir() */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003323 cgroup_exclude_rmdir(&memcg->css);
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003324 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003325 used = oldpage;
3326 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003327 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003328 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003329 unused = oldpage;
3330 }
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003331 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003332 * We disallowed uncharge of pages under migration because mapcount
3333 * of the page goes down to zero, temporarly.
3334 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003335 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003336 pc = lookup_page_cgroup(oldpage);
3337 lock_page_cgroup(pc);
3338 ClearPageCgroupMigration(pc);
3339 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003340
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003341 __mem_cgroup_uncharge_common(unused, MEM_CGROUP_CHARGE_TYPE_FORCE);
3342
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003343 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003344 * If a page is a file cache, radix-tree replacement is very atomic
3345 * and we can skip this check. When it was an Anon page, its mapcount
3346 * goes down to 0. But because we added MIGRATION flage, it's not
3347 * uncharged yet. There are several case but page->mapcount check
3348 * and USED bit check in mem_cgroup_uncharge_page() will do enough
3349 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003350 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003351 if (PageAnon(used))
3352 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003353 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003354 * At migration, we may charge account against cgroup which has no
3355 * tasks.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003356 * So, rmdir()->pre_destroy() can be called while we do this charge.
3357 * In that case, we need to call pre_destroy() again. check it here.
3358 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003359 cgroup_release_and_wakeup_rmdir(&memcg->css);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003360}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08003361
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003362#ifdef CONFIG_DEBUG_VM
3363static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
3364{
3365 struct page_cgroup *pc;
3366
3367 pc = lookup_page_cgroup(page);
3368 if (likely(pc) && PageCgroupUsed(pc))
3369 return pc;
3370 return NULL;
3371}
3372
3373bool mem_cgroup_bad_page_check(struct page *page)
3374{
3375 if (mem_cgroup_disabled())
3376 return false;
3377
3378 return lookup_page_cgroup_used(page) != NULL;
3379}
3380
3381void mem_cgroup_print_bad_page(struct page *page)
3382{
3383 struct page_cgroup *pc;
3384
3385 pc = lookup_page_cgroup_used(page);
3386 if (pc) {
3387 int ret = -1;
3388 char *path;
3389
3390 printk(KERN_ALERT "pc:%p pc->flags:%lx pc->mem_cgroup:%p",
3391 pc, pc->flags, pc->mem_cgroup);
3392
3393 path = kmalloc(PATH_MAX, GFP_KERNEL);
3394 if (path) {
3395 rcu_read_lock();
3396 ret = cgroup_path(pc->mem_cgroup->css.cgroup,
3397 path, PATH_MAX);
3398 rcu_read_unlock();
3399 }
3400
3401 printk(KERN_CONT "(%s)\n",
3402 (ret < 0) ? "cannot get the path" : path);
3403 kfree(path);
3404 }
3405}
3406#endif
3407
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003408static DEFINE_MUTEX(set_limit_mutex);
3409
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08003410static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003411 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003412{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003413 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003414 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003415 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003416 int children = mem_cgroup_count_children(memcg);
3417 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003418 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003419
3420 /*
3421 * For keeping hierarchical_reclaim simple, how long we should retry
3422 * is depends on callers. We set our retry-count to be function
3423 * of # of children which we should visit in this loop.
3424 */
3425 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
3426
3427 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003428
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003429 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003430 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003431 if (signal_pending(current)) {
3432 ret = -EINTR;
3433 break;
3434 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003435 /*
3436 * Rather than hide all in some function, I do this in
3437 * open coded manner. You see what this really does.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003438 * We have to guarantee memcg->res.limit < memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003439 */
3440 mutex_lock(&set_limit_mutex);
3441 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3442 if (memswlimit < val) {
3443 ret = -EINVAL;
3444 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003445 break;
3446 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003447
3448 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3449 if (memlimit < val)
3450 enlarge = 1;
3451
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003452 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003453 if (!ret) {
3454 if (memswlimit == val)
3455 memcg->memsw_is_minimum = true;
3456 else
3457 memcg->memsw_is_minimum = false;
3458 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003459 mutex_unlock(&set_limit_mutex);
3460
3461 if (!ret)
3462 break;
3463
Bob Liuaa20d482009-12-15 16:47:14 -08003464 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Ying Han0ae5e892011-05-26 16:25:25 -07003465 MEM_CGROUP_RECLAIM_SHRINK,
3466 NULL);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003467 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
3468 /* Usage is reduced ? */
3469 if (curusage >= oldusage)
3470 retry_count--;
3471 else
3472 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003473 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003474 if (!ret && enlarge)
3475 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08003476
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003477 return ret;
3478}
3479
Li Zefan338c8432009-06-17 16:27:15 -07003480static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
3481 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003482{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003483 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003484 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003485 int children = mem_cgroup_count_children(memcg);
3486 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003487 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003488
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003489 /* see mem_cgroup_resize_res_limit */
3490 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
3491 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003492 while (retry_count) {
3493 if (signal_pending(current)) {
3494 ret = -EINTR;
3495 break;
3496 }
3497 /*
3498 * Rather than hide all in some function, I do this in
3499 * open coded manner. You see what this really does.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003500 * We have to guarantee memcg->res.limit < memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003501 */
3502 mutex_lock(&set_limit_mutex);
3503 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3504 if (memlimit > val) {
3505 ret = -EINVAL;
3506 mutex_unlock(&set_limit_mutex);
3507 break;
3508 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003509 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3510 if (memswlimit < val)
3511 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003512 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003513 if (!ret) {
3514 if (memlimit == val)
3515 memcg->memsw_is_minimum = true;
3516 else
3517 memcg->memsw_is_minimum = false;
3518 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003519 mutex_unlock(&set_limit_mutex);
3520
3521 if (!ret)
3522 break;
3523
Balbir Singh4e416952009-09-23 15:56:39 -07003524 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh75822b42009-09-23 15:56:38 -07003525 MEM_CGROUP_RECLAIM_NOSWAP |
Ying Han0ae5e892011-05-26 16:25:25 -07003526 MEM_CGROUP_RECLAIM_SHRINK,
3527 NULL);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003528 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003529 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003530 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003531 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003532 else
3533 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003534 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003535 if (!ret && enlarge)
3536 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003537 return ret;
3538}
3539
Balbir Singh4e416952009-09-23 15:56:39 -07003540unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
Ying Han0ae5e892011-05-26 16:25:25 -07003541 gfp_t gfp_mask,
3542 unsigned long *total_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07003543{
3544 unsigned long nr_reclaimed = 0;
3545 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
3546 unsigned long reclaimed;
3547 int loop = 0;
3548 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003549 unsigned long long excess;
Ying Han0ae5e892011-05-26 16:25:25 -07003550 unsigned long nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003551
3552 if (order > 0)
3553 return 0;
3554
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07003555 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
Balbir Singh4e416952009-09-23 15:56:39 -07003556 /*
3557 * This loop can run a while, specially if mem_cgroup's continuously
3558 * keep exceeding their soft limit and putting the system under
3559 * pressure
3560 */
3561 do {
3562 if (next_mz)
3563 mz = next_mz;
3564 else
3565 mz = mem_cgroup_largest_soft_limit_node(mctz);
3566 if (!mz)
3567 break;
3568
Ying Han0ae5e892011-05-26 16:25:25 -07003569 nr_scanned = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07003570 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
3571 gfp_mask,
Ying Han0ae5e892011-05-26 16:25:25 -07003572 MEM_CGROUP_RECLAIM_SOFT,
3573 &nr_scanned);
Balbir Singh4e416952009-09-23 15:56:39 -07003574 nr_reclaimed += reclaimed;
Ying Han0ae5e892011-05-26 16:25:25 -07003575 *total_scanned += nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003576 spin_lock(&mctz->lock);
3577
3578 /*
3579 * If we failed to reclaim anything from this memory cgroup
3580 * it is time to move on to the next cgroup
3581 */
3582 next_mz = NULL;
3583 if (!reclaimed) {
3584 do {
3585 /*
3586 * Loop until we find yet another one.
3587 *
3588 * By the time we get the soft_limit lock
3589 * again, someone might have aded the
3590 * group back on the RB tree. Iterate to
3591 * make sure we get a different mem.
3592 * mem_cgroup_largest_soft_limit_node returns
3593 * NULL if no other cgroup is present on
3594 * the tree
3595 */
3596 next_mz =
3597 __mem_cgroup_largest_soft_limit_node(mctz);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003598 if (next_mz == mz)
Balbir Singh4e416952009-09-23 15:56:39 -07003599 css_put(&next_mz->mem->css);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003600 else /* next_mz == NULL or other memcg */
Balbir Singh4e416952009-09-23 15:56:39 -07003601 break;
3602 } while (1);
3603 }
Balbir Singh4e416952009-09-23 15:56:39 -07003604 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003605 excess = res_counter_soft_limit_excess(&mz->mem->res);
Balbir Singh4e416952009-09-23 15:56:39 -07003606 /*
3607 * One school of thought says that we should not add
3608 * back the node to the tree if reclaim returns 0.
3609 * But our reclaim could return 0, simply because due
3610 * to priority we are exposing a smaller subset of
3611 * memory to reclaim from. Consider this as a longer
3612 * term TODO.
3613 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003614 /* If excess == 0, no tree ops */
3615 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07003616 spin_unlock(&mctz->lock);
3617 css_put(&mz->mem->css);
3618 loop++;
3619 /*
3620 * Could not reclaim anything and there are no more
3621 * mem cgroups to try or we seem to be looping without
3622 * reclaiming anything.
3623 */
3624 if (!nr_reclaimed &&
3625 (next_mz == NULL ||
3626 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3627 break;
3628 } while (!nr_reclaimed);
3629 if (next_mz)
3630 css_put(&next_mz->mem->css);
3631 return nr_reclaimed;
3632}
3633
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003634/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003635 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003636 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
3637 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003638static int mem_cgroup_force_empty_list(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003639 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003640{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003641 struct zone *zone;
3642 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003643 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003644 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003645 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003646 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003647
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003648 zone = &NODE_DATA(node)->node_zones[zid];
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003649 mz = mem_cgroup_zoneinfo(memcg, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07003650 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003651
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003652 loop = MEM_CGROUP_ZSTAT(mz, lru);
3653 /* give some margin against EBUSY etc...*/
3654 loop += 256;
3655 busy = NULL;
3656 while (loop--) {
Johannes Weiner5564e882011-03-23 16:42:29 -07003657 struct page *page;
3658
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003659 ret = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003660 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003661 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003662 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003663 break;
3664 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003665 pc = list_entry(list->prev, struct page_cgroup, lru);
3666 if (busy == pc) {
3667 list_move(&pc->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08003668 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003669 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003670 continue;
3671 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003672 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003673
Johannes Weiner6b3ae582011-03-23 16:42:30 -07003674 page = lookup_cgroup_page(pc);
Johannes Weiner5564e882011-03-23 16:42:29 -07003675
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003676 ret = mem_cgroup_move_parent(page, pc, memcg, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003677 if (ret == -ENOMEM)
3678 break;
3679
3680 if (ret == -EBUSY || ret == -EINVAL) {
3681 /* found lock contention or "pc" is obsolete. */
3682 busy = pc;
3683 cond_resched();
3684 } else
3685 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003686 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003687
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003688 if (!ret && !list_empty(list))
3689 return -EBUSY;
3690 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003691}
3692
3693/*
3694 * make mem_cgroup's charge to be 0 if there is no task.
3695 * This enables deleting this mem_cgroup.
3696 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003697static int mem_cgroup_force_empty(struct mem_cgroup *memcg, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003698{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003699 int ret;
3700 int node, zid, shrink;
3701 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003702 struct cgroup *cgrp = memcg->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003703
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003704 css_get(&memcg->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003705
3706 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003707 /* should free all ? */
3708 if (free_all)
3709 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003710move_account:
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003711 do {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003712 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003713 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003714 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003715 ret = -EINTR;
3716 if (signal_pending(current))
3717 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003718 /* This is for making all *used* pages to be on LRU. */
3719 lru_add_drain_all();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003720 drain_all_stock_sync(memcg);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003721 ret = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003722 mem_cgroup_start_move(memcg);
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08003723 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003724 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07003725 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003726 for_each_lru(l) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003727 ret = mem_cgroup_force_empty_list(memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003728 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003729 if (ret)
3730 break;
3731 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003732 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003733 if (ret)
3734 break;
3735 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003736 mem_cgroup_end_move(memcg);
3737 memcg_oom_recover(memcg);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003738 /* it seems parent cgroup doesn't have enough mem */
3739 if (ret == -ENOMEM)
3740 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003741 cond_resched();
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003742 /* "ret" should also be checked to ensure all lists are empty. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003743 } while (memcg->res.usage > 0 || ret);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003744out:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003745 css_put(&memcg->css);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003746 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003747
3748try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003749 /* returns EBUSY if there is a task or if we come here twice. */
3750 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003751 ret = -EBUSY;
3752 goto out;
3753 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003754 /* we call try-to-free pages for make this cgroup empty */
3755 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003756 /* try to free all pages in this cgroup */
3757 shrink = 1;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003758 while (nr_retries && memcg->res.usage > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003759 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003760
3761 if (signal_pending(current)) {
3762 ret = -EINTR;
3763 goto out;
3764 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003765 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
Johannes Weiner185efc02011-09-14 16:21:58 -07003766 false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003767 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003768 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003769 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02003770 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003771 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003772
3773 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003774 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003775 /* try move_account...there may be some *locked* pages. */
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003776 goto move_account;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003777}
3778
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003779int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
3780{
3781 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
3782}
3783
3784
Balbir Singh18f59ea2009-01-07 18:08:07 -08003785static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
3786{
3787 return mem_cgroup_from_cont(cont)->use_hierarchy;
3788}
3789
3790static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
3791 u64 val)
3792{
3793 int retval = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003794 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003795 struct cgroup *parent = cont->parent;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003796 struct mem_cgroup *parent_memcg = NULL;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003797
3798 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003799 parent_memcg = mem_cgroup_from_cont(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003800
3801 cgroup_lock();
3802 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003803 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08003804 * in the child subtrees. If it is unset, then the change can
3805 * occur, provided the current cgroup has no children.
3806 *
3807 * For the root cgroup, parent_mem is NULL, we allow value to be
3808 * set if there are no children.
3809 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003810 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08003811 (val == 1 || val == 0)) {
3812 if (list_empty(&cont->children))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003813 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003814 else
3815 retval = -EBUSY;
3816 } else
3817 retval = -EINVAL;
3818 cgroup_unlock();
3819
3820 return retval;
3821}
3822
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003823
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003824static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003825 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003826{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003827 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003828 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003829
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003830 /* Per-cpu values can be negative, use a signed accumulator */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003831 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003832 val += mem_cgroup_read_stat(iter, idx);
3833
3834 if (val < 0) /* race ? */
3835 val = 0;
3836 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003837}
3838
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003839static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003840{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003841 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003842
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003843 if (!mem_cgroup_is_root(memcg)) {
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003844 if (!swap)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003845 return res_counter_read_u64(&memcg->res, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003846 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003847 return res_counter_read_u64(&memcg->memsw, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003848 }
3849
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003850 val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
3851 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003852
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003853 if (swap)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003854 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAPOUT);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003855
3856 return val << PAGE_SHIFT;
3857}
3858
Paul Menage2c3daa72008-04-29 00:59:58 -07003859static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003860{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003861 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003862 u64 val;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003863 int type, name;
3864
3865 type = MEMFILE_TYPE(cft->private);
3866 name = MEMFILE_ATTR(cft->private);
3867 switch (type) {
3868 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003869 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003870 val = mem_cgroup_usage(memcg, false);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003871 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003872 val = res_counter_read_u64(&memcg->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003873 break;
3874 case _MEMSWAP:
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, true);
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->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003879 break;
3880 default:
3881 BUG();
3882 break;
3883 }
3884 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003885}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003886/*
3887 * The user of this function is...
3888 * RES_LIMIT.
3889 */
Paul Menage856c13a2008-07-25 01:47:04 -07003890static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
3891 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003892{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003893 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003894 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003895 unsigned long long val;
3896 int ret;
3897
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003898 type = MEMFILE_TYPE(cft->private);
3899 name = MEMFILE_ATTR(cft->private);
3900 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003901 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07003902 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
3903 ret = -EINVAL;
3904 break;
3905 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003906 /* This function does all necessary parse...reuse it */
3907 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003908 if (ret)
3909 break;
3910 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003911 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003912 else
3913 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003914 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07003915 case RES_SOFT_LIMIT:
3916 ret = res_counter_memparse_write_strategy(buffer, &val);
3917 if (ret)
3918 break;
3919 /*
3920 * For memsw, soft limits are hard to implement in terms
3921 * of semantics, for now, we support soft limits for
3922 * control without swap
3923 */
3924 if (type == _MEM)
3925 ret = res_counter_set_soft_limit(&memcg->res, val);
3926 else
3927 ret = -EINVAL;
3928 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003929 default:
3930 ret = -EINVAL; /* should be BUG() ? */
3931 break;
3932 }
3933 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003934}
3935
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08003936static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
3937 unsigned long long *mem_limit, unsigned long long *memsw_limit)
3938{
3939 struct cgroup *cgroup;
3940 unsigned long long min_limit, min_memsw_limit, tmp;
3941
3942 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3943 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3944 cgroup = memcg->css.cgroup;
3945 if (!memcg->use_hierarchy)
3946 goto out;
3947
3948 while (cgroup->parent) {
3949 cgroup = cgroup->parent;
3950 memcg = mem_cgroup_from_cont(cgroup);
3951 if (!memcg->use_hierarchy)
3952 break;
3953 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
3954 min_limit = min(min_limit, tmp);
3955 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3956 min_memsw_limit = min(min_memsw_limit, tmp);
3957 }
3958out:
3959 *mem_limit = min_limit;
3960 *memsw_limit = min_memsw_limit;
3961 return;
3962}
3963
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003964static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003965{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003966 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003967 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003968
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003969 memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003970 type = MEMFILE_TYPE(event);
3971 name = MEMFILE_ATTR(event);
3972 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003973 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003974 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003975 res_counter_reset_max(&memcg->res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003976 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003977 res_counter_reset_max(&memcg->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003978 break;
3979 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003980 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003981 res_counter_reset_failcnt(&memcg->res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003982 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003983 res_counter_reset_failcnt(&memcg->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003984 break;
3985 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07003986
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07003987 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003988}
3989
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003990static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
3991 struct cftype *cft)
3992{
3993 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
3994}
3995
Daisuke Nishimura02491442010-03-10 15:22:17 -08003996#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003997static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
3998 struct cftype *cft, u64 val)
3999{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004000 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004001
4002 if (val >= (1 << NR_MOVE_TYPE))
4003 return -EINVAL;
4004 /*
4005 * We check this value several times in both in can_attach() and
4006 * attach(), so we need cgroup lock to prevent this value from being
4007 * inconsistent.
4008 */
4009 cgroup_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004010 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004011 cgroup_unlock();
4012
4013 return 0;
4014}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004015#else
4016static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
4017 struct cftype *cft, u64 val)
4018{
4019 return -ENOSYS;
4020}
4021#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004022
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004023
4024/* For read statistics */
4025enum {
4026 MCS_CACHE,
4027 MCS_RSS,
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08004028 MCS_FILE_MAPPED,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004029 MCS_PGPGIN,
4030 MCS_PGPGOUT,
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004031 MCS_SWAP,
Ying Han456f9982011-05-26 16:25:38 -07004032 MCS_PGFAULT,
4033 MCS_PGMAJFAULT,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004034 MCS_INACTIVE_ANON,
4035 MCS_ACTIVE_ANON,
4036 MCS_INACTIVE_FILE,
4037 MCS_ACTIVE_FILE,
4038 MCS_UNEVICTABLE,
4039 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004040};
4041
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004042struct mcs_total_stat {
4043 s64 stat[NR_MCS_STAT];
4044};
4045
4046struct {
4047 char *local_name;
4048 char *total_name;
4049} memcg_stat_strings[NR_MCS_STAT] = {
4050 {"cache", "total_cache"},
4051 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07004052 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004053 {"pgpgin", "total_pgpgin"},
4054 {"pgpgout", "total_pgpgout"},
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004055 {"swap", "total_swap"},
Ying Han456f9982011-05-26 16:25:38 -07004056 {"pgfault", "total_pgfault"},
4057 {"pgmajfault", "total_pgmajfault"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004058 {"inactive_anon", "total_inactive_anon"},
4059 {"active_anon", "total_active_anon"},
4060 {"inactive_file", "total_inactive_file"},
4061 {"active_file", "total_active_file"},
4062 {"unevictable", "total_unevictable"}
4063};
4064
4065
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004066static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004067mem_cgroup_get_local_stat(struct mem_cgroup *memcg, struct mcs_total_stat *s)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004068{
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004069 s64 val;
4070
4071 /* per cpu stat */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004072 val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_CACHE);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004073 s->stat[MCS_CACHE] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004074 val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_RSS);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004075 s->stat[MCS_RSS] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004076 val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_FILE_MAPPED);
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08004077 s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004078 val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGPGIN);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004079 s->stat[MCS_PGPGIN] += val;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004080 val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGPGOUT);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004081 s->stat[MCS_PGPGOUT] += val;
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004082 if (do_swap_account) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004083 val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_SWAPOUT);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004084 s->stat[MCS_SWAP] += val * PAGE_SIZE;
4085 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004086 val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGFAULT);
Ying Han456f9982011-05-26 16:25:38 -07004087 s->stat[MCS_PGFAULT] += val;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004088 val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGMAJFAULT);
Ying Han456f9982011-05-26 16:25:38 -07004089 s->stat[MCS_PGMAJFAULT] += val;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004090
4091 /* per zone stat */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004092 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_ANON));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004093 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004094 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_ANON));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004095 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004096 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_FILE));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004097 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004098 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_FILE));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004099 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004100 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004101 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004102}
4103
4104static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004105mem_cgroup_get_total_stat(struct mem_cgroup *memcg, struct mcs_total_stat *s)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004106{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004107 struct mem_cgroup *iter;
4108
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004109 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004110 mem_cgroup_get_local_stat(iter, s);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004111}
4112
Ying Han406eb0c2011-05-26 16:25:37 -07004113#ifdef CONFIG_NUMA
4114static int mem_control_numa_stat_show(struct seq_file *m, void *arg)
4115{
4116 int nid;
4117 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
4118 unsigned long node_nr;
4119 struct cgroup *cont = m->private;
4120 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
4121
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004122 total_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07004123 seq_printf(m, "total=%lu", total_nr);
4124 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004125 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07004126 seq_printf(m, " N%d=%lu", nid, node_nr);
4127 }
4128 seq_putc(m, '\n');
4129
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004130 file_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07004131 seq_printf(m, "file=%lu", file_nr);
4132 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004133 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
4134 LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07004135 seq_printf(m, " N%d=%lu", nid, node_nr);
4136 }
4137 seq_putc(m, '\n');
4138
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004139 anon_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07004140 seq_printf(m, "anon=%lu", anon_nr);
4141 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004142 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
4143 LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07004144 seq_printf(m, " N%d=%lu", nid, node_nr);
4145 }
4146 seq_putc(m, '\n');
4147
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004148 unevictable_nr = mem_cgroup_nr_lru_pages(mem_cont, BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07004149 seq_printf(m, "unevictable=%lu", unevictable_nr);
4150 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004151 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
4152 BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07004153 seq_printf(m, " N%d=%lu", nid, node_nr);
4154 }
4155 seq_putc(m, '\n');
4156 return 0;
4157}
4158#endif /* CONFIG_NUMA */
4159
Paul Menagec64745c2008-04-29 01:00:02 -07004160static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
4161 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004162{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004163 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004164 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004165 int i;
4166
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004167 memset(&mystat, 0, sizeof(mystat));
4168 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004169
Ying Han406eb0c2011-05-26 16:25:37 -07004170
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004171 for (i = 0; i < NR_MCS_STAT; i++) {
4172 if (i == MCS_SWAP && !do_swap_account)
4173 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004174 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004175 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004176
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004177 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004178 {
4179 unsigned long long limit, memsw_limit;
4180 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
4181 cb->fill(cb, "hierarchical_memory_limit", limit);
4182 if (do_swap_account)
4183 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
4184 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004185
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004186 memset(&mystat, 0, sizeof(mystat));
4187 mem_cgroup_get_total_stat(mem_cont, &mystat);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004188 for (i = 0; i < NR_MCS_STAT; i++) {
4189 if (i == MCS_SWAP && !do_swap_account)
4190 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004191 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004192 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004193
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004194#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08004195 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004196
4197 {
4198 int nid, zid;
4199 struct mem_cgroup_per_zone *mz;
4200 unsigned long recent_rotated[2] = {0, 0};
4201 unsigned long recent_scanned[2] = {0, 0};
4202
4203 for_each_online_node(nid)
4204 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
4205 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
4206
4207 recent_rotated[0] +=
4208 mz->reclaim_stat.recent_rotated[0];
4209 recent_rotated[1] +=
4210 mz->reclaim_stat.recent_rotated[1];
4211 recent_scanned[0] +=
4212 mz->reclaim_stat.recent_scanned[0];
4213 recent_scanned[1] +=
4214 mz->reclaim_stat.recent_scanned[1];
4215 }
4216 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
4217 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
4218 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
4219 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
4220 }
4221#endif
4222
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004223 return 0;
4224}
4225
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004226static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
4227{
4228 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4229
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07004230 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004231}
4232
4233static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
4234 u64 val)
4235{
4236 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4237 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08004238
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004239 if (val > 100)
4240 return -EINVAL;
4241
4242 if (cgrp->parent == NULL)
4243 return -EINVAL;
4244
4245 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08004246
4247 cgroup_lock();
4248
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004249 /* If under hierarchy, only empty-root can set this value */
4250 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08004251 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
4252 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004253 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08004254 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004255
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004256 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004257
Li Zefan068b38c2009-01-15 13:51:26 -08004258 cgroup_unlock();
4259
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004260 return 0;
4261}
4262
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004263static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
4264{
4265 struct mem_cgroup_threshold_ary *t;
4266 u64 usage;
4267 int i;
4268
4269 rcu_read_lock();
4270 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004271 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004272 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004273 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004274
4275 if (!t)
4276 goto unlock;
4277
4278 usage = mem_cgroup_usage(memcg, swap);
4279
4280 /*
4281 * current_threshold points to threshold just below usage.
4282 * If it's not true, a threshold was crossed after last
4283 * call of __mem_cgroup_threshold().
4284 */
Phil Carmody5407a562010-05-26 14:42:42 -07004285 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004286
4287 /*
4288 * Iterate backward over array of thresholds starting from
4289 * current_threshold and check if a threshold is crossed.
4290 * If none of thresholds below usage is crossed, we read
4291 * only one element of the array here.
4292 */
4293 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
4294 eventfd_signal(t->entries[i].eventfd, 1);
4295
4296 /* i = current_threshold + 1 */
4297 i++;
4298
4299 /*
4300 * Iterate forward over array of thresholds starting from
4301 * current_threshold+1 and check if a threshold is crossed.
4302 * If none of thresholds above usage is crossed, we read
4303 * only one element of the array here.
4304 */
4305 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
4306 eventfd_signal(t->entries[i].eventfd, 1);
4307
4308 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07004309 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004310unlock:
4311 rcu_read_unlock();
4312}
4313
4314static void mem_cgroup_threshold(struct mem_cgroup *memcg)
4315{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07004316 while (memcg) {
4317 __mem_cgroup_threshold(memcg, false);
4318 if (do_swap_account)
4319 __mem_cgroup_threshold(memcg, true);
4320
4321 memcg = parent_mem_cgroup(memcg);
4322 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004323}
4324
4325static int compare_thresholds(const void *a, const void *b)
4326{
4327 const struct mem_cgroup_threshold *_a = a;
4328 const struct mem_cgroup_threshold *_b = b;
4329
4330 return _a->threshold - _b->threshold;
4331}
4332
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004333static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004334{
4335 struct mem_cgroup_eventfd_list *ev;
4336
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004337 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004338 eventfd_signal(ev->eventfd, 1);
4339 return 0;
4340}
4341
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004342static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004343{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004344 struct mem_cgroup *iter;
4345
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004346 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004347 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004348}
4349
4350static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
4351 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004352{
4353 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004354 struct mem_cgroup_thresholds *thresholds;
4355 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004356 int type = MEMFILE_TYPE(cft->private);
4357 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004358 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004359
4360 ret = res_counter_memparse_write_strategy(args, &threshold);
4361 if (ret)
4362 return ret;
4363
4364 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004365
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004366 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004367 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004368 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004369 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004370 else
4371 BUG();
4372
4373 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4374
4375 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004376 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004377 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4378
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004379 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004380
4381 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004382 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004383 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004384 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004385 ret = -ENOMEM;
4386 goto unlock;
4387 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004388 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004389
4390 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004391 if (thresholds->primary) {
4392 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004393 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004394 }
4395
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004396 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004397 new->entries[size - 1].eventfd = eventfd;
4398 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004399
4400 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004401 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004402 compare_thresholds, NULL);
4403
4404 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004405 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004406 for (i = 0; i < size; i++) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004407 if (new->entries[i].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004408 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004409 * new->current_threshold will not be used until
4410 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004411 * it here.
4412 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004413 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004414 }
4415 }
4416
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004417 /* Free old spare buffer and save old primary buffer as spare */
4418 kfree(thresholds->spare);
4419 thresholds->spare = thresholds->primary;
4420
4421 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004422
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004423 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004424 synchronize_rcu();
4425
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004426unlock:
4427 mutex_unlock(&memcg->thresholds_lock);
4428
4429 return ret;
4430}
4431
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004432static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004433 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004434{
4435 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004436 struct mem_cgroup_thresholds *thresholds;
4437 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004438 int type = MEMFILE_TYPE(cft->private);
4439 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004440 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004441
4442 mutex_lock(&memcg->thresholds_lock);
4443 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004444 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004445 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004446 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004447 else
4448 BUG();
4449
4450 /*
4451 * Something went wrong if we trying to unregister a threshold
4452 * if we don't have thresholds
4453 */
4454 BUG_ON(!thresholds);
4455
4456 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4457
4458 /* Check if a threshold crossed before removing */
4459 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4460
4461 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004462 size = 0;
4463 for (i = 0; i < thresholds->primary->size; i++) {
4464 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004465 size++;
4466 }
4467
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004468 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004469
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004470 /* Set thresholds array to NULL if we don't have thresholds */
4471 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004472 kfree(new);
4473 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004474 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004475 }
4476
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004477 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004478
4479 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004480 new->current_threshold = -1;
4481 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
4482 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004483 continue;
4484
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004485 new->entries[j] = thresholds->primary->entries[i];
4486 if (new->entries[j].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004487 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004488 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004489 * until rcu_assign_pointer(), so it's safe to increment
4490 * it here.
4491 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004492 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004493 }
4494 j++;
4495 }
4496
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004497swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004498 /* Swap primary and spare array */
4499 thresholds->spare = thresholds->primary;
4500 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004501
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004502 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004503 synchronize_rcu();
4504
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004505 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004506}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004507
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004508static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
4509 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
4510{
4511 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4512 struct mem_cgroup_eventfd_list *event;
4513 int type = MEMFILE_TYPE(cft->private);
4514
4515 BUG_ON(type != _OOM_TYPE);
4516 event = kmalloc(sizeof(*event), GFP_KERNEL);
4517 if (!event)
4518 return -ENOMEM;
4519
Michal Hocko1af8efe2011-07-26 16:08:24 -07004520 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004521
4522 event->eventfd = eventfd;
4523 list_add(&event->list, &memcg->oom_notify);
4524
4525 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07004526 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004527 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07004528 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004529
4530 return 0;
4531}
4532
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004533static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004534 struct cftype *cft, struct eventfd_ctx *eventfd)
4535{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004536 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004537 struct mem_cgroup_eventfd_list *ev, *tmp;
4538 int type = MEMFILE_TYPE(cft->private);
4539
4540 BUG_ON(type != _OOM_TYPE);
4541
Michal Hocko1af8efe2011-07-26 16:08:24 -07004542 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004543
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004544 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004545 if (ev->eventfd == eventfd) {
4546 list_del(&ev->list);
4547 kfree(ev);
4548 }
4549 }
4550
Michal Hocko1af8efe2011-07-26 16:08:24 -07004551 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004552}
4553
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004554static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
4555 struct cftype *cft, struct cgroup_map_cb *cb)
4556{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004557 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004558
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004559 cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004560
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004561 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004562 cb->fill(cb, "under_oom", 1);
4563 else
4564 cb->fill(cb, "under_oom", 0);
4565 return 0;
4566}
4567
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004568static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
4569 struct cftype *cft, u64 val)
4570{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004571 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004572 struct mem_cgroup *parent;
4573
4574 /* cannot set to root cgroup and only 0 and 1 are allowed */
4575 if (!cgrp->parent || !((val == 0) || (val == 1)))
4576 return -EINVAL;
4577
4578 parent = mem_cgroup_from_cont(cgrp->parent);
4579
4580 cgroup_lock();
4581 /* oom-kill-disable is a flag for subhierarchy. */
4582 if ((parent->use_hierarchy) ||
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004583 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004584 cgroup_unlock();
4585 return -EINVAL;
4586 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004587 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07004588 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004589 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004590 cgroup_unlock();
4591 return 0;
4592}
4593
Ying Han406eb0c2011-05-26 16:25:37 -07004594#ifdef CONFIG_NUMA
4595static const struct file_operations mem_control_numa_stat_file_operations = {
4596 .read = seq_read,
4597 .llseek = seq_lseek,
4598 .release = single_release,
4599};
4600
4601static int mem_control_numa_stat_open(struct inode *unused, struct file *file)
4602{
4603 struct cgroup *cont = file->f_dentry->d_parent->d_fsdata;
4604
4605 file->f_op = &mem_control_numa_stat_file_operations;
4606 return single_open(file, mem_control_numa_stat_show, cont);
4607}
4608#endif /* CONFIG_NUMA */
4609
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004610static struct cftype mem_cgroup_files[] = {
4611 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004612 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004613 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07004614 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004615 .register_event = mem_cgroup_usage_register_event,
4616 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004617 },
4618 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004619 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004620 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004621 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004622 .read_u64 = mem_cgroup_read,
4623 },
4624 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004625 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004626 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07004627 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07004628 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004629 },
4630 {
Balbir Singh296c81d2009-09-23 15:56:36 -07004631 .name = "soft_limit_in_bytes",
4632 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
4633 .write_string = mem_cgroup_write,
4634 .read_u64 = mem_cgroup_read,
4635 },
4636 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004637 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004638 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004639 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07004640 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004641 },
Balbir Singh8697d332008-02-07 00:13:59 -08004642 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004643 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07004644 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004645 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004646 {
4647 .name = "force_empty",
4648 .trigger = mem_cgroup_force_empty_write,
4649 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08004650 {
4651 .name = "use_hierarchy",
4652 .write_u64 = mem_cgroup_hierarchy_write,
4653 .read_u64 = mem_cgroup_hierarchy_read,
4654 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004655 {
4656 .name = "swappiness",
4657 .read_u64 = mem_cgroup_swappiness_read,
4658 .write_u64 = mem_cgroup_swappiness_write,
4659 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004660 {
4661 .name = "move_charge_at_immigrate",
4662 .read_u64 = mem_cgroup_move_charge_read,
4663 .write_u64 = mem_cgroup_move_charge_write,
4664 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004665 {
4666 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004667 .read_map = mem_cgroup_oom_control_read,
4668 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004669 .register_event = mem_cgroup_oom_register_event,
4670 .unregister_event = mem_cgroup_oom_unregister_event,
4671 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
4672 },
Ying Han406eb0c2011-05-26 16:25:37 -07004673#ifdef CONFIG_NUMA
4674 {
4675 .name = "numa_stat",
4676 .open = mem_control_numa_stat_open,
KAMEZAWA Hiroyuki89577122011-06-15 15:08:41 -07004677 .mode = S_IRUGO,
Ying Han406eb0c2011-05-26 16:25:37 -07004678 },
4679#endif
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004680};
4681
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004682#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4683static struct cftype memsw_cgroup_files[] = {
4684 {
4685 .name = "memsw.usage_in_bytes",
4686 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
4687 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004688 .register_event = mem_cgroup_usage_register_event,
4689 .unregister_event = mem_cgroup_usage_unregister_event,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004690 },
4691 {
4692 .name = "memsw.max_usage_in_bytes",
4693 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
4694 .trigger = mem_cgroup_reset,
4695 .read_u64 = mem_cgroup_read,
4696 },
4697 {
4698 .name = "memsw.limit_in_bytes",
4699 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
4700 .write_string = mem_cgroup_write,
4701 .read_u64 = mem_cgroup_read,
4702 },
4703 {
4704 .name = "memsw.failcnt",
4705 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
4706 .trigger = mem_cgroup_reset,
4707 .read_u64 = mem_cgroup_read,
4708 },
4709};
4710
4711static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
4712{
4713 if (!do_swap_account)
4714 return 0;
4715 return cgroup_add_files(cont, ss, memsw_cgroup_files,
4716 ARRAY_SIZE(memsw_cgroup_files));
4717};
4718#else
4719static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
4720{
4721 return 0;
4722}
4723#endif
4724
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004725static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004726{
4727 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004728 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07004729 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004730 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004731 /*
4732 * This routine is called against possible nodes.
4733 * But it's BUG to call kmalloc() against offline node.
4734 *
4735 * TODO: this routine can waste much memory for nodes which will
4736 * never be onlined. It's better to use memory hotplug callback
4737 * function.
4738 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004739 if (!node_state(node, N_NORMAL_MEMORY))
4740 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08004741 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004742 if (!pn)
4743 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004744
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004745 memcg->info.nodeinfo[node] = pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004746 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4747 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07004748 for_each_lru(l)
4749 INIT_LIST_HEAD(&mz->lists[l]);
Balbir Singhf64c3f52009-09-23 15:56:37 -07004750 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07004751 mz->on_tree = false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004752 mz->mem = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004753 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004754 return 0;
4755}
4756
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004757static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004758{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004759 kfree(memcg->info.nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004760}
4761
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004762static struct mem_cgroup *mem_cgroup_alloc(void)
4763{
4764 struct mem_cgroup *mem;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004765 int size = sizeof(struct mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004766
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004767 /* Can be very big if MAX_NUMNODES is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08004768 if (size < PAGE_SIZE)
Jesper Juhl17295c82011-01-13 15:47:42 -08004769 mem = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004770 else
Jesper Juhl17295c82011-01-13 15:47:42 -08004771 mem = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004772
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07004773 if (!mem)
4774 return NULL;
4775
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004776 mem->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004777 if (!mem->stat)
4778 goto out_free;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07004779 spin_lock_init(&mem->pcp_counter_lock);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004780 return mem;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004781
4782out_free:
4783 if (size < PAGE_SIZE)
4784 kfree(mem);
4785 else
4786 vfree(mem);
4787 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004788}
4789
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004790/*
4791 * At destroying mem_cgroup, references from swap_cgroup can remain.
4792 * (scanning all at force_empty is too costly...)
4793 *
4794 * Instead of clearing all references at force_empty, we remember
4795 * the number of reference from swap_cgroup and free mem_cgroup when
4796 * it goes down to 0.
4797 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004798 * Removal of cgroup itself succeeds regardless of refs from swap.
4799 */
4800
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004801static void __mem_cgroup_free(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004802{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004803 int node;
4804
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004805 mem_cgroup_remove_from_trees(memcg);
4806 free_css_id(&mem_cgroup_subsys, &memcg->css);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004807
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004808 for_each_node_state(node, N_POSSIBLE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004809 free_mem_cgroup_per_zone_info(memcg, node);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004810
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004811 free_percpu(memcg->stat);
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004812 if (sizeof(struct mem_cgroup) < PAGE_SIZE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004813 kfree(memcg);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004814 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004815 vfree(memcg);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004816}
4817
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004818static void mem_cgroup_get(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004819{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004820 atomic_inc(&memcg->refcnt);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004821}
4822
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004823static void __mem_cgroup_put(struct mem_cgroup *memcg, int count)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004824{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004825 if (atomic_sub_and_test(count, &memcg->refcnt)) {
4826 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
4827 __mem_cgroup_free(memcg);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004828 if (parent)
4829 mem_cgroup_put(parent);
4830 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004831}
4832
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004833static void mem_cgroup_put(struct mem_cgroup *memcg)
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004834{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004835 __mem_cgroup_put(memcg, 1);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004836}
4837
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004838/*
4839 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
4840 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004841static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004842{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004843 if (!memcg->res.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004844 return NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004845 return mem_cgroup_from_res_counter(memcg->res.parent, res);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004846}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004847
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004848#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4849static void __init enable_swap_cgroup(void)
4850{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004851 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004852 do_swap_account = 1;
4853}
4854#else
4855static void __init enable_swap_cgroup(void)
4856{
4857}
4858#endif
4859
Balbir Singhf64c3f52009-09-23 15:56:37 -07004860static int mem_cgroup_soft_limit_tree_init(void)
4861{
4862 struct mem_cgroup_tree_per_node *rtpn;
4863 struct mem_cgroup_tree_per_zone *rtpz;
4864 int tmp, node, zone;
4865
4866 for_each_node_state(node, N_POSSIBLE) {
4867 tmp = node;
4868 if (!node_state(node, N_NORMAL_MEMORY))
4869 tmp = -1;
4870 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
4871 if (!rtpn)
4872 return 1;
4873
4874 soft_limit_tree.rb_tree_per_node[node] = rtpn;
4875
4876 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4877 rtpz = &rtpn->rb_tree_per_zone[zone];
4878 rtpz->rb_root = RB_ROOT;
4879 spin_lock_init(&rtpz->lock);
4880 }
4881 }
4882 return 0;
4883}
4884
Li Zefan0eb253e2009-01-15 13:51:25 -08004885static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004886mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
4887{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004888 struct mem_cgroup *memcg, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004889 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004890 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004891
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004892 memcg = mem_cgroup_alloc();
4893 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004894 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004895
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004896 for_each_node_state(node, N_POSSIBLE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004897 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004898 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004899
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004900 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004901 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004902 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004903 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004904 parent = NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004905 root_mem_cgroup = memcg;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004906 if (mem_cgroup_soft_limit_tree_init())
4907 goto free_out;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004908 for_each_possible_cpu(cpu) {
4909 struct memcg_stock_pcp *stock =
4910 &per_cpu(memcg_stock, cpu);
4911 INIT_WORK(&stock->work, drain_local_stock);
4912 }
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07004913 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004914 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004915 parent = mem_cgroup_from_cont(cont->parent);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004916 memcg->use_hierarchy = parent->use_hierarchy;
4917 memcg->oom_kill_disable = parent->oom_kill_disable;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004918 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004919
Balbir Singh18f59ea2009-01-07 18:08:07 -08004920 if (parent && parent->use_hierarchy) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004921 res_counter_init(&memcg->res, &parent->res);
4922 res_counter_init(&memcg->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004923 /*
4924 * We increment refcnt of the parent to ensure that we can
4925 * safely access it on res_counter_charge/uncharge.
4926 * This refcnt will be decremented when freeing this
4927 * mem_cgroup(see mem_cgroup_put).
4928 */
4929 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004930 } else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004931 res_counter_init(&memcg->res, NULL);
4932 res_counter_init(&memcg->memsw, NULL);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004933 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004934 memcg->last_scanned_child = 0;
4935 memcg->last_scanned_node = MAX_NUMNODES;
4936 INIT_LIST_HEAD(&memcg->oom_notify);
Balbir Singh6d61ef42009-01-07 18:08:06 -08004937
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004938 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004939 memcg->swappiness = mem_cgroup_swappiness(parent);
4940 atomic_set(&memcg->refcnt, 1);
4941 memcg->move_charge_at_immigrate = 0;
4942 mutex_init(&memcg->thresholds_lock);
4943 return &memcg->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004944free_out:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004945 __mem_cgroup_free(memcg);
Balbir Singh4b3bde42009-09-23 15:56:32 -07004946 root_mem_cgroup = NULL;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004947 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004948}
4949
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004950static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004951 struct cgroup *cont)
4952{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004953 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004954
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004955 return mem_cgroup_force_empty(memcg, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004956}
4957
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004958static void mem_cgroup_destroy(struct cgroup_subsys *ss,
4959 struct cgroup *cont)
4960{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004961 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004962
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004963 mem_cgroup_put(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004964}
4965
4966static int mem_cgroup_populate(struct cgroup_subsys *ss,
4967 struct cgroup *cont)
4968{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004969 int ret;
4970
4971 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
4972 ARRAY_SIZE(mem_cgroup_files));
4973
4974 if (!ret)
4975 ret = register_memsw_files(cont, ss);
4976 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004977}
4978
Daisuke Nishimura02491442010-03-10 15:22:17 -08004979#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004980/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004981#define PRECHARGE_COUNT_AT_ONCE 256
4982static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004983{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004984 int ret = 0;
4985 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004986 struct mem_cgroup *memcg = mc.to;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004987
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004988 if (mem_cgroup_is_root(memcg)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004989 mc.precharge += count;
4990 /* we don't need css_get for root */
4991 return ret;
4992 }
4993 /* try to charge at once */
4994 if (count > 1) {
4995 struct res_counter *dummy;
4996 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004997 * "memcg" cannot be under rmdir() because we've already checked
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004998 * by cgroup_lock_live_cgroup() that it is not removed and we
4999 * are still under the same cgroup_mutex. So we can postpone
5000 * css_get().
5001 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005002 if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy))
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005003 goto one_by_one;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005004 if (do_swap_account && res_counter_charge(&memcg->memsw,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005005 PAGE_SIZE * count, &dummy)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005006 res_counter_uncharge(&memcg->res, PAGE_SIZE * count);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005007 goto one_by_one;
5008 }
5009 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005010 return ret;
5011 }
5012one_by_one:
5013 /* fall back to one by one charge */
5014 while (count--) {
5015 if (signal_pending(current)) {
5016 ret = -EINTR;
5017 break;
5018 }
5019 if (!batch_count--) {
5020 batch_count = PRECHARGE_COUNT_AT_ONCE;
5021 cond_resched();
5022 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005023 ret = __mem_cgroup_try_charge(NULL,
5024 GFP_KERNEL, 1, &memcg, false);
5025 if (ret || !memcg)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005026 /* mem_cgroup_clear_mc() will do uncharge later */
5027 return -ENOMEM;
5028 mc.precharge++;
5029 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005030 return ret;
5031}
5032
5033/**
5034 * is_target_pte_for_mc - check a pte whether it is valid for move charge
5035 * @vma: the vma the pte to be checked belongs
5036 * @addr: the address corresponding to the pte to be checked
5037 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08005038 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005039 *
5040 * Returns
5041 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
5042 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
5043 * move charge. if @target is not NULL, the page is stored in target->page
5044 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08005045 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
5046 * target for charge migration. if @target is not NULL, the entry is stored
5047 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005048 *
5049 * Called with pte lock held.
5050 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005051union mc_target {
5052 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005053 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005054};
5055
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005056enum mc_target_type {
5057 MC_TARGET_NONE, /* not used */
5058 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08005059 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005060};
5061
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005062static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
5063 unsigned long addr, pte_t ptent)
5064{
5065 struct page *page = vm_normal_page(vma, addr, ptent);
5066
5067 if (!page || !page_mapped(page))
5068 return NULL;
5069 if (PageAnon(page)) {
5070 /* we don't move shared anon */
5071 if (!move_anon() || page_mapcount(page) > 2)
5072 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005073 } else if (!move_file())
5074 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005075 return NULL;
5076 if (!get_page_unless_zero(page))
5077 return NULL;
5078
5079 return page;
5080}
5081
5082static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5083 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5084{
5085 int usage_count;
5086 struct page *page = NULL;
5087 swp_entry_t ent = pte_to_swp_entry(ptent);
5088
5089 if (!move_anon() || non_swap_entry(ent))
5090 return NULL;
5091 usage_count = mem_cgroup_count_swap_user(ent, &page);
5092 if (usage_count > 1) { /* we don't move shared anon */
5093 if (page)
5094 put_page(page);
5095 return NULL;
5096 }
5097 if (do_swap_account)
5098 entry->val = ent.val;
5099
5100 return page;
5101}
5102
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005103static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
5104 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5105{
5106 struct page *page = NULL;
5107 struct inode *inode;
5108 struct address_space *mapping;
5109 pgoff_t pgoff;
5110
5111 if (!vma->vm_file) /* anonymous vma */
5112 return NULL;
5113 if (!move_file())
5114 return NULL;
5115
5116 inode = vma->vm_file->f_path.dentry->d_inode;
5117 mapping = vma->vm_file->f_mapping;
5118 if (pte_none(ptent))
5119 pgoff = linear_page_index(vma, addr);
5120 else /* pte_file(ptent) is true */
5121 pgoff = pte_to_pgoff(ptent);
5122
5123 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005124 page = find_get_page(mapping, pgoff);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005125
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005126#ifdef CONFIG_SWAP
5127 /* shmem/tmpfs may report page out on swap: account for that too. */
5128 if (radix_tree_exceptional_entry(page)) {
5129 swp_entry_t swap = radix_to_swp_entry(page);
5130 if (do_swap_account)
5131 *entry = swap;
5132 page = find_get_page(&swapper_space, swap.val);
5133 }
5134#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005135 return page;
5136}
5137
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005138static int is_target_pte_for_mc(struct vm_area_struct *vma,
5139 unsigned long addr, pte_t ptent, union mc_target *target)
5140{
Daisuke Nishimura02491442010-03-10 15:22:17 -08005141 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005142 struct page_cgroup *pc;
5143 int ret = 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005144 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005145
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005146 if (pte_present(ptent))
5147 page = mc_handle_present_pte(vma, addr, ptent);
5148 else if (is_swap_pte(ptent))
5149 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005150 else if (pte_none(ptent) || pte_file(ptent))
5151 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005152
5153 if (!page && !ent.val)
Daisuke Nishimura02491442010-03-10 15:22:17 -08005154 return 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005155 if (page) {
5156 pc = lookup_page_cgroup(page);
5157 /*
5158 * Do only loose check w/o page_cgroup lock.
5159 * mem_cgroup_move_account() checks the pc is valid or not under
5160 * the lock.
5161 */
5162 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
5163 ret = MC_TARGET_PAGE;
5164 if (target)
5165 target->page = page;
5166 }
5167 if (!ret || !target)
5168 put_page(page);
5169 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005170 /* There is a swap entry and a page doesn't exist or isn't charged */
5171 if (ent.val && !ret &&
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005172 css_id(&mc.from->css) == lookup_swap_cgroup(ent)) {
5173 ret = MC_TARGET_SWAP;
5174 if (target)
5175 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005176 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005177 return ret;
5178}
5179
5180static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
5181 unsigned long addr, unsigned long end,
5182 struct mm_walk *walk)
5183{
5184 struct vm_area_struct *vma = walk->private;
5185 pte_t *pte;
5186 spinlock_t *ptl;
5187
Dave Hansen03319322011-03-22 16:32:56 -07005188 split_huge_page_pmd(walk->mm, pmd);
5189
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005190 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5191 for (; addr != end; pte++, addr += PAGE_SIZE)
5192 if (is_target_pte_for_mc(vma, addr, *pte, NULL))
5193 mc.precharge++; /* increment precharge temporarily */
5194 pte_unmap_unlock(pte - 1, ptl);
5195 cond_resched();
5196
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005197 return 0;
5198}
5199
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005200static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
5201{
5202 unsigned long precharge;
5203 struct vm_area_struct *vma;
5204
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005205 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005206 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5207 struct mm_walk mem_cgroup_count_precharge_walk = {
5208 .pmd_entry = mem_cgroup_count_precharge_pte_range,
5209 .mm = mm,
5210 .private = vma,
5211 };
5212 if (is_vm_hugetlb_page(vma))
5213 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005214 walk_page_range(vma->vm_start, vma->vm_end,
5215 &mem_cgroup_count_precharge_walk);
5216 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005217 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005218
5219 precharge = mc.precharge;
5220 mc.precharge = 0;
5221
5222 return precharge;
5223}
5224
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005225static int mem_cgroup_precharge_mc(struct mm_struct *mm)
5226{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005227 unsigned long precharge = mem_cgroup_count_precharge(mm);
5228
5229 VM_BUG_ON(mc.moving_task);
5230 mc.moving_task = current;
5231 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005232}
5233
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005234/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
5235static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005236{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005237 struct mem_cgroup *from = mc.from;
5238 struct mem_cgroup *to = mc.to;
5239
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005240 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005241 if (mc.precharge) {
5242 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
5243 mc.precharge = 0;
5244 }
5245 /*
5246 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
5247 * we must uncharge here.
5248 */
5249 if (mc.moved_charge) {
5250 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
5251 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005252 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005253 /* we must fixup refcnts and charges */
5254 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005255 /* uncharge swap account from the old cgroup */
5256 if (!mem_cgroup_is_root(mc.from))
5257 res_counter_uncharge(&mc.from->memsw,
5258 PAGE_SIZE * mc.moved_swap);
5259 __mem_cgroup_put(mc.from, mc.moved_swap);
5260
5261 if (!mem_cgroup_is_root(mc.to)) {
5262 /*
5263 * we charged both to->res and to->memsw, so we should
5264 * uncharge to->res.
5265 */
5266 res_counter_uncharge(&mc.to->res,
5267 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005268 }
5269 /* we've already done mem_cgroup_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005270 mc.moved_swap = 0;
5271 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005272 memcg_oom_recover(from);
5273 memcg_oom_recover(to);
5274 wake_up_all(&mc.waitq);
5275}
5276
5277static void mem_cgroup_clear_mc(void)
5278{
5279 struct mem_cgroup *from = mc.from;
5280
5281 /*
5282 * we must clear moving_task before waking up waiters at the end of
5283 * task migration.
5284 */
5285 mc.moving_task = NULL;
5286 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005287 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005288 mc.from = NULL;
5289 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005290 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005291 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005292}
5293
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005294static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
5295 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005296 struct task_struct *p)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005297{
5298 int ret = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005299 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005300
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005301 if (memcg->move_charge_at_immigrate) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005302 struct mm_struct *mm;
5303 struct mem_cgroup *from = mem_cgroup_from_task(p);
5304
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005305 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005306
5307 mm = get_task_mm(p);
5308 if (!mm)
5309 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005310 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005311 if (mm->owner == p) {
5312 VM_BUG_ON(mc.from);
5313 VM_BUG_ON(mc.to);
5314 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005315 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005316 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005317 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005318 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005319 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005320 mc.to = memcg;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005321 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005322 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005323
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005324 ret = mem_cgroup_precharge_mc(mm);
5325 if (ret)
5326 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005327 }
5328 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005329 }
5330 return ret;
5331}
5332
5333static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
5334 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005335 struct task_struct *p)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005336{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005337 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005338}
5339
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005340static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
5341 unsigned long addr, unsigned long end,
5342 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005343{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005344 int ret = 0;
5345 struct vm_area_struct *vma = walk->private;
5346 pte_t *pte;
5347 spinlock_t *ptl;
5348
Dave Hansen03319322011-03-22 16:32:56 -07005349 split_huge_page_pmd(walk->mm, pmd);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005350retry:
5351 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5352 for (; addr != end; addr += PAGE_SIZE) {
5353 pte_t ptent = *(pte++);
5354 union mc_target target;
5355 int type;
5356 struct page *page;
5357 struct page_cgroup *pc;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005358 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005359
5360 if (!mc.precharge)
5361 break;
5362
5363 type = is_target_pte_for_mc(vma, addr, ptent, &target);
5364 switch (type) {
5365 case MC_TARGET_PAGE:
5366 page = target.page;
5367 if (isolate_lru_page(page))
5368 goto put;
5369 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07005370 if (!mem_cgroup_move_account(page, 1, pc,
5371 mc.from, mc.to, false)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005372 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005373 /* we uncharge from mc.from later. */
5374 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005375 }
5376 putback_lru_page(page);
5377put: /* is_target_pte_for_mc() gets the page */
5378 put_page(page);
5379 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005380 case MC_TARGET_SWAP:
5381 ent = target.ent;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005382 if (!mem_cgroup_move_swap_account(ent,
5383 mc.from, mc.to, false)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08005384 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005385 /* we fixup refcnts and charges later. */
5386 mc.moved_swap++;
5387 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08005388 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005389 default:
5390 break;
5391 }
5392 }
5393 pte_unmap_unlock(pte - 1, ptl);
5394 cond_resched();
5395
5396 if (addr != end) {
5397 /*
5398 * We have consumed all precharges we got in can_attach().
5399 * We try charge one by one, but don't do any additional
5400 * charges to mc.to if we have failed in charge once in attach()
5401 * phase.
5402 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005403 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005404 if (!ret)
5405 goto retry;
5406 }
5407
5408 return ret;
5409}
5410
5411static void mem_cgroup_move_charge(struct mm_struct *mm)
5412{
5413 struct vm_area_struct *vma;
5414
5415 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005416retry:
5417 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
5418 /*
5419 * Someone who are holding the mmap_sem might be waiting in
5420 * waitq. So we cancel all extra charges, wake up all waiters,
5421 * and retry. Because we cancel precharges, we might not be able
5422 * to move enough charges, but moving charge is a best-effort
5423 * feature anyway, so it wouldn't be a big problem.
5424 */
5425 __mem_cgroup_clear_mc();
5426 cond_resched();
5427 goto retry;
5428 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005429 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5430 int ret;
5431 struct mm_walk mem_cgroup_move_charge_walk = {
5432 .pmd_entry = mem_cgroup_move_charge_pte_range,
5433 .mm = mm,
5434 .private = vma,
5435 };
5436 if (is_vm_hugetlb_page(vma))
5437 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005438 ret = walk_page_range(vma->vm_start, vma->vm_end,
5439 &mem_cgroup_move_charge_walk);
5440 if (ret)
5441 /*
5442 * means we have consumed all precharges and failed in
5443 * doing additional charge. Just abandon here.
5444 */
5445 break;
5446 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005447 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005448}
5449
Balbir Singh67e465a2008-02-07 00:13:54 -08005450static void mem_cgroup_move_task(struct cgroup_subsys *ss,
5451 struct cgroup *cont,
5452 struct cgroup *old_cont,
Ben Blumf780bdb2011-05-26 16:25:19 -07005453 struct task_struct *p)
Balbir Singh67e465a2008-02-07 00:13:54 -08005454{
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005455 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005456
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005457 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005458 if (mc.to)
5459 mem_cgroup_move_charge(mm);
5460 put_swap_token(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005461 mmput(mm);
5462 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005463 if (mc.to)
5464 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08005465}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005466#else /* !CONFIG_MMU */
5467static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
5468 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005469 struct task_struct *p)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005470{
5471 return 0;
5472}
5473static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
5474 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005475 struct task_struct *p)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005476{
5477}
5478static void mem_cgroup_move_task(struct cgroup_subsys *ss,
5479 struct cgroup *cont,
5480 struct cgroup *old_cont,
Ben Blumf780bdb2011-05-26 16:25:19 -07005481 struct task_struct *p)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005482{
5483}
5484#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08005485
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005486struct cgroup_subsys mem_cgroup_subsys = {
5487 .name = "memory",
5488 .subsys_id = mem_cgroup_subsys_id,
5489 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005490 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005491 .destroy = mem_cgroup_destroy,
5492 .populate = mem_cgroup_populate,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005493 .can_attach = mem_cgroup_can_attach,
5494 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08005495 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005496 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005497 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005498};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005499
5500#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08005501static int __init enable_swap_account(char *s)
5502{
5503 /* consider enabled if no parameter or 1 is given */
Michal Hockoa2c89902011-05-24 17:12:50 -07005504 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08005505 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07005506 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08005507 really_do_swap_account = 0;
5508 return 1;
5509}
Michal Hockoa2c89902011-05-24 17:12:50 -07005510__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005511
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005512#endif