blob: b9afa060b8d62feb4da918cc84987f0a91f5f56f [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 *
Glauber Costa7ae1e1d2012-12-18 14:21:56 -080013 * Kernel Memory Controller
14 * Copyright (C) 2012 Parallels Inc. and Google Inc.
15 * Authors: Glauber Costa and Suleiman Souhlal
16 *
Balbir Singh8cdea7c2008-02-07 00:13:50 -080017 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 */
27
28#include <linux/res_counter.h>
29#include <linux/memcontrol.h>
30#include <linux/cgroup.h>
Pavel Emelianov78fb7462008-02-07 00:13:51 -080031#include <linux/mm.h>
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -080032#include <linux/hugetlb.h>
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -080033#include <linux/pagemap.h>
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080034#include <linux/smp.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080035#include <linux/page-flags.h>
Balbir Singh66e17072008-02-07 00:13:56 -080036#include <linux/backing-dev.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080037#include <linux/bit_spinlock.h>
38#include <linux/rcupdate.h>
Balbir Singhe2224322009-04-02 16:57:39 -070039#include <linux/limits.h>
Paul Gortmakerb9e15ba2011-05-26 16:00:52 -040040#include <linux/export.h>
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -080041#include <linux/mutex.h>
Balbir Singhf64c3f52009-09-23 15:56:37 -070042#include <linux/rbtree.h>
Balbir Singhb6ac57d2008-04-29 01:00:19 -070043#include <linux/slab.h>
Balbir Singh66e17072008-02-07 00:13:56 -080044#include <linux/swap.h>
Daisuke Nishimura02491442010-03-10 15:22:17 -080045#include <linux/swapops.h>
Balbir Singh66e17072008-02-07 00:13:56 -080046#include <linux/spinlock.h>
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -080047#include <linux/eventfd.h>
48#include <linux/sort.h>
Balbir Singh66e17072008-02-07 00:13:56 -080049#include <linux/fs.h>
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -080050#include <linux/seq_file.h>
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -070051#include <linux/vmalloc.h>
Christoph Lameterb69408e2008-10-18 20:26:14 -070052#include <linux/mm_inline.h>
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070053#include <linux/page_cgroup.h>
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -080054#include <linux/cpu.h>
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -070055#include <linux/oom.h>
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -080056#include "internal.h"
Glauber Costad1a4c0b2011-12-11 21:47:04 +000057#include <net/sock.h>
Michal Hocko4bd2c1e2012-10-08 16:33:10 -070058#include <net/ip.h>
Glauber Costad1a4c0b2011-12-11 21:47:04 +000059#include <net/tcp_memcontrol.h>
Balbir Singh8cdea7c2008-02-07 00:13:50 -080060
Balbir Singh8697d332008-02-07 00:13:59 -080061#include <asm/uaccess.h>
62
KOSAKI Motohirocc8e9702010-08-09 17:19:57 -070063#include <trace/events/vmscan.h>
64
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070065struct cgroup_subsys mem_cgroup_subsys __read_mostly;
David Rientjes68ae5642012-12-12 13:51:57 -080066EXPORT_SYMBOL(mem_cgroup_subsys);
67
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070068#define MEM_CGROUP_RECLAIM_RETRIES 5
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -070069static struct mem_cgroup *root_mem_cgroup __read_mostly;
Balbir Singh8cdea7c2008-02-07 00:13:50 -080070
Andrew Mortonc255a452012-07-31 16:43:02 -070071#ifdef CONFIG_MEMCG_SWAP
Li Zefan338c8432009-06-17 16:27:15 -070072/* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080073int do_swap_account __read_mostly;
Michal Hockoa42c3902010-11-24 12:57:08 -080074
75/* for remember boot option*/
Andrew Mortonc255a452012-07-31 16:43:02 -070076#ifdef CONFIG_MEMCG_SWAP_ENABLED
Michal Hockoa42c3902010-11-24 12:57:08 -080077static int really_do_swap_account __initdata = 1;
78#else
79static int really_do_swap_account __initdata = 0;
80#endif
81
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080082#else
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -070083#define do_swap_account 0
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080084#endif
85
86
Balbir Singh8cdea7c2008-02-07 00:13:50 -080087/*
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080088 * Statistics for memory cgroup.
89 */
90enum mem_cgroup_stat_index {
91 /*
92 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
93 */
94 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
Balbir Singhd69b0422009-06-17 16:26:34 -070095 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -080096 MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -070097 MEM_CGROUP_STAT_SWAP, /* # of pages, swapped out */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080098 MEM_CGROUP_STAT_NSTATS,
99};
100
Johannes Weineraf7c4b02012-05-29 15:07:08 -0700101static const char * const mem_cgroup_stat_names[] = {
102 "cache",
103 "rss",
104 "mapped_file",
105 "swap",
106};
107
Johannes Weinere9f89742011-03-23 16:42:37 -0700108enum mem_cgroup_events_index {
109 MEM_CGROUP_EVENTS_PGPGIN, /* # of pages paged in */
110 MEM_CGROUP_EVENTS_PGPGOUT, /* # of pages paged out */
Ying Han456f9982011-05-26 16:25:38 -0700111 MEM_CGROUP_EVENTS_PGFAULT, /* # of page-faults */
112 MEM_CGROUP_EVENTS_PGMAJFAULT, /* # of major page-faults */
Johannes Weinere9f89742011-03-23 16:42:37 -0700113 MEM_CGROUP_EVENTS_NSTATS,
114};
Johannes Weineraf7c4b02012-05-29 15:07:08 -0700115
116static const char * const mem_cgroup_events_names[] = {
117 "pgpgin",
118 "pgpgout",
119 "pgfault",
120 "pgmajfault",
121};
122
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700123/*
124 * Per memcg event counter is incremented at every pagein/pageout. With THP,
125 * it will be incremated by the number of pages. This counter is used for
126 * for trigger some periodic events. This is straightforward and better
127 * than using jiffies etc. to handle periodic memcg event.
128 */
129enum mem_cgroup_events_target {
130 MEM_CGROUP_TARGET_THRESH,
131 MEM_CGROUP_TARGET_SOFTLIMIT,
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700132 MEM_CGROUP_TARGET_NUMAINFO,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700133 MEM_CGROUP_NTARGETS,
134};
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700135#define THRESHOLDS_EVENTS_TARGET 128
136#define SOFTLIMIT_EVENTS_TARGET 1024
137#define NUMAINFO_EVENTS_TARGET 1024
Johannes Weinere9f89742011-03-23 16:42:37 -0700138
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800139struct mem_cgroup_stat_cpu {
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700140 long count[MEM_CGROUP_STAT_NSTATS];
Johannes Weinere9f89742011-03-23 16:42:37 -0700141 unsigned long events[MEM_CGROUP_EVENTS_NSTATS];
Johannes Weiner13114712012-05-29 15:07:07 -0700142 unsigned long nr_page_events;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700143 unsigned long targets[MEM_CGROUP_NTARGETS];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800144};
145
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800146struct mem_cgroup_reclaim_iter {
147 /* css_id of the last scanned hierarchy member */
148 int position;
149 /* scan generation, increased every round-trip */
150 unsigned int generation;
151};
152
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800153/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800154 * per-zone information in memory controller.
155 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800156struct mem_cgroup_per_zone {
Johannes Weiner6290df52012-01-12 17:18:10 -0800157 struct lruvec lruvec;
Hugh Dickins1eb49272012-03-21 16:34:19 -0700158 unsigned long lru_size[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800159
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800160 struct mem_cgroup_reclaim_iter reclaim_iter[DEF_PRIORITY + 1];
161
Balbir Singhf64c3f52009-09-23 15:56:37 -0700162 struct rb_node tree_node; /* RB tree node */
163 unsigned long long usage_in_excess;/* Set to the value by which */
164 /* the soft limit is exceeded*/
165 bool on_tree;
Hugh Dickinsd79154b2012-03-21 16:34:18 -0700166 struct mem_cgroup *memcg; /* Back pointer, we cannot */
Balbir Singh4e416952009-09-23 15:56:39 -0700167 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800168};
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800169
170struct mem_cgroup_per_node {
171 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
172};
173
174struct mem_cgroup_lru_info {
175 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
176};
177
178/*
Balbir Singhf64c3f52009-09-23 15:56:37 -0700179 * Cgroups above their limits are maintained in a RB-Tree, independent of
180 * their hierarchy representation
181 */
182
183struct mem_cgroup_tree_per_zone {
184 struct rb_root rb_root;
185 spinlock_t lock;
186};
187
188struct mem_cgroup_tree_per_node {
189 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
190};
191
192struct mem_cgroup_tree {
193 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
194};
195
196static struct mem_cgroup_tree soft_limit_tree __read_mostly;
197
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800198struct mem_cgroup_threshold {
199 struct eventfd_ctx *eventfd;
200 u64 threshold;
201};
202
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700203/* For threshold */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800204struct mem_cgroup_threshold_ary {
Sha Zhengju748dad32012-05-29 15:06:57 -0700205 /* An array index points to threshold just below or equal to usage. */
Phil Carmody5407a562010-05-26 14:42:42 -0700206 int current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800207 /* Size of entries[] */
208 unsigned int size;
209 /* Array of thresholds */
210 struct mem_cgroup_threshold entries[0];
211};
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700212
213struct mem_cgroup_thresholds {
214 /* Primary thresholds array */
215 struct mem_cgroup_threshold_ary *primary;
216 /*
217 * Spare threshold array.
218 * This is needed to make mem_cgroup_unregister_event() "never fail".
219 * It must be able to store at least primary->size - 1 entries.
220 */
221 struct mem_cgroup_threshold_ary *spare;
222};
223
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700224/* for OOM */
225struct mem_cgroup_eventfd_list {
226 struct list_head list;
227 struct eventfd_ctx *eventfd;
228};
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800229
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700230static void mem_cgroup_threshold(struct mem_cgroup *memcg);
231static void mem_cgroup_oom_notify(struct mem_cgroup *memcg);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800232
Balbir Singhf64c3f52009-09-23 15:56:37 -0700233/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800234 * The memory controller data structure. The memory controller controls both
235 * page cache and RSS per cgroup. We would eventually like to provide
236 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
237 * to help the administrator determine what knobs to tune.
238 *
239 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800240 * we hit the water mark. May be even add a low water mark, such that
241 * no reclaim occurs from a cgroup at it's low water mark, this is
242 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800243 */
244struct mem_cgroup {
245 struct cgroup_subsys_state css;
246 /*
247 * the counter to account for memory usage
248 */
249 struct res_counter res;
Hugh Dickins59927fb2012-03-15 15:17:07 -0700250
251 union {
252 /*
253 * the counter to account for mem+swap usage.
254 */
255 struct res_counter memsw;
256
257 /*
258 * rcu_freeing is used only when freeing struct mem_cgroup,
259 * so put it into a union to avoid wasting more memory.
260 * It must be disjoint from the css field. It could be
261 * in a union with the res field, but res plays a much
262 * larger part in mem_cgroup life than memsw, and might
263 * be of interest, even at time of free, when debugging.
264 * So share rcu_head with the less interesting memsw.
265 */
266 struct rcu_head rcu_freeing;
267 /*
Glauber Costa3afe36b2012-05-29 15:07:10 -0700268 * We also need some space for a worker in deferred freeing.
269 * By the time we call it, rcu_freeing is no longer in use.
Hugh Dickins59927fb2012-03-15 15:17:07 -0700270 */
271 struct work_struct work_freeing;
272 };
273
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800274 /*
Glauber Costa510fc4e2012-12-18 14:21:47 -0800275 * the counter to account for kernel memory usage.
276 */
277 struct res_counter kmem;
278 /*
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800279 * Per cgroup active and inactive list, similar to the
280 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800281 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800282 struct mem_cgroup_lru_info info;
Ying Han889976d2011-05-26 16:25:33 -0700283 int last_scanned_node;
284#if MAX_NUMNODES > 1
285 nodemask_t scan_nodes;
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700286 atomic_t numainfo_events;
287 atomic_t numainfo_updating;
Ying Han889976d2011-05-26 16:25:33 -0700288#endif
Balbir Singh18f59ea2009-01-07 18:08:07 -0800289 /*
290 * Should the accounting and control be hierarchical, per subtree?
291 */
292 bool use_hierarchy;
Glauber Costa510fc4e2012-12-18 14:21:47 -0800293 unsigned long kmem_account_flags; /* See KMEM_ACCOUNTED_*, below */
Michal Hocko79dfdac2011-07-26 16:08:23 -0700294
295 bool oom_lock;
296 atomic_t under_oom;
297
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800298 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800299
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -0700300 int swappiness;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -0700301 /* OOM-Killer disable */
302 int oom_kill_disable;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800303
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700304 /* set when res.limit == memsw.limit */
305 bool memsw_is_minimum;
306
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800307 /* protect arrays of thresholds */
308 struct mutex thresholds_lock;
309
310 /* thresholds for memory usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700311 struct mem_cgroup_thresholds thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700312
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800313 /* thresholds for mem+swap usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700314 struct mem_cgroup_thresholds memsw_thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700315
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700316 /* For oom notifier event fd */
317 struct list_head oom_notify;
Johannes Weiner185efc02011-09-14 16:21:58 -0700318
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800319 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800320 * Should we move charges of a task when a task is moved into this
321 * mem_cgroup ? And what type of charges should we move ?
322 */
323 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800324 /*
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -0700325 * set > 0 if pages under this cgroup are moving to other cgroup.
326 */
327 atomic_t moving_account;
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -0700328 /* taken only while moving_account > 0 */
329 spinlock_t move_lock;
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -0700330 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800331 * percpu counter.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800332 */
Kirill A. Shutemov3a7951b2012-05-29 15:06:56 -0700333 struct mem_cgroup_stat_cpu __percpu *stat;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700334 /*
335 * used when a cpu is offlined or other synchronizations
336 * See mem_cgroup_read_stat().
337 */
338 struct mem_cgroup_stat_cpu nocpu_base;
339 spinlock_t pcp_counter_lock;
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000340
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700341#if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_INET)
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000342 struct tcp_memcontrol tcp_mem;
343#endif
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800344};
345
Glauber Costa510fc4e2012-12-18 14:21:47 -0800346/* internal only representation about the status of kmem accounting. */
347enum {
348 KMEM_ACCOUNTED_ACTIVE = 0, /* accounted by this cgroup itself */
349};
350
351#define KMEM_ACCOUNTED_MASK (1 << KMEM_ACCOUNTED_ACTIVE)
352
353#ifdef CONFIG_MEMCG_KMEM
354static inline void memcg_kmem_set_active(struct mem_cgroup *memcg)
355{
356 set_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
357}
358#endif
359
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800360/* Stuffs for move charges at task migration. */
361/*
362 * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a
363 * left-shifted bitmap of these types.
364 */
365enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800366 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700367 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800368 NR_MOVE_TYPE,
369};
370
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800371/* "mc" and its members are protected by cgroup_mutex */
372static struct move_charge_struct {
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -0800373 spinlock_t lock; /* for from, to */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800374 struct mem_cgroup *from;
375 struct mem_cgroup *to;
376 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800377 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800378 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800379 struct task_struct *moving_task; /* a task moving charges */
380 wait_queue_head_t waitq; /* a waitq for other context */
381} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700382 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800383 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
384};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800385
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700386static bool move_anon(void)
387{
388 return test_bit(MOVE_CHARGE_TYPE_ANON,
389 &mc.to->move_charge_at_immigrate);
390}
391
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700392static bool move_file(void)
393{
394 return test_bit(MOVE_CHARGE_TYPE_FILE,
395 &mc.to->move_charge_at_immigrate);
396}
397
Balbir Singh4e416952009-09-23 15:56:39 -0700398/*
399 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
400 * limit reclaim to prevent infinite loops, if they ever occur.
401 */
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700402#define MEM_CGROUP_MAX_RECLAIM_LOOPS 100
403#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS 2
Balbir Singh4e416952009-09-23 15:56:39 -0700404
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800405enum charge_type {
406 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -0700407 MEM_CGROUP_CHARGE_TYPE_ANON,
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800408 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700409 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700410 NR_CHARGE_TYPE,
411};
412
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800413/* for encoding cft->private value on file */
Glauber Costa86ae53e2012-12-18 14:21:45 -0800414enum res_type {
415 _MEM,
416 _MEMSWAP,
417 _OOM_TYPE,
Glauber Costa510fc4e2012-12-18 14:21:47 -0800418 _KMEM,
Glauber Costa86ae53e2012-12-18 14:21:45 -0800419};
420
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700421#define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val))
422#define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800423#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700424/* Used for OOM nofiier */
425#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800426
Balbir Singh75822b42009-09-23 15:56:38 -0700427/*
428 * Reclaim flags for mem_cgroup_hierarchical_reclaim
429 */
430#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
431#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
432#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
433#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
434
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700435static void mem_cgroup_get(struct mem_cgroup *memcg);
436static void mem_cgroup_put(struct mem_cgroup *memcg);
Glauber Costae1aab162011-12-11 21:47:03 +0000437
Wanpeng Lib2145142012-07-31 16:46:01 -0700438static inline
439struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *s)
440{
441 return container_of(s, struct mem_cgroup, css);
442}
443
Michal Hocko7ffc0ed2012-10-08 16:33:13 -0700444static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
445{
446 return (memcg == root_mem_cgroup);
447}
448
Glauber Costae1aab162011-12-11 21:47:03 +0000449/* Writing them here to avoid exposing memcg's inner layout */
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700450#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
Glauber Costae1aab162011-12-11 21:47:03 +0000451
Glauber Costae1aab162011-12-11 21:47:03 +0000452void sock_update_memcg(struct sock *sk)
453{
Glauber Costa376be5f2012-01-20 04:57:14 +0000454 if (mem_cgroup_sockets_enabled) {
Glauber Costae1aab162011-12-11 21:47:03 +0000455 struct mem_cgroup *memcg;
Glauber Costa3f134612012-05-29 15:07:11 -0700456 struct cg_proto *cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000457
458 BUG_ON(!sk->sk_prot->proto_cgroup);
459
Glauber Costaf3f511e2012-01-05 20:16:39 +0000460 /* Socket cloning can throw us here with sk_cgrp already
461 * filled. It won't however, necessarily happen from
462 * process context. So the test for root memcg given
463 * the current task's memcg won't help us in this case.
464 *
465 * Respecting the original socket's memcg is a better
466 * decision in this case.
467 */
468 if (sk->sk_cgrp) {
469 BUG_ON(mem_cgroup_is_root(sk->sk_cgrp->memcg));
470 mem_cgroup_get(sk->sk_cgrp->memcg);
471 return;
472 }
473
Glauber Costae1aab162011-12-11 21:47:03 +0000474 rcu_read_lock();
475 memcg = mem_cgroup_from_task(current);
Glauber Costa3f134612012-05-29 15:07:11 -0700476 cg_proto = sk->sk_prot->proto_cgroup(memcg);
477 if (!mem_cgroup_is_root(memcg) && memcg_proto_active(cg_proto)) {
Glauber Costae1aab162011-12-11 21:47:03 +0000478 mem_cgroup_get(memcg);
Glauber Costa3f134612012-05-29 15:07:11 -0700479 sk->sk_cgrp = cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000480 }
481 rcu_read_unlock();
482 }
483}
484EXPORT_SYMBOL(sock_update_memcg);
485
486void sock_release_memcg(struct sock *sk)
487{
Glauber Costa376be5f2012-01-20 04:57:14 +0000488 if (mem_cgroup_sockets_enabled && sk->sk_cgrp) {
Glauber Costae1aab162011-12-11 21:47:03 +0000489 struct mem_cgroup *memcg;
490 WARN_ON(!sk->sk_cgrp->memcg);
491 memcg = sk->sk_cgrp->memcg;
492 mem_cgroup_put(memcg);
493 }
494}
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000495
496struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg)
497{
498 if (!memcg || mem_cgroup_is_root(memcg))
499 return NULL;
500
501 return &memcg->tcp_mem.cg_proto;
502}
503EXPORT_SYMBOL(tcp_proto_cgroup);
Glauber Costae1aab162011-12-11 21:47:03 +0000504
Glauber Costa3f134612012-05-29 15:07:11 -0700505static void disarm_sock_keys(struct mem_cgroup *memcg)
506{
507 if (!memcg_proto_activated(&memcg->tcp_mem.cg_proto))
508 return;
509 static_key_slow_dec(&memcg_socket_limit_enabled);
510}
511#else
512static void disarm_sock_keys(struct mem_cgroup *memcg)
513{
514}
515#endif
516
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700517static void drain_all_stock_async(struct mem_cgroup *memcg);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800518
Balbir Singhf64c3f52009-09-23 15:56:37 -0700519static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700520mem_cgroup_zoneinfo(struct mem_cgroup *memcg, int nid, int zid)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700521{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700522 return &memcg->info.nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700523}
524
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700525struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100526{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700527 return &memcg->css;
Wu Fengguangd3242362009-12-16 12:19:59 +0100528}
529
Balbir Singhf64c3f52009-09-23 15:56:37 -0700530static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700531page_cgroup_zoneinfo(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700532{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700533 int nid = page_to_nid(page);
534 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700535
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700536 return mem_cgroup_zoneinfo(memcg, nid, zid);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700537}
538
539static struct mem_cgroup_tree_per_zone *
540soft_limit_tree_node_zone(int nid, int zid)
541{
542 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
543}
544
545static struct mem_cgroup_tree_per_zone *
546soft_limit_tree_from_page(struct page *page)
547{
548 int nid = page_to_nid(page);
549 int zid = page_zonenum(page);
550
551 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
552}
553
554static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700555__mem_cgroup_insert_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700556 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700557 struct mem_cgroup_tree_per_zone *mctz,
558 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700559{
560 struct rb_node **p = &mctz->rb_root.rb_node;
561 struct rb_node *parent = NULL;
562 struct mem_cgroup_per_zone *mz_node;
563
564 if (mz->on_tree)
565 return;
566
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700567 mz->usage_in_excess = new_usage_in_excess;
568 if (!mz->usage_in_excess)
569 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700570 while (*p) {
571 parent = *p;
572 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
573 tree_node);
574 if (mz->usage_in_excess < mz_node->usage_in_excess)
575 p = &(*p)->rb_left;
576 /*
577 * We can't avoid mem cgroups that are over their soft
578 * limit by the same amount
579 */
580 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
581 p = &(*p)->rb_right;
582 }
583 rb_link_node(&mz->tree_node, parent, p);
584 rb_insert_color(&mz->tree_node, &mctz->rb_root);
585 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700586}
587
588static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700589__mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singh4e416952009-09-23 15:56:39 -0700590 struct mem_cgroup_per_zone *mz,
591 struct mem_cgroup_tree_per_zone *mctz)
592{
593 if (!mz->on_tree)
594 return;
595 rb_erase(&mz->tree_node, &mctz->rb_root);
596 mz->on_tree = false;
597}
598
599static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700600mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700601 struct mem_cgroup_per_zone *mz,
602 struct mem_cgroup_tree_per_zone *mctz)
603{
604 spin_lock(&mctz->lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700605 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700606 spin_unlock(&mctz->lock);
607}
608
Balbir Singhf64c3f52009-09-23 15:56:37 -0700609
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700610static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700611{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700612 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700613 struct mem_cgroup_per_zone *mz;
614 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700615 int nid = page_to_nid(page);
616 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700617 mctz = soft_limit_tree_from_page(page);
618
619 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700620 * Necessary to update all ancestors when hierarchy is used.
621 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700622 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700623 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
624 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
625 excess = res_counter_soft_limit_excess(&memcg->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700626 /*
627 * We have to update the tree if mz is on RB-tree or
628 * mem is over its softlimit.
629 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700630 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700631 spin_lock(&mctz->lock);
632 /* if on-tree, remove it */
633 if (mz->on_tree)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700634 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700635 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700636 * Insert again. mz->usage_in_excess will be updated.
637 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700638 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700639 __mem_cgroup_insert_exceeded(memcg, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700640 spin_unlock(&mctz->lock);
641 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700642 }
643}
644
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700645static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700646{
647 int node, zone;
648 struct mem_cgroup_per_zone *mz;
649 struct mem_cgroup_tree_per_zone *mctz;
650
Bob Liu3ed28fa2012-01-12 17:19:04 -0800651 for_each_node(node) {
Balbir Singhf64c3f52009-09-23 15:56:37 -0700652 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700653 mz = mem_cgroup_zoneinfo(memcg, node, zone);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700654 mctz = soft_limit_tree_node_zone(node, zone);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700655 mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700656 }
657 }
658}
659
Balbir Singh4e416952009-09-23 15:56:39 -0700660static struct mem_cgroup_per_zone *
661__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
662{
663 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700664 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700665
666retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700667 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700668 rightmost = rb_last(&mctz->rb_root);
669 if (!rightmost)
670 goto done; /* Nothing to reclaim from */
671
672 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
673 /*
674 * Remove the node now but someone else can add it back,
675 * we will to add it back at the end of reclaim to its correct
676 * position in the tree.
677 */
Hugh Dickinsd79154b2012-03-21 16:34:18 -0700678 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
679 if (!res_counter_soft_limit_excess(&mz->memcg->res) ||
680 !css_tryget(&mz->memcg->css))
Balbir Singh4e416952009-09-23 15:56:39 -0700681 goto retry;
682done:
683 return mz;
684}
685
686static struct mem_cgroup_per_zone *
687mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
688{
689 struct mem_cgroup_per_zone *mz;
690
691 spin_lock(&mctz->lock);
692 mz = __mem_cgroup_largest_soft_limit_node(mctz);
693 spin_unlock(&mctz->lock);
694 return mz;
695}
696
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700697/*
698 * Implementation Note: reading percpu statistics for memcg.
699 *
700 * Both of vmstat[] and percpu_counter has threshold and do periodic
701 * synchronization to implement "quick" read. There are trade-off between
702 * reading cost and precision of value. Then, we may have a chance to implement
703 * a periodic synchronizion of counter in memcg's counter.
704 *
705 * But this _read() function is used for user interface now. The user accounts
706 * memory usage by memory cgroup and he _always_ requires exact value because
707 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
708 * have to visit all online cpus and make sum. So, for now, unnecessary
709 * synchronization is not implemented. (just implemented for cpu hotplug)
710 *
711 * If there are kernel internal actions which can make use of some not-exact
712 * value, and reading all cpu value can be performance bottleneck in some
713 * common workload, threashold and synchonization as vmstat[] should be
714 * implemented.
715 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700716static long mem_cgroup_read_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700717 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800718{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700719 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800720 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800721
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700722 get_online_cpus();
723 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700724 val += per_cpu(memcg->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700725#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700726 spin_lock(&memcg->pcp_counter_lock);
727 val += memcg->nocpu_base.count[idx];
728 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700729#endif
730 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800731 return val;
732}
733
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700734static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700735 bool charge)
736{
737 int val = (charge) ? 1 : -1;
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -0700738 this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAP], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700739}
740
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700741static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg,
Johannes Weinere9f89742011-03-23 16:42:37 -0700742 enum mem_cgroup_events_index idx)
743{
744 unsigned long val = 0;
745 int cpu;
746
747 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700748 val += per_cpu(memcg->stat->events[idx], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -0700749#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700750 spin_lock(&memcg->pcp_counter_lock);
751 val += memcg->nocpu_base.events[idx];
752 spin_unlock(&memcg->pcp_counter_lock);
Johannes Weinere9f89742011-03-23 16:42:37 -0700753#endif
754 return val;
755}
756
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700757static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700758 bool anon, int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800759{
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800760 preempt_disable();
761
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700762 /*
763 * Here, RSS means 'mapped anon' and anon's SwapCache. Shmem/tmpfs is
764 * counted as CACHE even if it's on ANON LRU.
765 */
766 if (anon)
767 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700768 nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800769 else
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700770 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700771 nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700772
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800773 /* pagein of a big page is an event. So, ignore page size */
774 if (nr_pages > 0)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700775 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800776 else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700777 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800778 nr_pages = -nr_pages; /* for event */
779 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800780
Johannes Weiner13114712012-05-29 15:07:07 -0700781 __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800782
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800783 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800784}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800785
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700786unsigned long
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700787mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
Konstantin Khlebnikov074291f2012-05-29 15:07:00 -0700788{
789 struct mem_cgroup_per_zone *mz;
790
791 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
792 return mz->lru_size[lru];
793}
794
795static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700796mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg, int nid, int zid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700797 unsigned int lru_mask)
Ying Han889976d2011-05-26 16:25:33 -0700798{
799 struct mem_cgroup_per_zone *mz;
Hugh Dickinsf156ab932012-03-21 16:34:19 -0700800 enum lru_list lru;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700801 unsigned long ret = 0;
802
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700803 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700804
Hugh Dickinsf156ab932012-03-21 16:34:19 -0700805 for_each_lru(lru) {
806 if (BIT(lru) & lru_mask)
807 ret += mz->lru_size[lru];
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700808 }
809 return ret;
810}
811
812static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700813mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700814 int nid, unsigned int lru_mask)
815{
Ying Han889976d2011-05-26 16:25:33 -0700816 u64 total = 0;
817 int zid;
818
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700819 for (zid = 0; zid < MAX_NR_ZONES; zid++)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700820 total += mem_cgroup_zone_nr_lru_pages(memcg,
821 nid, zid, lru_mask);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700822
Ying Han889976d2011-05-26 16:25:33 -0700823 return total;
824}
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700825
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700826static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700827 unsigned int lru_mask)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800828{
Ying Han889976d2011-05-26 16:25:33 -0700829 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800830 u64 total = 0;
831
Lai Jiangshan31aaea42012-12-12 13:51:27 -0800832 for_each_node_state(nid, N_MEMORY)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700833 total += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800834 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800835}
836
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800837static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg,
838 enum mem_cgroup_events_target target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800839{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700840 unsigned long val, next;
841
Johannes Weiner13114712012-05-29 15:07:07 -0700842 val = __this_cpu_read(memcg->stat->nr_page_events);
Steven Rostedt47994012011-11-02 13:38:33 -0700843 next = __this_cpu_read(memcg->stat->targets[target]);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700844 /* from time_after() in jiffies.h */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800845 if ((long)next - (long)val < 0) {
846 switch (target) {
847 case MEM_CGROUP_TARGET_THRESH:
848 next = val + THRESHOLDS_EVENTS_TARGET;
849 break;
850 case MEM_CGROUP_TARGET_SOFTLIMIT:
851 next = val + SOFTLIMIT_EVENTS_TARGET;
852 break;
853 case MEM_CGROUP_TARGET_NUMAINFO:
854 next = val + NUMAINFO_EVENTS_TARGET;
855 break;
856 default:
857 break;
858 }
859 __this_cpu_write(memcg->stat->targets[target], next);
860 return true;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700861 }
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800862 return false;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800863}
864
865/*
866 * Check events in order.
867 *
868 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700869static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800870{
Steven Rostedt47994012011-11-02 13:38:33 -0700871 preempt_disable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800872 /* threshold event is triggered in finer grain than soft limit */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800873 if (unlikely(mem_cgroup_event_ratelimit(memcg,
874 MEM_CGROUP_TARGET_THRESH))) {
Andrew Morton82b3f2a2012-02-03 15:37:14 -0800875 bool do_softlimit;
876 bool do_numainfo __maybe_unused;
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800877
878 do_softlimit = mem_cgroup_event_ratelimit(memcg,
879 MEM_CGROUP_TARGET_SOFTLIMIT);
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700880#if MAX_NUMNODES > 1
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800881 do_numainfo = mem_cgroup_event_ratelimit(memcg,
882 MEM_CGROUP_TARGET_NUMAINFO);
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700883#endif
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800884 preempt_enable();
885
886 mem_cgroup_threshold(memcg);
887 if (unlikely(do_softlimit))
888 mem_cgroup_update_tree(memcg, page);
889#if MAX_NUMNODES > 1
890 if (unlikely(do_numainfo))
891 atomic_inc(&memcg->numainfo_events);
892#endif
893 } else
894 preempt_enable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800895}
896
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000897struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800898{
Wanpeng Lib2145142012-07-31 16:46:01 -0700899 return mem_cgroup_from_css(
900 cgroup_subsys_state(cont, mem_cgroup_subsys_id));
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800901}
902
Balbir Singhcf475ad2008-04-29 01:00:16 -0700903struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800904{
Balbir Singh31a78f22008-09-28 23:09:31 +0100905 /*
906 * mm_update_next_owner() may clear mm->owner to NULL
907 * if it races with swapoff, page migration, etc.
908 * So this can be called with p == NULL.
909 */
910 if (unlikely(!p))
911 return NULL;
912
Wanpeng Lib2145142012-07-31 16:46:01 -0700913 return mem_cgroup_from_css(task_subsys_state(p, mem_cgroup_subsys_id));
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800914}
915
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700916struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800917{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700918 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700919
920 if (!mm)
921 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800922 /*
923 * Because we have no locks, mm->owner's may be being moved to other
924 * cgroup. We use css_tryget() here even if this looks
925 * pessimistic (rather than adding locks here).
926 */
927 rcu_read_lock();
928 do {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700929 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
930 if (unlikely(!memcg))
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800931 break;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700932 } while (!css_tryget(&memcg->css));
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800933 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700934 return memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800935}
936
Johannes Weiner56600482012-01-12 17:17:59 -0800937/**
938 * mem_cgroup_iter - iterate over memory cgroup hierarchy
939 * @root: hierarchy root
940 * @prev: previously returned memcg, NULL on first invocation
941 * @reclaim: cookie for shared reclaim walks, NULL for full walks
942 *
943 * Returns references to children of the hierarchy below @root, or
944 * @root itself, or %NULL after a full round-trip.
945 *
946 * Caller must pass the return value in @prev on subsequent
947 * invocations for reference counting, or use mem_cgroup_iter_break()
948 * to cancel a hierarchy walk before the round-trip is complete.
949 *
950 * Reclaimers can specify a zone and a priority level in @reclaim to
951 * divide up the memcgs in the hierarchy among all concurrent
952 * reclaimers operating on the same zone and priority.
953 */
954struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
955 struct mem_cgroup *prev,
956 struct mem_cgroup_reclaim_cookie *reclaim)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700957{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800958 struct mem_cgroup *memcg = NULL;
959 int id = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700960
Johannes Weiner56600482012-01-12 17:17:59 -0800961 if (mem_cgroup_disabled())
962 return NULL;
963
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700964 if (!root)
965 root = root_mem_cgroup;
966
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800967 if (prev && !reclaim)
968 id = css_id(&prev->css);
969
970 if (prev && prev != root)
971 css_put(&prev->css);
972
973 if (!root->use_hierarchy && root != root_mem_cgroup) {
974 if (prev)
975 return NULL;
976 return root;
977 }
978
979 while (!memcg) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800980 struct mem_cgroup_reclaim_iter *uninitialized_var(iter);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800981 struct cgroup_subsys_state *css;
982
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800983 if (reclaim) {
984 int nid = zone_to_nid(reclaim->zone);
985 int zid = zone_idx(reclaim->zone);
986 struct mem_cgroup_per_zone *mz;
987
988 mz = mem_cgroup_zoneinfo(root, nid, zid);
989 iter = &mz->reclaim_iter[reclaim->priority];
990 if (prev && reclaim->generation != iter->generation)
991 return NULL;
992 id = iter->position;
993 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800994
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700995 rcu_read_lock();
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800996 css = css_get_next(&mem_cgroup_subsys, id + 1, &root->css, &id);
997 if (css) {
998 if (css == &root->css || css_tryget(css))
Wanpeng Lib2145142012-07-31 16:46:01 -0700999 memcg = mem_cgroup_from_css(css);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001000 } else
1001 id = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001002 rcu_read_unlock();
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001003
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001004 if (reclaim) {
1005 iter->position = id;
1006 if (!css)
1007 iter->generation++;
1008 else if (!prev && memcg)
1009 reclaim->generation = iter->generation;
1010 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001011
1012 if (prev && !css)
1013 return NULL;
1014 }
1015 return memcg;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001016}
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001017
Johannes Weiner56600482012-01-12 17:17:59 -08001018/**
1019 * mem_cgroup_iter_break - abort a hierarchy walk prematurely
1020 * @root: hierarchy root
1021 * @prev: last visited hierarchy member as returned by mem_cgroup_iter()
1022 */
1023void mem_cgroup_iter_break(struct mem_cgroup *root,
1024 struct mem_cgroup *prev)
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001025{
1026 if (!root)
1027 root = root_mem_cgroup;
1028 if (prev && prev != root)
1029 css_put(&prev->css);
1030}
1031
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001032/*
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001033 * Iteration constructs for visiting all cgroups (under a tree). If
1034 * loops are exited prematurely (break), mem_cgroup_iter_break() must
1035 * be used for reference counting.
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001036 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001037#define for_each_mem_cgroup_tree(iter, root) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001038 for (iter = mem_cgroup_iter(root, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001039 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001040 iter = mem_cgroup_iter(root, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001041
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001042#define for_each_mem_cgroup(iter) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001043 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001044 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001045 iter = mem_cgroup_iter(NULL, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001046
David Rientjes68ae5642012-12-12 13:51:57 -08001047void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
Ying Han456f9982011-05-26 16:25:38 -07001048{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001049 struct mem_cgroup *memcg;
Ying Han456f9982011-05-26 16:25:38 -07001050
Ying Han456f9982011-05-26 16:25:38 -07001051 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001052 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1053 if (unlikely(!memcg))
Ying Han456f9982011-05-26 16:25:38 -07001054 goto out;
1055
1056 switch (idx) {
Ying Han456f9982011-05-26 16:25:38 -07001057 case PGFAULT:
Johannes Weiner0e574a92012-01-12 17:18:35 -08001058 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]);
1059 break;
1060 case PGMAJFAULT:
1061 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]);
Ying Han456f9982011-05-26 16:25:38 -07001062 break;
1063 default:
1064 BUG();
1065 }
1066out:
1067 rcu_read_unlock();
1068}
David Rientjes68ae5642012-12-12 13:51:57 -08001069EXPORT_SYMBOL(__mem_cgroup_count_vm_event);
Ying Han456f9982011-05-26 16:25:38 -07001070
Johannes Weiner925b7672012-01-12 17:18:15 -08001071/**
1072 * mem_cgroup_zone_lruvec - get the lru list vector for a zone and memcg
1073 * @zone: zone of the wanted lruvec
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001074 * @memcg: memcg of the wanted lruvec
Johannes Weiner925b7672012-01-12 17:18:15 -08001075 *
1076 * Returns the lru list vector holding pages for the given @zone and
1077 * @mem. This can be the global zone lruvec, if the memory controller
1078 * is disabled.
1079 */
1080struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
1081 struct mem_cgroup *memcg)
1082{
1083 struct mem_cgroup_per_zone *mz;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001084 struct lruvec *lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001085
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001086 if (mem_cgroup_disabled()) {
1087 lruvec = &zone->lruvec;
1088 goto out;
1089 }
Johannes Weiner925b7672012-01-12 17:18:15 -08001090
1091 mz = mem_cgroup_zoneinfo(memcg, zone_to_nid(zone), zone_idx(zone));
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001092 lruvec = &mz->lruvec;
1093out:
1094 /*
1095 * Since a node can be onlined after the mem_cgroup was created,
1096 * we have to be prepared to initialize lruvec->zone here;
1097 * and if offlined then reonlined, we need to reinitialize it.
1098 */
1099 if (unlikely(lruvec->zone != zone))
1100 lruvec->zone = zone;
1101 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001102}
1103
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001104/*
1105 * Following LRU functions are allowed to be used without PCG_LOCK.
1106 * Operations are called by routine of global LRU independently from memcg.
1107 * What we have to take care of here is validness of pc->mem_cgroup.
1108 *
1109 * Changes to pc->mem_cgroup happens when
1110 * 1. charge
1111 * 2. moving account
1112 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
1113 * It is added to LRU before charge.
1114 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
1115 * When moving account, the page is not on LRU. It's isolated.
1116 */
1117
Johannes Weiner925b7672012-01-12 17:18:15 -08001118/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001119 * mem_cgroup_page_lruvec - return lruvec for adding an lru page
Johannes Weiner925b7672012-01-12 17:18:15 -08001120 * @page: the page
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001121 * @zone: zone of the page
Minchan Kim3f58a822011-03-22 16:32:53 -07001122 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001123struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct zone *zone)
Minchan Kim3f58a822011-03-22 16:32:53 -07001124{
1125 struct mem_cgroup_per_zone *mz;
Johannes Weiner925b7672012-01-12 17:18:15 -08001126 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001127 struct page_cgroup *pc;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001128 struct lruvec *lruvec;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001129
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001130 if (mem_cgroup_disabled()) {
1131 lruvec = &zone->lruvec;
1132 goto out;
1133 }
Christoph Lameterb69408e2008-10-18 20:26:14 -07001134
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001135 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08001136 memcg = pc->mem_cgroup;
Hugh Dickins75121022012-03-05 14:59:18 -08001137
1138 /*
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001139 * Surreptitiously switch any uncharged offlist page to root:
Hugh Dickins75121022012-03-05 14:59:18 -08001140 * an uncharged page off lru does nothing to secure
1141 * its former mem_cgroup from sudden removal.
1142 *
1143 * Our caller holds lru_lock, and PageCgroupUsed is updated
1144 * under page_cgroup lock: between them, they make all uses
1145 * of pc->mem_cgroup safe.
1146 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001147 if (!PageLRU(page) && !PageCgroupUsed(pc) && memcg != root_mem_cgroup)
Hugh Dickins75121022012-03-05 14:59:18 -08001148 pc->mem_cgroup = memcg = root_mem_cgroup;
1149
Johannes Weiner925b7672012-01-12 17:18:15 -08001150 mz = page_cgroup_zoneinfo(memcg, page);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001151 lruvec = &mz->lruvec;
1152out:
1153 /*
1154 * Since a node can be onlined after the mem_cgroup was created,
1155 * we have to be prepared to initialize lruvec->zone here;
1156 * and if offlined then reonlined, we need to reinitialize it.
1157 */
1158 if (unlikely(lruvec->zone != zone))
1159 lruvec->zone = zone;
1160 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001161}
1162
1163/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001164 * mem_cgroup_update_lru_size - account for adding or removing an lru page
1165 * @lruvec: mem_cgroup per zone lru vector
1166 * @lru: index of lru list the page is sitting on
1167 * @nr_pages: positive when adding or negative when removing
Johannes Weiner925b7672012-01-12 17:18:15 -08001168 *
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001169 * This function must be called when a page is added to or removed from an
1170 * lru list.
Johannes Weiner925b7672012-01-12 17:18:15 -08001171 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001172void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
1173 int nr_pages)
Johannes Weiner925b7672012-01-12 17:18:15 -08001174{
1175 struct mem_cgroup_per_zone *mz;
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001176 unsigned long *lru_size;
Johannes Weiner925b7672012-01-12 17:18:15 -08001177
1178 if (mem_cgroup_disabled())
1179 return;
1180
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001181 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
1182 lru_size = mz->lru_size + lru;
1183 *lru_size += nr_pages;
1184 VM_BUG_ON((long)(*lru_size) < 0);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001185}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001186
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001187/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001188 * Checks whether given mem is same or in the root_mem_cgroup's
Michal Hocko3e920412011-07-26 16:08:29 -07001189 * hierarchy subtree
1190 */
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001191bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1192 struct mem_cgroup *memcg)
Michal Hocko3e920412011-07-26 16:08:29 -07001193{
Johannes Weiner91c637342012-05-29 15:06:24 -07001194 if (root_memcg == memcg)
1195 return true;
Hugh Dickins3a981f42012-06-20 12:52:58 -07001196 if (!root_memcg->use_hierarchy || !memcg)
Johannes Weiner91c637342012-05-29 15:06:24 -07001197 return false;
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001198 return css_is_ancestor(&memcg->css, &root_memcg->css);
1199}
1200
1201static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1202 struct mem_cgroup *memcg)
1203{
1204 bool ret;
1205
Johannes Weiner91c637342012-05-29 15:06:24 -07001206 rcu_read_lock();
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001207 ret = __mem_cgroup_same_or_subtree(root_memcg, memcg);
Johannes Weiner91c637342012-05-29 15:06:24 -07001208 rcu_read_unlock();
1209 return ret;
Michal Hocko3e920412011-07-26 16:08:29 -07001210}
1211
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001212int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -08001213{
1214 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001215 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001216 struct task_struct *p;
David Rientjes4c4a2212008-02-07 00:14:06 -08001217
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001218 p = find_lock_task_mm(task);
David Rientjesde077d22012-01-12 17:18:52 -08001219 if (p) {
1220 curr = try_get_mem_cgroup_from_mm(p->mm);
1221 task_unlock(p);
1222 } else {
1223 /*
1224 * All threads may have already detached their mm's, but the oom
1225 * killer still needs to detect if they have already been oom
1226 * killed to prevent needlessly killing additional tasks.
1227 */
1228 task_lock(task);
1229 curr = mem_cgroup_from_task(task);
1230 if (curr)
1231 css_get(&curr->css);
1232 task_unlock(task);
1233 }
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001234 if (!curr)
1235 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001236 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001237 * We should check use_hierarchy of "memcg" not "curr". Because checking
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001238 * use_hierarchy of "curr" here make this function true if hierarchy is
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001239 * enabled in "curr" and "curr" is a child of "memcg" in *cgroup*
1240 * hierarchy(even if use_hierarchy is disabled in "memcg").
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001241 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001242 ret = mem_cgroup_same_or_subtree(memcg, curr);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001243 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001244 return ret;
1245}
1246
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001247int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001248{
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001249 unsigned long inactive_ratio;
Johannes Weiner9b272972011-11-02 13:38:23 -07001250 unsigned long inactive;
1251 unsigned long active;
1252 unsigned long gb;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001253
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001254 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_ANON);
1255 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001256
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001257 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1258 if (gb)
1259 inactive_ratio = int_sqrt(10 * gb);
1260 else
1261 inactive_ratio = 1;
1262
Johannes Weiner9b272972011-11-02 13:38:23 -07001263 return inactive * inactive_ratio < active;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001264}
1265
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001266int mem_cgroup_inactive_file_is_low(struct lruvec *lruvec)
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001267{
1268 unsigned long active;
1269 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001270
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001271 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_FILE);
1272 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_FILE);
Rik van Riel56e49d22009-06-16 15:32:28 -07001273
1274 return (active > inactive);
1275}
1276
Balbir Singh6d61ef42009-01-07 18:08:06 -08001277#define mem_cgroup_from_res_counter(counter, member) \
1278 container_of(counter, struct mem_cgroup, member)
1279
Johannes Weiner19942822011-02-01 15:52:43 -08001280/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001281 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
Wanpeng Lidad75572012-06-20 12:53:01 -07001282 * @memcg: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001283 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001284 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001285 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001286 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001287static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001288{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001289 unsigned long long margin;
1290
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001291 margin = res_counter_margin(&memcg->res);
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001292 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001293 margin = min(margin, res_counter_margin(&memcg->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001294 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001295}
1296
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001297int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001298{
1299 struct cgroup *cgrp = memcg->css.cgroup;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001300
1301 /* root ? */
1302 if (cgrp->parent == NULL)
1303 return vm_swappiness;
1304
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001305 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001306}
1307
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001308/*
1309 * memcg->moving_account is used for checking possibility that some thread is
1310 * calling move_account(). When a thread on CPU-A starts moving pages under
1311 * a memcg, other threads should check memcg->moving_account under
1312 * rcu_read_lock(), like this:
1313 *
1314 * CPU-A CPU-B
1315 * rcu_read_lock()
1316 * memcg->moving_account+1 if (memcg->mocing_account)
1317 * take heavy locks.
1318 * synchronize_rcu() update something.
1319 * rcu_read_unlock()
1320 * start move here.
1321 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001322
1323/* for quick checking without looking up memcg */
1324atomic_t memcg_moving __read_mostly;
1325
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001326static void mem_cgroup_start_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001327{
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001328 atomic_inc(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001329 atomic_inc(&memcg->moving_account);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001330 synchronize_rcu();
1331}
1332
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001333static void mem_cgroup_end_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001334{
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001335 /*
1336 * Now, mem_cgroup_clear_mc() may call this function with NULL.
1337 * We check NULL in callee rather than caller.
1338 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001339 if (memcg) {
1340 atomic_dec(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001341 atomic_dec(&memcg->moving_account);
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001342 }
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001343}
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001344
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001345/*
1346 * 2 routines for checking "mem" is under move_account() or not.
1347 *
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001348 * mem_cgroup_stolen() - checking whether a cgroup is mc.from or not. This
1349 * is used for avoiding races in accounting. If true,
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001350 * pc->mem_cgroup may be overwritten.
1351 *
1352 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1353 * under hierarchy of moving cgroups. This is for
1354 * waiting at hith-memory prressure caused by "move".
1355 */
1356
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001357static bool mem_cgroup_stolen(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001358{
1359 VM_BUG_ON(!rcu_read_lock_held());
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001360 return atomic_read(&memcg->moving_account) > 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001361}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001362
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001363static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001364{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001365 struct mem_cgroup *from;
1366 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001367 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001368 /*
1369 * Unlike task_move routines, we access mc.to, mc.from not under
1370 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1371 */
1372 spin_lock(&mc.lock);
1373 from = mc.from;
1374 to = mc.to;
1375 if (!from)
1376 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001377
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001378 ret = mem_cgroup_same_or_subtree(memcg, from)
1379 || mem_cgroup_same_or_subtree(memcg, to);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001380unlock:
1381 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001382 return ret;
1383}
1384
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001385static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001386{
1387 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001388 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001389 DEFINE_WAIT(wait);
1390 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1391 /* moving charge context might have finished. */
1392 if (mc.moving_task)
1393 schedule();
1394 finish_wait(&mc.waitq, &wait);
1395 return true;
1396 }
1397 }
1398 return false;
1399}
1400
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001401/*
1402 * Take this lock when
1403 * - a code tries to modify page's memcg while it's USED.
1404 * - a code tries to modify page state accounting in a memcg.
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001405 * see mem_cgroup_stolen(), too.
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001406 */
1407static void move_lock_mem_cgroup(struct mem_cgroup *memcg,
1408 unsigned long *flags)
1409{
1410 spin_lock_irqsave(&memcg->move_lock, *flags);
1411}
1412
1413static void move_unlock_mem_cgroup(struct mem_cgroup *memcg,
1414 unsigned long *flags)
1415{
1416 spin_unlock_irqrestore(&memcg->move_lock, *flags);
1417}
1418
Balbir Singhe2224322009-04-02 16:57:39 -07001419/**
Kirill A. Shutemov6a6135b2010-03-10 15:22:25 -08001420 * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode.
Balbir Singhe2224322009-04-02 16:57:39 -07001421 * @memcg: The memory cgroup that went over limit
1422 * @p: Task that is going to be killed
1423 *
1424 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1425 * enabled
1426 */
1427void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1428{
1429 struct cgroup *task_cgrp;
1430 struct cgroup *mem_cgrp;
1431 /*
1432 * Need a buffer in BSS, can't rely on allocations. The code relies
1433 * on the assumption that OOM is serialized for memory controller.
1434 * If this assumption is broken, revisit this code.
1435 */
1436 static char memcg_name[PATH_MAX];
1437 int ret;
1438
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001439 if (!memcg || !p)
Balbir Singhe2224322009-04-02 16:57:39 -07001440 return;
1441
Balbir Singhe2224322009-04-02 16:57:39 -07001442 rcu_read_lock();
1443
1444 mem_cgrp = memcg->css.cgroup;
1445 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1446
1447 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1448 if (ret < 0) {
1449 /*
1450 * Unfortunately, we are unable to convert to a useful name
1451 * But we'll still print out the usage information
1452 */
1453 rcu_read_unlock();
1454 goto done;
1455 }
1456 rcu_read_unlock();
1457
1458 printk(KERN_INFO "Task in %s killed", memcg_name);
1459
1460 rcu_read_lock();
1461 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1462 if (ret < 0) {
1463 rcu_read_unlock();
1464 goto done;
1465 }
1466 rcu_read_unlock();
1467
1468 /*
1469 * Continues from above, so we don't need an KERN_ level
1470 */
1471 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1472done:
1473
1474 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1475 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1476 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1477 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1478 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1479 "failcnt %llu\n",
1480 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1481 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1482 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
Glauber Costa510fc4e2012-12-18 14:21:47 -08001483 printk(KERN_INFO "kmem: usage %llukB, limit %llukB, failcnt %llu\n",
1484 res_counter_read_u64(&memcg->kmem, RES_USAGE) >> 10,
1485 res_counter_read_u64(&memcg->kmem, RES_LIMIT) >> 10,
1486 res_counter_read_u64(&memcg->kmem, RES_FAILCNT));
Balbir Singhe2224322009-04-02 16:57:39 -07001487}
1488
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001489/*
1490 * This function returns the number of memcg under hierarchy tree. Returns
1491 * 1(self count) if no children.
1492 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001493static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001494{
1495 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001496 struct mem_cgroup *iter;
1497
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001498 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001499 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001500 return num;
1501}
1502
Balbir Singh6d61ef42009-01-07 18:08:06 -08001503/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001504 * Return the memory (and swap, if configured) limit for a memcg.
1505 */
David Rientjes9cbb78b2012-07-31 16:43:44 -07001506static u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
David Rientjesa63d83f2010-08-09 17:19:46 -07001507{
1508 u64 limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001509
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001510 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001511
David Rientjesa63d83f2010-08-09 17:19:46 -07001512 /*
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001513 * Do not consider swap space if we cannot swap due to swappiness
David Rientjesa63d83f2010-08-09 17:19:46 -07001514 */
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001515 if (mem_cgroup_swappiness(memcg)) {
1516 u64 memsw;
1517
1518 limit += total_swap_pages << PAGE_SHIFT;
1519 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1520
1521 /*
1522 * If memsw is finite and limits the amount of swap space
1523 * available to this memcg, return that limit.
1524 */
1525 limit = min(limit, memsw);
1526 }
1527
1528 return limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001529}
1530
David Rientjes19965462012-12-11 16:00:26 -08001531static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
1532 int order)
David Rientjes9cbb78b2012-07-31 16:43:44 -07001533{
1534 struct mem_cgroup *iter;
1535 unsigned long chosen_points = 0;
1536 unsigned long totalpages;
1537 unsigned int points = 0;
1538 struct task_struct *chosen = NULL;
1539
David Rientjes876aafb2012-07-31 16:43:48 -07001540 /*
1541 * If current has a pending SIGKILL, then automatically select it. The
1542 * goal is to allow it to allocate so that it may quickly exit and free
1543 * its memory.
1544 */
1545 if (fatal_signal_pending(current)) {
1546 set_thread_flag(TIF_MEMDIE);
1547 return;
1548 }
1549
1550 check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, order, NULL);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001551 totalpages = mem_cgroup_get_limit(memcg) >> PAGE_SHIFT ? : 1;
1552 for_each_mem_cgroup_tree(iter, memcg) {
1553 struct cgroup *cgroup = iter->css.cgroup;
1554 struct cgroup_iter it;
1555 struct task_struct *task;
1556
1557 cgroup_iter_start(cgroup, &it);
1558 while ((task = cgroup_iter_next(cgroup, &it))) {
1559 switch (oom_scan_process_thread(task, totalpages, NULL,
1560 false)) {
1561 case OOM_SCAN_SELECT:
1562 if (chosen)
1563 put_task_struct(chosen);
1564 chosen = task;
1565 chosen_points = ULONG_MAX;
1566 get_task_struct(chosen);
1567 /* fall through */
1568 case OOM_SCAN_CONTINUE:
1569 continue;
1570 case OOM_SCAN_ABORT:
1571 cgroup_iter_end(cgroup, &it);
1572 mem_cgroup_iter_break(memcg, iter);
1573 if (chosen)
1574 put_task_struct(chosen);
1575 return;
1576 case OOM_SCAN_OK:
1577 break;
1578 };
1579 points = oom_badness(task, memcg, NULL, totalpages);
1580 if (points > chosen_points) {
1581 if (chosen)
1582 put_task_struct(chosen);
1583 chosen = task;
1584 chosen_points = points;
1585 get_task_struct(chosen);
1586 }
1587 }
1588 cgroup_iter_end(cgroup, &it);
1589 }
1590
1591 if (!chosen)
1592 return;
1593 points = chosen_points * 1000 / totalpages;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001594 oom_kill_process(chosen, gfp_mask, order, points, totalpages, memcg,
1595 NULL, "Memory cgroup out of memory");
David Rientjes9cbb78b2012-07-31 16:43:44 -07001596}
1597
Johannes Weiner56600482012-01-12 17:17:59 -08001598static unsigned long mem_cgroup_reclaim(struct mem_cgroup *memcg,
1599 gfp_t gfp_mask,
1600 unsigned long flags)
1601{
1602 unsigned long total = 0;
1603 bool noswap = false;
1604 int loop;
1605
1606 if (flags & MEM_CGROUP_RECLAIM_NOSWAP)
1607 noswap = true;
1608 if (!(flags & MEM_CGROUP_RECLAIM_SHRINK) && memcg->memsw_is_minimum)
1609 noswap = true;
1610
1611 for (loop = 0; loop < MEM_CGROUP_MAX_RECLAIM_LOOPS; loop++) {
1612 if (loop)
1613 drain_all_stock_async(memcg);
1614 total += try_to_free_mem_cgroup_pages(memcg, gfp_mask, noswap);
1615 /*
1616 * Allow limit shrinkers, which are triggered directly
1617 * by userspace, to catch signals and stop reclaim
1618 * after minimal progress, regardless of the margin.
1619 */
1620 if (total && (flags & MEM_CGROUP_RECLAIM_SHRINK))
1621 break;
1622 if (mem_cgroup_margin(memcg))
1623 break;
1624 /*
1625 * If nothing was reclaimed after two attempts, there
1626 * may be no reclaimable pages in this hierarchy.
1627 */
1628 if (loop && !total)
1629 break;
1630 }
1631 return total;
1632}
1633
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001634/**
1635 * test_mem_cgroup_node_reclaimable
Wanpeng Lidad75572012-06-20 12:53:01 -07001636 * @memcg: the target memcg
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001637 * @nid: the node ID to be checked.
1638 * @noswap : specify true here if the user wants flle only information.
1639 *
1640 * This function returns whether the specified memcg contains any
1641 * reclaimable pages on a node. Returns true if there are any reclaimable
1642 * pages in the node.
1643 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001644static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001645 int nid, bool noswap)
1646{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001647 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001648 return true;
1649 if (noswap || !total_swap_pages)
1650 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001651 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001652 return true;
1653 return false;
1654
1655}
Ying Han889976d2011-05-26 16:25:33 -07001656#if MAX_NUMNODES > 1
1657
1658/*
1659 * Always updating the nodemask is not very good - even if we have an empty
1660 * list or the wrong list here, we can start from some node and traverse all
1661 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1662 *
1663 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001664static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001665{
1666 int nid;
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001667 /*
1668 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1669 * pagein/pageout changes since the last update.
1670 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001671 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001672 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001673 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001674 return;
1675
Ying Han889976d2011-05-26 16:25:33 -07001676 /* make a nodemask where this memcg uses memory from */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001677 memcg->scan_nodes = node_states[N_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001678
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001679 for_each_node_mask(nid, node_states[N_MEMORY]) {
Ying Han889976d2011-05-26 16:25:33 -07001680
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001681 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1682 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001683 }
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001684
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001685 atomic_set(&memcg->numainfo_events, 0);
1686 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001687}
1688
1689/*
1690 * Selecting a node where we start reclaim from. Because what we need is just
1691 * reducing usage counter, start from anywhere is O,K. Considering
1692 * memory reclaim from current node, there are pros. and cons.
1693 *
1694 * Freeing memory from current node means freeing memory from a node which
1695 * we'll use or we've used. So, it may make LRU bad. And if several threads
1696 * hit limits, it will see a contention on a node. But freeing from remote
1697 * node means more costs for memory reclaim because of memory latency.
1698 *
1699 * Now, we use round-robin. Better algorithm is welcomed.
1700 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001701int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001702{
1703 int node;
1704
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001705 mem_cgroup_may_update_nodemask(memcg);
1706 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001707
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001708 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001709 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001710 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001711 /*
1712 * We call this when we hit limit, not when pages are added to LRU.
1713 * No LRU may hold pages because all pages are UNEVICTABLE or
1714 * memcg is too small and all pages are not on LRU. In that case,
1715 * we use curret node.
1716 */
1717 if (unlikely(node == MAX_NUMNODES))
1718 node = numa_node_id();
1719
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001720 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001721 return node;
1722}
1723
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001724/*
1725 * Check all nodes whether it contains reclaimable pages or not.
1726 * For quick scan, we make use of scan_nodes. This will allow us to skip
1727 * unused nodes. But scan_nodes is lazily updated and may not cotain
1728 * enough new information. We need to do double check.
1729 */
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001730static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001731{
1732 int nid;
1733
1734 /*
1735 * quick check...making use of scan_node.
1736 * We can skip unused nodes.
1737 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001738 if (!nodes_empty(memcg->scan_nodes)) {
1739 for (nid = first_node(memcg->scan_nodes);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001740 nid < MAX_NUMNODES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001741 nid = next_node(nid, memcg->scan_nodes)) {
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001742
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001743 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001744 return true;
1745 }
1746 }
1747 /*
1748 * Check rest of nodes.
1749 */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001750 for_each_node_state(nid, N_MEMORY) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001751 if (node_isset(nid, memcg->scan_nodes))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001752 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001753 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001754 return true;
1755 }
1756 return false;
1757}
1758
Ying Han889976d2011-05-26 16:25:33 -07001759#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001760int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001761{
1762 return 0;
1763}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001764
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001765static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001766{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001767 return test_mem_cgroup_node_reclaimable(memcg, 0, noswap);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001768}
Ying Han889976d2011-05-26 16:25:33 -07001769#endif
1770
Johannes Weiner56600482012-01-12 17:17:59 -08001771static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg,
1772 struct zone *zone,
1773 gfp_t gfp_mask,
1774 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001775{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001776 struct mem_cgroup *victim = NULL;
Johannes Weiner56600482012-01-12 17:17:59 -08001777 int total = 0;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001778 int loop = 0;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001779 unsigned long excess;
Johannes Weiner185efc02011-09-14 16:21:58 -07001780 unsigned long nr_scanned;
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001781 struct mem_cgroup_reclaim_cookie reclaim = {
1782 .zone = zone,
1783 .priority = 0,
1784 };
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001785
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001786 excess = res_counter_soft_limit_excess(&root_memcg->res) >> PAGE_SHIFT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001787
Balbir Singh4e416952009-09-23 15:56:39 -07001788 while (1) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001789 victim = mem_cgroup_iter(root_memcg, victim, &reclaim);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001790 if (!victim) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001791 loop++;
Balbir Singh4e416952009-09-23 15:56:39 -07001792 if (loop >= 2) {
1793 /*
1794 * If we have not been able to reclaim
1795 * anything, it might because there are
1796 * no reclaimable pages under this hierarchy
1797 */
Johannes Weiner56600482012-01-12 17:17:59 -08001798 if (!total)
Balbir Singh4e416952009-09-23 15:56:39 -07001799 break;
Balbir Singh4e416952009-09-23 15:56:39 -07001800 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001801 * We want to do more targeted reclaim.
Balbir Singh4e416952009-09-23 15:56:39 -07001802 * excess >> 2 is not to excessive so as to
1803 * reclaim too much, nor too less that we keep
1804 * coming back to reclaim from this cgroup
1805 */
1806 if (total >= (excess >> 2) ||
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001807 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS))
Balbir Singh4e416952009-09-23 15:56:39 -07001808 break;
Balbir Singh4e416952009-09-23 15:56:39 -07001809 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001810 continue;
Balbir Singh4e416952009-09-23 15:56:39 -07001811 }
Johannes Weiner56600482012-01-12 17:17:59 -08001812 if (!mem_cgroup_reclaimable(victim, false))
Balbir Singh6d61ef42009-01-07 18:08:06 -08001813 continue;
Johannes Weiner56600482012-01-12 17:17:59 -08001814 total += mem_cgroup_shrink_node_zone(victim, gfp_mask, false,
1815 zone, &nr_scanned);
1816 *total_scanned += nr_scanned;
1817 if (!res_counter_soft_limit_excess(&root_memcg->res))
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001818 break;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001819 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001820 mem_cgroup_iter_break(root_memcg, victim);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001821 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001822}
1823
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001824/*
1825 * Check OOM-Killer is already running under our hierarchy.
1826 * If someone is running, return false.
Michal Hocko1af8efe2011-07-26 16:08:24 -07001827 * Has to be called with memcg_oom_lock
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001828 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001829static bool mem_cgroup_oom_lock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001830{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001831 struct mem_cgroup *iter, *failed = NULL;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001832
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001833 for_each_mem_cgroup_tree(iter, memcg) {
Johannes Weiner23751be2011-08-25 15:59:16 -07001834 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001835 /*
1836 * this subtree of our hierarchy is already locked
1837 * so we cannot give a lock.
1838 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07001839 failed = iter;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001840 mem_cgroup_iter_break(memcg, iter);
1841 break;
Johannes Weiner23751be2011-08-25 15:59:16 -07001842 } else
1843 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001844 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001845
Michal Hocko79dfdac2011-07-26 16:08:23 -07001846 if (!failed)
Johannes Weiner23751be2011-08-25 15:59:16 -07001847 return true;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001848
1849 /*
1850 * OK, we failed to lock the whole subtree so we have to clean up
1851 * what we set up to the failing subtree
1852 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001853 for_each_mem_cgroup_tree(iter, memcg) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001854 if (iter == failed) {
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001855 mem_cgroup_iter_break(memcg, iter);
1856 break;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001857 }
1858 iter->oom_lock = false;
1859 }
Johannes Weiner23751be2011-08-25 15:59:16 -07001860 return false;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001861}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001862
Michal Hocko79dfdac2011-07-26 16:08:23 -07001863/*
Michal Hocko1af8efe2011-07-26 16:08:24 -07001864 * Has to be called with memcg_oom_lock
Michal Hocko79dfdac2011-07-26 16:08:23 -07001865 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001866static int mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001867{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001868 struct mem_cgroup *iter;
1869
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001870 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001871 iter->oom_lock = false;
1872 return 0;
1873}
1874
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001875static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001876{
1877 struct mem_cgroup *iter;
1878
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001879 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001880 atomic_inc(&iter->under_oom);
1881}
1882
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001883static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001884{
1885 struct mem_cgroup *iter;
1886
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001887 /*
1888 * When a new child is created while the hierarchy is under oom,
1889 * mem_cgroup_oom_lock() may not be called. We have to use
1890 * atomic_add_unless() here.
1891 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001892 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001893 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001894}
1895
Michal Hocko1af8efe2011-07-26 16:08:24 -07001896static DEFINE_SPINLOCK(memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001897static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1898
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001899struct oom_wait_info {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001900 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001901 wait_queue_t wait;
1902};
1903
1904static int memcg_oom_wake_function(wait_queue_t *wait,
1905 unsigned mode, int sync, void *arg)
1906{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001907 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
1908 struct mem_cgroup *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001909 struct oom_wait_info *oom_wait_info;
1910
1911 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001912 oom_wait_memcg = oom_wait_info->memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001913
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001914 /*
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001915 * Both of oom_wait_info->memcg and wake_memcg are stable under us.
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001916 * Then we can use css_is_ancestor without taking care of RCU.
1917 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001918 if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg)
1919 && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001920 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001921 return autoremove_wake_function(wait, mode, sync, arg);
1922}
1923
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001924static void memcg_wakeup_oom(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001925{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001926 /* for filtering, pass "memcg" as argument. */
1927 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001928}
1929
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001930static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001931{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001932 if (memcg && atomic_read(&memcg->under_oom))
1933 memcg_wakeup_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001934}
1935
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001936/*
1937 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
1938 */
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001939static bool mem_cgroup_handle_oom(struct mem_cgroup *memcg, gfp_t mask,
1940 int order)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001941{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001942 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001943 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001944
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001945 owait.memcg = memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001946 owait.wait.flags = 0;
1947 owait.wait.func = memcg_oom_wake_function;
1948 owait.wait.private = current;
1949 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001950 need_to_kill = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001951 mem_cgroup_mark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001952
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001953 /* At first, try to OOM lock hierarchy under memcg.*/
Michal Hocko1af8efe2011-07-26 16:08:24 -07001954 spin_lock(&memcg_oom_lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001955 locked = mem_cgroup_oom_lock(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001956 /*
1957 * Even if signal_pending(), we can't quit charge() loop without
1958 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
1959 * under OOM is always welcomed, use TASK_KILLABLE here.
1960 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001961 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001962 if (!locked || memcg->oom_kill_disable)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001963 need_to_kill = false;
1964 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001965 mem_cgroup_oom_notify(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07001966 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001967
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001968 if (need_to_kill) {
1969 finish_wait(&memcg_oom_waitq, &owait.wait);
David Rientjese845e192012-03-21 16:34:10 -07001970 mem_cgroup_out_of_memory(memcg, mask, order);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001971 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001972 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001973 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001974 }
Michal Hocko1af8efe2011-07-26 16:08:24 -07001975 spin_lock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001976 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001977 mem_cgroup_oom_unlock(memcg);
1978 memcg_wakeup_oom(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07001979 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001980
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001981 mem_cgroup_unmark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001982
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001983 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
1984 return false;
1985 /* Give chance to dying process */
KAMEZAWA Hiroyuki715a5ee2011-11-02 13:38:18 -07001986 schedule_timeout_uninterruptible(1);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001987 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001988}
1989
Balbir Singhd69b0422009-06-17 16:26:34 -07001990/*
1991 * Currently used to update mapped file statistics, but the routine can be
1992 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001993 *
1994 * Notes: Race condition
1995 *
1996 * We usually use page_cgroup_lock() for accessing page_cgroup member but
1997 * it tends to be costly. But considering some conditions, we doesn't need
1998 * to do so _always_.
1999 *
2000 * Considering "charge", lock_page_cgroup() is not required because all
2001 * file-stat operations happen after a page is attached to radix-tree. There
2002 * are no race with "charge".
2003 *
2004 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
2005 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
2006 * if there are race with "uncharge". Statistics itself is properly handled
2007 * by flags.
2008 *
2009 * Considering "move", this is an only case we see a race. To make the race
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002010 * small, we check mm->moving_account and detect there are possibility of race
2011 * If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07002012 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002013
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002014void __mem_cgroup_begin_update_page_stat(struct page *page,
2015 bool *locked, unsigned long *flags)
2016{
2017 struct mem_cgroup *memcg;
2018 struct page_cgroup *pc;
2019
2020 pc = lookup_page_cgroup(page);
2021again:
2022 memcg = pc->mem_cgroup;
2023 if (unlikely(!memcg || !PageCgroupUsed(pc)))
2024 return;
2025 /*
2026 * If this memory cgroup is not under account moving, we don't
Wanpeng Lida92c472012-07-31 16:43:26 -07002027 * need to take move_lock_mem_cgroup(). Because we already hold
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002028 * rcu_read_lock(), any calls to move_account will be delayed until
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002029 * rcu_read_unlock() if mem_cgroup_stolen() == true.
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002030 */
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002031 if (!mem_cgroup_stolen(memcg))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002032 return;
2033
2034 move_lock_mem_cgroup(memcg, flags);
2035 if (memcg != pc->mem_cgroup || !PageCgroupUsed(pc)) {
2036 move_unlock_mem_cgroup(memcg, flags);
2037 goto again;
2038 }
2039 *locked = true;
2040}
2041
2042void __mem_cgroup_end_update_page_stat(struct page *page, unsigned long *flags)
2043{
2044 struct page_cgroup *pc = lookup_page_cgroup(page);
2045
2046 /*
2047 * It's guaranteed that pc->mem_cgroup never changes while
2048 * lock is held because a routine modifies pc->mem_cgroup
Wanpeng Lida92c472012-07-31 16:43:26 -07002049 * should take move_lock_mem_cgroup().
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002050 */
2051 move_unlock_mem_cgroup(pc->mem_cgroup, flags);
2052}
2053
Greg Thelen2a7106f2011-01-13 15:47:37 -08002054void mem_cgroup_update_page_stat(struct page *page,
2055 enum mem_cgroup_page_stat_item idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07002056{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002057 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002058 struct page_cgroup *pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08002059 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07002060
Johannes Weinercfa44942012-01-12 17:18:38 -08002061 if (mem_cgroup_disabled())
Balbir Singhd69b0422009-06-17 16:26:34 -07002062 return;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002063
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002064 memcg = pc->mem_cgroup;
2065 if (unlikely(!memcg || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002066 return;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002067
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002068 switch (idx) {
Greg Thelen2a7106f2011-01-13 15:47:37 -08002069 case MEMCG_NR_FILE_MAPPED:
Greg Thelen2a7106f2011-01-13 15:47:37 -08002070 idx = MEM_CGROUP_STAT_FILE_MAPPED;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002071 break;
2072 default:
2073 BUG();
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002074 }
Balbir Singhd69b0422009-06-17 16:26:34 -07002075
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002076 this_cpu_add(memcg->stat->count[idx], val);
Balbir Singhd69b0422009-06-17 16:26:34 -07002077}
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002078
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002079/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002080 * size of first charge trial. "32" comes from vmscan.c's magic value.
2081 * TODO: maybe necessary to use big numbers in big irons.
2082 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002083#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002084struct memcg_stock_pcp {
2085 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002086 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002087 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002088 unsigned long flags;
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002089#define FLUSHING_CACHED_CHARGE 0
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002090};
2091static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002092static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002093
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002094/**
2095 * consume_stock: Try to consume stocked charge on this cpu.
2096 * @memcg: memcg to consume from.
2097 * @nr_pages: how many pages to charge.
2098 *
2099 * The charges will only happen if @memcg matches the current cpu's memcg
2100 * stock, and at least @nr_pages are available in that stock. Failure to
2101 * service an allocation will refill the stock.
2102 *
2103 * returns true if successful, false otherwise.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002104 */
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002105static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002106{
2107 struct memcg_stock_pcp *stock;
2108 bool ret = true;
2109
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002110 if (nr_pages > CHARGE_BATCH)
2111 return false;
2112
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002113 stock = &get_cpu_var(memcg_stock);
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002114 if (memcg == stock->cached && stock->nr_pages >= nr_pages)
2115 stock->nr_pages -= nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002116 else /* need to call res_counter_charge */
2117 ret = false;
2118 put_cpu_var(memcg_stock);
2119 return ret;
2120}
2121
2122/*
2123 * Returns stocks cached in percpu to res_counter and reset cached information.
2124 */
2125static void drain_stock(struct memcg_stock_pcp *stock)
2126{
2127 struct mem_cgroup *old = stock->cached;
2128
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002129 if (stock->nr_pages) {
2130 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2131
2132 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002133 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002134 res_counter_uncharge(&old->memsw, bytes);
2135 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002136 }
2137 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002138}
2139
2140/*
2141 * This must be called under preempt disabled or must be called by
2142 * a thread which is pinned to local cpu.
2143 */
2144static void drain_local_stock(struct work_struct *dummy)
2145{
2146 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
2147 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002148 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002149}
2150
2151/*
2152 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002153 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002154 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002155static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002156{
2157 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2158
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002159 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002160 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002161 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002162 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002163 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002164 put_cpu_var(memcg_stock);
2165}
2166
2167/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002168 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Michal Hockod38144b2011-07-26 16:08:28 -07002169 * of the hierarchy under it. sync flag says whether we should block
2170 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002171 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002172static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002173{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002174 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002175
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002176 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002177 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002178 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002179 for_each_online_cpu(cpu) {
2180 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002181 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002182
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002183 memcg = stock->cached;
2184 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002185 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002186 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002187 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002188 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2189 if (cpu == curcpu)
2190 drain_local_stock(&stock->work);
2191 else
2192 schedule_work_on(cpu, &stock->work);
2193 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002194 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002195 put_cpu();
Michal Hockod38144b2011-07-26 16:08:28 -07002196
2197 if (!sync)
2198 goto out;
2199
2200 for_each_online_cpu(cpu) {
2201 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002202 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002203 flush_work(&stock->work);
2204 }
2205out:
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002206 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002207}
2208
2209/*
2210 * Tries to drain stocked charges in other cpus. This function is asynchronous
2211 * and just put a work per cpu for draining localy on each cpu. Caller can
2212 * expects some charges will be back to res_counter later but cannot wait for
2213 * it.
2214 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002215static void drain_all_stock_async(struct mem_cgroup *root_memcg)
Michal Hockod38144b2011-07-26 16:08:28 -07002216{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002217 /*
2218 * If someone calls draining, avoid adding more kworker runs.
2219 */
2220 if (!mutex_trylock(&percpu_charge_mutex))
2221 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002222 drain_all_stock(root_memcg, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002223 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002224}
2225
2226/* This is a synchronous drain interface. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002227static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002228{
2229 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002230 mutex_lock(&percpu_charge_mutex);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002231 drain_all_stock(root_memcg, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002232 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002233}
2234
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002235/*
2236 * This function drains percpu counter value from DEAD cpu and
2237 * move it to local cpu. Note that this function can be preempted.
2238 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002239static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002240{
2241 int i;
2242
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002243 spin_lock(&memcg->pcp_counter_lock);
Johannes Weiner61046212012-05-29 15:07:05 -07002244 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002245 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002246
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002247 per_cpu(memcg->stat->count[i], cpu) = 0;
2248 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002249 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002250 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002251 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002252
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002253 per_cpu(memcg->stat->events[i], cpu) = 0;
2254 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002255 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002256 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002257}
2258
2259static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002260 unsigned long action,
2261 void *hcpu)
2262{
2263 int cpu = (unsigned long)hcpu;
2264 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002265 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002266
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002267 if (action == CPU_ONLINE)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002268 return NOTIFY_OK;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002269
Kirill A. Shutemovd8330492012-04-12 12:49:11 -07002270 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002271 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002272
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002273 for_each_mem_cgroup(iter)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002274 mem_cgroup_drain_pcp_counter(iter, cpu);
2275
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002276 stock = &per_cpu(memcg_stock, cpu);
2277 drain_stock(stock);
2278 return NOTIFY_OK;
2279}
2280
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002281
2282/* See __mem_cgroup_try_charge() for details */
2283enum {
2284 CHARGE_OK, /* success */
2285 CHARGE_RETRY, /* need to retry but retry is not bad */
2286 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2287 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
2288 CHARGE_OOM_DIE, /* the current is killed because of OOM */
2289};
2290
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002291static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002292 unsigned int nr_pages, unsigned int min_pages,
2293 bool oom_check)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002294{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002295 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002296 struct mem_cgroup *mem_over_limit;
2297 struct res_counter *fail_res;
2298 unsigned long flags = 0;
2299 int ret;
2300
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002301 ret = res_counter_charge(&memcg->res, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002302
2303 if (likely(!ret)) {
2304 if (!do_swap_account)
2305 return CHARGE_OK;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002306 ret = res_counter_charge(&memcg->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002307 if (likely(!ret))
2308 return CHARGE_OK;
2309
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002310 res_counter_uncharge(&memcg->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002311 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2312 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2313 } else
2314 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002315 /*
Johannes Weiner9221edb2011-02-01 15:52:42 -08002316 * Never reclaim on behalf of optional batching, retry with a
2317 * single page instead.
2318 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002319 if (nr_pages > min_pages)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002320 return CHARGE_RETRY;
2321
2322 if (!(gfp_mask & __GFP_WAIT))
2323 return CHARGE_WOULDBLOCK;
2324
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002325 if (gfp_mask & __GFP_NORETRY)
2326 return CHARGE_NOMEM;
2327
Johannes Weiner56600482012-01-12 17:17:59 -08002328 ret = mem_cgroup_reclaim(mem_over_limit, gfp_mask, flags);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002329 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002330 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002331 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002332 * Even though the limit is exceeded at this point, reclaim
2333 * may have been able to free some pages. Retry the charge
2334 * before killing the task.
2335 *
2336 * Only for regular pages, though: huge pages are rather
2337 * unlikely to succeed so close to the limit, and we fall back
2338 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002339 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002340 if (nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER) && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002341 return CHARGE_RETRY;
2342
2343 /*
2344 * At task move, charge accounts can be doubly counted. So, it's
2345 * better to wait until the end of task_move if something is going on.
2346 */
2347 if (mem_cgroup_wait_acct_move(mem_over_limit))
2348 return CHARGE_RETRY;
2349
2350 /* If we don't need to call oom-killer at el, return immediately */
2351 if (!oom_check)
2352 return CHARGE_NOMEM;
2353 /* check OOM */
David Rientjese845e192012-03-21 16:34:10 -07002354 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask, get_order(csize)))
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002355 return CHARGE_OOM_DIE;
2356
2357 return CHARGE_RETRY;
2358}
2359
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002360/*
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002361 * __mem_cgroup_try_charge() does
2362 * 1. detect memcg to be charged against from passed *mm and *ptr,
2363 * 2. update res_counter
2364 * 3. call memory reclaim if necessary.
2365 *
2366 * In some special case, if the task is fatal, fatal_signal_pending() or
2367 * has TIF_MEMDIE, this function returns -EINTR while writing root_mem_cgroup
2368 * to *ptr. There are two reasons for this. 1: fatal threads should quit as soon
2369 * as possible without any hazards. 2: all pages should have a valid
2370 * pc->mem_cgroup. If mm is NULL and the caller doesn't pass a valid memcg
2371 * pointer, that is treated as a charge to root_mem_cgroup.
2372 *
2373 * So __mem_cgroup_try_charge() will return
2374 * 0 ... on success, filling *ptr with a valid memcg pointer.
2375 * -ENOMEM ... charge failure because of resource limits.
2376 * -EINTR ... if thread is fatal. *ptr is filled with root_mem_cgroup.
2377 *
2378 * Unlike the exported interface, an "oom" parameter is added. if oom==true,
2379 * the oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002380 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002381static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002382 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002383 unsigned int nr_pages,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002384 struct mem_cgroup **ptr,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002385 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002386{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002387 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002388 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002389 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002390 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002391
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002392 /*
2393 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2394 * in system level. So, allow to go ahead dying process in addition to
2395 * MEMDIE process.
2396 */
2397 if (unlikely(test_thread_flag(TIF_MEMDIE)
2398 || fatal_signal_pending(current)))
2399 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002400
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002401 /*
Hugh Dickins3be912772008-02-07 00:14:19 -08002402 * We always charge the cgroup the mm_struct belongs to.
2403 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002404 * thread group leader migrates. It's possible that mm is not
Johannes Weiner24467ca2012-07-31 16:45:40 -07002405 * set, if so charge the root memcg (happens for pagecache usage).
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002406 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002407 if (!*ptr && !mm)
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002408 *ptr = root_mem_cgroup;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002409again:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002410 if (*ptr) { /* css should be a valid one */
2411 memcg = *ptr;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002412 if (mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002413 goto done;
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002414 if (consume_stock(memcg, nr_pages))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002415 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002416 css_get(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002417 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002418 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002419
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002420 rcu_read_lock();
2421 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002422 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002423 * Because we don't have task_lock(), "p" can exit.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002424 * In that case, "memcg" can point to root or p can be NULL with
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002425 * race with swapoff. Then, we have small risk of mis-accouning.
2426 * But such kind of mis-account by race always happens because
2427 * we don't have cgroup_mutex(). It's overkill and we allo that
2428 * small race, here.
2429 * (*) swapoff at el will charge against mm-struct not against
2430 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002431 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002432 memcg = mem_cgroup_from_task(p);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002433 if (!memcg)
2434 memcg = root_mem_cgroup;
2435 if (mem_cgroup_is_root(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002436 rcu_read_unlock();
2437 goto done;
2438 }
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002439 if (consume_stock(memcg, nr_pages)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002440 /*
2441 * It seems dagerous to access memcg without css_get().
2442 * But considering how consume_stok works, it's not
2443 * necessary. If consume_stock success, some charges
2444 * from this memcg are cached on this cpu. So, we
2445 * don't need to call css_get()/css_tryget() before
2446 * calling consume_stock().
2447 */
2448 rcu_read_unlock();
2449 goto done;
2450 }
2451 /* after here, we may be blocked. we need to get refcnt */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002452 if (!css_tryget(&memcg->css)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002453 rcu_read_unlock();
2454 goto again;
2455 }
2456 rcu_read_unlock();
2457 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002458
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002459 do {
2460 bool oom_check;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002461
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002462 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002463 if (fatal_signal_pending(current)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002464 css_put(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002465 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002466 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002467
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002468 oom_check = false;
2469 if (oom && !nr_oom_retries) {
2470 oom_check = true;
2471 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2472 }
Balbir Singh6d61ef42009-01-07 18:08:06 -08002473
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002474 ret = mem_cgroup_do_charge(memcg, gfp_mask, batch, nr_pages,
2475 oom_check);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002476 switch (ret) {
2477 case CHARGE_OK:
2478 break;
2479 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002480 batch = nr_pages;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002481 css_put(&memcg->css);
2482 memcg = NULL;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002483 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002484 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002485 css_put(&memcg->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002486 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002487 case CHARGE_NOMEM: /* OOM routine works */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002488 if (!oom) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002489 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002490 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002491 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002492 /* If oom, we never return -ENOMEM */
2493 nr_oom_retries--;
2494 break;
2495 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002496 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002497 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08002498 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002499 } while (ret != CHARGE_OK);
2500
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002501 if (batch > nr_pages)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002502 refill_stock(memcg, batch - nr_pages);
2503 css_put(&memcg->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002504done:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002505 *ptr = memcg;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002506 return 0;
2507nomem:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002508 *ptr = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002509 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002510bypass:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002511 *ptr = root_mem_cgroup;
2512 return -EINTR;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002513}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002514
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002515/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002516 * Somemtimes we have to undo a charge we got by try_charge().
2517 * This function is for that and do uncharge, put css's refcnt.
2518 * gotten by try_charge().
2519 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002520static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002521 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002522{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002523 if (!mem_cgroup_is_root(memcg)) {
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002524 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002525
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002526 res_counter_uncharge(&memcg->res, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002527 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002528 res_counter_uncharge(&memcg->memsw, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002529 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002530}
2531
2532/*
KAMEZAWA Hiroyukid01dd172012-05-29 15:07:03 -07002533 * Cancel chrages in this cgroup....doesn't propagate to parent cgroup.
2534 * This is useful when moving usage to parent cgroup.
2535 */
2536static void __mem_cgroup_cancel_local_charge(struct mem_cgroup *memcg,
2537 unsigned int nr_pages)
2538{
2539 unsigned long bytes = nr_pages * PAGE_SIZE;
2540
2541 if (mem_cgroup_is_root(memcg))
2542 return;
2543
2544 res_counter_uncharge_until(&memcg->res, memcg->res.parent, bytes);
2545 if (do_swap_account)
2546 res_counter_uncharge_until(&memcg->memsw,
2547 memcg->memsw.parent, bytes);
2548}
2549
2550/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002551 * A helper function to get mem_cgroup from ID. must be called under
Tejun Heoe9316082012-11-05 09:16:58 -08002552 * rcu_read_lock(). The caller is responsible for calling css_tryget if
2553 * the mem_cgroup is used for charging. (dropping refcnt from swap can be
2554 * called against removed memcg.)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002555 */
2556static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2557{
2558 struct cgroup_subsys_state *css;
2559
2560 /* ID 0 is unused ID */
2561 if (!id)
2562 return NULL;
2563 css = css_lookup(&mem_cgroup_subsys, id);
2564 if (!css)
2565 return NULL;
Wanpeng Lib2145142012-07-31 16:46:01 -07002566 return mem_cgroup_from_css(css);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002567}
2568
Wu Fengguange42d9d52009-12-16 12:19:59 +01002569struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002570{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002571 struct mem_cgroup *memcg = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002572 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002573 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002574 swp_entry_t ent;
2575
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002576 VM_BUG_ON(!PageLocked(page));
2577
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002578 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f632009-04-30 15:08:11 -07002579 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002580 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002581 memcg = pc->mem_cgroup;
2582 if (memcg && !css_tryget(&memcg->css))
2583 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002584 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002585 ent.val = page_private(page);
Bob Liu9fb4b7c2012-01-12 17:18:48 -08002586 id = lookup_swap_cgroup_id(ent);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002587 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002588 memcg = mem_cgroup_lookup(id);
2589 if (memcg && !css_tryget(&memcg->css))
2590 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002591 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002592 }
Daisuke Nishimurac0bd3f632009-04-30 15:08:11 -07002593 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002594 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002595}
2596
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002597static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,
Johannes Weiner5564e882011-03-23 16:42:29 -07002598 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002599 unsigned int nr_pages,
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002600 enum charge_type ctype,
2601 bool lrucare)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002602{
Johannes Weinerce587e62012-04-24 20:22:33 +02002603 struct page_cgroup *pc = lookup_page_cgroup(page);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002604 struct zone *uninitialized_var(zone);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002605 struct lruvec *lruvec;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002606 bool was_on_lru = false;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002607 bool anon;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002608
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002609 lock_page_cgroup(pc);
Johannes Weiner90deb782012-07-31 16:45:47 -07002610 VM_BUG_ON(PageCgroupUsed(pc));
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002611 /*
2612 * we don't need page_cgroup_lock about tail pages, becase they are not
2613 * accessed by any other context at this point.
2614 */
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002615
2616 /*
2617 * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page
2618 * may already be on some other mem_cgroup's LRU. Take care of it.
2619 */
2620 if (lrucare) {
2621 zone = page_zone(page);
2622 spin_lock_irq(&zone->lru_lock);
2623 if (PageLRU(page)) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002624 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002625 ClearPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002626 del_page_from_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002627 was_on_lru = true;
2628 }
2629 }
2630
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002631 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002632 /*
2633 * We access a page_cgroup asynchronously without lock_page_cgroup().
2634 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2635 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2636 * before USED bit, we need memory barrier here.
2637 * See mem_cgroup_add_lru_list(), etc.
2638 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002639 smp_wmb();
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002640 SetPageCgroupUsed(pc);
Hugh Dickins3be912772008-02-07 00:14:19 -08002641
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002642 if (lrucare) {
2643 if (was_on_lru) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002644 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002645 VM_BUG_ON(PageLRU(page));
2646 SetPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002647 add_page_to_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002648 }
2649 spin_unlock_irq(&zone->lru_lock);
2650 }
2651
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07002652 if (ctype == MEM_CGROUP_CHARGE_TYPE_ANON)
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002653 anon = true;
2654 else
2655 anon = false;
2656
2657 mem_cgroup_charge_statistics(memcg, anon, nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002658 unlock_page_cgroup(pc);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002659
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002660 /*
2661 * "charge_statistics" updated event counter. Then, check it.
2662 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2663 * if they exceeds softlimit.
2664 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002665 memcg_check_events(memcg, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002666}
2667
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002668#ifdef CONFIG_MEMCG_KMEM
2669static inline bool memcg_can_account_kmem(struct mem_cgroup *memcg)
2670{
2671 return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg) &&
2672 (memcg->kmem_account_flags & KMEM_ACCOUNTED_MASK);
2673}
2674
2675static int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp, u64 size)
2676{
2677 struct res_counter *fail_res;
2678 struct mem_cgroup *_memcg;
2679 int ret = 0;
2680 bool may_oom;
2681
2682 ret = res_counter_charge(&memcg->kmem, size, &fail_res);
2683 if (ret)
2684 return ret;
2685
2686 /*
2687 * Conditions under which we can wait for the oom_killer. Those are
2688 * the same conditions tested by the core page allocator
2689 */
2690 may_oom = (gfp & __GFP_FS) && !(gfp & __GFP_NORETRY);
2691
2692 _memcg = memcg;
2693 ret = __mem_cgroup_try_charge(NULL, gfp, size >> PAGE_SHIFT,
2694 &_memcg, may_oom);
2695
2696 if (ret == -EINTR) {
2697 /*
2698 * __mem_cgroup_try_charge() chosed to bypass to root due to
2699 * OOM kill or fatal signal. Since our only options are to
2700 * either fail the allocation or charge it to this cgroup, do
2701 * it as a temporary condition. But we can't fail. From a
2702 * kmem/slab perspective, the cache has already been selected,
2703 * by mem_cgroup_kmem_get_cache(), so it is too late to change
2704 * our minds.
2705 *
2706 * This condition will only trigger if the task entered
2707 * memcg_charge_kmem in a sane state, but was OOM-killed during
2708 * __mem_cgroup_try_charge() above. Tasks that were already
2709 * dying when the allocation triggers should have been already
2710 * directed to the root cgroup in memcontrol.h
2711 */
2712 res_counter_charge_nofail(&memcg->res, size, &fail_res);
2713 if (do_swap_account)
2714 res_counter_charge_nofail(&memcg->memsw, size,
2715 &fail_res);
2716 ret = 0;
2717 } else if (ret)
2718 res_counter_uncharge(&memcg->kmem, size);
2719
2720 return ret;
2721}
2722
2723static void memcg_uncharge_kmem(struct mem_cgroup *memcg, u64 size)
2724{
2725 res_counter_uncharge(&memcg->kmem, size);
2726 res_counter_uncharge(&memcg->res, size);
2727 if (do_swap_account)
2728 res_counter_uncharge(&memcg->memsw, size);
2729}
2730
2731/*
2732 * We need to verify if the allocation against current->mm->owner's memcg is
2733 * possible for the given order. But the page is not allocated yet, so we'll
2734 * need a further commit step to do the final arrangements.
2735 *
2736 * It is possible for the task to switch cgroups in this mean time, so at
2737 * commit time, we can't rely on task conversion any longer. We'll then use
2738 * the handle argument to return to the caller which cgroup we should commit
2739 * against. We could also return the memcg directly and avoid the pointer
2740 * passing, but a boolean return value gives better semantics considering
2741 * the compiled-out case as well.
2742 *
2743 * Returning true means the allocation is possible.
2744 */
2745bool
2746__memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **_memcg, int order)
2747{
2748 struct mem_cgroup *memcg;
2749 int ret;
2750
2751 *_memcg = NULL;
2752 memcg = try_get_mem_cgroup_from_mm(current->mm);
2753
2754 /*
2755 * very rare case described in mem_cgroup_from_task. Unfortunately there
2756 * isn't much we can do without complicating this too much, and it would
2757 * be gfp-dependent anyway. Just let it go
2758 */
2759 if (unlikely(!memcg))
2760 return true;
2761
2762 if (!memcg_can_account_kmem(memcg)) {
2763 css_put(&memcg->css);
2764 return true;
2765 }
2766
2767 mem_cgroup_get(memcg);
2768
2769 ret = memcg_charge_kmem(memcg, gfp, PAGE_SIZE << order);
2770 if (!ret)
2771 *_memcg = memcg;
2772 else
2773 mem_cgroup_put(memcg);
2774
2775 css_put(&memcg->css);
2776 return (ret == 0);
2777}
2778
2779void __memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg,
2780 int order)
2781{
2782 struct page_cgroup *pc;
2783
2784 VM_BUG_ON(mem_cgroup_is_root(memcg));
2785
2786 /* The page allocation failed. Revert */
2787 if (!page) {
2788 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
2789 mem_cgroup_put(memcg);
2790 return;
2791 }
2792
2793 pc = lookup_page_cgroup(page);
2794 lock_page_cgroup(pc);
2795 pc->mem_cgroup = memcg;
2796 SetPageCgroupUsed(pc);
2797 unlock_page_cgroup(pc);
2798}
2799
2800void __memcg_kmem_uncharge_pages(struct page *page, int order)
2801{
2802 struct mem_cgroup *memcg = NULL;
2803 struct page_cgroup *pc;
2804
2805
2806 pc = lookup_page_cgroup(page);
2807 /*
2808 * Fast unlocked return. Theoretically might have changed, have to
2809 * check again after locking.
2810 */
2811 if (!PageCgroupUsed(pc))
2812 return;
2813
2814 lock_page_cgroup(pc);
2815 if (PageCgroupUsed(pc)) {
2816 memcg = pc->mem_cgroup;
2817 ClearPageCgroupUsed(pc);
2818 }
2819 unlock_page_cgroup(pc);
2820
2821 /*
2822 * We trust that only if there is a memcg associated with the page, it
2823 * is a valid allocation
2824 */
2825 if (!memcg)
2826 return;
2827
2828 VM_BUG_ON(mem_cgroup_is_root(memcg));
2829 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
2830 mem_cgroup_put(memcg);
2831}
2832#endif /* CONFIG_MEMCG_KMEM */
2833
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002834#ifdef CONFIG_TRANSPARENT_HUGEPAGE
2835
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002836#define PCGF_NOCOPY_AT_SPLIT (1 << PCG_LOCK | 1 << PCG_MIGRATION)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002837/*
2838 * Because tail pages are not marked as "used", set it. We're under
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002839 * zone->lru_lock, 'splitting on pmd' and compound_lock.
2840 * charge/uncharge will be never happen and move_account() is done under
2841 * compound_lock(), so we don't have to take care of races.
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002842 */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002843void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002844{
2845 struct page_cgroup *head_pc = lookup_page_cgroup(head);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002846 struct page_cgroup *pc;
2847 int i;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002848
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08002849 if (mem_cgroup_disabled())
2850 return;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002851 for (i = 1; i < HPAGE_PMD_NR; i++) {
2852 pc = head_pc + i;
2853 pc->mem_cgroup = head_pc->mem_cgroup;
2854 smp_wmb();/* see __commit_charge() */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002855 pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
2856 }
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002857}
Hugh Dickins12d27102012-01-12 17:19:52 -08002858#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002859
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002860/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07002861 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07002862 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002863 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002864 * @pc: page_cgroup of the page.
2865 * @from: mem_cgroup which the page is moved from.
2866 * @to: mem_cgroup which the page is moved to. @from != @to.
2867 *
2868 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002869 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002870 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002871 *
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07002872 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
2873 * from old cgroup.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002874 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002875static int mem_cgroup_move_account(struct page *page,
2876 unsigned int nr_pages,
2877 struct page_cgroup *pc,
2878 struct mem_cgroup *from,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07002879 struct mem_cgroup *to)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002880{
Johannes Weinerde3638d2011-03-23 16:42:28 -07002881 unsigned long flags;
2882 int ret;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002883 bool anon = PageAnon(page);
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002884
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002885 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07002886 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07002887 /*
2888 * The page is isolated from LRU. So, collapse function
2889 * will not handle this page. But page splitting can happen.
2890 * Do this check under compound_page_lock(). The caller should
2891 * hold it.
2892 */
2893 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002894 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07002895 goto out;
2896
2897 lock_page_cgroup(pc);
2898
2899 ret = -EINVAL;
2900 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
2901 goto unlock;
2902
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07002903 move_lock_mem_cgroup(from, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002904
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07002905 if (!anon && page_mapped(page)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08002906 /* Update mapped_file data for mem_cgroup */
2907 preempt_disable();
2908 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2909 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2910 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07002911 }
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002912 mem_cgroup_charge_statistics(from, anon, -nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07002913
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002914 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002915 pc->mem_cgroup = to;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002916 mem_cgroup_charge_statistics(to, anon, nr_pages);
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07002917 move_unlock_mem_cgroup(from, &flags);
Johannes Weinerde3638d2011-03-23 16:42:28 -07002918 ret = 0;
2919unlock:
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002920 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002921 /*
2922 * check events
2923 */
Johannes Weiner5564e882011-03-23 16:42:29 -07002924 memcg_check_events(to, page);
2925 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07002926out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002927 return ret;
2928}
2929
Michal Hocko2ef37d32012-10-26 13:37:30 +02002930/**
2931 * mem_cgroup_move_parent - moves page to the parent group
2932 * @page: the page to move
2933 * @pc: page_cgroup of the page
2934 * @child: page's cgroup
2935 *
2936 * move charges to its parent or the root cgroup if the group has no
2937 * parent (aka use_hierarchy==0).
2938 * Although this might fail (get_page_unless_zero, isolate_lru_page or
2939 * mem_cgroup_move_account fails) the failure is always temporary and
2940 * it signals a race with a page removal/uncharge or migration. In the
2941 * first case the page is on the way out and it will vanish from the LRU
2942 * on the next attempt and the call should be retried later.
2943 * Isolation from the LRU fails only if page has been isolated from
2944 * the LRU since we looked at it and that usually means either global
2945 * reclaim or migration going on. The page will either get back to the
2946 * LRU or vanish.
2947 * Finaly mem_cgroup_move_account fails only if the page got uncharged
2948 * (!PageCgroupUsed) or moved to a different group. The page will
2949 * disappear in the next attempt.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002950 */
Johannes Weiner5564e882011-03-23 16:42:29 -07002951static int mem_cgroup_move_parent(struct page *page,
2952 struct page_cgroup *pc,
KAMEZAWA Hiroyuki6068bf02012-07-31 16:42:45 -07002953 struct mem_cgroup *child)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002954{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002955 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002956 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07002957 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002958 int ret;
2959
Michal Hockod8423012012-10-26 13:37:29 +02002960 VM_BUG_ON(mem_cgroup_is_root(child));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002961
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002962 ret = -EBUSY;
2963 if (!get_page_unless_zero(page))
2964 goto out;
2965 if (isolate_lru_page(page))
2966 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08002967
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002968 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002969
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07002970 parent = parent_mem_cgroup(child);
2971 /*
2972 * If no parent, move charges to root cgroup.
2973 */
2974 if (!parent)
2975 parent = root_mem_cgroup;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002976
Michal Hocko2ef37d32012-10-26 13:37:30 +02002977 if (nr_pages > 1) {
2978 VM_BUG_ON(!PageTransHuge(page));
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002979 flags = compound_lock_irqsave(page);
Michal Hocko2ef37d32012-10-26 13:37:30 +02002980 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002981
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07002982 ret = mem_cgroup_move_account(page, nr_pages,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07002983 pc, child, parent);
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07002984 if (!ret)
2985 __mem_cgroup_cancel_local_charge(child, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08002986
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002987 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002988 compound_unlock_irqrestore(page, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002989 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002990put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08002991 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002992out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002993 return ret;
2994}
2995
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002996/*
2997 * Charge the memory controller for page usage.
2998 * Return
2999 * 0 if the charge was successful
3000 * < 0 if the cgroup is over its limit
3001 */
3002static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07003003 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003004{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003005 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003006 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08003007 bool oom = true;
3008 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08003009
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003010 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003011 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003012 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08003013 /*
3014 * Never OOM-kill a process for a huge page. The
3015 * fault handler will fall back to regular pages.
3016 */
3017 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003018 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003019
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003020 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003021 if (ret == -ENOMEM)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003022 return ret;
Johannes Weinerce587e62012-04-24 20:22:33 +02003023 __mem_cgroup_commit_charge(memcg, page, nr_pages, ctype, false);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003024 return 0;
3025}
3026
3027int mem_cgroup_newpage_charge(struct page *page,
3028 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003029{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003030 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07003031 return 0;
Johannes Weiner7a0524c2012-01-12 17:18:43 -08003032 VM_BUG_ON(page_mapped(page));
3033 VM_BUG_ON(page->mapping && !PageAnon(page));
3034 VM_BUG_ON(!mm);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003035 return mem_cgroup_charge_common(page, mm, gfp_mask,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003036 MEM_CGROUP_CHARGE_TYPE_ANON);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003037}
3038
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003039/*
3040 * While swap-in, try_charge -> commit or cancel, the page is locked.
3041 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02003042 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003043 * "commit()" or removed by "cancel()"
3044 */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003045static int __mem_cgroup_try_charge_swapin(struct mm_struct *mm,
3046 struct page *page,
3047 gfp_t mask,
3048 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003049{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003050 struct mem_cgroup *memcg;
Johannes Weiner90deb782012-07-31 16:45:47 -07003051 struct page_cgroup *pc;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003052 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003053
Johannes Weiner90deb782012-07-31 16:45:47 -07003054 pc = lookup_page_cgroup(page);
3055 /*
3056 * Every swap fault against a single page tries to charge the
3057 * page, bail as early as possible. shmem_unuse() encounters
3058 * already charged pages, too. The USED bit is protected by
3059 * the page lock, which serializes swap cache removal, which
3060 * in turn serializes uncharging.
3061 */
3062 if (PageCgroupUsed(pc))
3063 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003064 if (!do_swap_account)
3065 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003066 memcg = try_get_mem_cgroup_from_page(page);
3067 if (!memcg)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003068 goto charge_cur_mm;
Johannes Weiner72835c82012-01-12 17:18:32 -08003069 *memcgp = memcg;
3070 ret = __mem_cgroup_try_charge(NULL, mask, 1, memcgp, true);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003071 css_put(&memcg->css);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003072 if (ret == -EINTR)
3073 ret = 0;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003074 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003075charge_cur_mm:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003076 ret = __mem_cgroup_try_charge(mm, mask, 1, memcgp, true);
3077 if (ret == -EINTR)
3078 ret = 0;
3079 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003080}
3081
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003082int mem_cgroup_try_charge_swapin(struct mm_struct *mm, struct page *page,
3083 gfp_t gfp_mask, struct mem_cgroup **memcgp)
3084{
3085 *memcgp = NULL;
3086 if (mem_cgroup_disabled())
3087 return 0;
Johannes Weinerbdf4f4d2012-07-31 16:45:50 -07003088 /*
3089 * A racing thread's fault, or swapoff, may have already
3090 * updated the pte, and even removed page from swap cache: in
3091 * those cases unuse_pte()'s pte_same() test will fail; but
3092 * there's also a KSM case which does need to charge the page.
3093 */
3094 if (!PageSwapCache(page)) {
3095 int ret;
3096
3097 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, memcgp, true);
3098 if (ret == -EINTR)
3099 ret = 0;
3100 return ret;
3101 }
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003102 return __mem_cgroup_try_charge_swapin(mm, page, gfp_mask, memcgp);
3103}
3104
Johannes Weiner827a03d2012-07-31 16:45:36 -07003105void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
3106{
3107 if (mem_cgroup_disabled())
3108 return;
3109 if (!memcg)
3110 return;
3111 __mem_cgroup_cancel_charge(memcg, 1);
3112}
3113
Daisuke Nishimura83aae4c72009-04-02 16:57:48 -07003114static void
Johannes Weiner72835c82012-01-12 17:18:32 -08003115__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *memcg,
Daisuke Nishimura83aae4c72009-04-02 16:57:48 -07003116 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003117{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003118 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003119 return;
Johannes Weiner72835c82012-01-12 17:18:32 -08003120 if (!memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003121 return;
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07003122
Johannes Weinerce587e62012-04-24 20:22:33 +02003123 __mem_cgroup_commit_charge(memcg, page, 1, ctype, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003124 /*
3125 * Now swap is on-memory. This means this page may be
3126 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08003127 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
3128 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
3129 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003130 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08003131 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003132 swp_entry_t ent = {.val = page_private(page)};
Hugh Dickins86493002012-05-29 15:06:52 -07003133 mem_cgroup_uncharge_swap(ent);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003134 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003135}
3136
Johannes Weiner72835c82012-01-12 17:18:32 -08003137void mem_cgroup_commit_charge_swapin(struct page *page,
3138 struct mem_cgroup *memcg)
Daisuke Nishimura83aae4c72009-04-02 16:57:48 -07003139{
Johannes Weiner72835c82012-01-12 17:18:32 -08003140 __mem_cgroup_commit_charge_swapin(page, memcg,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003141 MEM_CGROUP_CHARGE_TYPE_ANON);
Daisuke Nishimura83aae4c72009-04-02 16:57:48 -07003142}
3143
Johannes Weiner827a03d2012-07-31 16:45:36 -07003144int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
3145 gfp_t gfp_mask)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003146{
Johannes Weiner827a03d2012-07-31 16:45:36 -07003147 struct mem_cgroup *memcg = NULL;
3148 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
3149 int ret;
3150
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003151 if (mem_cgroup_disabled())
Johannes Weiner827a03d2012-07-31 16:45:36 -07003152 return 0;
3153 if (PageCompound(page))
3154 return 0;
3155
Johannes Weiner827a03d2012-07-31 16:45:36 -07003156 if (!PageSwapCache(page))
3157 ret = mem_cgroup_charge_common(page, mm, gfp_mask, type);
3158 else { /* page is swapcache/shmem */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003159 ret = __mem_cgroup_try_charge_swapin(mm, page,
3160 gfp_mask, &memcg);
Johannes Weiner827a03d2012-07-31 16:45:36 -07003161 if (!ret)
3162 __mem_cgroup_commit_charge_swapin(page, memcg, type);
3163 }
3164 return ret;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003165}
3166
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003167static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003168 unsigned int nr_pages,
3169 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003170{
3171 struct memcg_batch_info *batch = NULL;
3172 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003173
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003174 /* If swapout, usage of swap doesn't decrease */
3175 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
3176 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003177
3178 batch = &current->memcg_batch;
3179 /*
3180 * In usual, we do css_get() when we remember memcg pointer.
3181 * But in this case, we keep res->usage until end of a series of
3182 * uncharges. Then, it's ok to ignore memcg's refcnt.
3183 */
3184 if (!batch->memcg)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003185 batch->memcg = memcg;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003186 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003187 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003188 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003189 * the same cgroup and we have chance to coalesce uncharges.
3190 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
3191 * because we want to do uncharge as soon as possible.
3192 */
3193
3194 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
3195 goto direct_uncharge;
3196
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003197 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08003198 goto direct_uncharge;
3199
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003200 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003201 * In typical case, batch->memcg == mem. This means we can
3202 * merge a series of uncharges to an uncharge of res_counter.
3203 * If not, we uncharge res_counter ony by one.
3204 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003205 if (batch->memcg != memcg)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003206 goto direct_uncharge;
3207 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003208 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003209 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003210 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003211 return;
3212direct_uncharge:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003213 res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003214 if (uncharge_memsw)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003215 res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE);
3216 if (unlikely(batch->memcg != memcg))
3217 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003218}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003219
Balbir Singh8697d332008-02-07 00:13:59 -08003220/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003221 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003222 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003223static struct mem_cgroup *
Johannes Weiner0030f532012-07-31 16:45:25 -07003224__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype,
3225 bool end_migration)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003226{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003227 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003228 unsigned int nr_pages = 1;
3229 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003230 bool anon;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003231
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003232 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003233 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07003234
Johannes Weiner0c59b892012-07-31 16:45:31 -07003235 VM_BUG_ON(PageSwapCache(page));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003236
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003237 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003238 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003239 VM_BUG_ON(!PageTransHuge(page));
3240 }
Balbir Singh8697d332008-02-07 00:13:59 -08003241 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08003242 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08003243 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003244 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08003245 if (unlikely(!PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003246 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003247
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003248 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003249
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003250 memcg = pc->mem_cgroup;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003251
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003252 if (!PageCgroupUsed(pc))
3253 goto unlock_out;
3254
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003255 anon = PageAnon(page);
3256
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003257 switch (ctype) {
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003258 case MEM_CGROUP_CHARGE_TYPE_ANON:
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07003259 /*
3260 * Generally PageAnon tells if it's the anon statistics to be
3261 * updated; but sometimes e.g. mem_cgroup_uncharge_page() is
3262 * used before page reached the stage of being marked PageAnon.
3263 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003264 anon = true;
3265 /* fallthrough */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003266 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003267 /* See mem_cgroup_prepare_migration() */
Johannes Weiner0030f532012-07-31 16:45:25 -07003268 if (page_mapped(page))
3269 goto unlock_out;
3270 /*
3271 * Pages under migration may not be uncharged. But
3272 * end_migration() /must/ be the one uncharging the
3273 * unused post-migration page and so it has to call
3274 * here with the migration bit still set. See the
3275 * res_counter handling below.
3276 */
3277 if (!end_migration && PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003278 goto unlock_out;
3279 break;
3280 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
3281 if (!PageAnon(page)) { /* Shared memory */
3282 if (page->mapping && !page_is_file_cache(page))
3283 goto unlock_out;
3284 } else if (page_mapped(page)) /* Anon */
3285 goto unlock_out;
3286 break;
3287 default:
3288 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003289 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003290
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003291 mem_cgroup_charge_statistics(memcg, anon, -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003292
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003293 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08003294 /*
3295 * pc->mem_cgroup is not cleared here. It will be accessed when it's
3296 * freed from LRU. This is safe because uncharged page is expected not
3297 * to be reused (freed soon). Exception is SwapCache, it's handled by
3298 * special functions.
3299 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003300
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003301 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003302 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003303 * even after unlock, we have memcg->res.usage here and this memcg
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003304 * will never be freed.
3305 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003306 memcg_check_events(memcg, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003307 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003308 mem_cgroup_swap_statistics(memcg, true);
3309 mem_cgroup_get(memcg);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003310 }
Johannes Weiner0030f532012-07-31 16:45:25 -07003311 /*
3312 * Migration does not charge the res_counter for the
3313 * replacement page, so leave it alone when phasing out the
3314 * page that is unused after the migration.
3315 */
3316 if (!end_migration && !mem_cgroup_is_root(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003317 mem_cgroup_do_uncharge(memcg, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003318
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003319 return memcg;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003320
3321unlock_out:
3322 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003323 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08003324}
3325
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003326void mem_cgroup_uncharge_page(struct page *page)
3327{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003328 /* early check. */
3329 if (page_mapped(page))
3330 return;
Johannes Weiner40f23a22012-01-12 17:18:45 -08003331 VM_BUG_ON(page->mapping && !PageAnon(page));
Johannes Weiner0c59b892012-07-31 16:45:31 -07003332 if (PageSwapCache(page))
3333 return;
Johannes Weiner0030f532012-07-31 16:45:25 -07003334 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_ANON, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003335}
3336
3337void mem_cgroup_uncharge_cache_page(struct page *page)
3338{
3339 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07003340 VM_BUG_ON(page->mapping);
Johannes Weiner0030f532012-07-31 16:45:25 -07003341 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003342}
3343
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003344/*
3345 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
3346 * In that cases, pages are freed continuously and we can expect pages
3347 * are in the same memcg. All these calls itself limits the number of
3348 * pages freed at once, then uncharge_start/end() is called properly.
3349 * This may be called prural(2) times in a context,
3350 */
3351
3352void mem_cgroup_uncharge_start(void)
3353{
3354 current->memcg_batch.do_batch++;
3355 /* We can do nest. */
3356 if (current->memcg_batch.do_batch == 1) {
3357 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003358 current->memcg_batch.nr_pages = 0;
3359 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003360 }
3361}
3362
3363void mem_cgroup_uncharge_end(void)
3364{
3365 struct memcg_batch_info *batch = &current->memcg_batch;
3366
3367 if (!batch->do_batch)
3368 return;
3369
3370 batch->do_batch--;
3371 if (batch->do_batch) /* If stacked, do nothing. */
3372 return;
3373
3374 if (!batch->memcg)
3375 return;
3376 /*
3377 * This "batch->memcg" is valid without any css_get/put etc...
3378 * bacause we hide charges behind us.
3379 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003380 if (batch->nr_pages)
3381 res_counter_uncharge(&batch->memcg->res,
3382 batch->nr_pages * PAGE_SIZE);
3383 if (batch->memsw_nr_pages)
3384 res_counter_uncharge(&batch->memcg->memsw,
3385 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003386 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003387 /* forget this pointer (for sanity check) */
3388 batch->memcg = NULL;
3389}
3390
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003391#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003392/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003393 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003394 * memcg information is recorded to swap_cgroup of "ent"
3395 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003396void
3397mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003398{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003399 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003400 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003401
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003402 if (!swapout) /* this was a swap cache but the swap is unused ! */
3403 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
3404
Johannes Weiner0030f532012-07-31 16:45:25 -07003405 memcg = __mem_cgroup_uncharge_common(page, ctype, false);
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003406
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003407 /*
3408 * record memcg information, if swapout && memcg != NULL,
3409 * mem_cgroup_get() was called in uncharge().
3410 */
3411 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003412 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003413}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003414#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003415
Andrew Mortonc255a452012-07-31 16:43:02 -07003416#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003417/*
3418 * called from swap_entry_free(). remove record in swap_cgroup and
3419 * uncharge "memsw" account.
3420 */
3421void mem_cgroup_uncharge_swap(swp_entry_t ent)
3422{
3423 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003424 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003425
3426 if (!do_swap_account)
3427 return;
3428
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003429 id = swap_cgroup_record(ent, 0);
3430 rcu_read_lock();
3431 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003432 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003433 /*
3434 * We uncharge this because swap is freed.
3435 * This memcg can be obsolete one. We avoid calling css_tryget
3436 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003437 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07003438 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003439 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003440 mem_cgroup_put(memcg);
3441 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003442 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003443}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003444
3445/**
3446 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
3447 * @entry: swap entry to be moved
3448 * @from: mem_cgroup which the entry is moved from
3449 * @to: mem_cgroup which the entry is moved to
3450 *
3451 * It succeeds only when the swap_cgroup's record for this entry is the same
3452 * as the mem_cgroup's id of @from.
3453 *
3454 * Returns 0 on success, -EINVAL on failure.
3455 *
3456 * The caller must have charged to @to, IOW, called res_counter_charge() about
3457 * both res and memsw, and called css_get().
3458 */
3459static int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07003460 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003461{
3462 unsigned short old_id, new_id;
3463
3464 old_id = css_id(&from->css);
3465 new_id = css_id(&to->css);
3466
3467 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08003468 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003469 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003470 /*
3471 * This function is only called from task migration context now.
3472 * It postpones res_counter and refcount handling till the end
3473 * of task migration(mem_cgroup_clear_mc()) for performance
3474 * improvement. But we cannot postpone mem_cgroup_get(to)
3475 * because if the process that has been moved to @to does
3476 * swap-in, the refcount of @to might be decreased to 0.
3477 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08003478 mem_cgroup_get(to);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003479 return 0;
3480 }
3481 return -EINVAL;
3482}
3483#else
3484static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07003485 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003486{
3487 return -EINVAL;
3488}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003489#endif
3490
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003491/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003492 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
3493 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003494 */
Johannes Weiner0030f532012-07-31 16:45:25 -07003495void mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
3496 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003497{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003498 struct mem_cgroup *memcg = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00003499 unsigned int nr_pages = 1;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003500 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003501 enum charge_type ctype;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003502
Johannes Weiner72835c82012-01-12 17:18:32 -08003503 *memcgp = NULL;
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07003504
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003505 if (mem_cgroup_disabled())
Johannes Weiner0030f532012-07-31 16:45:25 -07003506 return;
Balbir Singh40779602008-04-04 14:29:59 -07003507
Mel Gormanb32967f2012-11-19 12:35:47 +00003508 if (PageTransHuge(page))
3509 nr_pages <<= compound_order(page);
3510
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003511 pc = lookup_page_cgroup(page);
3512 lock_page_cgroup(pc);
3513 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003514 memcg = pc->mem_cgroup;
3515 css_get(&memcg->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003516 /*
3517 * At migrating an anonymous page, its mapcount goes down
3518 * to 0 and uncharge() will be called. But, even if it's fully
3519 * unmapped, migration may fail and this page has to be
3520 * charged again. We set MIGRATION flag here and delay uncharge
3521 * until end_migration() is called
3522 *
3523 * Corner Case Thinking
3524 * A)
3525 * When the old page was mapped as Anon and it's unmap-and-freed
3526 * while migration was ongoing.
3527 * If unmap finds the old page, uncharge() of it will be delayed
3528 * until end_migration(). If unmap finds a new page, it's
3529 * uncharged when it make mapcount to be 1->0. If unmap code
3530 * finds swap_migration_entry, the new page will not be mapped
3531 * and end_migration() will find it(mapcount==0).
3532 *
3533 * B)
3534 * When the old page was mapped but migraion fails, the kernel
3535 * remaps it. A charge for it is kept by MIGRATION flag even
3536 * if mapcount goes down to 0. We can do remap successfully
3537 * without charging it again.
3538 *
3539 * C)
3540 * The "old" page is under lock_page() until the end of
3541 * migration, so, the old page itself will not be swapped-out.
3542 * If the new page is swapped out before end_migraton, our
3543 * hook to usual swap-out path will catch the event.
3544 */
3545 if (PageAnon(page))
3546 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003547 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003548 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003549 /*
3550 * If the page is not charged at this point,
3551 * we return here.
3552 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003553 if (!memcg)
Johannes Weiner0030f532012-07-31 16:45:25 -07003554 return;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003555
Johannes Weiner72835c82012-01-12 17:18:32 -08003556 *memcgp = memcg;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003557 /*
3558 * We charge new page before it's used/mapped. So, even if unlock_page()
3559 * is called before end_migration, we can catch all events on this new
3560 * page. In the case new page is migrated but not remapped, new page's
3561 * mapcount will be finally 0 and we call uncharge in end_migration().
3562 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003563 if (PageAnon(page))
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003564 ctype = MEM_CGROUP_CHARGE_TYPE_ANON;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003565 else
Johannes Weiner62ba7442012-07-31 16:45:39 -07003566 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
Johannes Weiner0030f532012-07-31 16:45:25 -07003567 /*
3568 * The page is committed to the memcg, but it's not actually
3569 * charged to the res_counter since we plan on replacing the
3570 * old one and only one page is going to be left afterwards.
3571 */
Mel Gormanb32967f2012-11-19 12:35:47 +00003572 __mem_cgroup_commit_charge(memcg, newpage, nr_pages, ctype, false);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003573}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08003574
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003575/* remove redundant charge if migration failed*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003576void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003577 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003578{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003579 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003580 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003581 bool anon;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003582
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003583 if (!memcg)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003584 return;
Tejun Heob25ed602012-11-05 09:16:59 -08003585
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003586 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003587 used = oldpage;
3588 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003589 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003590 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003591 unused = oldpage;
3592 }
Johannes Weiner0030f532012-07-31 16:45:25 -07003593 anon = PageAnon(used);
Johannes Weiner7d188952012-07-31 16:45:34 -07003594 __mem_cgroup_uncharge_common(unused,
3595 anon ? MEM_CGROUP_CHARGE_TYPE_ANON
3596 : MEM_CGROUP_CHARGE_TYPE_CACHE,
3597 true);
Johannes Weiner0030f532012-07-31 16:45:25 -07003598 css_put(&memcg->css);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003599 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003600 * We disallowed uncharge of pages under migration because mapcount
3601 * of the page goes down to zero, temporarly.
3602 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003603 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003604 pc = lookup_page_cgroup(oldpage);
3605 lock_page_cgroup(pc);
3606 ClearPageCgroupMigration(pc);
3607 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003608
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003609 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003610 * If a page is a file cache, radix-tree replacement is very atomic
3611 * and we can skip this check. When it was an Anon page, its mapcount
3612 * goes down to 0. But because we added MIGRATION flage, it's not
3613 * uncharged yet. There are several case but page->mapcount check
3614 * and USED bit check in mem_cgroup_uncharge_page() will do enough
3615 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003616 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003617 if (anon)
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003618 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003619}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08003620
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003621/*
3622 * At replace page cache, newpage is not under any memcg but it's on
3623 * LRU. So, this function doesn't touch res_counter but handles LRU
3624 * in correct way. Both pages are locked so we cannot race with uncharge.
3625 */
3626void mem_cgroup_replace_page_cache(struct page *oldpage,
3627 struct page *newpage)
3628{
Hugh Dickinsbde05d12012-05-29 15:06:38 -07003629 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003630 struct page_cgroup *pc;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003631 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003632
3633 if (mem_cgroup_disabled())
3634 return;
3635
3636 pc = lookup_page_cgroup(oldpage);
3637 /* fix accounting on old pages */
3638 lock_page_cgroup(pc);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07003639 if (PageCgroupUsed(pc)) {
3640 memcg = pc->mem_cgroup;
3641 mem_cgroup_charge_statistics(memcg, false, -1);
3642 ClearPageCgroupUsed(pc);
3643 }
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003644 unlock_page_cgroup(pc);
3645
Hugh Dickinsbde05d12012-05-29 15:06:38 -07003646 /*
3647 * When called from shmem_replace_page(), in some cases the
3648 * oldpage has already been charged, and in some cases not.
3649 */
3650 if (!memcg)
3651 return;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003652 /*
3653 * Even if newpage->mapping was NULL before starting replacement,
3654 * the newpage may be on LRU(or pagevec for LRU) already. We lock
3655 * LRU while we overwrite pc->mem_cgroup.
3656 */
Johannes Weinerce587e62012-04-24 20:22:33 +02003657 __mem_cgroup_commit_charge(memcg, newpage, 1, type, true);
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003658}
3659
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003660#ifdef CONFIG_DEBUG_VM
3661static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
3662{
3663 struct page_cgroup *pc;
3664
3665 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08003666 /*
3667 * Can be NULL while feeding pages into the page allocator for
3668 * the first time, i.e. during boot or memory hotplug;
3669 * or when mem_cgroup_disabled().
3670 */
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003671 if (likely(pc) && PageCgroupUsed(pc))
3672 return pc;
3673 return NULL;
3674}
3675
3676bool mem_cgroup_bad_page_check(struct page *page)
3677{
3678 if (mem_cgroup_disabled())
3679 return false;
3680
3681 return lookup_page_cgroup_used(page) != NULL;
3682}
3683
3684void mem_cgroup_print_bad_page(struct page *page)
3685{
3686 struct page_cgroup *pc;
3687
3688 pc = lookup_page_cgroup_used(page);
3689 if (pc) {
Hugh Dickins90b3fea2012-01-12 17:19:54 -08003690 printk(KERN_ALERT "pc:%p pc->flags:%lx pc->mem_cgroup:%p\n",
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003691 pc, pc->flags, pc->mem_cgroup);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003692 }
3693}
3694#endif
3695
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003696static DEFINE_MUTEX(set_limit_mutex);
3697
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08003698static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003699 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003700{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003701 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003702 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003703 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003704 int children = mem_cgroup_count_children(memcg);
3705 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003706 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003707
3708 /*
3709 * For keeping hierarchical_reclaim simple, how long we should retry
3710 * is depends on callers. We set our retry-count to be function
3711 * of # of children which we should visit in this loop.
3712 */
3713 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
3714
3715 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003716
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003717 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003718 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003719 if (signal_pending(current)) {
3720 ret = -EINTR;
3721 break;
3722 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003723 /*
3724 * Rather than hide all in some function, I do this in
3725 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07003726 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003727 */
3728 mutex_lock(&set_limit_mutex);
3729 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3730 if (memswlimit < val) {
3731 ret = -EINVAL;
3732 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003733 break;
3734 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003735
3736 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3737 if (memlimit < val)
3738 enlarge = 1;
3739
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003740 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003741 if (!ret) {
3742 if (memswlimit == val)
3743 memcg->memsw_is_minimum = true;
3744 else
3745 memcg->memsw_is_minimum = false;
3746 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003747 mutex_unlock(&set_limit_mutex);
3748
3749 if (!ret)
3750 break;
3751
Johannes Weiner56600482012-01-12 17:17:59 -08003752 mem_cgroup_reclaim(memcg, GFP_KERNEL,
3753 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003754 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
3755 /* Usage is reduced ? */
3756 if (curusage >= oldusage)
3757 retry_count--;
3758 else
3759 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003760 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003761 if (!ret && enlarge)
3762 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08003763
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003764 return ret;
3765}
3766
Li Zefan338c8432009-06-17 16:27:15 -07003767static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
3768 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003769{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003770 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003771 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003772 int children = mem_cgroup_count_children(memcg);
3773 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003774 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003775
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003776 /* see mem_cgroup_resize_res_limit */
3777 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
3778 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003779 while (retry_count) {
3780 if (signal_pending(current)) {
3781 ret = -EINTR;
3782 break;
3783 }
3784 /*
3785 * Rather than hide all in some function, I do this in
3786 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07003787 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003788 */
3789 mutex_lock(&set_limit_mutex);
3790 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3791 if (memlimit > val) {
3792 ret = -EINVAL;
3793 mutex_unlock(&set_limit_mutex);
3794 break;
3795 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003796 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3797 if (memswlimit < val)
3798 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003799 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003800 if (!ret) {
3801 if (memlimit == val)
3802 memcg->memsw_is_minimum = true;
3803 else
3804 memcg->memsw_is_minimum = false;
3805 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003806 mutex_unlock(&set_limit_mutex);
3807
3808 if (!ret)
3809 break;
3810
Johannes Weiner56600482012-01-12 17:17:59 -08003811 mem_cgroup_reclaim(memcg, GFP_KERNEL,
3812 MEM_CGROUP_RECLAIM_NOSWAP |
3813 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003814 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003815 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003816 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003817 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003818 else
3819 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003820 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003821 if (!ret && enlarge)
3822 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003823 return ret;
3824}
3825
Balbir Singh4e416952009-09-23 15:56:39 -07003826unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
Ying Han0ae5e892011-05-26 16:25:25 -07003827 gfp_t gfp_mask,
3828 unsigned long *total_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07003829{
3830 unsigned long nr_reclaimed = 0;
3831 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
3832 unsigned long reclaimed;
3833 int loop = 0;
3834 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003835 unsigned long long excess;
Ying Han0ae5e892011-05-26 16:25:25 -07003836 unsigned long nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003837
3838 if (order > 0)
3839 return 0;
3840
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07003841 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
Balbir Singh4e416952009-09-23 15:56:39 -07003842 /*
3843 * This loop can run a while, specially if mem_cgroup's continuously
3844 * keep exceeding their soft limit and putting the system under
3845 * pressure
3846 */
3847 do {
3848 if (next_mz)
3849 mz = next_mz;
3850 else
3851 mz = mem_cgroup_largest_soft_limit_node(mctz);
3852 if (!mz)
3853 break;
3854
Ying Han0ae5e892011-05-26 16:25:25 -07003855 nr_scanned = 0;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003856 reclaimed = mem_cgroup_soft_reclaim(mz->memcg, zone,
Johannes Weiner56600482012-01-12 17:17:59 -08003857 gfp_mask, &nr_scanned);
Balbir Singh4e416952009-09-23 15:56:39 -07003858 nr_reclaimed += reclaimed;
Ying Han0ae5e892011-05-26 16:25:25 -07003859 *total_scanned += nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003860 spin_lock(&mctz->lock);
3861
3862 /*
3863 * If we failed to reclaim anything from this memory cgroup
3864 * it is time to move on to the next cgroup
3865 */
3866 next_mz = NULL;
3867 if (!reclaimed) {
3868 do {
3869 /*
3870 * Loop until we find yet another one.
3871 *
3872 * By the time we get the soft_limit lock
3873 * again, someone might have aded the
3874 * group back on the RB tree. Iterate to
3875 * make sure we get a different mem.
3876 * mem_cgroup_largest_soft_limit_node returns
3877 * NULL if no other cgroup is present on
3878 * the tree
3879 */
3880 next_mz =
3881 __mem_cgroup_largest_soft_limit_node(mctz);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003882 if (next_mz == mz)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003883 css_put(&next_mz->memcg->css);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003884 else /* next_mz == NULL or other memcg */
Balbir Singh4e416952009-09-23 15:56:39 -07003885 break;
3886 } while (1);
3887 }
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003888 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
3889 excess = res_counter_soft_limit_excess(&mz->memcg->res);
Balbir Singh4e416952009-09-23 15:56:39 -07003890 /*
3891 * One school of thought says that we should not add
3892 * back the node to the tree if reclaim returns 0.
3893 * But our reclaim could return 0, simply because due
3894 * to priority we are exposing a smaller subset of
3895 * memory to reclaim from. Consider this as a longer
3896 * term TODO.
3897 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003898 /* If excess == 0, no tree ops */
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003899 __mem_cgroup_insert_exceeded(mz->memcg, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07003900 spin_unlock(&mctz->lock);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003901 css_put(&mz->memcg->css);
Balbir Singh4e416952009-09-23 15:56:39 -07003902 loop++;
3903 /*
3904 * Could not reclaim anything and there are no more
3905 * mem cgroups to try or we seem to be looping without
3906 * reclaiming anything.
3907 */
3908 if (!nr_reclaimed &&
3909 (next_mz == NULL ||
3910 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3911 break;
3912 } while (!nr_reclaimed);
3913 if (next_mz)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003914 css_put(&next_mz->memcg->css);
Balbir Singh4e416952009-09-23 15:56:39 -07003915 return nr_reclaimed;
3916}
3917
Michal Hocko2ef37d32012-10-26 13:37:30 +02003918/**
3919 * mem_cgroup_force_empty_list - clears LRU of a group
3920 * @memcg: group to clear
3921 * @node: NUMA node
3922 * @zid: zone id
3923 * @lru: lru to to clear
3924 *
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07003925 * Traverse a specified page_cgroup list and try to drop them all. This doesn't
Michal Hocko2ef37d32012-10-26 13:37:30 +02003926 * reclaim the pages page themselves - pages are moved to the parent (or root)
3927 * group.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003928 */
Michal Hocko2ef37d32012-10-26 13:37:30 +02003929static void mem_cgroup_force_empty_list(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003930 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003931{
Hugh Dickinsbea8c152012-11-16 14:14:54 -08003932 struct lruvec *lruvec;
Michal Hocko2ef37d32012-10-26 13:37:30 +02003933 unsigned long flags;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003934 struct list_head *list;
Johannes Weiner925b7672012-01-12 17:18:15 -08003935 struct page *busy;
3936 struct zone *zone;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003937
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003938 zone = &NODE_DATA(node)->node_zones[zid];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08003939 lruvec = mem_cgroup_zone_lruvec(zone, memcg);
3940 list = &lruvec->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003941
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003942 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02003943 do {
Johannes Weiner925b7672012-01-12 17:18:15 -08003944 struct page_cgroup *pc;
Johannes Weiner5564e882011-03-23 16:42:29 -07003945 struct page *page;
3946
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003947 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003948 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003949 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003950 break;
3951 }
Johannes Weiner925b7672012-01-12 17:18:15 -08003952 page = list_entry(list->prev, struct page, lru);
3953 if (busy == page) {
3954 list_move(&page->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08003955 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003956 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003957 continue;
3958 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003959 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003960
Johannes Weiner925b7672012-01-12 17:18:15 -08003961 pc = lookup_page_cgroup(page);
Johannes Weiner5564e882011-03-23 16:42:29 -07003962
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07003963 if (mem_cgroup_move_parent(page, pc, memcg)) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003964 /* found lock contention or "pc" is obsolete. */
Johannes Weiner925b7672012-01-12 17:18:15 -08003965 busy = page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003966 cond_resched();
3967 } else
3968 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02003969 } while (!list_empty(list));
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003970}
3971
3972/*
Michal Hockoc26251f2012-10-26 13:37:28 +02003973 * make mem_cgroup's charge to be 0 if there is no task by moving
3974 * all the charges and pages to the parent.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003975 * This enables deleting this mem_cgroup.
Michal Hockoc26251f2012-10-26 13:37:28 +02003976 *
3977 * Caller is responsible for holding css reference on the memcg.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003978 */
Michal Hockoab5196c2012-10-26 13:37:32 +02003979static void mem_cgroup_reparent_charges(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003980{
Michal Hockoc26251f2012-10-26 13:37:28 +02003981 int node, zid;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003982
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003983 do {
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003984 /* This is for making all *used* pages to be on LRU. */
3985 lru_add_drain_all();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003986 drain_all_stock_sync(memcg);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003987 mem_cgroup_start_move(memcg);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08003988 for_each_node_state(node, N_MEMORY) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02003989 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsf156ab932012-03-21 16:34:19 -07003990 enum lru_list lru;
3991 for_each_lru(lru) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02003992 mem_cgroup_force_empty_list(memcg,
Hugh Dickinsf156ab932012-03-21 16:34:19 -07003993 node, zid, lru);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003994 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003995 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003996 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003997 mem_cgroup_end_move(memcg);
3998 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003999 cond_resched();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004000
Michal Hocko2ef37d32012-10-26 13:37:30 +02004001 /*
4002 * This is a safety check because mem_cgroup_force_empty_list
4003 * could have raced with mem_cgroup_replace_page_cache callers
4004 * so the lru seemed empty but the page could have been added
4005 * right after the check. RES_USAGE should be safe as we always
4006 * charge before adding to the LRU.
4007 */
4008 } while (res_counter_read_u64(&memcg->res, RES_USAGE) > 0);
Michal Hockoc26251f2012-10-26 13:37:28 +02004009}
4010
4011/*
4012 * Reclaims as many pages from the given memcg as possible and moves
4013 * the rest to the parent.
4014 *
4015 * Caller is responsible for holding css reference for memcg.
4016 */
4017static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
4018{
4019 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
4020 struct cgroup *cgrp = memcg->css.cgroup;
4021
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004022 /* returns EBUSY if there is a task or if we come here twice. */
Michal Hockoc26251f2012-10-26 13:37:28 +02004023 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
4024 return -EBUSY;
4025
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004026 /* we call try-to-free pages for make this cgroup empty */
4027 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004028 /* try to free all pages in this cgroup */
Glauber Costa569530f2012-04-12 12:49:13 -07004029 while (nr_retries && res_counter_read_u64(&memcg->res, RES_USAGE) > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004030 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004031
Michal Hockoc26251f2012-10-26 13:37:28 +02004032 if (signal_pending(current))
4033 return -EINTR;
4034
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004035 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
Johannes Weiner185efc02011-09-14 16:21:58 -07004036 false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004037 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004038 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004039 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02004040 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004041 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004042
4043 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004044 lru_add_drain();
Michal Hockoab5196c2012-10-26 13:37:32 +02004045 mem_cgroup_reparent_charges(memcg);
4046
4047 return 0;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004048}
4049
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07004050static int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004051{
Michal Hockoc26251f2012-10-26 13:37:28 +02004052 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
4053 int ret;
4054
Michal Hockod8423012012-10-26 13:37:29 +02004055 if (mem_cgroup_is_root(memcg))
4056 return -EINVAL;
Michal Hockoc26251f2012-10-26 13:37:28 +02004057 css_get(&memcg->css);
4058 ret = mem_cgroup_force_empty(memcg);
4059 css_put(&memcg->css);
4060
4061 return ret;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004062}
4063
4064
Balbir Singh18f59ea2009-01-07 18:08:07 -08004065static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
4066{
4067 return mem_cgroup_from_cont(cont)->use_hierarchy;
4068}
4069
4070static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
4071 u64 val)
4072{
4073 int retval = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004074 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004075 struct cgroup *parent = cont->parent;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004076 struct mem_cgroup *parent_memcg = NULL;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004077
4078 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004079 parent_memcg = mem_cgroup_from_cont(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004080
4081 cgroup_lock();
Glauber Costa567fb432012-07-31 16:43:07 -07004082
4083 if (memcg->use_hierarchy == val)
4084 goto out;
4085
Balbir Singh18f59ea2009-01-07 18:08:07 -08004086 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004087 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08004088 * in the child subtrees. If it is unset, then the change can
4089 * occur, provided the current cgroup has no children.
4090 *
4091 * For the root cgroup, parent_mem is NULL, we allow value to be
4092 * set if there are no children.
4093 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004094 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08004095 (val == 1 || val == 0)) {
4096 if (list_empty(&cont->children))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004097 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004098 else
4099 retval = -EBUSY;
4100 } else
4101 retval = -EINVAL;
Glauber Costa567fb432012-07-31 16:43:07 -07004102
4103out:
Balbir Singh18f59ea2009-01-07 18:08:07 -08004104 cgroup_unlock();
4105
4106 return retval;
4107}
4108
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004109
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004110static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004111 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004112{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004113 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004114 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004115
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004116 /* Per-cpu values can be negative, use a signed accumulator */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004117 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004118 val += mem_cgroup_read_stat(iter, idx);
4119
4120 if (val < 0) /* race ? */
4121 val = 0;
4122 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004123}
4124
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004125static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004126{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004127 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004128
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004129 if (!mem_cgroup_is_root(memcg)) {
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004130 if (!swap)
Glauber Costa65c64ce2011-12-22 01:02:27 +00004131 return res_counter_read_u64(&memcg->res, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004132 else
Glauber Costa65c64ce2011-12-22 01:02:27 +00004133 return res_counter_read_u64(&memcg->memsw, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004134 }
4135
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004136 val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
4137 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004138
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004139 if (swap)
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07004140 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAP);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004141
4142 return val << PAGE_SHIFT;
4143}
4144
Tejun Heoaf36f902012-04-01 12:09:55 -07004145static ssize_t mem_cgroup_read(struct cgroup *cont, struct cftype *cft,
4146 struct file *file, char __user *buf,
4147 size_t nbytes, loff_t *ppos)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004148{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004149 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Tejun Heoaf36f902012-04-01 12:09:55 -07004150 char str[64];
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004151 u64 val;
Glauber Costa86ae53e2012-12-18 14:21:45 -08004152 int name, len;
4153 enum res_type type;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004154
4155 type = MEMFILE_TYPE(cft->private);
4156 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07004157
4158 if (!do_swap_account && type == _MEMSWAP)
4159 return -EOPNOTSUPP;
4160
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004161 switch (type) {
4162 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004163 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004164 val = mem_cgroup_usage(memcg, false);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004165 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004166 val = res_counter_read_u64(&memcg->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004167 break;
4168 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004169 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004170 val = mem_cgroup_usage(memcg, true);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004171 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004172 val = res_counter_read_u64(&memcg->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004173 break;
Glauber Costa510fc4e2012-12-18 14:21:47 -08004174 case _KMEM:
4175 val = res_counter_read_u64(&memcg->kmem, name);
4176 break;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004177 default:
4178 BUG();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004179 }
Tejun Heoaf36f902012-04-01 12:09:55 -07004180
4181 len = scnprintf(str, sizeof(str), "%llu\n", (unsigned long long)val);
4182 return simple_read_from_buffer(buf, nbytes, ppos, str, len);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004183}
Glauber Costa510fc4e2012-12-18 14:21:47 -08004184
4185static int memcg_update_kmem_limit(struct cgroup *cont, u64 val)
4186{
4187 int ret = -EINVAL;
4188#ifdef CONFIG_MEMCG_KMEM
4189 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
4190 /*
4191 * For simplicity, we won't allow this to be disabled. It also can't
4192 * be changed if the cgroup has children already, or if tasks had
4193 * already joined.
4194 *
4195 * If tasks join before we set the limit, a person looking at
4196 * kmem.usage_in_bytes will have no way to determine when it took
4197 * place, which makes the value quite meaningless.
4198 *
4199 * After it first became limited, changes in the value of the limit are
4200 * of course permitted.
4201 *
4202 * Taking the cgroup_lock is really offensive, but it is so far the only
4203 * way to guarantee that no children will appear. There are plenty of
4204 * other offenders, and they should all go away. Fine grained locking
4205 * is probably the way to go here. When we are fully hierarchical, we
4206 * can also get rid of the use_hierarchy check.
4207 */
4208 cgroup_lock();
4209 mutex_lock(&set_limit_mutex);
4210 if (!memcg->kmem_account_flags && val != RESOURCE_MAX) {
4211 if (cgroup_task_count(cont) || (memcg->use_hierarchy &&
4212 !list_empty(&cont->children))) {
4213 ret = -EBUSY;
4214 goto out;
4215 }
4216 ret = res_counter_set_limit(&memcg->kmem, val);
4217 VM_BUG_ON(ret);
4218
4219 memcg_kmem_set_active(memcg);
4220 } else
4221 ret = res_counter_set_limit(&memcg->kmem, val);
4222out:
4223 mutex_unlock(&set_limit_mutex);
4224 cgroup_unlock();
4225#endif
4226 return ret;
4227}
4228
4229static void memcg_propagate_kmem(struct mem_cgroup *memcg)
4230{
4231 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
4232 if (!parent)
4233 return;
4234 memcg->kmem_account_flags = parent->kmem_account_flags;
4235}
4236
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004237/*
4238 * The user of this function is...
4239 * RES_LIMIT.
4240 */
Paul Menage856c13a2008-07-25 01:47:04 -07004241static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
4242 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004243{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004244 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Glauber Costa86ae53e2012-12-18 14:21:45 -08004245 enum res_type type;
4246 int name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004247 unsigned long long val;
4248 int ret;
4249
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004250 type = MEMFILE_TYPE(cft->private);
4251 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07004252
4253 if (!do_swap_account && type == _MEMSWAP)
4254 return -EOPNOTSUPP;
4255
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004256 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004257 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07004258 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
4259 ret = -EINVAL;
4260 break;
4261 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004262 /* This function does all necessary parse...reuse it */
4263 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004264 if (ret)
4265 break;
4266 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004267 ret = mem_cgroup_resize_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004268 else if (type == _MEMSWAP)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004269 ret = mem_cgroup_resize_memsw_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004270 else if (type == _KMEM)
4271 ret = memcg_update_kmem_limit(cont, val);
4272 else
4273 return -EINVAL;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004274 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07004275 case RES_SOFT_LIMIT:
4276 ret = res_counter_memparse_write_strategy(buffer, &val);
4277 if (ret)
4278 break;
4279 /*
4280 * For memsw, soft limits are hard to implement in terms
4281 * of semantics, for now, we support soft limits for
4282 * control without swap
4283 */
4284 if (type == _MEM)
4285 ret = res_counter_set_soft_limit(&memcg->res, val);
4286 else
4287 ret = -EINVAL;
4288 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004289 default:
4290 ret = -EINVAL; /* should be BUG() ? */
4291 break;
4292 }
4293 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004294}
4295
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004296static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
4297 unsigned long long *mem_limit, unsigned long long *memsw_limit)
4298{
4299 struct cgroup *cgroup;
4300 unsigned long long min_limit, min_memsw_limit, tmp;
4301
4302 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4303 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4304 cgroup = memcg->css.cgroup;
4305 if (!memcg->use_hierarchy)
4306 goto out;
4307
4308 while (cgroup->parent) {
4309 cgroup = cgroup->parent;
4310 memcg = mem_cgroup_from_cont(cgroup);
4311 if (!memcg->use_hierarchy)
4312 break;
4313 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
4314 min_limit = min(min_limit, tmp);
4315 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4316 min_memsw_limit = min(min_memsw_limit, tmp);
4317 }
4318out:
4319 *mem_limit = min_limit;
4320 *memsw_limit = min_memsw_limit;
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004321}
4322
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004323static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004324{
Tejun Heoaf36f902012-04-01 12:09:55 -07004325 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Glauber Costa86ae53e2012-12-18 14:21:45 -08004326 int name;
4327 enum res_type type;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004328
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004329 type = MEMFILE_TYPE(event);
4330 name = MEMFILE_ATTR(event);
Tejun Heoaf36f902012-04-01 12:09:55 -07004331
4332 if (!do_swap_account && type == _MEMSWAP)
4333 return -EOPNOTSUPP;
4334
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004335 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004336 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004337 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004338 res_counter_reset_max(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004339 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004340 res_counter_reset_max(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004341 else if (type == _KMEM)
4342 res_counter_reset_max(&memcg->kmem);
4343 else
4344 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004345 break;
4346 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004347 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004348 res_counter_reset_failcnt(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004349 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004350 res_counter_reset_failcnt(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004351 else if (type == _KMEM)
4352 res_counter_reset_failcnt(&memcg->kmem);
4353 else
4354 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004355 break;
4356 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07004357
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07004358 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004359}
4360
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004361static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
4362 struct cftype *cft)
4363{
4364 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
4365}
4366
Daisuke Nishimura02491442010-03-10 15:22:17 -08004367#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004368static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
4369 struct cftype *cft, u64 val)
4370{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004371 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004372
4373 if (val >= (1 << NR_MOVE_TYPE))
4374 return -EINVAL;
4375 /*
4376 * We check this value several times in both in can_attach() and
4377 * attach(), so we need cgroup lock to prevent this value from being
4378 * inconsistent.
4379 */
4380 cgroup_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004381 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004382 cgroup_unlock();
4383
4384 return 0;
4385}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004386#else
4387static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
4388 struct cftype *cft, u64 val)
4389{
4390 return -ENOSYS;
4391}
4392#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004393
Ying Han406eb0c2011-05-26 16:25:37 -07004394#ifdef CONFIG_NUMA
Wanpeng Liab215882012-07-31 16:43:09 -07004395static int memcg_numa_stat_show(struct cgroup *cont, struct cftype *cft,
Johannes Weinerfada52c2012-05-29 15:07:06 -07004396 struct seq_file *m)
Ying Han406eb0c2011-05-26 16:25:37 -07004397{
4398 int nid;
4399 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
4400 unsigned long node_nr;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004401 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Ying Han406eb0c2011-05-26 16:25:37 -07004402
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004403 total_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07004404 seq_printf(m, "total=%lu", total_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004405 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004406 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07004407 seq_printf(m, " N%d=%lu", nid, node_nr);
4408 }
4409 seq_putc(m, '\n');
4410
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004411 file_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07004412 seq_printf(m, "file=%lu", file_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004413 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004414 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004415 LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07004416 seq_printf(m, " N%d=%lu", nid, node_nr);
4417 }
4418 seq_putc(m, '\n');
4419
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004420 anon_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07004421 seq_printf(m, "anon=%lu", anon_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004422 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004423 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004424 LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07004425 seq_printf(m, " N%d=%lu", nid, node_nr);
4426 }
4427 seq_putc(m, '\n');
4428
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004429 unevictable_nr = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07004430 seq_printf(m, "unevictable=%lu", unevictable_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004431 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004432 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004433 BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07004434 seq_printf(m, " N%d=%lu", nid, node_nr);
4435 }
4436 seq_putc(m, '\n');
4437 return 0;
4438}
4439#endif /* CONFIG_NUMA */
4440
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004441static const char * const mem_cgroup_lru_names[] = {
4442 "inactive_anon",
4443 "active_anon",
4444 "inactive_file",
4445 "active_file",
4446 "unevictable",
4447};
4448
4449static inline void mem_cgroup_lru_names_not_uptodate(void)
4450{
4451 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS);
4452}
4453
Wanpeng Liab215882012-07-31 16:43:09 -07004454static int memcg_stat_show(struct cgroup *cont, struct cftype *cft,
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07004455 struct seq_file *m)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004456{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004457 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004458 struct mem_cgroup *mi;
4459 unsigned int i;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004460
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004461 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07004462 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004463 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004464 seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i],
4465 mem_cgroup_read_stat(memcg, i) * PAGE_SIZE);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004466 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004467
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004468 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
4469 seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i],
4470 mem_cgroup_read_events(memcg, i));
4471
4472 for (i = 0; i < NR_LRU_LISTS; i++)
4473 seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i],
4474 mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE);
4475
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004476 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004477 {
4478 unsigned long long limit, memsw_limit;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004479 memcg_get_hierarchical_limit(memcg, &limit, &memsw_limit);
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07004480 seq_printf(m, "hierarchical_memory_limit %llu\n", limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004481 if (do_swap_account)
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07004482 seq_printf(m, "hierarchical_memsw_limit %llu\n",
4483 memsw_limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004484 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004485
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004486 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
4487 long long val = 0;
4488
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07004489 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004490 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004491 for_each_mem_cgroup_tree(mi, memcg)
4492 val += mem_cgroup_read_stat(mi, i) * PAGE_SIZE;
4493 seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i], val);
4494 }
4495
4496 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
4497 unsigned long long val = 0;
4498
4499 for_each_mem_cgroup_tree(mi, memcg)
4500 val += mem_cgroup_read_events(mi, i);
4501 seq_printf(m, "total_%s %llu\n",
4502 mem_cgroup_events_names[i], val);
4503 }
4504
4505 for (i = 0; i < NR_LRU_LISTS; i++) {
4506 unsigned long long val = 0;
4507
4508 for_each_mem_cgroup_tree(mi, memcg)
4509 val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE;
4510 seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004511 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004512
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004513#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004514 {
4515 int nid, zid;
4516 struct mem_cgroup_per_zone *mz;
Hugh Dickins89abfab2012-05-29 15:06:53 -07004517 struct zone_reclaim_stat *rstat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004518 unsigned long recent_rotated[2] = {0, 0};
4519 unsigned long recent_scanned[2] = {0, 0};
4520
4521 for_each_online_node(nid)
4522 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004523 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
Hugh Dickins89abfab2012-05-29 15:06:53 -07004524 rstat = &mz->lruvec.reclaim_stat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004525
Hugh Dickins89abfab2012-05-29 15:06:53 -07004526 recent_rotated[0] += rstat->recent_rotated[0];
4527 recent_rotated[1] += rstat->recent_rotated[1];
4528 recent_scanned[0] += rstat->recent_scanned[0];
4529 recent_scanned[1] += rstat->recent_scanned[1];
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004530 }
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07004531 seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]);
4532 seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]);
4533 seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]);
4534 seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004535 }
4536#endif
4537
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004538 return 0;
4539}
4540
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004541static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
4542{
4543 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4544
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07004545 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004546}
4547
4548static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
4549 u64 val)
4550{
4551 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4552 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08004553
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004554 if (val > 100)
4555 return -EINVAL;
4556
4557 if (cgrp->parent == NULL)
4558 return -EINVAL;
4559
4560 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08004561
4562 cgroup_lock();
4563
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004564 /* If under hierarchy, only empty-root can set this value */
4565 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08004566 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
4567 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004568 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08004569 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004570
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004571 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004572
Li Zefan068b38c2009-01-15 13:51:26 -08004573 cgroup_unlock();
4574
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004575 return 0;
4576}
4577
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004578static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
4579{
4580 struct mem_cgroup_threshold_ary *t;
4581 u64 usage;
4582 int i;
4583
4584 rcu_read_lock();
4585 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004586 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004587 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004588 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004589
4590 if (!t)
4591 goto unlock;
4592
4593 usage = mem_cgroup_usage(memcg, swap);
4594
4595 /*
Sha Zhengju748dad32012-05-29 15:06:57 -07004596 * current_threshold points to threshold just below or equal to usage.
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004597 * If it's not true, a threshold was crossed after last
4598 * call of __mem_cgroup_threshold().
4599 */
Phil Carmody5407a562010-05-26 14:42:42 -07004600 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004601
4602 /*
4603 * Iterate backward over array of thresholds starting from
4604 * current_threshold and check if a threshold is crossed.
4605 * If none of thresholds below usage is crossed, we read
4606 * only one element of the array here.
4607 */
4608 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
4609 eventfd_signal(t->entries[i].eventfd, 1);
4610
4611 /* i = current_threshold + 1 */
4612 i++;
4613
4614 /*
4615 * Iterate forward over array of thresholds starting from
4616 * current_threshold+1 and check if a threshold is crossed.
4617 * If none of thresholds above usage is crossed, we read
4618 * only one element of the array here.
4619 */
4620 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
4621 eventfd_signal(t->entries[i].eventfd, 1);
4622
4623 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07004624 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004625unlock:
4626 rcu_read_unlock();
4627}
4628
4629static void mem_cgroup_threshold(struct mem_cgroup *memcg)
4630{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07004631 while (memcg) {
4632 __mem_cgroup_threshold(memcg, false);
4633 if (do_swap_account)
4634 __mem_cgroup_threshold(memcg, true);
4635
4636 memcg = parent_mem_cgroup(memcg);
4637 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004638}
4639
4640static int compare_thresholds(const void *a, const void *b)
4641{
4642 const struct mem_cgroup_threshold *_a = a;
4643 const struct mem_cgroup_threshold *_b = b;
4644
4645 return _a->threshold - _b->threshold;
4646}
4647
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004648static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004649{
4650 struct mem_cgroup_eventfd_list *ev;
4651
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004652 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004653 eventfd_signal(ev->eventfd, 1);
4654 return 0;
4655}
4656
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004657static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004658{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004659 struct mem_cgroup *iter;
4660
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004661 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004662 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004663}
4664
4665static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
4666 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004667{
4668 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004669 struct mem_cgroup_thresholds *thresholds;
4670 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08004671 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004672 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004673 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004674
4675 ret = res_counter_memparse_write_strategy(args, &threshold);
4676 if (ret)
4677 return ret;
4678
4679 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004680
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004681 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004682 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004683 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004684 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004685 else
4686 BUG();
4687
4688 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4689
4690 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004691 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004692 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4693
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004694 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004695
4696 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004697 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004698 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004699 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004700 ret = -ENOMEM;
4701 goto unlock;
4702 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004703 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004704
4705 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004706 if (thresholds->primary) {
4707 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004708 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004709 }
4710
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004711 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004712 new->entries[size - 1].eventfd = eventfd;
4713 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004714
4715 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004716 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004717 compare_thresholds, NULL);
4718
4719 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004720 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004721 for (i = 0; i < size; i++) {
Sha Zhengju748dad32012-05-29 15:06:57 -07004722 if (new->entries[i].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004723 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004724 * new->current_threshold will not be used until
4725 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004726 * it here.
4727 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004728 ++new->current_threshold;
Sha Zhengju748dad32012-05-29 15:06:57 -07004729 } else
4730 break;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004731 }
4732
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004733 /* Free old spare buffer and save old primary buffer as spare */
4734 kfree(thresholds->spare);
4735 thresholds->spare = thresholds->primary;
4736
4737 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004738
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004739 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004740 synchronize_rcu();
4741
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004742unlock:
4743 mutex_unlock(&memcg->thresholds_lock);
4744
4745 return ret;
4746}
4747
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004748static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004749 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004750{
4751 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004752 struct mem_cgroup_thresholds *thresholds;
4753 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08004754 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004755 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004756 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004757
4758 mutex_lock(&memcg->thresholds_lock);
4759 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004760 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004761 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004762 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004763 else
4764 BUG();
4765
Anton Vorontsov371528c2012-02-24 05:14:46 +04004766 if (!thresholds->primary)
4767 goto unlock;
4768
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004769 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4770
4771 /* Check if a threshold crossed before removing */
4772 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4773
4774 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004775 size = 0;
4776 for (i = 0; i < thresholds->primary->size; i++) {
4777 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004778 size++;
4779 }
4780
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004781 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004782
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004783 /* Set thresholds array to NULL if we don't have thresholds */
4784 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004785 kfree(new);
4786 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004787 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004788 }
4789
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004790 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004791
4792 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004793 new->current_threshold = -1;
4794 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
4795 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004796 continue;
4797
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004798 new->entries[j] = thresholds->primary->entries[i];
Sha Zhengju748dad32012-05-29 15:06:57 -07004799 if (new->entries[j].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004800 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004801 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004802 * until rcu_assign_pointer(), so it's safe to increment
4803 * it here.
4804 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004805 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004806 }
4807 j++;
4808 }
4809
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004810swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004811 /* Swap primary and spare array */
4812 thresholds->spare = thresholds->primary;
Sha Zhengju8c757762012-05-10 13:01:45 -07004813 /* If all events are unregistered, free the spare array */
4814 if (!new) {
4815 kfree(thresholds->spare);
4816 thresholds->spare = NULL;
4817 }
4818
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004819 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004820
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004821 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004822 synchronize_rcu();
Anton Vorontsov371528c2012-02-24 05:14:46 +04004823unlock:
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004824 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004825}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004826
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004827static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
4828 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
4829{
4830 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4831 struct mem_cgroup_eventfd_list *event;
Glauber Costa86ae53e2012-12-18 14:21:45 -08004832 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004833
4834 BUG_ON(type != _OOM_TYPE);
4835 event = kmalloc(sizeof(*event), GFP_KERNEL);
4836 if (!event)
4837 return -ENOMEM;
4838
Michal Hocko1af8efe2011-07-26 16:08:24 -07004839 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004840
4841 event->eventfd = eventfd;
4842 list_add(&event->list, &memcg->oom_notify);
4843
4844 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07004845 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004846 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07004847 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004848
4849 return 0;
4850}
4851
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004852static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004853 struct cftype *cft, struct eventfd_ctx *eventfd)
4854{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004855 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004856 struct mem_cgroup_eventfd_list *ev, *tmp;
Glauber Costa86ae53e2012-12-18 14:21:45 -08004857 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004858
4859 BUG_ON(type != _OOM_TYPE);
4860
Michal Hocko1af8efe2011-07-26 16:08:24 -07004861 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004862
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004863 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004864 if (ev->eventfd == eventfd) {
4865 list_del(&ev->list);
4866 kfree(ev);
4867 }
4868 }
4869
Michal Hocko1af8efe2011-07-26 16:08:24 -07004870 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004871}
4872
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004873static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
4874 struct cftype *cft, struct cgroup_map_cb *cb)
4875{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004876 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004877
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004878 cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004879
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004880 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004881 cb->fill(cb, "under_oom", 1);
4882 else
4883 cb->fill(cb, "under_oom", 0);
4884 return 0;
4885}
4886
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004887static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
4888 struct cftype *cft, u64 val)
4889{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004890 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004891 struct mem_cgroup *parent;
4892
4893 /* cannot set to root cgroup and only 0 and 1 are allowed */
4894 if (!cgrp->parent || !((val == 0) || (val == 1)))
4895 return -EINVAL;
4896
4897 parent = mem_cgroup_from_cont(cgrp->parent);
4898
4899 cgroup_lock();
4900 /* oom-kill-disable is a flag for subhierarchy. */
4901 if ((parent->use_hierarchy) ||
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004902 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004903 cgroup_unlock();
4904 return -EINVAL;
4905 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004906 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07004907 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004908 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004909 cgroup_unlock();
4910 return 0;
4911}
4912
Andrew Mortonc255a452012-07-31 16:43:02 -07004913#ifdef CONFIG_MEMCG_KMEM
Glauber Costacbe128e32012-04-09 19:36:34 -03004914static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00004915{
Glauber Costa510fc4e2012-12-18 14:21:47 -08004916 memcg_propagate_kmem(memcg);
Glauber Costa1d62e432012-04-09 19:36:33 -03004917 return mem_cgroup_sockets_init(memcg, ss);
Glauber Costae5671df2011-12-11 21:47:01 +00004918};
4919
Glauber Costa1d62e432012-04-09 19:36:33 -03004920static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00004921{
Glauber Costa1d62e432012-04-09 19:36:33 -03004922 mem_cgroup_sockets_destroy(memcg);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00004923}
Glauber Costae5671df2011-12-11 21:47:01 +00004924#else
Glauber Costacbe128e32012-04-09 19:36:34 -03004925static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00004926{
4927 return 0;
4928}
Glauber Costad1a4c0b2011-12-11 21:47:04 +00004929
Glauber Costa1d62e432012-04-09 19:36:33 -03004930static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00004931{
4932}
Glauber Costae5671df2011-12-11 21:47:01 +00004933#endif
4934
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004935static struct cftype mem_cgroup_files[] = {
4936 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004937 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004938 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Tejun Heoaf36f902012-04-01 12:09:55 -07004939 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004940 .register_event = mem_cgroup_usage_register_event,
4941 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004942 },
4943 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004944 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004945 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004946 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07004947 .read = mem_cgroup_read,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004948 },
4949 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004950 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004951 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07004952 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07004953 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004954 },
4955 {
Balbir Singh296c81d2009-09-23 15:56:36 -07004956 .name = "soft_limit_in_bytes",
4957 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
4958 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07004959 .read = mem_cgroup_read,
Balbir Singh296c81d2009-09-23 15:56:36 -07004960 },
4961 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004962 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004963 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004964 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07004965 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004966 },
Balbir Singh8697d332008-02-07 00:13:59 -08004967 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004968 .name = "stat",
Wanpeng Liab215882012-07-31 16:43:09 -07004969 .read_seq_string = memcg_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004970 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004971 {
4972 .name = "force_empty",
4973 .trigger = mem_cgroup_force_empty_write,
4974 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08004975 {
4976 .name = "use_hierarchy",
4977 .write_u64 = mem_cgroup_hierarchy_write,
4978 .read_u64 = mem_cgroup_hierarchy_read,
4979 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004980 {
4981 .name = "swappiness",
4982 .read_u64 = mem_cgroup_swappiness_read,
4983 .write_u64 = mem_cgroup_swappiness_write,
4984 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004985 {
4986 .name = "move_charge_at_immigrate",
4987 .read_u64 = mem_cgroup_move_charge_read,
4988 .write_u64 = mem_cgroup_move_charge_write,
4989 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004990 {
4991 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004992 .read_map = mem_cgroup_oom_control_read,
4993 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004994 .register_event = mem_cgroup_oom_register_event,
4995 .unregister_event = mem_cgroup_oom_unregister_event,
4996 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
4997 },
Ying Han406eb0c2011-05-26 16:25:37 -07004998#ifdef CONFIG_NUMA
4999 {
5000 .name = "numa_stat",
Wanpeng Liab215882012-07-31 16:43:09 -07005001 .read_seq_string = memcg_numa_stat_show,
Ying Han406eb0c2011-05-26 16:25:37 -07005002 },
5003#endif
Andrew Mortonc255a452012-07-31 16:43:02 -07005004#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005005 {
5006 .name = "memsw.usage_in_bytes",
5007 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
Tejun Heoaf36f902012-04-01 12:09:55 -07005008 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005009 .register_event = mem_cgroup_usage_register_event,
5010 .unregister_event = mem_cgroup_usage_unregister_event,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005011 },
5012 {
5013 .name = "memsw.max_usage_in_bytes",
5014 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
5015 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005016 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005017 },
5018 {
5019 .name = "memsw.limit_in_bytes",
5020 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
5021 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005022 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005023 },
5024 {
5025 .name = "memsw.failcnt",
5026 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
5027 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005028 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005029 },
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005030#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08005031#ifdef CONFIG_MEMCG_KMEM
5032 {
5033 .name = "kmem.limit_in_bytes",
5034 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
5035 .write_string = mem_cgroup_write,
5036 .read = mem_cgroup_read,
5037 },
5038 {
5039 .name = "kmem.usage_in_bytes",
5040 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
5041 .read = mem_cgroup_read,
5042 },
5043 {
5044 .name = "kmem.failcnt",
5045 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
5046 .trigger = mem_cgroup_reset,
5047 .read = mem_cgroup_read,
5048 },
5049 {
5050 .name = "kmem.max_usage_in_bytes",
5051 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
5052 .trigger = mem_cgroup_reset,
5053 .read = mem_cgroup_read,
5054 },
5055#endif
Tejun Heo6bc10342012-04-01 12:09:55 -07005056 { }, /* terminate */
Tejun Heoaf36f902012-04-01 12:09:55 -07005057};
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005058
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005059static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005060{
5061 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005062 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005063 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005064 /*
5065 * This routine is called against possible nodes.
5066 * But it's BUG to call kmalloc() against offline node.
5067 *
5068 * TODO: this routine can waste much memory for nodes which will
5069 * never be onlined. It's better to use memory hotplug callback
5070 * function.
5071 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005072 if (!node_state(node, N_NORMAL_MEMORY))
5073 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08005074 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005075 if (!pn)
5076 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005077
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005078 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
5079 mz = &pn->zoneinfo[zone];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08005080 lruvec_init(&mz->lruvec);
Balbir Singhf64c3f52009-09-23 15:56:37 -07005081 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07005082 mz->on_tree = false;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005083 mz->memcg = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005084 }
Igor Mammedov0a619e52011-11-02 13:38:21 -07005085 memcg->info.nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005086 return 0;
5087}
5088
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005089static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005090{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005091 kfree(memcg->info.nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005092}
5093
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005094static struct mem_cgroup *mem_cgroup_alloc(void)
5095{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005096 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08005097 int size = sizeof(struct mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005098
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08005099 /* Can be very big if MAX_NUMNODES is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08005100 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005101 memcg = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005102 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005103 memcg = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005104
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005105 if (!memcg)
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07005106 return NULL;
5107
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005108 memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
5109 if (!memcg->stat)
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005110 goto out_free;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005111 spin_lock_init(&memcg->pcp_counter_lock);
5112 return memcg;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005113
5114out_free:
5115 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005116 kfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005117 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005118 vfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005119 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005120}
5121
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005122/*
Glauber Costa3afe36b2012-05-29 15:07:10 -07005123 * Helpers for freeing a kmalloc()ed/vzalloc()ed mem_cgroup by RCU,
Hugh Dickins59927fb2012-03-15 15:17:07 -07005124 * but in process context. The work_freeing structure is overlaid
5125 * on the rcu_freeing structure, which itself is overlaid on memsw.
5126 */
Glauber Costa3afe36b2012-05-29 15:07:10 -07005127static void free_work(struct work_struct *work)
Hugh Dickins59927fb2012-03-15 15:17:07 -07005128{
5129 struct mem_cgroup *memcg;
Glauber Costa3afe36b2012-05-29 15:07:10 -07005130 int size = sizeof(struct mem_cgroup);
Hugh Dickins59927fb2012-03-15 15:17:07 -07005131
5132 memcg = container_of(work, struct mem_cgroup, work_freeing);
Glauber Costa3f134612012-05-29 15:07:11 -07005133 /*
5134 * We need to make sure that (at least for now), the jump label
5135 * destruction code runs outside of the cgroup lock. This is because
5136 * get_online_cpus(), which is called from the static_branch update,
5137 * can't be called inside the cgroup_lock. cpusets are the ones
5138 * enforcing this dependency, so if they ever change, we might as well.
5139 *
5140 * schedule_work() will guarantee this happens. Be careful if you need
5141 * to move this code around, and make sure it is outside
5142 * the cgroup_lock.
5143 */
5144 disarm_sock_keys(memcg);
Glauber Costa3afe36b2012-05-29 15:07:10 -07005145 if (size < PAGE_SIZE)
5146 kfree(memcg);
5147 else
5148 vfree(memcg);
Hugh Dickins59927fb2012-03-15 15:17:07 -07005149}
Glauber Costa3afe36b2012-05-29 15:07:10 -07005150
5151static void free_rcu(struct rcu_head *rcu_head)
Hugh Dickins59927fb2012-03-15 15:17:07 -07005152{
5153 struct mem_cgroup *memcg;
5154
5155 memcg = container_of(rcu_head, struct mem_cgroup, rcu_freeing);
Glauber Costa3afe36b2012-05-29 15:07:10 -07005156 INIT_WORK(&memcg->work_freeing, free_work);
Hugh Dickins59927fb2012-03-15 15:17:07 -07005157 schedule_work(&memcg->work_freeing);
5158}
5159
5160/*
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005161 * At destroying mem_cgroup, references from swap_cgroup can remain.
5162 * (scanning all at force_empty is too costly...)
5163 *
5164 * Instead of clearing all references at force_empty, we remember
5165 * the number of reference from swap_cgroup and free mem_cgroup when
5166 * it goes down to 0.
5167 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005168 * Removal of cgroup itself succeeds regardless of refs from swap.
5169 */
5170
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005171static void __mem_cgroup_free(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005172{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08005173 int node;
5174
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005175 mem_cgroup_remove_from_trees(memcg);
5176 free_css_id(&mem_cgroup_subsys, &memcg->css);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005177
Bob Liu3ed28fa2012-01-12 17:19:04 -08005178 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005179 free_mem_cgroup_per_zone_info(memcg, node);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08005180
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005181 free_percpu(memcg->stat);
Glauber Costa3afe36b2012-05-29 15:07:10 -07005182 call_rcu(&memcg->rcu_freeing, free_rcu);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005183}
5184
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005185static void mem_cgroup_get(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005186{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005187 atomic_inc(&memcg->refcnt);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005188}
5189
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005190static void __mem_cgroup_put(struct mem_cgroup *memcg, int count)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005191{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005192 if (atomic_sub_and_test(count, &memcg->refcnt)) {
5193 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
5194 __mem_cgroup_free(memcg);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005195 if (parent)
5196 mem_cgroup_put(parent);
5197 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005198}
5199
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005200static void mem_cgroup_put(struct mem_cgroup *memcg)
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005201{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005202 __mem_cgroup_put(memcg, 1);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005203}
5204
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005205/*
5206 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
5207 */
Glauber Costae1aab162011-12-11 21:47:03 +00005208struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005209{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005210 if (!memcg->res.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005211 return NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005212 return mem_cgroup_from_res_counter(memcg->res.parent, res);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005213}
Glauber Costae1aab162011-12-11 21:47:03 +00005214EXPORT_SYMBOL(parent_mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005215
Andrew Mortonc255a452012-07-31 16:43:02 -07005216#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005217static void __init enable_swap_cgroup(void)
5218{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08005219 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005220 do_swap_account = 1;
5221}
5222#else
5223static void __init enable_swap_cgroup(void)
5224{
5225}
5226#endif
5227
Balbir Singhf64c3f52009-09-23 15:56:37 -07005228static int mem_cgroup_soft_limit_tree_init(void)
5229{
5230 struct mem_cgroup_tree_per_node *rtpn;
5231 struct mem_cgroup_tree_per_zone *rtpz;
5232 int tmp, node, zone;
5233
Bob Liu3ed28fa2012-01-12 17:19:04 -08005234 for_each_node(node) {
Balbir Singhf64c3f52009-09-23 15:56:37 -07005235 tmp = node;
5236 if (!node_state(node, N_NORMAL_MEMORY))
5237 tmp = -1;
5238 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
5239 if (!rtpn)
Michal Hockoc3cecc62012-01-12 17:18:50 -08005240 goto err_cleanup;
Balbir Singhf64c3f52009-09-23 15:56:37 -07005241
5242 soft_limit_tree.rb_tree_per_node[node] = rtpn;
5243
5244 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
5245 rtpz = &rtpn->rb_tree_per_zone[zone];
5246 rtpz->rb_root = RB_ROOT;
5247 spin_lock_init(&rtpz->lock);
5248 }
5249 }
5250 return 0;
Michal Hockoc3cecc62012-01-12 17:18:50 -08005251
5252err_cleanup:
Bob Liu3ed28fa2012-01-12 17:19:04 -08005253 for_each_node(node) {
Michal Hockoc3cecc62012-01-12 17:18:50 -08005254 if (!soft_limit_tree.rb_tree_per_node[node])
5255 break;
5256 kfree(soft_limit_tree.rb_tree_per_node[node]);
5257 soft_limit_tree.rb_tree_per_node[node] = NULL;
5258 }
5259 return 1;
5260
Balbir Singhf64c3f52009-09-23 15:56:37 -07005261}
5262
Li Zefan0eb253e2009-01-15 13:51:25 -08005263static struct cgroup_subsys_state * __ref
Tejun Heo92fb9742012-11-19 08:13:38 -08005264mem_cgroup_css_alloc(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005265{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005266 struct mem_cgroup *memcg, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005267 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005268 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005269
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005270 memcg = mem_cgroup_alloc();
5271 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005272 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08005273
Bob Liu3ed28fa2012-01-12 17:19:04 -08005274 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005275 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005276 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07005277
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005278 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005279 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08005280 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005281 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005282 parent = NULL;
Balbir Singhf64c3f52009-09-23 15:56:37 -07005283 if (mem_cgroup_soft_limit_tree_init())
5284 goto free_out;
Hillf Dantona41c58a2011-12-19 17:11:57 -08005285 root_mem_cgroup = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08005286 for_each_possible_cpu(cpu) {
5287 struct memcg_stock_pcp *stock =
5288 &per_cpu(memcg_stock, cpu);
5289 INIT_WORK(&stock->work, drain_local_stock);
5290 }
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07005291 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005292 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005293 parent = mem_cgroup_from_cont(cont->parent);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005294 memcg->use_hierarchy = parent->use_hierarchy;
5295 memcg->oom_kill_disable = parent->oom_kill_disable;
Balbir Singh18f59ea2009-01-07 18:08:07 -08005296 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005297
Balbir Singh18f59ea2009-01-07 18:08:07 -08005298 if (parent && parent->use_hierarchy) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005299 res_counter_init(&memcg->res, &parent->res);
5300 res_counter_init(&memcg->memsw, &parent->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005301 res_counter_init(&memcg->kmem, &parent->kmem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005302 /*
5303 * We increment refcnt of the parent to ensure that we can
5304 * safely access it on res_counter_charge/uncharge.
5305 * This refcnt will be decremented when freeing this
5306 * mem_cgroup(see mem_cgroup_put).
5307 */
5308 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005309 } else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005310 res_counter_init(&memcg->res, NULL);
5311 res_counter_init(&memcg->memsw, NULL);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005312 res_counter_init(&memcg->kmem, NULL);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07005313 /*
5314 * Deeper hierachy with use_hierarchy == false doesn't make
5315 * much sense so let cgroup subsystem know about this
5316 * unfortunate state in our controller.
5317 */
5318 if (parent && parent != root_mem_cgroup)
5319 mem_cgroup_subsys.broken_hierarchy = true;
Balbir Singh18f59ea2009-01-07 18:08:07 -08005320 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005321 memcg->last_scanned_node = MAX_NUMNODES;
5322 INIT_LIST_HEAD(&memcg->oom_notify);
Balbir Singh6d61ef42009-01-07 18:08:06 -08005323
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005324 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005325 memcg->swappiness = mem_cgroup_swappiness(parent);
5326 atomic_set(&memcg->refcnt, 1);
5327 memcg->move_charge_at_immigrate = 0;
5328 mutex_init(&memcg->thresholds_lock);
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07005329 spin_lock_init(&memcg->move_lock);
Glauber Costacbe128e32012-04-09 19:36:34 -03005330
5331 error = memcg_init_kmem(memcg, &mem_cgroup_subsys);
5332 if (error) {
5333 /*
5334 * We call put now because our (and parent's) refcnts
5335 * are already in place. mem_cgroup_put() will internally
5336 * call __mem_cgroup_free, so return directly
5337 */
5338 mem_cgroup_put(memcg);
5339 return ERR_PTR(error);
5340 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005341 return &memcg->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005342free_out:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005343 __mem_cgroup_free(memcg);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005344 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005345}
5346
Tejun Heo92fb9742012-11-19 08:13:38 -08005347static void mem_cgroup_css_offline(struct cgroup *cont)
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005348{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005349 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07005350
Michal Hockoab5196c2012-10-26 13:37:32 +02005351 mem_cgroup_reparent_charges(memcg);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005352}
5353
Tejun Heo92fb9742012-11-19 08:13:38 -08005354static void mem_cgroup_css_free(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005355{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005356 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08005357
Glauber Costa1d62e432012-04-09 19:36:33 -03005358 kmem_cgroup_destroy(memcg);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005359
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005360 mem_cgroup_put(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005361}
5362
Daisuke Nishimura02491442010-03-10 15:22:17 -08005363#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005364/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005365#define PRECHARGE_COUNT_AT_ONCE 256
5366static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005367{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005368 int ret = 0;
5369 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005370 struct mem_cgroup *memcg = mc.to;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005371
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005372 if (mem_cgroup_is_root(memcg)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005373 mc.precharge += count;
5374 /* we don't need css_get for root */
5375 return ret;
5376 }
5377 /* try to charge at once */
5378 if (count > 1) {
5379 struct res_counter *dummy;
5380 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005381 * "memcg" cannot be under rmdir() because we've already checked
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005382 * by cgroup_lock_live_cgroup() that it is not removed and we
5383 * are still under the same cgroup_mutex. So we can postpone
5384 * css_get().
5385 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005386 if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy))
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005387 goto one_by_one;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005388 if (do_swap_account && res_counter_charge(&memcg->memsw,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005389 PAGE_SIZE * count, &dummy)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005390 res_counter_uncharge(&memcg->res, PAGE_SIZE * count);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005391 goto one_by_one;
5392 }
5393 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005394 return ret;
5395 }
5396one_by_one:
5397 /* fall back to one by one charge */
5398 while (count--) {
5399 if (signal_pending(current)) {
5400 ret = -EINTR;
5401 break;
5402 }
5403 if (!batch_count--) {
5404 batch_count = PRECHARGE_COUNT_AT_ONCE;
5405 cond_resched();
5406 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005407 ret = __mem_cgroup_try_charge(NULL,
5408 GFP_KERNEL, 1, &memcg, false);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08005409 if (ret)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005410 /* mem_cgroup_clear_mc() will do uncharge later */
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08005411 return ret;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005412 mc.precharge++;
5413 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005414 return ret;
5415}
5416
5417/**
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005418 * get_mctgt_type - get target type of moving charge
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005419 * @vma: the vma the pte to be checked belongs
5420 * @addr: the address corresponding to the pte to be checked
5421 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08005422 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005423 *
5424 * Returns
5425 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
5426 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
5427 * move charge. if @target is not NULL, the page is stored in target->page
5428 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08005429 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
5430 * target for charge migration. if @target is not NULL, the entry is stored
5431 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005432 *
5433 * Called with pte lock held.
5434 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005435union mc_target {
5436 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005437 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005438};
5439
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005440enum mc_target_type {
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005441 MC_TARGET_NONE = 0,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005442 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08005443 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005444};
5445
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005446static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
5447 unsigned long addr, pte_t ptent)
5448{
5449 struct page *page = vm_normal_page(vma, addr, ptent);
5450
5451 if (!page || !page_mapped(page))
5452 return NULL;
5453 if (PageAnon(page)) {
5454 /* we don't move shared anon */
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005455 if (!move_anon())
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005456 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005457 } else if (!move_file())
5458 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005459 return NULL;
5460 if (!get_page_unless_zero(page))
5461 return NULL;
5462
5463 return page;
5464}
5465
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005466#ifdef CONFIG_SWAP
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005467static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5468 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5469{
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005470 struct page *page = NULL;
5471 swp_entry_t ent = pte_to_swp_entry(ptent);
5472
5473 if (!move_anon() || non_swap_entry(ent))
5474 return NULL;
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005475 /*
5476 * Because lookup_swap_cache() updates some statistics counter,
5477 * we call find_get_page() with swapper_space directly.
5478 */
5479 page = find_get_page(&swapper_space, ent.val);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005480 if (do_swap_account)
5481 entry->val = ent.val;
5482
5483 return page;
5484}
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005485#else
5486static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5487 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5488{
5489 return NULL;
5490}
5491#endif
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005492
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005493static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
5494 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5495{
5496 struct page *page = NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005497 struct address_space *mapping;
5498 pgoff_t pgoff;
5499
5500 if (!vma->vm_file) /* anonymous vma */
5501 return NULL;
5502 if (!move_file())
5503 return NULL;
5504
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005505 mapping = vma->vm_file->f_mapping;
5506 if (pte_none(ptent))
5507 pgoff = linear_page_index(vma, addr);
5508 else /* pte_file(ptent) is true */
5509 pgoff = pte_to_pgoff(ptent);
5510
5511 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005512 page = find_get_page(mapping, pgoff);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005513
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005514#ifdef CONFIG_SWAP
5515 /* shmem/tmpfs may report page out on swap: account for that too. */
5516 if (radix_tree_exceptional_entry(page)) {
5517 swp_entry_t swap = radix_to_swp_entry(page);
5518 if (do_swap_account)
5519 *entry = swap;
5520 page = find_get_page(&swapper_space, swap.val);
5521 }
5522#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005523 return page;
5524}
5525
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005526static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005527 unsigned long addr, pte_t ptent, union mc_target *target)
5528{
Daisuke Nishimura02491442010-03-10 15:22:17 -08005529 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005530 struct page_cgroup *pc;
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005531 enum mc_target_type ret = MC_TARGET_NONE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005532 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005533
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005534 if (pte_present(ptent))
5535 page = mc_handle_present_pte(vma, addr, ptent);
5536 else if (is_swap_pte(ptent))
5537 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005538 else if (pte_none(ptent) || pte_file(ptent))
5539 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005540
5541 if (!page && !ent.val)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005542 return ret;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005543 if (page) {
5544 pc = lookup_page_cgroup(page);
5545 /*
5546 * Do only loose check w/o page_cgroup lock.
5547 * mem_cgroup_move_account() checks the pc is valid or not under
5548 * the lock.
5549 */
5550 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
5551 ret = MC_TARGET_PAGE;
5552 if (target)
5553 target->page = page;
5554 }
5555 if (!ret || !target)
5556 put_page(page);
5557 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005558 /* There is a swap entry and a page doesn't exist or isn't charged */
5559 if (ent.val && !ret &&
Bob Liu9fb4b7c2012-01-12 17:18:48 -08005560 css_id(&mc.from->css) == lookup_swap_cgroup_id(ent)) {
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005561 ret = MC_TARGET_SWAP;
5562 if (target)
5563 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005564 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005565 return ret;
5566}
5567
Naoya Horiguchi12724852012-03-21 16:34:28 -07005568#ifdef CONFIG_TRANSPARENT_HUGEPAGE
5569/*
5570 * We don't consider swapping or file mapped pages because THP does not
5571 * support them for now.
5572 * Caller should make sure that pmd_trans_huge(pmd) is true.
5573 */
5574static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
5575 unsigned long addr, pmd_t pmd, union mc_target *target)
5576{
5577 struct page *page = NULL;
5578 struct page_cgroup *pc;
5579 enum mc_target_type ret = MC_TARGET_NONE;
5580
5581 page = pmd_page(pmd);
5582 VM_BUG_ON(!page || !PageHead(page));
5583 if (!move_anon())
5584 return ret;
5585 pc = lookup_page_cgroup(page);
5586 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
5587 ret = MC_TARGET_PAGE;
5588 if (target) {
5589 get_page(page);
5590 target->page = page;
5591 }
5592 }
5593 return ret;
5594}
5595#else
5596static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
5597 unsigned long addr, pmd_t pmd, union mc_target *target)
5598{
5599 return MC_TARGET_NONE;
5600}
5601#endif
5602
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005603static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
5604 unsigned long addr, unsigned long end,
5605 struct mm_walk *walk)
5606{
5607 struct vm_area_struct *vma = walk->private;
5608 pte_t *pte;
5609 spinlock_t *ptl;
5610
Naoya Horiguchi12724852012-03-21 16:34:28 -07005611 if (pmd_trans_huge_lock(pmd, vma) == 1) {
5612 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
5613 mc.precharge += HPAGE_PMD_NR;
5614 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07005615 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005616 }
Dave Hansen03319322011-03-22 16:32:56 -07005617
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07005618 if (pmd_trans_unstable(pmd))
5619 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005620 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5621 for (; addr != end; pte++, addr += PAGE_SIZE)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005622 if (get_mctgt_type(vma, addr, *pte, NULL))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005623 mc.precharge++; /* increment precharge temporarily */
5624 pte_unmap_unlock(pte - 1, ptl);
5625 cond_resched();
5626
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005627 return 0;
5628}
5629
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005630static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
5631{
5632 unsigned long precharge;
5633 struct vm_area_struct *vma;
5634
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005635 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005636 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5637 struct mm_walk mem_cgroup_count_precharge_walk = {
5638 .pmd_entry = mem_cgroup_count_precharge_pte_range,
5639 .mm = mm,
5640 .private = vma,
5641 };
5642 if (is_vm_hugetlb_page(vma))
5643 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005644 walk_page_range(vma->vm_start, vma->vm_end,
5645 &mem_cgroup_count_precharge_walk);
5646 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005647 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005648
5649 precharge = mc.precharge;
5650 mc.precharge = 0;
5651
5652 return precharge;
5653}
5654
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005655static int mem_cgroup_precharge_mc(struct mm_struct *mm)
5656{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005657 unsigned long precharge = mem_cgroup_count_precharge(mm);
5658
5659 VM_BUG_ON(mc.moving_task);
5660 mc.moving_task = current;
5661 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005662}
5663
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005664/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
5665static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005666{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005667 struct mem_cgroup *from = mc.from;
5668 struct mem_cgroup *to = mc.to;
5669
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005670 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005671 if (mc.precharge) {
5672 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
5673 mc.precharge = 0;
5674 }
5675 /*
5676 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
5677 * we must uncharge here.
5678 */
5679 if (mc.moved_charge) {
5680 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
5681 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005682 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005683 /* we must fixup refcnts and charges */
5684 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005685 /* uncharge swap account from the old cgroup */
5686 if (!mem_cgroup_is_root(mc.from))
5687 res_counter_uncharge(&mc.from->memsw,
5688 PAGE_SIZE * mc.moved_swap);
5689 __mem_cgroup_put(mc.from, mc.moved_swap);
5690
5691 if (!mem_cgroup_is_root(mc.to)) {
5692 /*
5693 * we charged both to->res and to->memsw, so we should
5694 * uncharge to->res.
5695 */
5696 res_counter_uncharge(&mc.to->res,
5697 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005698 }
5699 /* we've already done mem_cgroup_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005700 mc.moved_swap = 0;
5701 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005702 memcg_oom_recover(from);
5703 memcg_oom_recover(to);
5704 wake_up_all(&mc.waitq);
5705}
5706
5707static void mem_cgroup_clear_mc(void)
5708{
5709 struct mem_cgroup *from = mc.from;
5710
5711 /*
5712 * we must clear moving_task before waking up waiters at the end of
5713 * task migration.
5714 */
5715 mc.moving_task = NULL;
5716 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005717 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005718 mc.from = NULL;
5719 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005720 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005721 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005722}
5723
Li Zefan761b3ef52012-01-31 13:47:36 +08005724static int mem_cgroup_can_attach(struct cgroup *cgroup,
5725 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005726{
Tejun Heo2f7ee562011-12-12 18:12:21 -08005727 struct task_struct *p = cgroup_taskset_first(tset);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005728 int ret = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005729 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005730
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005731 if (memcg->move_charge_at_immigrate) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005732 struct mm_struct *mm;
5733 struct mem_cgroup *from = mem_cgroup_from_task(p);
5734
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005735 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005736
5737 mm = get_task_mm(p);
5738 if (!mm)
5739 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005740 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005741 if (mm->owner == p) {
5742 VM_BUG_ON(mc.from);
5743 VM_BUG_ON(mc.to);
5744 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005745 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005746 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005747 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005748 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005749 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005750 mc.to = memcg;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005751 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005752 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005753
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005754 ret = mem_cgroup_precharge_mc(mm);
5755 if (ret)
5756 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005757 }
5758 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005759 }
5760 return ret;
5761}
5762
Li Zefan761b3ef52012-01-31 13:47:36 +08005763static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
5764 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005765{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005766 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005767}
5768
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005769static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
5770 unsigned long addr, unsigned long end,
5771 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005772{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005773 int ret = 0;
5774 struct vm_area_struct *vma = walk->private;
5775 pte_t *pte;
5776 spinlock_t *ptl;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005777 enum mc_target_type target_type;
5778 union mc_target target;
5779 struct page *page;
5780 struct page_cgroup *pc;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005781
Naoya Horiguchi12724852012-03-21 16:34:28 -07005782 /*
5783 * We don't take compound_lock() here but no race with splitting thp
5784 * happens because:
5785 * - if pmd_trans_huge_lock() returns 1, the relevant thp is not
5786 * under splitting, which means there's no concurrent thp split,
5787 * - if another thread runs into split_huge_page() just after we
5788 * entered this if-block, the thread must wait for page table lock
5789 * to be unlocked in __split_huge_page_splitting(), where the main
5790 * part of thp split is not executed yet.
5791 */
5792 if (pmd_trans_huge_lock(pmd, vma) == 1) {
Hugh Dickins62ade862012-05-18 11:28:34 -07005793 if (mc.precharge < HPAGE_PMD_NR) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07005794 spin_unlock(&vma->vm_mm->page_table_lock);
5795 return 0;
5796 }
5797 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
5798 if (target_type == MC_TARGET_PAGE) {
5799 page = target.page;
5800 if (!isolate_lru_page(page)) {
5801 pc = lookup_page_cgroup(page);
5802 if (!mem_cgroup_move_account(page, HPAGE_PMD_NR,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07005803 pc, mc.from, mc.to)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07005804 mc.precharge -= HPAGE_PMD_NR;
5805 mc.moved_charge += HPAGE_PMD_NR;
5806 }
5807 putback_lru_page(page);
5808 }
5809 put_page(page);
5810 }
5811 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07005812 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005813 }
5814
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07005815 if (pmd_trans_unstable(pmd))
5816 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005817retry:
5818 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5819 for (; addr != end; addr += PAGE_SIZE) {
5820 pte_t ptent = *(pte++);
Daisuke Nishimura02491442010-03-10 15:22:17 -08005821 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005822
5823 if (!mc.precharge)
5824 break;
5825
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005826 switch (get_mctgt_type(vma, addr, ptent, &target)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005827 case MC_TARGET_PAGE:
5828 page = target.page;
5829 if (isolate_lru_page(page))
5830 goto put;
5831 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07005832 if (!mem_cgroup_move_account(page, 1, pc,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07005833 mc.from, mc.to)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005834 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005835 /* we uncharge from mc.from later. */
5836 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005837 }
5838 putback_lru_page(page);
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005839put: /* get_mctgt_type() gets the page */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005840 put_page(page);
5841 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005842 case MC_TARGET_SWAP:
5843 ent = target.ent;
Hugh Dickinse91cbb42012-05-29 15:06:51 -07005844 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08005845 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005846 /* we fixup refcnts and charges later. */
5847 mc.moved_swap++;
5848 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08005849 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005850 default:
5851 break;
5852 }
5853 }
5854 pte_unmap_unlock(pte - 1, ptl);
5855 cond_resched();
5856
5857 if (addr != end) {
5858 /*
5859 * We have consumed all precharges we got in can_attach().
5860 * We try charge one by one, but don't do any additional
5861 * charges to mc.to if we have failed in charge once in attach()
5862 * phase.
5863 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005864 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005865 if (!ret)
5866 goto retry;
5867 }
5868
5869 return ret;
5870}
5871
5872static void mem_cgroup_move_charge(struct mm_struct *mm)
5873{
5874 struct vm_area_struct *vma;
5875
5876 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005877retry:
5878 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
5879 /*
5880 * Someone who are holding the mmap_sem might be waiting in
5881 * waitq. So we cancel all extra charges, wake up all waiters,
5882 * and retry. Because we cancel precharges, we might not be able
5883 * to move enough charges, but moving charge is a best-effort
5884 * feature anyway, so it wouldn't be a big problem.
5885 */
5886 __mem_cgroup_clear_mc();
5887 cond_resched();
5888 goto retry;
5889 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005890 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5891 int ret;
5892 struct mm_walk mem_cgroup_move_charge_walk = {
5893 .pmd_entry = mem_cgroup_move_charge_pte_range,
5894 .mm = mm,
5895 .private = vma,
5896 };
5897 if (is_vm_hugetlb_page(vma))
5898 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005899 ret = walk_page_range(vma->vm_start, vma->vm_end,
5900 &mem_cgroup_move_charge_walk);
5901 if (ret)
5902 /*
5903 * means we have consumed all precharges and failed in
5904 * doing additional charge. Just abandon here.
5905 */
5906 break;
5907 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005908 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005909}
5910
Li Zefan761b3ef52012-01-31 13:47:36 +08005911static void mem_cgroup_move_task(struct cgroup *cont,
5912 struct cgroup_taskset *tset)
Balbir Singh67e465a2008-02-07 00:13:54 -08005913{
Tejun Heo2f7ee562011-12-12 18:12:21 -08005914 struct task_struct *p = cgroup_taskset_first(tset);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005915 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005916
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005917 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005918 if (mc.to)
5919 mem_cgroup_move_charge(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005920 mmput(mm);
5921 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005922 if (mc.to)
5923 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08005924}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005925#else /* !CONFIG_MMU */
Li Zefan761b3ef52012-01-31 13:47:36 +08005926static int mem_cgroup_can_attach(struct cgroup *cgroup,
5927 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005928{
5929 return 0;
5930}
Li Zefan761b3ef52012-01-31 13:47:36 +08005931static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
5932 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005933{
5934}
Li Zefan761b3ef52012-01-31 13:47:36 +08005935static void mem_cgroup_move_task(struct cgroup *cont,
5936 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005937{
5938}
5939#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08005940
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005941struct cgroup_subsys mem_cgroup_subsys = {
5942 .name = "memory",
5943 .subsys_id = mem_cgroup_subsys_id,
Tejun Heo92fb9742012-11-19 08:13:38 -08005944 .css_alloc = mem_cgroup_css_alloc,
5945 .css_offline = mem_cgroup_css_offline,
5946 .css_free = mem_cgroup_css_free,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005947 .can_attach = mem_cgroup_can_attach,
5948 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08005949 .attach = mem_cgroup_move_task,
Tejun Heo6bc10342012-04-01 12:09:55 -07005950 .base_cftypes = mem_cgroup_files,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005951 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005952 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005953};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005954
Andrew Mortonc255a452012-07-31 16:43:02 -07005955#ifdef CONFIG_MEMCG_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08005956static int __init enable_swap_account(char *s)
5957{
5958 /* consider enabled if no parameter or 1 is given */
Michal Hockoa2c89902011-05-24 17:12:50 -07005959 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08005960 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07005961 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08005962 really_do_swap_account = 0;
5963 return 1;
5964}
Michal Hockoa2c89902011-05-24 17:12:50 -07005965__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005966
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005967#endif