blob: 2d5755544afe5fa165e1524e2500ab56bf8ad8c8 [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
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800204static void mem_cgroup_threshold(struct mem_cgroup *mem);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700205static void mem_cgroup_oom_notify(struct mem_cgroup *mem);
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
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800365static void mem_cgroup_get(struct mem_cgroup *mem);
366static void mem_cgroup_put(struct mem_cgroup *mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -0800367static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -0700368static void drain_all_stock_async(struct mem_cgroup *mem);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800369
Balbir Singhf64c3f52009-09-23 15:56:37 -0700370static struct mem_cgroup_per_zone *
371mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
372{
373 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
374}
375
Wu Fengguangd3242362009-12-16 12:19:59 +0100376struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *mem)
377{
378 return &mem->css;
379}
380
Balbir Singhf64c3f52009-09-23 15:56:37 -0700381static struct mem_cgroup_per_zone *
Johannes Weiner97a6c372011-03-23 16:42:27 -0700382page_cgroup_zoneinfo(struct mem_cgroup *mem, 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
Balbir Singhf64c3f52009-09-23 15:56:37 -0700387 return mem_cgroup_zoneinfo(mem, nid, zid);
388}
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
Balbir Singh4e416952009-09-23 15:56:39 -0700406__mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
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
440__mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
441 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
Balbir Singhf64c3f52009-09-23 15:56:37 -0700451mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
452 struct mem_cgroup_per_zone *mz,
453 struct mem_cgroup_tree_per_zone *mctz)
454{
455 spin_lock(&mctz->lock);
Balbir Singh4e416952009-09-23 15:56:39 -0700456 __mem_cgroup_remove_exceeded(mem, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700457 spin_unlock(&mctz->lock);
458}
459
Balbir Singhf64c3f52009-09-23 15:56:37 -0700460
461static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
462{
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 */
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700474 for (; mem; mem = parent_mem_cgroup(mem)) {
475 mz = mem_cgroup_zoneinfo(mem, nid, zid);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700476 excess = res_counter_soft_limit_excess(&mem->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)
485 __mem_cgroup_remove_exceeded(mem, mz, mctz);
486 /*
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 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700490 __mem_cgroup_insert_exceeded(mem, 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
496static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
497{
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++) {
504 mz = mem_cgroup_zoneinfo(mem, node, zone);
505 mctz = soft_limit_tree_node_zone(node, zone);
506 mem_cgroup_remove_exceeded(mem, mz, mctz);
507 }
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 */
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700567static long mem_cgroup_read_stat(struct mem_cgroup *mem,
568 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)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800575 val += per_cpu(mem->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700576#ifdef CONFIG_HOTPLUG_CPU
577 spin_lock(&mem->pcp_counter_lock);
578 val += mem->nocpu_base.count[idx];
579 spin_unlock(&mem->pcp_counter_lock);
580#endif
581 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800582 return val;
583}
584
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700585static void mem_cgroup_swap_statistics(struct mem_cgroup *mem,
586 bool charge)
587{
588 int val = (charge) ? 1 : -1;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800589 this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_SWAPOUT], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700590}
591
Ying Han456f9982011-05-26 16:25:38 -0700592void mem_cgroup_pgfault(struct mem_cgroup *mem, int val)
593{
594 this_cpu_add(mem->stat->events[MEM_CGROUP_EVENTS_PGFAULT], val);
595}
596
597void mem_cgroup_pgmajfault(struct mem_cgroup *mem, int val)
598{
599 this_cpu_add(mem->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT], val);
600}
601
Johannes Weinere9f89742011-03-23 16:42:37 -0700602static unsigned long mem_cgroup_read_events(struct mem_cgroup *mem,
603 enum mem_cgroup_events_index idx)
604{
605 unsigned long val = 0;
606 int cpu;
607
608 for_each_online_cpu(cpu)
609 val += per_cpu(mem->stat->events[idx], cpu);
610#ifdef CONFIG_HOTPLUG_CPU
611 spin_lock(&mem->pcp_counter_lock);
612 val += mem->nocpu_base.events[idx];
613 spin_unlock(&mem->pcp_counter_lock);
614#endif
615 return val;
616}
617
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700618static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
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)
624 __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_CACHE], nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800625 else
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800626 __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_RSS], nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700627
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800628 /* pagein of a big page is an event. So, ignore page size */
629 if (nr_pages > 0)
Johannes Weinere9f89742011-03-23 16:42:37 -0700630 __this_cpu_inc(mem->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800631 else {
Johannes Weinere9f89742011-03-23 16:42:37 -0700632 __this_cpu_inc(mem->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800633 nr_pages = -nr_pages; /* for event */
634 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800635
Johannes Weinere9f89742011-03-23 16:42:37 -0700636 __this_cpu_add(mem->stat->events[MEM_CGROUP_EVENTS_COUNT], nr_pages);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800637
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800638 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800639}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800640
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700641unsigned long
642mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *mem, int nid, int zid,
643 unsigned int lru_mask)
Ying Han889976d2011-05-26 16:25:33 -0700644{
645 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700646 enum lru_list l;
647 unsigned long ret = 0;
648
649 mz = mem_cgroup_zoneinfo(mem, nid, zid);
650
651 for_each_lru(l) {
652 if (BIT(l) & lru_mask)
653 ret += MEM_CGROUP_ZSTAT(mz, l);
654 }
655 return ret;
656}
657
658static unsigned long
659mem_cgroup_node_nr_lru_pages(struct mem_cgroup *mem,
660 int nid, unsigned int lru_mask)
661{
Ying Han889976d2011-05-26 16:25:33 -0700662 u64 total = 0;
663 int zid;
664
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700665 for (zid = 0; zid < MAX_NR_ZONES; zid++)
666 total += mem_cgroup_zone_nr_lru_pages(mem, nid, zid, lru_mask);
667
Ying Han889976d2011-05-26 16:25:33 -0700668 return total;
669}
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700670
671static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *mem,
672 unsigned int lru_mask)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800673{
Ying Han889976d2011-05-26 16:25:33 -0700674 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800675 u64 total = 0;
676
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700677 for_each_node_state(nid, N_HIGH_MEMORY)
678 total += mem_cgroup_node_nr_lru_pages(mem, nid, lru_mask);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800679 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800680}
681
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700682static bool __memcg_event_check(struct mem_cgroup *mem, int target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800683{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700684 unsigned long val, next;
685
686 val = this_cpu_read(mem->stat->events[MEM_CGROUP_EVENTS_COUNT]);
687 next = this_cpu_read(mem->stat->targets[target]);
688 /* from time_after() in jiffies.h */
689 return ((long)next - (long)val < 0);
690}
691
692static void __mem_cgroup_target_update(struct mem_cgroup *mem, int target)
693{
694 unsigned long val, next;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800695
Johannes Weinere9f89742011-03-23 16:42:37 -0700696 val = this_cpu_read(mem->stat->events[MEM_CGROUP_EVENTS_COUNT]);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800697
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700698 switch (target) {
699 case MEM_CGROUP_TARGET_THRESH:
700 next = val + THRESHOLDS_EVENTS_TARGET;
701 break;
702 case MEM_CGROUP_TARGET_SOFTLIMIT:
703 next = val + SOFTLIMIT_EVENTS_TARGET;
704 break;
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700705 case MEM_CGROUP_TARGET_NUMAINFO:
706 next = val + NUMAINFO_EVENTS_TARGET;
707 break;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700708 default:
709 return;
710 }
711
712 this_cpu_write(mem->stat->targets[target], next);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800713}
714
715/*
716 * Check events in order.
717 *
718 */
719static void memcg_check_events(struct mem_cgroup *mem, struct page *page)
720{
721 /* threshold event is triggered in finer grain than soft limit */
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700722 if (unlikely(__memcg_event_check(mem, MEM_CGROUP_TARGET_THRESH))) {
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800723 mem_cgroup_threshold(mem);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700724 __mem_cgroup_target_update(mem, MEM_CGROUP_TARGET_THRESH);
725 if (unlikely(__memcg_event_check(mem,
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700726 MEM_CGROUP_TARGET_SOFTLIMIT))) {
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800727 mem_cgroup_update_tree(mem, page);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700728 __mem_cgroup_target_update(mem,
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700729 MEM_CGROUP_TARGET_SOFTLIMIT);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700730 }
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700731#if MAX_NUMNODES > 1
732 if (unlikely(__memcg_event_check(mem,
733 MEM_CGROUP_TARGET_NUMAINFO))) {
734 atomic_inc(&mem->numainfo_events);
735 __mem_cgroup_target_update(mem,
736 MEM_CGROUP_TARGET_NUMAINFO);
737 }
738#endif
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800739 }
740}
741
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800742static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800743{
744 return container_of(cgroup_subsys_state(cont,
745 mem_cgroup_subsys_id), struct mem_cgroup,
746 css);
747}
748
Balbir Singhcf475ad2008-04-29 01:00:16 -0700749struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800750{
Balbir Singh31a78f22008-09-28 23:09:31 +0100751 /*
752 * mm_update_next_owner() may clear mm->owner to NULL
753 * if it races with swapoff, page migration, etc.
754 * So this can be called with p == NULL.
755 */
756 if (unlikely(!p))
757 return NULL;
758
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800759 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
760 struct mem_cgroup, css);
761}
762
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700763struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800764{
765 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700766
767 if (!mm)
768 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800769 /*
770 * Because we have no locks, mm->owner's may be being moved to other
771 * cgroup. We use css_tryget() here even if this looks
772 * pessimistic (rather than adding locks here).
773 */
774 rcu_read_lock();
775 do {
776 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
777 if (unlikely(!mem))
778 break;
779 } while (!css_tryget(&mem->css));
780 rcu_read_unlock();
781 return mem;
782}
783
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700784/* The caller has to guarantee "mem" exists before calling this */
785static struct mem_cgroup *mem_cgroup_start_loop(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700786{
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700787 struct cgroup_subsys_state *css;
788 int found;
789
790 if (!mem) /* ROOT cgroup has the smallest ID */
791 return root_mem_cgroup; /*css_put/get against root is ignored*/
792 if (!mem->use_hierarchy) {
793 if (css_tryget(&mem->css))
794 return mem;
795 return NULL;
796 }
797 rcu_read_lock();
798 /*
799 * searching a memory cgroup which has the smallest ID under given
800 * ROOT cgroup. (ID >= 1)
801 */
802 css = css_get_next(&mem_cgroup_subsys, 1, &mem->css, &found);
803 if (css && css_tryget(css))
804 mem = container_of(css, struct mem_cgroup, css);
805 else
806 mem = NULL;
807 rcu_read_unlock();
808 return mem;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700809}
810
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700811static struct mem_cgroup *mem_cgroup_get_next(struct mem_cgroup *iter,
812 struct mem_cgroup *root,
813 bool cond)
814{
815 int nextid = css_id(&iter->css) + 1;
816 int found;
817 int hierarchy_used;
818 struct cgroup_subsys_state *css;
819
820 hierarchy_used = iter->use_hierarchy;
821
822 css_put(&iter->css);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700823 /* If no ROOT, walk all, ignore hierarchy */
824 if (!cond || (root && !hierarchy_used))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700825 return NULL;
826
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700827 if (!root)
828 root = root_mem_cgroup;
829
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700830 do {
831 iter = NULL;
832 rcu_read_lock();
833
834 css = css_get_next(&mem_cgroup_subsys, nextid,
835 &root->css, &found);
836 if (css && css_tryget(css))
837 iter = container_of(css, struct mem_cgroup, css);
838 rcu_read_unlock();
839 /* If css is NULL, no more cgroups will be found */
840 nextid = found + 1;
841 } while (css && !iter);
842
843 return iter;
844}
845/*
846 * for_eacn_mem_cgroup_tree() for visiting all cgroup under tree. Please
847 * be careful that "break" loop is not allowed. We have reference count.
848 * Instead of that modify "cond" to be false and "continue" to exit the loop.
849 */
850#define for_each_mem_cgroup_tree_cond(iter, root, cond) \
851 for (iter = mem_cgroup_start_loop(root);\
852 iter != NULL;\
853 iter = mem_cgroup_get_next(iter, root, cond))
854
855#define for_each_mem_cgroup_tree(iter, root) \
856 for_each_mem_cgroup_tree_cond(iter, root, true)
857
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700858#define for_each_mem_cgroup_all(iter) \
859 for_each_mem_cgroup_tree_cond(iter, NULL, true)
860
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700861
Balbir Singh4b3bde42009-09-23 15:56:32 -0700862static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
863{
864 return (mem == root_mem_cgroup);
865}
866
Ying Han456f9982011-05-26 16:25:38 -0700867void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
868{
869 struct mem_cgroup *mem;
870
871 if (!mm)
872 return;
873
874 rcu_read_lock();
875 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
876 if (unlikely(!mem))
877 goto out;
878
879 switch (idx) {
880 case PGMAJFAULT:
881 mem_cgroup_pgmajfault(mem, 1);
882 break;
883 case PGFAULT:
884 mem_cgroup_pgfault(mem, 1);
885 break;
886 default:
887 BUG();
888 }
889out:
890 rcu_read_unlock();
891}
892EXPORT_SYMBOL(mem_cgroup_count_vm_event);
893
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800894/*
895 * Following LRU functions are allowed to be used without PCG_LOCK.
896 * Operations are called by routine of global LRU independently from memcg.
897 * What we have to take care of here is validness of pc->mem_cgroup.
898 *
899 * Changes to pc->mem_cgroup happens when
900 * 1. charge
901 * 2. moving account
902 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
903 * It is added to LRU before charge.
904 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
905 * When moving account, the page is not on LRU. It's isolated.
906 */
907
908void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800909{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800910 struct page_cgroup *pc;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800911 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700912
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800913 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800914 return;
915 pc = lookup_page_cgroup(page);
916 /* can happen while we handle swapcache. */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700917 if (!TestClearPageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800918 return;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700919 VM_BUG_ON(!pc->mem_cgroup);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800920 /*
921 * We don't check PCG_USED bit. It's cleared when the "page" is finally
922 * removed from global LRU.
923 */
Johannes Weiner97a6c372011-03-23 16:42:27 -0700924 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -0800925 /* huge page split is done under lru_lock. so, we have no races. */
926 MEM_CGROUP_ZSTAT(mz, lru) -= 1 << compound_order(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700927 if (mem_cgroup_is_root(pc->mem_cgroup))
928 return;
929 VM_BUG_ON(list_empty(&pc->lru));
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800930 list_del_init(&pc->lru);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800931}
932
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800933void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800934{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800935 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800936}
937
Minchan Kim3f58a822011-03-22 16:32:53 -0700938/*
939 * Writeback is about to end against a page which has been marked for immediate
940 * reclaim. If it still appears to be reclaimable, move it to the tail of the
941 * inactive list.
942 */
943void mem_cgroup_rotate_reclaimable_page(struct page *page)
944{
945 struct mem_cgroup_per_zone *mz;
946 struct page_cgroup *pc;
947 enum lru_list lru = page_lru(page);
948
949 if (mem_cgroup_disabled())
950 return;
951
952 pc = lookup_page_cgroup(page);
953 /* unused or root page is not rotated. */
954 if (!PageCgroupUsed(pc))
955 return;
956 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
957 smp_rmb();
958 if (mem_cgroup_is_root(pc->mem_cgroup))
959 return;
Johannes Weiner97a6c372011-03-23 16:42:27 -0700960 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
Minchan Kim3f58a822011-03-22 16:32:53 -0700961 list_move_tail(&pc->lru, &mz->lists[lru]);
962}
963
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800964void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800965{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800966 struct mem_cgroup_per_zone *mz;
967 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800968
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800969 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700970 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700971
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800972 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700973 /* unused or root page is not rotated. */
Johannes Weiner713735b2011-01-20 14:44:31 -0800974 if (!PageCgroupUsed(pc))
975 return;
976 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
977 smp_rmb();
978 if (mem_cgroup_is_root(pc->mem_cgroup))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800979 return;
Johannes Weiner97a6c372011-03-23 16:42:27 -0700980 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700981 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800982}
983
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800984void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
985{
986 struct page_cgroup *pc;
987 struct mem_cgroup_per_zone *mz;
988
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800989 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800990 return;
991 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700992 VM_BUG_ON(PageCgroupAcctLRU(pc));
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800993 if (!PageCgroupUsed(pc))
994 return;
Johannes Weiner713735b2011-01-20 14:44:31 -0800995 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
996 smp_rmb();
Johannes Weiner97a6c372011-03-23 16:42:27 -0700997 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -0800998 /* huge page split is done under lru_lock. so, we have no races. */
999 MEM_CGROUP_ZSTAT(mz, lru) += 1 << compound_order(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -07001000 SetPageCgroupAcctLRU(pc);
1001 if (mem_cgroup_is_root(pc->mem_cgroup))
1002 return;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001003 list_add(&pc->lru, &mz->lists[lru]);
1004}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001005
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001006/*
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001007 * At handling SwapCache and other FUSE stuff, pc->mem_cgroup may be changed
1008 * while it's linked to lru because the page may be reused after it's fully
1009 * uncharged. To handle that, unlink page_cgroup from LRU when charge it again.
1010 * It's done under lock_page and expected that zone->lru_lock isnever held.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001011 */
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001012static void mem_cgroup_lru_del_before_commit(struct page *page)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001013{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001014 unsigned long flags;
1015 struct zone *zone = page_zone(page);
1016 struct page_cgroup *pc = lookup_page_cgroup(page);
1017
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001018 /*
1019 * Doing this check without taking ->lru_lock seems wrong but this
1020 * is safe. Because if page_cgroup's USED bit is unset, the page
1021 * will not be added to any memcg's LRU. If page_cgroup's USED bit is
1022 * set, the commit after this will fail, anyway.
1023 * This all charge/uncharge is done under some mutual execustion.
1024 * So, we don't need to taking care of changes in USED bit.
1025 */
1026 if (likely(!PageLRU(page)))
1027 return;
1028
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001029 spin_lock_irqsave(&zone->lru_lock, flags);
1030 /*
1031 * Forget old LRU when this page_cgroup is *not* used. This Used bit
1032 * is guarded by lock_page() because the page is SwapCache.
1033 */
1034 if (!PageCgroupUsed(pc))
1035 mem_cgroup_del_lru_list(page, page_lru(page));
1036 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001037}
1038
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001039static void mem_cgroup_lru_add_after_commit(struct page *page)
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001040{
1041 unsigned long flags;
1042 struct zone *zone = page_zone(page);
1043 struct page_cgroup *pc = lookup_page_cgroup(page);
1044
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001045 /* taking care of that the page is added to LRU while we commit it */
1046 if (likely(!PageLRU(page)))
1047 return;
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001048 spin_lock_irqsave(&zone->lru_lock, flags);
1049 /* link when the page is linked to LRU but page_cgroup isn't */
Balbir Singh4b3bde42009-09-23 15:56:32 -07001050 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001051 mem_cgroup_add_lru_list(page, page_lru(page));
1052 spin_unlock_irqrestore(&zone->lru_lock, flags);
1053}
1054
1055
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001056void mem_cgroup_move_lists(struct page *page,
1057 enum lru_list from, enum lru_list to)
1058{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001059 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001060 return;
1061 mem_cgroup_del_lru_list(page, from);
1062 mem_cgroup_add_lru_list(page, to);
1063}
1064
Michal Hocko3e920412011-07-26 16:08:29 -07001065/*
1066 * Checks whether given mem is same or in the root_mem's
1067 * hierarchy subtree
1068 */
1069static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_mem,
1070 struct mem_cgroup *mem)
1071{
1072 if (root_mem != mem) {
1073 return (root_mem->use_hierarchy &&
1074 css_is_ancestor(&mem->css, &root_mem->css));
1075 }
1076
1077 return true;
1078}
1079
David Rientjes4c4a2212008-02-07 00:14:06 -08001080int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
1081{
1082 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001083 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001084 struct task_struct *p;
David Rientjes4c4a2212008-02-07 00:14:06 -08001085
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001086 p = find_lock_task_mm(task);
1087 if (!p)
1088 return 0;
1089 curr = try_get_mem_cgroup_from_mm(p->mm);
1090 task_unlock(p);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001091 if (!curr)
1092 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001093 /*
1094 * We should check use_hierarchy of "mem" not "curr". Because checking
1095 * use_hierarchy of "curr" here make this function true if hierarchy is
1096 * enabled in "curr" and "curr" is a child of "mem" in *cgroup*
1097 * hierarchy(even if use_hierarchy is disabled in "mem").
1098 */
Michal Hocko3e920412011-07-26 16:08:29 -07001099 ret = mem_cgroup_same_or_subtree(mem, curr);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001100 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001101 return ret;
1102}
1103
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001104static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001105{
1106 unsigned long active;
1107 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001108 unsigned long gb;
1109 unsigned long inactive_ratio;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001110
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07001111 inactive = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_ANON));
1112 active = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_ANON));
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001113
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001114 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1115 if (gb)
1116 inactive_ratio = int_sqrt(10 * gb);
1117 else
1118 inactive_ratio = 1;
1119
1120 if (present_pages) {
1121 present_pages[0] = inactive;
1122 present_pages[1] = active;
1123 }
1124
1125 return inactive_ratio;
1126}
1127
1128int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
1129{
1130 unsigned long active;
1131 unsigned long inactive;
1132 unsigned long present_pages[2];
1133 unsigned long inactive_ratio;
1134
1135 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
1136
1137 inactive = present_pages[0];
1138 active = present_pages[1];
1139
1140 if (inactive * inactive_ratio < active)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001141 return 1;
1142
1143 return 0;
1144}
1145
Rik van Riel56e49d22009-06-16 15:32:28 -07001146int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
1147{
1148 unsigned long active;
1149 unsigned long inactive;
1150
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07001151 inactive = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_FILE));
1152 active = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_FILE));
Rik van Riel56e49d22009-06-16 15:32:28 -07001153
1154 return (active > inactive);
1155}
1156
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001157struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
1158 struct zone *zone)
1159{
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -07001160 int nid = zone_to_nid(zone);
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001161 int zid = zone_idx(zone);
1162 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
1163
1164 return &mz->reclaim_stat;
1165}
1166
1167struct zone_reclaim_stat *
1168mem_cgroup_get_reclaim_stat_from_page(struct page *page)
1169{
1170 struct page_cgroup *pc;
1171 struct mem_cgroup_per_zone *mz;
1172
1173 if (mem_cgroup_disabled())
1174 return NULL;
1175
1176 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -08001177 if (!PageCgroupUsed(pc))
1178 return NULL;
Johannes Weiner713735b2011-01-20 14:44:31 -08001179 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
1180 smp_rmb();
Johannes Weiner97a6c372011-03-23 16:42:27 -07001181 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001182 return &mz->reclaim_stat;
1183}
1184
Balbir Singh66e17072008-02-07 00:13:56 -08001185unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
1186 struct list_head *dst,
1187 unsigned long *scanned, int order,
Minchan Kim4356f212011-10-31 17:06:47 -07001188 isolate_mode_t mode,
1189 struct zone *z,
Balbir Singh66e17072008-02-07 00:13:56 -08001190 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001191 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -08001192{
1193 unsigned long nr_taken = 0;
1194 struct page *page;
1195 unsigned long scan;
1196 LIST_HEAD(pc_list);
1197 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001198 struct page_cgroup *pc, *tmp;
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -07001199 int nid = zone_to_nid(z);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001200 int zid = zone_idx(z);
1201 struct mem_cgroup_per_zone *mz;
Johannes Weinerb7c46d12009-09-21 17:02:56 -07001202 int lru = LRU_FILE * file + active;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001203 int ret;
Balbir Singh66e17072008-02-07 00:13:56 -08001204
Balbir Singhcf475ad2008-04-29 01:00:16 -07001205 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001206 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001207 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -08001208
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001209 scan = 0;
1210 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -08001211 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001212 break;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001213
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001214 if (unlikely(!PageCgroupUsed(pc)))
1215 continue;
Johannes Weiner5564e882011-03-23 16:42:29 -07001216
Johannes Weiner6b3ae582011-03-23 16:42:30 -07001217 page = lookup_cgroup_page(pc);
Johannes Weiner5564e882011-03-23 16:42:29 -07001218
Hugh Dickins436c65412008-02-07 00:14:12 -08001219 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001220 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001221
Hugh Dickins436c65412008-02-07 00:14:12 -08001222 scan++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001223 ret = __isolate_lru_page(page, mode, file);
1224 switch (ret) {
1225 case 0:
Balbir Singh66e17072008-02-07 00:13:56 -08001226 list_move(&page->lru, dst);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001227 mem_cgroup_del_lru(page);
Rik van Riel2c888cf2011-01-13 15:47:13 -08001228 nr_taken += hpage_nr_pages(page);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001229 break;
1230 case -EBUSY:
1231 /* we don't affect global LRU but rotate in our LRU */
1232 mem_cgroup_rotate_lru_list(page, page_lru(page));
1233 break;
1234 default:
1235 break;
Balbir Singh66e17072008-02-07 00:13:56 -08001236 }
1237 }
1238
Balbir Singh66e17072008-02-07 00:13:56 -08001239 *scanned = scan;
KOSAKI Motohirocc8e9702010-08-09 17:19:57 -07001240
1241 trace_mm_vmscan_memcg_isolate(0, nr_to_scan, scan, nr_taken,
1242 0, 0, 0, mode);
1243
Balbir Singh66e17072008-02-07 00:13:56 -08001244 return nr_taken;
1245}
1246
Balbir Singh6d61ef42009-01-07 18:08:06 -08001247#define mem_cgroup_from_res_counter(counter, member) \
1248 container_of(counter, struct mem_cgroup, member)
1249
Johannes Weiner19942822011-02-01 15:52:43 -08001250/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001251 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
1252 * @mem: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001253 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001254 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001255 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001256 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001257static unsigned long mem_cgroup_margin(struct mem_cgroup *mem)
Johannes Weiner19942822011-02-01 15:52:43 -08001258{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001259 unsigned long long margin;
1260
1261 margin = res_counter_margin(&mem->res);
1262 if (do_swap_account)
1263 margin = min(margin, res_counter_margin(&mem->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001264 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001265}
1266
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001267int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001268{
1269 struct cgroup *cgrp = memcg->css.cgroup;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001270
1271 /* root ? */
1272 if (cgrp->parent == NULL)
1273 return vm_swappiness;
1274
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001275 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001276}
1277
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001278static void mem_cgroup_start_move(struct mem_cgroup *mem)
1279{
1280 int cpu;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001281
1282 get_online_cpus();
1283 spin_lock(&mem->pcp_counter_lock);
1284 for_each_online_cpu(cpu)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001285 per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) += 1;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001286 mem->nocpu_base.count[MEM_CGROUP_ON_MOVE] += 1;
1287 spin_unlock(&mem->pcp_counter_lock);
1288 put_online_cpus();
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001289
1290 synchronize_rcu();
1291}
1292
1293static void mem_cgroup_end_move(struct mem_cgroup *mem)
1294{
1295 int cpu;
1296
1297 if (!mem)
1298 return;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001299 get_online_cpus();
1300 spin_lock(&mem->pcp_counter_lock);
1301 for_each_online_cpu(cpu)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001302 per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) -= 1;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001303 mem->nocpu_base.count[MEM_CGROUP_ON_MOVE] -= 1;
1304 spin_unlock(&mem->pcp_counter_lock);
1305 put_online_cpus();
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001306}
1307/*
1308 * 2 routines for checking "mem" is under move_account() or not.
1309 *
1310 * mem_cgroup_stealed() - checking a cgroup is mc.from or not. This is used
1311 * for avoiding race in accounting. If true,
1312 * pc->mem_cgroup may be overwritten.
1313 *
1314 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1315 * under hierarchy of moving cgroups. This is for
1316 * waiting at hith-memory prressure caused by "move".
1317 */
1318
1319static bool mem_cgroup_stealed(struct mem_cgroup *mem)
1320{
1321 VM_BUG_ON(!rcu_read_lock_held());
1322 return this_cpu_read(mem->stat->count[MEM_CGROUP_ON_MOVE]) > 0;
1323}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001324
1325static bool mem_cgroup_under_move(struct mem_cgroup *mem)
1326{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001327 struct mem_cgroup *from;
1328 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001329 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001330 /*
1331 * Unlike task_move routines, we access mc.to, mc.from not under
1332 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1333 */
1334 spin_lock(&mc.lock);
1335 from = mc.from;
1336 to = mc.to;
1337 if (!from)
1338 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001339
1340 ret = mem_cgroup_same_or_subtree(mem, from)
1341 || mem_cgroup_same_or_subtree(mem, to);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001342unlock:
1343 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001344 return ret;
1345}
1346
1347static bool mem_cgroup_wait_acct_move(struct mem_cgroup *mem)
1348{
1349 if (mc.moving_task && current != mc.moving_task) {
1350 if (mem_cgroup_under_move(mem)) {
1351 DEFINE_WAIT(wait);
1352 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1353 /* moving charge context might have finished. */
1354 if (mc.moving_task)
1355 schedule();
1356 finish_wait(&mc.waitq, &wait);
1357 return true;
1358 }
1359 }
1360 return false;
1361}
1362
Balbir Singhe2224322009-04-02 16:57:39 -07001363/**
Kirill A. Shutemov6a6135b2010-03-10 15:22:25 -08001364 * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode.
Balbir Singhe2224322009-04-02 16:57:39 -07001365 * @memcg: The memory cgroup that went over limit
1366 * @p: Task that is going to be killed
1367 *
1368 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1369 * enabled
1370 */
1371void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1372{
1373 struct cgroup *task_cgrp;
1374 struct cgroup *mem_cgrp;
1375 /*
1376 * Need a buffer in BSS, can't rely on allocations. The code relies
1377 * on the assumption that OOM is serialized for memory controller.
1378 * If this assumption is broken, revisit this code.
1379 */
1380 static char memcg_name[PATH_MAX];
1381 int ret;
1382
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001383 if (!memcg || !p)
Balbir Singhe2224322009-04-02 16:57:39 -07001384 return;
1385
1386
1387 rcu_read_lock();
1388
1389 mem_cgrp = memcg->css.cgroup;
1390 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1391
1392 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1393 if (ret < 0) {
1394 /*
1395 * Unfortunately, we are unable to convert to a useful name
1396 * But we'll still print out the usage information
1397 */
1398 rcu_read_unlock();
1399 goto done;
1400 }
1401 rcu_read_unlock();
1402
1403 printk(KERN_INFO "Task in %s killed", memcg_name);
1404
1405 rcu_read_lock();
1406 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1407 if (ret < 0) {
1408 rcu_read_unlock();
1409 goto done;
1410 }
1411 rcu_read_unlock();
1412
1413 /*
1414 * Continues from above, so we don't need an KERN_ level
1415 */
1416 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1417done:
1418
1419 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1420 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1421 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1422 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1423 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1424 "failcnt %llu\n",
1425 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1426 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1427 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1428}
1429
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001430/*
1431 * This function returns the number of memcg under hierarchy tree. Returns
1432 * 1(self count) if no children.
1433 */
1434static int mem_cgroup_count_children(struct mem_cgroup *mem)
1435{
1436 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001437 struct mem_cgroup *iter;
1438
1439 for_each_mem_cgroup_tree(iter, mem)
1440 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001441 return num;
1442}
1443
Balbir Singh6d61ef42009-01-07 18:08:06 -08001444/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001445 * Return the memory (and swap, if configured) limit for a memcg.
1446 */
1447u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
1448{
1449 u64 limit;
1450 u64 memsw;
1451
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001452 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
1453 limit += total_swap_pages << PAGE_SHIFT;
1454
David Rientjesa63d83f2010-08-09 17:19:46 -07001455 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1456 /*
1457 * If memsw is finite and limits the amount of swap space available
1458 * to this memcg, return that limit.
1459 */
1460 return min(limit, memsw);
1461}
1462
1463/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001464 * Visit the first child (need not be the first child as per the ordering
1465 * of the cgroup list, since we track last_scanned_child) of @mem and use
1466 * that to reclaim free pages from.
1467 */
1468static struct mem_cgroup *
1469mem_cgroup_select_victim(struct mem_cgroup *root_mem)
1470{
1471 struct mem_cgroup *ret = NULL;
1472 struct cgroup_subsys_state *css;
1473 int nextid, found;
1474
1475 if (!root_mem->use_hierarchy) {
1476 css_get(&root_mem->css);
1477 ret = root_mem;
1478 }
1479
1480 while (!ret) {
1481 rcu_read_lock();
1482 nextid = root_mem->last_scanned_child + 1;
1483 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
1484 &found);
1485 if (css && css_tryget(css))
1486 ret = container_of(css, struct mem_cgroup, css);
1487
1488 rcu_read_unlock();
1489 /* Updates scanning parameter */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001490 if (!css) {
1491 /* this means start scan from ID:1 */
1492 root_mem->last_scanned_child = 0;
1493 } else
1494 root_mem->last_scanned_child = found;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001495 }
1496
1497 return ret;
1498}
1499
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001500/**
1501 * test_mem_cgroup_node_reclaimable
1502 * @mem: the target memcg
1503 * @nid: the node ID to be checked.
1504 * @noswap : specify true here if the user wants flle only information.
1505 *
1506 * This function returns whether the specified memcg contains any
1507 * reclaimable pages on a node. Returns true if there are any reclaimable
1508 * pages in the node.
1509 */
1510static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *mem,
1511 int nid, bool noswap)
1512{
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07001513 if (mem_cgroup_node_nr_lru_pages(mem, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001514 return true;
1515 if (noswap || !total_swap_pages)
1516 return false;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07001517 if (mem_cgroup_node_nr_lru_pages(mem, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001518 return true;
1519 return false;
1520
1521}
Ying Han889976d2011-05-26 16:25:33 -07001522#if MAX_NUMNODES > 1
1523
1524/*
1525 * Always updating the nodemask is not very good - even if we have an empty
1526 * list or the wrong list here, we can start from some node and traverse all
1527 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1528 *
1529 */
1530static void mem_cgroup_may_update_nodemask(struct mem_cgroup *mem)
1531{
1532 int nid;
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001533 /*
1534 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1535 * pagein/pageout changes since the last update.
1536 */
1537 if (!atomic_read(&mem->numainfo_events))
1538 return;
1539 if (atomic_inc_return(&mem->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001540 return;
1541
Ying Han889976d2011-05-26 16:25:33 -07001542 /* make a nodemask where this memcg uses memory from */
1543 mem->scan_nodes = node_states[N_HIGH_MEMORY];
1544
1545 for_each_node_mask(nid, node_states[N_HIGH_MEMORY]) {
1546
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001547 if (!test_mem_cgroup_node_reclaimable(mem, nid, false))
1548 node_clear(nid, mem->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001549 }
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001550
1551 atomic_set(&mem->numainfo_events, 0);
1552 atomic_set(&mem->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001553}
1554
1555/*
1556 * Selecting a node where we start reclaim from. Because what we need is just
1557 * reducing usage counter, start from anywhere is O,K. Considering
1558 * memory reclaim from current node, there are pros. and cons.
1559 *
1560 * Freeing memory from current node means freeing memory from a node which
1561 * we'll use or we've used. So, it may make LRU bad. And if several threads
1562 * hit limits, it will see a contention on a node. But freeing from remote
1563 * node means more costs for memory reclaim because of memory latency.
1564 *
1565 * Now, we use round-robin. Better algorithm is welcomed.
1566 */
1567int mem_cgroup_select_victim_node(struct mem_cgroup *mem)
1568{
1569 int node;
1570
1571 mem_cgroup_may_update_nodemask(mem);
1572 node = mem->last_scanned_node;
1573
1574 node = next_node(node, mem->scan_nodes);
1575 if (node == MAX_NUMNODES)
1576 node = first_node(mem->scan_nodes);
1577 /*
1578 * We call this when we hit limit, not when pages are added to LRU.
1579 * No LRU may hold pages because all pages are UNEVICTABLE or
1580 * memcg is too small and all pages are not on LRU. In that case,
1581 * we use curret node.
1582 */
1583 if (unlikely(node == MAX_NUMNODES))
1584 node = numa_node_id();
1585
1586 mem->last_scanned_node = node;
1587 return node;
1588}
1589
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001590/*
1591 * Check all nodes whether it contains reclaimable pages or not.
1592 * For quick scan, we make use of scan_nodes. This will allow us to skip
1593 * unused nodes. But scan_nodes is lazily updated and may not cotain
1594 * enough new information. We need to do double check.
1595 */
1596bool mem_cgroup_reclaimable(struct mem_cgroup *mem, bool noswap)
1597{
1598 int nid;
1599
1600 /*
1601 * quick check...making use of scan_node.
1602 * We can skip unused nodes.
1603 */
1604 if (!nodes_empty(mem->scan_nodes)) {
1605 for (nid = first_node(mem->scan_nodes);
1606 nid < MAX_NUMNODES;
1607 nid = next_node(nid, mem->scan_nodes)) {
1608
1609 if (test_mem_cgroup_node_reclaimable(mem, nid, noswap))
1610 return true;
1611 }
1612 }
1613 /*
1614 * Check rest of nodes.
1615 */
1616 for_each_node_state(nid, N_HIGH_MEMORY) {
1617 if (node_isset(nid, mem->scan_nodes))
1618 continue;
1619 if (test_mem_cgroup_node_reclaimable(mem, nid, noswap))
1620 return true;
1621 }
1622 return false;
1623}
1624
Ying Han889976d2011-05-26 16:25:33 -07001625#else
1626int mem_cgroup_select_victim_node(struct mem_cgroup *mem)
1627{
1628 return 0;
1629}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001630
1631bool mem_cgroup_reclaimable(struct mem_cgroup *mem, bool noswap)
1632{
1633 return test_mem_cgroup_node_reclaimable(mem, 0, noswap);
1634}
Ying Han889976d2011-05-26 16:25:33 -07001635#endif
1636
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001637/*
1638 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1639 * we reclaimed from, so that we don't end up penalizing one child extensively
1640 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001641 *
1642 * root_mem is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001643 *
1644 * We give up and return to the caller when we visit root_mem twice.
1645 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001646 *
1647 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001648 */
1649static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
Balbir Singh4e416952009-09-23 15:56:39 -07001650 struct zone *zone,
Balbir Singh75822b42009-09-23 15:56:38 -07001651 gfp_t gfp_mask,
Ying Han0ae5e892011-05-26 16:25:25 -07001652 unsigned long reclaim_options,
1653 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001654{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001655 struct mem_cgroup *victim;
1656 int ret, total = 0;
1657 int loop = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001658 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1659 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
Balbir Singh4e416952009-09-23 15:56:39 -07001660 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001661 unsigned long excess;
Johannes Weiner185efc02011-09-14 16:21:58 -07001662 unsigned long nr_scanned;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001663
1664 excess = res_counter_soft_limit_excess(&root_mem->res) >> PAGE_SHIFT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001665
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001666 /* If memsw_is_minimum==1, swap-out is of-no-use. */
Daisuke Nishimura108b6a72011-07-26 16:08:25 -07001667 if (!check_soft && !shrink && root_mem->memsw_is_minimum)
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001668 noswap = true;
1669
Balbir Singh4e416952009-09-23 15:56:39 -07001670 while (1) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001671 victim = mem_cgroup_select_victim(root_mem);
Balbir Singh4e416952009-09-23 15:56:39 -07001672 if (victim == root_mem) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001673 loop++;
KAMEZAWA Hiroyukifbc29a22011-06-15 15:08:46 -07001674 /*
1675 * We are not draining per cpu cached charges during
1676 * soft limit reclaim because global reclaim doesn't
1677 * care about charges. It tries to free some memory and
1678 * charges will not give any.
1679 */
1680 if (!check_soft && loop >= 1)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07001681 drain_all_stock_async(root_mem);
Balbir Singh4e416952009-09-23 15:56:39 -07001682 if (loop >= 2) {
1683 /*
1684 * If we have not been able to reclaim
1685 * anything, it might because there are
1686 * no reclaimable pages under this hierarchy
1687 */
1688 if (!check_soft || !total) {
1689 css_put(&victim->css);
1690 break;
1691 }
1692 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001693 * We want to do more targeted reclaim.
Balbir Singh4e416952009-09-23 15:56:39 -07001694 * excess >> 2 is not to excessive so as to
1695 * reclaim too much, nor too less that we keep
1696 * coming back to reclaim from this cgroup
1697 */
1698 if (total >= (excess >> 2) ||
1699 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1700 css_put(&victim->css);
1701 break;
1702 }
1703 }
1704 }
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001705 if (!mem_cgroup_reclaimable(victim, noswap)) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001706 /* this cgroup's local usage == 0 */
1707 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001708 continue;
1709 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001710 /* we use swappiness of local cgroup */
Ying Han0ae5e892011-05-26 16:25:25 -07001711 if (check_soft) {
Balbir Singh4e416952009-09-23 15:56:39 -07001712 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
Johannes Weiner185efc02011-09-14 16:21:58 -07001713 noswap, zone, &nr_scanned);
1714 *total_scanned += nr_scanned;
Ying Han0ae5e892011-05-26 16:25:25 -07001715 } else
Balbir Singh4e416952009-09-23 15:56:39 -07001716 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
Johannes Weiner185efc02011-09-14 16:21:58 -07001717 noswap);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001718 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001719 /*
1720 * At shrinking usage, we can't check we should stop here or
1721 * reclaim more. It's depends on callers. last_scanned_child
1722 * will work enough for keeping fairness under tree.
1723 */
1724 if (shrink)
1725 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001726 total += ret;
Balbir Singh4e416952009-09-23 15:56:39 -07001727 if (check_soft) {
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001728 if (!res_counter_soft_limit_excess(&root_mem->res))
Balbir Singh4e416952009-09-23 15:56:39 -07001729 return total;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001730 } else if (mem_cgroup_margin(root_mem))
Johannes Weiner4fd14eb2011-05-26 16:25:35 -07001731 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001732 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001733 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001734}
1735
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001736/*
1737 * Check OOM-Killer is already running under our hierarchy.
1738 * If someone is running, return false.
Michal Hocko1af8efe2011-07-26 16:08:24 -07001739 * Has to be called with memcg_oom_lock
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001740 */
1741static bool mem_cgroup_oom_lock(struct mem_cgroup *mem)
1742{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001743 struct mem_cgroup *iter, *failed = NULL;
1744 bool cond = true;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001745
Michal Hocko79dfdac2011-07-26 16:08:23 -07001746 for_each_mem_cgroup_tree_cond(iter, mem, cond) {
Johannes Weiner23751be2011-08-25 15:59:16 -07001747 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001748 /*
1749 * this subtree of our hierarchy is already locked
1750 * so we cannot give a lock.
1751 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07001752 failed = iter;
1753 cond = false;
Johannes Weiner23751be2011-08-25 15:59:16 -07001754 } else
1755 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001756 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001757
Michal Hocko79dfdac2011-07-26 16:08:23 -07001758 if (!failed)
Johannes Weiner23751be2011-08-25 15:59:16 -07001759 return true;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001760
1761 /*
1762 * OK, we failed to lock the whole subtree so we have to clean up
1763 * what we set up to the failing subtree
1764 */
1765 cond = true;
1766 for_each_mem_cgroup_tree_cond(iter, mem, cond) {
1767 if (iter == failed) {
1768 cond = false;
1769 continue;
1770 }
1771 iter->oom_lock = false;
1772 }
Johannes Weiner23751be2011-08-25 15:59:16 -07001773 return false;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001774}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001775
Michal Hocko79dfdac2011-07-26 16:08:23 -07001776/*
Michal Hocko1af8efe2011-07-26 16:08:24 -07001777 * Has to be called with memcg_oom_lock
Michal Hocko79dfdac2011-07-26 16:08:23 -07001778 */
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001779static int mem_cgroup_oom_unlock(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001780{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001781 struct mem_cgroup *iter;
1782
Michal Hocko79dfdac2011-07-26 16:08:23 -07001783 for_each_mem_cgroup_tree(iter, mem)
1784 iter->oom_lock = false;
1785 return 0;
1786}
1787
1788static void mem_cgroup_mark_under_oom(struct mem_cgroup *mem)
1789{
1790 struct mem_cgroup *iter;
1791
1792 for_each_mem_cgroup_tree(iter, mem)
1793 atomic_inc(&iter->under_oom);
1794}
1795
1796static void mem_cgroup_unmark_under_oom(struct mem_cgroup *mem)
1797{
1798 struct mem_cgroup *iter;
1799
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001800 /*
1801 * When a new child is created while the hierarchy is under oom,
1802 * mem_cgroup_oom_lock() may not be called. We have to use
1803 * atomic_add_unless() here.
1804 */
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001805 for_each_mem_cgroup_tree(iter, mem)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001806 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001807}
1808
Michal Hocko1af8efe2011-07-26 16:08:24 -07001809static DEFINE_SPINLOCK(memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001810static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1811
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001812struct oom_wait_info {
1813 struct mem_cgroup *mem;
1814 wait_queue_t wait;
1815};
1816
1817static int memcg_oom_wake_function(wait_queue_t *wait,
1818 unsigned mode, int sync, void *arg)
1819{
Michal Hocko3e920412011-07-26 16:08:29 -07001820 struct mem_cgroup *wake_mem = (struct mem_cgroup *)arg,
1821 *oom_wait_mem;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001822 struct oom_wait_info *oom_wait_info;
1823
1824 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Michal Hocko3e920412011-07-26 16:08:29 -07001825 oom_wait_mem = oom_wait_info->mem;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001826
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001827 /*
1828 * Both of oom_wait_info->mem and wake_mem are stable under us.
1829 * Then we can use css_is_ancestor without taking care of RCU.
1830 */
Michal Hocko3e920412011-07-26 16:08:29 -07001831 if (!mem_cgroup_same_or_subtree(oom_wait_mem, wake_mem)
1832 && !mem_cgroup_same_or_subtree(wake_mem, oom_wait_mem))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001833 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001834 return autoremove_wake_function(wait, mode, sync, arg);
1835}
1836
1837static void memcg_wakeup_oom(struct mem_cgroup *mem)
1838{
1839 /* for filtering, pass "mem" as argument. */
1840 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, mem);
1841}
1842
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001843static void memcg_oom_recover(struct mem_cgroup *mem)
1844{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001845 if (mem && atomic_read(&mem->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001846 memcg_wakeup_oom(mem);
1847}
1848
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001849/*
1850 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
1851 */
1852bool mem_cgroup_handle_oom(struct mem_cgroup *mem, gfp_t mask)
1853{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001854 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001855 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001856
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001857 owait.mem = mem;
1858 owait.wait.flags = 0;
1859 owait.wait.func = memcg_oom_wake_function;
1860 owait.wait.private = current;
1861 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001862 need_to_kill = true;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001863 mem_cgroup_mark_under_oom(mem);
1864
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001865 /* At first, try to OOM lock hierarchy under mem.*/
Michal Hocko1af8efe2011-07-26 16:08:24 -07001866 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001867 locked = mem_cgroup_oom_lock(mem);
1868 /*
1869 * Even if signal_pending(), we can't quit charge() loop without
1870 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
1871 * under OOM is always welcomed, use TASK_KILLABLE here.
1872 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001873 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
1874 if (!locked || mem->oom_kill_disable)
1875 need_to_kill = false;
1876 if (locked)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07001877 mem_cgroup_oom_notify(mem);
Michal Hocko1af8efe2011-07-26 16:08:24 -07001878 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001879
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001880 if (need_to_kill) {
1881 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001882 mem_cgroup_out_of_memory(mem, mask);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001883 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001884 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001885 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001886 }
Michal Hocko1af8efe2011-07-26 16:08:24 -07001887 spin_lock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001888 if (locked)
1889 mem_cgroup_oom_unlock(mem);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001890 memcg_wakeup_oom(mem);
Michal Hocko1af8efe2011-07-26 16:08:24 -07001891 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001892
Michal Hocko79dfdac2011-07-26 16:08:23 -07001893 mem_cgroup_unmark_under_oom(mem);
1894
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001895 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
1896 return false;
1897 /* Give chance to dying process */
1898 schedule_timeout(1);
1899 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001900}
1901
Balbir Singhd69b0422009-06-17 16:26:34 -07001902/*
1903 * Currently used to update mapped file statistics, but the routine can be
1904 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001905 *
1906 * Notes: Race condition
1907 *
1908 * We usually use page_cgroup_lock() for accessing page_cgroup member but
1909 * it tends to be costly. But considering some conditions, we doesn't need
1910 * to do so _always_.
1911 *
1912 * Considering "charge", lock_page_cgroup() is not required because all
1913 * file-stat operations happen after a page is attached to radix-tree. There
1914 * are no race with "charge".
1915 *
1916 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
1917 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
1918 * if there are race with "uncharge". Statistics itself is properly handled
1919 * by flags.
1920 *
1921 * Considering "move", this is an only case we see a race. To make the race
1922 * small, we check MEM_CGROUP_ON_MOVE percpu value and detect there are
1923 * possibility of race condition. If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07001924 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001925
Greg Thelen2a7106f2011-01-13 15:47:37 -08001926void mem_cgroup_update_page_stat(struct page *page,
1927 enum mem_cgroup_page_stat_item idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07001928{
1929 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001930 struct page_cgroup *pc = lookup_page_cgroup(page);
1931 bool need_unlock = false;
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08001932 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07001933
Balbir Singhd69b0422009-06-17 16:26:34 -07001934 if (unlikely(!pc))
1935 return;
1936
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001937 rcu_read_lock();
Balbir Singhd69b0422009-06-17 16:26:34 -07001938 mem = pc->mem_cgroup;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001939 if (unlikely(!mem || !PageCgroupUsed(pc)))
1940 goto out;
1941 /* pc->mem_cgroup is unstable ? */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08001942 if (unlikely(mem_cgroup_stealed(mem)) || PageTransHuge(page)) {
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001943 /* take a lock against to access pc->mem_cgroup */
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08001944 move_lock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001945 need_unlock = true;
1946 mem = pc->mem_cgroup;
1947 if (!mem || !PageCgroupUsed(pc))
1948 goto out;
1949 }
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001950
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001951 switch (idx) {
Greg Thelen2a7106f2011-01-13 15:47:37 -08001952 case MEMCG_NR_FILE_MAPPED:
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001953 if (val > 0)
1954 SetPageCgroupFileMapped(pc);
1955 else if (!page_mapped(page))
KAMEZAWA Hiroyuki0c270f82010-10-27 15:33:39 -07001956 ClearPageCgroupFileMapped(pc);
Greg Thelen2a7106f2011-01-13 15:47:37 -08001957 idx = MEM_CGROUP_STAT_FILE_MAPPED;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001958 break;
1959 default:
1960 BUG();
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07001961 }
Balbir Singhd69b0422009-06-17 16:26:34 -07001962
Greg Thelen2a7106f2011-01-13 15:47:37 -08001963 this_cpu_add(mem->stat->count[idx], val);
1964
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001965out:
1966 if (unlikely(need_unlock))
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08001967 move_unlock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001968 rcu_read_unlock();
1969 return;
Balbir Singhd69b0422009-06-17 16:26:34 -07001970}
Greg Thelen2a7106f2011-01-13 15:47:37 -08001971EXPORT_SYMBOL(mem_cgroup_update_page_stat);
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001972
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001973/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001974 * size of first charge trial. "32" comes from vmscan.c's magic value.
1975 * TODO: maybe necessary to use big numbers in big irons.
1976 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001977#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001978struct memcg_stock_pcp {
1979 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07001980 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001981 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07001982 unsigned long flags;
1983#define FLUSHING_CACHED_CHARGE (0)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001984};
1985static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02001986static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001987
1988/*
Johannes Weiner11c9ea42011-03-23 16:42:34 -07001989 * Try to consume stocked charge on this cpu. If success, one page is consumed
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001990 * from local stock and true is returned. If the stock is 0 or charges from a
1991 * cgroup which is not current target, returns false. This stock will be
1992 * refilled.
1993 */
1994static bool consume_stock(struct mem_cgroup *mem)
1995{
1996 struct memcg_stock_pcp *stock;
1997 bool ret = true;
1998
1999 stock = &get_cpu_var(memcg_stock);
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002000 if (mem == stock->cached && stock->nr_pages)
2001 stock->nr_pages--;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002002 else /* need to call res_counter_charge */
2003 ret = false;
2004 put_cpu_var(memcg_stock);
2005 return ret;
2006}
2007
2008/*
2009 * Returns stocks cached in percpu to res_counter and reset cached information.
2010 */
2011static void drain_stock(struct memcg_stock_pcp *stock)
2012{
2013 struct mem_cgroup *old = stock->cached;
2014
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002015 if (stock->nr_pages) {
2016 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2017
2018 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002019 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002020 res_counter_uncharge(&old->memsw, bytes);
2021 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002022 }
2023 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002024}
2025
2026/*
2027 * This must be called under preempt disabled or must be called by
2028 * a thread which is pinned to local cpu.
2029 */
2030static void drain_local_stock(struct work_struct *dummy)
2031{
2032 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
2033 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002034 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002035}
2036
2037/*
2038 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002039 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002040 */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002041static void refill_stock(struct mem_cgroup *mem, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002042{
2043 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2044
2045 if (stock->cached != mem) { /* reset if necessary */
2046 drain_stock(stock);
2047 stock->cached = mem;
2048 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002049 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002050 put_cpu_var(memcg_stock);
2051}
2052
2053/*
Michal Hockod38144b2011-07-26 16:08:28 -07002054 * Drains all per-CPU charge caches for given root_mem resp. subtree
2055 * of the hierarchy under it. sync flag says whether we should block
2056 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002057 */
Michal Hockod38144b2011-07-26 16:08:28 -07002058static void drain_all_stock(struct mem_cgroup *root_mem, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002059{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002060 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002061
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002062 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002063 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002064 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002065 for_each_online_cpu(cpu) {
2066 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002067 struct mem_cgroup *mem;
2068
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002069 mem = stock->cached;
Michal Hockod1a05b62011-07-26 16:08:27 -07002070 if (!mem || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002071 continue;
Michal Hocko3e920412011-07-26 16:08:29 -07002072 if (!mem_cgroup_same_or_subtree(root_mem, mem))
2073 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002074 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2075 if (cpu == curcpu)
2076 drain_local_stock(&stock->work);
2077 else
2078 schedule_work_on(cpu, &stock->work);
2079 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002080 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002081 put_cpu();
Michal Hockod38144b2011-07-26 16:08:28 -07002082
2083 if (!sync)
2084 goto out;
2085
2086 for_each_online_cpu(cpu) {
2087 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002088 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002089 flush_work(&stock->work);
2090 }
2091out:
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002092 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002093}
2094
2095/*
2096 * Tries to drain stocked charges in other cpus. This function is asynchronous
2097 * and just put a work per cpu for draining localy on each cpu. Caller can
2098 * expects some charges will be back to res_counter later but cannot wait for
2099 * it.
2100 */
2101static void drain_all_stock_async(struct mem_cgroup *root_mem)
2102{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002103 /*
2104 * If someone calls draining, avoid adding more kworker runs.
2105 */
2106 if (!mutex_trylock(&percpu_charge_mutex))
2107 return;
Michal Hockod38144b2011-07-26 16:08:28 -07002108 drain_all_stock(root_mem, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002109 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002110}
2111
2112/* This is a synchronous drain interface. */
Michal Hockod38144b2011-07-26 16:08:28 -07002113static void drain_all_stock_sync(struct mem_cgroup *root_mem)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002114{
2115 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002116 mutex_lock(&percpu_charge_mutex);
Michal Hockod38144b2011-07-26 16:08:28 -07002117 drain_all_stock(root_mem, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002118 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002119}
2120
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002121/*
2122 * This function drains percpu counter value from DEAD cpu and
2123 * move it to local cpu. Note that this function can be preempted.
2124 */
2125static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *mem, int cpu)
2126{
2127 int i;
2128
2129 spin_lock(&mem->pcp_counter_lock);
2130 for (i = 0; i < MEM_CGROUP_STAT_DATA; i++) {
Johannes Weiner7a159cc2011-03-23 16:42:38 -07002131 long x = per_cpu(mem->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002132
2133 per_cpu(mem->stat->count[i], cpu) = 0;
2134 mem->nocpu_base.count[i] += x;
2135 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002136 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
2137 unsigned long x = per_cpu(mem->stat->events[i], cpu);
2138
2139 per_cpu(mem->stat->events[i], cpu) = 0;
2140 mem->nocpu_base.events[i] += x;
2141 }
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002142 /* need to clear ON_MOVE value, works as a kind of lock. */
2143 per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) = 0;
2144 spin_unlock(&mem->pcp_counter_lock);
2145}
2146
2147static void synchronize_mem_cgroup_on_move(struct mem_cgroup *mem, int cpu)
2148{
2149 int idx = MEM_CGROUP_ON_MOVE;
2150
2151 spin_lock(&mem->pcp_counter_lock);
2152 per_cpu(mem->stat->count[idx], cpu) = mem->nocpu_base.count[idx];
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002153 spin_unlock(&mem->pcp_counter_lock);
2154}
2155
2156static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002157 unsigned long action,
2158 void *hcpu)
2159{
2160 int cpu = (unsigned long)hcpu;
2161 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002162 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002163
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002164 if ((action == CPU_ONLINE)) {
2165 for_each_mem_cgroup_all(iter)
2166 synchronize_mem_cgroup_on_move(iter, cpu);
2167 return NOTIFY_OK;
2168 }
2169
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002170 if ((action != CPU_DEAD) || action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002171 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002172
2173 for_each_mem_cgroup_all(iter)
2174 mem_cgroup_drain_pcp_counter(iter, cpu);
2175
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002176 stock = &per_cpu(memcg_stock, cpu);
2177 drain_stock(stock);
2178 return NOTIFY_OK;
2179}
2180
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002181
2182/* See __mem_cgroup_try_charge() for details */
2183enum {
2184 CHARGE_OK, /* success */
2185 CHARGE_RETRY, /* need to retry but retry is not bad */
2186 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2187 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
2188 CHARGE_OOM_DIE, /* the current is killed because of OOM */
2189};
2190
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002191static int mem_cgroup_do_charge(struct mem_cgroup *mem, gfp_t gfp_mask,
2192 unsigned int nr_pages, bool oom_check)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002193{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002194 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002195 struct mem_cgroup *mem_over_limit;
2196 struct res_counter *fail_res;
2197 unsigned long flags = 0;
2198 int ret;
2199
2200 ret = res_counter_charge(&mem->res, csize, &fail_res);
2201
2202 if (likely(!ret)) {
2203 if (!do_swap_account)
2204 return CHARGE_OK;
2205 ret = res_counter_charge(&mem->memsw, csize, &fail_res);
2206 if (likely(!ret))
2207 return CHARGE_OK;
2208
KAMEZAWA Hiroyuki01c88e22011-01-25 15:07:27 -08002209 res_counter_uncharge(&mem->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002210 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2211 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2212 } else
2213 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002214 /*
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002215 * nr_pages can be either a huge page (HPAGE_PMD_NR), a batch
2216 * of regular pages (CHARGE_BATCH), or a single regular page (1).
Johannes Weiner9221edb2011-02-01 15:52:42 -08002217 *
2218 * Never reclaim on behalf of optional batching, retry with a
2219 * single page instead.
2220 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002221 if (nr_pages == CHARGE_BATCH)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002222 return CHARGE_RETRY;
2223
2224 if (!(gfp_mask & __GFP_WAIT))
2225 return CHARGE_WOULDBLOCK;
2226
2227 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
Ying Han0ae5e892011-05-26 16:25:25 -07002228 gfp_mask, flags, NULL);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002229 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002230 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002231 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002232 * Even though the limit is exceeded at this point, reclaim
2233 * may have been able to free some pages. Retry the charge
2234 * before killing the task.
2235 *
2236 * Only for regular pages, though: huge pages are rather
2237 * unlikely to succeed so close to the limit, and we fall back
2238 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002239 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002240 if (nr_pages == 1 && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002241 return CHARGE_RETRY;
2242
2243 /*
2244 * At task move, charge accounts can be doubly counted. So, it's
2245 * better to wait until the end of task_move if something is going on.
2246 */
2247 if (mem_cgroup_wait_acct_move(mem_over_limit))
2248 return CHARGE_RETRY;
2249
2250 /* If we don't need to call oom-killer at el, return immediately */
2251 if (!oom_check)
2252 return CHARGE_NOMEM;
2253 /* check OOM */
2254 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask))
2255 return CHARGE_OOM_DIE;
2256
2257 return CHARGE_RETRY;
2258}
2259
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002260/*
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002261 * Unlike exported interface, "oom" parameter is added. if oom==true,
2262 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002263 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002264static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002265 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002266 unsigned int nr_pages,
2267 struct mem_cgroup **memcg,
2268 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002269{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002270 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002271 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2272 struct mem_cgroup *mem = NULL;
2273 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002274
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002275 /*
2276 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2277 * in system level. So, allow to go ahead dying process in addition to
2278 * MEMDIE process.
2279 */
2280 if (unlikely(test_thread_flag(TIF_MEMDIE)
2281 || fatal_signal_pending(current)))
2282 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002283
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002284 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08002285 * We always charge the cgroup the mm_struct belongs to.
2286 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002287 * thread group leader migrates. It's possible that mm is not
2288 * set, if so charge the init_mm (happens for pagecache usage).
2289 */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002290 if (!*memcg && !mm)
2291 goto bypass;
2292again:
2293 if (*memcg) { /* css should be a valid one */
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002294 mem = *memcg;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002295 VM_BUG_ON(css_is_removed(&mem->css));
2296 if (mem_cgroup_is_root(mem))
2297 goto done;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002298 if (nr_pages == 1 && consume_stock(mem))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002299 goto done;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002300 css_get(&mem->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002301 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002302 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002303
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002304 rcu_read_lock();
2305 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002306 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002307 * Because we don't have task_lock(), "p" can exit.
2308 * In that case, "mem" can point to root or p can be NULL with
2309 * race with swapoff. Then, we have small risk of mis-accouning.
2310 * But such kind of mis-account by race always happens because
2311 * we don't have cgroup_mutex(). It's overkill and we allo that
2312 * small race, here.
2313 * (*) swapoff at el will charge against mm-struct not against
2314 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002315 */
2316 mem = mem_cgroup_from_task(p);
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002317 if (!mem || mem_cgroup_is_root(mem)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002318 rcu_read_unlock();
2319 goto done;
2320 }
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002321 if (nr_pages == 1 && consume_stock(mem)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002322 /*
2323 * It seems dagerous to access memcg without css_get().
2324 * But considering how consume_stok works, it's not
2325 * necessary. If consume_stock success, some charges
2326 * from this memcg are cached on this cpu. So, we
2327 * don't need to call css_get()/css_tryget() before
2328 * calling consume_stock().
2329 */
2330 rcu_read_unlock();
2331 goto done;
2332 }
2333 /* after here, we may be blocked. we need to get refcnt */
2334 if (!css_tryget(&mem->css)) {
2335 rcu_read_unlock();
2336 goto again;
2337 }
2338 rcu_read_unlock();
2339 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002340
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002341 do {
2342 bool oom_check;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002343
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002344 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002345 if (fatal_signal_pending(current)) {
2346 css_put(&mem->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002347 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002348 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002349
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002350 oom_check = false;
2351 if (oom && !nr_oom_retries) {
2352 oom_check = true;
2353 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2354 }
Balbir Singh6d61ef42009-01-07 18:08:06 -08002355
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002356 ret = mem_cgroup_do_charge(mem, gfp_mask, batch, oom_check);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002357 switch (ret) {
2358 case CHARGE_OK:
2359 break;
2360 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002361 batch = nr_pages;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002362 css_put(&mem->css);
2363 mem = NULL;
2364 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002365 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002366 css_put(&mem->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002367 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002368 case CHARGE_NOMEM: /* OOM routine works */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002369 if (!oom) {
2370 css_put(&mem->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002371 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002372 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002373 /* If oom, we never return -ENOMEM */
2374 nr_oom_retries--;
2375 break;
2376 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002377 css_put(&mem->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002378 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08002379 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002380 } while (ret != CHARGE_OK);
2381
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002382 if (batch > nr_pages)
2383 refill_stock(mem, batch - nr_pages);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002384 css_put(&mem->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002385done:
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002386 *memcg = mem;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002387 return 0;
2388nomem:
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002389 *memcg = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002390 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002391bypass:
2392 *memcg = NULL;
2393 return 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002394}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002395
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002396/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002397 * Somemtimes we have to undo a charge we got by try_charge().
2398 * This function is for that and do uncharge, put css's refcnt.
2399 * gotten by try_charge().
2400 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002401static void __mem_cgroup_cancel_charge(struct mem_cgroup *mem,
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002402 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002403{
2404 if (!mem_cgroup_is_root(mem)) {
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002405 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002406
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002407 res_counter_uncharge(&mem->res, bytes);
2408 if (do_swap_account)
2409 res_counter_uncharge(&mem->memsw, bytes);
2410 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002411}
2412
2413/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002414 * A helper function to get mem_cgroup from ID. must be called under
2415 * rcu_read_lock(). The caller must check css_is_removed() or some if
2416 * it's concern. (dropping refcnt from swap can be called against removed
2417 * memcg.)
2418 */
2419static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2420{
2421 struct cgroup_subsys_state *css;
2422
2423 /* ID 0 is unused ID */
2424 if (!id)
2425 return NULL;
2426 css = css_lookup(&mem_cgroup_subsys, id);
2427 if (!css)
2428 return NULL;
2429 return container_of(css, struct mem_cgroup, css);
2430}
2431
Wu Fengguange42d9d52009-12-16 12:19:59 +01002432struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002433{
Wu Fengguange42d9d52009-12-16 12:19:59 +01002434 struct mem_cgroup *mem = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002435 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002436 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002437 swp_entry_t ent;
2438
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002439 VM_BUG_ON(!PageLocked(page));
2440
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002441 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002442 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002443 if (PageCgroupUsed(pc)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002444 mem = pc->mem_cgroup;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002445 if (mem && !css_tryget(&mem->css))
2446 mem = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002447 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002448 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002449 id = lookup_swap_cgroup(ent);
2450 rcu_read_lock();
2451 mem = mem_cgroup_lookup(id);
2452 if (mem && !css_tryget(&mem->css))
2453 mem = NULL;
2454 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002455 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002456 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002457 return mem;
2458}
2459
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002460static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
Johannes Weiner5564e882011-03-23 16:42:29 -07002461 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002462 unsigned int nr_pages,
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002463 struct page_cgroup *pc,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002464 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002465{
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002466 lock_page_cgroup(pc);
2467 if (unlikely(PageCgroupUsed(pc))) {
2468 unlock_page_cgroup(pc);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002469 __mem_cgroup_cancel_charge(mem, nr_pages);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002470 return;
2471 }
2472 /*
2473 * we don't need page_cgroup_lock about tail pages, becase they are not
2474 * accessed by any other context at this point.
2475 */
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002476 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002477 /*
2478 * We access a page_cgroup asynchronously without lock_page_cgroup().
2479 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2480 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2481 * before USED bit, we need memory barrier here.
2482 * See mem_cgroup_add_lru_list(), etc.
2483 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002484 smp_wmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -07002485 switch (ctype) {
2486 case MEM_CGROUP_CHARGE_TYPE_CACHE:
2487 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
2488 SetPageCgroupCache(pc);
2489 SetPageCgroupUsed(pc);
2490 break;
2491 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
2492 ClearPageCgroupCache(pc);
2493 SetPageCgroupUsed(pc);
2494 break;
2495 default:
2496 break;
2497 }
Hugh Dickins3be91272008-02-07 00:14:19 -08002498
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002499 mem_cgroup_charge_statistics(mem, PageCgroupCache(pc), nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002500 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002501 /*
2502 * "charge_statistics" updated event counter. Then, check it.
2503 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2504 * if they exceeds softlimit.
2505 */
Johannes Weiner5564e882011-03-23 16:42:29 -07002506 memcg_check_events(mem, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002507}
2508
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002509#ifdef CONFIG_TRANSPARENT_HUGEPAGE
2510
2511#define PCGF_NOCOPY_AT_SPLIT ((1 << PCG_LOCK) | (1 << PCG_MOVE_LOCK) |\
2512 (1 << PCG_ACCT_LRU) | (1 << PCG_MIGRATION))
2513/*
2514 * Because tail pages are not marked as "used", set it. We're under
2515 * zone->lru_lock, 'splitting on pmd' and compund_lock.
2516 */
2517void mem_cgroup_split_huge_fixup(struct page *head, struct page *tail)
2518{
2519 struct page_cgroup *head_pc = lookup_page_cgroup(head);
2520 struct page_cgroup *tail_pc = lookup_page_cgroup(tail);
2521 unsigned long flags;
2522
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08002523 if (mem_cgroup_disabled())
2524 return;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002525 /*
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002526 * We have no races with charge/uncharge but will have races with
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002527 * page state accounting.
2528 */
2529 move_lock_page_cgroup(head_pc, &flags);
2530
2531 tail_pc->mem_cgroup = head_pc->mem_cgroup;
2532 smp_wmb(); /* see __commit_charge() */
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002533 if (PageCgroupAcctLRU(head_pc)) {
2534 enum lru_list lru;
2535 struct mem_cgroup_per_zone *mz;
2536
2537 /*
2538 * LRU flags cannot be copied because we need to add tail
2539 *.page to LRU by generic call and our hook will be called.
2540 * We hold lru_lock, then, reduce counter directly.
2541 */
2542 lru = page_lru(head);
Johannes Weiner97a6c372011-03-23 16:42:27 -07002543 mz = page_cgroup_zoneinfo(head_pc->mem_cgroup, head);
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002544 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
2545 }
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002546 tail_pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
2547 move_unlock_page_cgroup(head_pc, &flags);
2548}
2549#endif
2550
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002551/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07002552 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07002553 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002554 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002555 * @pc: page_cgroup of the page.
2556 * @from: mem_cgroup which the page is moved from.
2557 * @to: mem_cgroup which the page is moved to. @from != @to.
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002558 * @uncharge: whether we should call uncharge and css_put against @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002559 *
2560 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002561 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002562 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002563 *
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002564 * This function doesn't do "charge" nor css_get to new cgroup. It should be
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002565 * done by a caller(__mem_cgroup_try_charge would be useful). If @uncharge is
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002566 * true, this function does "uncharge" from old cgroup, but it doesn't if
2567 * @uncharge is false, so a caller should do "uncharge".
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002568 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002569static int mem_cgroup_move_account(struct page *page,
2570 unsigned int nr_pages,
2571 struct page_cgroup *pc,
2572 struct mem_cgroup *from,
2573 struct mem_cgroup *to,
2574 bool uncharge)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002575{
Johannes Weinerde3638d2011-03-23 16:42:28 -07002576 unsigned long flags;
2577 int ret;
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002578
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002579 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07002580 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07002581 /*
2582 * The page is isolated from LRU. So, collapse function
2583 * will not handle this page. But page splitting can happen.
2584 * Do this check under compound_page_lock(). The caller should
2585 * hold it.
2586 */
2587 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002588 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07002589 goto out;
2590
2591 lock_page_cgroup(pc);
2592
2593 ret = -EINVAL;
2594 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
2595 goto unlock;
2596
2597 move_lock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002598
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002599 if (PageCgroupFileMapped(pc)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08002600 /* Update mapped_file data for mem_cgroup */
2601 preempt_disable();
2602 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2603 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2604 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07002605 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002606 mem_cgroup_charge_statistics(from, PageCgroupCache(pc), -nr_pages);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002607 if (uncharge)
2608 /* This is not "cancel", but cancel_charge does all we need. */
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002609 __mem_cgroup_cancel_charge(from, nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07002610
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002611 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002612 pc->mem_cgroup = to;
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002613 mem_cgroup_charge_statistics(to, PageCgroupCache(pc), nr_pages);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002614 /*
2615 * We charges against "to" which may not have any tasks. Then, "to"
2616 * can be under rmdir(). But in current implementation, caller of
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08002617 * this function is just force_empty() and move charge, so it's
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002618 * guaranteed that "to" is never removed. So, we don't check rmdir
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08002619 * status here.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002620 */
Johannes Weinerde3638d2011-03-23 16:42:28 -07002621 move_unlock_page_cgroup(pc, &flags);
2622 ret = 0;
2623unlock:
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002624 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002625 /*
2626 * check events
2627 */
Johannes Weiner5564e882011-03-23 16:42:29 -07002628 memcg_check_events(to, page);
2629 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07002630out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002631 return ret;
2632}
2633
2634/*
2635 * move charges to its parent.
2636 */
2637
Johannes Weiner5564e882011-03-23 16:42:29 -07002638static int mem_cgroup_move_parent(struct page *page,
2639 struct page_cgroup *pc,
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002640 struct mem_cgroup *child,
2641 gfp_t gfp_mask)
2642{
2643 struct cgroup *cg = child->css.cgroup;
2644 struct cgroup *pcg = cg->parent;
2645 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002646 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07002647 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002648 int ret;
2649
2650 /* Is ROOT ? */
2651 if (!pcg)
2652 return -EINVAL;
2653
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002654 ret = -EBUSY;
2655 if (!get_page_unless_zero(page))
2656 goto out;
2657 if (isolate_lru_page(page))
2658 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08002659
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002660 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002661
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002662 parent = mem_cgroup_from_cont(pcg);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002663 ret = __mem_cgroup_try_charge(NULL, gfp_mask, nr_pages, &parent, false);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002664 if (ret || !parent)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002665 goto put_back;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002666
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002667 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002668 flags = compound_lock_irqsave(page);
2669
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002670 ret = mem_cgroup_move_account(page, nr_pages, pc, child, parent, true);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002671 if (ret)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002672 __mem_cgroup_cancel_charge(parent, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08002673
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002674 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002675 compound_unlock_irqrestore(page, flags);
Jesper Juhl8dba4742011-01-25 15:07:24 -08002676put_back:
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002677 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002678put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08002679 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002680out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002681 return ret;
2682}
2683
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002684/*
2685 * Charge the memory controller for page usage.
2686 * Return
2687 * 0 if the charge was successful
2688 * < 0 if the cgroup is over its limit
2689 */
2690static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002691 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002692{
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002693 struct mem_cgroup *mem = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002694 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08002695 struct page_cgroup *pc;
2696 bool oom = true;
2697 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08002698
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002699 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002700 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002701 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08002702 /*
2703 * Never OOM-kill a process for a huge page. The
2704 * fault handler will fall back to regular pages.
2705 */
2706 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002707 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002708
2709 pc = lookup_page_cgroup(page);
Johannes Weineraf4a6622011-03-23 16:42:24 -07002710 BUG_ON(!pc); /* XXX: remove this and move pc lookup into commit */
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002711
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002712 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &mem, oom);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002713 if (ret || !mem)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002714 return ret;
2715
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002716 __mem_cgroup_commit_charge(mem, page, nr_pages, pc, ctype);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002717 return 0;
2718}
2719
2720int mem_cgroup_newpage_charge(struct page *page,
2721 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002722{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002723 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002724 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002725 /*
2726 * If already mapped, we don't have to account.
2727 * If page cache, page->mapping has address_space.
2728 * But page->mapping may have out-of-use anon_vma pointer,
2729 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
2730 * is NULL.
2731 */
2732 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
2733 return 0;
2734 if (unlikely(!mm))
2735 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002736 return mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002737 MEM_CGROUP_CHARGE_TYPE_MAPPED);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002738}
2739
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002740static void
2741__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2742 enum charge_type ctype);
2743
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002744static void
2745__mem_cgroup_commit_charge_lrucare(struct page *page, struct mem_cgroup *mem,
2746 enum charge_type ctype)
2747{
2748 struct page_cgroup *pc = lookup_page_cgroup(page);
2749 /*
2750 * In some case, SwapCache, FUSE(splice_buf->radixtree), the page
2751 * is already on LRU. It means the page may on some other page_cgroup's
2752 * LRU. Take care of it.
2753 */
2754 mem_cgroup_lru_del_before_commit(page);
2755 __mem_cgroup_commit_charge(mem, page, 1, pc, ctype);
2756 mem_cgroup_lru_add_after_commit(page);
2757 return;
2758}
2759
Balbir Singhe1a1cd52008-02-07 00:14:02 -08002760int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
2761 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08002762{
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002763 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002764 int ret;
2765
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002766 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002767 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002768 if (PageCompound(page))
2769 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002770
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002771 if (unlikely(!mm))
Balbir Singh8697d332008-02-07 00:13:59 -08002772 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002773
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002774 if (page_is_file_cache(page)) {
2775 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, &mem, true);
2776 if (ret || !mem)
2777 return ret;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002778
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002779 /*
2780 * FUSE reuses pages without going through the final
2781 * put that would remove them from the LRU list, make
2782 * sure that they get relinked properly.
2783 */
2784 __mem_cgroup_commit_charge_lrucare(page, mem,
2785 MEM_CGROUP_CHARGE_TYPE_CACHE);
2786 return ret;
2787 }
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002788 /* shmem */
2789 if (PageSwapCache(page)) {
2790 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2791 if (!ret)
2792 __mem_cgroup_commit_charge_swapin(page, mem,
2793 MEM_CGROUP_CHARGE_TYPE_SHMEM);
2794 } else
2795 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002796 MEM_CGROUP_CHARGE_TYPE_SHMEM);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002797
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002798 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002799}
2800
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002801/*
2802 * While swap-in, try_charge -> commit or cancel, the page is locked.
2803 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02002804 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002805 * "commit()" or removed by "cancel()"
2806 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002807int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
2808 struct page *page,
2809 gfp_t mask, struct mem_cgroup **ptr)
2810{
2811 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002812 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002813
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07002814 *ptr = NULL;
2815
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002816 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002817 return 0;
2818
2819 if (!do_swap_account)
2820 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002821 /*
2822 * A racing thread's fault, or swapoff, may have already updated
Hugh Dickins407f9c82009-12-14 17:59:30 -08002823 * the pte, and even removed page from swap cache: in those cases
2824 * do_swap_page()'s pte_same() test will fail; but there's also a
2825 * KSM case which does need to charge the page.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002826 */
2827 if (!PageSwapCache(page))
Hugh Dickins407f9c82009-12-14 17:59:30 -08002828 goto charge_cur_mm;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002829 mem = try_get_mem_cgroup_from_page(page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002830 if (!mem)
2831 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002832 *ptr = mem;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002833 ret = __mem_cgroup_try_charge(NULL, mask, 1, ptr, true);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002834 css_put(&mem->css);
2835 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002836charge_cur_mm:
2837 if (unlikely(!mm))
2838 mm = &init_mm;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002839 return __mem_cgroup_try_charge(mm, mask, 1, ptr, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002840}
2841
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002842static void
2843__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2844 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002845{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002846 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002847 return;
2848 if (!ptr)
2849 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002850 cgroup_exclude_rmdir(&ptr->css);
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002851
2852 __mem_cgroup_commit_charge_lrucare(page, ptr, ctype);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002853 /*
2854 * Now swap is on-memory. This means this page may be
2855 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002856 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
2857 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
2858 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002859 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002860 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002861 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002862 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002863 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002864
2865 id = swap_cgroup_record(ent, 0);
2866 rcu_read_lock();
2867 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002868 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002869 /*
2870 * This recorded memcg can be obsolete one. So, avoid
2871 * calling css_tryget
2872 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002873 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002874 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002875 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002876 mem_cgroup_put(memcg);
2877 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002878 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002879 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002880 /*
2881 * At swapin, we may charge account against cgroup which has no tasks.
2882 * So, rmdir()->pre_destroy() can be called while we do this charge.
2883 * In that case, we need to call pre_destroy() again. check it here.
2884 */
2885 cgroup_release_and_wakeup_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002886}
2887
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002888void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
2889{
2890 __mem_cgroup_commit_charge_swapin(page, ptr,
2891 MEM_CGROUP_CHARGE_TYPE_MAPPED);
2892}
2893
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002894void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
2895{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002896 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002897 return;
2898 if (!mem)
2899 return;
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002900 __mem_cgroup_cancel_charge(mem, 1);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002901}
2902
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002903static void mem_cgroup_do_uncharge(struct mem_cgroup *mem,
2904 unsigned int nr_pages,
2905 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002906{
2907 struct memcg_batch_info *batch = NULL;
2908 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002909
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002910 /* If swapout, usage of swap doesn't decrease */
2911 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2912 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002913
2914 batch = &current->memcg_batch;
2915 /*
2916 * In usual, we do css_get() when we remember memcg pointer.
2917 * But in this case, we keep res->usage until end of a series of
2918 * uncharges. Then, it's ok to ignore memcg's refcnt.
2919 */
2920 if (!batch->memcg)
2921 batch->memcg = mem;
2922 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002923 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002924 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002925 * the same cgroup and we have chance to coalesce uncharges.
2926 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
2927 * because we want to do uncharge as soon as possible.
2928 */
2929
2930 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
2931 goto direct_uncharge;
2932
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002933 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08002934 goto direct_uncharge;
2935
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002936 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002937 * In typical case, batch->memcg == mem. This means we can
2938 * merge a series of uncharges to an uncharge of res_counter.
2939 * If not, we uncharge res_counter ony by one.
2940 */
2941 if (batch->memcg != mem)
2942 goto direct_uncharge;
2943 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07002944 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002945 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07002946 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002947 return;
2948direct_uncharge:
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002949 res_counter_uncharge(&mem->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002950 if (uncharge_memsw)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002951 res_counter_uncharge(&mem->memsw, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002952 if (unlikely(batch->memcg != mem))
2953 memcg_oom_recover(mem);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002954 return;
2955}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002956
Balbir Singh8697d332008-02-07 00:13:59 -08002957/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002958 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002959 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002960static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002961__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002962{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002963 struct mem_cgroup *mem = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002964 unsigned int nr_pages = 1;
2965 struct page_cgroup *pc;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002966
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002967 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002968 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07002969
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002970 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002971 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002972
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002973 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002974 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002975 VM_BUG_ON(!PageTransHuge(page));
2976 }
Balbir Singh8697d332008-02-07 00:13:59 -08002977 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08002978 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08002979 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002980 pc = lookup_page_cgroup(page);
2981 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002982 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002983
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002984 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002985
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002986 mem = pc->mem_cgroup;
2987
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002988 if (!PageCgroupUsed(pc))
2989 goto unlock_out;
2990
2991 switch (ctype) {
2992 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07002993 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002994 /* See mem_cgroup_prepare_migration() */
2995 if (page_mapped(page) || PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002996 goto unlock_out;
2997 break;
2998 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
2999 if (!PageAnon(page)) { /* Shared memory */
3000 if (page->mapping && !page_is_file_cache(page))
3001 goto unlock_out;
3002 } else if (page_mapped(page)) /* Anon */
3003 goto unlock_out;
3004 break;
3005 default:
3006 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003007 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003008
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003009 mem_cgroup_charge_statistics(mem, PageCgroupCache(pc), -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003010
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003011 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08003012 /*
3013 * pc->mem_cgroup is not cleared here. It will be accessed when it's
3014 * freed from LRU. This is safe because uncharged page is expected not
3015 * to be reused (freed soon). Exception is SwapCache, it's handled by
3016 * special functions.
3017 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003018
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003019 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003020 /*
3021 * even after unlock, we have mem->res.usage here and this memcg
3022 * will never be freed.
3023 */
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08003024 memcg_check_events(mem, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003025 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
3026 mem_cgroup_swap_statistics(mem, true);
3027 mem_cgroup_get(mem);
3028 }
3029 if (!mem_cgroup_is_root(mem))
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003030 mem_cgroup_do_uncharge(mem, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003031
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003032 return mem;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003033
3034unlock_out:
3035 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003036 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08003037}
3038
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003039void mem_cgroup_uncharge_page(struct page *page)
3040{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003041 /* early check. */
3042 if (page_mapped(page))
3043 return;
3044 if (page->mapping && !PageAnon(page))
3045 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003046 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
3047}
3048
3049void mem_cgroup_uncharge_cache_page(struct page *page)
3050{
3051 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07003052 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003053 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
3054}
3055
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003056/*
3057 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
3058 * In that cases, pages are freed continuously and we can expect pages
3059 * are in the same memcg. All these calls itself limits the number of
3060 * pages freed at once, then uncharge_start/end() is called properly.
3061 * This may be called prural(2) times in a context,
3062 */
3063
3064void mem_cgroup_uncharge_start(void)
3065{
3066 current->memcg_batch.do_batch++;
3067 /* We can do nest. */
3068 if (current->memcg_batch.do_batch == 1) {
3069 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003070 current->memcg_batch.nr_pages = 0;
3071 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003072 }
3073}
3074
3075void mem_cgroup_uncharge_end(void)
3076{
3077 struct memcg_batch_info *batch = &current->memcg_batch;
3078
3079 if (!batch->do_batch)
3080 return;
3081
3082 batch->do_batch--;
3083 if (batch->do_batch) /* If stacked, do nothing. */
3084 return;
3085
3086 if (!batch->memcg)
3087 return;
3088 /*
3089 * This "batch->memcg" is valid without any css_get/put etc...
3090 * bacause we hide charges behind us.
3091 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003092 if (batch->nr_pages)
3093 res_counter_uncharge(&batch->memcg->res,
3094 batch->nr_pages * PAGE_SIZE);
3095 if (batch->memsw_nr_pages)
3096 res_counter_uncharge(&batch->memcg->memsw,
3097 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003098 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003099 /* forget this pointer (for sanity check) */
3100 batch->memcg = NULL;
3101}
3102
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003103#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003104/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003105 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003106 * memcg information is recorded to swap_cgroup of "ent"
3107 */
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07003108void
3109mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003110{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003111 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07003112 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003113
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07003114 if (!swapout) /* this was a swap cache but the swap is unused ! */
3115 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
3116
3117 memcg = __mem_cgroup_uncharge_common(page, ctype);
3118
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003119 /*
3120 * record memcg information, if swapout && memcg != NULL,
3121 * mem_cgroup_get() was called in uncharge().
3122 */
3123 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003124 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003125}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003126#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003127
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003128#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3129/*
3130 * called from swap_entry_free(). remove record in swap_cgroup and
3131 * uncharge "memsw" account.
3132 */
3133void mem_cgroup_uncharge_swap(swp_entry_t ent)
3134{
3135 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003136 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003137
3138 if (!do_swap_account)
3139 return;
3140
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003141 id = swap_cgroup_record(ent, 0);
3142 rcu_read_lock();
3143 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003144 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003145 /*
3146 * We uncharge this because swap is freed.
3147 * This memcg can be obsolete one. We avoid calling css_tryget
3148 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003149 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07003150 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003151 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003152 mem_cgroup_put(memcg);
3153 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003154 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003155}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003156
3157/**
3158 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
3159 * @entry: swap entry to be moved
3160 * @from: mem_cgroup which the entry is moved from
3161 * @to: mem_cgroup which the entry is moved to
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003162 * @need_fixup: whether we should fixup res_counters and refcounts.
Daisuke Nishimura02491442010-03-10 15:22:17 -08003163 *
3164 * It succeeds only when the swap_cgroup's record for this entry is the same
3165 * as the mem_cgroup's id of @from.
3166 *
3167 * Returns 0 on success, -EINVAL on failure.
3168 *
3169 * The caller must have charged to @to, IOW, called res_counter_charge() about
3170 * both res and memsw, and called css_get().
3171 */
3172static int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003173 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003174{
3175 unsigned short old_id, new_id;
3176
3177 old_id = css_id(&from->css);
3178 new_id = css_id(&to->css);
3179
3180 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08003181 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003182 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003183 /*
3184 * This function is only called from task migration context now.
3185 * It postpones res_counter and refcount handling till the end
3186 * of task migration(mem_cgroup_clear_mc()) for performance
3187 * improvement. But we cannot postpone mem_cgroup_get(to)
3188 * because if the process that has been moved to @to does
3189 * swap-in, the refcount of @to might be decreased to 0.
3190 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08003191 mem_cgroup_get(to);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003192 if (need_fixup) {
3193 if (!mem_cgroup_is_root(from))
3194 res_counter_uncharge(&from->memsw, PAGE_SIZE);
3195 mem_cgroup_put(from);
3196 /*
3197 * we charged both to->res and to->memsw, so we should
3198 * uncharge to->res.
3199 */
3200 if (!mem_cgroup_is_root(to))
3201 res_counter_uncharge(&to->res, PAGE_SIZE);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003202 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08003203 return 0;
3204 }
3205 return -EINVAL;
3206}
3207#else
3208static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003209 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003210{
3211 return -EINVAL;
3212}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003213#endif
3214
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003215/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003216 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
3217 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003218 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003219int mem_cgroup_prepare_migration(struct page *page,
Miklos Szeredief6a3c62011-03-22 16:30:52 -07003220 struct page *newpage, struct mem_cgroup **ptr, gfp_t gfp_mask)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003221{
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003222 struct mem_cgroup *mem = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003223 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003224 enum charge_type ctype;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003225 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003226
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07003227 *ptr = NULL;
3228
Andrea Arcangeliec168512011-01-13 15:46:56 -08003229 VM_BUG_ON(PageTransHuge(page));
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003230 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07003231 return 0;
3232
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003233 pc = lookup_page_cgroup(page);
3234 lock_page_cgroup(pc);
3235 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003236 mem = pc->mem_cgroup;
3237 css_get(&mem->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003238 /*
3239 * At migrating an anonymous page, its mapcount goes down
3240 * to 0 and uncharge() will be called. But, even if it's fully
3241 * unmapped, migration may fail and this page has to be
3242 * charged again. We set MIGRATION flag here and delay uncharge
3243 * until end_migration() is called
3244 *
3245 * Corner Case Thinking
3246 * A)
3247 * When the old page was mapped as Anon and it's unmap-and-freed
3248 * while migration was ongoing.
3249 * If unmap finds the old page, uncharge() of it will be delayed
3250 * until end_migration(). If unmap finds a new page, it's
3251 * uncharged when it make mapcount to be 1->0. If unmap code
3252 * finds swap_migration_entry, the new page will not be mapped
3253 * and end_migration() will find it(mapcount==0).
3254 *
3255 * B)
3256 * When the old page was mapped but migraion fails, the kernel
3257 * remaps it. A charge for it is kept by MIGRATION flag even
3258 * if mapcount goes down to 0. We can do remap successfully
3259 * without charging it again.
3260 *
3261 * C)
3262 * The "old" page is under lock_page() until the end of
3263 * migration, so, the old page itself will not be swapped-out.
3264 * If the new page is swapped out before end_migraton, our
3265 * hook to usual swap-out path will catch the event.
3266 */
3267 if (PageAnon(page))
3268 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003269 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003270 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003271 /*
3272 * If the page is not charged at this point,
3273 * we return here.
3274 */
3275 if (!mem)
3276 return 0;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003277
Andrea Arcangeli93d5c9b2010-04-23 13:17:39 -04003278 *ptr = mem;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003279 ret = __mem_cgroup_try_charge(NULL, gfp_mask, 1, ptr, false);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003280 css_put(&mem->css);/* drop extra refcnt */
3281 if (ret || *ptr == NULL) {
3282 if (PageAnon(page)) {
3283 lock_page_cgroup(pc);
3284 ClearPageCgroupMigration(pc);
3285 unlock_page_cgroup(pc);
3286 /*
3287 * The old page may be fully unmapped while we kept it.
3288 */
3289 mem_cgroup_uncharge_page(page);
3290 }
3291 return -ENOMEM;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003292 }
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003293 /*
3294 * We charge new page before it's used/mapped. So, even if unlock_page()
3295 * is called before end_migration, we can catch all events on this new
3296 * page. In the case new page is migrated but not remapped, new page's
3297 * mapcount will be finally 0 and we call uncharge in end_migration().
3298 */
3299 pc = lookup_page_cgroup(newpage);
3300 if (PageAnon(page))
3301 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
3302 else if (page_is_file_cache(page))
3303 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
3304 else
3305 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003306 __mem_cgroup_commit_charge(mem, page, 1, pc, ctype);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003307 return ret;
3308}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08003309
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003310/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003311void mem_cgroup_end_migration(struct mem_cgroup *mem,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003312 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003313{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003314 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003315 struct page_cgroup *pc;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003316
3317 if (!mem)
3318 return;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003319 /* blocks rmdir() */
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003320 cgroup_exclude_rmdir(&mem->css);
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003321 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003322 used = oldpage;
3323 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003324 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003325 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003326 unused = oldpage;
3327 }
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003328 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003329 * We disallowed uncharge of pages under migration because mapcount
3330 * of the page goes down to zero, temporarly.
3331 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003332 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003333 pc = lookup_page_cgroup(oldpage);
3334 lock_page_cgroup(pc);
3335 ClearPageCgroupMigration(pc);
3336 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003337
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003338 __mem_cgroup_uncharge_common(unused, MEM_CGROUP_CHARGE_TYPE_FORCE);
3339
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003340 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003341 * If a page is a file cache, radix-tree replacement is very atomic
3342 * and we can skip this check. When it was an Anon page, its mapcount
3343 * goes down to 0. But because we added MIGRATION flage, it's not
3344 * uncharged yet. There are several case but page->mapcount check
3345 * and USED bit check in mem_cgroup_uncharge_page() will do enough
3346 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003347 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003348 if (PageAnon(used))
3349 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003350 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003351 * At migration, we may charge account against cgroup which has no
3352 * tasks.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003353 * So, rmdir()->pre_destroy() can be called while we do this charge.
3354 * In that case, we need to call pre_destroy() again. check it here.
3355 */
3356 cgroup_release_and_wakeup_rmdir(&mem->css);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003357}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08003358
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003359#ifdef CONFIG_DEBUG_VM
3360static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
3361{
3362 struct page_cgroup *pc;
3363
3364 pc = lookup_page_cgroup(page);
3365 if (likely(pc) && PageCgroupUsed(pc))
3366 return pc;
3367 return NULL;
3368}
3369
3370bool mem_cgroup_bad_page_check(struct page *page)
3371{
3372 if (mem_cgroup_disabled())
3373 return false;
3374
3375 return lookup_page_cgroup_used(page) != NULL;
3376}
3377
3378void mem_cgroup_print_bad_page(struct page *page)
3379{
3380 struct page_cgroup *pc;
3381
3382 pc = lookup_page_cgroup_used(page);
3383 if (pc) {
3384 int ret = -1;
3385 char *path;
3386
3387 printk(KERN_ALERT "pc:%p pc->flags:%lx pc->mem_cgroup:%p",
3388 pc, pc->flags, pc->mem_cgroup);
3389
3390 path = kmalloc(PATH_MAX, GFP_KERNEL);
3391 if (path) {
3392 rcu_read_lock();
3393 ret = cgroup_path(pc->mem_cgroup->css.cgroup,
3394 path, PATH_MAX);
3395 rcu_read_unlock();
3396 }
3397
3398 printk(KERN_CONT "(%s)\n",
3399 (ret < 0) ? "cannot get the path" : path);
3400 kfree(path);
3401 }
3402}
3403#endif
3404
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003405static DEFINE_MUTEX(set_limit_mutex);
3406
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08003407static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003408 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003409{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003410 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003411 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003412 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003413 int children = mem_cgroup_count_children(memcg);
3414 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003415 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003416
3417 /*
3418 * For keeping hierarchical_reclaim simple, how long we should retry
3419 * is depends on callers. We set our retry-count to be function
3420 * of # of children which we should visit in this loop.
3421 */
3422 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
3423
3424 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003425
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003426 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003427 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003428 if (signal_pending(current)) {
3429 ret = -EINTR;
3430 break;
3431 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003432 /*
3433 * Rather than hide all in some function, I do this in
3434 * open coded manner. You see what this really does.
3435 * We have to guarantee mem->res.limit < mem->memsw.limit.
3436 */
3437 mutex_lock(&set_limit_mutex);
3438 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3439 if (memswlimit < val) {
3440 ret = -EINVAL;
3441 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003442 break;
3443 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003444
3445 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3446 if (memlimit < val)
3447 enlarge = 1;
3448
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003449 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003450 if (!ret) {
3451 if (memswlimit == val)
3452 memcg->memsw_is_minimum = true;
3453 else
3454 memcg->memsw_is_minimum = false;
3455 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003456 mutex_unlock(&set_limit_mutex);
3457
3458 if (!ret)
3459 break;
3460
Bob Liuaa20d482009-12-15 16:47:14 -08003461 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Ying Han0ae5e892011-05-26 16:25:25 -07003462 MEM_CGROUP_RECLAIM_SHRINK,
3463 NULL);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003464 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
3465 /* Usage is reduced ? */
3466 if (curusage >= oldusage)
3467 retry_count--;
3468 else
3469 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003470 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003471 if (!ret && enlarge)
3472 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08003473
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003474 return ret;
3475}
3476
Li Zefan338c8432009-06-17 16:27:15 -07003477static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
3478 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003479{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003480 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003481 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003482 int children = mem_cgroup_count_children(memcg);
3483 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003484 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003485
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003486 /* see mem_cgroup_resize_res_limit */
3487 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
3488 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003489 while (retry_count) {
3490 if (signal_pending(current)) {
3491 ret = -EINTR;
3492 break;
3493 }
3494 /*
3495 * Rather than hide all in some function, I do this in
3496 * open coded manner. You see what this really does.
3497 * We have to guarantee mem->res.limit < mem->memsw.limit.
3498 */
3499 mutex_lock(&set_limit_mutex);
3500 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3501 if (memlimit > val) {
3502 ret = -EINVAL;
3503 mutex_unlock(&set_limit_mutex);
3504 break;
3505 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003506 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3507 if (memswlimit < val)
3508 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003509 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003510 if (!ret) {
3511 if (memlimit == val)
3512 memcg->memsw_is_minimum = true;
3513 else
3514 memcg->memsw_is_minimum = false;
3515 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003516 mutex_unlock(&set_limit_mutex);
3517
3518 if (!ret)
3519 break;
3520
Balbir Singh4e416952009-09-23 15:56:39 -07003521 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh75822b42009-09-23 15:56:38 -07003522 MEM_CGROUP_RECLAIM_NOSWAP |
Ying Han0ae5e892011-05-26 16:25:25 -07003523 MEM_CGROUP_RECLAIM_SHRINK,
3524 NULL);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003525 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003526 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003527 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003528 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003529 else
3530 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003531 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003532 if (!ret && enlarge)
3533 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003534 return ret;
3535}
3536
Balbir Singh4e416952009-09-23 15:56:39 -07003537unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
Ying Han0ae5e892011-05-26 16:25:25 -07003538 gfp_t gfp_mask,
3539 unsigned long *total_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07003540{
3541 unsigned long nr_reclaimed = 0;
3542 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
3543 unsigned long reclaimed;
3544 int loop = 0;
3545 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003546 unsigned long long excess;
Ying Han0ae5e892011-05-26 16:25:25 -07003547 unsigned long nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003548
3549 if (order > 0)
3550 return 0;
3551
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07003552 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
Balbir Singh4e416952009-09-23 15:56:39 -07003553 /*
3554 * This loop can run a while, specially if mem_cgroup's continuously
3555 * keep exceeding their soft limit and putting the system under
3556 * pressure
3557 */
3558 do {
3559 if (next_mz)
3560 mz = next_mz;
3561 else
3562 mz = mem_cgroup_largest_soft_limit_node(mctz);
3563 if (!mz)
3564 break;
3565
Ying Han0ae5e892011-05-26 16:25:25 -07003566 nr_scanned = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07003567 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
3568 gfp_mask,
Ying Han0ae5e892011-05-26 16:25:25 -07003569 MEM_CGROUP_RECLAIM_SOFT,
3570 &nr_scanned);
Balbir Singh4e416952009-09-23 15:56:39 -07003571 nr_reclaimed += reclaimed;
Ying Han0ae5e892011-05-26 16:25:25 -07003572 *total_scanned += nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003573 spin_lock(&mctz->lock);
3574
3575 /*
3576 * If we failed to reclaim anything from this memory cgroup
3577 * it is time to move on to the next cgroup
3578 */
3579 next_mz = NULL;
3580 if (!reclaimed) {
3581 do {
3582 /*
3583 * Loop until we find yet another one.
3584 *
3585 * By the time we get the soft_limit lock
3586 * again, someone might have aded the
3587 * group back on the RB tree. Iterate to
3588 * make sure we get a different mem.
3589 * mem_cgroup_largest_soft_limit_node returns
3590 * NULL if no other cgroup is present on
3591 * the tree
3592 */
3593 next_mz =
3594 __mem_cgroup_largest_soft_limit_node(mctz);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003595 if (next_mz == mz)
Balbir Singh4e416952009-09-23 15:56:39 -07003596 css_put(&next_mz->mem->css);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003597 else /* next_mz == NULL or other memcg */
Balbir Singh4e416952009-09-23 15:56:39 -07003598 break;
3599 } while (1);
3600 }
Balbir Singh4e416952009-09-23 15:56:39 -07003601 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003602 excess = res_counter_soft_limit_excess(&mz->mem->res);
Balbir Singh4e416952009-09-23 15:56:39 -07003603 /*
3604 * One school of thought says that we should not add
3605 * back the node to the tree if reclaim returns 0.
3606 * But our reclaim could return 0, simply because due
3607 * to priority we are exposing a smaller subset of
3608 * memory to reclaim from. Consider this as a longer
3609 * term TODO.
3610 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003611 /* If excess == 0, no tree ops */
3612 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07003613 spin_unlock(&mctz->lock);
3614 css_put(&mz->mem->css);
3615 loop++;
3616 /*
3617 * Could not reclaim anything and there are no more
3618 * mem cgroups to try or we seem to be looping without
3619 * reclaiming anything.
3620 */
3621 if (!nr_reclaimed &&
3622 (next_mz == NULL ||
3623 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3624 break;
3625 } while (!nr_reclaimed);
3626 if (next_mz)
3627 css_put(&next_mz->mem->css);
3628 return nr_reclaimed;
3629}
3630
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003631/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003632 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003633 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
3634 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003635static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003636 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003637{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003638 struct zone *zone;
3639 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003640 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003641 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003642 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003643 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003644
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003645 zone = &NODE_DATA(node)->node_zones[zid];
3646 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07003647 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003648
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003649 loop = MEM_CGROUP_ZSTAT(mz, lru);
3650 /* give some margin against EBUSY etc...*/
3651 loop += 256;
3652 busy = NULL;
3653 while (loop--) {
Johannes Weiner5564e882011-03-23 16:42:29 -07003654 struct page *page;
3655
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003656 ret = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003657 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003658 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003659 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003660 break;
3661 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003662 pc = list_entry(list->prev, struct page_cgroup, lru);
3663 if (busy == pc) {
3664 list_move(&pc->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08003665 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003666 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003667 continue;
3668 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003669 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003670
Johannes Weiner6b3ae582011-03-23 16:42:30 -07003671 page = lookup_cgroup_page(pc);
Johannes Weiner5564e882011-03-23 16:42:29 -07003672
3673 ret = mem_cgroup_move_parent(page, pc, mem, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003674 if (ret == -ENOMEM)
3675 break;
3676
3677 if (ret == -EBUSY || ret == -EINVAL) {
3678 /* found lock contention or "pc" is obsolete. */
3679 busy = pc;
3680 cond_resched();
3681 } else
3682 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003683 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003684
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003685 if (!ret && !list_empty(list))
3686 return -EBUSY;
3687 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003688}
3689
3690/*
3691 * make mem_cgroup's charge to be 0 if there is no task.
3692 * This enables deleting this mem_cgroup.
3693 */
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003694static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003695{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003696 int ret;
3697 int node, zid, shrink;
3698 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003699 struct cgroup *cgrp = mem->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003700
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003701 css_get(&mem->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003702
3703 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003704 /* should free all ? */
3705 if (free_all)
3706 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003707move_account:
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003708 do {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003709 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003710 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003711 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003712 ret = -EINTR;
3713 if (signal_pending(current))
3714 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003715 /* This is for making all *used* pages to be on LRU. */
3716 lru_add_drain_all();
Michal Hockod38144b2011-07-26 16:08:28 -07003717 drain_all_stock_sync(mem);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003718 ret = 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07003719 mem_cgroup_start_move(mem);
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08003720 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003721 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07003722 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003723 for_each_lru(l) {
3724 ret = mem_cgroup_force_empty_list(mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003725 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003726 if (ret)
3727 break;
3728 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003729 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003730 if (ret)
3731 break;
3732 }
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07003733 mem_cgroup_end_move(mem);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003734 memcg_oom_recover(mem);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003735 /* it seems parent cgroup doesn't have enough mem */
3736 if (ret == -ENOMEM)
3737 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003738 cond_resched();
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003739 /* "ret" should also be checked to ensure all lists are empty. */
3740 } while (mem->res.usage > 0 || ret);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003741out:
3742 css_put(&mem->css);
3743 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003744
3745try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003746 /* returns EBUSY if there is a task or if we come here twice. */
3747 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003748 ret = -EBUSY;
3749 goto out;
3750 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003751 /* we call try-to-free pages for make this cgroup empty */
3752 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003753 /* try to free all pages in this cgroup */
3754 shrink = 1;
3755 while (nr_retries && mem->res.usage > 0) {
3756 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003757
3758 if (signal_pending(current)) {
3759 ret = -EINTR;
3760 goto out;
3761 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003762 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
Johannes Weiner185efc02011-09-14 16:21:58 -07003763 false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003764 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003765 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003766 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02003767 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003768 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003769
3770 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003771 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003772 /* try move_account...there may be some *locked* pages. */
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003773 goto move_account;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003774}
3775
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003776int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
3777{
3778 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
3779}
3780
3781
Balbir Singh18f59ea2009-01-07 18:08:07 -08003782static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
3783{
3784 return mem_cgroup_from_cont(cont)->use_hierarchy;
3785}
3786
3787static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
3788 u64 val)
3789{
3790 int retval = 0;
3791 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
3792 struct cgroup *parent = cont->parent;
3793 struct mem_cgroup *parent_mem = NULL;
3794
3795 if (parent)
3796 parent_mem = mem_cgroup_from_cont(parent);
3797
3798 cgroup_lock();
3799 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003800 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08003801 * in the child subtrees. If it is unset, then the change can
3802 * occur, provided the current cgroup has no children.
3803 *
3804 * For the root cgroup, parent_mem is NULL, we allow value to be
3805 * set if there are no children.
3806 */
3807 if ((!parent_mem || !parent_mem->use_hierarchy) &&
3808 (val == 1 || val == 0)) {
3809 if (list_empty(&cont->children))
3810 mem->use_hierarchy = val;
3811 else
3812 retval = -EBUSY;
3813 } else
3814 retval = -EINVAL;
3815 cgroup_unlock();
3816
3817 return retval;
3818}
3819
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003820
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003821static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *mem,
3822 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003823{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003824 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003825 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003826
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003827 /* Per-cpu values can be negative, use a signed accumulator */
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003828 for_each_mem_cgroup_tree(iter, mem)
3829 val += mem_cgroup_read_stat(iter, idx);
3830
3831 if (val < 0) /* race ? */
3832 val = 0;
3833 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003834}
3835
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003836static inline u64 mem_cgroup_usage(struct mem_cgroup *mem, bool swap)
3837{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003838 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003839
3840 if (!mem_cgroup_is_root(mem)) {
3841 if (!swap)
3842 return res_counter_read_u64(&mem->res, RES_USAGE);
3843 else
3844 return res_counter_read_u64(&mem->memsw, RES_USAGE);
3845 }
3846
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003847 val = mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_CACHE);
3848 val += mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003849
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003850 if (swap)
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003851 val += mem_cgroup_recursive_stat(mem, MEM_CGROUP_STAT_SWAPOUT);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003852
3853 return val << PAGE_SHIFT;
3854}
3855
Paul Menage2c3daa72008-04-29 00:59:58 -07003856static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003857{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003858 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003859 u64 val;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003860 int type, name;
3861
3862 type = MEMFILE_TYPE(cft->private);
3863 name = MEMFILE_ATTR(cft->private);
3864 switch (type) {
3865 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003866 if (name == RES_USAGE)
3867 val = mem_cgroup_usage(mem, false);
3868 else
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003869 val = res_counter_read_u64(&mem->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003870 break;
3871 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003872 if (name == RES_USAGE)
3873 val = mem_cgroup_usage(mem, true);
3874 else
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003875 val = res_counter_read_u64(&mem->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003876 break;
3877 default:
3878 BUG();
3879 break;
3880 }
3881 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003882}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003883/*
3884 * The user of this function is...
3885 * RES_LIMIT.
3886 */
Paul Menage856c13a2008-07-25 01:47:04 -07003887static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
3888 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003889{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003890 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003891 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003892 unsigned long long val;
3893 int ret;
3894
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003895 type = MEMFILE_TYPE(cft->private);
3896 name = MEMFILE_ATTR(cft->private);
3897 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003898 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07003899 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
3900 ret = -EINVAL;
3901 break;
3902 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003903 /* This function does all necessary parse...reuse it */
3904 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003905 if (ret)
3906 break;
3907 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003908 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003909 else
3910 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003911 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07003912 case RES_SOFT_LIMIT:
3913 ret = res_counter_memparse_write_strategy(buffer, &val);
3914 if (ret)
3915 break;
3916 /*
3917 * For memsw, soft limits are hard to implement in terms
3918 * of semantics, for now, we support soft limits for
3919 * control without swap
3920 */
3921 if (type == _MEM)
3922 ret = res_counter_set_soft_limit(&memcg->res, val);
3923 else
3924 ret = -EINVAL;
3925 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003926 default:
3927 ret = -EINVAL; /* should be BUG() ? */
3928 break;
3929 }
3930 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003931}
3932
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08003933static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
3934 unsigned long long *mem_limit, unsigned long long *memsw_limit)
3935{
3936 struct cgroup *cgroup;
3937 unsigned long long min_limit, min_memsw_limit, tmp;
3938
3939 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3940 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3941 cgroup = memcg->css.cgroup;
3942 if (!memcg->use_hierarchy)
3943 goto out;
3944
3945 while (cgroup->parent) {
3946 cgroup = cgroup->parent;
3947 memcg = mem_cgroup_from_cont(cgroup);
3948 if (!memcg->use_hierarchy)
3949 break;
3950 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
3951 min_limit = min(min_limit, tmp);
3952 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3953 min_memsw_limit = min(min_memsw_limit, tmp);
3954 }
3955out:
3956 *mem_limit = min_limit;
3957 *memsw_limit = min_memsw_limit;
3958 return;
3959}
3960
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003961static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003962{
3963 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003964 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003965
3966 mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003967 type = MEMFILE_TYPE(event);
3968 name = MEMFILE_ATTR(event);
3969 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003970 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003971 if (type == _MEM)
3972 res_counter_reset_max(&mem->res);
3973 else
3974 res_counter_reset_max(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003975 break;
3976 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003977 if (type == _MEM)
3978 res_counter_reset_failcnt(&mem->res);
3979 else
3980 res_counter_reset_failcnt(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003981 break;
3982 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07003983
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07003984 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003985}
3986
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003987static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
3988 struct cftype *cft)
3989{
3990 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
3991}
3992
Daisuke Nishimura02491442010-03-10 15:22:17 -08003993#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003994static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
3995 struct cftype *cft, u64 val)
3996{
3997 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3998
3999 if (val >= (1 << NR_MOVE_TYPE))
4000 return -EINVAL;
4001 /*
4002 * We check this value several times in both in can_attach() and
4003 * attach(), so we need cgroup lock to prevent this value from being
4004 * inconsistent.
4005 */
4006 cgroup_lock();
4007 mem->move_charge_at_immigrate = val;
4008 cgroup_unlock();
4009
4010 return 0;
4011}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004012#else
4013static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
4014 struct cftype *cft, u64 val)
4015{
4016 return -ENOSYS;
4017}
4018#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004019
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004020
4021/* For read statistics */
4022enum {
4023 MCS_CACHE,
4024 MCS_RSS,
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08004025 MCS_FILE_MAPPED,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004026 MCS_PGPGIN,
4027 MCS_PGPGOUT,
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004028 MCS_SWAP,
Ying Han456f9982011-05-26 16:25:38 -07004029 MCS_PGFAULT,
4030 MCS_PGMAJFAULT,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004031 MCS_INACTIVE_ANON,
4032 MCS_ACTIVE_ANON,
4033 MCS_INACTIVE_FILE,
4034 MCS_ACTIVE_FILE,
4035 MCS_UNEVICTABLE,
4036 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004037};
4038
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004039struct mcs_total_stat {
4040 s64 stat[NR_MCS_STAT];
4041};
4042
4043struct {
4044 char *local_name;
4045 char *total_name;
4046} memcg_stat_strings[NR_MCS_STAT] = {
4047 {"cache", "total_cache"},
4048 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07004049 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004050 {"pgpgin", "total_pgpgin"},
4051 {"pgpgout", "total_pgpgout"},
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004052 {"swap", "total_swap"},
Ying Han456f9982011-05-26 16:25:38 -07004053 {"pgfault", "total_pgfault"},
4054 {"pgmajfault", "total_pgmajfault"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004055 {"inactive_anon", "total_inactive_anon"},
4056 {"active_anon", "total_active_anon"},
4057 {"inactive_file", "total_inactive_file"},
4058 {"active_file", "total_active_file"},
4059 {"unevictable", "total_unevictable"}
4060};
4061
4062
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004063static void
4064mem_cgroup_get_local_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004065{
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004066 s64 val;
4067
4068 /* per cpu stat */
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004069 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004070 s->stat[MCS_CACHE] += val * PAGE_SIZE;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004071 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004072 s->stat[MCS_RSS] += val * PAGE_SIZE;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004073 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_FILE_MAPPED);
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08004074 s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
Johannes Weinere9f89742011-03-23 16:42:37 -07004075 val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGPGIN);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004076 s->stat[MCS_PGPGIN] += val;
Johannes Weinere9f89742011-03-23 16:42:37 -07004077 val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGPGOUT);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004078 s->stat[MCS_PGPGOUT] += val;
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004079 if (do_swap_account) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004080 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_SWAPOUT);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004081 s->stat[MCS_SWAP] += val * PAGE_SIZE;
4082 }
Ying Han456f9982011-05-26 16:25:38 -07004083 val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGFAULT);
4084 s->stat[MCS_PGFAULT] += val;
4085 val = mem_cgroup_read_events(mem, MEM_CGROUP_EVENTS_PGMAJFAULT);
4086 s->stat[MCS_PGMAJFAULT] += val;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004087
4088 /* per zone stat */
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004089 val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_INACTIVE_ANON));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004090 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004091 val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_ACTIVE_ANON));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004092 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004093 val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_INACTIVE_FILE));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004094 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004095 val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_ACTIVE_FILE));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004096 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004097 val = mem_cgroup_nr_lru_pages(mem, BIT(LRU_UNEVICTABLE));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004098 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004099}
4100
4101static void
4102mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
4103{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004104 struct mem_cgroup *iter;
4105
4106 for_each_mem_cgroup_tree(iter, mem)
4107 mem_cgroup_get_local_stat(iter, s);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004108}
4109
Ying Han406eb0c2011-05-26 16:25:37 -07004110#ifdef CONFIG_NUMA
4111static int mem_control_numa_stat_show(struct seq_file *m, void *arg)
4112{
4113 int nid;
4114 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
4115 unsigned long node_nr;
4116 struct cgroup *cont = m->private;
4117 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
4118
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004119 total_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07004120 seq_printf(m, "total=%lu", total_nr);
4121 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004122 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07004123 seq_printf(m, " N%d=%lu", nid, node_nr);
4124 }
4125 seq_putc(m, '\n');
4126
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004127 file_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07004128 seq_printf(m, "file=%lu", file_nr);
4129 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004130 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
4131 LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07004132 seq_printf(m, " N%d=%lu", nid, node_nr);
4133 }
4134 seq_putc(m, '\n');
4135
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004136 anon_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07004137 seq_printf(m, "anon=%lu", anon_nr);
4138 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004139 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
4140 LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07004141 seq_printf(m, " N%d=%lu", nid, node_nr);
4142 }
4143 seq_putc(m, '\n');
4144
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004145 unevictable_nr = mem_cgroup_nr_lru_pages(mem_cont, BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07004146 seq_printf(m, "unevictable=%lu", unevictable_nr);
4147 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004148 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
4149 BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07004150 seq_printf(m, " N%d=%lu", nid, node_nr);
4151 }
4152 seq_putc(m, '\n');
4153 return 0;
4154}
4155#endif /* CONFIG_NUMA */
4156
Paul Menagec64745c2008-04-29 01:00:02 -07004157static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
4158 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004159{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004160 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004161 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004162 int i;
4163
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004164 memset(&mystat, 0, sizeof(mystat));
4165 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004166
Ying Han406eb0c2011-05-26 16:25:37 -07004167
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004168 for (i = 0; i < NR_MCS_STAT; i++) {
4169 if (i == MCS_SWAP && !do_swap_account)
4170 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004171 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004172 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004173
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004174 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004175 {
4176 unsigned long long limit, memsw_limit;
4177 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
4178 cb->fill(cb, "hierarchical_memory_limit", limit);
4179 if (do_swap_account)
4180 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
4181 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004182
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004183 memset(&mystat, 0, sizeof(mystat));
4184 mem_cgroup_get_total_stat(mem_cont, &mystat);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004185 for (i = 0; i < NR_MCS_STAT; i++) {
4186 if (i == MCS_SWAP && !do_swap_account)
4187 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004188 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004189 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004190
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004191#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08004192 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004193
4194 {
4195 int nid, zid;
4196 struct mem_cgroup_per_zone *mz;
4197 unsigned long recent_rotated[2] = {0, 0};
4198 unsigned long recent_scanned[2] = {0, 0};
4199
4200 for_each_online_node(nid)
4201 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
4202 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
4203
4204 recent_rotated[0] +=
4205 mz->reclaim_stat.recent_rotated[0];
4206 recent_rotated[1] +=
4207 mz->reclaim_stat.recent_rotated[1];
4208 recent_scanned[0] +=
4209 mz->reclaim_stat.recent_scanned[0];
4210 recent_scanned[1] +=
4211 mz->reclaim_stat.recent_scanned[1];
4212 }
4213 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
4214 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
4215 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
4216 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
4217 }
4218#endif
4219
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004220 return 0;
4221}
4222
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004223static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
4224{
4225 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4226
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07004227 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004228}
4229
4230static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
4231 u64 val)
4232{
4233 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4234 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08004235
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004236 if (val > 100)
4237 return -EINVAL;
4238
4239 if (cgrp->parent == NULL)
4240 return -EINVAL;
4241
4242 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08004243
4244 cgroup_lock();
4245
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004246 /* If under hierarchy, only empty-root can set this value */
4247 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08004248 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
4249 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004250 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08004251 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004252
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004253 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004254
Li Zefan068b38c2009-01-15 13:51:26 -08004255 cgroup_unlock();
4256
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004257 return 0;
4258}
4259
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004260static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
4261{
4262 struct mem_cgroup_threshold_ary *t;
4263 u64 usage;
4264 int i;
4265
4266 rcu_read_lock();
4267 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004268 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004269 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004270 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004271
4272 if (!t)
4273 goto unlock;
4274
4275 usage = mem_cgroup_usage(memcg, swap);
4276
4277 /*
4278 * current_threshold points to threshold just below usage.
4279 * If it's not true, a threshold was crossed after last
4280 * call of __mem_cgroup_threshold().
4281 */
Phil Carmody5407a562010-05-26 14:42:42 -07004282 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004283
4284 /*
4285 * Iterate backward over array of thresholds starting from
4286 * current_threshold and check if a threshold is crossed.
4287 * If none of thresholds below usage is crossed, we read
4288 * only one element of the array here.
4289 */
4290 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
4291 eventfd_signal(t->entries[i].eventfd, 1);
4292
4293 /* i = current_threshold + 1 */
4294 i++;
4295
4296 /*
4297 * Iterate forward over array of thresholds starting from
4298 * current_threshold+1 and check if a threshold is crossed.
4299 * If none of thresholds above usage is crossed, we read
4300 * only one element of the array here.
4301 */
4302 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
4303 eventfd_signal(t->entries[i].eventfd, 1);
4304
4305 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07004306 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004307unlock:
4308 rcu_read_unlock();
4309}
4310
4311static void mem_cgroup_threshold(struct mem_cgroup *memcg)
4312{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07004313 while (memcg) {
4314 __mem_cgroup_threshold(memcg, false);
4315 if (do_swap_account)
4316 __mem_cgroup_threshold(memcg, true);
4317
4318 memcg = parent_mem_cgroup(memcg);
4319 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004320}
4321
4322static int compare_thresholds(const void *a, const void *b)
4323{
4324 const struct mem_cgroup_threshold *_a = a;
4325 const struct mem_cgroup_threshold *_b = b;
4326
4327 return _a->threshold - _b->threshold;
4328}
4329
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004330static int mem_cgroup_oom_notify_cb(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004331{
4332 struct mem_cgroup_eventfd_list *ev;
4333
4334 list_for_each_entry(ev, &mem->oom_notify, list)
4335 eventfd_signal(ev->eventfd, 1);
4336 return 0;
4337}
4338
4339static void mem_cgroup_oom_notify(struct mem_cgroup *mem)
4340{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004341 struct mem_cgroup *iter;
4342
4343 for_each_mem_cgroup_tree(iter, mem)
4344 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004345}
4346
4347static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
4348 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004349{
4350 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004351 struct mem_cgroup_thresholds *thresholds;
4352 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004353 int type = MEMFILE_TYPE(cft->private);
4354 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004355 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004356
4357 ret = res_counter_memparse_write_strategy(args, &threshold);
4358 if (ret)
4359 return ret;
4360
4361 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004362
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004363 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004364 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004365 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004366 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004367 else
4368 BUG();
4369
4370 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4371
4372 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004373 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004374 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4375
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004376 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004377
4378 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004379 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004380 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004381 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004382 ret = -ENOMEM;
4383 goto unlock;
4384 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004385 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004386
4387 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004388 if (thresholds->primary) {
4389 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004390 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004391 }
4392
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004393 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004394 new->entries[size - 1].eventfd = eventfd;
4395 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004396
4397 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004398 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004399 compare_thresholds, NULL);
4400
4401 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004402 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004403 for (i = 0; i < size; i++) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004404 if (new->entries[i].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004405 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004406 * new->current_threshold will not be used until
4407 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004408 * it here.
4409 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004410 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004411 }
4412 }
4413
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004414 /* Free old spare buffer and save old primary buffer as spare */
4415 kfree(thresholds->spare);
4416 thresholds->spare = thresholds->primary;
4417
4418 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004419
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004420 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004421 synchronize_rcu();
4422
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004423unlock:
4424 mutex_unlock(&memcg->thresholds_lock);
4425
4426 return ret;
4427}
4428
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004429static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004430 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004431{
4432 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004433 struct mem_cgroup_thresholds *thresholds;
4434 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004435 int type = MEMFILE_TYPE(cft->private);
4436 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004437 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004438
4439 mutex_lock(&memcg->thresholds_lock);
4440 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004441 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004442 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004443 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004444 else
4445 BUG();
4446
4447 /*
4448 * Something went wrong if we trying to unregister a threshold
4449 * if we don't have thresholds
4450 */
4451 BUG_ON(!thresholds);
4452
4453 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4454
4455 /* Check if a threshold crossed before removing */
4456 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4457
4458 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004459 size = 0;
4460 for (i = 0; i < thresholds->primary->size; i++) {
4461 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004462 size++;
4463 }
4464
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004465 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004466
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004467 /* Set thresholds array to NULL if we don't have thresholds */
4468 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004469 kfree(new);
4470 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004471 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004472 }
4473
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004474 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004475
4476 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004477 new->current_threshold = -1;
4478 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
4479 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004480 continue;
4481
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004482 new->entries[j] = thresholds->primary->entries[i];
4483 if (new->entries[j].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004484 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004485 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004486 * until rcu_assign_pointer(), so it's safe to increment
4487 * it here.
4488 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004489 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004490 }
4491 j++;
4492 }
4493
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004494swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004495 /* Swap primary and spare array */
4496 thresholds->spare = thresholds->primary;
4497 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004498
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004499 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004500 synchronize_rcu();
4501
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004502 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004503}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004504
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004505static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
4506 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
4507{
4508 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4509 struct mem_cgroup_eventfd_list *event;
4510 int type = MEMFILE_TYPE(cft->private);
4511
4512 BUG_ON(type != _OOM_TYPE);
4513 event = kmalloc(sizeof(*event), GFP_KERNEL);
4514 if (!event)
4515 return -ENOMEM;
4516
Michal Hocko1af8efe2011-07-26 16:08:24 -07004517 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004518
4519 event->eventfd = eventfd;
4520 list_add(&event->list, &memcg->oom_notify);
4521
4522 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07004523 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004524 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07004525 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004526
4527 return 0;
4528}
4529
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004530static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004531 struct cftype *cft, struct eventfd_ctx *eventfd)
4532{
4533 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
4534 struct mem_cgroup_eventfd_list *ev, *tmp;
4535 int type = MEMFILE_TYPE(cft->private);
4536
4537 BUG_ON(type != _OOM_TYPE);
4538
Michal Hocko1af8efe2011-07-26 16:08:24 -07004539 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004540
4541 list_for_each_entry_safe(ev, tmp, &mem->oom_notify, list) {
4542 if (ev->eventfd == eventfd) {
4543 list_del(&ev->list);
4544 kfree(ev);
4545 }
4546 }
4547
Michal Hocko1af8efe2011-07-26 16:08:24 -07004548 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004549}
4550
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004551static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
4552 struct cftype *cft, struct cgroup_map_cb *cb)
4553{
4554 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
4555
4556 cb->fill(cb, "oom_kill_disable", mem->oom_kill_disable);
4557
Michal Hocko79dfdac2011-07-26 16:08:23 -07004558 if (atomic_read(&mem->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004559 cb->fill(cb, "under_oom", 1);
4560 else
4561 cb->fill(cb, "under_oom", 0);
4562 return 0;
4563}
4564
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004565static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
4566 struct cftype *cft, u64 val)
4567{
4568 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
4569 struct mem_cgroup *parent;
4570
4571 /* cannot set to root cgroup and only 0 and 1 are allowed */
4572 if (!cgrp->parent || !((val == 0) || (val == 1)))
4573 return -EINVAL;
4574
4575 parent = mem_cgroup_from_cont(cgrp->parent);
4576
4577 cgroup_lock();
4578 /* oom-kill-disable is a flag for subhierarchy. */
4579 if ((parent->use_hierarchy) ||
4580 (mem->use_hierarchy && !list_empty(&cgrp->children))) {
4581 cgroup_unlock();
4582 return -EINVAL;
4583 }
4584 mem->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07004585 if (!val)
4586 memcg_oom_recover(mem);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004587 cgroup_unlock();
4588 return 0;
4589}
4590
Ying Han406eb0c2011-05-26 16:25:37 -07004591#ifdef CONFIG_NUMA
4592static const struct file_operations mem_control_numa_stat_file_operations = {
4593 .read = seq_read,
4594 .llseek = seq_lseek,
4595 .release = single_release,
4596};
4597
4598static int mem_control_numa_stat_open(struct inode *unused, struct file *file)
4599{
4600 struct cgroup *cont = file->f_dentry->d_parent->d_fsdata;
4601
4602 file->f_op = &mem_control_numa_stat_file_operations;
4603 return single_open(file, mem_control_numa_stat_show, cont);
4604}
4605#endif /* CONFIG_NUMA */
4606
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004607static struct cftype mem_cgroup_files[] = {
4608 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004609 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004610 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07004611 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004612 .register_event = mem_cgroup_usage_register_event,
4613 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004614 },
4615 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004616 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004617 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004618 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004619 .read_u64 = mem_cgroup_read,
4620 },
4621 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004622 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004623 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07004624 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07004625 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004626 },
4627 {
Balbir Singh296c81d2009-09-23 15:56:36 -07004628 .name = "soft_limit_in_bytes",
4629 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
4630 .write_string = mem_cgroup_write,
4631 .read_u64 = mem_cgroup_read,
4632 },
4633 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004634 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004635 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004636 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07004637 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004638 },
Balbir Singh8697d332008-02-07 00:13:59 -08004639 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004640 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07004641 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004642 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004643 {
4644 .name = "force_empty",
4645 .trigger = mem_cgroup_force_empty_write,
4646 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08004647 {
4648 .name = "use_hierarchy",
4649 .write_u64 = mem_cgroup_hierarchy_write,
4650 .read_u64 = mem_cgroup_hierarchy_read,
4651 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004652 {
4653 .name = "swappiness",
4654 .read_u64 = mem_cgroup_swappiness_read,
4655 .write_u64 = mem_cgroup_swappiness_write,
4656 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004657 {
4658 .name = "move_charge_at_immigrate",
4659 .read_u64 = mem_cgroup_move_charge_read,
4660 .write_u64 = mem_cgroup_move_charge_write,
4661 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004662 {
4663 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004664 .read_map = mem_cgroup_oom_control_read,
4665 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004666 .register_event = mem_cgroup_oom_register_event,
4667 .unregister_event = mem_cgroup_oom_unregister_event,
4668 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
4669 },
Ying Han406eb0c2011-05-26 16:25:37 -07004670#ifdef CONFIG_NUMA
4671 {
4672 .name = "numa_stat",
4673 .open = mem_control_numa_stat_open,
KAMEZAWA Hiroyuki89577122011-06-15 15:08:41 -07004674 .mode = S_IRUGO,
Ying Han406eb0c2011-05-26 16:25:37 -07004675 },
4676#endif
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004677};
4678
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004679#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4680static struct cftype memsw_cgroup_files[] = {
4681 {
4682 .name = "memsw.usage_in_bytes",
4683 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
4684 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004685 .register_event = mem_cgroup_usage_register_event,
4686 .unregister_event = mem_cgroup_usage_unregister_event,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004687 },
4688 {
4689 .name = "memsw.max_usage_in_bytes",
4690 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
4691 .trigger = mem_cgroup_reset,
4692 .read_u64 = mem_cgroup_read,
4693 },
4694 {
4695 .name = "memsw.limit_in_bytes",
4696 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
4697 .write_string = mem_cgroup_write,
4698 .read_u64 = mem_cgroup_read,
4699 },
4700 {
4701 .name = "memsw.failcnt",
4702 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
4703 .trigger = mem_cgroup_reset,
4704 .read_u64 = mem_cgroup_read,
4705 },
4706};
4707
4708static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
4709{
4710 if (!do_swap_account)
4711 return 0;
4712 return cgroup_add_files(cont, ss, memsw_cgroup_files,
4713 ARRAY_SIZE(memsw_cgroup_files));
4714};
4715#else
4716static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
4717{
4718 return 0;
4719}
4720#endif
4721
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004722static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
4723{
4724 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004725 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07004726 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004727 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004728 /*
4729 * This routine is called against possible nodes.
4730 * But it's BUG to call kmalloc() against offline node.
4731 *
4732 * TODO: this routine can waste much memory for nodes which will
4733 * never be onlined. It's better to use memory hotplug callback
4734 * function.
4735 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004736 if (!node_state(node, N_NORMAL_MEMORY))
4737 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08004738 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004739 if (!pn)
4740 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004741
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004742 mem->info.nodeinfo[node] = pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004743 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4744 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07004745 for_each_lru(l)
4746 INIT_LIST_HEAD(&mz->lists[l]);
Balbir Singhf64c3f52009-09-23 15:56:37 -07004747 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07004748 mz->on_tree = false;
4749 mz->mem = mem;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004750 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004751 return 0;
4752}
4753
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004754static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
4755{
4756 kfree(mem->info.nodeinfo[node]);
4757}
4758
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004759static struct mem_cgroup *mem_cgroup_alloc(void)
4760{
4761 struct mem_cgroup *mem;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004762 int size = sizeof(struct mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004763
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004764 /* Can be very big if MAX_NUMNODES is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08004765 if (size < PAGE_SIZE)
Jesper Juhl17295c82011-01-13 15:47:42 -08004766 mem = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004767 else
Jesper Juhl17295c82011-01-13 15:47:42 -08004768 mem = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004769
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07004770 if (!mem)
4771 return NULL;
4772
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004773 mem->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004774 if (!mem->stat)
4775 goto out_free;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07004776 spin_lock_init(&mem->pcp_counter_lock);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004777 return mem;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004778
4779out_free:
4780 if (size < PAGE_SIZE)
4781 kfree(mem);
4782 else
4783 vfree(mem);
4784 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004785}
4786
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004787/*
4788 * At destroying mem_cgroup, references from swap_cgroup can remain.
4789 * (scanning all at force_empty is too costly...)
4790 *
4791 * Instead of clearing all references at force_empty, we remember
4792 * the number of reference from swap_cgroup and free mem_cgroup when
4793 * it goes down to 0.
4794 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004795 * Removal of cgroup itself succeeds regardless of refs from swap.
4796 */
4797
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004798static void __mem_cgroup_free(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004799{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004800 int node;
4801
Balbir Singhf64c3f52009-09-23 15:56:37 -07004802 mem_cgroup_remove_from_trees(mem);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004803 free_css_id(&mem_cgroup_subsys, &mem->css);
4804
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004805 for_each_node_state(node, N_POSSIBLE)
4806 free_mem_cgroup_per_zone_info(mem, node);
4807
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004808 free_percpu(mem->stat);
4809 if (sizeof(struct mem_cgroup) < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004810 kfree(mem);
4811 else
4812 vfree(mem);
4813}
4814
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004815static void mem_cgroup_get(struct mem_cgroup *mem)
4816{
4817 atomic_inc(&mem->refcnt);
4818}
4819
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004820static void __mem_cgroup_put(struct mem_cgroup *mem, int count)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004821{
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004822 if (atomic_sub_and_test(count, &mem->refcnt)) {
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004823 struct mem_cgroup *parent = parent_mem_cgroup(mem);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004824 __mem_cgroup_free(mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004825 if (parent)
4826 mem_cgroup_put(parent);
4827 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004828}
4829
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004830static void mem_cgroup_put(struct mem_cgroup *mem)
4831{
4832 __mem_cgroup_put(mem, 1);
4833}
4834
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004835/*
4836 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
4837 */
4838static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
4839{
4840 if (!mem->res.parent)
4841 return NULL;
4842 return mem_cgroup_from_res_counter(mem->res.parent, res);
4843}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004844
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004845#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4846static void __init enable_swap_cgroup(void)
4847{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004848 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004849 do_swap_account = 1;
4850}
4851#else
4852static void __init enable_swap_cgroup(void)
4853{
4854}
4855#endif
4856
Balbir Singhf64c3f52009-09-23 15:56:37 -07004857static int mem_cgroup_soft_limit_tree_init(void)
4858{
4859 struct mem_cgroup_tree_per_node *rtpn;
4860 struct mem_cgroup_tree_per_zone *rtpz;
4861 int tmp, node, zone;
4862
4863 for_each_node_state(node, N_POSSIBLE) {
4864 tmp = node;
4865 if (!node_state(node, N_NORMAL_MEMORY))
4866 tmp = -1;
4867 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
4868 if (!rtpn)
4869 return 1;
4870
4871 soft_limit_tree.rb_tree_per_node[node] = rtpn;
4872
4873 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4874 rtpz = &rtpn->rb_tree_per_zone[zone];
4875 rtpz->rb_root = RB_ROOT;
4876 spin_lock_init(&rtpz->lock);
4877 }
4878 }
4879 return 0;
4880}
4881
Li Zefan0eb253e2009-01-15 13:51:25 -08004882static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004883mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
4884{
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004885 struct mem_cgroup *mem, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004886 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004887 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004888
Jan Blunckc8dad2b2009-01-07 18:07:53 -08004889 mem = mem_cgroup_alloc();
4890 if (!mem)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004891 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004892
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004893 for_each_node_state(node, N_POSSIBLE)
4894 if (alloc_mem_cgroup_per_zone_info(mem, node))
4895 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004896
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004897 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004898 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004899 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004900 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004901 parent = NULL;
Balbir Singh4b3bde42009-09-23 15:56:32 -07004902 root_mem_cgroup = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004903 if (mem_cgroup_soft_limit_tree_init())
4904 goto free_out;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004905 for_each_possible_cpu(cpu) {
4906 struct memcg_stock_pcp *stock =
4907 &per_cpu(memcg_stock, cpu);
4908 INIT_WORK(&stock->work, drain_local_stock);
4909 }
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07004910 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004911 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004912 parent = mem_cgroup_from_cont(cont->parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004913 mem->use_hierarchy = parent->use_hierarchy;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004914 mem->oom_kill_disable = parent->oom_kill_disable;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004915 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004916
Balbir Singh18f59ea2009-01-07 18:08:07 -08004917 if (parent && parent->use_hierarchy) {
4918 res_counter_init(&mem->res, &parent->res);
4919 res_counter_init(&mem->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004920 /*
4921 * We increment refcnt of the parent to ensure that we can
4922 * safely access it on res_counter_charge/uncharge.
4923 * This refcnt will be decremented when freeing this
4924 * mem_cgroup(see mem_cgroup_put).
4925 */
4926 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004927 } else {
4928 res_counter_init(&mem->res, NULL);
4929 res_counter_init(&mem->memsw, NULL);
4930 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004931 mem->last_scanned_child = 0;
Ying Han889976d2011-05-26 16:25:33 -07004932 mem->last_scanned_node = MAX_NUMNODES;
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004933 INIT_LIST_HEAD(&mem->oom_notify);
Balbir Singh6d61ef42009-01-07 18:08:06 -08004934
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004935 if (parent)
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07004936 mem->swappiness = mem_cgroup_swappiness(parent);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004937 atomic_set(&mem->refcnt, 1);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004938 mem->move_charge_at_immigrate = 0;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004939 mutex_init(&mem->thresholds_lock);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004940 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004941free_out:
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004942 __mem_cgroup_free(mem);
Balbir Singh4b3bde42009-09-23 15:56:32 -07004943 root_mem_cgroup = NULL;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004944 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004945}
4946
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004947static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004948 struct cgroup *cont)
4949{
4950 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004951
4952 return mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004953}
4954
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004955static void mem_cgroup_destroy(struct cgroup_subsys *ss,
4956 struct cgroup *cont)
4957{
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004958 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004959
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004960 mem_cgroup_put(mem);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004961}
4962
4963static int mem_cgroup_populate(struct cgroup_subsys *ss,
4964 struct cgroup *cont)
4965{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004966 int ret;
4967
4968 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
4969 ARRAY_SIZE(mem_cgroup_files));
4970
4971 if (!ret)
4972 ret = register_memsw_files(cont, ss);
4973 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004974}
4975
Daisuke Nishimura02491442010-03-10 15:22:17 -08004976#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004977/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004978#define PRECHARGE_COUNT_AT_ONCE 256
4979static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004980{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004981 int ret = 0;
4982 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004983 struct mem_cgroup *mem = mc.to;
4984
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004985 if (mem_cgroup_is_root(mem)) {
4986 mc.precharge += count;
4987 /* we don't need css_get for root */
4988 return ret;
4989 }
4990 /* try to charge at once */
4991 if (count > 1) {
4992 struct res_counter *dummy;
4993 /*
4994 * "mem" cannot be under rmdir() because we've already checked
4995 * by cgroup_lock_live_cgroup() that it is not removed and we
4996 * are still under the same cgroup_mutex. So we can postpone
4997 * css_get().
4998 */
4999 if (res_counter_charge(&mem->res, PAGE_SIZE * count, &dummy))
5000 goto one_by_one;
5001 if (do_swap_account && res_counter_charge(&mem->memsw,
5002 PAGE_SIZE * count, &dummy)) {
5003 res_counter_uncharge(&mem->res, PAGE_SIZE * count);
5004 goto one_by_one;
5005 }
5006 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005007 return ret;
5008 }
5009one_by_one:
5010 /* fall back to one by one charge */
5011 while (count--) {
5012 if (signal_pending(current)) {
5013 ret = -EINTR;
5014 break;
5015 }
5016 if (!batch_count--) {
5017 batch_count = PRECHARGE_COUNT_AT_ONCE;
5018 cond_resched();
5019 }
Johannes Weiner7ec99d62011-03-23 16:42:36 -07005020 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, 1, &mem, false);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005021 if (ret || !mem)
5022 /* mem_cgroup_clear_mc() will do uncharge later */
5023 return -ENOMEM;
5024 mc.precharge++;
5025 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005026 return ret;
5027}
5028
5029/**
5030 * is_target_pte_for_mc - check a pte whether it is valid for move charge
5031 * @vma: the vma the pte to be checked belongs
5032 * @addr: the address corresponding to the pte to be checked
5033 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08005034 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005035 *
5036 * Returns
5037 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
5038 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
5039 * move charge. if @target is not NULL, the page is stored in target->page
5040 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08005041 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
5042 * target for charge migration. if @target is not NULL, the entry is stored
5043 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005044 *
5045 * Called with pte lock held.
5046 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005047union mc_target {
5048 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005049 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005050};
5051
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005052enum mc_target_type {
5053 MC_TARGET_NONE, /* not used */
5054 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08005055 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005056};
5057
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005058static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
5059 unsigned long addr, pte_t ptent)
5060{
5061 struct page *page = vm_normal_page(vma, addr, ptent);
5062
5063 if (!page || !page_mapped(page))
5064 return NULL;
5065 if (PageAnon(page)) {
5066 /* we don't move shared anon */
5067 if (!move_anon() || page_mapcount(page) > 2)
5068 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005069 } else if (!move_file())
5070 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005071 return NULL;
5072 if (!get_page_unless_zero(page))
5073 return NULL;
5074
5075 return page;
5076}
5077
5078static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5079 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5080{
5081 int usage_count;
5082 struct page *page = NULL;
5083 swp_entry_t ent = pte_to_swp_entry(ptent);
5084
5085 if (!move_anon() || non_swap_entry(ent))
5086 return NULL;
5087 usage_count = mem_cgroup_count_swap_user(ent, &page);
5088 if (usage_count > 1) { /* we don't move shared anon */
5089 if (page)
5090 put_page(page);
5091 return NULL;
5092 }
5093 if (do_swap_account)
5094 entry->val = ent.val;
5095
5096 return page;
5097}
5098
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005099static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
5100 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5101{
5102 struct page *page = NULL;
5103 struct inode *inode;
5104 struct address_space *mapping;
5105 pgoff_t pgoff;
5106
5107 if (!vma->vm_file) /* anonymous vma */
5108 return NULL;
5109 if (!move_file())
5110 return NULL;
5111
5112 inode = vma->vm_file->f_path.dentry->d_inode;
5113 mapping = vma->vm_file->f_mapping;
5114 if (pte_none(ptent))
5115 pgoff = linear_page_index(vma, addr);
5116 else /* pte_file(ptent) is true */
5117 pgoff = pte_to_pgoff(ptent);
5118
5119 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005120 page = find_get_page(mapping, pgoff);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005121
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005122#ifdef CONFIG_SWAP
5123 /* shmem/tmpfs may report page out on swap: account for that too. */
5124 if (radix_tree_exceptional_entry(page)) {
5125 swp_entry_t swap = radix_to_swp_entry(page);
5126 if (do_swap_account)
5127 *entry = swap;
5128 page = find_get_page(&swapper_space, swap.val);
5129 }
5130#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005131 return page;
5132}
5133
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005134static int is_target_pte_for_mc(struct vm_area_struct *vma,
5135 unsigned long addr, pte_t ptent, union mc_target *target)
5136{
Daisuke Nishimura02491442010-03-10 15:22:17 -08005137 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005138 struct page_cgroup *pc;
5139 int ret = 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005140 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005141
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005142 if (pte_present(ptent))
5143 page = mc_handle_present_pte(vma, addr, ptent);
5144 else if (is_swap_pte(ptent))
5145 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005146 else if (pte_none(ptent) || pte_file(ptent))
5147 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005148
5149 if (!page && !ent.val)
Daisuke Nishimura02491442010-03-10 15:22:17 -08005150 return 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005151 if (page) {
5152 pc = lookup_page_cgroup(page);
5153 /*
5154 * Do only loose check w/o page_cgroup lock.
5155 * mem_cgroup_move_account() checks the pc is valid or not under
5156 * the lock.
5157 */
5158 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
5159 ret = MC_TARGET_PAGE;
5160 if (target)
5161 target->page = page;
5162 }
5163 if (!ret || !target)
5164 put_page(page);
5165 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005166 /* There is a swap entry and a page doesn't exist or isn't charged */
5167 if (ent.val && !ret &&
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005168 css_id(&mc.from->css) == lookup_swap_cgroup(ent)) {
5169 ret = MC_TARGET_SWAP;
5170 if (target)
5171 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005172 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005173 return ret;
5174}
5175
5176static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
5177 unsigned long addr, unsigned long end,
5178 struct mm_walk *walk)
5179{
5180 struct vm_area_struct *vma = walk->private;
5181 pte_t *pte;
5182 spinlock_t *ptl;
5183
Dave Hansen03319322011-03-22 16:32:56 -07005184 split_huge_page_pmd(walk->mm, pmd);
5185
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005186 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5187 for (; addr != end; pte++, addr += PAGE_SIZE)
5188 if (is_target_pte_for_mc(vma, addr, *pte, NULL))
5189 mc.precharge++; /* increment precharge temporarily */
5190 pte_unmap_unlock(pte - 1, ptl);
5191 cond_resched();
5192
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005193 return 0;
5194}
5195
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005196static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
5197{
5198 unsigned long precharge;
5199 struct vm_area_struct *vma;
5200
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005201 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005202 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5203 struct mm_walk mem_cgroup_count_precharge_walk = {
5204 .pmd_entry = mem_cgroup_count_precharge_pte_range,
5205 .mm = mm,
5206 .private = vma,
5207 };
5208 if (is_vm_hugetlb_page(vma))
5209 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005210 walk_page_range(vma->vm_start, vma->vm_end,
5211 &mem_cgroup_count_precharge_walk);
5212 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005213 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005214
5215 precharge = mc.precharge;
5216 mc.precharge = 0;
5217
5218 return precharge;
5219}
5220
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005221static int mem_cgroup_precharge_mc(struct mm_struct *mm)
5222{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005223 unsigned long precharge = mem_cgroup_count_precharge(mm);
5224
5225 VM_BUG_ON(mc.moving_task);
5226 mc.moving_task = current;
5227 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005228}
5229
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005230/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
5231static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005232{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005233 struct mem_cgroup *from = mc.from;
5234 struct mem_cgroup *to = mc.to;
5235
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005236 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005237 if (mc.precharge) {
5238 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
5239 mc.precharge = 0;
5240 }
5241 /*
5242 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
5243 * we must uncharge here.
5244 */
5245 if (mc.moved_charge) {
5246 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
5247 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005248 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005249 /* we must fixup refcnts and charges */
5250 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005251 /* uncharge swap account from the old cgroup */
5252 if (!mem_cgroup_is_root(mc.from))
5253 res_counter_uncharge(&mc.from->memsw,
5254 PAGE_SIZE * mc.moved_swap);
5255 __mem_cgroup_put(mc.from, mc.moved_swap);
5256
5257 if (!mem_cgroup_is_root(mc.to)) {
5258 /*
5259 * we charged both to->res and to->memsw, so we should
5260 * uncharge to->res.
5261 */
5262 res_counter_uncharge(&mc.to->res,
5263 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005264 }
5265 /* we've already done mem_cgroup_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005266 mc.moved_swap = 0;
5267 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005268 memcg_oom_recover(from);
5269 memcg_oom_recover(to);
5270 wake_up_all(&mc.waitq);
5271}
5272
5273static void mem_cgroup_clear_mc(void)
5274{
5275 struct mem_cgroup *from = mc.from;
5276
5277 /*
5278 * we must clear moving_task before waking up waiters at the end of
5279 * task migration.
5280 */
5281 mc.moving_task = NULL;
5282 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005283 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005284 mc.from = NULL;
5285 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005286 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005287 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005288}
5289
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005290static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
5291 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005292 struct task_struct *p)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005293{
5294 int ret = 0;
5295 struct mem_cgroup *mem = mem_cgroup_from_cont(cgroup);
5296
5297 if (mem->move_charge_at_immigrate) {
5298 struct mm_struct *mm;
5299 struct mem_cgroup *from = mem_cgroup_from_task(p);
5300
5301 VM_BUG_ON(from == mem);
5302
5303 mm = get_task_mm(p);
5304 if (!mm)
5305 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005306 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005307 if (mm->owner == p) {
5308 VM_BUG_ON(mc.from);
5309 VM_BUG_ON(mc.to);
5310 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005311 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005312 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005313 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005314 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005315 mc.from = from;
5316 mc.to = mem;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005317 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005318 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005319
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005320 ret = mem_cgroup_precharge_mc(mm);
5321 if (ret)
5322 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005323 }
5324 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005325 }
5326 return ret;
5327}
5328
5329static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
5330 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005331 struct task_struct *p)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005332{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005333 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005334}
5335
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005336static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
5337 unsigned long addr, unsigned long end,
5338 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005339{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005340 int ret = 0;
5341 struct vm_area_struct *vma = walk->private;
5342 pte_t *pte;
5343 spinlock_t *ptl;
5344
Dave Hansen03319322011-03-22 16:32:56 -07005345 split_huge_page_pmd(walk->mm, pmd);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005346retry:
5347 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5348 for (; addr != end; addr += PAGE_SIZE) {
5349 pte_t ptent = *(pte++);
5350 union mc_target target;
5351 int type;
5352 struct page *page;
5353 struct page_cgroup *pc;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005354 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005355
5356 if (!mc.precharge)
5357 break;
5358
5359 type = is_target_pte_for_mc(vma, addr, ptent, &target);
5360 switch (type) {
5361 case MC_TARGET_PAGE:
5362 page = target.page;
5363 if (isolate_lru_page(page))
5364 goto put;
5365 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07005366 if (!mem_cgroup_move_account(page, 1, pc,
5367 mc.from, mc.to, false)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005368 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005369 /* we uncharge from mc.from later. */
5370 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005371 }
5372 putback_lru_page(page);
5373put: /* is_target_pte_for_mc() gets the page */
5374 put_page(page);
5375 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005376 case MC_TARGET_SWAP:
5377 ent = target.ent;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005378 if (!mem_cgroup_move_swap_account(ent,
5379 mc.from, mc.to, false)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08005380 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005381 /* we fixup refcnts and charges later. */
5382 mc.moved_swap++;
5383 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08005384 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005385 default:
5386 break;
5387 }
5388 }
5389 pte_unmap_unlock(pte - 1, ptl);
5390 cond_resched();
5391
5392 if (addr != end) {
5393 /*
5394 * We have consumed all precharges we got in can_attach().
5395 * We try charge one by one, but don't do any additional
5396 * charges to mc.to if we have failed in charge once in attach()
5397 * phase.
5398 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005399 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005400 if (!ret)
5401 goto retry;
5402 }
5403
5404 return ret;
5405}
5406
5407static void mem_cgroup_move_charge(struct mm_struct *mm)
5408{
5409 struct vm_area_struct *vma;
5410
5411 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005412retry:
5413 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
5414 /*
5415 * Someone who are holding the mmap_sem might be waiting in
5416 * waitq. So we cancel all extra charges, wake up all waiters,
5417 * and retry. Because we cancel precharges, we might not be able
5418 * to move enough charges, but moving charge is a best-effort
5419 * feature anyway, so it wouldn't be a big problem.
5420 */
5421 __mem_cgroup_clear_mc();
5422 cond_resched();
5423 goto retry;
5424 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005425 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5426 int ret;
5427 struct mm_walk mem_cgroup_move_charge_walk = {
5428 .pmd_entry = mem_cgroup_move_charge_pte_range,
5429 .mm = mm,
5430 .private = vma,
5431 };
5432 if (is_vm_hugetlb_page(vma))
5433 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005434 ret = walk_page_range(vma->vm_start, vma->vm_end,
5435 &mem_cgroup_move_charge_walk);
5436 if (ret)
5437 /*
5438 * means we have consumed all precharges and failed in
5439 * doing additional charge. Just abandon here.
5440 */
5441 break;
5442 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005443 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005444}
5445
Balbir Singh67e465a2008-02-07 00:13:54 -08005446static void mem_cgroup_move_task(struct cgroup_subsys *ss,
5447 struct cgroup *cont,
5448 struct cgroup *old_cont,
Ben Blumf780bdb2011-05-26 16:25:19 -07005449 struct task_struct *p)
Balbir Singh67e465a2008-02-07 00:13:54 -08005450{
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005451 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005452
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005453 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005454 if (mc.to)
5455 mem_cgroup_move_charge(mm);
5456 put_swap_token(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005457 mmput(mm);
5458 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005459 if (mc.to)
5460 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08005461}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005462#else /* !CONFIG_MMU */
5463static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
5464 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005465 struct task_struct *p)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005466{
5467 return 0;
5468}
5469static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
5470 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005471 struct task_struct *p)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005472{
5473}
5474static void mem_cgroup_move_task(struct cgroup_subsys *ss,
5475 struct cgroup *cont,
5476 struct cgroup *old_cont,
Ben Blumf780bdb2011-05-26 16:25:19 -07005477 struct task_struct *p)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005478{
5479}
5480#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08005481
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005482struct cgroup_subsys mem_cgroup_subsys = {
5483 .name = "memory",
5484 .subsys_id = mem_cgroup_subsys_id,
5485 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005486 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005487 .destroy = mem_cgroup_destroy,
5488 .populate = mem_cgroup_populate,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005489 .can_attach = mem_cgroup_can_attach,
5490 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08005491 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005492 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005493 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005494};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005495
5496#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08005497static int __init enable_swap_account(char *s)
5498{
5499 /* consider enabled if no parameter or 1 is given */
Michal Hockoa2c89902011-05-24 17:12:50 -07005500 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08005501 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07005502 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08005503 really_do_swap_account = 0;
5504 return 1;
5505}
Michal Hockoa2c89902011-05-24 17:12:50 -07005506__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005507
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005508#endif