blob: ce7b35d024e9e8701c44f1d6f22a44fc62084fa4 [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 Hiroyuki453a9bf2011-07-08 15:39:43 -0700110 MEM_CGROUP_TARGET_NUMAINFO,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700111 MEM_CGROUP_NTARGETS,
112};
113#define THRESHOLDS_EVENTS_TARGET (128)
114#define SOFTLIMIT_EVENTS_TARGET (1024)
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700115#define NUMAINFO_EVENTS_TARGET (1024)
Johannes Weinere9f89742011-03-23 16:42:37 -0700116
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800117struct mem_cgroup_stat_cpu {
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700118 long count[MEM_CGROUP_STAT_NSTATS];
Johannes Weinere9f89742011-03-23 16:42:37 -0700119 unsigned long events[MEM_CGROUP_EVENTS_NSTATS];
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700120 unsigned long targets[MEM_CGROUP_NTARGETS];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800121};
122
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800123/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800124 * per-zone information in memory controller.
125 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800126struct mem_cgroup_per_zone {
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800127 /*
128 * spin_lock to protect the per cgroup LRU
129 */
Christoph Lameterb69408e2008-10-18 20:26:14 -0700130 struct list_head lists[NR_LRU_LISTS];
131 unsigned long count[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800132
133 struct zone_reclaim_stat reclaim_stat;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700134 struct rb_node tree_node; /* RB tree node */
135 unsigned long long usage_in_excess;/* Set to the value by which */
136 /* the soft limit is exceeded*/
137 bool on_tree;
Balbir Singh4e416952009-09-23 15:56:39 -0700138 struct mem_cgroup *mem; /* Back pointer, we cannot */
139 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800140};
141/* Macro for accessing counter */
142#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
143
144struct mem_cgroup_per_node {
145 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
146};
147
148struct mem_cgroup_lru_info {
149 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
150};
151
152/*
Balbir Singhf64c3f52009-09-23 15:56:37 -0700153 * Cgroups above their limits are maintained in a RB-Tree, independent of
154 * their hierarchy representation
155 */
156
157struct mem_cgroup_tree_per_zone {
158 struct rb_root rb_root;
159 spinlock_t lock;
160};
161
162struct mem_cgroup_tree_per_node {
163 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
164};
165
166struct mem_cgroup_tree {
167 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
168};
169
170static struct mem_cgroup_tree soft_limit_tree __read_mostly;
171
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800172struct mem_cgroup_threshold {
173 struct eventfd_ctx *eventfd;
174 u64 threshold;
175};
176
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700177/* For threshold */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800178struct mem_cgroup_threshold_ary {
179 /* An array index points to threshold just below usage. */
Phil Carmody5407a562010-05-26 14:42:42 -0700180 int current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800181 /* Size of entries[] */
182 unsigned int size;
183 /* Array of thresholds */
184 struct mem_cgroup_threshold entries[0];
185};
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700186
187struct mem_cgroup_thresholds {
188 /* Primary thresholds array */
189 struct mem_cgroup_threshold_ary *primary;
190 /*
191 * Spare threshold array.
192 * This is needed to make mem_cgroup_unregister_event() "never fail".
193 * It must be able to store at least primary->size - 1 entries.
194 */
195 struct mem_cgroup_threshold_ary *spare;
196};
197
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700198/* for OOM */
199struct mem_cgroup_eventfd_list {
200 struct list_head list;
201 struct eventfd_ctx *eventfd;
202};
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800203
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700204static void mem_cgroup_threshold(struct mem_cgroup *memcg);
205static void mem_cgroup_oom_notify(struct mem_cgroup *memcg);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800206
Balbir Singhf64c3f52009-09-23 15:56:37 -0700207/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800208 * The memory controller data structure. The memory controller controls both
209 * page cache and RSS per cgroup. We would eventually like to provide
210 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
211 * to help the administrator determine what knobs to tune.
212 *
213 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800214 * we hit the water mark. May be even add a low water mark, such that
215 * no reclaim occurs from a cgroup at it's low water mark, this is
216 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800217 */
218struct mem_cgroup {
219 struct cgroup_subsys_state css;
220 /*
221 * the counter to account for memory usage
222 */
223 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800224 /*
KAMEZAWA 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 Hiroyuki453a9bf2011-07-08 15:39:43 -0700241 atomic_t numainfo_events;
242 atomic_t numainfo_updating;
Ying Han889976d2011-05-26 16:25:33 -0700243#endif
Balbir Singh18f59ea2009-01-07 18:08:07 -0800244 /*
245 * Should the accounting and control be hierarchical, per subtree?
246 */
247 bool use_hierarchy;
Michal Hocko79dfdac2011-07-26 16:08:23 -0700248
249 bool oom_lock;
250 atomic_t under_oom;
251
KAMEZAWA 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 Hiroyuki8a9478c2009-06-17 16:27:17 -0700341 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700342 NR_CHARGE_TYPE,
343};
344
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800345/* for encoding cft->private value on file */
346#define _MEM (0)
347#define _MEMSWAP (1)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700348#define _OOM_TYPE (2)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800349#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
350#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
351#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700352/* Used for OOM nofiier */
353#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800354
Balbir Singh75822b42009-09-23 15:56:38 -0700355/*
356 * Reclaim flags for mem_cgroup_hierarchical_reclaim
357 */
358#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
359#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
360#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
361#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
Balbir Singh4e416952009-09-23 15:56:39 -0700362#define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
363#define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
Balbir Singh75822b42009-09-23 15:56:38 -0700364
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700365static void mem_cgroup_get(struct mem_cgroup *memcg);
366static void mem_cgroup_put(struct mem_cgroup *memcg);
367static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg);
368static void drain_all_stock_async(struct mem_cgroup *memcg);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800369
Balbir Singhf64c3f52009-09-23 15:56:37 -0700370static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700371mem_cgroup_zoneinfo(struct mem_cgroup *memcg, int nid, int zid)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700372{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700373 return &memcg->info.nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700374}
375
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700376struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100377{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700378 return &memcg->css;
Wu Fengguangd3242362009-12-16 12:19:59 +0100379}
380
Balbir Singhf64c3f52009-09-23 15:56:37 -0700381static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700382page_cgroup_zoneinfo(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700383{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700384 int nid = page_to_nid(page);
385 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700386
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700387 return mem_cgroup_zoneinfo(memcg, nid, zid);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700388}
389
390static struct mem_cgroup_tree_per_zone *
391soft_limit_tree_node_zone(int nid, int zid)
392{
393 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
394}
395
396static struct mem_cgroup_tree_per_zone *
397soft_limit_tree_from_page(struct page *page)
398{
399 int nid = page_to_nid(page);
400 int zid = page_zonenum(page);
401
402 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
403}
404
405static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700406__mem_cgroup_insert_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700407 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700408 struct mem_cgroup_tree_per_zone *mctz,
409 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700410{
411 struct rb_node **p = &mctz->rb_root.rb_node;
412 struct rb_node *parent = NULL;
413 struct mem_cgroup_per_zone *mz_node;
414
415 if (mz->on_tree)
416 return;
417
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700418 mz->usage_in_excess = new_usage_in_excess;
419 if (!mz->usage_in_excess)
420 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700421 while (*p) {
422 parent = *p;
423 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
424 tree_node);
425 if (mz->usage_in_excess < mz_node->usage_in_excess)
426 p = &(*p)->rb_left;
427 /*
428 * We can't avoid mem cgroups that are over their soft
429 * limit by the same amount
430 */
431 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
432 p = &(*p)->rb_right;
433 }
434 rb_link_node(&mz->tree_node, parent, p);
435 rb_insert_color(&mz->tree_node, &mctz->rb_root);
436 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700437}
438
439static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700440__mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singh4e416952009-09-23 15:56:39 -0700441 struct mem_cgroup_per_zone *mz,
442 struct mem_cgroup_tree_per_zone *mctz)
443{
444 if (!mz->on_tree)
445 return;
446 rb_erase(&mz->tree_node, &mctz->rb_root);
447 mz->on_tree = false;
448}
449
450static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700451mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700452 struct mem_cgroup_per_zone *mz,
453 struct mem_cgroup_tree_per_zone *mctz)
454{
455 spin_lock(&mctz->lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700456 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700457 spin_unlock(&mctz->lock);
458}
459
Balbir Singhf64c3f52009-09-23 15:56:37 -0700460
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700461static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700462{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700463 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700464 struct mem_cgroup_per_zone *mz;
465 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700466 int nid = page_to_nid(page);
467 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700468 mctz = soft_limit_tree_from_page(page);
469
470 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700471 * Necessary to update all ancestors when hierarchy is used.
472 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700473 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700474 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
475 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
476 excess = res_counter_soft_limit_excess(&memcg->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700477 /*
478 * We have to update the tree if mz is on RB-tree or
479 * mem is over its softlimit.
480 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700481 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700482 spin_lock(&mctz->lock);
483 /* if on-tree, remove it */
484 if (mz->on_tree)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700485 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700486 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700487 * Insert again. mz->usage_in_excess will be updated.
488 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700489 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700490 __mem_cgroup_insert_exceeded(memcg, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700491 spin_unlock(&mctz->lock);
492 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700493 }
494}
495
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700496static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700497{
498 int node, zone;
499 struct mem_cgroup_per_zone *mz;
500 struct mem_cgroup_tree_per_zone *mctz;
501
502 for_each_node_state(node, N_POSSIBLE) {
503 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700504 mz = mem_cgroup_zoneinfo(memcg, node, zone);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700505 mctz = soft_limit_tree_node_zone(node, zone);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700506 mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700507 }
508 }
509}
510
Balbir Singh4e416952009-09-23 15:56:39 -0700511static struct mem_cgroup_per_zone *
512__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
513{
514 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700515 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700516
517retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700518 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700519 rightmost = rb_last(&mctz->rb_root);
520 if (!rightmost)
521 goto done; /* Nothing to reclaim from */
522
523 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
524 /*
525 * Remove the node now but someone else can add it back,
526 * we will to add it back at the end of reclaim to its correct
527 * position in the tree.
528 */
529 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
530 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
531 !css_tryget(&mz->mem->css))
532 goto retry;
533done:
534 return mz;
535}
536
537static struct mem_cgroup_per_zone *
538mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
539{
540 struct mem_cgroup_per_zone *mz;
541
542 spin_lock(&mctz->lock);
543 mz = __mem_cgroup_largest_soft_limit_node(mctz);
544 spin_unlock(&mctz->lock);
545 return mz;
546}
547
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700548/*
549 * Implementation Note: reading percpu statistics for memcg.
550 *
551 * Both of vmstat[] and percpu_counter has threshold and do periodic
552 * synchronization to implement "quick" read. There are trade-off between
553 * reading cost and precision of value. Then, we may have a chance to implement
554 * a periodic synchronizion of counter in memcg's counter.
555 *
556 * But this _read() function is used for user interface now. The user accounts
557 * memory usage by memory cgroup and he _always_ requires exact value because
558 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
559 * have to visit all online cpus and make sum. So, for now, unnecessary
560 * synchronization is not implemented. (just implemented for cpu hotplug)
561 *
562 * If there are kernel internal actions which can make use of some not-exact
563 * value, and reading all cpu value can be performance bottleneck in some
564 * common workload, threashold and synchonization as vmstat[] should be
565 * implemented.
566 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700567static long mem_cgroup_read_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700568 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800569{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700570 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800571 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800572
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700573 get_online_cpus();
574 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700575 val += per_cpu(memcg->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700576#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700577 spin_lock(&memcg->pcp_counter_lock);
578 val += memcg->nocpu_base.count[idx];
579 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700580#endif
581 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800582 return val;
583}
584
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700585static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700586 bool charge)
587{
588 int val = (charge) ? 1 : -1;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700589 this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAPOUT], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700590}
591
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700592void mem_cgroup_pgfault(struct mem_cgroup *memcg, int val)
Ying Han456f9982011-05-26 16:25:38 -0700593{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700594 this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT], val);
Ying Han456f9982011-05-26 16:25:38 -0700595}
596
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700597void mem_cgroup_pgmajfault(struct mem_cgroup *memcg, int val)
Ying Han456f9982011-05-26 16:25:38 -0700598{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700599 this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT], val);
Ying Han456f9982011-05-26 16:25:38 -0700600}
601
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700602static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg,
Johannes Weinere9f89742011-03-23 16:42:37 -0700603 enum mem_cgroup_events_index idx)
604{
605 unsigned long val = 0;
606 int cpu;
607
608 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700609 val += per_cpu(memcg->stat->events[idx], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -0700610#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700611 spin_lock(&memcg->pcp_counter_lock);
612 val += memcg->nocpu_base.events[idx];
613 spin_unlock(&memcg->pcp_counter_lock);
Johannes Weinere9f89742011-03-23 16:42:37 -0700614#endif
615 return val;
616}
617
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700618static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800619 bool file, int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800620{
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800621 preempt_disable();
622
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800623 if (file)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700624 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE],
625 nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800626 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700627 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS],
628 nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700629
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800630 /* pagein of a big page is an event. So, ignore page size */
631 if (nr_pages > 0)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700632 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800633 else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700634 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800635 nr_pages = -nr_pages; /* for event */
636 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800637
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700638 __this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_COUNT], nr_pages);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800639
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800640 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800641}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800642
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700643unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700644mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg, int nid, int zid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700645 unsigned int lru_mask)
Ying Han889976d2011-05-26 16:25:33 -0700646{
647 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700648 enum lru_list l;
649 unsigned long ret = 0;
650
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700651 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700652
653 for_each_lru(l) {
654 if (BIT(l) & lru_mask)
655 ret += MEM_CGROUP_ZSTAT(mz, l);
656 }
657 return ret;
658}
659
660static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700661mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700662 int nid, unsigned int lru_mask)
663{
Ying Han889976d2011-05-26 16:25:33 -0700664 u64 total = 0;
665 int zid;
666
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700667 for (zid = 0; zid < MAX_NR_ZONES; zid++)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700668 total += mem_cgroup_zone_nr_lru_pages(memcg,
669 nid, zid, lru_mask);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700670
Ying Han889976d2011-05-26 16:25:33 -0700671 return total;
672}
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700673
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700674static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700675 unsigned int lru_mask)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800676{
Ying Han889976d2011-05-26 16:25:33 -0700677 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800678 u64 total = 0;
679
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700680 for_each_node_state(nid, N_HIGH_MEMORY)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700681 total += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800682 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800683}
684
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700685static bool __memcg_event_check(struct mem_cgroup *memcg, int target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800686{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700687 unsigned long val, next;
688
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700689 val = this_cpu_read(memcg->stat->events[MEM_CGROUP_EVENTS_COUNT]);
690 next = this_cpu_read(memcg->stat->targets[target]);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700691 /* from time_after() in jiffies.h */
692 return ((long)next - (long)val < 0);
693}
694
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700695static void __mem_cgroup_target_update(struct mem_cgroup *memcg, int target)
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700696{
697 unsigned long val, next;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800698
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700699 val = this_cpu_read(memcg->stat->events[MEM_CGROUP_EVENTS_COUNT]);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800700
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700701 switch (target) {
702 case MEM_CGROUP_TARGET_THRESH:
703 next = val + THRESHOLDS_EVENTS_TARGET;
704 break;
705 case MEM_CGROUP_TARGET_SOFTLIMIT:
706 next = val + SOFTLIMIT_EVENTS_TARGET;
707 break;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700708 case MEM_CGROUP_TARGET_NUMAINFO:
709 next = val + NUMAINFO_EVENTS_TARGET;
710 break;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700711 default:
712 return;
713 }
714
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700715 this_cpu_write(memcg->stat->targets[target], next);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800716}
717
718/*
719 * Check events in order.
720 *
721 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700722static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800723{
724 /* threshold event is triggered in finer grain than soft limit */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700725 if (unlikely(__memcg_event_check(memcg, MEM_CGROUP_TARGET_THRESH))) {
726 mem_cgroup_threshold(memcg);
727 __mem_cgroup_target_update(memcg, MEM_CGROUP_TARGET_THRESH);
728 if (unlikely(__memcg_event_check(memcg,
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700729 MEM_CGROUP_TARGET_SOFTLIMIT))) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700730 mem_cgroup_update_tree(memcg, page);
731 __mem_cgroup_target_update(memcg,
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700732 MEM_CGROUP_TARGET_SOFTLIMIT);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700733 }
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700734#if MAX_NUMNODES > 1
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700735 if (unlikely(__memcg_event_check(memcg,
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700736 MEM_CGROUP_TARGET_NUMAINFO))) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700737 atomic_inc(&memcg->numainfo_events);
738 __mem_cgroup_target_update(memcg,
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700739 MEM_CGROUP_TARGET_NUMAINFO);
740 }
741#endif
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800742 }
743}
744
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800745static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800746{
747 return container_of(cgroup_subsys_state(cont,
748 mem_cgroup_subsys_id), struct mem_cgroup,
749 css);
750}
751
Balbir Singhcf475ad2008-04-29 01:00:16 -0700752struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800753{
Balbir Singh31a78f22008-09-28 23:09:31 +0100754 /*
755 * mm_update_next_owner() may clear mm->owner to NULL
756 * if it races with swapoff, page migration, etc.
757 * So this can be called with p == NULL.
758 */
759 if (unlikely(!p))
760 return NULL;
761
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800762 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
763 struct mem_cgroup, css);
764}
765
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700766struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800767{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700768 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700769
770 if (!mm)
771 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800772 /*
773 * Because we have no locks, mm->owner's may be being moved to other
774 * cgroup. We use css_tryget() here even if this looks
775 * pessimistic (rather than adding locks here).
776 */
777 rcu_read_lock();
778 do {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700779 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
780 if (unlikely(!memcg))
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800781 break;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700782 } while (!css_tryget(&memcg->css));
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800783 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700784 return memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800785}
786
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700787/* The caller has to guarantee "mem" exists before calling this */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700788static struct mem_cgroup *mem_cgroup_start_loop(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700789{
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700790 struct cgroup_subsys_state *css;
791 int found;
792
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700793 if (!memcg) /* ROOT cgroup has the smallest ID */
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700794 return root_mem_cgroup; /*css_put/get against root is ignored*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700795 if (!memcg->use_hierarchy) {
796 if (css_tryget(&memcg->css))
797 return memcg;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700798 return NULL;
799 }
800 rcu_read_lock();
801 /*
802 * searching a memory cgroup which has the smallest ID under given
803 * ROOT cgroup. (ID >= 1)
804 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700805 css = css_get_next(&mem_cgroup_subsys, 1, &memcg->css, &found);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700806 if (css && css_tryget(css))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700807 memcg = container_of(css, struct mem_cgroup, css);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700808 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700809 memcg = NULL;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700810 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700811 return memcg;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700812}
813
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700814static struct mem_cgroup *mem_cgroup_get_next(struct mem_cgroup *iter,
815 struct mem_cgroup *root,
816 bool cond)
817{
818 int nextid = css_id(&iter->css) + 1;
819 int found;
820 int hierarchy_used;
821 struct cgroup_subsys_state *css;
822
823 hierarchy_used = iter->use_hierarchy;
824
825 css_put(&iter->css);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700826 /* If no ROOT, walk all, ignore hierarchy */
827 if (!cond || (root && !hierarchy_used))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700828 return NULL;
829
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700830 if (!root)
831 root = root_mem_cgroup;
832
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700833 do {
834 iter = NULL;
835 rcu_read_lock();
836
837 css = css_get_next(&mem_cgroup_subsys, nextid,
838 &root->css, &found);
839 if (css && css_tryget(css))
840 iter = container_of(css, struct mem_cgroup, css);
841 rcu_read_unlock();
842 /* If css is NULL, no more cgroups will be found */
843 nextid = found + 1;
844 } while (css && !iter);
845
846 return iter;
847}
848/*
849 * for_eacn_mem_cgroup_tree() for visiting all cgroup under tree. Please
850 * be careful that "break" loop is not allowed. We have reference count.
851 * Instead of that modify "cond" to be false and "continue" to exit the loop.
852 */
853#define for_each_mem_cgroup_tree_cond(iter, root, cond) \
854 for (iter = mem_cgroup_start_loop(root);\
855 iter != NULL;\
856 iter = mem_cgroup_get_next(iter, root, cond))
857
858#define for_each_mem_cgroup_tree(iter, root) \
859 for_each_mem_cgroup_tree_cond(iter, root, true)
860
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700861#define for_each_mem_cgroup_all(iter) \
862 for_each_mem_cgroup_tree_cond(iter, NULL, true)
863
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700864
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700865static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
Balbir Singh4b3bde42009-09-23 15:56:32 -0700866{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700867 return (memcg == root_mem_cgroup);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700868}
869
Ying Han456f9982011-05-26 16:25:38 -0700870void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
871{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700872 struct mem_cgroup *memcg;
Ying Han456f9982011-05-26 16:25:38 -0700873
874 if (!mm)
875 return;
876
877 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700878 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
879 if (unlikely(!memcg))
Ying Han456f9982011-05-26 16:25:38 -0700880 goto out;
881
882 switch (idx) {
883 case PGMAJFAULT:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700884 mem_cgroup_pgmajfault(memcg, 1);
Ying Han456f9982011-05-26 16:25:38 -0700885 break;
886 case PGFAULT:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700887 mem_cgroup_pgfault(memcg, 1);
Ying Han456f9982011-05-26 16:25:38 -0700888 break;
889 default:
890 BUG();
891 }
892out:
893 rcu_read_unlock();
894}
895EXPORT_SYMBOL(mem_cgroup_count_vm_event);
896
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800897/*
898 * Following LRU functions are allowed to be used without PCG_LOCK.
899 * Operations are called by routine of global LRU independently from memcg.
900 * What we have to take care of here is validness of pc->mem_cgroup.
901 *
902 * Changes to pc->mem_cgroup happens when
903 * 1. charge
904 * 2. moving account
905 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
906 * It is added to LRU before charge.
907 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
908 * When moving account, the page is not on LRU. It's isolated.
909 */
910
911void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800912{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800913 struct page_cgroup *pc;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800914 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700915
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800916 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800917 return;
918 pc = lookup_page_cgroup(page);
919 /* can happen while we handle swapcache. */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700920 if (!TestClearPageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800921 return;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700922 VM_BUG_ON(!pc->mem_cgroup);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800923 /*
924 * We don't check PCG_USED bit. It's cleared when the "page" is finally
925 * removed from global LRU.
926 */
Johannes Weiner97a6c372011-03-23 16:42:27 -0700927 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -0800928 /* huge page split is done under lru_lock. so, we have no races. */
929 MEM_CGROUP_ZSTAT(mz, lru) -= 1 << compound_order(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700930 if (mem_cgroup_is_root(pc->mem_cgroup))
931 return;
932 VM_BUG_ON(list_empty(&pc->lru));
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800933 list_del_init(&pc->lru);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800934}
935
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800936void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800937{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800938 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800939}
940
Minchan Kim3f58a822011-03-22 16:32:53 -0700941/*
942 * Writeback is about to end against a page which has been marked for immediate
943 * reclaim. If it still appears to be reclaimable, move it to the tail of the
944 * inactive list.
945 */
946void mem_cgroup_rotate_reclaimable_page(struct page *page)
947{
948 struct mem_cgroup_per_zone *mz;
949 struct page_cgroup *pc;
950 enum lru_list lru = page_lru(page);
951
952 if (mem_cgroup_disabled())
953 return;
954
955 pc = lookup_page_cgroup(page);
956 /* unused or root page is not rotated. */
957 if (!PageCgroupUsed(pc))
958 return;
959 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
960 smp_rmb();
961 if (mem_cgroup_is_root(pc->mem_cgroup))
962 return;
Johannes Weiner97a6c372011-03-23 16:42:27 -0700963 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
Minchan Kim3f58a822011-03-22 16:32:53 -0700964 list_move_tail(&pc->lru, &mz->lists[lru]);
965}
966
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800967void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800968{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800969 struct mem_cgroup_per_zone *mz;
970 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800971
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800972 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700973 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700974
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800975 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700976 /* unused or root page is not rotated. */
Johannes Weiner713735b2011-01-20 14:44:31 -0800977 if (!PageCgroupUsed(pc))
978 return;
979 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
980 smp_rmb();
981 if (mem_cgroup_is_root(pc->mem_cgroup))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800982 return;
Johannes Weiner97a6c372011-03-23 16:42:27 -0700983 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700984 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800985}
986
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800987void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
988{
989 struct page_cgroup *pc;
990 struct mem_cgroup_per_zone *mz;
991
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800992 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800993 return;
994 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700995 VM_BUG_ON(PageCgroupAcctLRU(pc));
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800996 if (!PageCgroupUsed(pc))
997 return;
Johannes Weiner713735b2011-01-20 14:44:31 -0800998 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
999 smp_rmb();
Johannes Weiner97a6c372011-03-23 16:42:27 -07001000 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08001001 /* huge page split is done under lru_lock. so, we have no races. */
1002 MEM_CGROUP_ZSTAT(mz, lru) += 1 << compound_order(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -07001003 SetPageCgroupAcctLRU(pc);
1004 if (mem_cgroup_is_root(pc->mem_cgroup))
1005 return;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001006 list_add(&pc->lru, &mz->lists[lru]);
1007}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001008
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001009/*
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001010 * At handling SwapCache and other FUSE stuff, pc->mem_cgroup may be changed
1011 * while it's linked to lru because the page may be reused after it's fully
1012 * uncharged. To handle that, unlink page_cgroup from LRU when charge it again.
1013 * It's done under lock_page and expected that zone->lru_lock isnever held.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001014 */
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001015static void mem_cgroup_lru_del_before_commit(struct page *page)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001016{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001017 unsigned long flags;
1018 struct zone *zone = page_zone(page);
1019 struct page_cgroup *pc = lookup_page_cgroup(page);
1020
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001021 /*
1022 * Doing this check without taking ->lru_lock seems wrong but this
1023 * is safe. Because if page_cgroup's USED bit is unset, the page
1024 * will not be added to any memcg's LRU. If page_cgroup's USED bit is
1025 * set, the commit after this will fail, anyway.
1026 * This all charge/uncharge is done under some mutual execustion.
1027 * So, we don't need to taking care of changes in USED bit.
1028 */
1029 if (likely(!PageLRU(page)))
1030 return;
1031
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001032 spin_lock_irqsave(&zone->lru_lock, flags);
1033 /*
1034 * Forget old LRU when this page_cgroup is *not* used. This Used bit
1035 * is guarded by lock_page() because the page is SwapCache.
1036 */
1037 if (!PageCgroupUsed(pc))
1038 mem_cgroup_del_lru_list(page, page_lru(page));
1039 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001040}
1041
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001042static void mem_cgroup_lru_add_after_commit(struct page *page)
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001043{
1044 unsigned long flags;
1045 struct zone *zone = page_zone(page);
1046 struct page_cgroup *pc = lookup_page_cgroup(page);
1047
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001048 /* taking care of that the page is added to LRU while we commit it */
1049 if (likely(!PageLRU(page)))
1050 return;
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001051 spin_lock_irqsave(&zone->lru_lock, flags);
1052 /* link when the page is linked to LRU but page_cgroup isn't */
Balbir Singh4b3bde42009-09-23 15:56:32 -07001053 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001054 mem_cgroup_add_lru_list(page, page_lru(page));
1055 spin_unlock_irqrestore(&zone->lru_lock, flags);
1056}
1057
1058
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001059void mem_cgroup_move_lists(struct page *page,
1060 enum lru_list from, enum lru_list to)
1061{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001062 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001063 return;
1064 mem_cgroup_del_lru_list(page, from);
1065 mem_cgroup_add_lru_list(page, to);
1066}
1067
Michal Hocko3e920412011-07-26 16:08:29 -07001068/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001069 * Checks whether given mem is same or in the root_mem_cgroup's
Michal Hocko3e920412011-07-26 16:08:29 -07001070 * hierarchy subtree
1071 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001072static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1073 struct mem_cgroup *memcg)
Michal Hocko3e920412011-07-26 16:08:29 -07001074{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001075 if (root_memcg != memcg) {
1076 return (root_memcg->use_hierarchy &&
1077 css_is_ancestor(&memcg->css, &root_memcg->css));
Michal Hocko3e920412011-07-26 16:08:29 -07001078 }
1079
1080 return true;
1081}
1082
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001083int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -08001084{
1085 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001086 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001087 struct task_struct *p;
David Rientjes4c4a2212008-02-07 00:14:06 -08001088
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001089 p = find_lock_task_mm(task);
1090 if (!p)
1091 return 0;
1092 curr = try_get_mem_cgroup_from_mm(p->mm);
1093 task_unlock(p);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001094 if (!curr)
1095 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001096 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001097 * We should check use_hierarchy of "memcg" not "curr". Because checking
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001098 * use_hierarchy of "curr" here make this function true if hierarchy is
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001099 * enabled in "curr" and "curr" is a child of "memcg" in *cgroup*
1100 * hierarchy(even if use_hierarchy is disabled in "memcg").
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001101 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001102 ret = mem_cgroup_same_or_subtree(memcg, curr);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001103 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001104 return ret;
1105}
1106
Johannes Weiner9b272972011-11-02 13:38:23 -07001107int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg, struct zone *zone)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001108{
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001109 unsigned long inactive_ratio;
Johannes Weiner9b272972011-11-02 13:38:23 -07001110 int nid = zone_to_nid(zone);
1111 int zid = zone_idx(zone);
1112 unsigned long inactive;
1113 unsigned long active;
1114 unsigned long gb;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001115
Johannes Weiner9b272972011-11-02 13:38:23 -07001116 inactive = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid,
1117 BIT(LRU_INACTIVE_ANON));
1118 active = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid,
1119 BIT(LRU_ACTIVE_ANON));
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001120
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001121 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1122 if (gb)
1123 inactive_ratio = int_sqrt(10 * gb);
1124 else
1125 inactive_ratio = 1;
1126
Johannes Weiner9b272972011-11-02 13:38:23 -07001127 return inactive * inactive_ratio < active;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001128}
1129
Johannes Weiner9b272972011-11-02 13:38:23 -07001130int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg, struct zone *zone)
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001131{
1132 unsigned long active;
1133 unsigned long inactive;
Johannes Weiner9b272972011-11-02 13:38:23 -07001134 int zid = zone_idx(zone);
1135 int nid = zone_to_nid(zone);
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001136
Johannes Weiner9b272972011-11-02 13:38:23 -07001137 inactive = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid,
1138 BIT(LRU_INACTIVE_FILE));
1139 active = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid,
1140 BIT(LRU_ACTIVE_FILE));
Rik van Riel56e49d22009-06-16 15:32:28 -07001141
1142 return (active > inactive);
1143}
1144
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001145struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
1146 struct zone *zone)
1147{
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -07001148 int nid = zone_to_nid(zone);
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001149 int zid = zone_idx(zone);
1150 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
1151
1152 return &mz->reclaim_stat;
1153}
1154
1155struct zone_reclaim_stat *
1156mem_cgroup_get_reclaim_stat_from_page(struct page *page)
1157{
1158 struct page_cgroup *pc;
1159 struct mem_cgroup_per_zone *mz;
1160
1161 if (mem_cgroup_disabled())
1162 return NULL;
1163
1164 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -08001165 if (!PageCgroupUsed(pc))
1166 return NULL;
Johannes Weiner713735b2011-01-20 14:44:31 -08001167 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
1168 smp_rmb();
Johannes Weiner97a6c372011-03-23 16:42:27 -07001169 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001170 return &mz->reclaim_stat;
1171}
1172
Balbir Singh66e17072008-02-07 00:13:56 -08001173unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
1174 struct list_head *dst,
1175 unsigned long *scanned, int order,
Minchan Kim4356f212011-10-31 17:06:47 -07001176 isolate_mode_t mode,
1177 struct zone *z,
Balbir Singh66e17072008-02-07 00:13:56 -08001178 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001179 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -08001180{
1181 unsigned long nr_taken = 0;
1182 struct page *page;
1183 unsigned long scan;
1184 LIST_HEAD(pc_list);
1185 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001186 struct page_cgroup *pc, *tmp;
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -07001187 int nid = zone_to_nid(z);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001188 int zid = zone_idx(z);
1189 struct mem_cgroup_per_zone *mz;
Johannes Weinerb7c46d12009-09-21 17:02:56 -07001190 int lru = LRU_FILE * file + active;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001191 int ret;
Balbir Singh66e17072008-02-07 00:13:56 -08001192
Balbir Singhcf475ad2008-04-29 01:00:16 -07001193 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001194 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001195 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -08001196
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001197 scan = 0;
1198 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -08001199 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001200 break;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001201
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001202 if (unlikely(!PageCgroupUsed(pc)))
1203 continue;
Johannes Weiner5564e882011-03-23 16:42:29 -07001204
Johannes Weiner6b3ae582011-03-23 16:42:30 -07001205 page = lookup_cgroup_page(pc);
Johannes Weiner5564e882011-03-23 16:42:29 -07001206
Hugh Dickins436c65412008-02-07 00:14:12 -08001207 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001208 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001209
Hugh Dickins436c65412008-02-07 00:14:12 -08001210 scan++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001211 ret = __isolate_lru_page(page, mode, file);
1212 switch (ret) {
1213 case 0:
Balbir Singh66e17072008-02-07 00:13:56 -08001214 list_move(&page->lru, dst);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001215 mem_cgroup_del_lru(page);
Rik van Riel2c888cf2011-01-13 15:47:13 -08001216 nr_taken += hpage_nr_pages(page);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001217 break;
1218 case -EBUSY:
1219 /* we don't affect global LRU but rotate in our LRU */
1220 mem_cgroup_rotate_lru_list(page, page_lru(page));
1221 break;
1222 default:
1223 break;
Balbir Singh66e17072008-02-07 00:13:56 -08001224 }
1225 }
1226
Balbir Singh66e17072008-02-07 00:13:56 -08001227 *scanned = scan;
KOSAKI Motohirocc8e9702010-08-09 17:19:57 -07001228
1229 trace_mm_vmscan_memcg_isolate(0, nr_to_scan, scan, nr_taken,
1230 0, 0, 0, mode);
1231
Balbir Singh66e17072008-02-07 00:13:56 -08001232 return nr_taken;
1233}
1234
Balbir Singh6d61ef42009-01-07 18:08:06 -08001235#define mem_cgroup_from_res_counter(counter, member) \
1236 container_of(counter, struct mem_cgroup, member)
1237
Johannes Weiner19942822011-02-01 15:52:43 -08001238/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001239 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
1240 * @mem: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001241 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001242 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001243 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001244 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001245static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001246{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001247 unsigned long long margin;
1248
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001249 margin = res_counter_margin(&memcg->res);
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001250 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001251 margin = min(margin, res_counter_margin(&memcg->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001252 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001253}
1254
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001255int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001256{
1257 struct cgroup *cgrp = memcg->css.cgroup;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001258
1259 /* root ? */
1260 if (cgrp->parent == NULL)
1261 return vm_swappiness;
1262
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001263 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001264}
1265
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001266static void mem_cgroup_start_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001267{
1268 int cpu;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001269
1270 get_online_cpus();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001271 spin_lock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001272 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001273 per_cpu(memcg->stat->count[MEM_CGROUP_ON_MOVE], cpu) += 1;
1274 memcg->nocpu_base.count[MEM_CGROUP_ON_MOVE] += 1;
1275 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001276 put_online_cpus();
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001277
1278 synchronize_rcu();
1279}
1280
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001281static void mem_cgroup_end_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001282{
1283 int cpu;
1284
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001285 if (!memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001286 return;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001287 get_online_cpus();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001288 spin_lock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001289 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001290 per_cpu(memcg->stat->count[MEM_CGROUP_ON_MOVE], cpu) -= 1;
1291 memcg->nocpu_base.count[MEM_CGROUP_ON_MOVE] -= 1;
1292 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001293 put_online_cpus();
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001294}
1295/*
1296 * 2 routines for checking "mem" is under move_account() or not.
1297 *
1298 * mem_cgroup_stealed() - checking a cgroup is mc.from or not. This is used
1299 * for avoiding race in accounting. If true,
1300 * pc->mem_cgroup may be overwritten.
1301 *
1302 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1303 * under hierarchy of moving cgroups. This is for
1304 * waiting at hith-memory prressure caused by "move".
1305 */
1306
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001307static bool mem_cgroup_stealed(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001308{
1309 VM_BUG_ON(!rcu_read_lock_held());
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001310 return this_cpu_read(memcg->stat->count[MEM_CGROUP_ON_MOVE]) > 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001311}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001312
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001313static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001314{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001315 struct mem_cgroup *from;
1316 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001317 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001318 /*
1319 * Unlike task_move routines, we access mc.to, mc.from not under
1320 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1321 */
1322 spin_lock(&mc.lock);
1323 from = mc.from;
1324 to = mc.to;
1325 if (!from)
1326 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001327
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001328 ret = mem_cgroup_same_or_subtree(memcg, from)
1329 || mem_cgroup_same_or_subtree(memcg, to);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001330unlock:
1331 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001332 return ret;
1333}
1334
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001335static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001336{
1337 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001338 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001339 DEFINE_WAIT(wait);
1340 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1341 /* moving charge context might have finished. */
1342 if (mc.moving_task)
1343 schedule();
1344 finish_wait(&mc.waitq, &wait);
1345 return true;
1346 }
1347 }
1348 return false;
1349}
1350
Balbir Singhe2224322009-04-02 16:57:39 -07001351/**
Kirill A. Shutemov6a6135b2010-03-10 15:22:25 -08001352 * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode.
Balbir Singhe2224322009-04-02 16:57:39 -07001353 * @memcg: The memory cgroup that went over limit
1354 * @p: Task that is going to be killed
1355 *
1356 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1357 * enabled
1358 */
1359void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1360{
1361 struct cgroup *task_cgrp;
1362 struct cgroup *mem_cgrp;
1363 /*
1364 * Need a buffer in BSS, can't rely on allocations. The code relies
1365 * on the assumption that OOM is serialized for memory controller.
1366 * If this assumption is broken, revisit this code.
1367 */
1368 static char memcg_name[PATH_MAX];
1369 int ret;
1370
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001371 if (!memcg || !p)
Balbir Singhe2224322009-04-02 16:57:39 -07001372 return;
1373
1374
1375 rcu_read_lock();
1376
1377 mem_cgrp = memcg->css.cgroup;
1378 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1379
1380 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1381 if (ret < 0) {
1382 /*
1383 * Unfortunately, we are unable to convert to a useful name
1384 * But we'll still print out the usage information
1385 */
1386 rcu_read_unlock();
1387 goto done;
1388 }
1389 rcu_read_unlock();
1390
1391 printk(KERN_INFO "Task in %s killed", memcg_name);
1392
1393 rcu_read_lock();
1394 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1395 if (ret < 0) {
1396 rcu_read_unlock();
1397 goto done;
1398 }
1399 rcu_read_unlock();
1400
1401 /*
1402 * Continues from above, so we don't need an KERN_ level
1403 */
1404 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1405done:
1406
1407 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1408 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1409 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1410 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1411 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1412 "failcnt %llu\n",
1413 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1414 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1415 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1416}
1417
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001418/*
1419 * This function returns the number of memcg under hierarchy tree. Returns
1420 * 1(self count) if no children.
1421 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001422static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001423{
1424 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001425 struct mem_cgroup *iter;
1426
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001427 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001428 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001429 return num;
1430}
1431
Balbir Singh6d61ef42009-01-07 18:08:06 -08001432/*
David Rientjesa63d83f42010-08-09 17:19:46 -07001433 * Return the memory (and swap, if configured) limit for a memcg.
1434 */
1435u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
1436{
1437 u64 limit;
1438 u64 memsw;
1439
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001440 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
1441 limit += total_swap_pages << PAGE_SHIFT;
1442
David Rientjesa63d83f42010-08-09 17:19:46 -07001443 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1444 /*
1445 * If memsw is finite and limits the amount of swap space available
1446 * to this memcg, return that limit.
1447 */
1448 return min(limit, memsw);
1449}
1450
1451/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001452 * Visit the first child (need not be the first child as per the ordering
1453 * of the cgroup list, since we track last_scanned_child) of @mem and use
1454 * that to reclaim free pages from.
1455 */
1456static struct mem_cgroup *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001457mem_cgroup_select_victim(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001458{
1459 struct mem_cgroup *ret = NULL;
1460 struct cgroup_subsys_state *css;
1461 int nextid, found;
1462
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001463 if (!root_memcg->use_hierarchy) {
1464 css_get(&root_memcg->css);
1465 ret = root_memcg;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001466 }
1467
1468 while (!ret) {
1469 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001470 nextid = root_memcg->last_scanned_child + 1;
1471 css = css_get_next(&mem_cgroup_subsys, nextid, &root_memcg->css,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001472 &found);
1473 if (css && css_tryget(css))
1474 ret = container_of(css, struct mem_cgroup, css);
1475
1476 rcu_read_unlock();
1477 /* Updates scanning parameter */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001478 if (!css) {
1479 /* this means start scan from ID:1 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001480 root_memcg->last_scanned_child = 0;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001481 } else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001482 root_memcg->last_scanned_child = found;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001483 }
1484
1485 return ret;
1486}
1487
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001488/**
1489 * test_mem_cgroup_node_reclaimable
1490 * @mem: the target memcg
1491 * @nid: the node ID to be checked.
1492 * @noswap : specify true here if the user wants flle only information.
1493 *
1494 * This function returns whether the specified memcg contains any
1495 * reclaimable pages on a node. Returns true if there are any reclaimable
1496 * pages in the node.
1497 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001498static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001499 int nid, bool noswap)
1500{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001501 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001502 return true;
1503 if (noswap || !total_swap_pages)
1504 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001505 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001506 return true;
1507 return false;
1508
1509}
Ying Han889976d2011-05-26 16:25:33 -07001510#if MAX_NUMNODES > 1
1511
1512/*
1513 * Always updating the nodemask is not very good - even if we have an empty
1514 * list or the wrong list here, we can start from some node and traverse all
1515 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1516 *
1517 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001518static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001519{
1520 int nid;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001521 /*
1522 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1523 * pagein/pageout changes since the last update.
1524 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001525 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001526 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001527 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001528 return;
1529
Ying Han889976d2011-05-26 16:25:33 -07001530 /* make a nodemask where this memcg uses memory from */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001531 memcg->scan_nodes = node_states[N_HIGH_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001532
1533 for_each_node_mask(nid, node_states[N_HIGH_MEMORY]) {
1534
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001535 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1536 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001537 }
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001538
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001539 atomic_set(&memcg->numainfo_events, 0);
1540 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001541}
1542
1543/*
1544 * Selecting a node where we start reclaim from. Because what we need is just
1545 * reducing usage counter, start from anywhere is O,K. Considering
1546 * memory reclaim from current node, there are pros. and cons.
1547 *
1548 * Freeing memory from current node means freeing memory from a node which
1549 * we'll use or we've used. So, it may make LRU bad. And if several threads
1550 * hit limits, it will see a contention on a node. But freeing from remote
1551 * node means more costs for memory reclaim because of memory latency.
1552 *
1553 * Now, we use round-robin. Better algorithm is welcomed.
1554 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001555int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001556{
1557 int node;
1558
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001559 mem_cgroup_may_update_nodemask(memcg);
1560 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001561
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001562 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001563 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001564 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001565 /*
1566 * We call this when we hit limit, not when pages are added to LRU.
1567 * No LRU may hold pages because all pages are UNEVICTABLE or
1568 * memcg is too small and all pages are not on LRU. In that case,
1569 * we use curret node.
1570 */
1571 if (unlikely(node == MAX_NUMNODES))
1572 node = numa_node_id();
1573
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001574 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001575 return node;
1576}
1577
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001578/*
1579 * Check all nodes whether it contains reclaimable pages or not.
1580 * For quick scan, we make use of scan_nodes. This will allow us to skip
1581 * unused nodes. But scan_nodes is lazily updated and may not cotain
1582 * enough new information. We need to do double check.
1583 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001584bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001585{
1586 int nid;
1587
1588 /*
1589 * quick check...making use of scan_node.
1590 * We can skip unused nodes.
1591 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001592 if (!nodes_empty(memcg->scan_nodes)) {
1593 for (nid = first_node(memcg->scan_nodes);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001594 nid < MAX_NUMNODES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001595 nid = next_node(nid, memcg->scan_nodes)) {
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001596
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001597 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001598 return true;
1599 }
1600 }
1601 /*
1602 * Check rest of nodes.
1603 */
1604 for_each_node_state(nid, N_HIGH_MEMORY) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001605 if (node_isset(nid, memcg->scan_nodes))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001606 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001607 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001608 return true;
1609 }
1610 return false;
1611}
1612
Ying Han889976d2011-05-26 16:25:33 -07001613#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001614int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001615{
1616 return 0;
1617}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001618
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001619bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001620{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001621 return test_mem_cgroup_node_reclaimable(memcg, 0, noswap);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001622}
Ying Han889976d2011-05-26 16:25:33 -07001623#endif
1624
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001625/*
1626 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1627 * we reclaimed from, so that we don't end up penalizing one child extensively
1628 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001629 *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001630 * root_memcg is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001631 *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001632 * We give up and return to the caller when we visit root_memcg twice.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001633 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001634 *
1635 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001636 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001637static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_memcg,
Balbir Singh4e416952009-09-23 15:56:39 -07001638 struct zone *zone,
Balbir Singh75822b42009-09-23 15:56:38 -07001639 gfp_t gfp_mask,
Ying Han0ae5e892011-05-26 16:25:25 -07001640 unsigned long reclaim_options,
1641 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001642{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001643 struct mem_cgroup *victim;
1644 int ret, total = 0;
1645 int loop = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001646 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1647 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
Balbir Singh4e416952009-09-23 15:56:39 -07001648 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001649 unsigned long excess;
Johannes Weiner185efc02011-09-14 16:21:58 -07001650 unsigned long nr_scanned;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001651
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001652 excess = res_counter_soft_limit_excess(&root_memcg->res) >> PAGE_SHIFT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001653
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001654 /* If memsw_is_minimum==1, swap-out is of-no-use. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001655 if (!check_soft && !shrink && root_memcg->memsw_is_minimum)
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001656 noswap = true;
1657
Balbir Singh4e416952009-09-23 15:56:39 -07001658 while (1) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001659 victim = mem_cgroup_select_victim(root_memcg);
1660 if (victim == root_memcg) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001661 loop++;
KAMEZAWA Hiroyukifbc29a22011-06-15 15:08:46 -07001662 /*
1663 * We are not draining per cpu cached charges during
1664 * soft limit reclaim because global reclaim doesn't
1665 * care about charges. It tries to free some memory and
1666 * charges will not give any.
1667 */
1668 if (!check_soft && loop >= 1)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001669 drain_all_stock_async(root_memcg);
Balbir Singh4e416952009-09-23 15:56:39 -07001670 if (loop >= 2) {
1671 /*
1672 * If we have not been able to reclaim
1673 * anything, it might because there are
1674 * no reclaimable pages under this hierarchy
1675 */
1676 if (!check_soft || !total) {
1677 css_put(&victim->css);
1678 break;
1679 }
1680 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001681 * We want to do more targeted reclaim.
Balbir Singh4e416952009-09-23 15:56:39 -07001682 * excess >> 2 is not to excessive so as to
1683 * reclaim too much, nor too less that we keep
1684 * coming back to reclaim from this cgroup
1685 */
1686 if (total >= (excess >> 2) ||
1687 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1688 css_put(&victim->css);
1689 break;
1690 }
1691 }
1692 }
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001693 if (!mem_cgroup_reclaimable(victim, noswap)) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001694 /* this cgroup's local usage == 0 */
1695 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001696 continue;
1697 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001698 /* we use swappiness of local cgroup */
Ying Han0ae5e892011-05-26 16:25:25 -07001699 if (check_soft) {
Balbir Singh4e416952009-09-23 15:56:39 -07001700 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
Johannes Weiner185efc02011-09-14 16:21:58 -07001701 noswap, zone, &nr_scanned);
1702 *total_scanned += nr_scanned;
Ying Han0ae5e892011-05-26 16:25:25 -07001703 } else
Balbir Singh4e416952009-09-23 15:56:39 -07001704 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
Johannes Weiner185efc02011-09-14 16:21:58 -07001705 noswap);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001706 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001707 /*
1708 * At shrinking usage, we can't check we should stop here or
1709 * reclaim more. It's depends on callers. last_scanned_child
1710 * will work enough for keeping fairness under tree.
1711 */
1712 if (shrink)
1713 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001714 total += ret;
Balbir Singh4e416952009-09-23 15:56:39 -07001715 if (check_soft) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001716 if (!res_counter_soft_limit_excess(&root_memcg->res))
Balbir Singh4e416952009-09-23 15:56:39 -07001717 return total;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001718 } else if (mem_cgroup_margin(root_memcg))
Johannes Weiner4fd14eb2011-05-26 16:25:35 -07001719 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001720 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001721 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001722}
1723
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001724/*
1725 * Check OOM-Killer is already running under our hierarchy.
1726 * If someone is running, return false.
Michal Hocko1af8efe2011-07-26 16:08:24 -07001727 * Has to be called with memcg_oom_lock
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001728 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001729static bool mem_cgroup_oom_lock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001730{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001731 struct mem_cgroup *iter, *failed = NULL;
1732 bool cond = true;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001733
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001734 for_each_mem_cgroup_tree_cond(iter, memcg, cond) {
Johannes Weiner23751be2011-08-25 15:59:16 -07001735 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001736 /*
1737 * this subtree of our hierarchy is already locked
1738 * so we cannot give a lock.
1739 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07001740 failed = iter;
1741 cond = false;
Johannes Weiner23751be2011-08-25 15:59:16 -07001742 } else
1743 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001744 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001745
Michal Hocko79dfdac2011-07-26 16:08:23 -07001746 if (!failed)
Johannes Weiner23751be2011-08-25 15:59:16 -07001747 return true;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001748
1749 /*
1750 * OK, we failed to lock the whole subtree so we have to clean up
1751 * what we set up to the failing subtree
1752 */
1753 cond = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001754 for_each_mem_cgroup_tree_cond(iter, memcg, cond) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001755 if (iter == failed) {
1756 cond = false;
1757 continue;
1758 }
1759 iter->oom_lock = false;
1760 }
Johannes Weiner23751be2011-08-25 15:59:16 -07001761 return false;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001762}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001763
Michal Hocko79dfdac2011-07-26 16:08:23 -07001764/*
Michal Hocko1af8efe2011-07-26 16:08:24 -07001765 * Has to be called with memcg_oom_lock
Michal Hocko79dfdac2011-07-26 16:08:23 -07001766 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001767static int mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001768{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001769 struct mem_cgroup *iter;
1770
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001771 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001772 iter->oom_lock = false;
1773 return 0;
1774}
1775
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001776static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001777{
1778 struct mem_cgroup *iter;
1779
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001780 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001781 atomic_inc(&iter->under_oom);
1782}
1783
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001784static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001785{
1786 struct mem_cgroup *iter;
1787
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001788 /*
1789 * When a new child is created while the hierarchy is under oom,
1790 * mem_cgroup_oom_lock() may not be called. We have to use
1791 * atomic_add_unless() here.
1792 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001793 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001794 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001795}
1796
Michal Hocko1af8efe2011-07-26 16:08:24 -07001797static DEFINE_SPINLOCK(memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001798static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1799
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001800struct oom_wait_info {
1801 struct mem_cgroup *mem;
1802 wait_queue_t wait;
1803};
1804
1805static int memcg_oom_wake_function(wait_queue_t *wait,
1806 unsigned mode, int sync, void *arg)
1807{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001808 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg,
1809 *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001810 struct oom_wait_info *oom_wait_info;
1811
1812 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001813 oom_wait_memcg = oom_wait_info->mem;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001814
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001815 /*
1816 * Both of oom_wait_info->mem and wake_mem are stable under us.
1817 * Then we can use css_is_ancestor without taking care of RCU.
1818 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001819 if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg)
1820 && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001821 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001822 return autoremove_wake_function(wait, mode, sync, arg);
1823}
1824
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001825static void memcg_wakeup_oom(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001826{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001827 /* for filtering, pass "memcg" as argument. */
1828 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001829}
1830
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001831static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001832{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001833 if (memcg && atomic_read(&memcg->under_oom))
1834 memcg_wakeup_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001835}
1836
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001837/*
1838 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
1839 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001840bool mem_cgroup_handle_oom(struct mem_cgroup *memcg, gfp_t mask)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001841{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001842 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001843 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001844
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001845 owait.mem = memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001846 owait.wait.flags = 0;
1847 owait.wait.func = memcg_oom_wake_function;
1848 owait.wait.private = current;
1849 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001850 need_to_kill = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001851 mem_cgroup_mark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001852
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001853 /* At first, try to OOM lock hierarchy under memcg.*/
Michal Hocko1af8efe2011-07-26 16:08:24 -07001854 spin_lock(&memcg_oom_lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001855 locked = mem_cgroup_oom_lock(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001856 /*
1857 * Even if signal_pending(), we can't quit charge() loop without
1858 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
1859 * under OOM is always welcomed, use TASK_KILLABLE here.
1860 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001861 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001862 if (!locked || memcg->oom_kill_disable)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001863 need_to_kill = false;
1864 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001865 mem_cgroup_oom_notify(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07001866 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001867
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001868 if (need_to_kill) {
1869 finish_wait(&memcg_oom_waitq, &owait.wait);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001870 mem_cgroup_out_of_memory(memcg, mask);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001871 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001872 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001873 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001874 }
Michal Hocko1af8efe2011-07-26 16:08:24 -07001875 spin_lock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001876 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001877 mem_cgroup_oom_unlock(memcg);
1878 memcg_wakeup_oom(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07001879 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001880
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001881 mem_cgroup_unmark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001882
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001883 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
1884 return false;
1885 /* Give chance to dying process */
KAMEZAWA Hiroyuki715a5ee2011-11-02 13:38:18 -07001886 schedule_timeout_uninterruptible(1);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001887 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001888}
1889
Balbir Singhd69b0422009-06-17 16:26:34 -07001890/*
1891 * Currently used to update mapped file statistics, but the routine can be
1892 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001893 *
1894 * Notes: Race condition
1895 *
1896 * We usually use page_cgroup_lock() for accessing page_cgroup member but
1897 * it tends to be costly. But considering some conditions, we doesn't need
1898 * to do so _always_.
1899 *
1900 * Considering "charge", lock_page_cgroup() is not required because all
1901 * file-stat operations happen after a page is attached to radix-tree. There
1902 * are no race with "charge".
1903 *
1904 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
1905 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
1906 * if there are race with "uncharge". Statistics itself is properly handled
1907 * by flags.
1908 *
1909 * Considering "move", this is an only case we see a race. To make the race
1910 * small, we check MEM_CGROUP_ON_MOVE percpu value and detect there are
1911 * possibility of race condition. If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07001912 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001913
Greg Thelen2a7106f2011-01-13 15:47:37 -08001914void mem_cgroup_update_page_stat(struct page *page,
1915 enum mem_cgroup_page_stat_item idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07001916{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001917 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001918 struct page_cgroup *pc = lookup_page_cgroup(page);
1919 bool need_unlock = false;
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08001920 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07001921
Balbir Singhd69b0422009-06-17 16:26:34 -07001922 if (unlikely(!pc))
1923 return;
1924
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001925 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001926 memcg = pc->mem_cgroup;
1927 if (unlikely(!memcg || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001928 goto out;
1929 /* pc->mem_cgroup is unstable ? */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001930 if (unlikely(mem_cgroup_stealed(memcg)) || PageTransHuge(page)) {
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001931 /* take a lock against to access pc->mem_cgroup */
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08001932 move_lock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001933 need_unlock = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001934 memcg = pc->mem_cgroup;
1935 if (!memcg || !PageCgroupUsed(pc))
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001936 goto out;
1937 }
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001938
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001939 switch (idx) {
Greg Thelen2a7106f2011-01-13 15:47:37 -08001940 case MEMCG_NR_FILE_MAPPED:
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001941 if (val > 0)
1942 SetPageCgroupFileMapped(pc);
1943 else if (!page_mapped(page))
KAMEZAWA Hiroyuki0c270f82010-10-27 15:33:39 -07001944 ClearPageCgroupFileMapped(pc);
Greg Thelen2a7106f2011-01-13 15:47:37 -08001945 idx = MEM_CGROUP_STAT_FILE_MAPPED;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001946 break;
1947 default:
1948 BUG();
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07001949 }
Balbir Singhd69b0422009-06-17 16:26:34 -07001950
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001951 this_cpu_add(memcg->stat->count[idx], val);
Greg Thelen2a7106f2011-01-13 15:47:37 -08001952
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001953out:
1954 if (unlikely(need_unlock))
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08001955 move_unlock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001956 rcu_read_unlock();
1957 return;
Balbir Singhd69b0422009-06-17 16:26:34 -07001958}
Greg Thelen2a7106f2011-01-13 15:47:37 -08001959EXPORT_SYMBOL(mem_cgroup_update_page_stat);
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001960
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001961/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001962 * size of first charge trial. "32" comes from vmscan.c's magic value.
1963 * TODO: maybe necessary to use big numbers in big irons.
1964 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001965#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001966struct memcg_stock_pcp {
1967 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07001968 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001969 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07001970 unsigned long flags;
1971#define FLUSHING_CACHED_CHARGE (0)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001972};
1973static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02001974static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001975
1976/*
Johannes Weiner11c9ea42011-03-23 16:42:34 -07001977 * Try to consume stocked charge on this cpu. If success, one page is consumed
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001978 * from local stock and true is returned. If the stock is 0 or charges from a
1979 * cgroup which is not current target, returns false. This stock will be
1980 * refilled.
1981 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001982static bool consume_stock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001983{
1984 struct memcg_stock_pcp *stock;
1985 bool ret = true;
1986
1987 stock = &get_cpu_var(memcg_stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001988 if (memcg == stock->cached && stock->nr_pages)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07001989 stock->nr_pages--;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001990 else /* need to call res_counter_charge */
1991 ret = false;
1992 put_cpu_var(memcg_stock);
1993 return ret;
1994}
1995
1996/*
1997 * Returns stocks cached in percpu to res_counter and reset cached information.
1998 */
1999static void drain_stock(struct memcg_stock_pcp *stock)
2000{
2001 struct mem_cgroup *old = stock->cached;
2002
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002003 if (stock->nr_pages) {
2004 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2005
2006 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002007 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002008 res_counter_uncharge(&old->memsw, bytes);
2009 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002010 }
2011 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002012}
2013
2014/*
2015 * This must be called under preempt disabled or must be called by
2016 * a thread which is pinned to local cpu.
2017 */
2018static void drain_local_stock(struct work_struct *dummy)
2019{
2020 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
2021 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002022 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002023}
2024
2025/*
2026 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002027 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002028 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002029static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002030{
2031 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2032
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002033 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002034 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002035 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002036 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002037 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002038 put_cpu_var(memcg_stock);
2039}
2040
2041/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002042 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Michal Hockod38144b2011-07-26 16:08:28 -07002043 * of the hierarchy under it. sync flag says whether we should block
2044 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002045 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002046static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002047{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002048 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002049
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002050 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002051 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002052 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002053 for_each_online_cpu(cpu) {
2054 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002055 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002056
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002057 memcg = stock->cached;
2058 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002059 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002060 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002061 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002062 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2063 if (cpu == curcpu)
2064 drain_local_stock(&stock->work);
2065 else
2066 schedule_work_on(cpu, &stock->work);
2067 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002068 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002069 put_cpu();
Michal Hockod38144b2011-07-26 16:08:28 -07002070
2071 if (!sync)
2072 goto out;
2073
2074 for_each_online_cpu(cpu) {
2075 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002076 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002077 flush_work(&stock->work);
2078 }
2079out:
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002080 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002081}
2082
2083/*
2084 * Tries to drain stocked charges in other cpus. This function is asynchronous
2085 * and just put a work per cpu for draining localy on each cpu. Caller can
2086 * expects some charges will be back to res_counter later but cannot wait for
2087 * it.
2088 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002089static void drain_all_stock_async(struct mem_cgroup *root_memcg)
Michal Hockod38144b2011-07-26 16:08:28 -07002090{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002091 /*
2092 * If someone calls draining, avoid adding more kworker runs.
2093 */
2094 if (!mutex_trylock(&percpu_charge_mutex))
2095 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002096 drain_all_stock(root_memcg, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002097 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002098}
2099
2100/* This is a synchronous drain interface. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002101static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002102{
2103 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002104 mutex_lock(&percpu_charge_mutex);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002105 drain_all_stock(root_memcg, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002106 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002107}
2108
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002109/*
2110 * This function drains percpu counter value from DEAD cpu and
2111 * move it to local cpu. Note that this function can be preempted.
2112 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002113static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002114{
2115 int i;
2116
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002117 spin_lock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002118 for (i = 0; i < MEM_CGROUP_STAT_DATA; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002119 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002120
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002121 per_cpu(memcg->stat->count[i], cpu) = 0;
2122 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002123 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002124 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002125 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002126
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002127 per_cpu(memcg->stat->events[i], cpu) = 0;
2128 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002129 }
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002130 /* need to clear ON_MOVE value, works as a kind of lock. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002131 per_cpu(memcg->stat->count[MEM_CGROUP_ON_MOVE], cpu) = 0;
2132 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002133}
2134
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002135static void synchronize_mem_cgroup_on_move(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002136{
2137 int idx = MEM_CGROUP_ON_MOVE;
2138
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002139 spin_lock(&memcg->pcp_counter_lock);
2140 per_cpu(memcg->stat->count[idx], cpu) = memcg->nocpu_base.count[idx];
2141 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002142}
2143
2144static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002145 unsigned long action,
2146 void *hcpu)
2147{
2148 int cpu = (unsigned long)hcpu;
2149 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002150 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002151
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002152 if ((action == CPU_ONLINE)) {
2153 for_each_mem_cgroup_all(iter)
2154 synchronize_mem_cgroup_on_move(iter, cpu);
2155 return NOTIFY_OK;
2156 }
2157
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002158 if ((action != CPU_DEAD) || action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002159 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002160
2161 for_each_mem_cgroup_all(iter)
2162 mem_cgroup_drain_pcp_counter(iter, cpu);
2163
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002164 stock = &per_cpu(memcg_stock, cpu);
2165 drain_stock(stock);
2166 return NOTIFY_OK;
2167}
2168
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002169
2170/* See __mem_cgroup_try_charge() for details */
2171enum {
2172 CHARGE_OK, /* success */
2173 CHARGE_RETRY, /* need to retry but retry is not bad */
2174 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2175 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
2176 CHARGE_OOM_DIE, /* the current is killed because of OOM */
2177};
2178
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002179static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002180 unsigned int nr_pages, bool oom_check)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002181{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002182 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002183 struct mem_cgroup *mem_over_limit;
2184 struct res_counter *fail_res;
2185 unsigned long flags = 0;
2186 int ret;
2187
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002188 ret = res_counter_charge(&memcg->res, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002189
2190 if (likely(!ret)) {
2191 if (!do_swap_account)
2192 return CHARGE_OK;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002193 ret = res_counter_charge(&memcg->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002194 if (likely(!ret))
2195 return CHARGE_OK;
2196
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002197 res_counter_uncharge(&memcg->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002198 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2199 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2200 } else
2201 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002202 /*
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002203 * nr_pages can be either a huge page (HPAGE_PMD_NR), a batch
2204 * of regular pages (CHARGE_BATCH), or a single regular page (1).
Johannes Weiner9221edb2011-02-01 15:52:42 -08002205 *
2206 * Never reclaim on behalf of optional batching, retry with a
2207 * single page instead.
2208 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002209 if (nr_pages == CHARGE_BATCH)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002210 return CHARGE_RETRY;
2211
2212 if (!(gfp_mask & __GFP_WAIT))
2213 return CHARGE_WOULDBLOCK;
2214
2215 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
Ying Han0ae5e892011-05-26 16:25:25 -07002216 gfp_mask, flags, NULL);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002217 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002218 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002219 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002220 * Even though the limit is exceeded at this point, reclaim
2221 * may have been able to free some pages. Retry the charge
2222 * before killing the task.
2223 *
2224 * Only for regular pages, though: huge pages are rather
2225 * unlikely to succeed so close to the limit, and we fall back
2226 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002227 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002228 if (nr_pages == 1 && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002229 return CHARGE_RETRY;
2230
2231 /*
2232 * At task move, charge accounts can be doubly counted. So, it's
2233 * better to wait until the end of task_move if something is going on.
2234 */
2235 if (mem_cgroup_wait_acct_move(mem_over_limit))
2236 return CHARGE_RETRY;
2237
2238 /* If we don't need to call oom-killer at el, return immediately */
2239 if (!oom_check)
2240 return CHARGE_NOMEM;
2241 /* check OOM */
2242 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask))
2243 return CHARGE_OOM_DIE;
2244
2245 return CHARGE_RETRY;
2246}
2247
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002248/*
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002249 * Unlike exported interface, "oom" parameter is added. if oom==true,
2250 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002251 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002252static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002253 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002254 unsigned int nr_pages,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002255 struct mem_cgroup **ptr,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002256 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002257{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002258 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002259 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002260 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002261 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002262
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002263 /*
2264 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2265 * in system level. So, allow to go ahead dying process in addition to
2266 * MEMDIE process.
2267 */
2268 if (unlikely(test_thread_flag(TIF_MEMDIE)
2269 || fatal_signal_pending(current)))
2270 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002271
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002272 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08002273 * We always charge the cgroup the mm_struct belongs to.
2274 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002275 * thread group leader migrates. It's possible that mm is not
2276 * set, if so charge the init_mm (happens for pagecache usage).
2277 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002278 if (!*ptr && !mm)
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002279 goto bypass;
2280again:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002281 if (*ptr) { /* css should be a valid one */
2282 memcg = *ptr;
2283 VM_BUG_ON(css_is_removed(&memcg->css));
2284 if (mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002285 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002286 if (nr_pages == 1 && consume_stock(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002287 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002288 css_get(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002289 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002290 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002291
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002292 rcu_read_lock();
2293 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002294 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002295 * Because we don't have task_lock(), "p" can exit.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002296 * In that case, "memcg" can point to root or p can be NULL with
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002297 * race with swapoff. Then, we have small risk of mis-accouning.
2298 * But such kind of mis-account by race always happens because
2299 * we don't have cgroup_mutex(). It's overkill and we allo that
2300 * small race, here.
2301 * (*) swapoff at el will charge against mm-struct not against
2302 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002303 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002304 memcg = mem_cgroup_from_task(p);
2305 if (!memcg || mem_cgroup_is_root(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002306 rcu_read_unlock();
2307 goto done;
2308 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002309 if (nr_pages == 1 && consume_stock(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002310 /*
2311 * It seems dagerous to access memcg without css_get().
2312 * But considering how consume_stok works, it's not
2313 * necessary. If consume_stock success, some charges
2314 * from this memcg are cached on this cpu. So, we
2315 * don't need to call css_get()/css_tryget() before
2316 * calling consume_stock().
2317 */
2318 rcu_read_unlock();
2319 goto done;
2320 }
2321 /* after here, we may be blocked. we need to get refcnt */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002322 if (!css_tryget(&memcg->css)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002323 rcu_read_unlock();
2324 goto again;
2325 }
2326 rcu_read_unlock();
2327 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002328
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002329 do {
2330 bool oom_check;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002331
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002332 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002333 if (fatal_signal_pending(current)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002334 css_put(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002335 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002336 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002337
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002338 oom_check = false;
2339 if (oom && !nr_oom_retries) {
2340 oom_check = true;
2341 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2342 }
Balbir Singh6d61ef42009-01-07 18:08:06 -08002343
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002344 ret = mem_cgroup_do_charge(memcg, gfp_mask, batch, oom_check);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002345 switch (ret) {
2346 case CHARGE_OK:
2347 break;
2348 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002349 batch = nr_pages;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002350 css_put(&memcg->css);
2351 memcg = NULL;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002352 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002353 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002354 css_put(&memcg->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002355 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002356 case CHARGE_NOMEM: /* OOM routine works */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002357 if (!oom) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002358 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002359 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002360 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002361 /* If oom, we never return -ENOMEM */
2362 nr_oom_retries--;
2363 break;
2364 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002365 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002366 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08002367 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002368 } while (ret != CHARGE_OK);
2369
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002370 if (batch > nr_pages)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002371 refill_stock(memcg, batch - nr_pages);
2372 css_put(&memcg->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002373done:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002374 *ptr = memcg;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002375 return 0;
2376nomem:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002377 *ptr = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002378 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002379bypass:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002380 *ptr = NULL;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002381 return 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002382}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002383
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002384/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002385 * Somemtimes we have to undo a charge we got by try_charge().
2386 * This function is for that and do uncharge, put css's refcnt.
2387 * gotten by try_charge().
2388 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002389static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002390 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002391{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002392 if (!mem_cgroup_is_root(memcg)) {
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002393 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002394
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002395 res_counter_uncharge(&memcg->res, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002396 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002397 res_counter_uncharge(&memcg->memsw, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002398 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002399}
2400
2401/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002402 * A helper function to get mem_cgroup from ID. must be called under
2403 * rcu_read_lock(). The caller must check css_is_removed() or some if
2404 * it's concern. (dropping refcnt from swap can be called against removed
2405 * memcg.)
2406 */
2407static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2408{
2409 struct cgroup_subsys_state *css;
2410
2411 /* ID 0 is unused ID */
2412 if (!id)
2413 return NULL;
2414 css = css_lookup(&mem_cgroup_subsys, id);
2415 if (!css)
2416 return NULL;
2417 return container_of(css, struct mem_cgroup, css);
2418}
2419
Wu Fengguange42d9d52009-12-16 12:19:59 +01002420struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002421{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002422 struct mem_cgroup *memcg = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002423 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002424 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002425 swp_entry_t ent;
2426
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002427 VM_BUG_ON(!PageLocked(page));
2428
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002429 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002430 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002431 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002432 memcg = pc->mem_cgroup;
2433 if (memcg && !css_tryget(&memcg->css))
2434 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002435 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002436 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002437 id = lookup_swap_cgroup(ent);
2438 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002439 memcg = mem_cgroup_lookup(id);
2440 if (memcg && !css_tryget(&memcg->css))
2441 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002442 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002443 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002444 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002445 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002446}
2447
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002448static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,
Johannes Weiner5564e882011-03-23 16:42:29 -07002449 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002450 unsigned int nr_pages,
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002451 struct page_cgroup *pc,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002452 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002453{
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002454 lock_page_cgroup(pc);
2455 if (unlikely(PageCgroupUsed(pc))) {
2456 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002457 __mem_cgroup_cancel_charge(memcg, nr_pages);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002458 return;
2459 }
2460 /*
2461 * we don't need page_cgroup_lock about tail pages, becase they are not
2462 * accessed by any other context at this point.
2463 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002464 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002465 /*
2466 * We access a page_cgroup asynchronously without lock_page_cgroup().
2467 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2468 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2469 * before USED bit, we need memory barrier here.
2470 * See mem_cgroup_add_lru_list(), etc.
2471 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002472 smp_wmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -07002473 switch (ctype) {
2474 case MEM_CGROUP_CHARGE_TYPE_CACHE:
2475 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
2476 SetPageCgroupCache(pc);
2477 SetPageCgroupUsed(pc);
2478 break;
2479 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
2480 ClearPageCgroupCache(pc);
2481 SetPageCgroupUsed(pc);
2482 break;
2483 default:
2484 break;
2485 }
Hugh Dickins3be91272008-02-07 00:14:19 -08002486
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002487 mem_cgroup_charge_statistics(memcg, PageCgroupCache(pc), nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002488 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002489 /*
2490 * "charge_statistics" updated event counter. Then, check it.
2491 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2492 * if they exceeds softlimit.
2493 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002494 memcg_check_events(memcg, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002495}
2496
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002497#ifdef CONFIG_TRANSPARENT_HUGEPAGE
2498
2499#define PCGF_NOCOPY_AT_SPLIT ((1 << PCG_LOCK) | (1 << PCG_MOVE_LOCK) |\
2500 (1 << PCG_ACCT_LRU) | (1 << PCG_MIGRATION))
2501/*
2502 * Because tail pages are not marked as "used", set it. We're under
2503 * zone->lru_lock, 'splitting on pmd' and compund_lock.
2504 */
2505void mem_cgroup_split_huge_fixup(struct page *head, struct page *tail)
2506{
2507 struct page_cgroup *head_pc = lookup_page_cgroup(head);
2508 struct page_cgroup *tail_pc = lookup_page_cgroup(tail);
2509 unsigned long flags;
2510
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08002511 if (mem_cgroup_disabled())
2512 return;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002513 /*
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002514 * We have no races with charge/uncharge but will have races with
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002515 * page state accounting.
2516 */
2517 move_lock_page_cgroup(head_pc, &flags);
2518
2519 tail_pc->mem_cgroup = head_pc->mem_cgroup;
2520 smp_wmb(); /* see __commit_charge() */
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002521 if (PageCgroupAcctLRU(head_pc)) {
2522 enum lru_list lru;
2523 struct mem_cgroup_per_zone *mz;
2524
2525 /*
2526 * LRU flags cannot be copied because we need to add tail
2527 *.page to LRU by generic call and our hook will be called.
2528 * We hold lru_lock, then, reduce counter directly.
2529 */
2530 lru = page_lru(head);
Johannes Weiner97a6c372011-03-23 16:42:27 -07002531 mz = page_cgroup_zoneinfo(head_pc->mem_cgroup, head);
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002532 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
2533 }
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002534 tail_pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
2535 move_unlock_page_cgroup(head_pc, &flags);
2536}
2537#endif
2538
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002539/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07002540 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07002541 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002542 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002543 * @pc: page_cgroup of the page.
2544 * @from: mem_cgroup which the page is moved from.
2545 * @to: mem_cgroup which the page is moved to. @from != @to.
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002546 * @uncharge: whether we should call uncharge and css_put against @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002547 *
2548 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002549 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002550 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002551 *
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002552 * This function doesn't do "charge" nor css_get to new cgroup. It should be
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002553 * done by a caller(__mem_cgroup_try_charge would be useful). If @uncharge is
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002554 * true, this function does "uncharge" from old cgroup, but it doesn't if
2555 * @uncharge is false, so a caller should do "uncharge".
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002556 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002557static int mem_cgroup_move_account(struct page *page,
2558 unsigned int nr_pages,
2559 struct page_cgroup *pc,
2560 struct mem_cgroup *from,
2561 struct mem_cgroup *to,
2562 bool uncharge)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002563{
Johannes Weinerde3638d2011-03-23 16:42:28 -07002564 unsigned long flags;
2565 int ret;
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002566
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002567 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07002568 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07002569 /*
2570 * The page is isolated from LRU. So, collapse function
2571 * will not handle this page. But page splitting can happen.
2572 * Do this check under compound_page_lock(). The caller should
2573 * hold it.
2574 */
2575 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002576 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07002577 goto out;
2578
2579 lock_page_cgroup(pc);
2580
2581 ret = -EINVAL;
2582 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
2583 goto unlock;
2584
2585 move_lock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002586
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002587 if (PageCgroupFileMapped(pc)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08002588 /* Update mapped_file data for mem_cgroup */
2589 preempt_disable();
2590 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2591 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2592 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07002593 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002594 mem_cgroup_charge_statistics(from, PageCgroupCache(pc), -nr_pages);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002595 if (uncharge)
2596 /* This is not "cancel", but cancel_charge does all we need. */
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002597 __mem_cgroup_cancel_charge(from, nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07002598
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002599 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002600 pc->mem_cgroup = to;
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002601 mem_cgroup_charge_statistics(to, PageCgroupCache(pc), nr_pages);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002602 /*
2603 * We charges against "to" which may not have any tasks. Then, "to"
2604 * can be under rmdir(). But in current implementation, caller of
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08002605 * this function is just force_empty() and move charge, so it's
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002606 * guaranteed that "to" is never removed. So, we don't check rmdir
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08002607 * status here.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002608 */
Johannes Weinerde3638d2011-03-23 16:42:28 -07002609 move_unlock_page_cgroup(pc, &flags);
2610 ret = 0;
2611unlock:
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002612 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002613 /*
2614 * check events
2615 */
Johannes Weiner5564e882011-03-23 16:42:29 -07002616 memcg_check_events(to, page);
2617 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07002618out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002619 return ret;
2620}
2621
2622/*
2623 * move charges to its parent.
2624 */
2625
Johannes Weiner5564e882011-03-23 16:42:29 -07002626static int mem_cgroup_move_parent(struct page *page,
2627 struct page_cgroup *pc,
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002628 struct mem_cgroup *child,
2629 gfp_t gfp_mask)
2630{
2631 struct cgroup *cg = child->css.cgroup;
2632 struct cgroup *pcg = cg->parent;
2633 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002634 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07002635 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002636 int ret;
2637
2638 /* Is ROOT ? */
2639 if (!pcg)
2640 return -EINVAL;
2641
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002642 ret = -EBUSY;
2643 if (!get_page_unless_zero(page))
2644 goto out;
2645 if (isolate_lru_page(page))
2646 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08002647
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002648 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002649
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002650 parent = mem_cgroup_from_cont(pcg);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002651 ret = __mem_cgroup_try_charge(NULL, gfp_mask, nr_pages, &parent, false);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002652 if (ret || !parent)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002653 goto put_back;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002654
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002655 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002656 flags = compound_lock_irqsave(page);
2657
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002658 ret = mem_cgroup_move_account(page, nr_pages, pc, child, parent, true);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002659 if (ret)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002660 __mem_cgroup_cancel_charge(parent, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08002661
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002662 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002663 compound_unlock_irqrestore(page, flags);
Jesper Juhl8dba4742011-01-25 15:07:24 -08002664put_back:
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002665 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002666put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08002667 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002668out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002669 return ret;
2670}
2671
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002672/*
2673 * Charge the memory controller for page usage.
2674 * Return
2675 * 0 if the charge was successful
2676 * < 0 if the cgroup is over its limit
2677 */
2678static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002679 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002680{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002681 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002682 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08002683 struct page_cgroup *pc;
2684 bool oom = true;
2685 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08002686
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002687 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002688 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002689 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08002690 /*
2691 * Never OOM-kill a process for a huge page. The
2692 * fault handler will fall back to regular pages.
2693 */
2694 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002695 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002696
2697 pc = lookup_page_cgroup(page);
Johannes Weineraf4a6622011-03-23 16:42:24 -07002698 BUG_ON(!pc); /* XXX: remove this and move pc lookup into commit */
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002699
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002700 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom);
2701 if (ret || !memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002702 return ret;
2703
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002704 __mem_cgroup_commit_charge(memcg, page, nr_pages, pc, ctype);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002705 return 0;
2706}
2707
2708int mem_cgroup_newpage_charge(struct page *page,
2709 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002710{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002711 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002712 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002713 /*
2714 * If already mapped, we don't have to account.
2715 * If page cache, page->mapping has address_space.
2716 * But page->mapping may have out-of-use anon_vma pointer,
2717 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
2718 * is NULL.
2719 */
2720 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
2721 return 0;
2722 if (unlikely(!mm))
2723 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002724 return mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002725 MEM_CGROUP_CHARGE_TYPE_MAPPED);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002726}
2727
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002728static void
2729__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2730 enum charge_type ctype);
2731
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002732static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002733__mem_cgroup_commit_charge_lrucare(struct page *page, struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002734 enum charge_type ctype)
2735{
2736 struct page_cgroup *pc = lookup_page_cgroup(page);
2737 /*
2738 * In some case, SwapCache, FUSE(splice_buf->radixtree), the page
2739 * is already on LRU. It means the page may on some other page_cgroup's
2740 * LRU. Take care of it.
2741 */
2742 mem_cgroup_lru_del_before_commit(page);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002743 __mem_cgroup_commit_charge(memcg, page, 1, pc, ctype);
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002744 mem_cgroup_lru_add_after_commit(page);
2745 return;
2746}
2747
Balbir Singhe1a1cd52008-02-07 00:14:02 -08002748int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
2749 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08002750{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002751 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002752 int ret;
2753
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002754 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002755 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002756 if (PageCompound(page))
2757 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002758
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002759 if (unlikely(!mm))
Balbir Singh8697d332008-02-07 00:13:59 -08002760 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002761
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002762 if (page_is_file_cache(page)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002763 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, &memcg, true);
2764 if (ret || !memcg)
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002765 return ret;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002766
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002767 /*
2768 * FUSE reuses pages without going through the final
2769 * put that would remove them from the LRU list, make
2770 * sure that they get relinked properly.
2771 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002772 __mem_cgroup_commit_charge_lrucare(page, memcg,
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002773 MEM_CGROUP_CHARGE_TYPE_CACHE);
2774 return ret;
2775 }
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002776 /* shmem */
2777 if (PageSwapCache(page)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002778 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &memcg);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002779 if (!ret)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002780 __mem_cgroup_commit_charge_swapin(page, memcg,
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002781 MEM_CGROUP_CHARGE_TYPE_SHMEM);
2782 } else
2783 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002784 MEM_CGROUP_CHARGE_TYPE_SHMEM);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002785
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002786 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002787}
2788
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002789/*
2790 * While swap-in, try_charge -> commit or cancel, the page is locked.
2791 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02002792 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002793 * "commit()" or removed by "cancel()"
2794 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002795int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
2796 struct page *page,
2797 gfp_t mask, struct mem_cgroup **ptr)
2798{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002799 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002800 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002801
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07002802 *ptr = NULL;
2803
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002804 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002805 return 0;
2806
2807 if (!do_swap_account)
2808 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002809 /*
2810 * A racing thread's fault, or swapoff, may have already updated
Hugh Dickins407f9c82009-12-14 17:59:30 -08002811 * the pte, and even removed page from swap cache: in those cases
2812 * do_swap_page()'s pte_same() test will fail; but there's also a
2813 * KSM case which does need to charge the page.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002814 */
2815 if (!PageSwapCache(page))
Hugh Dickins407f9c82009-12-14 17:59:30 -08002816 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002817 memcg = try_get_mem_cgroup_from_page(page);
2818 if (!memcg)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002819 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002820 *ptr = memcg;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002821 ret = __mem_cgroup_try_charge(NULL, mask, 1, ptr, true);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002822 css_put(&memcg->css);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002823 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002824charge_cur_mm:
2825 if (unlikely(!mm))
2826 mm = &init_mm;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002827 return __mem_cgroup_try_charge(mm, mask, 1, ptr, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002828}
2829
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002830static void
2831__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2832 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002833{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002834 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002835 return;
2836 if (!ptr)
2837 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002838 cgroup_exclude_rmdir(&ptr->css);
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002839
2840 __mem_cgroup_commit_charge_lrucare(page, ptr, ctype);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002841 /*
2842 * Now swap is on-memory. This means this page may be
2843 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002844 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
2845 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
2846 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002847 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002848 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002849 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002850 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002851 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002852
2853 id = swap_cgroup_record(ent, 0);
2854 rcu_read_lock();
2855 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002856 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002857 /*
2858 * This recorded memcg can be obsolete one. So, avoid
2859 * calling css_tryget
2860 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002861 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002862 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002863 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002864 mem_cgroup_put(memcg);
2865 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002866 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002867 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002868 /*
2869 * At swapin, we may charge account against cgroup which has no tasks.
2870 * So, rmdir()->pre_destroy() can be called while we do this charge.
2871 * In that case, we need to call pre_destroy() again. check it here.
2872 */
2873 cgroup_release_and_wakeup_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002874}
2875
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002876void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
2877{
2878 __mem_cgroup_commit_charge_swapin(page, ptr,
2879 MEM_CGROUP_CHARGE_TYPE_MAPPED);
2880}
2881
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002882void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002883{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002884 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002885 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002886 if (!memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002887 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002888 __mem_cgroup_cancel_charge(memcg, 1);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002889}
2890
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002891static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002892 unsigned int nr_pages,
2893 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002894{
2895 struct memcg_batch_info *batch = NULL;
2896 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002897
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002898 /* If swapout, usage of swap doesn't decrease */
2899 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2900 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002901
2902 batch = &current->memcg_batch;
2903 /*
2904 * In usual, we do css_get() when we remember memcg pointer.
2905 * But in this case, we keep res->usage until end of a series of
2906 * uncharges. Then, it's ok to ignore memcg's refcnt.
2907 */
2908 if (!batch->memcg)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002909 batch->memcg = memcg;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002910 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002911 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002912 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002913 * the same cgroup and we have chance to coalesce uncharges.
2914 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
2915 * because we want to do uncharge as soon as possible.
2916 */
2917
2918 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
2919 goto direct_uncharge;
2920
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002921 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08002922 goto direct_uncharge;
2923
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002924 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002925 * In typical case, batch->memcg == mem. This means we can
2926 * merge a series of uncharges to an uncharge of res_counter.
2927 * If not, we uncharge res_counter ony by one.
2928 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002929 if (batch->memcg != memcg)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002930 goto direct_uncharge;
2931 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07002932 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002933 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07002934 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002935 return;
2936direct_uncharge:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002937 res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002938 if (uncharge_memsw)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002939 res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE);
2940 if (unlikely(batch->memcg != memcg))
2941 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002942 return;
2943}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002944
Balbir Singh8697d332008-02-07 00:13:59 -08002945/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002946 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002947 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002948static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002949__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002950{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002951 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002952 unsigned int nr_pages = 1;
2953 struct page_cgroup *pc;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002954
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002955 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002956 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07002957
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002958 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002959 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002960
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002961 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002962 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002963 VM_BUG_ON(!PageTransHuge(page));
2964 }
Balbir Singh8697d332008-02-07 00:13:59 -08002965 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08002966 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08002967 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002968 pc = lookup_page_cgroup(page);
2969 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002970 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002971
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002972 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002973
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002974 memcg = pc->mem_cgroup;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002975
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002976 if (!PageCgroupUsed(pc))
2977 goto unlock_out;
2978
2979 switch (ctype) {
2980 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002981 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002982 /* See mem_cgroup_prepare_migration() */
2983 if (page_mapped(page) || PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002984 goto unlock_out;
2985 break;
2986 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
2987 if (!PageAnon(page)) { /* Shared memory */
2988 if (page->mapping && !page_is_file_cache(page))
2989 goto unlock_out;
2990 } else if (page_mapped(page)) /* Anon */
2991 goto unlock_out;
2992 break;
2993 default:
2994 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002995 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002996
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002997 mem_cgroup_charge_statistics(memcg, PageCgroupCache(pc), -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002998
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002999 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08003000 /*
3001 * pc->mem_cgroup is not cleared here. It will be accessed when it's
3002 * freed from LRU. This is safe because uncharged page is expected not
3003 * to be reused (freed soon). Exception is SwapCache, it's handled by
3004 * special functions.
3005 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003006
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003007 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003008 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003009 * even after unlock, we have memcg->res.usage here and this memcg
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003010 * will never be freed.
3011 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003012 memcg_check_events(memcg, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003013 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003014 mem_cgroup_swap_statistics(memcg, true);
3015 mem_cgroup_get(memcg);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003016 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003017 if (!mem_cgroup_is_root(memcg))
3018 mem_cgroup_do_uncharge(memcg, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003019
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003020 return memcg;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003021
3022unlock_out:
3023 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003024 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08003025}
3026
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003027void mem_cgroup_uncharge_page(struct page *page)
3028{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003029 /* early check. */
3030 if (page_mapped(page))
3031 return;
3032 if (page->mapping && !PageAnon(page))
3033 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003034 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
3035}
3036
3037void mem_cgroup_uncharge_cache_page(struct page *page)
3038{
3039 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07003040 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003041 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
3042}
3043
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003044/*
3045 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
3046 * In that cases, pages are freed continuously and we can expect pages
3047 * are in the same memcg. All these calls itself limits the number of
3048 * pages freed at once, then uncharge_start/end() is called properly.
3049 * This may be called prural(2) times in a context,
3050 */
3051
3052void mem_cgroup_uncharge_start(void)
3053{
3054 current->memcg_batch.do_batch++;
3055 /* We can do nest. */
3056 if (current->memcg_batch.do_batch == 1) {
3057 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003058 current->memcg_batch.nr_pages = 0;
3059 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003060 }
3061}
3062
3063void mem_cgroup_uncharge_end(void)
3064{
3065 struct memcg_batch_info *batch = &current->memcg_batch;
3066
3067 if (!batch->do_batch)
3068 return;
3069
3070 batch->do_batch--;
3071 if (batch->do_batch) /* If stacked, do nothing. */
3072 return;
3073
3074 if (!batch->memcg)
3075 return;
3076 /*
3077 * This "batch->memcg" is valid without any css_get/put etc...
3078 * bacause we hide charges behind us.
3079 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003080 if (batch->nr_pages)
3081 res_counter_uncharge(&batch->memcg->res,
3082 batch->nr_pages * PAGE_SIZE);
3083 if (batch->memsw_nr_pages)
3084 res_counter_uncharge(&batch->memcg->memsw,
3085 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003086 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003087 /* forget this pointer (for sanity check) */
3088 batch->memcg = NULL;
3089}
3090
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003091#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003092/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003093 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003094 * memcg information is recorded to swap_cgroup of "ent"
3095 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003096void
3097mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003098{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003099 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003100 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003101
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003102 if (!swapout) /* this was a swap cache but the swap is unused ! */
3103 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
3104
3105 memcg = __mem_cgroup_uncharge_common(page, ctype);
3106
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003107 /*
3108 * record memcg information, if swapout && memcg != NULL,
3109 * mem_cgroup_get() was called in uncharge().
3110 */
3111 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003112 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003113}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003114#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003115
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003116#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3117/*
3118 * called from swap_entry_free(). remove record in swap_cgroup and
3119 * uncharge "memsw" account.
3120 */
3121void mem_cgroup_uncharge_swap(swp_entry_t ent)
3122{
3123 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003124 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003125
3126 if (!do_swap_account)
3127 return;
3128
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003129 id = swap_cgroup_record(ent, 0);
3130 rcu_read_lock();
3131 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003132 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003133 /*
3134 * We uncharge this because swap is freed.
3135 * This memcg can be obsolete one. We avoid calling css_tryget
3136 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003137 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07003138 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003139 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003140 mem_cgroup_put(memcg);
3141 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003142 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003143}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003144
3145/**
3146 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
3147 * @entry: swap entry to be moved
3148 * @from: mem_cgroup which the entry is moved from
3149 * @to: mem_cgroup which the entry is moved to
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003150 * @need_fixup: whether we should fixup res_counters and refcounts.
Daisuke Nishimura02491442010-03-10 15:22:17 -08003151 *
3152 * It succeeds only when the swap_cgroup's record for this entry is the same
3153 * as the mem_cgroup's id of @from.
3154 *
3155 * Returns 0 on success, -EINVAL on failure.
3156 *
3157 * The caller must have charged to @to, IOW, called res_counter_charge() about
3158 * both res and memsw, and called css_get().
3159 */
3160static int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003161 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003162{
3163 unsigned short old_id, new_id;
3164
3165 old_id = css_id(&from->css);
3166 new_id = css_id(&to->css);
3167
3168 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08003169 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003170 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003171 /*
3172 * This function is only called from task migration context now.
3173 * It postpones res_counter and refcount handling till the end
3174 * of task migration(mem_cgroup_clear_mc()) for performance
3175 * improvement. But we cannot postpone mem_cgroup_get(to)
3176 * because if the process that has been moved to @to does
3177 * swap-in, the refcount of @to might be decreased to 0.
3178 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08003179 mem_cgroup_get(to);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003180 if (need_fixup) {
3181 if (!mem_cgroup_is_root(from))
3182 res_counter_uncharge(&from->memsw, PAGE_SIZE);
3183 mem_cgroup_put(from);
3184 /*
3185 * we charged both to->res and to->memsw, so we should
3186 * uncharge to->res.
3187 */
3188 if (!mem_cgroup_is_root(to))
3189 res_counter_uncharge(&to->res, PAGE_SIZE);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003190 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08003191 return 0;
3192 }
3193 return -EINVAL;
3194}
3195#else
3196static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003197 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003198{
3199 return -EINVAL;
3200}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003201#endif
3202
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003203/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003204 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
3205 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003206 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003207int mem_cgroup_prepare_migration(struct page *page,
Miklos Szeredief6a3c62011-03-22 16:30:52 -07003208 struct page *newpage, struct mem_cgroup **ptr, gfp_t gfp_mask)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003209{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003210 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003211 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003212 enum charge_type ctype;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003213 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003214
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07003215 *ptr = NULL;
3216
Andrea Arcangeliec168512011-01-13 15:46:56 -08003217 VM_BUG_ON(PageTransHuge(page));
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003218 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07003219 return 0;
3220
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003221 pc = lookup_page_cgroup(page);
3222 lock_page_cgroup(pc);
3223 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003224 memcg = pc->mem_cgroup;
3225 css_get(&memcg->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003226 /*
3227 * At migrating an anonymous page, its mapcount goes down
3228 * to 0 and uncharge() will be called. But, even if it's fully
3229 * unmapped, migration may fail and this page has to be
3230 * charged again. We set MIGRATION flag here and delay uncharge
3231 * until end_migration() is called
3232 *
3233 * Corner Case Thinking
3234 * A)
3235 * When the old page was mapped as Anon and it's unmap-and-freed
3236 * while migration was ongoing.
3237 * If unmap finds the old page, uncharge() of it will be delayed
3238 * until end_migration(). If unmap finds a new page, it's
3239 * uncharged when it make mapcount to be 1->0. If unmap code
3240 * finds swap_migration_entry, the new page will not be mapped
3241 * and end_migration() will find it(mapcount==0).
3242 *
3243 * B)
3244 * When the old page was mapped but migraion fails, the kernel
3245 * remaps it. A charge for it is kept by MIGRATION flag even
3246 * if mapcount goes down to 0. We can do remap successfully
3247 * without charging it again.
3248 *
3249 * C)
3250 * The "old" page is under lock_page() until the end of
3251 * migration, so, the old page itself will not be swapped-out.
3252 * If the new page is swapped out before end_migraton, our
3253 * hook to usual swap-out path will catch the event.
3254 */
3255 if (PageAnon(page))
3256 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003257 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003258 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003259 /*
3260 * If the page is not charged at this point,
3261 * we return here.
3262 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003263 if (!memcg)
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003264 return 0;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003265
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003266 *ptr = memcg;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003267 ret = __mem_cgroup_try_charge(NULL, gfp_mask, 1, ptr, false);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003268 css_put(&memcg->css);/* drop extra refcnt */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003269 if (ret || *ptr == NULL) {
3270 if (PageAnon(page)) {
3271 lock_page_cgroup(pc);
3272 ClearPageCgroupMigration(pc);
3273 unlock_page_cgroup(pc);
3274 /*
3275 * The old page may be fully unmapped while we kept it.
3276 */
3277 mem_cgroup_uncharge_page(page);
3278 }
3279 return -ENOMEM;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003280 }
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003281 /*
3282 * We charge new page before it's used/mapped. So, even if unlock_page()
3283 * is called before end_migration, we can catch all events on this new
3284 * page. In the case new page is migrated but not remapped, new page's
3285 * mapcount will be finally 0 and we call uncharge in end_migration().
3286 */
3287 pc = lookup_page_cgroup(newpage);
3288 if (PageAnon(page))
3289 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
3290 else if (page_is_file_cache(page))
3291 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
3292 else
3293 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003294 __mem_cgroup_commit_charge(memcg, page, 1, pc, ctype);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003295 return ret;
3296}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08003297
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003298/* remove redundant charge if migration failed*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003299void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003300 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003301{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003302 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003303 struct page_cgroup *pc;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003304
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003305 if (!memcg)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003306 return;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003307 /* blocks rmdir() */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003308 cgroup_exclude_rmdir(&memcg->css);
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003309 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003310 used = oldpage;
3311 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003312 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003313 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003314 unused = oldpage;
3315 }
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003316 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003317 * We disallowed uncharge of pages under migration because mapcount
3318 * of the page goes down to zero, temporarly.
3319 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003320 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003321 pc = lookup_page_cgroup(oldpage);
3322 lock_page_cgroup(pc);
3323 ClearPageCgroupMigration(pc);
3324 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003325
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003326 __mem_cgroup_uncharge_common(unused, MEM_CGROUP_CHARGE_TYPE_FORCE);
3327
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003328 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003329 * If a page is a file cache, radix-tree replacement is very atomic
3330 * and we can skip this check. When it was an Anon page, its mapcount
3331 * goes down to 0. But because we added MIGRATION flage, it's not
3332 * uncharged yet. There are several case but page->mapcount check
3333 * and USED bit check in mem_cgroup_uncharge_page() will do enough
3334 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003335 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003336 if (PageAnon(used))
3337 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003338 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003339 * At migration, we may charge account against cgroup which has no
3340 * tasks.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003341 * So, rmdir()->pre_destroy() can be called while we do this charge.
3342 * In that case, we need to call pre_destroy() again. check it here.
3343 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003344 cgroup_release_and_wakeup_rmdir(&memcg->css);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003345}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08003346
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003347#ifdef CONFIG_DEBUG_VM
3348static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
3349{
3350 struct page_cgroup *pc;
3351
3352 pc = lookup_page_cgroup(page);
3353 if (likely(pc) && PageCgroupUsed(pc))
3354 return pc;
3355 return NULL;
3356}
3357
3358bool mem_cgroup_bad_page_check(struct page *page)
3359{
3360 if (mem_cgroup_disabled())
3361 return false;
3362
3363 return lookup_page_cgroup_used(page) != NULL;
3364}
3365
3366void mem_cgroup_print_bad_page(struct page *page)
3367{
3368 struct page_cgroup *pc;
3369
3370 pc = lookup_page_cgroup_used(page);
3371 if (pc) {
3372 int ret = -1;
3373 char *path;
3374
3375 printk(KERN_ALERT "pc:%p pc->flags:%lx pc->mem_cgroup:%p",
3376 pc, pc->flags, pc->mem_cgroup);
3377
3378 path = kmalloc(PATH_MAX, GFP_KERNEL);
3379 if (path) {
3380 rcu_read_lock();
3381 ret = cgroup_path(pc->mem_cgroup->css.cgroup,
3382 path, PATH_MAX);
3383 rcu_read_unlock();
3384 }
3385
3386 printk(KERN_CONT "(%s)\n",
3387 (ret < 0) ? "cannot get the path" : path);
3388 kfree(path);
3389 }
3390}
3391#endif
3392
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003393static DEFINE_MUTEX(set_limit_mutex);
3394
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08003395static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003396 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003397{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003398 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003399 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003400 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003401 int children = mem_cgroup_count_children(memcg);
3402 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003403 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003404
3405 /*
3406 * For keeping hierarchical_reclaim simple, how long we should retry
3407 * is depends on callers. We set our retry-count to be function
3408 * of # of children which we should visit in this loop.
3409 */
3410 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
3411
3412 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003413
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003414 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003415 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003416 if (signal_pending(current)) {
3417 ret = -EINTR;
3418 break;
3419 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003420 /*
3421 * Rather than hide all in some function, I do this in
3422 * open coded manner. You see what this really does.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003423 * We have to guarantee memcg->res.limit < memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003424 */
3425 mutex_lock(&set_limit_mutex);
3426 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3427 if (memswlimit < val) {
3428 ret = -EINVAL;
3429 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003430 break;
3431 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003432
3433 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3434 if (memlimit < val)
3435 enlarge = 1;
3436
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003437 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003438 if (!ret) {
3439 if (memswlimit == val)
3440 memcg->memsw_is_minimum = true;
3441 else
3442 memcg->memsw_is_minimum = false;
3443 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003444 mutex_unlock(&set_limit_mutex);
3445
3446 if (!ret)
3447 break;
3448
Bob Liuaa20d482009-12-15 16:47:14 -08003449 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Ying Han0ae5e892011-05-26 16:25:25 -07003450 MEM_CGROUP_RECLAIM_SHRINK,
3451 NULL);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003452 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
3453 /* Usage is reduced ? */
3454 if (curusage >= oldusage)
3455 retry_count--;
3456 else
3457 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003458 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003459 if (!ret && enlarge)
3460 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08003461
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003462 return ret;
3463}
3464
Li Zefan338c8432009-06-17 16:27:15 -07003465static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
3466 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003467{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003468 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003469 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003470 int children = mem_cgroup_count_children(memcg);
3471 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003472 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003473
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003474 /* see mem_cgroup_resize_res_limit */
3475 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
3476 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003477 while (retry_count) {
3478 if (signal_pending(current)) {
3479 ret = -EINTR;
3480 break;
3481 }
3482 /*
3483 * Rather than hide all in some function, I do this in
3484 * open coded manner. You see what this really does.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003485 * We have to guarantee memcg->res.limit < memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003486 */
3487 mutex_lock(&set_limit_mutex);
3488 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3489 if (memlimit > val) {
3490 ret = -EINVAL;
3491 mutex_unlock(&set_limit_mutex);
3492 break;
3493 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003494 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3495 if (memswlimit < val)
3496 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003497 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003498 if (!ret) {
3499 if (memlimit == val)
3500 memcg->memsw_is_minimum = true;
3501 else
3502 memcg->memsw_is_minimum = false;
3503 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003504 mutex_unlock(&set_limit_mutex);
3505
3506 if (!ret)
3507 break;
3508
Balbir Singh4e416952009-09-23 15:56:39 -07003509 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh75822b42009-09-23 15:56:38 -07003510 MEM_CGROUP_RECLAIM_NOSWAP |
Ying Han0ae5e892011-05-26 16:25:25 -07003511 MEM_CGROUP_RECLAIM_SHRINK,
3512 NULL);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003513 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003514 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003515 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003516 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003517 else
3518 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003519 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003520 if (!ret && enlarge)
3521 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003522 return ret;
3523}
3524
Balbir Singh4e416952009-09-23 15:56:39 -07003525unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
Ying Han0ae5e892011-05-26 16:25:25 -07003526 gfp_t gfp_mask,
3527 unsigned long *total_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07003528{
3529 unsigned long nr_reclaimed = 0;
3530 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
3531 unsigned long reclaimed;
3532 int loop = 0;
3533 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003534 unsigned long long excess;
Ying Han0ae5e892011-05-26 16:25:25 -07003535 unsigned long nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003536
3537 if (order > 0)
3538 return 0;
3539
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07003540 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
Balbir Singh4e416952009-09-23 15:56:39 -07003541 /*
3542 * This loop can run a while, specially if mem_cgroup's continuously
3543 * keep exceeding their soft limit and putting the system under
3544 * pressure
3545 */
3546 do {
3547 if (next_mz)
3548 mz = next_mz;
3549 else
3550 mz = mem_cgroup_largest_soft_limit_node(mctz);
3551 if (!mz)
3552 break;
3553
Ying Han0ae5e892011-05-26 16:25:25 -07003554 nr_scanned = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07003555 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
3556 gfp_mask,
Ying Han0ae5e892011-05-26 16:25:25 -07003557 MEM_CGROUP_RECLAIM_SOFT,
3558 &nr_scanned);
Balbir Singh4e416952009-09-23 15:56:39 -07003559 nr_reclaimed += reclaimed;
Ying Han0ae5e892011-05-26 16:25:25 -07003560 *total_scanned += nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003561 spin_lock(&mctz->lock);
3562
3563 /*
3564 * If we failed to reclaim anything from this memory cgroup
3565 * it is time to move on to the next cgroup
3566 */
3567 next_mz = NULL;
3568 if (!reclaimed) {
3569 do {
3570 /*
3571 * Loop until we find yet another one.
3572 *
3573 * By the time we get the soft_limit lock
3574 * again, someone might have aded the
3575 * group back on the RB tree. Iterate to
3576 * make sure we get a different mem.
3577 * mem_cgroup_largest_soft_limit_node returns
3578 * NULL if no other cgroup is present on
3579 * the tree
3580 */
3581 next_mz =
3582 __mem_cgroup_largest_soft_limit_node(mctz);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003583 if (next_mz == mz)
Balbir Singh4e416952009-09-23 15:56:39 -07003584 css_put(&next_mz->mem->css);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003585 else /* next_mz == NULL or other memcg */
Balbir Singh4e416952009-09-23 15:56:39 -07003586 break;
3587 } while (1);
3588 }
Balbir Singh4e416952009-09-23 15:56:39 -07003589 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003590 excess = res_counter_soft_limit_excess(&mz->mem->res);
Balbir Singh4e416952009-09-23 15:56:39 -07003591 /*
3592 * One school of thought says that we should not add
3593 * back the node to the tree if reclaim returns 0.
3594 * But our reclaim could return 0, simply because due
3595 * to priority we are exposing a smaller subset of
3596 * memory to reclaim from. Consider this as a longer
3597 * term TODO.
3598 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003599 /* If excess == 0, no tree ops */
3600 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07003601 spin_unlock(&mctz->lock);
3602 css_put(&mz->mem->css);
3603 loop++;
3604 /*
3605 * Could not reclaim anything and there are no more
3606 * mem cgroups to try or we seem to be looping without
3607 * reclaiming anything.
3608 */
3609 if (!nr_reclaimed &&
3610 (next_mz == NULL ||
3611 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3612 break;
3613 } while (!nr_reclaimed);
3614 if (next_mz)
3615 css_put(&next_mz->mem->css);
3616 return nr_reclaimed;
3617}
3618
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003619/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003620 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003621 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
3622 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003623static int mem_cgroup_force_empty_list(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003624 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003625{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003626 struct zone *zone;
3627 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003628 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003629 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003630 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003631 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003632
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003633 zone = &NODE_DATA(node)->node_zones[zid];
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003634 mz = mem_cgroup_zoneinfo(memcg, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07003635 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003636
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003637 loop = MEM_CGROUP_ZSTAT(mz, lru);
3638 /* give some margin against EBUSY etc...*/
3639 loop += 256;
3640 busy = NULL;
3641 while (loop--) {
Johannes Weiner5564e882011-03-23 16:42:29 -07003642 struct page *page;
3643
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003644 ret = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003645 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003646 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003647 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003648 break;
3649 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003650 pc = list_entry(list->prev, struct page_cgroup, lru);
3651 if (busy == pc) {
3652 list_move(&pc->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08003653 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003654 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003655 continue;
3656 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003657 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003658
Johannes Weiner6b3ae582011-03-23 16:42:30 -07003659 page = lookup_cgroup_page(pc);
Johannes Weiner5564e882011-03-23 16:42:29 -07003660
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003661 ret = mem_cgroup_move_parent(page, pc, memcg, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003662 if (ret == -ENOMEM)
3663 break;
3664
3665 if (ret == -EBUSY || ret == -EINVAL) {
3666 /* found lock contention or "pc" is obsolete. */
3667 busy = pc;
3668 cond_resched();
3669 } else
3670 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003671 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003672
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003673 if (!ret && !list_empty(list))
3674 return -EBUSY;
3675 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003676}
3677
3678/*
3679 * make mem_cgroup's charge to be 0 if there is no task.
3680 * This enables deleting this mem_cgroup.
3681 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003682static int mem_cgroup_force_empty(struct mem_cgroup *memcg, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003683{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003684 int ret;
3685 int node, zid, shrink;
3686 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003687 struct cgroup *cgrp = memcg->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003688
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003689 css_get(&memcg->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003690
3691 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003692 /* should free all ? */
3693 if (free_all)
3694 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003695move_account:
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003696 do {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003697 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003698 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003699 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003700 ret = -EINTR;
3701 if (signal_pending(current))
3702 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003703 /* This is for making all *used* pages to be on LRU. */
3704 lru_add_drain_all();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003705 drain_all_stock_sync(memcg);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003706 ret = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003707 mem_cgroup_start_move(memcg);
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08003708 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003709 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07003710 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003711 for_each_lru(l) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003712 ret = mem_cgroup_force_empty_list(memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003713 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003714 if (ret)
3715 break;
3716 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003717 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003718 if (ret)
3719 break;
3720 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003721 mem_cgroup_end_move(memcg);
3722 memcg_oom_recover(memcg);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003723 /* it seems parent cgroup doesn't have enough mem */
3724 if (ret == -ENOMEM)
3725 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003726 cond_resched();
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003727 /* "ret" should also be checked to ensure all lists are empty. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003728 } while (memcg->res.usage > 0 || ret);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003729out:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003730 css_put(&memcg->css);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003731 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003732
3733try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003734 /* returns EBUSY if there is a task or if we come here twice. */
3735 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003736 ret = -EBUSY;
3737 goto out;
3738 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003739 /* we call try-to-free pages for make this cgroup empty */
3740 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003741 /* try to free all pages in this cgroup */
3742 shrink = 1;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003743 while (nr_retries && memcg->res.usage > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003744 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003745
3746 if (signal_pending(current)) {
3747 ret = -EINTR;
3748 goto out;
3749 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003750 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
Johannes Weiner185efc02011-09-14 16:21:58 -07003751 false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003752 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003753 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003754 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02003755 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003756 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003757
3758 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003759 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003760 /* try move_account...there may be some *locked* pages. */
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003761 goto move_account;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003762}
3763
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003764int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
3765{
3766 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
3767}
3768
3769
Balbir Singh18f59ea2009-01-07 18:08:07 -08003770static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
3771{
3772 return mem_cgroup_from_cont(cont)->use_hierarchy;
3773}
3774
3775static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
3776 u64 val)
3777{
3778 int retval = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003779 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003780 struct cgroup *parent = cont->parent;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003781 struct mem_cgroup *parent_memcg = NULL;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003782
3783 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003784 parent_memcg = mem_cgroup_from_cont(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003785
3786 cgroup_lock();
3787 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003788 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08003789 * in the child subtrees. If it is unset, then the change can
3790 * occur, provided the current cgroup has no children.
3791 *
3792 * For the root cgroup, parent_mem is NULL, we allow value to be
3793 * set if there are no children.
3794 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003795 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08003796 (val == 1 || val == 0)) {
3797 if (list_empty(&cont->children))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003798 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003799 else
3800 retval = -EBUSY;
3801 } else
3802 retval = -EINVAL;
3803 cgroup_unlock();
3804
3805 return retval;
3806}
3807
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003808
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003809static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003810 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003811{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003812 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003813 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003814
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003815 /* Per-cpu values can be negative, use a signed accumulator */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003816 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003817 val += mem_cgroup_read_stat(iter, idx);
3818
3819 if (val < 0) /* race ? */
3820 val = 0;
3821 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003822}
3823
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003824static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003825{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003826 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003827
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003828 if (!mem_cgroup_is_root(memcg)) {
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003829 if (!swap)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003830 return res_counter_read_u64(&memcg->res, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003831 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003832 return res_counter_read_u64(&memcg->memsw, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003833 }
3834
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003835 val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
3836 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003837
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003838 if (swap)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003839 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAPOUT);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003840
3841 return val << PAGE_SHIFT;
3842}
3843
Paul Menage2c3daa72008-04-29 00:59:58 -07003844static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003845{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003846 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003847 u64 val;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003848 int type, name;
3849
3850 type = MEMFILE_TYPE(cft->private);
3851 name = MEMFILE_ATTR(cft->private);
3852 switch (type) {
3853 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003854 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003855 val = mem_cgroup_usage(memcg, false);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003856 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003857 val = res_counter_read_u64(&memcg->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003858 break;
3859 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003860 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003861 val = mem_cgroup_usage(memcg, true);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003862 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003863 val = res_counter_read_u64(&memcg->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003864 break;
3865 default:
3866 BUG();
3867 break;
3868 }
3869 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003870}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003871/*
3872 * The user of this function is...
3873 * RES_LIMIT.
3874 */
Paul Menage856c13a2008-07-25 01:47:04 -07003875static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
3876 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003877{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003878 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003879 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003880 unsigned long long val;
3881 int ret;
3882
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003883 type = MEMFILE_TYPE(cft->private);
3884 name = MEMFILE_ATTR(cft->private);
3885 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003886 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07003887 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
3888 ret = -EINVAL;
3889 break;
3890 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003891 /* This function does all necessary parse...reuse it */
3892 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003893 if (ret)
3894 break;
3895 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003896 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003897 else
3898 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003899 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07003900 case RES_SOFT_LIMIT:
3901 ret = res_counter_memparse_write_strategy(buffer, &val);
3902 if (ret)
3903 break;
3904 /*
3905 * For memsw, soft limits are hard to implement in terms
3906 * of semantics, for now, we support soft limits for
3907 * control without swap
3908 */
3909 if (type == _MEM)
3910 ret = res_counter_set_soft_limit(&memcg->res, val);
3911 else
3912 ret = -EINVAL;
3913 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003914 default:
3915 ret = -EINVAL; /* should be BUG() ? */
3916 break;
3917 }
3918 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003919}
3920
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08003921static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
3922 unsigned long long *mem_limit, unsigned long long *memsw_limit)
3923{
3924 struct cgroup *cgroup;
3925 unsigned long long min_limit, min_memsw_limit, tmp;
3926
3927 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3928 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3929 cgroup = memcg->css.cgroup;
3930 if (!memcg->use_hierarchy)
3931 goto out;
3932
3933 while (cgroup->parent) {
3934 cgroup = cgroup->parent;
3935 memcg = mem_cgroup_from_cont(cgroup);
3936 if (!memcg->use_hierarchy)
3937 break;
3938 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
3939 min_limit = min(min_limit, tmp);
3940 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3941 min_memsw_limit = min(min_memsw_limit, tmp);
3942 }
3943out:
3944 *mem_limit = min_limit;
3945 *memsw_limit = min_memsw_limit;
3946 return;
3947}
3948
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003949static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003950{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003951 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003952 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003953
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003954 memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003955 type = MEMFILE_TYPE(event);
3956 name = MEMFILE_ATTR(event);
3957 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003958 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003959 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003960 res_counter_reset_max(&memcg->res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003961 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003962 res_counter_reset_max(&memcg->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003963 break;
3964 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003965 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003966 res_counter_reset_failcnt(&memcg->res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003967 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003968 res_counter_reset_failcnt(&memcg->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003969 break;
3970 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07003971
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07003972 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003973}
3974
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003975static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
3976 struct cftype *cft)
3977{
3978 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
3979}
3980
Daisuke Nishimura02491442010-03-10 15:22:17 -08003981#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003982static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
3983 struct cftype *cft, u64 val)
3984{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003985 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003986
3987 if (val >= (1 << NR_MOVE_TYPE))
3988 return -EINVAL;
3989 /*
3990 * We check this value several times in both in can_attach() and
3991 * attach(), so we need cgroup lock to prevent this value from being
3992 * inconsistent.
3993 */
3994 cgroup_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003995 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003996 cgroup_unlock();
3997
3998 return 0;
3999}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004000#else
4001static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
4002 struct cftype *cft, u64 val)
4003{
4004 return -ENOSYS;
4005}
4006#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004007
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004008
4009/* For read statistics */
4010enum {
4011 MCS_CACHE,
4012 MCS_RSS,
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08004013 MCS_FILE_MAPPED,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004014 MCS_PGPGIN,
4015 MCS_PGPGOUT,
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004016 MCS_SWAP,
Ying Han456f9982011-05-26 16:25:38 -07004017 MCS_PGFAULT,
4018 MCS_PGMAJFAULT,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004019 MCS_INACTIVE_ANON,
4020 MCS_ACTIVE_ANON,
4021 MCS_INACTIVE_FILE,
4022 MCS_ACTIVE_FILE,
4023 MCS_UNEVICTABLE,
4024 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004025};
4026
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004027struct mcs_total_stat {
4028 s64 stat[NR_MCS_STAT];
4029};
4030
4031struct {
4032 char *local_name;
4033 char *total_name;
4034} memcg_stat_strings[NR_MCS_STAT] = {
4035 {"cache", "total_cache"},
4036 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07004037 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004038 {"pgpgin", "total_pgpgin"},
4039 {"pgpgout", "total_pgpgout"},
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004040 {"swap", "total_swap"},
Ying Han456f9982011-05-26 16:25:38 -07004041 {"pgfault", "total_pgfault"},
4042 {"pgmajfault", "total_pgmajfault"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004043 {"inactive_anon", "total_inactive_anon"},
4044 {"active_anon", "total_active_anon"},
4045 {"inactive_file", "total_inactive_file"},
4046 {"active_file", "total_active_file"},
4047 {"unevictable", "total_unevictable"}
4048};
4049
4050
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004051static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004052mem_cgroup_get_local_stat(struct mem_cgroup *memcg, struct mcs_total_stat *s)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004053{
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004054 s64 val;
4055
4056 /* per cpu stat */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004057 val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_CACHE);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004058 s->stat[MCS_CACHE] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004059 val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_RSS);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004060 s->stat[MCS_RSS] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004061 val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_FILE_MAPPED);
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08004062 s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004063 val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGPGIN);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004064 s->stat[MCS_PGPGIN] += val;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004065 val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGPGOUT);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004066 s->stat[MCS_PGPGOUT] += val;
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004067 if (do_swap_account) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004068 val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_SWAPOUT);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004069 s->stat[MCS_SWAP] += val * PAGE_SIZE;
4070 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004071 val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGFAULT);
Ying Han456f9982011-05-26 16:25:38 -07004072 s->stat[MCS_PGFAULT] += val;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004073 val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGMAJFAULT);
Ying Han456f9982011-05-26 16:25:38 -07004074 s->stat[MCS_PGMAJFAULT] += val;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004075
4076 /* per zone stat */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004077 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_ANON));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004078 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004079 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_ANON));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004080 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004081 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_FILE));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004082 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004083 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_FILE));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004084 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004085 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004086 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004087}
4088
4089static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004090mem_cgroup_get_total_stat(struct mem_cgroup *memcg, struct mcs_total_stat *s)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004091{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004092 struct mem_cgroup *iter;
4093
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004094 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004095 mem_cgroup_get_local_stat(iter, s);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004096}
4097
Ying Han406eb0c2011-05-26 16:25:37 -07004098#ifdef CONFIG_NUMA
4099static int mem_control_numa_stat_show(struct seq_file *m, void *arg)
4100{
4101 int nid;
4102 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
4103 unsigned long node_nr;
4104 struct cgroup *cont = m->private;
4105 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
4106
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004107 total_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07004108 seq_printf(m, "total=%lu", total_nr);
4109 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004110 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07004111 seq_printf(m, " N%d=%lu", nid, node_nr);
4112 }
4113 seq_putc(m, '\n');
4114
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004115 file_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07004116 seq_printf(m, "file=%lu", file_nr);
4117 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004118 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
4119 LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07004120 seq_printf(m, " N%d=%lu", nid, node_nr);
4121 }
4122 seq_putc(m, '\n');
4123
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004124 anon_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07004125 seq_printf(m, "anon=%lu", anon_nr);
4126 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004127 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
4128 LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07004129 seq_printf(m, " N%d=%lu", nid, node_nr);
4130 }
4131 seq_putc(m, '\n');
4132
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004133 unevictable_nr = mem_cgroup_nr_lru_pages(mem_cont, BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07004134 seq_printf(m, "unevictable=%lu", unevictable_nr);
4135 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004136 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
4137 BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07004138 seq_printf(m, " N%d=%lu", nid, node_nr);
4139 }
4140 seq_putc(m, '\n');
4141 return 0;
4142}
4143#endif /* CONFIG_NUMA */
4144
Paul Menagec64745c2008-04-29 01:00:02 -07004145static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
4146 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004147{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004148 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004149 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004150 int i;
4151
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004152 memset(&mystat, 0, sizeof(mystat));
4153 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004154
Ying Han406eb0c2011-05-26 16:25:37 -07004155
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004156 for (i = 0; i < NR_MCS_STAT; i++) {
4157 if (i == MCS_SWAP && !do_swap_account)
4158 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004159 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004160 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004161
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004162 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004163 {
4164 unsigned long long limit, memsw_limit;
4165 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
4166 cb->fill(cb, "hierarchical_memory_limit", limit);
4167 if (do_swap_account)
4168 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
4169 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004170
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004171 memset(&mystat, 0, sizeof(mystat));
4172 mem_cgroup_get_total_stat(mem_cont, &mystat);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004173 for (i = 0; i < NR_MCS_STAT; i++) {
4174 if (i == MCS_SWAP && !do_swap_account)
4175 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004176 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004177 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004178
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004179#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004180 {
4181 int nid, zid;
4182 struct mem_cgroup_per_zone *mz;
4183 unsigned long recent_rotated[2] = {0, 0};
4184 unsigned long recent_scanned[2] = {0, 0};
4185
4186 for_each_online_node(nid)
4187 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
4188 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
4189
4190 recent_rotated[0] +=
4191 mz->reclaim_stat.recent_rotated[0];
4192 recent_rotated[1] +=
4193 mz->reclaim_stat.recent_rotated[1];
4194 recent_scanned[0] +=
4195 mz->reclaim_stat.recent_scanned[0];
4196 recent_scanned[1] +=
4197 mz->reclaim_stat.recent_scanned[1];
4198 }
4199 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
4200 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
4201 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
4202 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
4203 }
4204#endif
4205
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004206 return 0;
4207}
4208
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004209static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
4210{
4211 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4212
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07004213 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004214}
4215
4216static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
4217 u64 val)
4218{
4219 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4220 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08004221
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004222 if (val > 100)
4223 return -EINVAL;
4224
4225 if (cgrp->parent == NULL)
4226 return -EINVAL;
4227
4228 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08004229
4230 cgroup_lock();
4231
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004232 /* If under hierarchy, only empty-root can set this value */
4233 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08004234 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
4235 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004236 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08004237 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004238
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004239 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004240
Li Zefan068b38c2009-01-15 13:51:26 -08004241 cgroup_unlock();
4242
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004243 return 0;
4244}
4245
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004246static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
4247{
4248 struct mem_cgroup_threshold_ary *t;
4249 u64 usage;
4250 int i;
4251
4252 rcu_read_lock();
4253 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004254 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004255 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004256 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004257
4258 if (!t)
4259 goto unlock;
4260
4261 usage = mem_cgroup_usage(memcg, swap);
4262
4263 /*
4264 * current_threshold points to threshold just below usage.
4265 * If it's not true, a threshold was crossed after last
4266 * call of __mem_cgroup_threshold().
4267 */
Phil Carmody5407a562010-05-26 14:42:42 -07004268 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004269
4270 /*
4271 * Iterate backward over array of thresholds starting from
4272 * current_threshold and check if a threshold is crossed.
4273 * If none of thresholds below usage is crossed, we read
4274 * only one element of the array here.
4275 */
4276 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
4277 eventfd_signal(t->entries[i].eventfd, 1);
4278
4279 /* i = current_threshold + 1 */
4280 i++;
4281
4282 /*
4283 * Iterate forward over array of thresholds starting from
4284 * current_threshold+1 and check if a threshold is crossed.
4285 * If none of thresholds above usage is crossed, we read
4286 * only one element of the array here.
4287 */
4288 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
4289 eventfd_signal(t->entries[i].eventfd, 1);
4290
4291 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07004292 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004293unlock:
4294 rcu_read_unlock();
4295}
4296
4297static void mem_cgroup_threshold(struct mem_cgroup *memcg)
4298{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07004299 while (memcg) {
4300 __mem_cgroup_threshold(memcg, false);
4301 if (do_swap_account)
4302 __mem_cgroup_threshold(memcg, true);
4303
4304 memcg = parent_mem_cgroup(memcg);
4305 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004306}
4307
4308static int compare_thresholds(const void *a, const void *b)
4309{
4310 const struct mem_cgroup_threshold *_a = a;
4311 const struct mem_cgroup_threshold *_b = b;
4312
4313 return _a->threshold - _b->threshold;
4314}
4315
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004316static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004317{
4318 struct mem_cgroup_eventfd_list *ev;
4319
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004320 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004321 eventfd_signal(ev->eventfd, 1);
4322 return 0;
4323}
4324
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004325static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004326{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004327 struct mem_cgroup *iter;
4328
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004329 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004330 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004331}
4332
4333static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
4334 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004335{
4336 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004337 struct mem_cgroup_thresholds *thresholds;
4338 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004339 int type = MEMFILE_TYPE(cft->private);
4340 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004341 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004342
4343 ret = res_counter_memparse_write_strategy(args, &threshold);
4344 if (ret)
4345 return ret;
4346
4347 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004348
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004349 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004350 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004351 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004352 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004353 else
4354 BUG();
4355
4356 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4357
4358 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004359 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004360 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4361
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004362 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004363
4364 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004365 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004366 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004367 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004368 ret = -ENOMEM;
4369 goto unlock;
4370 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004371 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004372
4373 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004374 if (thresholds->primary) {
4375 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004376 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004377 }
4378
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004379 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004380 new->entries[size - 1].eventfd = eventfd;
4381 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004382
4383 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004384 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004385 compare_thresholds, NULL);
4386
4387 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004388 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004389 for (i = 0; i < size; i++) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004390 if (new->entries[i].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004391 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004392 * new->current_threshold will not be used until
4393 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004394 * it here.
4395 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004396 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004397 }
4398 }
4399
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004400 /* Free old spare buffer and save old primary buffer as spare */
4401 kfree(thresholds->spare);
4402 thresholds->spare = thresholds->primary;
4403
4404 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004405
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004406 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004407 synchronize_rcu();
4408
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004409unlock:
4410 mutex_unlock(&memcg->thresholds_lock);
4411
4412 return ret;
4413}
4414
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004415static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004416 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004417{
4418 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004419 struct mem_cgroup_thresholds *thresholds;
4420 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004421 int type = MEMFILE_TYPE(cft->private);
4422 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004423 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004424
4425 mutex_lock(&memcg->thresholds_lock);
4426 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004427 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004428 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004429 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004430 else
4431 BUG();
4432
4433 /*
4434 * Something went wrong if we trying to unregister a threshold
4435 * if we don't have thresholds
4436 */
4437 BUG_ON(!thresholds);
4438
4439 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4440
4441 /* Check if a threshold crossed before removing */
4442 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4443
4444 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004445 size = 0;
4446 for (i = 0; i < thresholds->primary->size; i++) {
4447 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004448 size++;
4449 }
4450
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004451 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004452
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004453 /* Set thresholds array to NULL if we don't have thresholds */
4454 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004455 kfree(new);
4456 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004457 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004458 }
4459
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004460 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004461
4462 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004463 new->current_threshold = -1;
4464 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
4465 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004466 continue;
4467
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004468 new->entries[j] = thresholds->primary->entries[i];
4469 if (new->entries[j].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004470 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004471 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004472 * until rcu_assign_pointer(), so it's safe to increment
4473 * it here.
4474 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004475 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004476 }
4477 j++;
4478 }
4479
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004480swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004481 /* Swap primary and spare array */
4482 thresholds->spare = thresholds->primary;
4483 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004484
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004485 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004486 synchronize_rcu();
4487
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004488 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004489}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004490
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004491static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
4492 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
4493{
4494 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4495 struct mem_cgroup_eventfd_list *event;
4496 int type = MEMFILE_TYPE(cft->private);
4497
4498 BUG_ON(type != _OOM_TYPE);
4499 event = kmalloc(sizeof(*event), GFP_KERNEL);
4500 if (!event)
4501 return -ENOMEM;
4502
Michal Hocko1af8efe2011-07-26 16:08:24 -07004503 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004504
4505 event->eventfd = eventfd;
4506 list_add(&event->list, &memcg->oom_notify);
4507
4508 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07004509 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004510 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07004511 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004512
4513 return 0;
4514}
4515
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004516static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004517 struct cftype *cft, struct eventfd_ctx *eventfd)
4518{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004519 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004520 struct mem_cgroup_eventfd_list *ev, *tmp;
4521 int type = MEMFILE_TYPE(cft->private);
4522
4523 BUG_ON(type != _OOM_TYPE);
4524
Michal Hocko1af8efe2011-07-26 16:08:24 -07004525 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004526
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004527 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004528 if (ev->eventfd == eventfd) {
4529 list_del(&ev->list);
4530 kfree(ev);
4531 }
4532 }
4533
Michal Hocko1af8efe2011-07-26 16:08:24 -07004534 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004535}
4536
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004537static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
4538 struct cftype *cft, struct cgroup_map_cb *cb)
4539{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004540 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004541
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004542 cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004543
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004544 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004545 cb->fill(cb, "under_oom", 1);
4546 else
4547 cb->fill(cb, "under_oom", 0);
4548 return 0;
4549}
4550
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004551static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
4552 struct cftype *cft, u64 val)
4553{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004554 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004555 struct mem_cgroup *parent;
4556
4557 /* cannot set to root cgroup and only 0 and 1 are allowed */
4558 if (!cgrp->parent || !((val == 0) || (val == 1)))
4559 return -EINVAL;
4560
4561 parent = mem_cgroup_from_cont(cgrp->parent);
4562
4563 cgroup_lock();
4564 /* oom-kill-disable is a flag for subhierarchy. */
4565 if ((parent->use_hierarchy) ||
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004566 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004567 cgroup_unlock();
4568 return -EINVAL;
4569 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004570 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07004571 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004572 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004573 cgroup_unlock();
4574 return 0;
4575}
4576
Ying Han406eb0c2011-05-26 16:25:37 -07004577#ifdef CONFIG_NUMA
4578static const struct file_operations mem_control_numa_stat_file_operations = {
4579 .read = seq_read,
4580 .llseek = seq_lseek,
4581 .release = single_release,
4582};
4583
4584static int mem_control_numa_stat_open(struct inode *unused, struct file *file)
4585{
4586 struct cgroup *cont = file->f_dentry->d_parent->d_fsdata;
4587
4588 file->f_op = &mem_control_numa_stat_file_operations;
4589 return single_open(file, mem_control_numa_stat_show, cont);
4590}
4591#endif /* CONFIG_NUMA */
4592
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004593static struct cftype mem_cgroup_files[] = {
4594 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004595 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004596 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07004597 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004598 .register_event = mem_cgroup_usage_register_event,
4599 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004600 },
4601 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004602 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004603 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004604 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004605 .read_u64 = mem_cgroup_read,
4606 },
4607 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004608 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004609 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07004610 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07004611 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004612 },
4613 {
Balbir Singh296c81d2009-09-23 15:56:36 -07004614 .name = "soft_limit_in_bytes",
4615 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
4616 .write_string = mem_cgroup_write,
4617 .read_u64 = mem_cgroup_read,
4618 },
4619 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004620 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004621 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004622 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07004623 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004624 },
Balbir Singh8697d332008-02-07 00:13:59 -08004625 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004626 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07004627 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004628 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004629 {
4630 .name = "force_empty",
4631 .trigger = mem_cgroup_force_empty_write,
4632 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08004633 {
4634 .name = "use_hierarchy",
4635 .write_u64 = mem_cgroup_hierarchy_write,
4636 .read_u64 = mem_cgroup_hierarchy_read,
4637 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004638 {
4639 .name = "swappiness",
4640 .read_u64 = mem_cgroup_swappiness_read,
4641 .write_u64 = mem_cgroup_swappiness_write,
4642 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004643 {
4644 .name = "move_charge_at_immigrate",
4645 .read_u64 = mem_cgroup_move_charge_read,
4646 .write_u64 = mem_cgroup_move_charge_write,
4647 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004648 {
4649 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004650 .read_map = mem_cgroup_oom_control_read,
4651 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004652 .register_event = mem_cgroup_oom_register_event,
4653 .unregister_event = mem_cgroup_oom_unregister_event,
4654 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
4655 },
Ying Han406eb0c2011-05-26 16:25:37 -07004656#ifdef CONFIG_NUMA
4657 {
4658 .name = "numa_stat",
4659 .open = mem_control_numa_stat_open,
KAMEZAWA Hiroyuki89577122011-06-15 15:08:41 -07004660 .mode = S_IRUGO,
Ying Han406eb0c2011-05-26 16:25:37 -07004661 },
4662#endif
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004663};
4664
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004665#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4666static struct cftype memsw_cgroup_files[] = {
4667 {
4668 .name = "memsw.usage_in_bytes",
4669 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
4670 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004671 .register_event = mem_cgroup_usage_register_event,
4672 .unregister_event = mem_cgroup_usage_unregister_event,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004673 },
4674 {
4675 .name = "memsw.max_usage_in_bytes",
4676 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
4677 .trigger = mem_cgroup_reset,
4678 .read_u64 = mem_cgroup_read,
4679 },
4680 {
4681 .name = "memsw.limit_in_bytes",
4682 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
4683 .write_string = mem_cgroup_write,
4684 .read_u64 = mem_cgroup_read,
4685 },
4686 {
4687 .name = "memsw.failcnt",
4688 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
4689 .trigger = mem_cgroup_reset,
4690 .read_u64 = mem_cgroup_read,
4691 },
4692};
4693
4694static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
4695{
4696 if (!do_swap_account)
4697 return 0;
4698 return cgroup_add_files(cont, ss, memsw_cgroup_files,
4699 ARRAY_SIZE(memsw_cgroup_files));
4700};
4701#else
4702static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
4703{
4704 return 0;
4705}
4706#endif
4707
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004708static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004709{
4710 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004711 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07004712 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004713 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004714 /*
4715 * This routine is called against possible nodes.
4716 * But it's BUG to call kmalloc() against offline node.
4717 *
4718 * TODO: this routine can waste much memory for nodes which will
4719 * never be onlined. It's better to use memory hotplug callback
4720 * function.
4721 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004722 if (!node_state(node, N_NORMAL_MEMORY))
4723 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08004724 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004725 if (!pn)
4726 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004727
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004728 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4729 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07004730 for_each_lru(l)
4731 INIT_LIST_HEAD(&mz->lists[l]);
Balbir Singhf64c3f52009-09-23 15:56:37 -07004732 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07004733 mz->on_tree = false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004734 mz->mem = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004735 }
Igor Mammedov0a619e52011-11-02 13:38:21 -07004736 memcg->info.nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004737 return 0;
4738}
4739
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004740static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004741{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004742 kfree(memcg->info.nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004743}
4744
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004745static struct mem_cgroup *mem_cgroup_alloc(void)
4746{
4747 struct mem_cgroup *mem;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004748 int size = sizeof(struct mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004749
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004750 /* Can be very big if MAX_NUMNODES is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08004751 if (size < PAGE_SIZE)
Jesper Juhl17295c82011-01-13 15:47:42 -08004752 mem = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004753 else
Jesper Juhl17295c82011-01-13 15:47:42 -08004754 mem = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004755
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07004756 if (!mem)
4757 return NULL;
4758
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004759 mem->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004760 if (!mem->stat)
4761 goto out_free;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07004762 spin_lock_init(&mem->pcp_counter_lock);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004763 return mem;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004764
4765out_free:
4766 if (size < PAGE_SIZE)
4767 kfree(mem);
4768 else
4769 vfree(mem);
4770 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004771}
4772
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004773/*
4774 * At destroying mem_cgroup, references from swap_cgroup can remain.
4775 * (scanning all at force_empty is too costly...)
4776 *
4777 * Instead of clearing all references at force_empty, we remember
4778 * the number of reference from swap_cgroup and free mem_cgroup when
4779 * it goes down to 0.
4780 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004781 * Removal of cgroup itself succeeds regardless of refs from swap.
4782 */
4783
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004784static void __mem_cgroup_free(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004785{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004786 int node;
4787
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004788 mem_cgroup_remove_from_trees(memcg);
4789 free_css_id(&mem_cgroup_subsys, &memcg->css);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004790
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004791 for_each_node_state(node, N_POSSIBLE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004792 free_mem_cgroup_per_zone_info(memcg, node);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004793
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004794 free_percpu(memcg->stat);
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004795 if (sizeof(struct mem_cgroup) < PAGE_SIZE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004796 kfree(memcg);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004797 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004798 vfree(memcg);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004799}
4800
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004801static void mem_cgroup_get(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004802{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004803 atomic_inc(&memcg->refcnt);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004804}
4805
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004806static void __mem_cgroup_put(struct mem_cgroup *memcg, int count)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004807{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004808 if (atomic_sub_and_test(count, &memcg->refcnt)) {
4809 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
4810 __mem_cgroup_free(memcg);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004811 if (parent)
4812 mem_cgroup_put(parent);
4813 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004814}
4815
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004816static void mem_cgroup_put(struct mem_cgroup *memcg)
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004817{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004818 __mem_cgroup_put(memcg, 1);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004819}
4820
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004821/*
4822 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
4823 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004824static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004825{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004826 if (!memcg->res.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004827 return NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004828 return mem_cgroup_from_res_counter(memcg->res.parent, res);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004829}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004830
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004831#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4832static void __init enable_swap_cgroup(void)
4833{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004834 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004835 do_swap_account = 1;
4836}
4837#else
4838static void __init enable_swap_cgroup(void)
4839{
4840}
4841#endif
4842
Balbir Singhf64c3f52009-09-23 15:56:37 -07004843static int mem_cgroup_soft_limit_tree_init(void)
4844{
4845 struct mem_cgroup_tree_per_node *rtpn;
4846 struct mem_cgroup_tree_per_zone *rtpz;
4847 int tmp, node, zone;
4848
4849 for_each_node_state(node, N_POSSIBLE) {
4850 tmp = node;
4851 if (!node_state(node, N_NORMAL_MEMORY))
4852 tmp = -1;
4853 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
4854 if (!rtpn)
4855 return 1;
4856
4857 soft_limit_tree.rb_tree_per_node[node] = rtpn;
4858
4859 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4860 rtpz = &rtpn->rb_tree_per_zone[zone];
4861 rtpz->rb_root = RB_ROOT;
4862 spin_lock_init(&rtpz->lock);
4863 }
4864 }
4865 return 0;
4866}
4867
Li Zefan0eb253e2009-01-15 13:51:25 -08004868static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004869mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
4870{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004871 struct mem_cgroup *memcg, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004872 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004873 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004874
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004875 memcg = mem_cgroup_alloc();
4876 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004877 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004878
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004879 for_each_node_state(node, N_POSSIBLE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004880 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004881 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004882
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004883 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004884 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004885 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004886 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004887 parent = NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004888 root_mem_cgroup = memcg;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004889 if (mem_cgroup_soft_limit_tree_init())
4890 goto free_out;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004891 for_each_possible_cpu(cpu) {
4892 struct memcg_stock_pcp *stock =
4893 &per_cpu(memcg_stock, cpu);
4894 INIT_WORK(&stock->work, drain_local_stock);
4895 }
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07004896 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004897 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004898 parent = mem_cgroup_from_cont(cont->parent);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004899 memcg->use_hierarchy = parent->use_hierarchy;
4900 memcg->oom_kill_disable = parent->oom_kill_disable;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004901 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004902
Balbir Singh18f59ea2009-01-07 18:08:07 -08004903 if (parent && parent->use_hierarchy) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004904 res_counter_init(&memcg->res, &parent->res);
4905 res_counter_init(&memcg->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004906 /*
4907 * We increment refcnt of the parent to ensure that we can
4908 * safely access it on res_counter_charge/uncharge.
4909 * This refcnt will be decremented when freeing this
4910 * mem_cgroup(see mem_cgroup_put).
4911 */
4912 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004913 } else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004914 res_counter_init(&memcg->res, NULL);
4915 res_counter_init(&memcg->memsw, NULL);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004916 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004917 memcg->last_scanned_child = 0;
4918 memcg->last_scanned_node = MAX_NUMNODES;
4919 INIT_LIST_HEAD(&memcg->oom_notify);
Balbir Singh6d61ef42009-01-07 18:08:06 -08004920
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004921 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004922 memcg->swappiness = mem_cgroup_swappiness(parent);
4923 atomic_set(&memcg->refcnt, 1);
4924 memcg->move_charge_at_immigrate = 0;
4925 mutex_init(&memcg->thresholds_lock);
4926 return &memcg->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004927free_out:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004928 __mem_cgroup_free(memcg);
Balbir Singh4b3bde42009-09-23 15:56:32 -07004929 root_mem_cgroup = NULL;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004930 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004931}
4932
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004933static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004934 struct cgroup *cont)
4935{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004936 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004937
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004938 return mem_cgroup_force_empty(memcg, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004939}
4940
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004941static void mem_cgroup_destroy(struct cgroup_subsys *ss,
4942 struct cgroup *cont)
4943{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004944 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004945
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004946 mem_cgroup_put(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004947}
4948
4949static int mem_cgroup_populate(struct cgroup_subsys *ss,
4950 struct cgroup *cont)
4951{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004952 int ret;
4953
4954 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
4955 ARRAY_SIZE(mem_cgroup_files));
4956
4957 if (!ret)
4958 ret = register_memsw_files(cont, ss);
4959 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004960}
4961
Daisuke Nishimura02491442010-03-10 15:22:17 -08004962#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004963/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004964#define PRECHARGE_COUNT_AT_ONCE 256
4965static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004966{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004967 int ret = 0;
4968 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004969 struct mem_cgroup *memcg = mc.to;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004970
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004971 if (mem_cgroup_is_root(memcg)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004972 mc.precharge += count;
4973 /* we don't need css_get for root */
4974 return ret;
4975 }
4976 /* try to charge at once */
4977 if (count > 1) {
4978 struct res_counter *dummy;
4979 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004980 * "memcg" cannot be under rmdir() because we've already checked
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004981 * by cgroup_lock_live_cgroup() that it is not removed and we
4982 * are still under the same cgroup_mutex. So we can postpone
4983 * css_get().
4984 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004985 if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy))
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004986 goto one_by_one;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004987 if (do_swap_account && res_counter_charge(&memcg->memsw,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004988 PAGE_SIZE * count, &dummy)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004989 res_counter_uncharge(&memcg->res, PAGE_SIZE * count);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004990 goto one_by_one;
4991 }
4992 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004993 return ret;
4994 }
4995one_by_one:
4996 /* fall back to one by one charge */
4997 while (count--) {
4998 if (signal_pending(current)) {
4999 ret = -EINTR;
5000 break;
5001 }
5002 if (!batch_count--) {
5003 batch_count = PRECHARGE_COUNT_AT_ONCE;
5004 cond_resched();
5005 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005006 ret = __mem_cgroup_try_charge(NULL,
5007 GFP_KERNEL, 1, &memcg, false);
5008 if (ret || !memcg)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005009 /* mem_cgroup_clear_mc() will do uncharge later */
5010 return -ENOMEM;
5011 mc.precharge++;
5012 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005013 return ret;
5014}
5015
5016/**
5017 * is_target_pte_for_mc - check a pte whether it is valid for move charge
5018 * @vma: the vma the pte to be checked belongs
5019 * @addr: the address corresponding to the pte to be checked
5020 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08005021 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005022 *
5023 * Returns
5024 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
5025 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
5026 * move charge. if @target is not NULL, the page is stored in target->page
5027 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08005028 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
5029 * target for charge migration. if @target is not NULL, the entry is stored
5030 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005031 *
5032 * Called with pte lock held.
5033 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005034union mc_target {
5035 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005036 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005037};
5038
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005039enum mc_target_type {
5040 MC_TARGET_NONE, /* not used */
5041 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08005042 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005043};
5044
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005045static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
5046 unsigned long addr, pte_t ptent)
5047{
5048 struct page *page = vm_normal_page(vma, addr, ptent);
5049
5050 if (!page || !page_mapped(page))
5051 return NULL;
5052 if (PageAnon(page)) {
5053 /* we don't move shared anon */
5054 if (!move_anon() || page_mapcount(page) > 2)
5055 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005056 } else if (!move_file())
5057 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005058 return NULL;
5059 if (!get_page_unless_zero(page))
5060 return NULL;
5061
5062 return page;
5063}
5064
5065static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5066 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5067{
5068 int usage_count;
5069 struct page *page = NULL;
5070 swp_entry_t ent = pte_to_swp_entry(ptent);
5071
5072 if (!move_anon() || non_swap_entry(ent))
5073 return NULL;
5074 usage_count = mem_cgroup_count_swap_user(ent, &page);
5075 if (usage_count > 1) { /* we don't move shared anon */
5076 if (page)
5077 put_page(page);
5078 return NULL;
5079 }
5080 if (do_swap_account)
5081 entry->val = ent.val;
5082
5083 return page;
5084}
5085
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005086static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
5087 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5088{
5089 struct page *page = NULL;
5090 struct inode *inode;
5091 struct address_space *mapping;
5092 pgoff_t pgoff;
5093
5094 if (!vma->vm_file) /* anonymous vma */
5095 return NULL;
5096 if (!move_file())
5097 return NULL;
5098
5099 inode = vma->vm_file->f_path.dentry->d_inode;
5100 mapping = vma->vm_file->f_mapping;
5101 if (pte_none(ptent))
5102 pgoff = linear_page_index(vma, addr);
5103 else /* pte_file(ptent) is true */
5104 pgoff = pte_to_pgoff(ptent);
5105
5106 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005107 page = find_get_page(mapping, pgoff);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005108
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005109#ifdef CONFIG_SWAP
5110 /* shmem/tmpfs may report page out on swap: account for that too. */
5111 if (radix_tree_exceptional_entry(page)) {
5112 swp_entry_t swap = radix_to_swp_entry(page);
5113 if (do_swap_account)
5114 *entry = swap;
5115 page = find_get_page(&swapper_space, swap.val);
5116 }
5117#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005118 return page;
5119}
5120
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005121static int is_target_pte_for_mc(struct vm_area_struct *vma,
5122 unsigned long addr, pte_t ptent, union mc_target *target)
5123{
Daisuke Nishimura02491442010-03-10 15:22:17 -08005124 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005125 struct page_cgroup *pc;
5126 int ret = 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005127 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005128
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005129 if (pte_present(ptent))
5130 page = mc_handle_present_pte(vma, addr, ptent);
5131 else if (is_swap_pte(ptent))
5132 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005133 else if (pte_none(ptent) || pte_file(ptent))
5134 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005135
5136 if (!page && !ent.val)
Daisuke Nishimura02491442010-03-10 15:22:17 -08005137 return 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005138 if (page) {
5139 pc = lookup_page_cgroup(page);
5140 /*
5141 * Do only loose check w/o page_cgroup lock.
5142 * mem_cgroup_move_account() checks the pc is valid or not under
5143 * the lock.
5144 */
5145 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
5146 ret = MC_TARGET_PAGE;
5147 if (target)
5148 target->page = page;
5149 }
5150 if (!ret || !target)
5151 put_page(page);
5152 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005153 /* There is a swap entry and a page doesn't exist or isn't charged */
5154 if (ent.val && !ret &&
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005155 css_id(&mc.from->css) == lookup_swap_cgroup(ent)) {
5156 ret = MC_TARGET_SWAP;
5157 if (target)
5158 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005159 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005160 return ret;
5161}
5162
5163static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
5164 unsigned long addr, unsigned long end,
5165 struct mm_walk *walk)
5166{
5167 struct vm_area_struct *vma = walk->private;
5168 pte_t *pte;
5169 spinlock_t *ptl;
5170
Dave Hansen03319322011-03-22 16:32:56 -07005171 split_huge_page_pmd(walk->mm, pmd);
5172
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005173 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5174 for (; addr != end; pte++, addr += PAGE_SIZE)
5175 if (is_target_pte_for_mc(vma, addr, *pte, NULL))
5176 mc.precharge++; /* increment precharge temporarily */
5177 pte_unmap_unlock(pte - 1, ptl);
5178 cond_resched();
5179
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005180 return 0;
5181}
5182
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005183static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
5184{
5185 unsigned long precharge;
5186 struct vm_area_struct *vma;
5187
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005188 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005189 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5190 struct mm_walk mem_cgroup_count_precharge_walk = {
5191 .pmd_entry = mem_cgroup_count_precharge_pte_range,
5192 .mm = mm,
5193 .private = vma,
5194 };
5195 if (is_vm_hugetlb_page(vma))
5196 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005197 walk_page_range(vma->vm_start, vma->vm_end,
5198 &mem_cgroup_count_precharge_walk);
5199 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005200 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005201
5202 precharge = mc.precharge;
5203 mc.precharge = 0;
5204
5205 return precharge;
5206}
5207
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005208static int mem_cgroup_precharge_mc(struct mm_struct *mm)
5209{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005210 unsigned long precharge = mem_cgroup_count_precharge(mm);
5211
5212 VM_BUG_ON(mc.moving_task);
5213 mc.moving_task = current;
5214 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005215}
5216
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005217/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
5218static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005219{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005220 struct mem_cgroup *from = mc.from;
5221 struct mem_cgroup *to = mc.to;
5222
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005223 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005224 if (mc.precharge) {
5225 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
5226 mc.precharge = 0;
5227 }
5228 /*
5229 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
5230 * we must uncharge here.
5231 */
5232 if (mc.moved_charge) {
5233 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
5234 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005235 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005236 /* we must fixup refcnts and charges */
5237 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005238 /* uncharge swap account from the old cgroup */
5239 if (!mem_cgroup_is_root(mc.from))
5240 res_counter_uncharge(&mc.from->memsw,
5241 PAGE_SIZE * mc.moved_swap);
5242 __mem_cgroup_put(mc.from, mc.moved_swap);
5243
5244 if (!mem_cgroup_is_root(mc.to)) {
5245 /*
5246 * we charged both to->res and to->memsw, so we should
5247 * uncharge to->res.
5248 */
5249 res_counter_uncharge(&mc.to->res,
5250 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005251 }
5252 /* we've already done mem_cgroup_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005253 mc.moved_swap = 0;
5254 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005255 memcg_oom_recover(from);
5256 memcg_oom_recover(to);
5257 wake_up_all(&mc.waitq);
5258}
5259
5260static void mem_cgroup_clear_mc(void)
5261{
5262 struct mem_cgroup *from = mc.from;
5263
5264 /*
5265 * we must clear moving_task before waking up waiters at the end of
5266 * task migration.
5267 */
5268 mc.moving_task = NULL;
5269 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005270 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005271 mc.from = NULL;
5272 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005273 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005274 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005275}
5276
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005277static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
5278 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005279 struct task_struct *p)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005280{
5281 int ret = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005282 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005283
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005284 if (memcg->move_charge_at_immigrate) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005285 struct mm_struct *mm;
5286 struct mem_cgroup *from = mem_cgroup_from_task(p);
5287
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005288 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005289
5290 mm = get_task_mm(p);
5291 if (!mm)
5292 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005293 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005294 if (mm->owner == p) {
5295 VM_BUG_ON(mc.from);
5296 VM_BUG_ON(mc.to);
5297 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005298 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005299 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005300 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005301 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005302 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005303 mc.to = memcg;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005304 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005305 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005306
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005307 ret = mem_cgroup_precharge_mc(mm);
5308 if (ret)
5309 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005310 }
5311 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005312 }
5313 return ret;
5314}
5315
5316static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
5317 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005318 struct task_struct *p)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005319{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005320 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005321}
5322
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005323static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
5324 unsigned long addr, unsigned long end,
5325 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005326{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005327 int ret = 0;
5328 struct vm_area_struct *vma = walk->private;
5329 pte_t *pte;
5330 spinlock_t *ptl;
5331
Dave Hansen03319322011-03-22 16:32:56 -07005332 split_huge_page_pmd(walk->mm, pmd);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005333retry:
5334 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5335 for (; addr != end; addr += PAGE_SIZE) {
5336 pte_t ptent = *(pte++);
5337 union mc_target target;
5338 int type;
5339 struct page *page;
5340 struct page_cgroup *pc;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005341 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005342
5343 if (!mc.precharge)
5344 break;
5345
5346 type = is_target_pte_for_mc(vma, addr, ptent, &target);
5347 switch (type) {
5348 case MC_TARGET_PAGE:
5349 page = target.page;
5350 if (isolate_lru_page(page))
5351 goto put;
5352 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07005353 if (!mem_cgroup_move_account(page, 1, pc,
5354 mc.from, mc.to, false)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005355 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005356 /* we uncharge from mc.from later. */
5357 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005358 }
5359 putback_lru_page(page);
5360put: /* is_target_pte_for_mc() gets the page */
5361 put_page(page);
5362 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005363 case MC_TARGET_SWAP:
5364 ent = target.ent;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005365 if (!mem_cgroup_move_swap_account(ent,
5366 mc.from, mc.to, false)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08005367 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005368 /* we fixup refcnts and charges later. */
5369 mc.moved_swap++;
5370 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08005371 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005372 default:
5373 break;
5374 }
5375 }
5376 pte_unmap_unlock(pte - 1, ptl);
5377 cond_resched();
5378
5379 if (addr != end) {
5380 /*
5381 * We have consumed all precharges we got in can_attach().
5382 * We try charge one by one, but don't do any additional
5383 * charges to mc.to if we have failed in charge once in attach()
5384 * phase.
5385 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005386 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005387 if (!ret)
5388 goto retry;
5389 }
5390
5391 return ret;
5392}
5393
5394static void mem_cgroup_move_charge(struct mm_struct *mm)
5395{
5396 struct vm_area_struct *vma;
5397
5398 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005399retry:
5400 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
5401 /*
5402 * Someone who are holding the mmap_sem might be waiting in
5403 * waitq. So we cancel all extra charges, wake up all waiters,
5404 * and retry. Because we cancel precharges, we might not be able
5405 * to move enough charges, but moving charge is a best-effort
5406 * feature anyway, so it wouldn't be a big problem.
5407 */
5408 __mem_cgroup_clear_mc();
5409 cond_resched();
5410 goto retry;
5411 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005412 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5413 int ret;
5414 struct mm_walk mem_cgroup_move_charge_walk = {
5415 .pmd_entry = mem_cgroup_move_charge_pte_range,
5416 .mm = mm,
5417 .private = vma,
5418 };
5419 if (is_vm_hugetlb_page(vma))
5420 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005421 ret = walk_page_range(vma->vm_start, vma->vm_end,
5422 &mem_cgroup_move_charge_walk);
5423 if (ret)
5424 /*
5425 * means we have consumed all precharges and failed in
5426 * doing additional charge. Just abandon here.
5427 */
5428 break;
5429 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005430 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005431}
5432
Balbir Singh67e465a2008-02-07 00:13:54 -08005433static void mem_cgroup_move_task(struct cgroup_subsys *ss,
5434 struct cgroup *cont,
5435 struct cgroup *old_cont,
Ben Blumf780bdb2011-05-26 16:25:19 -07005436 struct task_struct *p)
Balbir Singh67e465a2008-02-07 00:13:54 -08005437{
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005438 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005439
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005440 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005441 if (mc.to)
5442 mem_cgroup_move_charge(mm);
5443 put_swap_token(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005444 mmput(mm);
5445 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005446 if (mc.to)
5447 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08005448}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005449#else /* !CONFIG_MMU */
5450static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
5451 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005452 struct task_struct *p)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005453{
5454 return 0;
5455}
5456static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
5457 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005458 struct task_struct *p)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005459{
5460}
5461static void mem_cgroup_move_task(struct cgroup_subsys *ss,
5462 struct cgroup *cont,
5463 struct cgroup *old_cont,
Ben Blumf780bdb2011-05-26 16:25:19 -07005464 struct task_struct *p)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005465{
5466}
5467#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08005468
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005469struct cgroup_subsys mem_cgroup_subsys = {
5470 .name = "memory",
5471 .subsys_id = mem_cgroup_subsys_id,
5472 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005473 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005474 .destroy = mem_cgroup_destroy,
5475 .populate = mem_cgroup_populate,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005476 .can_attach = mem_cgroup_can_attach,
5477 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08005478 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005479 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005480 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005481};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005482
5483#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08005484static int __init enable_swap_account(char *s)
5485{
5486 /* consider enabled if no parameter or 1 is given */
Michal Hockoa2c89902011-05-24 17:12:50 -07005487 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08005488 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07005489 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08005490 really_do_swap_account = 0;
5491 return 1;
5492}
Michal Hockoa2c89902011-05-24 17:12:50 -07005493__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005494
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005495#endif