blob: 01e0c725de659826aac9872de632985e1baa47c7 [file] [log] [blame]
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001/* memcontrol.c - Memory Controller
2 *
3 * Copyright IBM Corporation, 2007
4 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
5 *
Pavel Emelianov78fb7462008-02-07 00:13:51 -08006 * Copyright 2007 OpenVZ SWsoft Inc
7 * Author: Pavel Emelianov <xemul@openvz.org>
8 *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08009 * Memory thresholds
10 * Copyright (C) 2009 Nokia Corporation
11 * Author: Kirill A. Shutemov
12 *
Balbir Singh8cdea7c2008-02-07 00:13:50 -080013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 */
23
24#include <linux/res_counter.h>
25#include <linux/memcontrol.h>
26#include <linux/cgroup.h>
Pavel Emelianov78fb7462008-02-07 00:13:51 -080027#include <linux/mm.h>
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -080028#include <linux/hugetlb.h>
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -080029#include <linux/pagemap.h>
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080030#include <linux/smp.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080031#include <linux/page-flags.h>
Balbir Singh66e17072008-02-07 00:13:56 -080032#include <linux/backing-dev.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080033#include <linux/bit_spinlock.h>
34#include <linux/rcupdate.h>
Balbir Singhe2224322009-04-02 16:57:39 -070035#include <linux/limits.h>
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -080036#include <linux/mutex.h>
Balbir Singhf64c3f52009-09-23 15:56:37 -070037#include <linux/rbtree.h>
Balbir Singhb6ac57d2008-04-29 01:00:19 -070038#include <linux/slab.h>
Balbir Singh66e17072008-02-07 00:13:56 -080039#include <linux/swap.h>
Daisuke Nishimura02491442010-03-10 15:22:17 -080040#include <linux/swapops.h>
Balbir Singh66e17072008-02-07 00:13:56 -080041#include <linux/spinlock.h>
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -080042#include <linux/eventfd.h>
43#include <linux/sort.h>
Balbir Singh66e17072008-02-07 00:13:56 -080044#include <linux/fs.h>
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -080045#include <linux/seq_file.h>
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -070046#include <linux/vmalloc.h>
Christoph Lameterb69408e2008-10-18 20:26:14 -070047#include <linux/mm_inline.h>
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070048#include <linux/page_cgroup.h>
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -080049#include <linux/cpu.h>
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -070050#include <linux/oom.h>
KAMEZAWA 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 Hiroyuki072c56c2008-02-07 00:14:39 -0800127 /*
128 * spin_lock to protect the per cgroup LRU
129 */
Christoph Lameterb69408e2008-10-18 20:26:14 -0700130 struct list_head lists[NR_LRU_LISTS];
131 unsigned long count[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800132
133 struct zone_reclaim_stat reclaim_stat;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700134 struct rb_node tree_node; /* RB tree node */
135 unsigned long long usage_in_excess;/* Set to the value by which */
136 /* the soft limit is exceeded*/
137 bool on_tree;
Balbir Singh4e416952009-09-23 15:56:39 -0700138 struct mem_cgroup *mem; /* Back pointer, we cannot */
139 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800140};
141/* Macro for accessing counter */
142#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
143
144struct mem_cgroup_per_node {
145 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
146};
147
148struct mem_cgroup_lru_info {
149 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
150};
151
152/*
Balbir Singhf64c3f52009-09-23 15:56:37 -0700153 * Cgroups above their limits are maintained in a RB-Tree, independent of
154 * their hierarchy representation
155 */
156
157struct mem_cgroup_tree_per_zone {
158 struct rb_root rb_root;
159 spinlock_t lock;
160};
161
162struct mem_cgroup_tree_per_node {
163 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
164};
165
166struct mem_cgroup_tree {
167 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
168};
169
170static struct mem_cgroup_tree soft_limit_tree __read_mostly;
171
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800172struct mem_cgroup_threshold {
173 struct eventfd_ctx *eventfd;
174 u64 threshold;
175};
176
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700177/* For threshold */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800178struct mem_cgroup_threshold_ary {
179 /* An array index points to threshold just below usage. */
Phil Carmody5407a562010-05-26 14:42:42 -0700180 int current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800181 /* Size of entries[] */
182 unsigned int size;
183 /* Array of thresholds */
184 struct mem_cgroup_threshold entries[0];
185};
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700186
187struct mem_cgroup_thresholds {
188 /* Primary thresholds array */
189 struct mem_cgroup_threshold_ary *primary;
190 /*
191 * Spare threshold array.
192 * This is needed to make mem_cgroup_unregister_event() "never fail".
193 * It must be able to store at least primary->size - 1 entries.
194 */
195 struct mem_cgroup_threshold_ary *spare;
196};
197
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700198/* for OOM */
199struct mem_cgroup_eventfd_list {
200 struct list_head list;
201 struct eventfd_ctx *eventfd;
202};
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800203
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700204static void mem_cgroup_threshold(struct mem_cgroup *memcg);
205static void mem_cgroup_oom_notify(struct mem_cgroup *memcg);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800206
Balbir Singhf64c3f52009-09-23 15:56:37 -0700207/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800208 * The memory controller data structure. The memory controller controls both
209 * page cache and RSS per cgroup. We would eventually like to provide
210 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
211 * to help the administrator determine what knobs to tune.
212 *
213 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800214 * we hit the water mark. May be even add a low water mark, such that
215 * no reclaim occurs from a cgroup at it's low water mark, this is
216 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800217 */
218struct mem_cgroup {
219 struct cgroup_subsys_state css;
220 /*
221 * the counter to account for memory usage
222 */
223 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800224 /*
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800225 * the counter to account for mem+swap usage.
226 */
227 struct res_counter memsw;
228 /*
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800229 * Per cgroup active and inactive list, similar to the
230 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800231 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800232 struct mem_cgroup_lru_info info;
Balbir Singh6d61ef42009-01-07 18:08:06 -0800233 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200234 * While reclaiming in a hierarchy, we cache the last child we
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700235 * reclaimed from.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800236 */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700237 int last_scanned_child;
Ying Han889976d2011-05-26 16:25:33 -0700238 int last_scanned_node;
239#if MAX_NUMNODES > 1
240 nodemask_t scan_nodes;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700241 atomic_t numainfo_events;
242 atomic_t numainfo_updating;
Ying Han889976d2011-05-26 16:25:33 -0700243#endif
Balbir Singh18f59ea2009-01-07 18:08:07 -0800244 /*
245 * Should the accounting and control be hierarchical, per subtree?
246 */
247 bool use_hierarchy;
Michal Hocko79dfdac2011-07-26 16:08:23 -0700248
249 bool oom_lock;
250 atomic_t under_oom;
251
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800252 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800253
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -0700254 int swappiness;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -0700255 /* OOM-Killer disable */
256 int oom_kill_disable;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800257
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700258 /* set when res.limit == memsw.limit */
259 bool memsw_is_minimum;
260
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800261 /* protect arrays of thresholds */
262 struct mutex thresholds_lock;
263
264 /* thresholds for memory usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700265 struct mem_cgroup_thresholds thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700266
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800267 /* thresholds for mem+swap usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700268 struct mem_cgroup_thresholds memsw_thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700269
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700270 /* For oom notifier event fd */
271 struct list_head oom_notify;
Johannes Weiner185efc02011-09-14 16:21:58 -0700272
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800273 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800274 * Should we move charges of a task when a task is moved into this
275 * mem_cgroup ? And what type of charges should we move ?
276 */
277 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800278 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800279 * percpu counter.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800280 */
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800281 struct mem_cgroup_stat_cpu *stat;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700282 /*
283 * used when a cpu is offlined or other synchronizations
284 * See mem_cgroup_read_stat().
285 */
286 struct mem_cgroup_stat_cpu nocpu_base;
287 spinlock_t pcp_counter_lock;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800288};
289
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800290/* Stuffs for move charges at task migration. */
291/*
292 * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a
293 * left-shifted bitmap of these types.
294 */
295enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800296 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700297 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800298 NR_MOVE_TYPE,
299};
300
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800301/* "mc" and its members are protected by cgroup_mutex */
302static struct move_charge_struct {
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -0800303 spinlock_t lock; /* for from, to */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800304 struct mem_cgroup *from;
305 struct mem_cgroup *to;
306 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800307 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800308 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800309 struct task_struct *moving_task; /* a task moving charges */
310 wait_queue_head_t waitq; /* a waitq for other context */
311} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700312 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800313 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
314};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800315
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700316static bool move_anon(void)
317{
318 return test_bit(MOVE_CHARGE_TYPE_ANON,
319 &mc.to->move_charge_at_immigrate);
320}
321
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700322static bool move_file(void)
323{
324 return test_bit(MOVE_CHARGE_TYPE_FILE,
325 &mc.to->move_charge_at_immigrate);
326}
327
Balbir Singh4e416952009-09-23 15:56:39 -0700328/*
329 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
330 * limit reclaim to prevent infinite loops, if they ever occur.
331 */
332#define MEM_CGROUP_MAX_RECLAIM_LOOPS (100)
333#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2)
334
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800335enum charge_type {
336 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
337 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700338 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700339 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800340 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700341 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700342 NR_CHARGE_TYPE,
343};
344
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800345/* for encoding cft->private value on file */
346#define _MEM (0)
347#define _MEMSWAP (1)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700348#define _OOM_TYPE (2)
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800349#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
350#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
351#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700352/* Used for OOM nofiier */
353#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800354
Balbir Singh75822b42009-09-23 15:56:38 -0700355/*
356 * Reclaim flags for mem_cgroup_hierarchical_reclaim
357 */
358#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
359#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
360#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
361#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
Balbir Singh4e416952009-09-23 15:56:39 -0700362#define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
363#define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
Balbir Singh75822b42009-09-23 15:56:38 -0700364
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700365static void mem_cgroup_get(struct mem_cgroup *memcg);
366static void mem_cgroup_put(struct mem_cgroup *memcg);
367static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg);
368static void drain_all_stock_async(struct mem_cgroup *memcg);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -0800369
Balbir Singhf64c3f52009-09-23 15:56:37 -0700370static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700371mem_cgroup_zoneinfo(struct mem_cgroup *memcg, int nid, int zid)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700372{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700373 return &memcg->info.nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700374}
375
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700376struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100377{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700378 return &memcg->css;
Wu Fengguangd3242362009-12-16 12:19:59 +0100379}
380
Balbir Singhf64c3f52009-09-23 15:56:37 -0700381static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700382page_cgroup_zoneinfo(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700383{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700384 int nid = page_to_nid(page);
385 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700386
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700387 return mem_cgroup_zoneinfo(memcg, nid, zid);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700388}
389
390static struct mem_cgroup_tree_per_zone *
391soft_limit_tree_node_zone(int nid, int zid)
392{
393 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
394}
395
396static struct mem_cgroup_tree_per_zone *
397soft_limit_tree_from_page(struct page *page)
398{
399 int nid = page_to_nid(page);
400 int zid = page_zonenum(page);
401
402 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
403}
404
405static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700406__mem_cgroup_insert_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700407 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700408 struct mem_cgroup_tree_per_zone *mctz,
409 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700410{
411 struct rb_node **p = &mctz->rb_root.rb_node;
412 struct rb_node *parent = NULL;
413 struct mem_cgroup_per_zone *mz_node;
414
415 if (mz->on_tree)
416 return;
417
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700418 mz->usage_in_excess = new_usage_in_excess;
419 if (!mz->usage_in_excess)
420 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700421 while (*p) {
422 parent = *p;
423 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
424 tree_node);
425 if (mz->usage_in_excess < mz_node->usage_in_excess)
426 p = &(*p)->rb_left;
427 /*
428 * We can't avoid mem cgroups that are over their soft
429 * limit by the same amount
430 */
431 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
432 p = &(*p)->rb_right;
433 }
434 rb_link_node(&mz->tree_node, parent, p);
435 rb_insert_color(&mz->tree_node, &mctz->rb_root);
436 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700437}
438
439static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700440__mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singh4e416952009-09-23 15:56:39 -0700441 struct mem_cgroup_per_zone *mz,
442 struct mem_cgroup_tree_per_zone *mctz)
443{
444 if (!mz->on_tree)
445 return;
446 rb_erase(&mz->tree_node, &mctz->rb_root);
447 mz->on_tree = false;
448}
449
450static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700451mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700452 struct mem_cgroup_per_zone *mz,
453 struct mem_cgroup_tree_per_zone *mctz)
454{
455 spin_lock(&mctz->lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700456 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700457 spin_unlock(&mctz->lock);
458}
459
Balbir Singhf64c3f52009-09-23 15:56:37 -0700460
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700461static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700462{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700463 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700464 struct mem_cgroup_per_zone *mz;
465 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700466 int nid = page_to_nid(page);
467 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700468 mctz = soft_limit_tree_from_page(page);
469
470 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700471 * Necessary to update all ancestors when hierarchy is used.
472 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700473 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700474 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
475 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
476 excess = res_counter_soft_limit_excess(&memcg->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700477 /*
478 * We have to update the tree if mz is on RB-tree or
479 * mem is over its softlimit.
480 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700481 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700482 spin_lock(&mctz->lock);
483 /* if on-tree, remove it */
484 if (mz->on_tree)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700485 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700486 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700487 * Insert again. mz->usage_in_excess will be updated.
488 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700489 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700490 __mem_cgroup_insert_exceeded(memcg, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700491 spin_unlock(&mctz->lock);
492 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700493 }
494}
495
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700496static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700497{
498 int node, zone;
499 struct mem_cgroup_per_zone *mz;
500 struct mem_cgroup_tree_per_zone *mctz;
501
502 for_each_node_state(node, N_POSSIBLE) {
503 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700504 mz = mem_cgroup_zoneinfo(memcg, node, zone);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700505 mctz = soft_limit_tree_node_zone(node, zone);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700506 mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700507 }
508 }
509}
510
Balbir Singh4e416952009-09-23 15:56:39 -0700511static struct mem_cgroup_per_zone *
512__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
513{
514 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700515 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700516
517retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700518 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700519 rightmost = rb_last(&mctz->rb_root);
520 if (!rightmost)
521 goto done; /* Nothing to reclaim from */
522
523 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
524 /*
525 * Remove the node now but someone else can add it back,
526 * we will to add it back at the end of reclaim to its correct
527 * position in the tree.
528 */
529 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
530 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
531 !css_tryget(&mz->mem->css))
532 goto retry;
533done:
534 return mz;
535}
536
537static struct mem_cgroup_per_zone *
538mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
539{
540 struct mem_cgroup_per_zone *mz;
541
542 spin_lock(&mctz->lock);
543 mz = __mem_cgroup_largest_soft_limit_node(mctz);
544 spin_unlock(&mctz->lock);
545 return mz;
546}
547
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700548/*
549 * Implementation Note: reading percpu statistics for memcg.
550 *
551 * Both of vmstat[] and percpu_counter has threshold and do periodic
552 * synchronization to implement "quick" read. There are trade-off between
553 * reading cost and precision of value. Then, we may have a chance to implement
554 * a periodic synchronizion of counter in memcg's counter.
555 *
556 * But this _read() function is used for user interface now. The user accounts
557 * memory usage by memory cgroup and he _always_ requires exact value because
558 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
559 * have to visit all online cpus and make sum. So, for now, unnecessary
560 * synchronization is not implemented. (just implemented for cpu hotplug)
561 *
562 * If there are kernel internal actions which can make use of some not-exact
563 * value, and reading all cpu value can be performance bottleneck in some
564 * common workload, threashold and synchonization as vmstat[] should be
565 * implemented.
566 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700567static long mem_cgroup_read_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700568 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800569{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700570 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800571 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800572
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700573 get_online_cpus();
574 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700575 val += per_cpu(memcg->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700576#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700577 spin_lock(&memcg->pcp_counter_lock);
578 val += memcg->nocpu_base.count[idx];
579 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700580#endif
581 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800582 return val;
583}
584
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700585static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700586 bool charge)
587{
588 int val = (charge) ? 1 : -1;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700589 this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAPOUT], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700590}
591
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700592void mem_cgroup_pgfault(struct mem_cgroup *memcg, int val)
Ying Han456f9982011-05-26 16:25:38 -0700593{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700594 this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT], val);
Ying Han456f9982011-05-26 16:25:38 -0700595}
596
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700597void mem_cgroup_pgmajfault(struct mem_cgroup *memcg, int val)
Ying Han456f9982011-05-26 16:25:38 -0700598{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700599 this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT], val);
Ying Han456f9982011-05-26 16:25:38 -0700600}
601
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700602static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg,
Johannes Weinere9f89742011-03-23 16:42:37 -0700603 enum mem_cgroup_events_index idx)
604{
605 unsigned long val = 0;
606 int cpu;
607
608 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700609 val += per_cpu(memcg->stat->events[idx], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -0700610#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700611 spin_lock(&memcg->pcp_counter_lock);
612 val += memcg->nocpu_base.events[idx];
613 spin_unlock(&memcg->pcp_counter_lock);
Johannes Weinere9f89742011-03-23 16:42:37 -0700614#endif
615 return val;
616}
617
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700618static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800619 bool file, int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800620{
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800621 preempt_disable();
622
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800623 if (file)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700624 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE],
625 nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800626 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700627 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS],
628 nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700629
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800630 /* pagein of a big page is an event. So, ignore page size */
631 if (nr_pages > 0)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700632 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800633 else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700634 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800635 nr_pages = -nr_pages; /* for event */
636 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800637
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700638 __this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_COUNT], nr_pages);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800639
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800640 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800641}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800642
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700643unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700644mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg, int nid, int zid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700645 unsigned int lru_mask)
Ying Han889976d2011-05-26 16:25:33 -0700646{
647 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700648 enum lru_list l;
649 unsigned long ret = 0;
650
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700651 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700652
653 for_each_lru(l) {
654 if (BIT(l) & lru_mask)
655 ret += MEM_CGROUP_ZSTAT(mz, l);
656 }
657 return ret;
658}
659
660static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700661mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700662 int nid, unsigned int lru_mask)
663{
Ying Han889976d2011-05-26 16:25:33 -0700664 u64 total = 0;
665 int zid;
666
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700667 for (zid = 0; zid < MAX_NR_ZONES; zid++)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700668 total += mem_cgroup_zone_nr_lru_pages(memcg,
669 nid, zid, lru_mask);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700670
Ying Han889976d2011-05-26 16:25:33 -0700671 return total;
672}
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700673
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700674static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700675 unsigned int lru_mask)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800676{
Ying Han889976d2011-05-26 16:25:33 -0700677 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800678 u64 total = 0;
679
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700680 for_each_node_state(nid, N_HIGH_MEMORY)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700681 total += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800682 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800683}
684
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700685static bool __memcg_event_check(struct mem_cgroup *memcg, int target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800686{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700687 unsigned long val, next;
688
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 Takahashif8d66542009-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 Takahashif8d66542009-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 Takahashif8d66542009-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));
Johannes Weinera61ed3c2011-11-02 13:38:29 -0700996 /*
997 * putback: charge:
998 * SetPageLRU SetPageCgroupUsed
999 * smp_mb smp_mb
1000 * PageCgroupUsed && add to memcg LRU PageLRU && add to memcg LRU
1001 *
1002 * Ensure that one of the two sides adds the page to the memcg
1003 * LRU during a race.
1004 */
1005 smp_mb();
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001006 if (!PageCgroupUsed(pc))
1007 return;
Johannes Weiner713735b2011-01-20 14:44:31 -08001008 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
1009 smp_rmb();
Johannes Weiner97a6c372011-03-23 16:42:27 -07001010 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08001011 /* huge page split is done under lru_lock. so, we have no races. */
1012 MEM_CGROUP_ZSTAT(mz, lru) += 1 << compound_order(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -07001013 SetPageCgroupAcctLRU(pc);
1014 if (mem_cgroup_is_root(pc->mem_cgroup))
1015 return;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001016 list_add(&pc->lru, &mz->lists[lru]);
1017}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001018
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001019/*
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001020 * At handling SwapCache and other FUSE stuff, pc->mem_cgroup may be changed
1021 * while it's linked to lru because the page may be reused after it's fully
1022 * uncharged. To handle that, unlink page_cgroup from LRU when charge it again.
1023 * It's done under lock_page and expected that zone->lru_lock isnever held.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001024 */
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001025static void mem_cgroup_lru_del_before_commit(struct page *page)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001026{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001027 unsigned long flags;
1028 struct zone *zone = page_zone(page);
1029 struct page_cgroup *pc = lookup_page_cgroup(page);
1030
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001031 /*
1032 * Doing this check without taking ->lru_lock seems wrong but this
1033 * is safe. Because if page_cgroup's USED bit is unset, the page
1034 * will not be added to any memcg's LRU. If page_cgroup's USED bit is
1035 * set, the commit after this will fail, anyway.
1036 * This all charge/uncharge is done under some mutual execustion.
1037 * So, we don't need to taking care of changes in USED bit.
1038 */
1039 if (likely(!PageLRU(page)))
1040 return;
1041
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001042 spin_lock_irqsave(&zone->lru_lock, flags);
1043 /*
1044 * Forget old LRU when this page_cgroup is *not* used. This Used bit
1045 * is guarded by lock_page() because the page is SwapCache.
1046 */
1047 if (!PageCgroupUsed(pc))
1048 mem_cgroup_del_lru_list(page, page_lru(page));
1049 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001050}
1051
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001052static void mem_cgroup_lru_add_after_commit(struct page *page)
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001053{
1054 unsigned long flags;
1055 struct zone *zone = page_zone(page);
1056 struct page_cgroup *pc = lookup_page_cgroup(page);
Johannes Weinera61ed3c2011-11-02 13:38:29 -07001057 /*
1058 * putback: charge:
1059 * SetPageLRU SetPageCgroupUsed
1060 * smp_mb smp_mb
1061 * PageCgroupUsed && add to memcg LRU PageLRU && add to memcg LRU
1062 *
1063 * Ensure that one of the two sides adds the page to the memcg
1064 * LRU during a race.
1065 */
1066 smp_mb();
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07001067 /* taking care of that the page is added to LRU while we commit it */
1068 if (likely(!PageLRU(page)))
1069 return;
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001070 spin_lock_irqsave(&zone->lru_lock, flags);
1071 /* link when the page is linked to LRU but page_cgroup isn't */
Balbir Singh4b3bde42009-09-23 15:56:32 -07001072 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001073 mem_cgroup_add_lru_list(page, page_lru(page));
1074 spin_unlock_irqrestore(&zone->lru_lock, flags);
1075}
1076
1077
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001078void mem_cgroup_move_lists(struct page *page,
1079 enum lru_list from, enum lru_list to)
1080{
Hirokazu Takahashif8d66542009-01-07 18:08:02 -08001081 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001082 return;
1083 mem_cgroup_del_lru_list(page, from);
1084 mem_cgroup_add_lru_list(page, to);
1085}
1086
Michal Hocko3e920412011-07-26 16:08:29 -07001087/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001088 * Checks whether given mem is same or in the root_mem_cgroup's
Michal Hocko3e920412011-07-26 16:08:29 -07001089 * hierarchy subtree
1090 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001091static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1092 struct mem_cgroup *memcg)
Michal Hocko3e920412011-07-26 16:08:29 -07001093{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001094 if (root_memcg != memcg) {
1095 return (root_memcg->use_hierarchy &&
1096 css_is_ancestor(&memcg->css, &root_memcg->css));
Michal Hocko3e920412011-07-26 16:08:29 -07001097 }
1098
1099 return true;
1100}
1101
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001102int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -08001103{
1104 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001105 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001106 struct task_struct *p;
David Rientjes4c4a2212008-02-07 00:14:06 -08001107
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001108 p = find_lock_task_mm(task);
1109 if (!p)
1110 return 0;
1111 curr = try_get_mem_cgroup_from_mm(p->mm);
1112 task_unlock(p);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001113 if (!curr)
1114 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001115 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001116 * We should check use_hierarchy of "memcg" not "curr". Because checking
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001117 * use_hierarchy of "curr" here make this function true if hierarchy is
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001118 * enabled in "curr" and "curr" is a child of "memcg" in *cgroup*
1119 * hierarchy(even if use_hierarchy is disabled in "memcg").
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001120 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001121 ret = mem_cgroup_same_or_subtree(memcg, curr);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001122 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001123 return ret;
1124}
1125
Johannes Weiner9b272972011-11-02 13:38:23 -07001126int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg, struct zone *zone)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001127{
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001128 unsigned long inactive_ratio;
Johannes Weiner9b272972011-11-02 13:38:23 -07001129 int nid = zone_to_nid(zone);
1130 int zid = zone_idx(zone);
1131 unsigned long inactive;
1132 unsigned long active;
1133 unsigned long gb;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001134
Johannes Weiner9b272972011-11-02 13:38:23 -07001135 inactive = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid,
1136 BIT(LRU_INACTIVE_ANON));
1137 active = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid,
1138 BIT(LRU_ACTIVE_ANON));
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001139
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001140 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1141 if (gb)
1142 inactive_ratio = int_sqrt(10 * gb);
1143 else
1144 inactive_ratio = 1;
1145
Johannes Weiner9b272972011-11-02 13:38:23 -07001146 return inactive * inactive_ratio < active;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001147}
1148
Johannes Weiner9b272972011-11-02 13:38:23 -07001149int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg, struct zone *zone)
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001150{
1151 unsigned long active;
1152 unsigned long inactive;
Johannes Weiner9b272972011-11-02 13:38:23 -07001153 int zid = zone_idx(zone);
1154 int nid = zone_to_nid(zone);
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001155
Johannes Weiner9b272972011-11-02 13:38:23 -07001156 inactive = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid,
1157 BIT(LRU_INACTIVE_FILE));
1158 active = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid,
1159 BIT(LRU_ACTIVE_FILE));
Rik van Riel56e49d22009-06-16 15:32:28 -07001160
1161 return (active > inactive);
1162}
1163
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001164struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
1165 struct zone *zone)
1166{
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -07001167 int nid = zone_to_nid(zone);
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001168 int zid = zone_idx(zone);
1169 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
1170
1171 return &mz->reclaim_stat;
1172}
1173
1174struct zone_reclaim_stat *
1175mem_cgroup_get_reclaim_stat_from_page(struct page *page)
1176{
1177 struct page_cgroup *pc;
1178 struct mem_cgroup_per_zone *mz;
1179
1180 if (mem_cgroup_disabled())
1181 return NULL;
1182
1183 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -08001184 if (!PageCgroupUsed(pc))
1185 return NULL;
Johannes Weiner713735b2011-01-20 14:44:31 -08001186 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
1187 smp_rmb();
Johannes Weiner97a6c372011-03-23 16:42:27 -07001188 mz = page_cgroup_zoneinfo(pc->mem_cgroup, page);
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -08001189 return &mz->reclaim_stat;
1190}
1191
Balbir Singh66e17072008-02-07 00:13:56 -08001192unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
1193 struct list_head *dst,
1194 unsigned long *scanned, int order,
Minchan Kim4356f212011-10-31 17:06:47 -07001195 isolate_mode_t mode,
1196 struct zone *z,
Balbir Singh66e17072008-02-07 00:13:56 -08001197 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001198 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -08001199{
1200 unsigned long nr_taken = 0;
1201 struct page *page;
1202 unsigned long scan;
1203 LIST_HEAD(pc_list);
1204 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001205 struct page_cgroup *pc, *tmp;
KOSAKI Motohiro13d7e3a2010-08-10 18:03:06 -07001206 int nid = zone_to_nid(z);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001207 int zid = zone_idx(z);
1208 struct mem_cgroup_per_zone *mz;
Johannes Weinerb7c46d12009-09-21 17:02:56 -07001209 int lru = LRU_FILE * file + active;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001210 int ret;
Balbir Singh66e17072008-02-07 00:13:56 -08001211
Balbir Singhcf475ad2008-04-29 01:00:16 -07001212 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001213 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001214 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -08001215
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001216 scan = 0;
1217 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -08001218 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001219 break;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001220
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001221 if (unlikely(!PageCgroupUsed(pc)))
1222 continue;
Johannes Weiner5564e882011-03-23 16:42:29 -07001223
Johannes Weiner6b3ae582011-03-23 16:42:30 -07001224 page = lookup_cgroup_page(pc);
Johannes Weiner5564e882011-03-23 16:42:29 -07001225
Hugh Dickins436c65412008-02-07 00:14:12 -08001226 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001227 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001228
Hugh Dickins436c65412008-02-07 00:14:12 -08001229 scan++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001230 ret = __isolate_lru_page(page, mode, file);
1231 switch (ret) {
1232 case 0:
Balbir Singh66e17072008-02-07 00:13:56 -08001233 list_move(&page->lru, dst);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001234 mem_cgroup_del_lru(page);
Rik van Riel2c888cf2011-01-13 15:47:13 -08001235 nr_taken += hpage_nr_pages(page);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001236 break;
1237 case -EBUSY:
1238 /* we don't affect global LRU but rotate in our LRU */
1239 mem_cgroup_rotate_lru_list(page, page_lru(page));
1240 break;
1241 default:
1242 break;
Balbir Singh66e17072008-02-07 00:13:56 -08001243 }
1244 }
1245
Balbir Singh66e17072008-02-07 00:13:56 -08001246 *scanned = scan;
KOSAKI Motohirocc8e9702010-08-09 17:19:57 -07001247
1248 trace_mm_vmscan_memcg_isolate(0, nr_to_scan, scan, nr_taken,
1249 0, 0, 0, mode);
1250
Balbir Singh66e17072008-02-07 00:13:56 -08001251 return nr_taken;
1252}
1253
Balbir Singh6d61ef42009-01-07 18:08:06 -08001254#define mem_cgroup_from_res_counter(counter, member) \
1255 container_of(counter, struct mem_cgroup, member)
1256
Johannes Weiner19942822011-02-01 15:52:43 -08001257/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001258 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
1259 * @mem: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001260 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001261 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001262 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001263 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001264static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001265{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001266 unsigned long long margin;
1267
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001268 margin = res_counter_margin(&memcg->res);
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001269 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001270 margin = min(margin, res_counter_margin(&memcg->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001271 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001272}
1273
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001274int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001275{
1276 struct cgroup *cgrp = memcg->css.cgroup;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001277
1278 /* root ? */
1279 if (cgrp->parent == NULL)
1280 return vm_swappiness;
1281
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001282 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001283}
1284
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001285static void mem_cgroup_start_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001286{
1287 int cpu;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001288
1289 get_online_cpus();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001290 spin_lock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001291 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001292 per_cpu(memcg->stat->count[MEM_CGROUP_ON_MOVE], cpu) += 1;
1293 memcg->nocpu_base.count[MEM_CGROUP_ON_MOVE] += 1;
1294 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001295 put_online_cpus();
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001296
1297 synchronize_rcu();
1298}
1299
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001300static void mem_cgroup_end_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001301{
1302 int cpu;
1303
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001304 if (!memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001305 return;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001306 get_online_cpus();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001307 spin_lock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001308 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001309 per_cpu(memcg->stat->count[MEM_CGROUP_ON_MOVE], cpu) -= 1;
1310 memcg->nocpu_base.count[MEM_CGROUP_ON_MOVE] -= 1;
1311 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07001312 put_online_cpus();
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001313}
1314/*
1315 * 2 routines for checking "mem" is under move_account() or not.
1316 *
1317 * mem_cgroup_stealed() - checking a cgroup is mc.from or not. This is used
1318 * for avoiding race in accounting. If true,
1319 * pc->mem_cgroup may be overwritten.
1320 *
1321 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1322 * under hierarchy of moving cgroups. This is for
1323 * waiting at hith-memory prressure caused by "move".
1324 */
1325
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001326static bool mem_cgroup_stealed(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001327{
1328 VM_BUG_ON(!rcu_read_lock_held());
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001329 return this_cpu_read(memcg->stat->count[MEM_CGROUP_ON_MOVE]) > 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001330}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001331
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001332static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001333{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001334 struct mem_cgroup *from;
1335 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001336 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001337 /*
1338 * Unlike task_move routines, we access mc.to, mc.from not under
1339 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1340 */
1341 spin_lock(&mc.lock);
1342 from = mc.from;
1343 to = mc.to;
1344 if (!from)
1345 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001346
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001347 ret = mem_cgroup_same_or_subtree(memcg, from)
1348 || mem_cgroup_same_or_subtree(memcg, to);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001349unlock:
1350 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001351 return ret;
1352}
1353
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001354static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001355{
1356 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001357 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001358 DEFINE_WAIT(wait);
1359 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1360 /* moving charge context might have finished. */
1361 if (mc.moving_task)
1362 schedule();
1363 finish_wait(&mc.waitq, &wait);
1364 return true;
1365 }
1366 }
1367 return false;
1368}
1369
Balbir Singhe2224322009-04-02 16:57:39 -07001370/**
Kirill A. Shutemov6a6135b2010-03-10 15:22:25 -08001371 * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode.
Balbir Singhe2224322009-04-02 16:57:39 -07001372 * @memcg: The memory cgroup that went over limit
1373 * @p: Task that is going to be killed
1374 *
1375 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1376 * enabled
1377 */
1378void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1379{
1380 struct cgroup *task_cgrp;
1381 struct cgroup *mem_cgrp;
1382 /*
1383 * Need a buffer in BSS, can't rely on allocations. The code relies
1384 * on the assumption that OOM is serialized for memory controller.
1385 * If this assumption is broken, revisit this code.
1386 */
1387 static char memcg_name[PATH_MAX];
1388 int ret;
1389
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001390 if (!memcg || !p)
Balbir Singhe2224322009-04-02 16:57:39 -07001391 return;
1392
1393
1394 rcu_read_lock();
1395
1396 mem_cgrp = memcg->css.cgroup;
1397 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1398
1399 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1400 if (ret < 0) {
1401 /*
1402 * Unfortunately, we are unable to convert to a useful name
1403 * But we'll still print out the usage information
1404 */
1405 rcu_read_unlock();
1406 goto done;
1407 }
1408 rcu_read_unlock();
1409
1410 printk(KERN_INFO "Task in %s killed", memcg_name);
1411
1412 rcu_read_lock();
1413 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1414 if (ret < 0) {
1415 rcu_read_unlock();
1416 goto done;
1417 }
1418 rcu_read_unlock();
1419
1420 /*
1421 * Continues from above, so we don't need an KERN_ level
1422 */
1423 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1424done:
1425
1426 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1427 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1428 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1429 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1430 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1431 "failcnt %llu\n",
1432 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1433 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1434 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1435}
1436
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001437/*
1438 * This function returns the number of memcg under hierarchy tree. Returns
1439 * 1(self count) if no children.
1440 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001441static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001442{
1443 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001444 struct mem_cgroup *iter;
1445
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001446 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001447 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001448 return num;
1449}
1450
Balbir Singh6d61ef42009-01-07 18:08:06 -08001451/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001452 * Return the memory (and swap, if configured) limit for a memcg.
1453 */
1454u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
1455{
1456 u64 limit;
1457 u64 memsw;
1458
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001459 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
1460 limit += total_swap_pages << PAGE_SHIFT;
1461
David Rientjesa63d83f2010-08-09 17:19:46 -07001462 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1463 /*
1464 * If memsw is finite and limits the amount of swap space available
1465 * to this memcg, return that limit.
1466 */
1467 return min(limit, memsw);
1468}
1469
1470/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001471 * Visit the first child (need not be the first child as per the ordering
1472 * of the cgroup list, since we track last_scanned_child) of @mem and use
1473 * that to reclaim free pages from.
1474 */
1475static struct mem_cgroup *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001476mem_cgroup_select_victim(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001477{
1478 struct mem_cgroup *ret = NULL;
1479 struct cgroup_subsys_state *css;
1480 int nextid, found;
1481
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001482 if (!root_memcg->use_hierarchy) {
1483 css_get(&root_memcg->css);
1484 ret = root_memcg;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001485 }
1486
1487 while (!ret) {
1488 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001489 nextid = root_memcg->last_scanned_child + 1;
1490 css = css_get_next(&mem_cgroup_subsys, nextid, &root_memcg->css,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001491 &found);
1492 if (css && css_tryget(css))
1493 ret = container_of(css, struct mem_cgroup, css);
1494
1495 rcu_read_unlock();
1496 /* Updates scanning parameter */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001497 if (!css) {
1498 /* this means start scan from ID:1 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001499 root_memcg->last_scanned_child = 0;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001500 } else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001501 root_memcg->last_scanned_child = found;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001502 }
1503
1504 return ret;
1505}
1506
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001507/**
1508 * test_mem_cgroup_node_reclaimable
1509 * @mem: the target memcg
1510 * @nid: the node ID to be checked.
1511 * @noswap : specify true here if the user wants flle only information.
1512 *
1513 * This function returns whether the specified memcg contains any
1514 * reclaimable pages on a node. Returns true if there are any reclaimable
1515 * pages in the node.
1516 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001517static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001518 int nid, bool noswap)
1519{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001520 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001521 return true;
1522 if (noswap || !total_swap_pages)
1523 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001524 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001525 return true;
1526 return false;
1527
1528}
Ying Han889976d2011-05-26 16:25:33 -07001529#if MAX_NUMNODES > 1
1530
1531/*
1532 * Always updating the nodemask is not very good - even if we have an empty
1533 * list or the wrong list here, we can start from some node and traverse all
1534 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1535 *
1536 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001537static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001538{
1539 int nid;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001540 /*
1541 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1542 * pagein/pageout changes since the last update.
1543 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001544 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001545 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001546 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001547 return;
1548
Ying Han889976d2011-05-26 16:25:33 -07001549 /* make a nodemask where this memcg uses memory from */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001550 memcg->scan_nodes = node_states[N_HIGH_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001551
1552 for_each_node_mask(nid, node_states[N_HIGH_MEMORY]) {
1553
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001554 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1555 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001556 }
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001557
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001558 atomic_set(&memcg->numainfo_events, 0);
1559 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001560}
1561
1562/*
1563 * Selecting a node where we start reclaim from. Because what we need is just
1564 * reducing usage counter, start from anywhere is O,K. Considering
1565 * memory reclaim from current node, there are pros. and cons.
1566 *
1567 * Freeing memory from current node means freeing memory from a node which
1568 * we'll use or we've used. So, it may make LRU bad. And if several threads
1569 * hit limits, it will see a contention on a node. But freeing from remote
1570 * node means more costs for memory reclaim because of memory latency.
1571 *
1572 * Now, we use round-robin. Better algorithm is welcomed.
1573 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001574int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001575{
1576 int node;
1577
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001578 mem_cgroup_may_update_nodemask(memcg);
1579 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001580
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001581 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001582 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001583 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001584 /*
1585 * We call this when we hit limit, not when pages are added to LRU.
1586 * No LRU may hold pages because all pages are UNEVICTABLE or
1587 * memcg is too small and all pages are not on LRU. In that case,
1588 * we use curret node.
1589 */
1590 if (unlikely(node == MAX_NUMNODES))
1591 node = numa_node_id();
1592
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001593 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001594 return node;
1595}
1596
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001597/*
1598 * Check all nodes whether it contains reclaimable pages or not.
1599 * For quick scan, we make use of scan_nodes. This will allow us to skip
1600 * unused nodes. But scan_nodes is lazily updated and may not cotain
1601 * enough new information. We need to do double check.
1602 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001603bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001604{
1605 int nid;
1606
1607 /*
1608 * quick check...making use of scan_node.
1609 * We can skip unused nodes.
1610 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001611 if (!nodes_empty(memcg->scan_nodes)) {
1612 for (nid = first_node(memcg->scan_nodes);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001613 nid < MAX_NUMNODES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001614 nid = next_node(nid, memcg->scan_nodes)) {
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001615
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001616 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001617 return true;
1618 }
1619 }
1620 /*
1621 * Check rest of nodes.
1622 */
1623 for_each_node_state(nid, N_HIGH_MEMORY) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001624 if (node_isset(nid, memcg->scan_nodes))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001625 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001626 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001627 return true;
1628 }
1629 return false;
1630}
1631
Ying Han889976d2011-05-26 16:25:33 -07001632#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001633int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001634{
1635 return 0;
1636}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001637
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001638bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001639{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001640 return test_mem_cgroup_node_reclaimable(memcg, 0, noswap);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001641}
Ying Han889976d2011-05-26 16:25:33 -07001642#endif
1643
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001644/*
1645 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1646 * we reclaimed from, so that we don't end up penalizing one child extensively
1647 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001648 *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001649 * root_memcg is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001650 *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001651 * We give up and return to the caller when we visit root_memcg twice.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001652 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001653 *
1654 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001655 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001656static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_memcg,
Balbir Singh4e416952009-09-23 15:56:39 -07001657 struct zone *zone,
Balbir Singh75822b42009-09-23 15:56:38 -07001658 gfp_t gfp_mask,
Ying Han0ae5e892011-05-26 16:25:25 -07001659 unsigned long reclaim_options,
1660 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001661{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001662 struct mem_cgroup *victim;
1663 int ret, total = 0;
1664 int loop = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001665 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1666 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
Balbir Singh4e416952009-09-23 15:56:39 -07001667 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001668 unsigned long excess;
Johannes Weiner185efc02011-09-14 16:21:58 -07001669 unsigned long nr_scanned;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001670
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001671 excess = res_counter_soft_limit_excess(&root_memcg->res) >> PAGE_SHIFT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001672
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001673 /* If memsw_is_minimum==1, swap-out is of-no-use. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001674 if (!check_soft && !shrink && root_memcg->memsw_is_minimum)
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001675 noswap = true;
1676
Balbir Singh4e416952009-09-23 15:56:39 -07001677 while (1) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001678 victim = mem_cgroup_select_victim(root_memcg);
1679 if (victim == root_memcg) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001680 loop++;
KAMEZAWA Hiroyukifbc29a22011-06-15 15:08:46 -07001681 /*
1682 * We are not draining per cpu cached charges during
1683 * soft limit reclaim because global reclaim doesn't
1684 * care about charges. It tries to free some memory and
1685 * charges will not give any.
1686 */
1687 if (!check_soft && loop >= 1)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001688 drain_all_stock_async(root_memcg);
Balbir Singh4e416952009-09-23 15:56:39 -07001689 if (loop >= 2) {
1690 /*
1691 * If we have not been able to reclaim
1692 * anything, it might because there are
1693 * no reclaimable pages under this hierarchy
1694 */
1695 if (!check_soft || !total) {
1696 css_put(&victim->css);
1697 break;
1698 }
1699 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001700 * We want to do more targeted reclaim.
Balbir Singh4e416952009-09-23 15:56:39 -07001701 * excess >> 2 is not to excessive so as to
1702 * reclaim too much, nor too less that we keep
1703 * coming back to reclaim from this cgroup
1704 */
1705 if (total >= (excess >> 2) ||
1706 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1707 css_put(&victim->css);
1708 break;
1709 }
1710 }
1711 }
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001712 if (!mem_cgroup_reclaimable(victim, noswap)) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001713 /* this cgroup's local usage == 0 */
1714 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001715 continue;
1716 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001717 /* we use swappiness of local cgroup */
Ying Han0ae5e892011-05-26 16:25:25 -07001718 if (check_soft) {
Balbir Singh4e416952009-09-23 15:56:39 -07001719 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
Johannes Weiner185efc02011-09-14 16:21:58 -07001720 noswap, zone, &nr_scanned);
1721 *total_scanned += nr_scanned;
Ying Han0ae5e892011-05-26 16:25:25 -07001722 } else
Balbir Singh4e416952009-09-23 15:56:39 -07001723 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
Johannes Weiner185efc02011-09-14 16:21:58 -07001724 noswap);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001725 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001726 /*
1727 * At shrinking usage, we can't check we should stop here or
1728 * reclaim more. It's depends on callers. last_scanned_child
1729 * will work enough for keeping fairness under tree.
1730 */
1731 if (shrink)
1732 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001733 total += ret;
Balbir Singh4e416952009-09-23 15:56:39 -07001734 if (check_soft) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001735 if (!res_counter_soft_limit_excess(&root_memcg->res))
Balbir Singh4e416952009-09-23 15:56:39 -07001736 return total;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001737 } else if (mem_cgroup_margin(root_memcg))
Johannes Weiner4fd14eb2011-05-26 16:25:35 -07001738 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001739 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001740 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001741}
1742
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001743/*
1744 * Check OOM-Killer is already running under our hierarchy.
1745 * If someone is running, return false.
Michal Hocko1af8efe2011-07-26 16:08:24 -07001746 * Has to be called with memcg_oom_lock
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001747 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001748static bool mem_cgroup_oom_lock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001749{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001750 struct mem_cgroup *iter, *failed = NULL;
1751 bool cond = true;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001752
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001753 for_each_mem_cgroup_tree_cond(iter, memcg, cond) {
Johannes Weiner23751be2011-08-25 15:59:16 -07001754 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001755 /*
1756 * this subtree of our hierarchy is already locked
1757 * so we cannot give a lock.
1758 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07001759 failed = iter;
1760 cond = false;
Johannes Weiner23751be2011-08-25 15:59:16 -07001761 } else
1762 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001763 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001764
Michal Hocko79dfdac2011-07-26 16:08:23 -07001765 if (!failed)
Johannes Weiner23751be2011-08-25 15:59:16 -07001766 return true;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001767
1768 /*
1769 * OK, we failed to lock the whole subtree so we have to clean up
1770 * what we set up to the failing subtree
1771 */
1772 cond = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001773 for_each_mem_cgroup_tree_cond(iter, memcg, cond) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001774 if (iter == failed) {
1775 cond = false;
1776 continue;
1777 }
1778 iter->oom_lock = false;
1779 }
Johannes Weiner23751be2011-08-25 15:59:16 -07001780 return false;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001781}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001782
Michal Hocko79dfdac2011-07-26 16:08:23 -07001783/*
Michal Hocko1af8efe2011-07-26 16:08:24 -07001784 * Has to be called with memcg_oom_lock
Michal Hocko79dfdac2011-07-26 16:08:23 -07001785 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001786static int mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001787{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001788 struct mem_cgroup *iter;
1789
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001790 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001791 iter->oom_lock = false;
1792 return 0;
1793}
1794
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001795static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001796{
1797 struct mem_cgroup *iter;
1798
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001799 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001800 atomic_inc(&iter->under_oom);
1801}
1802
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001803static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001804{
1805 struct mem_cgroup *iter;
1806
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001807 /*
1808 * When a new child is created while the hierarchy is under oom,
1809 * mem_cgroup_oom_lock() may not be called. We have to use
1810 * atomic_add_unless() here.
1811 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001812 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001813 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001814}
1815
Michal Hocko1af8efe2011-07-26 16:08:24 -07001816static DEFINE_SPINLOCK(memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001817static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1818
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001819struct oom_wait_info {
1820 struct mem_cgroup *mem;
1821 wait_queue_t wait;
1822};
1823
1824static int memcg_oom_wake_function(wait_queue_t *wait,
1825 unsigned mode, int sync, void *arg)
1826{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001827 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg,
1828 *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001829 struct oom_wait_info *oom_wait_info;
1830
1831 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001832 oom_wait_memcg = oom_wait_info->mem;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001833
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001834 /*
1835 * Both of oom_wait_info->mem and wake_mem are stable under us.
1836 * Then we can use css_is_ancestor without taking care of RCU.
1837 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001838 if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg)
1839 && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001840 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001841 return autoremove_wake_function(wait, mode, sync, arg);
1842}
1843
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001844static void memcg_wakeup_oom(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001845{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001846 /* for filtering, pass "memcg" as argument. */
1847 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001848}
1849
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001850static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001851{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001852 if (memcg && atomic_read(&memcg->under_oom))
1853 memcg_wakeup_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001854}
1855
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001856/*
1857 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
1858 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001859bool mem_cgroup_handle_oom(struct mem_cgroup *memcg, gfp_t mask)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001860{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001861 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001862 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001863
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001864 owait.mem = memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001865 owait.wait.flags = 0;
1866 owait.wait.func = memcg_oom_wake_function;
1867 owait.wait.private = current;
1868 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001869 need_to_kill = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001870 mem_cgroup_mark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001871
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001872 /* At first, try to OOM lock hierarchy under memcg.*/
Michal Hocko1af8efe2011-07-26 16:08:24 -07001873 spin_lock(&memcg_oom_lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001874 locked = mem_cgroup_oom_lock(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001875 /*
1876 * Even if signal_pending(), we can't quit charge() loop without
1877 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
1878 * under OOM is always welcomed, use TASK_KILLABLE here.
1879 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001880 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001881 if (!locked || memcg->oom_kill_disable)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001882 need_to_kill = false;
1883 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001884 mem_cgroup_oom_notify(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07001885 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001886
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001887 if (need_to_kill) {
1888 finish_wait(&memcg_oom_waitq, &owait.wait);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001889 mem_cgroup_out_of_memory(memcg, mask);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001890 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001891 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001892 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001893 }
Michal Hocko1af8efe2011-07-26 16:08:24 -07001894 spin_lock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001895 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001896 mem_cgroup_oom_unlock(memcg);
1897 memcg_wakeup_oom(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07001898 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001899
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001900 mem_cgroup_unmark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001901
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001902 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
1903 return false;
1904 /* Give chance to dying process */
KAMEZAWA Hiroyuki715a5ee2011-11-02 13:38:18 -07001905 schedule_timeout_uninterruptible(1);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001906 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001907}
1908
Balbir Singhd69b0422009-06-17 16:26:34 -07001909/*
1910 * Currently used to update mapped file statistics, but the routine can be
1911 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001912 *
1913 * Notes: Race condition
1914 *
1915 * We usually use page_cgroup_lock() for accessing page_cgroup member but
1916 * it tends to be costly. But considering some conditions, we doesn't need
1917 * to do so _always_.
1918 *
1919 * Considering "charge", lock_page_cgroup() is not required because all
1920 * file-stat operations happen after a page is attached to radix-tree. There
1921 * are no race with "charge".
1922 *
1923 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
1924 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
1925 * if there are race with "uncharge". Statistics itself is properly handled
1926 * by flags.
1927 *
1928 * Considering "move", this is an only case we see a race. To make the race
1929 * small, we check MEM_CGROUP_ON_MOVE percpu value and detect there are
1930 * possibility of race condition. If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07001931 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001932
Greg Thelen2a7106f2011-01-13 15:47:37 -08001933void mem_cgroup_update_page_stat(struct page *page,
1934 enum mem_cgroup_page_stat_item idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07001935{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001936 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001937 struct page_cgroup *pc = lookup_page_cgroup(page);
1938 bool need_unlock = false;
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08001939 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07001940
Balbir Singhd69b0422009-06-17 16:26:34 -07001941 if (unlikely(!pc))
1942 return;
1943
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001944 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001945 memcg = pc->mem_cgroup;
1946 if (unlikely(!memcg || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001947 goto out;
1948 /* pc->mem_cgroup is unstable ? */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001949 if (unlikely(mem_cgroup_stealed(memcg)) || PageTransHuge(page)) {
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001950 /* take a lock against to access pc->mem_cgroup */
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08001951 move_lock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001952 need_unlock = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001953 memcg = pc->mem_cgroup;
1954 if (!memcg || !PageCgroupUsed(pc))
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001955 goto out;
1956 }
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001957
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001958 switch (idx) {
Greg Thelen2a7106f2011-01-13 15:47:37 -08001959 case MEMCG_NR_FILE_MAPPED:
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001960 if (val > 0)
1961 SetPageCgroupFileMapped(pc);
1962 else if (!page_mapped(page))
KAMEZAWA Hiroyuki0c270f82010-10-27 15:33:39 -07001963 ClearPageCgroupFileMapped(pc);
Greg Thelen2a7106f2011-01-13 15:47:37 -08001964 idx = MEM_CGROUP_STAT_FILE_MAPPED;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001965 break;
1966 default:
1967 BUG();
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07001968 }
Balbir Singhd69b0422009-06-17 16:26:34 -07001969
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001970 this_cpu_add(memcg->stat->count[idx], val);
Greg Thelen2a7106f2011-01-13 15:47:37 -08001971
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001972out:
1973 if (unlikely(need_unlock))
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08001974 move_unlock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001975 rcu_read_unlock();
1976 return;
Balbir Singhd69b0422009-06-17 16:26:34 -07001977}
Greg Thelen2a7106f2011-01-13 15:47:37 -08001978EXPORT_SYMBOL(mem_cgroup_update_page_stat);
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001979
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001980/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001981 * size of first charge trial. "32" comes from vmscan.c's magic value.
1982 * TODO: maybe necessary to use big numbers in big irons.
1983 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001984#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001985struct memcg_stock_pcp {
1986 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07001987 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001988 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07001989 unsigned long flags;
1990#define FLUSHING_CACHED_CHARGE (0)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001991};
1992static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02001993static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001994
1995/*
Johannes Weiner11c9ea42011-03-23 16:42:34 -07001996 * Try to consume stocked charge on this cpu. If success, one page is consumed
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001997 * from local stock and true is returned. If the stock is 0 or charges from a
1998 * cgroup which is not current target, returns false. This stock will be
1999 * refilled.
2000 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002001static bool consume_stock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002002{
2003 struct memcg_stock_pcp *stock;
2004 bool ret = true;
2005
2006 stock = &get_cpu_var(memcg_stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002007 if (memcg == stock->cached && stock->nr_pages)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002008 stock->nr_pages--;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002009 else /* need to call res_counter_charge */
2010 ret = false;
2011 put_cpu_var(memcg_stock);
2012 return ret;
2013}
2014
2015/*
2016 * Returns stocks cached in percpu to res_counter and reset cached information.
2017 */
2018static void drain_stock(struct memcg_stock_pcp *stock)
2019{
2020 struct mem_cgroup *old = stock->cached;
2021
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002022 if (stock->nr_pages) {
2023 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2024
2025 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002026 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002027 res_counter_uncharge(&old->memsw, bytes);
2028 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002029 }
2030 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002031}
2032
2033/*
2034 * This must be called under preempt disabled or must be called by
2035 * a thread which is pinned to local cpu.
2036 */
2037static void drain_local_stock(struct work_struct *dummy)
2038{
2039 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
2040 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002041 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002042}
2043
2044/*
2045 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002046 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002047 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002048static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002049{
2050 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2051
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002052 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002053 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002054 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002055 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002056 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002057 put_cpu_var(memcg_stock);
2058}
2059
2060/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002061 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Michal Hockod38144b2011-07-26 16:08:28 -07002062 * of the hierarchy under it. sync flag says whether we should block
2063 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002064 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002065static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002066{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002067 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002068
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002069 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002070 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002071 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002072 for_each_online_cpu(cpu) {
2073 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002074 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002075
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002076 memcg = stock->cached;
2077 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002078 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002079 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002080 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002081 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2082 if (cpu == curcpu)
2083 drain_local_stock(&stock->work);
2084 else
2085 schedule_work_on(cpu, &stock->work);
2086 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002087 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002088 put_cpu();
Michal Hockod38144b2011-07-26 16:08:28 -07002089
2090 if (!sync)
2091 goto out;
2092
2093 for_each_online_cpu(cpu) {
2094 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002095 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002096 flush_work(&stock->work);
2097 }
2098out:
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002099 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002100}
2101
2102/*
2103 * Tries to drain stocked charges in other cpus. This function is asynchronous
2104 * and just put a work per cpu for draining localy on each cpu. Caller can
2105 * expects some charges will be back to res_counter later but cannot wait for
2106 * it.
2107 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002108static void drain_all_stock_async(struct mem_cgroup *root_memcg)
Michal Hockod38144b2011-07-26 16:08:28 -07002109{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002110 /*
2111 * If someone calls draining, avoid adding more kworker runs.
2112 */
2113 if (!mutex_trylock(&percpu_charge_mutex))
2114 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002115 drain_all_stock(root_memcg, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002116 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002117}
2118
2119/* This is a synchronous drain interface. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002120static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002121{
2122 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002123 mutex_lock(&percpu_charge_mutex);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002124 drain_all_stock(root_memcg, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002125 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002126}
2127
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002128/*
2129 * This function drains percpu counter value from DEAD cpu and
2130 * move it to local cpu. Note that this function can be preempted.
2131 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002132static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002133{
2134 int i;
2135
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002136 spin_lock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002137 for (i = 0; i < MEM_CGROUP_STAT_DATA; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002138 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002139
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002140 per_cpu(memcg->stat->count[i], cpu) = 0;
2141 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002142 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002143 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002144 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002145
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002146 per_cpu(memcg->stat->events[i], cpu) = 0;
2147 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002148 }
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002149 /* need to clear ON_MOVE value, works as a kind of lock. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002150 per_cpu(memcg->stat->count[MEM_CGROUP_ON_MOVE], cpu) = 0;
2151 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002152}
2153
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002154static void synchronize_mem_cgroup_on_move(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002155{
2156 int idx = MEM_CGROUP_ON_MOVE;
2157
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002158 spin_lock(&memcg->pcp_counter_lock);
2159 per_cpu(memcg->stat->count[idx], cpu) = memcg->nocpu_base.count[idx];
2160 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002161}
2162
2163static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002164 unsigned long action,
2165 void *hcpu)
2166{
2167 int cpu = (unsigned long)hcpu;
2168 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002169 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002170
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002171 if ((action == CPU_ONLINE)) {
2172 for_each_mem_cgroup_all(iter)
2173 synchronize_mem_cgroup_on_move(iter, cpu);
2174 return NOTIFY_OK;
2175 }
2176
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002177 if ((action != CPU_DEAD) || action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002178 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002179
2180 for_each_mem_cgroup_all(iter)
2181 mem_cgroup_drain_pcp_counter(iter, cpu);
2182
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002183 stock = &per_cpu(memcg_stock, cpu);
2184 drain_stock(stock);
2185 return NOTIFY_OK;
2186}
2187
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002188
2189/* See __mem_cgroup_try_charge() for details */
2190enum {
2191 CHARGE_OK, /* success */
2192 CHARGE_RETRY, /* need to retry but retry is not bad */
2193 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2194 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
2195 CHARGE_OOM_DIE, /* the current is killed because of OOM */
2196};
2197
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002198static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002199 unsigned int nr_pages, bool oom_check)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002200{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002201 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002202 struct mem_cgroup *mem_over_limit;
2203 struct res_counter *fail_res;
2204 unsigned long flags = 0;
2205 int ret;
2206
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002207 ret = res_counter_charge(&memcg->res, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002208
2209 if (likely(!ret)) {
2210 if (!do_swap_account)
2211 return CHARGE_OK;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002212 ret = res_counter_charge(&memcg->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002213 if (likely(!ret))
2214 return CHARGE_OK;
2215
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002216 res_counter_uncharge(&memcg->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002217 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2218 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2219 } else
2220 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002221 /*
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002222 * nr_pages can be either a huge page (HPAGE_PMD_NR), a batch
2223 * of regular pages (CHARGE_BATCH), or a single regular page (1).
Johannes Weiner9221edb2011-02-01 15:52:42 -08002224 *
2225 * Never reclaim on behalf of optional batching, retry with a
2226 * single page instead.
2227 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002228 if (nr_pages == CHARGE_BATCH)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002229 return CHARGE_RETRY;
2230
2231 if (!(gfp_mask & __GFP_WAIT))
2232 return CHARGE_WOULDBLOCK;
2233
2234 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
Ying Han0ae5e892011-05-26 16:25:25 -07002235 gfp_mask, flags, NULL);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002236 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002237 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002238 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002239 * Even though the limit is exceeded at this point, reclaim
2240 * may have been able to free some pages. Retry the charge
2241 * before killing the task.
2242 *
2243 * Only for regular pages, though: huge pages are rather
2244 * unlikely to succeed so close to the limit, and we fall back
2245 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002246 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002247 if (nr_pages == 1 && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002248 return CHARGE_RETRY;
2249
2250 /*
2251 * At task move, charge accounts can be doubly counted. So, it's
2252 * better to wait until the end of task_move if something is going on.
2253 */
2254 if (mem_cgroup_wait_acct_move(mem_over_limit))
2255 return CHARGE_RETRY;
2256
2257 /* If we don't need to call oom-killer at el, return immediately */
2258 if (!oom_check)
2259 return CHARGE_NOMEM;
2260 /* check OOM */
2261 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask))
2262 return CHARGE_OOM_DIE;
2263
2264 return CHARGE_RETRY;
2265}
2266
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002267/*
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002268 * Unlike exported interface, "oom" parameter is added. if oom==true,
2269 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002270 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002271static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002272 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002273 unsigned int nr_pages,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002274 struct mem_cgroup **ptr,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002275 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002276{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002277 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002278 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002279 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002280 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002281
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002282 /*
2283 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2284 * in system level. So, allow to go ahead dying process in addition to
2285 * MEMDIE process.
2286 */
2287 if (unlikely(test_thread_flag(TIF_MEMDIE)
2288 || fatal_signal_pending(current)))
2289 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002290
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002291 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08002292 * We always charge the cgroup the mm_struct belongs to.
2293 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002294 * thread group leader migrates. It's possible that mm is not
2295 * set, if so charge the init_mm (happens for pagecache usage).
2296 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002297 if (!*ptr && !mm)
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002298 goto bypass;
2299again:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002300 if (*ptr) { /* css should be a valid one */
2301 memcg = *ptr;
2302 VM_BUG_ON(css_is_removed(&memcg->css));
2303 if (mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002304 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002305 if (nr_pages == 1 && consume_stock(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002306 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002307 css_get(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002308 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002309 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002310
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002311 rcu_read_lock();
2312 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002313 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002314 * Because we don't have task_lock(), "p" can exit.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002315 * In that case, "memcg" can point to root or p can be NULL with
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002316 * race with swapoff. Then, we have small risk of mis-accouning.
2317 * But such kind of mis-account by race always happens because
2318 * we don't have cgroup_mutex(). It's overkill and we allo that
2319 * small race, here.
2320 * (*) swapoff at el will charge against mm-struct not against
2321 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002322 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002323 memcg = mem_cgroup_from_task(p);
2324 if (!memcg || mem_cgroup_is_root(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002325 rcu_read_unlock();
2326 goto done;
2327 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002328 if (nr_pages == 1 && consume_stock(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002329 /*
2330 * It seems dagerous to access memcg without css_get().
2331 * But considering how consume_stok works, it's not
2332 * necessary. If consume_stock success, some charges
2333 * from this memcg are cached on this cpu. So, we
2334 * don't need to call css_get()/css_tryget() before
2335 * calling consume_stock().
2336 */
2337 rcu_read_unlock();
2338 goto done;
2339 }
2340 /* after here, we may be blocked. we need to get refcnt */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002341 if (!css_tryget(&memcg->css)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002342 rcu_read_unlock();
2343 goto again;
2344 }
2345 rcu_read_unlock();
2346 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002347
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002348 do {
2349 bool oom_check;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002350
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002351 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002352 if (fatal_signal_pending(current)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002353 css_put(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002354 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002355 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002356
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002357 oom_check = false;
2358 if (oom && !nr_oom_retries) {
2359 oom_check = true;
2360 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2361 }
Balbir Singh6d61ef42009-01-07 18:08:06 -08002362
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002363 ret = mem_cgroup_do_charge(memcg, gfp_mask, batch, oom_check);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002364 switch (ret) {
2365 case CHARGE_OK:
2366 break;
2367 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002368 batch = nr_pages;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002369 css_put(&memcg->css);
2370 memcg = NULL;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002371 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002372 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002373 css_put(&memcg->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002374 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002375 case CHARGE_NOMEM: /* OOM routine works */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002376 if (!oom) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002377 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002378 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002379 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002380 /* If oom, we never return -ENOMEM */
2381 nr_oom_retries--;
2382 break;
2383 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002384 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002385 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08002386 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002387 } while (ret != CHARGE_OK);
2388
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002389 if (batch > nr_pages)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002390 refill_stock(memcg, batch - nr_pages);
2391 css_put(&memcg->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002392done:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002393 *ptr = memcg;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002394 return 0;
2395nomem:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002396 *ptr = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002397 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002398bypass:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002399 *ptr = NULL;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002400 return 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002401}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002402
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002403/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002404 * Somemtimes we have to undo a charge we got by try_charge().
2405 * This function is for that and do uncharge, put css's refcnt.
2406 * gotten by try_charge().
2407 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002408static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
Johannes Weinere7018b82011-03-23 16:42:33 -07002409 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002410{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002411 if (!mem_cgroup_is_root(memcg)) {
Johannes Weinere7018b82011-03-23 16:42:33 -07002412 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002413
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002414 res_counter_uncharge(&memcg->res, bytes);
Johannes Weinere7018b82011-03-23 16:42:33 -07002415 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002416 res_counter_uncharge(&memcg->memsw, bytes);
Johannes Weinere7018b82011-03-23 16:42:33 -07002417 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002418}
2419
2420/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002421 * A helper function to get mem_cgroup from ID. must be called under
2422 * rcu_read_lock(). The caller must check css_is_removed() or some if
2423 * it's concern. (dropping refcnt from swap can be called against removed
2424 * memcg.)
2425 */
2426static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2427{
2428 struct cgroup_subsys_state *css;
2429
2430 /* ID 0 is unused ID */
2431 if (!id)
2432 return NULL;
2433 css = css_lookup(&mem_cgroup_subsys, id);
2434 if (!css)
2435 return NULL;
2436 return container_of(css, struct mem_cgroup, css);
2437}
2438
Wu Fengguange42d9d52009-12-16 12:19:59 +01002439struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002440{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002441 struct mem_cgroup *memcg = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002442 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002443 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002444 swp_entry_t ent;
2445
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002446 VM_BUG_ON(!PageLocked(page));
2447
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002448 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002449 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002450 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002451 memcg = pc->mem_cgroup;
2452 if (memcg && !css_tryget(&memcg->css))
2453 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002454 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002455 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002456 id = lookup_swap_cgroup(ent);
2457 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002458 memcg = mem_cgroup_lookup(id);
2459 if (memcg && !css_tryget(&memcg->css))
2460 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002461 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002462 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002463 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002464 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002465}
2466
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002467static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,
Johannes Weiner5564e882011-03-23 16:42:29 -07002468 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002469 unsigned int nr_pages,
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002470 struct page_cgroup *pc,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002471 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002472{
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002473 lock_page_cgroup(pc);
2474 if (unlikely(PageCgroupUsed(pc))) {
2475 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002476 __mem_cgroup_cancel_charge(memcg, nr_pages);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002477 return;
2478 }
2479 /*
2480 * we don't need page_cgroup_lock about tail pages, becase they are not
2481 * accessed by any other context at this point.
2482 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002483 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002484 /*
2485 * We access a page_cgroup asynchronously without lock_page_cgroup().
2486 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2487 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2488 * before USED bit, we need memory barrier here.
2489 * See mem_cgroup_add_lru_list(), etc.
2490 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002491 smp_wmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -07002492 switch (ctype) {
2493 case MEM_CGROUP_CHARGE_TYPE_CACHE:
2494 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
2495 SetPageCgroupCache(pc);
2496 SetPageCgroupUsed(pc);
2497 break;
2498 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
2499 ClearPageCgroupCache(pc);
2500 SetPageCgroupUsed(pc);
2501 break;
2502 default:
2503 break;
2504 }
Hugh Dickins3be91272008-02-07 00:14:19 -08002505
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002506 mem_cgroup_charge_statistics(memcg, PageCgroupCache(pc), nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002507 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002508 /*
2509 * "charge_statistics" updated event counter. Then, check it.
2510 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2511 * if they exceeds softlimit.
2512 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002513 memcg_check_events(memcg, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002514}
2515
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002516#ifdef CONFIG_TRANSPARENT_HUGEPAGE
2517
2518#define PCGF_NOCOPY_AT_SPLIT ((1 << PCG_LOCK) | (1 << PCG_MOVE_LOCK) |\
2519 (1 << PCG_ACCT_LRU) | (1 << PCG_MIGRATION))
2520/*
2521 * Because tail pages are not marked as "used", set it. We're under
2522 * zone->lru_lock, 'splitting on pmd' and compund_lock.
2523 */
2524void mem_cgroup_split_huge_fixup(struct page *head, struct page *tail)
2525{
2526 struct page_cgroup *head_pc = lookup_page_cgroup(head);
2527 struct page_cgroup *tail_pc = lookup_page_cgroup(tail);
2528 unsigned long flags;
2529
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08002530 if (mem_cgroup_disabled())
2531 return;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002532 /*
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002533 * We have no races with charge/uncharge but will have races with
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002534 * page state accounting.
2535 */
2536 move_lock_page_cgroup(head_pc, &flags);
2537
2538 tail_pc->mem_cgroup = head_pc->mem_cgroup;
2539 smp_wmb(); /* see __commit_charge() */
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002540 if (PageCgroupAcctLRU(head_pc)) {
2541 enum lru_list lru;
2542 struct mem_cgroup_per_zone *mz;
2543
2544 /*
2545 * LRU flags cannot be copied because we need to add tail
2546 *.page to LRU by generic call and our hook will be called.
2547 * We hold lru_lock, then, reduce counter directly.
2548 */
2549 lru = page_lru(head);
Johannes Weiner97a6c372011-03-23 16:42:27 -07002550 mz = page_cgroup_zoneinfo(head_pc->mem_cgroup, head);
KAMEZAWA Hiroyukiece35ca2011-01-20 14:44:24 -08002551 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
2552 }
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002553 tail_pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
2554 move_unlock_page_cgroup(head_pc, &flags);
2555}
2556#endif
2557
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002558/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07002559 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07002560 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002561 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002562 * @pc: page_cgroup of the page.
2563 * @from: mem_cgroup which the page is moved from.
2564 * @to: mem_cgroup which the page is moved to. @from != @to.
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002565 * @uncharge: whether we should call uncharge and css_put against @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002566 *
2567 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002568 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002569 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002570 *
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002571 * This function doesn't do "charge" nor css_get to new cgroup. It should be
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002572 * done by a caller(__mem_cgroup_try_charge would be useful). If @uncharge is
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002573 * true, this function does "uncharge" from old cgroup, but it doesn't if
2574 * @uncharge is false, so a caller should do "uncharge".
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002575 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002576static int mem_cgroup_move_account(struct page *page,
2577 unsigned int nr_pages,
2578 struct page_cgroup *pc,
2579 struct mem_cgroup *from,
2580 struct mem_cgroup *to,
2581 bool uncharge)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002582{
Johannes Weinerde3638d2011-03-23 16:42:28 -07002583 unsigned long flags;
2584 int ret;
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002585
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002586 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07002587 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07002588 /*
2589 * The page is isolated from LRU. So, collapse function
2590 * will not handle this page. But page splitting can happen.
2591 * Do this check under compound_page_lock(). The caller should
2592 * hold it.
2593 */
2594 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002595 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07002596 goto out;
2597
2598 lock_page_cgroup(pc);
2599
2600 ret = -EINVAL;
2601 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
2602 goto unlock;
2603
2604 move_lock_page_cgroup(pc, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002605
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002606 if (PageCgroupFileMapped(pc)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08002607 /* Update mapped_file data for mem_cgroup */
2608 preempt_disable();
2609 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2610 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2611 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07002612 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002613 mem_cgroup_charge_statistics(from, PageCgroupCache(pc), -nr_pages);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002614 if (uncharge)
2615 /* This is not "cancel", but cancel_charge does all we need. */
Johannes Weinere7018b82011-03-23 16:42:33 -07002616 __mem_cgroup_cancel_charge(from, nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07002617
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002618 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002619 pc->mem_cgroup = to;
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002620 mem_cgroup_charge_statistics(to, PageCgroupCache(pc), nr_pages);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002621 /*
2622 * We charges against "to" which may not have any tasks. Then, "to"
2623 * can be under rmdir(). But in current implementation, caller of
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08002624 * this function is just force_empty() and move charge, so it's
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002625 * guaranteed that "to" is never removed. So, we don't check rmdir
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08002626 * status here.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002627 */
Johannes Weinerde3638d2011-03-23 16:42:28 -07002628 move_unlock_page_cgroup(pc, &flags);
2629 ret = 0;
2630unlock:
Daisuke Nishimura57f9fd72009-12-15 16:47:11 -08002631 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002632 /*
2633 * check events
2634 */
Johannes Weiner5564e882011-03-23 16:42:29 -07002635 memcg_check_events(to, page);
2636 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07002637out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002638 return ret;
2639}
2640
2641/*
2642 * move charges to its parent.
2643 */
2644
Johannes Weiner5564e882011-03-23 16:42:29 -07002645static int mem_cgroup_move_parent(struct page *page,
2646 struct page_cgroup *pc,
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002647 struct mem_cgroup *child,
2648 gfp_t gfp_mask)
2649{
2650 struct cgroup *cg = child->css.cgroup;
2651 struct cgroup *pcg = cg->parent;
2652 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002653 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07002654 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002655 int ret;
2656
2657 /* Is ROOT ? */
2658 if (!pcg)
2659 return -EINVAL;
2660
Daisuke Nishimura57f9fd72009-12-15 16:47:11 -08002661 ret = -EBUSY;
2662 if (!get_page_unless_zero(page))
2663 goto out;
2664 if (isolate_lru_page(page))
2665 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08002666
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002667 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002668
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002669 parent = mem_cgroup_from_cont(pcg);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002670 ret = __mem_cgroup_try_charge(NULL, gfp_mask, nr_pages, &parent, false);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002671 if (ret || !parent)
Daisuke Nishimura57f9fd72009-12-15 16:47:11 -08002672 goto put_back;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002673
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002674 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002675 flags = compound_lock_irqsave(page);
2676
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002677 ret = mem_cgroup_move_account(page, nr_pages, pc, child, parent, true);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002678 if (ret)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002679 __mem_cgroup_cancel_charge(parent, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08002680
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002681 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002682 compound_unlock_irqrestore(page, flags);
Jesper Juhl8dba4742011-01-25 15:07:24 -08002683put_back:
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002684 putback_lru_page(page);
Daisuke Nishimura57f9fd72009-12-15 16:47:11 -08002685put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08002686 put_page(page);
Daisuke Nishimura57f9fd72009-12-15 16:47:11 -08002687out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002688 return ret;
2689}
2690
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002691/*
2692 * Charge the memory controller for page usage.
2693 * Return
2694 * 0 if the charge was successful
2695 * < 0 if the cgroup is over its limit
2696 */
2697static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002698 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002699{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002700 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002701 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08002702 struct page_cgroup *pc;
2703 bool oom = true;
2704 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08002705
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002706 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002707 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002708 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08002709 /*
2710 * Never OOM-kill a process for a huge page. The
2711 * fault handler will fall back to regular pages.
2712 */
2713 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002714 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002715
2716 pc = lookup_page_cgroup(page);
Johannes Weineraf4a6622011-03-23 16:42:24 -07002717 BUG_ON(!pc); /* XXX: remove this and move pc lookup into commit */
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002718
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002719 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom);
2720 if (ret || !memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002721 return ret;
2722
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002723 __mem_cgroup_commit_charge(memcg, page, nr_pages, pc, ctype);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002724 return 0;
2725}
2726
2727int mem_cgroup_newpage_charge(struct page *page,
2728 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002729{
Hirokazu Takahashif8d66542009-01-07 18:08:02 -08002730 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002731 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002732 /*
2733 * If already mapped, we don't have to account.
2734 * If page cache, page->mapping has address_space.
2735 * But page->mapping may have out-of-use anon_vma pointer,
2736 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
2737 * is NULL.
2738 */
2739 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
2740 return 0;
2741 if (unlikely(!mm))
2742 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002743 return mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002744 MEM_CGROUP_CHARGE_TYPE_MAPPED);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002745}
2746
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002747static void
2748__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2749 enum charge_type ctype);
2750
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002751static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002752__mem_cgroup_commit_charge_lrucare(struct page *page, struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002753 enum charge_type ctype)
2754{
2755 struct page_cgroup *pc = lookup_page_cgroup(page);
2756 /*
2757 * In some case, SwapCache, FUSE(splice_buf->radixtree), the page
2758 * is already on LRU. It means the page may on some other page_cgroup's
2759 * LRU. Take care of it.
2760 */
2761 mem_cgroup_lru_del_before_commit(page);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002762 __mem_cgroup_commit_charge(memcg, page, 1, pc, ctype);
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002763 mem_cgroup_lru_add_after_commit(page);
2764 return;
2765}
2766
Balbir Singhe1a1cd52008-02-07 00:14:02 -08002767int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
2768 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08002769{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002770 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002771 int ret;
2772
Hirokazu Takahashif8d66542009-01-07 18:08:02 -08002773 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002774 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002775 if (PageCompound(page))
2776 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002777
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002778 if (unlikely(!mm))
Balbir Singh8697d332008-02-07 00:13:59 -08002779 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002780
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002781 if (page_is_file_cache(page)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002782 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, &memcg, true);
2783 if (ret || !memcg)
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002784 return ret;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002785
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002786 /*
2787 * FUSE reuses pages without going through the final
2788 * put that would remove them from the LRU list, make
2789 * sure that they get relinked properly.
2790 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002791 __mem_cgroup_commit_charge_lrucare(page, memcg,
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002792 MEM_CGROUP_CHARGE_TYPE_CACHE);
2793 return ret;
2794 }
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002795 /* shmem */
2796 if (PageSwapCache(page)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002797 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &memcg);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002798 if (!ret)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002799 __mem_cgroup_commit_charge_swapin(page, memcg,
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002800 MEM_CGROUP_CHARGE_TYPE_SHMEM);
2801 } else
2802 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002803 MEM_CGROUP_CHARGE_TYPE_SHMEM);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002804
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002805 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002806}
2807
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002808/*
2809 * While swap-in, try_charge -> commit or cancel, the page is locked.
2810 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02002811 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002812 * "commit()" or removed by "cancel()"
2813 */
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002814int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
2815 struct page *page,
2816 gfp_t mask, struct mem_cgroup **ptr)
2817{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002818 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002819 int ret;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002820
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07002821 *ptr = NULL;
2822
Hirokazu Takahashif8d66542009-01-07 18:08:02 -08002823 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002824 return 0;
2825
2826 if (!do_swap_account)
2827 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002828 /*
2829 * A racing thread's fault, or swapoff, may have already updated
Hugh Dickins407f9c82009-12-14 17:59:30 -08002830 * the pte, and even removed page from swap cache: in those cases
2831 * do_swap_page()'s pte_same() test will fail; but there's also a
2832 * KSM case which does need to charge the page.
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002833 */
2834 if (!PageSwapCache(page))
Hugh Dickins407f9c82009-12-14 17:59:30 -08002835 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002836 memcg = try_get_mem_cgroup_from_page(page);
2837 if (!memcg)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002838 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002839 *ptr = memcg;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002840 ret = __mem_cgroup_try_charge(NULL, mask, 1, ptr, true);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002841 css_put(&memcg->css);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002842 return ret;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002843charge_cur_mm:
2844 if (unlikely(!mm))
2845 mm = &init_mm;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002846 return __mem_cgroup_try_charge(mm, mask, 1, ptr, true);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002847}
2848
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002849static void
2850__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2851 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002852{
Hirokazu Takahashif8d66542009-01-07 18:08:02 -08002853 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002854 return;
2855 if (!ptr)
2856 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002857 cgroup_exclude_rmdir(&ptr->css);
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002858
2859 __mem_cgroup_commit_charge_lrucare(page, ptr, ctype);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002860 /*
2861 * Now swap is on-memory. This means this page may be
2862 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002863 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
2864 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
2865 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002866 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002867 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002868 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002869 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002870 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002871
2872 id = swap_cgroup_record(ent, 0);
2873 rcu_read_lock();
2874 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002875 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002876 /*
2877 * This recorded memcg can be obsolete one. So, avoid
2878 * calling css_tryget
2879 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002880 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002881 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002882 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002883 mem_cgroup_put(memcg);
2884 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002885 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002886 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002887 /*
2888 * At swapin, we may charge account against cgroup which has no tasks.
2889 * So, rmdir()->pre_destroy() can be called while we do this charge.
2890 * In that case, we need to call pre_destroy() again. check it here.
2891 */
2892 cgroup_release_and_wakeup_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002893}
2894
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002895void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
2896{
2897 __mem_cgroup_commit_charge_swapin(page, ptr,
2898 MEM_CGROUP_CHARGE_TYPE_MAPPED);
2899}
2900
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002901void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002902{
Hirokazu Takahashif8d66542009-01-07 18:08:02 -08002903 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002904 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002905 if (!memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002906 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002907 __mem_cgroup_cancel_charge(memcg, 1);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002908}
2909
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002910static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002911 unsigned int nr_pages,
2912 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002913{
2914 struct memcg_batch_info *batch = NULL;
2915 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002916
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002917 /* If swapout, usage of swap doesn't decrease */
2918 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2919 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002920
2921 batch = &current->memcg_batch;
2922 /*
2923 * In usual, we do css_get() when we remember memcg pointer.
2924 * But in this case, we keep res->usage until end of a series of
2925 * uncharges. Then, it's ok to ignore memcg's refcnt.
2926 */
2927 if (!batch->memcg)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002928 batch->memcg = memcg;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002929 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002930 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002931 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002932 * the same cgroup and we have chance to coalesce uncharges.
2933 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
2934 * because we want to do uncharge as soon as possible.
2935 */
2936
2937 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
2938 goto direct_uncharge;
2939
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002940 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08002941 goto direct_uncharge;
2942
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002943 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002944 * In typical case, batch->memcg == mem. This means we can
2945 * merge a series of uncharges to an uncharge of res_counter.
2946 * If not, we uncharge res_counter ony by one.
2947 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002948 if (batch->memcg != memcg)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002949 goto direct_uncharge;
2950 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07002951 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002952 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07002953 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002954 return;
2955direct_uncharge:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002956 res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002957 if (uncharge_memsw)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002958 res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE);
2959 if (unlikely(batch->memcg != memcg))
2960 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002961 return;
2962}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002963
Balbir Singh8697d332008-02-07 00:13:59 -08002964/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002965 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002966 */
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002967static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002968__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002969{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002970 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002971 unsigned int nr_pages = 1;
2972 struct page_cgroup *pc;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002973
Hirokazu Takahashif8d66542009-01-07 18:08:02 -08002974 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002975 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07002976
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002977 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002978 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002979
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002980 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002981 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002982 VM_BUG_ON(!PageTransHuge(page));
2983 }
Balbir Singh8697d332008-02-07 00:13:59 -08002984 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08002985 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08002986 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002987 pc = lookup_page_cgroup(page);
2988 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002989 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002990
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002991 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002992
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002993 memcg = pc->mem_cgroup;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08002994
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002995 if (!PageCgroupUsed(pc))
2996 goto unlock_out;
2997
2998 switch (ctype) {
2999 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003000 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003001 /* See mem_cgroup_prepare_migration() */
3002 if (page_mapped(page) || PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003003 goto unlock_out;
3004 break;
3005 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
3006 if (!PageAnon(page)) { /* Shared memory */
3007 if (page->mapping && !page_is_file_cache(page))
3008 goto unlock_out;
3009 } else if (page_mapped(page)) /* Anon */
3010 goto unlock_out;
3011 break;
3012 default:
3013 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003014 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003015
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003016 mem_cgroup_charge_statistics(memcg, PageCgroupCache(pc), -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003017
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003018 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08003019 /*
3020 * pc->mem_cgroup is not cleared here. It will be accessed when it's
3021 * freed from LRU. This is safe because uncharged page is expected not
3022 * to be reused (freed soon). Exception is SwapCache, it's handled by
3023 * special functions.
3024 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003025
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003026 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003027 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003028 * even after unlock, we have memcg->res.usage here and this memcg
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003029 * will never be freed.
3030 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003031 memcg_check_events(memcg, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003032 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003033 mem_cgroup_swap_statistics(memcg, true);
3034 mem_cgroup_get(memcg);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003035 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003036 if (!mem_cgroup_is_root(memcg))
3037 mem_cgroup_do_uncharge(memcg, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003038
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003039 return memcg;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003040
3041unlock_out:
3042 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003043 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08003044}
3045
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003046void mem_cgroup_uncharge_page(struct page *page)
3047{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003048 /* early check. */
3049 if (page_mapped(page))
3050 return;
3051 if (page->mapping && !PageAnon(page))
3052 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003053 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
3054}
3055
3056void mem_cgroup_uncharge_cache_page(struct page *page)
3057{
3058 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07003059 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003060 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
3061}
3062
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003063/*
3064 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
3065 * In that cases, pages are freed continuously and we can expect pages
3066 * are in the same memcg. All these calls itself limits the number of
3067 * pages freed at once, then uncharge_start/end() is called properly.
3068 * This may be called prural(2) times in a context,
3069 */
3070
3071void mem_cgroup_uncharge_start(void)
3072{
3073 current->memcg_batch.do_batch++;
3074 /* We can do nest. */
3075 if (current->memcg_batch.do_batch == 1) {
3076 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003077 current->memcg_batch.nr_pages = 0;
3078 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003079 }
3080}
3081
3082void mem_cgroup_uncharge_end(void)
3083{
3084 struct memcg_batch_info *batch = &current->memcg_batch;
3085
3086 if (!batch->do_batch)
3087 return;
3088
3089 batch->do_batch--;
3090 if (batch->do_batch) /* If stacked, do nothing. */
3091 return;
3092
3093 if (!batch->memcg)
3094 return;
3095 /*
3096 * This "batch->memcg" is valid without any css_get/put etc...
3097 * bacause we hide charges behind us.
3098 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003099 if (batch->nr_pages)
3100 res_counter_uncharge(&batch->memcg->res,
3101 batch->nr_pages * PAGE_SIZE);
3102 if (batch->memsw_nr_pages)
3103 res_counter_uncharge(&batch->memcg->memsw,
3104 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003105 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003106 /* forget this pointer (for sanity check) */
3107 batch->memcg = NULL;
3108}
3109
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003110#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003111/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003112 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003113 * memcg information is recorded to swap_cgroup of "ent"
3114 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003115void
3116mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003117{
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003118 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003119 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003120
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003121 if (!swapout) /* this was a swap cache but the swap is unused ! */
3122 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
3123
3124 memcg = __mem_cgroup_uncharge_common(page, ctype);
3125
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003126 /*
3127 * record memcg information, if swapout && memcg != NULL,
3128 * mem_cgroup_get() was called in uncharge().
3129 */
3130 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003131 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003132}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003133#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003134
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003135#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3136/*
3137 * called from swap_entry_free(). remove record in swap_cgroup and
3138 * uncharge "memsw" account.
3139 */
3140void mem_cgroup_uncharge_swap(swp_entry_t ent)
3141{
3142 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003143 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003144
3145 if (!do_swap_account)
3146 return;
3147
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003148 id = swap_cgroup_record(ent, 0);
3149 rcu_read_lock();
3150 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003151 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003152 /*
3153 * We uncharge this because swap is freed.
3154 * This memcg can be obsolete one. We avoid calling css_tryget
3155 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003156 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07003157 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003158 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003159 mem_cgroup_put(memcg);
3160 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003161 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003162}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003163
3164/**
3165 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
3166 * @entry: swap entry to be moved
3167 * @from: mem_cgroup which the entry is moved from
3168 * @to: mem_cgroup which the entry is moved to
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003169 * @need_fixup: whether we should fixup res_counters and refcounts.
Daisuke Nishimura02491442010-03-10 15:22:17 -08003170 *
3171 * It succeeds only when the swap_cgroup's record for this entry is the same
3172 * as the mem_cgroup's id of @from.
3173 *
3174 * Returns 0 on success, -EINVAL on failure.
3175 *
3176 * The caller must have charged to @to, IOW, called res_counter_charge() about
3177 * both res and memsw, and called css_get().
3178 */
3179static int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003180 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003181{
3182 unsigned short old_id, new_id;
3183
3184 old_id = css_id(&from->css);
3185 new_id = css_id(&to->css);
3186
3187 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08003188 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003189 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003190 /*
3191 * This function is only called from task migration context now.
3192 * It postpones res_counter and refcount handling till the end
3193 * of task migration(mem_cgroup_clear_mc()) for performance
3194 * improvement. But we cannot postpone mem_cgroup_get(to)
3195 * because if the process that has been moved to @to does
3196 * swap-in, the refcount of @to might be decreased to 0.
3197 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08003198 mem_cgroup_get(to);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003199 if (need_fixup) {
3200 if (!mem_cgroup_is_root(from))
3201 res_counter_uncharge(&from->memsw, PAGE_SIZE);
3202 mem_cgroup_put(from);
3203 /*
3204 * we charged both to->res and to->memsw, so we should
3205 * uncharge to->res.
3206 */
3207 if (!mem_cgroup_is_root(to))
3208 res_counter_uncharge(&to->res, PAGE_SIZE);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003209 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08003210 return 0;
3211 }
3212 return -EINVAL;
3213}
3214#else
3215static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003216 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003217{
3218 return -EINVAL;
3219}
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003220#endif
3221
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003222/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003223 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
3224 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003225 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003226int mem_cgroup_prepare_migration(struct page *page,
Miklos Szeredief6a3c62011-03-22 16:30:52 -07003227 struct page *newpage, struct mem_cgroup **ptr, gfp_t gfp_mask)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003228{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003229 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003230 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003231 enum charge_type ctype;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003232 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003233
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07003234 *ptr = NULL;
3235
Andrea Arcangeliec168512011-01-13 15:46:56 -08003236 VM_BUG_ON(PageTransHuge(page));
Hirokazu Takahashif8d66542009-01-07 18:08:02 -08003237 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07003238 return 0;
3239
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003240 pc = lookup_page_cgroup(page);
3241 lock_page_cgroup(pc);
3242 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003243 memcg = pc->mem_cgroup;
3244 css_get(&memcg->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003245 /*
3246 * At migrating an anonymous page, its mapcount goes down
3247 * to 0 and uncharge() will be called. But, even if it's fully
3248 * unmapped, migration may fail and this page has to be
3249 * charged again. We set MIGRATION flag here and delay uncharge
3250 * until end_migration() is called
3251 *
3252 * Corner Case Thinking
3253 * A)
3254 * When the old page was mapped as Anon and it's unmap-and-freed
3255 * while migration was ongoing.
3256 * If unmap finds the old page, uncharge() of it will be delayed
3257 * until end_migration(). If unmap finds a new page, it's
3258 * uncharged when it make mapcount to be 1->0. If unmap code
3259 * finds swap_migration_entry, the new page will not be mapped
3260 * and end_migration() will find it(mapcount==0).
3261 *
3262 * B)
3263 * When the old page was mapped but migraion fails, the kernel
3264 * remaps it. A charge for it is kept by MIGRATION flag even
3265 * if mapcount goes down to 0. We can do remap successfully
3266 * without charging it again.
3267 *
3268 * C)
3269 * The "old" page is under lock_page() until the end of
3270 * migration, so, the old page itself will not be swapped-out.
3271 * If the new page is swapped out before end_migraton, our
3272 * hook to usual swap-out path will catch the event.
3273 */
3274 if (PageAnon(page))
3275 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003276 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003277 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003278 /*
3279 * If the page is not charged at this point,
3280 * we return here.
3281 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003282 if (!memcg)
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003283 return 0;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003284
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003285 *ptr = memcg;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003286 ret = __mem_cgroup_try_charge(NULL, gfp_mask, 1, ptr, false);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003287 css_put(&memcg->css);/* drop extra refcnt */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003288 if (ret || *ptr == NULL) {
3289 if (PageAnon(page)) {
3290 lock_page_cgroup(pc);
3291 ClearPageCgroupMigration(pc);
3292 unlock_page_cgroup(pc);
3293 /*
3294 * The old page may be fully unmapped while we kept it.
3295 */
3296 mem_cgroup_uncharge_page(page);
3297 }
3298 return -ENOMEM;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003299 }
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003300 /*
3301 * We charge new page before it's used/mapped. So, even if unlock_page()
3302 * is called before end_migration, we can catch all events on this new
3303 * page. In the case new page is migrated but not remapped, new page's
3304 * mapcount will be finally 0 and we call uncharge in end_migration().
3305 */
3306 pc = lookup_page_cgroup(newpage);
3307 if (PageAnon(page))
3308 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
3309 else if (page_is_file_cache(page))
3310 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
3311 else
3312 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003313 __mem_cgroup_commit_charge(memcg, page, 1, pc, ctype);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003314 return ret;
3315}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08003316
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003317/* remove redundant charge if migration failed*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003318void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003319 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003320{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003321 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003322 struct page_cgroup *pc;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003323
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003324 if (!memcg)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003325 return;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003326 /* blocks rmdir() */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003327 cgroup_exclude_rmdir(&memcg->css);
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003328 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003329 used = oldpage;
3330 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003331 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003332 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003333 unused = oldpage;
3334 }
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003335 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003336 * We disallowed uncharge of pages under migration because mapcount
3337 * of the page goes down to zero, temporarly.
3338 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003339 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003340 pc = lookup_page_cgroup(oldpage);
3341 lock_page_cgroup(pc);
3342 ClearPageCgroupMigration(pc);
3343 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003344
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003345 __mem_cgroup_uncharge_common(unused, MEM_CGROUP_CHARGE_TYPE_FORCE);
3346
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003347 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003348 * If a page is a file cache, radix-tree replacement is very atomic
3349 * and we can skip this check. When it was an Anon page, its mapcount
3350 * goes down to 0. But because we added MIGRATION flage, it's not
3351 * uncharged yet. There are several case but page->mapcount check
3352 * and USED bit check in mem_cgroup_uncharge_page() will do enough
3353 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003354 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003355 if (PageAnon(used))
3356 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003357 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003358 * At migration, we may charge account against cgroup which has no
3359 * tasks.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003360 * So, rmdir()->pre_destroy() can be called while we do this charge.
3361 * In that case, we need to call pre_destroy() again. check it here.
3362 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003363 cgroup_release_and_wakeup_rmdir(&memcg->css);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003364}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08003365
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003366#ifdef CONFIG_DEBUG_VM
3367static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
3368{
3369 struct page_cgroup *pc;
3370
3371 pc = lookup_page_cgroup(page);
3372 if (likely(pc) && PageCgroupUsed(pc))
3373 return pc;
3374 return NULL;
3375}
3376
3377bool mem_cgroup_bad_page_check(struct page *page)
3378{
3379 if (mem_cgroup_disabled())
3380 return false;
3381
3382 return lookup_page_cgroup_used(page) != NULL;
3383}
3384
3385void mem_cgroup_print_bad_page(struct page *page)
3386{
3387 struct page_cgroup *pc;
3388
3389 pc = lookup_page_cgroup_used(page);
3390 if (pc) {
3391 int ret = -1;
3392 char *path;
3393
3394 printk(KERN_ALERT "pc:%p pc->flags:%lx pc->mem_cgroup:%p",
3395 pc, pc->flags, pc->mem_cgroup);
3396
3397 path = kmalloc(PATH_MAX, GFP_KERNEL);
3398 if (path) {
3399 rcu_read_lock();
3400 ret = cgroup_path(pc->mem_cgroup->css.cgroup,
3401 path, PATH_MAX);
3402 rcu_read_unlock();
3403 }
3404
3405 printk(KERN_CONT "(%s)\n",
3406 (ret < 0) ? "cannot get the path" : path);
3407 kfree(path);
3408 }
3409}
3410#endif
3411
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003412static DEFINE_MUTEX(set_limit_mutex);
3413
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08003414static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003415 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003416{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003417 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003418 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003419 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003420 int children = mem_cgroup_count_children(memcg);
3421 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003422 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003423
3424 /*
3425 * For keeping hierarchical_reclaim simple, how long we should retry
3426 * is depends on callers. We set our retry-count to be function
3427 * of # of children which we should visit in this loop.
3428 */
3429 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
3430
3431 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003432
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003433 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003434 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003435 if (signal_pending(current)) {
3436 ret = -EINTR;
3437 break;
3438 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003439 /*
3440 * Rather than hide all in some function, I do this in
3441 * open coded manner. You see what this really does.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003442 * We have to guarantee memcg->res.limit < memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003443 */
3444 mutex_lock(&set_limit_mutex);
3445 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3446 if (memswlimit < val) {
3447 ret = -EINVAL;
3448 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003449 break;
3450 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003451
3452 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3453 if (memlimit < val)
3454 enlarge = 1;
3455
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003456 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003457 if (!ret) {
3458 if (memswlimit == val)
3459 memcg->memsw_is_minimum = true;
3460 else
3461 memcg->memsw_is_minimum = false;
3462 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003463 mutex_unlock(&set_limit_mutex);
3464
3465 if (!ret)
3466 break;
3467
Bob Liuaa20d482009-12-15 16:47:14 -08003468 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Ying Han0ae5e892011-05-26 16:25:25 -07003469 MEM_CGROUP_RECLAIM_SHRINK,
3470 NULL);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003471 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
3472 /* Usage is reduced ? */
3473 if (curusage >= oldusage)
3474 retry_count--;
3475 else
3476 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003477 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003478 if (!ret && enlarge)
3479 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08003480
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003481 return ret;
3482}
3483
Li Zefan338c8432009-06-17 16:27:15 -07003484static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
3485 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003486{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003487 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003488 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003489 int children = mem_cgroup_count_children(memcg);
3490 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003491 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003492
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003493 /* see mem_cgroup_resize_res_limit */
3494 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
3495 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003496 while (retry_count) {
3497 if (signal_pending(current)) {
3498 ret = -EINTR;
3499 break;
3500 }
3501 /*
3502 * Rather than hide all in some function, I do this in
3503 * open coded manner. You see what this really does.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003504 * We have to guarantee memcg->res.limit < memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003505 */
3506 mutex_lock(&set_limit_mutex);
3507 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3508 if (memlimit > val) {
3509 ret = -EINVAL;
3510 mutex_unlock(&set_limit_mutex);
3511 break;
3512 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003513 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3514 if (memswlimit < val)
3515 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003516 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003517 if (!ret) {
3518 if (memlimit == val)
3519 memcg->memsw_is_minimum = true;
3520 else
3521 memcg->memsw_is_minimum = false;
3522 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003523 mutex_unlock(&set_limit_mutex);
3524
3525 if (!ret)
3526 break;
3527
Balbir Singh4e416952009-09-23 15:56:39 -07003528 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh75822b42009-09-23 15:56:38 -07003529 MEM_CGROUP_RECLAIM_NOSWAP |
Ying Han0ae5e892011-05-26 16:25:25 -07003530 MEM_CGROUP_RECLAIM_SHRINK,
3531 NULL);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003532 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003533 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003534 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003535 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003536 else
3537 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003538 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003539 if (!ret && enlarge)
3540 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003541 return ret;
3542}
3543
Balbir Singh4e416952009-09-23 15:56:39 -07003544unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
Ying Han0ae5e892011-05-26 16:25:25 -07003545 gfp_t gfp_mask,
3546 unsigned long *total_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07003547{
3548 unsigned long nr_reclaimed = 0;
3549 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
3550 unsigned long reclaimed;
3551 int loop = 0;
3552 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003553 unsigned long long excess;
Ying Han0ae5e892011-05-26 16:25:25 -07003554 unsigned long nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003555
3556 if (order > 0)
3557 return 0;
3558
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07003559 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
Balbir Singh4e416952009-09-23 15:56:39 -07003560 /*
3561 * This loop can run a while, specially if mem_cgroup's continuously
3562 * keep exceeding their soft limit and putting the system under
3563 * pressure
3564 */
3565 do {
3566 if (next_mz)
3567 mz = next_mz;
3568 else
3569 mz = mem_cgroup_largest_soft_limit_node(mctz);
3570 if (!mz)
3571 break;
3572
Ying Han0ae5e892011-05-26 16:25:25 -07003573 nr_scanned = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07003574 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
3575 gfp_mask,
Ying Han0ae5e892011-05-26 16:25:25 -07003576 MEM_CGROUP_RECLAIM_SOFT,
3577 &nr_scanned);
Balbir Singh4e416952009-09-23 15:56:39 -07003578 nr_reclaimed += reclaimed;
Ying Han0ae5e892011-05-26 16:25:25 -07003579 *total_scanned += nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003580 spin_lock(&mctz->lock);
3581
3582 /*
3583 * If we failed to reclaim anything from this memory cgroup
3584 * it is time to move on to the next cgroup
3585 */
3586 next_mz = NULL;
3587 if (!reclaimed) {
3588 do {
3589 /*
3590 * Loop until we find yet another one.
3591 *
3592 * By the time we get the soft_limit lock
3593 * again, someone might have aded the
3594 * group back on the RB tree. Iterate to
3595 * make sure we get a different mem.
3596 * mem_cgroup_largest_soft_limit_node returns
3597 * NULL if no other cgroup is present on
3598 * the tree
3599 */
3600 next_mz =
3601 __mem_cgroup_largest_soft_limit_node(mctz);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003602 if (next_mz == mz)
Balbir Singh4e416952009-09-23 15:56:39 -07003603 css_put(&next_mz->mem->css);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003604 else /* next_mz == NULL or other memcg */
Balbir Singh4e416952009-09-23 15:56:39 -07003605 break;
3606 } while (1);
3607 }
Balbir Singh4e416952009-09-23 15:56:39 -07003608 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003609 excess = res_counter_soft_limit_excess(&mz->mem->res);
Balbir Singh4e416952009-09-23 15:56:39 -07003610 /*
3611 * One school of thought says that we should not add
3612 * back the node to the tree if reclaim returns 0.
3613 * But our reclaim could return 0, simply because due
3614 * to priority we are exposing a smaller subset of
3615 * memory to reclaim from. Consider this as a longer
3616 * term TODO.
3617 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003618 /* If excess == 0, no tree ops */
3619 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07003620 spin_unlock(&mctz->lock);
3621 css_put(&mz->mem->css);
3622 loop++;
3623 /*
3624 * Could not reclaim anything and there are no more
3625 * mem cgroups to try or we seem to be looping without
3626 * reclaiming anything.
3627 */
3628 if (!nr_reclaimed &&
3629 (next_mz == NULL ||
3630 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3631 break;
3632 } while (!nr_reclaimed);
3633 if (next_mz)
3634 css_put(&next_mz->mem->css);
3635 return nr_reclaimed;
3636}
3637
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07003638/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003639 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003640 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
3641 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003642static int mem_cgroup_force_empty_list(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003643 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003644{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003645 struct zone *zone;
3646 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003647 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003648 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c2008-02-07 00:14:39 -08003649 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003650 int ret = 0;
KAMEZAWA Hiroyuki072c56c2008-02-07 00:14:39 -08003651
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003652 zone = &NODE_DATA(node)->node_zones[zid];
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003653 mz = mem_cgroup_zoneinfo(memcg, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07003654 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003655
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003656 loop = MEM_CGROUP_ZSTAT(mz, lru);
3657 /* give some margin against EBUSY etc...*/
3658 loop += 256;
3659 busy = NULL;
3660 while (loop--) {
Johannes Weiner5564e882011-03-23 16:42:29 -07003661 struct page *page;
3662
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003663 ret = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003664 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003665 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003666 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003667 break;
3668 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003669 pc = list_entry(list->prev, struct page_cgroup, lru);
3670 if (busy == pc) {
3671 list_move(&pc->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08003672 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003673 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003674 continue;
3675 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003676 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003677
Johannes Weiner6b3ae582011-03-23 16:42:30 -07003678 page = lookup_cgroup_page(pc);
Johannes Weiner5564e882011-03-23 16:42:29 -07003679
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003680 ret = mem_cgroup_move_parent(page, pc, memcg, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003681 if (ret == -ENOMEM)
3682 break;
3683
3684 if (ret == -EBUSY || ret == -EINVAL) {
3685 /* found lock contention or "pc" is obsolete. */
3686 busy = pc;
3687 cond_resched();
3688 } else
3689 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003690 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003691
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003692 if (!ret && !list_empty(list))
3693 return -EBUSY;
3694 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003695}
3696
3697/*
3698 * make mem_cgroup's charge to be 0 if there is no task.
3699 * This enables deleting this mem_cgroup.
3700 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003701static int mem_cgroup_force_empty(struct mem_cgroup *memcg, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003702{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003703 int ret;
3704 int node, zid, shrink;
3705 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003706 struct cgroup *cgrp = memcg->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003707
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003708 css_get(&memcg->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003709
3710 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003711 /* should free all ? */
3712 if (free_all)
3713 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003714move_account:
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003715 do {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003716 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003717 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003718 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003719 ret = -EINTR;
3720 if (signal_pending(current))
3721 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003722 /* This is for making all *used* pages to be on LRU. */
3723 lru_add_drain_all();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003724 drain_all_stock_sync(memcg);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003725 ret = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003726 mem_cgroup_start_move(memcg);
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08003727 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003728 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07003729 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003730 for_each_lru(l) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003731 ret = mem_cgroup_force_empty_list(memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003732 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003733 if (ret)
3734 break;
3735 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003736 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003737 if (ret)
3738 break;
3739 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003740 mem_cgroup_end_move(memcg);
3741 memcg_oom_recover(memcg);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003742 /* it seems parent cgroup doesn't have enough mem */
3743 if (ret == -ENOMEM)
3744 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003745 cond_resched();
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003746 /* "ret" should also be checked to ensure all lists are empty. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003747 } while (memcg->res.usage > 0 || ret);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003748out:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003749 css_put(&memcg->css);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003750 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003751
3752try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003753 /* returns EBUSY if there is a task or if we come here twice. */
3754 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003755 ret = -EBUSY;
3756 goto out;
3757 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003758 /* we call try-to-free pages for make this cgroup empty */
3759 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003760 /* try to free all pages in this cgroup */
3761 shrink = 1;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003762 while (nr_retries && memcg->res.usage > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003763 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003764
3765 if (signal_pending(current)) {
3766 ret = -EINTR;
3767 goto out;
3768 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003769 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
Johannes Weiner185efc02011-09-14 16:21:58 -07003770 false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003771 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003772 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003773 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02003774 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003775 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003776
3777 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003778 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003779 /* try move_account...there may be some *locked* pages. */
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003780 goto move_account;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003781}
3782
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003783int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
3784{
3785 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
3786}
3787
3788
Balbir Singh18f59ea2009-01-07 18:08:07 -08003789static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
3790{
3791 return mem_cgroup_from_cont(cont)->use_hierarchy;
3792}
3793
3794static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
3795 u64 val)
3796{
3797 int retval = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003798 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003799 struct cgroup *parent = cont->parent;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003800 struct mem_cgroup *parent_memcg = NULL;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003801
3802 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003803 parent_memcg = mem_cgroup_from_cont(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003804
3805 cgroup_lock();
3806 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003807 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08003808 * in the child subtrees. If it is unset, then the change can
3809 * occur, provided the current cgroup has no children.
3810 *
3811 * For the root cgroup, parent_mem is NULL, we allow value to be
3812 * set if there are no children.
3813 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003814 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08003815 (val == 1 || val == 0)) {
3816 if (list_empty(&cont->children))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003817 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003818 else
3819 retval = -EBUSY;
3820 } else
3821 retval = -EINVAL;
3822 cgroup_unlock();
3823
3824 return retval;
3825}
3826
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003827
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003828static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003829 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003830{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003831 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003832 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003833
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003834 /* Per-cpu values can be negative, use a signed accumulator */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003835 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003836 val += mem_cgroup_read_stat(iter, idx);
3837
3838 if (val < 0) /* race ? */
3839 val = 0;
3840 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003841}
3842
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003843static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003844{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003845 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003846
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003847 if (!mem_cgroup_is_root(memcg)) {
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003848 if (!swap)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003849 return res_counter_read_u64(&memcg->res, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003850 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003851 return res_counter_read_u64(&memcg->memsw, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003852 }
3853
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003854 val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
3855 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003856
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003857 if (swap)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003858 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAPOUT);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003859
3860 return val << PAGE_SHIFT;
3861}
3862
Paul Menage2c3daa72008-04-29 00:59:58 -07003863static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003864{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003865 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003866 u64 val;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003867 int type, name;
3868
3869 type = MEMFILE_TYPE(cft->private);
3870 name = MEMFILE_ATTR(cft->private);
3871 switch (type) {
3872 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003873 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003874 val = mem_cgroup_usage(memcg, false);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003875 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003876 val = res_counter_read_u64(&memcg->res, name);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003877 break;
3878 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003879 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003880 val = mem_cgroup_usage(memcg, true);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003881 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003882 val = res_counter_read_u64(&memcg->memsw, name);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003883 break;
3884 default:
3885 BUG();
3886 break;
3887 }
3888 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003889}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003890/*
3891 * The user of this function is...
3892 * RES_LIMIT.
3893 */
Paul Menage856c13a2008-07-25 01:47:04 -07003894static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
3895 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003896{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003897 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003898 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003899 unsigned long long val;
3900 int ret;
3901
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003902 type = MEMFILE_TYPE(cft->private);
3903 name = MEMFILE_ATTR(cft->private);
3904 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003905 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07003906 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
3907 ret = -EINVAL;
3908 break;
3909 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003910 /* This function does all necessary parse...reuse it */
3911 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003912 if (ret)
3913 break;
3914 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003915 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003916 else
3917 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003918 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07003919 case RES_SOFT_LIMIT:
3920 ret = res_counter_memparse_write_strategy(buffer, &val);
3921 if (ret)
3922 break;
3923 /*
3924 * For memsw, soft limits are hard to implement in terms
3925 * of semantics, for now, we support soft limits for
3926 * control without swap
3927 */
3928 if (type == _MEM)
3929 ret = res_counter_set_soft_limit(&memcg->res, val);
3930 else
3931 ret = -EINVAL;
3932 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003933 default:
3934 ret = -EINVAL; /* should be BUG() ? */
3935 break;
3936 }
3937 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003938}
3939
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08003940static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
3941 unsigned long long *mem_limit, unsigned long long *memsw_limit)
3942{
3943 struct cgroup *cgroup;
3944 unsigned long long min_limit, min_memsw_limit, tmp;
3945
3946 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3947 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3948 cgroup = memcg->css.cgroup;
3949 if (!memcg->use_hierarchy)
3950 goto out;
3951
3952 while (cgroup->parent) {
3953 cgroup = cgroup->parent;
3954 memcg = mem_cgroup_from_cont(cgroup);
3955 if (!memcg->use_hierarchy)
3956 break;
3957 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
3958 min_limit = min(min_limit, tmp);
3959 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3960 min_memsw_limit = min(min_memsw_limit, tmp);
3961 }
3962out:
3963 *mem_limit = min_limit;
3964 *memsw_limit = min_memsw_limit;
3965 return;
3966}
3967
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003968static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003969{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003970 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003971 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003972
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003973 memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003974 type = MEMFILE_TYPE(event);
3975 name = MEMFILE_ATTR(event);
3976 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003977 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003978 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003979 res_counter_reset_max(&memcg->res);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003980 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003981 res_counter_reset_max(&memcg->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003982 break;
3983 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003984 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003985 res_counter_reset_failcnt(&memcg->res);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08003986 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003987 res_counter_reset_failcnt(&memcg->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003988 break;
3989 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07003990
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07003991 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003992}
3993
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003994static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
3995 struct cftype *cft)
3996{
3997 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
3998}
3999
Daisuke Nishimura02491442010-03-10 15:22:17 -08004000#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004001static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
4002 struct cftype *cft, u64 val)
4003{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004004 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004005
4006 if (val >= (1 << NR_MOVE_TYPE))
4007 return -EINVAL;
4008 /*
4009 * We check this value several times in both in can_attach() and
4010 * attach(), so we need cgroup lock to prevent this value from being
4011 * inconsistent.
4012 */
4013 cgroup_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004014 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004015 cgroup_unlock();
4016
4017 return 0;
4018}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004019#else
4020static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
4021 struct cftype *cft, u64 val)
4022{
4023 return -ENOSYS;
4024}
4025#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004026
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004027
4028/* For read statistics */
4029enum {
4030 MCS_CACHE,
4031 MCS_RSS,
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08004032 MCS_FILE_MAPPED,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004033 MCS_PGPGIN,
4034 MCS_PGPGOUT,
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004035 MCS_SWAP,
Ying Han456f9982011-05-26 16:25:38 -07004036 MCS_PGFAULT,
4037 MCS_PGMAJFAULT,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004038 MCS_INACTIVE_ANON,
4039 MCS_ACTIVE_ANON,
4040 MCS_INACTIVE_FILE,
4041 MCS_ACTIVE_FILE,
4042 MCS_UNEVICTABLE,
4043 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004044};
4045
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004046struct mcs_total_stat {
4047 s64 stat[NR_MCS_STAT];
4048};
4049
4050struct {
4051 char *local_name;
4052 char *total_name;
4053} memcg_stat_strings[NR_MCS_STAT] = {
4054 {"cache", "total_cache"},
4055 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07004056 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004057 {"pgpgin", "total_pgpgin"},
4058 {"pgpgout", "total_pgpgout"},
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004059 {"swap", "total_swap"},
Ying Han456f9982011-05-26 16:25:38 -07004060 {"pgfault", "total_pgfault"},
4061 {"pgmajfault", "total_pgmajfault"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004062 {"inactive_anon", "total_inactive_anon"},
4063 {"active_anon", "total_active_anon"},
4064 {"inactive_file", "total_inactive_file"},
4065 {"active_file", "total_active_file"},
4066 {"unevictable", "total_unevictable"}
4067};
4068
4069
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004070static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004071mem_cgroup_get_local_stat(struct mem_cgroup *memcg, struct mcs_total_stat *s)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004072{
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004073 s64 val;
4074
4075 /* per cpu stat */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004076 val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_CACHE);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004077 s->stat[MCS_CACHE] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004078 val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_RSS);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004079 s->stat[MCS_RSS] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004080 val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_FILE_MAPPED);
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08004081 s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004082 val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGPGIN);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004083 s->stat[MCS_PGPGIN] += val;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004084 val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGPGOUT);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004085 s->stat[MCS_PGPGOUT] += val;
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004086 if (do_swap_account) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004087 val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_SWAPOUT);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004088 s->stat[MCS_SWAP] += val * PAGE_SIZE;
4089 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004090 val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGFAULT);
Ying Han456f9982011-05-26 16:25:38 -07004091 s->stat[MCS_PGFAULT] += val;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004092 val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGMAJFAULT);
Ying Han456f9982011-05-26 16:25:38 -07004093 s->stat[MCS_PGMAJFAULT] += val;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004094
4095 /* per zone stat */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004096 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_ANON));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004097 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004098 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_ANON));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004099 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004100 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_FILE));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004101 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004102 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_FILE));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004103 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004104 val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE));
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004105 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004106}
4107
4108static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004109mem_cgroup_get_total_stat(struct mem_cgroup *memcg, struct mcs_total_stat *s)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004110{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004111 struct mem_cgroup *iter;
4112
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004113 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004114 mem_cgroup_get_local_stat(iter, s);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004115}
4116
Ying Han406eb0c2011-05-26 16:25:37 -07004117#ifdef CONFIG_NUMA
4118static int mem_control_numa_stat_show(struct seq_file *m, void *arg)
4119{
4120 int nid;
4121 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
4122 unsigned long node_nr;
4123 struct cgroup *cont = m->private;
4124 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
4125
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004126 total_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07004127 seq_printf(m, "total=%lu", total_nr);
4128 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004129 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07004130 seq_printf(m, " N%d=%lu", nid, node_nr);
4131 }
4132 seq_putc(m, '\n');
4133
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004134 file_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07004135 seq_printf(m, "file=%lu", file_nr);
4136 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004137 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
4138 LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07004139 seq_printf(m, " N%d=%lu", nid, node_nr);
4140 }
4141 seq_putc(m, '\n');
4142
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004143 anon_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07004144 seq_printf(m, "anon=%lu", anon_nr);
4145 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004146 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
4147 LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07004148 seq_printf(m, " N%d=%lu", nid, node_nr);
4149 }
4150 seq_putc(m, '\n');
4151
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004152 unevictable_nr = mem_cgroup_nr_lru_pages(mem_cont, BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07004153 seq_printf(m, "unevictable=%lu", unevictable_nr);
4154 for_each_node_state(nid, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004155 node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid,
4156 BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07004157 seq_printf(m, " N%d=%lu", nid, node_nr);
4158 }
4159 seq_putc(m, '\n');
4160 return 0;
4161}
4162#endif /* CONFIG_NUMA */
4163
Paul Menagec64745c2008-04-29 01:00:02 -07004164static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
4165 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004166{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004167 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004168 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004169 int i;
4170
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004171 memset(&mystat, 0, sizeof(mystat));
4172 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004173
Ying Han406eb0c2011-05-26 16:25:37 -07004174
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004175 for (i = 0; i < NR_MCS_STAT; i++) {
4176 if (i == MCS_SWAP && !do_swap_account)
4177 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004178 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004179 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004180
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004181 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004182 {
4183 unsigned long long limit, memsw_limit;
4184 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
4185 cb->fill(cb, "hierarchical_memory_limit", limit);
4186 if (do_swap_account)
4187 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
4188 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004189
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004190 memset(&mystat, 0, sizeof(mystat));
4191 mem_cgroup_get_total_stat(mem_cont, &mystat);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004192 for (i = 0; i < NR_MCS_STAT; i++) {
4193 if (i == MCS_SWAP && !do_swap_account)
4194 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004195 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004196 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004197
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004198#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004199 {
4200 int nid, zid;
4201 struct mem_cgroup_per_zone *mz;
4202 unsigned long recent_rotated[2] = {0, 0};
4203 unsigned long recent_scanned[2] = {0, 0};
4204
4205 for_each_online_node(nid)
4206 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
4207 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
4208
4209 recent_rotated[0] +=
4210 mz->reclaim_stat.recent_rotated[0];
4211 recent_rotated[1] +=
4212 mz->reclaim_stat.recent_rotated[1];
4213 recent_scanned[0] +=
4214 mz->reclaim_stat.recent_scanned[0];
4215 recent_scanned[1] +=
4216 mz->reclaim_stat.recent_scanned[1];
4217 }
4218 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
4219 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
4220 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
4221 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
4222 }
4223#endif
4224
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004225 return 0;
4226}
4227
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004228static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
4229{
4230 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4231
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07004232 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004233}
4234
4235static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
4236 u64 val)
4237{
4238 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4239 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08004240
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004241 if (val > 100)
4242 return -EINVAL;
4243
4244 if (cgrp->parent == NULL)
4245 return -EINVAL;
4246
4247 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08004248
4249 cgroup_lock();
4250
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004251 /* If under hierarchy, only empty-root can set this value */
4252 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08004253 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
4254 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004255 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08004256 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004257
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004258 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004259
Li Zefan068b38c2009-01-15 13:51:26 -08004260 cgroup_unlock();
4261
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004262 return 0;
4263}
4264
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004265static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
4266{
4267 struct mem_cgroup_threshold_ary *t;
4268 u64 usage;
4269 int i;
4270
4271 rcu_read_lock();
4272 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004273 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004274 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004275 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004276
4277 if (!t)
4278 goto unlock;
4279
4280 usage = mem_cgroup_usage(memcg, swap);
4281
4282 /*
4283 * current_threshold points to threshold just below usage.
4284 * If it's not true, a threshold was crossed after last
4285 * call of __mem_cgroup_threshold().
4286 */
Phil Carmody5407a562010-05-26 14:42:42 -07004287 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004288
4289 /*
4290 * Iterate backward over array of thresholds starting from
4291 * current_threshold and check if a threshold is crossed.
4292 * If none of thresholds below usage is crossed, we read
4293 * only one element of the array here.
4294 */
4295 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
4296 eventfd_signal(t->entries[i].eventfd, 1);
4297
4298 /* i = current_threshold + 1 */
4299 i++;
4300
4301 /*
4302 * Iterate forward over array of thresholds starting from
4303 * current_threshold+1 and check if a threshold is crossed.
4304 * If none of thresholds above usage is crossed, we read
4305 * only one element of the array here.
4306 */
4307 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
4308 eventfd_signal(t->entries[i].eventfd, 1);
4309
4310 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07004311 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004312unlock:
4313 rcu_read_unlock();
4314}
4315
4316static void mem_cgroup_threshold(struct mem_cgroup *memcg)
4317{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07004318 while (memcg) {
4319 __mem_cgroup_threshold(memcg, false);
4320 if (do_swap_account)
4321 __mem_cgroup_threshold(memcg, true);
4322
4323 memcg = parent_mem_cgroup(memcg);
4324 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004325}
4326
4327static int compare_thresholds(const void *a, const void *b)
4328{
4329 const struct mem_cgroup_threshold *_a = a;
4330 const struct mem_cgroup_threshold *_b = b;
4331
4332 return _a->threshold - _b->threshold;
4333}
4334
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004335static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004336{
4337 struct mem_cgroup_eventfd_list *ev;
4338
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004339 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004340 eventfd_signal(ev->eventfd, 1);
4341 return 0;
4342}
4343
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004344static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004345{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004346 struct mem_cgroup *iter;
4347
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004348 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004349 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004350}
4351
4352static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
4353 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004354{
4355 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004356 struct mem_cgroup_thresholds *thresholds;
4357 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004358 int type = MEMFILE_TYPE(cft->private);
4359 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004360 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004361
4362 ret = res_counter_memparse_write_strategy(args, &threshold);
4363 if (ret)
4364 return ret;
4365
4366 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004367
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004368 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004369 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004370 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004371 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004372 else
4373 BUG();
4374
4375 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4376
4377 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004378 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004379 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4380
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004381 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004382
4383 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004384 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004385 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004386 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004387 ret = -ENOMEM;
4388 goto unlock;
4389 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004390 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004391
4392 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004393 if (thresholds->primary) {
4394 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004395 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004396 }
4397
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004398 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004399 new->entries[size - 1].eventfd = eventfd;
4400 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004401
4402 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004403 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004404 compare_thresholds, NULL);
4405
4406 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004407 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004408 for (i = 0; i < size; i++) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004409 if (new->entries[i].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004410 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004411 * new->current_threshold will not be used until
4412 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004413 * it here.
4414 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004415 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004416 }
4417 }
4418
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004419 /* Free old spare buffer and save old primary buffer as spare */
4420 kfree(thresholds->spare);
4421 thresholds->spare = thresholds->primary;
4422
4423 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004424
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004425 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004426 synchronize_rcu();
4427
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004428unlock:
4429 mutex_unlock(&memcg->thresholds_lock);
4430
4431 return ret;
4432}
4433
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004434static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004435 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004436{
4437 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004438 struct mem_cgroup_thresholds *thresholds;
4439 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004440 int type = MEMFILE_TYPE(cft->private);
4441 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004442 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004443
4444 mutex_lock(&memcg->thresholds_lock);
4445 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004446 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004447 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004448 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004449 else
4450 BUG();
4451
4452 /*
4453 * Something went wrong if we trying to unregister a threshold
4454 * if we don't have thresholds
4455 */
4456 BUG_ON(!thresholds);
4457
4458 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4459
4460 /* Check if a threshold crossed before removing */
4461 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4462
4463 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004464 size = 0;
4465 for (i = 0; i < thresholds->primary->size; i++) {
4466 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004467 size++;
4468 }
4469
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004470 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004471
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004472 /* Set thresholds array to NULL if we don't have thresholds */
4473 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004474 kfree(new);
4475 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004476 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004477 }
4478
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004479 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004480
4481 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004482 new->current_threshold = -1;
4483 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
4484 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004485 continue;
4486
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004487 new->entries[j] = thresholds->primary->entries[i];
4488 if (new->entries[j].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004489 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004490 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004491 * until rcu_assign_pointer(), so it's safe to increment
4492 * it here.
4493 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004494 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004495 }
4496 j++;
4497 }
4498
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004499swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004500 /* Swap primary and spare array */
4501 thresholds->spare = thresholds->primary;
4502 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004503
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004504 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004505 synchronize_rcu();
4506
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004507 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004508}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004509
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004510static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
4511 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
4512{
4513 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4514 struct mem_cgroup_eventfd_list *event;
4515 int type = MEMFILE_TYPE(cft->private);
4516
4517 BUG_ON(type != _OOM_TYPE);
4518 event = kmalloc(sizeof(*event), GFP_KERNEL);
4519 if (!event)
4520 return -ENOMEM;
4521
Michal Hocko1af8efe2011-07-26 16:08:24 -07004522 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004523
4524 event->eventfd = eventfd;
4525 list_add(&event->list, &memcg->oom_notify);
4526
4527 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07004528 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004529 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07004530 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004531
4532 return 0;
4533}
4534
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004535static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004536 struct cftype *cft, struct eventfd_ctx *eventfd)
4537{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004538 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004539 struct mem_cgroup_eventfd_list *ev, *tmp;
4540 int type = MEMFILE_TYPE(cft->private);
4541
4542 BUG_ON(type != _OOM_TYPE);
4543
Michal Hocko1af8efe2011-07-26 16:08:24 -07004544 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004545
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004546 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004547 if (ev->eventfd == eventfd) {
4548 list_del(&ev->list);
4549 kfree(ev);
4550 }
4551 }
4552
Michal Hocko1af8efe2011-07-26 16:08:24 -07004553 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004554}
4555
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004556static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
4557 struct cftype *cft, struct cgroup_map_cb *cb)
4558{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004559 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004560
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004561 cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004562
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004563 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004564 cb->fill(cb, "under_oom", 1);
4565 else
4566 cb->fill(cb, "under_oom", 0);
4567 return 0;
4568}
4569
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004570static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
4571 struct cftype *cft, u64 val)
4572{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004573 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004574 struct mem_cgroup *parent;
4575
4576 /* cannot set to root cgroup and only 0 and 1 are allowed */
4577 if (!cgrp->parent || !((val == 0) || (val == 1)))
4578 return -EINVAL;
4579
4580 parent = mem_cgroup_from_cont(cgrp->parent);
4581
4582 cgroup_lock();
4583 /* oom-kill-disable is a flag for subhierarchy. */
4584 if ((parent->use_hierarchy) ||
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004585 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004586 cgroup_unlock();
4587 return -EINVAL;
4588 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004589 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07004590 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004591 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004592 cgroup_unlock();
4593 return 0;
4594}
4595
Ying Han406eb0c2011-05-26 16:25:37 -07004596#ifdef CONFIG_NUMA
4597static const struct file_operations mem_control_numa_stat_file_operations = {
4598 .read = seq_read,
4599 .llseek = seq_lseek,
4600 .release = single_release,
4601};
4602
4603static int mem_control_numa_stat_open(struct inode *unused, struct file *file)
4604{
4605 struct cgroup *cont = file->f_dentry->d_parent->d_fsdata;
4606
4607 file->f_op = &mem_control_numa_stat_file_operations;
4608 return single_open(file, mem_control_numa_stat_show, cont);
4609}
4610#endif /* CONFIG_NUMA */
4611
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004612static struct cftype mem_cgroup_files[] = {
4613 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004614 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004615 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07004616 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004617 .register_event = mem_cgroup_usage_register_event,
4618 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004619 },
4620 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004621 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004622 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004623 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004624 .read_u64 = mem_cgroup_read,
4625 },
4626 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004627 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004628 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07004629 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07004630 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004631 },
4632 {
Balbir Singh296c81d2009-09-23 15:56:36 -07004633 .name = "soft_limit_in_bytes",
4634 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
4635 .write_string = mem_cgroup_write,
4636 .read_u64 = mem_cgroup_read,
4637 },
4638 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004639 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004640 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004641 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07004642 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004643 },
Balbir Singh8697d332008-02-07 00:13:59 -08004644 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004645 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07004646 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004647 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004648 {
4649 .name = "force_empty",
4650 .trigger = mem_cgroup_force_empty_write,
4651 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08004652 {
4653 .name = "use_hierarchy",
4654 .write_u64 = mem_cgroup_hierarchy_write,
4655 .read_u64 = mem_cgroup_hierarchy_read,
4656 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004657 {
4658 .name = "swappiness",
4659 .read_u64 = mem_cgroup_swappiness_read,
4660 .write_u64 = mem_cgroup_swappiness_write,
4661 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004662 {
4663 .name = "move_charge_at_immigrate",
4664 .read_u64 = mem_cgroup_move_charge_read,
4665 .write_u64 = mem_cgroup_move_charge_write,
4666 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004667 {
4668 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004669 .read_map = mem_cgroup_oom_control_read,
4670 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004671 .register_event = mem_cgroup_oom_register_event,
4672 .unregister_event = mem_cgroup_oom_unregister_event,
4673 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
4674 },
Ying Han406eb0c2011-05-26 16:25:37 -07004675#ifdef CONFIG_NUMA
4676 {
4677 .name = "numa_stat",
4678 .open = mem_control_numa_stat_open,
KAMEZAWA Hiroyuki89577122011-06-15 15:08:41 -07004679 .mode = S_IRUGO,
Ying Han406eb0c2011-05-26 16:25:37 -07004680 },
4681#endif
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004682};
4683
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004684#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4685static struct cftype memsw_cgroup_files[] = {
4686 {
4687 .name = "memsw.usage_in_bytes",
4688 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
4689 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004690 .register_event = mem_cgroup_usage_register_event,
4691 .unregister_event = mem_cgroup_usage_unregister_event,
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004692 },
4693 {
4694 .name = "memsw.max_usage_in_bytes",
4695 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
4696 .trigger = mem_cgroup_reset,
4697 .read_u64 = mem_cgroup_read,
4698 },
4699 {
4700 .name = "memsw.limit_in_bytes",
4701 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
4702 .write_string = mem_cgroup_write,
4703 .read_u64 = mem_cgroup_read,
4704 },
4705 {
4706 .name = "memsw.failcnt",
4707 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
4708 .trigger = mem_cgroup_reset,
4709 .read_u64 = mem_cgroup_read,
4710 },
4711};
4712
4713static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
4714{
4715 if (!do_swap_account)
4716 return 0;
4717 return cgroup_add_files(cont, ss, memsw_cgroup_files,
4718 ARRAY_SIZE(memsw_cgroup_files));
4719};
4720#else
4721static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
4722{
4723 return 0;
4724}
4725#endif
4726
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004727static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004728{
4729 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004730 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07004731 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004732 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004733 /*
4734 * This routine is called against possible nodes.
4735 * But it's BUG to call kmalloc() against offline node.
4736 *
4737 * TODO: this routine can waste much memory for nodes which will
4738 * never be onlined. It's better to use memory hotplug callback
4739 * function.
4740 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004741 if (!node_state(node, N_NORMAL_MEMORY))
4742 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08004743 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004744 if (!pn)
4745 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004746
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004747 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4748 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07004749 for_each_lru(l)
4750 INIT_LIST_HEAD(&mz->lists[l]);
Balbir Singhf64c3f52009-09-23 15:56:37 -07004751 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07004752 mz->on_tree = false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004753 mz->mem = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004754 }
Igor Mammedov0a619e52011-11-02 13:38:21 -07004755 memcg->info.nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004756 return 0;
4757}
4758
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004759static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004760{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004761 kfree(memcg->info.nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004762}
4763
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004764static struct mem_cgroup *mem_cgroup_alloc(void)
4765{
4766 struct mem_cgroup *mem;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004767 int size = sizeof(struct mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004768
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004769 /* Can be very big if MAX_NUMNODES is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08004770 if (size < PAGE_SIZE)
Jesper Juhl17295c82011-01-13 15:47:42 -08004771 mem = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004772 else
Jesper Juhl17295c82011-01-13 15:47:42 -08004773 mem = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004774
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07004775 if (!mem)
4776 return NULL;
4777
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004778 mem->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004779 if (!mem->stat)
4780 goto out_free;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07004781 spin_lock_init(&mem->pcp_counter_lock);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004782 return mem;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004783
4784out_free:
4785 if (size < PAGE_SIZE)
4786 kfree(mem);
4787 else
4788 vfree(mem);
4789 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004790}
4791
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004792/*
4793 * At destroying mem_cgroup, references from swap_cgroup can remain.
4794 * (scanning all at force_empty is too costly...)
4795 *
4796 * Instead of clearing all references at force_empty, we remember
4797 * the number of reference from swap_cgroup and free mem_cgroup when
4798 * it goes down to 0.
4799 *
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004800 * Removal of cgroup itself succeeds regardless of refs from swap.
4801 */
4802
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004803static void __mem_cgroup_free(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004804{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004805 int node;
4806
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004807 mem_cgroup_remove_from_trees(memcg);
4808 free_css_id(&mem_cgroup_subsys, &memcg->css);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004809
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004810 for_each_node_state(node, N_POSSIBLE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004811 free_mem_cgroup_per_zone_info(memcg, node);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004812
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004813 free_percpu(memcg->stat);
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004814 if (sizeof(struct mem_cgroup) < PAGE_SIZE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004815 kfree(memcg);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004816 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004817 vfree(memcg);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004818}
4819
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004820static void mem_cgroup_get(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004821{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004822 atomic_inc(&memcg->refcnt);
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004823}
4824
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004825static void __mem_cgroup_put(struct mem_cgroup *memcg, int count)
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004826{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004827 if (atomic_sub_and_test(count, &memcg->refcnt)) {
4828 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
4829 __mem_cgroup_free(memcg);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004830 if (parent)
4831 mem_cgroup_put(parent);
4832 }
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004833}
4834
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004835static void mem_cgroup_put(struct mem_cgroup *memcg)
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004836{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004837 __mem_cgroup_put(memcg, 1);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004838}
4839
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004840/*
4841 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
4842 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004843static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004844{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004845 if (!memcg->res.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004846 return NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004847 return mem_cgroup_from_res_counter(memcg->res.parent, res);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004848}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004849
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004850#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4851static void __init enable_swap_cgroup(void)
4852{
Hirokazu Takahashif8d66542009-01-07 18:08:02 -08004853 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004854 do_swap_account = 1;
4855}
4856#else
4857static void __init enable_swap_cgroup(void)
4858{
4859}
4860#endif
4861
Balbir Singhf64c3f52009-09-23 15:56:37 -07004862static int mem_cgroup_soft_limit_tree_init(void)
4863{
4864 struct mem_cgroup_tree_per_node *rtpn;
4865 struct mem_cgroup_tree_per_zone *rtpz;
4866 int tmp, node, zone;
4867
4868 for_each_node_state(node, N_POSSIBLE) {
4869 tmp = node;
4870 if (!node_state(node, N_NORMAL_MEMORY))
4871 tmp = -1;
4872 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
4873 if (!rtpn)
4874 return 1;
4875
4876 soft_limit_tree.rb_tree_per_node[node] = rtpn;
4877
4878 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4879 rtpz = &rtpn->rb_tree_per_zone[zone];
4880 rtpz->rb_root = RB_ROOT;
4881 spin_lock_init(&rtpz->lock);
4882 }
4883 }
4884 return 0;
4885}
4886
Li Zefan0eb253e2009-01-15 13:51:25 -08004887static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004888mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
4889{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004890 struct mem_cgroup *memcg, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004891 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004892 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004893
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004894 memcg = mem_cgroup_alloc();
4895 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004896 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004897
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004898 for_each_node_state(node, N_POSSIBLE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004899 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004900 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004901
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004902 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004903 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004904 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004905 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004906 parent = NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004907 root_mem_cgroup = memcg;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004908 if (mem_cgroup_soft_limit_tree_init())
4909 goto free_out;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004910 for_each_possible_cpu(cpu) {
4911 struct memcg_stock_pcp *stock =
4912 &per_cpu(memcg_stock, cpu);
4913 INIT_WORK(&stock->work, drain_local_stock);
4914 }
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07004915 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004916 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004917 parent = mem_cgroup_from_cont(cont->parent);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004918 memcg->use_hierarchy = parent->use_hierarchy;
4919 memcg->oom_kill_disable = parent->oom_kill_disable;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004920 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004921
Balbir Singh18f59ea2009-01-07 18:08:07 -08004922 if (parent && parent->use_hierarchy) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004923 res_counter_init(&memcg->res, &parent->res);
4924 res_counter_init(&memcg->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004925 /*
4926 * We increment refcnt of the parent to ensure that we can
4927 * safely access it on res_counter_charge/uncharge.
4928 * This refcnt will be decremented when freeing this
4929 * mem_cgroup(see mem_cgroup_put).
4930 */
4931 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004932 } else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004933 res_counter_init(&memcg->res, NULL);
4934 res_counter_init(&memcg->memsw, NULL);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004935 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004936 memcg->last_scanned_child = 0;
4937 memcg->last_scanned_node = MAX_NUMNODES;
4938 INIT_LIST_HEAD(&memcg->oom_notify);
Balbir Singh6d61ef42009-01-07 18:08:06 -08004939
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004940 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004941 memcg->swappiness = mem_cgroup_swappiness(parent);
4942 atomic_set(&memcg->refcnt, 1);
4943 memcg->move_charge_at_immigrate = 0;
4944 mutex_init(&memcg->thresholds_lock);
4945 return &memcg->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004946free_out:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004947 __mem_cgroup_free(memcg);
Balbir Singh4b3bde42009-09-23 15:56:32 -07004948 root_mem_cgroup = NULL;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004949 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004950}
4951
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004952static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004953 struct cgroup *cont)
4954{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004955 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004956
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004957 return mem_cgroup_force_empty(memcg, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004958}
4959
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004960static void mem_cgroup_destroy(struct cgroup_subsys *ss,
4961 struct cgroup *cont)
4962{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004963 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004964
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004965 mem_cgroup_put(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004966}
4967
4968static int mem_cgroup_populate(struct cgroup_subsys *ss,
4969 struct cgroup *cont)
4970{
KAMEZAWA Hiroyuki8c7c6e32009-01-07 18:08:00 -08004971 int ret;
4972
4973 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
4974 ARRAY_SIZE(mem_cgroup_files));
4975
4976 if (!ret)
4977 ret = register_memsw_files(cont, ss);
4978 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004979}
4980
Daisuke Nishimura02491442010-03-10 15:22:17 -08004981#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004982/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004983#define PRECHARGE_COUNT_AT_ONCE 256
4984static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004985{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004986 int ret = 0;
4987 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004988 struct mem_cgroup *memcg = mc.to;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004989
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004990 if (mem_cgroup_is_root(memcg)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004991 mc.precharge += count;
4992 /* we don't need css_get for root */
4993 return ret;
4994 }
4995 /* try to charge at once */
4996 if (count > 1) {
4997 struct res_counter *dummy;
4998 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004999 * "memcg" cannot be under rmdir() because we've already checked
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005000 * by cgroup_lock_live_cgroup() that it is not removed and we
5001 * are still under the same cgroup_mutex. So we can postpone
5002 * css_get().
5003 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005004 if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy))
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005005 goto one_by_one;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005006 if (do_swap_account && res_counter_charge(&memcg->memsw,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005007 PAGE_SIZE * count, &dummy)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005008 res_counter_uncharge(&memcg->res, PAGE_SIZE * count);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005009 goto one_by_one;
5010 }
5011 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005012 return ret;
5013 }
5014one_by_one:
5015 /* fall back to one by one charge */
5016 while (count--) {
5017 if (signal_pending(current)) {
5018 ret = -EINTR;
5019 break;
5020 }
5021 if (!batch_count--) {
5022 batch_count = PRECHARGE_COUNT_AT_ONCE;
5023 cond_resched();
5024 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005025 ret = __mem_cgroup_try_charge(NULL,
5026 GFP_KERNEL, 1, &memcg, false);
5027 if (ret || !memcg)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005028 /* mem_cgroup_clear_mc() will do uncharge later */
5029 return -ENOMEM;
5030 mc.precharge++;
5031 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005032 return ret;
5033}
5034
5035/**
5036 * is_target_pte_for_mc - check a pte whether it is valid for move charge
5037 * @vma: the vma the pte to be checked belongs
5038 * @addr: the address corresponding to the pte to be checked
5039 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08005040 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005041 *
5042 * Returns
5043 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
5044 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
5045 * move charge. if @target is not NULL, the page is stored in target->page
5046 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08005047 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
5048 * target for charge migration. if @target is not NULL, the entry is stored
5049 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005050 *
5051 * Called with pte lock held.
5052 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005053union mc_target {
5054 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005055 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005056};
5057
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005058enum mc_target_type {
5059 MC_TARGET_NONE, /* not used */
5060 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08005061 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005062};
5063
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005064static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
5065 unsigned long addr, pte_t ptent)
5066{
5067 struct page *page = vm_normal_page(vma, addr, ptent);
5068
5069 if (!page || !page_mapped(page))
5070 return NULL;
5071 if (PageAnon(page)) {
5072 /* we don't move shared anon */
5073 if (!move_anon() || page_mapcount(page) > 2)
5074 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005075 } else if (!move_file())
5076 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005077 return NULL;
5078 if (!get_page_unless_zero(page))
5079 return NULL;
5080
5081 return page;
5082}
5083
5084static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5085 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5086{
5087 int usage_count;
5088 struct page *page = NULL;
5089 swp_entry_t ent = pte_to_swp_entry(ptent);
5090
5091 if (!move_anon() || non_swap_entry(ent))
5092 return NULL;
5093 usage_count = mem_cgroup_count_swap_user(ent, &page);
5094 if (usage_count > 1) { /* we don't move shared anon */
5095 if (page)
5096 put_page(page);
5097 return NULL;
5098 }
5099 if (do_swap_account)
5100 entry->val = ent.val;
5101
5102 return page;
5103}
5104
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005105static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
5106 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5107{
5108 struct page *page = NULL;
5109 struct inode *inode;
5110 struct address_space *mapping;
5111 pgoff_t pgoff;
5112
5113 if (!vma->vm_file) /* anonymous vma */
5114 return NULL;
5115 if (!move_file())
5116 return NULL;
5117
5118 inode = vma->vm_file->f_path.dentry->d_inode;
5119 mapping = vma->vm_file->f_mapping;
5120 if (pte_none(ptent))
5121 pgoff = linear_page_index(vma, addr);
5122 else /* pte_file(ptent) is true */
5123 pgoff = pte_to_pgoff(ptent);
5124
5125 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005126 page = find_get_page(mapping, pgoff);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005127
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005128#ifdef CONFIG_SWAP
5129 /* shmem/tmpfs may report page out on swap: account for that too. */
5130 if (radix_tree_exceptional_entry(page)) {
5131 swp_entry_t swap = radix_to_swp_entry(page);
5132 if (do_swap_account)
5133 *entry = swap;
5134 page = find_get_page(&swapper_space, swap.val);
5135 }
5136#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005137 return page;
5138}
5139
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005140static int is_target_pte_for_mc(struct vm_area_struct *vma,
5141 unsigned long addr, pte_t ptent, union mc_target *target)
5142{
Daisuke Nishimura02491442010-03-10 15:22:17 -08005143 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005144 struct page_cgroup *pc;
5145 int ret = 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005146 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005147
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005148 if (pte_present(ptent))
5149 page = mc_handle_present_pte(vma, addr, ptent);
5150 else if (is_swap_pte(ptent))
5151 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005152 else if (pte_none(ptent) || pte_file(ptent))
5153 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005154
5155 if (!page && !ent.val)
Daisuke Nishimura02491442010-03-10 15:22:17 -08005156 return 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005157 if (page) {
5158 pc = lookup_page_cgroup(page);
5159 /*
5160 * Do only loose check w/o page_cgroup lock.
5161 * mem_cgroup_move_account() checks the pc is valid or not under
5162 * the lock.
5163 */
5164 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
5165 ret = MC_TARGET_PAGE;
5166 if (target)
5167 target->page = page;
5168 }
5169 if (!ret || !target)
5170 put_page(page);
5171 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005172 /* There is a swap entry and a page doesn't exist or isn't charged */
5173 if (ent.val && !ret &&
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005174 css_id(&mc.from->css) == lookup_swap_cgroup(ent)) {
5175 ret = MC_TARGET_SWAP;
5176 if (target)
5177 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005178 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005179 return ret;
5180}
5181
5182static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
5183 unsigned long addr, unsigned long end,
5184 struct mm_walk *walk)
5185{
5186 struct vm_area_struct *vma = walk->private;
5187 pte_t *pte;
5188 spinlock_t *ptl;
5189
Dave Hansen03319322011-03-22 16:32:56 -07005190 split_huge_page_pmd(walk->mm, pmd);
5191
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005192 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5193 for (; addr != end; pte++, addr += PAGE_SIZE)
5194 if (is_target_pte_for_mc(vma, addr, *pte, NULL))
5195 mc.precharge++; /* increment precharge temporarily */
5196 pte_unmap_unlock(pte - 1, ptl);
5197 cond_resched();
5198
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005199 return 0;
5200}
5201
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005202static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
5203{
5204 unsigned long precharge;
5205 struct vm_area_struct *vma;
5206
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005207 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005208 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5209 struct mm_walk mem_cgroup_count_precharge_walk = {
5210 .pmd_entry = mem_cgroup_count_precharge_pte_range,
5211 .mm = mm,
5212 .private = vma,
5213 };
5214 if (is_vm_hugetlb_page(vma))
5215 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005216 walk_page_range(vma->vm_start, vma->vm_end,
5217 &mem_cgroup_count_precharge_walk);
5218 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005219 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005220
5221 precharge = mc.precharge;
5222 mc.precharge = 0;
5223
5224 return precharge;
5225}
5226
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005227static int mem_cgroup_precharge_mc(struct mm_struct *mm)
5228{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005229 unsigned long precharge = mem_cgroup_count_precharge(mm);
5230
5231 VM_BUG_ON(mc.moving_task);
5232 mc.moving_task = current;
5233 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005234}
5235
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005236/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
5237static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005238{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005239 struct mem_cgroup *from = mc.from;
5240 struct mem_cgroup *to = mc.to;
5241
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005242 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005243 if (mc.precharge) {
5244 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
5245 mc.precharge = 0;
5246 }
5247 /*
5248 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
5249 * we must uncharge here.
5250 */
5251 if (mc.moved_charge) {
5252 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
5253 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005254 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005255 /* we must fixup refcnts and charges */
5256 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005257 /* uncharge swap account from the old cgroup */
5258 if (!mem_cgroup_is_root(mc.from))
5259 res_counter_uncharge(&mc.from->memsw,
5260 PAGE_SIZE * mc.moved_swap);
5261 __mem_cgroup_put(mc.from, mc.moved_swap);
5262
5263 if (!mem_cgroup_is_root(mc.to)) {
5264 /*
5265 * we charged both to->res and to->memsw, so we should
5266 * uncharge to->res.
5267 */
5268 res_counter_uncharge(&mc.to->res,
5269 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005270 }
5271 /* we've already done mem_cgroup_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005272 mc.moved_swap = 0;
5273 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005274 memcg_oom_recover(from);
5275 memcg_oom_recover(to);
5276 wake_up_all(&mc.waitq);
5277}
5278
5279static void mem_cgroup_clear_mc(void)
5280{
5281 struct mem_cgroup *from = mc.from;
5282
5283 /*
5284 * we must clear moving_task before waking up waiters at the end of
5285 * task migration.
5286 */
5287 mc.moving_task = NULL;
5288 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005289 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005290 mc.from = NULL;
5291 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005292 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005293 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005294}
5295
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005296static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
5297 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005298 struct task_struct *p)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005299{
5300 int ret = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005301 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005302
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005303 if (memcg->move_charge_at_immigrate) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005304 struct mm_struct *mm;
5305 struct mem_cgroup *from = mem_cgroup_from_task(p);
5306
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005307 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005308
5309 mm = get_task_mm(p);
5310 if (!mm)
5311 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005312 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005313 if (mm->owner == p) {
5314 VM_BUG_ON(mc.from);
5315 VM_BUG_ON(mc.to);
5316 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005317 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005318 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005319 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005320 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005321 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005322 mc.to = memcg;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005323 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005324 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005325
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005326 ret = mem_cgroup_precharge_mc(mm);
5327 if (ret)
5328 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005329 }
5330 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005331 }
5332 return ret;
5333}
5334
5335static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
5336 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005337 struct task_struct *p)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005338{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005339 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005340}
5341
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005342static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
5343 unsigned long addr, unsigned long end,
5344 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005345{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005346 int ret = 0;
5347 struct vm_area_struct *vma = walk->private;
5348 pte_t *pte;
5349 spinlock_t *ptl;
5350
Dave Hansen03319322011-03-22 16:32:56 -07005351 split_huge_page_pmd(walk->mm, pmd);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005352retry:
5353 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5354 for (; addr != end; addr += PAGE_SIZE) {
5355 pte_t ptent = *(pte++);
5356 union mc_target target;
5357 int type;
5358 struct page *page;
5359 struct page_cgroup *pc;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005360 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005361
5362 if (!mc.precharge)
5363 break;
5364
5365 type = is_target_pte_for_mc(vma, addr, ptent, &target);
5366 switch (type) {
5367 case MC_TARGET_PAGE:
5368 page = target.page;
5369 if (isolate_lru_page(page))
5370 goto put;
5371 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07005372 if (!mem_cgroup_move_account(page, 1, pc,
5373 mc.from, mc.to, false)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005374 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005375 /* we uncharge from mc.from later. */
5376 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005377 }
5378 putback_lru_page(page);
5379put: /* is_target_pte_for_mc() gets the page */
5380 put_page(page);
5381 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005382 case MC_TARGET_SWAP:
5383 ent = target.ent;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005384 if (!mem_cgroup_move_swap_account(ent,
5385 mc.from, mc.to, false)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08005386 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005387 /* we fixup refcnts and charges later. */
5388 mc.moved_swap++;
5389 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08005390 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005391 default:
5392 break;
5393 }
5394 }
5395 pte_unmap_unlock(pte - 1, ptl);
5396 cond_resched();
5397
5398 if (addr != end) {
5399 /*
5400 * We have consumed all precharges we got in can_attach().
5401 * We try charge one by one, but don't do any additional
5402 * charges to mc.to if we have failed in charge once in attach()
5403 * phase.
5404 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005405 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005406 if (!ret)
5407 goto retry;
5408 }
5409
5410 return ret;
5411}
5412
5413static void mem_cgroup_move_charge(struct mm_struct *mm)
5414{
5415 struct vm_area_struct *vma;
5416
5417 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005418retry:
5419 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
5420 /*
5421 * Someone who are holding the mmap_sem might be waiting in
5422 * waitq. So we cancel all extra charges, wake up all waiters,
5423 * and retry. Because we cancel precharges, we might not be able
5424 * to move enough charges, but moving charge is a best-effort
5425 * feature anyway, so it wouldn't be a big problem.
5426 */
5427 __mem_cgroup_clear_mc();
5428 cond_resched();
5429 goto retry;
5430 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005431 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5432 int ret;
5433 struct mm_walk mem_cgroup_move_charge_walk = {
5434 .pmd_entry = mem_cgroup_move_charge_pte_range,
5435 .mm = mm,
5436 .private = vma,
5437 };
5438 if (is_vm_hugetlb_page(vma))
5439 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005440 ret = walk_page_range(vma->vm_start, vma->vm_end,
5441 &mem_cgroup_move_charge_walk);
5442 if (ret)
5443 /*
5444 * means we have consumed all precharges and failed in
5445 * doing additional charge. Just abandon here.
5446 */
5447 break;
5448 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005449 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005450}
5451
Balbir Singh67e465a2008-02-07 00:13:54 -08005452static void mem_cgroup_move_task(struct cgroup_subsys *ss,
5453 struct cgroup *cont,
5454 struct cgroup *old_cont,
Ben Blumf780bdb2011-05-26 16:25:19 -07005455 struct task_struct *p)
Balbir Singh67e465a2008-02-07 00:13:54 -08005456{
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005457 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005458
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005459 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005460 if (mc.to)
5461 mem_cgroup_move_charge(mm);
5462 put_swap_token(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005463 mmput(mm);
5464 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005465 if (mc.to)
5466 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08005467}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005468#else /* !CONFIG_MMU */
5469static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
5470 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005471 struct task_struct *p)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005472{
5473 return 0;
5474}
5475static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
5476 struct cgroup *cgroup,
Ben Blumf780bdb2011-05-26 16:25:19 -07005477 struct task_struct *p)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005478{
5479}
5480static void mem_cgroup_move_task(struct cgroup_subsys *ss,
5481 struct cgroup *cont,
5482 struct cgroup *old_cont,
Ben Blumf780bdb2011-05-26 16:25:19 -07005483 struct task_struct *p)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005484{
5485}
5486#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08005487
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005488struct cgroup_subsys mem_cgroup_subsys = {
5489 .name = "memory",
5490 .subsys_id = mem_cgroup_subsys_id,
5491 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005492 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005493 .destroy = mem_cgroup_destroy,
5494 .populate = mem_cgroup_populate,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005495 .can_attach = mem_cgroup_can_attach,
5496 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08005497 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005498 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005499 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005500};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005501
5502#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08005503static int __init enable_swap_account(char *s)
5504{
5505 /* consider enabled if no parameter or 1 is given */
Michal Hockoa2c89902011-05-24 17:12:50 -07005506 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08005507 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07005508 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08005509 really_do_swap_account = 0;
5510 return 1;
5511}
Michal Hockoa2c89902011-05-24 17:12:50 -07005512__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005513
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005514#endif